andre@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** Portable safe sprintf code. andre@0: ** andre@0: ** Author: Kipp E.B. Hickman andre@0: */ andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include "primpl.h" andre@0: #include "prprf.h" andre@0: #include "prlong.h" andre@0: #include "prlog.h" andre@0: #include "prmem.h" andre@0: andre@0: /* andre@0: ** WARNING: This code may *NOT* call PR_LOG (because PR_LOG calls it) andre@0: */ andre@0: andre@0: /* andre@0: ** XXX This needs to be internationalized! andre@0: */ andre@0: andre@0: typedef struct SprintfStateStr SprintfState; andre@0: andre@0: struct SprintfStateStr { andre@0: int (*stuff)(SprintfState *ss, const char *sp, PRUint32 len); andre@0: andre@0: char *base; andre@0: char *cur; andre@0: PRUint32 maxlen; andre@0: andre@0: int (*func)(void *arg, const char *sp, PRUint32 len); andre@0: void *arg; andre@0: }; andre@0: andre@0: /* andre@0: ** Numbered Argument andre@0: */ andre@0: struct NumArg { andre@0: int type; /* type of the numbered argument */ andre@0: union { /* the numbered argument */ andre@0: int i; andre@0: unsigned int ui; andre@0: PRInt32 i32; andre@0: PRUint32 ui32; andre@0: PRInt64 ll; andre@0: PRUint64 ull; andre@0: double d; andre@0: const char *s; andre@0: int *ip; andre@0: #ifdef WIN32 andre@0: const WCHAR *ws; andre@0: #endif andre@0: } u; andre@0: }; andre@0: andre@0: #define NAS_DEFAULT_NUM 20 /* default number of NumberedArgument array */ andre@0: andre@0: andre@0: #define TYPE_INT16 0 andre@0: #define TYPE_UINT16 1 andre@0: #define TYPE_INTN 2 andre@0: #define TYPE_UINTN 3 andre@0: #define TYPE_INT32 4 andre@0: #define TYPE_UINT32 5 andre@0: #define TYPE_INT64 6 andre@0: #define TYPE_UINT64 7 andre@0: #define TYPE_STRING 8 andre@0: #define TYPE_DOUBLE 9 andre@0: #define TYPE_INTSTR 10 andre@0: #ifdef WIN32 andre@0: #define TYPE_WSTRING 11 andre@0: #endif andre@0: #define TYPE_UNKNOWN 20 andre@0: andre@0: #define FLAG_LEFT 0x1 andre@0: #define FLAG_SIGNED 0x2 andre@0: #define FLAG_SPACED 0x4 andre@0: #define FLAG_ZEROS 0x8 andre@0: #define FLAG_NEG 0x10 andre@0: andre@0: /* andre@0: ** Fill into the buffer using the data in src andre@0: */ andre@0: static int fill2(SprintfState *ss, const char *src, int srclen, int width, andre@0: int flags) andre@0: { andre@0: char space = ' '; andre@0: int rv; andre@0: andre@0: width -= srclen; andre@0: if ((width > 0) && ((flags & FLAG_LEFT) == 0)) { /* Right adjusting */ andre@0: if (flags & FLAG_ZEROS) { andre@0: space = '0'; andre@0: } andre@0: while (--width >= 0) { andre@0: rv = (*ss->stuff)(ss, &space, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* Copy out the source data */ andre@0: rv = (*ss->stuff)(ss, src, srclen); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: andre@0: if ((width > 0) && ((flags & FLAG_LEFT) != 0)) { /* Left adjusting */ andre@0: while (--width >= 0) { andre@0: rv = (*ss->stuff)(ss, &space, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** Fill a number. The order is: optional-sign zero-filling conversion-digits andre@0: */ andre@0: static int fill_n(SprintfState *ss, const char *src, int srclen, int width, andre@0: int prec, int type, int flags) andre@0: { andre@0: int zerowidth = 0; andre@0: int precwidth = 0; andre@0: int signwidth = 0; andre@0: int leftspaces = 0; andre@0: int rightspaces = 0; andre@0: int cvtwidth; andre@0: int rv; andre@0: char sign; andre@0: andre@0: if ((type & 1) == 0) { andre@0: if (flags & FLAG_NEG) { andre@0: sign = '-'; andre@0: signwidth = 1; andre@0: } else if (flags & FLAG_SIGNED) { andre@0: sign = '+'; andre@0: signwidth = 1; andre@0: } else if (flags & FLAG_SPACED) { andre@0: sign = ' '; andre@0: signwidth = 1; andre@0: } andre@0: } andre@0: cvtwidth = signwidth + srclen; andre@0: andre@0: if (prec > 0) { andre@0: if (prec > srclen) { andre@0: precwidth = prec - srclen; /* Need zero filling */ andre@0: cvtwidth += precwidth; andre@0: } andre@0: } andre@0: andre@0: if ((flags & FLAG_ZEROS) && (prec < 0)) { andre@0: if (width > cvtwidth) { andre@0: zerowidth = width - cvtwidth; /* Zero filling */ andre@0: cvtwidth += zerowidth; andre@0: } andre@0: } andre@0: andre@0: if (flags & FLAG_LEFT) { andre@0: if (width > cvtwidth) { andre@0: /* Space filling on the right (i.e. left adjusting) */ andre@0: rightspaces = width - cvtwidth; andre@0: } andre@0: } else { andre@0: if (width > cvtwidth) { andre@0: /* Space filling on the left (i.e. right adjusting) */ andre@0: leftspaces = width - cvtwidth; andre@0: } andre@0: } andre@0: while (--leftspaces >= 0) { andre@0: rv = (*ss->stuff)(ss, " ", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: if (signwidth) { andre@0: rv = (*ss->stuff)(ss, &sign, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: while (--precwidth >= 0) { andre@0: rv = (*ss->stuff)(ss, "0", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: while (--zerowidth >= 0) { andre@0: rv = (*ss->stuff)(ss, "0", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: rv = (*ss->stuff)(ss, src, srclen); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: while (--rightspaces >= 0) { andre@0: rv = (*ss->stuff)(ss, " ", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** Convert a long into its printable form andre@0: */ andre@0: static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix, andre@0: int type, int flags, const char *hexp) andre@0: { andre@0: char cvtbuf[100]; andre@0: char *cvt; andre@0: int digits; andre@0: andre@0: /* according to the man page this needs to happen */ andre@0: if ((prec == 0) && (num == 0)) { andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** Converting decimal is a little tricky. In the unsigned case we andre@0: ** need to stop when we hit 10 digits. In the signed case, we can andre@0: ** stop when the number is zero. andre@0: */ andre@0: cvt = cvtbuf + sizeof(cvtbuf); andre@0: digits = 0; andre@0: while (num) { andre@0: int digit = (((unsigned long)num) % radix) & 0xF; andre@0: *--cvt = hexp[digit]; andre@0: digits++; andre@0: num = (long)(((unsigned long)num) / radix); andre@0: } andre@0: if (digits == 0) { andre@0: *--cvt = '0'; andre@0: digits++; andre@0: } andre@0: andre@0: /* andre@0: ** Now that we have the number converted without its sign, deal with andre@0: ** the sign and zero padding. andre@0: */ andre@0: return fill_n(ss, cvt, digits, width, prec, type, flags); andre@0: } andre@0: andre@0: /* andre@0: ** Convert a 64-bit integer into its printable form andre@0: */ andre@0: static int cvt_ll(SprintfState *ss, PRInt64 num, int width, int prec, int radix, andre@0: int type, int flags, const char *hexp) andre@0: { andre@0: char cvtbuf[100]; andre@0: char *cvt; andre@0: int digits; andre@0: PRInt64 rad; andre@0: andre@0: /* according to the man page this needs to happen */ andre@0: if ((prec == 0) && (LL_IS_ZERO(num))) { andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** Converting decimal is a little tricky. In the unsigned case we andre@0: ** need to stop when we hit 10 digits. In the signed case, we can andre@0: ** stop when the number is zero. andre@0: */ andre@0: LL_I2L(rad, radix); andre@0: cvt = cvtbuf + sizeof(cvtbuf); andre@0: digits = 0; andre@0: while (!LL_IS_ZERO(num)) { andre@0: PRInt32 digit; andre@0: PRInt64 quot, rem; andre@0: LL_UDIVMOD(", &rem, num, rad); andre@0: LL_L2I(digit, rem); andre@0: *--cvt = hexp[digit & 0xf]; andre@0: digits++; andre@0: num = quot; andre@0: } andre@0: if (digits == 0) { andre@0: *--cvt = '0'; andre@0: digits++; andre@0: } andre@0: andre@0: /* andre@0: ** Now that we have the number converted without its sign, deal with andre@0: ** the sign and zero padding. andre@0: */ andre@0: return fill_n(ss, cvt, digits, width, prec, type, flags); andre@0: } andre@0: andre@0: /* andre@0: ** Convert a double precision floating point number into its printable andre@0: ** form. andre@0: ** andre@0: ** XXX stop using sprintf to convert floating point andre@0: */ andre@0: static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) andre@0: { andre@0: char fin[20]; andre@0: char fout[300]; andre@0: int amount = fmt1 - fmt0; andre@0: andre@0: PR_ASSERT((amount > 0) && (amount < sizeof(fin))); andre@0: if (amount >= sizeof(fin)) { andre@0: /* Totally bogus % command to sprintf. Just ignore it */ andre@0: return 0; andre@0: } andre@0: memcpy(fin, fmt0, amount); andre@0: fin[amount] = 0; andre@0: andre@0: /* Convert floating point using the native sprintf code */ andre@0: #ifdef DEBUG andre@0: { andre@0: const char *p = fin; andre@0: while (*p) { andre@0: PR_ASSERT(*p != 'L'); andre@0: p++; andre@0: } andre@0: } andre@0: #endif andre@0: sprintf(fout, fin, d); andre@0: andre@0: /* andre@0: ** This assert will catch overflow's of fout, when building with andre@0: ** debugging on. At least this way we can track down the evil piece andre@0: ** of calling code and fix it! andre@0: */ andre@0: PR_ASSERT(strlen(fout) < sizeof(fout)); andre@0: andre@0: return (*ss->stuff)(ss, fout, strlen(fout)); andre@0: } andre@0: andre@0: /* andre@0: ** Convert a string into its printable form. "width" is the output andre@0: ** width. "prec" is the maximum number of characters of "s" to output, andre@0: ** where -1 means until NUL. andre@0: */ andre@0: static int cvt_s(SprintfState *ss, const char *str, int width, int prec, andre@0: int flags) andre@0: { andre@0: int slen; andre@0: andre@0: if (prec == 0) andre@0: return 0; andre@0: andre@0: /* Limit string length by precision value */ andre@0: if (!str) { andre@0: str = "(null)"; andre@0: } andre@0: if (prec > 0) { andre@0: /* this is: slen = strnlen(str, prec); */ andre@0: register const char *s; andre@0: andre@0: for(s = str; prec && *s; s++, prec-- ) andre@0: ; andre@0: slen = s - str; andre@0: } else { andre@0: slen = strlen(str); andre@0: } andre@0: andre@0: /* and away we go */ andre@0: return fill2(ss, str, slen, width, flags); andre@0: } andre@0: andre@0: /* andre@0: ** BuildArgArray stands for Numbered Argument list Sprintf andre@0: ** for example, andre@0: ** fmp = "%4$i, %2$d, %3s, %1d"; andre@0: ** the number must start from 1, and no gap among them andre@0: */ andre@0: andre@0: static struct NumArg* BuildArgArray( const char *fmt, va_list ap, int* rv, struct NumArg* nasArray ) andre@0: { andre@0: int number = 0, cn = 0, i; andre@0: const char* p; andre@0: char c; andre@0: struct NumArg* nas; andre@0: andre@0: andre@0: /* andre@0: ** first pass: andre@0: ** determine how many legal % I have got, then allocate space andre@0: */ andre@0: andre@0: p = fmt; andre@0: *rv = 0; andre@0: i = 0; andre@0: while( ( c = *p++ ) != 0 ){ andre@0: if( c != '%' ) andre@0: continue; andre@0: if( ( c = *p++ ) == '%' ) /* skip %% case */ andre@0: continue; andre@0: andre@0: while( c != 0 ){ andre@0: if( c > '9' || c < '0' ){ andre@0: if( c == '$' ){ /* numbered argument case */ andre@0: if( i > 0 ){ andre@0: *rv = -1; andre@0: return NULL; andre@0: } andre@0: number++; andre@0: } else{ /* non-numbered argument case */ andre@0: if( number > 0 ){ andre@0: *rv = -1; andre@0: return NULL; andre@0: } andre@0: i = 1; andre@0: } andre@0: break; andre@0: } andre@0: andre@0: c = *p++; andre@0: } andre@0: } andre@0: andre@0: if( number == 0 ){ andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: if( number > NAS_DEFAULT_NUM ){ andre@0: nas = (struct NumArg*)PR_MALLOC( number * sizeof( struct NumArg ) ); andre@0: if( !nas ){ andre@0: *rv = -1; andre@0: return NULL; andre@0: } andre@0: } else { andre@0: nas = nasArray; andre@0: } andre@0: andre@0: for( i = 0; i < number; i++ ){ andre@0: nas[i].type = TYPE_UNKNOWN; andre@0: } andre@0: andre@0: andre@0: /* andre@0: ** second pass: andre@0: ** set nas[].type andre@0: */ andre@0: andre@0: p = fmt; andre@0: while( ( c = *p++ ) != 0 ){ andre@0: if( c != '%' ) continue; andre@0: c = *p++; andre@0: if( c == '%' ) continue; andre@0: andre@0: cn = 0; andre@0: while( c && c != '$' ){ /* should imporve error check later */ andre@0: cn = cn*10 + c - '0'; andre@0: c = *p++; andre@0: } andre@0: andre@0: if( !c || cn < 1 || cn > number ){ andre@0: *rv = -1; andre@0: break; andre@0: } andre@0: andre@0: /* nas[cn] starts from 0, and make sure nas[cn].type is not assigned */ andre@0: cn--; andre@0: if( nas[cn].type != TYPE_UNKNOWN ) andre@0: continue; andre@0: andre@0: c = *p++; andre@0: andre@0: /* width */ andre@0: if (c == '*') { andre@0: /* not supported feature, for the argument is not numbered */ andre@0: *rv = -1; andre@0: break; andre@0: } andre@0: andre@0: while ((c >= '0') && (c <= '9')) { andre@0: c = *p++; andre@0: } andre@0: andre@0: /* precision */ andre@0: if (c == '.') { andre@0: c = *p++; andre@0: if (c == '*') { andre@0: /* not supported feature, for the argument is not numbered */ andre@0: *rv = -1; andre@0: break; andre@0: } andre@0: andre@0: while ((c >= '0') && (c <= '9')) { andre@0: c = *p++; andre@0: } andre@0: } andre@0: andre@0: /* size */ andre@0: nas[cn].type = TYPE_INTN; andre@0: if (c == 'h') { andre@0: nas[cn].type = TYPE_INT16; andre@0: c = *p++; andre@0: } else if (c == 'L') { andre@0: /* XXX not quite sure here */ andre@0: nas[cn].type = TYPE_INT64; andre@0: c = *p++; andre@0: } else if (c == 'l') { andre@0: nas[cn].type = TYPE_INT32; andre@0: c = *p++; andre@0: if (c == 'l') { andre@0: nas[cn].type = TYPE_INT64; andre@0: c = *p++; andre@0: } andre@0: } andre@0: andre@0: /* format */ andre@0: switch (c) { andre@0: case 'd': andre@0: case 'c': andre@0: case 'i': andre@0: case 'o': andre@0: case 'u': andre@0: case 'x': andre@0: case 'X': andre@0: break; andre@0: andre@0: case 'e': andre@0: case 'f': andre@0: case 'g': andre@0: nas[ cn ].type = TYPE_DOUBLE; andre@0: break; andre@0: andre@0: case 'p': andre@0: /* XXX should use cpp */ andre@0: if (sizeof(void *) == sizeof(PRInt32)) { andre@0: nas[ cn ].type = TYPE_UINT32; andre@0: } else if (sizeof(void *) == sizeof(PRInt64)) { andre@0: nas[ cn ].type = TYPE_UINT64; andre@0: } else if (sizeof(void *) == sizeof(PRIntn)) { andre@0: nas[ cn ].type = TYPE_UINTN; andre@0: } else { andre@0: nas[ cn ].type = TYPE_UNKNOWN; andre@0: } andre@0: break; andre@0: andre@0: case 'S': andre@0: #ifdef WIN32 andre@0: nas[ cn ].type = TYPE_WSTRING; andre@0: break; andre@0: #endif andre@0: case 'C': andre@0: case 'E': andre@0: case 'G': andre@0: /* XXX not supported I suppose */ andre@0: PR_ASSERT(0); andre@0: nas[ cn ].type = TYPE_UNKNOWN; andre@0: break; andre@0: andre@0: case 's': andre@0: nas[ cn ].type = TYPE_STRING; andre@0: break; andre@0: andre@0: case 'n': andre@0: nas[ cn ].type = TYPE_INTSTR; andre@0: break; andre@0: andre@0: default: andre@0: PR_ASSERT(0); andre@0: nas[ cn ].type = TYPE_UNKNOWN; andre@0: break; andre@0: } andre@0: andre@0: /* get a legal para. */ andre@0: if( nas[ cn ].type == TYPE_UNKNOWN ){ andre@0: *rv = -1; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: andre@0: /* andre@0: ** third pass andre@0: ** fill the nas[cn].ap andre@0: */ andre@0: andre@0: if( *rv < 0 ){ andre@0: if( nas != nasArray ) andre@0: PR_DELETE( nas ); andre@0: return NULL; andre@0: } andre@0: andre@0: cn = 0; andre@0: while( cn < number ){ andre@0: if( nas[cn].type == TYPE_UNKNOWN ){ andre@0: cn++; andre@0: continue; andre@0: } andre@0: andre@0: switch( nas[cn].type ){ andre@0: case TYPE_INT16: andre@0: case TYPE_UINT16: andre@0: case TYPE_INTN: andre@0: nas[cn].u.i = va_arg( ap, int ); andre@0: break; andre@0: andre@0: case TYPE_UINTN: andre@0: nas[cn].u.ui = va_arg( ap, unsigned int ); andre@0: break; andre@0: andre@0: case TYPE_INT32: andre@0: nas[cn].u.i32 = va_arg( ap, PRInt32 ); andre@0: break; andre@0: andre@0: case TYPE_UINT32: andre@0: nas[cn].u.ui32 = va_arg( ap, PRUint32 ); andre@0: break; andre@0: andre@0: case TYPE_INT64: andre@0: nas[cn].u.ll = va_arg( ap, PRInt64 ); andre@0: break; andre@0: andre@0: case TYPE_UINT64: andre@0: nas[cn].u.ull = va_arg( ap, PRUint64 ); andre@0: break; andre@0: andre@0: case TYPE_STRING: andre@0: nas[cn].u.s = va_arg( ap, char* ); andre@0: break; andre@0: andre@0: #ifdef WIN32 andre@0: case TYPE_WSTRING: andre@0: nas[cn].u.ws = va_arg( ap, WCHAR* ); andre@0: break; andre@0: #endif andre@0: andre@0: case TYPE_INTSTR: andre@0: nas[cn].u.ip = va_arg( ap, int* ); andre@0: break; andre@0: andre@0: case TYPE_DOUBLE: andre@0: nas[cn].u.d = va_arg( ap, double ); andre@0: break; andre@0: andre@0: default: andre@0: if( nas != nasArray ) andre@0: PR_DELETE( nas ); andre@0: *rv = -1; andre@0: return NULL; andre@0: } andre@0: andre@0: cn++; andre@0: } andre@0: andre@0: andre@0: return nas; andre@0: } andre@0: andre@0: /* andre@0: ** The workhorse sprintf code. andre@0: */ andre@0: static int dosprintf(SprintfState *ss, const char *fmt, va_list ap) andre@0: { andre@0: char c; andre@0: int flags, width, prec, radix, type; andre@0: union { andre@0: char ch; andre@0: int i; andre@0: long l; andre@0: PRInt64 ll; andre@0: double d; andre@0: const char *s; andre@0: int *ip; andre@0: #ifdef WIN32 andre@0: const WCHAR *ws; andre@0: #endif andre@0: } u; andre@0: const char *fmt0; andre@0: static char *hex = "0123456789abcdef"; andre@0: static char *HEX = "0123456789ABCDEF"; andre@0: char *hexp; andre@0: int rv, i; andre@0: struct NumArg* nas = NULL; andre@0: struct NumArg* nap; andre@0: struct NumArg nasArray[ NAS_DEFAULT_NUM ]; andre@0: char pattern[20]; andre@0: const char* dolPt = NULL; /* in "%4$.2f", dolPt will point to . */ andre@0: #ifdef WIN32 andre@0: char *pBuf = NULL; andre@0: #endif andre@0: andre@0: /* andre@0: ** build an argument array, IF the fmt is numbered argument andre@0: ** list style, to contain the Numbered Argument list pointers andre@0: */ andre@0: andre@0: nas = BuildArgArray( fmt, ap, &rv, nasArray ); andre@0: if( rv < 0 ){ andre@0: /* the fmt contains error Numbered Argument format, jliu@netscape.com */ andre@0: PR_ASSERT(0); andre@0: return rv; andre@0: } andre@0: andre@0: while ((c = *fmt++) != 0) { andre@0: if (c != '%') { andre@0: rv = (*ss->stuff)(ss, fmt - 1, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: continue; andre@0: } andre@0: fmt0 = fmt - 1; andre@0: andre@0: /* andre@0: ** Gobble up the % format string. Hopefully we have handled all andre@0: ** of the strange cases! andre@0: */ andre@0: flags = 0; andre@0: c = *fmt++; andre@0: if (c == '%') { andre@0: /* quoting a % with %% */ andre@0: rv = (*ss->stuff)(ss, fmt - 1, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: continue; andre@0: } andre@0: andre@0: if( nas != NULL ){ andre@0: /* the fmt contains the Numbered Arguments feature */ andre@0: i = 0; andre@0: while( c && c != '$' ){ /* should imporve error check later */ andre@0: i = ( i * 10 ) + ( c - '0' ); andre@0: c = *fmt++; andre@0: } andre@0: andre@0: if( nas[i-1].type == TYPE_UNKNOWN ){ andre@0: if( nas && ( nas != nasArray ) ) andre@0: PR_DELETE( nas ); andre@0: return -1; andre@0: } andre@0: andre@0: nap = &nas[i-1]; andre@0: dolPt = fmt; andre@0: c = *fmt++; andre@0: } andre@0: andre@0: /* andre@0: * Examine optional flags. Note that we do not implement the andre@0: * '#' flag of sprintf(). The ANSI C spec. of the '#' flag is andre@0: * somewhat ambiguous and not ideal, which is perhaps why andre@0: * the various sprintf() implementations are inconsistent andre@0: * on this feature. andre@0: */ andre@0: while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { andre@0: if (c == '-') flags |= FLAG_LEFT; andre@0: if (c == '+') flags |= FLAG_SIGNED; andre@0: if (c == ' ') flags |= FLAG_SPACED; andre@0: if (c == '0') flags |= FLAG_ZEROS; andre@0: c = *fmt++; andre@0: } andre@0: if (flags & FLAG_SIGNED) flags &= ~FLAG_SPACED; andre@0: if (flags & FLAG_LEFT) flags &= ~FLAG_ZEROS; andre@0: andre@0: /* width */ andre@0: if (c == '*') { andre@0: c = *fmt++; andre@0: width = va_arg(ap, int); andre@0: } else { andre@0: width = 0; andre@0: while ((c >= '0') && (c <= '9')) { andre@0: width = (width * 10) + (c - '0'); andre@0: c = *fmt++; andre@0: } andre@0: } andre@0: andre@0: /* precision */ andre@0: prec = -1; andre@0: if (c == '.') { andre@0: c = *fmt++; andre@0: if (c == '*') { andre@0: c = *fmt++; andre@0: prec = va_arg(ap, int); andre@0: } else { andre@0: prec = 0; andre@0: while ((c >= '0') && (c <= '9')) { andre@0: prec = (prec * 10) + (c - '0'); andre@0: c = *fmt++; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* size */ andre@0: type = TYPE_INTN; andre@0: if (c == 'h') { andre@0: type = TYPE_INT16; andre@0: c = *fmt++; andre@0: } else if (c == 'L') { andre@0: /* XXX not quite sure here */ andre@0: type = TYPE_INT64; andre@0: c = *fmt++; andre@0: } else if (c == 'l') { andre@0: type = TYPE_INT32; andre@0: c = *fmt++; andre@0: if (c == 'l') { andre@0: type = TYPE_INT64; andre@0: c = *fmt++; andre@0: } andre@0: } andre@0: andre@0: /* format */ andre@0: hexp = hex; andre@0: switch (c) { andre@0: case 'd': case 'i': /* decimal/integer */ andre@0: radix = 10; andre@0: goto fetch_and_convert; andre@0: andre@0: case 'o': /* octal */ andre@0: radix = 8; andre@0: type |= 1; andre@0: goto fetch_and_convert; andre@0: andre@0: case 'u': /* unsigned decimal */ andre@0: radix = 10; andre@0: type |= 1; andre@0: goto fetch_and_convert; andre@0: andre@0: case 'x': /* unsigned hex */ andre@0: radix = 16; andre@0: type |= 1; andre@0: goto fetch_and_convert; andre@0: andre@0: case 'X': /* unsigned HEX */ andre@0: radix = 16; andre@0: hexp = HEX; andre@0: type |= 1; andre@0: goto fetch_and_convert; andre@0: andre@0: fetch_and_convert: andre@0: switch (type) { andre@0: case TYPE_INT16: andre@0: u.l = nas ? nap->u.i : va_arg(ap, int); andre@0: if (u.l < 0) { andre@0: u.l = -u.l; andre@0: flags |= FLAG_NEG; andre@0: } andre@0: goto do_long; andre@0: case TYPE_UINT16: andre@0: u.l = (nas ? nap->u.i : va_arg(ap, int)) & 0xffff; andre@0: goto do_long; andre@0: case TYPE_INTN: andre@0: u.l = nas ? nap->u.i : va_arg(ap, int); andre@0: if (u.l < 0) { andre@0: u.l = -u.l; andre@0: flags |= FLAG_NEG; andre@0: } andre@0: goto do_long; andre@0: case TYPE_UINTN: andre@0: u.l = (long)(nas ? nap->u.ui : va_arg(ap, unsigned int)); andre@0: goto do_long; andre@0: andre@0: case TYPE_INT32: andre@0: u.l = nas ? nap->u.i32 : va_arg(ap, PRInt32); andre@0: if (u.l < 0) { andre@0: u.l = -u.l; andre@0: flags |= FLAG_NEG; andre@0: } andre@0: goto do_long; andre@0: case TYPE_UINT32: andre@0: u.l = (long)(nas ? nap->u.ui32 : va_arg(ap, PRUint32)); andre@0: do_long: andre@0: rv = cvt_l(ss, u.l, width, prec, radix, type, flags, hexp); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: break; andre@0: andre@0: case TYPE_INT64: andre@0: u.ll = nas ? nap->u.ll : va_arg(ap, PRInt64); andre@0: if (!LL_GE_ZERO(u.ll)) { andre@0: LL_NEG(u.ll, u.ll); andre@0: flags |= FLAG_NEG; andre@0: } andre@0: goto do_longlong; andre@0: case TYPE_UINT64: andre@0: u.ll = nas ? nap->u.ull : va_arg(ap, PRUint64); andre@0: do_longlong: andre@0: rv = cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: break; andre@0: } andre@0: break; andre@0: andre@0: case 'e': andre@0: case 'E': andre@0: case 'f': andre@0: case 'g': andre@0: u.d = nas ? nap->u.d : va_arg(ap, double); andre@0: if( nas != NULL ){ andre@0: i = fmt - dolPt; andre@0: if( i < sizeof( pattern ) ){ andre@0: pattern[0] = '%'; andre@0: memcpy( &pattern[1], dolPt, i ); andre@0: rv = cvt_f(ss, u.d, pattern, &pattern[i+1] ); andre@0: } andre@0: } else andre@0: rv = cvt_f(ss, u.d, fmt0, fmt); andre@0: andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: break; andre@0: andre@0: case 'c': andre@0: u.ch = nas ? nap->u.i : va_arg(ap, int); andre@0: if ((flags & FLAG_LEFT) == 0) { andre@0: while (width-- > 1) { andre@0: rv = (*ss->stuff)(ss, " ", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: } andre@0: rv = (*ss->stuff)(ss, &u.ch, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: if (flags & FLAG_LEFT) { andre@0: while (width-- > 1) { andre@0: rv = (*ss->stuff)(ss, " ", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: } andre@0: break; andre@0: andre@0: case 'p': andre@0: if (sizeof(void *) == sizeof(PRInt32)) { andre@0: type = TYPE_UINT32; andre@0: } else if (sizeof(void *) == sizeof(PRInt64)) { andre@0: type = TYPE_UINT64; andre@0: } else if (sizeof(void *) == sizeof(int)) { andre@0: type = TYPE_UINTN; andre@0: } else { andre@0: PR_ASSERT(0); andre@0: break; andre@0: } andre@0: radix = 16; andre@0: goto fetch_and_convert; andre@0: andre@0: #ifndef WIN32 andre@0: case 'S': andre@0: /* XXX not supported I suppose */ andre@0: PR_ASSERT(0); andre@0: break; andre@0: #endif andre@0: andre@0: #if 0 andre@0: case 'C': andre@0: case 'E': andre@0: case 'G': andre@0: /* XXX not supported I suppose */ andre@0: PR_ASSERT(0); andre@0: break; andre@0: #endif andre@0: andre@0: #ifdef WIN32 andre@0: case 'S': andre@0: u.ws = nas ? nap->u.ws : va_arg(ap, const WCHAR*); andre@0: andre@0: /* Get the required size in rv */ andre@0: rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); andre@0: if (rv == 0) andre@0: rv = 1; andre@0: pBuf = PR_MALLOC(rv); andre@0: WideCharToMultiByte(CP_ACP, 0, u.ws, -1, pBuf, (int)rv, NULL, NULL); andre@0: pBuf[rv-1] = '\0'; andre@0: andre@0: rv = cvt_s(ss, pBuf, width, prec, flags); andre@0: andre@0: /* We don't need the allocated buffer anymore */ andre@0: PR_Free(pBuf); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: break; andre@0: andre@0: #endif andre@0: andre@0: case 's': andre@0: u.s = nas ? nap->u.s : va_arg(ap, const char*); andre@0: rv = cvt_s(ss, u.s, width, prec, flags); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: break; andre@0: andre@0: case 'n': andre@0: u.ip = nas ? nap->u.ip : va_arg(ap, int*); andre@0: if (u.ip) { andre@0: *u.ip = ss->cur - ss->base; andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: /* Not a % token after all... skip it */ andre@0: #if 0 andre@0: PR_ASSERT(0); andre@0: #endif andre@0: rv = (*ss->stuff)(ss, "%", 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: rv = (*ss->stuff)(ss, fmt - 1, 1); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* Stuff trailing NUL */ andre@0: rv = (*ss->stuff)(ss, "\0", 1); andre@0: andre@0: if( nas && ( nas != nasArray ) ){ andre@0: PR_DELETE( nas ); andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: static int FuncStuff(SprintfState *ss, const char *sp, PRUint32 len) andre@0: { andre@0: int rv; andre@0: andre@0: rv = (*ss->func)(ss->arg, sp, len); andre@0: if (rv < 0) { andre@0: return rv; andre@0: } andre@0: ss->maxlen += len; andre@0: return 0; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_sxprintf(PRStuffFunc func, void *arg, andre@0: const char *fmt, ...) andre@0: { andre@0: va_list ap; andre@0: PRUint32 rv; andre@0: andre@0: va_start(ap, fmt); andre@0: rv = PR_vsxprintf(func, arg, fmt, ap); andre@0: va_end(ap); andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_vsxprintf(PRStuffFunc func, void *arg, andre@0: const char *fmt, va_list ap) andre@0: { andre@0: SprintfState ss; andre@0: int rv; andre@0: andre@0: ss.stuff = FuncStuff; andre@0: ss.func = func; andre@0: ss.arg = arg; andre@0: ss.maxlen = 0; andre@0: rv = dosprintf(&ss, fmt, ap); andre@0: return (rv < 0) ? (PRUint32)-1 : ss.maxlen; andre@0: } andre@0: andre@0: /* andre@0: ** Stuff routine that automatically grows the malloc'd output buffer andre@0: ** before it overflows. andre@0: */ andre@0: static int GrowStuff(SprintfState *ss, const char *sp, PRUint32 len) andre@0: { andre@0: ptrdiff_t off; andre@0: char *newbase; andre@0: PRUint32 newlen; andre@0: andre@0: off = ss->cur - ss->base; andre@0: if (off + len >= ss->maxlen) { andre@0: /* Grow the buffer */ andre@0: newlen = ss->maxlen + ((len > 32) ? len : 32); andre@0: if (ss->base) { andre@0: newbase = (char*) PR_REALLOC(ss->base, newlen); andre@0: } else { andre@0: newbase = (char*) PR_MALLOC(newlen); andre@0: } andre@0: if (!newbase) { andre@0: /* Ran out of memory */ andre@0: return -1; andre@0: } andre@0: ss->base = newbase; andre@0: ss->maxlen = newlen; andre@0: ss->cur = ss->base + off; andre@0: } andre@0: andre@0: /* Copy data */ andre@0: while (len) { andre@0: --len; andre@0: *ss->cur++ = *sp++; andre@0: } andre@0: PR_ASSERT((PRUint32)(ss->cur - ss->base) <= ss->maxlen); andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** sprintf into a malloc'd buffer andre@0: */ andre@0: PR_IMPLEMENT(char *) PR_smprintf(const char *fmt, ...) andre@0: { andre@0: va_list ap; andre@0: char *rv; andre@0: andre@0: va_start(ap, fmt); andre@0: rv = PR_vsmprintf(fmt, ap); andre@0: va_end(ap); andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: ** Free memory allocated, for the caller, by PR_smprintf andre@0: */ andre@0: PR_IMPLEMENT(void) PR_smprintf_free(char *mem) andre@0: { andre@0: PR_DELETE(mem); andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) PR_vsmprintf(const char *fmt, va_list ap) andre@0: { andre@0: SprintfState ss; andre@0: int rv; andre@0: andre@0: ss.stuff = GrowStuff; andre@0: ss.base = 0; andre@0: ss.cur = 0; andre@0: ss.maxlen = 0; andre@0: rv = dosprintf(&ss, fmt, ap); andre@0: if (rv < 0) { andre@0: if (ss.base) { andre@0: PR_DELETE(ss.base); andre@0: } andre@0: return 0; andre@0: } andre@0: return ss.base; andre@0: } andre@0: andre@0: /* andre@0: ** Stuff routine that discards overflow data andre@0: */ andre@0: static int LimitStuff(SprintfState *ss, const char *sp, PRUint32 len) andre@0: { andre@0: PRUint32 limit = ss->maxlen - (ss->cur - ss->base); andre@0: andre@0: if (len > limit) { andre@0: len = limit; andre@0: } andre@0: while (len) { andre@0: --len; andre@0: *ss->cur++ = *sp++; andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: ** sprintf into a fixed size buffer. Make sure there is a NUL at the end andre@0: ** when finished. andre@0: */ andre@0: PR_IMPLEMENT(PRUint32) PR_snprintf(char *out, PRUint32 outlen, const char *fmt, ...) andre@0: { andre@0: va_list ap; andre@0: PRUint32 rv; andre@0: andre@0: va_start(ap, fmt); andre@0: rv = PR_vsnprintf(out, outlen, fmt, ap); andre@0: va_end(ap); andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_vsnprintf(char *out, PRUint32 outlen,const char *fmt, andre@0: va_list ap) andre@0: { andre@0: SprintfState ss; andre@0: PRUint32 n; andre@0: andre@0: PR_ASSERT((PRInt32)outlen > 0); andre@0: if ((PRInt32)outlen <= 0) { andre@0: return 0; andre@0: } andre@0: andre@0: ss.stuff = LimitStuff; andre@0: ss.base = out; andre@0: ss.cur = out; andre@0: ss.maxlen = outlen; andre@0: (void) dosprintf(&ss, fmt, ap); andre@0: andre@0: /* If we added chars, and we didn't append a null, do it now. */ andre@0: if( (ss.cur != ss.base) && (*(ss.cur - 1) != '\0') ) andre@0: *(ss.cur - 1) = '\0'; andre@0: andre@0: n = ss.cur - ss.base; andre@0: return n ? n - 1 : n; andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) PR_sprintf_append(char *last, const char *fmt, ...) andre@0: { andre@0: va_list ap; andre@0: char *rv; andre@0: andre@0: va_start(ap, fmt); andre@0: rv = PR_vsprintf_append(last, fmt, ap); andre@0: va_end(ap); andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) PR_vsprintf_append(char *last, const char *fmt, va_list ap) andre@0: { andre@0: SprintfState ss; andre@0: int rv; andre@0: andre@0: ss.stuff = GrowStuff; andre@0: if (last) { andre@0: int lastlen = strlen(last); andre@0: ss.base = last; andre@0: ss.cur = last + lastlen; andre@0: ss.maxlen = lastlen; andre@0: } else { andre@0: ss.base = 0; andre@0: ss.cur = 0; andre@0: ss.maxlen = 0; andre@0: } andre@0: rv = dosprintf(&ss, fmt, ap); andre@0: if (rv < 0) { andre@0: if (ss.base) { andre@0: PR_DELETE(ss.base); andre@0: } andre@0: return 0; andre@0: } andre@0: return ss.base; andre@0: } andre@0: