VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strformatrt.cpp@ 80496

Last change on this file since 80496 was 80496, checked in by vboxsync, 5 years ago

IPRT/string.h: Added added two more human readable size formatting variants for skipping the 'i' in 'KiB' and such. Made the human readable size formatting use the space format flag to specify a space between the value and the unit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 74.8 KB
Line 
1/* $Id: strformatrt.cpp 80496 2019-08-29 11:58:21Z vboxsync $ */
2/** @file
3 * IPRT - IPRT String Formatter Extensions.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_STRING
32#include <iprt/string.h>
33#ifndef RT_NO_EXPORT_SYMBOL
34# define RT_NO_EXPORT_SYMBOL /* don't slurp <linux/module.h> which then again
35 slurps arch-specific headers defining symbols */
36#endif
37#include "internal/iprt.h"
38
39#include <iprt/log.h>
40#include <iprt/assert.h>
41#include <iprt/string.h>
42#include <iprt/stdarg.h>
43#ifdef IN_RING3
44# include <iprt/errcore.h>
45# include <iprt/thread.h>
46# include <iprt/utf16.h>
47#endif
48#include <iprt/ctype.h>
49#include <iprt/time.h>
50#include <iprt/net.h>
51#include <iprt/path.h>
52#include <iprt/asm.h>
53#define STRFORMAT_WITH_X86
54#ifdef STRFORMAT_WITH_X86
55# include <iprt/x86.h>
56#endif
57#include "internal/string.h"
58
59
60/*********************************************************************************************************************************
61* Global Variables *
62*********************************************************************************************************************************/
63static char g_szHexDigits[17] = "0123456789abcdef";
64#ifdef IN_RING3
65static char g_szHexDigitsUpper[17] = "0123456789ABCDEF";
66#endif
67
68
69/**
70 * Helper that formats a 16-bit hex word in a IPv6 address.
71 *
72 * @returns Length in chars.
73 * @param pszDst The output buffer. Written from the start.
74 * @param uWord The word to format as hex.
75 */
76static size_t rtstrFormatIPv6HexWord(char *pszDst, uint16_t uWord)
77{
78 size_t off;
79 uint16_t cDigits;
80
81 if (uWord & UINT16_C(0xff00))
82 cDigits = uWord & UINT16_C(0xf000) ? 4 : 3;
83 else
84 cDigits = uWord & UINT16_C(0x00f0) ? 2 : 1;
85
86 off = 0;
87 switch (cDigits)
88 {
89 case 4: pszDst[off++] = g_szHexDigits[(uWord >> 12) & 0xf]; RT_FALL_THRU();
90 case 3: pszDst[off++] = g_szHexDigits[(uWord >> 8) & 0xf]; RT_FALL_THRU();
91 case 2: pszDst[off++] = g_szHexDigits[(uWord >> 4) & 0xf]; RT_FALL_THRU();
92 case 1: pszDst[off++] = g_szHexDigits[(uWord >> 0) & 0xf];
93 break;
94 }
95 pszDst[off] = '\0';
96 return off;
97}
98
99
100/**
101 * Helper function to format IPv6 address according to RFC 5952.
102 *
103 * @returns The number of bytes formatted.
104 * @param pfnOutput Pointer to output function.
105 * @param pvArgOutput Argument for the output function.
106 * @param pIpv6Addr IPv6 address
107 */
108static size_t rtstrFormatIPv6(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PCRTNETADDRIPV6 pIpv6Addr)
109{
110 size_t cch; /* result */
111 bool fEmbeddedIpv4;
112 size_t cwHexPart;
113 size_t cwLongestZeroRun;
114 size_t iLongestZeroStart;
115 size_t idx;
116 char szHexWord[8];
117
118 Assert(pIpv6Addr != NULL);
119
120 /*
121 * Check for embedded IPv4 address.
122 *
123 * IPv4-compatible - ::11.22.33.44 (obsolete)
124 * IPv4-mapped - ::ffff:11.22.33.44
125 * IPv4-translated - ::ffff:0:11.22.33.44 (RFC 2765)
126 */
127 fEmbeddedIpv4 = false;
128 cwHexPart = RT_ELEMENTS(pIpv6Addr->au16);
129 if ( pIpv6Addr->au64[0] == 0
130 && ( ( pIpv6Addr->au32[2] == 0
131 && pIpv6Addr->au32[3] != 0
132 && pIpv6Addr->au32[3] != RT_H2BE_U32_C(1) )
133 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0x0000ffff)
134 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0xffff0000) ) )
135 {
136 fEmbeddedIpv4 = true;
137 cwHexPart -= 2;
138 }
139
140 /*
141 * Find the longest sequences of two or more zero words.
142 */
143 cwLongestZeroRun = 0;
144 iLongestZeroStart = 0;
145 for (idx = 0; idx < cwHexPart; idx++)
146 if (pIpv6Addr->au16[idx] == 0)
147 {
148 size_t iZeroStart = idx;
149 size_t cwZeroRun;
150 do
151 idx++;
152 while (idx < cwHexPart && pIpv6Addr->au16[idx] == 0);
153 cwZeroRun = idx - iZeroStart;
154 if (cwZeroRun > 1 && cwZeroRun > cwLongestZeroRun)
155 {
156 cwLongestZeroRun = cwZeroRun;
157 iLongestZeroStart = iZeroStart;
158 if (cwZeroRun >= cwHexPart - idx)
159 break;
160 }
161 }
162
163 /*
164 * Do the formatting.
165 */
166 cch = 0;
167 if (cwLongestZeroRun == 0)
168 {
169 for (idx = 0; idx < cwHexPart; ++idx)
170 {
171 if (idx > 0)
172 cch += pfnOutput(pvArgOutput, ":", 1);
173 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
174 }
175
176 if (fEmbeddedIpv4)
177 cch += pfnOutput(pvArgOutput, ":", 1);
178 }
179 else
180 {
181 const size_t iLongestZeroEnd = iLongestZeroStart + cwLongestZeroRun;
182
183 if (iLongestZeroStart == 0)
184 cch += pfnOutput(pvArgOutput, ":", 1);
185 else
186 for (idx = 0; idx < iLongestZeroStart; ++idx)
187 {
188 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
189 cch += pfnOutput(pvArgOutput, ":", 1);
190 }
191
192 if (iLongestZeroEnd == cwHexPart)
193 cch += pfnOutput(pvArgOutput, ":", 1);
194 else
195 {
196 for (idx = iLongestZeroEnd; idx < cwHexPart; ++idx)
197 {
198 cch += pfnOutput(pvArgOutput, ":", 1);
199 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
200 }
201
202 if (fEmbeddedIpv4)
203 cch += pfnOutput(pvArgOutput, ":", 1);
204 }
205 }
206
207 if (fEmbeddedIpv4)
208 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
209 "%u.%u.%u.%u",
210 pIpv6Addr->au8[12],
211 pIpv6Addr->au8[13],
212 pIpv6Addr->au8[14],
213 pIpv6Addr->au8[15]);
214
215 return cch;
216}
217
218
219/**
220 * Callback to format iprt formatting extentions.
221 * See @ref pg_rt_str_format for a reference on the format types.
222 *
223 * @returns The number of bytes formatted.
224 * @param pfnOutput Pointer to output function.
225 * @param pvArgOutput Argument for the output function.
226 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
227 * after the format specifier.
228 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
229 * @param cchWidth Format Width. -1 if not specified.
230 * @param cchPrecision Format Precision. -1 if not specified.
231 * @param fFlags Flags (RTSTR_NTFS_*).
232 * @param chArgSize The argument size specifier, 'l' or 'L'.
233 */
234DECLHIDDEN(size_t) rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs,
235 int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
236{
237 const char *pszFormatOrg = *ppszFormat;
238 char ch = *(*ppszFormat)++;
239 size_t cch;
240 char szBuf[80];
241
242 if (ch == 'R')
243 {
244 ch = *(*ppszFormat)++;
245 switch (ch)
246 {
247 /*
248 * Groups 1 and 2.
249 */
250 case 'T':
251 case 'G':
252 case 'H':
253 case 'R':
254 case 'C':
255 case 'I':
256 case 'X':
257 case 'U':
258 case 'K':
259 {
260 /*
261 * Interpret the type.
262 */
263 typedef enum
264 {
265 RTSF_INT,
266 RTSF_INTW,
267 RTSF_BOOL,
268 RTSF_FP16,
269 RTSF_FP32,
270 RTSF_FP64,
271 RTSF_IPV4,
272 RTSF_IPV6,
273 RTSF_MAC,
274 RTSF_NETADDR,
275 RTSF_UUID
276 } RTSF;
277 static const struct
278 {
279 uint8_t cch; /**< the length of the string. */
280 char sz[10]; /**< the part following 'R'. */
281 uint8_t cb; /**< the size of the type. */
282 uint8_t u8Base; /**< the size of the type. */
283 RTSF enmFormat; /**< The way to format it. */
284 uint16_t fFlags; /**< additional RTSTR_F_* flags. */
285 }
286 /** Sorted array of types, looked up using binary search! */
287 s_aTypes[] =
288 {
289#define STRMEM(str) sizeof(str) - 1, str
290 { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
291 { STRMEM("Cp"), sizeof(RTCCPHYS), 16, RTSF_INTW, 0 },
292 { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
293 { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
294 { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
295 { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
296 { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
297 { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
298 { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
299 { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
300 { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
301 { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
302 { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
303 { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
304 { STRMEM("Hr"), sizeof(RTHCUINTREG), 16, RTSF_INTW, 0 },
305 { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
306 { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
307 { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
308 { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
309 { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
310 { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
311 { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
312 { STRMEM("Kv"), sizeof(RTHCPTR), 16, RTSF_INT, RTSTR_F_OBFUSCATE_PTR },
313 { STRMEM("Rv"), sizeof(RTRCPTR), 16, RTSF_INTW, 0 },
314 { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
315 { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
316 { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
317 { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
318 { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
319 { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
320 { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
321 { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
322 { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
323 { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
324 { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
325 { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
326 { STRMEM("Tmac"), sizeof(PCRTMAC), 16, RTSF_MAC, 0 },
327 { STRMEM("Tnaddr"), sizeof(PCRTNETADDR), 10, RTSF_NETADDR,0 },
328 { STRMEM("Tnaipv4"), sizeof(RTNETADDRIPV4), 10, RTSF_IPV4, 0 },
329 { STRMEM("Tnaipv6"), sizeof(PCRTNETADDRIPV6),16, RTSF_IPV6, 0 },
330 { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
331 { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
332 { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
333 { STRMEM("Treg"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
334 { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
335 { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
336 { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
337 { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
338 { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
339 { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
340 { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
341 { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
342 { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
343 { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
344 { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
345 { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
346 { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
347 { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
348 { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
349 { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
350 { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
351 { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
352#undef STRMEM
353 };
354 static const char s_szNull[] = "<NULL>";
355
356 const char *pszType = *ppszFormat - 1;
357 int iStart = 0;
358 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
359 int i = RT_ELEMENTS(s_aTypes) / 2;
360
361 union
362 {
363 uint8_t u8;
364 uint16_t u16;
365 uint32_t u32;
366 uint64_t u64;
367 int8_t i8;
368 int16_t i16;
369 int32_t i32;
370 int64_t i64;
371 RTR0INTPTR uR0Ptr;
372 RTFAR16 fp16;
373 RTFAR32 fp32;
374 RTFAR64 fp64;
375 bool fBool;
376 PCRTMAC pMac;
377 RTNETADDRIPV4 Ipv4Addr;
378 PCRTNETADDRIPV6 pIpv6Addr;
379 PCRTNETADDR pNetAddr;
380 PCRTUUID pUuid;
381 } u;
382
383 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
384 RT_NOREF_PV(chArgSize);
385
386 /*
387 * Lookup the type - binary search.
388 */
389 for (;;)
390 {
391 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
392 if (!iDiff)
393 break;
394 if (iEnd == iStart)
395 {
396 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
397 return 0;
398 }
399 if (iDiff < 0)
400 iEnd = i - 1;
401 else
402 iStart = i + 1;
403 if (iEnd < iStart)
404 {
405 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
406 return 0;
407 }
408 i = iStart + (iEnd - iStart) / 2;
409 }
410
411 /*
412 * Advance the format string and merge flags.
413 */
414 *ppszFormat += s_aTypes[i].cch - 1;
415 fFlags |= s_aTypes[i].fFlags;
416
417 /*
418 * Fetch the argument.
419 * It's important that a signed value gets sign-extended up to 64-bit.
420 */
421 RT_ZERO(u);
422 if (fFlags & RTSTR_F_VALSIGNED)
423 {
424 switch (s_aTypes[i].cb)
425 {
426 case sizeof(int8_t):
427 u.i64 = va_arg(*pArgs, /*int8_t*/int);
428 fFlags |= RTSTR_F_8BIT;
429 break;
430 case sizeof(int16_t):
431 u.i64 = va_arg(*pArgs, /*int16_t*/int);
432 fFlags |= RTSTR_F_16BIT;
433 break;
434 case sizeof(int32_t):
435 u.i64 = va_arg(*pArgs, int32_t);
436 fFlags |= RTSTR_F_32BIT;
437 break;
438 case sizeof(int64_t):
439 u.i64 = va_arg(*pArgs, int64_t);
440 fFlags |= RTSTR_F_64BIT;
441 break;
442 default:
443 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
444 break;
445 }
446 }
447 else
448 {
449 switch (s_aTypes[i].cb)
450 {
451 case sizeof(uint8_t):
452 u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
453 fFlags |= RTSTR_F_8BIT;
454 break;
455 case sizeof(uint16_t):
456 u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
457 fFlags |= RTSTR_F_16BIT;
458 break;
459 case sizeof(uint32_t):
460 u.u32 = va_arg(*pArgs, uint32_t);
461 fFlags |= RTSTR_F_32BIT;
462 break;
463 case sizeof(uint64_t):
464 u.u64 = va_arg(*pArgs, uint64_t);
465 fFlags |= RTSTR_F_64BIT;
466 break;
467 case sizeof(RTFAR32):
468 u.fp32 = va_arg(*pArgs, RTFAR32);
469 break;
470 case sizeof(RTFAR64):
471 u.fp64 = va_arg(*pArgs, RTFAR64);
472 break;
473 default:
474 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
475 break;
476 }
477 }
478
479#ifndef DEBUG
480 /*
481 * For now don't show the address.
482 */
483 if (fFlags & RTSTR_F_OBFUSCATE_PTR)
484 {
485 cch = rtStrFormatKernelAddress(szBuf, sizeof(szBuf), u.uR0Ptr, cchWidth, cchPrecision, fFlags);
486 return pfnOutput(pvArgOutput, szBuf, cch);
487 }
488#endif
489
490 /*
491 * Format the output.
492 */
493 switch (s_aTypes[i].enmFormat)
494 {
495 case RTSF_INT:
496 {
497 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
498 break;
499 }
500
501 /* hex which defaults to max width. */
502 case RTSF_INTW:
503 {
504 Assert(s_aTypes[i].u8Base == 16);
505 if (cchWidth < 0)
506 {
507 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
508 fFlags |= RTSTR_F_ZEROPAD;
509 }
510 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
511 break;
512 }
513
514 case RTSF_BOOL:
515 {
516 static const char s_szTrue[] = "true ";
517 static const char s_szFalse[] = "false";
518 if (u.u64 == 1)
519 return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
520 if (u.u64 == 0)
521 return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
522 /* invalid boolean value */
523 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
524 }
525
526 case RTSF_FP16:
527 {
528 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
529 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
530 Assert(cch == 4);
531 szBuf[4] = ':';
532 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
533 Assert(cch == 4);
534 cch = 4 + 1 + 4;
535 break;
536 }
537 case RTSF_FP32:
538 {
539 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
540 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
541 Assert(cch == 4);
542 szBuf[4] = ':';
543 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
544 Assert(cch == 8);
545 cch = 4 + 1 + 8;
546 break;
547 }
548 case RTSF_FP64:
549 {
550 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
551 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
552 Assert(cch == 4);
553 szBuf[4] = ':';
554 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
555 Assert(cch == 16);
556 cch = 4 + 1 + 16;
557 break;
558 }
559
560 case RTSF_IPV4:
561 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
562 "%u.%u.%u.%u",
563 u.Ipv4Addr.au8[0],
564 u.Ipv4Addr.au8[1],
565 u.Ipv4Addr.au8[2],
566 u.Ipv4Addr.au8[3]);
567
568 case RTSF_IPV6:
569 {
570 if (VALID_PTR(u.pIpv6Addr))
571 return rtstrFormatIPv6(pfnOutput, pvArgOutput, u.pIpv6Addr);
572 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
573 }
574
575 case RTSF_MAC:
576 {
577 if (VALID_PTR(u.pMac))
578 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
579 "%02x:%02x:%02x:%02x:%02x:%02x",
580 u.pMac->au8[0],
581 u.pMac->au8[1],
582 u.pMac->au8[2],
583 u.pMac->au8[3],
584 u.pMac->au8[4],
585 u.pMac->au8[5]);
586 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
587 }
588
589 case RTSF_NETADDR:
590 {
591 if (VALID_PTR(u.pNetAddr))
592 {
593 switch (u.pNetAddr->enmType)
594 {
595 case RTNETADDRTYPE_IPV4:
596 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
597 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
598 "%u.%u.%u.%u",
599 u.pNetAddr->uAddr.IPv4.au8[0],
600 u.pNetAddr->uAddr.IPv4.au8[1],
601 u.pNetAddr->uAddr.IPv4.au8[2],
602 u.pNetAddr->uAddr.IPv4.au8[3]);
603 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
604 "%u.%u.%u.%u:%u",
605 u.pNetAddr->uAddr.IPv4.au8[0],
606 u.pNetAddr->uAddr.IPv4.au8[1],
607 u.pNetAddr->uAddr.IPv4.au8[2],
608 u.pNetAddr->uAddr.IPv4.au8[3],
609 u.pNetAddr->uPort);
610
611 case RTNETADDRTYPE_IPV6:
612 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
613 return rtstrFormatIPv6(pfnOutput, pvArgOutput, &u.pNetAddr->uAddr.IPv6);
614
615 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
616 "[%RTnaipv6]:%u",
617 &u.pNetAddr->uAddr.IPv6,
618 u.pNetAddr->uPort);
619
620 case RTNETADDRTYPE_MAC:
621 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
622 "%02x:%02x:%02x:%02x:%02x:%02x",
623 u.pNetAddr->uAddr.Mac.au8[0],
624 u.pNetAddr->uAddr.Mac.au8[1],
625 u.pNetAddr->uAddr.Mac.au8[2],
626 u.pNetAddr->uAddr.Mac.au8[3],
627 u.pNetAddr->uAddr.Mac.au8[4],
628 u.pNetAddr->uAddr.Mac.au8[5]);
629
630 default:
631 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
632 "unsupported-netaddr-type=%u", u.pNetAddr->enmType);
633
634 }
635 }
636 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
637 }
638
639 case RTSF_UUID:
640 {
641 if (VALID_PTR(u.pUuid))
642 {
643 /* cannot call RTUuidToStr because of GC/R0. */
644 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
645 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
646 RT_H2LE_U32(u.pUuid->Gen.u32TimeLow),
647 RT_H2LE_U16(u.pUuid->Gen.u16TimeMid),
648 RT_H2LE_U16(u.pUuid->Gen.u16TimeHiAndVersion),
649 u.pUuid->Gen.u8ClockSeqHiAndReserved,
650 u.pUuid->Gen.u8ClockSeqLow,
651 u.pUuid->Gen.au8Node[0],
652 u.pUuid->Gen.au8Node[1],
653 u.pUuid->Gen.au8Node[2],
654 u.pUuid->Gen.au8Node[3],
655 u.pUuid->Gen.au8Node[4],
656 u.pUuid->Gen.au8Node[5]);
657 }
658 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
659 }
660
661 default:
662 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
663 return 0;
664 }
665
666 /*
667 * Finally, output the formatted string and return.
668 */
669 return pfnOutput(pvArgOutput, szBuf, cch);
670 }
671
672
673 /* Group 3 */
674
675 /*
676 * Base name printing, big endian UTF-16.
677 */
678 case 'b':
679 {
680 switch (*(*ppszFormat)++)
681 {
682 case 'n':
683 {
684 const char *pszLastSep;
685 const char *psz = pszLastSep = va_arg(*pArgs, const char *);
686 if (!VALID_PTR(psz))
687 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
688
689 while ((ch = *psz) != '\0')
690 {
691 if (RTPATH_IS_SEP(ch))
692 {
693 do
694 psz++;
695 while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
696 if (!ch)
697 break;
698 pszLastSep = psz;
699 }
700 psz++;
701 }
702
703 return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
704 }
705
706 /* %lRbs */
707 case 's':
708 if (chArgSize == 'l')
709 {
710 /* utf-16BE -> utf-8 */
711 int cchStr;
712 PCRTUTF16 pwszStr = va_arg(*pArgs, PRTUTF16);
713
714 if (RT_VALID_PTR(pwszStr))
715 {
716 cchStr = 0;
717 while (cchStr < cchPrecision && pwszStr[cchStr] != '\0')
718 cchStr++;
719 }
720 else
721 {
722 static RTUTF16 s_wszBigNull[] =
723 {
724 RT_H2BE_U16_C((uint16_t)'<'), RT_H2BE_U16_C((uint16_t)'N'), RT_H2BE_U16_C((uint16_t)'U'),
725 RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'>'), '\0'
726 };
727 pwszStr = s_wszBigNull;
728 cchStr = RT_ELEMENTS(s_wszBigNull) - 1;
729 }
730
731 cch = 0;
732 if (!(fFlags & RTSTR_F_LEFT))
733 while (--cchWidth >= cchStr)
734 cch += pfnOutput(pvArgOutput, " ", 1);
735 cchWidth -= cchStr;
736 while (cchStr-- > 0)
737 {
738/** @todo \#ifndef IN_RC*/
739#ifdef IN_RING3
740 RTUNICP Cp = 0;
741 RTUtf16BigGetCpEx(&pwszStr, &Cp);
742 char *pszEnd = RTStrPutCp(szBuf, Cp);
743 *pszEnd = '\0';
744 cch += pfnOutput(pvArgOutput, szBuf, pszEnd - szBuf);
745#else
746 szBuf[0] = (char)(*pwszStr++ >> 8);
747 cch += pfnOutput(pvArgOutput, szBuf, 1);
748#endif
749 }
750 while (--cchWidth >= 0)
751 cch += pfnOutput(pvArgOutput, " ", 1);
752 return cch;
753 }
754 RT_FALL_THRU();
755
756 default:
757 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
758 break;
759 }
760 break;
761 }
762
763
764 /*
765 * Pretty function / method name printing.
766 */
767 case 'f':
768 {
769 switch (*(*ppszFormat)++)
770 {
771 /*
772 * Pretty function / method name printing.
773 * This isn't 100% right (see classic signal prototype) and it assumes
774 * standardized names, but it'll do for today.
775 */
776 case 'n':
777 {
778 const char *pszStart;
779 const char *psz = pszStart = va_arg(*pArgs, const char *);
780 int cAngle = 0;
781
782 if (!VALID_PTR(psz))
783 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
784
785 while ((ch = *psz) != '\0' && ch != '(')
786 {
787 if (RT_C_IS_BLANK(ch))
788 {
789 psz++;
790 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
791 psz++;
792 if (ch && cAngle == 0)
793 pszStart = psz;
794 }
795 else if (ch == '(')
796 break;
797 else if (ch == '<')
798 {
799 cAngle++;
800 psz++;
801 }
802 else if (ch == '>')
803 {
804 cAngle--;
805 psz++;
806 }
807 else
808 psz++;
809 }
810
811 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
812 }
813
814 default:
815 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
816 break;
817 }
818 break;
819 }
820
821
822 /*
823 * hex dumping, COM/XPCOM, human readable sizes.
824 */
825 case 'h':
826 {
827 ch = *(*ppszFormat)++;
828 switch (ch)
829 {
830 /*
831 * Hex stuff.
832 */
833 case 'x':
834 {
835 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
836 if (cchPrecision < 0)
837 cchPrecision = 16;
838 if (pu8)
839 {
840 switch (*(*ppszFormat)++)
841 {
842 /*
843 * Regular hex dump.
844 */
845 case 'd':
846 {
847 int off = 0;
848 cch = 0;
849
850 if (cchWidth <= 0)
851 cchWidth = 16;
852
853 while (off < cchPrecision)
854 {
855 int i;
856 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
857 "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
858 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
859 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
860 off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ",
861 pu8[i]);
862 while (i++ < cchWidth)
863 cch += pfnOutput(pvArgOutput, " ", 3);
864
865 cch += pfnOutput(pvArgOutput, " ", 1);
866
867 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
868 {
869 uint8_t u8 = pu8[i];
870 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
871 }
872
873 /* next */
874 pu8 += cchWidth;
875 off += cchWidth;
876 }
877 return cch;
878 }
879
880 /*
881 * Regular hex dump with dittoing.
882 */
883 case 'D':
884 {
885 int offEndDupCheck;
886 int cDuplicates = 0;
887 int off = 0;
888 cch = 0;
889
890 if (cchWidth <= 0)
891 cchWidth = 16;
892 offEndDupCheck = cchPrecision - cchWidth;
893
894 while (off < cchPrecision)
895 {
896 int i;
897 if ( off >= offEndDupCheck
898 || off <= 0
899 || memcmp(pu8, pu8 - cchWidth, cchWidth) != 0
900 || ( cDuplicates == 0
901 && ( off + cchWidth >= offEndDupCheck
902 || memcmp(pu8 + cchWidth, pu8, cchWidth) != 0)) )
903 {
904 if (cDuplicates > 0)
905 {
906 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
907 "\n%.*s **** <ditto x %u>",
908 sizeof(pu8) * 2, "****************", cDuplicates);
909 cDuplicates = 0;
910 }
911
912 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
913 "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
914 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
915 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
916 off + i < cchPrecision ? !(i & 7) && i
917 ? "-%02x" : " %02x" : " ",
918 pu8[i]);
919 while (i++ < cchWidth)
920 cch += pfnOutput(pvArgOutput, " ", 3);
921
922 cch += pfnOutput(pvArgOutput, " ", 1);
923
924 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
925 {
926 uint8_t u8 = pu8[i];
927 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
928 }
929 }
930 else
931 cDuplicates++;
932
933 /* next */
934 pu8 += cchWidth;
935 off += cchWidth;
936 }
937 return cch;
938 }
939
940 /*
941 * Hex string.
942 */
943 case 's':
944 {
945 if (cchPrecision-- > 0)
946 {
947 cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
948 for (; cchPrecision > 0; cchPrecision--, pu8++)
949 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
950 return cch;
951 }
952 break;
953 }
954
955 default:
956 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
957 break;
958 }
959 }
960 else
961 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
962 break;
963 }
964
965
966#ifdef IN_RING3
967 /*
968 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
969 * ASSUMES: If Windows Then COM else XPCOM.
970 */
971 case 'r':
972 {
973 uint32_t hrc = va_arg(*pArgs, uint32_t);
974 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
975 switch (*(*ppszFormat)++)
976 {
977 case 'c':
978 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
979 case 'f':
980 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
981 case 'a':
982 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
983 default:
984 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
985 return 0;
986 }
987 break;
988 }
989#endif /* IN_RING3 */
990
991 /*
992 * Human readable sizes.
993 */
994 case 'c':
995 case 'u':
996 {
997 unsigned i;
998 ssize_t cchBuf;
999 uint64_t uValue;
1000 uint64_t uFraction = 0;
1001 const char *pszPrefix = NULL;
1002 char ch2 = *(*ppszFormat)++;
1003 AssertMsgReturn(ch2 == 'b' || ch2 == 'B' || ch2 == 'i', ("invalid type '%.10s'!\n", pszFormatOrg), 0);
1004 uValue = va_arg(*pArgs, uint64_t);
1005
1006 if (!(fFlags & RTSTR_F_PRECISION))
1007 cchPrecision = 1; /** @todo default to flexible decimal point. */
1008 else if (cchPrecision > 3)
1009 cchPrecision = 3;
1010 else if (cchPrecision < 0)
1011 cchPrecision = 0;
1012
1013 if (ch2 == 'b' || ch2 == 'B')
1014 {
1015 static const struct
1016 {
1017 const char *pszPrefix;
1018 uint8_t cShift;
1019 uint64_t cbMin;
1020 uint64_t cbMinZeroPrecision;
1021 } s_aUnits[] =
1022 {
1023 { "Ei", 60, _1E, _1E*2 },
1024 { "Pi", 50, _1P, _1P*2 },
1025 { "Ti", 40, _1T, _1T*2 },
1026 { "Gi", 30, _1G, _1G64*2 },
1027 { "Mi", 20, _1M, _1M*2 },
1028 { "Ki", 10, _1K, _1K*2 },
1029 };
1030 for (i = 0; i < RT_ELEMENTS(s_aUnits); i++)
1031 if ( uValue >= s_aUnits[i].cbMin
1032 && (cchPrecision > 0 || uValue >= s_aUnits[i].cbMinZeroPrecision))
1033 {
1034 if (cchPrecision != 0)
1035 {
1036 uFraction = uValue & (RT_BIT_64(s_aUnits[i].cShift) - 1);
1037 uFraction *= cchPrecision == 1 ? 10 : cchPrecision == 2 ? 100 : 1000;
1038 uFraction >>= s_aUnits[i].cShift;
1039 }
1040 uValue >>= s_aUnits[i].cShift;
1041 pszPrefix = s_aUnits[i].pszPrefix;
1042 break;
1043 }
1044 }
1045 else
1046 {
1047 static const struct
1048 {
1049 const char *pszPrefix;
1050 uint64_t cbFactor;
1051 uint64_t cbMinZeroPrecision;
1052 } s_aUnits[] =
1053 {
1054 { "E", UINT64_C(1000000000000000000), UINT64_C(1010000000000000000), },
1055 { "P", UINT64_C(1000000000000000), UINT64_C(1010000000000000), },
1056 { "T", UINT64_C(1000000000000), UINT64_C(1010000000000), },
1057 { "G", UINT64_C(1000000000), UINT64_C(1010000000), },
1058 { "M", UINT64_C(1000000), UINT64_C(1010000), },
1059 { "k", UINT64_C(1000), UINT64_C(1010), },
1060 };
1061 for (i = 0; i < RT_ELEMENTS(s_aUnits); i++)
1062 if ( uValue >= s_aUnits[i].cbFactor
1063 && (cchPrecision > 0 || uValue >= s_aUnits[i].cbMinZeroPrecision))
1064 {
1065 if (cchPrecision == 0)
1066 uValue /= s_aUnits[i].cbFactor;
1067 else
1068 {
1069 uFraction = uValue % s_aUnits[i].cbFactor;
1070 uValue = uValue / s_aUnits[i].cbFactor;
1071 uFraction *= cchPrecision == 1 ? 10 : cchPrecision == 2 ? 100 : 1000;
1072 uFraction += s_aUnits[i].cbFactor >> 1;
1073 uFraction /= s_aUnits[i].cbFactor;
1074 }
1075 pszPrefix = s_aUnits[i].pszPrefix;
1076 break;
1077 }
1078 }
1079
1080 cchBuf = RTStrFormatU64(szBuf, sizeof(szBuf), uValue, 10, 0, 0, 0);
1081 if (pszPrefix)
1082 {
1083 if (cchPrecision)
1084 {
1085 szBuf[cchBuf++] = '.';
1086 cchBuf += RTStrFormatU64(&szBuf[cchBuf], sizeof(szBuf) - cchBuf, uFraction, 10, cchPrecision, 0,
1087 RTSTR_F_ZEROPAD | RTSTR_F_WIDTH);
1088 }
1089 if (fFlags & RTSTR_F_BLANK)
1090 szBuf[cchBuf++] = ' ';
1091 szBuf[cchBuf++] = *pszPrefix++;
1092 if (*pszPrefix && ch2 != 'B')
1093 szBuf[cchBuf++] = *pszPrefix;
1094 }
1095 else if (fFlags & RTSTR_F_BLANK)
1096 szBuf[cchBuf++] = ' ';
1097 if (ch == 'c')
1098 szBuf[cchBuf++] = 'B';
1099 szBuf[cchBuf] = '\0';
1100
1101 cch = 0;
1102 if ((fFlags & RTSTR_F_WIDTH) && !(fFlags & RTSTR_F_LEFT))
1103 while (cchBuf < cchWidth)
1104 {
1105 cch += pfnOutput(pvArgOutput, fFlags & RTSTR_F_ZEROPAD ? "0" : " ", 1);
1106 cchWidth--;
1107 }
1108 cch += pfnOutput(pvArgOutput, szBuf, cchBuf);
1109 return cch;
1110 }
1111
1112 default:
1113 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
1114 return 0;
1115
1116 }
1117 break;
1118 }
1119
1120 /*
1121 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
1122 */
1123 case 'r':
1124 {
1125 int rc = va_arg(*pArgs, int);
1126#ifdef IN_RING3 /* we don't want this anywhere else yet. */
1127 PCRTSTATUSMSG pMsg = RTErrGet(rc);
1128 switch (*(*ppszFormat)++)
1129 {
1130 case 'c':
1131 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
1132 case 's':
1133 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
1134 case 'f':
1135 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
1136 case 'a':
1137 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
1138 default:
1139 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
1140 return 0;
1141 }
1142#else /* !IN_RING3 */
1143 switch (*(*ppszFormat)++)
1144 {
1145 case 'c':
1146 case 's':
1147 case 'f':
1148 case 'a':
1149 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
1150 default:
1151 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
1152 return 0;
1153 }
1154#endif /* !IN_RING3 */
1155 break;
1156 }
1157
1158#if defined(IN_RING3)
1159 /*
1160 * Windows status code: %Rwc, %Rwf, %Rwa
1161 */
1162 case 'w':
1163 {
1164 long rc = va_arg(*pArgs, long);
1165# if defined(RT_OS_WINDOWS)
1166 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
1167# endif
1168 switch (*(*ppszFormat)++)
1169 {
1170# if defined(RT_OS_WINDOWS)
1171 case 'c':
1172 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
1173 case 'f':
1174 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
1175 case 'a':
1176 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
1177# else
1178 case 'c':
1179 case 'f':
1180 case 'a':
1181 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
1182# endif
1183 default:
1184 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
1185 return 0;
1186 }
1187 break;
1188 }
1189#endif /* IN_RING3 */
1190
1191 /*
1192 * Group 4, structure dumpers.
1193 */
1194 case 'D':
1195 {
1196 /*
1197 * Interpret the type.
1198 */
1199 typedef enum
1200 {
1201 RTST_TIMESPEC
1202 } RTST;
1203/** Set if it's a pointer */
1204#define RTST_FLAGS_POINTER RT_BIT(0)
1205 static const struct
1206 {
1207 uint8_t cch; /**< the length of the string. */
1208 char sz[16-2]; /**< the part following 'R'. */
1209 uint8_t cb; /**< the size of the argument. */
1210 uint8_t fFlags; /**< RTST_FLAGS_* */
1211 RTST enmType; /**< The structure type. */
1212 }
1213 /** Sorted array of types, looked up using binary search! */
1214 s_aTypes[] =
1215 {
1216#define STRMEM(str) sizeof(str) - 1, str
1217 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
1218#undef STRMEM
1219 };
1220 const char *pszType = *ppszFormat - 1;
1221 int iStart = 0;
1222 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
1223 int i = RT_ELEMENTS(s_aTypes) / 2;
1224
1225 union
1226 {
1227 const void *pv;
1228 uint64_t u64;
1229 PCRTTIMESPEC pTimeSpec;
1230 } u;
1231
1232 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
1233
1234 /*
1235 * Lookup the type - binary search.
1236 */
1237 for (;;)
1238 {
1239 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
1240 if (!iDiff)
1241 break;
1242 if (iEnd == iStart)
1243 {
1244 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1245 return 0;
1246 }
1247 if (iDiff < 0)
1248 iEnd = i - 1;
1249 else
1250 iStart = i + 1;
1251 if (iEnd < iStart)
1252 {
1253 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1254 return 0;
1255 }
1256 i = iStart + (iEnd - iStart) / 2;
1257 }
1258 *ppszFormat += s_aTypes[i].cch - 1;
1259
1260 /*
1261 * Fetch the argument.
1262 */
1263 u.u64 = 0;
1264 switch (s_aTypes[i].cb)
1265 {
1266 case sizeof(const void *):
1267 u.pv = va_arg(*pArgs, const void *);
1268 break;
1269 default:
1270 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
1271 break;
1272 }
1273
1274 /*
1275 * If it's a pointer, we'll check if it's valid before going on.
1276 */
1277 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
1278 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
1279
1280 /*
1281 * Format the output.
1282 */
1283 switch (s_aTypes[i].enmType)
1284 {
1285 case RTST_TIMESPEC:
1286 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
1287
1288 default:
1289 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
1290 break;
1291 }
1292 break;
1293 }
1294
1295#ifdef IN_RING3
1296
1297 /*
1298 * Group 5, XML / HTML, JSON and URI escapers.
1299 */
1300 case 'M':
1301 {
1302 char chWhat = (*ppszFormat)[0];
1303 if (chWhat == 'a' || chWhat == 'e')
1304 {
1305 /* XML attributes and element values. */
1306 bool fAttr = chWhat == 'a';
1307 char chType = (*ppszFormat)[1];
1308 *ppszFormat += 2;
1309 switch (chType)
1310 {
1311 case 's':
1312 {
1313 static const char s_szElemEscape[] = "<>&\"'";
1314 static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
1315 const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
1316 size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
1317 size_t cchOutput = 0;
1318 const char *pszStr = va_arg(*pArgs, char *);
1319 ssize_t cchStr;
1320 ssize_t offCur;
1321 ssize_t offLast;
1322
1323 if (!VALID_PTR(pszStr))
1324 pszStr = "<NULL>";
1325 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1326
1327 if (fAttr)
1328 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1329 if (!(fFlags & RTSTR_F_LEFT))
1330 while (--cchWidth >= cchStr)
1331 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1332
1333 offLast = offCur = 0;
1334 while (offCur < cchStr)
1335 {
1336 if (memchr(pszEscape, pszStr[offCur], cchEscape))
1337 {
1338 if (offLast < offCur)
1339 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1340 switch (pszStr[offCur])
1341 {
1342 case '<': cchOutput += pfnOutput(pvArgOutput, "&lt;", 4); break;
1343 case '>': cchOutput += pfnOutput(pvArgOutput, "&gt;", 4); break;
1344 case '&': cchOutput += pfnOutput(pvArgOutput, "&amp;", 5); break;
1345 case '\'': cchOutput += pfnOutput(pvArgOutput, "&apos;", 6); break;
1346 case '"': cchOutput += pfnOutput(pvArgOutput, "&quot;", 6); break;
1347 case '\n': cchOutput += pfnOutput(pvArgOutput, "&#xA;", 5); break;
1348 case '\r': cchOutput += pfnOutput(pvArgOutput, "&#xD;", 5); break;
1349 default:
1350 AssertFailed();
1351 }
1352 offLast = offCur + 1;
1353 }
1354 offCur++;
1355 }
1356 if (offLast < offCur)
1357 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1358
1359 while (--cchWidth >= cchStr)
1360 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1361 if (fAttr)
1362 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1363 return cchOutput;
1364 }
1365
1366 default:
1367 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1368 }
1369 }
1370 else if (chWhat == 'j')
1371 {
1372 /* JSON string escaping. */
1373 char const chType = (*ppszFormat)[1];
1374 *ppszFormat += 2;
1375 switch (chType)
1376 {
1377 case 's':
1378 {
1379 const char *pszStr = va_arg(*pArgs, char *);
1380 size_t cchOutput;
1381 ssize_t cchStr;
1382 ssize_t offCur;
1383 ssize_t offLast;
1384
1385 if (!VALID_PTR(pszStr))
1386 pszStr = "<NULL>";
1387 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1388
1389 cchOutput = pfnOutput(pvArgOutput, "\"", 1);
1390 if (!(fFlags & RTSTR_F_LEFT))
1391 while (--cchWidth >= cchStr)
1392 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1393
1394 offLast = offCur = 0;
1395 while (offCur < cchStr)
1396 {
1397 unsigned int const uch = pszStr[offCur];
1398 if ( uch >= 0x5d
1399 || (uch >= 0x20 && uch != 0x22 && uch != 0x5c))
1400 offCur++;
1401 else
1402 {
1403 if (offLast < offCur)
1404 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1405 switch ((char)uch)
1406 {
1407 case '"': cchOutput += pfnOutput(pvArgOutput, "\\\"", 2); break;
1408 case '\\': cchOutput += pfnOutput(pvArgOutput, "\\\\", 2); break;
1409 case '/': cchOutput += pfnOutput(pvArgOutput, "\\/", 2); break;
1410 case '\b': cchOutput += pfnOutput(pvArgOutput, "\\b", 2); break;
1411 case '\f': cchOutput += pfnOutput(pvArgOutput, "\\f", 2); break;
1412 case '\n': cchOutput += pfnOutput(pvArgOutput, "\\n", 2); break;
1413 case '\t': cchOutput += pfnOutput(pvArgOutput, "\\t", 2); break;
1414 default:
1415 {
1416 RTUNICP uc = 0xfffd; /* replacement character */
1417 const char *pszCur = &pszStr[offCur];
1418 int rc = RTStrGetCpEx(&pszCur, &uc);
1419 if (RT_SUCCESS(rc))
1420 offCur += pszCur - &pszStr[offCur] - 1;
1421 if (uc >= 0xfffe)
1422 uc = 0xfffd; /* replacement character */
1423 szBuf[0] = '\\';
1424 szBuf[1] = 'u';
1425 szBuf[2] = g_szHexDigits[(uc >> 12) & 0xf];
1426 szBuf[3] = g_szHexDigits[(uc >> 8) & 0xf];
1427 szBuf[4] = g_szHexDigits[(uc >> 4) & 0xf];
1428 szBuf[5] = g_szHexDigits[ uc & 0xf];
1429 szBuf[6] = '\0';
1430 cchOutput += pfnOutput(pvArgOutput, szBuf, 6);
1431 break;
1432 }
1433 }
1434 offLast = ++offCur;
1435 }
1436 }
1437 if (offLast < offCur)
1438 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1439
1440 while (--cchWidth >= cchStr)
1441 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1442 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1443 return cchOutput;
1444 }
1445
1446 default:
1447 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1448 }
1449 }
1450 else if (chWhat == 'p')
1451 {
1452 /* Percent encoded string (RTC-3986). */
1453 char const chVariant = (*ppszFormat)[1];
1454 char const chAddSafe = chVariant == 'p' ? '/'
1455 : chVariant == 'q' ? '+' /* '+' in queries is problematic, so no escape. */
1456 : '~' /* whatever */;
1457 size_t cchOutput = 0;
1458 const char *pszStr = va_arg(*pArgs, char *);
1459 ssize_t cchStr;
1460 ssize_t offCur;
1461 ssize_t offLast;
1462
1463 *ppszFormat += 2;
1464 AssertMsgBreak(chVariant == 'a' || chVariant == 'p' || chVariant == 'q' || chVariant == 'f',
1465 ("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1466
1467 if (!VALID_PTR(pszStr))
1468 pszStr = "<NULL>";
1469 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1470
1471 if (!(fFlags & RTSTR_F_LEFT))
1472 while (--cchWidth >= cchStr)
1473 cchOutput += pfnOutput(pvArgOutput, "%20", 3);
1474
1475 offLast = offCur = 0;
1476 while (offCur < cchStr)
1477 {
1478 ch = pszStr[offCur];
1479 if ( RT_C_IS_ALPHA(ch)
1480 || RT_C_IS_DIGIT(ch)
1481 || ch == '-'
1482 || ch == '.'
1483 || ch == '_'
1484 || ch == '~'
1485 || ch == chAddSafe)
1486 offCur++;
1487 else
1488 {
1489 if (offLast < offCur)
1490 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1491 if (ch != ' ' || chVariant != 'f')
1492 {
1493 szBuf[0] = '%';
1494 szBuf[1] = g_szHexDigitsUpper[((uint8_t)ch >> 4) & 0xf];
1495 szBuf[2] = g_szHexDigitsUpper[(uint8_t)ch & 0xf];
1496 szBuf[3] = '\0';
1497 cchOutput += pfnOutput(pvArgOutput, szBuf, 3);
1498 }
1499 else
1500 cchOutput += pfnOutput(pvArgOutput, "+", 1);
1501 offLast = ++offCur;
1502 }
1503 }
1504 if (offLast < offCur)
1505 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1506
1507 while (--cchWidth >= cchStr)
1508 cchOutput += pfnOutput(pvArgOutput, "%20", 3);
1509 }
1510 else
1511 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1512 break;
1513 }
1514
1515#endif /* IN_RING3 */
1516
1517 /*
1518 * Groups 6 - CPU Architecture Register Formatters.
1519 * "%RAarch[reg]"
1520 */
1521 case 'A':
1522 {
1523 char const * const pszArch = *ppszFormat;
1524 const char *pszReg = pszArch;
1525 size_t cchOutput = 0;
1526 int cPrinted = 0;
1527 size_t cchReg;
1528
1529 /* Parse out the */
1530 while ((ch = *pszReg++) && ch != '[')
1531 { /* nothing */ }
1532 AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1533
1534 cchReg = 0;
1535 while ((ch = pszReg[cchReg]) && ch != ']')
1536 cchReg++;
1537 AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1538
1539 *ppszFormat = &pszReg[cchReg + 1];
1540
1541
1542#define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
1543#define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
1544 do { \
1545 if ((a_uVal) & (a_fBitMask)) \
1546 { \
1547 if (!cPrinted++) \
1548 cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
1549 else \
1550 cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
1551 (a_uVal) &= ~(a_fBitMask); \
1552 } \
1553 } while (0)
1554#define REG_OUT_CLOSE(a_uVal) \
1555 do { \
1556 if ((a_uVal)) \
1557 { \
1558 cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
1559 cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
1560 cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
1561 cPrinted++; \
1562 } \
1563 if (cPrinted) \
1564 cchOutput += pfnOutput(pvArgOutput, "}", 1); \
1565 } while (0)
1566
1567
1568 if (0)
1569 { /* dummy */ }
1570#ifdef STRFORMAT_WITH_X86
1571 /*
1572 * X86 & AMD64.
1573 */
1574 else if ( pszReg - pszArch == 3 + 1
1575 && pszArch[0] == 'x'
1576 && pszArch[1] == '8'
1577 && pszArch[2] == '6')
1578 {
1579 if (REG_EQUALS("cr0"))
1580 {
1581 uint64_t cr0 = va_arg(*pArgs, uint64_t);
1582 fFlags |= RTSTR_F_64BIT;
1583 cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1584 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1585 REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
1586 REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
1587 REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
1588 REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
1589 REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
1590 REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
1591 REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
1592 REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
1593 REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
1594 REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
1595 REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
1596 REG_OUT_CLOSE(cr0);
1597 }
1598 else if (REG_EQUALS("cr4"))
1599 {
1600 uint64_t cr4 = va_arg(*pArgs, uint64_t);
1601 fFlags |= RTSTR_F_64BIT;
1602 cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1603 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1604 REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
1605 REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
1606 REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
1607 REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
1608 REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
1609 REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
1610 REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
1611 REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
1612 REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
1613 REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
1614 REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
1615 REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
1616 REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
1617 REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
1618 REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
1619 REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
1620 REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
1621 REG_OUT_CLOSE(cr4);
1622 }
1623 else
1624 AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
1625 }
1626#endif
1627 else
1628 AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
1629#undef REG_OUT_BIT
1630#undef REG_OUT_CLOSE
1631#undef REG_EQUALS
1632 return cchOutput;
1633 }
1634
1635 /*
1636 * Invalid/Unknown. Bitch about it.
1637 */
1638 default:
1639 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1640 break;
1641 }
1642 }
1643 else
1644 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1645
1646 NOREF(pszFormatOrg);
1647 return 0;
1648}
1649
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette