1a18c5681Sdrh /* 2a18c5681Sdrh ** The "printf" code that follows dates from the 1980's. It is in 338b4149cSdrh ** the public domain. 4e84a306bSdrh ** 5e84a306bSdrh ************************************************************************** 6a18c5681Sdrh ** 7ed1fddf4Sdrh ** This file contains code for a set of "printf"-like routines. These 8ed1fddf4Sdrh ** routines format strings much like the printf() from the standard C 9ed1fddf4Sdrh ** library, though the implementation here has enhancements to support 10f5b5f9b9Smistachkin ** SQLite. 11a18c5681Sdrh */ 12a18c5681Sdrh #include "sqliteInt.h" 137c68d60bSdrh 14a18c5681Sdrh /* 15a18c5681Sdrh ** Conversion types fall into various categories as defined by the 16a18c5681Sdrh ** following enumeration. 17a18c5681Sdrh */ 182c338a9dSdrh #define etRADIX 0 /* non-decimal integer types. %x %o */ 19ad5a9d71Sdrh #define etFLOAT 1 /* Floating point. %f */ 20ad5a9d71Sdrh #define etEXP 2 /* Exponentional notation. %e and %E */ 21ad5a9d71Sdrh #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ 22ad5a9d71Sdrh #define etSIZE 4 /* Return number of characters processed so far. %n */ 23ad5a9d71Sdrh #define etSTRING 5 /* Strings. %s */ 24ad5a9d71Sdrh #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ 25ad5a9d71Sdrh #define etPERCENT 7 /* Percent symbol. %% */ 26ad5a9d71Sdrh #define etCHARX 8 /* Characters. %c */ 27a18c5681Sdrh /* The rest are extensions, not normally found in printf() */ 28ad5a9d71Sdrh #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ 29ad5a9d71Sdrh #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', 300cfcf3fbSchw NULL pointers replaced by SQL NULL. %Q */ 31ad5a9d71Sdrh #define etTOKEN 11 /* a pointer to a Token structure */ 32ad5a9d71Sdrh #define etSRCLIST 12 /* a pointer to a SrcList */ 33ad5a9d71Sdrh #define etPOINTER 13 /* The %p conversion */ 34ad5a9d71Sdrh #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ 35ad5a9d71Sdrh #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ 362c338a9dSdrh #define etDECIMAL 16 /* %d or %u, but not %x, %o */ 37e84a306bSdrh 382c338a9dSdrh #define etINVALID 17 /* Any unrecognized conversion type */ 39874ba04cSdrh 40e84a306bSdrh 41e84a306bSdrh /* 42e84a306bSdrh ** An "etByte" is an 8-bit unsigned value. 43e84a306bSdrh */ 44e84a306bSdrh typedef unsigned char etByte; 45a18c5681Sdrh 46a18c5681Sdrh /* 47a18c5681Sdrh ** Each builtin conversion character (ex: the 'd' in "%d") is described 48a18c5681Sdrh ** by an instance of the following structure 49a18c5681Sdrh */ 50a18c5681Sdrh typedef struct et_info { /* Information about each format field */ 51e84a306bSdrh char fmttype; /* The format field code letter */ 52e84a306bSdrh etByte base; /* The base for radix conversion */ 53e84a306bSdrh etByte flags; /* One or more of FLAG_ constants below */ 54e84a306bSdrh etByte type; /* Conversion paradigm */ 5576ff3a0eSdrh etByte charset; /* Offset into aDigits[] of the digits string */ 5676ff3a0eSdrh etByte prefix; /* Offset into aPrefix[] of the prefix string */ 57a18c5681Sdrh } et_info; 58a18c5681Sdrh 59a18c5681Sdrh /* 60e84a306bSdrh ** Allowed values for et_info.flags 61e84a306bSdrh */ 62e84a306bSdrh #define FLAG_SIGNED 1 /* True if the value to convert is signed */ 632c338a9dSdrh #define FLAG_STRING 4 /* Allow infinite precision */ 64e84a306bSdrh 65e84a306bSdrh 66e84a306bSdrh /* 67a18c5681Sdrh ** The following table is searched linearly, so it is good to put the 68a18c5681Sdrh ** most frequently used conversion types first. 69a18c5681Sdrh */ 7076ff3a0eSdrh static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; 7176ff3a0eSdrh static const char aPrefix[] = "-x0\000X0"; 725719628aSdrh static const et_info fmtinfo[] = { 732c338a9dSdrh { 'd', 10, 1, etDECIMAL, 0, 0 }, 744794f735Sdrh { 's', 0, 4, etSTRING, 0, 0 }, 75557cc60fSdrh { 'g', 0, 1, etGENERIC, 30, 0 }, 76153c62c4Sdrh { 'z', 0, 4, etDYNSTRING, 0, 0 }, 774794f735Sdrh { 'q', 0, 4, etSQLESCAPE, 0, 0 }, 784794f735Sdrh { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, 79f3b863edSdanielk1977 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, 80e84a306bSdrh { 'c', 0, 0, etCHARX, 0, 0 }, 8176ff3a0eSdrh { 'o', 8, 0, etRADIX, 0, 2 }, 822c338a9dSdrh { 'u', 10, 0, etDECIMAL, 0, 0 }, 8376ff3a0eSdrh { 'x', 16, 0, etRADIX, 16, 1 }, 8476ff3a0eSdrh { 'X', 16, 0, etRADIX, 0, 4 }, 85b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 86e84a306bSdrh { 'f', 0, 1, etFLOAT, 0, 0 }, 8776ff3a0eSdrh { 'e', 0, 1, etEXP, 30, 0 }, 8876ff3a0eSdrh { 'E', 0, 1, etEXP, 14, 0 }, 8976ff3a0eSdrh { 'G', 0, 1, etGENERIC, 14, 0 }, 90b37df7b9Sdrh #endif 912c338a9dSdrh { 'i', 10, 1, etDECIMAL, 0, 0 }, 92e84a306bSdrh { 'n', 0, 0, etSIZE, 0, 0 }, 93e84a306bSdrh { '%', 0, 0, etPERCENT, 0, 0 }, 9476ff3a0eSdrh { 'p', 16, 0, etPOINTER, 0, 1 }, 957e3ff5d8Sdrh 968236f688Sdrh /* All the rest are undocumented and are for internal use only */ 978236f688Sdrh { 'T', 0, 0, etTOKEN, 0, 0 }, 988236f688Sdrh { 'S', 0, 0, etSRCLIST, 0, 0 }, 998236f688Sdrh { 'r', 10, 1, etORDINAL, 0, 0 }, 100a18c5681Sdrh }; 101a18c5681Sdrh 102a18c5681Sdrh /* 103b37df7b9Sdrh ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point 104a18c5681Sdrh ** conversions will work. 105a18c5681Sdrh */ 106b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 107a18c5681Sdrh /* 108a18c5681Sdrh ** "*val" is a double such that 0.1 <= *val < 10.0 109a18c5681Sdrh ** Return the ascii code for the leading digit of *val, then 110a18c5681Sdrh ** multiply "*val" by 10.0 to renormalize. 111a18c5681Sdrh ** 112a18c5681Sdrh ** Example: 113a18c5681Sdrh ** input: *val = 3.14159 114a18c5681Sdrh ** output: *val = 1.4159 function return = '3' 115a18c5681Sdrh ** 116a18c5681Sdrh ** The counter *cnt is incremented each time. After counter exceeds 117a18c5681Sdrh ** 16 (the number of significant digits in a 64-bit float) '0' is 118a18c5681Sdrh ** always returned. 119a18c5681Sdrh */ 120ea678832Sdrh static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ 121a18c5681Sdrh int digit; 122384eef32Sdrh LONGDOUBLE_TYPE d; 12372b3fbc7Sdrh if( (*cnt)<=0 ) return '0'; 12472b3fbc7Sdrh (*cnt)--; 125a18c5681Sdrh digit = (int)*val; 126a18c5681Sdrh d = digit; 127a18c5681Sdrh digit += '0'; 128a18c5681Sdrh *val = (*val - d)*10.0; 129ea678832Sdrh return (char)digit; 130a18c5681Sdrh } 131b37df7b9Sdrh #endif /* SQLITE_OMIT_FLOATING_POINT */ 132a18c5681Sdrh 13379158e18Sdrh /* 134a6353a3fSdrh ** Set the StrAccum object to an error mode. 135a6353a3fSdrh */ 136a5c1416dSdrh static void setStrAccumError(StrAccum *p, u8 eError){ 137c0490572Sdrh assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); 138a6353a3fSdrh p->accError = eError; 139a6353a3fSdrh p->nAlloc = 0; 140a6353a3fSdrh } 141a6353a3fSdrh 142a6353a3fSdrh /* 143a5c1416dSdrh ** Extra argument values from a PrintfArguments object 144a5c1416dSdrh */ 145a5c1416dSdrh static sqlite3_int64 getIntArg(PrintfArguments *p){ 146a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 147a5c1416dSdrh return sqlite3_value_int64(p->apArg[p->nUsed++]); 148a5c1416dSdrh } 149a5c1416dSdrh static double getDoubleArg(PrintfArguments *p){ 150a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0.0; 151a5c1416dSdrh return sqlite3_value_double(p->apArg[p->nUsed++]); 152a5c1416dSdrh } 153a5c1416dSdrh static char *getTextArg(PrintfArguments *p){ 154a5c1416dSdrh if( p->nArg<=p->nUsed ) return 0; 155a5c1416dSdrh return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); 156a5c1416dSdrh } 157a5c1416dSdrh 158a5c1416dSdrh 159a5c1416dSdrh /* 16079158e18Sdrh ** On machines with a small stack size, you can redefine the 161ed1fddf4Sdrh ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. 16279158e18Sdrh */ 16379158e18Sdrh #ifndef SQLITE_PRINT_BUF_SIZE 16459eedf79Sdrh # define SQLITE_PRINT_BUF_SIZE 70 16550d654daSdrh #endif 16679158e18Sdrh #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ 167a18c5681Sdrh 168a18c5681Sdrh /* 169ed1fddf4Sdrh ** Render a string given by "fmt" into the StrAccum object. 170a18c5681Sdrh */ 171f089aa45Sdrh void sqlite3VXPrintf( 172ade86483Sdrh StrAccum *pAccum, /* Accumulate results here */ 1735f968436Sdrh const char *fmt, /* Format string */ 1745f968436Sdrh va_list ap /* arguments */ 175a18c5681Sdrh ){ 176e84a306bSdrh int c; /* Next character in the format string */ 177e84a306bSdrh char *bufpt; /* Pointer to the conversion buffer */ 178e84a306bSdrh int precision; /* Precision of the current field */ 179e84a306bSdrh int length; /* Length of the field */ 180e84a306bSdrh int idx; /* A general purpose loop counter */ 181a18c5681Sdrh int width; /* Width of the current field */ 182e84a306bSdrh etByte flag_leftjustify; /* True if "-" flag is present */ 1832c338a9dSdrh etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ 184e84a306bSdrh etByte flag_alternateform; /* True if "#" flag is present */ 185531fe878Sdrh etByte flag_altform2; /* True if "!" flag is present */ 186e84a306bSdrh etByte flag_zeropad; /* True if field width constant starts with zero */ 1872c338a9dSdrh etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ 1883e9aeec0Sdrh etByte done; /* Loop termination flag */ 1892c338a9dSdrh etByte cThousand; /* Thousands separator for %d and %u */ 190ad5a9d71Sdrh etByte xtype = etINVALID; /* Conversion paradigm */ 191a5c1416dSdrh u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ 192ed1fddf4Sdrh char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ 19327436af7Sdrh sqlite_uint64 longvalue; /* Value for integer types */ 194384eef32Sdrh LONGDOUBLE_TYPE realvalue; /* Value for real types */ 1955719628aSdrh const et_info *infop; /* Pointer to the appropriate info structure */ 19659eedf79Sdrh char *zOut; /* Rendering buffer */ 19759eedf79Sdrh int nOut; /* Size of the rendering buffer */ 198af8f513fSdrh char *zExtra = 0; /* Malloced memory used by some conversion */ 199b37df7b9Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 200557cc60fSdrh int exp, e2; /* exponent of real numbers */ 201ed1fddf4Sdrh int nsd; /* Number of significant digits returned */ 202a18c5681Sdrh double rounder; /* Used for rounding floating point values */ 203e84a306bSdrh etByte flag_dp; /* True if decimal point should be shown */ 204e84a306bSdrh etByte flag_rtz; /* True if trailing zeros should be removed */ 205a18c5681Sdrh #endif 206a5c1416dSdrh PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ 207ed1fddf4Sdrh char buf[etBUFSIZE]; /* Conversion buffer */ 208a18c5681Sdrh 209a18c5681Sdrh bufpt = 0; 2108236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ 211a5c1416dSdrh pArgList = va_arg(ap, PrintfArguments*); 2128236f688Sdrh bArgList = 1; 213a5c1416dSdrh }else{ 2148236f688Sdrh bArgList = 0; 215a5c1416dSdrh } 216a18c5681Sdrh for(; (c=(*fmt))!=0; ++fmt){ 217a18c5681Sdrh if( c!='%' ){ 218a18c5681Sdrh bufpt = (char *)fmt; 219760b1598Sdrh #if HAVE_STRCHRNUL 220760b1598Sdrh fmt = strchrnul(fmt, '%'); 221760b1598Sdrh #else 222760b1598Sdrh do{ fmt++; }while( *fmt && *fmt != '%' ); 223760b1598Sdrh #endif 224a70a073bSdrh sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); 225760b1598Sdrh if( *fmt==0 ) break; 226a18c5681Sdrh } 227a18c5681Sdrh if( (c=(*++fmt))==0 ){ 228ade86483Sdrh sqlite3StrAccumAppend(pAccum, "%", 1); 229a18c5681Sdrh break; 230a18c5681Sdrh } 231a18c5681Sdrh /* Find out what flags are present */ 2322c338a9dSdrh flag_leftjustify = flag_prefix = cThousand = 233557cc60fSdrh flag_alternateform = flag_altform2 = flag_zeropad = 0; 2343e9aeec0Sdrh done = 0; 235a18c5681Sdrh do{ 236a18c5681Sdrh switch( c ){ 2373e9aeec0Sdrh case '-': flag_leftjustify = 1; break; 2382c338a9dSdrh case '+': flag_prefix = '+'; break; 2392c338a9dSdrh case ' ': flag_prefix = ' '; break; 2403e9aeec0Sdrh case '#': flag_alternateform = 1; break; 2413e9aeec0Sdrh case '!': flag_altform2 = 1; break; 2423e9aeec0Sdrh case '0': flag_zeropad = 1; break; 2432c338a9dSdrh case ',': cThousand = ','; break; 2443e9aeec0Sdrh default: done = 1; break; 245a18c5681Sdrh } 2463e9aeec0Sdrh }while( !done && (c=(*++fmt))!=0 ); 247a18c5681Sdrh /* Get the field width */ 248a18c5681Sdrh if( c=='*' ){ 249a5c1416dSdrh if( bArgList ){ 250a5c1416dSdrh width = (int)getIntArg(pArgList); 251a5c1416dSdrh }else{ 252a18c5681Sdrh width = va_arg(ap,int); 253a5c1416dSdrh } 254a18c5681Sdrh if( width<0 ){ 255a18c5681Sdrh flag_leftjustify = 1; 256b6f47debSdrh width = width >= -2147483647 ? -width : 0; 257a18c5681Sdrh } 258a18c5681Sdrh c = *++fmt; 259a18c5681Sdrh }else{ 260b6f47debSdrh unsigned wx = 0; 26117a68934Sdrh while( c>='0' && c<='9' ){ 262b6f47debSdrh wx = wx*10 + c - '0'; 263a18c5681Sdrh c = *++fmt; 264a18c5681Sdrh } 265b6f47debSdrh testcase( wx>0x7fffffff ); 266b6f47debSdrh width = wx & 0x7fffffff; 267a18c5681Sdrh } 268c386ef4fSdrh assert( width>=0 ); 269c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 270c386ef4fSdrh if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ 271c386ef4fSdrh width = SQLITE_PRINTF_PRECISION_LIMIT; 272c386ef4fSdrh } 273c386ef4fSdrh #endif 2748c069147Sdan 275a18c5681Sdrh /* Get the precision */ 276a18c5681Sdrh if( c=='.' ){ 277a18c5681Sdrh c = *++fmt; 278a18c5681Sdrh if( c=='*' ){ 279a5c1416dSdrh if( bArgList ){ 280a5c1416dSdrh precision = (int)getIntArg(pArgList); 281a5c1416dSdrh }else{ 282a18c5681Sdrh precision = va_arg(ap,int); 283a5c1416dSdrh } 284a18c5681Sdrh c = *++fmt; 285b6f47debSdrh if( precision<0 ){ 286b6f47debSdrh precision = precision >= -2147483647 ? -precision : -1; 287b6f47debSdrh } 288a18c5681Sdrh }else{ 289b6f47debSdrh unsigned px = 0; 29017a68934Sdrh while( c>='0' && c<='9' ){ 291b6f47debSdrh px = px*10 + c - '0'; 292a18c5681Sdrh c = *++fmt; 293a18c5681Sdrh } 294b6f47debSdrh testcase( px>0x7fffffff ); 295b6f47debSdrh precision = px & 0x7fffffff; 296a18c5681Sdrh } 297a18c5681Sdrh }else{ 298a18c5681Sdrh precision = -1; 299a18c5681Sdrh } 300c386ef4fSdrh assert( precision>=(-1) ); 301c386ef4fSdrh #ifdef SQLITE_PRINTF_PRECISION_LIMIT 302c386ef4fSdrh if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ 303c386ef4fSdrh precision = SQLITE_PRINTF_PRECISION_LIMIT; 304c386ef4fSdrh } 305c386ef4fSdrh #endif 306c386ef4fSdrh 307c386ef4fSdrh 308a18c5681Sdrh /* Get the conversion type modifier */ 309a18c5681Sdrh if( c=='l' ){ 310a18c5681Sdrh flag_long = 1; 311a18c5681Sdrh c = *++fmt; 312a34b6764Sdrh if( c=='l' ){ 3132c338a9dSdrh flag_long = 2; 314a34b6764Sdrh c = *++fmt; 315a34b6764Sdrh } 316a34b6764Sdrh }else{ 3172c338a9dSdrh flag_long = 0; 318a18c5681Sdrh } 319a18c5681Sdrh /* Fetch the info entry for the field */ 320874ba04cSdrh infop = &fmtinfo[0]; 321874ba04cSdrh xtype = etINVALID; 32200e13613Sdanielk1977 for(idx=0; idx<ArraySize(fmtinfo); idx++){ 323a18c5681Sdrh if( c==fmtinfo[idx].fmttype ){ 324a18c5681Sdrh infop = &fmtinfo[idx]; 325e84a306bSdrh xtype = infop->type; 326a18c5681Sdrh break; 327a18c5681Sdrh } 328a18c5681Sdrh } 32943617e9aSdrh 330a18c5681Sdrh /* 331a18c5681Sdrh ** At this point, variables are initialized as follows: 332a18c5681Sdrh ** 333a18c5681Sdrh ** flag_alternateform TRUE if a '#' is present. 3343e9aeec0Sdrh ** flag_altform2 TRUE if a '!' is present. 3352c338a9dSdrh ** flag_prefix '+' or ' ' or zero 336a18c5681Sdrh ** flag_leftjustify TRUE if a '-' is present or if the 337a18c5681Sdrh ** field width was negative. 338a18c5681Sdrh ** flag_zeropad TRUE if the width began with 0. 3392c338a9dSdrh ** flag_long 1 for "l", 2 for "ll" 340a18c5681Sdrh ** width The specified field width. This is 341a18c5681Sdrh ** always non-negative. Zero is the default. 342a18c5681Sdrh ** precision The specified precision. The default 343a18c5681Sdrh ** is -1. 344a18c5681Sdrh ** xtype The class of the conversion. 345a18c5681Sdrh ** infop Pointer to the appropriate info struct. 346a18c5681Sdrh */ 347a18c5681Sdrh switch( xtype ){ 348fe63d1c9Sdrh case etPOINTER: 3492c338a9dSdrh flag_long = sizeof(char*)==sizeof(i64) ? 2 : 3502c338a9dSdrh sizeof(char*)==sizeof(long int) ? 1 : 0; 351fe63d1c9Sdrh /* Fall through into the next case */ 3529a99334dSdrh case etORDINAL: 353a18c5681Sdrh case etRADIX: 3542c338a9dSdrh cThousand = 0; 3552c338a9dSdrh /* Fall through into the next case */ 3562c338a9dSdrh case etDECIMAL: 357e84a306bSdrh if( infop->flags & FLAG_SIGNED ){ 358e9707671Sdrh i64 v; 359a5c1416dSdrh if( bArgList ){ 360a5c1416dSdrh v = getIntArg(pArgList); 361eeb23a4cSdrh }else if( flag_long ){ 3622c338a9dSdrh if( flag_long==2 ){ 3632c338a9dSdrh v = va_arg(ap,i64) ; 3642c338a9dSdrh }else{ 365eeb23a4cSdrh v = va_arg(ap,long int); 3662c338a9dSdrh } 367eeb23a4cSdrh }else{ 368eeb23a4cSdrh v = va_arg(ap,int); 369eeb23a4cSdrh } 370e9707671Sdrh if( v<0 ){ 371158b9cb9Sdrh if( v==SMALLEST_INT64 ){ 372158b9cb9Sdrh longvalue = ((u64)1)<<63; 373158b9cb9Sdrh }else{ 374158b9cb9Sdrh longvalue = -v; 375158b9cb9Sdrh } 376cfcdaefeSdanielk1977 prefix = '-'; 377cfcdaefeSdanielk1977 }else{ 378e9707671Sdrh longvalue = v; 3792c338a9dSdrh prefix = flag_prefix; 380cfcdaefeSdanielk1977 } 381e9707671Sdrh }else{ 382a5c1416dSdrh if( bArgList ){ 383a5c1416dSdrh longvalue = (u64)getIntArg(pArgList); 384eeb23a4cSdrh }else if( flag_long ){ 3852c338a9dSdrh if( flag_long==2 ){ 3862c338a9dSdrh longvalue = va_arg(ap,u64); 3872c338a9dSdrh }else{ 388eeb23a4cSdrh longvalue = va_arg(ap,unsigned long int); 3892c338a9dSdrh } 390eeb23a4cSdrh }else{ 391eeb23a4cSdrh longvalue = va_arg(ap,unsigned int); 392eeb23a4cSdrh } 393e9707671Sdrh prefix = 0; 394e9707671Sdrh } 395e9707671Sdrh if( longvalue==0 ) flag_alternateform = 0; 396a18c5681Sdrh if( flag_zeropad && precision<width-(prefix!=0) ){ 397a18c5681Sdrh precision = width-(prefix!=0); 398a18c5681Sdrh } 3992c338a9dSdrh if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ 40059eedf79Sdrh nOut = etBUFSIZE; 40159eedf79Sdrh zOut = buf; 40259eedf79Sdrh }else{ 4035f42995aSdrh u64 n = (u64)precision + 10 + precision/3; 4045f42995aSdrh zOut = zExtra = sqlite3Malloc( n ); 40559eedf79Sdrh if( zOut==0 ){ 406a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 40759eedf79Sdrh return; 40859eedf79Sdrh } 4095f42995aSdrh nOut = (int)n; 41059eedf79Sdrh } 41159eedf79Sdrh bufpt = &zOut[nOut-1]; 4129a99334dSdrh if( xtype==etORDINAL ){ 41343f6e064Sdrh static const char zOrd[] = "thstndrd"; 414ea678832Sdrh int x = (int)(longvalue % 10); 41543f6e064Sdrh if( x>=4 || (longvalue/10)%10==1 ){ 41643f6e064Sdrh x = 0; 41743f6e064Sdrh } 41859eedf79Sdrh *(--bufpt) = zOrd[x*2+1]; 41959eedf79Sdrh *(--bufpt) = zOrd[x*2]; 4209a99334dSdrh } 421a18c5681Sdrh { 4220e682099Sdrh const char *cset = &aDigits[infop->charset]; 4230e682099Sdrh u8 base = infop->base; 424a18c5681Sdrh do{ /* Convert to ascii */ 425a18c5681Sdrh *(--bufpt) = cset[longvalue%base]; 426a18c5681Sdrh longvalue = longvalue/base; 427a18c5681Sdrh }while( longvalue>0 ); 428a18c5681Sdrh } 42959eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 4302c338a9dSdrh while( precision>length ){ 431a18c5681Sdrh *(--bufpt) = '0'; /* Zero pad */ 4322c338a9dSdrh length++; 4332c338a9dSdrh } 4342c338a9dSdrh if( cThousand ){ 4352c338a9dSdrh int nn = (length - 1)/3; /* Number of "," to insert */ 4362c338a9dSdrh int ix = (length - 1)%3 + 1; 4372c338a9dSdrh bufpt -= nn; 4382c338a9dSdrh for(idx=0; nn>0; idx++){ 4392c338a9dSdrh bufpt[idx] = bufpt[idx+nn]; 4402c338a9dSdrh ix--; 4412c338a9dSdrh if( ix==0 ){ 4422c338a9dSdrh bufpt[++idx] = cThousand; 4432c338a9dSdrh nn--; 4442c338a9dSdrh ix = 3; 4452c338a9dSdrh } 4462c338a9dSdrh } 447a18c5681Sdrh } 448a18c5681Sdrh if( prefix ) *(--bufpt) = prefix; /* Add sign */ 449a18c5681Sdrh if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ 45076ff3a0eSdrh const char *pre; 45176ff3a0eSdrh char x; 45276ff3a0eSdrh pre = &aPrefix[infop->prefix]; 45376ff3a0eSdrh for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; 454a18c5681Sdrh } 45559eedf79Sdrh length = (int)(&zOut[nOut-1]-bufpt); 456a18c5681Sdrh break; 457a18c5681Sdrh case etFLOAT: 458a18c5681Sdrh case etEXP: 459a18c5681Sdrh case etGENERIC: 460a5c1416dSdrh if( bArgList ){ 461a5c1416dSdrh realvalue = getDoubleArg(pArgList); 462a5c1416dSdrh }else{ 463a18c5681Sdrh realvalue = va_arg(ap,double); 464a5c1416dSdrh } 46569ef7036Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 46669ef7036Sdrh length = 0; 46769ef7036Sdrh #else 468a18c5681Sdrh if( precision<0 ) precision = 6; /* Set default precision */ 469a18c5681Sdrh if( realvalue<0.0 ){ 470a18c5681Sdrh realvalue = -realvalue; 471a18c5681Sdrh prefix = '-'; 472a18c5681Sdrh }else{ 4732c338a9dSdrh prefix = flag_prefix; 474a18c5681Sdrh } 4753e9aeec0Sdrh if( xtype==etGENERIC && precision>0 ) precision--; 476a30d22a7Sdrh testcase( precision>0xfff ); 47774b42275Sdrh for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} 4783e9aeec0Sdrh if( xtype==etFLOAT ) realvalue += rounder; 479a18c5681Sdrh /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ 480a18c5681Sdrh exp = 0; 481ea678832Sdrh if( sqlite3IsNaN((double)realvalue) ){ 48253c14021Sdrh bufpt = "NaN"; 48353c14021Sdrh length = 3; 48453c14021Sdrh break; 48553c14021Sdrh } 486a18c5681Sdrh if( realvalue>0.0 ){ 48772b3fbc7Sdrh LONGDOUBLE_TYPE scale = 1.0; 4884ef94130Sdrh while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} 4892a8f6712Sdrh while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } 49072b3fbc7Sdrh while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } 49172b3fbc7Sdrh realvalue /= scale; 492af005fbcSdrh while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } 493af005fbcSdrh while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } 494af005fbcSdrh if( exp>350 ){ 4952a8f6712Sdrh bufpt = buf; 4962a8f6712Sdrh buf[0] = prefix; 4972a8f6712Sdrh memcpy(buf+(prefix!=0),"Inf",4); 4982a8f6712Sdrh length = 3+(prefix!=0); 499a18c5681Sdrh break; 500a18c5681Sdrh } 501a18c5681Sdrh } 502a18c5681Sdrh bufpt = buf; 503a18c5681Sdrh /* 504a18c5681Sdrh ** If the field type is etGENERIC, then convert to either etEXP 505a18c5681Sdrh ** or etFLOAT, as appropriate. 506a18c5681Sdrh */ 507a18c5681Sdrh if( xtype!=etFLOAT ){ 508a18c5681Sdrh realvalue += rounder; 509a18c5681Sdrh if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } 510a18c5681Sdrh } 511a18c5681Sdrh if( xtype==etGENERIC ){ 512a18c5681Sdrh flag_rtz = !flag_alternateform; 513a18c5681Sdrh if( exp<-4 || exp>precision ){ 514a18c5681Sdrh xtype = etEXP; 515a18c5681Sdrh }else{ 516a18c5681Sdrh precision = precision - exp; 517a18c5681Sdrh xtype = etFLOAT; 518a18c5681Sdrh } 519a18c5681Sdrh }else{ 52072b3fbc7Sdrh flag_rtz = flag_altform2; 521a18c5681Sdrh } 522557cc60fSdrh if( xtype==etEXP ){ 523557cc60fSdrh e2 = 0; 524557cc60fSdrh }else{ 525557cc60fSdrh e2 = exp; 526557cc60fSdrh } 52774b42275Sdrh if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ 52874b42275Sdrh bufpt = zExtra 52974b42275Sdrh = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); 53059eedf79Sdrh if( bufpt==0 ){ 531a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 53259eedf79Sdrh return; 53359eedf79Sdrh } 53459eedf79Sdrh } 53559eedf79Sdrh zOut = bufpt; 53672b3fbc7Sdrh nsd = 16 + flag_altform2*10; 537ea678832Sdrh flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; 538557cc60fSdrh /* The sign in front of the number */ 539557cc60fSdrh if( prefix ){ 540557cc60fSdrh *(bufpt++) = prefix; 541557cc60fSdrh } 542557cc60fSdrh /* Digits prior to the decimal point */ 543557cc60fSdrh if( e2<0 ){ 544557cc60fSdrh *(bufpt++) = '0'; 545557cc60fSdrh }else{ 546557cc60fSdrh for(; e2>=0; e2--){ 547557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 548557cc60fSdrh } 549557cc60fSdrh } 550557cc60fSdrh /* The decimal point */ 551557cc60fSdrh if( flag_dp ){ 552557cc60fSdrh *(bufpt++) = '.'; 553557cc60fSdrh } 554557cc60fSdrh /* "0" digits after the decimal point but before the first 555557cc60fSdrh ** significant digit of the number */ 556af005fbcSdrh for(e2++; e2<0; precision--, e2++){ 557af005fbcSdrh assert( precision>0 ); 558a18c5681Sdrh *(bufpt++) = '0'; 559a18c5681Sdrh } 560557cc60fSdrh /* Significant digits after the decimal point */ 561557cc60fSdrh while( (precision--)>0 ){ 562557cc60fSdrh *(bufpt++) = et_getdigit(&realvalue,&nsd); 563a18c5681Sdrh } 564557cc60fSdrh /* Remove trailing zeros and the "." if no digits follow the "." */ 565557cc60fSdrh if( flag_rtz && flag_dp ){ 5663e9aeec0Sdrh while( bufpt[-1]=='0' ) *(--bufpt) = 0; 56759eedf79Sdrh assert( bufpt>zOut ); 5683e9aeec0Sdrh if( bufpt[-1]=='.' ){ 569557cc60fSdrh if( flag_altform2 ){ 570557cc60fSdrh *(bufpt++) = '0'; 571557cc60fSdrh }else{ 572557cc60fSdrh *(--bufpt) = 0; 573a18c5681Sdrh } 574557cc60fSdrh } 575557cc60fSdrh } 576557cc60fSdrh /* Add the "eNNN" suffix */ 57759eedf79Sdrh if( xtype==etEXP ){ 57876ff3a0eSdrh *(bufpt++) = aDigits[infop->charset]; 579557cc60fSdrh if( exp<0 ){ 580557cc60fSdrh *(bufpt++) = '-'; exp = -exp; 581557cc60fSdrh }else{ 582557cc60fSdrh *(bufpt++) = '+'; 583557cc60fSdrh } 584a18c5681Sdrh if( exp>=100 ){ 585ea678832Sdrh *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ 586a18c5681Sdrh exp %= 100; 587a18c5681Sdrh } 588ea678832Sdrh *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ 589ea678832Sdrh *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ 590a18c5681Sdrh } 591557cc60fSdrh *bufpt = 0; 592557cc60fSdrh 593a18c5681Sdrh /* The converted number is in buf[] and zero terminated. Output it. 594a18c5681Sdrh ** Note that the number is in the usual order, not reversed as with 595a18c5681Sdrh ** integer conversions. */ 59659eedf79Sdrh length = (int)(bufpt-zOut); 59759eedf79Sdrh bufpt = zOut; 598a18c5681Sdrh 599a18c5681Sdrh /* Special case: Add leading zeros if the flag_zeropad flag is 600a18c5681Sdrh ** set and we are not left justified */ 601a18c5681Sdrh if( flag_zeropad && !flag_leftjustify && length < width){ 602a18c5681Sdrh int i; 603a18c5681Sdrh int nPad = width - length; 604a18c5681Sdrh for(i=width; i>=nPad; i--){ 605a18c5681Sdrh bufpt[i] = bufpt[i-nPad]; 606a18c5681Sdrh } 607a18c5681Sdrh i = prefix!=0; 608a18c5681Sdrh while( nPad-- ) bufpt[i++] = '0'; 609a18c5681Sdrh length = width; 610a18c5681Sdrh } 61169ef7036Sdrh #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ 612a18c5681Sdrh break; 613a18c5681Sdrh case etSIZE: 614fc6ee9dfSdrh if( !bArgList ){ 615fc6ee9dfSdrh *(va_arg(ap,int*)) = pAccum->nChar; 616fc6ee9dfSdrh } 617a18c5681Sdrh length = width = 0; 618a18c5681Sdrh break; 619a18c5681Sdrh case etPERCENT: 620a18c5681Sdrh buf[0] = '%'; 621a18c5681Sdrh bufpt = buf; 622a18c5681Sdrh length = 1; 623a18c5681Sdrh break; 624a18c5681Sdrh case etCHARX: 625a5c1416dSdrh if( bArgList ){ 626fc6ee9dfSdrh bufpt = getTextArg(pArgList); 627*136102beSdrh length = 0; 628*136102beSdrh if( bufpt ){ 629*136102beSdrh buf[0] = c = *(bufpt++); 630*136102beSdrh length = 1; 631*136102beSdrh if( (c&0xc0)==0xc0 ){ 632*136102beSdrh while( length<4 && (bufpt[0]&0xc0)==0x80 ){ 633*136102beSdrh buf[length++] = *(bufpt++); 634*136102beSdrh } 635*136102beSdrh } 636*136102beSdrh } 637a5c1416dSdrh }else{ 638*136102beSdrh unsigned int ch = va_arg(ap,unsigned int); 639*136102beSdrh if( ch<0x00080 ){ 640*136102beSdrh buf[0] = ch & 0xff; 641*136102beSdrh length = 1; 642*136102beSdrh }else if( ch<0x00800 ){ 643*136102beSdrh buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); 644*136102beSdrh buf[1] = 0x80 + (u8)(ch & 0x3f); 645*136102beSdrh length = 2; 646*136102beSdrh }else if( ch<0x10000 ){ 647*136102beSdrh buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); 648*136102beSdrh buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); 649*136102beSdrh buf[2] = 0x80 + (u8)(ch & 0x3f); 650*136102beSdrh length = 3; 651*136102beSdrh }else{ 652*136102beSdrh buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); 653*136102beSdrh buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); 654*136102beSdrh buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); 655*136102beSdrh buf[3] = 0x80 + (u8)(ch & 0x3f); 656*136102beSdrh length = 4; 657*136102beSdrh } 658a5c1416dSdrh } 659af8f513fSdrh if( precision>1 ){ 660af8f513fSdrh width -= precision-1; 661af8f513fSdrh if( width>1 && !flag_leftjustify ){ 662af8f513fSdrh sqlite3AppendChar(pAccum, width-1, ' '); 663af8f513fSdrh width = 0; 664a18c5681Sdrh } 665*136102beSdrh while( precision-- > 1 ){ 666*136102beSdrh sqlite3StrAccumAppend(pAccum, buf, length); 667af8f513fSdrh } 668*136102beSdrh } 669a18c5681Sdrh bufpt = buf; 670a18c5681Sdrh break; 671a18c5681Sdrh case etSTRING: 672d93d8a81Sdrh case etDYNSTRING: 673a5c1416dSdrh if( bArgList ){ 674a5c1416dSdrh bufpt = getTextArg(pArgList); 6752a8f6712Sdrh xtype = etSTRING; 676a5c1416dSdrh }else{ 677cb485882Sdrh bufpt = va_arg(ap,char*); 678a5c1416dSdrh } 679d93d8a81Sdrh if( bufpt==0 ){ 680d93d8a81Sdrh bufpt = ""; 6812a8f6712Sdrh }else if( xtype==etDYNSTRING ){ 682d93d8a81Sdrh zExtra = bufpt; 683d93d8a81Sdrh } 684e509094bSdrh if( precision>=0 ){ 68562856465Sdrh if( flag_altform2 ){ 68662856465Sdrh /* Set length to the number of bytes needed in order to display 68762856465Sdrh ** precision characters */ 68862856465Sdrh unsigned char *z = (unsigned char*)bufpt; 68962856465Sdrh while( precision-- > 0 && z[0] ){ 69062856465Sdrh SQLITE_SKIP_UTF8(z); 69162856465Sdrh } 69262856465Sdrh length = (int)(z - (unsigned char*)bufpt); 69362856465Sdrh }else{ 694e509094bSdrh for(length=0; length<precision && bufpt[length]; length++){} 69562856465Sdrh } 696e509094bSdrh }else{ 697c84ddf14Sdrh length = 0x7fffffff & (int)strlen(bufpt); 698e509094bSdrh } 69957e3ba76Sdrh adjust_width_for_utf8: 70062856465Sdrh if( flag_altform2 && width>0 ){ 70162856465Sdrh /* Adjust width to account for extra bytes in UTF-8 characters */ 70262856465Sdrh int ii = length - 1; 70362856465Sdrh while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; 70462856465Sdrh } 705a18c5681Sdrh break; 70657e3ba76Sdrh case etSQLESCAPE: /* %q: Escape ' characters */ 70757e3ba76Sdrh case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ 70857e3ba76Sdrh case etSQLESCAPE3: { /* %w: Escape " characters */ 7098965b50eSdrh int i, j, k, n, isnull; 7104794f735Sdrh int needQuote; 711ea678832Sdrh char ch; 712f3b863edSdanielk1977 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ 713a5c1416dSdrh char *escarg; 714a5c1416dSdrh 715a5c1416dSdrh if( bArgList ){ 716a5c1416dSdrh escarg = getTextArg(pArgList); 717a5c1416dSdrh }else{ 718a5c1416dSdrh escarg = va_arg(ap,char*); 719a5c1416dSdrh } 720f0113000Sdanielk1977 isnull = escarg==0; 721f0113000Sdanielk1977 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); 72257e3ba76Sdrh /* For %q, %Q, and %w, the precision is the number of byte (or 72357e3ba76Sdrh ** characters if the ! flags is present) to use from the input. 72457e3ba76Sdrh ** Because of the extra quoting characters inserted, the number 72557e3ba76Sdrh ** of output characters may be larger than the precision. 72657e3ba76Sdrh */ 7278965b50eSdrh k = precision; 72860d4a304Sdan for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ 729f3b863edSdanielk1977 if( ch==q ) n++; 73057e3ba76Sdrh if( flag_altform2 && (ch&0xc0)==0xc0 ){ 73157e3ba76Sdrh while( (escarg[i+1]&0xc0)==0x80 ){ i++; } 73257e3ba76Sdrh } 733a18c5681Sdrh } 7344794f735Sdrh needQuote = !isnull && xtype==etSQLESCAPE2; 7352a8f6712Sdrh n += i + 3; 736a18c5681Sdrh if( n>etBUFSIZE ){ 737e5ae5735Sdrh bufpt = zExtra = sqlite3Malloc( n ); 738d164fd34Sdrh if( bufpt==0 ){ 739a6353a3fSdrh setStrAccumError(pAccum, STRACCUM_NOMEM); 740d164fd34Sdrh return; 741d164fd34Sdrh } 742a18c5681Sdrh }else{ 743a18c5681Sdrh bufpt = buf; 744a18c5681Sdrh } 7450cfcf3fbSchw j = 0; 746f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 7478965b50eSdrh k = i; 7488965b50eSdrh for(i=0; i<k; i++){ 7498965b50eSdrh bufpt[j++] = ch = escarg[i]; 750f3b863edSdanielk1977 if( ch==q ) bufpt[j++] = ch; 751a18c5681Sdrh } 752f3b863edSdanielk1977 if( needQuote ) bufpt[j++] = q; 753a18c5681Sdrh bufpt[j] = 0; 754a18c5681Sdrh length = j; 75557e3ba76Sdrh goto adjust_width_for_utf8; 7565eba8c09Sdrh } 7575f968436Sdrh case etTOKEN: { 7588236f688Sdrh Token *pToken; 7598236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 7608236f688Sdrh pToken = va_arg(ap, Token*); 761a5c1416dSdrh assert( bArgList==0 ); 762a9ab481fSdrh if( pToken && pToken->n ){ 763ade86483Sdrh sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); 764ad6d9460Sdrh } 7655f968436Sdrh length = width = 0; 7665f968436Sdrh break; 7675f968436Sdrh } 7685f968436Sdrh case etSRCLIST: { 7698236f688Sdrh SrcList *pSrc; 7708236f688Sdrh int k; 7718236f688Sdrh struct SrcList_item *pItem; 7728236f688Sdrh if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; 7738236f688Sdrh pSrc = va_arg(ap, SrcList*); 7748236f688Sdrh k = va_arg(ap, int); 7758236f688Sdrh pItem = &pSrc->a[k]; 776a5c1416dSdrh assert( bArgList==0 ); 7775f968436Sdrh assert( k>=0 && k<pSrc->nSrc ); 77893a960a0Sdrh if( pItem->zDatabase ){ 779a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); 780ade86483Sdrh sqlite3StrAccumAppend(pAccum, ".", 1); 7815f968436Sdrh } 782a6353a3fSdrh sqlite3StrAccumAppendAll(pAccum, pItem->zName); 7835f968436Sdrh length = width = 0; 7845f968436Sdrh break; 7855f968436Sdrh } 786874ba04cSdrh default: { 787874ba04cSdrh assert( xtype==etINVALID ); 788874ba04cSdrh return; 789874ba04cSdrh } 790a18c5681Sdrh }/* End switch over the format type */ 791a18c5681Sdrh /* 792a18c5681Sdrh ** The text of the conversion is pointed to by "bufpt" and is 793a18c5681Sdrh ** "length" characters long. The field width is "width". Do 79462856465Sdrh ** the output. Both length and width are in bytes, not characters, 79562856465Sdrh ** at this point. If the "!" flag was present on string conversions 79662856465Sdrh ** indicating that width and precision should be expressed in characters, 79762856465Sdrh ** then the values have been translated prior to reaching this point. 798a18c5681Sdrh */ 799a70a073bSdrh width -= length; 8008236f688Sdrh if( width>0 ){ 8018236f688Sdrh if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 802ade86483Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 8038236f688Sdrh if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); 8048236f688Sdrh }else{ 8058236f688Sdrh sqlite3StrAccumAppend(pAccum, bufpt, length); 8068236f688Sdrh } 807a70a073bSdrh 808af8f513fSdrh if( zExtra ){ 80996ceaf86Sdrh sqlite3DbFree(pAccum->db, zExtra); 810af8f513fSdrh zExtra = 0; 811af8f513fSdrh } 812a18c5681Sdrh }/* End for loop over the format string */ 813a18c5681Sdrh } /* End of function */ 814a18c5681Sdrh 815a18c5681Sdrh /* 816a70a073bSdrh ** Enlarge the memory allocation on a StrAccum object so that it is 817a70a073bSdrh ** able to accept at least N more bytes of text. 818a70a073bSdrh ** 819a70a073bSdrh ** Return the number of bytes of text that StrAccum is able to accept 820a70a073bSdrh ** after the attempted enlargement. The value returned might be zero. 821a18c5681Sdrh */ 822a70a073bSdrh static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ 823a6353a3fSdrh char *zNew; 824a30d22a7Sdrh assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ 825b49bc86aSdrh if( p->accError ){ 826b49bc86aSdrh testcase(p->accError==STRACCUM_TOOBIG); 827b49bc86aSdrh testcase(p->accError==STRACCUM_NOMEM); 828a70a073bSdrh return 0; 829ade86483Sdrh } 830c0490572Sdrh if( p->mxAlloc==0 ){ 831ade86483Sdrh N = p->nAlloc - p->nChar - 1; 832a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 833a70a073bSdrh return N; 834a18c5681Sdrh }else{ 8355f4a686fSdrh char *zOld = isMalloced(p) ? p->zText : 0; 83693a960a0Sdrh i64 szNew = p->nChar; 837b1a6c3c1Sdrh szNew += N + 1; 8387b4d780bSdrh if( szNew+p->nChar<=p->mxAlloc ){ 8397b4d780bSdrh /* Force exponential buffer size growth as long as it does not overflow, 8407b4d780bSdrh ** to avoid having to call this routine too often */ 8417b4d780bSdrh szNew += p->nChar; 8427b4d780bSdrh } 843b1a6c3c1Sdrh if( szNew > p->mxAlloc ){ 844ade86483Sdrh sqlite3StrAccumReset(p); 845a6353a3fSdrh setStrAccumError(p, STRACCUM_TOOBIG); 846a70a073bSdrh return 0; 847b1a6c3c1Sdrh }else{ 848ea678832Sdrh p->nAlloc = (int)szNew; 849a18c5681Sdrh } 850c0490572Sdrh if( p->db ){ 851a9ef7097Sdan zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); 852b975598eSdrh }else{ 853f3cdcdccSdrh zNew = sqlite3_realloc64(zOld, p->nAlloc); 854b975598eSdrh } 85553f733c7Sdrh if( zNew ){ 8567ef4d1c4Sdrh assert( p->zText!=0 || p->nChar==0 ); 8575f4a686fSdrh if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); 858ade86483Sdrh p->zText = zNew; 8597f5a7ecdSdrh p->nAlloc = sqlite3DbMallocSize(p->db, zNew); 8605f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 861ade86483Sdrh }else{ 862ade86483Sdrh sqlite3StrAccumReset(p); 863a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 864a70a073bSdrh return 0; 865a70a073bSdrh } 866a70a073bSdrh } 867a70a073bSdrh return N; 868a70a073bSdrh } 869a70a073bSdrh 870a70a073bSdrh /* 871af8f513fSdrh ** Append N copies of character c to the given string buffer. 872a70a073bSdrh */ 873af8f513fSdrh void sqlite3AppendChar(StrAccum *p, int N, char c){ 874a30d22a7Sdrh testcase( p->nChar + (i64)N > 0x7fffffff ); 875a30d22a7Sdrh if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ 876a30d22a7Sdrh return; 877a30d22a7Sdrh } 878af8f513fSdrh while( (N--)>0 ) p->zText[p->nChar++] = c; 879a70a073bSdrh } 880a70a073bSdrh 881a70a073bSdrh /* 882a70a073bSdrh ** The StrAccum "p" is not large enough to accept N new bytes of z[]. 883a70a073bSdrh ** So enlarge if first, then do the append. 884a70a073bSdrh ** 885a70a073bSdrh ** This is a helper routine to sqlite3StrAccumAppend() that does special-case 886a70a073bSdrh ** work (enlarging the buffer) using tail recursion, so that the 887a70a073bSdrh ** sqlite3StrAccumAppend() routine can use fast calling semantics. 888a70a073bSdrh */ 889172087fbSdrh static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ 890a70a073bSdrh N = sqlite3StrAccumEnlarge(p, N); 891a70a073bSdrh if( N>0 ){ 892a70a073bSdrh memcpy(&p->zText[p->nChar], z, N); 893a70a073bSdrh p->nChar += N; 894a70a073bSdrh } 895a70a073bSdrh } 896a70a073bSdrh 897a70a073bSdrh /* 898a70a073bSdrh ** Append N bytes of text from z to the StrAccum object. Increase the 899a70a073bSdrh ** size of the memory allocation for StrAccum if necessary. 900a70a073bSdrh */ 901a70a073bSdrh void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ 9023457338cSdrh assert( z!=0 || N==0 ); 903a70a073bSdrh assert( p->zText!=0 || p->nChar==0 || p->accError ); 904a70a073bSdrh assert( N>=0 ); 905a70a073bSdrh assert( p->accError==0 || p->nAlloc==0 ); 906a70a073bSdrh if( p->nChar+N >= p->nAlloc ){ 907a70a073bSdrh enlargeAndAppend(p,z,N); 908895decf6Sdan }else if( N ){ 909b07028f7Sdrh assert( p->zText ); 910ade86483Sdrh p->nChar += N; 911172087fbSdrh memcpy(&p->zText[p->nChar-N], z, N); 912172087fbSdrh } 913483750baSdrh } 914483750baSdrh 915483750baSdrh /* 916a6353a3fSdrh ** Append the complete text of zero-terminated string z[] to the p string. 917a6353a3fSdrh */ 918a6353a3fSdrh void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ 91953cd9646Smistachkin sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); 920a6353a3fSdrh } 921a6353a3fSdrh 922a6353a3fSdrh 923a6353a3fSdrh /* 924ade86483Sdrh ** Finish off a string by making sure it is zero-terminated. 925ade86483Sdrh ** Return a pointer to the resulting string. Return a NULL 926ade86483Sdrh ** pointer if any kind of error was encountered. 9275f968436Sdrh */ 928043e586eSdrh static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ 9293f18e6d7Sdrh char *zText; 930043e586eSdrh assert( p->mxAlloc>0 && !isMalloced(p) ); 9313f18e6d7Sdrh zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); 9323f18e6d7Sdrh if( zText ){ 9333f18e6d7Sdrh memcpy(zText, p->zText, p->nChar+1); 9345f4a686fSdrh p->printfFlags |= SQLITE_PRINTF_MALLOCED; 935ade86483Sdrh }else{ 936a6353a3fSdrh setStrAccumError(p, STRACCUM_NOMEM); 937ade86483Sdrh } 9383f18e6d7Sdrh p->zText = zText; 9393f18e6d7Sdrh return zText; 940043e586eSdrh } 941043e586eSdrh char *sqlite3StrAccumFinish(StrAccum *p){ 942043e586eSdrh if( p->zText ){ 943043e586eSdrh p->zText[p->nChar] = 0; 944043e586eSdrh if( p->mxAlloc>0 && !isMalloced(p) ){ 945043e586eSdrh return strAccumFinishRealloc(p); 946ade86483Sdrh } 947ade86483Sdrh } 948ade86483Sdrh return p->zText; 949ade86483Sdrh } 950ade86483Sdrh 951ade86483Sdrh /* 952ade86483Sdrh ** Reset an StrAccum string. Reclaim all malloced memory. 953ade86483Sdrh */ 954ade86483Sdrh void sqlite3StrAccumReset(StrAccum *p){ 9555f4a686fSdrh if( isMalloced(p) ){ 956633e6d57Sdrh sqlite3DbFree(p->db, p->zText); 9575f4a686fSdrh p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; 958ade86483Sdrh } 959f089aa45Sdrh p->zText = 0; 960ade86483Sdrh } 961ade86483Sdrh 962ade86483Sdrh /* 963c0490572Sdrh ** Initialize a string accumulator. 964c0490572Sdrh ** 965c0490572Sdrh ** p: The accumulator to be initialized. 966c0490572Sdrh ** db: Pointer to a database connection. May be NULL. Lookaside 967c0490572Sdrh ** memory is used if not NULL. db->mallocFailed is set appropriately 968c0490572Sdrh ** when not NULL. 969c0490572Sdrh ** zBase: An initial buffer. May be NULL in which case the initial buffer 970c0490572Sdrh ** is malloced. 971c0490572Sdrh ** n: Size of zBase in bytes. If total space requirements never exceed 972c0490572Sdrh ** n then no memory allocations ever occur. 973c0490572Sdrh ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory 974c0490572Sdrh ** allocations will ever occur. 975ade86483Sdrh */ 976c0490572Sdrh void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ 9773f18e6d7Sdrh p->zText = zBase; 978c0490572Sdrh p->db = db; 979ade86483Sdrh p->nAlloc = n; 980bb4957f8Sdrh p->mxAlloc = mx; 9813f18e6d7Sdrh p->nChar = 0; 982b49bc86aSdrh p->accError = 0; 9835f4a686fSdrh p->printfFlags = 0; 9845f968436Sdrh } 9855f968436Sdrh 9865f968436Sdrh /* 9875f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 9885f968436Sdrh ** %-conversion extensions. 9895f968436Sdrh */ 99017435752Sdrh char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ 99117435752Sdrh char *z; 99279158e18Sdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 993ade86483Sdrh StrAccum acc; 994bc6160b0Sdrh assert( db!=0 ); 995c0490572Sdrh sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), 996bc6160b0Sdrh db->aLimit[SQLITE_LIMIT_LENGTH]); 9975f4a686fSdrh acc.printfFlags = SQLITE_PRINTF_INTERNAL; 9985f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 999ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 1000b49bc86aSdrh if( acc.accError==STRACCUM_NOMEM ){ 10014a642b60Sdrh sqlite3OomFault(db); 100217435752Sdrh } 100317435752Sdrh return z; 10045f968436Sdrh } 10055f968436Sdrh 10065f968436Sdrh /* 10075f968436Sdrh ** Print into memory obtained from sqliteMalloc(). Use the internal 10085f968436Sdrh ** %-conversion extensions. 10095f968436Sdrh */ 101017435752Sdrh char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ 10115f968436Sdrh va_list ap; 10125f968436Sdrh char *z; 10135f968436Sdrh va_start(ap, zFormat); 1014ade86483Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 10155f968436Sdrh va_end(ap); 10165f968436Sdrh return z; 10175f968436Sdrh } 10185f968436Sdrh 10195f968436Sdrh /* 102028dd479cSdrh ** Print into memory obtained from sqlite3_malloc(). Omit the internal 102128dd479cSdrh ** %-conversion extensions. 102228dd479cSdrh */ 102328dd479cSdrh char *sqlite3_vmprintf(const char *zFormat, va_list ap){ 1024ade86483Sdrh char *z; 102528dd479cSdrh char zBase[SQLITE_PRINT_BUF_SIZE]; 1026ade86483Sdrh StrAccum acc; 10279ca95730Sdrh 10289ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 10299ca95730Sdrh if( zFormat==0 ){ 10309ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 10319ca95730Sdrh return 0; 10329ca95730Sdrh } 10339ca95730Sdrh #endif 1034ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 1035ff1590eeSdrh if( sqlite3_initialize() ) return 0; 1036ff1590eeSdrh #endif 1037c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); 10385f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 1039ade86483Sdrh z = sqlite3StrAccumFinish(&acc); 1040ade86483Sdrh return z; 104128dd479cSdrh } 104228dd479cSdrh 104328dd479cSdrh /* 104428dd479cSdrh ** Print into memory obtained from sqlite3_malloc()(). Omit the internal 104528dd479cSdrh ** %-conversion extensions. 1046483750baSdrh */ 10476f8a503dSdanielk1977 char *sqlite3_mprintf(const char *zFormat, ...){ 1048a18c5681Sdrh va_list ap; 10495f968436Sdrh char *z; 1050ff1590eeSdrh #ifndef SQLITE_OMIT_AUTOINIT 1051ff1590eeSdrh if( sqlite3_initialize() ) return 0; 1052ff1590eeSdrh #endif 1053a18c5681Sdrh va_start(ap, zFormat); 1054b3738b6cSdrh z = sqlite3_vmprintf(zFormat, ap); 1055a18c5681Sdrh va_end(ap); 10565f968436Sdrh return z; 1057a18c5681Sdrh } 1058a18c5681Sdrh 1059a18c5681Sdrh /* 10606f8a503dSdanielk1977 ** sqlite3_snprintf() works like snprintf() except that it ignores the 106193a5c6bdSdrh ** current locale settings. This is important for SQLite because we 106293a5c6bdSdrh ** are not able to use a "," as the decimal point in place of "." as 106393a5c6bdSdrh ** specified by some locales. 1064db26d4c9Sdrh ** 1065db26d4c9Sdrh ** Oops: The first two arguments of sqlite3_snprintf() are backwards 1066db26d4c9Sdrh ** from the snprintf() standard. Unfortunately, it is too late to change 1067db26d4c9Sdrh ** this without breaking compatibility, so we just have to live with the 1068db26d4c9Sdrh ** mistake. 1069db26d4c9Sdrh ** 1070db26d4c9Sdrh ** sqlite3_vsnprintf() is the varargs version. 107193a5c6bdSdrh */ 1072db26d4c9Sdrh char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ 1073db26d4c9Sdrh StrAccum acc; 1074db26d4c9Sdrh if( n<=0 ) return zBuf; 10759ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 10769ca95730Sdrh if( zBuf==0 || zFormat==0 ) { 10779ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 107896c707a3Sdrh if( zBuf ) zBuf[0] = 0; 10799ca95730Sdrh return zBuf; 10809ca95730Sdrh } 10819ca95730Sdrh #endif 1082c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); 10835f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 1084e9bb5660Sdrh zBuf[acc.nChar] = 0; 1085e9bb5660Sdrh return zBuf; 1086db26d4c9Sdrh } 10876f8a503dSdanielk1977 char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ 10885f968436Sdrh char *z; 108993a5c6bdSdrh va_list ap; 109093a5c6bdSdrh va_start(ap,zFormat); 1091db26d4c9Sdrh z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); 109293a5c6bdSdrh va_end(ap); 10935f968436Sdrh return z; 109493a5c6bdSdrh } 109593a5c6bdSdrh 10963f280701Sdrh /* 10977c0c460fSdrh ** This is the routine that actually formats the sqlite3_log() message. 10987c0c460fSdrh ** We house it in a separate routine from sqlite3_log() to avoid using 10997c0c460fSdrh ** stack space on small-stack systems when logging is disabled. 11007c0c460fSdrh ** 11017c0c460fSdrh ** sqlite3_log() must render into a static buffer. It cannot dynamically 11027c0c460fSdrh ** allocate memory because it might be called while the memory allocator 11037c0c460fSdrh ** mutex is held. 1104a8dbd52aSdrh ** 1105a8dbd52aSdrh ** sqlite3VXPrintf() might ask for *temporary* memory allocations for 1106a8dbd52aSdrh ** certain format characters (%q) or for very large precisions or widths. 1107a8dbd52aSdrh ** Care must be taken that any sqlite3_log() calls that occur while the 1108a8dbd52aSdrh ** memory mutex is held do not use these mechanisms. 11097c0c460fSdrh */ 11107c0c460fSdrh static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ 11117c0c460fSdrh StrAccum acc; /* String accumulator */ 1112a64fa912Sdrh char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ 11137c0c460fSdrh 1114c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); 11155f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 11167c0c460fSdrh sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, 11177c0c460fSdrh sqlite3StrAccumFinish(&acc)); 11187c0c460fSdrh } 11197c0c460fSdrh 11207c0c460fSdrh /* 11213f280701Sdrh ** Format and write a message to the log if logging is enabled. 11223f280701Sdrh */ 1123a7564663Sdrh void sqlite3_log(int iErrCode, const char *zFormat, ...){ 11243f280701Sdrh va_list ap; /* Vararg list */ 11257c0c460fSdrh if( sqlite3GlobalConfig.xLog ){ 11263f280701Sdrh va_start(ap, zFormat); 11277c0c460fSdrh renderLogMsg(iErrCode, zFormat, ap); 11283f280701Sdrh va_end(ap); 11293f280701Sdrh } 11303f280701Sdrh } 11313f280701Sdrh 113202b0e267Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) 1133e54ca3feSdrh /* 1134e54ca3feSdrh ** A version of printf() that understands %lld. Used for debugging. 1135e54ca3feSdrh ** The printf() built into some versions of windows does not understand %lld 1136e54ca3feSdrh ** and segfaults if you give it a long long int. 1137e54ca3feSdrh */ 1138e54ca3feSdrh void sqlite3DebugPrintf(const char *zFormat, ...){ 1139e54ca3feSdrh va_list ap; 1140ade86483Sdrh StrAccum acc; 1141e54ca3feSdrh char zBuf[500]; 1142c0490572Sdrh sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 1143e54ca3feSdrh va_start(ap,zFormat); 11445f4a686fSdrh sqlite3VXPrintf(&acc, zFormat, ap); 1145e54ca3feSdrh va_end(ap); 1146bed8e7e5Sdrh sqlite3StrAccumFinish(&acc); 11474a9ff918Smistachkin #ifdef SQLITE_OS_TRACE_PROC 11484a9ff918Smistachkin { 11494a9ff918Smistachkin extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); 11504a9ff918Smistachkin SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); 11514a9ff918Smistachkin } 11524a9ff918Smistachkin #else 1153485f0039Sdrh fprintf(stdout,"%s", zBuf); 11542ac3ee97Sdrh fflush(stdout); 11554a9ff918Smistachkin #endif 1156e54ca3feSdrh } 1157e54ca3feSdrh #endif 1158c7bc4fdeSdrh 11594fa4a54fSdrh 1160c7bc4fdeSdrh /* 1161d37bea5bSdrh ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument 1162d37bea5bSdrh ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. 1163c7bc4fdeSdrh */ 11645f4a686fSdrh void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ 1165c7bc4fdeSdrh va_list ap; 1166c7bc4fdeSdrh va_start(ap,zFormat); 11675f4a686fSdrh sqlite3VXPrintf(p, zFormat, ap); 1168c7bc4fdeSdrh va_end(ap); 1169c7bc4fdeSdrh } 1170