Repository navigation
Expand file tree
/
Copy pathFormatting.cpp
More file actions
568 lines (514 loc) · 20.2 KB
/
Copy pathFormatting.cpp
File metadata and controls
568 lines (514 loc) · 20.2 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <algorithm>
#include <cstdarg>
#include <cctype>
#include <cstring>
#include <string>
#include <cerrno>
#include <cstdlib>
#include <memory>
#include "prx/libc/include/General.hpp"
#include "SceTypes.hpp"
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/WindowsFormatting.hpp"
#ifdef _WIN32
#include "prx/libc/include/WindowsScanning.hpp"
#endif
#if defined(__GLIBC__)
extern "C" {
int __isoc99_vsscanf(const char*, const char*, std::va_list) noexcept;
int __isoc99_vfscanf(std::FILE*, const char*, std::va_list);
}
#endif
namespace {
#ifndef _WIN32
bool RequiresGuestFormatting(const char* format) {
if (format == nullptr) return false;
while ((format = std::strchr(format, '%')) != nullptr) {
++format;
if (*format == '%') { ++format; continue; }
while (*format && std::strchr("-+ #0'0123456789.*$", *format)) ++format;
if (*format == 'S' || *format == 'C' || *format == 'm' || *format == 'p' ||
(*format == 'l' && (format[1] == 's' || format[1] == 'c' || format[1] == 'p'))) return true;
if (*format) ++format;
}
return false;
}
void RejectGuestWideScan(const char* format) {
if (format == nullptr) return;
while ((format = std::strchr(format, '%')) != nullptr) {
++format;
if (*format == '%') { ++format; continue; }
while (*format && std::strchr("-+ #0'0123456789.*$", *format)) ++format;
if (*format == 'S' || *format == 'C' || (*format == 'l' && format[1] != '\0' && std::strchr("sc[", format[1])))
NotImplemented_nid_no_patch("scanf 16-bit wide conversion on Linux");
if (*format == '[') {
++format;
if (*format == '^') ++format;
if (*format == ']') ++format;
while (*format && *format != ']') ++format;
}
if (*format) ++format;
}
}
#endif
std::string HostLength(const std::string& length, char conversion) {
#ifdef _WIN32
if (length == "l" && std::strchr("diouxXn", conversion) != nullptr) return "ll";
#else
(void)conversion;
#endif
return length;
}
int HostVsscanf(const char* input, const char* format, std::va_list args) {
#if defined(__GLIBC__)
return __isoc99_vsscanf(input, format, args);
#else
return std::vsscanf(input, format, args);
#endif
}
int HostSscanf(const char* input, const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = HostVsscanf(input, format, args);
va_end(args);
return result;
}
#ifndef _WIN32
int HostVfscanf(std::FILE* stream, const char* format, std::va_list args) {
#if defined(__GLIBC__)
return __isoc99_vfscanf(stream, format, args);
#else
return std::vfscanf(stream, format, args);
#endif
}
#endif
template <typename NextPointer, typename NextCapacity>
int ScanGuest(const char* buffer, const char* format, bool secure, NextPointer nextPointer, NextCapacity nextCapacity) {
if (buffer == nullptr || format == nullptr) return EOF;
const char* input = buffer;
int assigned = 0;
bool converted = false;
const char* cursor = format;
const auto finish = [](int result) { return result; };
while (*cursor != '\0') {
if (std::isspace(static_cast<unsigned char>(*cursor))) {
while (std::isspace(static_cast<unsigned char>(*cursor))) ++cursor;
while (std::isspace(static_cast<unsigned char>(*input))) ++input;
continue;
}
if (*cursor != '%' || cursor[1] == '%') {
const char expected = *cursor == '%' ? '%' : *cursor;
cursor += *cursor == '%' ? 2 : 1;
if (expected == '%') while (std::isspace(static_cast<unsigned char>(*input))) ++input;
if (*input != expected) return finish(!converted && *input == '\0' ? EOF : assigned);
++input;
continue;
}
++cursor;
const bool suppress = *cursor == '*';
if (suppress) ++cursor;
unsigned long width = 0;
while (std::isdigit(static_cast<unsigned char>(*cursor))) width = width * 10 + static_cast<unsigned long>(*cursor++ - '0');
const char* lengthStart = cursor;
while (std::strchr("hljztL", *cursor) != nullptr && *cursor != '\0') ++cursor;
const std::string length(lengthStart, cursor);
const char conversion = *cursor;
if (conversion == '\0') return finish(assigned);
std::string specifier(1, conversion);
if (conversion == '[') {
const char* setStart = cursor++;
if (*cursor == '^') ++cursor;
if (*cursor == ']') ++cursor;
while (*cursor != '\0' && *cursor != ']') ++cursor;
if (*cursor != ']') return finish(assigned);
specifier.assign(setStart, cursor + 1);
}
++cursor;
if (conversion == 'n') {
if (!suppress) {
void* target = nextPointer();
if (target == nullptr) return EOF;
const auto count = input - buffer;
if (length == "hh") *static_cast<signed char*>(target) = static_cast<signed char>(count);
else if (length == "h") *static_cast<short*>(target) = static_cast<short>(count);
else if (length.empty()) *static_cast<int*>(target) = static_cast<int>(count);
else if (length == "l" || length == "ll" || length == "j" || length == "z" || length == "t") {
const std::int64_t value = count;
std::memcpy(target, &value, sizeof(value));
} else throw std::invalid_argument("Invalid scan count length");
}
continue;
}
if (conversion != 'c' && conversion != '[') {
const char* probe = input;
while (std::isspace(static_cast<unsigned char>(*probe))) ++probe;
if (*probe == '\0') return finish(converted ? assigned : EOF);
}
const bool sized = secure && (conversion == 's' || conversion == 'c' || conversion == '[');
bool capped = false;
std::string directive = "%";
void* target = nullptr;
if (suppress) directive += '*';
else {
target = nextPointer();
if (target == nullptr) return EOF;
}
if (sized && !suppress) {
const auto capacity = static_cast<unsigned long>(nextCapacity());
const unsigned long limit = conversion == 'c' ? capacity : (capacity == 0 ? 0 : capacity - 1);
const unsigned long wanted = conversion == 'c' ? (width == 0 ? 1 : width) : width;
if (limit == 0 || (conversion == 'c' && wanted > limit)) {
if (capacity != 0) static_cast<char*>(target)[0] = '\0';
return finish(assigned);
}
capped = wanted == 0 || wanted > limit;
width = wanted == 0 ? limit : std::min(wanted, limit);
}
if (width != 0) directive += std::to_string(width);
directive += HostLength(length, conversion);
directive += specifier;
directive += "%n";
int consumed = -1;
const int matched = suppress ? HostSscanf(input, directive.c_str(), &consumed) : HostSscanf(input, directive.c_str(), target, &consumed);
if (consumed < 0 || (!suppress && matched != 1)) return finish(!converted && *input == '\0' ? EOF : assigned);
if (capped && conversion != 'c') {
const char next = input[consumed];
bool overflow = false;
if (conversion == 's') overflow = next != '\0' && !std::isspace(static_cast<unsigned char>(next));
else if (next != '\0') {
char probe[2];
const std::string test = "%1" + specifier;
overflow = std::sscanf(input + consumed, test.c_str(), probe) == 1;
}
if (overflow) {
static_cast<char*>(target)[0] = '\0';
return finish(assigned);
}
}
input += consumed;
converted = true;
if (!suppress) ++assigned;
}
return finish(assigned);
}
}
extern "C" {
int APS5_VABI vasprintf_nid_postfix(char** destination, const char* format, VaList* args) {
if (!destination) { errno = 22; return -1; }
*destination = nullptr;
if (!format || !args) { errno = 22; return -1; }
try {
#ifdef _WIN32
std::string text;
const int count = LibcDetail::FormatWindows(nullptr, 0, format, args, &text);
const char* source = text.c_str();
#else
std::string guestText;
const bool guestFormatting = RequiresGuestFormatting(format);
char* text = nullptr;
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(args));
const int count = guestFormatting ? LibcDetail::FormatWindows(nullptr, 0, format, args, &guestText) : ::vasprintf(&text, format, copy);
va_end(copy);
std::unique_ptr<char, decltype(&std::free)> owner(text, std::free);
if (count < 0) return -1;
const char* source = guestFormatting ? guestText.c_str() : text;
#endif
auto* output = static_cast<char*>(ApplicationHeapAllocate_nid_no_patch(static_cast<size_t>(count) + 1));
if (!output) {
errno = 12;
return -1;
}
std::memcpy(output, source, static_cast<size_t>(count) + 1);
*destination = output;
return count;
} catch (const std::bad_alloc&) {
errno = 12;
return -1;
}
}
int APS5_VABI vfprintf_nid_postfix(FileStream* stream, const char* format, VaList* args) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
auto* native = GetNativeStream(stream);
#ifdef _WIN32
std::string buffer;
const int count = LibcDetail::FormatWindows(nullptr, 0, format, args, &buffer);
const int result = std::fwrite(buffer.data(), 1, static_cast<size_t>(count), native) ==
static_cast<size_t>(count) ? count : -1;
#else
if (RequiresGuestFormatting(format)) {
std::string buffer;
const int count = LibcDetail::FormatWindows(nullptr, 0, format, args, &buffer);
const int result = std::fwrite(buffer.data(), 1, static_cast<size_t>(count), native) == static_cast<size_t>(count) ? count : -1;
stream->SyncStatus();
return result;
}
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(args));
const int result = std::vfprintf(native, format, copy);
va_end(copy);
#endif
stream->SyncStatus();
return result;
}
int APS5_VABI fprintf_nid_postfix(FileStream* stream, const char* format, ...) {
#ifdef _WIN32
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
#else
std::va_list args;
va_start(args, format);
#endif
const int result = vfprintf_nid_postfix(stream, format, reinterpret_cast<VaList*>(args));
#ifdef _WIN32
__builtin_sysv_va_end(args);
#else
va_end(args);
#endif
return result;
}
int APS5_VABI fscanf_nid_postfix(FileStream* stream, const char* format, ...) {
if (stream && !stream->IsOpen()) { errno = 9; return -1; }
auto* native = GetNativeStream(stream);
#ifdef _WIN32
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::ScanFileWindows_nid_no_patch(native, format, args);
__builtin_sysv_va_end(args);
#else
RejectGuestWideScan(format);
std::va_list args;
va_start(args, format);
const int result = HostVfscanf(native, format, args);
va_end(args);
#endif
stream->SyncStatus();
return result;
}
#ifdef _WIN32
int APS5_VABI printf_nid_postfix(const char* format, ...) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::PrintWindows(format, args);
__builtin_sysv_va_end(args);
return result;
}
int APS5_VABI libc_printf_nid_postfix(const char* format, ...) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::PrintWindows(format, args);
__builtin_sysv_va_end(args);
return result;
}
int APS5_VABI snprintf_nid_postfix(char* buffer, size_t size, const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::FormatWindows(buffer, size, format, args);
__builtin_sysv_va_end(args);
return result;
}
int APS5_VABI sprintf_nid_postfix(char* buffer, const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::FormatWindows(buffer, SIZE_MAX, format, args);
__builtin_sysv_va_end(args);
return result;
}
#else
int APS5_VABI printf_nid_postfix(const char* format, ...) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::PrintWindows(format, args) : std::vprintf(format, args);
va_end(args);
return result;
}
int APS5_VABI libc_printf_nid_postfix(const char* format, ...) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::PrintWindows(format, args) : std::vprintf(format, args);
va_end(args);
return result;
}
int APS5_VABI snprintf_nid_postfix(char* buffer, size_t size, const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::FormatWindows(buffer, size, format, args) : std::vsnprintf(buffer, size, format, args);
va_end(args);
return result;
}
int APS5_VABI sprintf_nid_postfix(char* buffer, const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::FormatWindows(buffer, SIZE_MAX, format, args) : std::vsprintf(buffer, format, args);
va_end(args);
return result;
}
#endif
int APS5_VABI vsscanf_nid_postfix(const char* input, const char* format, VaList* args) {
#ifdef _WIN32
return LibcDetail::ScanWindows(input, format, args);
#else
RejectGuestWideScan(format);
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(args));
const int result = HostVsscanf(input, format, copy);
va_end(copy);
return result;
#endif
}
#ifdef _WIN32
int APS5_VABI sscanf_nid_postfix(const char* input, const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = vsscanf_nid_postfix(input, format, reinterpret_cast<VaList*>(args));
__builtin_sysv_va_end(args);
return result;
}
#else
int APS5_VABI sscanf_nid_postfix(const char* input, const char* format, ...) {
RejectGuestWideScan(format);
std::va_list args;
va_start(args, format);
const int result = HostVsscanf(input, format, args);
va_end(args);
return result;
}
#endif
#ifdef _WIN32
int APS5_VABI sscanf_s_nid_postfix(const char* buffer, const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = ScanGuest(buffer, format, true, [&] { return __builtin_va_arg(args, void*); }, [&] { return __builtin_va_arg(args, unsigned int); });
__builtin_sysv_va_end(args);
return result;
}
#else
int APS5_VABI sscanf_s_nid_postfix(const char* buffer, const char* format, ...) {
RejectGuestWideScan(format);
std::va_list args;
va_start(args, format);
const int result = ScanGuest(buffer, format, true, [&] { return va_arg(args, void*); }, [&] { return va_arg(args, unsigned int); });
va_end(args);
return result;
}
#endif
int APS5_VABI vprintf_nid_postfix(const char* str, VaList* c) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
#ifdef _WIN32
return LibcDetail::PrintWindows(str, c);
#else
if (RequiresGuestFormatting(str)) return LibcDetail::PrintWindows(str, c);
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(c));
const int result = std::vprintf(str, copy);
va_end(copy);
return result;
#endif
}
int APS5_VABI vsprintf_nid_postfix(char* str, const char* format, VaList* args) {
#ifdef _WIN32
return LibcDetail::FormatWindows(str, SIZE_MAX, format, args);
#else
if (RequiresGuestFormatting(format)) return LibcDetail::FormatWindows(str, SIZE_MAX, format, args);
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(args));
const int result = std::vsprintf(str, format, copy);
va_end(copy);
return result;
#endif
}
int APS5_VABI vsnprintf_nid_postfix(char* str, size_t size, const char* format, VaList* c) {
#ifdef _WIN32
return LibcDetail::FormatWindows(str, size, format, c);
#else
if (RequiresGuestFormatting(format)) return LibcDetail::FormatWindows(str, size, format, c);
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(c));
const int result = std::vsnprintf(str, size, format, copy);
va_end(copy);
return result;
#endif
}
static int FormatSecure(char* buffer, size_t size, const char* format, const void* args) {
constexpr size_t RsizeMax = SIZE_MAX >> 1;
int result = -1;
if (buffer != nullptr && format != nullptr && args != nullptr && size != 0 && size <= RsizeMax) {
try {
result = LibcDetail::FormatWindows(buffer, size, format, args, nullptr, true);
} catch (const LibcDetail::FormatConstraintViolation&) {
}
}
if (result < 0 && buffer != nullptr && size != 0 && size <= RsizeMax) buffer[0] = 0;
return result;
}
int APS5_VABI vsnprintf_s_nid_postfix(char* buffer, size_t size, const char* format, VaList* args) {
return FormatSecure(buffer, size, format, args);
}
#ifdef _WIN32
int APS5_VABI vsscanf_s_nid_postfix(const char* buffer, const char* format, VaList* args) {
LibcDetail::FormatArguments arguments(args);
return ScanGuest(buffer, format, true, [&] { return arguments.Next<void*>(); }, [&] { return arguments.Next<unsigned int>(); });
}
int APS5_VABI snprintf_s_nid_postfix(char* buffer, size_t size, const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = FormatSecure(buffer, size, format, args);
__builtin_sysv_va_end(args);
return result;
}
int APS5_VABI printf_s_nid_postfix(const char* format, ...) {
__builtin_sysv_va_list args;
__builtin_sysv_va_start(args, format);
const int result = LibcDetail::PrintWindows(format, args);
__builtin_sysv_va_end(args);
return result;
}
#else
int APS5_VABI vsscanf_s_nid_postfix(const char* buffer, const char* format, VaList* args) {
RejectGuestWideScan(format);
LibcDetail::FormatArguments arguments(args);
return ScanGuest(buffer, format, true, [&] { return arguments.Next<void*>(); }, [&] { return arguments.Next<unsigned int>(); });
}
int APS5_VABI vsprintf_s_nid_postfix(char* buffer, size_t size, const char* format, VaList* args) {
if (RequiresGuestFormatting(format)) return LibcDetail::FormatWindows(buffer, size, format, args);
std::va_list copy;
va_copy(copy, *reinterpret_cast<std::va_list*>(args));
const int result = std::vsnprintf(buffer, size, format, copy);
va_end(copy);
return result;
}
int APS5_VABI sprintf_s_nid_postfix(char* buffer, size_t size, const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::FormatWindows(buffer, size, format, args) : std::vsnprintf(buffer, size, format, args);
va_end(args);
return result;
}
int APS5_VABI snprintf_s_nid_postfix(char* buffer, size_t size, const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = FormatSecure(buffer, size, format, args);
va_end(args);
return result;
}
int APS5_VABI printf_s_nid_postfix(const char* format, ...) {
std::va_list args;
va_start(args, format);
const int result = RequiresGuestFormatting(format) ? LibcDetail::PrintWindows(format, args) : std::vprintf(format, args);
va_end(args);
return result;
}
#endif
int APS5_VABI puts_nid_postfix(const char* s) {
if (!_Stdout_nid_postfix.IsOpen()) { errno = 9; return -1; }
return std::puts(s);
}
}