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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
|
/* $Id: strformatrt.cpp $ */
/** @file
* IPRT - IPRT String Formatter Extensions.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_STRING
#include <iprt/string.h>
#ifndef RT_NO_EXPORT_SYMBOL
# define RT_NO_EXPORT_SYMBOL /* don't slurp <linux/module.h> which then again
slurps arch-specific headers defining symbols */
#endif
#include "internal/iprt.h"
#include <iprt/log.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include <iprt/stdarg.h>
#ifdef IN_RING3
# include <iprt/errcore.h>
# include <iprt/thread.h>
# include <iprt/utf16.h>
#endif
#include <iprt/ctype.h>
#include <iprt/time.h>
#include <iprt/net.h>
#include <iprt/path.h>
#include <iprt/asm.h>
#define STRFORMAT_WITH_X86
#ifdef STRFORMAT_WITH_X86
# include <iprt/x86.h>
#endif
#include "internal/string.h"
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
static char g_szHexDigits[17] = "0123456789abcdef";
#ifdef IN_RING3
static char g_szHexDigitsUpper[17] = "0123456789ABCDEF";
#endif
/**
* Helper that formats a 16-bit hex word in a IPv6 address.
*
* @returns Length in chars.
* @param pszDst The output buffer. Written from the start.
* @param uWord The word to format as hex.
*/
static size_t rtstrFormatIPv6HexWord(char *pszDst, uint16_t uWord)
{
size_t off;
uint16_t cDigits;
if (uWord & UINT16_C(0xff00))
cDigits = uWord & UINT16_C(0xf000) ? 4 : 3;
else
cDigits = uWord & UINT16_C(0x00f0) ? 2 : 1;
off = 0;
switch (cDigits)
{
case 4: pszDst[off++] = g_szHexDigits[(uWord >> 12) & 0xf]; RT_FALL_THRU();
case 3: pszDst[off++] = g_szHexDigits[(uWord >> 8) & 0xf]; RT_FALL_THRU();
case 2: pszDst[off++] = g_szHexDigits[(uWord >> 4) & 0xf]; RT_FALL_THRU();
case 1: pszDst[off++] = g_szHexDigits[(uWord >> 0) & 0xf];
break;
}
pszDst[off] = '\0';
return off;
}
/**
* Helper function to format IPv6 address according to RFC 5952.
*
* @returns The number of bytes formatted.
* @param pfnOutput Pointer to output function.
* @param pvArgOutput Argument for the output function.
* @param pIpv6Addr IPv6 address
*/
static size_t rtstrFormatIPv6(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PCRTNETADDRIPV6 pIpv6Addr)
{
size_t cch; /* result */
bool fEmbeddedIpv4;
size_t cwHexPart;
size_t cwLongestZeroRun;
size_t iLongestZeroStart;
size_t idx;
char szHexWord[8];
Assert(pIpv6Addr != NULL);
/*
* Check for embedded IPv4 address.
*
* IPv4-compatible - ::11.22.33.44 (obsolete)
* IPv4-mapped - ::ffff:11.22.33.44
* IPv4-translated - ::ffff:0:11.22.33.44 (RFC 2765)
*/
fEmbeddedIpv4 = false;
cwHexPart = RT_ELEMENTS(pIpv6Addr->au16);
if ( pIpv6Addr->au64[0] == 0
&& ( ( pIpv6Addr->au32[2] == 0
&& pIpv6Addr->au32[3] != 0
&& pIpv6Addr->au32[3] != RT_H2BE_U32_C(1) )
|| pIpv6Addr->au32[2] == RT_H2BE_U32_C(0x0000ffff)
|| pIpv6Addr->au32[2] == RT_H2BE_U32_C(0xffff0000) ) )
{
fEmbeddedIpv4 = true;
cwHexPart -= 2;
}
/*
* Find the longest sequences of two or more zero words.
*/
cwLongestZeroRun = 0;
iLongestZeroStart = 0;
for (idx = 0; idx < cwHexPart; idx++)
if (pIpv6Addr->au16[idx] == 0)
{
size_t iZeroStart = idx;
size_t cwZeroRun;
do
idx++;
while (idx < cwHexPart && pIpv6Addr->au16[idx] == 0);
cwZeroRun = idx - iZeroStart;
if (cwZeroRun > 1 && cwZeroRun > cwLongestZeroRun)
{
cwLongestZeroRun = cwZeroRun;
iLongestZeroStart = iZeroStart;
if (cwZeroRun >= cwHexPart - idx)
break;
}
}
/*
* Do the formatting.
*/
cch = 0;
if (cwLongestZeroRun == 0)
{
for (idx = 0; idx < cwHexPart; ++idx)
{
if (idx > 0)
cch += pfnOutput(pvArgOutput, ":", 1);
cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
}
if (fEmbeddedIpv4)
cch += pfnOutput(pvArgOutput, ":", 1);
}
else
{
const size_t iLongestZeroEnd = iLongestZeroStart + cwLongestZeroRun;
if (iLongestZeroStart == 0)
cch += pfnOutput(pvArgOutput, ":", 1);
else
for (idx = 0; idx < iLongestZeroStart; ++idx)
{
cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
cch += pfnOutput(pvArgOutput, ":", 1);
}
if (iLongestZeroEnd == cwHexPart)
cch += pfnOutput(pvArgOutput, ":", 1);
else
{
for (idx = iLongestZeroEnd; idx < cwHexPart; ++idx)
{
cch += pfnOutput(pvArgOutput, ":", 1);
cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
}
if (fEmbeddedIpv4)
cch += pfnOutput(pvArgOutput, ":", 1);
}
}
if (fEmbeddedIpv4)
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%u.%u.%u.%u",
pIpv6Addr->au8[12],
pIpv6Addr->au8[13],
pIpv6Addr->au8[14],
pIpv6Addr->au8[15]);
return cch;
}
/**
* Callback to format iprt formatting extentions.
* See @ref pg_rt_str_format for a reference on the format types.
*
* @returns The number of bytes formatted.
* @param pfnOutput Pointer to output function.
* @param pvArgOutput Argument for the output function.
* @param ppszFormat Pointer to the format string pointer. Advance this till the char
* after the format specifier.
* @param pArgs Pointer to the argument list. Use this to fetch the arguments.
* @param cchWidth Format Width. -1 if not specified.
* @param cchPrecision Format Precision. -1 if not specified.
* @param fFlags Flags (RTSTR_NTFS_*).
* @param chArgSize The argument size specifier, 'l' or 'L'.
*/
DECLHIDDEN(size_t) rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs,
int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
{
const char *pszFormatOrg = *ppszFormat;
char ch = *(*ppszFormat)++;
size_t cch;
char szBuf[80];
if (ch == 'R')
{
ch = *(*ppszFormat)++;
switch (ch)
{
/*
* Groups 1 and 2.
*/
case 'T':
case 'G':
case 'H':
case 'R':
case 'C':
case 'I':
case 'X':
case 'U':
case 'K':
{
/*
* Interpret the type.
*/
typedef enum
{
RTSF_INT,
RTSF_INTW,
RTSF_BOOL,
RTSF_FP16,
RTSF_FP32,
RTSF_FP64,
RTSF_IPV4,
RTSF_IPV6,
RTSF_MAC,
RTSF_NETADDR,
RTSF_UUID
} RTSF;
static const struct
{
uint8_t cch; /**< the length of the string. */
char sz[10]; /**< the part following 'R'. */
uint8_t cb; /**< the size of the type. */
uint8_t u8Base; /**< the size of the type. */
RTSF enmFormat; /**< The way to format it. */
uint16_t fFlags; /**< additional RTSTR_F_* flags. */
}
/** Sorted array of types, looked up using binary search! */
s_aTypes[] =
{
#define STRMEM(str) sizeof(str) - 1, str
{ STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Cp"), sizeof(RTCCPHYS), 16, RTSF_INTW, 0 },
{ STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
{ STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
{ STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
{ STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
{ STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
{ STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
{ STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
{ STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
{ STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
{ STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
{ STRMEM("Hr"), sizeof(RTHCUINTREG), 16, RTSF_INTW, 0 },
{ STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
{ STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
{ STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
{ STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Kv"), sizeof(RTHCPTR), 16, RTSF_INT, RTSTR_F_OBFUSCATE_PTR },
{ STRMEM("Rv"), sizeof(RTRCPTR), 16, RTSF_INTW, 0 },
{ STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
{ STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
{ STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
{ STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
{ STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
{ STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
{ STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
{ STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
{ STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
{ STRMEM("Tmac"), sizeof(PCRTMAC), 16, RTSF_MAC, 0 },
{ STRMEM("Tnaddr"), sizeof(PCRTNETADDR), 10, RTSF_NETADDR,0 },
{ STRMEM("Tnaipv4"), sizeof(RTNETADDRIPV4), 10, RTSF_IPV4, 0 },
{ STRMEM("Tnaipv6"), sizeof(PCRTNETADDRIPV6),16, RTSF_IPV6, 0 },
{ STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
{ STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
{ STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
{ STRMEM("Treg"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
{ STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
{ STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
{ STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
{ STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
{ STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
{ STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
{ STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
{ STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
{ STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
{ STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
{ STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
{ STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
{ STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
{ STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
{ STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
{ STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
{ STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
{ STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
#undef STRMEM
};
static const char s_szNull[] = "<NULL>";
const char *pszType = *ppszFormat - 1;
int iStart = 0;
int iEnd = RT_ELEMENTS(s_aTypes) - 1;
int i = RT_ELEMENTS(s_aTypes) / 2;
union
{
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
RTR0INTPTR uR0Ptr;
RTFAR16 fp16;
RTFAR32 fp32;
RTFAR64 fp64;
bool fBool;
PCRTMAC pMac;
RTNETADDRIPV4 Ipv4Addr;
PCRTNETADDRIPV6 pIpv6Addr;
PCRTNETADDR pNetAddr;
PCRTUUID pUuid;
} u;
AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
RT_NOREF_PV(chArgSize);
/*
* Lookup the type - binary search.
*/
for (;;)
{
int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
if (!iDiff)
break;
if (iEnd == iStart)
{
AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
return 0;
}
if (iDiff < 0)
iEnd = i - 1;
else
iStart = i + 1;
if (iEnd < iStart)
{
AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
return 0;
}
i = iStart + (iEnd - iStart) / 2;
}
/*
* Advance the format string and merge flags.
*/
*ppszFormat += s_aTypes[i].cch - 1;
fFlags |= s_aTypes[i].fFlags;
/*
* Fetch the argument.
* It's important that a signed value gets sign-extended up to 64-bit.
*/
RT_ZERO(u);
if (fFlags & RTSTR_F_VALSIGNED)
{
switch (s_aTypes[i].cb)
{
case sizeof(int8_t):
u.i64 = va_arg(*pArgs, /*int8_t*/int);
fFlags |= RTSTR_F_8BIT;
break;
case sizeof(int16_t):
u.i64 = va_arg(*pArgs, /*int16_t*/int);
fFlags |= RTSTR_F_16BIT;
break;
case sizeof(int32_t):
u.i64 = va_arg(*pArgs, int32_t);
fFlags |= RTSTR_F_32BIT;
break;
case sizeof(int64_t):
u.i64 = va_arg(*pArgs, int64_t);
fFlags |= RTSTR_F_64BIT;
break;
default:
AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
break;
}
}
else
{
switch (s_aTypes[i].cb)
{
case sizeof(uint8_t):
u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
fFlags |= RTSTR_F_8BIT;
break;
case sizeof(uint16_t):
u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
fFlags |= RTSTR_F_16BIT;
break;
case sizeof(uint32_t):
u.u32 = va_arg(*pArgs, uint32_t);
fFlags |= RTSTR_F_32BIT;
break;
case sizeof(uint64_t):
u.u64 = va_arg(*pArgs, uint64_t);
fFlags |= RTSTR_F_64BIT;
break;
case sizeof(RTFAR32):
u.fp32 = va_arg(*pArgs, RTFAR32);
break;
case sizeof(RTFAR64):
u.fp64 = va_arg(*pArgs, RTFAR64);
break;
default:
AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
break;
}
}
#ifndef DEBUG
/*
* For now don't show the address.
*/
if (fFlags & RTSTR_F_OBFUSCATE_PTR)
{
cch = rtStrFormatKernelAddress(szBuf, sizeof(szBuf), u.uR0Ptr, cchWidth, cchPrecision, fFlags);
return pfnOutput(pvArgOutput, szBuf, cch);
}
#endif
/*
* Format the output.
*/
switch (s_aTypes[i].enmFormat)
{
case RTSF_INT:
{
cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
break;
}
/* hex which defaults to max width. */
case RTSF_INTW:
{
Assert(s_aTypes[i].u8Base == 16);
if (cchWidth < 0)
{
cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
fFlags |= RTSTR_F_ZEROPAD;
}
cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
break;
}
case RTSF_BOOL:
{
static const char s_szTrue[] = "true ";
static const char s_szFalse[] = "false";
if (u.u64 == 1)
return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
if (u.u64 == 0)
return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
/* invalid boolean value */
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
}
case RTSF_FP16:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
Assert(cch == 4);
szBuf[4] = ':';
cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
Assert(cch == 4);
cch = 4 + 1 + 4;
break;
}
case RTSF_FP32:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
Assert(cch == 4);
szBuf[4] = ':';
cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
Assert(cch == 8);
cch = 4 + 1 + 8;
break;
}
case RTSF_FP64:
{
fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
Assert(cch == 4);
szBuf[4] = ':';
cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
Assert(cch == 16);
cch = 4 + 1 + 16;
break;
}
case RTSF_IPV4:
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%u.%u.%u.%u",
u.Ipv4Addr.au8[0],
u.Ipv4Addr.au8[1],
u.Ipv4Addr.au8[2],
u.Ipv4Addr.au8[3]);
case RTSF_IPV6:
{
if (VALID_PTR(u.pIpv6Addr))
return rtstrFormatIPv6(pfnOutput, pvArgOutput, u.pIpv6Addr);
return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
}
case RTSF_MAC:
{
if (VALID_PTR(u.pMac))
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%02x:%02x:%02x:%02x:%02x:%02x",
u.pMac->au8[0],
u.pMac->au8[1],
u.pMac->au8[2],
u.pMac->au8[3],
u.pMac->au8[4],
u.pMac->au8[5]);
return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
}
case RTSF_NETADDR:
{
if (VALID_PTR(u.pNetAddr))
{
switch (u.pNetAddr->enmType)
{
case RTNETADDRTYPE_IPV4:
if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%u.%u.%u.%u",
u.pNetAddr->uAddr.IPv4.au8[0],
u.pNetAddr->uAddr.IPv4.au8[1],
u.pNetAddr->uAddr.IPv4.au8[2],
u.pNetAddr->uAddr.IPv4.au8[3]);
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%u.%u.%u.%u:%u",
u.pNetAddr->uAddr.IPv4.au8[0],
u.pNetAddr->uAddr.IPv4.au8[1],
u.pNetAddr->uAddr.IPv4.au8[2],
u.pNetAddr->uAddr.IPv4.au8[3],
u.pNetAddr->uPort);
case RTNETADDRTYPE_IPV6:
if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
return rtstrFormatIPv6(pfnOutput, pvArgOutput, &u.pNetAddr->uAddr.IPv6);
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"[%RTnaipv6]:%u",
&u.pNetAddr->uAddr.IPv6,
u.pNetAddr->uPort);
case RTNETADDRTYPE_MAC:
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%02x:%02x:%02x:%02x:%02x:%02x",
u.pNetAddr->uAddr.Mac.au8[0],
u.pNetAddr->uAddr.Mac.au8[1],
u.pNetAddr->uAddr.Mac.au8[2],
u.pNetAddr->uAddr.Mac.au8[3],
u.pNetAddr->uAddr.Mac.au8[4],
u.pNetAddr->uAddr.Mac.au8[5]);
default:
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"unsupported-netaddr-type=%u", u.pNetAddr->enmType);
}
}
return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
}
case RTSF_UUID:
{
if (VALID_PTR(u.pUuid))
{
/* cannot call RTUuidToStr because of GC/R0. */
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
RT_H2LE_U32(u.pUuid->Gen.u32TimeLow),
RT_H2LE_U16(u.pUuid->Gen.u16TimeMid),
RT_H2LE_U16(u.pUuid->Gen.u16TimeHiAndVersion),
u.pUuid->Gen.u8ClockSeqHiAndReserved,
u.pUuid->Gen.u8ClockSeqLow,
u.pUuid->Gen.au8Node[0],
u.pUuid->Gen.au8Node[1],
u.pUuid->Gen.au8Node[2],
u.pUuid->Gen.au8Node[3],
u.pUuid->Gen.au8Node[4],
u.pUuid->Gen.au8Node[5]);
}
return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
}
default:
AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
return 0;
}
/*
* Finally, output the formatted string and return.
*/
return pfnOutput(pvArgOutput, szBuf, cch);
}
/* Group 3 */
/*
* Base name printing, big endian UTF-16.
*/
case 'b':
{
switch (*(*ppszFormat)++)
{
case 'n':
{
const char *pszLastSep;
const char *psz = pszLastSep = va_arg(*pArgs, const char *);
if (!VALID_PTR(psz))
return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
while ((ch = *psz) != '\0')
{
if (RTPATH_IS_SEP(ch))
{
do
psz++;
while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
if (!ch)
break;
pszLastSep = psz;
}
psz++;
}
return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
}
/* %lRbs */
case 's':
if (chArgSize == 'l')
{
/* utf-16BE -> utf-8 */
int cchStr;
PCRTUTF16 pwszStr = va_arg(*pArgs, PRTUTF16);
if (RT_VALID_PTR(pwszStr))
{
cchStr = 0;
while (cchStr < cchPrecision && pwszStr[cchStr] != '\0')
cchStr++;
}
else
{
static RTUTF16 s_wszBigNull[] =
{
RT_H2BE_U16_C((uint16_t)'<'), RT_H2BE_U16_C((uint16_t)'N'), RT_H2BE_U16_C((uint16_t)'U'),
RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'>'), '\0'
};
pwszStr = s_wszBigNull;
cchStr = RT_ELEMENTS(s_wszBigNull) - 1;
}
cch = 0;
if (!(fFlags & RTSTR_F_LEFT))
while (--cchWidth >= cchStr)
cch += pfnOutput(pvArgOutput, " ", 1);
cchWidth -= cchStr;
while (cchStr-- > 0)
{
/** @todo \#ifndef IN_RC*/
#ifdef IN_RING3
RTUNICP Cp = 0;
RTUtf16BigGetCpEx(&pwszStr, &Cp);
char *pszEnd = RTStrPutCp(szBuf, Cp);
*pszEnd = '\0';
cch += pfnOutput(pvArgOutput, szBuf, pszEnd - szBuf);
#else
szBuf[0] = (char)(*pwszStr++ >> 8);
cch += pfnOutput(pvArgOutput, szBuf, 1);
#endif
}
while (--cchWidth >= 0)
cch += pfnOutput(pvArgOutput, " ", 1);
return cch;
}
RT_FALL_THRU();
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
break;
}
break;
}
/*
* Pretty function / method name printing.
*/
case 'f':
{
switch (*(*ppszFormat)++)
{
/*
* Pretty function / method name printing.
* This isn't 100% right (see classic signal prototype) and it assumes
* standardized names, but it'll do for today.
*/
case 'n':
{
const char *pszStart;
const char *psz = pszStart = va_arg(*pArgs, const char *);
int cAngle = 0;
if (!VALID_PTR(psz))
return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
while ((ch = *psz) != '\0' && ch != '(')
{
if (RT_C_IS_BLANK(ch))
{
psz++;
while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
psz++;
if (ch && cAngle == 0)
pszStart = psz;
}
else if (ch == '(')
break;
else if (ch == '<')
{
cAngle++;
psz++;
}
else if (ch == '>')
{
cAngle--;
psz++;
}
else
psz++;
}
return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
}
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
break;
}
break;
}
/*
* hex dumping, COM/XPCOM, human readable sizes.
*/
case 'h':
{
ch = *(*ppszFormat)++;
switch (ch)
{
/*
* Hex stuff.
*/
case 'x':
{
uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
if (cchPrecision < 0)
cchPrecision = 16;
if (pu8)
{
switch (*(*ppszFormat)++)
{
/*
* Regular hex dump.
*/
case 'd':
{
int off = 0;
cch = 0;
if (cchWidth <= 0)
cchWidth = 16;
while (off < cchPrecision)
{
int i;
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ",
pu8[i]);
while (i++ < cchWidth)
cch += pfnOutput(pvArgOutput, " ", 3);
cch += pfnOutput(pvArgOutput, " ", 1);
for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
{
uint8_t u8 = pu8[i];
cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
}
/* next */
pu8 += cchWidth;
off += cchWidth;
}
return cch;
}
/*
* Regular hex dump with dittoing.
*/
case 'D':
{
int offEndDupCheck;
int cDuplicates = 0;
int off = 0;
cch = 0;
if (cchWidth <= 0)
cchWidth = 16;
offEndDupCheck = cchPrecision - cchWidth;
while (off < cchPrecision)
{
int i;
if ( off >= offEndDupCheck
|| off <= 0
|| memcmp(pu8, pu8 - cchWidth, cchWidth) != 0
|| ( cDuplicates == 0
&& ( off + cchWidth >= offEndDupCheck
|| memcmp(pu8 + cchWidth, pu8, cchWidth) != 0)) )
{
if (cDuplicates > 0)
{
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"\n%.*s **** <ditto x %u>",
sizeof(pu8) * 2, "****************", cDuplicates);
cDuplicates = 0;
}
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
"%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
off + i < cchPrecision ? !(i & 7) && i
? "-%02x" : " %02x" : " ",
pu8[i]);
while (i++ < cchWidth)
cch += pfnOutput(pvArgOutput, " ", 3);
cch += pfnOutput(pvArgOutput, " ", 1);
for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
{
uint8_t u8 = pu8[i];
cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
}
}
else
cDuplicates++;
/* next */
pu8 += cchWidth;
off += cchWidth;
}
return cch;
}
/*
* Hex string.
*/
case 's':
{
if (cchPrecision-- > 0)
{
cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
for (; cchPrecision > 0; cchPrecision--, pu8++)
cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
return cch;
}
break;
}
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
break;
}
}
else
return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
break;
}
#ifdef IN_RING3
/*
* XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
* ASSUMES: If Windows Then COM else XPCOM.
*/
case 'r':
{
uint32_t hrc = va_arg(*pArgs, uint32_t);
PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
switch (*(*ppszFormat)++)
{
case 'c':
return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
case 'f':
return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
return 0;
}
break;
}
#endif /* IN_RING3 */
case 'c':
case 'u':
{
unsigned i;
ssize_t cchBuf;
uint64_t uValue;
uint64_t uFraction = 0;
const char *pszPrefix = NULL;
unsigned cchFixedPart;
char ch2 = *(*ppszFormat)++;
AssertMsgReturn(ch2 == 'b' || ch2 == 'i', ("invalid type '%.10s'!\n", pszFormatOrg), 0);
uValue = va_arg(*pArgs, uint64_t);
if (!(fFlags & RTSTR_F_PRECISION))
cchPrecision = 1;
else if (cchPrecision > 3)
cchPrecision = 3;
else if (cchPrecision < 0)
cchPrecision = 0;
cchFixedPart = cchPrecision + (cchPrecision != 0) + (ch == 'c');
if (ch2 == 'b')
{
static const struct
{
const char *pszPrefix;
uint8_t cShift;
uint64_t cbMin;
uint64_t cbMinZeroPrecision;
} s_aUnits[] =
{
{ "Ei", 60, _1E, _1E*2 },
{ "Pi", 50, _1P, _1P*2 },
{ "Ti", 40, _1T, _1T*2 },
{ "Gi", 30, _1G, _1G64*2 },
{ "Mi", 20, _1M, _1M*2 },
{ "Ki", 10, _1K, _1K*2 },
};
for (i = 0; i < RT_ELEMENTS(s_aUnits); i++)
if ( uValue >= s_aUnits[i].cbMin
&& (cchPrecision > 0 || uValue >= s_aUnits[i].cbMinZeroPrecision))
{
if (cchPrecision != 0)
{
uFraction = uValue & (RT_BIT_64(s_aUnits[i].cShift) - 1);
uFraction *= cchPrecision == 1 ? 10 : cchPrecision == 2 ? 100 : 1000;
uFraction >>= s_aUnits[i].cShift;
}
uValue >>= s_aUnits[i].cShift;
pszPrefix = s_aUnits[i].pszPrefix;
cchFixedPart += 2;
break;
}
}
else
{
static const struct
{
const char *pszPrefix;
uint64_t cbFactor;
uint64_t cbMinZeroPrecision;
} s_aUnits[] =
{
{ "E", UINT64_C(1000000000000000000), UINT64_C(1010000000000000000), },
{ "P", UINT64_C(1000000000000000), UINT64_C(1010000000000000), },
{ "T", UINT64_C(1000000000000), UINT64_C(1010000000000), },
{ "G", UINT64_C(1000000000), UINT64_C(1010000000), },
{ "M", UINT64_C(1000000), UINT64_C(1010000), },
{ "k", UINT64_C(1000), UINT64_C(1010), },
};
for (i = 0; i < RT_ELEMENTS(s_aUnits); i++)
if ( uValue >= s_aUnits[i].cbFactor
&& (cchPrecision > 0 || uValue >= s_aUnits[i].cbMinZeroPrecision))
{
if (cchPrecision == 0)
uValue /= s_aUnits[i].cbFactor;
else
{
uFraction = uValue % s_aUnits[i].cbFactor;
uValue = uValue / s_aUnits[i].cbFactor;
uFraction *= cchPrecision == 1 ? 10 : cchPrecision == 2 ? 100 : 1000;
uFraction += s_aUnits[i].cbFactor >> 1;
uFraction /= s_aUnits[i].cbFactor;
}
pszPrefix = s_aUnits[i].pszPrefix;
cchFixedPart += 1;
break;
}
}
cchBuf = RTStrFormatU64(szBuf, sizeof(szBuf), uValue, 10, 0, 0, 0);
if (pszPrefix)
{
if (cchPrecision)
{
szBuf[cchBuf++] = '.';
cchBuf += RTStrFormatU64(&szBuf[cchBuf], sizeof(szBuf) - cchBuf, uFraction, 10, cchPrecision, 0,
RTSTR_F_ZEROPAD | RTSTR_F_WIDTH);
}
szBuf[cchBuf++] = *pszPrefix++;
if (*pszPrefix)
szBuf[cchBuf++] = *pszPrefix;
}
if (ch == 'c')
szBuf[cchBuf++] = 'B';
szBuf[cchBuf] = '\0';
cch = 0;
if ((fFlags & RTSTR_F_WIDTH) && !(fFlags & RTSTR_F_LEFT))
while (cchBuf < cchWidth)
{
cch += pfnOutput(pvArgOutput, fFlags & RTSTR_F_ZEROPAD ? "0" : " ", 1);
cchWidth--;
}
cch += pfnOutput(pvArgOutput, szBuf, cchBuf);
return cch;
}
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
return 0;
}
break;
}
/*
* iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
*/
case 'r':
{
int rc = va_arg(*pArgs, int);
#ifdef IN_RING3 /* we don't want this anywhere else yet. */
PCRTSTATUSMSG pMsg = RTErrGet(rc);
switch (*(*ppszFormat)++)
{
case 'c':
return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
case 's':
return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
case 'f':
return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
return 0;
}
#else /* !IN_RING3 */
switch (*(*ppszFormat)++)
{
case 'c':
case 's':
case 'f':
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
return 0;
}
#endif /* !IN_RING3 */
break;
}
#if defined(IN_RING3)
/*
* Windows status code: %Rwc, %Rwf, %Rwa
*/
case 'w':
{
long rc = va_arg(*pArgs, long);
# if defined(RT_OS_WINDOWS)
PCRTWINERRMSG pMsg = RTErrWinGet(rc);
# endif
switch (*(*ppszFormat)++)
{
# if defined(RT_OS_WINDOWS)
case 'c':
return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
case 'f':
return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
# else
case 'c':
case 'f':
case 'a':
return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
# endif
default:
AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
return 0;
}
break;
}
#endif /* IN_RING3 */
/*
* Group 4, structure dumpers.
*/
case 'D':
{
/*
* Interpret the type.
*/
typedef enum
{
RTST_TIMESPEC
} RTST;
/** Set if it's a pointer */
#define RTST_FLAGS_POINTER RT_BIT(0)
static const struct
{
uint8_t cch; /**< the length of the string. */
char sz[16-2]; /**< the part following 'R'. */
uint8_t cb; /**< the size of the argument. */
uint8_t fFlags; /**< RTST_FLAGS_* */
RTST enmType; /**< The structure type. */
}
/** Sorted array of types, looked up using binary search! */
s_aTypes[] =
{
#define STRMEM(str) sizeof(str) - 1, str
{ STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
#undef STRMEM
};
const char *pszType = *ppszFormat - 1;
int iStart = 0;
int iEnd = RT_ELEMENTS(s_aTypes) - 1;
int i = RT_ELEMENTS(s_aTypes) / 2;
union
{
const void *pv;
uint64_t u64;
PCRTTIMESPEC pTimeSpec;
} u;
AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
/*
* Lookup the type - binary search.
*/
for (;;)
{
int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
if (!iDiff)
break;
if (iEnd == iStart)
{
AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
return 0;
}
if (iDiff < 0)
iEnd = i - 1;
else
iStart = i + 1;
if (iEnd < iStart)
{
AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
return 0;
}
i = iStart + (iEnd - iStart) / 2;
}
*ppszFormat += s_aTypes[i].cch - 1;
/*
* Fetch the argument.
*/
u.u64 = 0;
switch (s_aTypes[i].cb)
{
case sizeof(const void *):
u.pv = va_arg(*pArgs, const void *);
break;
default:
AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
break;
}
/*
* If it's a pointer, we'll check if it's valid before going on.
*/
if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
/*
* Format the output.
*/
switch (s_aTypes[i].enmType)
{
case RTST_TIMESPEC:
return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
default:
AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
break;
}
break;
}
#ifdef IN_RING3
/*
* Group 5, XML / HTML, JSON and URI escapers.
*/
case 'M':
{
char chWhat = (*ppszFormat)[0];
if (chWhat == 'a' || chWhat == 'e')
{
/* XML attributes and element values. */
bool fAttr = chWhat == 'a';
char chType = (*ppszFormat)[1];
*ppszFormat += 2;
switch (chType)
{
case 's':
{
static const char s_szElemEscape[] = "<>&\"'";
static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
size_t cchOutput = 0;
const char *pszStr = va_arg(*pArgs, char *);
ssize_t cchStr;
ssize_t offCur;
ssize_t offLast;
if (!VALID_PTR(pszStr))
pszStr = "<NULL>";
cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
if (fAttr)
cchOutput += pfnOutput(pvArgOutput, "\"", 1);
if (!(fFlags & RTSTR_F_LEFT))
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, " ", 1);
offLast = offCur = 0;
while (offCur < cchStr)
{
if (memchr(pszEscape, pszStr[offCur], cchEscape))
{
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
switch (pszStr[offCur])
{
case '<': cchOutput += pfnOutput(pvArgOutput, "<", 4); break;
case '>': cchOutput += pfnOutput(pvArgOutput, ">", 4); break;
case '&': cchOutput += pfnOutput(pvArgOutput, "&", 5); break;
case '\'': cchOutput += pfnOutput(pvArgOutput, "'", 6); break;
case '"': cchOutput += pfnOutput(pvArgOutput, """, 6); break;
case '\n': cchOutput += pfnOutput(pvArgOutput, "
", 5); break;
case '\r': cchOutput += pfnOutput(pvArgOutput, "
", 5); break;
default:
AssertFailed();
}
offLast = offCur + 1;
}
offCur++;
}
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, " ", 1);
if (fAttr)
cchOutput += pfnOutput(pvArgOutput, "\"", 1);
return cchOutput;
}
default:
AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
}
}
else if (chWhat == 'j')
{
/* JSON string escaping. */
char const chType = (*ppszFormat)[1];
*ppszFormat += 2;
switch (chType)
{
case 's':
{
const char *pszStr = va_arg(*pArgs, char *);
size_t cchOutput;
ssize_t cchStr;
ssize_t offCur;
ssize_t offLast;
if (!VALID_PTR(pszStr))
pszStr = "<NULL>";
cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
cchOutput = pfnOutput(pvArgOutput, "\"", 1);
if (!(fFlags & RTSTR_F_LEFT))
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, " ", 1);
offLast = offCur = 0;
while (offCur < cchStr)
{
unsigned int const uch = pszStr[offCur];
if ( uch >= 0x5d
|| (uch >= 0x20 && uch != 0x22 && uch != 0x5c))
offCur++;
else
{
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
switch ((char)uch)
{
case '"': cchOutput += pfnOutput(pvArgOutput, "\\\"", 2); break;
case '\\': cchOutput += pfnOutput(pvArgOutput, "\\\\", 2); break;
case '/': cchOutput += pfnOutput(pvArgOutput, "\\/", 2); break;
case '\b': cchOutput += pfnOutput(pvArgOutput, "\\b", 2); break;
case '\f': cchOutput += pfnOutput(pvArgOutput, "\\f", 2); break;
case '\n': cchOutput += pfnOutput(pvArgOutput, "\\n", 2); break;
case '\t': cchOutput += pfnOutput(pvArgOutput, "\\t", 2); break;
default:
{
RTUNICP uc = 0xfffd; /* replacement character */
const char *pszCur = &pszStr[offCur];
int rc = RTStrGetCpEx(&pszCur, &uc);
if (RT_SUCCESS(rc))
offCur += pszCur - &pszStr[offCur] - 1;
if (uc >= 0xfffe)
uc = 0xfffd; /* replacement character */
szBuf[0] = '\\';
szBuf[1] = 'u';
szBuf[2] = g_szHexDigits[(uc >> 12) & 0xf];
szBuf[3] = g_szHexDigits[(uc >> 8) & 0xf];
szBuf[4] = g_szHexDigits[(uc >> 4) & 0xf];
szBuf[5] = g_szHexDigits[ uc & 0xf];
szBuf[6] = '\0';
cchOutput += pfnOutput(pvArgOutput, szBuf, 6);
break;
}
}
offLast = ++offCur;
}
}
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, " ", 1);
cchOutput += pfnOutput(pvArgOutput, "\"", 1);
return cchOutput;
}
default:
AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
}
}
else if (chWhat == 'p')
{
/* Percent encoded string (RTC-3986). */
char const chVariant = (*ppszFormat)[1];
char const chAddSafe = chVariant == 'p' ? '/'
: chVariant == 'q' ? '+' /* '+' in queries is problematic, so no escape. */
: '~' /* whatever */;
size_t cchOutput = 0;
const char *pszStr = va_arg(*pArgs, char *);
ssize_t cchStr;
ssize_t offCur;
ssize_t offLast;
*ppszFormat += 2;
AssertMsgBreak(chVariant == 'a' || chVariant == 'p' || chVariant == 'q' || chVariant == 'f',
("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
if (!VALID_PTR(pszStr))
pszStr = "<NULL>";
cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
if (!(fFlags & RTSTR_F_LEFT))
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, "%20", 3);
offLast = offCur = 0;
while (offCur < cchStr)
{
ch = pszStr[offCur];
if ( RT_C_IS_ALPHA(ch)
|| RT_C_IS_DIGIT(ch)
|| ch == '-'
|| ch == '.'
|| ch == '_'
|| ch == '~'
|| ch == chAddSafe)
offCur++;
else
{
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
if (ch != ' ' || chVariant != 'f')
{
szBuf[0] = '%';
szBuf[1] = g_szHexDigitsUpper[((uint8_t)ch >> 4) & 0xf];
szBuf[2] = g_szHexDigitsUpper[(uint8_t)ch & 0xf];
szBuf[3] = '\0';
cchOutput += pfnOutput(pvArgOutput, szBuf, 3);
}
else
cchOutput += pfnOutput(pvArgOutput, "+", 1);
offLast = ++offCur;
}
}
if (offLast < offCur)
cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
while (--cchWidth >= cchStr)
cchOutput += pfnOutput(pvArgOutput, "%20", 3);
}
else
AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
break;
}
#endif /* IN_RING3 */
/*
* Groups 6 - CPU Architecture Register Formatters.
* "%RAarch[reg]"
*/
case 'A':
{
char const * const pszArch = *ppszFormat;
const char *pszReg = pszArch;
size_t cchOutput = 0;
int cPrinted = 0;
size_t cchReg;
/* Parse out the */
while ((ch = *pszReg++) && ch != '[')
{ /* nothing */ }
AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
cchReg = 0;
while ((ch = pszReg[cchReg]) && ch != ']')
cchReg++;
AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
*ppszFormat = &pszReg[cchReg + 1];
#define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
#define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
do { \
if ((a_uVal) & (a_fBitMask)) \
{ \
if (!cPrinted++) \
cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
else \
cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
(a_uVal) &= ~(a_fBitMask); \
} \
} while (0)
#define REG_OUT_CLOSE(a_uVal) \
do { \
if ((a_uVal)) \
{ \
cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
cPrinted++; \
} \
if (cPrinted) \
cchOutput += pfnOutput(pvArgOutput, "}", 1); \
} while (0)
if (0)
{ /* dummy */ }
#ifdef STRFORMAT_WITH_X86
/*
* X86 & AMD64.
*/
else if ( pszReg - pszArch == 3 + 1
&& pszArch[0] == 'x'
&& pszArch[1] == '8'
&& pszArch[2] == '6')
{
if (REG_EQUALS("cr0"))
{
uint64_t cr0 = va_arg(*pArgs, uint64_t);
fFlags |= RTSTR_F_64BIT;
cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
REG_OUT_CLOSE(cr0);
}
else if (REG_EQUALS("cr4"))
{
uint64_t cr4 = va_arg(*pArgs, uint64_t);
fFlags |= RTSTR_F_64BIT;
cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
REG_OUT_CLOSE(cr4);
}
else
AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
}
#endif
else
AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
#undef REG_OUT_BIT
#undef REG_OUT_CLOSE
#undef REG_EQUALS
return cchOutput;
}
/*
* Invalid/Unknown. Bitch about it.
*/
default:
AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
break;
}
}
else
AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
NOREF(pszFormatOrg);
return 0;
}
|