xref: /sqlite-3.40.0/src/util.c (revision 48864df9)
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   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
119     db->errCode = err_code;
120     if( zFormat ){
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{
128       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
129     }
130   }
131 }
132 
133 /*
134 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
135 ** The following formatting characters are allowed:
136 **
137 **      %s      Insert a string
138 **      %z      A string that should be freed after use
139 **      %d      Insert an integer
140 **      %T      Insert a token
141 **      %S      Insert the first element of a SrcList
142 **
143 ** This function should be used to report any error that occurs whilst
144 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
145 ** last thing the sqlite3_prepare() function does is copy the error
146 ** stored by this function into the database handle using sqlite3Error().
147 ** Function sqlite3Error() should be used during statement execution
148 ** (sqlite3_step() etc.).
149 */
150 void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
151   char *zMsg;
152   va_list ap;
153   sqlite3 *db = pParse->db;
154   va_start(ap, zFormat);
155   zMsg = sqlite3VMPrintf(db, zFormat, ap);
156   va_end(ap);
157   if( db->suppressErr ){
158     sqlite3DbFree(db, zMsg);
159   }else{
160     pParse->nErr++;
161     sqlite3DbFree(db, pParse->zErrMsg);
162     pParse->zErrMsg = zMsg;
163     pParse->rc = SQLITE_ERROR;
164   }
165 }
166 
167 /*
168 ** Convert an SQL-style quoted string into a normal string by removing
169 ** the quote characters.  The conversion is done in-place.  If the
170 ** input does not begin with a quote character, then this routine
171 ** is a no-op.
172 **
173 ** The input string must be zero-terminated.  A new zero-terminator
174 ** is added to the dequoted string.
175 **
176 ** The return value is -1 if no dequoting occurs or the length of the
177 ** dequoted string, exclusive of the zero terminator, if dequoting does
178 ** occur.
179 **
180 ** 2002-Feb-14: This routine is extended to remove MS-Access style
181 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
182 ** "a-b-c".
183 */
184 int sqlite3Dequote(char *z){
185   char quote;
186   int i, j;
187   if( z==0 ) return -1;
188   quote = z[0];
189   switch( quote ){
190     case '\'':  break;
191     case '"':   break;
192     case '`':   break;                /* For MySQL compatibility */
193     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
194     default:    return -1;
195   }
196   for(i=1, j=0; ALWAYS(z[i]); 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 9223372036854665808, return 2.  This special
468 ** case is broken out because while 9223372036854665808 cannot be a
469 ** signed 64-bit integer, its negative -9223372036854665808 can be.
470 **
471 ** If zNum is too big for a 64-bit integer and is not
472 ** 9223372036854665808  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 = SMALLEST_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+nonNum!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
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       assert( (*pNum)==SMALLEST_INT64 );
546       return neg ? 0 : 2;
547     }
548   }
549 }
550 
551 /*
552 ** If zNum represents an integer that will fit in 32-bits, then set
553 ** *pValue to that integer and return true.  Otherwise return false.
554 **
555 ** Any non-numeric characters that following zNum are ignored.
556 ** This is different from sqlite3Atoi64() which requires the
557 ** input number to be zero-terminated.
558 */
559 int sqlite3GetInt32(const char *zNum, int *pValue){
560   sqlite_int64 v = 0;
561   int i, c;
562   int neg = 0;
563   if( zNum[0]=='-' ){
564     neg = 1;
565     zNum++;
566   }else if( zNum[0]=='+' ){
567     zNum++;
568   }
569   while( zNum[0]=='0' ) zNum++;
570   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
571     v = v*10 + c;
572   }
573 
574   /* The longest decimal representation of a 32 bit integer is 10 digits:
575   **
576   **             1234567890
577   **     2^31 -> 2147483648
578   */
579   testcase( i==10 );
580   if( i>10 ){
581     return 0;
582   }
583   testcase( v-neg==2147483647 );
584   if( v-neg>2147483647 ){
585     return 0;
586   }
587   if( neg ){
588     v = -v;
589   }
590   *pValue = (int)v;
591   return 1;
592 }
593 
594 /*
595 ** Return a 32-bit integer value extracted from a string.  If the
596 ** string is not an integer, just return 0.
597 */
598 int sqlite3Atoi(const char *z){
599   int x = 0;
600   if( z ) sqlite3GetInt32(z, &x);
601   return x;
602 }
603 
604 /*
605 ** The variable-length integer encoding is as follows:
606 **
607 ** KEY:
608 **         A = 0xxxxxxx    7 bits of data and one flag bit
609 **         B = 1xxxxxxx    7 bits of data and one flag bit
610 **         C = xxxxxxxx    8 bits of data
611 **
612 **  7 bits - A
613 ** 14 bits - BA
614 ** 21 bits - BBA
615 ** 28 bits - BBBA
616 ** 35 bits - BBBBA
617 ** 42 bits - BBBBBA
618 ** 49 bits - BBBBBBA
619 ** 56 bits - BBBBBBBA
620 ** 64 bits - BBBBBBBBC
621 */
622 
623 /*
624 ** Write a 64-bit variable-length integer to memory starting at p[0].
625 ** The length of data write will be between 1 and 9 bytes.  The number
626 ** of bytes written is returned.
627 **
628 ** A variable-length integer consists of the lower 7 bits of each byte
629 ** for all bytes that have the 8th bit set and one byte with the 8th
630 ** bit clear.  Except, if we get to the 9th byte, it stores the full
631 ** 8 bits and is the last byte.
632 */
633 int sqlite3PutVarint(unsigned char *p, u64 v){
634   int i, j, n;
635   u8 buf[10];
636   if( v & (((u64)0xff000000)<<32) ){
637     p[8] = (u8)v;
638     v >>= 8;
639     for(i=7; i>=0; i--){
640       p[i] = (u8)((v & 0x7f) | 0x80);
641       v >>= 7;
642     }
643     return 9;
644   }
645   n = 0;
646   do{
647     buf[n++] = (u8)((v & 0x7f) | 0x80);
648     v >>= 7;
649   }while( v!=0 );
650   buf[0] &= 0x7f;
651   assert( n<=9 );
652   for(i=0, j=n-1; j>=0; j--, i++){
653     p[i] = buf[j];
654   }
655   return n;
656 }
657 
658 /*
659 ** This routine is a faster version of sqlite3PutVarint() that only
660 ** works for 32-bit positive integers and which is optimized for
661 ** the common case of small integers.  A MACRO version, putVarint32,
662 ** is provided which inlines the single-byte case.  All code should use
663 ** the MACRO version as this function assumes the single-byte case has
664 ** already been handled.
665 */
666 int sqlite3PutVarint32(unsigned char *p, u32 v){
667 #ifndef putVarint32
668   if( (v & ~0x7f)==0 ){
669     p[0] = v;
670     return 1;
671   }
672 #endif
673   if( (v & ~0x3fff)==0 ){
674     p[0] = (u8)((v>>7) | 0x80);
675     p[1] = (u8)(v & 0x7f);
676     return 2;
677   }
678   return sqlite3PutVarint(p, v);
679 }
680 
681 /*
682 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
683 ** are defined here rather than simply putting the constant expressions
684 ** inline in order to work around bugs in the RVT compiler.
685 **
686 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
687 **
688 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
689 */
690 #define SLOT_2_0     0x001fc07f
691 #define SLOT_4_2_0   0xf01fc07f
692 
693 
694 /*
695 ** Read a 64-bit variable-length integer from memory starting at p[0].
696 ** Return the number of bytes read.  The value is stored in *v.
697 */
698 u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
699   u32 a,b,s;
700 
701   a = *p;
702   /* a: p0 (unmasked) */
703   if (!(a&0x80))
704   {
705     *v = a;
706     return 1;
707   }
708 
709   p++;
710   b = *p;
711   /* b: p1 (unmasked) */
712   if (!(b&0x80))
713   {
714     a &= 0x7f;
715     a = a<<7;
716     a |= b;
717     *v = a;
718     return 2;
719   }
720 
721   /* Verify that constants are precomputed correctly */
722   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
723   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
724 
725   p++;
726   a = a<<14;
727   a |= *p;
728   /* a: p0<<14 | p2 (unmasked) */
729   if (!(a&0x80))
730   {
731     a &= SLOT_2_0;
732     b &= 0x7f;
733     b = b<<7;
734     a |= b;
735     *v = a;
736     return 3;
737   }
738 
739   /* CSE1 from below */
740   a &= SLOT_2_0;
741   p++;
742   b = b<<14;
743   b |= *p;
744   /* b: p1<<14 | p3 (unmasked) */
745   if (!(b&0x80))
746   {
747     b &= SLOT_2_0;
748     /* moved CSE1 up */
749     /* a &= (0x7f<<14)|(0x7f); */
750     a = a<<7;
751     a |= b;
752     *v = a;
753     return 4;
754   }
755 
756   /* a: p0<<14 | p2 (masked) */
757   /* b: p1<<14 | p3 (unmasked) */
758   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
759   /* moved CSE1 up */
760   /* a &= (0x7f<<14)|(0x7f); */
761   b &= SLOT_2_0;
762   s = a;
763   /* s: p0<<14 | p2 (masked) */
764 
765   p++;
766   a = a<<14;
767   a |= *p;
768   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
769   if (!(a&0x80))
770   {
771     /* we can skip these cause they were (effectively) done above in calc'ing s */
772     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
773     /* b &= (0x7f<<14)|(0x7f); */
774     b = b<<7;
775     a |= b;
776     s = s>>18;
777     *v = ((u64)s)<<32 | a;
778     return 5;
779   }
780 
781   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
782   s = s<<7;
783   s |= b;
784   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
785 
786   p++;
787   b = b<<14;
788   b |= *p;
789   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
790   if (!(b&0x80))
791   {
792     /* we can skip this cause it was (effectively) done above in calc'ing s */
793     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
794     a &= SLOT_2_0;
795     a = a<<7;
796     a |= b;
797     s = s>>18;
798     *v = ((u64)s)<<32 | a;
799     return 6;
800   }
801 
802   p++;
803   a = a<<14;
804   a |= *p;
805   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
806   if (!(a&0x80))
807   {
808     a &= SLOT_4_2_0;
809     b &= SLOT_2_0;
810     b = b<<7;
811     a |= b;
812     s = s>>11;
813     *v = ((u64)s)<<32 | a;
814     return 7;
815   }
816 
817   /* CSE2 from below */
818   a &= SLOT_2_0;
819   p++;
820   b = b<<14;
821   b |= *p;
822   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
823   if (!(b&0x80))
824   {
825     b &= SLOT_4_2_0;
826     /* moved CSE2 up */
827     /* a &= (0x7f<<14)|(0x7f); */
828     a = a<<7;
829     a |= b;
830     s = s>>4;
831     *v = ((u64)s)<<32 | a;
832     return 8;
833   }
834 
835   p++;
836   a = a<<15;
837   a |= *p;
838   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
839 
840   /* moved CSE2 up */
841   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
842   b &= SLOT_2_0;
843   b = b<<8;
844   a |= b;
845 
846   s = s<<4;
847   b = p[-4];
848   b &= 0x7f;
849   b = b>>3;
850   s |= b;
851 
852   *v = ((u64)s)<<32 | a;
853 
854   return 9;
855 }
856 
857 /*
858 ** Read a 32-bit variable-length integer from memory starting at p[0].
859 ** Return the number of bytes read.  The value is stored in *v.
860 **
861 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
862 ** integer, then set *v to 0xffffffff.
863 **
864 ** A MACRO version, getVarint32, is provided which inlines the
865 ** single-byte case.  All code should use the MACRO version as
866 ** this function assumes the single-byte case has already been handled.
867 */
868 u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
869   u32 a,b;
870 
871   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
872   ** by the getVarin32() macro */
873   a = *p;
874   /* a: p0 (unmasked) */
875 #ifndef getVarint32
876   if (!(a&0x80))
877   {
878     /* Values between 0 and 127 */
879     *v = a;
880     return 1;
881   }
882 #endif
883 
884   /* The 2-byte case */
885   p++;
886   b = *p;
887   /* b: p1 (unmasked) */
888   if (!(b&0x80))
889   {
890     /* Values between 128 and 16383 */
891     a &= 0x7f;
892     a = a<<7;
893     *v = a | b;
894     return 2;
895   }
896 
897   /* The 3-byte case */
898   p++;
899   a = a<<14;
900   a |= *p;
901   /* a: p0<<14 | p2 (unmasked) */
902   if (!(a&0x80))
903   {
904     /* Values between 16384 and 2097151 */
905     a &= (0x7f<<14)|(0x7f);
906     b &= 0x7f;
907     b = b<<7;
908     *v = a | b;
909     return 3;
910   }
911 
912   /* A 32-bit varint is used to store size information in btrees.
913   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
914   ** A 3-byte varint is sufficient, for example, to record the size
915   ** of a 1048569-byte BLOB or string.
916   **
917   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
918   ** rare larger cases can be handled by the slower 64-bit varint
919   ** routine.
920   */
921 #if 1
922   {
923     u64 v64;
924     u8 n;
925 
926     p -= 2;
927     n = sqlite3GetVarint(p, &v64);
928     assert( n>3 && n<=9 );
929     if( (v64 & SQLITE_MAX_U32)!=v64 ){
930       *v = 0xffffffff;
931     }else{
932       *v = (u32)v64;
933     }
934     return n;
935   }
936 
937 #else
938   /* For following code (kept for historical record only) shows an
939   ** unrolling for the 3- and 4-byte varint cases.  This code is
940   ** slightly faster, but it is also larger and much harder to test.
941   */
942   p++;
943   b = b<<14;
944   b |= *p;
945   /* b: p1<<14 | p3 (unmasked) */
946   if (!(b&0x80))
947   {
948     /* Values between 2097152 and 268435455 */
949     b &= (0x7f<<14)|(0x7f);
950     a &= (0x7f<<14)|(0x7f);
951     a = a<<7;
952     *v = a | b;
953     return 4;
954   }
955 
956   p++;
957   a = a<<14;
958   a |= *p;
959   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
960   if (!(a&0x80))
961   {
962     /* Values  between 268435456 and 34359738367 */
963     a &= SLOT_4_2_0;
964     b &= SLOT_4_2_0;
965     b = b<<7;
966     *v = a | b;
967     return 5;
968   }
969 
970   /* We can only reach this point when reading a corrupt database
971   ** file.  In that case we are not in any hurry.  Use the (relatively
972   ** slow) general-purpose sqlite3GetVarint() routine to extract the
973   ** value. */
974   {
975     u64 v64;
976     u8 n;
977 
978     p -= 4;
979     n = sqlite3GetVarint(p, &v64);
980     assert( n>5 && n<=9 );
981     *v = (u32)v64;
982     return n;
983   }
984 #endif
985 }
986 
987 /*
988 ** Return the number of bytes that will be needed to store the given
989 ** 64-bit integer.
990 */
991 int sqlite3VarintLen(u64 v){
992   int i = 0;
993   do{
994     i++;
995     v >>= 7;
996   }while( v!=0 && ALWAYS(i<9) );
997   return i;
998 }
999 
1000 
1001 /*
1002 ** Read or write a four-byte big-endian integer value.
1003 */
1004 u32 sqlite3Get4byte(const u8 *p){
1005   return (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     *pA += iB;
1127   }else{
1128     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
1129     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
1130     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
1131     *pA += iB;
1132   }
1133   return 0;
1134 }
1135 int sqlite3SubInt64(i64 *pA, i64 iB){
1136   testcase( iB==SMALLEST_INT64+1 );
1137   if( iB==SMALLEST_INT64 ){
1138     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
1139     if( (*pA)>=0 ) return 1;
1140     *pA -= iB;
1141     return 0;
1142   }else{
1143     return sqlite3AddInt64(pA, -iB);
1144   }
1145 }
1146 #define TWOPOWER32 (((i64)1)<<32)
1147 #define TWOPOWER31 (((i64)1)<<31)
1148 int sqlite3MulInt64(i64 *pA, i64 iB){
1149   i64 iA = *pA;
1150   i64 iA1, iA0, iB1, iB0, r;
1151 
1152   iA1 = iA/TWOPOWER32;
1153   iA0 = iA % TWOPOWER32;
1154   iB1 = iB/TWOPOWER32;
1155   iB0 = iB % TWOPOWER32;
1156   if( iA1*iB1 != 0 ) return 1;
1157   assert( iA1*iB0==0 || iA0*iB1==0 );
1158   r = iA1*iB0 + iA0*iB1;
1159   testcase( r==(-TWOPOWER31)-1 );
1160   testcase( r==(-TWOPOWER31) );
1161   testcase( r==TWOPOWER31 );
1162   testcase( r==TWOPOWER31-1 );
1163   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
1164   r *= TWOPOWER32;
1165   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
1166   *pA = r;
1167   return 0;
1168 }
1169 
1170 /*
1171 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
1172 ** if the integer has a value of -2147483648, return +2147483647
1173 */
1174 int sqlite3AbsInt32(int x){
1175   if( x>=0 ) return x;
1176   if( x==(int)0x80000000 ) return 0x7fffffff;
1177   return -x;
1178 }
1179 
1180 #ifdef SQLITE_ENABLE_8_3_NAMES
1181 /*
1182 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
1183 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
1184 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
1185 ** three characters, then shorten the suffix on z[] to be the last three
1186 ** characters of the original suffix.
1187 **
1188 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
1189 ** do the suffix shortening regardless of URI parameter.
1190 **
1191 ** Examples:
1192 **
1193 **     test.db-journal    =>   test.nal
1194 **     test.db-wal        =>   test.wal
1195 **     test.db-shm        =>   test.shm
1196 **     test.db-mj7f3319fa =>   test.9fa
1197 */
1198 void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
1199 #if SQLITE_ENABLE_8_3_NAMES<2
1200   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
1201 #endif
1202   {
1203     int i, sz;
1204     sz = sqlite3Strlen30(z);
1205     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
1206     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
1207   }
1208 }
1209 #endif
1210