VirtualBox

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

Last change on this file since 57358 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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