jsprf.c revision 6b15695578f07a3f72c4c9475c1a261a3021472a
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
** Portable safe sprintf code.
**
** Author: Kipp E.B. Hickman
*/
#include "jsstddef.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "jsprf.h"
#include "jslong.h"
#include "jsutil.h" /* Added by JSIFY */
/*
** Note: on some platforms va_list is defined as an array,
** and requires array notation.
*/
#ifdef HAVE_VA_COPY
#elif defined(HAVE_VA_LIST_AS_ARRAY)
#else
#endif
/*
** WARNING: This code may *NOT* call JS_LOG (because JS_LOG calls it)
*/
/*
** XXX This needs to be internationalized!
*/
typedef struct SprintfStateStr SprintfState;
struct SprintfStateStr {
char *base;
char *cur;
void *arg;
};
/*
** Numbered Arguement State
*/
struct NumArgState{
int type; /* type of the current ap */
};
#define TYPE_INT16 0
#define TYPE_UINT16 1
#define TYPE_INTN 2
#define TYPE_UINTN 3
#define TYPE_INT32 4
#define TYPE_UINT32 5
#define TYPE_INT64 6
#define TYPE_UINT64 7
#define TYPE_STRING 8
#define TYPE_DOUBLE 9
#define TYPE_INTSTR 10
#define TYPE_UNKNOWN 20
#define FLAG_LEFT 0x1
#define FLAG_SIGNED 0x2
#define FLAG_SPACED 0x4
#define FLAG_ZEROS 0x8
#define FLAG_NEG 0x10
/*
** Fill into the buffer using the data in src
*/
int flags)
{
char space = ' ';
int rv;
if (flags & FLAG_ZEROS) {
space = '0';
}
while (--width >= 0) {
if (rv < 0) {
return rv;
}
}
}
/* Copy out the source data */
if (rv < 0) {
return rv;
}
while (--width >= 0) {
if (rv < 0) {
return rv;
}
}
}
return 0;
}
/*
** Fill a number. The order is: optional-sign zero-filling conversion-digits
*/
{
int zerowidth = 0;
int precwidth = 0;
int signwidth = 0;
int leftspaces = 0;
int rightspaces = 0;
int cvtwidth;
int rv;
char sign;
if ((type & 1) == 0) {
sign = '-';
signwidth = 1;
} else if (flags & FLAG_SIGNED) {
sign = '+';
signwidth = 1;
} else if (flags & FLAG_SPACED) {
sign = ' ';
signwidth = 1;
}
}
if (prec > 0) {
}
}
}
}
/* Space filling on the right (i.e. left adjusting) */
}
} else {
/* Space filling on the left (i.e. right adjusting) */
}
}
while (--leftspaces >= 0) {
if (rv < 0) {
return rv;
}
}
if (signwidth) {
if (rv < 0) {
return rv;
}
}
while (--precwidth >= 0) {
if (rv < 0) {
return rv;
}
}
while (--zerowidth >= 0) {
if (rv < 0) {
return rv;
}
}
if (rv < 0) {
return rv;
}
while (--rightspaces >= 0) {
if (rv < 0) {
return rv;
}
}
return 0;
}
/*
** Convert a long into its printable form
*/
{
char cvtbuf[100];
char *cvt;
int digits;
/* according to the man page this needs to happen */
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.
*/
digits = 0;
while (num) {
digits++;
}
if (digits == 0) {
*--cvt = '0';
digits++;
}
/*
** Now that we have the number converted without its sign, deal with
** the sign and zero padding.
*/
}
/*
** Convert a 64-bit integer into its printable form
*/
{
char cvtbuf[100];
char *cvt;
int digits;
/* according to the man page this needs to happen */
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.
*/
digits = 0;
while (!JSLL_IS_ZERO(num)) {
digits++;
}
if (digits == 0) {
*--cvt = '0';
digits++;
}
/*
** Now that we have the number converted without its sign, deal with
** the sign and zero padding.
*/
}
/*
** Convert a double precision floating point number into its printable
** form.
**
** XXX stop using sprintf to convert floating point
*/
{
char fin[20];
char fout[300];
/* Totally bogus % command to sprintf. Just ignore it */
return 0;
}
/* Convert floating point using the native sprintf code */
#ifdef DEBUG
{
const char *p = fin;
while (*p) {
JS_ASSERT(*p != 'L');
p++;
}
}
#endif
/*
** This assert will catch overflow's of fout, when building with
** debugging on. At least this way we can track down the evil piece
** of calling code and fix it!
*/
}
/*
** 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.
*/
int flags)
{
int slen;
if (prec == 0)
return 0;
/* Limit string length by precision value */
if (prec > 0) {
}
}
/* and away we go */
}
/*
** BiuldArgArray 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 char *fmt, va_list ap, int* rv, struct NumArgState* nasArray )
{
const char* p;
char c;
struct NumArgState* nas;
/*
** first pass:
** detemine how many legal % I have got, then allocate space
*/
p = fmt;
*rv = 0;
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 csae */
if( i > 0 ){
*rv = -1;
return NULL;
}
number++;
} else { /* non-numbered argument case */
if( number > 0 ){
*rv = -1;
return NULL;
}
i = 1;
}
break;
}
c = *p++;
}
}
if( number == 0 ){
return NULL;
}
if( number > NAS_DEFAULT_NUM ){
if( !nas ){
*rv = -1;
return NULL;
}
} else {
}
for( i = 0; i < number; i++ ){
}
/*
** 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 imporve error check later */
c = *p++;
}
*rv = -1;
break;
}
/* nas[cn] starts from 0, and make sure nas[cn].type is not assigned */
cn--;
continue;
c = *p++;
/* width */
if (c == '*') {
/* not supported feature, for the argument is not numbered */
*rv = -1;
break;
}
while ((c >= '0') && (c <= '9')) {
c = *p++;
}
/* precision */
if (c == '.') {
c = *p++;
if (c == '*') {
/* not supported feature, for the argument is not numbered */
*rv = -1;
break;
}
while ((c >= '0') && (c <= '9')) {
c = *p++;
}
}
/* size */
if (c == 'h') {
c = *p++;
} else if (c == 'L') {
/* XXX not quite sure here */
c = *p++;
} else if (c == 'l') {
c = *p++;
if (c == 'l') {
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':
break;
case 'p':
/* XXX should use cpp */
if (sizeof(void *) == sizeof(JSInt32)) {
} else if (sizeof(void *) == sizeof(JSInt64)) {
} else if (sizeof(void *) == sizeof(JSIntn)) {
} else {
}
break;
case 'C':
case 'S':
case 'E':
case 'G':
/* XXX not supported I suppose */
JS_ASSERT(0);
break;
case 's':
break;
case 'n':
break;
default:
JS_ASSERT(0);
break;
}
/* get a legal para. */
*rv = -1;
break;
}
}
/*
** third pass
** fill the nas[cn].ap
*/
if( *rv < 0 ){
return NULL;
}
cn = 0;
cn++;
continue;
}
case TYPE_INT16:
case TYPE_UINT16:
case TYPE_INTN:
default:
*rv = -1;
return NULL;
}
cn++;
}
return nas;
}
/*
** The workhorse sprintf code.
*/
{
char c;
union {
char ch;
int i;
long l;
double d;
const char *s;
int *ip;
} u;
const char *fmt0;
static char *hex = "0123456789abcdef";
static char *HEX = "0123456789ABCDEF";
char *hexp;
int rv, i;
char pattern[20];
/*
** build an argument array, IF the fmt is numbered argument
** list style, to contain the Numbered Argument list pointers
*/
if( rv < 0 ){
/* the fmt contains error Numbered Argument format, jliu@netscape.com */
JS_ASSERT(0);
return rv;
}
while ((c = *fmt++) != 0) {
if (c != '%') {
if (rv < 0) {
return rv;
}
continue;
}
/*
** Gobble up the % format string. Hopefully we have handled all
** of the strange cases!
*/
flags = 0;
c = *fmt++;
if (c == '%') {
/* quoting a % with %% */
if (rv < 0) {
return rv;
}
continue;
}
/* the fmt contains the Numbered Arguments feature */
i = 0;
while( c && c != '$' ){ /* should imporve error check later */
i = ( i * 10 ) + ( c - '0' );
c = *fmt++;
}
return -1;
}
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')) {
c = *fmt++;
}
/* width */
if (c == '*') {
c = *fmt++;
} else {
width = 0;
while ((c >= '0') && (c <= '9')) {
c = *fmt++;
}
}
/* precision */
prec = -1;
if (c == '.') {
c = *fmt++;
if (c == '*') {
c = *fmt++;
} else {
prec = 0;
while ((c >= '0') && (c <= '9')) {
c = *fmt++;
}
}
}
/* size */
if (c == 'h') {
type = TYPE_INT16;
c = *fmt++;
} else if (c == 'L') {
/* XXX not quite sure here */
type = TYPE_INT64;
c = *fmt++;
} else if (c == 'l') {
type = TYPE_INT32;
c = *fmt++;
if (c == 'l') {
type = TYPE_INT64;
c = *fmt++;
}
}
/* format */
switch (c) {
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;
type |= 1;
goto fetch_and_convert;
switch (type) {
case TYPE_INT16:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINT16:
goto do_long;
case TYPE_INTN:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINTN:
goto do_long;
case TYPE_INT32:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINT32:
if (rv < 0) {
return rv;
}
break;
case TYPE_INT64:
if (!JSLL_GE_ZERO(u.ll)) {
}
goto do_longlong;
case TYPE_UINT64:
if (rv < 0) {
return rv;
}
break;
}
break;
case 'e':
case 'E':
case 'f':
case 'g':
if( i < (int)sizeof( pattern ) ){
pattern[0] = '%';
}
} else
if (rv < 0) {
return rv;
}
break;
case 'c':
while (width-- > 1) {
if (rv < 0) {
return rv;
}
}
}
if (rv < 0) {
return rv;
}
while (width-- > 1) {
if (rv < 0) {
return rv;
}
}
}
break;
case 'p':
if (sizeof(void *) == sizeof(JSInt32)) {
type = TYPE_UINT32;
} else if (sizeof(void *) == sizeof(JSInt64)) {
type = TYPE_UINT64;
} else if (sizeof(void *) == sizeof(int)) {
type = TYPE_UINTN;
} else {
JS_ASSERT(0);
break;
}
radix = 16;
goto fetch_and_convert;
#if 0
case 'C':
case 'S':
case 'E':
case 'G':
/* XXX not supported I suppose */
JS_ASSERT(0);
break;
#endif
case 's':
if (rv < 0) {
return rv;
}
break;
case 'n':
if (u.ip) {
}
break;
default:
/* Not a % token after all... skip it */
#if 0
JS_ASSERT(0);
#endif
if (rv < 0) {
return rv;
}
if (rv < 0) {
return rv;
}
}
}
/* Stuff trailing NUL */
}
return rv;
}
/************************************************************************/
{
int rv;
if (rv < 0) {
return rv;
}
return 0;
}
const char *fmt, ...)
{
int rv;
return rv;
}
{
int rv;
}
/*
** Stuff routine that automatically grows the malloc'd output buffer
** before it overflows.
*/
{
char *newbase;
/* Grow the buffer */
} else {
}
if (!newbase) {
/* Ran out of memory */
return -1;
}
}
/* Copy data */
while (len) {
--len;
}
return 0;
}
/*
** sprintf into a malloc'd buffer
*/
{
char *rv;
return rv;
}
/*
** Free memory allocated, for the caller, by JS_smprintf
*/
{
}
{
int rv;
if (rv < 0) {
}
return 0;
}
}
/*
** Stuff routine that discards overflow data
*/
{
}
while (len) {
--len;
}
return 0;
}
/*
** sprintf into a fixed size buffer. Make sure there is a NUL at the end
** when finished.
*/
{
int rv;
return 0;
}
return rv;
}
{
JSUint32 n;
return 0;
}
/* If we added chars, and we didn't append a null, do it now. */
return n ? n - 1 : n;
}
{
char *rv;
return rv;
}
{
int rv;
if (last) {
} else {
}
if (rv < 0) {
}
return 0;
}
}