| 1 | #define LONGDOUBLE_TYPE long double |
| 2 | #include <stdint.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <math.h> |
| 7 | |
| 8 | typedef uint8_t u8; |
| 9 | typedef uint32_t u32; |
| 10 | typedef int64_t i64; |
| 11 | typedef uint64_t u64; |
| 12 | |
| 13 | typedef int64_t sqlite3_int64; |
| 14 | typedef uint64_t sqlite_uint64; |
| 15 | |
| 16 | #define sqlite3Malloc malloc |
| 17 | #define sqlite3_realloc64 realloc |
| 18 | #define sqlite3IsNaN isnan |
| 19 | |
| 20 | #define ArraySize(X) ((int)(sizeof(X) / sizeof(X[0]))) |
| 21 | #define LARGEST_INT64 (0xffffffff | (((i64)0x7fffffff) << 32)) |
| 22 | #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) |
| 23 | |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #define SQLITE_SKIP_UTF8(zIn) \ |
| 27 | { \ |
| 28 | if ((*(zIn++)) >= 0xc0) { \ |
| 29 | while ((*zIn & 0xc0) == 0x80) { \ |
| 30 | zIn++; \ |
| 31 | } \ |
| 32 | } \ |
| 33 | } |
| 34 | |
| 35 | #ifndef MAX |
| 36 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
| 37 | #endif |
| 38 | |
| 39 | #ifndef SQLITE_MAX_LENGTH |
| 40 | #define SQLITE_MAX_LENGTH 1000000000 |
| 41 | #endif |
| 42 | |
| 43 | struct StrAccum { |
| 44 | void *db; /* Optional database for lookaside. Can be NULL */ |
| 45 | char *zText; /* The string collected so far */ |
| 46 | u32 nAlloc; /* Amount of space allocated in zText */ |
| 47 | u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ |
| 48 | u32 nChar; /* Length of the string so far */ |
| 49 | u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ |
| 50 | u8 printfFlags; /* SQLITE_PRINTF flags below */ |
| 51 | }; |
| 52 | #define STRACCUM_NOMEM 1 |
| 53 | #define STRACCUM_TOOBIG 2 |
| 54 | #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */ |
| 55 | #define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */ |
| 56 | #define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */ |
| 57 | |
| 58 | #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED) != 0) |
| 59 | |
| 60 | typedef struct StrAccum StrAccum; |
| 61 | |
| 62 | void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N); |
| 63 | void sqlite3AppendChar(StrAccum *p, int N, char c); |
| 64 | void sqlite3StrAccumAppendAll(StrAccum *p, const char *z); |
| 65 | void sqlite3StrAccumReset(StrAccum *p); |
| 66 | |
| 67 | /* |
| 68 | ** The "printf" code that follows dates from the 1980's. It is in |
| 69 | ** the public domain. |
| 70 | ** |
| 71 | ************************************************************************** |
| 72 | ** |
| 73 | ** This file contains code for a set of "printf"-like routines. These |
| 74 | ** routines format strings much like the printf() from the standard C |
| 75 | ** library, though the implementation here has enhancements to support |
| 76 | ** SQLite. |
| 77 | */ |
| 78 | //#include "sqliteInt.h" |
| 79 | |
| 80 | /* |
| 81 | ** Conversion types fall into various categories as defined by the |
| 82 | ** following enumeration. |
| 83 | */ |
| 84 | #define etRADIX 0 /* non-decimal integer types. %x %o */ |
| 85 | #define etFLOAT 1 /* Floating point. %f */ |
| 86 | #define etEXP 2 /* Exponentional notation. %e and %E */ |
| 87 | #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ |
| 88 | #define etSIZE 4 /* Return number of characters processed so far. %n */ |
| 89 | #define etSTRING 5 /* Strings. %s */ |
| 90 | #define etDYNSTRING 6 /* Dynamically allocated strings. %z */ |
| 91 | #define etPERCENT 7 /* Percent symbol. %% */ |
| 92 | #define etCHARX 8 /* Characters. %c */ |
| 93 | /* The rest are extensions, not normally found in printf() */ |
| 94 | #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ |
| 95 | #define etSQLESCAPE2 \ |
| 96 | 10 /* Strings with '\'' doubled and enclosed in '', \ |
| 97 | NULL pointers replaced by SQL NULL. %Q */ |
| 98 | #define etTOKEN 11 /* a pointer to a Token structure */ |
| 99 | #define etSRCLIST 12 /* a pointer to a SrcList */ |
| 100 | #define etPOINTER 13 /* The %p conversion */ |
| 101 | #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ |
| 102 | #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 103 | #define etDECIMAL 16 /* %d or %u, but not %x, %o */ |
| 104 | |
| 105 | #define etINVALID 17 /* Any unrecognized conversion type */ |
| 106 | |
| 107 | /* |
| 108 | ** An "etByte" is an 8-bit unsigned value. |
| 109 | */ |
| 110 | typedef unsigned char etByte; |
| 111 | |
| 112 | /* |
| 113 | ** Each builtin conversion character (ex: the 'd' in "%d") is described |
| 114 | ** by an instance of the following structure |
| 115 | */ |
| 116 | typedef struct et_info { /* Information about each format field */ |
| 117 | char fmttype; /* The format field code letter */ |
| 118 | etByte base; /* The base for radix conversion */ |
| 119 | etByte flags; /* One or more of FLAG_ constants below */ |
| 120 | etByte type; /* Conversion paradigm */ |
| 121 | etByte charset; /* Offset into aDigits[] of the digits string */ |
| 122 | etByte prefix; /* Offset into aPrefix[] of the prefix string */ |
| 123 | } et_info; |
| 124 | |
| 125 | /* |
| 126 | ** Allowed values for et_info.flags |
| 127 | */ |
| 128 | #define FLAG_SIGNED 1 /* True if the value to convert is signed */ |
| 129 | #define FLAG_STRING 4 /* Allow infinite precision */ |
| 130 | |
| 131 | /* |
| 132 | ** The following table is searched linearly, so it is good to put the |
| 133 | ** most frequently used conversion types first. |
| 134 | */ |
| 135 | static const char aDigits[] = "0123456789ABCDEF0123456789abcdef" ; |
| 136 | static const char aPrefix[] = "-x0\000X0" ; |
| 137 | static const et_info fmtinfo[] = { |
| 138 | {'d', 10, 1, etDECIMAL, 0, 0}, |
| 139 | {'s', 0, 4, etSTRING, 0, 0}, |
| 140 | {'g', 0, 1, etGENERIC, 30, 0}, |
| 141 | {'z', 0, 4, etDYNSTRING, 0, 0}, |
| 142 | {'q', 0, 4, etSQLESCAPE, 0, 0}, |
| 143 | {'Q', 0, 4, etSQLESCAPE2, 0, 0}, |
| 144 | {'w', 0, 4, etSQLESCAPE3, 0, 0}, |
| 145 | {'c', 0, 0, etCHARX, 0, 0}, |
| 146 | {'o', 8, 0, etRADIX, 0, 2}, |
| 147 | {'u', 10, 0, etDECIMAL, 0, 0}, |
| 148 | {'x', 16, 0, etRADIX, 16, 1}, |
| 149 | {'X', 16, 0, etRADIX, 0, 4}, |
| 150 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 151 | {'f', 0, 1, etFLOAT, 0, 0}, |
| 152 | {'e', 0, 1, etEXP, 30, 0}, |
| 153 | {'E', 0, 1, etEXP, 14, 0}, |
| 154 | {'G', 0, 1, etGENERIC, 14, 0}, |
| 155 | #endif |
| 156 | {'i', 10, 1, etDECIMAL, 0, 0}, |
| 157 | {'n', 0, 0, etSIZE, 0, 0}, |
| 158 | {'%', 0, 0, etPERCENT, 0, 0}, |
| 159 | {'p', 16, 0, etPOINTER, 0, 1}, |
| 160 | |
| 161 | /* All the rest are undocumented and are for internal use only */ |
| 162 | {'T', 0, 0, etTOKEN, 0, 0}, |
| 163 | {'S', 0, 0, etSRCLIST, 0, 0}, |
| 164 | {'r', 10, 1, etORDINAL, 0, 0}, |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
| 169 | ** conversions will work. |
| 170 | */ |
| 171 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 172 | /* |
| 173 | ** "*val" is a double such that 0.1 <= *val < 10.0 |
| 174 | ** Return the ascii code for the leading digit of *val, then |
| 175 | ** multiply "*val" by 10.0 to renormalize. |
| 176 | ** |
| 177 | ** Example: |
| 178 | ** input: *val = 3.14159 |
| 179 | ** output: *val = 1.4159 function return = '3' |
| 180 | ** |
| 181 | ** The counter *cnt is incremented each time. After counter exceeds |
| 182 | ** 16 (the number of significant digits in a 64-bit float) '0' is |
| 183 | ** always returned. |
| 184 | */ |
| 185 | static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt) { |
| 186 | int digit; |
| 187 | LONGDOUBLE_TYPE d; |
| 188 | if ((*cnt) <= 0) |
| 189 | return '0'; |
| 190 | (*cnt)--; |
| 191 | digit = (int)*val; |
| 192 | d = digit; |
| 193 | digit += '0'; |
| 194 | *val = (*val - d) * 10.0; |
| 195 | return (char)digit; |
| 196 | } |
| 197 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 198 | |
| 199 | /* |
| 200 | ** Set the StrAccum object to an error mode. |
| 201 | */ |
| 202 | static void setStrAccumError(StrAccum *p, u8 eError) { |
| 203 | assert(eError == STRACCUM_NOMEM || eError == STRACCUM_TOOBIG); |
| 204 | p->accError = eError; |
| 205 | p->nAlloc = 0; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | ** On machines with a small stack size, you can redefine the |
| 210 | ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
| 211 | */ |
| 212 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 213 | #define SQLITE_PRINT_BUF_SIZE 70 |
| 214 | #endif |
| 215 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 216 | |
| 217 | /* |
| 218 | ** Render a string given by "fmt" into the StrAccum object. |
| 219 | */ |
| 220 | void sqlite3VXPrintf(StrAccum *pAccum, /* Accumulate results here */ |
| 221 | const char *fmt, /* Format string */ |
| 222 | va_list ap /* arguments */ |
| 223 | ) { |
| 224 | int c; /* Next character in the format string */ |
| 225 | char *bufpt; /* Pointer to the conversion buffer */ |
| 226 | int precision; /* Precision of the current field */ |
| 227 | int length; /* Length of the field */ |
| 228 | int idx; /* A general purpose loop counter */ |
| 229 | int width; /* Width of the current field */ |
| 230 | etByte flag_leftjustify; /* True if "-" flag is present */ |
| 231 | etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ |
| 232 | etByte flag_alternateform; /* True if "#" flag is present */ |
| 233 | etByte flag_altform2; /* True if "!" flag is present */ |
| 234 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
| 235 | etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ |
| 236 | etByte done; /* Loop termination flag */ |
| 237 | etByte cThousand; /* Thousands separator for %d and %u */ |
| 238 | etByte xtype = etINVALID; /* Conversion paradigm */ |
| 239 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 240 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 241 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 242 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 243 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 244 | char *zOut; /* Rendering buffer */ |
| 245 | int nOut; /* Size of the rendering buffer */ |
| 246 | char * = 0; /* Malloced memory used by some conversion */ |
| 247 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 248 | int exp, e2; /* exponent of real numbers */ |
| 249 | int nsd; /* Number of significant digits returned */ |
| 250 | double rounder; /* Used for rounding floating point values */ |
| 251 | etByte flag_dp; /* True if decimal point should be shown */ |
| 252 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 253 | #endif |
| 254 | // void *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 255 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 256 | |
| 257 | /* pAccum never starts out with an empty buffer that was obtained from |
| 258 | ** malloc(). This precondition is required by the mprintf("%z...") |
| 259 | ** optimization. */ |
| 260 | assert(pAccum->nChar > 0 || (pAccum->printfFlags & SQLITE_PRINTF_MALLOCED) == 0); |
| 261 | |
| 262 | bufpt = 0; |
| 263 | if ((pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC) != 0) { |
| 264 | // pArgList = va_arg(ap, PrintfArguments*); |
| 265 | // bArgList = 1; |
| 266 | assert(0); |
| 267 | } else { |
| 268 | bArgList = 0; |
| 269 | } |
| 270 | for (; (c = (*fmt)) != 0; ++fmt) { |
| 271 | if (c != '%') { |
| 272 | bufpt = (char *)fmt; |
| 273 | #if HAVE_STRCHRNUL |
| 274 | fmt = strchrnul(fmt, '%'); |
| 275 | #else |
| 276 | do { |
| 277 | fmt++; |
| 278 | } while (*fmt && *fmt != '%'); |
| 279 | #endif |
| 280 | sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); |
| 281 | if (*fmt == 0) |
| 282 | break; |
| 283 | } |
| 284 | if ((c = (*++fmt)) == 0) { |
| 285 | sqlite3StrAccumAppend(pAccum, "%" , 1); |
| 286 | break; |
| 287 | } |
| 288 | /* Find out what flags are present */ |
| 289 | flag_leftjustify = flag_prefix = cThousand = flag_alternateform = flag_altform2 = flag_zeropad = 0; |
| 290 | done = 0; |
| 291 | do { |
| 292 | switch (c) { |
| 293 | case '-': |
| 294 | flag_leftjustify = 1; |
| 295 | break; |
| 296 | case '+': |
| 297 | flag_prefix = '+'; |
| 298 | break; |
| 299 | case ' ': |
| 300 | flag_prefix = ' '; |
| 301 | break; |
| 302 | case '#': |
| 303 | flag_alternateform = 1; |
| 304 | break; |
| 305 | case '!': |
| 306 | flag_altform2 = 1; |
| 307 | break; |
| 308 | case '0': |
| 309 | flag_zeropad = 1; |
| 310 | break; |
| 311 | case ',': |
| 312 | cThousand = ','; |
| 313 | break; |
| 314 | default: |
| 315 | done = 1; |
| 316 | break; |
| 317 | } |
| 318 | } while (!done && (c = (*++fmt)) != 0); |
| 319 | /* Get the field width */ |
| 320 | if (c == '*') { |
| 321 | if (bArgList) { |
| 322 | assert(0); |
| 323 | // width = (int)getIntArg(pArgList); |
| 324 | } else { |
| 325 | width = va_arg(ap, int); |
| 326 | } |
| 327 | if (width < 0) { |
| 328 | flag_leftjustify = 1; |
| 329 | width = width >= -2147483647 ? -width : 0; |
| 330 | } |
| 331 | c = *++fmt; |
| 332 | } else { |
| 333 | unsigned wx = 0; |
| 334 | while (c >= '0' && c <= '9') { |
| 335 | wx = wx * 10 + c - '0'; |
| 336 | c = *++fmt; |
| 337 | } |
| 338 | // testcase( wx>0x7fffffff ); |
| 339 | width = wx & 0x7fffffff; |
| 340 | } |
| 341 | assert(width >= 0); |
| 342 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 343 | if (width > SQLITE_PRINTF_PRECISION_LIMIT) { |
| 344 | width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 345 | } |
| 346 | #endif |
| 347 | |
| 348 | /* Get the precision */ |
| 349 | if (c == '.') { |
| 350 | c = *++fmt; |
| 351 | if (c == '*') { |
| 352 | if (bArgList) { |
| 353 | assert(0); |
| 354 | // precision = (int)getIntArg(pArgList); |
| 355 | } else { |
| 356 | precision = va_arg(ap, int); |
| 357 | } |
| 358 | c = *++fmt; |
| 359 | if (precision < 0) { |
| 360 | precision = precision >= -2147483647 ? -precision : -1; |
| 361 | } |
| 362 | } else { |
| 363 | unsigned px = 0; |
| 364 | while (c >= '0' && c <= '9') { |
| 365 | px = px * 10 + c - '0'; |
| 366 | c = *++fmt; |
| 367 | } |
| 368 | // testcase( px>0x7fffffff ); |
| 369 | precision = px & 0x7fffffff; |
| 370 | } |
| 371 | } else { |
| 372 | precision = -1; |
| 373 | } |
| 374 | assert(precision >= (-1)); |
| 375 | #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 376 | if (precision > SQLITE_PRINTF_PRECISION_LIMIT) { |
| 377 | precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 378 | } |
| 379 | #endif |
| 380 | |
| 381 | /* Get the conversion type modifier */ |
| 382 | if (c == 'l') { |
| 383 | flag_long = 1; |
| 384 | c = *++fmt; |
| 385 | if (c == 'l') { |
| 386 | flag_long = 2; |
| 387 | c = *++fmt; |
| 388 | } |
| 389 | } else { |
| 390 | flag_long = 0; |
| 391 | } |
| 392 | /* Fetch the info entry for the field */ |
| 393 | infop = &fmtinfo[0]; |
| 394 | xtype = etINVALID; |
| 395 | for (idx = 0; idx < ArraySize(fmtinfo); idx++) { |
| 396 | if (c == fmtinfo[idx].fmttype) { |
| 397 | infop = &fmtinfo[idx]; |
| 398 | xtype = infop->type; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | ** At this point, variables are initialized as follows: |
| 405 | ** |
| 406 | ** flag_alternateform TRUE if a '#' is present. |
| 407 | ** flag_altform2 TRUE if a '!' is present. |
| 408 | ** flag_prefix '+' or ' ' or zero |
| 409 | ** flag_leftjustify TRUE if a '-' is present or if the |
| 410 | ** field width was negative. |
| 411 | ** flag_zeropad TRUE if the width began with 0. |
| 412 | ** flag_long 1 for "l", 2 for "ll" |
| 413 | ** width The specified field width. This is |
| 414 | ** always non-negative. Zero is the default. |
| 415 | ** precision The specified precision. The default |
| 416 | ** is -1. |
| 417 | ** xtype The class of the conversion. |
| 418 | ** infop Pointer to the appropriate info struct. |
| 419 | */ |
| 420 | switch (xtype) { |
| 421 | case etPOINTER: |
| 422 | flag_long = sizeof(char *) == sizeof(i64) ? 2 : sizeof(char *) == sizeof(long int) ? 1 : 0; |
| 423 | /* Fall through into the next case */ |
| 424 | case etORDINAL: |
| 425 | case etRADIX: |
| 426 | cThousand = 0; |
| 427 | /* Fall through into the next case */ |
| 428 | case etDECIMAL: |
| 429 | if (infop->flags & FLAG_SIGNED) { |
| 430 | i64 v; |
| 431 | if (bArgList) { |
| 432 | // v = getIntArg(pArgList); |
| 433 | assert(0); |
| 434 | } else if (flag_long) { |
| 435 | if (flag_long == 2) { |
| 436 | v = va_arg(ap, i64); |
| 437 | } else { |
| 438 | v = va_arg(ap, long int); |
| 439 | } |
| 440 | } else { |
| 441 | v = va_arg(ap, int); |
| 442 | } |
| 443 | if (v < 0) { |
| 444 | if (v == SMALLEST_INT64) { |
| 445 | longvalue = ((u64)1) << 63; |
| 446 | } else { |
| 447 | longvalue = -v; |
| 448 | } |
| 449 | prefix = '-'; |
| 450 | } else { |
| 451 | longvalue = v; |
| 452 | prefix = flag_prefix; |
| 453 | } |
| 454 | } else { |
| 455 | if (bArgList) { |
| 456 | assert(0); |
| 457 | // longvalue = (u64)getIntArg(pArgList); |
| 458 | } else if (flag_long) { |
| 459 | if (flag_long == 2) { |
| 460 | longvalue = va_arg(ap, u64); |
| 461 | } else { |
| 462 | longvalue = va_arg(ap, unsigned long int); |
| 463 | } |
| 464 | } else { |
| 465 | longvalue = va_arg(ap, unsigned int); |
| 466 | } |
| 467 | prefix = 0; |
| 468 | } |
| 469 | if (longvalue == 0) |
| 470 | flag_alternateform = 0; |
| 471 | if (flag_zeropad && precision < width - (prefix != 0)) { |
| 472 | precision = width - (prefix != 0); |
| 473 | } |
| 474 | if (precision < etBUFSIZE - 10 - etBUFSIZE / 3) { |
| 475 | nOut = etBUFSIZE; |
| 476 | zOut = buf; |
| 477 | } else { |
| 478 | u64 n = (u64)precision + 10 + precision / 3; |
| 479 | zOut = zExtra = sqlite3Malloc(n); |
| 480 | if (zOut == 0) { |
| 481 | setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 482 | return; |
| 483 | } |
| 484 | nOut = (int)n; |
| 485 | } |
| 486 | bufpt = &zOut[nOut - 1]; |
| 487 | if (xtype == etORDINAL) { |
| 488 | static const char zOrd[] = "thstndrd" ; |
| 489 | int x = (int)(longvalue % 10); |
| 490 | if (x >= 4 || (longvalue / 10) % 10 == 1) { |
| 491 | x = 0; |
| 492 | } |
| 493 | *(--bufpt) = zOrd[x * 2 + 1]; |
| 494 | *(--bufpt) = zOrd[x * 2]; |
| 495 | } |
| 496 | { |
| 497 | const char *cset = &aDigits[infop->charset]; |
| 498 | u8 base = infop->base; |
| 499 | do { /* Convert to ascii */ |
| 500 | *(--bufpt) = cset[longvalue % base]; |
| 501 | longvalue = longvalue / base; |
| 502 | } while (longvalue > 0); |
| 503 | } |
| 504 | length = (int)(&zOut[nOut - 1] - bufpt); |
| 505 | while (precision > length) { |
| 506 | *(--bufpt) = '0'; /* Zero pad */ |
| 507 | length++; |
| 508 | } |
| 509 | if (cThousand) { |
| 510 | int nn = (length - 1) / 3; /* Number of "," to insert */ |
| 511 | int ix = (length - 1) % 3 + 1; |
| 512 | bufpt -= nn; |
| 513 | for (idx = 0; nn > 0; idx++) { |
| 514 | bufpt[idx] = bufpt[idx + nn]; |
| 515 | ix--; |
| 516 | if (ix == 0) { |
| 517 | bufpt[++idx] = cThousand; |
| 518 | nn--; |
| 519 | ix = 3; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | if (prefix) |
| 524 | *(--bufpt) = prefix; /* Add sign */ |
| 525 | if (flag_alternateform && infop->prefix) { /* Add "0" or "0x" */ |
| 526 | const char *pre; |
| 527 | char x; |
| 528 | pre = &aPrefix[infop->prefix]; |
| 529 | for (; (x = (*pre)) != 0; pre++) |
| 530 | *(--bufpt) = x; |
| 531 | } |
| 532 | length = (int)(&zOut[nOut - 1] - bufpt); |
| 533 | break; |
| 534 | case etFLOAT: |
| 535 | case etEXP: |
| 536 | case etGENERIC: |
| 537 | if (bArgList) { |
| 538 | assert(0); |
| 539 | // realvalue = getDoubleArg(pArgList); |
| 540 | } else { |
| 541 | realvalue = va_arg(ap, double); |
| 542 | } |
| 543 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 544 | length = 0; |
| 545 | #else |
| 546 | if (precision < 0) |
| 547 | precision = 6; /* Set default precision */ |
| 548 | if (realvalue < 0.0) { |
| 549 | realvalue = -realvalue; |
| 550 | prefix = '-'; |
| 551 | } else { |
| 552 | prefix = flag_prefix; |
| 553 | } |
| 554 | if (xtype == etGENERIC && precision > 0) |
| 555 | precision--; |
| 556 | // testcase( precision>0xfff ); |
| 557 | for (idx = precision & 0xfff, rounder = 0.5; idx > 0; idx--, rounder *= 0.1) { |
| 558 | } |
| 559 | if (xtype == etFLOAT) |
| 560 | realvalue += rounder; |
| 561 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 562 | exp = 0; |
| 563 | if (sqlite3IsNaN((double)realvalue)) { |
| 564 | bufpt = "NaN" ; |
| 565 | length = 3; |
| 566 | break; |
| 567 | } |
| 568 | if (realvalue > 0.0) { |
| 569 | LONGDOUBLE_TYPE scale = 1.0; |
| 570 | while (realvalue >= 1e100 * scale && exp <= 350) { |
| 571 | scale *= 1e100; |
| 572 | exp += 100; |
| 573 | } |
| 574 | while (realvalue >= 1e10 * scale && exp <= 350) { |
| 575 | scale *= 1e10; |
| 576 | exp += 10; |
| 577 | } |
| 578 | while (realvalue >= 10.0 * scale && exp <= 350) { |
| 579 | scale *= 10.0; |
| 580 | exp++; |
| 581 | } |
| 582 | realvalue /= scale; |
| 583 | while (realvalue < 1e-8) { |
| 584 | realvalue *= 1e8; |
| 585 | exp -= 8; |
| 586 | } |
| 587 | while (realvalue < 1.0) { |
| 588 | realvalue *= 10.0; |
| 589 | exp--; |
| 590 | } |
| 591 | if (exp > 350) { |
| 592 | bufpt = buf; |
| 593 | buf[0] = prefix; |
| 594 | memcpy(buf + (prefix != 0), "Inf" , 4); |
| 595 | length = 3 + (prefix != 0); |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | bufpt = buf; |
| 600 | /* |
| 601 | ** If the field type is etGENERIC, then convert to either etEXP |
| 602 | ** or etFLOAT, as appropriate. |
| 603 | */ |
| 604 | if (xtype != etFLOAT) { |
| 605 | realvalue += rounder; |
| 606 | if (realvalue >= 10.0) { |
| 607 | realvalue *= 0.1; |
| 608 | exp++; |
| 609 | } |
| 610 | } |
| 611 | if (xtype == etGENERIC) { |
| 612 | flag_rtz = !flag_alternateform; |
| 613 | if (exp < -4 || exp > precision) { |
| 614 | xtype = etEXP; |
| 615 | } else { |
| 616 | precision = precision - exp; |
| 617 | xtype = etFLOAT; |
| 618 | } |
| 619 | } else { |
| 620 | flag_rtz = flag_altform2; |
| 621 | } |
| 622 | if (xtype == etEXP) { |
| 623 | e2 = 0; |
| 624 | } else { |
| 625 | e2 = exp; |
| 626 | } |
| 627 | if (MAX(e2, 0) + (i64)precision + (i64)width > etBUFSIZE - 15) { |
| 628 | bufpt = zExtra = sqlite3Malloc(MAX(e2, 0) + (i64)precision + (i64)width + 15); |
| 629 | if (bufpt == 0) { |
| 630 | setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 631 | return; |
| 632 | } |
| 633 | } |
| 634 | zOut = bufpt; |
| 635 | nsd = 16 + flag_altform2 * 10; |
| 636 | flag_dp = (precision > 0 ? 1 : 0) | flag_alternateform | flag_altform2; |
| 637 | /* The sign in front of the number */ |
| 638 | if (prefix) { |
| 639 | *(bufpt++) = prefix; |
| 640 | } |
| 641 | /* Digits prior to the decimal point */ |
| 642 | if (e2 < 0) { |
| 643 | *(bufpt++) = '0'; |
| 644 | } else { |
| 645 | for (; e2 >= 0; e2--) { |
| 646 | *(bufpt++) = et_getdigit(&realvalue, &nsd); |
| 647 | } |
| 648 | } |
| 649 | /* The decimal point */ |
| 650 | if (flag_dp) { |
| 651 | *(bufpt++) = '.'; |
| 652 | } |
| 653 | /* "0" digits after the decimal point but before the first |
| 654 | ** significant digit of the number */ |
| 655 | for (e2++; e2 < 0; precision--, e2++) { |
| 656 | assert(precision > 0); |
| 657 | *(bufpt++) = '0'; |
| 658 | } |
| 659 | /* Significant digits after the decimal point */ |
| 660 | while ((precision--) > 0) { |
| 661 | *(bufpt++) = et_getdigit(&realvalue, &nsd); |
| 662 | } |
| 663 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 664 | if (flag_rtz && flag_dp) { |
| 665 | while (bufpt[-1] == '0') |
| 666 | *(--bufpt) = 0; |
| 667 | assert(bufpt > zOut); |
| 668 | if (bufpt[-1] == '.') { |
| 669 | if (flag_altform2) { |
| 670 | *(bufpt++) = '0'; |
| 671 | } else { |
| 672 | *(--bufpt) = 0; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | /* Add the "eNNN" suffix */ |
| 677 | if (xtype == etEXP) { |
| 678 | *(bufpt++) = aDigits[infop->charset]; |
| 679 | if (exp < 0) { |
| 680 | *(bufpt++) = '-'; |
| 681 | exp = -exp; |
| 682 | } else { |
| 683 | *(bufpt++) = '+'; |
| 684 | } |
| 685 | if (exp >= 100) { |
| 686 | *(bufpt++) = (char)((exp / 100) + '0'); /* 100's digit */ |
| 687 | exp %= 100; |
| 688 | } |
| 689 | *(bufpt++) = (char)(exp / 10 + '0'); /* 10's digit */ |
| 690 | *(bufpt++) = (char)(exp % 10 + '0'); /* 1's digit */ |
| 691 | } |
| 692 | *bufpt = 0; |
| 693 | |
| 694 | /* The converted number is in buf[] and zero terminated. Output it. |
| 695 | ** Note that the number is in the usual order, not reversed as with |
| 696 | ** integer conversions. */ |
| 697 | length = (int)(bufpt - zOut); |
| 698 | bufpt = zOut; |
| 699 | |
| 700 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 701 | ** set and we are not left justified */ |
| 702 | if (flag_zeropad && !flag_leftjustify && length < width) { |
| 703 | int i; |
| 704 | int nPad = width - length; |
| 705 | for (i = width; i >= nPad; i--) { |
| 706 | bufpt[i] = bufpt[i - nPad]; |
| 707 | } |
| 708 | i = prefix != 0; |
| 709 | while (nPad--) |
| 710 | bufpt[i++] = '0'; |
| 711 | length = width; |
| 712 | } |
| 713 | #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ |
| 714 | break; |
| 715 | case etSIZE: |
| 716 | if (!bArgList) { |
| 717 | *(va_arg(ap, int *)) = pAccum->nChar; |
| 718 | } |
| 719 | length = width = 0; |
| 720 | break; |
| 721 | case etPERCENT: |
| 722 | buf[0] = '%'; |
| 723 | bufpt = buf; |
| 724 | length = 1; |
| 725 | break; |
| 726 | case etCHARX: |
| 727 | if (bArgList) { |
| 728 | assert(0); |
| 729 | /* |
| 730 | bufpt = getTextArg(pArgList); |
| 731 | length = 1; |
| 732 | if( bufpt ){ |
| 733 | buf[0] = c = *(bufpt++); |
| 734 | if( (c&0xc0)==0xc0 ){ |
| 735 | while( length<4 && (bufpt[0]&0xc0)==0x80 ){ |
| 736 | buf[length++] = *(bufpt++); |
| 737 | } |
| 738 | } |
| 739 | }else{ |
| 740 | buf[0] = 0; |
| 741 | } */ |
| 742 | } else { |
| 743 | unsigned int ch = va_arg(ap, unsigned int); |
| 744 | if (ch < 0x00080) { |
| 745 | buf[0] = ch & 0xff; |
| 746 | length = 1; |
| 747 | } else if (ch < 0x00800) { |
| 748 | buf[0] = 0xc0 + (u8)((ch >> 6) & 0x1f); |
| 749 | buf[1] = 0x80 + (u8)(ch & 0x3f); |
| 750 | length = 2; |
| 751 | } else if (ch < 0x10000) { |
| 752 | buf[0] = 0xe0 + (u8)((ch >> 12) & 0x0f); |
| 753 | buf[1] = 0x80 + (u8)((ch >> 6) & 0x3f); |
| 754 | buf[2] = 0x80 + (u8)(ch & 0x3f); |
| 755 | length = 3; |
| 756 | } else { |
| 757 | buf[0] = 0xf0 + (u8)((ch >> 18) & 0x07); |
| 758 | buf[1] = 0x80 + (u8)((ch >> 12) & 0x3f); |
| 759 | buf[2] = 0x80 + (u8)((ch >> 6) & 0x3f); |
| 760 | buf[3] = 0x80 + (u8)(ch & 0x3f); |
| 761 | length = 4; |
| 762 | } |
| 763 | } |
| 764 | if (precision > 1) { |
| 765 | width -= precision - 1; |
| 766 | if (width > 1 && !flag_leftjustify) { |
| 767 | sqlite3AppendChar(pAccum, width - 1, ' '); |
| 768 | width = 0; |
| 769 | } |
| 770 | while (precision-- > 1) { |
| 771 | sqlite3StrAccumAppend(pAccum, buf, length); |
| 772 | } |
| 773 | } |
| 774 | bufpt = buf; |
| 775 | flag_altform2 = 1; |
| 776 | goto adjust_width_for_utf8; |
| 777 | case etSTRING: |
| 778 | case etDYNSTRING: |
| 779 | if (bArgList) { |
| 780 | assert(0); |
| 781 | // bufpt = getTextArg(pArgList); |
| 782 | // xtype = etSTRING; |
| 783 | } else { |
| 784 | bufpt = va_arg(ap, char *); |
| 785 | } |
| 786 | if (bufpt == 0) { |
| 787 | bufpt = "" ; |
| 788 | } else if (xtype == etDYNSTRING) { |
| 789 | // |
| 790 | // if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){ |
| 791 | // /* Special optimization for sqlite3_mprintf("%z..."): |
| 792 | // ** Extend an existing memory allocation rather than creating |
| 793 | // ** a new one. */ |
| 794 | // assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); |
| 795 | // pAccum->zText = bufpt; |
| 796 | // pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); |
| 797 | // pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); |
| 798 | // pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 799 | // length = 0; |
| 800 | // break; |
| 801 | // } |
| 802 | zExtra = bufpt; |
| 803 | } |
| 804 | if (precision >= 0) { |
| 805 | if (flag_altform2) { |
| 806 | /* Set length to the number of bytes needed in order to display |
| 807 | ** precision characters */ |
| 808 | unsigned char *z = (unsigned char *)bufpt; |
| 809 | while (precision-- > 0 && z[0]) { |
| 810 | SQLITE_SKIP_UTF8(z); |
| 811 | } |
| 812 | length = (int)(z - (unsigned char *)bufpt); |
| 813 | } else { |
| 814 | for (length = 0; length < precision && bufpt[length]; length++) { |
| 815 | } |
| 816 | } |
| 817 | } else { |
| 818 | length = 0x7fffffff & (int)strlen(bufpt); |
| 819 | } |
| 820 | adjust_width_for_utf8: |
| 821 | if (flag_altform2 && width > 0) { |
| 822 | /* Adjust width to account for extra bytes in UTF-8 characters */ |
| 823 | int ii = length - 1; |
| 824 | while (ii >= 0) |
| 825 | if ((bufpt[ii--] & 0xc0) == 0x80) |
| 826 | width++; |
| 827 | } |
| 828 | break; |
| 829 | case etSQLESCAPE: /* %q: Escape ' characters */ |
| 830 | case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ |
| 831 | case etSQLESCAPE3: { /* %w: Escape " characters */ |
| 832 | int i, j, k, n, isnull; |
| 833 | int needQuote; |
| 834 | char ch; |
| 835 | char q = ((xtype == etSQLESCAPE3) ? '"' : '\''); /* Quote character */ |
| 836 | char *escarg; |
| 837 | |
| 838 | if (bArgList) { |
| 839 | assert(0); |
| 840 | // escarg = getTextArg(pArgList); |
| 841 | } else { |
| 842 | escarg = va_arg(ap, char *); |
| 843 | } |
| 844 | isnull = escarg == 0; |
| 845 | if (isnull) |
| 846 | escarg = (xtype == etSQLESCAPE2 ? "NULL" : "(NULL)" ); |
| 847 | /* For %q, %Q, and %w, the precision is the number of byte (or |
| 848 | ** characters if the ! flags is present) to use from the input. |
| 849 | ** Because of the extra quoting characters inserted, the number |
| 850 | ** of output characters may be larger than the precision. |
| 851 | */ |
| 852 | k = precision; |
| 853 | for (i = n = 0; k != 0 && (ch = escarg[i]) != 0; i++, k--) { |
| 854 | if (ch == q) |
| 855 | n++; |
| 856 | if (flag_altform2 && (ch & 0xc0) == 0xc0) { |
| 857 | while ((escarg[i + 1] & 0xc0) == 0x80) { |
| 858 | i++; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | needQuote = !isnull && xtype == etSQLESCAPE2; |
| 863 | n += i + 3; |
| 864 | if (n > etBUFSIZE) { |
| 865 | bufpt = zExtra = sqlite3Malloc(n); |
| 866 | if (bufpt == 0) { |
| 867 | setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 868 | return; |
| 869 | } |
| 870 | } else { |
| 871 | bufpt = buf; |
| 872 | } |
| 873 | j = 0; |
| 874 | if (needQuote) |
| 875 | bufpt[j++] = q; |
| 876 | k = i; |
| 877 | for (i = 0; i < k; i++) { |
| 878 | bufpt[j++] = ch = escarg[i]; |
| 879 | if (ch == q) |
| 880 | bufpt[j++] = ch; |
| 881 | } |
| 882 | if (needQuote) |
| 883 | bufpt[j++] = q; |
| 884 | bufpt[j] = 0; |
| 885 | length = j; |
| 886 | goto adjust_width_for_utf8; |
| 887 | } |
| 888 | case etTOKEN: { |
| 889 | /* |
| 890 | Token *pToken; |
| 891 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 892 | pToken = va_arg(ap, Token*); |
| 893 | assert( bArgList==0 ); |
| 894 | if( pToken && pToken->n ){ |
| 895 | sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); |
| 896 | } |
| 897 | length = width = 0; |
| 898 | */ |
| 899 | assert(0); |
| 900 | break; |
| 901 | } |
| 902 | case etSRCLIST: { |
| 903 | /* |
| 904 | SrcList *pSrc; |
| 905 | int k; |
| 906 | struct SrcList_item *pItem; |
| 907 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 908 | pSrc = va_arg(ap, SrcList*); |
| 909 | k = va_arg(ap, int); |
| 910 | pItem = &pSrc->a[k]; |
| 911 | assert( bArgList==0 ); |
| 912 | assert( k>=0 && k<pSrc->nSrc ); |
| 913 | if( pItem->zDatabase ){ |
| 914 | sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); |
| 915 | sqlite3StrAccumAppend(pAccum, ".", 1); |
| 916 | } |
| 917 | sqlite3StrAccumAppendAll(pAccum, pItem->zName); |
| 918 | length = width = 0; |
| 919 | */ |
| 920 | assert(0); |
| 921 | break; |
| 922 | } |
| 923 | default: { |
| 924 | assert(xtype == etINVALID); |
| 925 | return; |
| 926 | } |
| 927 | } /* End switch over the format type */ |
| 928 | /* |
| 929 | ** The text of the conversion is pointed to by "bufpt" and is |
| 930 | ** "length" characters long. The field width is "width". Do |
| 931 | ** the output. Both length and width are in bytes, not characters, |
| 932 | ** at this point. If the "!" flag was present on string conversions |
| 933 | ** indicating that width and precision should be expressed in characters, |
| 934 | ** then the values have been translated prior to reaching this point. |
| 935 | */ |
| 936 | width -= length; |
| 937 | if (width > 0) { |
| 938 | if (!flag_leftjustify) |
| 939 | sqlite3AppendChar(pAccum, width, ' '); |
| 940 | sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 941 | if (flag_leftjustify) |
| 942 | sqlite3AppendChar(pAccum, width, ' '); |
| 943 | } else { |
| 944 | sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 945 | } |
| 946 | |
| 947 | if (zExtra) { |
| 948 | free(zExtra); |
| 949 | zExtra = 0; |
| 950 | } |
| 951 | } /* End for loop over the format string */ |
| 952 | } /* End of function */ |
| 953 | |
| 954 | /* |
| 955 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 956 | ** able to accept at least N more bytes of text. |
| 957 | ** |
| 958 | ** Return the number of bytes of text that StrAccum is able to accept |
| 959 | ** after the attempted enlargement. The value returned might be zero. |
| 960 | */ |
| 961 | static int sqlite3StrAccumEnlarge(StrAccum *p, int N) { |
| 962 | char *zNew; |
| 963 | assert(p->nChar + (i64)N >= p->nAlloc); /* Only called if really needed */ |
| 964 | if (p->accError) { |
| 965 | // testcase(p->accError==STRACCUM_TOOBIG); |
| 966 | // testcase(p->accError==STRACCUM_NOMEM); |
| 967 | return 0; |
| 968 | } |
| 969 | if (p->mxAlloc == 0) { |
| 970 | N = p->nAlloc - p->nChar - 1; |
| 971 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 972 | return N; |
| 973 | } else { |
| 974 | char *zOld = isMalloced(p) ? p->zText : 0; |
| 975 | i64 szNew = p->nChar; |
| 976 | szNew += N + 1; |
| 977 | if (szNew + p->nChar <= p->mxAlloc) { |
| 978 | /* Force exponential buffer size growth as long as it does not overflow, |
| 979 | ** to avoid having to call this routine too often */ |
| 980 | szNew += p->nChar; |
| 981 | } |
| 982 | if (szNew > p->mxAlloc) { |
| 983 | sqlite3StrAccumReset(p); |
| 984 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 985 | return 0; |
| 986 | } else { |
| 987 | p->nAlloc = (int)szNew; |
| 988 | } |
| 989 | if (p->db) { |
| 990 | assert(0); |
| 991 | // zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 992 | } else { |
| 993 | zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 994 | } |
| 995 | if (zNew) { |
| 996 | assert(p->zText != 0 || p->nChar == 0); |
| 997 | if (!isMalloced(p) && p->nChar > 0) |
| 998 | memcpy(zNew, p->zText, p->nChar); |
| 999 | p->zText = zNew; |
| 1000 | // p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 1001 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1002 | } else { |
| 1003 | sqlite3StrAccumReset(p); |
| 1004 | setStrAccumError(p, STRACCUM_NOMEM); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | } |
| 1008 | return N; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | ** Append N copies of character c to the given string buffer. |
| 1013 | */ |
| 1014 | void sqlite3AppendChar(StrAccum *p, int N, char c) { |
| 1015 | // testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 1016 | if (p->nChar + (i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N)) <= 0) { |
| 1017 | return; |
| 1018 | } |
| 1019 | while ((N--) > 0) |
| 1020 | p->zText[p->nChar++] = c; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 1025 | ** So enlarge if first, then do the append. |
| 1026 | ** |
| 1027 | ** This is a helper routine to sqlite3StrAccumAppend() that does special-case |
| 1028 | ** work (enlarging the buffer) using tail recursion, so that the |
| 1029 | ** sqlite3StrAccumAppend() routine can use fast calling semantics. |
| 1030 | */ |
| 1031 | static void enlargeAndAppend(StrAccum *p, const char *z, int N) { |
| 1032 | N = sqlite3StrAccumEnlarge(p, N); |
| 1033 | if (N > 0) { |
| 1034 | memcpy(&p->zText[p->nChar], z, N); |
| 1035 | p->nChar += N; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 1041 | ** size of the memory allocation for StrAccum if necessary. |
| 1042 | */ |
| 1043 | void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N) { |
| 1044 | assert(z != 0 || N == 0); |
| 1045 | assert(p->zText != 0 || p->nChar == 0 || p->accError); |
| 1046 | assert(N >= 0); |
| 1047 | assert(p->accError == 0 || p->nAlloc == 0); |
| 1048 | if (p->nChar + N >= p->nAlloc) { |
| 1049 | enlargeAndAppend(p, z, N); |
| 1050 | } else if (N) { |
| 1051 | assert(p->zText); |
| 1052 | p->nChar += N; |
| 1053 | memcpy(&p->zText[p->nChar - N], z, N); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | ** Append the complete text of zero-terminated string z[] to the p string. |
| 1059 | */ |
| 1060 | void sqlite3StrAccumAppendAll(StrAccum *p, const char *z) { |
| 1061 | sqlite3StrAccumAppend(p, z, strlen(z)); |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | ** Finish off a string by making sure it is zero-terminated. |
| 1066 | ** Return a pointer to the resulting string. Return a NULL |
| 1067 | ** pointer if any kind of error was encountered. |
| 1068 | */ |
| 1069 | static char *strAccumFinishRealloc(StrAccum *p) { |
| 1070 | char *zText; |
| 1071 | assert(p->mxAlloc > 0 && !isMalloced(p)); |
| 1072 | zText = malloc(p->nChar + 1); |
| 1073 | if (zText) { |
| 1074 | memcpy(zText, p->zText, p->nChar + 1); |
| 1075 | p->printfFlags |= SQLITE_PRINTF_MALLOCED; |
| 1076 | } else { |
| 1077 | setStrAccumError(p, STRACCUM_NOMEM); |
| 1078 | } |
| 1079 | p->zText = zText; |
| 1080 | return zText; |
| 1081 | } |
| 1082 | char *sqlite3StrAccumFinish(StrAccum *p) { |
| 1083 | if (p->zText) { |
| 1084 | p->zText[p->nChar] = 0; |
| 1085 | if (p->mxAlloc > 0 && !isMalloced(p)) { |
| 1086 | return strAccumFinishRealloc(p); |
| 1087 | } |
| 1088 | } |
| 1089 | return p->zText; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 1094 | */ |
| 1095 | void sqlite3StrAccumReset(StrAccum *p) { |
| 1096 | if (isMalloced(p)) { |
| 1097 | // sqlite3DbFree(p->db, p->zText); |
| 1098 | free(p->zText); |
| 1099 | p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; |
| 1100 | } |
| 1101 | p->zText = 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | ** Initialize a string accumulator. |
| 1106 | ** |
| 1107 | ** p: The accumulator to be initialized. |
| 1108 | ** db: Pointer to a database connection. May be NULL. Lookaside |
| 1109 | ** memory is used if not NULL. db->mallocFailed is set appropriately |
| 1110 | ** when not NULL. |
| 1111 | ** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 1112 | ** is malloced. |
| 1113 | ** n: Size of zBase in bytes. If total space requirements never exceed |
| 1114 | ** n then no memory allocations ever occur. |
| 1115 | ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 1116 | ** allocations will ever occur. |
| 1117 | */ |
| 1118 | void sqlite3StrAccumInit(StrAccum *p, void *db, char *zBase, int n, int mx) { |
| 1119 | p->zText = zBase; |
| 1120 | assert(!db); |
| 1121 | p->db = db; |
| 1122 | p->nAlloc = n; |
| 1123 | p->mxAlloc = mx; |
| 1124 | p->nChar = 0; |
| 1125 | p->accError = 0; |
| 1126 | p->printfFlags = 0; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 1131 | ** %-conversion extensions. |
| 1132 | */ |
| 1133 | char *sqlite3_vmprintf(const char *zFormat, va_list ap) { |
| 1134 | char *z; |
| 1135 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 1136 | StrAccum acc; |
| 1137 | |
| 1138 | sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 1139 | sqlite3VXPrintf(&acc, zFormat, ap); |
| 1140 | z = sqlite3StrAccumFinish(&acc); |
| 1141 | return z; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
| 1146 | ** %-conversion extensions. |
| 1147 | */ |
| 1148 | char *sqlite3_mprintf(const char *zFormat, ...) { |
| 1149 | va_list ap; |
| 1150 | char *z; |
| 1151 | va_start(ap, zFormat); |
| 1152 | z = sqlite3_vmprintf(zFormat, ap); |
| 1153 | va_end(ap); |
| 1154 | return z; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | ** sqlite3_snprintf() works like snprintf() except that it ignores the |
| 1159 | ** current locale settings. This is important for SQLite because we |
| 1160 | ** are not able to use a "," as the decimal point in place of "." as |
| 1161 | ** specified by some locales. |
| 1162 | ** |
| 1163 | ** Oops: The first two arguments of sqlite3_snprintf() are backwards |
| 1164 | ** from the snprintf() standard. Unfortunately, it is too late to change |
| 1165 | ** this without breaking compatibility, so we just have to live with the |
| 1166 | ** mistake. |
| 1167 | ** |
| 1168 | ** sqlite3_vsnprintf() is the varargs version. |
| 1169 | */ |
| 1170 | char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap) { |
| 1171 | StrAccum acc; |
| 1172 | if (n <= 0) |
| 1173 | return zBuf; |
| 1174 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1175 | if (zBuf == 0 || zFormat == 0) { |
| 1176 | (void)SQLITE_MISUSE_BKPT; |
| 1177 | if (zBuf) |
| 1178 | zBuf[0] = 0; |
| 1179 | return zBuf; |
| 1180 | } |
| 1181 | #endif |
| 1182 | sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 1183 | sqlite3VXPrintf(&acc, zFormat, ap); |
| 1184 | zBuf[acc.nChar] = 0; |
| 1185 | return zBuf; |
| 1186 | } |
| 1187 | char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...) { |
| 1188 | char *z; |
| 1189 | va_list ap; |
| 1190 | va_start(ap, zFormat); |
| 1191 | z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); |
| 1192 | va_end(ap); |
| 1193 | return z; |
| 1194 | } |
| 1195 | |