| 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/. */ |
| 6 |
|
| 7 |
/* |
| 8 |
* Portable safe sprintf code. |
| 9 |
* |
| 10 |
* Author: Kipp E.B. Hickman |
| 11 |
*/ |
| 12 |
|
| 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" |
| 18 |
|
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#if defined(XP_WIN) |
| 25 |
# include <windows.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
using double_conversion::DoubleToStringConverter; |
| 29 |
using DTSC = DoubleToStringConverter; |
| 30 |
|
| 31 |
/* |
| 32 |
* Numbered Argument State |
| 33 |
*/ |
| 34 |
struct NumArgState { |
| 35 |
int type; // type of the current ap |
| 36 |
va_list ap; // point to the corresponding position on ap |
| 37 |
}; |
| 38 |
|
| 39 |
using NumArgStateVector = |
| 40 |
mozilla::Vector<NumArgState, 20, mozilla::MallocAllocPolicy>; |
| 41 |
|
| 42 |
// For values up to and including TYPE_DOUBLE, the lowest bit indicates |
| 43 |
// whether the type is signed (0) or unsigned (1). |
| 44 |
#define TYPE_SHORT 0 |
| 45 |
#define TYPE_USHORT 1 |
| 46 |
#define TYPE_INTN 2 |
| 47 |
#define TYPE_UINTN 3 |
| 48 |
#define TYPE_LONG 4 |
| 49 |
#define TYPE_ULONG 5 |
| 50 |
#define TYPE_LONGLONG 6 |
| 51 |
#define TYPE_ULONGLONG 7 |
| 52 |
#define TYPE_DOUBLE 8 |
| 53 |
#define TYPE_STRING 9 |
| 54 |
#define TYPE_INTSTR 10 |
| 55 |
#define TYPE_POINTER 11 |
| 56 |
#if defined(XP_WIN) |
| 57 |
# define TYPE_WSTRING 12 |
| 58 |
#endif |
| 59 |
#define TYPE_SCHAR 14 |
| 60 |
#define TYPE_UCHAR 15 |
| 61 |
#define TYPE_UNKNOWN 20 |
| 62 |
|
| 63 |
#define FLAG_LEFT 0x1 |
| 64 |
#define FLAG_SIGNED 0x2 |
| 65 |
#define FLAG_SPACED 0x4 |
| 66 |
#define FLAG_ZEROS 0x8 |
| 67 |
#define FLAG_NEG 0x10 |
| 68 |
|
| 69 |
static const char hex[] = "0123456789abcdef"; |
| 70 |
static const char HEX[] = "0123456789ABCDEF"; |
| 71 |
|
| 72 |
// Fill into the buffer using the data in src |
| 73 |
bool mozilla::PrintfTarget::fill2(const char* src, int srclen, int width, |
| 74 |
int flags) { |
| 75 |
char space = ' '; |
| 76 |
|
| 77 |
width -= srclen; |
| 78 |
if (width > 0 && (flags & FLAG_LEFT) == 0) { // Right adjusting |
| 79 |
if (flags & FLAG_ZEROS) { |
| 80 |
space = '0'; |
| 81 |
} |
| 82 |
while (--width >= 0) { |
| 83 |
if (!emit(&space, 1)) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Copy out the source data |
| 90 |
if (!emit(src, srclen)) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if (width > 0 && (flags & FLAG_LEFT) != 0) { // Left adjusting |
| 95 |
while (--width >= 0) { |
| 96 |
if (!emit(&space, 1)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* Fill a number. The order is: optional-sign zero-filling conversion-digits |
| 106 |
*/ |
| 107 |
bool mozilla::PrintfTarget::fill_n(const char* src, int srclen, int width, |
| 108 |
int prec, int type, int flags) { |
| 109 |
int zerowidth = 0; |
| 110 |
int precwidth = 0; |
| 111 |
int leftspaces = 0; |
| 112 |
int rightspaces = 0; |
| 113 |
int cvtwidth; |
| 114 |
char sign = 0; |
| 115 |
|
| 116 |
if ((type & 1) == 0) { |
| 117 |
if (flags & FLAG_NEG) { |
| 118 |
sign = '-'; |
| 119 |
} else if (flags & FLAG_SIGNED) { |
| 120 |
sign = '+'; |
| 121 |
} else if (flags & FLAG_SPACED) { |
| 122 |
sign = ' '; |
| 123 |
} |
| 124 |
} |
| 125 |
cvtwidth = (sign ? 1 : 0) + srclen; |
| 126 |
|
| 127 |
if (prec > 0 && (type != TYPE_DOUBLE)) { |
| 128 |
if (prec > srclen) { |
| 129 |
precwidth = prec - srclen; // Need zero filling |
| 130 |
cvtwidth += precwidth; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ((flags & FLAG_ZEROS) && ((type == TYPE_DOUBLE) || (prec < 0))) { |
| 135 |
if (width > cvtwidth) { |
| 136 |
zerowidth = width - cvtwidth; // Zero filling |
| 137 |
cvtwidth += zerowidth; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (flags & FLAG_LEFT) { |
| 142 |
if (width > cvtwidth) { |
| 143 |
// Space filling on the right (i.e. left adjusting) |
| 144 |
rightspaces = width - cvtwidth; |
| 145 |
} |
| 146 |
} else { |
| 147 |
if (width > cvtwidth) { |
| 148 |
// Space filling on the left (i.e. right adjusting) |
| 149 |
leftspaces = width - cvtwidth; |
| 150 |
} |
| 151 |
} |
| 152 |
while (--leftspaces >= 0) { |
| 153 |
if (!emit(" ", 1)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
if (sign) { |
| 158 |
if (!emit(&sign, 1)) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
while (--precwidth >= 0) { |
| 163 |
if (!emit("0", 1)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
} |
| 167 |
while (--zerowidth >= 0) { |
| 168 |
if (!emit("0", 1)) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |
| 172 |
if (!emit(src, uint32_t(srclen))) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
while (--rightspaces >= 0) { |
| 176 |
if (!emit(" ", 1)) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
// All that the cvt_* functions care about as far as the TYPE_* constants is |
| 184 |
// that the low bit is set to indicate unsigned, or unset to indicate signed. |
| 185 |
// So we don't try to hard to ensure that the passed TYPE_* constant lines |
| 186 |
// up with the actual size of the number being printed here. The main printf |
| 187 |
// code, below, does have to care so that the correct bits are extracted from |
| 188 |
// the varargs list. |
| 189 |
bool mozilla::PrintfTarget::appendIntDec(int32_t num) { |
| 190 |
int flags = 0; |
| 191 |
long n = num; |
| 192 |
if (n < 0) { |
| 193 |
n = -n; |
| 194 |
flags |= FLAG_NEG; |
| 195 |
} |
| 196 |
return cvt_l(n, -1, -1, 10, TYPE_INTN, flags, hex); |
| 197 |
} |
| 198 |
|
| 199 |
bool mozilla::PrintfTarget::appendIntDec(uint32_t num) { |
| 200 |
return cvt_l(num, -1, -1, 10, TYPE_UINTN, 0, hex); |
| 201 |
} |
| 202 |
|
| 203 |
bool mozilla::PrintfTarget::appendIntOct(uint32_t num) { |
| 204 |
return cvt_l(num, -1, -1, 8, TYPE_UINTN, 0, hex); |
| 205 |
} |
| 206 |
|
| 207 |
bool mozilla::PrintfTarget::appendIntHex(uint32_t num) { |
| 208 |
return cvt_l(num, -1, -1, 16, TYPE_UINTN, 0, hex); |
| 209 |
} |
| 210 |
|
| 211 |
bool mozilla::PrintfTarget::appendIntDec(int64_t num) { |
| 212 |
int flags = 0; |
| 213 |
if (num < 0) { |
| 214 |
num = -num; |
| 215 |
flags |= FLAG_NEG; |
| 216 |
} |
| 217 |
return cvt_ll(num, -1, -1, 10, TYPE_INTN, flags, hex); |
| 218 |
} |
| 219 |
|
| 220 |
bool mozilla::PrintfTarget::appendIntDec(uint64_t num) { |
| 221 |
return cvt_ll(num, -1, -1, 10, TYPE_UINTN, 0, hex); |
| 222 |
} |
| 223 |
|
| 224 |
bool mozilla::PrintfTarget::appendIntOct(uint64_t num) { |
| 225 |
return cvt_ll(num, -1, -1, 8, TYPE_UINTN, 0, hex); |
| 226 |
} |
| 227 |
|
| 228 |
bool mozilla::PrintfTarget::appendIntHex(uint64_t num) { |
| 229 |
return cvt_ll(num, -1, -1, 16, TYPE_UINTN, 0, hex); |
| 230 |
} |
| 231 |
|
| 232 |
/* Convert a long into its printable form. */ |
| 233 |
bool mozilla::PrintfTarget::cvt_l(long num, int width, int prec, int radix, |
| 234 |
int type, int flags, const char* hexp) { |
| 235 |
char cvtbuf[100]; |
| 236 |
char* cvt; |
| 237 |
int digits; |
| 238 |
|
| 239 |
// according to the man page this needs to happen |
| 240 |
if ((prec == 0) && (num == 0)) { |
| 241 |
return fill_n("", 0, width, prec, type, flags); |
| 242 |
} |
| 243 |
|
| 244 |
// Converting decimal is a little tricky. In the unsigned case we |
| 245 |
// need to stop when we hit 10 digits. In the signed case, we can |
| 246 |
// stop when the number is zero. |
| 247 |
cvt = cvtbuf + sizeof(cvtbuf); |
| 248 |
digits = 0; |
| 249 |
while (num) { |
| 250 |
int digit = (((unsigned long)num) % radix) & 0xF; |
| 251 |
*--cvt = hexp[digit]; |
| 252 |
digits++; |
| 253 |
num = (long)(((unsigned long)num) / radix); |
| 254 |
} |
| 255 |
if (digits == 0) { |
| 256 |
*--cvt = '0'; |
| 257 |
digits++; |
| 258 |
} |
| 259 |
|
| 260 |
// Now that we have the number converted without its sign, deal with |
| 261 |
// the sign and zero padding. |
| 262 |
return fill_n(cvt, digits, width, prec, type, flags); |
| 263 |
} |
| 264 |
|
| 265 |
/* Convert a 64-bit integer into its printable form. */ |
| 266 |
bool mozilla::PrintfTarget::cvt_ll(int64_t num, int width, int prec, int radix, |
| 267 |
int type, int flags, const char* hexp) { |
| 268 |
// According to the man page, this needs to happen. |
| 269 |
if (prec == 0 && num == 0) { |
| 270 |
return fill_n("", 0, width, prec, type, flags); |
| 271 |
} |
| 272 |
|
| 273 |
// Converting decimal is a little tricky. In the unsigned case we |
| 274 |
// need to stop when we hit 10 digits. In the signed case, we can |
| 275 |
// stop when the number is zero. |
| 276 |
int64_t rad = int64_t(radix); |
| 277 |
char cvtbuf[100]; |
| 278 |
char* cvt = cvtbuf + sizeof(cvtbuf); |
| 279 |
int digits = 0; |
| 280 |
while (num != 0) { |
| 281 |
int64_t quot = uint64_t(num) / rad; |
| 282 |
int64_t rem = uint64_t(num) % rad; |
| 283 |
int32_t digit = int32_t(rem); |
| 284 |
*--cvt = hexp[digit & 0xf]; |
| 285 |
digits++; |
| 286 |
num = quot; |
| 287 |
} |
| 288 |
if (digits == 0) { |
| 289 |
*--cvt = '0'; |
| 290 |
digits++; |
| 291 |
} |
| 292 |
|
| 293 |
// Now that we have the number converted without its sign, deal with |
| 294 |
// the sign and zero padding. |
| 295 |
return fill_n(cvt, digits, width, prec, type, flags); |
| 296 |
} |
| 297 |
|
| 298 |
template <size_t N> |
| 299 |
constexpr static size_t lengthof(const char (&)[N]) { |
| 300 |
return N - 1; |
| 301 |
} |
| 302 |
|
| 303 |
// Longest possible output from ToFixed for positive numbers: |
| 304 |
// [0-9]{kMaxFixedDigitsBeforePoint}\.[0-9]{kMaxFixedDigitsAfterPoint}? |
| 305 |
constexpr int FIXED_MAX_CHARS = |
| 306 |
DTSC::kMaxFixedDigitsBeforePoint + 1 + DTSC::kMaxFixedDigitsAfterPoint; |
| 307 |
|
| 308 |
// Longest possible output from ToExponential: |
| 309 |
// [1-9]\.[0-9]{kMaxExponentialDigits}e[+-][0-9]{1,3} |
| 310 |
// (std::numeric_limits<double>::max() has exponent 308). |
| 311 |
constexpr int EXPONENTIAL_MAX_CHARS = |
| 312 |
lengthof("1.") + DTSC::kMaxExponentialDigits + lengthof("e+999"); |
| 313 |
|
| 314 |
// Longest possible output from ToPrecise: |
| 315 |
// [0-9\.]{kMaxPrecisionDigits+1} or |
| 316 |
// [1-9]\.[0-9]{kMaxPrecisionDigits-1}e[+-][0-9]{1,3} |
| 317 |
constexpr int PRECISE_MAX_CHARS = |
| 318 |
lengthof("1.") + DTSC::kMaxPrecisionDigits - 1 + lengthof("e+999"); |
| 319 |
|
| 320 |
constexpr int DTSC_MAX_CHARS = |
| 321 |
std::max({FIXED_MAX_CHARS, EXPONENTIAL_MAX_CHARS, PRECISE_MAX_CHARS}); |
| 322 |
|
| 323 |
/* |
| 324 |
* Convert a double precision floating point number into its printable |
| 325 |
* form. |
| 326 |
*/ |
| 327 |
bool mozilla::PrintfTarget::cvt_f(double d, char c, int width, int prec, |
| 328 |
int flags) { |
| 329 |
bool lower = islower(c); |
| 330 |
const char* inf = lower ? "inf" : "INF"; |
| 331 |
const char* nan = lower ? "nan" : "NAN"; |
| 332 |
char e = lower ? 'e' : 'E'; |
| 333 |
DoubleToStringConverter converter(DTSC::UNIQUE_ZERO | DTSC::NO_TRAILING_ZERO | |
| 334 |
DTSC::EMIT_POSITIVE_EXPONENT_SIGN, |
| 335 |
inf, nan, e, 0, 0, 4, 0, 2); |
| 336 |
// Longest of the above cases, plus space for a terminal nul character. |
| 337 |
char buf[DTSC_MAX_CHARS + 1]; |
| 338 |
double_conversion::StringBuilder builder(buf, sizeof(buf)); |
| 339 |
bool success = false; |
| 340 |
if (std::signbit(d)) { |
| 341 |
d = std::abs(d); |
| 342 |
flags |= FLAG_NEG; |
| 343 |
} |
| 344 |
if (!std::isfinite(d)) { |
| 345 |
flags &= ~FLAG_ZEROS; |
| 346 |
} |
| 347 |
// "If the precision is missing, it shall be taken as 6." |
| 348 |
if (prec < 0) { |
| 349 |
prec = 6; |
| 350 |
} |
| 351 |
switch (c) { |
| 352 |
case 'e': |
| 353 |
case 'E': |
| 354 |
success = converter.ToExponential(d, prec, &builder); |
| 355 |
break; |
| 356 |
case 'f': |
| 357 |
case 'F': |
| 358 |
success = converter.ToFixed(d, prec, &builder); |
| 359 |
break; |
| 360 |
case 'g': |
| 361 |
case 'G': |
| 362 |
// "If an explicit precision is zero, it shall be taken as 1." |
| 363 |
success = converter.ToPrecision(d, prec ? prec : 1, &builder); |
| 364 |
break; |
| 365 |
} |
| 366 |
if (!success) { |
| 367 |
return false; |
| 368 |
} |
| 369 |
int len = builder.position(); |
| 370 |
char* cvt = builder.Finalize(); |
| 371 |
return fill_n(cvt, len, width, prec, TYPE_DOUBLE, flags); |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
* Convert a string into its printable form. "width" is the output |
| 376 |
* width. "prec" is the maximum number of characters of "s" to output, |
| 377 |
* where -1 means until NUL. |
| 378 |
*/ |
| 379 |
bool mozilla::PrintfTarget::cvt_s(const char* s, int width, int prec, |
| 380 |
int flags) { |
| 381 |
if (prec == 0) { |
| 382 |
return true; |
| 383 |
} |
| 384 |
if (!s) { |
| 385 |
s = "(null)"; |
| 386 |
} |
| 387 |
|
| 388 |
// Limit string length by precision value |
| 389 |
size_t slen = strnlen(s, size_t(prec)); |
| 390 |
if (slen > INT_MAX) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
return fill2(s, int(slen), width, flags); |
| 394 |
} |
| 395 |
|
| 396 |
/* |
| 397 |
* BuildArgArray stands for Numbered Argument list Sprintf |
| 398 |
* for example, |
| 399 |
* fmp = "%4$i, %2$d, %3s, %1d"; |
| 400 |
* the number must start from 1, and no gap among them |
| 401 |
*/ |
| 402 |
static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) { |
| 403 |
size_t number = 0, cn = 0, i; |
| 404 |
const char* p; |
| 405 |
char c; |
| 406 |
|
| 407 |
// First pass: |
| 408 |
// Detemine how many legal % I have got, then allocate space. |
| 409 |
|
| 410 |
p = fmt; |
| 411 |
i = 0; |
| 412 |
while ((c = *p++) != 0) { |
| 413 |
if (c != '%') { |
| 414 |
continue; |
| 415 |
} |
| 416 |
if ((c = *p++) == '%') { // skip %% case |
| 417 |
continue; |
| 418 |
} |
| 419 |
|
| 420 |
while (c != 0) { |
| 421 |
if (c > '9' || c < '0') { |
| 422 |
if (c == '$') { // numbered argument case |
| 423 |
if (i > 0) { |
| 424 |
MOZ_CRASH("Bad format string"); |
| 425 |
} |
| 426 |
number++; |
| 427 |
} else { // non-numbered argument case |
| 428 |
if (number > 0) { |
| 429 |
MOZ_CRASH("Bad format string"); |
| 430 |
} |
| 431 |
i = 1; |
| 432 |
} |
| 433 |
break; |
| 434 |
} |
| 435 |
|
| 436 |
c = *p++; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (number == 0) { |
| 441 |
return true; |
| 442 |
} |
| 443 |
|
| 444 |
// Only allow a limited number of arguments. |
| 445 |
MOZ_RELEASE_ASSERT(number <= 20); |
| 446 |
|
| 447 |
if (!nas.growByUninitialized(number)) { |
| 448 |
return false; |
| 449 |
} |
| 450 |
|
| 451 |
for (i = 0; i < number; i++) { |
| 452 |
nas[i].type = TYPE_UNKNOWN; |
| 453 |
} |
| 454 |
|
| 455 |
// Second pass: |
| 456 |
// Set nas[].type. |
| 457 |
|
| 458 |
p = fmt; |
| 459 |
while ((c = *p++) != 0) { |
| 460 |
if (c != '%') { |
| 461 |
continue; |
| 462 |
} |
| 463 |
c = *p++; |
| 464 |
if (c == '%') { |
| 465 |
continue; |
| 466 |
} |
| 467 |
|
| 468 |
cn = 0; |
| 469 |
while (c && c != '$') { // should improve error check later |
| 470 |
cn = cn * 10 + c - '0'; |
| 471 |
c = *p++; |
| 472 |
} |
| 473 |
|
| 474 |
if (!c || cn < 1 || cn > number) { |
| 475 |
MOZ_CRASH("Bad format string"); |
| 476 |
} |
| 477 |
|
| 478 |
// nas[cn] starts from 0, and make sure nas[cn].type is not assigned. |
| 479 |
cn--; |
| 480 |
if (nas[cn].type != TYPE_UNKNOWN) { |
| 481 |
continue; |
| 482 |
} |
| 483 |
|
| 484 |
c = *p++; |
| 485 |
|
| 486 |
// flags |
| 487 |
while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { |
| 488 |
c = *p++; |
| 489 |
} |
| 490 |
|
| 491 |
// width |
| 492 |
if (c == '*') { |
| 493 |
// not supported feature, for the argument is not numbered |
| 494 |
MOZ_CRASH("Bad format string"); |
| 495 |
} |
| 496 |
|
| 497 |
while ((c >= '0') && (c <= '9')) { |
| 498 |
c = *p++; |
| 499 |
} |
| 500 |
|
| 501 |
// precision |
| 502 |
if (c == '.') { |
| 503 |
c = *p++; |
| 504 |
if (c == '*') { |
| 505 |
// not supported feature, for the argument is not numbered |
| 506 |
MOZ_CRASH("Bad format string"); |
| 507 |
} |
| 508 |
|
| 509 |
while ((c >= '0') && (c <= '9')) { |
| 510 |
c = *p++; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
// size |
| 515 |
nas[cn].type = TYPE_INTN; |
| 516 |
if (c == 'h') { |
| 517 |
nas[cn].type = TYPE_SHORT; |
| 518 |
c = *p++; |
| 519 |
if (c == 'h') { |
| 520 |
nas[cn].type = TYPE_SCHAR; |
| 521 |
c = *p++; |
| 522 |
} |
| 523 |
} else if (c == 'L') { |
| 524 |
nas[cn].type = TYPE_LONGLONG; |
| 525 |
c = *p++; |
| 526 |
} else if (c == 'l') { |
| 527 |
nas[cn].type = TYPE_LONG; |
| 528 |
c = *p++; |
| 529 |
if (c == 'l') { |
| 530 |
nas[cn].type = TYPE_LONGLONG; |
| 531 |
c = *p++; |
| 532 |
} |
| 533 |
} else if (c == 'z' || c == 'I') { |
| 534 |
static_assert(sizeof(size_t) == sizeof(int) || |
| 535 |
sizeof(size_t) == sizeof(long) || |
| 536 |
sizeof(size_t) == sizeof(long long), |
| 537 |
"size_t is not one of the expected sizes"); |
| 538 |
nas[cn].type = sizeof(size_t) == sizeof(int) ? TYPE_INTN |
| 539 |
: sizeof(size_t) == sizeof(long) ? TYPE_LONG |
| 540 |
: TYPE_LONGLONG; |
| 541 |
c = *p++; |
| 542 |
} else if (c == 't') { |
| 543 |
static_assert(sizeof(ptrdiff_t) == sizeof(int) || |
| 544 |
sizeof(ptrdiff_t) == sizeof(long) || |
| 545 |
sizeof(ptrdiff_t) == sizeof(long long), |
| 546 |
"ptrdiff_t is not one of the expected sizes"); |
| 547 |
nas[cn].type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN |
| 548 |
: sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG |
| 549 |
: TYPE_LONGLONG; |
| 550 |
c = *p++; |
| 551 |
} else if (c == 'j') { |
| 552 |
static_assert(sizeof(intmax_t) == sizeof(int) || |
| 553 |
sizeof(intmax_t) == sizeof(long) || |
| 554 |
sizeof(intmax_t) == sizeof(long long), |
| 555 |
"intmax_t is not one of the expected sizes"); |
| 556 |
nas[cn].type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN |
| 557 |
: sizeof(intmax_t) == sizeof(long) ? TYPE_LONG |
| 558 |
: TYPE_LONGLONG; |
| 559 |
c = *p++; |
| 560 |
} |
| 561 |
|
| 562 |
// format |
| 563 |
switch (c) { |
| 564 |
case 'd': |
| 565 |
case 'c': |
| 566 |
case 'i': |
| 567 |
break; |
| 568 |
|
| 569 |
case 'o': |
| 570 |
case 'u': |
| 571 |
case 'x': |
| 572 |
case 'X': |
| 573 |
// Mark as unsigned type. |
| 574 |
nas[cn].type |= 1; |
| 575 |
break; |
| 576 |
|
| 577 |
case 'e': |
| 578 |
case 'E': |
| 579 |
case 'f': |
| 580 |
case 'F': |
| 581 |
case 'g': |
| 582 |
case 'G': |
| 583 |
nas[cn].type = TYPE_DOUBLE; |
| 584 |
break; |
| 585 |
|
| 586 |
case 'p': |
| 587 |
nas[cn].type = TYPE_POINTER; |
| 588 |
break; |
| 589 |
|
| 590 |
case 'S': |
| 591 |
#if defined(XP_WIN) |
| 592 |
nas[cn].type = TYPE_WSTRING; |
| 593 |
#else |
| 594 |
MOZ_ASSERT(0); |
| 595 |
nas[cn].type = TYPE_UNKNOWN; |
| 596 |
#endif |
| 597 |
break; |
| 598 |
|
| 599 |
case 's': |
| 600 |
#if defined(XP_WIN) |
| 601 |
if (nas[cn].type == TYPE_LONG) { |
| 602 |
nas[cn].type = TYPE_WSTRING; |
| 603 |
break; |
| 604 |
} |
| 605 |
#endif |
| 606 |
// Other type sizes are not supported here. |
| 607 |
MOZ_ASSERT(nas[cn].type == TYPE_INTN); |
| 608 |
nas[cn].type = TYPE_STRING; |
| 609 |
break; |
| 610 |
|
| 611 |
case 'n': |
| 612 |
nas[cn].type = TYPE_INTSTR; |
| 613 |
break; |
| 614 |
|
| 615 |
default: |
| 616 |
MOZ_ASSERT(0); |
| 617 |
nas[cn].type = TYPE_UNKNOWN; |
| 618 |
break; |
| 619 |
} |
| 620 |
|
| 621 |
// get a legal para. |
| 622 |
if (nas[cn].type == TYPE_UNKNOWN) { |
| 623 |
MOZ_CRASH("Bad format string"); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
// Third pass: |
| 628 |
// Fill nas[].ap. |
| 629 |
|
| 630 |
cn = 0; |
| 631 |
while (cn < number) { |
| 632 |
// A TYPE_UNKNOWN here means that the format asked for a |
| 633 |
// positional argument without specifying the meaning of some |
| 634 |
// earlier argument. |
| 635 |
MOZ_ASSERT(nas[cn].type != TYPE_UNKNOWN); |
| 636 |
|
| 637 |
va_copy(nas[cn].ap, ap); |
| 638 |
|
| 639 |
switch (nas[cn].type) { |
| 640 |
case TYPE_SCHAR: |
| 641 |
case TYPE_UCHAR: |
| 642 |
case TYPE_SHORT: |
| 643 |
case TYPE_USHORT: |
| 644 |
case TYPE_INTN: |
| 645 |
case TYPE_UINTN: |
| 646 |
(void)va_arg(ap, int); |
| 647 |
break; |
| 648 |
case TYPE_LONG: |
| 649 |
(void)va_arg(ap, long); |
| 650 |
break; |
| 651 |
case TYPE_ULONG: |
| 652 |
(void)va_arg(ap, unsigned long); |
| 653 |
break; |
| 654 |
case TYPE_LONGLONG: |
| 655 |
(void)va_arg(ap, long long); |
| 656 |
break; |
| 657 |
case TYPE_ULONGLONG: |
| 658 |
(void)va_arg(ap, unsigned long long); |
| 659 |
break; |
| 660 |
case TYPE_STRING: |
| 661 |
(void)va_arg(ap, char*); |
| 662 |
break; |
| 663 |
case TYPE_INTSTR: |
| 664 |
(void)va_arg(ap, int*); |
| 665 |
break; |
| 666 |
case TYPE_DOUBLE: |
| 667 |
(void)va_arg(ap, double); |
| 668 |
break; |
| 669 |
case TYPE_POINTER: |
| 670 |
(void)va_arg(ap, void*); |
| 671 |
break; |
| 672 |
#if defined(XP_WIN) |
| 673 |
case TYPE_WSTRING: |
| 674 |
(void)va_arg(ap, wchar_t*); |
| 675 |
break; |
| 676 |
#endif |
| 677 |
|
| 678 |
default: |
| 679 |
MOZ_CRASH(); |
| 680 |
} |
| 681 |
|
| 682 |
cn++; |
| 683 |
} |
| 684 |
|
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
mozilla::PrintfTarget::PrintfTarget() : mEmitted(0) {} |
| 689 |
|
| 690 |
bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) { |
| 691 |
char c; |
| 692 |
int flags, width, prec, radix, type; |
| 693 |
union { |
| 694 |
char ch; |
| 695 |
int i; |
| 696 |
long l; |
| 697 |
long long ll; |
| 698 |
double d; |
| 699 |
const char* s; |
| 700 |
int* ip; |
| 701 |
void* p; |
| 702 |
#if defined(XP_WIN) |
| 703 |
const wchar_t* ws; |
| 704 |
#endif |
| 705 |
} u{}; |
| 706 |
const char* hexp; |
| 707 |
int i; |
| 708 |
|
| 709 |
// Build an argument array, IF the fmt is numbered argument |
| 710 |
// list style, to contain the Numbered Argument list pointers. |
| 711 |
|
| 712 |
NumArgStateVector nas; |
| 713 |
if (!BuildArgArray(fmt, ap, nas)) { |
| 714 |
// the fmt contains error Numbered Argument format, [email protected] |
| 715 |
MOZ_CRASH("Bad format string"); |
| 716 |
} |
| 717 |
|
| 718 |
while ((c = *fmt++) != 0) { |
| 719 |
if (c != '%') { |
| 720 |
if (!emit(fmt - 1, 1)) { |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
continue; |
| 725 |
} |
| 726 |
|
| 727 |
// Gobble up the % format string. Hopefully we have handled all |
| 728 |
// of the strange cases! |
| 729 |
flags = 0; |
| 730 |
c = *fmt++; |
| 731 |
if (c == '%') { |
| 732 |
// quoting a % with %% |
| 733 |
if (!emit(fmt - 1, 1)) { |
| 734 |
return false; |
| 735 |
} |
| 736 |
|
| 737 |
continue; |
| 738 |
} |
| 739 |
|
| 740 |
if (!nas.empty()) { |
| 741 |
// the fmt contains the Numbered Arguments feature |
| 742 |
i = 0; |
| 743 |
while (c && c != '$') { // should improve error check later |
| 744 |
i = (i * 10) + (c - '0'); |
| 745 |
c = *fmt++; |
| 746 |
} |
| 747 |
|
| 748 |
if (nas[i - 1].type == TYPE_UNKNOWN) { |
| 749 |
MOZ_CRASH("Bad format string"); |
| 750 |
} |
| 751 |
|
| 752 |
ap = nas[i - 1].ap; |
| 753 |
c = *fmt++; |
| 754 |
} |
| 755 |
|
| 756 |
// Examine optional flags. Note that we do not implement the |
| 757 |
// '#' flag of sprintf(). The ANSI C spec. of the '#' flag is |
| 758 |
// somewhat ambiguous and not ideal, which is perhaps why |
| 759 |
// the various sprintf() implementations are inconsistent |
| 760 |
// on this feature. |
| 761 |
while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { |
| 762 |
if (c == '-') { |
| 763 |
flags |= FLAG_LEFT; |
| 764 |
} |
| 765 |
if (c == '+') { |
| 766 |
flags |= FLAG_SIGNED; |
| 767 |
} |
| 768 |
if (c == ' ') { |
| 769 |
flags |= FLAG_SPACED; |
| 770 |
} |
| 771 |
if (c == '0') { |
| 772 |
flags |= FLAG_ZEROS; |
| 773 |
} |
| 774 |
c = *fmt++; |
| 775 |
} |
| 776 |
if (flags & FLAG_SIGNED) { |
| 777 |
flags &= ~FLAG_SPACED; |
| 778 |
} |
| 779 |
if (flags & FLAG_LEFT) { |
| 780 |
flags &= ~FLAG_ZEROS; |
| 781 |
} |
| 782 |
|
| 783 |
// width |
| 784 |
if (c == '*') { |
| 785 |
c = *fmt++; |
| 786 |
width = va_arg(ap, int); |
| 787 |
if (width < 0) { |
| 788 |
width = -width; |
| 789 |
flags |= FLAG_LEFT; |
| 790 |
flags &= ~FLAG_ZEROS; |
| 791 |
} |
| 792 |
} else { |
| 793 |
width = 0; |
| 794 |
while ((c >= '0') && (c <= '9')) { |
| 795 |
width = (width * 10) + (c - '0'); |
| 796 |
c = *fmt++; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
// precision |
| 801 |
prec = -1; |
| 802 |
if (c == '.') { |
| 803 |
c = *fmt++; |
| 804 |
if (c == '*') { |
| 805 |
c = *fmt++; |
| 806 |
prec = va_arg(ap, int); |
| 807 |
} else { |
| 808 |
prec = 0; |
| 809 |
while ((c >= '0') && (c <= '9')) { |
| 810 |
prec = (prec * 10) + (c - '0'); |
| 811 |
c = *fmt++; |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
// size |
| 817 |
type = TYPE_INTN; |
| 818 |
if (c == 'h') { |
| 819 |
type = TYPE_SHORT; |
| 820 |
c = *fmt++; |
| 821 |
if (c == 'h') { |
| 822 |
type = TYPE_SCHAR; |
| 823 |
c = *fmt++; |
| 824 |
} |
| 825 |
} else if (c == 'L') { |
| 826 |
type = TYPE_LONGLONG; |
| 827 |
c = *fmt++; |
| 828 |
} else if (c == 'l') { |
| 829 |
type = TYPE_LONG; |
| 830 |
c = *fmt++; |
| 831 |
if (c == 'l') { |
| 832 |
type = TYPE_LONGLONG; |
| 833 |
c = *fmt++; |
| 834 |
} |
| 835 |
} else if (c == 'z' || c == 'I') { |
| 836 |
static_assert(sizeof(size_t) == sizeof(int) || |
| 837 |
sizeof(size_t) == sizeof(long) || |
| 838 |
sizeof(size_t) == sizeof(long long), |
| 839 |
"size_t is not one of the expected sizes"); |
| 840 |
type = sizeof(size_t) == sizeof(int) ? TYPE_INTN |
| 841 |
: sizeof(size_t) == sizeof(long) ? TYPE_LONG |
| 842 |
: TYPE_LONGLONG; |
| 843 |
c = *fmt++; |
| 844 |
} else if (c == 't') { |
| 845 |
static_assert(sizeof(ptrdiff_t) == sizeof(int) || |
| 846 |
sizeof(ptrdiff_t) == sizeof(long) || |
| 847 |
sizeof(ptrdiff_t) == sizeof(long long), |
| 848 |
"ptrdiff_t is not one of the expected sizes"); |
| 849 |
type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN |
| 850 |
: sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG |
| 851 |
: TYPE_LONGLONG; |
| 852 |
c = *fmt++; |
| 853 |
} else if (c == 'j') { |
| 854 |
static_assert(sizeof(intmax_t) == sizeof(int) || |
| 855 |
sizeof(intmax_t) == sizeof(long) || |
| 856 |
sizeof(intmax_t) == sizeof(long long), |
| 857 |
"intmax_t is not one of the expected sizes"); |
| 858 |
type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN |
| 859 |
: sizeof(intmax_t) == sizeof(long) ? TYPE_LONG |
| 860 |
: TYPE_LONGLONG; |
| 861 |
c = *fmt++; |
| 862 |
} |
| 863 |
|
| 864 |
// format |
| 865 |
hexp = hex; |
| 866 |
switch (c) { |
| 867 |
case 'd': |
| 868 |
case 'i': // decimal/integer |
| 869 |
radix = 10; |
| 870 |
goto fetch_and_convert; |
| 871 |
|
| 872 |
case 'o': // octal |
| 873 |
radix = 8; |
| 874 |
type |= 1; |
| 875 |
goto fetch_and_convert; |
| 876 |
|
| 877 |
case 'u': // unsigned decimal |
| 878 |
radix = 10; |
| 879 |
type |= 1; |
| 880 |
goto fetch_and_convert; |
| 881 |
|
| 882 |
case 'x': // unsigned hex |
| 883 |
radix = 16; |
| 884 |
type |= 1; |
| 885 |
goto fetch_and_convert; |
| 886 |
|
| 887 |
case 'X': // unsigned HEX |
| 888 |
radix = 16; |
| 889 |
hexp = HEX; |
| 890 |
type |= 1; |
| 891 |
goto fetch_and_convert; |
| 892 |
|
| 893 |
fetch_and_convert: |
| 894 |
switch (type) { |
| 895 |
case TYPE_SCHAR: |
| 896 |
u.l = (signed char)va_arg(ap, int); |
| 897 |
if (u.l < 0) { |
| 898 |
u.l = -u.l; |
| 899 |
flags |= FLAG_NEG; |
| 900 |
} |
| 901 |
goto do_long; |
| 902 |
case TYPE_UCHAR: |
| 903 |
u.l = (unsigned char)va_arg(ap, unsigned int); |
| 904 |
goto do_long; |
| 905 |
case TYPE_SHORT: |
| 906 |
u.l = (short)va_arg(ap, int); |
| 907 |
if (u.l < 0) { |
| 908 |
u.l = -u.l; |
| 909 |
flags |= FLAG_NEG; |
| 910 |
} |
| 911 |
goto do_long; |
| 912 |
case TYPE_USHORT: |
| 913 |
u.l = (unsigned short)va_arg(ap, unsigned int); |
| 914 |
goto do_long; |
| 915 |
case TYPE_INTN: |
| 916 |
u.l = va_arg(ap, int); |
| 917 |
if (u.l < 0) { |
| 918 |
u.l = -u.l; |
| 919 |
flags |= FLAG_NEG; |
| 920 |
} |
| 921 |
goto do_long; |
| 922 |
case TYPE_UINTN: |
| 923 |
u.l = (long)va_arg(ap, unsigned int); |
| 924 |
goto do_long; |
| 925 |
|
| 926 |
case TYPE_LONG: |
| 927 |
u.l = va_arg(ap, long); |
| 928 |
if (u.l < 0) { |
| 929 |
u.l = -u.l; |
| 930 |
flags |= FLAG_NEG; |
| 931 |
} |
| 932 |
goto do_long; |
| 933 |
case TYPE_ULONG: |
| 934 |
u.l = (long)va_arg(ap, unsigned long); |
| 935 |
do_long: |
| 936 |
if (!cvt_l(u.l, width, prec, radix, type, flags, hexp)) { |
| 937 |
return false; |
| 938 |
} |
| 939 |
|
| 940 |
break; |
| 941 |
|
| 942 |
case TYPE_LONGLONG: |
| 943 |
u.ll = va_arg(ap, long long); |
| 944 |
if (u.ll < 0) { |
| 945 |
u.ll = -u.ll; |
| 946 |
flags |= FLAG_NEG; |
| 947 |
} |
| 948 |
goto do_longlong; |
| 949 |
case TYPE_POINTER: |
| 950 |
u.ll = (uintptr_t)va_arg(ap, void*); |
| 951 |
goto do_longlong; |
| 952 |
case TYPE_ULONGLONG: |
| 953 |
u.ll = va_arg(ap, unsigned long long); |
| 954 |
do_longlong: |
| 955 |
if (!cvt_ll(u.ll, width, prec, radix, type, flags, hexp)) { |
| 956 |
return false; |
| 957 |
} |
| 958 |
|
| 959 |
break; |
| 960 |
} |
| 961 |
break; |
| 962 |
|
| 963 |
case 'e': |
| 964 |
case 'E': |
| 965 |
case 'f': |
| 966 |
case 'F': |
| 967 |
case 'g': |
| 968 |
case 'G': |
| 969 |
u.d = va_arg(ap, double); |
| 970 |
if (!cvt_f(u.d, c, width, prec, flags)) { |
| 971 |
return false; |
| 972 |
} |
| 973 |
|
| 974 |
break; |
| 975 |
|
| 976 |
case 'c': |
| 977 |
if ((flags & FLAG_LEFT) == 0) { |
| 978 |
while (width-- > 1) { |
| 979 |
if (!emit(" ", 1)) { |
| 980 |
return false; |
| 981 |
} |
| 982 |
} |
| 983 |
} |
| 984 |
switch (type) { |
| 985 |
case TYPE_SHORT: |
| 986 |
case TYPE_INTN: |
| 987 |
u.ch = va_arg(ap, int); |
| 988 |
if (!emit(&u.ch, 1)) { |
| 989 |
return false; |
| 990 |
} |
| 991 |
break; |
| 992 |
} |
| 993 |
if (flags & FLAG_LEFT) { |
| 994 |
while (width-- > 1) { |
| 995 |
if (!emit(" ", 1)) { |
| 996 |
return false; |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
break; |
| 1001 |
|
| 1002 |
case 'p': |
| 1003 |
type = TYPE_POINTER; |
| 1004 |
radix = 16; |
| 1005 |
goto fetch_and_convert; |
| 1006 |
|
| 1007 |
case 's': |
| 1008 |
if (type == TYPE_INTN) { |
| 1009 |
u.s = va_arg(ap, const char*); |
| 1010 |
if (!cvt_s(u.s, width, prec, flags)) { |
| 1011 |
return false; |
| 1012 |
} |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
MOZ_ASSERT(type == TYPE_LONG); |
| 1016 |
[[fallthrough]]; |
| 1017 |
case 'S': |
| 1018 |
#if defined(XP_WIN) |
| 1019 |
{ |
| 1020 |
u.ws = va_arg(ap, const wchar_t*); |
| 1021 |
|
| 1022 |
int rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); |
| 1023 |
if (rv == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) { |
| 1024 |
if (!cvt_s("<unicode errors in string>", width, prec, flags)) { |
| 1025 |
return false; |
| 1026 |
} |
| 1027 |
} else { |
| 1028 |
if (rv == 0) { |
| 1029 |
rv = 1; |
| 1030 |
} |
| 1031 |
UniqueFreePtr<char[]> buf((char*)malloc(rv)); |
| 1032 |
WideCharToMultiByte(CP_ACP, 0, u.ws, -1, buf.get(), rv, NULL, NULL); |
| 1033 |
buf[rv - 1] = '\0'; |
| 1034 |
|
| 1035 |
if (!cvt_s(buf.get(), width, prec, flags)) { |
| 1036 |
return false; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
#else |
| 1041 |
// Not supported here. |
| 1042 |
MOZ_ASSERT(0); |
| 1043 |
#endif |
| 1044 |
break; |
| 1045 |
|
| 1046 |
case 'n': |
| 1047 |
u.ip = va_arg(ap, int*); |
| 1048 |
if (u.ip) { |
| 1049 |
*u.ip = mEmitted; |
| 1050 |
} |
| 1051 |
break; |
| 1052 |
|
| 1053 |
default: |
| 1054 |
// Not a % token after all... skip it |
| 1055 |
if (!emit("%", 1)) { |
| 1056 |
return false; |
| 1057 |
} |
| 1058 |
if (!emit(fmt - 1, 1)) { |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
return true; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/************************************************************************/ |
| 1068 |
|
| 1069 |
bool mozilla::PrintfTarget::print(const char* format, ...) { |
| 1070 |
va_list ap; |
| 1071 |
|
| 1072 |
va_start(ap, format); |
| 1073 |
bool result = vprint(format, ap); |
| 1074 |
va_end(ap); |
| 1075 |
return result; |
| 1076 |
} |
| 1077 |
|
| 1078 |
#undef TYPE_SHORT |
| 1079 |
#undef TYPE_USHORT |
| 1080 |
#undef TYPE_INTN |
| 1081 |
#undef TYPE_UINTN |
| 1082 |
#undef TYPE_LONG |
| 1083 |
#undef TYPE_ULONG |
| 1084 |
#undef TYPE_LONGLONG |
| 1085 |
#undef TYPE_ULONGLONG |
| 1086 |
#undef TYPE_STRING |
| 1087 |
#undef TYPE_DOUBLE |
| 1088 |
#undef TYPE_INTSTR |
| 1089 |
#undef TYPE_POINTER |
| 1090 |
#undef TYPE_WSTRING |
| 1091 |
#undef TYPE_UNKNOWN |
| 1092 |
#undef TYPE_SCHAR |
| 1093 |
#undef TYPE_UCHAR |
| 1094 |
|
| 1095 |
#undef FLAG_LEFT |
| 1096 |
#undef FLAG_SIGNED |
| 1097 |
#undef FLAG_SPACED |
| 1098 |
#undef FLAG_ZEROS |
| 1099 |
#undef FLAG_NEG |