]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - ubuntu/vbox/vboxguest/common/string/strformatrt.c
UBUNTU: ubuntu: vbox -- update to 5.1.24-dfsg-1
[mirror_ubuntu-artful-kernel.git] / ubuntu / vbox / vboxguest / common / string / strformatrt.c
1 /* $Id: strformatrt.cpp $ */
2 /** @file
3 * IPRT - IPRT String Formatter Extensions.
4 */
5
6 /*
7 * Copyright (C) 2006-2016 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/thread.h>
45 # include <iprt/err.h>
46 #endif
47 #include <iprt/ctype.h>
48 #include <iprt/time.h>
49 #include <iprt/net.h>
50 #include <iprt/path.h>
51 #include <iprt/asm.h>
52 #define STRFORMAT_WITH_X86
53 #ifdef STRFORMAT_WITH_X86
54 # include <iprt/x86.h>
55 #endif
56 #include "internal/string.h"
57
58
59 /*********************************************************************************************************************************
60 * Global Variables *
61 *********************************************************************************************************************************/
62 static char g_szHexDigits[17] = "0123456789abcdef";
63
64
65 /**
66 * Helper that formats a 16-bit hex word in a IPv6 address.
67 *
68 * @returns Length in chars.
69 * @param pszDst The output buffer. Written from the start.
70 * @param uWord The word to format as hex.
71 */
72 static size_t rtstrFormatIPv6HexWord(char *pszDst, uint16_t uWord)
73 {
74 size_t off;
75 uint16_t cDigits;
76
77 if (uWord & UINT16_C(0xff00))
78 cDigits = uWord & UINT16_C(0xf000) ? 4 : 3;
79 else
80 cDigits = uWord & UINT16_C(0x00f0) ? 2 : 1;
81
82 off = 0;
83 switch (cDigits)
84 {
85 case 4: pszDst[off++] = g_szHexDigits[(uWord >> 12) & 0xf]; /* fall thru */
86 case 3: pszDst[off++] = g_szHexDigits[(uWord >> 8) & 0xf]; /* fall thru */
87 case 2: pszDst[off++] = g_szHexDigits[(uWord >> 4) & 0xf]; /* fall thru */
88 case 1: pszDst[off++] = g_szHexDigits[(uWord >> 0) & 0xf];
89 break;
90 }
91 pszDst[off] = '\0';
92 return off;
93 }
94
95
96 /**
97 * Helper function to format IPv6 address according to RFC 5952.
98 *
99 * @returns The number of bytes formatted.
100 * @param pfnOutput Pointer to output function.
101 * @param pvArgOutput Argument for the output function.
102 * @param pIpv6Addr IPv6 address
103 */
104 static size_t rtstrFormatIPv6(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PCRTNETADDRIPV6 pIpv6Addr)
105 {
106 size_t cch; /* result */
107 bool fEmbeddedIpv4;
108 size_t cwHexPart;
109 size_t cwLongestZeroRun;
110 size_t iLongestZeroStart;
111 size_t idx;
112 char szHexWord[8];
113
114 Assert(pIpv6Addr != NULL);
115
116 /*
117 * Check for embedded IPv4 address.
118 *
119 * IPv4-compatible - ::11.22.33.44 (obsolete)
120 * IPv4-mapped - ::ffff:11.22.33.44
121 * IPv4-translated - ::ffff:0:11.22.33.44 (RFC 2765)
122 */
123 fEmbeddedIpv4 = false;
124 cwHexPart = RT_ELEMENTS(pIpv6Addr->au16);
125 if ( pIpv6Addr->au64[0] == 0
126 && ( ( pIpv6Addr->au32[2] == 0
127 && pIpv6Addr->au32[3] != 0
128 && pIpv6Addr->au32[3] != RT_H2BE_U32_C(1) )
129 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0x0000ffff)
130 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0xffff0000) ) )
131 {
132 fEmbeddedIpv4 = true;
133 cwHexPart -= 2;
134 }
135
136 /*
137 * Find the longest sequences of two or more zero words.
138 */
139 cwLongestZeroRun = 0;
140 iLongestZeroStart = 0;
141 for (idx = 0; idx < cwHexPart; idx++)
142 if (pIpv6Addr->au16[idx] == 0)
143 {
144 size_t iZeroStart = idx;
145 size_t cwZeroRun;
146 do
147 idx++;
148 while (idx < cwHexPart && pIpv6Addr->au16[idx] == 0);
149 cwZeroRun = idx - iZeroStart;
150 if (cwZeroRun > 1 && cwZeroRun > cwLongestZeroRun)
151 {
152 cwLongestZeroRun = cwZeroRun;
153 iLongestZeroStart = iZeroStart;
154 if (cwZeroRun >= cwHexPart - idx)
155 break;
156 }
157 }
158
159 /*
160 * Do the formatting.
161 */
162 cch = 0;
163 if (cwLongestZeroRun == 0)
164 {
165 for (idx = 0; idx < cwHexPart; ++idx)
166 {
167 if (idx > 0)
168 cch += pfnOutput(pvArgOutput, ":", 1);
169 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
170 }
171
172 if (fEmbeddedIpv4)
173 cch += pfnOutput(pvArgOutput, ":", 1);
174 }
175 else
176 {
177 const size_t iLongestZeroEnd = iLongestZeroStart + cwLongestZeroRun;
178
179 if (iLongestZeroStart == 0)
180 cch += pfnOutput(pvArgOutput, ":", 1);
181 else
182 for (idx = 0; idx < iLongestZeroStart; ++idx)
183 {
184 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
185 cch += pfnOutput(pvArgOutput, ":", 1);
186 }
187
188 if (iLongestZeroEnd == cwHexPart)
189 cch += pfnOutput(pvArgOutput, ":", 1);
190 else
191 {
192 for (idx = iLongestZeroEnd; idx < cwHexPart; ++idx)
193 {
194 cch += pfnOutput(pvArgOutput, ":", 1);
195 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
196 }
197
198 if (fEmbeddedIpv4)
199 cch += pfnOutput(pvArgOutput, ":", 1);
200 }
201 }
202
203 if (fEmbeddedIpv4)
204 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
205 "%u.%u.%u.%u",
206 pIpv6Addr->au8[12],
207 pIpv6Addr->au8[13],
208 pIpv6Addr->au8[14],
209 pIpv6Addr->au8[15]);
210
211 return cch;
212 }
213
214
215 /**
216 * Callback to format iprt formatting extentions.
217 * See @ref pg_rt_str_format for a reference on the format types.
218 *
219 * @returns The number of bytes formatted.
220 * @param pfnOutput Pointer to output function.
221 * @param pvArgOutput Argument for the output function.
222 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
223 * after the format specifier.
224 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
225 * @param cchWidth Format Width. -1 if not specified.
226 * @param cchPrecision Format Precision. -1 if not specified.
227 * @param fFlags Flags (RTSTR_NTFS_*).
228 * @param chArgSize The argument size specifier, 'l' or 'L'.
229 */
230 DECLHIDDEN(size_t) rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs,
231 int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
232 {
233 const char *pszFormatOrg = *ppszFormat;
234 char ch = *(*ppszFormat)++;
235 size_t cch;
236 char szBuf[80];
237
238 if (ch == 'R')
239 {
240 ch = *(*ppszFormat)++;
241 switch (ch)
242 {
243 /*
244 * Groups 1 and 2.
245 */
246 case 'T':
247 case 'G':
248 case 'H':
249 case 'R':
250 case 'C':
251 case 'I':
252 case 'X':
253 case 'U':
254 case 'K':
255 {
256 /*
257 * Interpret the type.
258 */
259 typedef enum
260 {
261 RTSF_INT,
262 RTSF_INTW,
263 RTSF_BOOL,
264 RTSF_FP16,
265 RTSF_FP32,
266 RTSF_FP64,
267 RTSF_IPV4,
268 RTSF_IPV6,
269 RTSF_MAC,
270 RTSF_NETADDR,
271 RTSF_UUID
272 } RTSF;
273 static const struct
274 {
275 uint8_t cch; /**< the length of the string. */
276 char sz[10]; /**< the part following 'R'. */
277 uint8_t cb; /**< the size of the type. */
278 uint8_t u8Base; /**< the size of the type. */
279 RTSF enmFormat; /**< The way to format it. */
280 uint16_t fFlags; /**< additional RTSTR_F_* flags. */
281 }
282 /** Sorted array of types, looked up using binary search! */
283 s_aTypes[] =
284 {
285 #define STRMEM(str) sizeof(str) - 1, str
286 { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
287 { STRMEM("Cp"), sizeof(RTCCPHYS), 16, RTSF_INTW, 0 },
288 { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
289 { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
290 { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
291 { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
292 { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
293 { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
294 { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
295 { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
296 { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
297 { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
298 { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
299 { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
300 { STRMEM("Hr"), sizeof(RTHCUINTREG), 16, RTSF_INTW, 0 },
301 { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
302 { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
303 { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
304 { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
305 { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
306 { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
307 { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
308 { STRMEM("Kv"), sizeof(RTHCPTR), 16, RTSF_INT, RTSTR_F_OBFUSCATE_PTR },
309 { STRMEM("Rv"), sizeof(RTRCPTR), 16, RTSF_INTW, 0 },
310 { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
311 { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
312 { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
313 { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
314 { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
315 { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
316 { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
317 { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
318 { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
319 { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
320 { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
321 { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
322 { STRMEM("Tmac"), sizeof(PCRTMAC), 16, RTSF_MAC, 0 },
323 { STRMEM("Tnaddr"), sizeof(PCRTNETADDR), 10, RTSF_NETADDR,0 },
324 { STRMEM("Tnaipv4"), sizeof(RTNETADDRIPV4), 10, RTSF_IPV4, 0 },
325 { STRMEM("Tnaipv6"), sizeof(PCRTNETADDRIPV6),16, RTSF_IPV6, 0 },
326 { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
327 { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
328 { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
329 { STRMEM("Treg"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
330 { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
331 { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
332 { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
333 { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
334 { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
335 { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
336 { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
337 { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
338 { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
339 { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
340 { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
341 { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
342 { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
343 { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
344 { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
345 { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
346 { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
347 { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
348 #undef STRMEM
349 };
350 static const char s_szNull[] = "<NULL>";
351
352 const char *pszType = *ppszFormat - 1;
353 int iStart = 0;
354 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
355 int i = RT_ELEMENTS(s_aTypes) / 2;
356
357 union
358 {
359 uint8_t u8;
360 uint16_t u16;
361 uint32_t u32;
362 uint64_t u64;
363 int8_t i8;
364 int16_t i16;
365 int32_t i32;
366 int64_t i64;
367 RTR0INTPTR uR0Ptr;
368 RTFAR16 fp16;
369 RTFAR32 fp32;
370 RTFAR64 fp64;
371 bool fBool;
372 PCRTMAC pMac;
373 RTNETADDRIPV4 Ipv4Addr;
374 PCRTNETADDRIPV6 pIpv6Addr;
375 PCRTNETADDR pNetAddr;
376 PCRTUUID pUuid;
377 } u;
378
379 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
380 RT_NOREF_PV(chArgSize);
381
382 /*
383 * Lookup the type - binary search.
384 */
385 for (;;)
386 {
387 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
388 if (!iDiff)
389 break;
390 if (iEnd == iStart)
391 {
392 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
393 return 0;
394 }
395 if (iDiff < 0)
396 iEnd = i - 1;
397 else
398 iStart = i + 1;
399 if (iEnd < iStart)
400 {
401 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
402 return 0;
403 }
404 i = iStart + (iEnd - iStart) / 2;
405 }
406
407 /*
408 * Advance the format string and merge flags.
409 */
410 *ppszFormat += s_aTypes[i].cch - 1;
411 fFlags |= s_aTypes[i].fFlags;
412
413 /*
414 * Fetch the argument.
415 * It's important that a signed value gets sign-extended up to 64-bit.
416 */
417 RT_ZERO(u);
418 if (fFlags & RTSTR_F_VALSIGNED)
419 {
420 switch (s_aTypes[i].cb)
421 {
422 case sizeof(int8_t):
423 u.i64 = va_arg(*pArgs, /*int8_t*/int);
424 fFlags |= RTSTR_F_8BIT;
425 break;
426 case sizeof(int16_t):
427 u.i64 = va_arg(*pArgs, /*int16_t*/int);
428 fFlags |= RTSTR_F_16BIT;
429 break;
430 case sizeof(int32_t):
431 u.i64 = va_arg(*pArgs, int32_t);
432 fFlags |= RTSTR_F_32BIT;
433 break;
434 case sizeof(int64_t):
435 u.i64 = va_arg(*pArgs, int64_t);
436 fFlags |= RTSTR_F_64BIT;
437 break;
438 default:
439 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
440 break;
441 }
442 }
443 else
444 {
445 switch (s_aTypes[i].cb)
446 {
447 case sizeof(uint8_t):
448 u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
449 fFlags |= RTSTR_F_8BIT;
450 break;
451 case sizeof(uint16_t):
452 u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
453 fFlags |= RTSTR_F_16BIT;
454 break;
455 case sizeof(uint32_t):
456 u.u32 = va_arg(*pArgs, uint32_t);
457 fFlags |= RTSTR_F_32BIT;
458 break;
459 case sizeof(uint64_t):
460 u.u64 = va_arg(*pArgs, uint64_t);
461 fFlags |= RTSTR_F_64BIT;
462 break;
463 case sizeof(RTFAR32):
464 u.fp32 = va_arg(*pArgs, RTFAR32);
465 break;
466 case sizeof(RTFAR64):
467 u.fp64 = va_arg(*pArgs, RTFAR64);
468 break;
469 default:
470 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
471 break;
472 }
473 }
474
475 #ifndef DEBUG
476 /*
477 * For now don't show the address.
478 */
479 if (fFlags & RTSTR_F_OBFUSCATE_PTR)
480 {
481 cch = rtStrFormatKernelAddress(szBuf, sizeof(szBuf), u.uR0Ptr, cchWidth, cchPrecision, fFlags);
482 return pfnOutput(pvArgOutput, szBuf, cch);
483 }
484 #endif
485
486 /*
487 * Format the output.
488 */
489 switch (s_aTypes[i].enmFormat)
490 {
491 case RTSF_INT:
492 {
493 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
494 break;
495 }
496
497 /* hex which defaults to max width. */
498 case RTSF_INTW:
499 {
500 Assert(s_aTypes[i].u8Base == 16);
501 if (cchWidth < 0)
502 {
503 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
504 fFlags |= RTSTR_F_ZEROPAD;
505 }
506 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
507 break;
508 }
509
510 case RTSF_BOOL:
511 {
512 static const char s_szTrue[] = "true ";
513 static const char s_szFalse[] = "false";
514 if (u.u64 == 1)
515 return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
516 if (u.u64 == 0)
517 return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
518 /* invalid boolean value */
519 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
520 }
521
522 case RTSF_FP16:
523 {
524 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
525 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
526 Assert(cch == 4);
527 szBuf[4] = ':';
528 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
529 Assert(cch == 4);
530 cch = 4 + 1 + 4;
531 break;
532 }
533 case RTSF_FP32:
534 {
535 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
536 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
537 Assert(cch == 4);
538 szBuf[4] = ':';
539 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
540 Assert(cch == 8);
541 cch = 4 + 1 + 8;
542 break;
543 }
544 case RTSF_FP64:
545 {
546 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
547 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
548 Assert(cch == 4);
549 szBuf[4] = ':';
550 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
551 Assert(cch == 16);
552 cch = 4 + 1 + 16;
553 break;
554 }
555
556 case RTSF_IPV4:
557 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
558 "%u.%u.%u.%u",
559 u.Ipv4Addr.au8[0],
560 u.Ipv4Addr.au8[1],
561 u.Ipv4Addr.au8[2],
562 u.Ipv4Addr.au8[3]);
563
564 case RTSF_IPV6:
565 {
566 if (VALID_PTR(u.pIpv6Addr))
567 return rtstrFormatIPv6(pfnOutput, pvArgOutput, u.pIpv6Addr);
568 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
569 }
570
571 case RTSF_MAC:
572 {
573 if (VALID_PTR(u.pMac))
574 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
575 "%02x:%02x:%02x:%02x:%02x:%02x",
576 u.pMac->au8[0],
577 u.pMac->au8[1],
578 u.pMac->au8[2],
579 u.pMac->au8[3],
580 u.pMac->au8[4],
581 u.pMac->au8[5]);
582 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
583 }
584
585 case RTSF_NETADDR:
586 {
587 if (VALID_PTR(u.pNetAddr))
588 {
589 switch (u.pNetAddr->enmType)
590 {
591 case RTNETADDRTYPE_IPV4:
592 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
593 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
594 "%u.%u.%u.%u",
595 u.pNetAddr->uAddr.IPv4.au8[0],
596 u.pNetAddr->uAddr.IPv4.au8[1],
597 u.pNetAddr->uAddr.IPv4.au8[2],
598 u.pNetAddr->uAddr.IPv4.au8[3]);
599 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
600 "%u.%u.%u.%u:%u",
601 u.pNetAddr->uAddr.IPv4.au8[0],
602 u.pNetAddr->uAddr.IPv4.au8[1],
603 u.pNetAddr->uAddr.IPv4.au8[2],
604 u.pNetAddr->uAddr.IPv4.au8[3],
605 u.pNetAddr->uPort);
606
607 case RTNETADDRTYPE_IPV6:
608 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
609 return rtstrFormatIPv6(pfnOutput, pvArgOutput, &u.pNetAddr->uAddr.IPv6);
610
611 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
612 "[%RTnaipv6]:%u",
613 &u.pNetAddr->uAddr.IPv6,
614 u.pNetAddr->uPort);
615
616 case RTNETADDRTYPE_MAC:
617 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
618 "%02x:%02x:%02x:%02x:%02x:%02x",
619 u.pNetAddr->uAddr.Mac.au8[0],
620 u.pNetAddr->uAddr.Mac.au8[1],
621 u.pNetAddr->uAddr.Mac.au8[2],
622 u.pNetAddr->uAddr.Mac.au8[3],
623 u.pNetAddr->uAddr.Mac.au8[4],
624 u.pNetAddr->uAddr.Mac.au8[5]);
625
626 default:
627 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
628 "unsupported-netaddr-type=%u", u.pNetAddr->enmType);
629
630 }
631 }
632 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
633 }
634
635 case RTSF_UUID:
636 {
637 if (VALID_PTR(u.pUuid))
638 {
639 /* cannot call RTUuidToStr because of GC/R0. */
640 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
641 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
642 RT_H2LE_U32(u.pUuid->Gen.u32TimeLow),
643 RT_H2LE_U16(u.pUuid->Gen.u16TimeMid),
644 RT_H2LE_U16(u.pUuid->Gen.u16TimeHiAndVersion),
645 u.pUuid->Gen.u8ClockSeqHiAndReserved,
646 u.pUuid->Gen.u8ClockSeqLow,
647 u.pUuid->Gen.au8Node[0],
648 u.pUuid->Gen.au8Node[1],
649 u.pUuid->Gen.au8Node[2],
650 u.pUuid->Gen.au8Node[3],
651 u.pUuid->Gen.au8Node[4],
652 u.pUuid->Gen.au8Node[5]);
653 }
654 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
655 }
656
657 default:
658 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
659 return 0;
660 }
661
662 /*
663 * Finally, output the formatted string and return.
664 */
665 return pfnOutput(pvArgOutput, szBuf, cch);
666 }
667
668
669 /* Group 3 */
670
671 /*
672 * Base name printing.
673 */
674 case 'b':
675 {
676 switch (*(*ppszFormat)++)
677 {
678 case 'n':
679 {
680 const char *pszLastSep;
681 const char *psz = pszLastSep = va_arg(*pArgs, const char *);
682 if (!VALID_PTR(psz))
683 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
684
685 while ((ch = *psz) != '\0')
686 {
687 if (RTPATH_IS_SEP(ch))
688 {
689 do
690 psz++;
691 while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
692 if (!ch)
693 break;
694 pszLastSep = psz;
695 }
696 psz++;
697 }
698
699 return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
700 }
701
702 default:
703 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
704 break;
705 }
706 break;
707 }
708
709
710 /*
711 * Pretty function / method name printing.
712 */
713 case 'f':
714 {
715 switch (*(*ppszFormat)++)
716 {
717 /*
718 * Pretty function / method name printing.
719 * This isn't 100% right (see classic signal prototype) and it assumes
720 * standardized names, but it'll do for today.
721 */
722 case 'n':
723 {
724 const char *pszStart;
725 const char *psz = pszStart = va_arg(*pArgs, const char *);
726 int cAngle = 0;
727
728 if (!VALID_PTR(psz))
729 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
730
731 while ((ch = *psz) != '\0' && ch != '(')
732 {
733 if (RT_C_IS_BLANK(ch))
734 {
735 psz++;
736 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
737 psz++;
738 if (ch && cAngle == 0)
739 pszStart = psz;
740 }
741 else if (ch == '(')
742 break;
743 else if (ch == '<')
744 {
745 cAngle++;
746 psz++;
747 }
748 else if (ch == '>')
749 {
750 cAngle--;
751 psz++;
752 }
753 else
754 psz++;
755 }
756
757 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
758 }
759
760 default:
761 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
762 break;
763 }
764 break;
765 }
766
767
768 /*
769 * hex dumping and COM/XPCOM.
770 */
771 case 'h':
772 {
773 switch (*(*ppszFormat)++)
774 {
775 /*
776 * Hex stuff.
777 */
778 case 'x':
779 {
780 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
781 if (cchPrecision < 0)
782 cchPrecision = 16;
783 if (pu8)
784 {
785 switch (*(*ppszFormat)++)
786 {
787 /*
788 * Regular hex dump.
789 */
790 case 'd':
791 {
792 int off = 0;
793 cch = 0;
794
795 if (cchWidth <= 0)
796 cchWidth = 16;
797
798 while (off < cchPrecision)
799 {
800 int i;
801 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
802 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
803 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
804 off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
805 while (i++ < cchWidth)
806 cch += pfnOutput(pvArgOutput, " ", 3);
807
808 cch += pfnOutput(pvArgOutput, " ", 1);
809
810 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
811 {
812 uint8_t u8 = pu8[i];
813 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
814 }
815
816 /* next */
817 pu8 += cchWidth;
818 off += cchWidth;
819 }
820 return cch;
821 }
822
823 /*
824 * Hex string.
825 */
826 case 's':
827 {
828 if (cchPrecision-- > 0)
829 {
830 cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
831 for (; cchPrecision > 0; cchPrecision--, pu8++)
832 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
833 return cch;
834 }
835 break;
836 }
837
838 default:
839 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
840 break;
841 }
842 }
843 else
844 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
845 break;
846 }
847
848
849 #ifdef IN_RING3
850 /*
851 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
852 * ASSUMES: If Windows Then COM else XPCOM.
853 */
854 case 'r':
855 {
856 uint32_t hrc = va_arg(*pArgs, uint32_t);
857 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
858 switch (*(*ppszFormat)++)
859 {
860 case 'c':
861 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
862 case 'f':
863 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
864 case 'a':
865 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
866 default:
867 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
868 return 0;
869 }
870 break;
871 }
872 #endif /* IN_RING3 */
873
874 default:
875 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
876 return 0;
877
878 }
879 break;
880 }
881
882 /*
883 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
884 */
885 case 'r':
886 {
887 int rc = va_arg(*pArgs, int);
888 #ifdef IN_RING3 /* we don't want this anywhere else yet. */
889 PCRTSTATUSMSG pMsg = RTErrGet(rc);
890 switch (*(*ppszFormat)++)
891 {
892 case 'c':
893 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
894 case 's':
895 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
896 case 'f':
897 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
898 case 'a':
899 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
900 default:
901 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
902 return 0;
903 }
904 #else /* !IN_RING3 */
905 switch (*(*ppszFormat)++)
906 {
907 case 'c':
908 case 's':
909 case 'f':
910 case 'a':
911 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
912 default:
913 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
914 return 0;
915 }
916 #endif /* !IN_RING3 */
917 break;
918 }
919
920 #if defined(IN_RING3)
921 /*
922 * Windows status code: %Rwc, %Rwf, %Rwa
923 */
924 case 'w':
925 {
926 long rc = va_arg(*pArgs, long);
927 # if defined(RT_OS_WINDOWS)
928 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
929 # endif
930 switch (*(*ppszFormat)++)
931 {
932 # if defined(RT_OS_WINDOWS)
933 case 'c':
934 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
935 case 'f':
936 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
937 case 'a':
938 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
939 # else
940 case 'c':
941 case 'f':
942 case 'a':
943 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
944 # endif
945 default:
946 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
947 return 0;
948 }
949 break;
950 }
951 #endif /* IN_RING3 */
952
953 /*
954 * Group 4, structure dumpers.
955 */
956 case 'D':
957 {
958 /*
959 * Interpret the type.
960 */
961 typedef enum
962 {
963 RTST_TIMESPEC
964 } RTST;
965 /** Set if it's a pointer */
966 #define RTST_FLAGS_POINTER RT_BIT(0)
967 static const struct
968 {
969 uint8_t cch; /**< the length of the string. */
970 char sz[16-2]; /**< the part following 'R'. */
971 uint8_t cb; /**< the size of the argument. */
972 uint8_t fFlags; /**< RTST_FLAGS_* */
973 RTST enmType; /**< The structure type. */
974 }
975 /** Sorted array of types, looked up using binary search! */
976 s_aTypes[] =
977 {
978 #define STRMEM(str) sizeof(str) - 1, str
979 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
980 #undef STRMEM
981 };
982 const char *pszType = *ppszFormat - 1;
983 int iStart = 0;
984 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
985 int i = RT_ELEMENTS(s_aTypes) / 2;
986
987 union
988 {
989 const void *pv;
990 uint64_t u64;
991 PCRTTIMESPEC pTimeSpec;
992 } u;
993
994 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
995
996 /*
997 * Lookup the type - binary search.
998 */
999 for (;;)
1000 {
1001 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
1002 if (!iDiff)
1003 break;
1004 if (iEnd == iStart)
1005 {
1006 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1007 return 0;
1008 }
1009 if (iDiff < 0)
1010 iEnd = i - 1;
1011 else
1012 iStart = i + 1;
1013 if (iEnd < iStart)
1014 {
1015 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1016 return 0;
1017 }
1018 i = iStart + (iEnd - iStart) / 2;
1019 }
1020 *ppszFormat += s_aTypes[i].cch - 1;
1021
1022 /*
1023 * Fetch the argument.
1024 */
1025 u.u64 = 0;
1026 switch (s_aTypes[i].cb)
1027 {
1028 case sizeof(const void *):
1029 u.pv = va_arg(*pArgs, const void *);
1030 break;
1031 default:
1032 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
1033 break;
1034 }
1035
1036 /*
1037 * If it's a pointer, we'll check if it's valid before going on.
1038 */
1039 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
1040 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
1041
1042 /*
1043 * Format the output.
1044 */
1045 switch (s_aTypes[i].enmType)
1046 {
1047 case RTST_TIMESPEC:
1048 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
1049
1050 default:
1051 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
1052 break;
1053 }
1054 break;
1055 }
1056
1057 #ifdef IN_RING3
1058 /*
1059 * Group 5, XML / HTML escapers.
1060 */
1061 case 'M':
1062 {
1063 char chWhat = (*ppszFormat)[0];
1064 bool fAttr = chWhat == 'a';
1065 char chType = (*ppszFormat)[1];
1066 AssertMsgBreak(chWhat == 'a' || chWhat == 'e', ("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1067 *ppszFormat += 2;
1068 switch (chType)
1069 {
1070 case 's':
1071 {
1072 static const char s_szElemEscape[] = "<>&\"'";
1073 static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
1074 const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
1075 size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
1076 size_t cchOutput = 0;
1077 const char *pszStr = va_arg(*pArgs, char *);
1078 ssize_t cchStr;
1079 ssize_t offCur;
1080 ssize_t offLast;
1081
1082 if (!VALID_PTR(pszStr))
1083 pszStr = "<NULL>";
1084 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1085
1086 if (fAttr)
1087 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1088 if (!(fFlags & RTSTR_F_LEFT))
1089 while (--cchWidth >= cchStr)
1090 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1091
1092 offLast = offCur = 0;
1093 while (offCur < cchStr)
1094 {
1095 if (memchr(pszEscape, pszStr[offCur], cchEscape))
1096 {
1097 if (offLast < offCur)
1098 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1099 switch (pszStr[offCur])
1100 {
1101 case '<': cchOutput += pfnOutput(pvArgOutput, "&lt;", 4); break;
1102 case '>': cchOutput += pfnOutput(pvArgOutput, "&gt;", 4); break;
1103 case '&': cchOutput += pfnOutput(pvArgOutput, "&amp;", 5); break;
1104 case '\'': cchOutput += pfnOutput(pvArgOutput, "&apos;", 6); break;
1105 case '"': cchOutput += pfnOutput(pvArgOutput, "&quot;", 6); break;
1106 case '\n': cchOutput += pfnOutput(pvArgOutput, "&#xA;", 5); break;
1107 case '\r': cchOutput += pfnOutput(pvArgOutput, "&#xD;", 5); break;
1108 default:
1109 AssertFailed();
1110 }
1111 offLast = offCur + 1;
1112 }
1113 offCur++;
1114 }
1115 if (offLast < offCur)
1116 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1117
1118 while (--cchWidth >= cchStr)
1119 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1120 if (fAttr)
1121 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1122 return cchOutput;
1123 }
1124
1125 default:
1126 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1127 }
1128 break;
1129 }
1130 #endif /* IN_RING3 */
1131
1132
1133 /*
1134 * Groups 6 - CPU Architecture Register Formatters.
1135 * "%RAarch[reg]"
1136 */
1137 case 'A':
1138 {
1139 char const * const pszArch = *ppszFormat;
1140 const char *pszReg = pszArch;
1141 size_t cchOutput = 0;
1142 int cPrinted = 0;
1143 size_t cchReg;
1144
1145 /* Parse out the */
1146 while ((ch = *pszReg++) && ch != '[')
1147 { /* nothing */ }
1148 AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1149
1150 cchReg = 0;
1151 while ((ch = pszReg[cchReg]) && ch != ']')
1152 cchReg++;
1153 AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1154
1155 *ppszFormat = &pszReg[cchReg + 1];
1156
1157
1158 #define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
1159 #define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
1160 do { \
1161 if ((a_uVal) & (a_fBitMask)) \
1162 { \
1163 if (!cPrinted++) \
1164 cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
1165 else \
1166 cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
1167 (a_uVal) &= ~(a_fBitMask); \
1168 } \
1169 } while (0)
1170 #define REG_OUT_CLOSE(a_uVal) \
1171 do { \
1172 if ((a_uVal)) \
1173 { \
1174 cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
1175 cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
1176 cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
1177 cPrinted++; \
1178 } \
1179 if (cPrinted) \
1180 cchOutput += pfnOutput(pvArgOutput, "}", 1); \
1181 } while (0)
1182
1183
1184 if (0)
1185 { /* dummy */ }
1186 #ifdef STRFORMAT_WITH_X86
1187 /*
1188 * X86 & AMD64.
1189 */
1190 else if ( pszReg - pszArch == 3 + 1
1191 && pszArch[0] == 'x'
1192 && pszArch[1] == '8'
1193 && pszArch[2] == '6')
1194 {
1195 if (REG_EQUALS("cr0"))
1196 {
1197 uint64_t cr0 = va_arg(*pArgs, uint64_t);
1198 fFlags |= RTSTR_F_64BIT;
1199 cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1200 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1201 REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
1202 REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
1203 REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
1204 REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
1205 REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
1206 REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
1207 REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
1208 REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
1209 REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
1210 REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
1211 REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
1212 REG_OUT_CLOSE(cr0);
1213 }
1214 else if (REG_EQUALS("cr4"))
1215 {
1216 uint64_t cr4 = va_arg(*pArgs, uint64_t);
1217 fFlags |= RTSTR_F_64BIT;
1218 cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1219 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1220 REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
1221 REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
1222 REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
1223 REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
1224 REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
1225 REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
1226 REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
1227 REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
1228 REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
1229 REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
1230 REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
1231 REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
1232 REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
1233 REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
1234 REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
1235 REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
1236 REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
1237 REG_OUT_CLOSE(cr4);
1238 }
1239 else
1240 AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
1241 }
1242 #endif
1243 else
1244 AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
1245 #undef REG_OUT_BIT
1246 #undef REG_OUT_CLOSE
1247 #undef REG_EQUALS
1248 return cchOutput;
1249 }
1250
1251 /*
1252 * Invalid/Unknown. Bitch about it.
1253 */
1254 default:
1255 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1256 break;
1257 }
1258 }
1259 else
1260 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1261
1262 NOREF(pszFormatOrg);
1263 return 0;
1264 }
1265