Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 669ca26b0aa4ec16d997ad6f2287aa45fa84d0ab [file] [log] [blame]
drha18c5682000-10-08 22:20:571/*
2** The "printf" code that follows dates from the 1980's. It is in
drh38b41492015-06-08 15:08:153** the public domain.
drhe84a3062004-02-02 12:29:254**
5**************************************************************************
drha18c5682000-10-08 22:20:576**
drhed1fddf2011-10-12 18:52:597** 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:5710** SQLite.
drha18c5682000-10-08 22:20:5711*/
12#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:5113
drha18c5682000-10-08 22:20:5714/*
drha18c5682000-10-08 22:20:5715** Conversion types fall into various categories as defined by the
16** following enumeration.
17*/
drh2c338a92017-02-10 19:38:3618#define etRADIX 0 /* non-decimal integer types. %x %o */
drhad5a9d72016-05-05 11:53:1219#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:5727/* The rest are extensions, not normally found in printf() */
drha3283ec2025-02-23 11:48:0728#define etESCAPE_q 9 /* Strings with '\'' doubled. %q */
29#define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '',
30 NULL pointers replaced by SQL NULL. %Q */
31#define etTOKEN 11 /* a pointer to a Token structure */
32#define etSRCITEM 12 /* a pointer to a SrcItem */
33#define etPOINTER 13 /* The %p conversion */
34#define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */
35#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
36#define etDECIMAL 16 /* %d or %u, but not %x, %o */
drhe84a3062004-02-02 12:29:2537
drha3283ec2025-02-23 11:48:0738#define etINVALID 17 /* Any unrecognized conversion type */
drh874ba042009-04-08 16:10:0439
drhe84a3062004-02-02 12:29:2540
41/*
42** An "etByte" is an 8-bit unsigned value.
43*/
44typedef unsigned char etByte;
drha18c5682000-10-08 22:20:5745
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:2551 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:3055 etByte charset; /* Offset into aDigits[] of the digits string */
56 etByte prefix; /* Offset into aPrefix[] of the prefix string */
drha18c5682000-10-08 22:20:5757} et_info;
58
59/*
drhe84a3062004-02-02 12:29:2560** Allowed values for et_info.flags
61*/
drh2c338a92017-02-10 19:38:3662#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:2564
65
66/*
drha18c5682000-10-08 22:20:5767** 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:3070static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
71static const char aPrefix[] = "-x0\000X0";
drh57196282004-10-06 15:41:1672static const et_info fmtinfo[] = {
drh2c338a92017-02-10 19:38:3673 { 'd', 10, 1, etDECIMAL, 0, 0 },
drh4794f732004-11-05 17:17:5074 { 's', 0, 4, etSTRING, 0, 0 },
drh557cc602005-08-13 12:59:1475 { 'g', 0, 1, etGENERIC, 30, 0 },
drh153c62c2007-08-24 03:51:3376 { 'z', 0, 4, etDYNSTRING, 0, 0 },
drha3283ec2025-02-23 11:48:0777 { 'q', 0, 4, etESCAPE_q, 0, 0 },
78 { 'Q', 0, 4, etESCAPE_Q, 0, 0 },
79 { 'w', 0, 4, etESCAPE_w, 0, 0 },
drh76ff3a02004-09-24 22:32:3080 { 'c', 0, 0, etCHARX, 0, 0 },
81 { 'o', 8, 0, etRADIX, 0, 2 },
drh2c338a92017-02-10 19:38:3682 { 'u', 10, 0, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:3083 { 'x', 16, 0, etRADIX, 16, 1 },
84 { 'X', 16, 0, etRADIX, 0, 4 },
drhb37df7b2005-10-13 02:09:4985#ifndef SQLITE_OMIT_FLOATING_POINT
drh76ff3a02004-09-24 22:32:3086 { '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:3089 { 'G', 0, 1, etGENERIC, 14, 0 },
drhb37df7b2005-10-13 02:09:4990#endif
drh2c338a92017-02-10 19:38:3691 { 'i', 10, 1, etDECIMAL, 0, 0 },
drh76ff3a02004-09-24 22:32:3092 { 'n', 0, 0, etSIZE, 0, 0 },
93 { '%', 0, 0, etPERCENT, 0, 0 },
94 { 'p', 16, 0, etPOINTER, 0, 1 },
drh7e3ff5d2009-04-08 11:49:4295
drh8236f682017-01-04 00:26:2896 /* All the rest are undocumented and are for internal use only */
97 { 'T', 0, 0, etTOKEN, 0, 0 },
drha9799932021-03-19 13:00:2898 { 'S', 0, 0, etSRCITEM, 0, 0 },
drh8236f682017-01-04 00:26:2899 { 'r', 10, 1, etORDINAL, 0, 0 },
drha18c5682000-10-08 22:20:57100};
drha18c5682000-10-08 22:20:57101
drha9799932021-03-19 13:00:28102/* Notes:
103**
104** %S Takes a pointer to SrcItem. Shows name or database.name
drh2f2091b2021-03-20 15:46:01105** %!S Like %S but prefer the zName over the zAlias
drha9799932021-03-19 13:00:28106*/
107
drh79158e12005-09-06 21:40:45108/*
drha6353a32013-12-09 19:03:26109** Set the StrAccum object to an error mode.
110*/
drhf06db3e2021-10-01 00:25:06111void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
drh0cdbe1a2018-05-09 13:46:26112 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
drha6353a32013-12-09 19:03:26113 p->accError = eError;
drh255a81f2019-02-22 15:42:10114 if( p->mxAlloc ) sqlite3_str_reset(p);
drhc3dcdba2019-04-09 21:32:46115 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
drha6353a32013-12-09 19:03:26116}
117
118/*
drha5c14162013-12-17 15:03:06119** Extra argument values from a PrintfArguments object
120*/
121static sqlite3_int64 getIntArg(PrintfArguments *p){
122 if( p->nArg<=p->nUsed ) return 0;
123 return sqlite3_value_int64(p->apArg[p->nUsed++]);
124}
125static double getDoubleArg(PrintfArguments *p){
126 if( p->nArg<=p->nUsed ) return 0.0;
127 return sqlite3_value_double(p->apArg[p->nUsed++]);
128}
129static char *getTextArg(PrintfArguments *p){
130 if( p->nArg<=p->nUsed ) return 0;
131 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
132}
133
drh29642252019-02-01 20:29:04134/*
135** Allocate memory for a temporary buffer needed for printf rendering.
136**
137** If the requested size of the temp buffer is larger than the size
138** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
139** Do the size check before the memory allocation to prevent rogue
140** SQL from requesting large allocations using the precision or width
141** field of the printf() function.
142*/
143static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
144 char *z;
drh255a81f2019-02-22 15:42:10145 if( pAccum->accError ) return 0;
drh29642252019-02-01 20:29:04146 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
drhf06db3e2021-10-01 00:25:06147 sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG);
drh29642252019-02-01 20:29:04148 return 0;
149 }
150 z = sqlite3DbMallocRaw(pAccum->db, n);
151 if( z==0 ){
drhf06db3e2021-10-01 00:25:06152 sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM);
drh29642252019-02-01 20:29:04153 }
154 return z;
155}
drha5c14162013-12-17 15:03:06156
157/*
drh79158e12005-09-06 21:40:45158** On machines with a small stack size, you can redefine the
drhed1fddf2011-10-12 18:52:59159** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
drh79158e12005-09-06 21:40:45160*/
161#ifndef SQLITE_PRINT_BUF_SIZE
drh59eedf72011-10-11 17:54:54162# define SQLITE_PRINT_BUF_SIZE 70
drh79158e12005-09-06 21:40:45163#endif
164#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
drha18c5682000-10-08 22:20:57165
166/*
drhdd6c33d2020-05-23 19:58:07167** Hard limit on the precision of floating-point conversions.
168*/
169#ifndef SQLITE_PRINTF_PRECISION_LIMIT
170# define SQLITE_FP_PRECISION_LIMIT 100000000
171#endif
172
173/*
drhed1fddf2011-10-12 18:52:59174** Render a string given by "fmt" into the StrAccum object.
drha18c5682000-10-08 22:20:57175*/
drh0cdbe1a2018-05-09 13:46:26176void sqlite3_str_vappendf(
177 sqlite3_str *pAccum, /* Accumulate results here */
drha5c14162013-12-17 15:03:06178 const char *fmt, /* Format string */
179 va_list ap /* arguments */
drha18c5682000-10-08 22:20:57180){
drhe84a3062004-02-02 12:29:25181 int c; /* Next character in the format string */
182 char *bufpt; /* Pointer to the conversion buffer */
183 int precision; /* Precision of the current field */
184 int length; /* Length of the field */
185 int idx; /* A general purpose loop counter */
drhe84a3062004-02-02 12:29:25186 int width; /* Width of the current field */
187 etByte flag_leftjustify; /* True if "-" flag is present */
drh2c338a92017-02-10 19:38:36188 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
drhe84a3062004-02-02 12:29:25189 etByte flag_alternateform; /* True if "#" flag is present */
drh531fe872005-08-13 13:40:42190 etByte flag_altform2; /* True if "!" flag is present */
drhe84a3062004-02-02 12:29:25191 etByte flag_zeropad; /* True if field width constant starts with zero */
drh2c338a92017-02-10 19:38:36192 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
drh3e9aeec2005-08-13 13:39:02193 etByte done; /* Loop termination flag */
drh2c338a92017-02-10 19:38:36194 etByte cThousand; /* Thousands separator for %d and %u */
drhad5a9d72016-05-05 11:53:12195 etByte xtype = etINVALID; /* Conversion paradigm */
drha5c14162013-12-17 15:03:06196 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59197 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
drh27436af2006-03-28 23:57:17198 sqlite_uint64 longvalue; /* Value for integer types */
drhaebeaba2023-06-30 23:18:44199 double realvalue; /* Value for real types */
drh57196282004-10-06 15:41:16200 const et_info *infop; /* Pointer to the appropriate info structure */
drh59eedf72011-10-11 17:54:54201 char *zOut; /* Rendering buffer */
202 int nOut; /* Size of the rendering buffer */
drhaf8f5132014-10-29 18:20:18203 char *zExtra = 0; /* Malloced memory used by some conversion */
drhaebeaba2023-06-30 23:18:44204 int exp, e2; /* exponent of real numbers */
drhe84a3062004-02-02 12:29:25205 etByte flag_dp; /* True if decimal point should be shown */
206 etByte flag_rtz; /* True if trailing zeros should be removed */
drhaebeaba2023-06-30 23:18:44207
drha5c14162013-12-17 15:03:06208 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
drhed1fddf2011-10-12 18:52:59209 char buf[etBUFSIZE]; /* Conversion buffer */
drha18c5682000-10-08 22:20:57210
drhcc398962018-02-20 15:23:37211 /* pAccum never starts out with an empty buffer that was obtained from
212 ** malloc(). This precondition is required by the mprintf("%z...")
213 ** optimization. */
214 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
215
drha18c5682000-10-08 22:20:57216 bufpt = 0;
drh8236f682017-01-04 00:26:28217 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
218 pArgList = va_arg(ap, PrintfArguments*);
219 bArgList = 1;
drha5c14162013-12-17 15:03:06220 }else{
drh8236f682017-01-04 00:26:28221 bArgList = 0;
drha5c14162013-12-17 15:03:06222 }
drha18c5682000-10-08 22:20:57223 for(; (c=(*fmt))!=0; ++fmt){
224 if( c!='%' ){
drha18c5682000-10-08 22:20:57225 bufpt = (char *)fmt;
drh760b1592014-09-18 01:50:09226#if HAVE_STRCHRNUL
227 fmt = strchrnul(fmt, '%');
228#else
229 do{ fmt++; }while( *fmt && *fmt != '%' );
230#endif
drh0cdbe1a2018-05-09 13:46:26231 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
drh760b1592014-09-18 01:50:09232 if( *fmt==0 ) break;
drha18c5682000-10-08 22:20:57233 }
234 if( (c=(*++fmt))==0 ){
drh0cdbe1a2018-05-09 13:46:26235 sqlite3_str_append(pAccum, "%", 1);
drha18c5682000-10-08 22:20:57236 break;
237 }
238 /* Find out what flags are present */
drh2c338a92017-02-10 19:38:36239 flag_leftjustify = flag_prefix = cThousand =
drh557cc602005-08-13 12:59:14240 flag_alternateform = flag_altform2 = flag_zeropad = 0;
drh3e9aeec2005-08-13 13:39:02241 done = 0;
drh9a6d01b2019-02-01 18:46:41242 width = 0;
243 flag_long = 0;
244 precision = -1;
drha18c5682000-10-08 22:20:57245 do{
246 switch( c ){
drh3e9aeec2005-08-13 13:39:02247 case '-': flag_leftjustify = 1; break;
drh2c338a92017-02-10 19:38:36248 case '+': flag_prefix = '+'; break;
249 case ' ': flag_prefix = ' '; break;
drh3e9aeec2005-08-13 13:39:02250 case '#': flag_alternateform = 1; break;
251 case '!': flag_altform2 = 1; break;
252 case '0': flag_zeropad = 1; break;
drh2c338a92017-02-10 19:38:36253 case ',': cThousand = ','; break;
drh3e9aeec2005-08-13 13:39:02254 default: done = 1; break;
drh9a6d01b2019-02-01 18:46:41255 case 'l': {
256 flag_long = 1;
257 c = *++fmt;
258 if( c=='l' ){
259 c = *++fmt;
260 flag_long = 2;
261 }
262 done = 1;
263 break;
264 }
265 case '1': case '2': case '3': case '4': case '5':
266 case '6': case '7': case '8': case '9': {
267 unsigned wx = c - '0';
268 while( (c = *++fmt)>='0' && c<='9' ){
269 wx = wx*10 + c - '0';
270 }
271 testcase( wx>0x7fffffff );
272 width = wx & 0x7fffffff;
273#ifdef SQLITE_PRINTF_PRECISION_LIMIT
274 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
275 width = SQLITE_PRINTF_PRECISION_LIMIT;
276 }
277#endif
278 if( c!='.' && c!='l' ){
279 done = 1;
280 }else{
281 fmt--;
282 }
283 break;
284 }
285 case '*': {
286 if( bArgList ){
287 width = (int)getIntArg(pArgList);
288 }else{
289 width = va_arg(ap,int);
290 }
291 if( width<0 ){
292 flag_leftjustify = 1;
293 width = width >= -2147483647 ? -width : 0;
294 }
295#ifdef SQLITE_PRINTF_PRECISION_LIMIT
296 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
297 width = SQLITE_PRINTF_PRECISION_LIMIT;
298 }
299#endif
300 if( (c = fmt[1])!='.' && c!='l' ){
301 c = *++fmt;
302 done = 1;
303 }
304 break;
305 }
306 case '.': {
307 c = *++fmt;
308 if( c=='*' ){
309 if( bArgList ){
310 precision = (int)getIntArg(pArgList);
311 }else{
312 precision = va_arg(ap,int);
313 }
314 if( precision<0 ){
315 precision = precision >= -2147483647 ? -precision : -1;
316 }
317 c = *++fmt;
318 }else{
319 unsigned px = 0;
320 while( c>='0' && c<='9' ){
321 px = px*10 + c - '0';
322 c = *++fmt;
323 }
324 testcase( px>0x7fffffff );
325 precision = px & 0x7fffffff;
326 }
327#ifdef SQLITE_PRINTF_PRECISION_LIMIT
328 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
329 precision = SQLITE_PRINTF_PRECISION_LIMIT;
330 }
331#endif
332 if( c=='l' ){
333 --fmt;
334 }else{
335 done = 1;
336 }
337 break;
338 }
drha18c5682000-10-08 22:20:57339 }
drh3e9aeec2005-08-13 13:39:02340 }while( !done && (c=(*++fmt))!=0 );
dan8c069142015-04-07 14:38:57341
drha18c5682000-10-08 22:20:57342 /* Fetch the info entry for the field */
drh874ba042009-04-08 16:10:04343 infop = &fmtinfo[0];
344 xtype = etINVALID;
danielk197700e13612008-11-17 19:18:54345 for(idx=0; idx<ArraySize(fmtinfo); idx++){
drha18c5682000-10-08 22:20:57346 if( c==fmtinfo[idx].fmttype ){
347 infop = &fmtinfo[idx];
drh8236f682017-01-04 00:26:28348 xtype = infop->type;
drha18c5682000-10-08 22:20:57349 break;
350 }
351 }
drh43617e92006-03-06 20:55:46352
drha18c5682000-10-08 22:20:57353 /*
354 ** At this point, variables are initialized as follows:
355 **
356 ** flag_alternateform TRUE if a '#' is present.
drh3e9aeec2005-08-13 13:39:02357 ** flag_altform2 TRUE if a '!' is present.
drh2c338a92017-02-10 19:38:36358 ** flag_prefix '+' or ' ' or zero
drha18c5682000-10-08 22:20:57359 ** flag_leftjustify TRUE if a '-' is present or if the
360 ** field width was negative.
361 ** flag_zeropad TRUE if the width began with 0.
drh2c338a92017-02-10 19:38:36362 ** flag_long 1 for "l", 2 for "ll"
drha18c5682000-10-08 22:20:57363 ** width The specified field width. This is
364 ** always non-negative. Zero is the default.
365 ** precision The specified precision. The default
366 ** is -1.
367 ** xtype The class of the conversion.
368 ** infop Pointer to the appropriate info struct.
369 */
drhb6907e22020-05-25 15:41:03370 assert( width>=0 );
371 assert( precision>=(-1) );
drha18c5682000-10-08 22:20:57372 switch( xtype ){
drhfe63d1c2004-09-08 20:13:04373 case etPOINTER:
drh2c338a92017-02-10 19:38:36374 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
375 sizeof(char*)==sizeof(long int) ? 1 : 0;
drh08b92082020-08-10 14:18:00376 /* no break */ deliberate_fall_through
drh9a993342007-12-13 02:45:31377 case etORDINAL:
drh2c338a92017-02-10 19:38:36378 case etRADIX:
379 cThousand = 0;
drh08b92082020-08-10 14:18:00380 /* no break */ deliberate_fall_through
drh2c338a92017-02-10 19:38:36381 case etDECIMAL:
drhe84a3062004-02-02 12:29:25382 if( infop->flags & FLAG_SIGNED ){
drhe9707672004-06-25 01:10:48383 i64 v;
drha5c14162013-12-17 15:03:06384 if( bArgList ){
385 v = getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16386 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36387 if( flag_long==2 ){
388 v = va_arg(ap,i64) ;
389 }else{
390 v = va_arg(ap,long int);
391 }
drheeb23a42009-05-04 20:20:16392 }else{
393 v = va_arg(ap,int);
394 }
drhe9707672004-06-25 01:10:48395 if( v<0 ){
drhe2678b92020-08-28 12:58:21396 testcase( v==SMALLEST_INT64 );
397 testcase( v==(-1) );
398 longvalue = ~v;
399 longvalue++;
drhe9707672004-06-25 01:10:48400 prefix = '-';
danielk1977cfcdaef2004-05-12 07:33:33401 }else{
drhe9707672004-06-25 01:10:48402 longvalue = v;
drh2c338a92017-02-10 19:38:36403 prefix = flag_prefix;
danielk1977cfcdaef2004-05-12 07:33:33404 }
drhe9707672004-06-25 01:10:48405 }else{
drha5c14162013-12-17 15:03:06406 if( bArgList ){
407 longvalue = (u64)getIntArg(pArgList);
drheeb23a42009-05-04 20:20:16408 }else if( flag_long ){
drh2c338a92017-02-10 19:38:36409 if( flag_long==2 ){
410 longvalue = va_arg(ap,u64);
411 }else{
412 longvalue = va_arg(ap,unsigned long int);
413 }
drheeb23a42009-05-04 20:20:16414 }else{
415 longvalue = va_arg(ap,unsigned int);
416 }
drhe9707672004-06-25 01:10:48417 prefix = 0;
418 }
drh67f70822025-06-16 13:51:09419
drh6ed5aa42025-06-16 15:34:26420#if WHERETRACE_ENABLED
421 if( xtype==etPOINTER && sqlite3WhereTrace & 0x100000 ) longvalue = 0;
422#endif
423#if TREETRACE_ENABLED
424 if( xtype==etPOINTER && sqlite3TreeTrace & 0x100000 ) longvalue = 0;
drh67f70822025-06-16 13:51:09425#endif
426
drhe9707672004-06-25 01:10:48427 if( longvalue==0 ) flag_alternateform = 0;
drha18c5682000-10-08 22:20:57428 if( flag_zeropad && precision<width-(prefix!=0) ){
429 precision = width-(prefix!=0);
drh9adf9ac2002-05-15 11:44:13430 }
drh2c338a92017-02-10 19:38:36431 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
drh59eedf72011-10-11 17:54:54432 nOut = etBUFSIZE;
433 zOut = buf;
434 }else{
drh7ba03ea2019-02-01 21:08:27435 u64 n;
436 n = (u64)precision + 10;
437 if( cThousand ) n += precision/3;
drh29642252019-02-01 20:29:04438 zOut = zExtra = printfTempBuf(pAccum, n);
439 if( zOut==0 ) return;
drh5f429952017-03-20 16:34:18440 nOut = (int)n;
drh59eedf72011-10-11 17:54:54441 }
442 bufpt = &zOut[nOut-1];
drh9a993342007-12-13 02:45:31443 if( xtype==etORDINAL ){
drh43f6e062007-12-13 17:50:22444 static const char zOrd[] = "thstndrd";
drhea678832008-12-10 19:26:22445 int x = (int)(longvalue % 10);
drh43f6e062007-12-13 17:50:22446 if( x>=4 || (longvalue/10)%10==1 ){
447 x = 0;
448 }
drh59eedf72011-10-11 17:54:54449 *(--bufpt) = zOrd[x*2+1];
450 *(--bufpt) = zOrd[x*2];
drh9a993342007-12-13 02:45:31451 }
drha18c5682000-10-08 22:20:57452 {
drh0e682092014-03-17 15:06:57453 const char *cset = &aDigits[infop->charset];
454 u8 base = infop->base;
drha18c5682000-10-08 22:20:57455 do{ /* Convert to ascii */
456 *(--bufpt) = cset[longvalue%base];
457 longvalue = longvalue/base;
458 }while( longvalue>0 );
drh9adf9ac2002-05-15 11:44:13459 }
drh59eedf72011-10-11 17:54:54460 length = (int)(&zOut[nOut-1]-bufpt);
drh2c338a92017-02-10 19:38:36461 while( precision>length ){
drha18c5682000-10-08 22:20:57462 *(--bufpt) = '0'; /* Zero pad */
drh2c338a92017-02-10 19:38:36463 length++;
464 }
465 if( cThousand ){
466 int nn = (length - 1)/3; /* Number of "," to insert */
467 int ix = (length - 1)%3 + 1;
468 bufpt -= nn;
469 for(idx=0; nn>0; idx++){
470 bufpt[idx] = bufpt[idx+nn];
471 ix--;
472 if( ix==0 ){
473 bufpt[++idx] = cThousand;
474 nn--;
475 ix = 3;
476 }
477 }
drh9adf9ac2002-05-15 11:44:13478 }
drha18c5682000-10-08 22:20:57479 if( prefix ) *(--bufpt) = prefix; /* Add sign */
480 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
drh76ff3a02004-09-24 22:32:30481 const char *pre;
482 char x;
483 pre = &aPrefix[infop->prefix];
drhaf005fb2008-07-09 16:51:51484 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
drha18c5682000-10-08 22:20:57485 }
drh59eedf72011-10-11 17:54:54486 length = (int)(&zOut[nOut-1]-bufpt);
drha18c5682000-10-08 22:20:57487 break;
488 case etFLOAT:
489 case etEXP:
drhaebeaba2023-06-30 23:18:44490 case etGENERIC: {
491 FpDecode s;
drh9ee94442023-07-01 15:23:24492 int iRound;
drh453be552023-07-01 18:33:26493 int j;
drhaebeaba2023-06-30 23:18:44494
drha5c14162013-12-17 15:03:06495 if( bArgList ){
496 realvalue = getDoubleArg(pArgList);
497 }else{
498 realvalue = va_arg(ap,double);
499 }
drha18c5682000-10-08 22:20:57500 if( precision<0 ) precision = 6; /* Set default precision */
drhdd6c33d2020-05-23 19:58:07501#ifdef SQLITE_FP_PRECISION_LIMIT
502 if( precision>SQLITE_FP_PRECISION_LIMIT ){
503 precision = SQLITE_FP_PRECISION_LIMIT;
504 }
505#endif
drh9ee94442023-07-01 15:23:24506 if( xtype==etFLOAT ){
507 iRound = -precision;
508 }else if( xtype==etGENERIC ){
drh6161cdd2024-02-17 03:32:31509 if( precision==0 ) precision = 1;
drh9ee94442023-07-01 15:23:24510 iRound = precision;
511 }else{
512 iRound = precision+1;
drh42d042e2023-07-01 14:03:50513 }
drh17c20bb2023-07-01 17:56:00514 sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16);
drh9ee94442023-07-01 15:23:24515 if( s.isSpecial ){
516 if( s.isSpecial==2 ){
517 bufpt = flag_zeropad ? "null" : "NaN";
518 length = sqlite3Strlen30(bufpt);
519 break;
520 }else if( flag_zeropad ){
521 s.z[0] = '9';
522 s.iDP = 1000;
523 s.n = 1;
drhaebeaba2023-06-30 23:18:44524 }else{
drh9ee94442023-07-01 15:23:24525 memcpy(buf, "-Inf", 5);
526 bufpt = buf;
527 if( s.sign=='-' ){
528 /* no-op */
529 }else if( flag_prefix ){
530 buf[0] = flag_prefix;
531 }else{
532 bufpt++;
533 }
534 length = sqlite3Strlen30(bufpt);
535 break;
drhaebeaba2023-06-30 23:18:44536 }
drhaebeaba2023-06-30 23:18:44537 }
drh42d042e2023-07-01 14:03:50538 if( s.sign=='-' ){
drha18c5682000-10-08 22:20:57539 prefix = '-';
drh9adf9ac2002-05-15 11:44:13540 }else{
drh42d042e2023-07-01 14:03:50541 prefix = flag_prefix;
drh9adf9ac2002-05-15 11:44:13542 }
drh4df23a32023-02-23 21:18:47543
drhaebeaba2023-06-30 23:18:44544 exp = s.iDP-1;
drh4df23a32023-02-23 21:18:47545
drha18c5682000-10-08 22:20:57546 /*
547 ** If the field type is etGENERIC, then convert to either etEXP
548 ** or etFLOAT, as appropriate.
549 */
drha18c5682000-10-08 22:20:57550 if( xtype==etGENERIC ){
drhb468e682024-02-20 13:11:27551 assert( precision>0 );
552 precision--;
drha18c5682000-10-08 22:20:57553 flag_rtz = !flag_alternateform;
554 if( exp<-4 || exp>precision ){
555 xtype = etEXP;
556 }else{
557 precision = precision - exp;
558 xtype = etFLOAT;
559 }
drh9adf9ac2002-05-15 11:44:13560 }else{
drh72b3fbc2012-06-19 03:11:25561 flag_rtz = flag_altform2;
drh9adf9ac2002-05-15 11:44:13562 }
drh557cc602005-08-13 12:59:14563 if( xtype==etEXP ){
564 e2 = 0;
565 }else{
drhaebeaba2023-06-30 23:18:44566 e2 = s.iDP - 1;
drh557cc602005-08-13 12:59:14567 }
drh4df23a32023-02-23 21:18:47568 bufpt = buf;
drh29642252019-02-01 20:29:04569 {
570 i64 szBufNeeded; /* Size of a temporary buffer needed */
571 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
dan77eb3e32023-05-05 19:36:13572 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
drh29642252019-02-01 20:29:04573 if( szBufNeeded > etBUFSIZE ){
574 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
575 if( bufpt==0 ) return;
drh59eedf72011-10-11 17:54:54576 }
577 }
578 zOut = bufpt;
drhea678832008-12-10 19:26:22579 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
drh557cc602005-08-13 12:59:14580 /* The sign in front of the number */
581 if( prefix ){
582 *(bufpt++) = prefix;
583 }
584 /* Digits prior to the decimal point */
drhaebeaba2023-06-30 23:18:44585 j = 0;
drh557cc602005-08-13 12:59:14586 if( e2<0 ){
587 *(bufpt++) = '0';
588 }else{
589 for(; e2>=0; e2--){
drh42d042e2023-07-01 14:03:50590 *(bufpt++) = j<s.n ? s.z[j++] : '0';
drhe8468092023-05-04 13:07:49591 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
drha18c5682000-10-08 22:20:57592 }
drh9adf9ac2002-05-15 11:44:13593 }
drh557cc602005-08-13 12:59:14594 /* The decimal point */
595 if( flag_dp ){
596 *(bufpt++) = '.';
597 }
598 /* "0" digits after the decimal point but before the first
599 ** significant digit of the number */
drhaebeaba2023-06-30 23:18:44600 for(e2++; e2<0 && precision>0; precision--, e2++){
drh557cc602005-08-13 12:59:14601 *(bufpt++) = '0';
602 }
603 /* Significant digits after the decimal point */
drhaebeaba2023-06-30 23:18:44604 while( (precision--)>0 ){
605 *(bufpt++) = j<s.n ? s.z[j++] : '0';
drh557cc602005-08-13 12:59:14606 }
607 /* Remove trailing zeros and the "." if no digits follow the "." */
608 if( flag_rtz && flag_dp ){
drh3e9aeec2005-08-13 13:39:02609 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
drh59eedf72011-10-11 17:54:54610 assert( bufpt>zOut );
drh3e9aeec2005-08-13 13:39:02611 if( bufpt[-1]=='.' ){
drh557cc602005-08-13 12:59:14612 if( flag_altform2 ){
613 *(bufpt++) = '0';
614 }else{
615 *(--bufpt) = 0;
616 }
617 }
618 }
619 /* Add the "eNNN" suffix */
drh59eedf72011-10-11 17:54:54620 if( xtype==etEXP ){
drhaebeaba2023-06-30 23:18:44621 exp = s.iDP - 1;
drh557cc602005-08-13 12:59:14622 *(bufpt++) = aDigits[infop->charset];
623 if( exp<0 ){
624 *(bufpt++) = '-'; exp = -exp;
625 }else{
626 *(bufpt++) = '+';
627 }
628 if( exp>=100 ){
drhea678832008-12-10 19:26:22629 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
drh557cc602005-08-13 12:59:14630 exp %= 100;
631 }
drhea678832008-12-10 19:26:22632 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
633 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
drh557cc602005-08-13 12:59:14634 }
635 *bufpt = 0;
636
drha18c5682000-10-08 22:20:57637 /* The converted number is in buf[] and zero terminated. Output it.
638 ** Note that the number is in the usual order, not reversed as with
639 ** integer conversions. */
drh59eedf72011-10-11 17:54:54640 length = (int)(bufpt-zOut);
641 bufpt = zOut;
drha18c5682000-10-08 22:20:57642
643 /* Special case: Add leading zeros if the flag_zeropad flag is
644 ** set and we are not left justified */
645 if( flag_zeropad && !flag_leftjustify && length < width){
646 int i;
647 int nPad = width - length;
648 for(i=width; i>=nPad; i--){
649 bufpt[i] = bufpt[i-nPad];
650 }
651 i = prefix!=0;
652 while( nPad-- ) bufpt[i++] = '0';
653 length = width;
654 }
drha18c5682000-10-08 22:20:57655 break;
drhaebeaba2023-06-30 23:18:44656 }
drha18c5682000-10-08 22:20:57657 case etSIZE:
drhfc6ee9d2013-12-17 15:58:42658 if( !bArgList ){
659 *(va_arg(ap,int*)) = pAccum->nChar;
660 }
drha18c5682000-10-08 22:20:57661 length = width = 0;
662 break;
663 case etPERCENT:
664 buf[0] = '%';
665 bufpt = buf;
666 length = 1;
667 break;
drha18c5682000-10-08 22:20:57668 case etCHARX:
drha5c14162013-12-17 15:03:06669 if( bArgList ){
drhfc6ee9d2013-12-17 15:58:42670 bufpt = getTextArg(pArgList);
drha15a7c32018-02-19 21:58:16671 length = 1;
drh136102b2018-02-19 18:56:52672 if( bufpt ){
673 buf[0] = c = *(bufpt++);
drh136102b2018-02-19 18:56:52674 if( (c&0xc0)==0xc0 ){
675 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
676 buf[length++] = *(bufpt++);
677 }
678 }
drha15a7c32018-02-19 21:58:16679 }else{
680 buf[0] = 0;
drh136102b2018-02-19 18:56:52681 }
drha5c14162013-12-17 15:03:06682 }else{
drh136102b2018-02-19 18:56:52683 unsigned int ch = va_arg(ap,unsigned int);
drha357a902025-02-25 11:47:34684 length = sqlite3AppendOneUtf8Character(buf, ch);
drha5c14162013-12-17 15:03:06685 }
drhaf8f5132014-10-29 18:20:18686 if( precision>1 ){
drh79b9bc42022-12-21 19:11:56687 i64 nPrior = 1;
drhaf8f5132014-10-29 18:20:18688 width -= precision-1;
689 if( width>1 && !flag_leftjustify ){
drh0cdbe1a2018-05-09 13:46:26690 sqlite3_str_appendchar(pAccum, width-1, ' ');
drhaf8f5132014-10-29 18:20:18691 width = 0;
692 }
drh79b9bc42022-12-21 19:11:56693 sqlite3_str_append(pAccum, buf, length);
694 precision--;
695 while( precision > 1 ){
696 i64 nCopyBytes;
697 if( nPrior > precision-1 ) nPrior = precision - 1;
698 nCopyBytes = length*nPrior;
699 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){
700 sqlite3StrAccumEnlarge(pAccum, nCopyBytes);
701 }
702 if( pAccum->accError ) break;
703 sqlite3_str_append(pAccum,
704 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes);
705 precision -= nPrior;
706 nPrior *= 2;
drh136102b2018-02-19 18:56:52707 }
drh9adf9ac2002-05-15 11:44:13708 }
drha18c5682000-10-08 22:20:57709 bufpt = buf;
drhcf7c8372018-02-19 20:23:20710 flag_altform2 = 1;
711 goto adjust_width_for_utf8;
drha18c5682000-10-08 22:20:57712 case etSTRING:
drhd93d8a82003-06-16 03:08:18713 case etDYNSTRING:
drha5c14162013-12-17 15:03:06714 if( bArgList ){
715 bufpt = getTextArg(pArgList);
drh2a8f6712015-09-02 21:00:48716 xtype = etSTRING;
drha5c14162013-12-17 15:03:06717 }else{
718 bufpt = va_arg(ap,char*);
719 }
drhd93d8a82003-06-16 03:08:18720 if( bufpt==0 ){
721 bufpt = "";
drh2a8f6712015-09-02 21:00:48722 }else if( xtype==etDYNSTRING ){
drhaf524a62018-09-13 17:07:12723 if( pAccum->nChar==0
724 && pAccum->mxAlloc
725 && width==0
726 && precision<0
727 && pAccum->accError==0
728 ){
drhcc398962018-02-20 15:23:37729 /* Special optimization for sqlite3_mprintf("%z..."):
730 ** Extend an existing memory allocation rather than creating
731 ** a new one. */
732 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
733 pAccum->zText = bufpt;
734 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
735 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
736 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
737 length = 0;
738 break;
739 }
drhd93d8a82003-06-16 03:08:18740 zExtra = bufpt;
741 }
drhe5090942008-04-29 15:22:27742 if( precision>=0 ){
drh62856462018-02-19 17:03:23743 if( flag_altform2 ){
744 /* Set length to the number of bytes needed in order to display
745 ** precision characters */
746 unsigned char *z = (unsigned char*)bufpt;
747 while( precision-- > 0 && z[0] ){
748 SQLITE_SKIP_UTF8(z);
749 }
750 length = (int)(z - (unsigned char*)bufpt);
751 }else{
752 for(length=0; length<precision && bufpt[length]; length++){}
753 }
drhe5090942008-04-29 15:22:27754 }else{
drhc84ddf12017-08-19 20:38:18755 length = 0x7fffffff & (int)strlen(bufpt);
drhe5090942008-04-29 15:22:27756 }
drh57e3ba72018-02-19 18:03:10757 adjust_width_for_utf8:
drh62856462018-02-19 17:03:23758 if( flag_altform2 && width>0 ){
759 /* Adjust width to account for extra bytes in UTF-8 characters */
760 int ii = length - 1;
761 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
762 }
drha18c5682000-10-08 22:20:57763 break;
drha3283ec2025-02-23 11:48:07764 case etESCAPE_q: /* %q: Escape ' characters */
765 case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */
766 case etESCAPE_w: { /* %w: Escape " characters */
drh077e17b2022-07-18 15:02:00767 i64 i, j, k, n;
drhd4c686e2025-02-25 12:18:27768 int needQuote = 0;
drhea678832008-12-10 19:26:22769 char ch;
drha5c14162013-12-17 15:03:06770 char *escarg;
drha3283ec2025-02-23 11:48:07771 char q;
drha5c14162013-12-17 15:03:06772
773 if( bArgList ){
774 escarg = getTextArg(pArgList);
775 }else{
776 escarg = va_arg(ap,char*);
777 }
drhd4c686e2025-02-25 12:18:27778 if( escarg==0 ){
779 escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)");
780 }else if( xtype==etESCAPE_Q ){
781 needQuote = 1;
782 }
drha3283ec2025-02-23 11:48:07783 if( xtype==etESCAPE_w ){
784 q = '"';
785 flag_alternateform = 0;
786 }else{
787 q = '\'';
788 }
drhb6907e22020-05-25 15:41:03789 /* For %q, %Q, and %w, the precision is the number of bytes (or
drh57e3ba72018-02-19 18:03:10790 ** characters if the ! flags is present) to use from the input.
791 ** Because of the extra quoting characters inserted, the number
792 ** of output characters may be larger than the precision.
793 */
drh8965b502009-11-25 16:53:37794 k = precision;
dan60d4a302010-03-04 17:58:45795 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
danielk1977f3b863e2007-06-24 06:32:17796 if( ch==q ) n++;
drh57e3ba72018-02-19 18:03:10797 if( flag_altform2 && (ch&0xc0)==0xc0 ){
798 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
799 }
drha18c5682000-10-08 22:20:57800 }
drh07ce1822025-02-23 00:09:24801 if( flag_alternateform ){
drha3283ec2025-02-23 11:48:07802 /* For %#q, do unistr()-style backslash escapes for
803 ** all control characters, and for backslash itself.
804 ** For %#Q, do the same but only if there is at least
805 ** one control character. */
806 u32 nBack = 0;
807 u32 nCtrl = 0;
drh07ce1822025-02-23 00:09:24808 for(k=0; k<i; k++){
809 if( escarg[k]=='\\' ){
drha3283ec2025-02-23 11:48:07810 nBack++;
drhc171cf12025-03-02 20:29:49811 }else if( ((u8*)escarg)[k]<=0x1f ){
drha3283ec2025-02-23 11:48:07812 nCtrl++;
drh07ce1822025-02-23 00:09:24813 }
814 }
drha3283ec2025-02-23 11:48:07815 if( nCtrl || xtype==etESCAPE_q ){
816 n += nBack + 5*nCtrl;
817 if( xtype==etESCAPE_Q ){
818 n += 10;
819 needQuote = 2;
820 }
821 }else{
822 flag_alternateform = 0;
823 }
drh07ce1822025-02-23 00:09:24824 }
drh2a8f6712015-09-02 21:00:48825 n += i + 3;
drh5eba8c02005-08-19 02:26:27826 if( n>etBUFSIZE ){
drh29642252019-02-01 20:29:04827 bufpt = zExtra = printfTempBuf(pAccum, n);
828 if( bufpt==0 ) return;
drh5eba8c02005-08-19 02:26:27829 }else{
830 bufpt = buf;
831 }
832 j = 0;
drha3283ec2025-02-23 11:48:07833 if( needQuote ){
834 if( needQuote==2 ){
835 memcpy(&bufpt[j], "unistr('", 8);
836 j += 8;
837 }else{
838 bufpt[j++] = '\'';
839 }
840 }
drh8965b502009-11-25 16:53:37841 k = i;
drhd4c686e2025-02-25 12:18:27842 if( flag_alternateform ){
843 for(i=0; i<k; i++){
844 bufpt[j++] = ch = escarg[i];
845 if( ch==q ){
846 bufpt[j++] = ch;
847 }else if( ch=='\\' ){
drh07ce1822025-02-23 00:09:24848 bufpt[j++] = '\\';
drhc171cf12025-03-02 20:29:49849 }else if( ((unsigned char)ch)<=0x1f ){
drh07ce1822025-02-23 00:09:24850 bufpt[j-1] = '\\';
drha3283ec2025-02-23 11:48:07851 bufpt[j++] = 'u';
drh07ce1822025-02-23 00:09:24852 bufpt[j++] = '0';
853 bufpt[j++] = '0';
854 bufpt[j++] = ch>=0x10 ? '1' : '0';
855 bufpt[j++] = "0123456789abcdef"[ch&0xf];
856 }
857 }
drhd4c686e2025-02-25 12:18:27858 }else{
859 for(i=0; i<k; i++){
860 bufpt[j++] = ch = escarg[i];
861 if( ch==q ) bufpt[j++] = ch;
862 }
drh5eba8c02005-08-19 02:26:27863 }
drha3283ec2025-02-23 11:48:07864 if( needQuote ){
865 bufpt[j++] = '\'';
866 if( needQuote==2 ) bufpt[j++] = ')';
867 }
drh5eba8c02005-08-19 02:26:27868 bufpt[j] = 0;
869 length = j;
drh57e3ba72018-02-19 18:03:10870 goto adjust_width_for_utf8;
drh5eba8c02005-08-19 02:26:27871 }
drh5f968432004-02-21 19:02:30872 case etTOKEN: {
drh8236f682017-01-04 00:26:28873 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
drh62fc0692022-02-06 00:30:04874 if( flag_alternateform ){
875 /* %#T means an Expr pointer that uses Expr.u.zToken */
876 Expr *pExpr = va_arg(ap,Expr*);
drh5d20a212022-02-06 23:54:41877 if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){
drh62fc0692022-02-06 00:30:04878 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
879 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
880 }
881 }else{
882 /* %T means a Token pointer */
883 Token *pToken = va_arg(ap, Token*);
884 assert( bArgList==0 );
885 if( pToken && pToken->n ){
886 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
887 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
888 }
drhad6d9462004-09-19 02:15:24889 }
drh5f968432004-02-21 19:02:30890 length = width = 0;
891 break;
892 }
drha9799932021-03-19 13:00:28893 case etSRCITEM: {
drh76012942021-02-21 21:04:54894 SrcItem *pItem;
drh8236f682017-01-04 00:26:28895 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
drha9799932021-03-19 13:00:28896 pItem = va_arg(ap, SrcItem*);
drha5c14162013-12-17 15:03:06897 assert( bArgList==0 );
drh2f2091b2021-03-20 15:46:01898 if( pItem->zAlias && !flag_altform2 ){
drh82102332021-03-20 15:11:29899 sqlite3_str_appendall(pAccum, pItem->zAlias);
900 }else if( pItem->zName ){
drh1521ca42024-08-19 22:48:30901 if( pItem->fg.fixedSchema==0
902 && pItem->fg.isSubquery==0
903 && pItem->u4.zDatabase!=0
904 ){
drh8797bd62024-08-17 19:46:49905 sqlite3_str_appendall(pAccum, pItem->u4.zDatabase);
drh82102332021-03-20 15:11:29906 sqlite3_str_append(pAccum, ".", 1);
907 }
drha9799932021-03-19 13:00:28908 sqlite3_str_appendall(pAccum, pItem->zName);
drh2f2091b2021-03-20 15:46:01909 }else if( pItem->zAlias ){
910 sqlite3_str_appendall(pAccum, pItem->zAlias);
drh0766cbf2024-08-20 20:01:21911 }else if( ALWAYS(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */
drh1521ca42024-08-19 22:48:30912 Select *pSel = pItem->u4.pSubq->pSelect;
913 assert( pSel!=0 );
drhda653b82022-04-22 17:36:10914 if( pSel->selFlags & SF_NestedFrom ){
915 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
drh27a5ee82024-03-18 12:49:30916 }else if( pSel->selFlags & SF_MultiValue ){
drhac7c6f52024-03-18 13:31:24917 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy );
drh27a5ee82024-03-18 12:49:30918 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
919 pItem->u1.nRow);
drhda653b82022-04-22 17:36:10920 }else{
921 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
922 }
drha9799932021-03-19 13:00:28923 }
drh5f968432004-02-21 19:02:30924 length = width = 0;
925 break;
926 }
drh874ba042009-04-08 16:10:04927 default: {
928 assert( xtype==etINVALID );
929 return;
930 }
drha18c5682000-10-08 22:20:57931 }/* End switch over the format type */
932 /*
933 ** The text of the conversion is pointed to by "bufpt" and is
934 ** "length" characters long. The field width is "width". Do
drh62856462018-02-19 17:03:23935 ** the output. Both length and width are in bytes, not characters,
936 ** at this point. If the "!" flag was present on string conversions
937 ** indicating that width and precision should be expressed in characters,
938 ** then the values have been translated prior to reaching this point.
drha18c5682000-10-08 22:20:57939 */
drha70a0732014-03-17 14:24:27940 width -= length;
drh8236f682017-01-04 00:26:28941 if( width>0 ){
drh0cdbe1a2018-05-09 13:46:26942 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
943 sqlite3_str_append(pAccum, bufpt, length);
944 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
drh8236f682017-01-04 00:26:28945 }else{
drh0cdbe1a2018-05-09 13:46:26946 sqlite3_str_append(pAccum, bufpt, length);
drh8236f682017-01-04 00:26:28947 }
drha70a0732014-03-17 14:24:27948
drhaf8f5132014-10-29 18:20:18949 if( zExtra ){
drh96ceaf82015-11-14 22:04:22950 sqlite3DbFree(pAccum->db, zExtra);
drhaf8f5132014-10-29 18:20:18951 zExtra = 0;
952 }
drha18c5682000-10-08 22:20:57953 }/* End for loop over the format string */
drha18c5682000-10-08 22:20:57954} /* End of function */
955
drhf62641e2021-12-24 20:22:13956
957/*
958** The z string points to the first character of a token that is
959** associated with an error. If db does not already have an error
960** byte offset recorded, try to compute the error byte offset for
961** z and set the error byte offset in db.
962*/
963void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
964 const Parse *pParse;
965 const char *zText;
966 const char *zEnd;
967 assert( z!=0 );
968 if( NEVER(db==0) ) return;
969 if( db->errByteOffset!=(-2) ) return;
970 pParse = db->pParse;
971 if( NEVER(pParse==0) ) return;
972 zText =pParse->zTail;
973 if( NEVER(zText==0) ) return;
974 zEnd = &zText[strlen(zText)];
975 if( SQLITE_WITHIN(z,zText,zEnd) ){
976 db->errByteOffset = (int)(z-zText);
977 }
978}
979
drhade86482007-11-28 22:36:40980/*
drh4f77c922022-02-05 23:11:19981** If pExpr has a byte offset for the start of a token, record that as
982** as the error offset.
983*/
984void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){
drha6e8ee12022-05-13 16:38:40985 while( pExpr
986 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
987 ){
drh4f77c922022-02-05 23:11:19988 pExpr = pExpr->pLeft;
989 }
990 if( pExpr==0 ) return;
drh95669822024-11-20 11:34:16991 if( ExprHasProperty(pExpr, EP_FromDDL) ) return;
drh4f77c922022-02-05 23:11:19992 db->errByteOffset = pExpr->w.iOfst;
993}
994
995/*
drha70a0732014-03-17 14:24:27996** Enlarge the memory allocation on a StrAccum object so that it is
997** able to accept at least N more bytes of text.
998**
999** Return the number of bytes of text that StrAccum is able to accept
1000** after the attempted enlargement. The value returned might be zero.
1001*/
drh79b9bc42022-12-21 19:11:561002int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){
drha70a0732014-03-17 14:24:271003 char *zNew;
drh79b9bc42022-12-21 19:11:561004 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
drha70a0732014-03-17 14:24:271005 if( p->accError ){
drh0cdbe1a2018-05-09 13:46:261006 testcase(p->accError==SQLITE_TOOBIG);
1007 testcase(p->accError==SQLITE_NOMEM);
drha70a0732014-03-17 14:24:271008 return 0;
1009 }
drhc0490572015-05-02 11:45:531010 if( p->mxAlloc==0 ){
drhf06db3e2021-10-01 00:25:061011 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
drh255a81f2019-02-22 15:42:101012 return p->nAlloc - p->nChar - 1;
drha70a0732014-03-17 14:24:271013 }else{
drh5f4a6862016-01-30 12:50:251014 char *zOld = isMalloced(p) ? p->zText : 0;
drh79b9bc42022-12-21 19:11:561015 i64 szNew = p->nChar + N + 1;
drh7b4d7802014-11-03 14:46:291016 if( szNew+p->nChar<=p->mxAlloc ){
1017 /* Force exponential buffer size growth as long as it does not overflow,
1018 ** to avoid having to call this routine too often */
1019 szNew += p->nChar;
1020 }
drha70a0732014-03-17 14:24:271021 if( szNew > p->mxAlloc ){
drh0cdbe1a2018-05-09 13:46:261022 sqlite3_str_reset(p);
drhf06db3e2021-10-01 00:25:061023 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
drha70a0732014-03-17 14:24:271024 return 0;
1025 }else{
1026 p->nAlloc = (int)szNew;
1027 }
drhc0490572015-05-02 11:45:531028 if( p->db ){
drha70a0732014-03-17 14:24:271029 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
1030 }else{
drhd924e7b2020-05-17 00:26:441031 zNew = sqlite3Realloc(zOld, p->nAlloc);
drha70a0732014-03-17 14:24:271032 }
1033 if( zNew ){
drh7ef4d1c2014-05-31 15:39:531034 assert( p->zText!=0 || p->nChar==0 );
drh5f4a6862016-01-30 12:50:251035 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
drha70a0732014-03-17 14:24:271036 p->zText = zNew;
drh7f5a7ec2014-11-03 13:24:121037 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
drh5f4a6862016-01-30 12:50:251038 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
drha70a0732014-03-17 14:24:271039 }else{
drh0cdbe1a2018-05-09 13:46:261040 sqlite3_str_reset(p);
drhf06db3e2021-10-01 00:25:061041 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
drha70a0732014-03-17 14:24:271042 return 0;
1043 }
1044 }
drh79b9bc42022-12-21 19:11:561045 assert( N>=0 && N<=0x7fffffff );
1046 return (int)N;
drha70a0732014-03-17 14:24:271047}
1048
1049/*
drhaf8f5132014-10-29 18:20:181050** Append N copies of character c to the given string buffer.
drha70a0732014-03-17 14:24:271051*/
drh0cdbe1a2018-05-09 13:46:261052void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
drha30d22a2015-04-07 13:28:411053 testcase( p->nChar + (i64)N > 0x7fffffff );
1054 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
1055 return;
1056 }
drhaf8f5132014-10-29 18:20:181057 while( (N--)>0 ) p->zText[p->nChar++] = c;
drha70a0732014-03-17 14:24:271058}
1059
1060/*
1061** The StrAccum "p" is not large enough to accept N new bytes of z[].
1062** So enlarge if first, then do the append.
1063**
drh0cdbe1a2018-05-09 13:46:261064** This is a helper routine to sqlite3_str_append() that does special-case
drha70a0732014-03-17 14:24:271065** work (enlarging the buffer) using tail recursion, so that the
drh0cdbe1a2018-05-09 13:46:261066** sqlite3_str_append() routine can use fast calling semantics.
drha70a0732014-03-17 14:24:271067*/
drh172087f2014-08-22 15:40:201068static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
drha70a0732014-03-17 14:24:271069 N = sqlite3StrAccumEnlarge(p, N);
1070 if( N>0 ){
1071 memcpy(&p->zText[p->nChar], z, N);
1072 p->nChar += N;
1073 }
1074}
1075
1076/*
1077** Append N bytes of text from z to the StrAccum object. Increase the
1078** size of the memory allocation for StrAccum if necessary.
drha18c5682000-10-08 22:20:571079*/
drh0cdbe1a2018-05-09 13:46:261080void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
drh34573382015-04-15 05:38:351081 assert( z!=0 || N==0 );
drha6353a32013-12-09 19:03:261082 assert( p->zText!=0 || p->nChar==0 || p->accError );
1083 assert( N>=0 );
drh255a81f2019-02-22 15:42:101084 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
drhade86482007-11-28 22:36:401085 if( p->nChar+N >= p->nAlloc ){
drha70a0732014-03-17 14:24:271086 enlargeAndAppend(p,z,N);
dan895decf2016-12-30 14:15:561087 }else if( N ){
drh172087f2014-08-22 15:40:201088 assert( p->zText );
1089 p->nChar += N;
1090 memcpy(&p->zText[p->nChar-N], z, N);
drh5f968432004-02-21 19:02:301091 }
drh483750b2003-01-29 18:46:511092}
1093
1094/*
drha6353a32013-12-09 19:03:261095** Append the complete text of zero-terminated string z[] to the p string.
1096*/
drh0cdbe1a2018-05-09 13:46:261097void sqlite3_str_appendall(sqlite3_str *p, const char *z){
1098 sqlite3_str_append(p, z, sqlite3Strlen30(z));
drha6353a32013-12-09 19:03:261099}
1100
1101
1102/*
drhade86482007-11-28 22:36:401103** Finish off a string by making sure it is zero-terminated.
1104** Return a pointer to the resulting string. Return a NULL
1105** pointer if any kind of error was encountered.
drh5f968432004-02-21 19:02:301106*/
drh043e5862016-11-25 15:11:261107static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
drh3f18e6d2017-08-12 02:01:551108 char *zText;
drh043e5862016-11-25 15:11:261109 assert( p->mxAlloc>0 && !isMalloced(p) );
drhef86b942025-02-17 17:33:141110 zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar );
drh3f18e6d2017-08-12 02:01:551111 if( zText ){
1112 memcpy(zText, p->zText, p->nChar+1);
drh043e5862016-11-25 15:11:261113 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
1114 }else{
drhf06db3e2021-10-01 00:25:061115 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
drh043e5862016-11-25 15:11:261116 }
drh3f18e6d2017-08-12 02:01:551117 p->zText = zText;
1118 return zText;
drh043e5862016-11-25 15:11:261119}
drhade86482007-11-28 22:36:401120char *sqlite3StrAccumFinish(StrAccum *p){
1121 if( p->zText ){
1122 p->zText[p->nChar] = 0;
drh5f4a6862016-01-30 12:50:251123 if( p->mxAlloc>0 && !isMalloced(p) ){
drh043e5862016-11-25 15:11:261124 return strAccumFinishRealloc(p);
drhade86482007-11-28 22:36:401125 }
1126 }
1127 return p->zText;
1128}
1129
drhf80bba92018-05-16 15:35:031130/*
drh5bf47152021-10-03 00:12:431131** Use the content of the StrAccum passed as the second argument
1132** as the result of an SQL function.
1133*/
1134void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
1135 if( p->accError ){
1136 sqlite3_result_error_code(pCtx, p->accError);
1137 sqlite3_str_reset(p);
1138 }else if( isMalloced(p) ){
1139 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
1140 }else{
1141 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
1142 sqlite3_str_reset(p);
1143 }
1144}
1145
1146/*
drhf80bba92018-05-16 15:35:031147** This singleton is an sqlite3_str object that is returned if
1148** sqlite3_malloc() fails to provide space for a real one. This
1149** sqlite3_str object accepts no new text and always returns
1150** an SQLITE_NOMEM error.
1151*/
1152static sqlite3_str sqlite3OomStr = {
drh3e62ddb2018-05-30 00:59:091153 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
drhf80bba92018-05-16 15:35:031154};
1155
drh0cdbe1a2018-05-09 13:46:261156/* Finalize a string created using sqlite3_str_new().
1157*/
1158char *sqlite3_str_finish(sqlite3_str *p){
1159 char *z;
drhf80bba92018-05-16 15:35:031160 if( p!=0 && p!=&sqlite3OomStr ){
drh0cdbe1a2018-05-09 13:46:261161 z = sqlite3StrAccumFinish(p);
drh446135d2018-05-09 14:29:401162 sqlite3_free(p);
drh0cdbe1a2018-05-09 13:46:261163 }else{
1164 z = 0;
1165 }
1166 return z;
1167}
1168
1169/* Return any error code associated with p */
1170int sqlite3_str_errcode(sqlite3_str *p){
1171 return p ? p->accError : SQLITE_NOMEM;
1172}
1173
1174/* Return the current length of p in bytes */
1175int sqlite3_str_length(sqlite3_str *p){
1176 return p ? p->nChar : 0;
1177}
1178
1179/* Return the current value for p */
1180char *sqlite3_str_value(sqlite3_str *p){
drh446135d2018-05-09 14:29:401181 if( p==0 || p->nChar==0 ) return 0;
1182 p->zText[p->nChar] = 0;
1183 return p->zText;
drh0cdbe1a2018-05-09 13:46:261184}
1185
drhade86482007-11-28 22:36:401186/*
1187** Reset an StrAccum string. Reclaim all malloced memory.
1188*/
drh0cdbe1a2018-05-09 13:46:261189void sqlite3_str_reset(StrAccum *p){
drh5f4a6862016-01-30 12:50:251190 if( isMalloced(p) ){
drhc0490572015-05-02 11:45:531191 sqlite3DbFree(p->db, p->zText);
drh5f4a6862016-01-30 12:50:251192 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
drhade86482007-11-28 22:36:401193 }
drh446135d2018-05-09 14:29:401194 p->nAlloc = 0;
1195 p->nChar = 0;
drhf089aa42008-07-08 19:34:061196 p->zText = 0;
drhade86482007-11-28 22:36:401197}
1198
1199/*
drhc0490572015-05-02 11:45:531200** Initialize a string accumulator.
1201**
1202** p: The accumulator to be initialized.
1203** db: Pointer to a database connection. May be NULL. Lookaside
1204** memory is used if not NULL. db->mallocFailed is set appropriately
1205** when not NULL.
1206** zBase: An initial buffer. May be NULL in which case the initial buffer
1207** is malloced.
1208** n: Size of zBase in bytes. If total space requirements never exceed
1209** n then no memory allocations ever occur.
1210** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
1211** allocations will ever occur.
drhade86482007-11-28 22:36:401212*/
drhc0490572015-05-02 11:45:531213void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
drh3f18e6d2017-08-12 02:01:551214 p->zText = zBase;
drhc0490572015-05-02 11:45:531215 p->db = db;
drhade86482007-11-28 22:36:401216 p->nAlloc = n;
drhbb4957f2008-03-20 14:03:291217 p->mxAlloc = mx;
drh3f18e6d2017-08-12 02:01:551218 p->nChar = 0;
drhb49bc862013-08-21 21:12:101219 p->accError = 0;
drh5f4a6862016-01-30 12:50:251220 p->printfFlags = 0;
drh5f968432004-02-21 19:02:301221}
1222
drh0cdbe1a2018-05-09 13:46:261223/* Allocate and initialize a new dynamic string object */
1224sqlite3_str *sqlite3_str_new(sqlite3 *db){
drh446135d2018-05-09 14:29:401225 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
drh0cdbe1a2018-05-09 13:46:261226 if( p ){
drh446135d2018-05-09 14:29:401227 sqlite3StrAccumInit(p, 0, 0, 0,
1228 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
drhf80bba92018-05-16 15:35:031229 }else{
1230 p = &sqlite3OomStr;
drh0cdbe1a2018-05-09 13:46:261231 }
1232 return p;
1233}
1234
drh5f968432004-02-21 19:02:301235/*
1236** Print into memory obtained from sqliteMalloc(). Use the internal
1237** %-conversion extensions.
1238*/
drh17435752007-08-16 04:30:381239char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
1240 char *z;
drh79158e12005-09-06 21:40:451241 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:401242 StrAccum acc;
drhbc6160b2009-04-08 15:45:311243 assert( db!=0 );
drhc0490572015-05-02 11:45:531244 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
drhbc6160b2009-04-08 15:45:311245 db->aLimit[SQLITE_LIMIT_LENGTH]);
drh5f4a6862016-01-30 12:50:251246 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
drh0cdbe1a2018-05-09 13:46:261247 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:401248 z = sqlite3StrAccumFinish(&acc);
drh0cdbe1a2018-05-09 13:46:261249 if( acc.accError==SQLITE_NOMEM ){
drh4a642b62016-02-05 01:55:271250 sqlite3OomFault(db);
drh17435752007-08-16 04:30:381251 }
1252 return z;
drh5f968432004-02-21 19:02:301253}
1254
1255/*
1256** Print into memory obtained from sqliteMalloc(). Use the internal
1257** %-conversion extensions.
1258*/
drh17435752007-08-16 04:30:381259char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
drh5f968432004-02-21 19:02:301260 va_list ap;
1261 char *z;
drh5f968432004-02-21 19:02:301262 va_start(ap, zFormat);
drhade86482007-11-28 22:36:401263 z = sqlite3VMPrintf(db, zFormat, ap);
drh5f968432004-02-21 19:02:301264 va_end(ap);
1265 return z;
1266}
1267
1268/*
drh28dd4792006-06-26 21:35:441269** Print into memory obtained from sqlite3_malloc(). Omit the internal
1270** %-conversion extensions.
1271*/
1272char *sqlite3_vmprintf(const char *zFormat, va_list ap){
drhade86482007-11-28 22:36:401273 char *z;
drh28dd4792006-06-26 21:35:441274 char zBase[SQLITE_PRINT_BUF_SIZE];
drhade86482007-11-28 22:36:401275 StrAccum acc;
drh9ca95732014-10-24 00:35:581276
1277#ifdef SQLITE_ENABLE_API_ARMOR
1278 if( zFormat==0 ){
1279 (void)SQLITE_MISUSE_BKPT;
1280 return 0;
1281 }
1282#endif
drhff1590e2008-07-14 12:52:531283#ifndef SQLITE_OMIT_AUTOINIT
1284 if( sqlite3_initialize() ) return 0;
1285#endif
drhc0490572015-05-02 11:45:531286 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
drh0cdbe1a2018-05-09 13:46:261287 sqlite3_str_vappendf(&acc, zFormat, ap);
drhade86482007-11-28 22:36:401288 z = sqlite3StrAccumFinish(&acc);
1289 return z;
drh28dd4792006-06-26 21:35:441290}
1291
1292/*
1293** Print into memory obtained from sqlite3_malloc()(). Omit the internal
1294** %-conversion extensions.
drh483750b2003-01-29 18:46:511295*/
danielk19776f8a5032004-05-10 10:34:511296char *sqlite3_mprintf(const char *zFormat, ...){
drha18c5682000-10-08 22:20:571297 va_list ap;
drh5f968432004-02-21 19:02:301298 char *z;
drhff1590e2008-07-14 12:52:531299#ifndef SQLITE_OMIT_AUTOINIT
1300 if( sqlite3_initialize() ) return 0;
1301#endif
drh28dd4792006-06-26 21:35:441302 va_start(ap, zFormat);
drhb3738b62007-03-31 15:02:491303 z = sqlite3_vmprintf(zFormat, ap);
drha18c5682000-10-08 22:20:571304 va_end(ap);
drh5f968432004-02-21 19:02:301305 return z;
drha18c5682000-10-08 22:20:571306}
1307
drh93a5c6b2003-12-23 02:17:351308/*
danielk19776f8a5032004-05-10 10:34:511309** sqlite3_snprintf() works like snprintf() except that it ignores the
drh93a5c6b2003-12-23 02:17:351310** current locale settings. This is important for SQLite because we
1311** are not able to use a "," as the decimal point in place of "." as
1312** specified by some locales.
drhdb26d4c2011-01-05 12:20:091313**
1314** Oops: The first two arguments of sqlite3_snprintf() are backwards
1315** from the snprintf() standard. Unfortunately, it is too late to change
1316** this without breaking compatibility, so we just have to live with the
1317** mistake.
1318**
1319** sqlite3_vsnprintf() is the varargs version.
drh93a5c6b2003-12-23 02:17:351320*/
drhdb26d4c2011-01-05 12:20:091321char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
1322 StrAccum acc;
1323 if( n<=0 ) return zBuf;
drh9ca95732014-10-24 00:35:581324#ifdef SQLITE_ENABLE_API_ARMOR
1325 if( zBuf==0 || zFormat==0 ) {
1326 (void)SQLITE_MISUSE_BKPT;
drh96c707a2015-02-13 16:36:141327 if( zBuf ) zBuf[0] = 0;
drh9ca95732014-10-24 00:35:581328 return zBuf;
1329 }
1330#endif
drhc0490572015-05-02 11:45:531331 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drh0cdbe1a2018-05-09 13:46:261332 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe9bb5662016-11-25 15:47:531333 zBuf[acc.nChar] = 0;
1334 return zBuf;
drhdb26d4c2011-01-05 12:20:091335}
danielk19776f8a5032004-05-10 10:34:511336char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
drh0f8aebb2023-02-25 12:50:541337 StrAccum acc;
drh93a5c6b2003-12-23 02:17:351338 va_list ap;
drh0f8aebb2023-02-25 12:50:541339 if( n<=0 ) return zBuf;
1340#ifdef SQLITE_ENABLE_API_ARMOR
1341 if( zBuf==0 || zFormat==0 ) {
1342 (void)SQLITE_MISUSE_BKPT;
1343 if( zBuf ) zBuf[0] = 0;
1344 return zBuf;
1345 }
1346#endif
1347 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
drh93a5c6b2003-12-23 02:17:351348 va_start(ap,zFormat);
drh0f8aebb2023-02-25 12:50:541349 sqlite3_str_vappendf(&acc, zFormat, ap);
drh93a5c6b2003-12-23 02:17:351350 va_end(ap);
drh0f8aebb2023-02-25 12:50:541351 zBuf[acc.nChar] = 0;
1352 return zBuf;
drh93a5c6b2003-12-23 02:17:351353}
1354
drh56747d12025-04-17 17:46:281355/* Maximum size of an sqlite3_log() message. */
1356#if defined(SQLITE_MAX_LOG_MESSAGE)
1357 /* Leave the definition as supplied */
1358#elif SQLITE_PRINT_BUF_SIZE*10>10000
1359# define SQLITE_MAX_LOG_MESSAGE 10000
1360#else
1361# define SQLITE_MAX_LOG_MESSAGE (SQLITE_PRINT_BUF_SIZE*10)
1362#endif
1363
drh3f280702010-02-18 18:45:091364/*
drh7c0c4602010-03-03 22:25:181365** This is the routine that actually formats the sqlite3_log() message.
1366** We house it in a separate routine from sqlite3_log() to avoid using
1367** stack space on small-stack systems when logging is disabled.
1368**
1369** sqlite3_log() must render into a static buffer. It cannot dynamically
1370** allocate memory because it might be called while the memory allocator
1371** mutex is held.
drha8dbd522015-07-14 22:43:371372**
drh0cdbe1a2018-05-09 13:46:261373** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
drha8dbd522015-07-14 22:43:371374** certain format characters (%q) or for very large precisions or widths.
1375** Care must be taken that any sqlite3_log() calls that occur while the
1376** memory mutex is held do not use these mechanisms.
drh7c0c4602010-03-03 22:25:181377*/
1378static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
drha64fa912010-03-04 00:53:321379 StrAccum acc; /* String accumulator */
drh56747d12025-04-17 17:46:281380 char zMsg[SQLITE_MAX_LOG_MESSAGE]; /* Complete log message */
drh7c0c4602010-03-03 22:25:181381
drhc0490572015-05-02 11:45:531382 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
drh0cdbe1a2018-05-09 13:46:261383 sqlite3_str_vappendf(&acc, zFormat, ap);
drh7c0c4602010-03-03 22:25:181384 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
1385 sqlite3StrAccumFinish(&acc));
1386}
1387
1388/*
drh3f280702010-02-18 18:45:091389** Format and write a message to the log if logging is enabled.
1390*/
drha7564662010-02-22 19:32:311391void sqlite3_log(int iErrCode, const char *zFormat, ...){
drh3f280702010-02-18 18:45:091392 va_list ap; /* Vararg list */
drh7c0c4602010-03-03 22:25:181393 if( sqlite3GlobalConfig.xLog ){
drh3f280702010-02-18 18:45:091394 va_start(ap, zFormat);
drh7c0c4602010-03-03 22:25:181395 renderLogMsg(iErrCode, zFormat, ap);
drh3f280702010-02-18 18:45:091396 va_end(ap);
drh3f280702010-02-18 18:45:091397 }
1398}
1399
mistachkin02b0e262015-04-16 03:37:191400#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drhe54ca3f2004-06-07 01:52:141401/*
1402** A version of printf() that understands %lld. Used for debugging.
1403** The printf() built into some versions of windows does not understand %lld
1404** and segfaults if you give it a long long int.
1405*/
1406void sqlite3DebugPrintf(const char *zFormat, ...){
1407 va_list ap;
drhade86482007-11-28 22:36:401408 StrAccum acc;
mistachkina8e41ec2020-05-15 01:18:071409 char zBuf[SQLITE_PRINT_BUF_SIZE*10];
drhc0490572015-05-02 11:45:531410 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
drhade86482007-11-28 22:36:401411 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:261412 sqlite3_str_vappendf(&acc, zFormat, ap);
drhe54ca3f2004-06-07 01:52:141413 va_end(ap);
drhbed8e7e2007-12-08 17:55:351414 sqlite3StrAccumFinish(&acc);
mistachkin4a9ff912017-11-09 17:29:041415#ifdef SQLITE_OS_TRACE_PROC
1416 {
1417 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
1418 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
1419 }
1420#else
drh485f0032007-01-26 19:23:331421 fprintf(stdout,"%s", zBuf);
drh2ac3ee92004-06-07 16:27:461422 fflush(stdout);
mistachkin4a9ff912017-11-09 17:29:041423#endif
drhe54ca3f2004-06-07 01:52:141424}
1425#endif
drhc7bc4fd2009-11-25 18:03:421426
drh4fa4a542014-09-30 12:33:331427
drhc7bc4fd2009-11-25 18:03:421428/*
drh0cdbe1a2018-05-09 13:46:261429** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
drhd37bea52015-09-02 15:37:501430** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
drhc7bc4fd2009-11-25 18:03:421431*/
drh0cdbe1a2018-05-09 13:46:261432void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
drhc7bc4fd2009-11-25 18:03:421433 va_list ap;
1434 va_start(ap,zFormat);
drh0cdbe1a2018-05-09 13:46:261435 sqlite3_str_vappendf(p, zFormat, ap);
drhc7bc4fd2009-11-25 18:03:421436 va_end(ap);
1437}
drhf02cc9a2023-07-25 15:08:181438
1439
1440/*****************************************************************************
drhca1ce772023-12-01 12:57:121441** Reference counted string/blob storage
drhf02cc9a2023-07-25 15:08:181442*****************************************************************************/
1443
1444/*
1445** Increase the reference count of the string by one.
1446**
1447** The input parameter is returned.
1448*/
1449char *sqlite3RCStrRef(char *z){
1450 RCStr *p = (RCStr*)z;
1451 assert( p!=0 );
1452 p--;
1453 p->nRCRef++;
1454 return z;
1455}
1456
1457/*
1458** Decrease the reference count by one. Free the string when the
1459** reference count reaches zero.
1460*/
drh43dc31c2023-10-17 19:33:521461void sqlite3RCStrUnref(void *z){
drhf02cc9a2023-07-25 15:08:181462 RCStr *p = (RCStr*)z;
1463 assert( p!=0 );
1464 p--;
1465 assert( p->nRCRef>0 );
drhf02cc9a2023-07-25 15:08:181466 if( p->nRCRef>=2 ){
1467 p->nRCRef--;
1468 }else{
drhf02cc9a2023-07-25 15:08:181469 sqlite3_free(p);
1470 }
1471}
1472
drhf02cc9a2023-07-25 15:08:181473/*
1474** Create a new string that is capable of holding N bytes of text, not counting
1475** the zero byte at the end. The string is uninitialized.
1476**
1477** The reference count is initially 1. Call sqlite3RCStrUnref() to free the
1478** newly allocated string.
1479**
1480** This routine returns 0 on an OOM.
1481*/
1482char *sqlite3RCStrNew(u64 N){
drh59b8e662023-07-25 20:26:471483 RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 );
drhf02cc9a2023-07-25 15:08:181484 if( p==0 ) return 0;
1485 p->nRCRef = 1;
drhf02cc9a2023-07-25 15:08:181486 return (char*)&p[1];
1487}
1488
drhf02cc9a2023-07-25 15:08:181489/*
1490** Change the size of the string so that it is able to hold N bytes.
1491** The string might be reallocated, so return the new allocation.
1492*/
1493char *sqlite3RCStrResize(char *z, u64 N){
1494 RCStr *p = (RCStr*)z;
1495 RCStr *pNew;
1496 assert( p!=0 );
1497 p--;
1498 assert( p->nRCRef==1 );
drhf02cc9a2023-07-25 15:08:181499 pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1);
1500 if( pNew==0 ){
1501 sqlite3_free(p);
1502 return 0;
1503 }else{
1504 return (char*)&pNew[1];
1505 }
1506}