forked from hasherezade/tiny_tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cpp
138 lines (119 loc) · 3.27 KB
/
Util.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "Util.h"
#include <algorithm>
#include <sstream>
#include <iomanip>
#include "pin.H"
std::wstring util::hexdump(const uint8_t* in_buf, const size_t max_size)
{
std::wstringstream ss;
for (size_t i = 0; i < max_size; i++) {
if (!PIN_CheckReadAccess(const_cast<uint8_t*>(in_buf) + i)) {
break;
}
if (IS_PRINTABLE(in_buf[i])) {
ss << char(in_buf[i]);
}
else {
ss << "\\x" << std::setfill(L'0') << std::setw(2) << std::hex << (unsigned int)in_buf[i];
}
}
return ss.str();
}
size_t util::getAsciiLen(const char *inp, size_t maxInp)
{
size_t i = 0;
for (; i < maxInp; i++) {
if (!PIN_CheckReadAccess(const_cast<char*>(inp) + i)) {
return 0;
}
const char c = inp[i];
if (c == '\0') return i; //end of string
if (!IS_PRINTABLE(c) && !IS_ENDLINE(c)) return 0;
}
return 0;
}
size_t util::getAsciiLenW(const wchar_t *inp, size_t maxInp)
{
size_t i = 0;
for (; i < maxInp; i++) {
if (!PIN_CheckReadAccess(const_cast<wchar_t*>(inp) + i)) {
return 0;
}
const wchar_t w = inp[i];
if (w == 0) return i; //end of string
if (!IS_PRINTABLE(w) && !IS_ENDLINE(w)) return 0;
}
return 0;
}
std::string util::getDllName(const std::string& str)
{
std::size_t len = str.length();
if (!len) return str;
std::size_t found = str.find_last_of("/\\");
std::size_t ext = str.find_last_of(".");
if (ext > len) ext = len;
std::string name = str.substr(found + 1, ext - (found + 1));
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c){ return std::tolower(c); });
return name;
}
bool util::iequals(const std::string& a, const std::string& b)
{
size_t aLen = a.size();
if (b.size() != aLen) return false;
for (size_t i = 0; i < aLen; ++i) {
if (tolower(a[i]) != tolower(b[i])) return false;
}
return true;
}
size_t util::splitList(const std::string &sline, const char delimiter, std::vector<std::string> &args)
{
std::istringstream f(sline);
std::string s;
while (getline(f, s, delimiter)) {
args.push_back(s);
}
return args.size();
}
static inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
static inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
void util::trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
int util::loadInt(const std::string &str, bool as_hex)
{
int intVal = 0;
std::stringstream ss;
ss << (as_hex ? std::hex : std::dec) << str;
ss >> intVal;
return intVal;
}
std::string util::stripQuotes(const std::string& str)
{
std::string s = str;
s.erase(std::remove(s.begin(), s.end(), '\"'), s.end());
return s;
}
bool util::isStrEqualI(const std::string &str1, const std::string &str2)
{
if (str1.length() != str2.length()) {
return false;
}
for (size_t i = 0; i < str1.length(); i++) {
if (tolower(str1[i]) != tolower(str2[i])) {
return false;
}
}
return true;
}