VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-StrFormatV.c@ 60367

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

bs3kit: Use \#define with BS3_DATA_NM to map data symbols to names accessible in all context. (Underscores in 16-bit and 32-bit, no underscores in 64-bit.) Detect PSE.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.7 KB
Line 
1/* $Id: bs3-cmn-StrFormatV.c 60311 2016-04-04 17:01:14Z vboxsync $ */
2/** @file
3 * BS3Kit - Bs3StrFormatV
4 */
5
6/*
7 * Copyright (C) 2007-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "bs3kit-template-header.h"
32#include <iprt/ctype.h>
33
34
35/*********************************************************************************************************************************
36* Defined Constants And Macros *
37*********************************************************************************************************************************/
38#define STR_F_CAPITAL 0x0001
39#define STR_F_LEFT 0x0002
40#define STR_F_ZEROPAD 0x0004
41#define STR_F_SPECIAL 0x0008
42#define STR_F_VALSIGNED 0x0010
43#define STR_F_PLUS 0x0020
44#define STR_F_BLANK 0x0040
45#define STR_F_WIDTH 0x0080
46#define STR_F_PRECISION 0x0100
47#define STR_F_THOUSAND_SEP 0x0200
48#define STR_F_NEGATIVE 0x0400 /**< Used to indicated '-' must be printed. */
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54/** Size of the temporary buffer. */
55#define BS3FMT_TMP_SIZE 64
56
57/**
58 * BS3kit string format state.
59 */
60typedef struct BS3FMTSTATE
61{
62 /** The output function. */
63 PFNBS3STRFORMATOUTPUT pfnOutput;
64 /** User argument for pfnOutput. */
65 void BS3_FAR *pvUser;
66
67 /** STR_F_XXX flags. */
68 unsigned fFlags;
69 /** The width when STR_F_WIDTH is specific. */
70 int cchWidth;
71 /** The width when STR_F_PRECISION is specific. */
72 int cchPrecision;
73 /** The number format base. */
74 unsigned uBase;
75 /** Temporary buffer. */
76 char szTmp[BS3FMT_TMP_SIZE];
77} BS3FMTSTATE;
78/** Pointer to a BS3Kit string formatter state. */
79typedef BS3FMTSTATE *PBS3FMTSTATE;
80
81
82
83/*********************************************************************************************************************************
84* Internal Functions *
85*********************************************************************************************************************************/
86#if ARCH_BITS != 64
87static size_t bs3StrFormatU32(PBS3FMTSTATE pState, uint32_t uValue);
88#endif
89
90
91
92/**
93 * Formats a number string.
94 *
95 * @returns Number of chars printed.
96 * @param pState The string formatter state.
97 * @param pszNumber The formatted number string.
98 * @param cchNumber The length of the number.
99 */
100static size_t bs3StrFormatNumberString(PBS3FMTSTATE pState, char const *pszNumber, size_t cchNumber)
101{
102 /*
103 * Calc the length of the core number with prefixes.
104 */
105 size_t cchActual = 0;
106 size_t cchRet = cchNumber;
107
108 /* Accunt for sign char. */
109 cchRet += !!(pState->fFlags & (STR_F_NEGATIVE | STR_F_PLUS | STR_F_BLANK));
110
111 /* Account for the hex prefix: '0x' or '0X' */
112 if (pState->fFlags & STR_F_SPECIAL)
113 {
114 cchRet += 2;
115 BS3_ASSERT(pState->uBase == 16);
116 }
117
118 /* Account for thousand separators (applied while printing). */
119 if (pState->fFlags & STR_F_THOUSAND_SEP)
120 cchRet += (cchNumber - 1) / (pState->uBase == 10 ? 3 : 8);
121
122 /*
123 * Do left blank padding.
124 */
125 if ((pState->fFlags & (STR_F_ZEROPAD | STR_F_LEFT | STR_F_WIDTH)) == STR_F_WIDTH)
126 while (cchRet < pState->cchWidth)
127 {
128 cchActual += pState->pfnOutput(' ', pState->pvUser);
129 cchRet++;
130 }
131
132 /*
133 * Sign indicator / space.
134 */
135 if (pState->fFlags & (STR_F_NEGATIVE | STR_F_PLUS | STR_F_BLANK))
136 {
137 char ch;
138 if (pState->fFlags & STR_F_NEGATIVE)
139 ch = '-';
140 else if (pState->fFlags & STR_F_PLUS)
141 ch = '+';
142 else
143 ch = ' ';
144 cchActual += pState->pfnOutput(ch, pState->pvUser);
145 }
146
147 /*
148 * Hex prefix.
149 */
150 if (pState->fFlags & STR_F_SPECIAL)
151 {
152 cchActual += pState->pfnOutput('0', pState->pvUser);
153 cchActual += pState->pfnOutput(!(pState->fFlags & STR_F_CAPITAL) ? 'x' : 'X', pState->pvUser);
154 }
155
156 /*
157 * Zero padding.
158 */
159 if (pState->fFlags & STR_F_ZEROPAD)
160 while (cchRet < pState->cchWidth)
161 {
162 cchActual += pState->pfnOutput('0', pState->pvUser);
163 cchRet++;
164 }
165
166 /*
167 * Output the number.
168 */
169 if ( !(pState->fFlags & STR_F_THOUSAND_SEP)
170 || cchNumber < 4)
171 while (cchNumber-- > 0)
172 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
173 else
174 {
175 char const chSep = pState->uBase == 10 ? ' ' : '\'';
176 unsigned const cchEvery = pState->uBase == 10 ? 3 : 8;
177 unsigned cchLeft = --cchNumber % cchEvery;
178
179 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
180 while (cchNumber-- > 0)
181 {
182 if (cchLeft == 0)
183 {
184 cchActual += pState->pfnOutput(chSep, pState->pvUser);
185 cchLeft = cchEvery;
186 }
187 cchLeft--;
188 cchActual += pState->pfnOutput(*pszNumber++, pState->pvUser);
189 }
190 }
191
192 /*
193 * Do right blank padding.
194 */
195 if ((pState->fFlags & (STR_F_ZEROPAD | STR_F_LEFT | STR_F_WIDTH)) == (STR_F_WIDTH | STR_F_LEFT))
196 while (cchRet < pState->cchWidth)
197 {
198 cchActual += pState->pfnOutput(' ', pState->pvUser);
199 cchRet++;
200 }
201
202 return cchActual;
203}
204
205
206/**
207 * Format a 64-bit number.
208 *
209 * @returns Number of characters.
210 * @param pState The string formatter state.
211 * @param uValue The value.
212 */
213static size_t bs3StrFormatU64(PBS3FMTSTATE pState, uint64_t uValue)
214{
215#if ARCH_BITS != 64
216 /* Avoid 64-bit division by formatting 64-bit numbers as hex if they're higher than _4G. */
217 if ( pState->uBase == 10
218 && !(uValue >> 32)) /* uValue <= UINT32_MAX does not work, trouble with 64-bit compile time math! */
219 return bs3StrFormatU32(pState, uValue);
220 pState->fFlags |= STR_F_SPECIAL;
221 pState->uBase = 16;
222#endif
223
224 {
225 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL) ? g_achBs3HexDigits : g_achBs3HexDigitsUpper;
226 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
227
228 *--psz = '\0';
229#if ARCH_BITS == 64
230 if (pState->uBase == 10)
231 {
232 do
233 {
234 *--psz = pachDigits[uValue % 10];
235 uValue /= 10;
236 } while (uValue > 0);
237 }
238 else
239#endif
240 {
241 BS3_ASSERT(pState->uBase == 16);
242 do
243 {
244 *--psz = pachDigits[uValue & 0xf];
245 uValue >>= 4;
246 } while (uValue > 0);
247 }
248 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
249 }
250}
251
252
253/**
254 * Format a 32-bit number.
255 *
256 * @returns Number of characters.
257 * @param pState The string formatter state.
258 * @param uValue The value.
259 */
260static size_t bs3StrFormatU32(PBS3FMTSTATE pState, uint32_t uValue)
261{
262#if ARCH_BITS < 64
263 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL) ? g_achBs3HexDigits : g_achBs3HexDigitsUpper;
264 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
265
266 *--psz = '\0';
267 if (pState->uBase == 10)
268 {
269 do
270 {
271 *--psz = pachDigits[uValue % 10];
272 uValue /= 10;
273 } while (uValue > 0);
274 }
275 else
276 {
277 BS3_ASSERT(pState->uBase == 16);
278 do
279 {
280 *--psz = pachDigits[uValue & 0xf];
281 uValue >>= 4;
282 } while (uValue > 0);
283 }
284 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
285
286#else
287 /* We've got native 64-bit division, save space. */
288 return bs3StrFormatU64(pState, uValue);
289#endif
290}
291
292
293#if ARCH_BITS == 16
294/**
295 * Format a 16-bit number.
296 *
297 * @returns Number of characters.
298 * @param pState The string formatter state.
299 * @param uValue The value.
300 */
301static size_t bs3StrFormatU16(PBS3FMTSTATE pState, uint16_t uValue)
302{
303 if (pState->uBase == 10)
304 {
305 const char *pachDigits = !(pState->fFlags & STR_F_CAPITAL)
306 ? g_achBs3HexDigits : g_achBs3HexDigitsUpper;
307 char *psz = &pState->szTmp[BS3FMT_TMP_SIZE];
308
309 *--psz = '\0';
310 do
311 {
312 *--psz = pachDigits[uValue % 10];
313 uValue /= 10;
314 } while (uValue > 0);
315 return bs3StrFormatNumberString(pState, psz, &pState->szTmp[BS3FMT_TMP_SIZE - 1] - psz);
316 }
317
318 /*
319 * 32-bit shifting is reasonably cheap and inlined, so combine with 32-bit.
320 */
321 return bs3StrFormatU32(pState, uValue);
322}
323#endif
324
325
326static size_t bs3StrFormatS64(PBS3FMTSTATE pState, int32_t iValue)
327{
328 if (iValue < 0)
329 {
330 iValue = -iValue;
331 pState->fFlags |= STR_F_NEGATIVE;
332 }
333 return bs3StrFormatU64(pState, iValue);
334}
335
336
337static size_t bs3StrFormatS32(PBS3FMTSTATE pState, int32_t iValue)
338{
339 if (iValue < 0)
340 {
341 iValue = -iValue;
342 pState->fFlags |= STR_F_NEGATIVE;
343 }
344 return bs3StrFormatU32(pState, iValue);
345}
346
347
348#if ARCH_BITS == 16
349static size_t bs3StrFormatS16(PBS3FMTSTATE pState, int16_t iValue)
350{
351 if (iValue < 0)
352 {
353 iValue = -iValue;
354 pState->fFlags |= STR_F_NEGATIVE;
355 }
356 return bs3StrFormatU16(pState, iValue);
357}
358#endif
359
360
361BS3_DECL(size_t) Bs3StrFormatV(const char BS3_FAR *pszFormat, va_list va,
362 PFNBS3STRFORMATOUTPUT pfnOutput, void BS3_FAR *pvUser)
363{
364 BS3FMTSTATE State;
365 size_t cchRet = 0;
366 char ch;
367
368 State.pfnOutput = pfnOutput;
369 State.pvUser = pvUser;
370
371 while ((ch = *pszFormat++) != '\0')
372 {
373 char chArgSize;
374
375 /*
376 * Deal with plain chars.
377 */
378 if (ch != '%')
379 {
380 cchRet += State.pfnOutput(ch, State.pvUser);
381 continue;
382 }
383
384 ch = *pszFormat++;
385 if (ch == '%')
386 {
387 cchRet += State.pfnOutput(ch, State.pvUser);
388 continue;
389 }
390
391 /*
392 * Flags.
393 */
394 State.fFlags = 0;
395 for (;;)
396 {
397 unsigned int fThis;
398 switch (ch)
399 {
400 default: fThis = 0; break;
401 case '#': fThis = STR_F_SPECIAL; break;
402 case '-': fThis = STR_F_LEFT; break;
403 case '+': fThis = STR_F_PLUS; break;
404 case ' ': fThis = STR_F_BLANK; break;
405 case '0': fThis = STR_F_ZEROPAD; break;
406 case '\'': fThis = STR_F_THOUSAND_SEP; break;
407 }
408 if (!fThis)
409 break;
410 State.fFlags |= fThis;
411 ch = *pszFormat++;
412 }
413
414 /*
415 * Width.
416 */
417 State.cchWidth = 0;
418 if (RT_C_IS_DIGIT(ch))
419 {
420 do
421 {
422 State.cchWidth *= 10;
423 State.cchWidth = ch - '0';
424 ch = *pszFormat++;
425 } while (RT_C_IS_DIGIT(ch));
426 State.fFlags |= STR_F_WIDTH;
427 }
428 else if (ch == '*')
429 {
430 State.cchWidth = va_arg(va, int);
431 if (State.cchWidth < 0)
432 {
433 State.cchWidth = -State.cchWidth;
434 State.fFlags |= STR_F_LEFT;
435 }
436 State.fFlags |= STR_F_WIDTH;
437 }
438
439 /*
440 * Precision
441 */
442 State.cchPrecision = 0;
443 if (RT_C_IS_DIGIT(ch))
444 {
445 do
446 {
447 State.cchPrecision *= 10;
448 State.cchPrecision = ch - '0';
449 ch = *pszFormat++;
450 } while (RT_C_IS_DIGIT(ch));
451 State.fFlags |= STR_F_PRECISION;
452 }
453 else if (ch == '*')
454 {
455 State.cchPrecision = va_arg(va, int);
456 if (State.cchPrecision < 0)
457 State.cchPrecision = 0;
458 State.fFlags |= STR_F_PRECISION;
459 }
460
461 /*
462 * Argument size.
463 */
464 chArgSize = ch;
465 switch (ch)
466 {
467 default:
468 chArgSize = 0;
469 break;
470
471 case 'z':
472 case 'L':
473 case 'j':
474 case 't':
475 ch = *pszFormat++;
476 break;
477
478 case 'l':
479 ch = *pszFormat++;
480 if (ch == 'l')
481 {
482 chArgSize = 'L';
483 ch = *pszFormat++;
484 }
485 break;
486
487 case 'h':
488 ch = *pszFormat++;
489 if (ch == 'h')
490 {
491 chArgSize = 'H';
492 ch = *pszFormat++;
493 }
494 break;
495 }
496
497 /*
498 * The type.
499 */
500 switch (ch)
501 {
502 /*
503 * Char
504 */
505 case 'c':
506 {
507 char ch = va_arg(va, int /*char*/);
508 cchRet += State.pfnOutput(ch, State.pvUser);
509 break;
510 }
511
512 /*
513 * String.
514 */
515 case 's':
516 {
517 const char BS3_FAR *psz = va_arg(va, const char BS3_FAR *);
518 size_t cch;
519 if (psz != NULL)
520 cch = Bs3StrNLen(psz, State.fFlags & STR_F_PRECISION ? RT_ABS(State.cchPrecision) : ~(size_t)0);
521 else
522 {
523 psz = "<NULL>";
524 cch = 6;
525 }
526
527 if ((State.fFlags & (STR_F_LEFT | STR_F_WIDTH)) == STR_F_WIDTH)
528 while (--State.cchWidth >= cch)
529 cchRet += State.pfnOutput(' ', State.pvUser);
530
531 cchRet += cch;
532 while (cch-- > 0)
533 cchRet += State.pfnOutput(*psz++, State.pvUser);
534
535 if ((State.fFlags & (STR_F_LEFT | STR_F_WIDTH)) == (STR_F_LEFT | STR_F_WIDTH))
536 while (--State.cchWidth >= cch)
537 cchRet += State.pfnOutput(' ', State.pvUser);
538 break;
539 }
540
541 /*
542 * Signed integers.
543 */
544 case 'i':
545 case 'd':
546 State.fFlags &= ~STR_F_SPECIAL;
547 State.fFlags |= STR_F_VALSIGNED;
548 State.uBase = 10;
549 switch (chArgSize)
550 {
551 case 0:
552 case 'h': /* signed short should be promoted to int or be the same as int */
553 case 'H': /* signed char should be promoted to int. */
554 {
555 signed int iValue = va_arg(va, signed int);
556#if ARCH_BITS == 16
557 cchRet += bs3StrFormatS16(&State, iValue);
558#else
559 cchRet += bs3StrFormatS32(&State, iValue);
560#endif
561 break;
562 }
563 case 'l':
564 {
565 signed long lValue = va_arg(va, signed long);
566 if (sizeof(lValue) == 4)
567 cchRet += bs3StrFormatS32(&State, lValue);
568 else
569 cchRet += bs3StrFormatS64(&State, lValue);
570 break;
571 }
572 case 'L':
573 {
574 unsigned long long ullValue = va_arg(va, unsigned long long);
575 cchRet += bs3StrFormatS64(&State, ullValue);
576 break;
577 }
578 }
579 break;
580
581 /*
582 * Unsigned integers.
583 */
584 case 'X':
585 State.fFlags |= STR_F_CAPITAL;
586 case 'x':
587 case 'u':
588 {
589 if (ch == 'u')
590 {
591 State.uBase = 10;
592 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK | STR_F_SPECIAL);
593 }
594 else
595 {
596 State.uBase = 16;
597 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
598 }
599 switch (chArgSize)
600 {
601 case 0:
602 case 'h': /* unsigned short should be promoted to int or be the same as int */
603 case 'H': /* unsigned char should be promoted to int. */
604 {
605 unsigned int uValue = va_arg(va, unsigned int);
606#if ARCH_BITS == 16
607 cchRet += bs3StrFormatU16(&State, uValue);
608#else
609 cchRet += bs3StrFormatU32(&State, uValue);
610#endif
611 break;
612 }
613 case 'l':
614 {
615 unsigned long ulValue = va_arg(va, unsigned long);
616 if (sizeof(ulValue) == 4)
617 cchRet += bs3StrFormatU32(&State, ulValue);
618 else
619 cchRet += bs3StrFormatU64(&State, ulValue);
620 break;
621 }
622 case 'L':
623 {
624 unsigned long long ullValue = va_arg(va, unsigned long long);
625 cchRet += bs3StrFormatU64(&State, ullValue);
626 break;
627 }
628 }
629 break;
630 }
631
632 /*
633 * Our stuff.
634 */
635 case 'R':
636 {
637 ch = *pszFormat++;
638 switch (ch)
639 {
640 case 'I':
641 State.fFlags |= STR_F_VALSIGNED;
642 State.uBase &= ~STR_F_SPECIAL;
643 State.uBase = 10;
644 break;
645 case 'U':
646 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK | STR_F_SPECIAL);
647 State.uBase = 10;
648 break;
649 case 'X':
650 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
651 State.uBase = 16;
652 break;
653 default:
654 State.uBase = 0;
655 break;
656 }
657 if (State.uBase)
658 {
659 ch = *pszFormat++;
660 switch (ch)
661 {
662#if ARCH_BITS != 16
663 case '3':
664 case '1': /* Will an unsigned 16-bit value always be promoted
665 to a 16-bit unsigned int. It certainly will be promoted to a 32-bit int. */
666 pszFormat++; /* Assumes (1)'6' or (3)'2' */
667#else
668 case '1':
669 pszFormat++; /* Assumes (1)'6' */
670#endif
671 case '8': /* An unsigned 8-bit value should be promoted to int, which is at least 16-bit. */
672 {
673 unsigned int uValue = va_arg(va, unsigned int);
674#if ARCH_BITS == 16
675 cchRet += bs3StrFormatU16(&State, uValue);
676#else
677 cchRet += bs3StrFormatU32(&State, uValue);
678#endif
679 break;
680 }
681#if ARCH_BITS == 16
682 case '3':
683 {
684 uint32_t uValue = va_arg(va, uint32_t);
685 pszFormat++;
686 cchRet += bs3StrFormatU32(&State, uValue);
687 break;
688 }
689#endif
690 case '6':
691 {
692 uint64_t uValue = va_arg(va, uint64_t);
693 pszFormat++;
694 cchRet += bs3StrFormatU64(&State, uValue);
695 break;
696 }
697 }
698 }
699 break;
700 }
701
702 /*
703 * Pointers.
704 */
705 case 'P':
706 State.fFlags |= STR_F_CAPITAL;
707 /* fall thru */
708 case 'p':
709 {
710 void BS3_FAR *pv = va_arg(va, void BS3_FAR *);
711 State.uBase = 16;
712 State.fFlags &= ~(STR_F_PLUS | STR_F_BLANK);
713#if ARCH_BITS == 16
714 State.fFlags |= STR_F_ZEROPAD;
715 State.cchWidth = State.fFlags & STR_F_SPECIAL ? 6: 4;
716 cchRet += bs3StrFormatU16(&State, BS3_FP_SEG(pv));
717 cchRet += State.pfnOutput(':', State.pvUser);
718 cchRet += bs3StrFormatU16(&State, BS3_FP_OFF(pv));
719#elif ARCH_BITS == 32
720 State.fFlags |= STR_F_SPECIAL | STR_F_ZEROPAD;
721 State.cchWidth = 10;
722 cchRet += bs3StrFormatU32(&State, (uintptr_t)pv);
723#elif ARCH_BITS == 64
724 State.fFlags |= STR_F_SPECIAL | STR_F_ZEROPAD | STR_F_THOUSAND_SEP;
725 State.cchWidth = 19;
726 cchRet += bs3StrFormatU64(&State, (uintptr_t)pv);
727#else
728# error "Undefined or invalid ARCH_BITS."
729#endif
730 break;
731 }
732
733 }
734 }
735
736 /*
737 * Termination call.
738 */
739 cchRet += State.pfnOutput(0, State.pvUser);
740
741 return cchRet;
742}
743
Note: See TracBrowser for help on using the repository browser.

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