-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmacro_replacer.cpp
More file actions
96 lines (79 loc) · 2.21 KB
/
Copy pathmacro_replacer.cpp
File metadata and controls
96 lines (79 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2019-2026 Elias Bachaalany
// SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
//
// This file is licensed under the Human-Origin Source License v1.0.
// See LICENSE.
/*
Macro replacement engine implementation. PURE (no IDA SDK) so it can be unit
tested headlessly with an injected evaluator. The regex-replace-with-callback
helper is reused from libidacpp::text.
*/
#include "macro_replacer.h"
#include <cctype>
#include <libidacpp/text/text.hpp>
using libidacpp::text::regex_replace_cb;
//-------------------------------------------------------------------------
// Macro Replacer Implementation
//-------------------------------------------------------------------------
std::regex macro_replacer_t::RE_EVAL = std::regex(R"(\$\{(.+?)\}\$)");
macro_replacer_t::macro_replacer_t(repl_func_t repl_func)
: m_repl_func(repl_func)
{
}
std::string macro_replacer_t::operator()(const char *text)
{
return operator()(std::string(text));
}
std::string macro_replacer_t::operator()(std::string text)
{
if (!replace_map.empty())
text = regex_replace_cb(text, re_replace, [this](auto &m) { return replace_map[m.str(0)]; });
return regex_replace_cb(text, RE_EVAL, [this](auto &m) { return m_repl_func(m.str(1)); });
}
// Similar to Python's "re.escape()"
std::string macro_replacer_t::escape_re(const std::string re_text)
{
std::string out;
out.reserve(re_text.size() * 2);
for (auto ch : re_text)
{
if (isalnum(ch))
{
out += ch;
continue;
}
else if (ch == 0)
{
out += "\\x0";
}
else
{
out += '\\';
out += ch;
}
}
return out;
}
void macro_replacer_t::begin_update()
{
replace_map.clear();
}
void macro_replacer_t::update(std::string macro, std::string expr)
{
replace_map[macro] = expr;
}
void macro_replacer_t::end_update()
{
if (replace_map.empty())
return;
std::string re_str;
for (auto &kv : replace_map)
{
re_str.append(escape_re(kv.first));
re_str.append("|");
}
// Get rid of the trailing '|'
re_str.pop_back();
// Form the single regular expression
re_replace = re_str;
}