xref: /sqlite-3.40.0/src/util.c (revision 962f9669)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** Utility functions used throughout sqlite.
13 **
14 ** This file contains functions for allocating memory, comparing
15 ** strings, and stuff like that.
16 **
17 */
18 #include "sqliteInt.h"
19 #include <stdarg.h>
20 #ifdef SQLITE_HAVE_ISNAN
21 # include <math.h>
22 #endif
23 
24 /*
25 ** Routine needed to support the testcase() macro.
26 */
27 #ifdef SQLITE_COVERAGE_TEST
28 void sqlite3Coverage(int x){
29   static unsigned dummy = 0;
30   dummy += (unsigned)x;
31 }
32 #endif
33 
34 #ifndef SQLITE_OMIT_FLOATING_POINT
35 /*
36 ** Return true if the floating point value is Not a Number (NaN).
37 **
38 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
39 ** Otherwise, we have our own implementation that works on most systems.
40 */
41 int sqlite3IsNaN(double x){
42   int rc;   /* The value return */
43 #if !defined(SQLITE_HAVE_ISNAN)
44   /*
45   ** Systems that support the isnan() library function should probably
46   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
47   ** found that many systems do not have a working isnan() function so
48   ** this implementation is provided as an alternative.
49   **
50   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
51   ** On the other hand, the use of -ffast-math comes with the following
52   ** warning:
53   **
54   **      This option [-ffast-math] should never be turned on by any
55   **      -O option since it can result in incorrect output for programs
56   **      which depend on an exact implementation of IEEE or ISO
57   **      rules/specifications for math functions.
58   **
59   ** Under MSVC, this NaN test may fail if compiled with a floating-
60   ** point precision mode other than /fp:precise.  From the MSDN
61   ** documentation:
62   **
63   **      The compiler [with /fp:precise] will properly handle comparisons
64   **      involving NaN. For example, x != x evaluates to true if x is NaN
65   **      ...
66   */
67 #ifdef __FAST_MATH__
68 # error SQLite will not work correctly with the -ffast-math option of GCC.
69 #endif
70   volatile double y = x;
71   volatile double z = y;
72   rc = (y!=z);
73 #else  /* if defined(SQLITE_HAVE_ISNAN) */
74   rc = isnan(x);
75 #endif /* SQLITE_HAVE_ISNAN */
76   testcase( rc );
77   return rc;
78 }
79 #endif /* SQLITE_OMIT_FLOATING_POINT */
80 
81 /*
82 ** Compute a string length that is limited to what can be stored in
83 ** lower 30 bits of a 32-bit signed integer.
84 **
85 ** The value returned will never be negative.  Nor will it ever be greater
86 ** than the actual length of the string.  For very long strings (greater
87 ** than 1GiB) the value returned might be less than the true string length.
88 */
89 int sqlite3Strlen30(const char *z){
90   const char *z2 = z;
91   if( z==0 ) return 0;
92   while( *z2 ){ z2++; }
93   return 0x3fffffff & (int)(z2 - z);
94 }
95 
96 /*
97 ** Set the most recent error code and error string for the sqlite
98 ** handle "db". The error code is set to "err_code".
99 **
100 ** If it is not NULL, string zFormat specifies the format of the
101 ** error string in the style of the printf functions: The following
102 ** format characters are allowed:
103 **
104 **      %s      Insert a string
105 **      %z      A string that should be freed after use
106 **      %d      Insert an integer
107 **      %T      Insert a token
108 **      %S      Insert the first element of a SrcList
109 **
110 ** zFormat and any string tokens that follow it are assumed to be
111 ** encoded in UTF-8.
112 **
113 ** To clear the most recent error for sqlite handle "db", sqlite3Error
114 ** should be called with err_code set to SQLITE_OK and zFormat set
115 ** to NULL.
116 */
117 void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
118   assert( db!=0 );
119   db->errCode = err_code;
120   if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
121     char *z;
122     va_list ap;
123     va_start(ap, zFormat);
124     z = sqlite3VMPrintf(db, zFormat, ap);
125     va_end(ap);
126     sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
127   }else if( db->pErr ){
128     sqlite3ValueSetNull(db->pErr);
129   }
130 }
131 
132 /*
133 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
134 ** The following formatting characters are allowed:
135 **
136 **      %s      Insert a string
137 **      %z      A string that should be freed after use
138 **      %d      Insert an integer
139 **      %T      Insert a token
140 **      %S      Insert the first element of a SrcList
141 **
142 ** This function should be used to report any error that occurs whilst
143 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
144 ** last thing the sqlite3_prepare() function does is copy the error
145 ** stored by this function into the database handle using sqlite3Error().
146 ** Function sqlite3Error() should be used during statement execution
147 ** (sqlite3_step() etc.).
148 */
149 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
150   char *zMsg;
151   va_list ap;
152   sqlite3 *db = pParse->db;
153   va_start(ap, zFormat);
154   zMsg = sqlite3VMPrintf(db, zFormat, ap);
155   va_end(ap);
156   if( db->suppressErr ){
157     sqlite3DbFree(db, zMsg);
158   }else{
159     pParse->nErr++;
160     sqlite3DbFree(db, pParse->zErrMsg);
161     pParse->zErrMsg = zMsg;
162     pParse->rc = SQLITE_ERROR;
163   }
164 }
165 
166 /*
167 ** Convert an SQL-style quoted string into a normal string by removing
168 ** the quote characters.  The conversion is done in-place.  If the
169 ** input does not begin with a quote character, then this routine
170 ** is a no-op.
171 **
172 ** The input string must be zero-terminated.  A new zero-terminator
173 ** is added to the dequoted string.
174 **
175 ** The return value is -1 if no dequoting occurs or the length of the
176 ** dequoted string, exclusive of the zero terminator, if dequoting does
177 ** occur.
178 **
179 ** 2002-Feb-14: This routine is extended to remove MS-Access style
180 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
181 ** "a-b-c".
182 */
183 int sqlite3Dequote(char *z){
184   char quote;
185   int i, j;
186   if( z==0 ) return -1;
187   quote = z[0];
188   switch( quote ){
189     case '\'':  break;
190     case '"':   break;
191     case '`':   break;                /* For MySQL compatibility */
192     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
193     default:    return -1;
194   }
195   for(i=1, j=0;; i++){
196     assert( z[i] );
197     if( z[i]==quote ){
198       if( z[i+1]==quote ){
199         z[j++] = quote;
200         i++;
201       }else{
202         break;
203       }
204     }else{
205       z[j++] = z[i];
206     }
207   }
208   z[j] = 0;
209   return j;
210 }
211 
212 /* Convenient short-hand */
213 #define UpperToLower sqlite3UpperToLower
214 
215 /*
216 ** Some systems have stricmp().  Others have strcasecmp().  Because
217 ** there is no consistency, we will define our own.
218 **
219 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
220 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
221 ** the contents of two buffers containing UTF-8 strings in a
222 ** case-independent fashion, using the same definition of "case
223 ** independence" that SQLite uses internally when comparing identifiers.
224 */
225 int sqlite3_stricmp(const char *zLeft, const char *zRight){
226   register unsigned char *a, *b;
227   a = (unsigned char *)zLeft;
228   b = (unsigned char *)zRight;
229   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
230   return UpperToLower[*a] - UpperToLower[*b];
231 }
232 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
233   register unsigned char *a, *b;
234   a = (unsigned char *)zLeft;
235   b = (unsigned char *)zRight;
236   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
237   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
238 }
239 
240 /*
241 ** The string z[] is an text representation of a real number.
242 ** Convert this string to a double and write it into *pResult.
243 **
244 ** The string z[] is length bytes in length (bytes, not characters) and
245 ** uses the encoding enc.  The string is not necessarily zero-terminated.
246 **
247 ** Return TRUE if the result is a valid real number (or integer) and FALSE
248 ** if the string is empty or contains extraneous text.  Valid numbers
249 ** are in one of these formats:
250 **
251 **    [+-]digits[E[+-]digits]
252 **    [+-]digits.[digits][E[+-]digits]
253 **    [+-].digits[E[+-]digits]
254 **
255 ** Leading and trailing whitespace is ignored for the purpose of determining
256 ** validity.
257 **
258 ** If some prefix of the input string is a valid number, this routine
259 ** returns FALSE but it still converts the prefix and writes the result
260 ** into *pResult.
261 */
262 int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
263 #ifndef SQLITE_OMIT_FLOATING_POINT
264   int incr;
265   const char *zEnd = z + length;
266   /* sign * significand * (10 ^ (esign * exponent)) */
267   int sign = 1;    /* sign of significand */
268   i64 s = 0;       /* significand */
269   int d = 0;       /* adjust exponent for shifting decimal point */
270   int esign = 1;   /* sign of exponent */
271   int e = 0;       /* exponent */
272   int eValid = 1;  /* True exponent is either not used or is well-formed */
273   double result;
274   int nDigits = 0;
275   int nonNum = 0;
276 
277   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
278   *pResult = 0.0;   /* Default return value, in case of an error */
279 
280   if( enc==SQLITE_UTF8 ){
281     incr = 1;
282   }else{
283     int i;
284     incr = 2;
285     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
286     for(i=3-enc; i<length && z[i]==0; i+=2){}
287     nonNum = i<length;
288     zEnd = z+i+enc-3;
289     z += (enc&1);
290   }
291 
292   /* skip leading spaces */
293   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
294   if( z>=zEnd ) return 0;
295 
296   /* get sign of significand */
297   if( *z=='-' ){
298     sign = -1;
299     z+=incr;
300   }else if( *z=='+' ){
301     z+=incr;
302   }
303 
304   /* skip leading zeroes */
305   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
306 
307   /* copy max significant digits to significand */
308   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
309     s = s*10 + (*z - '0');
310     z+=incr, nDigits++;
311   }
312 
313   /* skip non-significant significand digits
314   ** (increase exponent by d to shift decimal left) */
315   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
316   if( z>=zEnd ) goto do_atof_calc;
317 
318   /* if decimal point is present */
319   if( *z=='.' ){
320     z+=incr;
321     /* copy digits from after decimal to significand
322     ** (decrease exponent by d to shift decimal right) */
323     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
324       s = s*10 + (*z - '0');
325       z+=incr, nDigits++, d--;
326     }
327     /* skip non-significant digits */
328     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
329   }
330   if( z>=zEnd ) goto do_atof_calc;
331 
332   /* if exponent is present */
333   if( *z=='e' || *z=='E' ){
334     z+=incr;
335     eValid = 0;
336     if( z>=zEnd ) goto do_atof_calc;
337     /* get sign of exponent */
338     if( *z=='-' ){
339       esign = -1;
340       z+=incr;
341     }else if( *z=='+' ){
342       z+=incr;
343     }
344     /* copy digits to exponent */
345     while( z<zEnd && sqlite3Isdigit(*z) ){
346       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
347       z+=incr;
348       eValid = 1;
349     }
350   }
351 
352   /* skip trailing spaces */
353   if( nDigits && eValid ){
354     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
355   }
356 
357 do_atof_calc:
358   /* adjust exponent by d, and update sign */
359   e = (e*esign) + d;
360   if( e<0 ) {
361     esign = -1;
362     e *= -1;
363   } else {
364     esign = 1;
365   }
366 
367   /* if 0 significand */
368   if( !s ) {
369     /* In the IEEE 754 standard, zero is signed.
370     ** Add the sign if we've seen at least one digit */
371     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
372   } else {
373     /* attempt to reduce exponent */
374     if( esign>0 ){
375       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
376     }else{
377       while( !(s%10) && e>0 ) e--,s/=10;
378     }
379 
380     /* adjust the sign of significand */
381     s = sign<0 ? -s : s;
382 
383     /* if exponent, scale significand as appropriate
384     ** and store in result. */
385     if( e ){
386       LONGDOUBLE_TYPE scale = 1.0;
387       /* attempt to handle extremely small/large numbers better */
388       if( e>307 && e<342 ){
389         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
390         if( esign<0 ){
391           result = s / scale;
392           result /= 1.0e+308;
393         }else{
394           result = s * scale;
395           result *= 1.0e+308;
396         }
397       }else if( e>=342 ){
398         if( esign<0 ){
399           result = 0.0*s;
400         }else{
401           result = 1e308*1e308*s;  /* Infinity */
402         }
403       }else{
404         /* 1.0e+22 is the largest power of 10 than can be
405         ** represented exactly. */
406         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
407         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
408         if( esign<0 ){
409           result = s / scale;
410         }else{
411           result = s * scale;
412         }
413       }
414     } else {
415       result = (double)s;
416     }
417   }
418 
419   /* store the result */
420   *pResult = result;
421 
422   /* return true if number and no extra non-whitespace chracters after */
423   return z>=zEnd && nDigits>0 && eValid && nonNum==0;
424 #else
425   return !sqlite3Atoi64(z, pResult, length, enc);
426 #endif /* SQLITE_OMIT_FLOATING_POINT */
427 }
428 
429 /*
430 ** Compare the 19-character string zNum against the text representation
431 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
432 ** if zNum is less than, equal to, or greater than the string.
433 ** Note that zNum must contain exactly 19 characters.
434 **
435 ** Unlike memcmp() this routine is guaranteed to return the difference
436 ** in the values of the last digit if the only difference is in the
437 ** last digit.  So, for example,
438 **
439 **      compare2pow63("9223372036854775800", 1)
440 **
441 ** will return -8.
442 */
443 static int compare2pow63(const char *zNum, int incr){
444   int c = 0;
445   int i;
446                     /* 012345678901234567 */
447   const char *pow63 = "922337203685477580";
448   for(i=0; c==0 && i<18; i++){
449     c = (zNum[i*incr]-pow63[i])*10;
450   }
451   if( c==0 ){
452     c = zNum[18*incr] - '8';
453     testcase( c==(-1) );
454     testcase( c==0 );
455     testcase( c==(+1) );
456   }
457   return c;
458 }
459 
460 
461 /*
462 ** Convert zNum to a 64-bit signed integer.
463 **
464 ** If the zNum value is representable as a 64-bit twos-complement
465 ** integer, then write that value into *pNum and return 0.
466 **
467 ** If zNum is exactly 9223372036854775808, return 2.  This special
468 ** case is broken out because while 9223372036854775808 cannot be a
469 ** signed 64-bit integer, its negative -9223372036854775808 can be.
470 **
471 ** If zNum is too big for a 64-bit integer and is not
472 ** 9223372036854775808  or if zNum contains any non-numeric text,
473 ** then return 1.
474 **
475 ** length is the number of bytes in the string (bytes, not characters).
476 ** The string is not necessarily zero-terminated.  The encoding is
477 ** given by enc.
478 */
479 int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
480   int incr;
481   u64 u = 0;
482   int neg = 0; /* assume positive */
483   int i;
484   int c = 0;
485   int nonNum = 0;
486   const char *zStart;
487   const char *zEnd = zNum + length;
488   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
489   if( enc==SQLITE_UTF8 ){
490     incr = 1;
491   }else{
492     incr = 2;
493     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
494     for(i=3-enc; i<length && zNum[i]==0; i+=2){}
495     nonNum = i<length;
496     zEnd = zNum+i+enc-3;
497     zNum += (enc&1);
498   }
499   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
500   if( zNum<zEnd ){
501     if( *zNum=='-' ){
502       neg = 1;
503       zNum+=incr;
504     }else if( *zNum=='+' ){
505       zNum+=incr;
506     }
507   }
508   zStart = zNum;
509   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
510   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
511     u = u*10 + c - '0';
512   }
513   if( u>LARGEST_INT64 ){
514     *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
515   }else if( neg ){
516     *pNum = -(i64)u;
517   }else{
518     *pNum = (i64)u;
519   }
520   testcase( i==18 );
521   testcase( i==19 );
522   testcase( i==20 );
523   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
524     /* zNum is empty or contains non-numeric text or is longer
525     ** than 19 digits (thus guaranteeing that it is too large) */
526     return 1;
527   }else if( i<19*incr ){
528     /* Less than 19 digits, so we know that it fits in 64 bits */
529     assert( u<=LARGEST_INT64 );
530     return 0;
531   }else{
532     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
533     c = compare2pow63(zNum, incr);
534     if( c<0 ){
535       /* zNum is less than 9223372036854775808 so it fits */
536       assert( u<=LARGEST_INT64 );
537       return 0;
538     }else if( c>0 ){
539       /* zNum is greater than 9223372036854775808 so it overflows */
540       return 1;
541     }else{
542       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
543       ** special case 2 overflow if positive */
544       assert( u-1==LARGEST_INT64 );
545       return neg ? 0 : 2;
546     }
547   }
548 }
549 
550 /*
551 ** If zNum represents an integer that will fit in 32-bits, then set
552 ** *pValue to that integer and return true.  Otherwise return false.
553 **
554 ** Any non-numeric characters that following zNum are ignored.
555 ** This is different from sqlite3Atoi64() which requires the
556 ** input number to be zero-terminated.
557 */
558 int sqlite3GetInt32(const char *zNum, int *pValue){
559   sqlite_int64 v = 0;
560   int i, c;
561   int neg = 0;
562   if( zNum[0]=='-' ){
563     neg = 1;
564     zNum++;
565   }else if( zNum[0]=='+' ){
566     zNum++;
567   }
568   while( zNum[0]=='0' ) zNum++;
569   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
570     v = v*10 + c;
571   }
572 
573   /* The longest decimal representation of a 32 bit integer is 10 digits:
574   **
575   **             1234567890
576   **     2^31 -> 2147483648
577   */
578   testcase( i==10 );
579   if( i>10 ){
580     return 0;
581   }
582   testcase( v-neg==2147483647 );
583   if( v-neg>2147483647 ){
584     return 0;
585   }
586   if( neg ){
587     v = -v;
588   }
589   *pValue = (int)v;
590   return 1;
591 }
592 
593 /*
594 ** Return a 32-bit integer value extracted from a string.  If the
595 ** string is not an integer, just return 0.
596 */
597 int sqlite3Atoi(const char *z){
598   int x = 0;
599   if( z ) sqlite3GetInt32(z, &x);
600   return x;
601 }
602 
603 /*
604 ** The variable-length integer encoding is as follows:
605 **
606 ** KEY:
607 **         A = 0xxxxxxx    7 bits of data and one flag bit
608 **         B = 1xxxxxxx    7 bits of data and one flag bit
609 **         C = xxxxxxxx    8 bits of data
610 **
611 **  7 bits - A
612 ** 14 bits - BA
613 ** 21 bits - BBA
614 ** 28 bits - BBBA
615 ** 35 bits - BBBBA
616 ** 42 bits - BBBBBA
617 ** 49 bits - BBBBBBA
618 ** 56 bits - BBBBBBBA
619 ** 64 bits - BBBBBBBBC
620 */
621 
622 /*
623 ** Write a 64-bit variable-length integer to memory starting at p[0].
624 ** The length of data write will be between 1 and 9 bytes.  The number
625 ** of bytes written is returned.
626 **
627 ** A variable-length integer consists of the lower 7 bits of each byte
628 ** for all bytes that have the 8th bit set and one byte with the 8th
629 ** bit clear.  Except, if we get to the 9th byte, it stores the full
630 ** 8 bits and is the last byte.
631 */
632 int sqlite3PutVarint(unsigned char *p, u64 v){
633   int i, j, n;
634   u8 buf[10];
635   if( v & (((u64)0xff000000)<<32) ){
636     p[8] = (u8)v;
637     v >>= 8;
638     for(i=7; i>=0; i--){
639       p[i] = (u8)((v & 0x7f) | 0x80);
640       v >>= 7;
641     }
642     return 9;
643   }
644   n = 0;
645   do{
646     buf[n++] = (u8)((v & 0x7f) | 0x80);
647     v >>= 7;
648   }while( v!=0 );
649   buf[0] &= 0x7f;
650   assert( n<=9 );
651   for(i=0, j=n-1; j>=0; j--, i++){
652     p[i] = buf[j];
653   }
654   return n;
655 }
656 
657 /*
658 ** This routine is a faster version of sqlite3PutVarint() that only
659 ** works for 32-bit positive integers and which is optimized for
660 ** the common case of small integers.  A MACRO version, putVarint32,
661 ** is provided which inlines the single-byte case.  All code should use
662 ** the MACRO version as this function assumes the single-byte case has
663 ** already been handled.
664 */
665 int sqlite3PutVarint32(unsigned char *p, u32 v){
666 #ifndef putVarint32
667   if( (v & ~0x7f)==0 ){
668     p[0] = v;
669     return 1;
670   }
671 #endif
672   if( (v & ~0x3fff)==0 ){
673     p[0] = (u8)((v>>7) | 0x80);
674     p[1] = (u8)(v & 0x7f);
675     return 2;
676   }
677   return sqlite3PutVarint(p, v);
678 }
679 
680 /*
681 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
682 ** are defined here rather than simply putting the constant expressions
683 ** inline in order to work around bugs in the RVT compiler.
684 **
685 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
686 **
687 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
688 */
689 #define SLOT_2_0     0x001fc07f
690 #define SLOT_4_2_0   0xf01fc07f
691 
692 
693 /*
694 ** Read a 64-bit variable-length integer from memory starting at p[0].
695 ** Return the number of bytes read.  The value is stored in *v.
696 */
697 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
698   u32 a,b,s;
699 
700   a = *p;
701   /* a: p0 (unmasked) */
702   if (!(a&0x80))
703   {
704     *v = a;
705     return 1;
706   }
707 
708   p++;
709   b = *p;
710   /* b: p1 (unmasked) */
711   if (!(b&0x80))
712   {
713     a &= 0x7f;
714     a = a<<7;
715     a |= b;
716     *v = a;
717     return 2;
718   }
719 
720   /* Verify that constants are precomputed correctly */
721   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
722   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
723 
724   p++;
725   a = a<<14;
726   a |= *p;
727   /* a: p0<<14 | p2 (unmasked) */
728   if (!(a&0x80))
729   {
730     a &= SLOT_2_0;
731     b &= 0x7f;
732     b = b<<7;
733     a |= b;
734     *v = a;
735     return 3;
736   }
737 
738   /* CSE1 from below */
739   a &= SLOT_2_0;
740   p++;
741   b = b<<14;
742   b |= *p;
743   /* b: p1<<14 | p3 (unmasked) */
744   if (!(b&0x80))
745   {
746     b &= SLOT_2_0;
747     /* moved CSE1 up */
748     /* a &= (0x7f<<14)|(0x7f); */
749     a = a<<7;
750     a |= b;
751     *v = a;
752     return 4;
753   }
754 
755   /* a: p0<<14 | p2 (masked) */
756   /* b: p1<<14 | p3 (unmasked) */
757   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
758   /* moved CSE1 up */
759   /* a &= (0x7f<<14)|(0x7f); */
760   b &= SLOT_2_0;
761   s = a;
762   /* s: p0<<14 | p2 (masked) */
763 
764   p++;
765   a = a<<14;
766   a |= *p;
767   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
768   if (!(a&0x80))
769   {
770     /* we can skip these cause they were (effectively) done above in calc'ing s */
771     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
772     /* b &= (0x7f<<14)|(0x7f); */
773     b = b<<7;
774     a |= b;
775     s = s>>18;
776     *v = ((u64)s)<<32 | a;
777     return 5;
778   }
779 
780   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
781   s = s<<7;
782   s |= b;
783   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
784 
785   p++;
786   b = b<<14;
787   b |= *p;
788   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
789   if (!(b&0x80))
790   {
791     /* we can skip this cause it was (effectively) done above in calc'ing s */
792     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
793     a &= SLOT_2_0;
794     a = a<<7;
795     a |= b;
796     s = s>>18;
797     *v = ((u64)s)<<32 | a;
798     return 6;
799   }
800 
801   p++;
802   a = a<<14;
803   a |= *p;
804   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
805   if (!(a&0x80))
806   {
807     a &= SLOT_4_2_0;
808     b &= SLOT_2_0;
809     b = b<<7;
810     a |= b;
811     s = s>>11;
812     *v = ((u64)s)<<32 | a;
813     return 7;
814   }
815 
816   /* CSE2 from below */
817   a &= SLOT_2_0;
818   p++;
819   b = b<<14;
820   b |= *p;
821   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
822   if (!(b&0x80))
823   {
824     b &= SLOT_4_2_0;
825     /* moved CSE2 up */
826     /* a &= (0x7f<<14)|(0x7f); */
827     a = a<<7;
828     a |= b;
829     s = s>>4;
830     *v = ((u64)s)<<32 | a;
831     return 8;
832   }
833 
834   p++;
835   a = a<<15;
836   a |= *p;
837   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
838 
839   /* moved CSE2 up */
840   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
841   b &= SLOT_2_0;
842   b = b<<8;
843   a |= b;
844 
845   s = s<<4;
846   b = p[-4];
847   b &= 0x7f;
848   b = b>>3;
849   s |= b;
850 
851   *v = ((u64)s)<<32 | a;
852 
853   return 9;
854 }
855 
856 /*
857 ** Read a 32-bit variable-length integer from memory starting at p[0].
858 ** Return the number of bytes read.  The value is stored in *v.
859 **
860 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
861 ** integer, then set *v to 0xffffffff.
862 **
863 ** A MACRO version, getVarint32, is provided which inlines the
864 ** single-byte case.  All code should use the MACRO version as
865 ** this function assumes the single-byte case has already been handled.
866 */
867 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
868   u32 a,b;
869 
870   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
871   ** by the getVarin32() macro */
872   a = *p;
873   /* a: p0 (unmasked) */
874 #ifndef getVarint32
875   if (!(a&0x80))
876   {
877     /* Values between 0 and 127 */
878     *v = a;
879     return 1;
880   }
881 #endif
882 
883   /* The 2-byte case */
884   p++;
885   b = *p;
886   /* b: p1 (unmasked) */
887   if (!(b&0x80))
888   {
889     /* Values between 128 and 16383 */
890     a &= 0x7f;
891     a = a<<7;
892     *v = a | b;
893     return 2;
894   }
895 
896   /* The 3-byte case */
897   p++;
898   a = a<<14;
899   a |= *p;
900   /* a: p0<<14 | p2 (unmasked) */
901   if (!(a&0x80))
902   {
903     /* Values between 16384 and 2097151 */
904     a &= (0x7f<<14)|(0x7f);
905     b &= 0x7f;
906     b = b<<7;
907     *v = a | b;
908     return 3;
909   }
910 
911   /* A 32-bit varint is used to store size information in btrees.
912   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
913   ** A 3-byte varint is sufficient, for example, to record the size
914   ** of a 1048569-byte BLOB or string.
915   **
916   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
917   ** rare larger cases can be handled by the slower 64-bit varint
918   ** routine.
919   */
920 #if 1
921   {
922     u64 v64;
923     u8 n;
924 
925     p -= 2;
926     n = sqlite3GetVarint(p, &v64);
927     assert( n>3 && n<=9 );
928     if( (v64 & SQLITE_MAX_U32)!=v64 ){
929       *v = 0xffffffff;
930     }else{
931       *v = (u32)v64;
932     }
933     return n;
934   }
935 
936 #else
937   /* For following code (kept for historical record only) shows an
938   ** unrolling for the 3- and 4-byte varint cases.  This code is
939   ** slightly faster, but it is also larger and much harder to test.
940   */
941   p++;
942   b = b<<14;
943   b |= *p;
944   /* b: p1<<14 | p3 (unmasked) */
945   if (!(b&0x80))
946   {
947     /* Values between 2097152 and 268435455 */
948     b &= (0x7f<<14)|(0x7f);
949     a &= (0x7f<<14)|(0x7f);
950     a = a<<7;
951     *v = a | b;
952     return 4;
953   }
954 
955   p++;
956   a = a<<14;
957   a |= *p;
958   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
959   if (!(a&0x80))
960   {
961     /* Values  between 268435456 and 34359738367 */
962     a &= SLOT_4_2_0;
963     b &= SLOT_4_2_0;
964     b = b<<7;
965     *v = a | b;
966     return 5;
967   }
968 
969   /* We can only reach this point when reading a corrupt database
970   ** file.  In that case we are not in any hurry.  Use the (relatively
971   ** slow) general-purpose sqlite3GetVarint() routine to extract the
972   ** value. */
973   {
974     u64 v64;
975     u8 n;
976 
977     p -= 4;
978     n = sqlite3GetVarint(p, &v64);
979     assert( n>5 && n<=9 );
980     *v = (u32)v64;
981     return n;
982   }
983 #endif
984 }
985 
986 /*
987 ** Return the number of bytes that will be needed to store the given
988 ** 64-bit integer.
989 */
990 int sqlite3VarintLen(u64 v){
991   int i = 0;
992   do{
993     i++;
994     v >>= 7;
995   }while( v!=0 && ALWAYS(i<9) );
996   return i;
997 }
998 
999 
1000 /*
1001 ** Read or write a four-byte big-endian integer value.
1002 */
1003 u32 sqlite3Get4byte(const u8 *p){
1004   testcase( p[0]&0x80 );
1005   return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
1006 }
1007 void sqlite3Put4byte(unsigned char *p, u32 v){
1008   p[0] = (u8)(v>>24);
1009   p[1] = (u8)(v>>16);
1010   p[2] = (u8)(v>>8);
1011   p[3] = (u8)v;
1012 }
1013 
1014 
1015 
1016 /*
1017 ** Translate a single byte of Hex into an integer.
1018 ** This routine only works if h really is a valid hexadecimal
1019 ** character:  0..9a..fA..F
1020 */
1021 u8 sqlite3HexToInt(int h){
1022   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
1023 #ifdef SQLITE_ASCII
1024   h += 9*(1&(h>>6));
1025 #endif
1026 #ifdef SQLITE_EBCDIC
1027   h += 9*(1&~(h>>4));
1028 #endif
1029   return (u8)(h & 0xf);
1030 }
1031 
1032 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1033 /*
1034 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1035 ** value.  Return a pointer to its binary value.  Space to hold the
1036 ** binary value has been obtained from malloc and must be freed by
1037 ** the calling routine.
1038 */
1039 void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
1040   char *zBlob;
1041   int i;
1042 
1043   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
1044   n--;
1045   if( zBlob ){
1046     for(i=0; i<n; i+=2){
1047       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
1048     }
1049     zBlob[i/2] = 0;
1050   }
1051   return zBlob;
1052 }
1053 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1054 
1055 /*
1056 ** Log an error that is an API call on a connection pointer that should
1057 ** not have been used.  The "type" of connection pointer is given as the
1058 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
1059 */
1060 static void logBadConnection(const char *zType){
1061   sqlite3_log(SQLITE_MISUSE,
1062      "API call with %s database connection pointer",
1063      zType
1064   );
1065 }
1066 
1067 /*
1068 ** Check to make sure we have a valid db pointer.  This test is not
1069 ** foolproof but it does provide some measure of protection against
1070 ** misuse of the interface such as passing in db pointers that are
1071 ** NULL or which have been previously closed.  If this routine returns
1072 ** 1 it means that the db pointer is valid and 0 if it should not be
1073 ** dereferenced for any reason.  The calling function should invoke
1074 ** SQLITE_MISUSE immediately.
1075 **
1076 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
1077 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1078 ** open properly and is not fit for general use but which can be
1079 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
1080 */
1081 int sqlite3SafetyCheckOk(sqlite3 *db){
1082   u32 magic;
1083   if( db==0 ){
1084     logBadConnection("NULL");
1085     return 0;
1086   }
1087   magic = db->magic;
1088   if( magic!=SQLITE_MAGIC_OPEN ){
1089     if( sqlite3SafetyCheckSickOrOk(db) ){
1090       testcase( sqlite3GlobalConfig.xLog!=0 );
1091       logBadConnection("unopened");
1092     }
1093     return 0;
1094   }else{
1095     return 1;
1096   }
1097 }
1098 int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
1099   u32 magic;
1100   magic = db->magic;
1101   if( magic!=SQLITE_MAGIC_SICK &&
1102       magic!=SQLITE_MAGIC_OPEN &&
1103       magic!=SQLITE_MAGIC_BUSY ){
1104     testcase( sqlite3GlobalConfig.xLog!=0 );
1105     logBadConnection("invalid");
1106     return 0;
1107   }else{
1108     return 1;
1109   }
1110 }
1111 
1112 /*
1113 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
1114 ** the other 64-bit signed integer at *pA and store the result in *pA.
1115 ** Return 0 on success.  Or if the operation would have resulted in an
1116 ** overflow, leave *pA unchanged and return 1.
1117 */
1118 int sqlite3AddInt64(i64 *pA, i64 iB){
1119   i64 iA = *pA;
1120   testcase( iA==0 ); testcase( iA==1 );
1121   testcase( iB==-1 ); testcase( iB==0 );
1122   if( iB>=0 ){
1123     testcase( iA>0 && LARGEST_INT64 - iA == iB );
1124     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
1125     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
1126   }else{
1127     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
1128     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
1129     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
1130   }
1131   *pA += iB;
1132   return 0;
1133 }
1134 int sqlite3SubInt64(i64 *pA, i64 iB){
1135   testcase( iB==SMALLEST_INT64+1 );
1136   if( iB==SMALLEST_INT64 ){
1137     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
1138     if( (*pA)>=0 ) return 1;
1139     *pA -= iB;
1140     return 0;
1141   }else{
1142     return sqlite3AddInt64(pA, -iB);
1143   }
1144 }
1145 #define TWOPOWER32 (((i64)1)<<32)
1146 #define TWOPOWER31 (((i64)1)<<31)
1147 int sqlite3MulInt64(i64 *pA, i64 iB){
1148   i64 iA = *pA;
1149   i64 iA1, iA0, iB1, iB0, r;
1150 
1151   iA1 = iA/TWOPOWER32;
1152   iA0 = iA % TWOPOWER32;
1153   iB1 = iB/TWOPOWER32;
1154   iB0 = iB % TWOPOWER32;
1155   if( iA1==0 ){
1156     if( iB1==0 ){
1157       *pA *= iB;
1158       return 0;
1159     }
1160     r = iA0*iB1;
1161   }else if( iB1==0 ){
1162     r = iA1*iB0;
1163   }else{
1164     /* If both iA1 and iB1 are non-zero, overflow will result */
1165     return 1;
1166   }
1167   testcase( r==(-TWOPOWER31)-1 );
1168   testcase( r==(-TWOPOWER31) );
1169   testcase( r==TWOPOWER31 );
1170   testcase( r==TWOPOWER31-1 );
1171   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
1172   r *= TWOPOWER32;
1173   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
1174   *pA = r;
1175   return 0;
1176 }
1177 
1178 /*
1179 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
1180 ** if the integer has a value of -2147483648, return +2147483647
1181 */
1182 int sqlite3AbsInt32(int x){
1183   if( x>=0 ) return x;
1184   if( x==(int)0x80000000 ) return 0x7fffffff;
1185   return -x;
1186 }
1187 
1188 #ifdef SQLITE_ENABLE_8_3_NAMES
1189 /*
1190 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
1191 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
1192 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
1193 ** three characters, then shorten the suffix on z[] to be the last three
1194 ** characters of the original suffix.
1195 **
1196 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
1197 ** do the suffix shortening regardless of URI parameter.
1198 **
1199 ** Examples:
1200 **
1201 **     test.db-journal    =>   test.nal
1202 **     test.db-wal        =>   test.wal
1203 **     test.db-shm        =>   test.shm
1204 **     test.db-mj7f3319fa =>   test.9fa
1205 */
1206 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
1207 #if SQLITE_ENABLE_8_3_NAMES<2
1208   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
1209 #endif
1210   {
1211     int i, sz;
1212     sz = sqlite3Strlen30(z);
1213     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
1214     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
1215   }
1216 }
1217 #endif
1218 
1219 /*
1220 ** Find (an approximate) sum of two LogEst values.  This computation is
1221 ** not a simple "+" operator because LogEst is stored as a logarithmic
1222 ** value.
1223 **
1224 */
1225 LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
1226   static const unsigned char x[] = {
1227      10, 10,                         /* 0,1 */
1228       9, 9,                          /* 2,3 */
1229       8, 8,                          /* 4,5 */
1230       7, 7, 7,                       /* 6,7,8 */
1231       6, 6, 6,                       /* 9,10,11 */
1232       5, 5, 5,                       /* 12-14 */
1233       4, 4, 4, 4,                    /* 15-18 */
1234       3, 3, 3, 3, 3, 3,              /* 19-24 */
1235       2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
1236   };
1237   if( a>=b ){
1238     if( a>b+49 ) return a;
1239     if( a>b+31 ) return a+1;
1240     return a+x[a-b];
1241   }else{
1242     if( b>a+49 ) return b;
1243     if( b>a+31 ) return b+1;
1244     return b+x[b-a];
1245   }
1246 }
1247 
1248 /*
1249 ** Convert an integer into a LogEst.  In other words, compute a
1250 ** good approximatation for 10*log2(x).
1251 */
1252 LogEst sqlite3LogEst(u64 x){
1253   static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
1254   LogEst y = 40;
1255   if( x<8 ){
1256     if( x<2 ) return 0;
1257     while( x<8 ){  y -= 10; x <<= 1; }
1258   }else{
1259     while( x>255 ){ y += 40; x >>= 4; }
1260     while( x>15 ){  y += 10; x >>= 1; }
1261   }
1262   return a[x&7] + y - 10;
1263 }
1264 
1265 #ifndef SQLITE_OMIT_VIRTUALTABLE
1266 /*
1267 ** Convert a double into a LogEst
1268 ** In other words, compute an approximation for 10*log2(x).
1269 */
1270 LogEst sqlite3LogEstFromDouble(double x){
1271   u64 a;
1272   LogEst e;
1273   assert( sizeof(x)==8 && sizeof(a)==8 );
1274   if( x<=1 ) return 0;
1275   if( x<=2000000000 ) return sqlite3LogEst((u64)x);
1276   memcpy(&a, &x, 8);
1277   e = (a>>52) - 1022;
1278   return e*10;
1279 }
1280 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1281 
1282 /*
1283 ** Convert a LogEst into an integer.
1284 */
1285 u64 sqlite3LogEstToInt(LogEst x){
1286   u64 n;
1287   if( x<10 ) return 1;
1288   n = x%10;
1289   x /= 10;
1290   if( n>=5 ) n -= 2;
1291   else if( n>=1 ) n -= 1;
1292   if( x>=3 ){
1293     return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
1294   }
1295   return (n+8)>>(3-x);
1296 }
1297