blob: fc77f68dffa2241e526c063ebbef15170c0b87db [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 ){
drhef7d5182019-05-27 00:29:15 +0000530 double rx = (double)realvalue;
531 sqlite3_uint64 u;
532 int ex;
533 memcpy(&u, &rx, sizeof(u));
534 ex = -1023 + (int)((u>>52)&0x7ff);
535 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
drha0ed86b2019-05-24 22:58:16 +0000536 realvalue += rounder;
537 }
drha18c5682000-10-08 22:20:57 +0000538 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
539 exp = 0;
drhea678832008-12-10 19:26:22 +0000540 if( sqlite3IsNaN((double)realvalue) ){
drh53c14022007-05-10 17:23:11 +0000541 bufpt = "NaN";
542 length = 3;
543 break;
544 }
drha18c5682000-10-08 22:20:57 +0000545 if( realvalue>0.0 ){
drh72b3fbc2012-06-19 03:11:25 +0000546 LONGDOUBLE_TYPE scale = 1.0;
drh4ef94132012-06-19 03:35:05 +0000547 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
drh2a8f6712015-09-02 21:00:48 +0000548 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
drh72b3fbc2012-06-19 03:11:25 +0000549 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
550 realvalue /= scale;
drh93a960a2008-07-10 00:32:42 +0000551 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
552 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
drhaf005fb2008-07-09 16:51:51 +0000553 if( exp>350 ){
drh2a8f6712015-09-02 21:00:48 +0000554 bufpt = buf;
555 buf[0] = prefix;
556 memcpy(buf+(prefix!=0),"Inf",4);
557 length = 3+(prefix!=0);
drha18c5682000-10-08 22:20:57 +0000558 break;
559 }
drh9adf9ac2002-05-15 11:44:13 +0000560 }
drha18c5682000-10-08 22:20:57 +0000561 bufpt = buf;
562 /*
563 ** If the field type is etGENERIC, then convert to either etEXP
564 ** or etFLOAT, as appropriate.
565 */
drha18c5682000-10-08 22:20:57 +0000566 if( xtype!=etFLOAT ){
567 realvalue += rounder;
568 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
569 }
570 if( xtype==etGENERIC ){
571 flag_rtz = !flag_alternateform;
572 if( exp<-4 || exp>precision ){
573 xtype = etEXP;
574 }else{
575 precision = precision - exp;
576 xtype = etFLOAT;
577 }
drh9adf9ac2002-05-15 11:44:13 +0000578 }else{
drh72b3fbc2012-06-19 03:11:25 +0000579 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13 +0000580 }
drh557cc602005-08-13 12:59:14 +0000581 if( xtype==etEXP ){
582 e2 = 0;
583 }else{
584 e2 = exp;
585 }
drh29642252019-02-01 20:29:04 +0000586 {
587 i64 szBufNeeded; /* Size of a temporary buffer needed */
588 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
589 if( szBufNeeded > etBUFSIZE ){
590 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
591 if( bufpt==0 ) return;
drh59eedf72011-10-11 17:54:54 +0000592 }
593 }
594 zOut = bufpt;
drh72b3fbc2012-06-19 03:11:25 +0000595 nsd = 16 + flag_altform2*10;
drhea678832008-12-10 19:26:22 +0000596 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14 +0000597 /* The sign in front of the number */
598 if( prefix ){
599 *(bufpt++) = prefix;
600 }
601 /* Digits prior to the decimal point */
602 if( e2<0 ){
603 *(bufpt++) = '0';
604 }else{
605 for(; e2>=0; e2--){
606 *(bufpt++) = et_getdigit(&realvalue,&nsd);
drha18c5682000-10-08 22:20:57 +0000607 }
drh9adf9ac2002-05-15 11:44:13 +0000608 }
drh557cc602005-08-13 12:59:14 +0000609 /* The decimal point */
610 if( flag_dp ){
611 *(bufpt++) = '.';
612 }
613 /* "0" digits after the decimal point but before the first
614 ** significant digit of the number */
drhaf005fb2008-07-09 16:51:51 +0000615 for(e2++; e2<0; precision--, e2++){
616 assert( precision>0 );
drh557cc602005-08-13 12:59:14 +0000617 *(bufpt++) = '0';
618 }
619 /* Significant digits after the decimal point */
620 while( (precision--)>0 ){
621 *(bufpt++) = et_getdigit(&realvalue,&nsd);
622 }
623 /* Remove trailing zeros and the "." if no digits follow the "." */
624 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02 +0000625 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54 +0000626 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02 +0000627 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14 +0000628 if( flag_altform2 ){
629 *(bufpt++) = '0';
630 }else{
631 *(--bufpt) = 0;
632 }
633 }
634 }
635 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54 +0000636 if( xtype==etEXP ){
drh557cc602005-08-13 12:59:14 +0000637 *(bufpt++) = aDigits[infop->charset];
638 if( exp<0 ){
639 *(bufpt++) = '-'; exp = -exp;
640 }else{
641 *(bufpt++) = '+';
642 }
643 if( exp>=100 ){
drhea678832008-12-10 19:26:22 +0000644 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14 +0000645 exp %= 100;
646 }
drhea678832008-12-10 19:26:22 +0000647 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
648 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14 +0000649 }
650 *bufpt = 0;
651
drha18c5682000-10-08 22:20:57 +0000652 /* The converted number is in buf[] and zero terminated. Output it.
653 ** Note that the number is in the usual order, not reversed as with
654 ** integer conversions. */
drh59eedf72011-10-11 17:54:54 +0000655 length = (int)(bufpt-zOut);
656 bufpt = zOut;
drha18c5682000-10-08 22:20:57 +0000657
658 /* Special case: Add leading zeros if the flag_zeropad flag is
659 ** set and we are not left justified */
660 if( flag_zeropad && !flag_leftjustify && length < width){
661 int i;
662 int nPad = width - length;
663 for(i=width; i>=nPad; i--){
664 bufpt[i] = bufpt[i-nPad];
665 }
666 i = prefix!=0;
667 while( nPad-- ) bufpt[i++] = '0';
668 length = width;
669 }
drh69ef7032010-03-04 17:11:31 +0000670#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
drha18c5682000-10-08 22:20:57 +0000671 break;
672 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42 +0000673 if( !bArgList ){
674 *(va_arg(ap,int*)) = pAccum->nChar;
675 }
drha18c5682000-10-08 22:20:57 +0000676 length = width = 0;
677 break;
678 case etPERCENT:
679 buf[0] = '%';
680 bufpt = buf;
681 length = 1;
682 break;
drha18c5682000-10-08 22:20:57 +0000683 case etCHARX:
drha5c14162013-12-17 15:03:06 +0000684 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42 +0000685 bufpt = getTextArg(pArgList);
drha15a7c32018-02-19 21:58:16 +0000686 length = 1;
drh136102b2018-02-19 18:56:52 +0000687 if( bufpt ){
688 buf[0] = c = *(bufpt++);
drh136102b2018-02-19 18:56:52 +0000689 if( (c&0xc0)==0xc0 ){
690 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
691 buf[length++] = *(bufpt++);
692 }
693 }
drha15a7c32018-02-19 21:58:16 +0000694 }else{
695 buf[0] = 0;
drh136102b2018-02-19 18:56:52 +0000696 }
drha5c14162013-12-17 15:03:06 +0000697 }else{
drh136102b2018-02-19 18:56:52 +0000698 unsigned int ch = va_arg(ap,unsigned int);
699 if( ch<0x00080 ){
700 buf[0] = ch & 0xff;
701 length = 1;
702 }else if( ch<0x00800 ){
703 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
704 buf[1] = 0x80 + (u8)(ch & 0x3f);
705 length = 2;
706 }else if( ch<0x10000 ){
707 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
708 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
709 buf[2] = 0x80 + (u8)(ch & 0x3f);
710 length = 3;
711 }else{
712 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
713 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
714 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
715 buf[3] = 0x80 + (u8)(ch & 0x3f);
716 length = 4;
717 }
drha5c14162013-12-17 15:03:06 +0000718 }
drhaf8f5132014-10-29 18:20:18 +0000719 if( precision>1 ){
720 width -= precision-1;
721 if( width>1 && !flag_leftjustify ){
drh0cdbe1a2018-05-09 13:46:26 +0000722 sqlite3_str_appendchar(pAccum, width-1, ' ');
drhaf8f5132014-10-29 18:20:18 +0000723 width = 0;
724 }
drh136102b2018-02-19 18:56:52 +0000725 while( precision-- > 1 ){
drh0cdbe1a2018-05-09 13:46:26 +0000726 sqlite3_str_append(pAccum, buf, length);
drh136102b2018-02-19 18:56:52 +0000727 }
drh9adf9ac2002-05-15 11:44:13 +0000728 }
drha18c5682000-10-08 22:20:57 +0000729 bufpt = buf;
drhcf7c8372018-02-19 20:23:20 +0000730 flag_altform2 = 1;
731 goto adjust_width_for_utf8;
drha18c5682000-10-08 22:20:57 +0000732 case etSTRING:
drhd93d8a82003-06-16 03:08:18 +0000733 case etDYNSTRING:
drha5c14162013-12-17 15:03:06 +0000734 if( bArgList ){
735 bufpt = getTextArg(pArgList);
drh2a8f6712015-09-02 21:00:48 +0000736 xtype = etSTRING;
drha5c14162013-12-17 15:03:06 +0000737 }else{
738 bufpt = va_arg(ap,char*);
739 }
drhd93d8a82003-06-16 03:08:18 +0000740 if( bufpt==0 ){
741 bufpt = "";
drh2a8f6712015-09-02 21:00:48 +0000742 }else if( xtype==etDYNSTRING ){
drhaf524a62018-09-13 17:07:12 +0000743 if( pAccum->nChar==0
744 && pAccum->mxAlloc
745 && width==0
746 && precision<0
747 && pAccum->accError==0
748 ){
drhcc398962018-02-20 15:23:37 +0000749 /* Special optimization for sqlite3_mprintf("%z..."):
750 ** Extend an existing memory allocation rather than creating
751 ** a new one. */
752 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
753 pAccum->zText = bufpt;
754 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
755 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
756 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
757 length = 0;
758 break;
759 }
drhd93d8a82003-06-16 03:08:18 +0000760 zExtra = bufpt;
761 }
drhe5090942008-04-29 15:22:27 +0000762 if( precision>=0 ){
drh62856462018-02-19 17:03:23 +0000763 if( flag_altform2 ){
764 /* Set length to the number of bytes needed in order to display
765 ** precision characters */
766 unsigned char *z = (unsigned char*)bufpt;
767 while( precision-- > 0 && z[0] ){
768 SQLITE_SKIP_UTF8(z);
769 }
770 length = (int)(z - (unsigned char*)bufpt);
771 }else{
772 for(length=0; length<precision && bufpt[length]; length++){}
773 }
drhe5090942008-04-29 15:22:27 +0000774 }else{
drhc84ddf12017-08-19 20:38:18 +0000775 length = 0x7fffffff & (int)strlen(bufpt);
drhe5090942008-04-29 15:22:27 +0000776 }
drh57e3ba72018-02-19 18:03:10 +0000777 adjust_width_for_utf8:
drh62856462018-02-19 17:03:23 +0000778 if( flag_altform2 && width>0 ){
779 /* Adjust width to account for extra bytes in UTF-8 characters */
780 int ii = length - 1;
781 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
782 }
drha18c5682000-10-08 22:20:57 +0000783 break;
drh57e3ba72018-02-19 18:03:10 +0000784 case etSQLESCAPE: /* %q: Escape ' characters */
785 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
786 case etSQLESCAPE3: { /* %w: Escape " characters */
drh8965b502009-11-25 16:53:37 +0000787 int i, j, k, n, isnull;
drh5eba8c02005-08-19 02:26:27 +0000788 int needQuote;
drhea678832008-12-10 19:26:22 +0000789 char ch;
danielk1977f3b863e2007-06-24 06:32:17 +0000790 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
drha5c14162013-12-17 15:03:06 +0000791 char *escarg;
792
793 if( bArgList ){
794 escarg = getTextArg(pArgList);
795 }else{
796 escarg = va_arg(ap,char*);
797 }
danielk1977f0113002006-01-24 12:09:17 +0000798 isnull = escarg==0;
799 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
drh57e3ba72018-02-19 18:03:10 +0000800 /* For %q, %Q, and %w, the precision is the number of byte (or
801 ** characters if the ! flags is present) to use from the input.
802 ** Because of the extra quoting characters inserted, the number
803 ** of output characters may be larger than the precision.
804 */
drh8965b502009-11-25 16:53:37 +0000805 k = precision;
dan60d4a302010-03-04 17:58:45 +0000806 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17 +0000807 if( ch==q ) n++;
drh57e3ba72018-02-19 18:03:10 +0000808 if( flag_altform2 && (ch&0xc0)==0xc0 ){
809 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
810 }
drha18c5682000-10-08 22:20:57 +0000811 }
drh5eba8c02005-08-19 02:26:27 +0000812 needQuote = !isnull && xtype==etSQLESCAPE2;
drh2a8f6712015-09-02 21:00:48 +0000813 n += i + 3;
drh5eba8c02005-08-19 02:26:27 +0000814 if( n>etBUFSIZE ){
drh29642252019-02-01 20:29:04 +0000815 bufpt = zExtra = printfTempBuf(pAccum, n);
816 if( bufpt==0 ) return;
drh5eba8c02005-08-19 02:26:27 +0000817 }else{
818 bufpt = buf;
819 }
820 j = 0;
danielk1977f3b863e2007-06-24 06:32:17 +0000821 if( needQuote ) bufpt[j++] = q;
drh8965b502009-11-25 16:53:37 +0000822 k = i;
823 for(i=0; i<k; i++){
824 bufpt[j++] = ch = escarg[i];
danielk1977f3b863e2007-06-24 06:32:17 +0000825 if( ch==q ) bufpt[j++] = ch;
drh5eba8c02005-08-19 02:26:27 +0000826 }
danielk1977f3b863e2007-06-24 06:32:17 +0000827 if( needQuote ) bufpt[j++] = q;
drh5eba8c02005-08-19 02:26:27 +0000828 bufpt[j] = 0;
829 length = j;
drh57e3ba72018-02-19 18:03:10 +0000830 goto adjust_width_for_utf8;
drh5eba8c02005-08-19 02:26:27 +0000831 }
drh5f968432004-02-21 19:02:30 +0000832 case etTOKEN: {
drh8236f682017-01-04 00:26:28 +0000833 Token *pToken;
834 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
835 pToken = va_arg(ap, Token*);
drha5c14162013-12-17 15:03:06 +0000836 assert( bArgList==0 );
drha9ab4812013-12-11 11:00:44 +0000837 if( pToken && pToken->n ){
drh0cdbe1a2018-05-09 13:46:26 +0000838 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
drhad6d9462004-09-19 02:15:24 +0000839 }
drh5f968432004-02-21 19:02:30 +0000840 length = width = 0;
841 break;
842 }
843 case etSRCLIST: {
drh8236f682017-01-04 00:26:28 +0000844 SrcList *pSrc;
845 int k;
846 struct SrcList_item *pItem;
847 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
848 pSrc = va_arg(ap, SrcList*);
849 k = va_arg(ap, int);
850 pItem = &pSrc->a[k];
drha5c14162013-12-17 15:03:06 +0000851 assert( bArgList==0 );
drh5f968432004-02-21 19:02:30 +0000852 assert( k>=0 && k<pSrc->nSrc );
drh93a960a2008-07-10 00:32:42 +0000853 if( pItem->zDatabase ){
drh0cdbe1a2018-05-09 13:46:26 +0000854 sqlite3_str_appendall(pAccum, pItem->zDatabase);
855 sqlite3_str_append(pAccum, ".", 1);
drh5f968432004-02-21 19:02:30 +0000856 }
drh0cdbe1a2018-05-09 13:46:26 +0000857 sqlite3_str_appendall(pAccum, pItem->zName);
drh5f968432004-02-21 19:02:30 +0000858 length = width = 0;
859 break;
860 }
drh874ba042009-04-08 16:10:04 +0000861 default: {
862 assert( xtype==etINVALID );
863 return;
864 }
drha18c5682000-10-08 22:20:57 +0000865 }/* End switch over the format type */
866 /*
867 ** The text of the conversion is pointed to by "bufpt" and is
868 ** "length" characters long. The field width is "width". Do
drh62856462018-02-19 17:03:23 +0000869 ** the output. Both length and width are in bytes, not characters,
870 ** at this point. If the "!" flag was present on string conversions
871 ** indicating that width and precision should be expressed in characters,
872 ** then the values have been translated prior to reaching this point.
drha18c5682000-10-08 22:20:57 +0000873 */
drha70a0732014-03-17 14:24:27 +0000874 width -= length;
drh8236f682017-01-04 00:26:28 +0000875 if( width>0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000876 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
877 sqlite3_str_append(pAccum, bufpt, length);
878 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
drh8236f682017-01-04 00:26:28 +0000879 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000880 sqlite3_str_append(pAccum, bufpt, length);
drh8236f682017-01-04 00:26:28 +0000881 }
drha70a0732014-03-17 14:24:27 +0000882
drhaf8f5132014-10-29 18:20:18 +0000883 if( zExtra ){
drh96ceaf82015-11-14 22:04:22 +0000884 sqlite3DbFree(pAccum->db, zExtra);
drhaf8f5132014-10-29 18:20:18 +0000885 zExtra = 0;
886 }
drha18c5682000-10-08 22:20:57 +0000887 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57 +0000888} /* End of function */
889
drhade86482007-11-28 22:36:40 +0000890/*
drha70a0732014-03-17 14:24:27 +0000891** Enlarge the memory allocation on a StrAccum object so that it is
892** able to accept at least N more bytes of text.
893**
894** Return the number of bytes of text that StrAccum is able to accept
895** after the attempted enlargement. The value returned might be zero.
896*/
897static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
898 char *zNew;
drha30d22a2015-04-07 13:28:41 +0000899 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
drha70a0732014-03-17 14:24:27 +0000900 if( p->accError ){
drh0cdbe1a2018-05-09 13:46:26 +0000901 testcase(p->accError==SQLITE_TOOBIG);
902 testcase(p->accError==SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000903 return 0;
904 }
drhc0490572015-05-02 11:45:53 +0000905 if( p->mxAlloc==0 ){
drh0cdbe1a2018-05-09 13:46:26 +0000906 setStrAccumError(p, SQLITE_TOOBIG);
drh255a81f2019-02-22 15:42:10 +0000907 return p->nAlloc - p->nChar - 1;
drha70a0732014-03-17 14:24:27 +0000908 }else{
drh5f4a6862016-01-30 12:50:25 +0000909 char *zOld = isMalloced(p) ? p->zText : 0;
drha70a0732014-03-17 14:24:27 +0000910 i64 szNew = p->nChar;
911 szNew += N + 1;
drh7b4d7802014-11-03 14:46:29 +0000912 if( szNew+p->nChar<=p->mxAlloc ){
913 /* Force exponential buffer size growth as long as it does not overflow,
914 ** to avoid having to call this routine too often */
915 szNew += p->nChar;
916 }
drha70a0732014-03-17 14:24:27 +0000917 if( szNew > p->mxAlloc ){
drh0cdbe1a2018-05-09 13:46:26 +0000918 sqlite3_str_reset(p);
919 setStrAccumError(p, SQLITE_TOOBIG);
drha70a0732014-03-17 14:24:27 +0000920 return 0;
921 }else{
922 p->nAlloc = (int)szNew;
923 }
drhc0490572015-05-02 11:45:53 +0000924 if( p->db ){
drha70a0732014-03-17 14:24:27 +0000925 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
926 }else{
drhf3cdcdc2015-04-29 16:50:28 +0000927 zNew = sqlite3_realloc64(zOld, p->nAlloc);
drha70a0732014-03-17 14:24:27 +0000928 }
929 if( zNew ){
drh7ef4d1c2014-05-31 15:39:53 +0000930 assert( p->zText!=0 || p->nChar==0 );
drh5f4a6862016-01-30 12:50:25 +0000931 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
drha70a0732014-03-17 14:24:27 +0000932 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:12 +0000933 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drh5f4a6862016-01-30 12:50:25 +0000934 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
drha70a0732014-03-17 14:24:27 +0000935 }else{
drh0cdbe1a2018-05-09 13:46:26 +0000936 sqlite3_str_reset(p);
937 setStrAccumError(p, SQLITE_NOMEM);
drha70a0732014-03-17 14:24:27 +0000938 return 0;
939 }
940 }
941 return N;
942}
943
944/*
drhaf8f5132014-10-29 18:20:18 +0000945** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:27 +0000946*/
drh0cdbe1a2018-05-09 13:46:26 +0000947void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
drha30d22a2015-04-07 13:28:41 +0000948 testcase( p->nChar + (i64)N > 0x7fffffff );
949 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
950 return;
951 }
drhaf8f5132014-10-29 18:20:18 +0000952 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:27 +0000953}
954
955/*
956** The StrAccum "p" is not large enough to accept N new bytes of z[].
957** So enlarge if first, then do the append.
958**
drh0cdbe1a2018-05-09 13:46:26 +0000959** This is a helper routine to sqlite3_str_append() that does special-case
drha70a0732014-03-17 14:24:27 +0000960** work (enlarging the buffer) using tail recursion, so that the
drh0cdbe1a2018-05-09 13:46:26 +0000961** sqlite3_str_append() routine can use fast calling semantics.
drha70a0732014-03-17 14:24:27 +0000962*/
drh172087f2014-08-22 15:40:20 +0000963static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:27 +0000964 N = sqlite3StrAccumEnlarge(p, N);
965 if( N>0 ){
966 memcpy(&p->zText[p->nChar], z, N);
967 p->nChar += N;
968 }
969}
970
971/*
972** Append N bytes of text from z to the StrAccum object. Increase the
973** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:57 +0000974*/
drh0cdbe1a2018-05-09 13:46:26 +0000975void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
drh34573382015-04-15 05:38:35 +0000976 assert( z!=0 || N==0 );
drha6353a32013-12-09 19:03:26 +0000977 assert( p->zText!=0 || p->nChar==0 || p->accError );
978 assert( N>=0 );
drh255a81f2019-02-22 15:42:10 +0000979 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
drhade86482007-11-28 22:36:40 +0000980 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:27 +0000981 enlargeAndAppend(p,z,N);
dan895decf2016-12-30 14:15:56 +0000982 }else if( N ){
drh172087f2014-08-22 15:40:20 +0000983 assert( p->zText );
984 p->nChar += N;
985 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:30 +0000986 }
drh483750b2003-01-29 18:46:51 +0000987}
988
989/*
drha6353a32013-12-09 19:03:26 +0000990** Append the complete text of zero-terminated string z[] to the p string.
991*/
drh0cdbe1a2018-05-09 13:46:26 +0000992void sqlite3_str_appendall(sqlite3_str *p, const char *z){
993 sqlite3_str_append(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:26 +0000994}
995
996
997/*
drhade86482007-11-28 22:36:40 +0000998** Finish off a string by making sure it is zero-terminated.
999** Return a pointer to the resulting string. Return a NULL
1000** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:30 +00001001*/
drh043e5862016-11-25 15:11:26 +00001002static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
drh3f18e6d2017-08-12 02:01:55 +00001003 char *zText;
drh043e5862016-11-25 15:11:26 +00001004 assert( p->mxAlloc>0 && !isMalloced(p) );
drh3f18e6d2017-08-12 02:01:55 +00001005 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
1006 if( zText ){
1007 memcpy(zText, p->zText, p->nChar+1);
drh043e5862016-11-25 15:11:26 +00001008 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
1009 }else{
drh0cdbe1a2018-05-09 13:46:26 +00001010 setStrAccumError(p, SQLITE_NOMEM);
drh043e5862016-11-25 15:11:26 +00001011 }
drh3f18e6d2017-08-12 02:01:55 +00001012 p->zText = zText;
1013 return zText;
drh043e5862016-11-25 15:11:26 +00001014}
drhade86482007-11-28 22:36:40 +00001015char *sqlite3StrAccumFinish(StrAccum *p){
1016 if( p->zText ){
1017 p->zText[p->nChar] = 0;
drh5f4a6862016-01-30 12:50:25 +00001018 if( p->mxAlloc>0 && !isMalloced(p) ){
drh043e5862016-11-25 15:11:26 +00001019 return strAccumFinishRealloc(p);
drhade86482007-11-28 22:36:40 +00001020 }
1021 }
1022 return p->zText;
1023}
1024
drhf80bba92018-05-16 15:35:03 +00001025/*
1026** This singleton is an sqlite3_str object that is returned if
1027** sqlite3_malloc() fails to provide space for a real one. This
1028** sqlite3_str object accepts no new text and always returns
1029** an SQLITE_NOMEM error.
1030*/
1031static sqlite3_str sqlite3OomStr = {
drh3e62ddb2018-05-30 00:59:09 +00001032 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
drhf80bba92018-05-16 15:35:03 +00001033};
1034
drh0cdbe1a2018-05-09 13:46:26 +00001035/* Finalize a string created using sqlite3_str_new().
1036*/
1037char *sqlite3_str_finish(sqlite3_str *p){
1038 char *z;
drhf80bba92018-05-16 15:35:03 +00001039 if( p!=0 && p!=&sqlite3OomStr ){
drh0cdbe1a2018-05-09 13:46:26 +00001040 z = sqlite3StrAccumFinish(p);
drh446135d2018-05-09 14:29:40 +00001041 sqlite3_free(p);
drh0cdbe1a2018-05-09 13:46:26 +00001042 }else{
1043 z = 0;
1044 }
1045 return z;
1046}
1047
1048/* Return any error code associated with p */
1049int sqlite3_str_errcode(sqlite3_str *p){
1050 return p ? p->accError : SQLITE_NOMEM;
1051}
1052
1053/* Return the current length of p in bytes */
1054int sqlite3_str_length(sqlite3_str *p){
1055 return p ? p->nChar : 0;
1056}
1057
1058/* Return the current value for p */
1059char *sqlite3_str_value(sqlite3_str *p){
drh446135d2018-05-09 14:29:40 +00001060 if( p==0 || p->nChar==0 ) return 0;
1061 p->zText[p->nChar] = 0;
1062 return p->zText;
drh0cdbe1a2018-05-09 13:46:26 +00001063}
1064
drhade86482007-11-28 22:36:40 +00001065/*
1066** Reset an StrAccum string. Reclaim all malloced memory.
1067*/
drh0cdbe1a2018-05-09 13:46:26 +00001068void sqlite3_str_reset(StrAccum *p){
drh5f4a6862016-01-30 12:50:25 +00001069 if( isMalloced(p) ){
drhc0490572015-05-02 11:45:53 +00001070 sqlite3DbFree(p->db, p->zText);
drh5f4a6862016-01-30 12:50:25 +00001071 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
drhade86482007-11-28 22:36:40 +00001072 }
drh446135d2018-05-09 14:29:40 +00001073 p->nAlloc = 0;
1074 p->nChar = 0;
drhf089aa42008-07-08 19:34:06 +00001075 p->zText = 0;
drhade86482007-11-28 22:36:40 +00001076}
1077
1078/*
drhc0490572015-05-02 11:45:53 +00001079** Initialize a string accumulator.
1080**
1081** p: The accumulator to be initialized.
1082** db: Pointer to a database connection. May be NULL. Lookaside
1083** memory is used if not NULL. db->mallocFailed is set appropriately
1084** when not NULL.
1085** zBase: An initial buffer. May be NULL in which case the initial buffer
1086** is malloced.
1087** n: Size of zBase in bytes. If total space requirements never exceed
1088** n then no memory allocations ever occur.
1089** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
1090** allocations will ever occur.
drhade86482007-11-28 22:36:40 +00001091*/
drhc0490572015-05-02 11:45:53 +00001092void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
drh3f18e6d2017-08-12 02:01:55 +00001093 p->zText = zBase;
drhc0490572015-05-02 11:45:53 +00001094 p->db = db;
drhade86482007-11-28 22:36:40 +00001095 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:29 +00001096 p->mxAlloc = mx;
drh3f18e6d2017-08-12 02:01:55 +00001097 p->nChar = 0;
drhb49bc862013-08-21 21:12:10 +00001098 p->accError = 0;
drh5f4a6862016-01-30 12:50:25 +00001099 p->printfFlags = 0;
drh5f968432004-02-21 19:02:30 +00001100}
1101
drh0cdbe1a2018-05-09 13:46:26 +00001102/* Allocate and initialize a new dynamic string object */
1103sqlite3_str *sqlite3_str_new(sqlite3 *db){
drh446135d2018-05-09 14:29:40 +00001104 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
drh0cdbe1a2018-05-09 13:46:26 +00001105 if( p ){
drh446135d2018-05-09 14:29:40 +00001106 sqlite3StrAccumInit(p, 0, 0, 0,
1107 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
drhf80bba92018-05-16 15:35:03 +00001108 }else{
1109 p = &sqlite3OomStr;
drh0cdbe1a2018-05-09 13:46:26 +00001110 }
1111 return p;
1112}
1113
drh5f968432004-02-21 19:02:30 +00001114/*
1115** Print into memory obtained from sqliteMalloc(). Use the internal
1116** %-conversion extensions.
1117*/
drh17435752007-08-16 04:30:38 +00001118char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
1119 char *z;
drh79158e12005-09-06 21:40:45 +00001120 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001121 StrAccum acc;
drhbc6160b2009-04-08 15:45:31 +00001122 assert( db!=0 );
drhc0490572015-05-02 11:45:53 +00001123 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:31 +00001124 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh5f4a6862016-01-30 12:50:25 +00001125 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
drh0cdbe1a2018-05-09 13:46:26 +00001126 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001127 z = sqlite3StrAccumFinish(&acc);
drh0cdbe1a2018-05-09 13:46:26 +00001128 if( acc.accError==SQLITE_NOMEM ){
drh4a642b62016-02-05 01:55:27 +00001129 sqlite3OomFault(db);
drh17435752007-08-16 04:30:38 +00001130 }
1131 return z;
drh5f968432004-02-21 19:02:30 +00001132}
1133
1134/*
1135** Print into memory obtained from sqliteMalloc(). Use the internal
1136** %-conversion extensions.
1137*/
drh17435752007-08-16 04:30:38 +00001138char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001139 va_list ap;
1140 char *z;
drh5f968432004-02-21 19:02:30 +00001141 va_start(ap, zFormat);
drhade86482007-11-28 22:36:40 +00001142 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:30 +00001143 va_end(ap);
1144 return z;
1145}
1146
1147/*
drh28dd4792006-06-26 21:35:44 +00001148** Print into memory obtained from sqlite3_malloc(). Omit the internal
1149** %-conversion extensions.
1150*/
1151char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:40 +00001152 char *z;
drh28dd4792006-06-26 21:35:44 +00001153 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:40 +00001154 StrAccum acc;
drh9ca95732014-10-24 00:35:58 +00001155
1156#ifdef SQLITE_ENABLE_API_ARMOR
1157 if( zFormat==0 ){
1158 (void)SQLITE_MISUSE_BKPT;
1159 return 0;
1160 }
1161#endif
drhff1590e2008-07-14 12:52:53 +00001162#ifndef SQLITE_OMIT_AUTOINIT
1163 if( sqlite3_initialize() ) return 0;
1164#endif
drhc0490572015-05-02 11:45:53 +00001165 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drh0cdbe1a2018-05-09 13:46:26 +00001166 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:40 +00001167 z = sqlite3StrAccumFinish(&acc);
1168 return z;
drh28dd4792006-06-26 21:35:44 +00001169}
1170
1171/*
1172** Print into memory obtained from sqlite3_malloc()(). Omit the internal
1173** %-conversion extensions.
drh483750b2003-01-29 18:46:51 +00001174*/
danielk19776f8a5032004-05-10 10:34:51 +00001175char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:57 +00001176 va_list ap;
drh5f968432004-02-21 19:02:30 +00001177 char *z;
drhff1590e2008-07-14 12:52:53 +00001178#ifndef SQLITE_OMIT_AUTOINIT
1179 if( sqlite3_initialize() ) return 0;
1180#endif
drh28dd4792006-06-26 21:35:44 +00001181 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:49 +00001182 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:57 +00001183 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001184 return z;
drha18c5682000-10-08 22:20:57 +00001185}
1186
drh93a5c6b2003-12-23 02:17:35 +00001187/*
danielk19776f8a5032004-05-10 10:34:51 +00001188** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:35 +00001189** current locale settings. This is important for SQLite because we
1190** are not able to use a "," as the decimal point in place of "." as
1191** specified by some locales.
drhdb26d4c2011-01-05 12:20:09 +00001192**
1193** Oops: The first two arguments of sqlite3_snprintf() are backwards
1194** from the snprintf() standard. Unfortunately, it is too late to change
1195** this without breaking compatibility, so we just have to live with the
1196** mistake.
1197**
1198** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:35 +00001199*/
drhdb26d4c2011-01-05 12:20:09 +00001200char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1201 StrAccum acc;
1202 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:58 +00001203#ifdef SQLITE_ENABLE_API_ARMOR
1204 if( zBuf==0 || zFormat==0 ) {
1205 (void)SQLITE_MISUSE_BKPT;
drh96c707a2015-02-13 16:36:14 +00001206 if( zBuf ) zBuf[0] = 0;
drh9ca95732014-10-24 00:35:58 +00001207 return zBuf;
1208 }
1209#endif
drhc0490572015-05-02 11:45:53 +00001210 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drh0cdbe1a2018-05-09 13:46:26 +00001211 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe9bb5662016-11-25 15:47:53 +00001212 zBuf[acc.nChar] = 0;
1213 return zBuf;
drhdb26d4c2011-01-05 12:20:09 +00001214}
danielk19776f8a5032004-05-10 10:34:51 +00001215char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh5f968432004-02-21 19:02:30 +00001216 char *z;
drh93a5c6b2003-12-23 02:17:35 +00001217 va_list ap;
drh93a5c6b2003-12-23 02:17:35 +00001218 va_start(ap,zFormat);
drhdb26d4c2011-01-05 12:20:09 +00001219 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
drh93a5c6b2003-12-23 02:17:35 +00001220 va_end(ap);
drh5f968432004-02-21 19:02:30 +00001221 return z;
drh93a5c6b2003-12-23 02:17:35 +00001222}
1223
drh3f280702010-02-18 18:45:09 +00001224/*
drh7c0c4602010-03-03 22:25:18 +00001225** This is the routine that actually formats the sqlite3_log() message.
1226** We house it in a separate routine from sqlite3_log() to avoid using
1227** stack space on small-stack systems when logging is disabled.
1228**
1229** sqlite3_log() must render into a static buffer. It cannot dynamically
1230** allocate memory because it might be called while the memory allocator
1231** mutex is held.
drha8dbd522015-07-14 22:43:37 +00001232**
drh0cdbe1a2018-05-09 13:46:26 +00001233** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
drha8dbd522015-07-14 22:43:37 +00001234** certain format characters (%q) or for very large precisions or widths.
1235** Care must be taken that any sqlite3_log() calls that occur while the
1236** memory mutex is held do not use these mechanisms.
drh7c0c4602010-03-03 22:25:18 +00001237*/
1238static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:32 +00001239 StrAccum acc; /* String accumulator */
1240 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
drh7c0c4602010-03-03 22:25:18 +00001241
drhc0490572015-05-02 11:45:53 +00001242 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
drh0cdbe1a2018-05-09 13:46:26 +00001243 sqlite3_str_vappendf(&acc, zFormat, ap);
drh7c0c4602010-03-03 22:25:18 +00001244 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1245 sqlite3StrAccumFinish(&acc));
1246}
1247
1248/*
drh3f280702010-02-18 18:45:09 +00001249** Format and write a message to the log if logging is enabled.
1250*/
drha7564662010-02-22 19:32:31 +00001251void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:09 +00001252 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:18 +00001253 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:09 +00001254 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:18 +00001255 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:09 +00001256 va_end(ap);
drh3f280702010-02-18 18:45:09 +00001257 }
1258}
1259
mistachkin02b0e262015-04-16 03:37:19 +00001260#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drhe54ca3f2004-06-07 01:52:14 +00001261/*
1262** A version of printf() that understands %lld. Used for debugging.
1263** The printf() built into some versions of windows does not understand %lld
1264** and segfaults if you give it a long long int.
1265*/
1266void sqlite3DebugPrintf(const char *zFormat, ...){
1267 va_list ap;
drhade86482007-11-28 22:36:40 +00001268 StrAccum acc;
drhe54ca3f2004-06-07 01:52:14 +00001269 char zBuf[500];
drhc0490572015-05-02 11:45:53 +00001270 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:40 +00001271 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001272 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe54ca3f2004-06-07 01:52:14 +00001273 va_end(ap);
drhbed8e7e2007-12-08 17:55:35 +00001274 sqlite3StrAccumFinish(&acc);
mistachkin4a9ff912017-11-09 17:29:04 +00001275#ifdef SQLITE_OS_TRACE_PROC
1276 {
1277 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
1278 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
1279 }
1280#else
drh485f0032007-01-26 19:23:33 +00001281 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:46 +00001282 fflush(stdout);
mistachkin4a9ff912017-11-09 17:29:04 +00001283#endif
drhe54ca3f2004-06-07 01:52:14 +00001284}
1285#endif
drhc7bc4fd2009-11-25 18:03:42 +00001286
drh4fa4a542014-09-30 12:33:33 +00001287
drhc7bc4fd2009-11-25 18:03:42 +00001288/*
drh0cdbe1a2018-05-09 13:46:26 +00001289** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
drhd37bea52015-09-02 15:37:50 +00001290** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
drhc7bc4fd2009-11-25 18:03:42 +00001291*/
drh0cdbe1a2018-05-09 13:46:26 +00001292void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:42 +00001293 va_list ap;
1294 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:26 +00001295 sqlite3_str_vappendf(p, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:42 +00001296 va_end(ap);
1297}