DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (4f16fe418e32)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Portable safe sprintf code.
 *
 * Author: Kipp E.B. Hickman
 */

#include "mozilla/AllocPolicy.h"
#include "mozilla/Likely.h"
#include "mozilla/Printf.h"
#include "mozilla/Sprintf.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/Vector.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(XP_WIN)
#  include <windows.h>
#endif

/*
 * Note: on some platforms va_list is defined as an array,
 * and requires array notation.
 */
#ifdef HAVE_VA_COPY
#  define VARARGS_ASSIGN(foo, bar) VA_COPY(foo, bar)
#elif defined(HAVE_VA_LIST_AS_ARRAY)
#  define VARARGS_ASSIGN(foo, bar) foo[0] = bar[0]
#else
#  define VARARGS_ASSIGN(foo, bar) (foo) = (bar)
#endif

/*
 * Numbered Argument State
 */
struct NumArgState {
  int type;    // type of the current ap
  va_list ap;  // point to the corresponding position on ap
};

typedef mozilla::Vector<NumArgState, 20, mozilla::MallocAllocPolicy>
    NumArgStateVector;

#define TYPE_SHORT 0
#define TYPE_USHORT 1
#define TYPE_INTN 2
#define TYPE_UINTN 3
#define TYPE_LONG 4
#define TYPE_ULONG 5
#define TYPE_LONGLONG 6
#define TYPE_ULONGLONG 7
#define TYPE_STRING 8
#define TYPE_DOUBLE 9
#define TYPE_INTSTR 10
#define TYPE_POINTER 11
#if defined(XP_WIN)
#  define TYPE_WSTRING 12
#endif
#define TYPE_UNKNOWN 20

#define FLAG_LEFT 0x1
#define FLAG_SIGNED 0x2
#define FLAG_SPACED 0x4
#define FLAG_ZEROS 0x8
#define FLAG_NEG 0x10

static const char hex[] = "0123456789abcdef";
static const char HEX[] = "0123456789ABCDEF";

// Fill into the buffer using the data in src
bool mozilla::PrintfTarget::fill2(const char* src, int srclen, int width,
                                  int flags) {
  char space = ' ';

  width -= srclen;
  if (width > 0 && (flags & FLAG_LEFT) == 0) {  // Right adjusting
    if (flags & FLAG_ZEROS) space = '0';
    while (--width >= 0) {
      if (!emit(&space, 1)) return false;
    }
  }

  // Copy out the source data
  if (!emit(src, srclen)) return false;

  if (width > 0 && (flags & FLAG_LEFT) != 0) {  // Left adjusting
    while (--width >= 0) {
      if (!emit(&space, 1)) return false;
    }
  }
  return true;
}

/*
 * Fill a number. The order is: optional-sign zero-filling conversion-digits
 */
bool mozilla::PrintfTarget::fill_n(const char* src, int srclen, int width,
                                   int prec, int type, int flags) {
  int zerowidth = 0;
  int precwidth = 0;
  int signwidth = 0;
  int leftspaces = 0;
  int rightspaces = 0;
  int cvtwidth;
  char sign;

  if ((type & 1) == 0) {
    if (flags & FLAG_NEG) {
      sign = '-';
      signwidth = 1;
    } else if (flags & FLAG_SIGNED) {
      sign = '+';
      signwidth = 1;
    } else if (flags & FLAG_SPACED) {
      sign = ' ';
      signwidth = 1;
    }
  }
  cvtwidth = signwidth + srclen;

  if (prec > 0) {
    if (prec > srclen) {
      precwidth = prec - srclen;  // Need zero filling
      cvtwidth += precwidth;
    }
  }

  if ((flags & FLAG_ZEROS) && (prec < 0)) {
    if (width > cvtwidth) {
      zerowidth = width - cvtwidth;  // Zero filling
      cvtwidth += zerowidth;
    }
  }

  if (flags & FLAG_LEFT) {
    if (width > cvtwidth) {
      // Space filling on the right (i.e. left adjusting)
      rightspaces = width - cvtwidth;
    }
  } else {
    if (width > cvtwidth) {
      // Space filling on the left (i.e. right adjusting)
      leftspaces = width - cvtwidth;
    }
  }
  while (--leftspaces >= 0) {
    if (!emit(" ", 1)) return false;
  }
  if (signwidth) {
    if (!emit(&sign, 1)) return false;
  }
  while (--precwidth >= 0) {
    if (!emit("0", 1)) return false;
  }
  while (--zerowidth >= 0) {
    if (!emit("0", 1)) return false;
  }
  if (!emit(src, uint32_t(srclen))) return false;
  while (--rightspaces >= 0) {
    if (!emit(" ", 1)) return false;
  }
  return true;
}

// All that the cvt_* functions care about as far as the TYPE_* constants is
// that the low bit is set to indicate unsigned, or unset to indicate signed.
// So we don't try to hard to ensure that the passed TYPE_* constant lines
// up with the actual size of the number being printed here.  The main printf
// code, below, does have to care so that the correct bits are extracted from
// the varargs list.
bool mozilla::PrintfTarget::appendIntDec(int32_t num) {
  int flags = 0;
  long n = num;
  if (n < 0) {
    n = -n;
    flags |= FLAG_NEG;
  }
  return cvt_l(n, -1, -1, 10, TYPE_INTN, flags, hex);
}

bool mozilla::PrintfTarget::appendIntDec(uint32_t num) {
  return cvt_l(num, -1, -1, 10, TYPE_UINTN, 0, hex);
}

bool mozilla::PrintfTarget::appendIntOct(uint32_t num) {
  return cvt_l(num, -1, -1, 8, TYPE_UINTN, 0, hex);
}

bool mozilla::PrintfTarget::appendIntHex(uint32_t num) {
  return cvt_l(num, -1, -1, 16, TYPE_UINTN, 0, hex);
}

bool mozilla::PrintfTarget::appendIntDec(int64_t num) {
  int flags = 0;
  if (num < 0) {
    num = -num;
    flags |= FLAG_NEG;
  }
  return cvt_ll(num, -1, -1, 10, TYPE_INTN, flags, hex);
}

bool mozilla::PrintfTarget::appendIntDec(uint64_t num) {
  return cvt_ll(num, -1, -1, 10, TYPE_UINTN, 0, hex);
}

bool mozilla::PrintfTarget::appendIntOct(uint64_t num) {
  return cvt_ll(num, -1, -1, 8, TYPE_UINTN, 0, hex);
}

bool mozilla::PrintfTarget::appendIntHex(uint64_t num) {
  return cvt_ll(num, -1, -1, 16, TYPE_UINTN, 0, hex);
}

/* Convert a long into its printable form. */
bool mozilla::PrintfTarget::cvt_l(long num, int width, int prec, int radix,
                                  int type, int flags, const char* hexp) {
  char cvtbuf[100];
  char* cvt;
  int digits;

  // according to the man page this needs to happen
  if ((prec == 0) && (num == 0)) return true;

  // Converting decimal is a little tricky. In the unsigned case we
  // need to stop when we hit 10 digits. In the signed case, we can
  // stop when the number is zero.
  cvt = cvtbuf + sizeof(cvtbuf);
  digits = 0;
  while (num) {
    int digit = (((unsigned long)num) % radix) & 0xF;
    *--cvt = hexp[digit];
    digits++;
    num = (long)(((unsigned long)num) / radix);
  }
  if (digits == 0) {
    *--cvt = '0';
    digits++;
  }

  // Now that we have the number converted without its sign, deal with
  // the sign and zero padding.
  return fill_n(cvt, digits, width, prec, type, flags);
}

/* Convert a 64-bit integer into its printable form. */
bool mozilla::PrintfTarget::cvt_ll(int64_t num, int width, int prec, int radix,
                                   int type, int flags, const char* hexp) {
  // According to the man page, this needs to happen.
  if (prec == 0 && num == 0) return true;

  // Converting decimal is a little tricky. In the unsigned case we
  // need to stop when we hit 10 digits. In the signed case, we can
  // stop when the number is zero.
  int64_t rad = int64_t(radix);
  char cvtbuf[100];
  char* cvt = cvtbuf + sizeof(cvtbuf);
  int digits = 0;
  while (num != 0) {
    int64_t quot = uint64_t(num) / rad;
    int64_t rem = uint64_t(num) % rad;
    int32_t digit = int32_t(rem);
    *--cvt = hexp[digit & 0xf];
    digits++;
    num = quot;
  }
  if (digits == 0) {
    *--cvt = '0';
    digits++;
  }

  // Now that we have the number converted without its sign, deal with
  // the sign and zero padding.
  return fill_n(cvt, digits, width, prec, type, flags);
}

/*
 * Convert a double precision floating point number into its printable
 * form.
 */
bool mozilla::PrintfTarget::cvt_f(double d, const char* fmt0,
                                  const char* fmt1) {
  char fin[20];
  // The size is chosen such that we can print DBL_MAX.  See bug#1350097.
  char fout[320];
  int amount = fmt1 - fmt0;

  MOZ_ASSERT((amount > 0) && (amount < (int)sizeof(fin)));
  if (amount >= (int)sizeof(fin)) {
    // Totally bogus % command to sprintf. Just ignore it
    return true;
  }
  memcpy(fin, fmt0, (size_t)amount);
  fin[amount] = 0;

  // Convert floating point using the native snprintf code
#ifdef DEBUG
  {
    const char* p = fin;
    while (*p) {
      MOZ_ASSERT(*p != 'L');
      p++;
    }
  }
#endif
  size_t len = SprintfLiteral(fout, fin, d);
  // Note that SprintfLiteral will always write a \0 at the end, so a
  // "<=" check here would be incorrect -- the buffer size passed to
  // snprintf includes the trailing \0, but the returned length does
  // not.
  if (MOZ_LIKELY(len < sizeof(fout))) {
    return emit(fout, len);
  }

  // Maybe the user used "%500.500f" or something like that.
  size_t buf_size = len + 1;
  UniqueFreePtr<char> buf((char*)malloc(buf_size));
  if (!buf) {
    return false;
  }
  len = snprintf(buf.get(), buf_size, fin, d);
  // If this assert fails, then SprintfLiteral has a bug -- and in
  // this case we would like to learn of it, which is why there is a
  // release assert.
  MOZ_RELEASE_ASSERT(len < buf_size);

  return emit(buf.get(), len);
}

/*
 * Convert a string into its printable form.  "width" is the output
 * width. "prec" is the maximum number of characters of "s" to output,
 * where -1 means until NUL.
 */
bool mozilla::PrintfTarget::cvt_s(const char* s, int width, int prec,
                                  int flags) {
  if (prec == 0) return true;
  if (!s) s = "(null)";

  // Limit string length by precision value
  int slen = int(strlen(s));
  if (0 < prec && prec < slen) slen = prec;

  // and away we go
  return fill2(s, slen, width, flags);
}

/*
 * BuildArgArray stands for Numbered Argument list Sprintf
 * for example,
 *      fmp = "%4$i, %2$d, %3s, %1d";
 * the number must start from 1, and no gap among them
 */
static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) {
  size_t number = 0, cn = 0, i;
  const char* p;
  char c;

  // First pass:
  // Detemine how many legal % I have got, then allocate space.

  p = fmt;
  i = 0;
  while ((c = *p++) != 0) {
    if (c != '%') continue;
    if ((c = *p++) == '%')  // skip %% case
      continue;

    while (c != 0) {
      if (c > '9' || c < '0') {
        if (c == '$') {  // numbered argument case
          if (i > 0) MOZ_CRASH("Bad format string");
          number++;
        } else {  // non-numbered argument case
          if (number > 0) MOZ_CRASH("Bad format string");
          i = 1;
        }
        break;
      }

      c = *p++;
    }
  }

  if (number == 0) return true;

  // Only allow a limited number of arguments.
  MOZ_RELEASE_ASSERT(number <= 20);

  if (!nas.growByUninitialized(number)) return false;

  for (i = 0; i < number; i++) nas[i].type = TYPE_UNKNOWN;

  // Second pass:
  // Set nas[].type.

  p = fmt;
  while ((c = *p++) != 0) {
    if (c != '%') continue;
    c = *p++;
    if (c == '%') continue;

    cn = 0;
    while (c && c != '$') {  // should improve error check later
      cn = cn * 10 + c - '0';
      c = *p++;
    }

    if (!c || cn < 1 || cn > number) MOZ_CRASH("Bad format string");

    // nas[cn] starts from 0, and make sure nas[cn].type is not assigned.
    cn--;
    if (nas[cn].type != TYPE_UNKNOWN) continue;

    c = *p++;

    // flags
    while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) {
      c = *p++;
    }

    // width
    if (c == '*') {
      // not supported feature, for the argument is not numbered
      MOZ_CRASH("Bad format string");
    }

    while ((c >= '0') && (c <= '9')) {
      c = *p++;
    }

    // precision
    if (c == '.') {
      c = *p++;
      if (c == '*') {
        // not supported feature, for the argument is not numbered
        MOZ_CRASH("Bad format string");
      }

      while ((c >= '0') && (c <= '9')) {
        c = *p++;
      }
    }

    // size
    nas[cn].type = TYPE_INTN;
    if (c == 'h') {
      nas[cn].type = TYPE_SHORT;
      c = *p++;
    } else if (c == 'L') {
      nas[cn].type = TYPE_LONGLONG;
      c = *p++;
    } else if (c == 'l') {
      nas[cn].type = TYPE_LONG;
      c = *p++;
      if (c == 'l') {
        nas[cn].type = TYPE_LONGLONG;
        c = *p++;
      }
    } else if (c == 'z' || c == 'I') {
      static_assert(sizeof(size_t) == sizeof(int) ||
                        sizeof(size_t) == sizeof(long) ||
                        sizeof(size_t) == sizeof(long long),
                    "size_t is not one of the expected sizes");
      nas[cn].type =
          sizeof(size_t) == sizeof(int)
              ? TYPE_INTN
              : sizeof(size_t) == sizeof(long) ? TYPE_LONG : TYPE_LONGLONG;
      c = *p++;
    }

    // format
    switch (c) {
      case 'd':
      case 'c':
      case 'i':
        break;

      case 'o':
      case 'u':
      case 'x':
      case 'X':
        // Mark as unsigned type.
        nas[cn].type |= 1;
        break;

      case 'e':
      case 'f':
      case 'g':
        nas[cn].type = TYPE_DOUBLE;
        break;

      case 'p':
        nas[cn].type = TYPE_POINTER;
        break;

      case 'S':
#if defined(XP_WIN)
        nas[cn].type = TYPE_WSTRING;
#else
        MOZ_ASSERT(0);
        nas[cn].type = TYPE_UNKNOWN;
#endif
        break;

      case 's':
#if defined(XP_WIN)
        if (nas[cn].type == TYPE_LONG) {
          nas[cn].type = TYPE_WSTRING;
          break;
        }
#endif
        // Other type sizes are not supported here.
        MOZ_ASSERT(nas[cn].type == TYPE_INTN);
        nas[cn].type = TYPE_STRING;
        break;

      case 'n':
        nas[cn].type = TYPE_INTSTR;
        break;

      default:
        MOZ_ASSERT(0);
        nas[cn].type = TYPE_UNKNOWN;
        break;
    }

    // get a legal para.
    if (nas[cn].type == TYPE_UNKNOWN) MOZ_CRASH("Bad format string");
  }

  // Third pass:
  // Fill nas[].ap.

  cn = 0;
  while (cn < number) {
    // A TYPE_UNKNOWN here means that the format asked for a
    // positional argument without specifying the meaning of some
    // earlier argument.
    MOZ_ASSERT(nas[cn].type != TYPE_UNKNOWN);

    VARARGS_ASSIGN(nas[cn].ap, ap);

    switch (nas[cn].type) {
      case TYPE_SHORT:
      case TYPE_USHORT:
      case TYPE_INTN:
      case TYPE_UINTN:
        (void)va_arg(ap, int);
        break;
      case TYPE_LONG:
        (void)va_arg(ap, long);
        break;
      case TYPE_ULONG:
        (void)va_arg(ap, unsigned long);
        break;
      case TYPE_LONGLONG:
        (void)va_arg(ap, long long);
        break;
      case TYPE_ULONGLONG:
        (void)va_arg(ap, unsigned long long);
        break;
      case TYPE_STRING:
        (void)va_arg(ap, char*);
        break;
      case TYPE_INTSTR:
        (void)va_arg(ap, int*);
        break;
      case TYPE_DOUBLE:
        (void)va_arg(ap, double);
        break;
      case TYPE_POINTER:
        (void)va_arg(ap, void*);
        break;
#if defined(XP_WIN)
      case TYPE_WSTRING:
        (void)va_arg(ap, wchar_t*);
        break;
#endif

      default:
        MOZ_CRASH();
    }

    cn++;
  }

  return true;
}

mozilla::PrintfTarget::PrintfTarget() : mEmitted(0) {}

bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) {
  char c;
  int flags, width, prec, radix, type;
  union {
    char ch;
    int i;
    long l;
    long long ll;
    double d;
    const char* s;
    int* ip;
    void* p;
#if defined(XP_WIN)
    const wchar_t* ws;
#endif
  } u;
  const char* fmt0;
  const char* hexp;
  int i;
  char pattern[20];
  const char* dolPt = nullptr;  // in "%4$.2f", dolPt will point to '.'

  // Build an argument array, IF the fmt is numbered argument
  // list style, to contain the Numbered Argument list pointers.

  NumArgStateVector nas;
  if (!BuildArgArray(fmt, ap, nas)) {
    // the fmt contains error Numbered Argument format, jliu@netscape.com
    MOZ_CRASH("Bad format string");
  }

  while ((c = *fmt++) != 0) {
    if (c != '%') {
      if (!emit(fmt - 1, 1)) return false;

      continue;
    }
    fmt0 = fmt - 1;

    // Gobble up the % format string. Hopefully we have handled all
    // of the strange cases!
    flags = 0;
    c = *fmt++;
    if (c == '%') {
      // quoting a % with %%
      if (!emit(fmt - 1, 1)) return false;

      continue;
    }

    if (!nas.empty()) {
      // the fmt contains the Numbered Arguments feature
      i = 0;
      while (c && c != '$') {  // should improve error check later
        i = (i * 10) + (c - '0');
        c = *fmt++;
      }

      if (nas[i - 1].type == TYPE_UNKNOWN) MOZ_CRASH("Bad format string");

      ap = nas[i - 1].ap;
      dolPt = fmt;
      c = *fmt++;
    }

    // Examine optional flags.  Note that we do not implement the
    // '#' flag of sprintf().  The ANSI C spec. of the '#' flag is
    // somewhat ambiguous and not ideal, which is perhaps why
    // the various sprintf() implementations are inconsistent
    // on this feature.
    while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) {
      if (c == '-') flags |= FLAG_LEFT;
      if (c == '+') flags |= FLAG_SIGNED;
      if (c == ' ') flags |= FLAG_SPACED;
      if (c == '0') flags |= FLAG_ZEROS;
      c = *fmt++;
    }
    if (flags & FLAG_SIGNED) flags &= ~FLAG_SPACED;
    if (flags & FLAG_LEFT) flags &= ~FLAG_ZEROS;

    // width
    if (c == '*') {
      c = *fmt++;
      width = va_arg(ap, int);
      if (width < 0) {
        width = -width;
        flags |= FLAG_LEFT;
        flags &= ~FLAG_ZEROS;
      }
    } else {
      width = 0;
      while ((c >= '0') && (c <= '9')) {
        width = (width * 10) + (c - '0');
        c = *fmt++;
      }
    }

    // precision
    prec = -1;
    if (c == '.') {
      c = *fmt++;
      if (c == '*') {
        c = *fmt++;
        prec = va_arg(ap, int);
      } else {
        prec = 0;
        while ((c >= '0') && (c <= '9')) {
          prec = (prec * 10) + (c - '0');
          c = *fmt++;
        }
      }
    }

    // size
    type = TYPE_INTN;
    if (c == 'h') {
      type = TYPE_SHORT;
      c = *fmt++;
    } else if (c == 'L') {
      type = TYPE_LONGLONG;
      c = *fmt++;
    } else if (c == 'l') {
      type = TYPE_LONG;
      c = *fmt++;
      if (c == 'l') {
        type = TYPE_LONGLONG;
        c = *fmt++;
      }
    } else if (c == 'z' || c == 'I') {
      static_assert(sizeof(size_t) == sizeof(int) ||
                        sizeof(size_t) == sizeof(long) ||
                        sizeof(size_t) == sizeof(long long),
                    "size_t is not one of the expected sizes");
      type = sizeof(size_t) == sizeof(int)
                 ? TYPE_INTN
                 : sizeof(size_t) == sizeof(long) ? TYPE_LONG : TYPE_LONGLONG;
      c = *fmt++;
    }

    // format
    hexp = hex;
    switch (c) {
      case 'd':
      case 'i':  // decimal/integer
        radix = 10;
        goto fetch_and_convert;

      case 'o':  // octal
        radix = 8;
        type |= 1;
        goto fetch_and_convert;

      case 'u':  // unsigned decimal
        radix = 10;
        type |= 1;
        goto fetch_and_convert;

      case 'x':  // unsigned hex
        radix = 16;
        type |= 1;
        goto fetch_and_convert;

      case 'X':  // unsigned HEX
        radix = 16;
        hexp = HEX;
        type |= 1;
        goto fetch_and_convert;

      fetch_and_convert:
        switch (type) {
          case TYPE_SHORT:
            u.l = va_arg(ap, int);
            if (u.l < 0) {
              u.l = -u.l;
              flags |= FLAG_NEG;
            }
            goto do_long;
          case TYPE_USHORT:
            u.l = (unsigned short)va_arg(ap, unsigned int);
            goto do_long;
          case TYPE_INTN:
            u.l = va_arg(ap, int);
            if (u.l < 0) {
              u.l = -u.l;
              flags |= FLAG_NEG;
            }
            goto do_long;
          case TYPE_UINTN:
            u.l = (long)va_arg(ap, unsigned int);
            goto do_long;

          case TYPE_LONG:
            u.l = va_arg(ap, long);
            if (u.l < 0) {
              u.l = -u.l;
              flags |= FLAG_NEG;
            }
            goto do_long;
          case TYPE_ULONG:
            u.l = (long)va_arg(ap, unsigned long);
          do_long:
            if (!cvt_l(u.l, width, prec, radix, type, flags, hexp))
              return false;

            break;

          case TYPE_LONGLONG:
            u.ll = va_arg(ap, long long);
            if (u.ll < 0) {
              u.ll = -u.ll;
              flags |= FLAG_NEG;
            }
            goto do_longlong;
          case TYPE_POINTER:
            u.ll = (uintptr_t)va_arg(ap, void*);
            goto do_longlong;
          case TYPE_ULONGLONG:
            u.ll = va_arg(ap, unsigned long long);
          do_longlong:
            if (!cvt_ll(u.ll, width, prec, radix, type, flags, hexp))
              return false;

            break;
        }
        break;

      case 'e':
      case 'E':
      case 'f':
      case 'g':
        u.d = va_arg(ap, double);
        if (!nas.empty()) {
          i = fmt - dolPt;
          if (i < int(sizeof(pattern))) {
            pattern[0] = '%';
            memcpy(&pattern[1], dolPt, size_t(i));
            if (!cvt_f(u.d, pattern, &pattern[i + 1])) return false;
          }
        } else {
          if (!cvt_f(u.d, fmt0, fmt)) return false;
        }

        break;

      case 'c':
        if ((flags & FLAG_LEFT) == 0) {
          while (width-- > 1) {
            if (!emit(" ", 1)) return false;
          }
        }
        switch (type) {
          case TYPE_SHORT:
          case TYPE_INTN:
            u.ch = va_arg(ap, int);
            if (!emit(&u.ch, 1)) return false;
            break;
        }
        if (flags & FLAG_LEFT) {
          while (width-- > 1) {
            if (!emit(" ", 1)) return false;
          }
        }
        break;

      case 'p':
        type = TYPE_POINTER;
        radix = 16;
        goto fetch_and_convert;

      case 's':
        if (type == TYPE_INTN) {
          u.s = va_arg(ap, const char*);
          if (!cvt_s(u.s, width, prec, flags)) return false;
          break;
        }
        MOZ_ASSERT(type == TYPE_LONG);
        [[fallthrough]];
      case 'S':
#if defined(XP_WIN)
      {
        u.ws = va_arg(ap, const wchar_t*);

        int rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL);
        if (rv == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
          if (!cvt_s("<unicode errors in string>", width, prec, flags)) {
            return false;
          }
        } else {
          if (rv == 0) {
            rv = 1;
          }
          UniqueFreePtr<char[]> buf((char*)malloc(rv));
          WideCharToMultiByte(CP_ACP, 0, u.ws, -1, buf.get(), rv, NULL, NULL);
          buf[rv - 1] = '\0';

          if (!cvt_s(buf.get(), width, prec, flags)) {
            return false;
          }
        }
      }
#else
        // Not supported here.
        MOZ_ASSERT(0);
#endif
      break;

      case 'n':
        u.ip = va_arg(ap, int*);
        if (u.ip) {
          *u.ip = mEmitted;
        }
        break;

      default:
        // Not a % token after all... skip it
        if (!emit("%", 1)) return false;
        if (!emit(fmt - 1, 1)) return false;
    }
  }

  return true;
}

/************************************************************************/

bool mozilla::PrintfTarget::print(const char* format, ...) {
  va_list ap;

  va_start(ap, format);
  bool result = vprint(format, ap);
  va_end(ap);
  return result;
}

#undef TYPE_SHORT
#undef TYPE_USHORT
#undef TYPE_INTN
#undef TYPE_UINTN
#undef TYPE_LONG
#undef TYPE_ULONG
#undef TYPE_LONGLONG
#undef TYPE_ULONGLONG
#undef TYPE_STRING
#undef TYPE_DOUBLE
#undef TYPE_INTSTR
#undef TYPE_POINTER
#undef TYPE_WSTRING
#undef TYPE_UNKNOWN

#undef FLAG_LEFT
#undef FLAG_SIGNED
#undef FLAG_SPACED
#undef FLAG_ZEROS
#undef FLAG_NEG