-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringtool.cpp
More file actions
467 lines (404 loc) · 12.1 KB
/
Copy pathstringtool.cpp
File metadata and controls
467 lines (404 loc) · 12.1 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// stringtool.cpp
#include "stringtool.h"
#include <vector>
#include <locale>
#include <malloc.h>
#include <mbstring.h>
/* ************************************************************************** *
STRLCPY(3) OpenBSD Programmer's Manual STRLCPY(3)
NAME
strlcpy, strlcat - size-bounded string copying and concatenation
SYNOPSIS
#include <string.h>
size_t
strlcpy(char *dst, const char *src, size_t size);
size_t
strlcat(char *dst, const char *src, size_t size);
DESCRIPTION
The strlcpy() and strlcat() functions copy and concatenate strings re-
spectively. They are designed to be safer, more consistent, and less er-
ror prone replacements for strncpy(3) and strncat(3). Unlike those func-
tions, strlcpy() and strlcat() take the full size of the buffer (not just
the length) and guarantee to NUL-terminate the result (as long as size is
larger than 0). Note that you should include a byte for the NUL in size.
The strlcpy() function copies up to size - 1 characters from the NUL-ter-
minated string src to dst, NUL-terminating the result.
The strlcat() function appends the NUL-terminated string src to the end
of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
nating the result.
RETURN VALUES
The strlcpy() and strlcat() functions return the total length of the
string they tried to create. For strlcpy() that means the length of src.
For strlcat() that means the initial length of dst plus the length of
src. While this may seem somewhat confusing it was done to make trunca-
tion detection simple.
EXAMPLES
The following code fragment illustrates the simple case:
char *s, *p, buf[BUFSIZ];
...
(void)strlcpy(buf, s, sizeof(buf));
(void)strlcat(buf, p, sizeof(buf));
To detect truncation, perhaps while building a pathname, something like
the following might be used:
char *dir, *file, pname[MAXPATHNAMELEN];
...
if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
goto toolong;
if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
goto toolong;
Since we know how many characters we copied the first time, we can speed
things up a bit by using a copy instead on an append:
char *dir, *file, pname[MAXPATHNAMELEN];
size_t n;
...
n = strlcpy(pname, dir, sizeof(pname));
if (n >= sizeof(pname))
goto toolong;
if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname)-n)
goto toolong;
However, one may question the validity of such optimizations, as they de-
feat the whole purpose of strlcpy() and strlcat(). As a matter of fact,
the first version of this manual page got it wrong.
SEE ALSO
snprintf(3), strncat(3), strncpy(3)
OpenBSD 2.6 June 22, 1998 2
-------------------------------------------------------------------------------
Source: OpenBSD 2.6 man pages. Copyright: Portions are copyrighted by BERKELEY
SOFTWARE DESIGN, INC., The Regents of the University of California,
Massachusetts Institute of Technology, Free Software Foundation, FreeBSD Inc.,
and others.
* ************************************************************************** */
// copy
template <class T>
static inline size_t xstrlcpy(T *o_dest, const T *i_src, size_t i_destSize)
{
T *d = o_dest;
const T *s = i_src;
size_t n = i_destSize;
ASSERT( o_dest != NULL );
ASSERT( i_src != NULL );
// Copy as many bytes as will fit
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
// Not enough room in o_dest, add NUL and traverse rest of i_src
if (n == 0) {
if (i_destSize != 0)
*d = T(); // NUL-terminate o_dest
while (*s++)
;
}
return (s - i_src - 1); // count does not include NUL
}
// copy
size_t wcslcpy(wchar_t *o_dest, const wchar_t *i_src, size_t i_destSize)
{
return xstrlcpy(o_dest, i_src, i_destSize);
}
/// stream output
std::wostream &operator<<(std::wostream &i_ost, const wstringq &i_data)
{
i_ost << L"\"";
for (const wchar_t *s = i_data.c_str(); *s; ++ s) {
switch (*s) {
case L'\a':
i_ost << L"\\a";
break;
case L'\f':
i_ost << L"\\f";
break;
case L'\n':
i_ost << L"\\n";
break;
case L'\r':
i_ost << L"\\r";
break;
case L'\t':
i_ost << L"\\t";
break;
case L'\v':
i_ost << L"\\v";
break;
case L'"':
i_ost << L"\\\"";
break;
default:
if (iswprint(*s)) {
wchar_t buf[2] = { *s, 0 };
i_ost << buf;
} else {
i_ost << L"\\x";
wchar_t buf[5];
_snwprintf(buf, NUMBER_OF(buf), L"%04x", *s);
i_ost << buf;
}
break;
}
}
i_ost << L"\"";
return i_ost;
}
// interpret meta characters such as \n
std::wstring interpretMetaCharacters(const wchar_t *i_str, size_t i_len,
const wchar_t *i_quote,
bool i_doesUseRegexpBackReference)
{
// interpreted string is always less than i_len
std::vector<wchar_t> result(i_len + 1);
// destination
wchar_t *d = result.data();
// end pointer
const wchar_t *end = i_str + i_len;
while (i_str < end && *i_str) {
if (*i_str != L'\\') {
*d++ = *i_str++;
} else if (*(i_str + 1) != L'\0') {
i_str ++;
if (i_quote && wcschr(i_quote, *i_str))
*d++ = *i_str++;
else
switch (*i_str) {
case L'a':
*d++ = L'\x07';
i_str ++;
break;
//case L'b': *d++ = L'\b'; i_str ++; break;
case L'e':
*d++ = L'\x1b';
i_str ++;
break;
case L'f':
*d++ = L'\f';
i_str ++;
break;
case L'n':
*d++ = L'\n';
i_str ++;
break;
case L'r':
*d++ = L'\r';
i_str ++;
break;
case L't':
*d++ = L'\t';
i_str ++;
break;
case L'v':
*d++ = L'\v';
i_str ++;
break;
//case L'?': *d++ = L'\x7f'; i_str ++; break;
//case L'_': *d++ = L' '; i_str ++; break;
//case L'\\': *d++ = L'\\'; i_str ++; break;
case L'\'':
*d++ = L'\'';
i_str ++;
break;
case L'"':
*d++ = L'"';
i_str ++;
break;
case L'\\':
*d++ = L'\\';
i_str ++;
break;
case L'c': // control code, for example '\c[' is escape: '\x1b'
i_str ++;
if (i_str < end && *i_str) {
static const wchar_t *ctrlchar =
L"@ABCDEFGHIJKLMNO"
L"PQRSTUVWXYZ[\\]^_"
L"@abcdefghijklmno"
L"pqrstuvwxyz@@@@?";
static const wchar_t *ctrlcode =
L"\00\01\02\03\04\05\06\07\10\11\12\13\14\15\16\17"
L"\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37"
L"\00\01\02\03\04\05\06\07\10\11\12\13\14\15\16\17"
L"\20\21\22\23\24\25\26\27\30\31\32\00\00\00\00\177";
if (const wchar_t *c = wcschr(ctrlchar, *i_str))
*d++ = ctrlcode[c - ctrlchar], i_str ++;
}
break;
case L'x':
case L'X': {
i_str ++;
static const wchar_t *hexchar = L"0123456789ABCDEFabcdef";
static int hexvalue[] = { 0, 1, 2, 3, 4, 5 ,6, 7, 8, 9,
10, 11, 12, 13, 14, 15,
10, 11, 12, 13, 14, 15,
};
bool brace = false;
if (i_str < end && *i_str == L'{') {
i_str ++;
brace = true;
}
int n = 0;
for (; i_str < end && *i_str; i_str ++)
if (const wchar_t *c = wcschr(hexchar, *i_str))
n = n * 16 + hexvalue[c - hexchar];
else
break;
if (i_str < end && *i_str == L'}' && brace)
i_str ++;
if (0 < n)
*d++ = static_cast<wchar_t>(n);
break;
}
case L'1':
case L'2':
case L'3':
case L'4':
case L'5':
case L'6':
case L'7':
if (i_doesUseRegexpBackReference)
goto case_default;
// fall through
case L'0': {
static const wchar_t *octalchar = L"01234567";
static int octalvalue[] = { 0, 1, 2, 3, 4, 5 ,6, 7, };
int n = 0;
for (; i_str < end && *i_str; i_str ++)
if (const wchar_t *c = wcschr(octalchar, *i_str))
n = n * 8 + octalvalue[c - octalchar];
else
break;
if (0 < n)
*d++ = static_cast<wchar_t>(n);
break;
}
default:
case_default:
*d++ = L'\\';
*d++ = *i_str++;
break;
}
}
}
*d =L'\0';
return result.data();
}
// add session id to i_str
std::wstring addSessionId(const wchar_t *i_str)
{
DWORD sessionId;
std::wstring s(i_str);
if (ProcessIdToSessionId(GetCurrentProcessId(), &sessionId)) {
wchar_t buf[20];
_snwprintf(buf, NUMBER_OF(buf), L"%u", sessionId);
s += buf;
}
return s;
}
// converter
std::wstring to_wstring(const std::string &i_str)
{
size_t size = mbstowcs(NULL, i_str.c_str(), i_str.size() + 1);
if (size == (size_t)-1)
return std::wstring();
std::vector<wchar_t> result(size + 1);
mbstowcs(result.data(), i_str.c_str(), i_str.size() + 1);
return std::wstring(result.data());
}
// converter
std::string to_string(const std::wstring &i_str)
{
size_t size = wcstombs(NULL, i_str.c_str(), i_str.size() + 1);
if (size == (size_t)-1)
return std::string();
std::vector<char> result(size + 1);
wcstombs(result.data(), i_str.c_str(), i_str.size() + 1);
return std::string(result.data());
}
/// stream output
std::wostream &operator<<(std::wostream &i_ost, const wregex_stored &i_data)
{
return i_ost << i_data.str();
}
/// get lower string
std::wstring toLower(const std::wstring &i_str)
{
std::wstring str(i_str);
for (size_t i = 0; i < str.size(); ++ i) {
wchar_t c = str[i];
if (c <= 0x007f)
str[i] = static_cast<wchar_t>(tolower(c)); // ASCII only
}
return str;
}
// spell out control characters; see the header for why this spelling
std::wstring escapeControlChars(const std::wstring &i_str)
{
static const wchar_t hex[] = L"0123456789ABCDEF";
std::wstring o;
o.reserve(i_str.size());
for (size_t i = 0; i < i_str.size(); ++ i) {
wchar_t c = i_str[i];
if (!iswcntrl(c)) {
o += c;
continue;
}
switch (c) {
case L'\t':
o += L"<TAB>";
break;
case L'\n':
o += L"<LF>";
break;
case L'\r':
o += L"<CR>";
break;
default: {
unsigned u = static_cast<unsigned>(c);
o += L'<';
if (0xFF < u) {
o += L"U+";
o += hex[(u >> 12) & 0xF];
o += hex[(u >> 8) & 0xF];
}
o += hex[(u >> 4) & 0xF];
o += hex[u & 0xF];
o += L'>';
break;
}
}
}
return o;
}
// convert wstring to UTF-8
std::string to_UTF8(const std::wstring &i_str)
{
if (i_str.empty()) return std::string();
// Upper bound: a BMP wchar_t produces at most 3 UTF-8 bytes (3*1=3).
// A surrogate pair uses 2 wchar_t to produce 4 UTF-8 bytes (4 < 3*2=6).
// So size()*3 is always a sufficient upper bound.
std::string s(i_str.size() * 3, '\0');
int len = WideCharToMultiByte(CP_UTF8, 0,
i_str.c_str(), static_cast<int>(i_str.size()),
&s[0], static_cast<int>(s.size()),
NULL, NULL);
if (len <= 0) return std::string();
s.resize(len);
s.shrink_to_fit();
return s;
}
// convert UTF-8 encoded string to wstring
std::wstring from_UTF8(const std::string &i_str)
{
if (i_str.empty()) return std::wstring();
// Upper bound: UTF-8 byte count >= wchar_t count
// (ASCII 1 byte -> 1 wchar_t is the worst case;
// 4-byte sequences -> 2 wchar_t, so bytes always outnumber wchar_t units)
std::wstring ws(i_str.size(), L'\0');
int len = MultiByteToWideChar(CP_UTF8, 0,
i_str.c_str(), static_cast<int>(i_str.size()),
&ws[0], static_cast<int>(ws.size()));
if (len <= 0) return std::wstring();
ws.resize(len);
ws.shrink_to_fit();
return ws;
}