blob: f1634799ba5c67fd9e38109c0bc3140f0bb7f2c2 [file] [log] [blame]
drha18c5682000-10-08 22:20:57 +00001/*
2** The "printf" code that follows dates from the 1980's. It is in
drh38b41492015-06-08 15:08:15 +00003** the public domain.
drhe84a3062004-02-02 12:29:25 +00004**
5**************************************************************************
drha18c5682000-10-08 22:20:57 +00006**
drhed1fddf2011-10-12 18:52:59 +00007** 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
mistachkinf5b5f9b2015-06-08 17:42:57 +000010** SQLite.
drha18c5682000-10-08 22:20:57 +000011*/
12#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000013
drha18c5682000-10-08 22:20:57 +000014/*
drha18c5682000-10-08 22:20:57 +000015** Conversion types fall into various categories as defined by the
16** following enumeration.
17*/
drh2c338a92017-02-10 19:38:36 +000018#define etRADIX 0 /* non-decimal integer types. %x %o */
drhad5a9d72016-05-05 11:53:12 +000019#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 */
drha18c5682000-10-08 22:20:57 +000027/* The rest are extensions, not normally found in printf() */
drhad5a9d72016-05-05 11:53:12 +000028#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
29#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
chw0cfcf3f2002-06-16 04:55:48 +000030 NULL pointers replaced by SQL NULL. %Q */
drhad5a9d72016-05-05 11:53:12 +000031#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 */
drh2c338a92017-02-10 19:38:36 +000036#define etDECIMAL 16 /* %d or %u, but not %x, %o */
drhe84a3062004-02-02 12:29:25 +000037
drh2c338a92017-02-10 19:38:36 +000038#define etINVALID 17 /* Any unrecognized conversion type */
drh874ba042009-04-08 16:10:04 +000039
drhe84a3062004-02-02 12:29:25 +000040
41/*
42** An "etByte" is an 8-bit unsigned value.
43*/
44typedef unsigned char etByte;
drha18c5682000-10-08 22:20:57 +000045
46/*
47** Each builtin conversion character (ex: the 'd' in "%d") is described
48** by an instance of the following structure
49*/
50typedef struct et_info { /* Information about each format field */
drhe84a3062004-02-02 12:29:25 +000051 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 */
drh76ff3a02004-09-24 22:32:30 +000055 etByte charset; /* Offset into aDigits[] of the digits string */
56 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:57 +000057} et_info;
58
59/*
drhe84a3062004-02-02 12:29:25 +000060** Allowed values for et_info.flags
61*/
drh2c338a92017-02-10 19:38:36 +000062#define FLAG_SIGNED 1 /* True if the value to convert is signed */
63#define FLAG_STRING 4 /* Allow infinite precision */
drhe84a3062004-02-02 12:29:25 +000064
65
66/*
drha18c5682000-10-08 22:20:57 +000067** The following table is searched linearly, so it is good to put the
68** most frequently used conversion types first.
69*/
drh76ff3a02004-09-24 22:32:30 +000070static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
71static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:16 +000072static const et_info fmtinfo[] = {
drh2c338a92017-02-10 19:38:36 +000073 { 'd', 10, 1, etDECIMAL, 0, 0 },
drh4794f732004-11-05 17:17:50 +000074 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:14 +000075 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:33 +000076 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drh4794f732004-11-05 17:17:50 +000077 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
78 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
danielk1977f3b863e2007-06-24 06:32:17 +000079 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000080 { 'c', 0, 0, etCHARX, 0, 0 },
81 { 'o', 8, 0, etRADIX, 0, 2 },
drh2c338a92017-02-10 19:38:36 +000082 { 'u', 10, 0, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000083 { 'x', 16, 0, etRADIX, 16, 1 },
84 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:49 +000085#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:30 +000086 { 'f', 0, 1, etFLOAT, 0, 0 },
87 { 'e', 0, 1, etEXP, 30, 0 },
88 { 'E', 0, 1, etEXP, 14, 0 },
drh76ff3a02004-09-24 22:32:30 +000089 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:49 +000090#endif
drh2c338a92017-02-10 19:38:36 +000091 { 'i', 10, 1, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:30 +000092 { 'n', 0, 0, etSIZE, 0, 0 },
93 { '%', 0, 0, etPERCENT, 0, 0 },
94 { 'p', 16, 0, etPOINTER, 0, 1 },
drh7e3ff5d2009-04-08 11:49:42 +000095
drh8236f682017-01-04 00:26:28 +000096 /* 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 },
drha18c5682000-10-08 22:20:57 +0000100};
drha18c5682000-10-08 22:20:57 +0000101
drha0ed86b2019-05-24 22:58:16 +0000102/* Floating point constants used for rounding */
103static const double arRound[] = {
104 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
105 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
106};
107
drha18c5682000-10-08 22:20:57 +0000108/*
drhb37df7b2005-10-13 02:09:49 +0000109** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
drha18c5682000-10-08 22:20:57 +0000110** conversions will work.
111*/
drhb37df7b2005-10-13 02:09:49 +0000112#ifndef SQLITE_OMIT_FLOATING_POINT
drha18c5682000-10-08 22:20:57 +0000113/*
114** "*val" is a double such that 0.1 <= *val < 10.0
115** Return the ascii code for the leading digit of *val, then
116** multiply "*val" by 10.0 to renormalize.
117**
118** Example:
119** input: *val = 3.14159
120** output: *val = 1.4159 function return = '3'
121**
122** The counter *cnt is incremented each time. After counter exceeds
123** 16 (the number of significant digits in a 64-bit float) '0' is
124** always returned.
125*/
drhea678832008-12-10 19:26:22 +0000126static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
drha18c5682000-10-08 22:20:57 +0000127 int digit;
drh384eef32004-01-07 03:04:27 +0000128 LONGDOUBLE_TYPE d;
drh72b3fbc2012-06-19 03:11:25 +0000129 if( (*cnt)<=0 ) return '0';
130 (*cnt)--;
drha18c5682000-10-08 22:20:57 +0000131 digit = (int)*val;
132 d = digit;
133 digit += '0';
134 *val = (*val - d)*10.0;
drhea678832008-12-10 19:26:22 +0000135 return (char)digit;
drha18c5682000-10-08 22:20:57 +0000136}
drhb37df7b2005-10-13 02:09:49 +0000137#endif /* SQLITE_OMIT_FLOATING_POINT */
drha18c5682000-10-08 22:20:57 +0000138
drh79158e12005-09-06 21:40:45 +0000139/*
drha6353a32013-12-09 19:03:26 +0000140** Set the StrAccum object to an error mode.
141*/
drha5c14162013-12-17 15:03:06 +0000142static void setStrAccumError(StrAccum *p, u8 eError){
drh0cdbe1a2018-05-09 13:46:26 +0000143 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
drha6353a32013-12-09 19:03:26 +0000144 p->accError = eError;
drh255a81f2019-02-22 15:42:10 +0000145 if( p->mxAlloc ) sqlite3_str_reset(p);
drhc3dcdba2019-04-09 21:32:46 +0000146 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
drha6353a32013-12-09 19:03:26 +0000147}
148
149/*
drha5c14162013-12-17 15:03:06 +0000150** Extra argument values from a PrintfArguments object
151*/
152static sqlite3_int64 getIntArg(PrintfArguments *p){
153 if( p->nArg<=p->nUsed ) return 0;
154 return sqlite3_value_int64(p->apArg[p->nUsed++]);
155}
156static double getDoubleArg(PrintfArguments *p){
157 if( p->nArg<=p->nUsed ) return 0.0;
158 return sqlite3_value_double(p->apArg[p->nUsed++]);
159}
160static char *getTextArg(PrintfArguments *p){
161 if( p->nArg<=p->nUsed ) return 0;
162 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
163}
164
drh29642252019-02-01 20:29:04 +0000165/*
166** Allocate memory for a temporary buffer needed for printf rendering.
167**
168** If the requested size of the temp buffer is larger than the size
169** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
170** Do the size check before the memory allocation to prevent rogue
171** SQL from requesting large allocations using the precision or width
172** field of the printf() function.
173*/
174static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
175 char *z;
drh255a81f2019-02-22 15:42:10 +0000176 if( pAccum->accError ) return 0;
drh29642252019-02-01 20:29:04 +0000177 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
178 setStrAccumError(pAccum, SQLITE_TOOBIG);
179 return 0;
180 }
181 z = sqlite3DbMallocRaw(pAccum->db, n);
182 if( z==0 ){
183 setStrAccumError(pAccum, SQLITE_NOMEM);
184 }
185 return z;
186}
drha5c14162013-12-17 15:03:06 +0000187
188/*
drh79158e12005-09-06 21:40:45 +0000189** On machines with a small stack size, you can redefine the
drhed1fddf2011-10-12 18:52:59 +0000190** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
drh79158e12005-09-06 21:40:45 +0000191*/
192#ifndef SQLITE_PRINT_BUF_SIZE
drh59eedf72011-10-11 17:54:54 +0000193# define SQLITE_PRINT_BUF_SIZE 70
drh79158e12005-09-06 21:40:45 +0000194#endif
195#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57 +0000196
197/*
drhed1fddf2011-10-12 18:52:59 +0000198** Render a string given by "fmt" into the StrAccum object.
drha18c5682000-10-08 22:20:57 +0000199*/
drh0cdbe1a2018-05-09 13:46:26 +0000200void sqlite3_str_vappendf(
201 sqlite3_str *pAccum, /* Accumulate results here */
drha5c14162013-12-17 15:03:06 +0000202 const char *fmt, /* Format string */
203 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57 +0000204){
drhe84a3062004-02-02 12:29:25 +0000205 int c; /* Next character in the format string */
206 char *bufpt; /* Pointer to the conversion buffer */
207 int precision; /* Precision of the current field */
208 int length; /* Length of the field */
209 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25 +0000210 int width; /* Width of the current field */
211 etByte flag_leftjustify; /* True if "-" flag is present */
drh2c338a92017-02-10 19:38:36 +0000212 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
drhe84a3062004-02-02 12:29:25 +0000213 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42 +0000214 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25 +0000215 etByte flag_zeropad; /* True if field width constant starts with zero */
drh2c338a92017-02-10 19:38:36 +0000216 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
drh3e9aeec2005-08-13 13:39:02 +0000217 etByte done; /* Loop termination flag */
drh2c338a92017-02-10 19:38:36 +0000218 etByte cThousand; /* Thousands separator for %d and %u */
drhad5a9d72016-05-05 11:53:12 +0000219 etByte xtype = etINVALID; /* Conversion paradigm */
drha5c14162013-12-17 15:03:06 +0000220 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000221 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drh27436af2006-03-28 23:57:17 +0000222 sqlite_uint64 longvalue; /* Value for integer types */
drh384eef32004-01-07 03:04:27 +0000223 LONGDOUBLE_TYPE realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16 +0000224 const et_info *infop; /* Pointer to the appropriate info structure */
drh59eedf72011-10-11 17:54:54 +0000225 char *zOut; /* Rendering buffer */
226 int nOut; /* Size of the rendering buffer */
drhaf8f5132014-10-29 18:20:18 +0000227 char *zExtra = 0; /* Malloced memory used by some conversion */
drhb37df7b2005-10-13 02:09:49 +0000228#ifndef SQLITE_OMIT_FLOATING_POINT
drh557cc602005-08-13 12:59:14 +0000229 int exp, e2; /* exponent of real numbers */
drhed1fddf2011-10-12 18:52:59 +0000230 int nsd; /* Number of significant digits returned */
drhe84a3062004-02-02 12:29:25 +0000231 double rounder; /* Used for rounding floating point values */
232 etByte flag_dp; /* True if decimal point should be shown */
233 etByte flag_rtz; /* True if trailing zeros should be removed */
drha18c5682000-10-08 22:20:57 +0000234#endif
drha5c14162013-12-17 15:03:06 +0000235 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59 +0000236 char buf[etBUFSIZE]; /* Conversion buffer */
drha18c5682000-10-08 22:20:57 +0000237
drhcc398962018-02-20 15:23:37 +0000238 /* pAccum never starts out with an empty buffer that was obtained from
239 ** malloc(). This precondition is required by the mprintf("%z...")
240 ** optimization. */
241 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
242
drha18c5682000-10-08 22:20:57 +0000243 bufpt = 0;
drh8236f682017-01-04 00:26:28 +0000244 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
245 pArgList = va_arg(ap, PrintfArguments*);
246 bArgList = 1;
drha5c14162013-12-17 15:03:06 +0000247 }else{
drh8236f682017-01-04 00:26:28 +0000248 bArgList = 0;
drha5c14162013-12-17 15:03:06 +0000249 }
drha18c5682000-10-08 22:20:57 +0000250 for(; (c=(*fmt))!=0; ++fmt){
251 if( c!='%' ){
drha18c5682000-10-08 22:20:57 +0000252 bufpt = (char *)fmt;
drh760b1592014-09-18 01:50:09 +0000253#if HAVE_STRCHRNUL
254 fmt = strchrnul(fmt, '%');
255#else
256 do{ fmt++; }while( *fmt && *fmt != '%' );
257#endif
drh0cdbe1a2018-05-09 13:46:26 +0000258 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
drh760b1592014-09-18 01:50:09 +0000259 if( *fmt==0 ) break;
drha18c5682000-10-08 22:20:57 +0000260 }
261 if( (c=(*++fmt))==0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000262 sqlite3_str_append(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57 +0000263 break;
264 }
265 /* Find out what flags are present */
drh2c338a92017-02-10 19:38:36 +0000266 flag_leftjustify = flag_prefix = cThousand =
drh557cc602005-08-13 12:59:14 +0000267 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02 +0000268 done = 0;
drh9a6d01b2019-02-01 18:46:41 +0000269 width = 0;
270 flag_long = 0;
271 precision = -1;
drha18c5682000-10-08 22:20:57 +0000272 do{
273 switch( c ){
drh3e9aeec2005-08-13 13:39:02 +0000274 case '-': flag_leftjustify = 1; break;
drh2c338a92017-02-10 19:38:36 +0000275 case '+': flag_prefix = '+'; break;
276 case ' ': flag_prefix = ' '; break;
drh3e9aeec2005-08-13 13:39:02 +0000277 case '#': flag_alternateform = 1; break;
278 case '!': flag_altform2 = 1; break;
279 case '0': flag_zeropad = 1; break;
drh2c338a92017-02-10 19:38:36 +0000280 case ',': cThousand = ','; break;
drh3e9aeec2005-08-13 13:39:02 +0000281 default: done = 1; break;
drh9a6d01b2019-02-01 18:46:41 +0000282 case 'l': {
283 flag_long = 1;
284 c = *++fmt;
285 if( c=='l' ){
286 c = *++fmt;
287 flag_long = 2;
288 }
289 done = 1;
290 break;
291 }
292 case '1': case '2': case '3': case '4': case '5':
293 case '6': case '7': case '8': case '9': {
294 unsigned wx = c - '0';
295 while( (c = *++fmt)>='0' && c<='9' ){
296 wx = wx*10 + c - '0';
297 }
298 testcase( wx>0x7fffffff );
299 width = wx & 0x7fffffff;
300#ifdef SQLITE_PRINTF_PRECISION_LIMIT
301 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
302 width = SQLITE_PRINTF_PRECISION_LIMIT;
303 }
304#endif
305 if( c!='.' && c!='l' ){
306 done = 1;
307 }else{
308 fmt--;
309 }
310 break;
311 }
312 case '*': {
313 if( bArgList ){
314 width = (int)getIntArg(pArgList);
315 }else{
316 width = va_arg(ap,int);
317 }
318 if( width<0 ){
319 flag_leftjustify = 1;
320 width = width >= -2147483647 ? -width : 0;
321 }
322#ifdef SQLITE_PRINTF_PRECISION_LIMIT
323 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
324 width = SQLITE_PRINTF_PRECISION_LIMIT;
325 }
326#endif
327 if( (c = fmt[1])!='.' && c!='l' ){
328 c = *++fmt;
329 done = 1;
330 }
331 break;
332 }
333 case '.': {
334 c = *++fmt;
335 if( c=='*' ){
336 if( bArgList ){
337 precision = (int)getIntArg(pArgList);
338 }else{
339 precision = va_arg(ap,int);
340 }
341 if( precision<0 ){
342 precision = precision >= -2147483647 ? -precision : -1;
343 }
344 c = *++fmt;
345 }else{
346 unsigned px = 0;
347 while( c>='0' && c<='9' ){
348 px = px*10 + c - '0';
349 c = *++fmt;
350 }
351 testcase( px>0x7fffffff );
352 precision = px & 0x7fffffff;
353 }
354#ifdef SQLITE_PRINTF_PRECISION_LIMIT
355 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
356 precision = SQLITE_PRINTF_PRECISION_LIMIT;
357 }
358#endif
359 if( c=='l' ){
360 --fmt;
361 }else{
362 done = 1;
363 }
364 break;
365 }
drha18c5682000-10-08 22:20:57 +0000366 }
drh3e9aeec2005-08-13 13:39:02 +0000367 }while( !done && (c=(*++fmt))!=0 );
dan8c069142015-04-07 14:38:57 +0000368
drha18c5682000-10-08 22:20:57 +0000369 /* Fetch the info entry for the field */
drh874ba042009-04-08 16:10:04 +0000370 infop = &fmtinfo[0];
371 xtype = etINVALID;
danielk197700e13612008-11-17 19:18:54 +0000372 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57 +0000373 if( c==fmtinfo[idx].fmttype ){
374 infop = &fmtinfo[idx];
drh8236f682017-01-04 00:26:28 +0000375 xtype = infop->type;
drha18c5682000-10-08 22:20:57 +0000376 break;
377 }
378 }
drh43617e92006-03-06 20:55:46 +0000379
drha18c5682000-10-08 22:20:57 +0000380 /*
381 ** At this point, variables are initialized as follows:
382 **
383 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02 +0000384 ** flag_altform2 TRUE if a '!' is present.
drh2c338a92017-02-10 19:38:36 +0000385 ** flag_prefix '+' or ' ' or zero
drha18c5682000-10-08 22:20:57 +0000386 ** flag_leftjustify TRUE if a '-' is present or if the
387 ** field width was negative.
388 ** flag_zeropad TRUE if the width began with 0.
drh2c338a92017-02-10 19:38:36 +0000389 ** flag_long 1 for "l", 2 for "ll"
drha18c5682000-10-08 22:20:57 +0000390 ** width The specified field width. This is
391 ** always non-negative. Zero is the default.
392 ** precision The specified precision. The default
393 ** is -1.
394 ** xtype The class of the conversion.
395 ** infop Pointer to the appropriate info struct.
396 */
397 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04 +0000398 case etPOINTER:
drh2c338a92017-02-10 19:38:36 +0000399 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
400 sizeof(char*)==sizeof(long int) ? 1 : 0;
drhfe63d1c2004-09-08 20:13:04 +0000401 /* Fall through into the next case */
drh9a993342007-12-13 02:45:31 +0000402 case etORDINAL:
drh2c338a92017-02-10 19:38:36 +0000403 case etRADIX:
404 cThousand = 0;
405 /* Fall through into the next case */
406 case etDECIMAL:
drhe84a3062004-02-02 12:29:25 +0000407 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48 +0000408 i64 v;
drha5c14162013-12-17 15:03:06 +0000409 if( bArgList ){
410 v = getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16 +0000411 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36 +0000412 if( flag_long==2 ){
413 v = va_arg(ap,i64) ;
414 }else{
415 v = va_arg(ap,long int);
416 }
drheeb23a42009-05-04 20:20:16 +0000417 }else{
418 v = va_arg(ap,int);
419 }
drhe9707672004-06-25 01:10:48 +0000420 if( v<0 ){
drh158b9cb2011-03-05 20:59:46 +0000421 if( v==SMALLEST_INT64 ){
422 longvalue = ((u64)1)<<63;
423 }else{
424 longvalue = -v;
425 }
drhe9707672004-06-25 01:10:48 +0000426 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33 +0000427 }else{
drhe9707672004-06-25 01:10:48 +0000428 longvalue = v;
drh2c338a92017-02-10 19:38:36 +0000429 prefix = flag_prefix;
danielk1977cfcdaef2004-05-12 07:33:33 +0000430 }
drhe9707672004-06-25 01:10:48 +0000431 }else{
drha5c14162013-12-17 15:03:06 +0000432 if( bArgList ){
433 longvalue = (u64)getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16 +0000434 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36 +0000435 if( flag_long==2 ){
436 longvalue = va_arg(ap,u64);
437 }else{
438 longvalue = va_arg(ap,unsigned long int);
439 }
drheeb23a42009-05-04 20:20:16 +0000440 }else{
441 longvalue = va_arg(ap,unsigned int);
442 }
drhe9707672004-06-25 01:10:48 +0000443 prefix = 0;
444 }
445 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57 +0000446 if( flag_zeropad && precision<width-(prefix!=0) ){
447 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13 +0000448 }
drh2c338a92017-02-10 19:38:36 +0000449 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
drh59eedf72011-10-11 17:54:54 +0000450 nOut = etBUFSIZE;
451 zOut = buf;
452 }else{
drh7ba03ea2019-02-01 21:08:27 +0000453 u64 n;
454 n = (u64)precision + 10;
455 if( cThousand ) n += precision/3;
drh29642252019-02-01 20:29:04 +0000456 zOut = zExtra = printfTempBuf(pAccum, n);
457 if( zOut==0 ) return;
drh5f429952017-03-20 16:34:18 +0000458 nOut = (int)n;
drh59eedf72011-10-11 17:54:54 +0000459 }
460 bufpt = &zOut[nOut-1];
drh9a993342007-12-13 02:45:31 +0000461 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22 +0000462 static const char zOrd[] = "thstndrd";
drhea678832008-12-10 19:26:22 +0000463 int x = (int)(longvalue % 10);
drh43f6e062007-12-13 17:50:22 +0000464 if( x>=4 || (longvalue/10)%10==1 ){
465 x = 0;
466 }
drh59eedf72011-10-11 17:54:54 +0000467 *(--bufpt) = zOrd[x*2+1];
468 *(--bufpt) = zOrd[x*2];
drh9a993342007-12-13 02:45:31 +0000469 }
drha18c5682000-10-08 22:20:57 +0000470 {
drh0e682092014-03-17 15:06:57 +0000471 const char *cset = &aDigits[infop->charset];
472 u8 base = infop->base;
drha18c5682000-10-08 22:20:57 +0000473 do{ /* Convert to ascii */
474 *(--bufpt) = cset[longvalue%base];
475 longvalue = longvalue/base;
476 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13 +0000477 }
drh59eedf72011-10-11 17:54:54 +0000478 length = (int)(&zOut[nOut-1]-bufpt);
drh2c338a92017-02-10 19:38:36 +0000479 while( precision>length ){
drha18c5682000-10-08 22:20:57 +0000480 *(--bufpt) = '0'; /* Zero pad */
drh2c338a92017-02-10 19:38:36 +0000481 length++;
482 }
483 if( cThousand ){
484 int nn = (length - 1)/3; /* Number of "," to insert */
485 int ix = (length - 1)%3 + 1;
486 bufpt -= nn;
487 for(idx=0; nn>0; idx++){
488 bufpt[idx] = bufpt[idx+nn];
489 ix--;
490 if( ix==0 ){
491 bufpt[++idx] = cThousand;
492 nn--;
493 ix = 3;
494 }
495 }
drh9adf9ac2002-05-15 11:44:13 +0000496 }
drha18c5682000-10-08 22:20:57 +0000497 if( prefix ) *(--bufpt) = prefix; /* Add sign */
498 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30 +0000499 const char *pre;
500 char x;
501 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51 +0000502 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57 +0000503 }
drh59eedf72011-10-11 17:54:54 +0000504 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57 +0000505 break;
506 case etFLOAT:
507 case etEXP:
508 case etGENERIC:
drha5c14162013-12-17 15:03:06 +0000509 if( bArgList ){
510 realvalue = getDoubleArg(pArgList);
511 }else{
512 realvalue = va_arg(ap,double);
513 }
drh69ef7032010-03-04 17:11:31 +0000514#ifdef SQLITE_OMIT_FLOATING_POINT
515 length = 0;
516#else
drha18c5682000-10-08 22:20:57 +0000517 if( precision<0 ) precision = 6; /* Set default precision */
drha18c5682000-10-08 22:20:57 +0000518 if( realvalue<0.0 ){
519 realvalue = -realvalue;
520 prefix = '-';
drh9adf9ac2002-05-15 11:44:13 +0000521 }else{
drh2c338a92017-02-10 19:38:36 +0000522 prefix = flag_prefix;
drh9adf9ac2002-05-15 11:44:13 +0000523 }
drh3e9aeec2005-08-13 13:39:02 +0000524 if( xtype==etGENERIC && precision>0 ) precision--;
drha30d22a2015-04-07 13:28:41 +0000525 testcase( precision>0xfff );
drha0ed86b2019-05-24 22:58:16 +0000526 idx = precision & 0xfff;
527 rounder = arRound[idx%10];
528 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
529 if( xtype==etFLOAT ){
530 if( precision<17) rounder += realvalue*2.0e-16;
531 realvalue += rounder;
532 }
drha18c5682000-10-08 22:20:57 +0000533 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
534 exp = 0;
drhea678832008-12-10 19:26:22 +0000535 if( sqlite3IsNaN((double)realvalue) ){
drh53c14022007-05-10 17:23:11 +0000536 bufpt = "NaN";
537 length = 3;
538 break;
539 }
drha18c5682000-10-08 22:20:57 +0000540 if( realvalue>0.0 ){
drh72b3fbc2012-06-19 03:11:25 +0000541 LONGDOUBLE_TYPE scale = 1.0;
drh4ef94132012-06-19 03:35:05 +0000542 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
drh2a8f6712015-09-02 21:00:48 +0000543 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
drh72b3fbc2012-06-19 03:11:25 +0000544 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
545 realvalue /= scale;
drh93a960a2008-07-10 00:32:42 +0000546 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
547 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000548 if( exp>350 ){
drh2a8f6712015-09-02 21:00:48 +0000549 bufpt = buf;
550 buf[0] = prefix;
551 memcpy(buf+(prefix!=0),"Inf",4);
552 length = 3+(prefix!=0);
drha18c5682000-10-08 22:20:57 +0000553 break;
554 }
drh9adf9ac2002-05-15 11:44:13 +0000555 }
drha18c5682000-10-08 22:20:57 +0000556 bufpt = buf;
557 /*
558 ** If the field type is etGENERIC, then convert to either etEXP
559 ** or etFLOAT, as appropriate.
560 */
drha18c5682000-10-08 22:20:57 +0000561 if( xtype!=etFLOAT ){
562 realvalue += rounder;
563 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
564 }
565 if( xtype==etGENERIC ){
566 flag_rtz = !flag_alternateform;
567 if( exp<-4 || exp>precision ){
568 xtype = etEXP;
569 }else{
570 precision = precision - exp;
571 xtype = etFLOAT;
572 }
drh9adf9ac2002-05-15 11:44:13 +0000573 }else{
drh72b3fbc2012-06-19 03:11:25 +0000574 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13 +0000575 }
drh557cc602005-08-13 12:59:14 +0000576 if( xtype==etEXP ){
577 e2 = 0;
578 }else{
579 e2 = exp;
580 }
drh29642252019-02-01 20:29:04 +0000581 {
582 i64 szBufNeeded; /* Size of a temporary buffer needed */
583 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
584 if( szBufNeeded > etBUFSIZE ){
585 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
586 if( bufpt==0 ) return;
drh59eedf72011-10-11 17:54:54 +0000587 }
588 }
589 zOut = bufpt;
drh72b3fbc2012-06-19 03:11:25 +0000590 nsd = 16 + flag_altform2*10;
drhea678832008-12-10 19:26:22 +0000591 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14 +0000592 /* The sign in front of the number */
593 if( prefix ){
594 *(bufpt++) = prefix;
595 }
596 /* Digits prior to the decimal point */
597 if( e2<0 ){
598 *(bufpt++) = '0';
599 }else{
600 for(; e2>=0; e2--){
601 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000602 }
drh9adf9ac2002-05-15 11:44:13 +0000603 }
drh557cc602005-08-13 12:59:14 +0000604 /* The decimal point */
605 if( flag_dp ){
606 *(bufpt++) = '.';
607 }
608 /* "0" digits after the decimal point but before the first
609 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000610 for(e2++; e2<0; precision--, e2++){
611 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000612 *(bufpt++) = '0';
613 }
614 /* Significant digits after the decimal point */
615 while( (precision--)>0 ){
616 *(bufpt++) = et_getdigit(&realvalue,&nsd);
617 }
618 /* Remove trailing zeros and the "." if no digits follow the "." */
619 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000620 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54 +0000621 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02 +0000622 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000623 if( flag_altform2 ){
624 *(bufpt++) = '0';
625 }else{
626 *(--bufpt) = 0;
627 }
628 }
629 }
630 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54 +0000631 if( xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000632 *(bufpt++) = aDigits[infop->charset];
633 if( exp<0 ){
634 *(bufpt++) = '-'; exp = -exp;
635 }else{
636 *(bufpt++) = '+';
637 }
638 if( exp>=100 ){
drhea678832008-12-10 19:26:22 +0000639 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14 +0000640 exp %= 100;
641 }
drhea678832008-12-10 19:26:22 +0000642 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
643 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14 +0000644 }
645 *bufpt = 0;
646
drha18c5682000-10-08 22:20:57 +0000647 /* The converted number is in buf[] and zero terminated. Output it.
648 ** Note that the number is in the usual order, not reversed as with
649 ** integer conversions. */
drh59eedf72011-10-11 17:54:54 +0000650 length = (int)(bufpt-zOut);
651 bufpt = zOut;
drha18c5682000-10-08 22:20:57 +0000652
653 /* Special case: Add leading zeros if the flag_zeropad flag is
654 ** set and we are not left justified */
655 if( flag_zeropad && !flag_leftjustify && length < width){
656 int i;
657 int nPad = width - length;
658 for(i=width; i>=nPad; i--){
659 bufpt[i] = bufpt[i-nPad];
660 }
661 i = prefix!=0;
662 while( nPad-- ) bufpt[i++] = '0';
663 length = width;
664 }
drh69ef7032010-03-04 17:11:31 +0000665#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
drha18c5682000-10-08 22:20:57 +0000666 break;
667 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42 +0000668 if( !bArgList ){
669 *(va_arg(ap,int*)) = pAccum->nChar;
670 }
drha18c5682000-10-08 22:20:57 +0000671 length = width = 0;
672 break;
673 case etPERCENT:
674 buf[0] = '%';
675 bufpt = buf;
676 length = 1;
677 break;
drha18c5682000-10-08 22:20:57 +0000678 case etCHARX:
drha5c14162013-12-17 15:03:06 +0000679 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42 +0000680 bufpt = getTextArg(pArgList);
drha15a7c32018-02-19 21:58:16 +0000681 length = 1;
drh136102b2018-02-19 18:56:52 +0000682 if( bufpt ){
683 buf[0] = c = *(bufpt++);
drh136102b2018-02-19 18:56:52 +0000684 if( (c&0xc0)==0xc0 ){
685 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
686 buf[length++] = *(bufpt++);
687 }
688 }
drha15a7c32018-02-19 21:58:16 +0000689 }else{
690 buf[0] = 0;
drh136102b2018-02-19 18:56:52 +0000691 }
drha5c14162013-12-17 15:03:06 +0000692 }else{
drh136102b2018-02-19 18:56:52 +0000693 unsigned int ch = va_arg(ap,unsigned int);
694 if( ch<0x00080 ){
695 buf[0] = ch & 0xff;
696 length = 1;
697 }else if( ch<0x00800 ){
698 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
699 buf[1] = 0x80 + (u8)(ch & 0x3f);
700 length = 2;
701 }else if( ch<0x10000 ){
702 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
703 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
704 buf[2] = 0x80 + (u8)(ch & 0x3f);
705 length = 3;
706 }else{
707 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
708 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
709 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
710 buf[3] = 0x80 + (u8)(ch & 0x3f);
711 length = 4;
712 }
drha5c14162013-12-17 15:03:06 +0000713 }
drhaf8f5132014-10-29 18:20:18 +0000714 if( precision>1 ){
715 width -= precision-1;
716 if( width>1 && !flag_leftjustify ){
drh0cdbe1a2018-05-09 13:46:26 +0000717 sqlite3_str_appendchar(pAccum, width-1, ' ');
drhaf8f5132014-10-29 18:20:18 +0000718 width = 0;
719 }
drh136102b2018-02-19 18:56:52 +0000720 while( precision-- > 1 ){
drh0cdbe1a2018-05-09 13:46:26 +0000721 sqlite3_str_append(pAccum, buf, length);
drh136102b2018-02-19 18:56:52 +0000722 }
drh9adf9ac2002-05-15 11:44:13 +0000723 }
drha18c5682000-10-08 22:20:57 +0000724 bufpt = buf;
drhcf7c8372018-02-19 20:23:20 +0000725 flag_altform2 = 1;
726 goto adjust_width_for_utf8;
drha18c5682000-10-08 22:20:57 +0000727 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000728 case etDYNSTRING:
drha5c14162013-12-17 15:03:06 +0000729 if( bArgList ){
730 bufpt = getTextArg(pArgList);
drh2a8f6712015-09-02 21:00:48 +0000731 xtype = etSTRING;
drha5c14162013-12-17 15:03:06 +0000732 }else{
733 bufpt = va_arg(ap,char*);
734 }
drhd93d8a82003-06-16 03:08:18 +0000735 if( bufpt==0 ){
736 bufpt = "";
drh2a8f6712015-09-02 21:00:48 +0000737 }else if( xtype==etDYNSTRING ){
drhaf524a62018-09-13 17:07:12 +0000738 if( pAccum->nChar==0
739 && pAccum->mxAlloc
740 && width==0
741 && precision<0
742 && pAccum->accError==0
743 ){
drhcc398962018-02-20 15:23:37 +0000744 /* Special optimization for sqlite3_mprintf("%z..."):
745 ** Extend an existing memory allocation rather than creating
746 ** a new one. */
747 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
748 pAccum->zText = bufpt;
749 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
750 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
751 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
752 length = 0;
753 break;
754 }
drhd93d8a82003-06-16 03:08:18 +0000755 zExtra = bufpt;
756 }
drhe5090942008-04-29 15:22:27 +0000757 if( precision>=0 ){
drh62856462018-02-19 17:03:23 +0000758 if( flag_altform2 ){
759 /* Set length to the number of bytes needed in order to display
760 ** precision characters */
761 unsigned char *z = (unsigned char*)bufpt;
762 while( precision-- > 0 && z[0] ){
763 SQLITE_SKIP_UTF8(z);
764 }
765 length = (int)(z - (unsigned char*)bufpt);
766 }else{
767 for(length=0; length<precision && bufpt[length]; length++){}
768 }
drhe5090942008-04-29 15:22:27 +0000769 }else{
drhc84ddf12017-08-19 20:38:18 +0000770 length = 0x7fffffff & (int)strlen(bufpt);
drhe5090942008-04-29 15:22:27 +0000771 }
drh57e3ba72018-02-19 18:03:10 +0000772 adjust_width_for_utf8:
drh62856462018-02-19 17:03:23 +0000773 if( flag_altform2 && width>0 ){
774 /* Adjust width to account for extra bytes in UTF-8 characters */
775 int ii = length - 1;
776 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
777 }
drha18c5682000-10-08 22:20:57 +0000778 break;
drh57e3ba72018-02-19 18:03:10 +0000779 case etSQLESCAPE: /* %q: Escape ' characters */
780 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
781 case etSQLESCAPE3: { /* %w: Escape " characters */
drh8965b502009-11-25 16:53:37 +0000782 int i, j, k, n, isnull;
drh5eba8c02005-08-19 02:26:27 +0000783 int needQuote;
drhea678832008-12-10 19:26:22 +0000784 char ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000785 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
drha5c14162013-12-17 15:03:06 +0000786 char *escarg;
787
788 if( bArgList ){
789 escarg = getTextArg(pArgList);
790 }else{
791 escarg = va_arg(ap,char*);
792 }
danielk1977f0113002006-01-24 12:09:17 +0000793 isnull = escarg==0;
794 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drh57e3ba72018-02-19 18:03:10 +0000795 /* For %q, %Q, and %w, the precision is the number of byte (or
796 ** characters if the ! flags is present) to use from the input.
797 ** Because of the extra quoting characters inserted, the number
798 ** of output characters may be larger than the precision.
799 */
drh8965b502009-11-25 16:53:37 +0000800 k = precision;
dan60d4a302010-03-04 17:58:45 +0000801 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17 +0000802 if( ch==q ) n++;
drh57e3ba72018-02-19 18:03:10 +0000803 if( flag_altform2 && (ch&0xc0)==0xc0 ){
804 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
805 }
drha18c5682000-10-08 22:20:57 +0000806 }
drh5eba8c02005-08-19 02:26:27 +0000807 needQuote = !isnull && xtype==etSQLESCAPE2;
drh2a8f6712015-09-02 21:00:48 +0000808 n += i + 3;
drh5eba8c02005-08-19 02:26:27 +0000809 if( n>etBUFSIZE ){
drh29642252019-02-01 20:29:04 +0000810 bufpt = zExtra = printfTempBuf(pAccum, n);
811 if( bufpt==0 ) return;
drh5eba8c02005-08-19 02:26:27 +0000812 }else{
813 bufpt = buf;
814 }
815 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000816 if( needQuote ) bufpt[j++] = q;
drh8965b502009-11-25 16:53:37 +0000817 k = i;
818 for(i=0; i<k; i++){
819 bufpt[j++] = ch = escarg[i];
danielk1977f3b863e2007-06-24 06:32:17 +0000820 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000821 }
danielk1977f3b863e2007-06-24 06:32:17 +0000822 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000823 bufpt[j] = 0;
824 length = j;
drh57e3ba72018-02-19 18:03:10 +0000825 goto adjust_width_for_utf8;
drh5eba8c02005-08-19 02:26:27 +0000826 }
drh5f968432004-02-21 19:02:30 +0000827 case etTOKEN: {
drh8236f682017-01-04 00:26:28 +0000828 Token *pToken;
829 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
830 pToken = va_arg(ap, Token*);
drha5c14162013-12-17 15:03:06 +0000831 assert( bArgList==0 );
drha9ab4812013-12-11 11:00:44 +0000832 if( pToken && pToken->n ){
drh0cdbe1a2018-05-09 13:46:26 +0000833 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000834 }
drh5f968432004-02-21 19:02:30 +0000835 length = width = 0;
836 break;
837 }
838 case etSRCLIST: {
drh8236f682017-01-04 00:26:28 +0000839 SrcList *pSrc;
840 int k;
841 struct SrcList_item *pItem;
842 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
843 pSrc = va_arg(ap, SrcList*);
844 k = va_arg(ap, int);
845 pItem = &pSrc->a[k];
drha5c14162013-12-17 15:03:06 +0000846 assert( bArgList==0 );
drh5f968432004-02-21 19:02:30 +0000847 assert( k>=0 && k<pSrc->nSrc );
drh93a960a2008-07-10 00:32:42 +0000848 if( pItem->zDatabase ){
drh0cdbe1a2018-05-09 13:46:26 +0000849 sqlite3_str_appendall(pAccum, pItem->zDatabase);
850 sqlite3_str_append(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000851 }
drh0cdbe1a2018-05-09 13:46:26 +0000852 sqlite3_str_appendall(pAccum, pItem->zName);
drh5f968432004-02-21 19:02:30 +0000853 length = width = 0;
854 break;
855 }
drh874ba042009-04-08 16:10:04 +0000856 default: {
857 assert( xtype==etINVALID );
858 return;
859 }
drha18c5682000-10-08 22:20:57 +0000860 }/* End switch over the format type */
861 /*
862 ** The text of the conversion is pointed to by "bufpt" and is
863 ** "length" characters long. The field width is "width". Do
drh62856462018-02-19 17:03:23 +0000864 ** the output. Both length and width are in bytes, not characters,
865 ** at this point. If the "!" flag was present on string conversions
866 ** indicating that width and precision should be expressed in characters,
867 ** then the values have been translated prior to reaching this point.
drha18c5682000-10-08 22:20:57 +0000868 */
drha70a0732014-03-17 14:24:27 +0000869 width -= length;
drh8236f682017-01-04 00:26:28 +0000870 if( width>0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000871 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
872 sqlite3_str_append(pAccum, bufpt, length);
873 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
drh8236f682017-01-04 00:26:28 +0000874 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000875 sqlite3_str_append(pAccum, bufpt, length);
drh8236f682017-01-04 00:26:28 +0000876 }
drha70a0732014-03-17 14:24:27 +0000877
drhaf8f5132014-10-29 18:20:18 +0000878 if( zExtra ){
drh96ceaf82015-11-14 22:04:22 +0000879 sqlite3DbFree(pAccum->db, zExtra);
drhaf8f5132014-10-29 18:20:18 +0000880 zExtra = 0;
881 }
drha18c5682000-10-08 22:20:57 +0000882 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000883} /* End of function */
884
drhade86482007-11-28 22:36:40 +0000885/*
drha70a0732014-03-17 14:24:27 +0000886** Enlarge the memory allocation on a StrAccum object so that it is
887** able to accept at least N more bytes of text.
888**
889** Return the number of bytes of text that StrAccum is able to accept
890** after the attempted enlargement. The value returned might be zero.
891*/
892static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
893 char *zNew;
drha30d22a2015-04-07 13:28:41 +0000894 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
drha70a0732014-03-17 14:24:27 +0000895 if( p->accError ){
drh0cdbe1a2018-05-09 13:46:26 +0000896 testcase(p->accError==SQLITE_TOOBIG);
897 testcase(p->accError==SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000898 return 0;
899 }
drhc0490572015-05-02 11:45:53 +0000900 if( p->mxAlloc==0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000901 setStrAccumError(p, SQLITE_TOOBIG);
drh255a81f2019-02-22 15:42:10 +0000902 return p->nAlloc - p->nChar - 1;
drha70a0732014-03-17 14:24:27 +0000903 }else{
drh5f4a6862016-01-30 12:50:25 +0000904 char *zOld = isMalloced(p) ? p->zText : 0;
drha70a0732014-03-17 14:24:27 +0000905 i64 szNew = p->nChar;
906 szNew += N + 1;
drh7b4d7802014-11-03 14:46:29 +0000907 if( szNew+p->nChar<=p->mxAlloc ){
908 /* Force exponential buffer size growth as long as it does not overflow,
909 ** to avoid having to call this routine too often */
910 szNew += p->nChar;
911 }
drha70a0732014-03-17 14:24:27 +0000912 if( szNew > p->mxAlloc ){
drh0cdbe1a2018-05-09 13:46:26 +0000913 sqlite3_str_reset(p);
914 setStrAccumError(p, SQLITE_TOOBIG);
drha70a0732014-03-17 14:24:27 +0000915 return 0;
916 }else{
917 p->nAlloc = (int)szNew;
918 }
drhc0490572015-05-02 11:45:53 +0000919 if( p->db ){
drha70a0732014-03-17 14:24:27 +0000920 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
921 }else{
drhf3cdcdc2015-04-29 16:50:28 +0000922 zNew = sqlite3_realloc64(zOld, p->nAlloc);
drha70a0732014-03-17 14:24:27 +0000923 }
924 if( zNew ){
drh7ef4d1c2014-05-31 15:39:53 +0000925 assert( p->zText!=0 || p->nChar==0 );
drh5f4a6862016-01-30 12:50:25 +0000926 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
drha70a0732014-03-17 14:24:27 +0000927 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:12 +0000928 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drh5f4a6862016-01-30 12:50:25 +0000929 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
drha70a0732014-03-17 14:24:27 +0000930 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000931 sqlite3_str_reset(p);
932 setStrAccumError(p, SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000933 return 0;
934 }
935 }
936 return N;
937}
938
939/*
drhaf8f5132014-10-29 18:20:18 +0000940** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:27 +0000941*/
drh0cdbe1a2018-05-09 13:46:26 +0000942void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
drha30d22a2015-04-07 13:28:41 +0000943 testcase( p->nChar + (i64)N > 0x7fffffff );
944 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
945 return;
946 }
drhaf8f5132014-10-29 18:20:18 +0000947 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:27 +0000948}
949
950/*
951** The StrAccum "p" is not large enough to accept N new bytes of z[].
952** So enlarge if first, then do the append.
953**
drh0cdbe1a2018-05-09 13:46:26 +0000954** This is a helper routine to sqlite3_str_append() that does special-case
drha70a0732014-03-17 14:24:27 +0000955** work (enlarging the buffer) using tail recursion, so that the
drh0cdbe1a2018-05-09 13:46:26 +0000956** sqlite3_str_append() routine can use fast calling semantics.
drha70a0732014-03-17 14:24:27 +0000957*/
drh172087f2014-08-22 15:40:20 +0000958static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:27 +0000959 N = sqlite3StrAccumEnlarge(p, N);
960 if( N>0 ){
961 memcpy(&p->zText[p->nChar], z, N);
962 p->nChar += N;
963 }
964}
965
966/*
967** Append N bytes of text from z to the StrAccum object. Increase the
968** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:57 +0000969*/
drh0cdbe1a2018-05-09 13:46:26 +0000970void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
drh34573382015-04-15 05:38:35 +0000971 assert( z!=0 || N==0 );
drha6353a32013-12-09 19:03:26 +0000972 assert( p->zText!=0 || p->nChar==0 || p->accError );
973 assert( N>=0 );
drh255a81f2019-02-22 15:42:10 +0000974 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
drhade86482007-11-28 22:36:40 +0000975 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:27 +0000976 enlargeAndAppend(p,z,N);
dan895decf2016-12-30 14:15:56 +0000977 }else if( N ){
drh172087f2014-08-22 15:40:20 +0000978 assert( p->zText );
979 p->nChar += N;
980 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:30 +0000981 }
drh483750b2003-01-29 18:46:51 +0000982}
983
984/*
drha6353a32013-12-09 19:03:26 +0000985** Append the complete text of zero-terminated string z[] to the p string.
986*/
drh0cdbe1a2018-05-09 13:46:26 +0000987void sqlite3_str_appendall(sqlite3_str *p, const char *z){
988 sqlite3_str_append(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:26 +0000989}
990
991
992/*
drhade86482007-11-28 22:36:40 +0000993** Finish off a string by making sure it is zero-terminated.
994** Return a pointer to the resulting string. Return a NULL
995** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +0000996*/
drh043e5862016-11-25 15:11:26 +0000997static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
drh3f18e6d2017-08-12 02:01:55 +0000998 char *zText;
drh043e5862016-11-25 15:11:26 +0000999 assert( p->mxAlloc>0 && !isMalloced(p) );
drh3f18e6d2017-08-12 02:01:55 +00001000 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
1001 if( zText ){
1002 memcpy(zText, p->zText, p->nChar+1);
drh043e5862016-11-25 15:11:26 +00001003 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
1004 }else{
drh0cdbe1a2018-05-09 13:46:26 +00001005 setStrAccumError(p, SQLITE_NOMEM);
drh043e5862016-11-25 15:11:26 +00001006 }
drh3f18e6d2017-08-12 02:01:55 +00001007 p->zText = zText;
1008 return zText;
drh043e5862016-11-25 15:11:26 +00001009}
drhade86482007-11-28 22:36:40 +00001010char *sqlite3StrAccumFinish(StrAccum *p){
1011 if( p->zText ){
1012 p->zText[p->nChar] = 0;
drh5f4a6862016-01-30 12:50:25 +00001013 if( p->mxAlloc>0 && !isMalloced(p) ){
drh043e5862016-11-25 15:11:26 +00001014 return strAccumFinishRealloc(p);
drhade86482007-11-28 22:36:40 +00001015 }
1016 }
1017 return p->zText;
1018}
1019
drhf80bba92018-05-16 15:35:03 +00001020/*
1021** This singleton is an sqlite3_str object that is returned if
1022** sqlite3_malloc() fails to provide space for a real one. This
1023** sqlite3_str object accepts no new text and always returns
1024** an SQLITE_NOMEM error.
1025*/
1026static sqlite3_str sqlite3OomStr = {
drh3e62ddb2018-05-30 00:59:09 +00001027 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
drhf80bba92018-05-16 15:35:03 +00001028};
1029
drh0cdbe1a2018-05-09 13:46:26 +00001030/* Finalize a string created using sqlite3_str_new().
1031*/
1032char *sqlite3_str_finish(sqlite3_str *p){
1033 char *z;
drhf80bba92018-05-16 15:35:03 +00001034 if( p!=0 && p!=&sqlite3OomStr ){
drh0cdbe1a2018-05-09 13:46:26 +00001035 z = sqlite3StrAccumFinish(p);
drh446135d2018-05-09 14:29:40 +00001036 sqlite3_free(p);
drh0cdbe1a2018-05-09 13:46:26 +00001037 }else{
1038 z = 0;
1039 }
1040 return z;
1041}
1042
1043/* Return any error code associated with p */
1044int sqlite3_str_errcode(sqlite3_str *p){
1045 return p ? p->accError : SQLITE_NOMEM;
1046}
1047
1048/* Return the current length of p in bytes */
1049int sqlite3_str_length(sqlite3_str *p){
1050 return p ? p->nChar : 0;
1051}
1052
1053/* Return the current value for p */
1054char *sqlite3_str_value(sqlite3_str *p){
drh446135d2018-05-09 14:29:40 +00001055 if( p==0 || p->nChar==0 ) return 0;
1056 p->zText[p->nChar] = 0;
1057 return p->zText;
drh0cdbe1a2018-05-09 13:46:26 +00001058}
1059
drhade86482007-11-28 22:36:40 +00001060/*
1061** Reset an StrAccum string. Reclaim all malloced memory.
1062*/
drh0cdbe1a2018-05-09 13:46:26 +00001063void sqlite3_str_reset(StrAccum *p){
drh5f4a6862016-01-30 12:50:25 +00001064 if( isMalloced(p) ){
drhc0490572015-05-02 11:45:53 +00001065 sqlite3DbFree(p->db, p->zText);
drh5f4a6862016-01-30 12:50:25 +00001066 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
drhade86482007-11-28 22:36:40 +00001067 }
drh446135d2018-05-09 14:29:40 +00001068 p->nAlloc = 0;
1069 p->nChar = 0;
drhf089aa42008-07-08 19:34:06 +00001070 p->zText = 0;
drhade86482007-11-28 22:36:40 +00001071}
1072
1073/*
drhc0490572015-05-02 11:45:53 +00001074** Initialize a string accumulator.
1075**
1076** p: The accumulator to be initialized.
1077** db: Pointer to a database connection. May be NULL. Lookaside
1078** memory is used if not NULL. db->mallocFailed is set appropriately
1079** when not NULL.
1080** zBase: An initial buffer. May be NULL in which case the initial buffer
1081** is malloced.
1082** n: Size of zBase in bytes. If total space requirements never exceed
1083** n then no memory allocations ever occur.
1084** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
1085** allocations will ever occur.
drhade86482007-11-28 22:36:40 +00001086*/
drhc0490572015-05-02 11:45:53 +00001087void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
drh3f18e6d2017-08-12 02:01:55 +00001088 p->zText = zBase;
drhc0490572015-05-02 11:45:53 +00001089 p->db = db;
drhade86482007-11-28 22:36:40 +00001090 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +00001091 p->mxAlloc = mx;
drh3f18e6d2017-08-12 02:01:55 +00001092 p->nChar = 0;
drhb49bc862013-08-21 21:12:10 +00001093 p->accError = 0;
drh5f4a6862016-01-30 12:50:25 +00001094 p->printfFlags = 0;
drh5f968432004-02-21 19:02:30 +00001095}
1096
drh0cdbe1a2018-05-09 13:46:26 +00001097/* Allocate and initialize a new dynamic string object */
1098sqlite3_str *sqlite3_str_new(sqlite3 *db){
drh446135d2018-05-09 14:29:40 +00001099 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
drh0cdbe1a2018-05-09 13:46:26 +00001100 if( p ){
drh446135d2018-05-09 14:29:40 +00001101 sqlite3StrAccumInit(p, 0, 0, 0,
1102 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
drhf80bba92018-05-16 15:35:03 +00001103 }else{
1104 p = &sqlite3OomStr;
drh0cdbe1a2018-05-09 13:46:26 +00001105 }
1106 return p;
1107}
1108
drh5f968432004-02-21 19:02:30 +00001109/*
1110** Print into memory obtained from sqliteMalloc(). Use the internal
1111** %-conversion extensions.
1112*/
drh17435752007-08-16 04:30:38 +00001113char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
1114 char *z;
drh79158e12005-09-06 21:40:45 +00001115 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001116 StrAccum acc;
drhbc6160b2009-04-08 15:45:31 +00001117 assert( db!=0 );
drhc0490572015-05-02 11:45:53 +00001118 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:31 +00001119 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh5f4a6862016-01-30 12:50:25 +00001120 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
drh0cdbe1a2018-05-09 13:46:26 +00001121 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001122 z = sqlite3StrAccumFinish(&acc);
drh0cdbe1a2018-05-09 13:46:26 +00001123 if( acc.accError==SQLITE_NOMEM ){
drh4a642b62016-02-05 01:55:27 +00001124 sqlite3OomFault(db);
drh17435752007-08-16 04:30:38 +00001125 }
1126 return z;
drh5f968432004-02-21 19:02:30 +00001127}
1128
1129/*
1130** Print into memory obtained from sqliteMalloc(). Use the internal
1131** %-conversion extensions.
1132*/
drh17435752007-08-16 04:30:38 +00001133char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001134 va_list ap;
1135 char *z;
drh5f968432004-02-21 19:02:30 +00001136 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +00001137 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +00001138 va_end(ap);
1139 return z;
1140}
1141
1142/*
drh28dd4792006-06-26 21:35:44 +00001143** Print into memory obtained from sqlite3_malloc(). Omit the internal
1144** %-conversion extensions.
1145*/
1146char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +00001147 char *z;
drh28dd4792006-06-26 21:35:44 +00001148 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001149 StrAccum acc;
drh9ca95732014-10-24 00:35:58 +00001150
1151#ifdef SQLITE_ENABLE_API_ARMOR
1152 if( zFormat==0 ){
1153 (void)SQLITE_MISUSE_BKPT;
1154 return 0;
1155 }
1156#endif
drhff1590e2008-07-14 12:52:53 +00001157#ifndef SQLITE_OMIT_AUTOINIT
1158 if( sqlite3_initialize() ) return 0;
1159#endif
drhc0490572015-05-02 11:45:53 +00001160 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drh0cdbe1a2018-05-09 13:46:26 +00001161 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001162 z = sqlite3StrAccumFinish(&acc);
1163 return z;
drh28dd4792006-06-26 21:35:44 +00001164}
1165
1166/*
1167** Print into memory obtained from sqlite3_malloc()(). Omit the internal
1168** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +00001169*/
danielk19776f8a5032004-05-10 10:34:51 +00001170char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +00001171 va_list ap;
drh5f968432004-02-21 19:02:30 +00001172 char *z;
drhff1590e2008-07-14 12:52:53 +00001173#ifndef SQLITE_OMIT_AUTOINIT
1174 if( sqlite3_initialize() ) return 0;
1175#endif
drh28dd4792006-06-26 21:35:44 +00001176 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +00001177 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +00001178 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001179 return z;
drha18c5682000-10-08 22:20:57 +00001180}
1181
drh93a5c6b2003-12-23 02:17:35 +00001182/*
danielk19776f8a5032004-05-10 10:34:51 +00001183** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +00001184** current locale settings. This is important for SQLite because we
1185** are not able to use a "," as the decimal point in place of "." as
1186** specified by some locales.
drhdb26d4c2011-01-05 12:20:09 +00001187**
1188** Oops: The first two arguments of sqlite3_snprintf() are backwards
1189** from the snprintf() standard. Unfortunately, it is too late to change
1190** this without breaking compatibility, so we just have to live with the
1191** mistake.
1192**
1193** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:35 +00001194*/
drhdb26d4c2011-01-05 12:20:09 +00001195char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1196 StrAccum acc;
1197 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:58 +00001198#ifdef SQLITE_ENABLE_API_ARMOR
1199 if( zBuf==0 || zFormat==0 ) {
1200 (void)SQLITE_MISUSE_BKPT;
drh96c707a2015-02-13 16:36:14 +00001201 if( zBuf ) zBuf[0] = 0;
drh9ca95732014-10-24 00:35:58 +00001202 return zBuf;
1203 }
1204#endif
drhc0490572015-05-02 11:45:53 +00001205 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drh0cdbe1a2018-05-09 13:46:26 +00001206 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe9bb5662016-11-25 15:47:53 +00001207 zBuf[acc.nChar] = 0;
1208 return zBuf;
drhdb26d4c2011-01-05 12:20:09 +00001209}
danielk19776f8a5032004-05-10 10:34:51 +00001210char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001211 char *z;
drh93a5c6b2003-12-23 02:17:35 +00001212 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +00001213 va_start(ap,zFormat);
drhdb26d4c2011-01-05 12:20:09 +00001214 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +00001215 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001216 return z;
drh93a5c6b2003-12-23 02:17:35 +00001217}
1218
drh3f280702010-02-18 18:45:09 +00001219/*
drh7c0c4602010-03-03 22:25:18 +00001220** This is the routine that actually formats the sqlite3_log() message.
1221** We house it in a separate routine from sqlite3_log() to avoid using
1222** stack space on small-stack systems when logging is disabled.
1223**
1224** sqlite3_log() must render into a static buffer. It cannot dynamically
1225** allocate memory because it might be called while the memory allocator
1226** mutex is held.
drha8dbd522015-07-14 22:43:37 +00001227**
drh0cdbe1a2018-05-09 13:46:26 +00001228** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
drha8dbd522015-07-14 22:43:37 +00001229** certain format characters (%q) or for very large precisions or widths.
1230** Care must be taken that any sqlite3_log() calls that occur while the
1231** memory mutex is held do not use these mechanisms.
drh7c0c4602010-03-03 22:25:18 +00001232*/
1233static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:32 +00001234 StrAccum acc; /* String accumulator */
1235 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
drh7c0c4602010-03-03 22:25:18 +00001236
drhc0490572015-05-02 11:45:53 +00001237 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
drh0cdbe1a2018-05-09 13:46:26 +00001238 sqlite3_str_vappendf(&acc, zFormat, ap);
drh7c0c4602010-03-03 22:25:18 +00001239 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1240 sqlite3StrAccumFinish(&acc));
1241}
1242
1243/*
drh3f280702010-02-18 18:45:09 +00001244** Format and write a message to the log if logging is enabled.
1245*/
drha7564662010-02-22 19:32:31 +00001246void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:09 +00001247 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:18 +00001248 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:09 +00001249 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:18 +00001250 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:09 +00001251 va_end(ap);
drh3f280702010-02-18 18:45:09 +00001252 }
1253}
1254
mistachkin02b0e262015-04-16 03:37:19 +00001255#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drhe54ca3f2004-06-07 01:52:14 +00001256/*
1257** A version of printf() that understands %lld. Used for debugging.
1258** The printf() built into some versions of windows does not understand %lld
1259** and segfaults if you give it a long long int.
1260*/
1261void sqlite3DebugPrintf(const char *zFormat, ...){
1262 va_list ap;
drhade86482007-11-28 22:36:40 +00001263 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +00001264 char zBuf[500];
drhc0490572015-05-02 11:45:53 +00001265 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +00001266 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001267 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +00001268 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +00001269 sqlite3StrAccumFinish(&acc);
mistachkin4a9ff912017-11-09 17:29:04 +00001270#ifdef SQLITE_OS_TRACE_PROC
1271 {
1272 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
1273 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
1274 }
1275#else
drh485f0032007-01-26 19:23:33 +00001276 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +00001277 fflush(stdout);
mistachkin4a9ff912017-11-09 17:29:04 +00001278#endif
drhe54ca3f2004-06-07 01:52:14 +00001279}
1280#endif
drhc7bc4fd2009-11-25 18:03:42 +00001281
drh4fa4a542014-09-30 12:33:33 +00001282
drhc7bc4fd2009-11-25 18:03:42 +00001283/*
drh0cdbe1a2018-05-09 13:46:26 +00001284** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
drhd37bea52015-09-02 15:37:50 +00001285** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
drhc7bc4fd2009-11-25 18:03:42 +00001286*/
drh0cdbe1a2018-05-09 13:46:26 +00001287void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:42 +00001288 va_list ap;
1289 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001290 sqlite3_str_vappendf(p, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:42 +00001291 va_end(ap);
1292}