-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathUtil.cpp
More file actions
156 lines (135 loc) · 3.9 KB
/
Util.cpp
File metadata and controls
156 lines (135 loc) · 3.9 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#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();
}
namespace util {
template <class CHAR_T>
size_t getStrLen(const CHAR_T* inp, size_t bytesMax)
{
const size_t maxInp = bytesMax / sizeof(CHAR_T);
for (size_t i = 0; i < maxInp; i++) {
if (!PIN_CheckReadAccess(const_cast<CHAR_T*>(inp) + i)) {
return 0;
}
const CHAR_T 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::getAsciiLen(const char *inp, size_t maxInp)
{
return getStrLen<char>(inp, maxInp);
}
size_t util::getAsciiLenW(const wchar_t *inp, size_t maxInp)
{
return getStrLen<wchar_t>(inp, maxInp);
}
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);
}
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;
}
std::string util::getDirectory(const std::string& filepath)
{
size_t pos = filepath.find_last_of("/\\");
if (pos == std::string::npos || pos == filepath.length()) {
return filepath;
}
return filepath.substr(0, pos);
}
std::string util::getFilename(const std::string& filepath)
{
size_t pos = filepath.find_last_of("/\\");
if (pos == std::string::npos || pos == filepath.length()) {
return filepath;
}
return filepath.substr(pos + 1, filepath.length());
}
std::string util::makePath(const std::string& outDir, const std::string& module_name, const std::string& ext)
{
std::string filename = util::getFilename(module_name);
std::stringstream fnamestr;
if (!outDir.empty()) {
fnamestr << outDir << PATH_SEPARATOR;
}
fnamestr << filename << '.' << ext;
return fnamestr.str();
}