VirtualBox

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

最後變更 在這個檔案從66731是 66731,由 vboxsync 提交於 8 年 前

iprt/string.h,iprt/utf16.h: Added some big endian UTF-16 related functions/features.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 56.8 KB
 
1/* $Id: strformatrt.cpp 66731 2017-05-01 23:21:06Z vboxsync $ */
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.alldomusa.eu.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]; /* 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 */
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 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, big endian UTF-16.
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 /* %lRbs */
703 case 's':
704 if (chArgSize == 'l')
705 {
706 /* utf-16BE -> utf-8 */
707 int cchStr;
708 PCRTUTF16 pwszStr = va_arg(*pArgs, PRTUTF16);
709
710 if (RT_VALID_PTR(pwszStr))
711 {
712 cchStr = 0;
713 while (cchStr < cchPrecision && pwszStr[cchStr] != '\0')
714 cchStr++;
715 }
716 else
717 {
718 static RTUTF16 s_wszBigNull[] =
719 {
720 RT_H2BE_U16_C((uint16_t)'<'), RT_H2BE_U16_C((uint16_t)'N'), RT_H2BE_U16_C((uint16_t)'U'),
721 RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'L'), RT_H2BE_U16_C((uint16_t)'>'), '\0'
722 };
723 pwszStr = s_wszBigNull;
724 cchStr = RT_ELEMENTS(s_wszBigNull) - 1;
725 }
726
727 cch = 0;
728 if (!(fFlags & RTSTR_F_LEFT))
729 while (--cchWidth >= cchStr)
730 cch += pfnOutput(pvArgOutput, " ", 1);
731 cchWidth -= cchStr;
732 while (cchStr-- > 0)
733 {
734/** @todo \#ifndef IN_RC*/
735#ifdef IN_RING3
736 RTUNICP Cp = 0;
737 RTUtf16BigGetCpEx(&pwszStr, &Cp);
738 char *pszEnd = RTStrPutCp(szBuf, Cp);
739 *pszEnd = '\0';
740 cch += pfnOutput(pvArgOutput, szBuf, pszEnd - szBuf);
741#else
742 char ch = (char)(*pwszStr++ >> 8);
743 cch += pfnOutput(pvArgOutput, &ch, 1);
744#endif
745 }
746 while (--cchWidth >= 0)
747 cch += pfnOutput(pvArgOutput, " ", 1);
748 return cch;
749 }
750 /* fall thru */
751
752 default:
753 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
754 break;
755 }
756 break;
757 }
758
759
760 /*
761 * Pretty function / method name printing.
762 */
763 case 'f':
764 {
765 switch (*(*ppszFormat)++)
766 {
767 /*
768 * Pretty function / method name printing.
769 * This isn't 100% right (see classic signal prototype) and it assumes
770 * standardized names, but it'll do for today.
771 */
772 case 'n':
773 {
774 const char *pszStart;
775 const char *psz = pszStart = va_arg(*pArgs, const char *);
776 int cAngle = 0;
777
778 if (!VALID_PTR(psz))
779 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
780
781 while ((ch = *psz) != '\0' && ch != '(')
782 {
783 if (RT_C_IS_BLANK(ch))
784 {
785 psz++;
786 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
787 psz++;
788 if (ch && cAngle == 0)
789 pszStart = psz;
790 }
791 else if (ch == '(')
792 break;
793 else if (ch == '<')
794 {
795 cAngle++;
796 psz++;
797 }
798 else if (ch == '>')
799 {
800 cAngle--;
801 psz++;
802 }
803 else
804 psz++;
805 }
806
807 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
808 }
809
810 default:
811 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
812 break;
813 }
814 break;
815 }
816
817
818 /*
819 * hex dumping and COM/XPCOM.
820 */
821 case 'h':
822 {
823 switch (*(*ppszFormat)++)
824 {
825 /*
826 * Hex stuff.
827 */
828 case 'x':
829 {
830 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
831 if (cchPrecision < 0)
832 cchPrecision = 16;
833 if (pu8)
834 {
835 switch (*(*ppszFormat)++)
836 {
837 /*
838 * Regular hex dump.
839 */
840 case 'd':
841 {
842 int off = 0;
843 cch = 0;
844
845 if (cchWidth <= 0)
846 cchWidth = 16;
847
848 while (off < cchPrecision)
849 {
850 int i;
851 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
852 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
853 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
854 off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
855 while (i++ < cchWidth)
856 cch += pfnOutput(pvArgOutput, " ", 3);
857
858 cch += pfnOutput(pvArgOutput, " ", 1);
859
860 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
861 {
862 uint8_t u8 = pu8[i];
863 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
864 }
865
866 /* next */
867 pu8 += cchWidth;
868 off += cchWidth;
869 }
870 return cch;
871 }
872
873 /*
874 * Hex string.
875 */
876 case 's':
877 {
878 if (cchPrecision-- > 0)
879 {
880 cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
881 for (; cchPrecision > 0; cchPrecision--, pu8++)
882 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
883 return cch;
884 }
885 break;
886 }
887
888 default:
889 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
890 break;
891 }
892 }
893 else
894 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
895 break;
896 }
897
898
899#ifdef IN_RING3
900 /*
901 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
902 * ASSUMES: If Windows Then COM else XPCOM.
903 */
904 case 'r':
905 {
906 uint32_t hrc = va_arg(*pArgs, uint32_t);
907 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
908 switch (*(*ppszFormat)++)
909 {
910 case 'c':
911 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
912 case 'f':
913 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
914 case 'a':
915 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
916 default:
917 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
918 return 0;
919 }
920 break;
921 }
922#endif /* IN_RING3 */
923
924 default:
925 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
926 return 0;
927
928 }
929 break;
930 }
931
932 /*
933 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
934 */
935 case 'r':
936 {
937 int rc = va_arg(*pArgs, int);
938#ifdef IN_RING3 /* we don't want this anywhere else yet. */
939 PCRTSTATUSMSG pMsg = RTErrGet(rc);
940 switch (*(*ppszFormat)++)
941 {
942 case 'c':
943 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
944 case 's':
945 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
946 case 'f':
947 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
948 case 'a':
949 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
950 default:
951 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
952 return 0;
953 }
954#else /* !IN_RING3 */
955 switch (*(*ppszFormat)++)
956 {
957 case 'c':
958 case 's':
959 case 'f':
960 case 'a':
961 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
962 default:
963 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
964 return 0;
965 }
966#endif /* !IN_RING3 */
967 break;
968 }
969
970#if defined(IN_RING3)
971 /*
972 * Windows status code: %Rwc, %Rwf, %Rwa
973 */
974 case 'w':
975 {
976 long rc = va_arg(*pArgs, long);
977# if defined(RT_OS_WINDOWS)
978 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
979# endif
980 switch (*(*ppszFormat)++)
981 {
982# if defined(RT_OS_WINDOWS)
983 case 'c':
984 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
985 case 'f':
986 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
987 case 'a':
988 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
989# else
990 case 'c':
991 case 'f':
992 case 'a':
993 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
994# endif
995 default:
996 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
997 return 0;
998 }
999 break;
1000 }
1001#endif /* IN_RING3 */
1002
1003 /*
1004 * Group 4, structure dumpers.
1005 */
1006 case 'D':
1007 {
1008 /*
1009 * Interpret the type.
1010 */
1011 typedef enum
1012 {
1013 RTST_TIMESPEC
1014 } RTST;
1015/** Set if it's a pointer */
1016#define RTST_FLAGS_POINTER RT_BIT(0)
1017 static const struct
1018 {
1019 uint8_t cch; /**< the length of the string. */
1020 char sz[16-2]; /**< the part following 'R'. */
1021 uint8_t cb; /**< the size of the argument. */
1022 uint8_t fFlags; /**< RTST_FLAGS_* */
1023 RTST enmType; /**< The structure type. */
1024 }
1025 /** Sorted array of types, looked up using binary search! */
1026 s_aTypes[] =
1027 {
1028#define STRMEM(str) sizeof(str) - 1, str
1029 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
1030#undef STRMEM
1031 };
1032 const char *pszType = *ppszFormat - 1;
1033 int iStart = 0;
1034 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
1035 int i = RT_ELEMENTS(s_aTypes) / 2;
1036
1037 union
1038 {
1039 const void *pv;
1040 uint64_t u64;
1041 PCRTTIMESPEC pTimeSpec;
1042 } u;
1043
1044 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
1045
1046 /*
1047 * Lookup the type - binary search.
1048 */
1049 for (;;)
1050 {
1051 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
1052 if (!iDiff)
1053 break;
1054 if (iEnd == iStart)
1055 {
1056 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1057 return 0;
1058 }
1059 if (iDiff < 0)
1060 iEnd = i - 1;
1061 else
1062 iStart = i + 1;
1063 if (iEnd < iStart)
1064 {
1065 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1066 return 0;
1067 }
1068 i = iStart + (iEnd - iStart) / 2;
1069 }
1070 *ppszFormat += s_aTypes[i].cch - 1;
1071
1072 /*
1073 * Fetch the argument.
1074 */
1075 u.u64 = 0;
1076 switch (s_aTypes[i].cb)
1077 {
1078 case sizeof(const void *):
1079 u.pv = va_arg(*pArgs, const void *);
1080 break;
1081 default:
1082 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
1083 break;
1084 }
1085
1086 /*
1087 * If it's a pointer, we'll check if it's valid before going on.
1088 */
1089 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
1090 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
1091
1092 /*
1093 * Format the output.
1094 */
1095 switch (s_aTypes[i].enmType)
1096 {
1097 case RTST_TIMESPEC:
1098 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
1099
1100 default:
1101 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
1102 break;
1103 }
1104 break;
1105 }
1106
1107#ifdef IN_RING3
1108 /*
1109 * Group 5, XML / HTML escapers.
1110 */
1111 case 'M':
1112 {
1113 char chWhat = (*ppszFormat)[0];
1114 bool fAttr = chWhat == 'a';
1115 char chType = (*ppszFormat)[1];
1116 AssertMsgBreak(chWhat == 'a' || chWhat == 'e', ("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1117 *ppszFormat += 2;
1118 switch (chType)
1119 {
1120 case 's':
1121 {
1122 static const char s_szElemEscape[] = "<>&\"'";
1123 static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
1124 const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
1125 size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
1126 size_t cchOutput = 0;
1127 const char *pszStr = va_arg(*pArgs, char *);
1128 ssize_t cchStr;
1129 ssize_t offCur;
1130 ssize_t offLast;
1131
1132 if (!VALID_PTR(pszStr))
1133 pszStr = "<NULL>";
1134 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1135
1136 if (fAttr)
1137 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1138 if (!(fFlags & RTSTR_F_LEFT))
1139 while (--cchWidth >= cchStr)
1140 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1141
1142 offLast = offCur = 0;
1143 while (offCur < cchStr)
1144 {
1145 if (memchr(pszEscape, pszStr[offCur], cchEscape))
1146 {
1147 if (offLast < offCur)
1148 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1149 switch (pszStr[offCur])
1150 {
1151 case '<': cchOutput += pfnOutput(pvArgOutput, "&lt;", 4); break;
1152 case '>': cchOutput += pfnOutput(pvArgOutput, "&gt;", 4); break;
1153 case '&': cchOutput += pfnOutput(pvArgOutput, "&amp;", 5); break;
1154 case '\'': cchOutput += pfnOutput(pvArgOutput, "&apos;", 6); break;
1155 case '"': cchOutput += pfnOutput(pvArgOutput, "&quot;", 6); break;
1156 case '\n': cchOutput += pfnOutput(pvArgOutput, "&#xA;", 5); break;
1157 case '\r': cchOutput += pfnOutput(pvArgOutput, "&#xD;", 5); break;
1158 default:
1159 AssertFailed();
1160 }
1161 offLast = offCur + 1;
1162 }
1163 offCur++;
1164 }
1165 if (offLast < offCur)
1166 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1167
1168 while (--cchWidth >= cchStr)
1169 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1170 if (fAttr)
1171 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1172 return cchOutput;
1173 }
1174
1175 default:
1176 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1177 }
1178 break;
1179 }
1180#endif /* IN_RING3 */
1181
1182
1183 /*
1184 * Groups 6 - CPU Architecture Register Formatters.
1185 * "%RAarch[reg]"
1186 */
1187 case 'A':
1188 {
1189 char const * const pszArch = *ppszFormat;
1190 const char *pszReg = pszArch;
1191 size_t cchOutput = 0;
1192 int cPrinted = 0;
1193 size_t cchReg;
1194
1195 /* Parse out the */
1196 while ((ch = *pszReg++) && ch != '[')
1197 { /* nothing */ }
1198 AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1199
1200 cchReg = 0;
1201 while ((ch = pszReg[cchReg]) && ch != ']')
1202 cchReg++;
1203 AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1204
1205 *ppszFormat = &pszReg[cchReg + 1];
1206
1207
1208#define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
1209#define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
1210 do { \
1211 if ((a_uVal) & (a_fBitMask)) \
1212 { \
1213 if (!cPrinted++) \
1214 cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
1215 else \
1216 cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
1217 (a_uVal) &= ~(a_fBitMask); \
1218 } \
1219 } while (0)
1220#define REG_OUT_CLOSE(a_uVal) \
1221 do { \
1222 if ((a_uVal)) \
1223 { \
1224 cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
1225 cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
1226 cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
1227 cPrinted++; \
1228 } \
1229 if (cPrinted) \
1230 cchOutput += pfnOutput(pvArgOutput, "}", 1); \
1231 } while (0)
1232
1233
1234 if (0)
1235 { /* dummy */ }
1236#ifdef STRFORMAT_WITH_X86
1237 /*
1238 * X86 & AMD64.
1239 */
1240 else if ( pszReg - pszArch == 3 + 1
1241 && pszArch[0] == 'x'
1242 && pszArch[1] == '8'
1243 && pszArch[2] == '6')
1244 {
1245 if (REG_EQUALS("cr0"))
1246 {
1247 uint64_t cr0 = va_arg(*pArgs, uint64_t);
1248 fFlags |= RTSTR_F_64BIT;
1249 cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1250 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1251 REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
1252 REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
1253 REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
1254 REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
1255 REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
1256 REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
1257 REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
1258 REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
1259 REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
1260 REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
1261 REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
1262 REG_OUT_CLOSE(cr0);
1263 }
1264 else if (REG_EQUALS("cr4"))
1265 {
1266 uint64_t cr4 = va_arg(*pArgs, uint64_t);
1267 fFlags |= RTSTR_F_64BIT;
1268 cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1269 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1270 REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
1271 REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
1272 REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
1273 REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
1274 REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
1275 REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
1276 REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
1277 REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
1278 REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
1279 REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
1280 REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
1281 REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
1282 REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
1283 REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
1284 REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
1285 REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
1286 REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
1287 REG_OUT_CLOSE(cr4);
1288 }
1289 else
1290 AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
1291 }
1292#endif
1293 else
1294 AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
1295#undef REG_OUT_BIT
1296#undef REG_OUT_CLOSE
1297#undef REG_EQUALS
1298 return cchOutput;
1299 }
1300
1301 /*
1302 * Invalid/Unknown. Bitch about it.
1303 */
1304 default:
1305 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1306 break;
1307 }
1308 }
1309 else
1310 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1311
1312 NOREF(pszFormatOrg);
1313 return 0;
1314}
1315
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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