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