-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathEscapeUtils.cpp
254 lines (213 loc) · 8.03 KB
/
EscapeUtils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// sktoolslib - common files for SK tools
// Copyright (C) 2013, 2017, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "EscapeUtils.h"
#include "StringUtils.h"
#include "UnicodeUtils.h"
#if defined(_M_IX86) || defined(_M_X64)
# include <emmintrin.h>
#endif
static BOOL sse2Supported = ::IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVA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*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
/* 128 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 192 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
bool CEscapeUtils::ContainsEscapedChars(const char* psz, size_t length)
{
// most of our strings will be tens of bytes long
// -> afford some minor overhead to handle the main part very fast
const char* end = psz + length;
#if defined(_M_IX86) || defined(_M_X64)
if (sse2Supported)
{
__m128i mask = _mm_set_epi8('%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%', '%');
for (; psz + sizeof(mask) <= end; psz += sizeof(mask))
{
// fetch the next 16 bytes from the source
__m128i chunk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(psz));
// check for non-ASCII
int flags = _mm_movemask_epi8(_mm_cmpeq_epi8(chunk, mask));
if (flags != 0)
return true;
};
}
#endif
// return odd bytes at the end of the string
for (; psz < end; ++psz)
if (*psz == '%')
return true;
return false;
}
char* CEscapeUtils::Unescape(char* psz)
{
char* pszSource = psz;
char* pszDest = psz;
static constexpr char szHex[] = "0123456789ABCDEF";
// Unescape special characters. The number of characters
// in the *pszDest is assumed to be <= the number of characters
// in pszSource (they are both the same string anyway)
while (*pszSource != '\0' && *pszDest != '\0')
{
if (*pszSource == '%')
{
// The next two chars following '%' should be digits
if (*(pszSource + 1) == '\0' ||
*(pszSource + 2) == '\0')
{
// nothing left to do
break;
}
char nValue = '?';
const char* pszHigh = nullptr;
pszSource++;
*pszSource = static_cast<char>(toupper(*pszSource));
pszHigh = strchr(szHex, *pszSource);
if (pszHigh != nullptr)
{
pszSource++;
*pszSource = static_cast<char>(toupper(*pszSource));
const char* pszLow = strchr(szHex, *pszSource);
if (pszLow != nullptr)
{
nValue = static_cast<char>(((pszHigh - szHex) << 4) +
(pszLow - szHex));
}
}
else
{
pszSource--;
nValue = *pszSource;
}
*pszDest++ = nValue;
}
else
*pszDest++ = *pszSource;
pszSource++;
}
*pszDest = '\0';
return pszDest;
}
std::string CEscapeUtils::EscapeString(const std::string& str)
{
std::string ret2;
int c;
int i;
for (i = 0; str[i]; ++i)
{
c = static_cast<unsigned char>(str[i]);
if (iri_escape_chars[c])
{
// no escaping needed for that char
ret2 += static_cast<unsigned char>(str[i]);
}
else
{
// char needs escaping
ret2 += CStringUtils::Format("%%%02X", static_cast<unsigned char>(c));
}
}
std::string ret;
for (i = 0; ret2[i]; ++i)
{
c = static_cast<unsigned char>(ret2[i]);
if (uri_autoescape_chars[c])
{
if ((c == '%') && (DoesPercentNeedEscaping(ret2.substr(i).c_str())))
{
// this percent sign needs escaping!
ret += CStringUtils::Format("%%%02X", static_cast<unsigned char>(c));
}
else
{
// no escaping needed for that char
ret += static_cast<unsigned char>(ret2[i]);
}
}
else
{
// char needs escaping
ret += CStringUtils::Format("%%%02X", static_cast<unsigned char>(c));
}
}
return ret;
}
std::wstring CEscapeUtils::EscapeString(const std::wstring& str)
{
return CUnicodeUtils::StdGetUnicode(EscapeString(CUnicodeUtils::StdGetUTF8(str)));
}
std::string CEscapeUtils::StringUnescape(const std::string& str)
{
auto urlabuf = std::make_unique<char[]>(str.size() + 1);
strcpy_s(urlabuf.get(), str.size() + 1, str.c_str());
Unescape(urlabuf.get());
return urlabuf.get();
}
std::wstring CEscapeUtils::StringUnescape(const std::wstring& str)
{
auto len = str.size();
if (len == 0)
return std::wstring();
std::string stra = CUnicodeUtils::StdGetUTF8(str);
stra = StringUnescape(stra);
return CUnicodeUtils::StdGetUnicode(stra);
}
bool CEscapeUtils::DoesPercentNeedEscaping(LPCSTR str)
{
if (str[1] == 0)
return true;
if (!(((str[1] >= '0') && (str[1] <= '9')) || ((str[1] >= 'A') && (str[1] <= 'F')) || ((str[1] >= 'a') && (str[1] <= 'f'))))
return true;
if (!(((str[2] >= '0') && (str[2] <= '9')) || ((str[2] >= 'A') && (str[2] <= 'F')) || ((str[2] >= 'a') && (str[2] <= 'f'))))
return true;
return false;
}