Files
palemoon27/xpcom/glue/nsTextFormatter.cpp
roytam1 a51002fbca import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1202386: Add logging macros for HAL IPC, r=shuang (246eb96f38)
- Bug 1202386: Output clear HAL IPC errors, r=shuang (eaba0dc9a8)
- Bug 1202704: Move Bluetooth IPC pack functions to generic HAL IPC, r=shuang (4c745de79c)
- Bug 1202704: Move Bluetooth IPC unpack functions to generic HAL IPC, r=shuang (4f8600030b)
- Bug 1202704: Move Bluetooth's |UnpackPDUInitOp| to generic HAL IPC code, r=shuang (345f21c637)
- Bug 1209085: Add 6-argument operator () to |UnpackPDUInitOp|, r=joliu (f5f8cf2dd9)
- Bug 1123760 - make autocomplete dropmarker in the urlbar actually work when activated through a11y APIs, r=surkov (eee42195f4)
- Bug 1123760 - bustage follow-up: remove unused variable, rs=bustage on a CLOSED TREE (6de8519b23)
- Bug 1152836 - QR Decoder: Let everywhere, style nits: 580ms -> 400ms r=past (92a7fd6a15)
- Bug 1212430 - Remove CrashAtUnhandlableOOM() and replace with AutoEnterOOMUnsafeRegion r=jandem (f595b87951)
- Bug 1175755 - Only clear GC statistics aborted flag at the end of the outermost nested GC r=bbouvier (ca73f34c69)
- reapply  Bug 1221385 - Handle OOM during JitRuntime (e7def65b78)
- No bug: Clarify documentation for js::NewObjectMetadataState. DONTBUILD r=fitzgen (41022b4137)
- Bug 1211164 - Collect JS deprecated language extension telemetry for Add-ons. r=till,bsmedberg (eacd40ad66)
- Bug 1212296 - undo a state change on OOM. r=till (6e272353d0)
- Bug 1214006 - Take account of the fact that JSScript::atoms may be null while tracing r=terrence (19f61d7494)
- missing bits of Bug 1208665 (c31173b0d4)
- Bug 1221891 - "Fix a typo in TraceLoggingGraph.h". r=hv1989 (e1fc11f8df)
- Bug 1221460 - "TraceLogger: Enable several new optimizations in 'TLLOG=IonCompiler'". r=hv1989 (ab3398646e)
- Bug 1204365 - Repair external view source file name and extension. r=mconley (f033e55a0e)
- Bug 1163693 - Fix View Source external editor fallback. r=jryans. (9c8becc93b)
- Bug 1207629 - Don't assume that viewSourceUtils.js has Services in scope. r=jryans (07977953c0)
- add back some telemetry (1e3b5bde5b)
- Bug 1186785 - Replace nsBaseHashtable::EnumerateRead() calls in toolkit/ with iterators. r=froydnj. (e93e098dd8)
- add back some crashreporter (80e325b3be)
- Bug 1220035 - Fix -Wimplicit-fallthrough warnings in xpcom. r=mccr8 (2ef9ecad5f)
- Bug 1215629 - Remove nsDebug logger. r=froydnj (46784f05fd)
- Bug 1137963 - Use a spin lock for TraceRefCnt. r=waldo, r=froydnj (b2420c97c0)
- Bug 1196430 - part 1 - rename serialNumberRecord to SerialNumberRecord; r=mccr8 (933670070d)
- Bug 1196430 - part 2 - give SerialNumberRecord a proper constructor; r=mccr8 (3ddf3d5e69)
- Bug 1196430 - part 3 - remove unnecessary nsString.h include from nsTraceRefcnt.cpp; r=mccr8 (034954e692)
- Bug 1196430 - part 4 - record allocation stacks for classes in XPCOM_MEM_LOG_CLASSES; r=mccr8 (366b612807)
- Bug 1196430 - part 5 - dump allocation stacks for leaked objects in XPCOM_MEM_LOG_CLASSES; r=mccr8 (dce7b9cca2)
- Bug 1180745 - Fix logging test screenshotting errors. r=jgriffin (a589f0d322)
- Bug 1091285 - move dumpScreen in a new mozscreenshot package. r=jgriffin This also completely remove build/automationutils.py. (6e633359ef)
- some crashreporter stuff (2d0bc9c95d)
- Bug 1196430 - part 6 - move cut-and-paste stack fixer code into mozrunner; r=wlach (ffc7ccd521)
- Bug 1196430 - part 7 - teach process_leak_log how to symbolicate leaked object stacks; r=mccr8 (0b5a4ace7c)
- Bug 1196430 - part 8 - use less reinterpret_cast in nsTraceRefcnt.cpp; r=mccr8 (758cfca0aa)
- Bug 1196430 follow-up: Hide the usage of gCodeAddressService behind #ifdef MOZ_STACKWALKING (e8d62dd73e)
- Bug 487494 - Add an xpcshell selftest for readable stacks from assertions.;r=ted (ea15cf3cbb)
- Bug 1156977 - Assert when aClassName is empty in BloatEntry. r=froydnj Bug 1116550 - Part 1: Turn HaveLeaks and Clear into methods. r=froydnj (8d7f88f498)
- Bug 1116550 - Part 2: Print out negative values for leaks when there are more dtors than ctors. r=froydnj (7c9e3e7848)
- Bug 1190483 - Add a way to record a DMD log late in shutdown. r=erahm (df7c22e64d)
- Bug 1174344 - make error message for mismatched leak log entries more helpful; r=mccr8 (7f969e72c0)
- Bug 1190483 - Followup to address review comment. (d3873f76fd)
- Bug 1186025. Optimize the usage of regions. r=mstange (263080a66e)
- Bug 1211841 - Style off the main thread markers differently, r=jsantell (1a183c5d3e)
- Bug 1211839 - Don't allow off the main thread markers to nest under main thread markers, r=smaug, jsantell (f4d4b7ccf1)
- Bug 1207161 - fix run-by-dir leak in test_bug846906.xul; r=mccr8 (5511752103)
- Bug 1205348 - Always do shutdown CCs when NS_FREE_PERMANENT_DATA is defined. r=smaug (7fd7a7455c)
- Bug 1208157, part 1 - Add and use nsCycleCollector::IsIdle() predicate. r=smaug (a06c2bd1db)
- Bug 1208157, part 2 - Make the fields of nsCycleCollector private. r=smaug (494637fbef)
- Bug 1207368 - Use swap() instead of forget() to remove MessageElement::mMessage. r=froydnj (8c58d38a55)
- Bug 1181520 - Remove support for passing in reftest arguments via the command line, r=jmaher (75e9440e40)
- Bug 1196814 - Fail Android mochitest, robocop, reftests when Fennec is not installed; r=jmaher (6ec15636eb)
- Bug 1183717 - Increase default timeout for Android Debug reftests; r=jmaher (1b07fc1c9b)
- Bug 1181516 - Allow reftests to take paths to multiple directories containing tests on the command line, r=jmaher (1e27ef0d69)
- Bug 1198944 - remove vmware recording support from mochitest; r=khuey (06e79556fb)
- Bug 1162003 - Enable run-by-dir mode on Fx desktop opt builds. r=jmaher (0ef288a33c)
- Bug 1087629 - Add two new test cases for ICE connection states. r=ekr (374112f2d0)
- Bug 1186551 - [mozlog] add structured action process_start/process_exit. r=jgraham (13ce88dbf7)
- Bug 1154111 - Colorize SKIP in test logs. r=jgraham (85910e0f8b)
- Bug 1191267 - Fix mozlog log buffering command line option, r=chmanchester (2bd6592f9b)
- Bug 1185244 - Improve mach support for running mochitests on Valgrind. r=jgraham, njn. (7f5a830fa0)
- Bug 1219870 - [mozlog] ensure correct suite state when logging suite_start/suite_end via StructuredLogger.log_raw, r=chmanchester (21710387a4)
- Bug 1198257 - Better support for providing a directory name and discovering reftests under that directory, r=jmaher (f6255fc44c)
- Bug 1208220 - Remove test of manifest filename that breaks my workflow. r=jgraham (ae4e45946d)
- Bug 1186888 - [mozlog] Ability to use a pre-existing logger with commandline.setup_logging(), r=jgraham (80dfa2a8a8)
- Bug 1042998 - Use StructuredLog.jsm for mochitest logging, r=chmanchester (8851b1b6f9)
- Bug 1218010 - Shorten the polling interval when waiting for httpd.js startup in mochitest. r=ahal (a1f2c81a8e)
- Bug 1224305 - Add an option to the mochitest harness to provide a copy of the extra chrome manifest it writes. r=ahal (e617971f41)
- Bug 1170342 - Don't disable XInput2 for mochitests on GTK3, off by default now. r=karlt (05d53439da)
- Bug 1145375 - Don't kill the debugger if the user Ctrl-C's as running a mochitest; r=ted (6c310500b8)
- Bug 1199241 - Average runtime data across platforms instead of keeping it distinct, r=jgriffin (cd497f509c)
- Bug 1157852 - Mochitest DevTools test directories run multiple times. r=ahal (415ab41a3a)
- Bug 1186791 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in storage/ with iterators. r=mak. (84f6f1f566)
- Bug 1186791 (part 2) - Replace nsBaseHashtable::EnumerateRead() calls in storage/ with iterators. r=mak. (9c67504d0c)
- Bug 1186791 (part 3) - Replace nsBaseHashtable::EnumerateRead() calls in storage/ with iterators. r=mak. (02f472c197)
- Bug 1219238 - remove AutoArray from mozStorageSQLFunctions.cpp; r=mak (45e751d2b7)
- Bug 1166931 - JS Warning in MobileIdentityManager.jsm r=ferjm (2022d4cccd)
- var-const (b7800ec532)
- Trivial, no bug: add missing semicolon to nsBlocklistService.js to avoid a strict warning. (ee6b8a7593)
- Bug 1208242 - Part 1: hook up the blocklist service to b2g web extensions r=mossop,ferjm (d0ad653af4)
- Bug 1208242 - Part 3: don't ship things that should not ship r=me (cd75e88080)
- Bug 1009795 - Use toLocalString to format download size instead of the decimalSymbol hook; r=mak (a4b4442d2c)
- Bug 1009795 - Part 2: Revert to the old gDecimalSymbol hack if the Internationalization API is not available; r=mak (465e23f2c3)
- Bug 1116385. r=Mossop (f0a7b7d450)
- let-var (cb5d9d1d07)
- Bug 1210459: Add originAttributes for tests that implement nsILoadContext. r=bholley (ea6be1490a)
- var-let (d7d4533b53)
- Bug 1034724 - Fixed Unicode values of prefs in about:support. r=adw (1c8253ac5b)
- Bug 1153381. Add a D3D11 ANGLE blacklist. r=mstange (9008483ca5)
- add back some WIn XP and 2k3 stuff (896a4a7e9b)
- Bug 1141783 - Correct user message for mismatched drivers. Don't mismatch if the DLLs are missing. r=jrmuizelaar (872d0c3aff)
- missing members version (4771ff5f24)
- bug 1170987 - Fix gfx/2d to build on iOS. r=jrmuizel (1e555cb6b3)
- Bug 1201937 - push transform onto cairo context when evaluating path bounds. r=eihrul (6a4d8d98ec)
- Bug 1165900 - Make MaybeSnapToDevicePixels return a boolean to indicate whether snapping occurred. r=Bas (8f33f6cf7d)
- Bug 1190705 - Add crashtest for canvas 2d. r=Bas (bf3afb2acc)
- Bug 1161277 - verify SkPath is finite before doing ContainsPoint queries. r=jmuizelaar (c56f9ef322)
- Bug 1218900 - Make shell function startTimingMutator() fail with an error rather than asserting when called at the wrong time r=sfink (856e8678ce)
- small type fix (b36cfdf416)
- Bug 1214846 - Make SPSProfiler::enter() report OOM to the context r=terrence (6086f60d17)
- Bug 1218637 - IonMonkey: MIPS64: Add support into vm. r=arai (ae22538418)
- gfx: add back `AddCrashReportAnnotations()` prototype (c13f61cd8a)
2022-12-29 09:22:12 +08:00

1368 lines
29 KiB
C++

/* -*- 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.
*
* Code based on mozilla/nsprpub/src/io/prprf.c rev 3.7
*
* Contributor(s):
* Kipp E.B. Hickman <kipp@netscape.com> (original author)
* Frank Yung-Fong Tang <ftang@netscape.com>
* Daniele Nicolodi <daniele@grinta.net>
*/
/*
* Copied from xpcom/ds/nsTextFormatter.cpp r1.22
* Changed to use nsMemory and Frozen linkage
* -- Prasad <prasad@medhas.org>
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "prdtoa.h"
#include "mozilla/Logging.h"
#include "mozilla/Snprintf.h"
#include "prmem.h"
#include "nsCRTGlue.h"
#include "nsTextFormatter.h"
#include "nsMemory.h"
/*
** 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
typedef struct SprintfStateStr SprintfState;
struct SprintfStateStr
{
int (*stuff)(SprintfState* aState, const char16_t* aStr, uint32_t aLen);
char16_t* base;
char16_t* cur;
uint32_t maxlen;
void* stuffclosure;
};
/*
** Numbered Arguement State
*/
struct NumArgState
{
int type; /* type of the current ap */
va_list ap; /* point to the corresponding position on ap */
enum Type
{
INT16,
UINT16,
INTN,
UINTN,
INT32,
UINT32,
INT64,
UINT64,
STRING,
DOUBLE,
INTSTR,
UNISTRING,
UNKNOWN
};
};
#define NAS_DEFAULT_NUM 20 /* default number of NumberedArgumentState array */
#define _LEFT 0x1
#define _SIGNED 0x2
#define _SPACED 0x4
#define _ZEROS 0x8
#define _NEG 0x10
#define ELEMENTS_OF(array_) (sizeof(array_) / sizeof(array_[0]))
/*
** Fill into the buffer using the data in src
*/
static int
fill2(SprintfState* aState, const char16_t* aSrc, int aSrcLen, int aWidth,
int aFlags)
{
char16_t space = ' ';
int rv;
aWidth -= aSrcLen;
/* Right adjusting */
if ((aWidth > 0) && ((aFlags & _LEFT) == 0)) {
if (aFlags & _ZEROS) {
space = '0';
}
while (--aWidth >= 0) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
}
/* Copy out the source data */
rv = (*aState->stuff)(aState, aSrc, aSrcLen);
if (rv < 0) {
return rv;
}
/* Left adjusting */
if ((aWidth > 0) && ((aFlags & _LEFT) != 0)) {
while (--aWidth >= 0) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
}
return 0;
}
/*
** Fill a number. The order is: optional-sign zero-filling conversion-digits
*/
static int
fill_n(SprintfState* aState, const char16_t* aSrc, int aSrcLen, int aWidth,
int aPrec, int aType, int aFlags)
{
int zerowidth = 0;
int precwidth = 0;
int signwidth = 0;
int leftspaces = 0;
int rightspaces = 0;
int cvtwidth;
int rv;
char16_t sign;
char16_t space = ' ';
char16_t zero = '0';
if ((aType & 1) == 0) {
if (aFlags & _NEG) {
sign = '-';
signwidth = 1;
} else if (aFlags & _SIGNED) {
sign = '+';
signwidth = 1;
} else if (aFlags & _SPACED) {
sign = ' ';
signwidth = 1;
}
}
cvtwidth = signwidth + aSrcLen;
if (aPrec > 0) {
if (aPrec > aSrcLen) {
/* Need zero filling */
precwidth = aPrec - aSrcLen;
cvtwidth += precwidth;
}
}
if ((aFlags & _ZEROS) && (aPrec < 0)) {
if (aWidth > cvtwidth) {
/* Zero filling */
zerowidth = aWidth - cvtwidth;
cvtwidth += zerowidth;
}
}
if (aFlags & _LEFT) {
if (aWidth > cvtwidth) {
/* Space filling on the right (i.e. left adjusting) */
rightspaces = aWidth - cvtwidth;
}
} else {
if (aWidth > cvtwidth) {
/* Space filling on the left (i.e. right adjusting) */
leftspaces = aWidth - cvtwidth;
}
}
while (--leftspaces >= 0) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
if (signwidth) {
rv = (*aState->stuff)(aState, &sign, 1);
if (rv < 0) {
return rv;
}
}
while (--precwidth >= 0) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
while (--zerowidth >= 0) {
rv = (*aState->stuff)(aState, &zero, 1);
if (rv < 0) {
return rv;
}
}
rv = (*aState->stuff)(aState, aSrc, aSrcLen);
if (rv < 0) {
return rv;
}
while (--rightspaces >= 0) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
return 0;
}
/*
** Convert a long into its printable form
*/
static int
cvt_l(SprintfState* aState, long aNum, int aWidth, int aPrec, int aRadix,
int aType, int aFlags, const char16_t* aHexStr)
{
char16_t cvtbuf[100];
char16_t* cvt;
int digits;
/* according to the man page this needs to happen */
if ((aPrec == 0) && (aNum == 0)) {
return 0;
}
/*
** 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[0] + ELEMENTS_OF(cvtbuf);
digits = 0;
while (aNum) {
int digit = (((unsigned long)aNum) % aRadix) & 0xF;
*--cvt = aHexStr[digit];
digits++;
aNum = (long)(((unsigned long)aNum) / aRadix);
}
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(aState, cvt, digits, aWidth, aPrec, aType, aFlags);
}
/*
** Convert a 64-bit integer into its printable form
*/
static int
cvt_ll(SprintfState* aState, int64_t aNum, int aWidth, int aPrec, int aRadix,
int aType, int aFlags, const char16_t* aHexStr)
{
char16_t cvtbuf[100];
char16_t* cvt;
int digits;
int64_t rad;
/* according to the man page this needs to happen */
if (aPrec == 0 && aNum == 0) {
return 0;
}
/*
** 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.
*/
rad = aRadix;
cvt = &cvtbuf[0] + ELEMENTS_OF(cvtbuf);
digits = 0;
while (aNum != 0) {
*--cvt = aHexStr[int32_t(aNum % rad) & 0xf];
digits++;
aNum /= rad;
}
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(aState, cvt, digits, aWidth, aPrec, aType, aFlags);
}
/*
** Convert a double precision floating point number into its printable
** form.
*/
static int
cvt_f(SprintfState* aState, double aDouble, int aWidth, int aPrec,
const char16_t aType, int aFlags)
{
int mode = 2;
int decpt;
int sign;
char buf[256];
char* bufp = buf;
int bufsz = 256;
char num[256];
char* nump;
char* endnum;
int numdigits = 0;
char exp = 'e';
if (aPrec == -1) {
aPrec = 6;
} else if (aPrec > 50) {
// limit precision to avoid PR_dtoa bug 108335
// and to prevent buffers overflows
aPrec = 50;
}
switch (aType) {
case 'f':
numdigits = aPrec;
mode = 3;
break;
case 'E':
exp = 'E';
MOZ_FALLTHROUGH;
case 'e':
numdigits = aPrec + 1;
mode = 2;
break;
case 'G':
exp = 'E';
MOZ_FALLTHROUGH;
case 'g':
if (aPrec == 0) {
aPrec = 1;
}
numdigits = aPrec;
mode = 2;
break;
default:
NS_ERROR("invalid aType passed to cvt_f");
}
if (PR_dtoa(aDouble, mode, numdigits, &decpt, &sign,
&endnum, num, bufsz) == PR_FAILURE) {
buf[0] = '\0';
return -1;
}
numdigits = endnum - num;
nump = num;
if (sign) {
*bufp++ = '-';
} else if (aFlags & _SIGNED) {
*bufp++ = '+';
}
if (decpt == 9999) {
while ((*bufp++ = *nump++)) {
}
} else {
switch (aType) {
case 'E':
case 'e':
*bufp++ = *nump++;
if (aPrec > 0) {
*bufp++ = '.';
while (*nump) {
*bufp++ = *nump++;
aPrec--;
}
while (aPrec-- > 0) {
*bufp++ = '0';
}
}
*bufp++ = exp;
snprintf(bufp, bufsz - (bufp - buf), "%+03d", decpt - 1);
break;
case 'f':
if (decpt < 1) {
*bufp++ = '0';
if (aPrec > 0) {
*bufp++ = '.';
while (decpt++ && aPrec-- > 0) {
*bufp++ = '0';
}
while (*nump && aPrec-- > 0) {
*bufp++ = *nump++;
}
while (aPrec-- > 0) {
*bufp++ = '0';
}
}
} else {
while (*nump && decpt-- > 0) {
*bufp++ = *nump++;
}
while (decpt-- > 0) {
*bufp++ = '0';
}
if (aPrec > 0) {
*bufp++ = '.';
while (*nump && aPrec-- > 0) {
*bufp++ = *nump++;
}
while (aPrec-- > 0) {
*bufp++ = '0';
}
}
}
*bufp = '\0';
break;
case 'G':
case 'g':
if ((decpt < -3) || ((decpt - 1) >= aPrec)) {
*bufp++ = *nump++;
numdigits--;
if (numdigits > 0) {
*bufp++ = '.';
while (*nump) {
*bufp++ = *nump++;
}
}
*bufp++ = exp;
snprintf(bufp, bufsz - (bufp - buf), "%+03d", decpt - 1);
} else {
if (decpt < 1) {
*bufp++ = '0';
if (aPrec > 0) {
*bufp++ = '.';
while (decpt++) {
*bufp++ = '0';
}
while (*nump) {
*bufp++ = *nump++;
}
}
} else {
while (*nump && decpt-- > 0) {
*bufp++ = *nump++;
numdigits--;
}
while (decpt-- > 0) {
*bufp++ = '0';
}
if (numdigits > 0) {
*bufp++ = '.';
while (*nump) {
*bufp++ = *nump++;
}
}
}
*bufp = '\0';
}
}
}
char16_t rbuf[256];
char16_t* rbufp = rbuf;
bufp = buf;
// cast to char16_t
while ((*rbufp++ = *bufp++)) {
}
*rbufp = '\0';
return fill2(aState, rbuf, NS_strlen(rbuf), aWidth, aFlags);
}
/*
** Convert a string into its printable form. |aWidth| is the output
** width. |aPrec| is the maximum number of characters of |aStr| to output,
** where -1 means until NUL.
*/
static int
cvt_S(SprintfState* aState, const char16_t* aStr, int aWidth, int aPrec,
int aFlags)
{
int slen;
if (aPrec == 0) {
return 0;
}
/* Limit string length by precision value */
slen = aStr ? NS_strlen(aStr) : 6;
if (aPrec > 0) {
if (aPrec < slen) {
slen = aPrec;
}
}
/* and away we go */
return fill2(aState, aStr ? aStr : MOZ_UTF16("(null)"), slen, aWidth, aFlags);
}
/*
** Convert a string into its printable form. |aWidth| is the output
** width. |aPrec| is the maximum number of characters of |aStr| to output,
** where -1 means until NUL.
*/
static int
cvt_s(SprintfState* aState, const char* aStr, int aWidth, int aPrec, int aFlags)
{
NS_ConvertUTF8toUTF16 utf16Val(aStr);
return cvt_S(aState, utf16Val.get(), aWidth, aPrec, aFlags);
}
/*
** 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 struct NumArgState*
BuildArgArray(const char16_t* aFmt, va_list aAp, int* aRv,
struct NumArgState* aNasArray)
{
int number = 0, cn = 0, i;
const char16_t* p;
char16_t c;
struct NumArgState* nas;
/*
** first pass:
** detemine how many legal % I have got, then allocate space
*/
p = aFmt;
*aRv = 0;
i = 0;
while ((c = *p++) != 0) {
if (c != '%') {
continue;
}
/* skip %% case */
if ((c = *p++) == '%') {
continue;
}
while (c != 0) {
if (c > '9' || c < '0') {
/* numbered argument csae */
if (c == '$') {
if (i > 0) {
*aRv = -1;
return nullptr;
}
number++;
break;
} else {
/* non-numbered argument case */
if (number > 0) {
*aRv = -1;
return nullptr;
}
i = 1;
break;
}
}
c = *p++;
}
}
if (number == 0) {
return nullptr;
}
if (number > NAS_DEFAULT_NUM) {
nas = (struct NumArgState*)moz_xmalloc(number * sizeof(struct NumArgState));
if (!nas) {
*aRv = -1;
return nullptr;
}
} else {
nas = aNasArray;
}
for (i = 0; i < number; i++) {
nas[i].type = NumArgState::UNKNOWN;
}
/*
** second pass:
** set nas[].type
*/
p = aFmt;
while ((c = *p++) != 0) {
if (c != '%') {
continue;
}
c = *p++;
if (c == '%') {
continue;
}
cn = 0;
/* should imporve error check later */
while (c && c != '$') {
cn = cn * 10 + c - '0';
c = *p++;
}
if (!c || cn < 1 || cn > number) {
*aRv = -1;
break;
}
/* nas[cn] starts from 0, and make sure
nas[cn].type is not assigned */
cn--;
if (nas[cn].type != NumArgState::UNKNOWN) {
continue;
}
c = *p++;
/* width */
if (c == '*') {
/* not supported feature, for the argument is not numbered */
*aRv = -1;
break;
} else {
while ((c >= '0') && (c <= '9')) {
c = *p++;
}
}
/* precision */
if (c == '.') {
c = *p++;
if (c == '*') {
/* not supported feature, for the argument is not numbered */
*aRv = -1;
break;
} else {
while ((c >= '0') && (c <= '9')) {
c = *p++;
}
}
}
/* size */
nas[cn].type = NumArgState::INTN;
if (c == 'h') {
nas[cn].type = NumArgState::INT16;
c = *p++;
} else if (c == 'L') {
/* XXX not quite sure here */
nas[cn].type = NumArgState::INT64;
c = *p++;
} else if (c == 'l') {
nas[cn].type = NumArgState::INT32;
c = *p++;
if (c == 'l') {
nas[cn].type = NumArgState::INT64;
c = *p++;
}
}
/* format */
switch (c) {
case 'd':
case 'c':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
break;
case 'e':
case 'f':
case 'g':
nas[cn].type = NumArgState::DOUBLE;
break;
case 'p':
/* XXX should use cpp */
if (sizeof(void*) == sizeof(int32_t)) {
nas[cn].type = NumArgState::UINT32;
} else if (sizeof(void*) == sizeof(int64_t)) {
nas[cn].type = NumArgState::UINT64;
} else if (sizeof(void*) == sizeof(int)) {
nas[cn].type = NumArgState::UINTN;
} else {
nas[cn].type = NumArgState::UNKNOWN;
}
break;
case 'C':
/* XXX not supported I suppose */
PR_ASSERT(0);
nas[cn].type = NumArgState::UNKNOWN;
break;
case 'S':
nas[cn].type = NumArgState::UNISTRING;
break;
case 's':
nas[cn].type = NumArgState::STRING;
break;
case 'n':
nas[cn].type = NumArgState::INTSTR;
break;
default:
PR_ASSERT(0);
nas[cn].type = NumArgState::UNKNOWN;
break;
}
/* get a legal para. */
if (nas[cn].type == NumArgState::UNKNOWN) {
*aRv = -1;
break;
}
}
/*
** third pass
** fill the nas[cn].ap
*/
if (*aRv < 0) {
if (nas != aNasArray) {
PR_DELETE(nas);
}
return nullptr;
}
cn = 0;
while (cn < number) {
if (nas[cn].type == NumArgState::UNKNOWN) {
cn++;
continue;
}
VARARGS_ASSIGN(nas[cn].ap, aAp);
switch (nas[cn].type) {
case NumArgState::INT16:
case NumArgState::UINT16:
case NumArgState::INTN:
case NumArgState::UINTN: (void)va_arg(aAp, int); break;
case NumArgState::INT32: (void)va_arg(aAp, int32_t); break;
case NumArgState::UINT32: (void)va_arg(aAp, uint32_t); break;
case NumArgState::INT64: (void)va_arg(aAp, int64_t); break;
case NumArgState::UINT64: (void)va_arg(aAp, uint64_t); break;
case NumArgState::STRING: (void)va_arg(aAp, char*); break;
case NumArgState::INTSTR: (void)va_arg(aAp, int*); break;
case NumArgState::DOUBLE: (void)va_arg(aAp, double); break;
case NumArgState::UNISTRING: (void)va_arg(aAp, char16_t*); break;
default:
if (nas != aNasArray) {
PR_DELETE(nas);
}
*aRv = -1;
return nullptr;
}
cn++;
}
return nas;
}
/*
** The workhorse sprintf code.
*/
static int
dosprintf(SprintfState* aState, const char16_t* aFmt, va_list aAp)
{
char16_t c;
int flags, width, prec, radix, type;
union
{
char16_t ch;
int i;
long l;
int64_t ll;
double d;
const char* s;
const char16_t* S;
int* ip;
} u;
char16_t space = ' ';
nsAutoString hex;
hex.AssignLiteral("0123456789abcdef");
nsAutoString HEX;
HEX.AssignLiteral("0123456789ABCDEF");
const char16_t* hexp;
int rv, i;
struct NumArgState* nas = nullptr;
struct NumArgState nasArray[NAS_DEFAULT_NUM];
/*
** build an argument array, IF the aFmt is numbered argument
** list style, to contain the Numbered Argument list pointers
*/
nas = BuildArgArray(aFmt, aAp, &rv, nasArray);
if (rv < 0) {
/* the aFmt contains error Numbered Argument format, jliu@netscape.com */
PR_ASSERT(0);
return rv;
}
while ((c = *aFmt++) != 0) {
if (c != '%') {
rv = (*aState->stuff)(aState, aFmt - 1, 1);
if (rv < 0) {
return rv;
}
continue;
}
/*
** Gobble up the % format string. Hopefully we have handled all
** of the strange cases!
*/
flags = 0;
c = *aFmt++;
if (c == '%') {
/* quoting a % with %% */
rv = (*aState->stuff)(aState, aFmt - 1, 1);
if (rv < 0) {
return rv;
}
continue;
}
if (nas) {
/* the aFmt contains the Numbered Arguments feature */
i = 0;
/* should imporve error check later */
while (c && c != '$') {
i = (i * 10) + (c - '0');
c = *aFmt++;
}
if (nas[i - 1].type == NumArgState::UNKNOWN) {
if (nas && (nas != nasArray)) {
PR_DELETE(nas);
}
return -1;
}
VARARGS_ASSIGN(aAp, nas[i - 1].ap);
c = *aFmt++;
}
/*
* 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 |= _LEFT;
}
if (c == '+') {
flags |= _SIGNED;
}
if (c == ' ') {
flags |= _SPACED;
}
if (c == '0') {
flags |= _ZEROS;
}
c = *aFmt++;
}
if (flags & _SIGNED) {
flags &= ~_SPACED;
}
if (flags & _LEFT) {
flags &= ~_ZEROS;
}
/* width */
if (c == '*') {
c = *aFmt++;
width = va_arg(aAp, int);
} else {
width = 0;
while ((c >= '0') && (c <= '9')) {
width = (width * 10) + (c - '0');
c = *aFmt++;
}
}
/* precision */
prec = -1;
if (c == '.') {
c = *aFmt++;
if (c == '*') {
c = *aFmt++;
prec = va_arg(aAp, int);
} else {
prec = 0;
while ((c >= '0') && (c <= '9')) {
prec = (prec * 10) + (c - '0');
c = *aFmt++;
}
}
}
/* size */
type = NumArgState::INTN;
if (c == 'h') {
type = NumArgState::INT16;
c = *aFmt++;
} else if (c == 'L') {
/* XXX not quite sure here */
type = NumArgState::INT64;
c = *aFmt++;
} else if (c == 'l') {
type = NumArgState::INT32;
c = *aFmt++;
if (c == 'l') {
type = NumArgState::INT64;
c = *aFmt++;
}
}
/* format */
hexp = hex.get();
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.get();
type |= 1;
goto fetch_and_convert;
fetch_and_convert:
switch (type) {
case NumArgState::INT16:
u.l = va_arg(aAp, int);
if (u.l < 0) {
u.l = -u.l;
flags |= _NEG;
}
goto do_long;
case NumArgState::UINT16:
u.l = va_arg(aAp, int) & 0xffff;
goto do_long;
case NumArgState::INTN:
u.l = va_arg(aAp, int);
if (u.l < 0) {
u.l = -u.l;
flags |= _NEG;
}
goto do_long;
case NumArgState::UINTN:
u.l = (long)va_arg(aAp, unsigned int);
goto do_long;
case NumArgState::INT32:
u.l = va_arg(aAp, int32_t);
if (u.l < 0) {
u.l = -u.l;
flags |= _NEG;
}
goto do_long;
case NumArgState::UINT32:
u.l = (long)va_arg(aAp, uint32_t);
do_long:
rv = cvt_l(aState, u.l, width, prec, radix, type, flags, hexp);
if (rv < 0) {
return rv;
}
break;
case NumArgState::INT64:
u.ll = va_arg(aAp, int64_t);
if (u.ll < 0) {
u.ll = -u.ll;
flags |= _NEG;
}
goto do_longlong;
case NumArgState::UINT64:
u.ll = va_arg(aAp, uint64_t);
do_longlong:
rv = cvt_ll(aState, u.ll, width, prec, radix, type, flags, hexp);
if (rv < 0) {
return rv;
}
break;
}
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
u.d = va_arg(aAp, double);
rv = cvt_f(aState, u.d, width, prec, c, flags);
if (rv < 0) {
return rv;
}
break;
case 'c':
u.ch = va_arg(aAp, int);
if ((flags & _LEFT) == 0) {
while (width-- > 1) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
}
rv = (*aState->stuff)(aState, &u.ch, 1);
if (rv < 0) {
return rv;
}
if (flags & _LEFT) {
while (width-- > 1) {
rv = (*aState->stuff)(aState, &space, 1);
if (rv < 0) {
return rv;
}
}
}
break;
case 'p':
if (sizeof(void*) == sizeof(int32_t)) {
type = NumArgState::UINT32;
} else if (sizeof(void*) == sizeof(int64_t)) {
type = NumArgState::UINT64;
} else if (sizeof(void*) == sizeof(int)) {
type = NumArgState::UINTN;
} else {
PR_ASSERT(0);
break;
}
radix = 16;
goto fetch_and_convert;
#if 0
case 'C':
/* XXX not supported I suppose */
PR_ASSERT(0);
break;
#endif
case 'S':
u.S = va_arg(aAp, const char16_t*);
rv = cvt_S(aState, u.S, width, prec, flags);
if (rv < 0) {
return rv;
}
break;
case 's':
u.s = va_arg(aAp, const char*);
rv = cvt_s(aState, u.s, width, prec, flags);
if (rv < 0) {
return rv;
}
break;
case 'n':
u.ip = va_arg(aAp, int*);
if (u.ip) {
*u.ip = aState->cur - aState->base;
}
break;
default:
/* Not a % token after all... skip it */
#if 0
PR_ASSERT(0);
#endif
char16_t perct = '%';
rv = (*aState->stuff)(aState, &perct, 1);
if (rv < 0) {
return rv;
}
rv = (*aState->stuff)(aState, aFmt - 1, 1);
if (rv < 0) {
return rv;
}
}
}
/* Stuff trailing NUL */
char16_t null = '\0';
rv = (*aState->stuff)(aState, &null, 1);
if (nas && (nas != nasArray)) {
PR_DELETE(nas);
}
return rv;
}
/************************************************************************/
static int
StringStuff(SprintfState* aState, const char16_t* aStr, uint32_t aLen)
{
if (*aStr == '\0') {
return 0;
}
ptrdiff_t off = aState->cur - aState->base;
nsAString* str = static_cast<nsAString*>(aState->stuffclosure);
str->Append(aStr, aLen);
aState->base = str->BeginWriting();
aState->cur = aState->base + off;
return 0;
}
/*
** Stuff routine that automatically grows the malloc'd output buffer
** before it overflows.
*/
static int
GrowStuff(SprintfState* aState, const char16_t* aStr, uint32_t aLen)
{
ptrdiff_t off;
char16_t* newbase;
uint32_t newlen;
off = aState->cur - aState->base;
if (off + aLen >= aState->maxlen) {
/* Grow the buffer */
newlen = aState->maxlen + ((aLen > 32) ? aLen : 32);
if (aState->base) {
newbase = (char16_t*)moz_xrealloc(aState->base,
newlen * sizeof(char16_t));
} else {
newbase = (char16_t*)moz_xmalloc(newlen * sizeof(char16_t));
}
if (!newbase) {
/* Ran out of memory */
return -1;
}
aState->base = newbase;
aState->maxlen = newlen;
aState->cur = aState->base + off;
}
/* Copy data */
while (aLen) {
--aLen;
*aState->cur++ = *aStr++;
}
PR_ASSERT((uint32_t)(aState->cur - aState->base) <= aState->maxlen);
return 0;
}
/*
** sprintf into a malloc'd buffer
*/
char16_t*
nsTextFormatter::smprintf(const char16_t* aFmt, ...)
{
va_list ap;
char16_t* rv;
va_start(ap, aFmt);
rv = nsTextFormatter::vsmprintf(aFmt, ap);
va_end(ap);
return rv;
}
uint32_t
nsTextFormatter::ssprintf(nsAString& aOut, const char16_t* aFmt, ...)
{
va_list ap;
uint32_t rv;
va_start(ap, aFmt);
rv = nsTextFormatter::vssprintf(aOut, aFmt, ap);
va_end(ap);
return rv;
}
uint32_t
nsTextFormatter::vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp)
{
SprintfState ss;
ss.stuff = StringStuff;
ss.base = 0;
ss.cur = 0;
ss.maxlen = 0;
ss.stuffclosure = &aOut;
aOut.Truncate();
int n = dosprintf(&ss, aFmt, aAp);
return n ? n - 1 : n;
}
char16_t*
nsTextFormatter::vsmprintf(const char16_t* aFmt, va_list aAp)
{
SprintfState ss;
int rv;
ss.stuff = GrowStuff;
ss.base = 0;
ss.cur = 0;
ss.maxlen = 0;
rv = dosprintf(&ss, aFmt, aAp);
if (rv < 0) {
if (ss.base) {
PR_DELETE(ss.base);
}
return 0;
}
return ss.base;
}
/*
** Stuff routine that discards overflow data
*/
static int
LimitStuff(SprintfState* aState, const char16_t* aStr, uint32_t aLen)
{
uint32_t limit = aState->maxlen - (aState->cur - aState->base);
if (aLen > limit) {
aLen = limit;
}
while (aLen) {
--aLen;
*aState->cur++ = *aStr++;
}
return 0;
}
/*
** sprintf into a fixed size buffer. Make sure there is a NUL at the end
** when finished.
*/
uint32_t
nsTextFormatter::snprintf(char16_t* aOut, uint32_t aOutLen,
const char16_t* aFmt, ...)
{
va_list ap;
uint32_t rv;
PR_ASSERT((int32_t)aOutLen > 0);
if ((int32_t)aOutLen <= 0) {
return 0;
}
va_start(ap, aFmt);
rv = nsTextFormatter::vsnprintf(aOut, aOutLen, aFmt, ap);
va_end(ap);
return rv;
}
uint32_t
nsTextFormatter::vsnprintf(char16_t* aOut, uint32_t aOutLen,
const char16_t* aFmt, va_list aAp)
{
SprintfState ss;
uint32_t n;
PR_ASSERT((int32_t)aOutLen > 0);
if ((int32_t)aOutLen <= 0) {
return 0;
}
ss.stuff = LimitStuff;
ss.base = aOut;
ss.cur = aOut;
ss.maxlen = aOutLen;
(void) dosprintf(&ss, aFmt, aAp);
/* If we added chars, and we didn't append a null, do it now. */
if ((ss.cur != ss.base) && (*(ss.cur - 1) != '\0')) {
*(--ss.cur) = '\0';
}
n = ss.cur - ss.base;
return n ? n - 1 : n;
}
/*
* Free memory allocated, for the caller, by smprintf
*/
void
nsTextFormatter::smprintf_free(char16_t* aMem)
{
free(aMem);
}