forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.cpp
More file actions
1812 lines (1601 loc) · 47.5 KB
/
printf.cpp
File metadata and controls
1812 lines (1601 loc) · 47.5 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
printf.c
Abstract:
Implementation of the printf family functions.
Revision History:
--*/
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/cruntime.h"
#include "pal/thread.hpp"
#include "pal/threadsusp.hpp"
#include "pal/printfcpp.hpp"
/* <stdarg.h> needs to be included after "palinternal.h" to avoid name
collision for va_start and va_end */
#include <stdarg.h>
#include <errno.h>
SET_DEFAULT_DEBUG_CHANNEL(CRT);
#if SSCANF_SUPPORT_ll
const static char *scanf_longlongfmt = "ll";
#else
const static char *scanf_longlongfmt = "q";
#endif
#if SSCANF_CANNOT_HANDLE_MISSING_EXPONENT
static int SscanfFloatCheckExponent(LPCSTR buff, LPCSTR floatFmt,
void * voidPtr, int * pn);
#endif // SSCANF_CANNOT_HANDLE_MISSING_EXPONENT
/*******************************************************************************
Function:
Internal_AddPaddingA
Parameters:
Out
- buffer to place padding and given string (In)
Count
- maximum chars to be copied so as not to overrun given buffer
In
- string to place into (Out) accompanied with padding
Padding
- number of padding chars to add
Flags
- padding style flags (PRINTF_FORMAT_FLAGS)
*******************************************************************************/
__attribute__((no_instrument_function))
BOOL Internal_AddPaddingA(LPSTR *Out, INT Count, LPSTR In,
INT Padding, INT Flags)
{
LPSTR OutOriginal = *Out;
INT PaddingOriginal = Padding;
INT LengthInStr;
LengthInStr = strlen(In);
if (Padding < 0)
{
/* this is used at the bottom to determine if the buffer ran out */
PaddingOriginal = 0;
}
if (Flags & PFF_MINUS) /* pad on right */
{
if (strncpy_s(*Out, Count, In, min(LengthInStr + 1, Count)) != SAFECRT_SUCCESS)
{
return FALSE;
}
*Out += min(LengthInStr, Count);
}
if (Padding > 0)
{
if (Flags & PFF_ZERO) /* '0', pad with zeros */
{
while (Padding-- && Count > *Out - OutOriginal)
{
*(*Out)++ = '0';
}
}
else /* pad left with spaces */
{
while (Padding-- && Count > *Out - OutOriginal)
{
*(*Out)++ = ' ';
}
}
}
if (!(Flags & PFF_MINUS)) /* put 'In' after padding */
{
if (strncpy_s(*Out, Count, In,
min(LengthInStr + 1, Count - (*Out - OutOriginal))) != SAFECRT_SUCCESS)
{
return FALSE;
}
*Out += min(LengthInStr, Count - (*Out - OutOriginal));
}
if (LengthInStr + PaddingOriginal > Count)
{
return FALSE;
}
else
{
return TRUE;
}
}
/*******************************************************************************
Function:
PAL_printf_arg_remover
Parameters:
ap
- pointer to the va_list from which to remove arguments
Width
- the width of the current format operation
Precision
- the precision of the current format option
Type
- the type of the argument for the current format option
Prefix
- the prefix for the current format option
*******************************************************************************/
__attribute__((no_instrument_function))
void PAL_printf_arg_remover(va_list *ap, INT Width, INT Precision, INT Type, INT Prefix)
{
/* remove arg and precision if needed */
if (PRECISION_STAR == Precision ||
PRECISION_INVALID == Precision)
{
(void)va_arg(*ap, int);
}
if (WIDTH_STAR == Width ||
WIDTH_INVALID == Width)
{
(void)va_arg(*ap, int);
}
if (Type == PFF_TYPE_FLOAT)
{
(void)va_arg(*ap, double);
}
else if (Type == PFF_TYPE_INT && Prefix == PFF_PREFIX_LONGLONG)
{
(void)va_arg(*ap, INT64);
}
else if (Type == PFF_TYPE_INT || Type == PFF_TYPE_CHAR)
{
(void)va_arg(*ap, int);
}
else
{
(void)va_arg(*ap, void *);
}
}
/*++
Function:
PAL_printf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
PAL_printf(
const char *format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(printf);
ENTRY("PAL_printf (format=%p (%s))\n", format, format);
va_start(ap, format);
Length = PAL_vprintf(format, ap);
va_end(ap);
LOGEXIT("PAL_printf returns int %d\n", Length);
PERF_EXIT(printf);
return Length;
}
/*++
Function:
PAL_fprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
PAL_fprintf(PAL_FILE *stream,const char *format,...)
{
LONG Length = 0;
va_list ap;
PERF_ENTRY(fprintf);
ENTRY("PAL_fprintf(stream=%p,format=%p (%s))\n",stream, format, format);
va_start(ap, format);
Length = PAL_vfprintf( stream, format, ap);
va_end(ap);
LOGEXIT("PAL_fprintf returns int %d\n", Length);
PERF_EXIT(fprintf);
return Length;
}
/*++
Function:
PAL_wprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
PAL_wprintf(
const char16_t *format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(wprintf);
ENTRY("PAL_wprintf (format=%p (%S))\n", format, format);
va_start(ap, format);
Length = PAL_vfwprintf( PAL_get_stdout(PAL_get_caller), format, ap);
va_end(ap);
LOGEXIT("PAL_wprintf returns int %d\n", Length);
PERF_EXIT(wprintf);
return Length;
}
/*++
Function:
PAL_vprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
PAL_vprintf(
const char *format,
va_list ap)
{
LONG Length;
PERF_ENTRY(vprintf);
ENTRY("PAL_vprintf (format=%p (%s))\n", format, format);
Length = PAL_vfprintf( PAL_get_stdout(PAL_get_caller), format, ap);
LOGEXIT("PAL_vprintf returns int %d\n", Length);
PERF_EXIT(vprintf);
return Length;
}
/*++
Function:
wsprintfA
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
PALAPIV
wsprintfA(
OUT LPSTR buffer,
IN LPCSTR format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(wsprintfA);
ENTRY("wsprintfA (buffer=%p, format=%p (%s))\n", buffer, format, format);
va_start(ap, format);
Length = PAL__vsnprintf(buffer, 1024, format, ap);
va_end(ap);
LOGEXIT("wsprintfA returns int %d\n", Length);
PERF_EXIT(wsprintfA);
return Length;
}
/*++
Function:
wsprintfW
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
PALAPIV
wsprintfW(
OUT LPWSTR buffer,
IN LPCWSTR format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(wsprintfW);
ENTRY("wsprintfW (buffer=%p, format=%p (%S))\n", buffer, format, format);
va_start(ap, format);
Length = PAL__wvsnprintf(buffer, 1024, format, ap);
va_end(ap);
LOGEXIT("wsprintfW returns int %d\n", Length);
PERF_EXIT(wsprintfW);
return Length;
}
/*++
Function:
_snprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
_snprintf(
char *buffer,
size_t count,
const char *format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(_snprintf);
ENTRY("_snprintf (buffer=%p, count=%lu, format=%p (%s))\n",
buffer, (unsigned long) count, format, format);
va_start(ap, format);
Length = PAL__vsnprintf(buffer, count, format, ap);
va_end(ap);
LOGEXIT("_snprintf returns int %d\n", Length);
PERF_EXIT(_snprintf);
return Length;
}
/*++
Function:
_snwprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
_snwprintf(
char16_t *buffer,
size_t count,
const char16_t *format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(_snwprintf);
ENTRY("_snwprintf (buffer=%p, count=%lu, format=%p (%S))\n",
buffer, (unsigned long) count, format, format);
va_start(ap, format);
Length = PAL__wvsnprintf(buffer, count, format, ap);
va_end(ap);
LOGEXIT("_snwprintf returns int %d\n", Length);
PERF_EXIT(_snwprintf);
return Length;
}
/*++
Function:
fwprintf
See MSDN doc.
--*/
__attribute__((no_instrument_function))
int
__cdecl
PAL_fwprintf(
PAL_FILE *stream,
const char16_t *format,
...)
{
LONG Length;
va_list ap;
PERF_ENTRY(fwprintf);
ENTRY("PAL_fwprintf (stream=%p, format=%p (%S))\n", stream, format, format);
va_start(ap, format);
Length = PAL_vfwprintf( stream, format, ap);
va_end(ap);
LOGEXIT("PAL_fwprintf returns int %d\n", Length);
PERF_EXIT(fwprintf);
return Length;
}
/*******************************************************************************
Function:
Internal_ScanfExtractFormatA
Paramaters:
Fmt
- format string to parse
- first character must be a '%'
- paramater gets updated to point to the character after
the %<foo> format string
Out
- buffer will contain the %<foo> format string
Store
- boolean value representing whether to store the type to be parsed
- '*' flag
Width
- will contain the width specified by the format string
- -1 if none given
Prefix
- an enumeration of the type prefix
Type
- an enumeration of the value type to be parsed
Notes:
- I'm also handling the undocumented %ws, %wc, %w...
*******************************************************************************/
#define CHECK_OUT_IN_ITS_RANGE(Out,BeginOut,EndOut) \
if ((Out)<(BeginOut) || (Out)>=(EndOut)) \
{ \
SetLastError(ERROR_INSUFFICIENT_BUFFER); \
ERROR("Pointer Out wanted to access 0x%p. However the range of buffer is [0x%p,0x%p).",\
(Out), (BeginOut), (EndOut)); \
return false; \
}
__attribute__((no_instrument_function))
static BOOL Internal_ScanfExtractFormatA(LPCSTR *Fmt, LPSTR Out, int iOutSize, LPBOOL Store,
LPINT Width, LPINT Prefix, LPINT Type)
{
BOOL Result = FALSE;
LPSTR TempStr;
LPSTR TempStrPtr;
LPSTR BaseOut = Out;
LPSTR EndOut = Out + iOutSize;
*Width = -1;
*Store = TRUE;
*Prefix = -1;
*Type = -1;
if (*Fmt && **Fmt == '%')
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = *(*Fmt)++;
}
else
{
return Result;
}
/* we'll never need a temp string longer than the original */
TempStrPtr = TempStr = (LPSTR) PAL_malloc(strlen(*Fmt)+1);
if (!TempStr)
{
ERROR("PAL_malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return Result;
}
/* parse '*' flag which means don't store */
if (**Fmt == '*')
{
*Store = FALSE;
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = *(*Fmt)++;
}
/* grab width specifier */
if (isdigit((unsigned char) **Fmt))
{
TempStrPtr = TempStr;
while (isdigit((unsigned char) **Fmt))
{
*TempStrPtr++ = **Fmt;
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = *(*Fmt)++;
}
*TempStrPtr = 0; /* end string */
*Width = atoi(TempStr);
if (*Width < 0)
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
SetLastError(ERROR_INTERNAL_ERROR);
return Result;
}
}
#ifdef BIT64
if (**Fmt == 'p')
{
*Prefix = SCANF_PREFIX_LONGLONG;
}
#endif
/* grab prefix of 'I64' for __int64 */
if ((*Fmt)[0] == 'I' && (*Fmt)[1] == '6' && (*Fmt)[2] == '4')
{
/* convert to 'q'/'ll' so Unix sscanf can handle it */
*Fmt += 3;
*Prefix = SCANF_PREFIX_LONGLONG;
}
/* grab a prefix of 'h' */
else if (**Fmt == 'h')
{
*Prefix = SCANF_PREFIX_SHORT;
++(*Fmt);
}
/* grab prefix of 'l' or the undocumented 'w' (at least in MSDN) */
else if (**Fmt == 'l' || **Fmt == 'w')
{
++(*Fmt);
#ifdef BIT64
// Only want to change the prefix on 64 bit when inputing characters.
if (**Fmt == 'c' || **Fmt == 's')
#endif
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (**Fmt == 'l')
{
*Prefix = SCANF_PREFIX_LONGLONG;
++(*Fmt);
}
}
else if (**Fmt == 'L')
{
/* a prefix of 'L' seems to be ignored */
++(*Fmt);
}
/* grab type 'c' */
if (**Fmt == 'c' || **Fmt == 'C')
{
*Type = SCANF_TYPE_CHAR;
if (*Prefix != SCANF_PREFIX_SHORT && **Fmt == 'C')
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (*Prefix == SCANF_PREFIX_LONG)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'l';
}
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'c';
++(*Fmt);
Result = TRUE;
}
/* grab type 's' */
else if (**Fmt == 's' || **Fmt == 'S')
{
*Type = SCANF_TYPE_STRING;
if (*Prefix != SCANF_PREFIX_SHORT && **Fmt == 'S')
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (*Prefix == SCANF_PREFIX_LONG)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'l';
}
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 's';
++(*Fmt);
Result = TRUE;
}
/* grab int types */
else if (**Fmt == 'd' || **Fmt == 'i' || **Fmt == 'o' ||
**Fmt == 'u' || **Fmt == 'x' || **Fmt == 'X' ||
**Fmt == 'p')
{
*Type = SCANF_TYPE_INT;
if (*Prefix == SCANF_PREFIX_SHORT)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'h';
}
else if (*Prefix == SCANF_PREFIX_LONG)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'l';
}
else if (*Prefix == SCANF_PREFIX_LONGLONG)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
if (strcpy_s(Out, iOutSize-(Out-BaseOut), scanf_longlongfmt) != SAFECRT_SUCCESS)
{
ERROR("strcpy_s failed\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
Out += strlen(scanf_longlongfmt);
}
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = *(*Fmt)++;
Result = TRUE;
}
else if (**Fmt == 'e' || **Fmt == 'E' || **Fmt == 'f' ||
**Fmt == 'g' || **Fmt == 'G')
{
/* we can safely ignore the prefixes and only add the type*/
*Type = SCANF_TYPE_FLOAT;
/* this gets rid of %E/%G since they're they're the
same when scanning */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = tolower( *(*Fmt)++ );
Result = TRUE;
}
else if (**Fmt == 'n')
{
if (*Prefix == SCANF_PREFIX_SHORT)
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'h';
}
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = *(*Fmt)++;
*Type = SCANF_TYPE_N;
Result = TRUE;
}
else if (**Fmt == '[')
{
/* There is a small compatibility problem in the handling of the []
option in FreeBSD vs. Windows. In Windows, you can have [z-a]
as well as [a-z]. In FreeBSD, [z-a] fails. So, we need to
reverse the instances of z-a to a-z (and [m-e] to [e-m], etc). */
/* step 1 : copy the leading [ */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '[';
(*Fmt)++;
/* step 2 : copy a leading ^, if present */
if( '^' == **Fmt )
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '^';
(*Fmt)++;
}
/* step 3 : copy a leading ], if present; a ] immediately after the
leading [ (or [^) does *not* end the sequence, it is part of the
characters to match */
if( ']' == **Fmt )
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = ']';
(*Fmt)++;
}
/* step 4 : if the next character is already a '-', it's not part of an
interval specifier, so just copy it */
if('-' == **Fmt )
{
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '-';
(*Fmt)++;
}
/* ok then, process the rest of it */
while( '\0' != **Fmt )
{
if(']' == **Fmt)
{
/* ']' marks end of the format specifier; we're done */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = ']';
(*Fmt)++;
break;
}
if('-' == **Fmt)
{
if( ']' == (*Fmt)[1] )
{
/* got a '-', next character is the terminating ']';
copy '-' literally */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '-';
(*Fmt)++;
}
else
{
/* got a '-' indicating an interval specifier */
unsigned char prev, next;
/* get the interval boundaries */
prev = (*Fmt)[-1];
next = (*Fmt)[1];
/* if boundaries were inverted, replace the already-copied
low boundary by the 'real' low boundary */
if( prev > next )
{
CHECK_OUT_IN_ITS_RANGE(Out-1,BaseOut,EndOut)
Out[-1] = next;
/* ...and save the 'real' upper boundary, which will be
copied to 'Out' below */
next = prev;
}
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '-';
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = next;
/* skip over the '-' and the next character, which we
already copied */
(*Fmt)+=2;
}
}
else
{
/* plain character; just copy it */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = **Fmt;
(*Fmt)++;
}
}
*Type = SCANF_TYPE_BRACKETS;
Result = TRUE;
}
else if (**Fmt == ' ')
{
*Type = SCANF_TYPE_SPACE;
}
/* add %n so we know how far to increment the pointer */
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = '%';
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out++ = 'n';
CHECK_OUT_IN_ITS_RANGE(Out,BaseOut,EndOut)
*Out = 0; /* end the string */
PAL_free(TempStr);
return Result;
}
/*******************************************************************************
Function:
Internal_ScanfExtractFormatW
-- see Internal_ScanfExtractFormatA above
*******************************************************************************/
__attribute__((no_instrument_function))
static BOOL Internal_ScanfExtractFormatW(LPCWSTR *Fmt, LPSTR Out, int iOutSize, LPBOOL Store,
LPINT Width, LPINT Prefix, LPINT Type)
{
BOOL Result = FALSE;
LPSTR TempStr;
LPSTR TempStrPtr;
*Width = -1;
*Store = TRUE;
*Prefix = -1;
*Type = -1;
if (*Fmt && **Fmt == '%')
{
*Out++ = *(*Fmt)++;
}
else
{
return Result;
}
/* we'll never need a temp string longer than the original */
TempStrPtr = TempStr = (LPSTR) PAL_malloc(PAL_wcslen(*Fmt)+1);
if (!TempStr)
{
ERROR("PAL_malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return Result;
}
/* parse '*' flag which means don't store */
if (**Fmt == '*')
{
*Store = FALSE;
*Out++ = *(*Fmt)++;
}
/* grab width specifier */
if (isdigit(**Fmt))
{
TempStrPtr = TempStr;
while (isdigit(**Fmt))
{
*TempStrPtr++ = **Fmt;
*Out++ = *(*Fmt)++;
}
*TempStrPtr = 0; /* end string */
*Width = atoi(TempStr);
if (*Width < 0)
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
SetLastError(ERROR_INTERNAL_ERROR);
return Result;
}
}
#ifdef BIT64
if (**Fmt == 'p')
{
*Prefix = SCANF_PREFIX_LONGLONG;
}
#endif
/* grab prefix of 'I64' for __int64 */
if ((*Fmt)[0] == 'I' && (*Fmt)[1] == '6' && (*Fmt)[2] == '4')
{
/* convert to 'q'/'ll' so that Unix sscanf can handle it */
*Fmt += 3;
*Prefix = SCANF_PREFIX_LONGLONG;
}
/* grab a prefix of 'h' */
else if (**Fmt == 'h')
{
*Prefix = SCANF_PREFIX_SHORT;
++(*Fmt);
}
/* grab prefix of 'l' or the undocumented 'w' (at least in MSDN) */
else if (**Fmt == 'l' || **Fmt == 'w')
{
++(*Fmt);
#ifdef BIT64
// Only want to change the prefix on 64 bit when inputing characters.
if (**Fmt == 'C' || **Fmt == 'S')
#endif
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (**Fmt == 'l')
{
*Prefix = SCANF_PREFIX_LONGLONG;
++(*Fmt);
}
}
else if (**Fmt == 'L')
{
/* a prefix of 'L' seems to be ignored */
++(*Fmt);
}
/* grab type 'c' */
if (**Fmt == 'c' || **Fmt == 'C')
{
*Type = SCANF_TYPE_CHAR;
if (*Prefix != SCANF_PREFIX_SHORT && **Fmt == 'c')
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (*Prefix == SCANF_PREFIX_LONG)
{
*Out++ = 'l';
}
*Out++ = 'c';
++(*Fmt);
Result = TRUE;
}
/* grab type 's' */
else if (**Fmt == 's' || **Fmt == 'S')
{
*Type = SCANF_TYPE_STRING;
if (*Prefix != SCANF_PREFIX_SHORT && **Fmt == 's')
{
*Prefix = SCANF_PREFIX_LONG; /* give it a wide prefix */
}
if (*Prefix == SCANF_PREFIX_LONG)
{
*Out++ = 'l';
}
*Out++ = 's';
++(*Fmt);
Result = TRUE;
}
/* grab int types */
else if (**Fmt == 'd' || **Fmt == 'i' || **Fmt == 'o' ||
**Fmt == 'u' || **Fmt == 'x' || **Fmt == 'X' ||
**Fmt == 'p')
{
*Type = SCANF_TYPE_INT;
if (*Prefix == SCANF_PREFIX_SHORT)
{
*Out++ = 'h';
}
else if (*Prefix == SCANF_PREFIX_LONG)
{
*Out++ = 'l';
}
else if (*Prefix == SCANF_PREFIX_LONGLONG)
{
if (strcpy_s(Out, iOutSize, scanf_longlongfmt) != SAFECRT_SUCCESS)
{
ERROR("strcpy_s failed\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
Out += strlen(scanf_longlongfmt);
}
*Out++ = *(*Fmt)++;
Result = TRUE;
}
else if (**Fmt == 'e' || **Fmt == 'E' || **Fmt == 'f' ||
**Fmt == 'g' || **Fmt == 'G')
{
/* we can safely ignore the prefixes and only add the type*/
*Type = SCANF_TYPE_FLOAT;
/* this gets rid of %E/%G since they're they're the
same when scanning */
*Out++ = tolower( *(*Fmt)++ );
Result = TRUE;
}
else if (**Fmt == 'n')
{
if (*Prefix == SCANF_PREFIX_SHORT)
{
*Out++ = 'h';
}
*Out++ = *(*Fmt)++;
*Type = SCANF_TYPE_N;
Result = TRUE;
}
else if (**Fmt == '[')
{
/* There is a small compatibility problem in the handling of the []
option in FreeBSD vs. Windows. In Windows, you can have [z-a]
as well as [a-z]. In FreeBSD, [z-a] fails. So, we need to
reverse the instances of z-a to a-z (and [m-e] to [e-m], etc). */
/* step 1 : copy the leading [ */
*Out++ = '[';
(*Fmt)++;
/* step 2 : copy a leading ^, if present */
if( '^' == **Fmt )
{
*Out++ = '^';
(*Fmt)++;
}
/* step 3 : copy a leading ], if present; a ] immediately after the
leading [ (or [^) does *not* end the sequence, it is part of the
characters to match */
if( ']' == **Fmt )
{
*Out++ = ']';
(*Fmt)++;
}
/* step 4 : if the next character is already a '-', it's not part of an