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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
|
/* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
Copyright (c) 2014-2015 Datalight, Inc.
All Rights Reserved Worldwide.
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; use version 2 of the License.
This program is distributed in the hope that it will be useful,
but "AS-IS," 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, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Businesses and individuals that for commercial or other reasons cannot
comply with the terms of the GPLv2 license may obtain a commercial license
before incorporating Reliance Edge into proprietary software for
distribution in any form. Visit http://www.datalight.com/reliance-edge for
more information.
*/
/** @file
@brief Implements functions for printing.
These functions are intended to be used in portable test code, which cannot
assume the standard I/O functions will be available. Similar to their ANSI
C counterparts, these functions allow formatting text strings and (if the
configuration allows it) outputing formatted text. The latter ability
relies on the RedOsOutputString() OS service function.
Do *not* use these functions in code which can safely assume the standard
I/O functions are available (e.g., in host tools code).
Do *not* use these functions from within the file system driver. These
functions use variable arguments and thus are not MISRA-C:2012 compliant.
*/
#include <redfs.h>
#include <redtestutils.h>
#include <limits.h>
#include <stdarg.h>
/** @brief Maximum number of bytes of output supported by RedPrintf().
Typically only Datalight code uses these functions, and that could should be
written to respect this limit, so it should not normally be necessary to
adjust this value.
*/
#define OUTPUT_BUFFER_SIZE 256U
typedef enum
{
PRFMT_UNKNOWN = 0,
PRFMT_CHAR,
PRFMT_ANSISTRING,
PRFMT_SIGNED8BIT,
PRFMT_UNSIGNED8BIT,
PRFMT_SIGNED16BIT,
PRFMT_UNSIGNED16BIT,
PRFMT_SIGNED32BIT,
PRFMT_UNSIGNED32BIT,
PRFMT_SIGNED64BIT,
PRFMT_UNSIGNED64BIT,
PRFMT_HEX8BIT,
PRFMT_HEX16BIT,
PRFMT_HEX32BIT,
PRFMT_HEX64BIT,
PRFMT_POINTER,
PRFMT_DOUBLEPERCENT
} PRINTTYPE;
typedef struct
{
PRINTTYPE type; /* The PRFMT_* type found */
uint32_t ulSpecifierIdx; /* Returns a pointer to the % sign */
uint32_t ulFillLen;
char cFillChar;
bool fLeftJustified;
bool fHasIllegalType; /* TRUE if an illegal sequence was skipped over */
bool fHasVarWidth;
} PRINTFORMAT;
/* Our output handlers are written for standard fixed width data types. Map
the standard ANSI C data types onto our handlers. Currently this code has
the following requirements:
1) shorts must be either 16 or 32 bits
2) ints must be either 16 or 32 bits
3) longs must be between 32 or 64 bits
4) long longs must be 64 bits
*/
#if (USHRT_MAX == 0xFFFFU)
#define MAPSHORT PRFMT_SIGNED16BIT
#define MAPUSHORT PRFMT_UNSIGNED16BIT
#define MAPHEXUSHORT PRFMT_HEX16BIT
#elif (USHRT_MAX == 0xFFFFFFFFU)
#define MAPSHORT PRFMT_SIGNED32BIT
#define MAPUSHORT PRFMT_UNSIGNED32BIT
#define MAPHEXUSHORT PRFMT_HEX32BIT
#else
#error "The 'short' data type does not have a 16 or 32-bit width"
#endif
#if (UINT_MAX == 0xFFFFU)
#define MAPINT PRFMT_SIGNED16BIT
#define MAPUINT PRFMT_UNSIGNED16BIT
#define MAPHEXUINT PRFMT_HEX16BIT
#elif (UINT_MAX == 0xFFFFFFFFU)
#define MAPINT PRFMT_SIGNED32BIT
#define MAPUINT PRFMT_UNSIGNED32BIT
#define MAPHEXUINT PRFMT_HEX32BIT
#else
#error "The 'int' data type does not have a 16 or 32-bit width"
#endif
#if (ULONG_MAX == 0xFFFFFFFFU)
#define MAPLONG PRFMT_SIGNED32BIT
#define MAPULONG PRFMT_UNSIGNED32BIT
#define MAPHEXULONG PRFMT_HEX32BIT
#elif (ULONG_MAX <= UINT64_SUFFIX(0xFFFFFFFFFFFFFFFF))
/* We've run into unusual environments where "longs" are 40-bits wide.
In this event, map them to 64-bit types so no data is lost.
*/
#define MAPLONG PRFMT_SIGNED64BIT
#define MAPULONG PRFMT_UNSIGNED64BIT
#define MAPHEXULONG PRFMT_HEX64BIT
#else
#error "The 'long' data type is not between 32 and 64 bits wide"
#endif
#if defined(ULLONG_MAX) && (ULLONG_MAX != UINT64_SUFFIX(0xFFFFFFFFFFFFFFFF))
#error "The 'long long' data type is not 64 bits wide"
#else
#define MAPLONGLONG PRFMT_SIGNED64BIT
#define MAPULONGLONG PRFMT_UNSIGNED64BIT
#define MAPHEXULONGLONG PRFMT_HEX64BIT
#endif
static uint32_t ProcessFormatSegment(char *pcBuffer, uint32_t ulBufferLen, const char *pszFormat, PRINTFORMAT *pFormat, uint32_t *pulSpecifierLen);
static uint32_t ParseFormatSpecifier(char const *pszFormat, PRINTFORMAT *pFormatType);
static PRINTTYPE ParseFormatType(const char *pszFormat, uint32_t *pulTypeLen);
static uint32_t LtoA(char *pcBuffer, uint32_t ulBufferLen, int32_t lNum, uint32_t ulFillLen, char cFill);
static uint32_t LLtoA(char *pcBuffer, uint32_t ulBufferLen, int64_t llNum, uint32_t ulFillLen, char cFill);
static uint32_t ULtoA(char *pcBuffer, uint32_t ulBufferLen, uint32_t ulNum, bool fHex, uint32_t ulFillLen, char cFill);
static uint32_t ULLtoA(char *pcBuffer, uint32_t ulBufferLen, uint64_t ullNum, bool fHex, uint32_t ulFillLen, char cFill);
static uint32_t FinishToA(const char *pcDigits, uint32_t ulDigits, char *pcOutBuffer, uint32_t ulBufferLen, uint32_t ulFillLen, char cFill);
/* Digits for the *LtoA() routines.
*/
static const char gacDigits[] = "0123456789ABCDEF";
#if REDCONF_OUTPUT == 1
/** @brief Print formatted data with a variable length argument list.
This function provides a subset of the ANSI C printf() functionality with
several extensions to support fixed size data types.
See RedVSNPrintf() for the list of supported types.
@param pszFormat A pointer to the null-terminated format string.
@param ... The variable length argument list.
*/
void RedPrintf(
const char *pszFormat,
...)
{
va_list arglist;
va_start(arglist, pszFormat);
RedVPrintf(pszFormat, arglist);
va_end(arglist);
}
/** @brief Print formatted data using a pointer to a variable length argument
list.
This function provides a subset of the ANSI C vprintf() functionality.
See RedVSNPrintf() for the list of supported types.
This function accommodates a maximum output length of #OUTPUT_BUFFER_SIZE.
If this function must truncate the output, and the original string was
\n terminated, the truncated output will be \n terminated as well.
@param pszFormat A pointer to the null-terminated format string.
@param arglist The variable length argument list.
*/
void RedVPrintf(
const char *pszFormat,
va_list arglist)
{
char achBuffer[OUTPUT_BUFFER_SIZE];
if(RedVSNPrintf(achBuffer, sizeof(achBuffer), pszFormat, arglist) == -1)
{
/* Ensture the buffer is null terminated.
*/
achBuffer[sizeof(achBuffer) - 1U] = '\0';
/* If the original string was \n terminated and the new one is not, due to
truncation, stuff a \n into the new one.
*/
if(pszFormat[RedStrLen(pszFormat) - 1U] == '\n')
{
achBuffer[sizeof(achBuffer) - 2U] = '\n';
}
}
RedOsOutputString(achBuffer);
}
#endif /* #if REDCONF_OUTPUT == 1 */
/** @brief Format arguments into a string using a subset of the ANSI C
vsprintf() functionality.
This function is modeled after the Microsoft _snprint() extension to the
ANSI C sprintf() function, and allows a buffer length to be specified so
that overflow is avoided.
See RedVSNPrintf() for the list of supported types.
@param pcBuffer A pointer to the output buffer
@param ulBufferLen The output buffer length
@param pszFormat A pointer to the null terminated format string
@param ... Variable argument list
@return The length output, or -1 if the buffer filled up. If -1 is
returned, the output buffer may not be null-terminated.
*/
int32_t RedSNPrintf(
char *pcBuffer,
uint32_t ulBufferLen,
const char *pszFormat,
...)
{
int32_t iLen;
va_list arglist;
va_start(arglist, pszFormat);
iLen = RedVSNPrintf(pcBuffer, ulBufferLen, pszFormat, arglist);
va_end(arglist);
return iLen;
}
/** @brief Format arguments into a string using a subset of the ANSI C
vsprintf() functionality.
This function is modeled after the Microsoft _vsnprint() extension to the
ANSI C vsprintf() function, and requires a buffer length to be specified so
that overflow is avoided.
The following ANSI C standard formatting codes are supported:
| Code | Meaning |
| ---- | ---------------------------------- |
| %c | Format a character |
| %s | Format a null-terminated C string |
| %hd | Format a signed short |
| %hu | Format an unsigned short |
| %d | Format a signed integer |
| %u | Format an unsigned integer |
| %ld | Format a signed long |
| %lu | Format an unsigned long |
| %lld | Format a signed long long |
| %llu | Format an unsigned long long |
| %hx | Format a short in hex |
| %x | Format an integer in hex |
| %lx | Format a long in hex |
| %llx | Format a long long in hex |
| %p | Format a pointer (hex value) |
@note All formatting codes are case-sensitive.
Fill characters and field widths are supported per the ANSI standard, as is
left justification with the '-' character.
The only supported fill characters are '0', ' ', and '_'.
'*' is supported to specify variable length field widths.
Hexidecimal numbers are always displayed in upper case. Formatting codes
which specifically request upper case (e.g., "%lX") are not supported.
Unsupported behaviors:
- Precision is not supported.
- Floating point is not supported.
Errata:
- There is a subtle difference in the return value for this function versus
the Microsoft implementation. In the Microsoft version, if the buffer
exactly fills up, but there is no room for a null-terminator, the return
value will be the length of the buffer. In this code, -1 will be returned
when this happens.
- When using left justified strings, the only supported fill character is a
space, regardless of what may be specified. It is not clear if this is
ANSI standard or just the way the Microsoft function works, but we emulate
the Microsoft behavior.
@param pcBuffer A pointer to the output buffer.
@param ulBufferLen The output buffer length.
@param pszFormat A pointer to the null terminated ANSI format string.
@param arglist Variable argument list.
@return The length output, or -1 if the buffer filled up. If -1 is
returned, the output buffer may not be null-terminated.
*/
int32_t RedVSNPrintf(
char *pcBuffer,
uint32_t ulBufferLen,
const char *pszFormat,
va_list arglist)
{
uint32_t ulBufIdx = 0U;
uint32_t ulFmtIdx = 0U;
int32_t iLen;
while((pszFormat[ulFmtIdx] != '\0') && (ulBufIdx < ulBufferLen))
{
PRINTFORMAT fmt;
uint32_t ulSpecifierLen;
uint32_t ulWidth;
/* Process the next segment of the format string, outputting
any non-format specifiers, as output buffer space allows,
and return information about the next format specifier.
*/
ulWidth = ProcessFormatSegment(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, &pszFormat[ulFmtIdx], &fmt, &ulSpecifierLen);
if(ulWidth)
{
REDASSERT(ulWidth <= (ulBufferLen - ulBufIdx));
ulBufIdx += ulWidth;
}
/* If no specifier was found, or if the output buffer is
full, we're done -- get out.
*/
if((ulSpecifierLen == 0U) || (ulBufIdx == ulBufferLen))
{
break;
}
/* Otherwise, the math should add up for these things...
*/
REDASSERT(&pszFormat[fmt.ulSpecifierIdx] == &pszFormat[ulWidth]);
/* Point past the specifier, to the next piece of the format string.
*/
ulFmtIdx = ulFmtIdx + fmt.ulSpecifierIdx + ulSpecifierLen;
if(fmt.fHasVarWidth)
{
int iFillLen = va_arg(arglist, int);
if(iFillLen >= 0)
{
fmt.ulFillLen = (uint32_t)iFillLen;
}
else
{
/* Bogus fill length. Ignore.
*/
fmt.ulFillLen = 0U;
}
}
switch(fmt.type)
{
case PRFMT_DOUBLEPERCENT:
{
/* Nothing to do. A single percent has already been output,
and we just finished skipping past the second percent.
*/
break;
}
/*-----------------> Small int handling <------------------
*
* Values smaller than "int" will be promoted to "int" by
* the compiler, so we must retrieve them using "int" when
* calling va_arg(). Once we've done that, we immediately
* put the value into the desired data type.
*---------------------------------------------------------*/
case PRFMT_CHAR:
{
pcBuffer[ulBufIdx] = (char)va_arg(arglist, int);
ulBufIdx++;
break;
}
case PRFMT_SIGNED8BIT:
{
int8_t num = (int8_t)va_arg(arglist, int);
ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, num, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_UNSIGNED8BIT:
{
uint8_t bNum = (uint8_t)va_arg(arglist, unsigned);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, bNum, false, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_HEX8BIT:
{
uint8_t bNum = (uint8_t)va_arg(arglist, unsigned);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, bNum, true, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_SIGNED16BIT:
{
int16_t num = (int16_t)va_arg(arglist, int);
ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, num, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_UNSIGNED16BIT:
{
uint16_t uNum = (uint16_t)va_arg(arglist, unsigned);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, uNum, false, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_HEX16BIT:
{
uint16_t uNum = (uint16_t)va_arg(arglist, unsigned);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, uNum, true, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_SIGNED32BIT:
{
int32_t lNum = va_arg(arglist, int32_t);
ulBufIdx += LtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, lNum, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_UNSIGNED32BIT:
{
uint32_t ulNum = va_arg(arglist, uint32_t);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulNum, false, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_HEX32BIT:
{
uint32_t ulNum = va_arg(arglist, uint32_t);
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulNum, true, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_SIGNED64BIT:
{
int64_t llNum = va_arg(arglist, int64_t);
ulBufIdx += LLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, llNum, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_UNSIGNED64BIT:
{
uint64_t ullNum = va_arg(arglist, uint64_t);
ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullNum, false, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_HEX64BIT:
{
uint64_t ullNum = va_arg(arglist, uint64_t);
ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullNum, true, fmt.ulFillLen, fmt.cFillChar);
break;
}
case PRFMT_POINTER:
{
const void *ptr = va_arg(arglist, const void *);
/* Assert our assumption.
*/
REDASSERT(sizeof(void *) <= 8U);
/* Format as either a 64-bit or a 32-bit value.
*/
if(sizeof(void *) > 4U)
{
/* Attempt to quiet warnings.
*/
uintptr_t ptrval = (uintptr_t)ptr;
uint64_t ullPtrVal = (uint64_t)ptrval;
ulBufIdx += ULLtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ullPtrVal, true, fmt.ulFillLen, fmt.cFillChar);
}
else
{
/* Attempt to quiet warnings.
*/
uintptr_t ptrval = (uintptr_t)ptr;
uint32_t ulPtrVal = (uint32_t)ptrval;
ulBufIdx += ULtoA(&pcBuffer[ulBufIdx], ulBufferLen - ulBufIdx, ulPtrVal, true, fmt.ulFillLen, fmt.cFillChar);
}
break;
}
case PRFMT_ANSISTRING:
{
const char *pszArg = va_arg(arglist, const char *);
uint32_t ulArgIdx = 0U;
if(pszArg == NULL)
{
pszArg = "null";
}
if(fmt.ulFillLen > 0U)
{
if(!fmt.fLeftJustified)
{
uint32_t ulLen = RedStrLen(pszArg);
/* So long as we are not left justifying, fill as many
characters as is necessary to make the string right
justified.
*/
while(((ulBufferLen - ulBufIdx) > 0U) && (fmt.ulFillLen > ulLen))
{
pcBuffer[ulBufIdx] = fmt.cFillChar;
ulBufIdx++;
fmt.ulFillLen--;
}
}
/* Move as many characters as we have space for into the
output buffer.
*/
while(((ulBufferLen - ulBufIdx) > 0U) && (pszArg[ulArgIdx] != '\0'))
{
pcBuffer[ulBufIdx] = pszArg[ulArgIdx];
ulBufIdx++;
ulArgIdx++;
if(fmt.ulFillLen > 0U)
{
fmt.ulFillLen--;
}
}
/* If there is any space left to fill, do it (the string
must have been left justified).
*/
while(((ulBufferLen - ulBufIdx) > 0U) && (fmt.ulFillLen > 0U))
{
/* This is NOT a typo -- when using left justified
strings, spaces are the only allowed fill character.
See the errata.
*/
pcBuffer[ulBufIdx] = ' ';
ulBufIdx++;
fmt.ulFillLen--;
}
}
else
{
/* No fill characters, just move up to as many
characters as we have space for in the output
buffer.
*/
while(((ulBufferLen - ulBufIdx) > 0U) && (pszArg[ulArgIdx] != '\0'))
{
pcBuffer[ulBufIdx] = pszArg[ulArgIdx];
ulBufIdx++;
ulArgIdx++;
}
}
break;
}
default:
{
REDERROR();
break;
}
}
}
/* If there is space, tack on a null and return the output length
processed, not including the null.
*/
if(ulBufIdx < ulBufferLen)
{
pcBuffer[ulBufIdx] = '\0';
iLen = (int32_t)ulBufIdx;
}
else
{
/* Not enough space, just return -1, with no null termination
*/
iLen = -1;
}
return iLen;
}
/** @brief Process the next segment of the format string, outputting any
non-format specifiers, as output buffer space allows, and return
information about the next format specifier.
@note If the returned value is the same as the supplied @p ulBufferLen,
the output buffer will not be null-terminated. In all other cases,
the result will be null-terminated. The returned length will never
include the null in the count.
@param pcBuffer The output buffer.
@param ulBufferLen The output buffer length.
@param pszFormat The format string to process.
@param pFormat The PRINTFORMAT structure to fill.
@param pulSpecifierLen Returns the length of any format specifier string,
or zero if no specifier was found.
@return The count of characters from pszFormatt which were processed and
copied to pcBuffer.
- If zero is returned and *pulSpecifierLen is non-zero, then
a format specifier string was found at the start of pszFmt.
- If non-zero is returned and *pulSpecifierLen is zero, then
no format specifier string was found, and the entire pszFmt
string was copied to pBuffer (or as much as will fit).
*/
static uint32_t ProcessFormatSegment(
char *pcBuffer,
uint32_t ulBufferLen,
const char *pszFormat,
PRINTFORMAT *pFormat,
uint32_t *pulSpecifierLen)
{
uint32_t ulWidth = 0U;
/* Find the next format specifier string, and information about it.
*/
*pulSpecifierLen = ParseFormatSpecifier(pszFormat, pFormat);
if(*pulSpecifierLen == 0U)
{
/* If no specifier was found at all, then simply output the full length
of the string, or as much as will fit.
*/
ulWidth = REDMIN(ulBufferLen, RedStrLen(pszFormat));
RedMemCpy(pcBuffer, pszFormat, ulWidth);
}
else
{
/* If we encountered a double percent, skip past one of them so it is
copied into the output buffer.
*/
if(pFormat->type == PRFMT_DOUBLEPERCENT)
{
pFormat->ulSpecifierIdx++;
/* A double percent specifier always has a length of two. Since
we're processing one of those percent signs, reduce the length
to one. Assert it so.
*/
REDASSERT(*pulSpecifierLen == 2U);
(*pulSpecifierLen)--;
}
/* So long as the specifier is not the very first thing in the format
string...
*/
if(pFormat->ulSpecifierIdx != 0U)
{
/* A specifier was found, but there is other data preceding it.
Copy as much as allowed to the output buffer.
*/
ulWidth = REDMIN(ulBufferLen, pFormat->ulSpecifierIdx);
RedMemCpy(pcBuffer, pszFormat, ulWidth);
}
}
/* If there is room in the output buffer, null-terminate whatever is there.
But note that the returned length never includes the null.
*/
if(ulWidth < ulBufferLen)
{
pcBuffer[ulWidth] = 0U;
}
return ulWidth;
}
/** @brief Parse the specified format string for a valid RedVSNPrintf() format
sequence, and return information about it.
@param pszFormat The format string to process.
@param pFormatType The PRINTFORMAT structure to fill. The data is only
valid if a non-zero length is returned.
@return The length of the full format specifier string, starting at
pFormat->ulSpecifierIdx. Returns zero if a valid specifier was
not found.
*/
static uint32_t ParseFormatSpecifier(
char const *pszFormat,
PRINTFORMAT *pFormatType)
{
bool fContainsIllegalSequence = false;
uint32_t ulLen = 0U;
uint32_t ulIdx = 0U;
while(pszFormat[ulIdx] != '\0')
{
uint32_t ulTypeLen;
/* general output
*/
if(pszFormat[ulIdx] != '%')
{
ulIdx++;
}
else
{
RedMemSet(pFormatType, 0U, sizeof(*pFormatType));
/* Record the location of the start of the format sequence
*/
pFormatType->ulSpecifierIdx = ulIdx;
ulIdx++;
if(pszFormat[ulIdx] == '-')
{
pFormatType->fLeftJustified = true;
ulIdx++;
}
if((pszFormat[ulIdx] == '0') || (pszFormat[ulIdx] == '_'))
{
pFormatType->cFillChar = pszFormat[ulIdx];
ulIdx++;
}
else
{
pFormatType->cFillChar = ' ';
}
if(pszFormat[ulIdx] == '*')
{
pFormatType->fHasVarWidth = true;
ulIdx++;
}
else if(ISDIGIT(pszFormat[ulIdx]))
{
pFormatType->ulFillLen = (uint32_t)RedAtoI(&pszFormat[ulIdx]);
while(ISDIGIT(pszFormat[ulIdx]))
{
ulIdx++;
}
}
else
{
/* No fill length.
*/
}
pFormatType->type = ParseFormatType(&pszFormat[ulIdx], &ulTypeLen);
if(pFormatType->type != PRFMT_UNKNOWN)
{
/* Even though we are returning successfully, keep track of
whether an illegal sequence was encountered and skipped.
*/
pFormatType->fHasIllegalType = fContainsIllegalSequence;
ulLen = (ulIdx - pFormatType->ulSpecifierIdx) + ulTypeLen;
break;
}
/* In the case of an unrecognized type string, simply ignore
it entirely. Reset the pointer to the position following
the percent sign, so it is not found again.
*/
fContainsIllegalSequence = false;
ulIdx = pFormatType->ulSpecifierIdx + 1U;
}
}
return ulLen;
}
/** @brief Parse a RedPrintf() format type string to determine the proper data
type.
@param pszFormat The format string to process. This must be a pointer to
the character following any width or justification
characters.
@param pulTypeLen The location in which to store the type length. The
value will be 0 if PRFMT_UNKNOWN is returned.
@return Rhe PRFMT_* type value, or PRFMT_UNKNOWN if the type is not
recognized.
*/
static PRINTTYPE ParseFormatType(
const char *pszFormat,
uint32_t *pulTypeLen)
{
PRINTTYPE fmtType = PRFMT_UNKNOWN;
uint32_t ulIdx = 0U;
switch(pszFormat[ulIdx])
{
case '%':
fmtType = PRFMT_DOUBLEPERCENT;
break;
case 'c':
fmtType = PRFMT_CHAR;
break;
case 's':
fmtType = PRFMT_ANSISTRING;
break;
case 'p':
fmtType = PRFMT_POINTER;
break;
case 'd':
fmtType = MAPINT;
break;
case 'u':
fmtType = MAPUINT;
break;
case 'x':
fmtType = MAPHEXUINT;
break;
case 'h':
{
ulIdx++;
switch(pszFormat[ulIdx])
{
case 'd':
fmtType = MAPSHORT;
break;
case 'u':
fmtType = MAPUSHORT;
break;
case 'x':
fmtType = MAPHEXUSHORT;
break;
default:
break;
}
break;
}
case 'l':
{
ulIdx++;
switch(pszFormat[ulIdx])
{
case 'd':
fmtType = MAPLONG;
break;
case 'u':
fmtType = MAPULONG;
break;
case 'x':
fmtType = MAPHEXULONG;
break;
case 'l':
{
ulIdx++;
switch(pszFormat[ulIdx])
{
case 'd':
fmtType = MAPLONGLONG;
break;
case 'u':
fmtType = MAPULONGLONG;
break;
case 'x':
case 'X':
fmtType = MAPHEXULONGLONG;
break;
default:
break;
}
break;
}
default:
break;
}
break;
}
default:
break;
}
if(fmtType != PRFMT_UNKNOWN)
{
*pulTypeLen = ulIdx + 1U;
}
else
{
*pulTypeLen = 0U;
}
return fmtType;
}
/** @brief Format a signed 32-bit integer as a base 10 ASCII string.
@note If the output buffer length is exhausted, the result will *not* be
null-terminated.
@note If the @p ulFillLen value is greater than or equal to the buffer
length, the result will not be null-terminated, even if the
formatted portion of the data is shorter than the buffer length.
@param pcBuffer The output buffer
@param ulBufferLen A pointer to the output buffer length
@param lNum The 32-bit signed number to convert
@param ulFillLen The fill length, if any
@param cFill The fill character to use
@return The length of the string.
*/
static uint32_t LtoA(
char *pcBuffer,
uint32_t ulBufferLen,
int32_t lNum,
uint32_t ulFillLen,
char cFill)
{
uint32_t ulLen;
if(pcBuffer == NULL)
{
REDERROR();
ulLen = 0U;
}
else
{
char ach[12U]; /* big enough for a int32_t in base 10 */
uint32_t ulDigits = 0U;
uint32_t ulNum;
bool fSign;
if(lNum < 0)
{
fSign = true;
ulNum = (uint32_t)-lNum;
}
else
{
fSign = false;
ulNum = (uint32_t)lNum;
}
do
{
ach[ulDigits] = gacDigits[ulNum % 10U];
ulNum = ulNum / 10U;
ulDigits++;
}
while(ulNum);
if(fSign)
{
ach[ulDigits] = '-';
ulDigits++;
}
ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
}
return ulLen;
}
/** @brief Format a signed 64-bit integer as a base 10 ASCII string.
@note If the output buffer length is exhausted, the result will *not* be
null-terminated.
@note If the @p ulFillLen value is greater than or equal to the buffer
length, the result will not be null-terminated, even if the
formatted portion of the data is shorter than the buffer length.
@param pcBuffer The output buffer
@param ulBufferLen A pointer to the output buffer length
@param llNum The 64-bit signed number to convert
@param ulFillLen The fill length, if any
@param cFill The fill character to use
@return The length of the string.
*/
static uint32_t LLtoA(
char *pcBuffer,
uint32_t ulBufferLen,
int64_t llNum,
uint32_t ulFillLen,
char cFill)
{
uint32_t ulLen;
if(pcBuffer == NULL)
{
REDERROR();
ulLen = 0U;
}
else
{
char ach[12U]; /* big enough for a int32_t in base 10 */
uint32_t ulDigits = 0U;
uint64_t ullNum;
bool fSign;
if(llNum < 0)
{
fSign = true;
ullNum = (uint64_t)-llNum;
}
else
{
fSign = false;
ullNum = (uint64_t)llNum;
}
/* Not allowed to assume that 64-bit division is OK, so use a
software division routine.
*/
do
{
uint64_t ullQuotient;
uint32_t ulRemainder;
/* Note: RedUint64DivMod32() is smart enough to use normal division
once ullNumericVal <= UINT32_MAX.
*/
ullQuotient = RedUint64DivMod32(ullNum, 10U, &ulRemainder);
ach[ulDigits] = gacDigits[ulRemainder];
ullNum = ullQuotient;
ulDigits++;
}
while(ullNum > 0U);
if(fSign)
{
ach[ulDigits] = '-';
ulDigits++;
}
ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
}
return ulLen;
}
/** @brief Format an unsigned 32-bit integer as an ASCII string as decimal or
hex.
@note If the output buffer length is exhausted, the result will *not* be
null-terminated.
@param pcBuffer The output buffer
@param ulBufferLen The output buffer length
@param ulNum The 32-bit unsigned number to convert
@param fHex If true, format as hex; if false, decimal.
@param ulFillLen The fill length, if any
@param cFill The fill character to use
@return The length of the string.
*/
static uint32_t ULtoA(
char *pcBuffer,
uint32_t ulBufferLen,
uint32_t ulNum,
bool fHex,
uint32_t ulFillLen,
char cFill)
{
uint32_t ulLen;
if(pcBuffer == NULL)
{
REDERROR();
ulLen = 0U;
}
else
{
char ach[11U]; /* Big enough for a uint32_t in radix 10 */
uint32_t ulDigits = 0U;
uint32_t ulNumericVal = ulNum;
uint32_t ulRadix = fHex ? 16U : 10U;
do
{
ach[ulDigits] = gacDigits[ulNumericVal % ulRadix];
ulNumericVal = ulNumericVal / ulRadix;
ulDigits++;
}
while(ulNumericVal > 0U);
ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
}
return ulLen;
}
/** @brief Format an unsigned 64-bit integer as an ASCII string as decimal or
hex.
@note If the output buffer length is exhausted, the result will *not* be
null-terminated.
@param pcBuffer The output buffer.
@param ulBufferLen The output buffer length.
@param ullNum The unsigned 64-bit number to convert.
@param fHex If true, format as hex; if false, decimal.
@param ulFillLen The fill length, if any.
@param cFill The fill character to use.
@return The length of the string.
*/
static uint32_t ULLtoA(
char *pcBuffer,
uint32_t ulBufferLen,
uint64_t ullNum,
bool fHex,
uint32_t ulFillLen,
char cFill)
{
uint32_t ulLen;
if(pcBuffer == NULL)
{
REDERROR();
ulLen = 0U;
}
else
{
char ach[21U]; /* Big enough for a uint64_t in radix 10 */
uint32_t ulDigits = 0U;
uint64_t ullNumericVal = ullNum;
if(fHex)
{
/* We can figure out the digits using bit operations.
*/
do
{
ach[ulDigits] = gacDigits[ullNumericVal & 15U];
ullNumericVal >>= 4U;
ulDigits++;
}
while(ullNumericVal > 0U);
}
else
{
/* Not allowed to assume that 64-bit division is OK, so use a
software division routine.
*/
do
{
uint64_t ullQuotient;
uint32_t ulRemainder;
/* Note: RedUint64DivMod32() is smart enough to use normal division
once ullNumericVal <= UINT32_MAX.
*/
ullQuotient = RedUint64DivMod32(ullNumericVal, 10U, &ulRemainder);
ach[ulDigits] = gacDigits[ulRemainder];
ullNumericVal = ullQuotient;
ulDigits++;
}
while(ullNumericVal > 0U);
}
ulLen = FinishToA(ach, ulDigits, pcBuffer, ulBufferLen, ulFillLen, cFill);
}
return ulLen;
}
/** @brief Finish converting a number into an ASCII string representing that
number.
This helper function contains common logic that needs to run at the end of
all the "toA" functions. It adds the fill character and reverses the digits
string.
@param pcDigits The digits (and sign) for the ASCII string, in reverse
order as they were computed.
@param ulDigits The number of digit characters.
@param pcOutBuffer The output buffer.
@param ulBufferLen The length of the output buffer.
@param ulFillLen The fill length. If the number string is shorter than
this, the remaining bytes are filled with @p cFill.
@param cFill The fill character.
@return The length of @p pcOutBuffer.
*/
static uint32_t FinishToA(
const char *pcDigits,
uint32_t ulDigits,
char *pcOutBuffer,
uint32_t ulBufferLen,
uint32_t ulFillLen,
char cFill)
{
uint32_t ulIdx = 0U;
uint32_t ulDigitIdx = ulDigits;
/* user may have asked for a fill char
*/
if(ulFillLen > ulDigits)
{
uint32_t ulFillRem = ulFillLen - ulDigits;
while((ulFillRem > 0U) && (ulIdx < ulBufferLen))
{
pcOutBuffer[ulIdx] = cFill;
ulIdx++;
ulFillRem--;
}
}
/* reverse the string
*/
while((ulDigitIdx > 0) && (ulIdx < ulBufferLen))
{
ulDigitIdx--;
pcOutBuffer[ulIdx] = pcDigits[ulDigitIdx];
ulIdx++;
}
if(ulIdx < ulBufferLen)
{
pcOutBuffer[ulIdx] = '\0';
}
return ulIdx;
}
|