sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** The "printf" code that follows dates from the 1980's. It is in |
| 3 | ** the public domain. |
| 4 | ** |
| 5 | ************************************************************************** |
| 6 | ** |
| 7 | ** This file contains code for a set of "printf"-like routines. These |
| 8 | ** routines format strings much like the printf() from the standard C |
| 9 | ** library, though the implementation here has enhancements to support |
| 10 | ** SQLite. |
| 11 | */ |
| 12 | #include "sqliteInt.h" |
| 13 | |
| 14 | /* |
| 15 | ** Conversion types fall into various categories as defined by the |
| 16 | ** following enumeration. |
| 17 | */ |
| 18 | #define etRADIX 0 /* non-decimal integer types. %x %o */ |
| 19 | #define etFLOAT 1 /* Floating point. %f */ |
| 20 | #define etEXP 2 /* Exponentional notation. %e and %E */ |
| 21 | #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ |
| 22 | #define etSIZE 4 /* Return number of characters processed so far. %n */ |
| 23 | #define etSTRING 5 /* Strings. %s */ |
| 24 | #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ |
| 25 | #define etPERCENT 7 /* Percent symbol. %% */ |
| 26 | #define etCHARX 8 /* Characters. %c */ |
| 27 | /* The rest are extensions, not normally found in printf() */ |
| 28 | #define etESCAPE_q 9 /* Strings with '\'' doubled. %q */ |
| 29 | #define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '', |
| 30 | NULL pointers replaced by SQL NULL. %Q */ |
| 31 | #define etTOKEN 11 /* a pointer to a Token structure */ |
| 32 | #define etSRCITEM 12 /* a pointer to a SrcItem */ |
| 33 | #define etPOINTER 13 /* The %p conversion */ |
| 34 | #define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */ |
| 35 | #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 36 | #define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
| 37 | #define etESCAPE_j 17 /* %j -> JSON string literal w/o "..." */ |
| 38 | #define etESCAPE_J 18 /* %J -> JSON string literal with "..." */ |
| 39 | |
| 40 | #define etINVALID 19 /* Any unrecognized conversion type */ |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | ** An "etByte" is an 8-bit unsigned value. |
| 45 | */ |
| 46 | typedef unsigned char etByte; |
| 47 | |
| 48 | /* |
| 49 | ** Each builtin conversion character (ex: the 'd' in "%d") is described |
| 50 | ** by an instance of the following structure |
| 51 | */ |
| 52 | typedef struct et_info { /* Information about each format field */ |
| 53 | char fmttype; /* The format field code letter */ |
| 54 | etByte base; /* The base for radix conversion */ |
| 55 | etByte flags; /* One or more of FLAG_ constants below */ |
| 56 | etByte type; /* Conversion paradigm */ |
| 57 | etByte charset; /* Offset into aDigits[] of the digits string */ |
| 58 | etByte prefix; /* Offset into aPrefix[] of the prefix string */ |
| 59 | char iNxt; /* Next with same hash, or 0 for end of chain */ |
| 60 | } et_info; |
| 61 | |
| 62 | /* |
| 63 | ** Allowed values for et_info.flags |
| 64 | */ |
| 65 | #define FLAG_SIGNED 1 /* True if the value to convert is signed */ |
| 66 | #define FLAG_STRING 4 /* Allow infinite precision */ |
| 67 | |
| 68 | /* |
| 69 | ** The table is searched by hash. In the case of %C where C is the character |
| 70 | ** and that character has ASCII value j, then the hash is j%25. |
| 71 | ** |
| 72 | ** The order of the entries in fmtinfo[] and the hash chain was entered |
| 73 | ** manually, but based on the output of the following TCL script: |
| 74 | */ |
| 75 | #if 0 /***** Beginning of script ******/ |
| 76 | foreach c {d s g z q Q w c o u x X f e E G i n % p T S r J j} { |
| 77 | scan $c %c x |
| 78 | set n($c) $x |
| 79 | } |
| 80 | set mx [llength [array names n]] |
| 81 | puts "count: $mx" |
| 82 | |
| 83 | set mx 25 |
| 84 | puts "*********** mx=$mx ************" |
| 85 | for {set r 0} {$r<$mx} {incr r} { |
| 86 | puts -nonewline [format %2d: $r] |
| 87 | foreach c [array names n] { |
| 88 | if {($n($c))%$mx==$r} {puts -nonewline " $c"} |
| 89 | } |
| 90 | puts "" |
| 91 | } |
| 92 | #endif /***** End of script ********/ |
| 93 | |
| 94 | static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; |
| 95 | static const char aHex[] = "0123456789abcdef"; |
| 96 | static const char aPrefix[] = "-x0\000X0"; |
| 97 | static const et_info fmtinfo[25] = { |
| 98 | /* 0 */ { 'd', 10, 1, etDECIMAL, 0, 0, 0 }, |
| 99 | /* 1 */ { 'e', 0, 1, etEXP, 30, 0, 0 }, |
| 100 | /* 2 */ { 'f', 0, 1, etFLOAT, 0, 0, 0 }, |
| 101 | /* 3 */ { 'g', 0, 1, etGENERIC, 30, 0, 0 }, |
| 102 | /* 4 */ { 'j', 0, 0, etESCAPE_j, 0, 0, 0 }, /* Hash: 6 */ |
| 103 | /* 5 */ { 'i', 10, 1, etDECIMAL, 0, 0, 0 }, |
| 104 | /* 6 */ { 'Q', 0, 4, etESCAPE_Q, 0, 0, 4 }, |
| 105 | /* 7 */ { 'p', 16, 0, etPOINTER, 0, 1, 0 }, /* Hash: 12 */ |
| 106 | /* 8 */ { 'S', 0, 0, etSRCITEM, 0, 0, 0 }, |
| 107 | /* 9 */ { 'T', 0, 0, etTOKEN, 0, 0, 0 }, |
| 108 | /* 10 */ { 'n', 0, 0, etSIZE, 0, 0, 0 }, |
| 109 | /* 11 */ { 'o', 8, 0, etRADIX, 0, 2, 0 }, |
| 110 | /* 12 */ { '%', 0, 0, etPERCENT, 0, 0, 7 }, |
| 111 | /* 13 */ { 'q', 0, 4, etESCAPE_q, 0, 0, 16 }, |
| 112 | /* 14 */ { 'r', 10, 1, etORDINAL, 0, 0, 0 }, |
| 113 | /* 15 */ { 's', 0, 4, etSTRING, 0, 0, 0 }, |
| 114 | /* 16 */ { 'X', 16, 0, etRADIX, 0, 4, 0 }, /* Hash: 13 */ |
| 115 | /* 17 */ { 'u', 10, 0, etDECIMAL, 0, 0, 0 }, |
| 116 | /* 18 */ { 'w', 0, 4, etESCAPE_w, 0, 0, 0 }, /* Hash: 19 */ |
| 117 | /* 19 */ { 'E', 0, 1, etEXP, 14, 0, 18 }, |
| 118 | /* 20 */ { 'x', 16, 0, etRADIX, 16, 1, 0 }, |
| 119 | /* 21 */ { 'G', 0, 1, etGENERIC, 14, 0, 0 }, |
| 120 | /* 22 */ { 'z', 0, 4, etDYNSTRING, 0, 0, 0 }, |
| 121 | /* 23 */ { 'J', 0, 0, etESCAPE_J, 0, 0, 0 }, /* Hash: 24 */ |
| 122 | /* 24 */ { 'c', 0, 0, etCHARX, 0, 0, 23 } |
| 123 | }; |
| 124 | |
| 125 | /* Additional Notes: |
| 126 | ** |
| 127 | ** %S Takes a pointer to SrcItem. Shows name or database.name |
| 128 | ** %!S Like %S but prefer the zName over the zAlias |
| 129 | */ |
| 130 | |
| 131 | /* |
| 132 | ** Set the StrAccum object to an error mode. |
| 133 | */ |
| 134 | void sqlite3StrAccumSetError(StrAccum *p, u8 eError){ |
| 135 | assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); |
| 136 | p->accError = eError; |
| 137 | if( p->mxAlloc ) sqlite3_str_reset(p); |
| 138 | if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | ** Extra argument values from a PrintfArguments object |
| 143 | */ |
| 144 | static sqlite3_int64 getIntArg(PrintfArguments *p){ |
| 145 | if( p->nArg<=p->nUsed ) return 0; |
| 146 | return sqlite3_value_int64(p->apArg[p->nUsed++]); |
| 147 | } |
| 148 | static double getDoubleArg(PrintfArguments *p){ |
| 149 | if( p->nArg<=p->nUsed ) return 0.0; |
| 150 | return sqlite3_value_double(p->apArg[p->nUsed++]); |
| 151 | } |
| 152 | static char *getTextArg(PrintfArguments *p){ |
| 153 | if( p->nArg<=p->nUsed ) return 0; |
| 154 | return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | ** Allocate memory for a temporary buffer needed for printf rendering. |
| 159 | ** |
| 160 | ** If the requested size of the temp buffer is larger than the size |
| 161 | ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. |
| 162 | ** Do the size check before the memory allocation to prevent rogue |
| 163 | ** SQL from requesting large allocations using the precision or width |
| 164 | ** field of the printf() function. |
| 165 | */ |
| 166 | static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ |
| 167 | char *z; |
| 168 | if( pAccum->accError ) return 0; |
| 169 | if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ |
| 170 | sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG); |
| 171 | return 0; |
| 172 | } |
| 173 | z = sqlite3_malloc(n); |
| 174 | if( z==0 ){ |
| 175 | sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM); |
| 176 | } |
| 177 | return z; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | ** On machines with a small stack size, you can redefine the |
| 182 | ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
| 183 | */ |
| 184 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 185 | # define SQLITE_PRINT_BUF_SIZE 70 |
| 186 | #endif |
| 187 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 188 | |
| 189 | /* |
| 190 | ** Hard limit on the precision of floating-point conversions to |
| 191 | ** the minimum of SQLITE_PRINTF_PRECISION_LIMIT and 100 million. |
| 192 | */ |
| 193 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 194 | # if SQLITE_PRINTF_PRECISION_LIMIT<100000000 |
| 195 | # define SQLITE_FP_PRECISION_LIMIT SQLITE_PRINTF_PRECISION_LIMIT |
| 196 | # else |
| 197 | # define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 198 | # endif |
| 199 | #else |
| 200 | # define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 201 | #endif |
| 202 | |
| 203 | /* Forward reference */ |
| 204 | static void sqlite3StrAppend64(sqlite3_str *p, const char *z, i64 N); |
| 205 | static void sqlite3StrAppendchar64(sqlite3_str *p, i64 N, char c); |
| 206 | |
| 207 | /* |
| 208 | ** Render a string given by "fmt" into the StrAccum object. |
| 209 | */ |
| 210 | void sqlite3_str_vappendf( |
| 211 | sqlite3_str *pAccum, /* Accumulate results here */ |
| 212 | const char *fmt, /* Format string */ |
| 213 | va_list ap /* arguments */ |
| 214 | ){ |
| 215 | int c; /* Next character in the format string */ |
| 216 | char *bufpt; /* Pointer to the conversion buffer */ |
| 217 | i64 precision; /* Precision of the current field */ |
| 218 | i64 length; /* Length of the field */ |
| 219 | int idx; /* A general purpose loop counter */ |
| 220 | i64 width; /* Width of the current field */ |
| 221 | etByte flag_leftjustify; /* True if "-" flag is present */ |
| 222 | etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ |
| 223 | etByte flag_alternateform; /* True if "#" flag is present */ |
| 224 | etByte flag_altform2; /* True if "!" flag is present */ |
| 225 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
| 226 | etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ |
| 227 | etByte done; /* Loop termination flag */ |
| 228 | etByte cThousand; /* Thousands separator for %d and %u */ |
| 229 | etByte xtype = etINVALID; /* Conversion paradigm */ |
| 230 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 231 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 232 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 233 | double realvalue; /* Value for real types */ |
| 234 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 235 | char *zOut; /* Rendering buffer */ |
| 236 | int nOut; /* Size of the rendering buffer */ |
| 237 | char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 238 | int exp, e2; /* exponent of real numbers */ |
| 239 | etByte flag_dp; /* True if decimal point should be shown */ |
| 240 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 241 | |
| 242 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 243 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 244 | |
| 245 | /* pAccum never starts out with an empty buffer that was obtained from |
| 246 | ** malloc(). This precondition is required by the mprintf("%z...") |
| 247 | ** optimization. */ |
| 248 | assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 249 | |
| 250 | bufpt = 0; |
| 251 | if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ |
| 252 | pArgList = va_arg(ap, PrintfArguments*); |
| 253 | bArgList = 1; |
| 254 | }else{ |
| 255 | bArgList = 0; |
| 256 | } |
| 257 | for(; (c=(*fmt))!=0; ++fmt){ |
| 258 | if( c!='%' ){ |
| 259 | bufpt = (char *)fmt; |
| 260 | #if HAVE_STRCHRNUL |
| 261 | fmt = strchrnul(fmt, '%'); |
| 262 | #else |
| 263 | fmt = strchr(fmt, '%'); |
| 264 | if( fmt==0 ){ |
| 265 | fmt = bufpt + strlen(bufpt); |
| 266 | } |
| 267 | #endif |
| 268 | sqlite3StrAppend64(pAccum, bufpt, fmt - bufpt); |
| 269 | if( *fmt==0 ) break; |
| 270 | } |
| 271 | if( (c=(*++fmt))==0 ){ |
| 272 | sqlite3_str_append(pAccum, "%", 1); |
| 273 | break; |
| 274 | } |
| 275 | /* Find out what flags are present */ |
| 276 | flag_leftjustify = flag_prefix = cThousand = |
| 277 | flag_alternateform = flag_altform2 = flag_zeropad = 0; |
| 278 | done = 0; |
| 279 | width = 0; |
| 280 | flag_long = 0; |
| 281 | precision = -1; |
| 282 | do{ |
| 283 | switch( c ){ |
| 284 | case '-': flag_leftjustify = 1; break; |
| 285 | case '+': flag_prefix = '+'; break; |
| 286 | case ' ': flag_prefix = ' '; break; |
| 287 | case '#': flag_alternateform = 1; break; |
| 288 | case '!': flag_altform2 = 1; break; |
| 289 | case '0': flag_zeropad = 1; break; |
| 290 | case ',': cThousand = ','; break; |
| 291 | default: done = 1; break; |
| 292 | case 'l': { |
| 293 | flag_long = 1; |
| 294 | c = *++fmt; |
| 295 | if( c=='l' ){ |
| 296 | c = *++fmt; |
| 297 | flag_long = 2; |
| 298 | } |
| 299 | done = 1; |
| 300 | break; |
| 301 | } |
| 302 | case '1': case '2': case '3': case '4': case '5': |
| 303 | case '6': case '7': case '8': case '9': { |
| 304 | unsigned wx = c - '0'; |
| 305 | while( (c = *++fmt)>='0' && c<='9' ){ |
| 306 | wx = wx*10 + c - '0'; |
| 307 | } |
| 308 | testcase( wx>0x7fffffff ); |
| 309 | width = wx & 0x7fffffff; |
| 310 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 311 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 312 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 313 | } |
| 314 | #endif |
| 315 | if( c!='.' && c!='l' ){ |
| 316 | done = 1; |
| 317 | }else{ |
| 318 | fmt--; |
| 319 | } |
| 320 | break; |
| 321 | } |
| 322 | case '*': { |
| 323 | if( bArgList ){ |
| 324 | width = (int)getIntArg(pArgList); |
| 325 | }else{ |
| 326 | width = va_arg(ap,int); |
| 327 | } |
| 328 | if( width<0 ){ |
| 329 | flag_leftjustify = 1; |
| 330 | width = width >= -2147483647 ? -width : 0; |
| 331 | } |
| 332 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 333 | if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 334 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 335 | } |
| 336 | #endif |
| 337 | if( (c = fmt[1])!='.' && c!='l' ){ |
| 338 | c = *++fmt; |
| 339 | done = 1; |
| 340 | } |
| 341 | break; |
| 342 | } |
| 343 | case '.': { |
| 344 | c = *++fmt; |
| 345 | if( c=='*' ){ |
| 346 | if( bArgList ){ |
| 347 | precision = (int)getIntArg(pArgList); |
| 348 | }else{ |
| 349 | precision = va_arg(ap,int); |
| 350 | } |
| 351 | if( precision<0 ){ |
| 352 | precision = precision >= -2147483647 ? -precision : -1; |
| 353 | } |
| 354 | c = *++fmt; |
| 355 | }else{ |
| 356 | unsigned px = 0; |
| 357 | while( c>='0' && c<='9' ){ |
| 358 | px = px*10 + c - '0'; |
| 359 | c = *++fmt; |
| 360 | } |
| 361 | testcase( px>0x7fffffff ); |
| 362 | precision = px & 0x7fffffff; |
| 363 | } |
| 364 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 365 | if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 366 | precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 367 | } |
| 368 | #endif |
| 369 | if( c=='l' ){ |
| 370 | --fmt; |
| 371 | }else{ |
| 372 | done = 1; |
| 373 | } |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | }while( !done && (c=(*++fmt))!=0 ); |
| 378 | |
| 379 | /* Fetch the info entry for the field */ |
| 380 | #ifdef SQLITE_EBCDIC |
| 381 | /* The hash table only works for ASCII. For EBCDIC, we need to do |
| 382 | ** a linear search of the table */ |
| 383 | infop = &fmtinfo[0]; |
| 384 | xtype = etINVALID; |
| 385 | for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
| 386 | if( c==fmtinfo[idx].fmttype ){ |
| 387 | infop = &fmtinfo[idx]; |
| 388 | xtype = infop->type; |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | #else |
| 393 | /* Fast hash-table lookup */ |
| 394 | assert( ArraySize(fmtinfo)==25 ); |
| 395 | idx = ((unsigned)c) % 25; |
| 396 | if( fmtinfo[idx].fmttype==c |
| 397 | || fmtinfo[idx = fmtinfo[idx].iNxt].fmttype==c |
| 398 | ){ |
| 399 | infop = &fmtinfo[idx]; |
| 400 | xtype = infop->type; |
| 401 | }else{ |
| 402 | infop = &fmtinfo[0]; |
| 403 | xtype = etINVALID; |
| 404 | } |
| 405 | #endif |
| 406 | |
| 407 | /* |
| 408 | ** At this point, variables are initialized as follows: |
| 409 | ** |
| 410 | ** flag_alternateform TRUE if a '#' is present. |
| 411 | ** flag_altform2 TRUE if a '!' is present. |
| 412 | ** flag_prefix '+' or ' ' or zero |
| 413 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 414 | ** field width was negative. |
| 415 | ** flag_zeropad TRUE if the width began with 0. |
| 416 | ** flag_long 1 for "l", 2 for "ll" |
| 417 | ** width The specified field width. This is |
| 418 | ** always non-negative. Zero is the default. |
| 419 | ** precision The specified precision. The default |
| 420 | ** is -1. |
| 421 | ** xtype The class of the conversion. |
| 422 | ** infop Pointer to the appropriate info struct. |
| 423 | */ |
| 424 | assert( width>=0 ); |
| 425 | assert( precision>=(-1) ); |
| 426 | switch( xtype ){ |
| 427 | case etPOINTER: |
| 428 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 429 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
| 430 | /* no break */ deliberate_fall_through |
| 431 | case etORDINAL: |
| 432 | case etRADIX: |
| 433 | cThousand = 0; |
| 434 | /* no break */ deliberate_fall_through |
| 435 | case etDECIMAL: |
| 436 | if( infop->flags & FLAG_SIGNED ){ |
| 437 | i64 v; |
| 438 | if( bArgList ){ |
| 439 | v = getIntArg(pArgList); |
| 440 | }else if( flag_long ){ |
| 441 | if( flag_long==2 ){ |
| 442 | v = va_arg(ap,i64) ; |
| 443 | }else{ |
| 444 | v = va_arg(ap,long int); |
| 445 | } |
| 446 | }else{ |
| 447 | v = va_arg(ap,int); |
| 448 | } |
| 449 | if( v<0 ){ |
| 450 | testcase( v==SMALLEST_INT64 ); |
| 451 | testcase( v==(-1) ); |
| 452 | longvalue = ~v; |
| 453 | longvalue++; |
| 454 | prefix = '-'; |
| 455 | }else{ |
| 456 | longvalue = v; |
| 457 | prefix = flag_prefix; |
| 458 | } |
| 459 | }else{ |
| 460 | if( bArgList ){ |
| 461 | longvalue = (u64)getIntArg(pArgList); |
| 462 | }else if( flag_long ){ |
| 463 | if( flag_long==2 ){ |
| 464 | longvalue = va_arg(ap,u64); |
| 465 | }else{ |
| 466 | longvalue = va_arg(ap,unsigned long int); |
| 467 | } |
| 468 | }else{ |
| 469 | longvalue = va_arg(ap,unsigned int); |
| 470 | } |
| 471 | prefix = 0; |
| 472 | } |
| 473 | |
| 474 | #if WHERETRACE_ENABLED |
| 475 | if( xtype==etPOINTER && sqlite3WhereTrace & 0x100000 ) longvalue = 0; |
| 476 | #endif |
| 477 | #if TREETRACE_ENABLED |
| 478 | if( xtype==etPOINTER && sqlite3TreeTrace & 0x100000 ) longvalue = 0; |
| 479 | #endif |
| 480 | |
| 481 | if( longvalue==0 ) flag_alternateform = 0; |
| 482 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 483 | precision = width-(prefix!=0); |
| 484 | } |
| 485 | if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ |
| 486 | nOut = etBUFSIZE; |
| 487 | zOut = buf; |
| 488 | }else{ |
| 489 | u64 n; |
| 490 | n = (u64)precision + 10; |
| 491 | if( cThousand ) n += precision/3; |
| 492 | zOut = zExtra = printfTempBuf(pAccum, n); |
| 493 | if( zOut==0 ) return; |
| 494 | nOut = (int)n; |
| 495 | } |
| 496 | bufpt = &zOut[nOut-1]; |
| 497 | if( xtype==etORDINAL ){ |
| 498 | static const char zOrd[] = "thstndrd"; |
| 499 | int x = (int)(longvalue % 10); |
| 500 | if( x>=4 || (longvalue/10)%10==1 ){ |
| 501 | x = 0; |
| 502 | } |
| 503 | *(--bufpt) = zOrd[x*2+1]; |
| 504 | *(--bufpt) = zOrd[x*2]; |
| 505 | } |
| 506 | { |
| 507 | const char *cset = &aDigits[infop->charset]; |
| 508 | u8 base = infop->base; |
| 509 | do{ /* Convert to ascii */ |
| 510 | *(--bufpt) = cset[longvalue%base]; |
| 511 | longvalue = longvalue/base; |
| 512 | }while( longvalue>0 ); |
| 513 | } |
| 514 | length = &zOut[nOut-1] - bufpt; |
| 515 | if( precision>length ){ /* zero pad */ |
| 516 | i64 nn = precision-length; |
| 517 | bufpt -= nn; |
| 518 | memset(bufpt,'0',nn); |
| 519 | length = precision; |
| 520 | } |
| 521 | if( cThousand ){ |
| 522 | i64 nn = (length - 1)/3; /* Number of "," to insert */ |
| 523 | i64 ix = (length - 1)%3 + 1; |
| 524 | int ii; |
| 525 | bufpt -= nn; |
| 526 | for(ii=0; nn>0; ii++){ |
| 527 | bufpt[ii] = bufpt[ii+nn]; |
| 528 | ix--; |
| 529 | if( ix==0 ){ |
| 530 | bufpt[++ii] = cThousand; |
| 531 | nn--; |
| 532 | ix = 3; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 537 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
| 538 | const char *pre; |
| 539 | char x; |
| 540 | pre = &aPrefix[infop->prefix]; |
| 541 | for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
| 542 | } |
| 543 | length = &zOut[nOut-1] - bufpt; |
| 544 | break; |
| 545 | case etFLOAT: |
| 546 | case etEXP: |
| 547 | case etGENERIC: { |
| 548 | FpDecode s; |
| 549 | int iRound; |
| 550 | int j; |
| 551 | i64 szBufNeeded; /* Size needed to hold the output */ |
| 552 | |
| 553 | if( bArgList ){ |
| 554 | realvalue = getDoubleArg(pArgList); |
| 555 | }else{ |
| 556 | realvalue = va_arg(ap,double); |
| 557 | } |
| 558 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 559 | if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 560 | precision = SQLITE_FP_PRECISION_LIMIT; |
| 561 | } |
| 562 | if( xtype==etFLOAT ){ |
| 563 | iRound = -precision; |
| 564 | }else if( xtype==etGENERIC ){ |
| 565 | if( precision==0 ) precision = 1; |
| 566 | iRound = precision; |
| 567 | }else{ |
| 568 | iRound = precision+1; |
| 569 | } |
| 570 | sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 20 : 16); |
| 571 | if( s.isSpecial ){ |
| 572 | if( s.isSpecial==2 ){ |
| 573 | if( flag_zeropad ){ |
| 574 | bufpt = "null"; |
| 575 | length = 4; |
| 576 | }else{ |
| 577 | bufpt = "NaN"; |
| 578 | length = 3; |
| 579 | } |
| 580 | break; |
| 581 | }else if( flag_zeropad ){ |
| 582 | s.z[0] = '9'; |
| 583 | s.iDP = 1000; |
| 584 | s.n = 1; |
| 585 | }else{ |
| 586 | memcpy(buf, "-Inf", 5); |
| 587 | bufpt = buf; |
| 588 | if( s.sign=='-' ){ |
| 589 | /* no-op */ |
| 590 | }else if( flag_prefix ){ |
| 591 | buf[0] = flag_prefix; |
| 592 | }else{ |
| 593 | bufpt++; |
| 594 | } |
| 595 | length = strlen(bufpt); |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | if( s.sign=='-' ){ |
| 600 | if( flag_alternateform |
| 601 | && !flag_prefix |
| 602 | && xtype==etFLOAT |
| 603 | && s.iDP<=iRound |
| 604 | ){ |
| 605 | /* Suppress the minus sign if all of the following are true: |
| 606 | ** * The value displayed is zero |
| 607 | ** * The '#' flag is used |
| 608 | ** * The '+' flag is not used, and |
| 609 | ** * The format is %f |
| 610 | */ |
| 611 | prefix = 0; |
| 612 | }else{ |
| 613 | prefix = '-'; |
| 614 | } |
| 615 | }else{ |
| 616 | prefix = flag_prefix; |
| 617 | } |
| 618 | |
| 619 | exp = s.iDP-1; |
| 620 | |
| 621 | /* |
| 622 | ** If the field type is etGENERIC, then convert to either etEXP |
| 623 | ** or etFLOAT, as appropriate. |
| 624 | */ |
| 625 | if( xtype==etGENERIC ){ |
| 626 | assert( precision>0 ); |
| 627 | precision--; |
| 628 | flag_rtz = !flag_alternateform; |
| 629 | if( exp<-4 || exp>precision ){ |
| 630 | xtype = etEXP; |
| 631 | }else{ |
| 632 | precision = precision - exp; |
| 633 | xtype = etFLOAT; |
| 634 | } |
| 635 | }else{ |
| 636 | flag_rtz = flag_altform2; |
| 637 | } |
| 638 | if( xtype==etEXP ){ |
| 639 | e2 = 0; |
| 640 | }else{ |
| 641 | e2 = s.iDP - 1; |
| 642 | } |
| 643 | |
| 644 | szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+10; |
| 645 | if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3; |
| 646 | if( szBufNeeded + pAccum->nChar >= pAccum->nAlloc ){ |
| 647 | if( pAccum->mxAlloc==0 && pAccum->accError==0 ){ |
| 648 | /* Unable to allocate space in pAccum, perhaps because it |
| 649 | ** is coming from sqlite3_snprintf() or similar. We'll have |
| 650 | ** to render into temporary space and the memcpy() it over. */ |
| 651 | bufpt = sqlite3_malloc64(szBufNeeded); |
| 652 | if( bufpt==0 ){ |
| 653 | sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM); |
| 654 | return; |
| 655 | } |
| 656 | zExtra = bufpt; |
| 657 | }else if( sqlite3StrAccumEnlarge(pAccum, szBufNeeded)<szBufNeeded ){ |
| 658 | width = length = 0; |
| 659 | break; |
| 660 | }else{ |
| 661 | bufpt = pAccum->zText + pAccum->nChar; |
| 662 | } |
| 663 | }else{ |
| 664 | bufpt = pAccum->zText + pAccum->nChar; |
| 665 | } |
| 666 | zOut = bufpt; |
| 667 | |
| 668 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
| 669 | /* The sign in front of the number */ |
| 670 | if( prefix ){ |
| 671 | *(bufpt++) = prefix; |
| 672 | } |
| 673 | /* Digits prior to the decimal point */ |
| 674 | j = 0; |
| 675 | assert( s.n>0 ); |
| 676 | if( e2<0 ){ |
| 677 | *(bufpt++) = '0'; |
| 678 | }else if( cThousand ){ |
| 679 | for(; e2>=0; e2--){ |
| 680 | *(bufpt++) = j<s.n ? s.z[j++] : '0'; |
| 681 | if( (e2%3)==0 && e2>1 ) *(bufpt++) = ','; |
| 682 | } |
| 683 | }else{ |
| 684 | j = e2+1; |
| 685 | if( j>s.n ) j = s.n; |
| 686 | memcpy(bufpt, s.z, j); |
| 687 | bufpt += j; |
| 688 | e2 -= j; |
| 689 | if( e2>=0 ){ |
| 690 | memset(bufpt, '0', e2+1); |
| 691 | bufpt += e2+1; |
| 692 | e2 = -1; |
| 693 | } |
| 694 | } |
| 695 | /* The decimal point */ |
| 696 | if( flag_dp ){ |
| 697 | *(bufpt++) = '.'; |
| 698 | } |
| 699 | /* "0" digits after the decimal point but before the first |
| 700 | ** significant digit of the number */ |
| 701 | if( e2<(-1) && precision>0 ){ |
| 702 | int nn = -1-e2; |
| 703 | if( nn>precision ) nn = precision; |
| 704 | memset(bufpt, '0', nn); |
| 705 | bufpt += nn; |
| 706 | precision -= nn; |
| 707 | } |
| 708 | /* Significant digits after the decimal point */ |
| 709 | if( precision>0 ){ |
| 710 | int nn = s.n - j; |
| 711 | if( NEVER(nn>precision) ) nn = precision; |
| 712 | if( nn>0 ){ |
| 713 | memcpy(bufpt, s.z+j, nn); |
| 714 | bufpt += nn; |
| 715 | precision -= nn; |
| 716 | } |
| 717 | if( precision>0 && !flag_rtz ){ |
| 718 | memset(bufpt, '0', precision); |
| 719 | bufpt += precision; |
| 720 | } |
| 721 | } |
| 722 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 723 | if( flag_rtz && flag_dp ){ |
| 724 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 725 | assert( bufpt>zOut ); |
| 726 | if( bufpt[-1]=='.' ){ |
| 727 | if( flag_altform2 ){ |
| 728 | *(bufpt++) = '0'; |
| 729 | }else{ |
| 730 | *(--bufpt) = 0; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | /* Add the "eNNN" suffix */ |
| 735 | if( xtype==etEXP ){ |
| 736 | exp = s.iDP - 1; |
| 737 | *(bufpt++) = aDigits[infop->charset]; |
| 738 | if( exp<0 ){ |
| 739 | *(bufpt++) = '-'; exp = -exp; |
| 740 | }else{ |
| 741 | *(bufpt++) = '+'; |
| 742 | } |
| 743 | if( exp>=100 ){ |
| 744 | *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ |
| 745 | exp %= 100; |
| 746 | } |
| 747 | *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ |
| 748 | *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ |
| 749 | } |
| 750 | |
| 751 | length = bufpt - zOut; |
| 752 | assert( length <= szBufNeeded ); |
| 753 | if( length<width ){ |
| 754 | i64 nPad = width - length; |
| 755 | if( flag_leftjustify ){ |
| 756 | memset(bufpt, ' ', nPad); |
| 757 | }else if( !flag_zeropad ){ |
| 758 | memmove(zOut+nPad, zOut, length); |
| 759 | memset(zOut, ' ', nPad); |
| 760 | }else{ |
| 761 | int adj = prefix!=0; |
| 762 | memmove(zOut+nPad+adj, zOut+adj, length-adj); |
| 763 | memset(zOut+adj, '0', nPad); |
| 764 | } |
| 765 | length = width; |
| 766 | } |
| 767 | |
| 768 | if( zExtra==0 ){ |
| 769 | /* The result is being rendered directory into pAccum. This |
| 770 | ** is the common and fast case */ |
| 771 | assert( pAccum->nChar + length < SMXV(pAccum->nChar) ); |
| 772 | pAccum->nChar += length; |
| 773 | zOut[length] = 0; |
| 774 | continue; |
| 775 | }else{ |
| 776 | /* We were unable to render directly into pAccum because we |
| 777 | ** couldn't allocate sufficient memory. We need to memcpy() |
| 778 | ** the rendering (or some prefix thereof) into the output |
| 779 | ** buffer. */ |
| 780 | bufpt[0] = 0; |
| 781 | bufpt = zExtra; |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | case etSIZE: |
| 786 | if( !bArgList ){ |
| 787 | *(va_arg(ap,int*)) = pAccum->nChar; |
| 788 | } |
| 789 | length = width = 0; |
| 790 | break; |
| 791 | case etPERCENT: |
| 792 | buf[0] = '%'; |
| 793 | bufpt = buf; |
| 794 | length = 1; |
| 795 | break; |
| 796 | case etCHARX: |
| 797 | if( bArgList ){ |
| 798 | bufpt = getTextArg(pArgList); |
| 799 | length = 1; |
| 800 | if( bufpt ){ |
| 801 | buf[0] = c = *(bufpt++); |
| 802 | if( (c&0xc0)==0xc0 ){ |
| 803 | while( length<4 && (bufpt[0]&0xc0)==0x80 ){ |
| 804 | buf[length++] = *(bufpt++); |
| 805 | } |
| 806 | } |
| 807 | }else{ |
| 808 | buf[0] = 0; |
| 809 | } |
| 810 | }else{ |
| 811 | unsigned int ch = va_arg(ap,unsigned int); |
| 812 | length = sqlite3AppendOneUtf8Character(buf, ch); |
| 813 | } |
| 814 | if( precision>1 ){ |
| 815 | i64 nPrior = 1; |
| 816 | width -= precision-1; |
| 817 | if( width>1 && !flag_leftjustify ){ |
| 818 | sqlite3StrAppendchar64(pAccum, width-1, ' '); |
| 819 | width = 0; |
| 820 | } |
| 821 | sqlite3StrAppend64(pAccum, buf, length); |
| 822 | precision--; |
| 823 | while( precision > 1 ){ |
| 824 | i64 nCopyBytes; |
| 825 | if( nPrior > precision-1 ) nPrior = precision - 1; |
| 826 | nCopyBytes = length*nPrior; |
| 827 | if( sqlite3StrAccumEnlargeIfNeeded(pAccum, nCopyBytes) ){ |
| 828 | break; |
| 829 | } |
| 830 | sqlite3_str_append(pAccum, |
| 831 | &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); |
| 832 | precision -= nPrior; |
| 833 | nPrior *= 2; |
| 834 | } |
| 835 | } |
| 836 | bufpt = buf; |
| 837 | flag_altform2 = 1; |
| 838 | goto adjust_width_for_utf8; |
| 839 | case etSTRING: |
| 840 | case etDYNSTRING: |
| 841 | if( bArgList ){ |
| 842 | bufpt = getTextArg(pArgList); |
| 843 | xtype = etSTRING; |
| 844 | }else{ |
| 845 | bufpt = va_arg(ap,char*); |
| 846 | } |
| 847 | if( bufpt==0 ){ |
| 848 | bufpt = ""; |
| 849 | }else if( xtype==etDYNSTRING ){ |
| 850 | if( pAccum->nChar==0 |
| 851 | && pAccum->mxAlloc |
| 852 | && width==0 |
| 853 | && precision<0 |
| 854 | && pAccum->accError==0 |
| 855 | ){ |
| 856 | /* Special optimization for sqlite3_mprintf("%z..."): |
| 857 | ** Extend an existing memory allocation rather than creating |
| 858 | ** a new one. */ |
| 859 | assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 860 | pAccum->zText = bufpt; |
| 861 | pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); |
| 862 | pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); |
| 863 | pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 864 | length = 0; |
| 865 | break; |
| 866 | } |
| 867 | zExtra = bufpt; |
| 868 | } |
| 869 | if( precision>=0 ){ |
| 870 | if( flag_altform2 ){ |
| 871 | /* Set length to the number of bytes needed in order to display |
| 872 | ** precision characters */ |
| 873 | unsigned char *z = (unsigned char*)bufpt; |
| 874 | while( precision-- > 0 && z[0] ){ |
| 875 | SQLITE_SKIP_UTF8(z); |
| 876 | } |
| 877 | length = z - (unsigned char*)bufpt; |
| 878 | }else{ |
| 879 | for(length=0; length<precision && bufpt[length]; length++){} |
| 880 | } |
| 881 | }else{ |
| 882 | length = strlen(bufpt); |
| 883 | } |
| 884 | adjust_width_for_utf8: |
| 885 | if( flag_altform2 && width>0 ){ |
| 886 | /* Adjust width to account for extra bytes in UTF-8 characters */ |
| 887 | i64 ii = length - 1; |
| 888 | while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; |
| 889 | } |
| 890 | break; |
| 891 | case etESCAPE_j: /* %j: JSON string literal w/o "..." */ |
| 892 | case etESCAPE_J: { /* %J: Generate a JSON string literal */ |
| 893 | char *escarg; |
| 894 | i64 i, j, px, iStart; |
| 895 | unsigned char ch; |
| 896 | |
| 897 | if( bArgList ){ |
| 898 | escarg = getTextArg(pArgList); |
| 899 | }else{ |
| 900 | escarg = va_arg(ap,char*); |
| 901 | } |
| 902 | iStart = sqlite3_str_length(pAccum); |
| 903 | if( escarg==0 ){ |
| 904 | if( xtype==etESCAPE_J ) sqlite3_str_append(pAccum, "null", 4); |
| 905 | }else{ |
| 906 | if( xtype==etESCAPE_J ) sqlite3_str_append(pAccum, "\"", 1); |
| 907 | px = precision; |
| 908 | testcase( px==0 ); |
| 909 | if( px<0 ){ |
| 910 | px = 0x7fffffff; |
| 911 | }else if( flag_altform2 ){ |
| 912 | /* Convert precision from code-points to bytes */ |
| 913 | for(i=0; i<px && escarg[i]; i++){ |
| 914 | if( (escarg[i]&0xc0)==0x80 ) px++; |
| 915 | } |
| 916 | if( i==px ){ |
| 917 | while( (escarg[px]&0xc0)==0x80 ) px++; |
| 918 | } |
| 919 | } |
| 920 | for(i=j=0; i<px; i++){ |
| 921 | if( (ch = ((u8*)escarg)[i])<=0x1f || ch=='"' || ch=='\\' ){ |
| 922 | if( j<i ) sqlite3StrAppend64(pAccum, &escarg[j], i-j); |
| 923 | j = i+1; |
| 924 | if( ch==0 ) break; |
| 925 | sqlite3_str_appendchar(pAccum, 1, '\\'); |
| 926 | if( ch>0x1f ){ |
| 927 | sqlite3_str_appendchar(pAccum, 1, ch); |
| 928 | }else if( ((1u<<ch)&0x3700)!=0 ){ |
| 929 | ch = "btn?fr"[ch-8]; |
| 930 | sqlite3_str_appendchar(pAccum, 1, ch); |
| 931 | }else{ |
| 932 | sqlite3_str_append(pAccum, "u00", 3); |
| 933 | sqlite3_str_appendchar(pAccum, 1, aHex[ch>>4]); |
| 934 | sqlite3_str_appendchar(pAccum, 1, aHex[ch&0xf]); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | if( j<i ) sqlite3StrAppend64(pAccum, &escarg[j], i-j); |
| 939 | if( xtype==etESCAPE_J ) sqlite3_str_append(pAccum, "\"", 1); |
| 940 | } |
| 941 | if( width>0 && sqlite3_str_errcode(pAccum)==SQLITE_OK ){ |
| 942 | sqlite3_int64 n = sqlite3_str_length(pAccum) - iStart; |
| 943 | sqlite3_int64 len = n; |
| 944 | char *zz; |
| 945 | if( flag_altform2 && n>0 ){ |
| 946 | zz = sqlite3_str_value(pAccum); |
| 947 | for(i=iStart; zz[i]; i++){ |
| 948 | if( (zz[i]&0xc0)==0x80 ) len--; |
| 949 | } |
| 950 | } |
| 951 | if( width>len ){ |
| 952 | sqlite3_int64 sp = width-len; |
| 953 | assert( sp>0 && sp<=0x7fffffff ); |
| 954 | sqlite3StrAppendchar64(pAccum, (int)sp, ' '); |
| 955 | if( !flag_leftjustify |
| 956 | && n>0 |
| 957 | && sqlite3_str_errcode(pAccum)==0 |
| 958 | ){ |
| 959 | zz = sqlite3_str_value(pAccum); |
| 960 | zz += iStart; |
| 961 | memmove(zz+sp, zz, n); |
| 962 | memset(zz, ' ', sp); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | continue; |
| 967 | } |
| 968 | case etESCAPE_q: /* %q: Escape ' characters */ |
| 969 | case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */ |
| 970 | case etESCAPE_w: { /* %w: Escape " characters */ |
| 971 | i64 i, j, k, n; |
| 972 | int needQuote = 0; |
| 973 | char ch; |
| 974 | char *escarg; |
| 975 | char q; |
| 976 | |
| 977 | if( bArgList ){ |
| 978 | escarg = getTextArg(pArgList); |
| 979 | }else{ |
| 980 | escarg = va_arg(ap,char*); |
| 981 | } |
| 982 | if( escarg==0 ){ |
| 983 | escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)"); |
| 984 | }else if( xtype==etESCAPE_Q ){ |
| 985 | needQuote = 1; |
| 986 | } |
| 987 | if( xtype==etESCAPE_w ){ |
| 988 | q = '"'; |
| 989 | flag_alternateform = 0; |
| 990 | }else{ |
| 991 | q = '\''; |
| 992 | } |
| 993 | /* For %q, %Q, and %w, the precision is the number of bytes (or |
| 994 | ** characters if the ! flags is present) to use from the input. |
| 995 | ** Because of the extra quoting characters inserted, the number |
| 996 | ** of output characters may be larger than the precision. |
| 997 | */ |
| 998 | k = precision; |
| 999 | for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ |
| 1000 | if( ch==q ) n++; |
| 1001 | if( flag_altform2 && (ch&0xc0)==0xc0 ){ |
| 1002 | while( (escarg[i+1]&0xc0)==0x80 ){ i++; } |
| 1003 | } |
| 1004 | } |
| 1005 | if( flag_alternateform ){ |
| 1006 | /* For %#q, do unistr()-style backslash escapes for |
| 1007 | ** all control characters, and for backslash itself. |
| 1008 | ** For %#Q, do the same but only if there is at least |
| 1009 | ** one control character. */ |
| 1010 | i64 nBack = 0; |
| 1011 | i64 nCtrl = 0; |
| 1012 | for(k=0; k<i; k++){ |
| 1013 | if( escarg[k]=='\\' ){ |
| 1014 | nBack++; |
| 1015 | }else if( ((u8*)escarg)[k]<=0x1f ){ |
| 1016 | nCtrl++; |
| 1017 | } |
| 1018 | } |
| 1019 | if( nCtrl || xtype==etESCAPE_q ){ |
| 1020 | n += nBack + 5*nCtrl; |
| 1021 | if( xtype==etESCAPE_Q ){ |
| 1022 | n += 10; |
| 1023 | needQuote = 2; |
| 1024 | } |
| 1025 | }else{ |
| 1026 | flag_alternateform = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | n += i + 3; |
| 1030 | if( n>etBUFSIZE ){ |
| 1031 | bufpt = zExtra = printfTempBuf(pAccum, n); |
| 1032 | if( bufpt==0 ) return; |
| 1033 | }else{ |
| 1034 | bufpt = buf; |
| 1035 | } |
| 1036 | j = 0; |
| 1037 | if( needQuote ){ |
| 1038 | if( needQuote==2 ){ |
| 1039 | memcpy(&bufpt[j], "unistr('", 8); |
| 1040 | j += 8; |
| 1041 | }else{ |
| 1042 | bufpt[j++] = '\''; |
| 1043 | } |
| 1044 | } |
| 1045 | k = i; |
| 1046 | if( flag_alternateform ){ |
| 1047 | for(i=0; i<k; i++){ |
| 1048 | bufpt[j++] = ch = escarg[i]; |
| 1049 | if( ch==q ){ |
| 1050 | bufpt[j++] = ch; |
| 1051 | }else if( ch=='\\' ){ |
| 1052 | bufpt[j++] = '\\'; |
| 1053 | }else if( ((unsigned char)ch)<=0x1f ){ |
| 1054 | bufpt[j-1] = '\\'; |
| 1055 | bufpt[j++] = 'u'; |
| 1056 | bufpt[j++] = '0'; |
| 1057 | bufpt[j++] = '0'; |
| 1058 | bufpt[j++] = ch>=0x10 ? '1' : '0'; |
| 1059 | bufpt[j++] = aHex[ch&0xf]; |
| 1060 | } |
| 1061 | } |
| 1062 | }else{ |
| 1063 | for(i=0; i<k; i++){ |
| 1064 | bufpt[j++] = ch = escarg[i]; |
| 1065 | if( ch==q ) bufpt[j++] = ch; |
| 1066 | } |
| 1067 | } |
| 1068 | if( needQuote ){ |
| 1069 | bufpt[j++] = '\''; |
| 1070 | if( needQuote==2 ) bufpt[j++] = ')'; |
| 1071 | } |
| 1072 | bufpt[j] = 0; |
| 1073 | length = j; |
| 1074 | goto adjust_width_for_utf8; |
| 1075 | } |
| 1076 | case etTOKEN: { |
| 1077 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 1078 | if( flag_alternateform ){ |
| 1079 | /* %#T means an Expr pointer that uses Expr.u.zToken */ |
| 1080 | Expr *pExpr = va_arg(ap,Expr*); |
| 1081 | if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){ |
| 1082 | sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); |
| 1083 | sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); |
| 1084 | } |
| 1085 | }else{ |
| 1086 | /* %T means a Token pointer */ |
| 1087 | Token *pToken = va_arg(ap, Token*); |
| 1088 | assert( bArgList==0 ); |
| 1089 | if( pToken && pToken->n ){ |
| 1090 | sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
| 1091 | sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); |
| 1092 | } |
| 1093 | } |
| 1094 | length = width = 0; |
| 1095 | break; |
| 1096 | } |
| 1097 | case etSRCITEM: { |
| 1098 | SrcItem *pItem; |
| 1099 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 1100 | pItem = va_arg(ap, SrcItem*); |
| 1101 | assert( bArgList==0 ); |
| 1102 | if( pItem->zAlias && !flag_altform2 ){ |
| 1103 | sqlite3_str_appendall(pAccum, pItem->zAlias); |
| 1104 | }else if( pItem->zName ){ |
| 1105 | if( pItem->fg.fixedSchema==0 |
| 1106 | && pItem->fg.isSubquery==0 |
| 1107 | && pItem->u4.zDatabase!=0 |
| 1108 | ){ |
| 1109 | sqlite3_str_appendall(pAccum, pItem->u4.zDatabase); |
| 1110 | sqlite3_str_append(pAccum, ".", 1); |
| 1111 | } |
| 1112 | sqlite3_str_appendall(pAccum, pItem->zName); |
| 1113 | }else if( pItem->zAlias ){ |
| 1114 | sqlite3_str_appendall(pAccum, pItem->zAlias); |
| 1115 | }else if( ALWAYS(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */ |
| 1116 | Select *pSel = pItem->u4.pSubq->pSelect; |
| 1117 | assert( pSel!=0 ); |
| 1118 | if( pSel->selFlags & SF_NestedFrom ){ |
| 1119 | sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); |
| 1120 | }else if( pSel->selFlags & SF_MultiValue ){ |
| 1121 | assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); |
| 1122 | sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", |
| 1123 | pItem->u1.nRow); |
| 1124 | }else{ |
| 1125 | sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); |
| 1126 | } |
| 1127 | } |
| 1128 | length = width = 0; |
| 1129 | break; |
| 1130 | } |
| 1131 | default: { |
| 1132 | assert( xtype==etINVALID ); |
| 1133 | return; |
| 1134 | } |
| 1135 | }/* End switch over the format type */ |
| 1136 | /* |
| 1137 | ** The text of the conversion is pointed to by "bufpt" and is |
| 1138 | ** "length" characters long. The field width is "width". Do |
| 1139 | ** the output. Both length and width are in bytes, not characters, |
| 1140 | ** at this point. If the "!" flag was present on string conversions |
| 1141 | ** indicating that width and precision should be expressed in characters, |
| 1142 | ** then the values have been translated prior to reaching this point. |
| 1143 | */ |
| 1144 | width -= length; |
| 1145 | if( width>0 ){ |
| 1146 | if( !flag_leftjustify ) sqlite3StrAppendchar64(pAccum, width, ' '); |
| 1147 | sqlite3StrAppend64(pAccum, bufpt, length); |
| 1148 | if( flag_leftjustify ) sqlite3StrAppendchar64(pAccum, width, ' '); |
| 1149 | }else{ |
| 1150 | sqlite3StrAppend64(pAccum, bufpt, length); |
| 1151 | } |
| 1152 | |
| 1153 | if( zExtra ){ |
| 1154 | sqlite3DbFree(pAccum->db, zExtra); |
| 1155 | zExtra = 0; |
| 1156 | } |
| 1157 | }/* End for loop over the format string */ |
| 1158 | } /* End of function */ |
| 1159 | |
| 1160 | |
| 1161 | /* |
| 1162 | ** The z string points to the first character of a token that is |
| 1163 | ** associated with an error. If db does not already have an error |
| 1164 | ** byte offset recorded, try to compute the error byte offset for |
| 1165 | ** z and set the error byte offset in db. |
| 1166 | */ |
| 1167 | void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){ |
| 1168 | const Parse *pParse; |
| 1169 | const char *zText; |
| 1170 | const char *zEnd; |
| 1171 | assert( z!=0 ); |
| 1172 | if( NEVER(db==0) ) return; |
| 1173 | if( db->errByteOffset!=(-2) ) return; |
| 1174 | pParse = db->pParse; |
| 1175 | if( NEVER(pParse==0) ) return; |
| 1176 | zText =pParse->zTail; |
| 1177 | if( NEVER(zText==0) ) return; |
| 1178 | zEnd = &zText[strlen(zText)]; |
| 1179 | if( SQLITE_WITHIN(z,zText,zEnd) ){ |
| 1180 | db->errByteOffset = (int)(z-zText); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | ** If pExpr has a byte offset for the start of a token, record that as |
| 1186 | ** as the error offset. |
| 1187 | */ |
| 1188 | void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ |
| 1189 | while( pExpr ){ |
| 1190 | if( ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ) return; |
| 1191 | if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0 ){ |
| 1192 | pExpr = pExpr->pLeft; |
| 1193 | }else{ |
| 1194 | break; |
| 1195 | } |
| 1196 | } |
| 1197 | if( pExpr==0 ) return; |
| 1198 | if( ExprHasProperty(pExpr, EP_FromDDL) ) return; |
| 1199 | db->errByteOffset = pExpr->w.iOfst; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 1204 | ** able to accept at least N more bytes of text. |
| 1205 | ** |
| 1206 | ** Return the number of bytes of text that StrAccum is able to accept |
| 1207 | ** after the attempted enlargement. The value returned might be zero. |
| 1208 | */ |
| 1209 | int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){ |
| 1210 | char *zNew; |
| 1211 | assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ |
| 1212 | if( p->accError ){ |
| 1213 | testcase(p->accError==SQLITE_TOOBIG); |
| 1214 | testcase(p->accError==SQLITE_NOMEM); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | if( p->mxAlloc==0 ){ |
| 1218 | sqlite3StrAccumSetError(p, SQLITE_TOOBIG); |
| 1219 | return p->nAlloc - p->nChar - 1; |
| 1220 | }else{ |
| 1221 | char *zOld = isMalloced(p) ? p->zText : 0; |
| 1222 | i64 szNew = p->nChar + N + 1; |
| 1223 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 1224 | /* Force exponential buffer size growth as long as it does not overflow, |
| 1225 | ** to avoid having to call this routine too often */ |
| 1226 | szNew += p->nChar; |
| 1227 | } |
| 1228 | if( szNew > p->mxAlloc ){ |
| 1229 | sqlite3_str_reset(p); |
| 1230 | sqlite3StrAccumSetError(p, SQLITE_TOOBIG); |
| 1231 | return 0; |
| 1232 | }else{ |
| 1233 | p->nAlloc = (int)szNew; |
| 1234 | } |
| 1235 | if( p->db ){ |
| 1236 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 1237 | }else{ |
| 1238 | zNew = sqlite3Realloc(zOld, p->nAlloc); |
| 1239 | } |
| 1240 | if( zNew ){ |
| 1241 | assert( p->zText!=0 || p->nChar==0 ); |
| 1242 | if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 1243 | p->zText = zNew; |
| 1244 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 1245 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1246 | }else{ |
| 1247 | sqlite3_str_reset(p); |
| 1248 | sqlite3StrAccumSetError(p, SQLITE_NOMEM); |
| 1249 | return 0; |
| 1250 | } |
| 1251 | } |
| 1252 | assert( N>=0 && N<=0x7fffffff ); |
| 1253 | return (int)N; |
| 1254 | } |
| 1255 | |
| 1256 | int sqlite3StrAccumEnlargeIfNeeded(StrAccum *p, i64 N){ |
| 1257 | if( N + p->nChar >= p->nAlloc ){ |
| 1258 | sqlite3StrAccumEnlarge(p, N); |
| 1259 | } |
| 1260 | return p->accError; |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | ** Append N copies of character c to the given string buffer. |
| 1265 | */ |
| 1266 | void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ |
| 1267 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 1268 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 1269 | return; |
| 1270 | } |
| 1271 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 1272 | } |
| 1273 | static void sqlite3StrAppendchar64(sqlite3_str *p, i64 N, char c){ |
| 1274 | testcase( p->nChar + N > 0x7fffffff ); |
| 1275 | if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 1276 | return; |
| 1277 | } |
| 1278 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 1283 | ** So enlarge if first, then do the append. |
| 1284 | ** |
| 1285 | ** This is a helper routine to sqlite3_str_append() that does special-case |
| 1286 | ** work (enlarging the buffer) using tail recursion, so that the |
| 1287 | ** sqlite3_str_append() routine can use fast calling semantics. |
| 1288 | */ |
| 1289 | static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ |
| 1290 | N = sqlite3StrAccumEnlarge(p, N); |
| 1291 | assert( z!=0 || N==0 ); |
| 1292 | if( N>0 ){ |
| 1293 | memcpy(&p->zText[p->nChar], z, N); |
| 1294 | p->nChar += N; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 1300 | ** size of the memory allocation for StrAccum if necessary. |
| 1301 | */ |
| 1302 | void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ |
| 1303 | assert( z!=0 || N==0 ); |
| 1304 | assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 1305 | assert( N>=0 ); |
| 1306 | assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); |
| 1307 | if( p->nChar+N >= p->nAlloc ){ |
| 1308 | enlargeAndAppend(p,z,N); |
| 1309 | }else if( N ){ |
| 1310 | assert( p->zText ); |
| 1311 | p->nChar += N; |
| 1312 | memcpy(&p->zText[p->nChar-N], z, N); |
| 1313 | } |
| 1314 | } |
| 1315 | static void sqlite3StrAppend64(sqlite3_str *p, const char *z, i64 N){ |
| 1316 | assert( z!=0 || N==0 ); |
| 1317 | assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 1318 | assert( N>=0 ); |
| 1319 | assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); |
| 1320 | if( p->nChar+N >= (i64)p->nAlloc ){ |
| 1321 | enlargeAndAppend(p,z,N); |
| 1322 | }else if( N ){ |
| 1323 | assert( p->zText ); |
| 1324 | p->nChar += N; |
| 1325 | memcpy(&p->zText[p->nChar-N], z, N); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | /* |
| 1331 | ** Append the complete text of zero-terminated string z[] to the p string. |
| 1332 | */ |
| 1333 | void sqlite3_str_appendall(sqlite3_str *p, const char *z){ |
| 1334 | sqlite3_str_append(p, z, sqlite3Strlen30(z)); |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /* |
| 1339 | ** Finish off a string by making sure it is zero-terminated. |
| 1340 | ** Return a pointer to the resulting string. Return a NULL |
| 1341 | ** pointer if any kind of error was encountered. |
| 1342 | */ |
| 1343 | static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ |
| 1344 | char *zText; |
| 1345 | assert( p->mxAlloc>0 && !isMalloced(p) ); |
| 1346 | zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar ); |
| 1347 | if( zText ){ |
| 1348 | memcpy(zText, p->zText, p->nChar+1); |
| 1349 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1350 | }else{ |
| 1351 | sqlite3StrAccumSetError(p, SQLITE_NOMEM); |
| 1352 | } |
| 1353 | p->zText = zText; |
| 1354 | return zText; |
| 1355 | } |
| 1356 | char *sqlite3StrAccumFinish(StrAccum *p){ |
| 1357 | if( p->zText ){ |
| 1358 | p->zText[p->nChar] = 0; |
| 1359 | if( p->mxAlloc>0 && !isMalloced(p) ){ |
| 1360 | return strAccumFinishRealloc(p); |
| 1361 | } |
| 1362 | } |
| 1363 | return p->zText; |
| 1364 | } |
| 1365 | |
| 1366 | /* Finalize a string created using sqlite3_str_new(). |
| 1367 | */ |
| 1368 | char *sqlite3_str_finish(sqlite3_str *p){ |
| 1369 | char *z; |
| 1370 | if( p!=0 && p!=(sqlite3_str*)&sqlite3OomStr ){ |
| 1371 | z = sqlite3StrAccumFinish(p); |
| 1372 | sqlite3_free(p); |
| 1373 | }else{ |
| 1374 | z = 0; |
| 1375 | } |
| 1376 | return z; |
| 1377 | } |
| 1378 | |
| 1379 | /* Return any error code associated with p */ |
| 1380 | int sqlite3_str_errcode(sqlite3_str *p){ |
| 1381 | return p ? p->accError : SQLITE_NOMEM; |
| 1382 | } |
| 1383 | |
| 1384 | /* Return the current length of p in bytes */ |
| 1385 | int sqlite3_str_length(sqlite3_str *p){ |
| 1386 | return p ? p->nChar : 0; |
| 1387 | } |
| 1388 | |
| 1389 | /* Truncate the text of the string to be no more than N bytes. */ |
| 1390 | void sqlite3_str_truncate(sqlite3_str *p, int N){ |
| 1391 | if( p!=0 && N>=0 && (u32)N<p->nChar ){ |
| 1392 | p->nChar = N; |
| 1393 | p->zText[p->nChar] = 0; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /* Return the current value for p */ |
| 1398 | char *sqlite3_str_value(sqlite3_str *p){ |
| 1399 | if( p==0 || p->nChar==0 ) return 0; |
| 1400 | p->zText[p->nChar] = 0; |
| 1401 | return p->zText; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 1406 | */ |
| 1407 | void sqlite3_str_reset(StrAccum *p){ |
| 1408 | if( isMalloced(p) ){ |
| 1409 | sqlite3DbFree(p->db, p->zText); |
| 1410 | p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; |
| 1411 | }else if( p==(sqlite3_str*)&sqlite3OomStr ){ |
| 1412 | return; |
| 1413 | } |
| 1414 | p->nAlloc = 0; |
| 1415 | p->nChar = 0; |
| 1416 | p->zText = 0; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | ** Destroy a dynamically allocate sqlite3_str object and all |
| 1421 | ** of its content, all in one call. |
| 1422 | */ |
| 1423 | void sqlite3_str_free(sqlite3_str *p){ |
| 1424 | if( p!=0 && p!=(sqlite3_str*)&sqlite3OomStr ){ |
| 1425 | sqlite3_str_reset(p); |
| 1426 | sqlite3_free(p); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | ** Initialize a string accumulator. |
| 1432 | ** |
| 1433 | ** p: The accumulator to be initialized. |
| 1434 | ** db: Pointer to a database connection. May be NULL. Lookaside |
| 1435 | ** memory is used if not NULL. db->mallocFailed is set appropriately |
| 1436 | ** when not NULL. |
| 1437 | ** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 1438 | ** is malloced. |
| 1439 | ** n: Size of zBase in bytes. If total space requirements never exceed |
| 1440 | ** n then no memory allocations ever occur. |
| 1441 | ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 1442 | ** allocations will ever occur. |
| 1443 | */ |
| 1444 | void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ |
| 1445 | p->zText = zBase; |
| 1446 | p->db = db; |
| 1447 | p->nAlloc = n; |
| 1448 | p->mxAlloc = mx; |
| 1449 | p->nChar = 0; |
| 1450 | p->accError = 0; |
| 1451 | p->printfFlags = 0; |
| 1452 | } |
| 1453 | |
| 1454 | /* Allocate and initialize a new dynamic string object */ |
| 1455 | sqlite3_str *sqlite3_str_new(sqlite3 *db){ |
| 1456 | sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); |
| 1457 | if( p ){ |
| 1458 | sqlite3StrAccumInit(p, 0, 0, 0, |
| 1459 | db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); |
| 1460 | }else{ |
| 1461 | p = (sqlite3_str*)&sqlite3OomStr; |
| 1462 | } |
| 1463 | return p; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1468 | ** %-conversion extensions. |
| 1469 | */ |
| 1470 | char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ |
| 1471 | char *z; |
| 1472 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 1473 | StrAccum acc; |
| 1474 | assert( db!=0 ); |
| 1475 | sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), |
| 1476 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 1477 | acc.printfFlags = SQLITE_PRINTF_INTERNAL; |
| 1478 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1479 | z = sqlite3StrAccumFinish(&acc); |
| 1480 | if( acc.accError==SQLITE_NOMEM ){ |
| 1481 | sqlite3OomFault(db); |
| 1482 | } |
| 1483 | return z; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 1488 | ** %-conversion extensions. |
| 1489 | */ |
| 1490 | char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ |
| 1491 | va_list ap; |
| 1492 | char *z; |
| 1493 | va_start(ap, zFormat); |
| 1494 | z = sqlite3VMPrintf(db, zFormat, ap); |
| 1495 | va_end(ap); |
| 1496 | return z; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 1501 | ** %-conversion extensions. |
| 1502 | */ |
| 1503 | char *sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| 1504 | char *z; |
| 1505 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 1506 | StrAccum acc; |
| 1507 | |
| 1508 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1509 | if( zFormat==0 ){ |
| 1510 | (void)SQLITE_MISUSE_BKPT; |
| 1511 | return 0; |
| 1512 | } |
| 1513 | #endif |
| 1514 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1515 | if( sqlite3_initialize() ) return 0; |
| 1516 | #endif |
| 1517 | sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 1518 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1519 | z = sqlite3StrAccumFinish(&acc); |
| 1520 | return z; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
| 1525 | ** %-conversion extensions. |
| 1526 | */ |
| 1527 | char *sqlite3_mprintf(const char *zFormat, ...){ |
| 1528 | va_list ap; |
| 1529 | char *z; |
| 1530 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1531 | if( sqlite3_initialize() ) return 0; |
| 1532 | #endif |
| 1533 | va_start(ap, zFormat); |
| 1534 | z = sqlite3_vmprintf(zFormat, ap); |
| 1535 | va_end(ap); |
| 1536 | return z; |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | ** sqlite3_snprintf() works like snprintf() except that it ignores the |
| 1541 | ** current locale settings. This is important for SQLite because we |
| 1542 | ** are not able to use a "," as the decimal point in place of "." as |
| 1543 | ** specified by some locales. |
| 1544 | ** |
| 1545 | ** Oops: The first two arguments of sqlite3_snprintf() are backwards |
| 1546 | ** from the snprintf() standard. Unfortunately, it is too late to change |
| 1547 | ** this without breaking compatibility, so we just have to live with the |
| 1548 | ** mistake. |
| 1549 | ** |
| 1550 | ** sqlite3_vsnprintf() is the varargs version. |
| 1551 | */ |
| 1552 | char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ |
| 1553 | StrAccum acc; |
| 1554 | if( n<=0 ) return zBuf; |
| 1555 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1556 | if( zBuf==0 || zFormat==0 ) { |
| 1557 | (void)SQLITE_MISUSE_BKPT; |
| 1558 | if( zBuf ) zBuf[0] = 0; |
| 1559 | return zBuf; |
| 1560 | } |
| 1561 | #endif |
| 1562 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 1563 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1564 | zBuf[acc.nChar] = 0; |
| 1565 | return zBuf; |
| 1566 | } |
| 1567 | char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
| 1568 | StrAccum acc; |
| 1569 | va_list ap; |
| 1570 | if( n<=0 ) return zBuf; |
| 1571 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1572 | if( zBuf==0 || zFormat==0 ) { |
| 1573 | (void)SQLITE_MISUSE_BKPT; |
| 1574 | if( zBuf ) zBuf[0] = 0; |
| 1575 | return zBuf; |
| 1576 | } |
| 1577 | #endif |
| 1578 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 1579 | va_start(ap,zFormat); |
| 1580 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1581 | va_end(ap); |
| 1582 | zBuf[acc.nChar] = 0; |
| 1583 | return zBuf; |
| 1584 | } |
| 1585 | |
| 1586 | /* Maximum size of an sqlite3_log() message. */ |
| 1587 | #if defined(SQLITE_MAX_LOG_MESSAGE) |
| 1588 | /* Leave the definition as supplied */ |
| 1589 | #elif SQLITE_PRINT_BUF_SIZE*10>10000 |
| 1590 | # define SQLITE_MAX_LOG_MESSAGE 10000 |
| 1591 | #else |
| 1592 | # define SQLITE_MAX_LOG_MESSAGE (SQLITE_PRINT_BUF_SIZE*10) |
| 1593 | #endif |
| 1594 | |
| 1595 | /* |
| 1596 | ** This is the routine that actually formats the sqlite3_log() message. |
| 1597 | ** We house it in a separate routine from sqlite3_log() to avoid using |
| 1598 | ** stack space on small-stack systems when logging is disabled. |
| 1599 | ** |
| 1600 | ** sqlite3_log() must render into a static buffer. It cannot dynamically |
| 1601 | ** allocate memory because it might be called while the memory allocator |
| 1602 | ** mutex is held. |
| 1603 | ** |
| 1604 | ** sqlite3_str_vappendf() might ask for *temporary* memory allocations for |
| 1605 | ** certain format characters (%q) or for very large precisions or widths. |
| 1606 | ** Care must be taken that any sqlite3_log() calls that occur while the |
| 1607 | ** memory mutex is held do not use these mechanisms. |
| 1608 | */ |
| 1609 | static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ |
| 1610 | StrAccum acc; /* String accumulator */ |
| 1611 | char zMsg[SQLITE_MAX_LOG_MESSAGE]; /* Complete log message */ |
| 1612 | |
| 1613 | sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); |
| 1614 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1615 | sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, |
| 1616 | sqlite3StrAccumFinish(&acc)); |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | ** Format and write a message to the log if logging is enabled. |
| 1621 | */ |
| 1622 | void sqlite3_log(int iErrCode, const char *zFormat, ...){ |
| 1623 | va_list ap; /* Vararg list */ |
| 1624 | if( sqlite3GlobalConfig.xLog ){ |
| 1625 | va_start(ap, zFormat); |
| 1626 | renderLogMsg(iErrCode, zFormat, ap); |
| 1627 | va_end(ap); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 1632 | /* |
| 1633 | ** A version of printf() that understands %lld. Used for debugging. |
| 1634 | ** The printf() built into some versions of windows does not understand %lld |
| 1635 | ** and segfaults if you give it a long long int. |
| 1636 | */ |
| 1637 | void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 1638 | va_list ap; |
| 1639 | StrAccum acc; |
| 1640 | char zBuf[SQLITE_PRINT_BUF_SIZE*10]; |
| 1641 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 1642 | va_start(ap,zFormat); |
| 1643 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 1644 | va_end(ap); |
| 1645 | sqlite3StrAccumFinish(&acc); |
| 1646 | #ifdef SQLITE_OS_TRACE_PROC |
| 1647 | { |
| 1648 | extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); |
| 1649 | SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); |
| 1650 | } |
| 1651 | #else |
| 1652 | fprintf(stdout,"%s", zBuf); |
| 1653 | fflush(stdout); |
| 1654 | #endif |
| 1655 | } |
| 1656 | #endif |
| 1657 | |
| 1658 | |
| 1659 | /* |
| 1660 | ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument |
| 1661 | ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. |
| 1662 | */ |
| 1663 | void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ |
| 1664 | va_list ap; |
| 1665 | va_start(ap,zFormat); |
| 1666 | sqlite3_str_vappendf(p, zFormat, ap); |
| 1667 | va_end(ap); |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | /***************************************************************************** |
| 1672 | ** Reference counted string/blob storage |
| 1673 | *****************************************************************************/ |
| 1674 | |
| 1675 | /* |
| 1676 | ** Increase the reference count of the string by one. |
| 1677 | ** |
| 1678 | ** The input parameter is returned. |
| 1679 | */ |
| 1680 | char *sqlite3RCStrRef(char *z){ |
| 1681 | RCStr *p = (RCStr*)z; |
| 1682 | assert( p!=0 ); |
| 1683 | p--; |
| 1684 | p->nRCRef++; |
| 1685 | return z; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | ** Decrease the reference count by one. Free the string when the |
| 1690 | ** reference count reaches zero. |
| 1691 | */ |
| 1692 | void sqlite3RCStrUnref(void *z){ |
| 1693 | RCStr *p = (RCStr*)z; |
| 1694 | assert( p!=0 ); |
| 1695 | p--; |
| 1696 | assert( p->nRCRef>0 ); |
| 1697 | if( p->nRCRef>=2 ){ |
| 1698 | p->nRCRef--; |
| 1699 | }else{ |
| 1700 | sqlite3_free(p); |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | ** Create a new string that is capable of holding N bytes of text, not counting |
| 1706 | ** the zero byte at the end. The string is uninitialized. |
| 1707 | ** |
| 1708 | ** The reference count is initially 1. Call sqlite3RCStrUnref() to free the |
| 1709 | ** newly allocated string. |
| 1710 | ** |
| 1711 | ** This routine returns 0 on an OOM. |
| 1712 | */ |
| 1713 | char *sqlite3RCStrNew(u64 N){ |
| 1714 | RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 ); |
| 1715 | if( p==0 ) return 0; |
| 1716 | p->nRCRef = 1; |
| 1717 | return (char*)&p[1]; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | ** Change the size of the string so that it is able to hold N bytes. |
| 1722 | ** The string might be reallocated, so return the new allocation. |
| 1723 | */ |
| 1724 | char *sqlite3RCStrResize(char *z, u64 N){ |
| 1725 | RCStr *p = (RCStr*)z; |
| 1726 | RCStr *pNew; |
| 1727 | assert( p!=0 ); |
| 1728 | p--; |
| 1729 | assert( p->nRCRef==1 ); |
| 1730 | pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1); |
| 1731 | if( pNew==0 ){ |
| 1732 | sqlite3_free(p); |
| 1733 | return 0; |
| 1734 | }else{ |
| 1735 | return (char*)&pNew[1]; |
| 1736 | } |
| 1737 | } |