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