1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Portable safe sprintf code.
10 * Author: Kipp E.B. Hickman
13 #include "double-conversion/double-to-string.h"
14 #include "mozilla/AllocPolicy.h"
15 #include "mozilla/Printf.h"
16 #include "mozilla/UniquePtrExtensions.h"
17 #include "mozilla/Vector.h"
28 using double_conversion::DoubleToStringConverter
;
29 using DTSC
= DoubleToStringConverter
;
32 * Numbered Argument State
35 int type
; // type of the current ap
36 va_list ap
; // point to the corresponding position on ap
39 using NumArgStateVector
=
40 mozilla::Vector
<NumArgState
, 20, mozilla::MallocAllocPolicy
>;
42 // For values up to and including TYPE_DOUBLE, the lowest bit indicates
43 // whether the type is signed (0) or unsigned (1).
50 #define TYPE_LONGLONG 6
51 #define TYPE_ULONGLONG 7
54 #define TYPE_INTSTR 10
55 #define TYPE_POINTER 11
57 # define TYPE_WSTRING 12
61 #define TYPE_UNKNOWN 20
64 #define FLAG_SIGNED 0x2
65 #define FLAG_SPACED 0x4
66 #define FLAG_ZEROS 0x8
69 static const char hex
[] = "0123456789abcdef";
70 static const char HEX
[] = "0123456789ABCDEF";
72 // Fill into the buffer using the data in src
73 bool mozilla::PrintfTarget::fill2(const char* src
, int srclen
, int width
,
78 if (width
> 0 && (flags
& FLAG_LEFT
) == 0) { // Right adjusting
79 if (flags
& FLAG_ZEROS
) {
82 while (--width
>= 0) {
83 if (!emit(&space
, 1)) {
89 // Copy out the source data
90 if (!emit(src
, srclen
)) {
94 if (width
> 0 && (flags
& FLAG_LEFT
) != 0) { // Left adjusting
95 while (--width
>= 0) {
96 if (!emit(&space
, 1)) {
105 * Fill a number. The order is: optional-sign zero-filling conversion-digits
107 bool mozilla::PrintfTarget::fill_n(const char* src
, int srclen
, int width
,
108 int prec
, int type
, int flags
) {
117 if ((type
& 1) == 0) {
118 if (flags
& FLAG_NEG
) {
121 } else if (flags
& FLAG_SIGNED
) {
124 } else if (flags
& FLAG_SPACED
) {
129 cvtwidth
= signwidth
+ srclen
;
131 if (prec
> 0 && (type
!= TYPE_DOUBLE
)) {
133 precwidth
= prec
- srclen
; // Need zero filling
134 cvtwidth
+= precwidth
;
138 if ((flags
& FLAG_ZEROS
) && ((type
== TYPE_DOUBLE
) || (prec
< 0))) {
139 if (width
> cvtwidth
) {
140 zerowidth
= width
- cvtwidth
; // Zero filling
141 cvtwidth
+= zerowidth
;
145 if (flags
& FLAG_LEFT
) {
146 if (width
> cvtwidth
) {
147 // Space filling on the right (i.e. left adjusting)
148 rightspaces
= width
- cvtwidth
;
151 if (width
> cvtwidth
) {
152 // Space filling on the left (i.e. right adjusting)
153 leftspaces
= width
- cvtwidth
;
156 while (--leftspaces
>= 0) {
162 if (!emit(&sign
, 1)) {
166 while (--precwidth
>= 0) {
171 while (--zerowidth
>= 0) {
176 if (!emit(src
, uint32_t(srclen
))) {
179 while (--rightspaces
>= 0) {
187 // All that the cvt_* functions care about as far as the TYPE_* constants is
188 // that the low bit is set to indicate unsigned, or unset to indicate signed.
189 // So we don't try to hard to ensure that the passed TYPE_* constant lines
190 // up with the actual size of the number being printed here. The main printf
191 // code, below, does have to care so that the correct bits are extracted from
193 bool mozilla::PrintfTarget::appendIntDec(int32_t num
) {
200 return cvt_l(n
, -1, -1, 10, TYPE_INTN
, flags
, hex
);
203 bool mozilla::PrintfTarget::appendIntDec(uint32_t num
) {
204 return cvt_l(num
, -1, -1, 10, TYPE_UINTN
, 0, hex
);
207 bool mozilla::PrintfTarget::appendIntOct(uint32_t num
) {
208 return cvt_l(num
, -1, -1, 8, TYPE_UINTN
, 0, hex
);
211 bool mozilla::PrintfTarget::appendIntHex(uint32_t num
) {
212 return cvt_l(num
, -1, -1, 16, TYPE_UINTN
, 0, hex
);
215 bool mozilla::PrintfTarget::appendIntDec(int64_t num
) {
221 return cvt_ll(num
, -1, -1, 10, TYPE_INTN
, flags
, hex
);
224 bool mozilla::PrintfTarget::appendIntDec(uint64_t num
) {
225 return cvt_ll(num
, -1, -1, 10, TYPE_UINTN
, 0, hex
);
228 bool mozilla::PrintfTarget::appendIntOct(uint64_t num
) {
229 return cvt_ll(num
, -1, -1, 8, TYPE_UINTN
, 0, hex
);
232 bool mozilla::PrintfTarget::appendIntHex(uint64_t num
) {
233 return cvt_ll(num
, -1, -1, 16, TYPE_UINTN
, 0, hex
);
236 /* Convert a long into its printable form. */
237 bool mozilla::PrintfTarget::cvt_l(long num
, int width
, int prec
, int radix
,
238 int type
, int flags
, const char* hexp
) {
243 // according to the man page this needs to happen
244 if ((prec
== 0) && (num
== 0)) {
245 return fill_n("", 0, width
, prec
, type
, flags
);
248 // Converting decimal is a little tricky. In the unsigned case we
249 // need to stop when we hit 10 digits. In the signed case, we can
250 // stop when the number is zero.
251 cvt
= cvtbuf
+ sizeof(cvtbuf
);
254 int digit
= (((unsigned long)num
) % radix
) & 0xF;
255 *--cvt
= hexp
[digit
];
257 num
= (long)(((unsigned long)num
) / radix
);
264 // Now that we have the number converted without its sign, deal with
265 // the sign and zero padding.
266 return fill_n(cvt
, digits
, width
, prec
, type
, flags
);
269 /* Convert a 64-bit integer into its printable form. */
270 bool mozilla::PrintfTarget::cvt_ll(int64_t num
, int width
, int prec
, int radix
,
271 int type
, int flags
, const char* hexp
) {
272 // According to the man page, this needs to happen.
273 if (prec
== 0 && num
== 0) {
274 return fill_n("", 0, width
, prec
, type
, flags
);
277 // Converting decimal is a little tricky. In the unsigned case we
278 // need to stop when we hit 10 digits. In the signed case, we can
279 // stop when the number is zero.
280 int64_t rad
= int64_t(radix
);
282 char* cvt
= cvtbuf
+ sizeof(cvtbuf
);
285 int64_t quot
= uint64_t(num
) / rad
;
286 int64_t rem
= uint64_t(num
) % rad
;
287 int32_t digit
= int32_t(rem
);
288 *--cvt
= hexp
[digit
& 0xf];
297 // Now that we have the number converted without its sign, deal with
298 // the sign and zero padding.
299 return fill_n(cvt
, digits
, width
, prec
, type
, flags
);
303 constexpr static size_t lengthof(const char (&)[N
]) {
307 // Longest possible output from ToFixed for positive numbers:
308 // [0-9]{kMaxFixedDigitsBeforePoint}\.[0-9]{kMaxFixedDigitsAfterPoint}?
309 constexpr int FIXED_MAX_CHARS
=
310 DTSC::kMaxFixedDigitsBeforePoint
+ 1 + DTSC::kMaxFixedDigitsAfterPoint
;
312 // Longest possible output from ToExponential:
313 // [1-9]\.[0-9]{kMaxExponentialDigits}e[+-][0-9]{1,3}
314 // (std::numeric_limits<double>::max() has exponent 308).
315 constexpr int EXPONENTIAL_MAX_CHARS
=
316 lengthof("1.") + DTSC::kMaxExponentialDigits
+ lengthof("e+999");
318 // Longest possible output from ToPrecise:
319 // [0-9\.]{kMaxPrecisionDigits+1} or
320 // [1-9]\.[0-9]{kMaxPrecisionDigits-1}e[+-][0-9]{1,3}
321 constexpr int PRECISE_MAX_CHARS
=
322 lengthof("1.") + DTSC::kMaxPrecisionDigits
- 1 + lengthof("e+999");
324 constexpr int DTSC_MAX_CHARS
=
325 std::max({FIXED_MAX_CHARS
, EXPONENTIAL_MAX_CHARS
, PRECISE_MAX_CHARS
});
328 * Convert a double precision floating point number into its printable
331 bool mozilla::PrintfTarget::cvt_f(double d
, char c
, int width
, int prec
,
333 bool lower
= islower(c
);
334 const char* inf
= lower
? "inf" : "INF";
335 const char* nan
= lower
? "nan" : "NAN";
336 char e
= lower
? 'e' : 'E';
337 DoubleToStringConverter
converter(DTSC::UNIQUE_ZERO
| DTSC::NO_TRAILING_ZERO
|
338 DTSC::EMIT_POSITIVE_EXPONENT_SIGN
,
339 inf
, nan
, e
, 0, 0, 4, 0, 2);
340 // Longest of the above cases, plus space for a terminal nul character.
341 char buf
[DTSC_MAX_CHARS
+ 1];
342 double_conversion::StringBuilder
builder(buf
, sizeof(buf
));
343 bool success
= false;
344 if (std::signbit(d
)) {
348 if (!std::isfinite(d
)) {
349 flags
&= ~FLAG_ZEROS
;
351 // "If the precision is missing, it shall be taken as 6."
358 success
= converter
.ToExponential(d
, prec
, &builder
);
362 success
= converter
.ToFixed(d
, prec
, &builder
);
366 // "If an explicit precision is zero, it shall be taken as 1."
367 success
= converter
.ToPrecision(d
, prec
? prec
: 1, &builder
);
373 int len
= builder
.position();
374 char* cvt
= builder
.Finalize();
375 return fill_n(cvt
, len
, width
, prec
, TYPE_DOUBLE
, flags
);
379 * Convert a string into its printable form. "width" is the output
380 * width. "prec" is the maximum number of characters of "s" to output,
381 * where -1 means until NUL.
383 bool mozilla::PrintfTarget::cvt_s(const char* s
, int width
, int prec
,
392 // Limit string length by precision value
393 int slen
= int(strlen(s
));
394 if (0 < prec
&& prec
< slen
) {
399 return fill2(s
, slen
, width
, flags
);
403 * BuildArgArray stands for Numbered Argument list Sprintf
405 * fmp = "%4$i, %2$d, %3s, %1d";
406 * the number must start from 1, and no gap among them
408 static bool BuildArgArray(const char* fmt
, va_list ap
, NumArgStateVector
& nas
) {
409 size_t number
= 0, cn
= 0, i
;
414 // Detemine how many legal % I have got, then allocate space.
418 while ((c
= *p
++) != 0) {
422 if ((c
= *p
++) == '%') { // skip %% case
427 if (c
> '9' || c
< '0') {
428 if (c
== '$') { // numbered argument case
430 MOZ_CRASH("Bad format string");
433 } else { // non-numbered argument case
435 MOZ_CRASH("Bad format string");
450 // Only allow a limited number of arguments.
451 MOZ_RELEASE_ASSERT(number
<= 20);
453 if (!nas
.growByUninitialized(number
)) {
457 for (i
= 0; i
< number
; i
++) {
458 nas
[i
].type
= TYPE_UNKNOWN
;
465 while ((c
= *p
++) != 0) {
475 while (c
&& c
!= '$') { // should improve error check later
476 cn
= cn
* 10 + c
- '0';
480 if (!c
|| cn
< 1 || cn
> number
) {
481 MOZ_CRASH("Bad format string");
484 // nas[cn] starts from 0, and make sure nas[cn].type is not assigned.
486 if (nas
[cn
].type
!= TYPE_UNKNOWN
) {
493 while ((c
== '-') || (c
== '+') || (c
== ' ') || (c
== '0')) {
499 // not supported feature, for the argument is not numbered
500 MOZ_CRASH("Bad format string");
503 while ((c
>= '0') && (c
<= '9')) {
511 // not supported feature, for the argument is not numbered
512 MOZ_CRASH("Bad format string");
515 while ((c
>= '0') && (c
<= '9')) {
521 nas
[cn
].type
= TYPE_INTN
;
523 nas
[cn
].type
= TYPE_SHORT
;
526 nas
[cn
].type
= TYPE_SCHAR
;
529 } else if (c
== 'L') {
530 nas
[cn
].type
= TYPE_LONGLONG
;
532 } else if (c
== 'l') {
533 nas
[cn
].type
= TYPE_LONG
;
536 nas
[cn
].type
= TYPE_LONGLONG
;
539 } else if (c
== 'z' || c
== 'I') {
540 static_assert(sizeof(size_t) == sizeof(int) ||
541 sizeof(size_t) == sizeof(long) ||
542 sizeof(size_t) == sizeof(long long),
543 "size_t is not one of the expected sizes");
544 nas
[cn
].type
= sizeof(size_t) == sizeof(int) ? TYPE_INTN
545 : sizeof(size_t) == sizeof(long) ? TYPE_LONG
548 } else if (c
== 't') {
549 static_assert(sizeof(ptrdiff_t) == sizeof(int) ||
550 sizeof(ptrdiff_t) == sizeof(long) ||
551 sizeof(ptrdiff_t) == sizeof(long long),
552 "ptrdiff_t is not one of the expected sizes");
553 nas
[cn
].type
= sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN
554 : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG
557 } else if (c
== 'j') {
558 static_assert(sizeof(intmax_t) == sizeof(int) ||
559 sizeof(intmax_t) == sizeof(long) ||
560 sizeof(intmax_t) == sizeof(long long),
561 "intmax_t is not one of the expected sizes");
562 nas
[cn
].type
= sizeof(intmax_t) == sizeof(int) ? TYPE_INTN
563 : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG
579 // Mark as unsigned type.
589 nas
[cn
].type
= TYPE_DOUBLE
;
593 nas
[cn
].type
= TYPE_POINTER
;
598 nas
[cn
].type
= TYPE_WSTRING
;
601 nas
[cn
].type
= TYPE_UNKNOWN
;
607 if (nas
[cn
].type
== TYPE_LONG
) {
608 nas
[cn
].type
= TYPE_WSTRING
;
612 // Other type sizes are not supported here.
613 MOZ_ASSERT(nas
[cn
].type
== TYPE_INTN
);
614 nas
[cn
].type
= TYPE_STRING
;
618 nas
[cn
].type
= TYPE_INTSTR
;
623 nas
[cn
].type
= TYPE_UNKNOWN
;
628 if (nas
[cn
].type
== TYPE_UNKNOWN
) {
629 MOZ_CRASH("Bad format string");
637 while (cn
< number
) {
638 // A TYPE_UNKNOWN here means that the format asked for a
639 // positional argument without specifying the meaning of some
641 MOZ_ASSERT(nas
[cn
].type
!= TYPE_UNKNOWN
);
643 va_copy(nas
[cn
].ap
, ap
);
645 switch (nas
[cn
].type
) {
652 (void)va_arg(ap
, int);
655 (void)va_arg(ap
, long);
658 (void)va_arg(ap
, unsigned long);
661 (void)va_arg(ap
, long long);
664 (void)va_arg(ap
, unsigned long long);
667 (void)va_arg(ap
, char*);
670 (void)va_arg(ap
, int*);
673 (void)va_arg(ap
, double);
676 (void)va_arg(ap
, void*);
680 (void)va_arg(ap
, wchar_t*);
694 mozilla::PrintfTarget::PrintfTarget() : mEmitted(0) {}
696 bool mozilla::PrintfTarget::vprint(const char* fmt
, va_list ap
) {
698 int flags
, width
, prec
, radix
, type
;
715 // Build an argument array, IF the fmt is numbered argument
716 // list style, to contain the Numbered Argument list pointers.
718 NumArgStateVector nas
;
719 if (!BuildArgArray(fmt
, ap
, nas
)) {
720 // the fmt contains error Numbered Argument format, jliu@netscape.com
721 MOZ_CRASH("Bad format string");
724 while ((c
= *fmt
++) != 0) {
726 if (!emit(fmt
- 1, 1)) {
733 // Gobble up the % format string. Hopefully we have handled all
734 // of the strange cases!
738 // quoting a % with %%
739 if (!emit(fmt
- 1, 1)) {
747 // the fmt contains the Numbered Arguments feature
749 while (c
&& c
!= '$') { // should improve error check later
750 i
= (i
* 10) + (c
- '0');
754 if (nas
[i
- 1].type
== TYPE_UNKNOWN
) {
755 MOZ_CRASH("Bad format string");
762 // Examine optional flags. Note that we do not implement the
763 // '#' flag of sprintf(). The ANSI C spec. of the '#' flag is
764 // somewhat ambiguous and not ideal, which is perhaps why
765 // the various sprintf() implementations are inconsistent
767 while ((c
== '-') || (c
== '+') || (c
== ' ') || (c
== '0')) {
772 flags
|= FLAG_SIGNED
;
775 flags
|= FLAG_SPACED
;
782 if (flags
& FLAG_SIGNED
) {
783 flags
&= ~FLAG_SPACED
;
785 if (flags
& FLAG_LEFT
) {
786 flags
&= ~FLAG_ZEROS
;
792 width
= va_arg(ap
, int);
796 flags
&= ~FLAG_ZEROS
;
800 while ((c
>= '0') && (c
<= '9')) {
801 width
= (width
* 10) + (c
- '0');
812 prec
= va_arg(ap
, int);
815 while ((c
>= '0') && (c
<= '9')) {
816 prec
= (prec
* 10) + (c
- '0');
831 } else if (c
== 'L') {
832 type
= TYPE_LONGLONG
;
834 } else if (c
== 'l') {
838 type
= TYPE_LONGLONG
;
841 } else if (c
== 'z' || c
== 'I') {
842 static_assert(sizeof(size_t) == sizeof(int) ||
843 sizeof(size_t) == sizeof(long) ||
844 sizeof(size_t) == sizeof(long long),
845 "size_t is not one of the expected sizes");
846 type
= sizeof(size_t) == sizeof(int) ? TYPE_INTN
847 : sizeof(size_t) == sizeof(long) ? TYPE_LONG
850 } else if (c
== 't') {
851 static_assert(sizeof(ptrdiff_t) == sizeof(int) ||
852 sizeof(ptrdiff_t) == sizeof(long) ||
853 sizeof(ptrdiff_t) == sizeof(long long),
854 "ptrdiff_t is not one of the expected sizes");
855 type
= sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN
856 : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG
859 } else if (c
== 'j') {
860 static_assert(sizeof(intmax_t) == sizeof(int) ||
861 sizeof(intmax_t) == sizeof(long) ||
862 sizeof(intmax_t) == sizeof(long long),
863 "intmax_t is not one of the expected sizes");
864 type
= sizeof(intmax_t) == sizeof(int) ? TYPE_INTN
865 : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG
874 case 'i': // decimal/integer
876 goto fetch_and_convert
;
881 goto fetch_and_convert
;
883 case 'u': // unsigned decimal
886 goto fetch_and_convert
;
888 case 'x': // unsigned hex
891 goto fetch_and_convert
;
893 case 'X': // unsigned HEX
897 goto fetch_and_convert
;
902 u
.l
= (signed char)va_arg(ap
, int);
909 u
.l
= (unsigned char)va_arg(ap
, unsigned int);
912 u
.l
= (short)va_arg(ap
, int);
919 u
.l
= (unsigned short)va_arg(ap
, unsigned int);
922 u
.l
= va_arg(ap
, int);
929 u
.l
= (long)va_arg(ap
, unsigned int);
933 u
.l
= va_arg(ap
, long);
940 u
.l
= (long)va_arg(ap
, unsigned long);
942 if (!cvt_l(u
.l
, width
, prec
, radix
, type
, flags
, hexp
)) {
949 u
.ll
= va_arg(ap
, long long);
956 u
.ll
= (uintptr_t)va_arg(ap
, void*);
959 u
.ll
= va_arg(ap
, unsigned long long);
961 if (!cvt_ll(u
.ll
, width
, prec
, radix
, type
, flags
, hexp
)) {
975 u
.d
= va_arg(ap
, double);
976 if (!cvt_f(u
.d
, c
, width
, prec
, flags
)) {
983 if ((flags
& FLAG_LEFT
) == 0) {
984 while (width
-- > 1) {
993 u
.ch
= va_arg(ap
, int);
994 if (!emit(&u
.ch
, 1)) {
999 if (flags
& FLAG_LEFT
) {
1000 while (width
-- > 1) {
1001 if (!emit(" ", 1)) {
1009 type
= TYPE_POINTER
;
1011 goto fetch_and_convert
;
1014 if (type
== TYPE_INTN
) {
1015 u
.s
= va_arg(ap
, const char*);
1016 if (!cvt_s(u
.s
, width
, prec
, flags
)) {
1021 MOZ_ASSERT(type
== TYPE_LONG
);
1026 u
.ws
= va_arg(ap
, const wchar_t*);
1028 int rv
= WideCharToMultiByte(CP_ACP
, 0, u
.ws
, -1, NULL
, 0, NULL
, NULL
);
1029 if (rv
== 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION
) {
1030 if (!cvt_s("<unicode errors in string>", width
, prec
, flags
)) {
1037 UniqueFreePtr
<char[]> buf((char*)malloc(rv
));
1038 WideCharToMultiByte(CP_ACP
, 0, u
.ws
, -1, buf
.get(), rv
, NULL
, NULL
);
1041 if (!cvt_s(buf
.get(), width
, prec
, flags
)) {
1047 // Not supported here.
1053 u
.ip
= va_arg(ap
, int*);
1060 // Not a % token after all... skip it
1061 if (!emit("%", 1)) {
1064 if (!emit(fmt
- 1, 1)) {
1073 /************************************************************************/
1075 bool mozilla::PrintfTarget::print(const char* format
, ...) {
1078 va_start(ap
, format
);
1079 bool result
= vprint(format
, ap
);
1090 #undef TYPE_LONGLONG
1091 #undef TYPE_ULONGLONG