blob: bcf55e0a05e24af50213c307dc5cf1cfa03c26f4 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Utility functions used throughout sqlite.
13**
14** This file contains functions for allocating memory, comparing
15** strings, and stuff like that.
16**
drh0a687d12008-07-08 14:52:07 +000017** $Id: util.c,v 1.234 2008/07/08 14:52:10 drh Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
20#include <stdarg.h>
21#include <ctype.h>
22
drh75897232000-05-29 14:26:00 +000023
24/*
drh0de3ae92008-04-28 16:55:26 +000025** Return true if the floating point value is Not a Number.
26*/
27int sqlite3IsNaN(double x){
drh8a54f9f2008-05-09 13:47:58 +000028 /* This NaN test sometimes fails if compiled on GCC with -ffast-math.
29 ** On the other hand, the use of -ffast-math comes with the following
30 ** warning:
31 **
32 ** This option [-ffast-math] should never be turned on by any
33 ** -O option since it can result in incorrect output for programs
34 ** which depend on an exact implementation of IEEE or ISO
35 ** rules/specifications for math functions.
36 */
drh8c4d5b22008-07-06 00:21:35 +000037#ifdef __FAST_MATH__
38# error SQLite will not work correctly with the -ffast-math option of GCC.
39#endif
drh0de3ae92008-04-28 16:55:26 +000040 volatile double y = x;
41 return x!=y;
42}
43
44/*
drh0a687d12008-07-08 14:52:07 +000045** Return the length of a string, except do not allow the string length
46** to exceed the SQLITE_LIMIT_LENGTH setting.
47*/
48int sqlite3Strlen(sqlite3 *db, const char *z){
49 const char *z2 = z;
50 while( *z2 ){ z2++; }
51 if( z2 > &z[db->aLimit[SQLITE_LIMIT_LENGTH]] ){
52 return db->aLimit[SQLITE_LIMIT_LENGTH];
53 }else{
54 return (int)(z2 - z);
55 }
56}
57
58/*
danielk19776622cce2004-05-20 11:00:52 +000059** Set the most recent error code and error string for the sqlite
60** handle "db". The error code is set to "err_code".
61**
62** If it is not NULL, string zFormat specifies the format of the
63** error string in the style of the printf functions: The following
64** format characters are allowed:
65**
66** %s Insert a string
67** %z A string that should be freed after use
68** %d Insert an integer
69** %T Insert a token
70** %S Insert the first element of a SrcList
71**
72** zFormat and any string tokens that follow it are assumed to be
73** encoded in UTF-8.
74**
danielk19770bb8f362005-05-23 13:00:57 +000075** To clear the most recent error for sqlite handle "db", sqlite3Error
danielk19776622cce2004-05-20 11:00:52 +000076** should be called with err_code set to SQLITE_OK and zFormat set
77** to NULL.
78*/
drh9bb575f2004-09-06 17:24:11 +000079void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
danielk19771e536952007-08-16 10:09:01 +000080 if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
danielk1977bfd6cce2004-06-18 04:24:54 +000081 db->errCode = err_code;
82 if( zFormat ){
83 char *z;
84 va_list ap;
85 va_start(ap, zFormat);
danielk19771e536952007-08-16 10:09:01 +000086 z = sqlite3VMPrintf(db, zFormat, ap);
danielk1977bfd6cce2004-06-18 04:24:54 +000087 va_end(ap);
drhb21c8cd2007-08-21 19:33:56 +000088 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free);
danielk1977bfd6cce2004-06-18 04:24:54 +000089 }else{
drhb21c8cd2007-08-21 19:33:56 +000090 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
danielk1977bfd6cce2004-06-18 04:24:54 +000091 }
danielk19776622cce2004-05-20 11:00:52 +000092 }
93}
94
95/*
drhda93d232003-03-31 02:12:46 +000096** Add an error message to pParse->zErrMsg and increment pParse->nErr.
97** The following formatting characters are allowed:
98**
99** %s Insert a string
100** %z A string that should be freed after use
101** %d Insert an integer
102** %T Insert a token
103** %S Insert the first element of a SrcList
danielk19779e6db7d2004-06-21 08:18:51 +0000104**
105** This function should be used to report any error that occurs whilst
106** compiling an SQL statement (i.e. within sqlite3_prepare()). The
107** last thing the sqlite3_prepare() function does is copy the error
108** stored by this function into the database handle using sqlite3Error().
109** Function sqlite3Error() should be used during statement execution
110** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +0000111*/
danielk19774adee202004-05-08 08:23:19 +0000112void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +0000113 va_list ap;
drhda93d232003-03-31 02:12:46 +0000114 pParse->nErr++;
danielk19771e536952007-08-16 10:09:01 +0000115 sqlite3_free(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +0000116 va_start(ap, zFormat);
danielk19771e536952007-08-16 10:09:01 +0000117 pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap);
drhda93d232003-03-31 02:12:46 +0000118 va_end(ap);
drh7e326c02007-05-15 16:51:37 +0000119 if( pParse->rc==SQLITE_OK ){
120 pParse->rc = SQLITE_ERROR;
121 }
drhda93d232003-03-31 02:12:46 +0000122}
123
124/*
drha0733842005-12-29 01:11:36 +0000125** Clear the error message in pParse, if any
126*/
127void sqlite3ErrorClear(Parse *pParse){
danielk19771e536952007-08-16 10:09:01 +0000128 sqlite3_free(pParse->zErrMsg);
drha0733842005-12-29 01:11:36 +0000129 pParse->zErrMsg = 0;
130 pParse->nErr = 0;
131}
132
133/*
drh982cef72000-05-30 16:27:03 +0000134** Convert an SQL-style quoted string into a normal string by removing
135** the quote characters. The conversion is done in-place. If the
136** input does not begin with a quote character, then this routine
137** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000138**
139** 2002-Feb-14: This routine is extended to remove MS-Access style
140** brackets from around identifers. For example: "[a-b-c]" becomes
141** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000142*/
danielk19774adee202004-05-08 08:23:19 +0000143void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000144 int quote;
145 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000146 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000147 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000148 switch( quote ){
149 case '\'': break;
150 case '"': break;
drh3d946622005-08-13 18:15:42 +0000151 case '`': break; /* For MySQL compatibility */
152 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
drh2f4392f2002-02-14 21:42:51 +0000153 default: return;
154 }
drh982cef72000-05-30 16:27:03 +0000155 for(i=1, j=0; z[i]; i++){
156 if( z[i]==quote ){
157 if( z[i+1]==quote ){
158 z[j++] = quote;
159 i++;
160 }else{
161 z[j++] = 0;
162 break;
163 }
164 }else{
165 z[j++] = z[i];
166 }
167 }
168}
169
drh40257ff2008-06-13 18:24:27 +0000170/* Convenient short-hand */
drh4e5ffc52004-08-31 00:52:37 +0000171#define UpperToLower sqlite3UpperToLower
drh75897232000-05-29 14:26:00 +0000172
173/*
drh967e8b72000-06-21 13:59:10 +0000174** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000175** there is no consistency, we will define our own.
176*/
danielk19774adee202004-05-08 08:23:19 +0000177int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000178 register unsigned char *a, *b;
179 a = (unsigned char *)zLeft;
180 b = (unsigned char *)zRight;
181 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drh2b735012004-07-15 13:23:21 +0000182 return UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000183}
danielk19774adee202004-05-08 08:23:19 +0000184int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000185 register unsigned char *a, *b;
186 a = (unsigned char *)zLeft;
187 b = (unsigned char *)zRight;
188 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000189 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000190}
191
drha5c2ad02000-09-14 01:21:10 +0000192/*
drh7a7c7392001-11-24 00:31:46 +0000193** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000194** string contains any character which is not part of a number. If
195** the string is numeric and contains the '.' character, set *realnum
196** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000197**
drh873cdcb2004-09-05 23:23:41 +0000198** An empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000199*/
danielk19778a6b5412004-05-24 07:04:25 +0000200int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000201 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000202 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000203 if( *z=='-' || *z=='+' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000204 if( !isdigit(*(u8*)z) ){
drhbb07e9a2003-04-16 02:17:35 +0000205 return 0;
drha5c2ad02000-09-14 01:21:10 +0000206 }
danielk19778a6b5412004-05-24 07:04:25 +0000207 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000208 if( realnum ) *realnum = 0;
drh4c755c02004-08-08 20:22:17 +0000209 while( isdigit(*(u8*)z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000210 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000211 z += incr;
drh4c755c02004-08-08 20:22:17 +0000212 if( !isdigit(*(u8*)z) ) return 0;
213 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000214 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000215 }
216 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000217 z += incr;
218 if( *z=='+' || *z=='-' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000219 if( !isdigit(*(u8*)z) ) return 0;
220 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000221 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000222 }
drh7a7c7392001-11-24 00:31:46 +0000223 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000224}
225
drh93a5c6b2003-12-23 02:17:35 +0000226/*
227** The string z[] is an ascii representation of a real number.
228** Convert this string to a double.
229**
230** This routine assumes that z[] really is a valid number. If it
231** is not, the result is undefined.
232**
233** This routine is used instead of the library atof() function because
234** the library atof() might want to use "," as the decimal point instead
235** of "." depending on how locale is set. But that would cause problems
236** for SQL. So this routine always uses "." regardless of locale.
237*/
drh487e2622005-06-25 18:42:14 +0000238int sqlite3AtoF(const char *z, double *pResult){
drhb37df7b2005-10-13 02:09:49 +0000239#ifndef SQLITE_OMIT_FLOATING_POINT
drh93a5c6b2003-12-23 02:17:35 +0000240 int sign = 1;
drh487e2622005-06-25 18:42:14 +0000241 const char *zBegin = z;
drh384eef32004-01-07 03:04:27 +0000242 LONGDOUBLE_TYPE v1 = 0.0;
drha06f17f2008-05-11 11:07:06 +0000243 int nSignificant = 0;
danielk19772be2be92007-05-16 17:50:45 +0000244 while( isspace(*(u8*)z) ) z++;
drh93a5c6b2003-12-23 02:17:35 +0000245 if( *z=='-' ){
246 sign = -1;
247 z++;
248 }else if( *z=='+' ){
249 z++;
250 }
drha06f17f2008-05-11 11:07:06 +0000251 while( z[0]=='0' ){
252 z++;
253 }
drh4c755c02004-08-08 20:22:17 +0000254 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000255 v1 = v1*10.0 + (*z - '0');
256 z++;
drha06f17f2008-05-11 11:07:06 +0000257 nSignificant++;
drh93a5c6b2003-12-23 02:17:35 +0000258 }
259 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000260 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000261 z++;
drha06f17f2008-05-11 11:07:06 +0000262 if( nSignificant==0 ){
263 while( z[0]=='0' ){
264 divisor *= 10.0;
265 z++;
266 }
267 }
drh4c755c02004-08-08 20:22:17 +0000268 while( isdigit(*(u8*)z) ){
drha06f17f2008-05-11 11:07:06 +0000269 if( nSignificant<18 ){
270 v1 = v1*10.0 + (*z - '0');
271 divisor *= 10.0;
272 nSignificant++;
273 }
drh93a5c6b2003-12-23 02:17:35 +0000274 z++;
275 }
276 v1 /= divisor;
277 }
278 if( *z=='e' || *z=='E' ){
279 int esign = 1;
280 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000281 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000282 z++;
283 if( *z=='-' ){
284 esign = -1;
285 z++;
286 }else if( *z=='+' ){
287 z++;
288 }
drh4c755c02004-08-08 20:22:17 +0000289 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000290 eval = eval*10 + *z - '0';
291 z++;
292 }
293 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
294 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
295 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
296 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
297 if( esign<0 ){
298 v1 /= scale;
299 }else{
300 v1 *= scale;
301 }
302 }
drh487e2622005-06-25 18:42:14 +0000303 *pResult = sign<0 ? -v1 : v1;
304 return z - zBegin;
drhb37df7b2005-10-13 02:09:49 +0000305#else
drhee858132007-05-08 20:37:38 +0000306 return sqlite3Atoi64(z, pResult);
drhb37df7b2005-10-13 02:09:49 +0000307#endif /* SQLITE_OMIT_FLOATING_POINT */
drh93a5c6b2003-12-23 02:17:35 +0000308}
309
drh202b2df2004-01-06 01:13:46 +0000310/*
drh217f4902007-06-25 17:28:00 +0000311** Compare the 19-character string zNum against the text representation
312** value 2^63: 9223372036854775808. Return negative, zero, or positive
313** if zNum is less than, equal to, or greater than the string.
314**
315** Unlike memcmp() this routine is guaranteed to return the difference
316** in the values of the last digit if the only difference is in the
317** last digit. So, for example,
318**
319** compare2pow63("9223372036854775800")
320**
321** will return -8.
322*/
323static int compare2pow63(const char *zNum){
324 int c;
325 c = memcmp(zNum,"922337203685477580",18);
326 if( c==0 ){
327 c = zNum[18] - '8';
328 }
329 return c;
330}
331
332
333/*
drhfec19aa2004-05-19 20:41:03 +0000334** Return TRUE if zNum is a 64-bit signed integer and write
335** the value of the integer into *pNum. If zNum is not an integer
336** or is an integer that is too large to be expressed with 64 bits,
drh217f4902007-06-25 17:28:00 +0000337** then return false.
drhfec19aa2004-05-19 20:41:03 +0000338**
339** When this routine was originally written it dealt with only
340** 32-bit numbers. At that time, it was much faster than the
341** atoi() library routine in RedHat 7.2.
342*/
drhb6a9ece2007-06-26 00:37:27 +0000343int sqlite3Atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000344 i64 v = 0;
345 int neg;
346 int i, c;
drh4b815922008-05-19 15:54:59 +0000347 const char *zStart;
danielk19772be2be92007-05-16 17:50:45 +0000348 while( isspace(*(u8*)zNum) ) zNum++;
drhfec19aa2004-05-19 20:41:03 +0000349 if( *zNum=='-' ){
350 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000351 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000352 }else if( *zNum=='+' ){
353 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000354 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000355 }else{
356 neg = 0;
357 }
drh4b815922008-05-19 15:54:59 +0000358 zStart = zNum;
drh217f4902007-06-25 17:28:00 +0000359 while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
drheb2e1762004-05-27 01:53:56 +0000360 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000361 v = v*10 + c - '0';
362 }
363 *pNum = neg ? -v : v;
drh4b815922008-05-19 15:54:59 +0000364 if( c!=0 || (i==0 && zStart==zNum) || i>19 ){
drh217f4902007-06-25 17:28:00 +0000365 /* zNum is empty or contains non-numeric text or is longer
366 ** than 19 digits (thus guaranting that it is too large) */
367 return 0;
368 }else if( i<19 ){
369 /* Less than 19 digits, so we know that it fits in 64 bits */
drhfec19aa2004-05-19 20:41:03 +0000370 return 1;
drh217f4902007-06-25 17:28:00 +0000371 }else{
372 /* 19-digit numbers must be no larger than 9223372036854775807 if positive
373 ** or 9223372036854775808 if negative. Note that 9223372036854665808
374 ** is 2^63. */
375 return compare2pow63(zNum)<neg;
drhfec19aa2004-05-19 20:41:03 +0000376 }
drhfec19aa2004-05-19 20:41:03 +0000377}
378
379/*
380** The string zNum represents an integer. There might be some other
381** information following the integer too, but that part is ignored.
382** If the integer that the prefix of zNum represents will fit in a
383** 64-bit signed integer, return TRUE. Otherwise return FALSE.
384**
385** This routine returns FALSE for the string -9223372036854775808 even that
386** that number will, in theory fit in a 64-bit integer. Positive
387** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
388** false.
389*/
drh598f1342007-10-23 15:39:45 +0000390int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
drhfec19aa2004-05-19 20:41:03 +0000391 int i, c;
drh217f4902007-06-25 17:28:00 +0000392 int neg = 0;
393 if( *zNum=='-' ){
394 neg = 1;
395 zNum++;
396 }else if( *zNum=='+' ){
397 zNum++;
398 }
drh598f1342007-10-23 15:39:45 +0000399 if( negFlag ) neg = 1-neg;
drh217f4902007-06-25 17:28:00 +0000400 while( *zNum=='0' ){
401 zNum++; /* Skip leading zeros. Ticket #2454 */
402 }
drhfec19aa2004-05-19 20:41:03 +0000403 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
drh217f4902007-06-25 17:28:00 +0000404 if( i<19 ){
405 /* Guaranteed to fit if less than 19 digits */
406 return 1;
407 }else if( i>19 ){
408 /* Guaranteed to be too big if greater than 19 digits */
409 return 0;
410 }else{
411 /* Compare against 2^63. */
412 return compare2pow63(zNum)<neg;
413 }
drhfec19aa2004-05-19 20:41:03 +0000414}
415
drh217f4902007-06-25 17:28:00 +0000416/*
417** If zNum represents an integer that will fit in 32-bits, then set
418** *pValue to that integer and return true. Otherwise return false.
419**
420** Any non-numeric characters that following zNum are ignored.
drhb6a9ece2007-06-26 00:37:27 +0000421** This is different from sqlite3Atoi64() which requires the
drh217f4902007-06-25 17:28:00 +0000422** input number to be zero-terminated.
423*/
424int sqlite3GetInt32(const char *zNum, int *pValue){
425 sqlite_int64 v = 0;
426 int i, c;
427 int neg = 0;
428 if( zNum[0]=='-' ){
429 neg = 1;
430 zNum++;
431 }else if( zNum[0]=='+' ){
432 zNum++;
433 }
434 while( zNum[0]=='0' ) zNum++;
danielk1977b8cdbec2007-09-01 10:01:12 +0000435 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
drh217f4902007-06-25 17:28:00 +0000436 v = v*10 + c;
437 }
danielk1977b8cdbec2007-09-01 10:01:12 +0000438
439 /* The longest decimal representation of a 32 bit integer is 10 digits:
440 **
441 ** 1234567890
442 ** 2^31 -> 2147483648
443 */
444 if( i>10 ){
drh217f4902007-06-25 17:28:00 +0000445 return 0;
446 }
447 if( v-neg>2147483647 ){
448 return 0;
449 }
450 if( neg ){
451 v = -v;
452 }
453 *pValue = (int)v;
454 return 1;
455}
drhdce2cbe2000-05-31 02:27:49 +0000456
457/*
drhd8820e82004-05-18 15:57:42 +0000458** The variable-length integer encoding is as follows:
459**
460** KEY:
461** A = 0xxxxxxx 7 bits of data and one flag bit
462** B = 1xxxxxxx 7 bits of data and one flag bit
463** C = xxxxxxxx 8 bits of data
464**
465** 7 bits - A
466** 14 bits - BA
467** 21 bits - BBA
468** 28 bits - BBBA
469** 35 bits - BBBBA
470** 42 bits - BBBBBA
471** 49 bits - BBBBBBA
472** 56 bits - BBBBBBBA
473** 64 bits - BBBBBBBBC
474*/
475
476/*
drh6d2fb152004-05-14 16:50:06 +0000477** Write a 64-bit variable-length integer to memory starting at p[0].
478** The length of data write will be between 1 and 9 bytes. The number
479** of bytes written is returned.
480**
481** A variable-length integer consists of the lower 7 bits of each byte
482** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +0000483** bit clear. Except, if we get to the 9th byte, it stores the full
484** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +0000485*/
danielk1977192ac1d2004-05-10 07:17:30 +0000486int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +0000487 int i, j, n;
488 u8 buf[10];
drhfe2093d2005-01-20 22:48:47 +0000489 if( v & (((u64)0xff000000)<<32) ){
drhd8820e82004-05-18 15:57:42 +0000490 p[8] = v;
491 v >>= 8;
492 for(i=7; i>=0; i--){
493 p[i] = (v & 0x7f) | 0x80;
494 v >>= 7;
495 }
496 return 9;
497 }
drh6d2fb152004-05-14 16:50:06 +0000498 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +0000499 do{
drh6d2fb152004-05-14 16:50:06 +0000500 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +0000501 v >>= 7;
502 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +0000503 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +0000504 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +0000505 for(i=0, j=n-1; j>=0; j--, i++){
506 p[i] = buf[j];
507 }
508 return n;
danielk1977192ac1d2004-05-10 07:17:30 +0000509}
danielk19774adee202004-05-08 08:23:19 +0000510
drh6d2fb152004-05-14 16:50:06 +0000511/*
drh35b5a332008-04-05 18:41:42 +0000512** This routine is a faster version of sqlite3PutVarint() that only
513** works for 32-bit positive integers and which is optimized for
shane952856a2008-04-28 17:41:30 +0000514** the common case of small integers. A MACRO version, putVarint32,
515** is provided which inlines the single-byte case. All code should use
516** the MACRO version as this function assumes the single-byte case has
517** already been handled.
drh35b5a332008-04-05 18:41:42 +0000518*/
519int sqlite3PutVarint32(unsigned char *p, u32 v){
shane952856a2008-04-28 17:41:30 +0000520#ifndef putVarint32
drh35b5a332008-04-05 18:41:42 +0000521 if( (v & ~0x7f)==0 ){
522 p[0] = v;
523 return 1;
shane952856a2008-04-28 17:41:30 +0000524 }
525#endif
526 if( (v & ~0x3fff)==0 ){
drh35b5a332008-04-05 18:41:42 +0000527 p[0] = (v>>7) | 0x80;
528 p[1] = v & 0x7f;
529 return 2;
drh35b5a332008-04-05 18:41:42 +0000530 }
shane952856a2008-04-28 17:41:30 +0000531 return sqlite3PutVarint(p, v);
drh35b5a332008-04-05 18:41:42 +0000532}
533
534/*
drh6d2fb152004-05-14 16:50:06 +0000535** Read a 64-bit variable-length integer from memory starting at p[0].
536** Return the number of bytes read. The value is stored in *v.
537*/
danielk197790e4d952004-05-10 10:05:53 +0000538int sqlite3GetVarint(const unsigned char *p, u64 *v){
shane356574e2008-05-01 02:47:03 +0000539 u32 a,b,s;
540
541 a = *p;
shane022e8af2008-06-12 02:16:44 +0000542 /* a: p0 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000543 if (!(a&0x80))
544 {
545 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000546 return 1;
547 }
shane356574e2008-05-01 02:47:03 +0000548
549 p++;
550 b = *p;
shane022e8af2008-06-12 02:16:44 +0000551 /* b: p1 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000552 if (!(b&0x80))
553 {
554 a &= 0x7f;
555 a = a<<7;
556 a |= b;
557 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000558 return 2;
559 }
shane356574e2008-05-01 02:47:03 +0000560
561 p++;
562 a = a<<14;
563 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000564 /* a: p0<<14 | p2 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000565 if (!(a&0x80))
566 {
567 a &= (0x7f<<14)|(0x7f);
568 b &= 0x7f;
569 b = b<<7;
570 a |= b;
571 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000572 return 3;
573 }
shane356574e2008-05-01 02:47:03 +0000574
shane022e8af2008-06-12 02:16:44 +0000575 /* CSE1 from below */
shane356574e2008-05-01 02:47:03 +0000576 a &= (0x7f<<14)|(0x7f);
577 p++;
578 b = b<<14;
579 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000580 /* b: p1<<14 | p3 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000581 if (!(b&0x80))
582 {
583 b &= (0x7f<<14)|(0x7f);
shane022e8af2008-06-12 02:16:44 +0000584 /* moved CSE1 up */
585 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000586 a = a<<7;
587 a |= b;
588 *v = a;
drh6d2fb152004-05-14 16:50:06 +0000589 return 4;
590 }
shane356574e2008-05-01 02:47:03 +0000591
shane022e8af2008-06-12 02:16:44 +0000592 /* a: p0<<14 | p2 (masked) */
593 /* b: p1<<14 | p3 (unmasked) */
594 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
595 /* moved CSE1 up */
596 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000597 b &= (0x7f<<14)|(0x7f);
598 s = a;
shane022e8af2008-06-12 02:16:44 +0000599 /* s: p0<<14 | p2 (masked) */
shane356574e2008-05-01 02:47:03 +0000600
601 p++;
602 a = a<<14;
603 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000604 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000605 if (!(a&0x80))
606 {
shane022e8af2008-06-12 02:16:44 +0000607 /* we can skip these cause they were (effectively) done above in calc'ing s */
608 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
609 /* b &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000610 b = b<<7;
611 a |= b;
612 s = s>>18;
613 *v = ((u64)s)<<32 | a;
614 return 5;
615 }
616
shane022e8af2008-06-12 02:16:44 +0000617 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
shane356574e2008-05-01 02:47:03 +0000618 s = s<<7;
619 s |= b;
shane022e8af2008-06-12 02:16:44 +0000620 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
shane356574e2008-05-01 02:47:03 +0000621
622 p++;
623 b = b<<14;
624 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000625 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000626 if (!(b&0x80))
627 {
shane022e8af2008-06-12 02:16:44 +0000628 /* we can skip this cause it was (effectively) done above in calc'ing s */
629 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000630 a &= (0x7f<<14)|(0x7f);
631 a = a<<7;
632 a |= b;
633 s = s>>18;
634 *v = ((u64)s)<<32 | a;
635 return 6;
636 }
637
638 p++;
639 a = a<<14;
640 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000641 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000642 if (!(a&0x80))
643 {
644 a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
645 b &= (0x7f<<14)|(0x7f);
646 b = b<<7;
647 a |= b;
648 s = s>>11;
649 *v = ((u64)s)<<32 | a;
650 return 7;
651 }
652
shane022e8af2008-06-12 02:16:44 +0000653 /* CSE2 from below */
shane356574e2008-05-01 02:47:03 +0000654 a &= (0x7f<<14)|(0x7f);
655 p++;
656 b = b<<14;
657 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000658 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000659 if (!(b&0x80))
660 {
661 b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
shane022e8af2008-06-12 02:16:44 +0000662 /* moved CSE2 up */
663 /* a &= (0x7f<<14)|(0x7f); */
shane356574e2008-05-01 02:47:03 +0000664 a = a<<7;
665 a |= b;
666 s = s>>4;
667 *v = ((u64)s)<<32 | a;
668 return 8;
669 }
670
671 p++;
672 a = a<<15;
673 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000674 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000675
shane022e8af2008-06-12 02:16:44 +0000676 /* moved CSE2 up */
677 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
shane356574e2008-05-01 02:47:03 +0000678 b &= (0x7f<<14)|(0x7f);
679 b = b<<8;
680 a |= b;
681
682 s = s<<4;
683 b = p[-4];
684 b &= 0x7f;
685 b = b>>3;
686 s |= b;
687
688 *v = ((u64)s)<<32 | a;
689
690 return 9;
drh6d2fb152004-05-14 16:50:06 +0000691}
692
693/*
694** Read a 32-bit variable-length integer from memory starting at p[0].
695** Return the number of bytes read. The value is stored in *v.
shane952856a2008-04-28 17:41:30 +0000696** A MACRO version, getVarint32, is provided which inlines the
697** single-byte case. All code should use the MACRO version as
698** this function assumes the single-byte case has already been handled.
drh6d2fb152004-05-14 16:50:06 +0000699*/
700int sqlite3GetVarint32(const unsigned char *p, u32 *v){
shane356574e2008-05-01 02:47:03 +0000701 u32 a,b;
702
703 a = *p;
shane022e8af2008-06-12 02:16:44 +0000704 /* a: p0 (unmasked) */
shane952856a2008-04-28 17:41:30 +0000705#ifndef getVarint32
shane356574e2008-05-01 02:47:03 +0000706 if (!(a&0x80))
707 {
708 *v = a;
drhe6f85e72004-12-25 01:03:13 +0000709 return 1;
710 }
shane952856a2008-04-28 17:41:30 +0000711#endif
shane356574e2008-05-01 02:47:03 +0000712
713 p++;
714 b = *p;
shane022e8af2008-06-12 02:16:44 +0000715 /* b: p1 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000716 if (!(b&0x80))
717 {
718 a &= 0x7f;
719 a = a<<7;
720 *v = a | b;
drhe6f85e72004-12-25 01:03:13 +0000721 return 2;
722 }
shane356574e2008-05-01 02:47:03 +0000723
724 p++;
725 a = a<<14;
726 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000727 /* a: p0<<14 | p2 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000728 if (!(a&0x80))
729 {
730 a &= (0x7f<<14)|(0x7f);
731 b &= 0x7f;
732 b = b<<7;
733 *v = a | b;
734 return 3;
735 }
736
737 p++;
738 b = b<<14;
739 b |= *p;
shane022e8af2008-06-12 02:16:44 +0000740 /* b: p1<<14 | p3 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000741 if (!(b&0x80))
742 {
743 b &= (0x7f<<14)|(0x7f);
744 a &= (0x7f<<14)|(0x7f);
745 a = a<<7;
746 *v = a | b;
747 return 4;
748 }
749
750 p++;
751 a = a<<14;
752 a |= *p;
shane022e8af2008-06-12 02:16:44 +0000753 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
shane356574e2008-05-01 02:47:03 +0000754 if (!(a&0x80))
755 {
756 a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
757 b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
758 b = b<<7;
759 *v = a | b;
760 return 5;
761 }
762
drhcec3e3e2008-05-13 16:41:50 +0000763 /* We can only reach this point when reading a corrupt database
764 ** file. In that case we are not in any hurry. Use the (relatively
765 ** slow) general-purpose sqlite3GetVarint() routine to extract the
766 ** value. */
shane356574e2008-05-01 02:47:03 +0000767 {
drhcec3e3e2008-05-13 16:41:50 +0000768 u64 v64;
769 int n;
770
771 p -= 4;
772 n = sqlite3GetVarint(p, &v64);
773 assert( n>5 && n<=9 );
774 *v = (u32)v64;
775 return n;
shane356574e2008-05-01 02:47:03 +0000776 }
danielk1977192ac1d2004-05-10 07:17:30 +0000777}
778
drh6d2fb152004-05-14 16:50:06 +0000779/*
780** Return the number of bytes that will be needed to store the given
781** 64-bit integer.
782*/
danielk1977192ac1d2004-05-10 07:17:30 +0000783int sqlite3VarintLen(u64 v){
784 int i = 0;
785 do{
786 i++;
787 v >>= 7;
drhd8820e82004-05-18 15:57:42 +0000788 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000789 return i;
790}
danielk1977c572ef72004-05-27 09:28:41 +0000791
drha3152892007-05-05 11:48:52 +0000792
793/*
danielk19771cc5ed82007-05-16 17:28:43 +0000794** Read or write a four-byte big-endian integer value.
drha3152892007-05-05 11:48:52 +0000795*/
drha3152892007-05-05 11:48:52 +0000796u32 sqlite3Get4byte(const u8 *p){
797 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
798}
drha3152892007-05-05 11:48:52 +0000799void sqlite3Put4byte(unsigned char *p, u32 v){
800 p[0] = v>>24;
801 p[1] = v>>16;
802 p[2] = v>>8;
803 p[3] = v;
804}
805
806
807
drh34533152008-03-19 13:03:33 +0000808#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000809/*
810** Translate a single byte of Hex into an integer.
drh335d29d2008-04-04 15:12:21 +0000811** This routinen only works if h really is a valid hexadecimal
812** character: 0..9a..fA..F
drh9d213ef2004-06-30 04:02:11 +0000813*/
814static int hexToInt(int h){
drh06fbb732008-04-11 19:37:55 +0000815 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
drh04924d82008-04-14 14:34:44 +0000816#ifdef SQLITE_ASCII
drh06fbb732008-04-11 19:37:55 +0000817 h += 9*(1&(h>>6));
drh04924d82008-04-14 14:34:44 +0000818#endif
819#ifdef SQLITE_EBCDIC
drh06fbb732008-04-11 19:37:55 +0000820 h += 9*(1&~(h>>4));
drh335d29d2008-04-04 15:12:21 +0000821#endif
drh06fbb732008-04-11 19:37:55 +0000822 return h & 0xf;
drh9d213ef2004-06-30 04:02:11 +0000823}
drh34533152008-03-19 13:03:33 +0000824#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drh9d213ef2004-06-30 04:02:11 +0000825
drhbf8aa332004-11-04 04:34:14 +0000826#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000827/*
828** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
829** value. Return a pointer to its binary value. Space to hold the
830** binary value has been obtained from malloc and must be freed by
831** the calling routine.
832*/
drhca48c902008-01-18 14:08:24 +0000833void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
danielk1977c572ef72004-05-27 09:28:41 +0000834 char *zBlob;
835 int i;
danielk1977c572ef72004-05-27 09:28:41 +0000836
drhca48c902008-01-18 14:08:24 +0000837 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
838 n--;
drh76f80792006-07-11 12:40:25 +0000839 if( zBlob ){
840 for(i=0; i<n; i+=2){
841 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
842 }
drhca48c902008-01-18 14:08:24 +0000843 zBlob[i/2] = 0;
danielk1977c572ef72004-05-27 09:28:41 +0000844 }
danielk19773fd0a732004-05-27 13:35:19 +0000845 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +0000846}
drhbf8aa332004-11-04 04:34:14 +0000847#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drhfe63d1c2004-09-08 20:13:04 +0000848
drha3152892007-05-05 11:48:52 +0000849
drhfe63d1c2004-09-08 20:13:04 +0000850/*
drha3152892007-05-05 11:48:52 +0000851** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
852** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
853** when this routine is called.
854**
855** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
856** value indicates that the database connection passed into the API is
857** open and is not being used by another thread. By changing the value
858** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
859** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
860** when the API exits.
861**
862** This routine is a attempt to detect if two threads use the
863** same sqlite* pointer at the same time. There is a race
864** condition so it is possible that the error is not detected.
865** But usually the problem will be seen. The result will be an
866** error which can be used to debug the application that is
867** using SQLite incorrectly.
868**
869** Ticket #202: If db->magic is not a valid open value, take care not
870** to modify the db structure at all. It could be that db is a stale
871** pointer. In other words, it could be that there has been a prior
872** call to sqlite3_close(db) and db has been deallocated. And we do
873** not want to write into deallocated memory.
drhfe63d1c2004-09-08 20:13:04 +0000874*/
drh7e8b8482008-01-23 03:03:05 +0000875#ifdef SQLITE_DEBUG
drha3152892007-05-05 11:48:52 +0000876int sqlite3SafetyOn(sqlite3 *db){
877 if( db->magic==SQLITE_MAGIC_OPEN ){
878 db->magic = SQLITE_MAGIC_BUSY;
drha0af99f2008-04-16 00:49:12 +0000879 assert( sqlite3_mutex_held(db->mutex) );
drha3152892007-05-05 11:48:52 +0000880 return 0;
881 }else if( db->magic==SQLITE_MAGIC_BUSY ){
882 db->magic = SQLITE_MAGIC_ERROR;
883 db->u1.isInterrupted = 1;
drhfe63d1c2004-09-08 20:13:04 +0000884 }
drha3152892007-05-05 11:48:52 +0000885 return 1;
drhfe63d1c2004-09-08 20:13:04 +0000886}
drh7e8b8482008-01-23 03:03:05 +0000887#endif
drha3152892007-05-05 11:48:52 +0000888
889/*
890** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
891** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
892** when this routine is called.
893*/
drh7e8b8482008-01-23 03:03:05 +0000894#ifdef SQLITE_DEBUG
drha3152892007-05-05 11:48:52 +0000895int sqlite3SafetyOff(sqlite3 *db){
896 if( db->magic==SQLITE_MAGIC_BUSY ){
897 db->magic = SQLITE_MAGIC_OPEN;
drha0af99f2008-04-16 00:49:12 +0000898 assert( sqlite3_mutex_held(db->mutex) );
drha3152892007-05-05 11:48:52 +0000899 return 0;
drh7e8b8482008-01-23 03:03:05 +0000900 }else{
drha3152892007-05-05 11:48:52 +0000901 db->magic = SQLITE_MAGIC_ERROR;
902 db->u1.isInterrupted = 1;
903 return 1;
904 }
905}
drh7e8b8482008-01-23 03:03:05 +0000906#endif
drh55176252008-01-22 14:50:16 +0000907
908/*
909** Check to make sure we have a valid db pointer. This test is not
910** foolproof but it does provide some measure of protection against
911** misuse of the interface such as passing in db pointers that are
912** NULL or which have been previously closed. If this routine returns
drh7e8b8482008-01-23 03:03:05 +0000913** 1 it means that the db pointer is valid and 0 if it should not be
drh55176252008-01-22 14:50:16 +0000914** dereferenced for any reason. The calling function should invoke
915** SQLITE_MISUSE immediately.
drh7e8b8482008-01-23 03:03:05 +0000916**
917** sqlite3SafetyCheckOk() requires that the db pointer be valid for
918** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
919** open properly and is not fit for general use but which can be
920** used as an argument to sqlite3_errmsg() or sqlite3_close().
drh55176252008-01-22 14:50:16 +0000921*/
drh7e8b8482008-01-23 03:03:05 +0000922int sqlite3SafetyCheckOk(sqlite3 *db){
drh55176252008-01-22 14:50:16 +0000923 int magic;
drh7e8b8482008-01-23 03:03:05 +0000924 if( db==0 ) return 0;
drh55176252008-01-22 14:50:16 +0000925 magic = db->magic;
drh7e8b8482008-01-23 03:03:05 +0000926 if( magic!=SQLITE_MAGIC_OPEN &&
927 magic!=SQLITE_MAGIC_BUSY ) return 0;
928 return 1;
929}
930int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
931 int magic;
932 if( db==0 ) return 0;
933 magic = db->magic;
934 if( magic!=SQLITE_MAGIC_SICK &&
935 magic!=SQLITE_MAGIC_OPEN &&
936 magic!=SQLITE_MAGIC_BUSY ) return 0;
937 return 1;
drh55176252008-01-22 14:50:16 +0000938}