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
|
#include "utils.h"
namespace Utils
{
//----------------------------------------------------------------------------------------
// Purpose: For converting a string pattern to an array of bytes. Doesnt support wildcards
//----------------------------------------------------------------------------------------
std::vector<uint8_t> StringPatternToBytes(const char* szInput)
{
const char* pszPatternStart = const_cast<char*>(szInput);
const char* pszPatternEnd = pszPatternStart + strlen(szInput);
std::vector<uint8_t> vBytes;
for (const char* pszCurrentByte = pszPatternStart; pszCurrentByte < pszPatternEnd; ++pszCurrentByte)
{
vBytes.push_back(strtoul(pszCurrentByte, const_cast<char**>(&pszCurrentByte), 16));
}
return vBytes;
};
//----------------------------------------------------------------------------------------
// Purpose: For converting a string pattern with wildcards to an array of bytes.
//----------------------------------------------------------------------------------------
std::vector<int> PatternToBytes(const char* szInput)
{
const char* pszPatternStart = const_cast<char*>(szInput);
const char* pszPatternEnd = pszPatternStart + strlen(szInput);
std::vector<int> vBytes;
for (const char* pszCurrentByte = pszPatternStart; pszCurrentByte < pszPatternEnd; ++pszCurrentByte)
{
if (*pszCurrentByte == '?')
{
++pszCurrentByte;
if (*pszCurrentByte == '?')
{
++pszCurrentByte; // Skip double wildcard.
}
vBytes.push_back(-1); // Push the byte back as invalid.
}
else
{
vBytes.push_back(strtoul(pszCurrentByte, const_cast<char**>(&pszCurrentByte), 16));
}
}
return vBytes;
};
//----------------------------------------------------------------------------------------
// Purpose: For converting a string pattern with wildcards to an array of bytes and mask.
//----------------------------------------------------------------------------------------
std::pair<std::vector<uint8_t>, std::string> PatternToMaskedBytes(const char* szInput)
{
const char* pszPatternStart = const_cast<char*>(szInput);
const char* pszPatternEnd = pszPatternStart + strlen(szInput);
std::vector<uint8_t> vBytes;
std::string svMask;
for (const char* pszCurrentByte = pszPatternStart; pszCurrentByte < pszPatternEnd; ++pszCurrentByte)
{
if (*pszCurrentByte == '?')
{
++pszCurrentByte;
if (*pszCurrentByte == '?')
{
++pszCurrentByte; // Skip double wildcard.
}
vBytes.push_back(0); // Push the byte back as invalid.
svMask += '?';
}
else
{
vBytes.push_back(uint8_t(strtoul(pszCurrentByte, const_cast<char**>(&pszCurrentByte), 16)));
svMask += 'x';
}
}
return make_pair(vBytes, svMask);
};
//----------------------------------------------------------------------------------------
// Purpose: For converting a string to an array of bytes.
//----------------------------------------------------------------------------------------
std::vector<int> StringToBytes(const char* szInput, bool bNullTerminator)
{
const char* pszStringStart = const_cast<char*>(szInput);
const char* pszStringEnd = pszStringStart + strlen(szInput);
std::vector<int> vBytes;
for (const char* pszCurrentByte = pszStringStart; pszCurrentByte < pszStringEnd; ++pszCurrentByte)
{
// Dereference character and push back the byte.
vBytes.push_back(*pszCurrentByte);
}
if (bNullTerminator)
{
vBytes.push_back('\0');
}
return vBytes;
};
//----------------------------------------------------------------------------------------
// Purpose: For converting a string to an array of masked bytes.
//----------------------------------------------------------------------------------------
std::pair<std::vector<uint8_t>, std::string> StringToMaskedBytes(const char* szInput, bool bNullTerminator)
{
const char* pszStringStart = const_cast<char*>(szInput);
const char* pszStringEnd = pszStringStart + strlen(szInput);
std::vector<uint8_t> vBytes;
std::string svMask;
for (const char* pszCurrentByte = pszStringStart; pszCurrentByte < pszStringEnd; ++pszCurrentByte)
{
// Dereference character and push back the byte.
vBytes.push_back(*pszCurrentByte);
svMask += 'x';
}
if (bNullTerminator)
{
vBytes.push_back(0x0);
svMask += 'x';
}
return make_pair(vBytes, svMask);
};
}
|