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