blob: a5fb28f6e0c74eaa77a41d126226833d6cf797c0 [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**
danielk19772be2be92007-05-16 17:50:45 +000017** $Id: util.c,v 1.205 2007/05/16 17:50:46 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
danielk197713a68c32005-12-15 10:11:30 +000020#include "os.h"
drh75897232000-05-29 14:26:00 +000021#include <stdarg.h>
22#include <ctype.h>
23
drh75897232000-05-29 14:26:00 +000024
25/*
danielk19776622cce2004-05-20 11:00:52 +000026** Set the most recent error code and error string for the sqlite
27** handle "db". The error code is set to "err_code".
28**
29** If it is not NULL, string zFormat specifies the format of the
30** error string in the style of the printf functions: The following
31** format characters are allowed:
32**
33** %s Insert a string
34** %z A string that should be freed after use
35** %d Insert an integer
36** %T Insert a token
37** %S Insert the first element of a SrcList
38**
39** zFormat and any string tokens that follow it are assumed to be
40** encoded in UTF-8.
41**
danielk19770bb8f362005-05-23 13:00:57 +000042** To clear the most recent error for sqlite handle "db", sqlite3Error
danielk19776622cce2004-05-20 11:00:52 +000043** should be called with err_code set to SQLITE_OK and zFormat set
44** to NULL.
45*/
drh9bb575f2004-09-06 17:24:11 +000046void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
drhd1167392006-01-23 13:00:35 +000047 if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
danielk1977bfd6cce2004-06-18 04:24:54 +000048 db->errCode = err_code;
49 if( zFormat ){
50 char *z;
51 va_list ap;
52 va_start(ap, zFormat);
53 z = sqlite3VMPrintf(zFormat, ap);
54 va_end(ap);
55 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
56 }else{
57 sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
58 }
danielk19776622cce2004-05-20 11:00:52 +000059 }
60}
61
62/*
drhda93d232003-03-31 02:12:46 +000063** Add an error message to pParse->zErrMsg and increment pParse->nErr.
64** The following formatting 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
danielk19779e6db7d2004-06-21 08:18:51 +000071**
72** This function should be used to report any error that occurs whilst
73** compiling an SQL statement (i.e. within sqlite3_prepare()). The
74** last thing the sqlite3_prepare() function does is copy the error
75** stored by this function into the database handle using sqlite3Error().
76** Function sqlite3Error() should be used during statement execution
77** (sqlite3_step() etc.).
drhda93d232003-03-31 02:12:46 +000078*/
danielk19774adee202004-05-08 08:23:19 +000079void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
drhda93d232003-03-31 02:12:46 +000080 va_list ap;
drhda93d232003-03-31 02:12:46 +000081 pParse->nErr++;
drhda93d232003-03-31 02:12:46 +000082 sqliteFree(pParse->zErrMsg);
drhda93d232003-03-31 02:12:46 +000083 va_start(ap, zFormat);
danielk19774adee202004-05-08 08:23:19 +000084 pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
drhda93d232003-03-31 02:12:46 +000085 va_end(ap);
drh7e326c02007-05-15 16:51:37 +000086 if( pParse->rc==SQLITE_OK ){
87 pParse->rc = SQLITE_ERROR;
88 }
drhda93d232003-03-31 02:12:46 +000089}
90
91/*
drha0733842005-12-29 01:11:36 +000092** Clear the error message in pParse, if any
93*/
94void sqlite3ErrorClear(Parse *pParse){
95 sqliteFree(pParse->zErrMsg);
96 pParse->zErrMsg = 0;
97 pParse->nErr = 0;
98}
99
100/*
drh982cef72000-05-30 16:27:03 +0000101** Convert an SQL-style quoted string into a normal string by removing
102** the quote characters. The conversion is done in-place. If the
103** input does not begin with a quote character, then this routine
104** is a no-op.
drh2f4392f2002-02-14 21:42:51 +0000105**
106** 2002-Feb-14: This routine is extended to remove MS-Access style
107** brackets from around identifers. For example: "[a-b-c]" becomes
108** "a-b-c".
drh982cef72000-05-30 16:27:03 +0000109*/
danielk19774adee202004-05-08 08:23:19 +0000110void sqlite3Dequote(char *z){
drh982cef72000-05-30 16:27:03 +0000111 int quote;
112 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000113 if( z==0 ) return;
drh982cef72000-05-30 16:27:03 +0000114 quote = z[0];
drh2f4392f2002-02-14 21:42:51 +0000115 switch( quote ){
116 case '\'': break;
117 case '"': break;
drh3d946622005-08-13 18:15:42 +0000118 case '`': break; /* For MySQL compatibility */
119 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
drh2f4392f2002-02-14 21:42:51 +0000120 default: return;
121 }
drh982cef72000-05-30 16:27:03 +0000122 for(i=1, j=0; z[i]; i++){
123 if( z[i]==quote ){
124 if( z[i+1]==quote ){
125 z[j++] = quote;
126 i++;
127 }else{
128 z[j++] = 0;
129 break;
130 }
131 }else{
132 z[j++] = z[i];
133 }
134 }
135}
136
drh75897232000-05-29 14:26:00 +0000137/* An array to map all upper-case characters into their corresponding
138** lower-case character.
139*/
drh4e5ffc52004-08-31 00:52:37 +0000140const unsigned char sqlite3UpperToLower[] = {
drh9b8f4472006-04-04 01:54:55 +0000141#ifdef SQLITE_ASCII
drh75897232000-05-29 14:26:00 +0000142 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
143 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
144 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
145 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
146 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
147 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
148 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
149 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
150 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
151 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
152 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
153 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
154 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
155 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
156 252,253,254,255
drh9b8f4472006-04-04 01:54:55 +0000157#endif
158#ifdef SQLITE_EBCDIC
159 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
160 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
161 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
162 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
163 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
164 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
165 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
166 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
167 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
168 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
169 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
170 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
171 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
172 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
173 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
174 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
175#endif
drh75897232000-05-29 14:26:00 +0000176};
drh4e5ffc52004-08-31 00:52:37 +0000177#define UpperToLower sqlite3UpperToLower
drh75897232000-05-29 14:26:00 +0000178
179/*
drh967e8b72000-06-21 13:59:10 +0000180** Some systems have stricmp(). Others have strcasecmp(). Because
drh75897232000-05-29 14:26:00 +0000181** there is no consistency, we will define our own.
182*/
danielk19774adee202004-05-08 08:23:19 +0000183int sqlite3StrICmp(const char *zLeft, const char *zRight){
drh75897232000-05-29 14:26:00 +0000184 register unsigned char *a, *b;
185 a = (unsigned char *)zLeft;
186 b = (unsigned char *)zRight;
187 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
drh2b735012004-07-15 13:23:21 +0000188 return UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000189}
danielk19774adee202004-05-08 08:23:19 +0000190int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
drh75897232000-05-29 14:26:00 +0000191 register unsigned char *a, *b;
192 a = (unsigned char *)zLeft;
193 b = (unsigned char *)zRight;
194 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
danielk19770202b292004-06-09 09:55:16 +0000195 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
drh75897232000-05-29 14:26:00 +0000196}
197
drha5c2ad02000-09-14 01:21:10 +0000198/*
drh7a7c7392001-11-24 00:31:46 +0000199** Return TRUE if z is a pure numeric string. Return FALSE if the
danielk19773d1bfea2004-05-14 11:00:53 +0000200** string contains any character which is not part of a number. If
201** the string is numeric and contains the '.' character, set *realnum
202** to TRUE (otherwise FALSE).
drh7a7c7392001-11-24 00:31:46 +0000203**
drh873cdcb2004-09-05 23:23:41 +0000204** An empty string is considered non-numeric.
drha5c2ad02000-09-14 01:21:10 +0000205*/
danielk19778a6b5412004-05-24 07:04:25 +0000206int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
danielk1977dc8453f2004-06-12 00:42:34 +0000207 int incr = (enc==SQLITE_UTF8?1:2);
danielk197793cd0392004-06-30 02:35:51 +0000208 if( enc==SQLITE_UTF16BE ) z++;
danielk19778a6b5412004-05-24 07:04:25 +0000209 if( *z=='-' || *z=='+' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000210 if( !isdigit(*(u8*)z) ){
drhbb07e9a2003-04-16 02:17:35 +0000211 return 0;
drha5c2ad02000-09-14 01:21:10 +0000212 }
danielk19778a6b5412004-05-24 07:04:25 +0000213 z += incr;
danielk19773d1bfea2004-05-14 11:00:53 +0000214 if( realnum ) *realnum = 0;
drh4c755c02004-08-08 20:22:17 +0000215 while( isdigit(*(u8*)z) ){ z += incr; }
drh7a7c7392001-11-24 00:31:46 +0000216 if( *z=='.' ){
danielk19778a6b5412004-05-24 07:04:25 +0000217 z += incr;
drh4c755c02004-08-08 20:22:17 +0000218 if( !isdigit(*(u8*)z) ) return 0;
219 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000220 if( realnum ) *realnum = 1;
drhbb07e9a2003-04-16 02:17:35 +0000221 }
222 if( *z=='e' || *z=='E' ){
danielk19778a6b5412004-05-24 07:04:25 +0000223 z += incr;
224 if( *z=='+' || *z=='-' ) z += incr;
drh4c755c02004-08-08 20:22:17 +0000225 if( !isdigit(*(u8*)z) ) return 0;
226 while( isdigit(*(u8*)z) ){ z += incr; }
danielk19773d1bfea2004-05-14 11:00:53 +0000227 if( realnum ) *realnum = 1;
drha5c2ad02000-09-14 01:21:10 +0000228 }
drh7a7c7392001-11-24 00:31:46 +0000229 return *z==0;
drha5c2ad02000-09-14 01:21:10 +0000230}
231
drh93a5c6b2003-12-23 02:17:35 +0000232/*
233** The string z[] is an ascii representation of a real number.
234** Convert this string to a double.
235**
236** This routine assumes that z[] really is a valid number. If it
237** is not, the result is undefined.
238**
239** This routine is used instead of the library atof() function because
240** the library atof() might want to use "," as the decimal point instead
241** of "." depending on how locale is set. But that would cause problems
242** for SQL. So this routine always uses "." regardless of locale.
243*/
drh487e2622005-06-25 18:42:14 +0000244int sqlite3AtoF(const char *z, double *pResult){
drhb37df7b2005-10-13 02:09:49 +0000245#ifndef SQLITE_OMIT_FLOATING_POINT
drh93a5c6b2003-12-23 02:17:35 +0000246 int sign = 1;
drh487e2622005-06-25 18:42:14 +0000247 const char *zBegin = z;
drh384eef32004-01-07 03:04:27 +0000248 LONGDOUBLE_TYPE v1 = 0.0;
danielk19772be2be92007-05-16 17:50:45 +0000249 while( isspace(*(u8*)z) ) z++;
drh93a5c6b2003-12-23 02:17:35 +0000250 if( *z=='-' ){
251 sign = -1;
252 z++;
253 }else if( *z=='+' ){
254 z++;
255 }
drh4c755c02004-08-08 20:22:17 +0000256 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000257 v1 = v1*10.0 + (*z - '0');
258 z++;
259 }
260 if( *z=='.' ){
drh384eef32004-01-07 03:04:27 +0000261 LONGDOUBLE_TYPE divisor = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000262 z++;
drh4c755c02004-08-08 20:22:17 +0000263 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000264 v1 = v1*10.0 + (*z - '0');
265 divisor *= 10.0;
266 z++;
267 }
268 v1 /= divisor;
269 }
270 if( *z=='e' || *z=='E' ){
271 int esign = 1;
272 int eval = 0;
drh384eef32004-01-07 03:04:27 +0000273 LONGDOUBLE_TYPE scale = 1.0;
drh93a5c6b2003-12-23 02:17:35 +0000274 z++;
275 if( *z=='-' ){
276 esign = -1;
277 z++;
278 }else if( *z=='+' ){
279 z++;
280 }
drh4c755c02004-08-08 20:22:17 +0000281 while( isdigit(*(u8*)z) ){
drh93a5c6b2003-12-23 02:17:35 +0000282 eval = eval*10 + *z - '0';
283 z++;
284 }
285 while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
286 while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
287 while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
288 while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
289 if( esign<0 ){
290 v1 /= scale;
291 }else{
292 v1 *= scale;
293 }
294 }
drh487e2622005-06-25 18:42:14 +0000295 *pResult = sign<0 ? -v1 : v1;
296 return z - zBegin;
drhb37df7b2005-10-13 02:09:49 +0000297#else
drhee858132007-05-08 20:37:38 +0000298 return sqlite3Atoi64(z, pResult);
drhb37df7b2005-10-13 02:09:49 +0000299#endif /* SQLITE_OMIT_FLOATING_POINT */
drh93a5c6b2003-12-23 02:17:35 +0000300}
301
drh202b2df2004-01-06 01:13:46 +0000302/*
drhfec19aa2004-05-19 20:41:03 +0000303** Return TRUE if zNum is a 64-bit signed integer and write
304** the value of the integer into *pNum. If zNum is not an integer
305** or is an integer that is too large to be expressed with 64 bits,
306** then return false. If n>0 and the integer is string is not
307** exactly n bytes long, return false.
308**
309** When this routine was originally written it dealt with only
310** 32-bit numbers. At that time, it was much faster than the
311** atoi() library routine in RedHat 7.2.
312*/
drhee858132007-05-08 20:37:38 +0000313int sqlite3Atoi64(const char *zNum, i64 *pNum){
drhfec19aa2004-05-19 20:41:03 +0000314 i64 v = 0;
315 int neg;
316 int i, c;
danielk19772be2be92007-05-16 17:50:45 +0000317 while( isspace(*(u8*)zNum) ) zNum++;
drhfec19aa2004-05-19 20:41:03 +0000318 if( *zNum=='-' ){
319 neg = 1;
drheb2e1762004-05-27 01:53:56 +0000320 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000321 }else if( *zNum=='+' ){
322 neg = 0;
drheb2e1762004-05-27 01:53:56 +0000323 zNum++;
drhfec19aa2004-05-19 20:41:03 +0000324 }else{
325 neg = 0;
326 }
drheb2e1762004-05-27 01:53:56 +0000327 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
drhfec19aa2004-05-19 20:41:03 +0000328 v = v*10 + c - '0';
329 }
330 *pNum = neg ? -v : v;
331 return c==0 && i>0 &&
332 (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
333}
334
335/*
drh202b2df2004-01-06 01:13:46 +0000336** The string zNum represents an integer. There might be some other
337** information following the integer too, but that part is ignored.
338** If the integer that the prefix of zNum represents will fit in a
339** 32-bit signed integer, return TRUE. Otherwise return FALSE.
340**
341** This routine returns FALSE for the string -2147483648 even that
drh873cdcb2004-09-05 23:23:41 +0000342** that number will in fact fit in a 32-bit integer. But positive
drh202b2df2004-01-06 01:13:46 +0000343** 2147483648 will not fit in 32 bits. So it seems safer to return
344** false.
345*/
drhfec19aa2004-05-19 20:41:03 +0000346static int sqlite3FitsIn32Bits(const char *zNum){
drh202b2df2004-01-06 01:13:46 +0000347 int i, c;
348 if( *zNum=='-' || *zNum=='+' ) zNum++;
349 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
350 return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
351}
352
drhfec19aa2004-05-19 20:41:03 +0000353/*
354** If zNum represents an integer that will fit in 32-bits, then set
355** *pValue to that integer and return true. Otherwise return false.
356*/
357int sqlite3GetInt32(const char *zNum, int *pValue){
358 if( sqlite3FitsIn32Bits(zNum) ){
359 *pValue = atoi(zNum);
360 return 1;
361 }
362 return 0;
363}
364
365/*
366** The string zNum represents an integer. There might be some other
367** information following the integer too, but that part is ignored.
368** If the integer that the prefix of zNum represents will fit in a
369** 64-bit signed integer, return TRUE. Otherwise return FALSE.
370**
371** This routine returns FALSE for the string -9223372036854775808 even that
372** that number will, in theory fit in a 64-bit integer. Positive
373** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
374** false.
375*/
376int sqlite3FitsIn64Bits(const char *zNum){
377 int i, c;
378 if( *zNum=='-' || *zNum=='+' ) zNum++;
379 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
380 return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
381}
382
drhdce2cbe2000-05-31 02:27:49 +0000383
384/*
drhc60d0442004-09-30 13:43:13 +0000385** Check to make sure we have a valid db pointer. This test is not
386** foolproof but it does provide some measure of protection against
387** misuse of the interface such as passing in db pointers that are
388** NULL or which have been previously closed. If this routine returns
389** TRUE it means that the db pointer is invalid and should not be
390** dereferenced for any reason. The calling function should invoke
391** SQLITE_MISUSE immediately.
drhc22bd472002-05-10 13:14:07 +0000392*/
drh9bb575f2004-09-06 17:24:11 +0000393int sqlite3SafetyCheck(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +0000394 int magic;
395 if( db==0 ) return 1;
396 magic = db->magic;
397 if( magic!=SQLITE_MAGIC_CLOSED &&
398 magic!=SQLITE_MAGIC_OPEN &&
399 magic!=SQLITE_MAGIC_BUSY ) return 1;
drhc22bd472002-05-10 13:14:07 +0000400 return 0;
drh5e00f6c2001-09-13 13:46:56 +0000401}
danielk19774adee202004-05-08 08:23:19 +0000402
drh6d2fb152004-05-14 16:50:06 +0000403/*
drhd8820e82004-05-18 15:57:42 +0000404** The variable-length integer encoding is as follows:
405**
406** KEY:
407** A = 0xxxxxxx 7 bits of data and one flag bit
408** B = 1xxxxxxx 7 bits of data and one flag bit
409** C = xxxxxxxx 8 bits of data
410**
411** 7 bits - A
412** 14 bits - BA
413** 21 bits - BBA
414** 28 bits - BBBA
415** 35 bits - BBBBA
416** 42 bits - BBBBBA
417** 49 bits - BBBBBBA
418** 56 bits - BBBBBBBA
419** 64 bits - BBBBBBBBC
420*/
421
422/*
drh6d2fb152004-05-14 16:50:06 +0000423** Write a 64-bit variable-length integer to memory starting at p[0].
424** The length of data write will be between 1 and 9 bytes. The number
425** of bytes written is returned.
426**
427** A variable-length integer consists of the lower 7 bits of each byte
428** for all bytes that have the 8th bit set and one byte with the 8th
drhd8820e82004-05-18 15:57:42 +0000429** bit clear. Except, if we get to the 9th byte, it stores the full
430** 8 bits and is the last byte.
drh6d2fb152004-05-14 16:50:06 +0000431*/
danielk1977192ac1d2004-05-10 07:17:30 +0000432int sqlite3PutVarint(unsigned char *p, u64 v){
drh6d2fb152004-05-14 16:50:06 +0000433 int i, j, n;
434 u8 buf[10];
drhfe2093d2005-01-20 22:48:47 +0000435 if( v & (((u64)0xff000000)<<32) ){
drhd8820e82004-05-18 15:57:42 +0000436 p[8] = v;
437 v >>= 8;
438 for(i=7; i>=0; i--){
439 p[i] = (v & 0x7f) | 0x80;
440 v >>= 7;
441 }
442 return 9;
443 }
drh6d2fb152004-05-14 16:50:06 +0000444 n = 0;
danielk1977192ac1d2004-05-10 07:17:30 +0000445 do{
drh6d2fb152004-05-14 16:50:06 +0000446 buf[n++] = (v & 0x7f) | 0x80;
danielk1977192ac1d2004-05-10 07:17:30 +0000447 v >>= 7;
448 }while( v!=0 );
drh6d2fb152004-05-14 16:50:06 +0000449 buf[0] &= 0x7f;
drhd8820e82004-05-18 15:57:42 +0000450 assert( n<=9 );
drh6d2fb152004-05-14 16:50:06 +0000451 for(i=0, j=n-1; j>=0; j--, i++){
452 p[i] = buf[j];
453 }
454 return n;
danielk1977192ac1d2004-05-10 07:17:30 +0000455}
danielk19774adee202004-05-08 08:23:19 +0000456
drh6d2fb152004-05-14 16:50:06 +0000457/*
458** Read a 64-bit variable-length integer from memory starting at p[0].
459** Return the number of bytes read. The value is stored in *v.
460*/
danielk197790e4d952004-05-10 10:05:53 +0000461int sqlite3GetVarint(const unsigned char *p, u64 *v){
drh6d2fb152004-05-14 16:50:06 +0000462 u32 x;
463 u64 x64;
464 int n;
465 unsigned char c;
drhe51c44f2004-05-30 20:46:09 +0000466 if( ((c = p[0]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000467 *v = c;
468 return 1;
469 }
470 x = c & 0x7f;
drhe51c44f2004-05-30 20:46:09 +0000471 if( ((c = p[1]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000472 *v = (x<<7) | c;
473 return 2;
474 }
475 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000476 if( ((c = p[2]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000477 *v = (x<<7) | c;
478 return 3;
479 }
480 x = (x<<7) | (c&0x7f);
drhe51c44f2004-05-30 20:46:09 +0000481 if( ((c = p[3]) & 0x80)==0 ){
drh6d2fb152004-05-14 16:50:06 +0000482 *v = (x<<7) | c;
483 return 4;
484 }
485 x64 = (x<<7) | (c&0x7f);
486 n = 4;
487 do{
488 c = p[n++];
drhd8820e82004-05-18 15:57:42 +0000489 if( n==9 ){
490 x64 = (x64<<8) | c;
491 break;
492 }
drh6d2fb152004-05-14 16:50:06 +0000493 x64 = (x64<<7) | (c&0x7f);
494 }while( (c & 0x80)!=0 );
495 *v = x64;
496 return n;
497}
498
499/*
500** Read a 32-bit variable-length integer from memory starting at p[0].
501** Return the number of bytes read. The value is stored in *v.
502*/
503int sqlite3GetVarint32(const unsigned char *p, u32 *v){
drhe51c44f2004-05-30 20:46:09 +0000504 u32 x;
505 int n;
506 unsigned char c;
drhe6f85e72004-12-25 01:03:13 +0000507 if( ((signed char*)p)[0]>=0 ){
508 *v = p[0];
509 return 1;
510 }
511 x = p[0] & 0x7f;
512 if( ((signed char*)p)[1]>=0 ){
513 *v = (x<<7) | p[1];
514 return 2;
515 }
516 x = (x<<7) | (p[1] & 0x7f);
drh9d213ef2004-06-30 04:02:11 +0000517 n = 2;
drhe51c44f2004-05-30 20:46:09 +0000518 do{
519 x = (x<<7) | ((c = p[n++])&0x7f);
520 }while( (c & 0x80)!=0 && n<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000521 *v = x;
522 return n;
523}
524
drh6d2fb152004-05-14 16:50:06 +0000525/*
526** Return the number of bytes that will be needed to store the given
527** 64-bit integer.
528*/
danielk1977192ac1d2004-05-10 07:17:30 +0000529int sqlite3VarintLen(u64 v){
530 int i = 0;
531 do{
532 i++;
533 v >>= 7;
drhd8820e82004-05-18 15:57:42 +0000534 }while( v!=0 && i<9 );
danielk1977192ac1d2004-05-10 07:17:30 +0000535 return i;
536}
danielk1977c572ef72004-05-27 09:28:41 +0000537
drha3152892007-05-05 11:48:52 +0000538
539/*
danielk19771cc5ed82007-05-16 17:28:43 +0000540** Read or write a four-byte big-endian integer value.
drha3152892007-05-05 11:48:52 +0000541*/
drha3152892007-05-05 11:48:52 +0000542u32 sqlite3Get4byte(const u8 *p){
543 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
544}
drha3152892007-05-05 11:48:52 +0000545void sqlite3Put4byte(unsigned char *p, u32 v){
546 p[0] = v>>24;
547 p[1] = v>>16;
548 p[2] = v>>8;
549 p[3] = v;
550}
551
552
553
drh33fa5352005-03-10 12:35:45 +0000554#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
drha71aa002004-11-03 13:59:04 +0000555 || defined(SQLITE_TEST)
drh9d213ef2004-06-30 04:02:11 +0000556/*
557** Translate a single byte of Hex into an integer.
558*/
559static int hexToInt(int h){
560 if( h>='0' && h<='9' ){
561 return h - '0';
562 }else if( h>='a' && h<='f' ){
563 return h - 'a' + 10;
drh9d213ef2004-06-30 04:02:11 +0000564 }else{
drhcacb2082005-01-11 15:28:33 +0000565 assert( h>='A' && h<='F' );
566 return h - 'A' + 10;
drh9d213ef2004-06-30 04:02:11 +0000567 }
568}
drh33fa5352005-03-10 12:35:45 +0000569#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
drh9d213ef2004-06-30 04:02:11 +0000570
drhbf8aa332004-11-04 04:34:14 +0000571#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
drh9d213ef2004-06-30 04:02:11 +0000572/*
573** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
574** value. Return a pointer to its binary value. Space to hold the
575** binary value has been obtained from malloc and must be freed by
576** the calling routine.
577*/
danielk1977cbb18d22004-05-28 11:37:27 +0000578void *sqlite3HexToBlob(const char *z){
danielk1977c572ef72004-05-27 09:28:41 +0000579 char *zBlob;
580 int i;
581 int n = strlen(z);
582 if( n%2 ) return 0;
583
584 zBlob = (char *)sqliteMalloc(n/2);
drh76f80792006-07-11 12:40:25 +0000585 if( zBlob ){
586 for(i=0; i<n; i+=2){
587 zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
588 }
danielk1977c572ef72004-05-27 09:28:41 +0000589 }
danielk19773fd0a732004-05-27 13:35:19 +0000590 return zBlob;
danielk1977c572ef72004-05-27 09:28:41 +0000591}
drhbf8aa332004-11-04 04:34:14 +0000592#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
drhfe63d1c2004-09-08 20:13:04 +0000593
drha3152892007-05-05 11:48:52 +0000594
drhfe63d1c2004-09-08 20:13:04 +0000595/*
drha3152892007-05-05 11:48:52 +0000596** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
597** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
598** when this routine is called.
599**
600** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
601** value indicates that the database connection passed into the API is
602** open and is not being used by another thread. By changing the value
603** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
604** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
605** when the API exits.
606**
607** This routine is a attempt to detect if two threads use the
608** same sqlite* pointer at the same time. There is a race
609** condition so it is possible that the error is not detected.
610** But usually the problem will be seen. The result will be an
611** error which can be used to debug the application that is
612** using SQLite incorrectly.
613**
614** Ticket #202: If db->magic is not a valid open value, take care not
615** to modify the db structure at all. It could be that db is a stale
616** pointer. In other words, it could be that there has been a prior
617** call to sqlite3_close(db) and db has been deallocated. And we do
618** not want to write into deallocated memory.
drhfe63d1c2004-09-08 20:13:04 +0000619*/
drha3152892007-05-05 11:48:52 +0000620int sqlite3SafetyOn(sqlite3 *db){
621 if( db->magic==SQLITE_MAGIC_OPEN ){
622 db->magic = SQLITE_MAGIC_BUSY;
623 return 0;
624 }else if( db->magic==SQLITE_MAGIC_BUSY ){
625 db->magic = SQLITE_MAGIC_ERROR;
626 db->u1.isInterrupted = 1;
drhfe63d1c2004-09-08 20:13:04 +0000627 }
drha3152892007-05-05 11:48:52 +0000628 return 1;
drhfe63d1c2004-09-08 20:13:04 +0000629}
drha3152892007-05-05 11:48:52 +0000630
631/*
632** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
633** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
634** when this routine is called.
635*/
636int sqlite3SafetyOff(sqlite3 *db){
637 if( db->magic==SQLITE_MAGIC_BUSY ){
638 db->magic = SQLITE_MAGIC_OPEN;
639 return 0;
640 }else {
641 db->magic = SQLITE_MAGIC_ERROR;
642 db->u1.isInterrupted = 1;
643 return 1;
644 }
645}
danielk1977261919c2005-12-06 12:52:59 +0000646
647/*
danielk1977e501b892006-01-09 06:29:47 +0000648** Return a pointer to the ThreadData associated with the calling thread.
danielk1977261919c2005-12-06 12:52:59 +0000649*/
danielk1977e501b892006-01-09 06:29:47 +0000650ThreadData *sqlite3ThreadData(){
danielk197776e8d1a2006-01-18 18:22:43 +0000651 ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
652 if( !p ){
653 sqlite3FailedMalloc();
654 }
655 return p;
drh6f7adc82006-01-11 21:41:20 +0000656}
657
658/*
659** Return a pointer to the ThreadData associated with the calling thread.
660** If no ThreadData has been allocated to this thread yet, return a pointer
661** to a substitute ThreadData structure that is all zeros.
662*/
663const ThreadData *sqlite3ThreadDataReadOnly(){
drhd1167392006-01-23 13:00:35 +0000664 static const ThreadData zeroData = {0}; /* Initializer to silence warnings
665 ** from broken compilers */
drh6f7adc82006-01-11 21:41:20 +0000666 const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
667 return pTd ? pTd : &zeroData;
668}
669
670/*
671** Check to see if the ThreadData for this thread is all zero. If it
672** is, then deallocate it.
673*/
674void sqlite3ReleaseThreadData(){
drh70ff98a2006-01-12 01:25:18 +0000675 sqlite3OsThreadSpecificData(-1);
danielk1977261919c2005-12-06 12:52:59 +0000676}