ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** Utility functions used throughout sqlite. ds@1952: ** ds@1952: ** This file contains functions for allocating memory, comparing ds@1952: ** strings, and stuff like that. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: #include ds@1952: ds@1952: ds@1952: /* ds@1952: ** Set the most recent error code and error string for the sqlite ds@1952: ** handle "db". The error code is set to "err_code". ds@1952: ** ds@1952: ** If it is not NULL, string zFormat specifies the format of the ds@1952: ** error string in the style of the printf functions: The following ds@1952: ** format characters are allowed: ds@1952: ** ds@1952: ** %s Insert a string ds@1952: ** %z A string that should be freed after use ds@1952: ** %d Insert an integer ds@1952: ** %T Insert a token ds@1952: ** %S Insert the first element of a SrcList ds@1952: ** ds@1952: ** zFormat and any string tokens that follow it are assumed to be ds@1952: ** encoded in UTF-8. ds@1952: ** ds@1952: ** To clear the most recent error for sqlite handle "db", sqlite3Error ds@1952: ** should be called with err_code set to SQLITE_OK and zFormat set ds@1952: ** to NULL. ds@1952: */ ds@1952: void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ ds@1952: if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){ ds@1952: db->errCode = err_code; ds@1952: if( zFormat ){ ds@1952: char *z; ds@1952: va_list ap; ds@1952: va_start(ap, zFormat); ds@1952: z = sqlite3VMPrintf(db, zFormat, ap); ds@1952: va_end(ap); ds@1952: sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free); ds@1952: }else{ ds@1952: sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Add an error message to pParse->zErrMsg and increment pParse->nErr. ds@1952: ** The following formatting characters are allowed: ds@1952: ** ds@1952: ** %s Insert a string ds@1952: ** %z A string that should be freed after use ds@1952: ** %d Insert an integer ds@1952: ** %T Insert a token ds@1952: ** %S Insert the first element of a SrcList ds@1952: ** ds@1952: ** This function should be used to report any error that occurs whilst ds@1952: ** compiling an SQL statement (i.e. within sqlite3_prepare()). The ds@1952: ** last thing the sqlite3_prepare() function does is copy the error ds@1952: ** stored by this function into the database handle using sqlite3Error(). ds@1952: ** Function sqlite3Error() should be used during statement execution ds@1952: ** (sqlite3_step() etc.). ds@1952: */ ds@1952: void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ ds@1952: va_list ap; ds@1952: pParse->nErr++; ds@1952: sqlite3_free(pParse->zErrMsg); ds@1952: va_start(ap, zFormat); ds@1952: pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap); ds@1952: va_end(ap); ds@1952: if( pParse->rc==SQLITE_OK ){ ds@1952: pParse->rc = SQLITE_ERROR; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Clear the error message in pParse, if any ds@1952: */ ds@1952: void sqlite3ErrorClear(Parse *pParse){ ds@1952: sqlite3_free(pParse->zErrMsg); ds@1952: pParse->zErrMsg = 0; ds@1952: pParse->nErr = 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert an SQL-style quoted string into a normal string by removing ds@1952: ** the quote characters. The conversion is done in-place. If the ds@1952: ** input does not begin with a quote character, then this routine ds@1952: ** is a no-op. ds@1952: ** ds@1952: ** 2002-Feb-14: This routine is extended to remove MS-Access style ds@1952: ** brackets from around identifers. For example: "[a-b-c]" becomes ds@1952: ** "a-b-c". ds@1952: */ ds@1952: void sqlite3Dequote(char *z){ ds@1952: int quote; ds@1952: int i, j; ds@1952: if( z==0 ) return; ds@1952: quote = z[0]; ds@1952: switch( quote ){ ds@1952: case '\'': break; ds@1952: case '"': break; ds@1952: case '`': break; /* For MySQL compatibility */ ds@1952: case '[': quote = ']'; break; /* For MS SqlServer compatibility */ ds@1952: default: return; ds@1952: } ds@1952: for(i=1, j=0; z[i]; i++){ ds@1952: if( z[i]==quote ){ ds@1952: if( z[i+1]==quote ){ ds@1952: z[j++] = quote; ds@1952: i++; ds@1952: }else{ ds@1952: z[j++] = 0; ds@1952: break; ds@1952: } ds@1952: }else{ ds@1952: z[j++] = z[i]; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* An array to map all upper-case characters into their corresponding ds@1952: ** lower-case character. ds@1952: */ ds@1952: const unsigned char sqlite3UpperToLower[] = { ds@1952: #ifdef SQLITE_ASCII ds@1952: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ds@1952: 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, ds@1952: 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, ds@1952: 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, ds@1952: 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, ds@1952: 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, ds@1952: 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, ds@1952: 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, ds@1952: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, ds@1952: 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, ds@1952: 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, ds@1952: 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, ds@1952: 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, ds@1952: 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, ds@1952: 252,253,254,255 ds@1952: #endif ds@1952: #ifdef SQLITE_EBCDIC ds@1952: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */ ds@1952: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */ ds@1952: 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */ ds@1952: 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */ ds@1952: 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */ ds@1952: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */ ds@1952: 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */ ds@1952: 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */ ds@1952: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */ ds@1952: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */ ds@1952: 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */ ds@1952: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */ ds@1952: 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */ ds@1952: 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */ ds@1952: 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */ ds@1952: 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */ ds@1952: #endif ds@1952: }; ds@1952: #define UpperToLower sqlite3UpperToLower ds@1952: ds@1952: /* ds@1952: ** Some systems have stricmp(). Others have strcasecmp(). Because ds@1952: ** there is no consistency, we will define our own. ds@1952: */ ds@1952: int sqlite3StrICmp(const char *zLeft, const char *zRight){ ds@1952: register unsigned char *a, *b; ds@1952: a = (unsigned char *)zLeft; ds@1952: b = (unsigned char *)zRight; ds@1952: while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } ds@1952: return UpperToLower[*a] - UpperToLower[*b]; ds@1952: } ds@1952: int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){ ds@1952: register unsigned char *a, *b; ds@1952: a = (unsigned char *)zLeft; ds@1952: b = (unsigned char *)zRight; ds@1952: while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } ds@1952: return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return TRUE if z is a pure numeric string. Return FALSE if the ds@1952: ** string contains any character which is not part of a number. If ds@1952: ** the string is numeric and contains the '.' character, set *realnum ds@1952: ** to TRUE (otherwise FALSE). ds@1952: ** ds@1952: ** An empty string is considered non-numeric. ds@1952: */ ds@1952: int sqlite3IsNumber(const char *z, int *realnum, u8 enc){ ds@1952: int incr = (enc==SQLITE_UTF8?1:2); ds@1952: if( enc==SQLITE_UTF16BE ) z++; ds@1952: if( *z=='-' || *z=='+' ) z += incr; ds@1952: if( !isdigit(*(u8*)z) ){ ds@1952: return 0; ds@1952: } ds@1952: z += incr; ds@1952: if( realnum ) *realnum = 0; ds@1952: while( isdigit(*(u8*)z) ){ z += incr; } ds@1952: if( *z=='.' ){ ds@1952: z += incr; ds@1952: if( !isdigit(*(u8*)z) ) return 0; ds@1952: while( isdigit(*(u8*)z) ){ z += incr; } ds@1952: if( realnum ) *realnum = 1; ds@1952: } ds@1952: if( *z=='e' || *z=='E' ){ ds@1952: z += incr; ds@1952: if( *z=='+' || *z=='-' ) z += incr; ds@1952: if( !isdigit(*(u8*)z) ) return 0; ds@1952: while( isdigit(*(u8*)z) ){ z += incr; } ds@1952: if( realnum ) *realnum = 1; ds@1952: } ds@1952: return *z==0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The string z[] is an ascii representation of a real number. ds@1952: ** Convert this string to a double. ds@1952: ** ds@1952: ** This routine assumes that z[] really is a valid number. If it ds@1952: ** is not, the result is undefined. ds@1952: ** ds@1952: ** This routine is used instead of the library atof() function because ds@1952: ** the library atof() might want to use "," as the decimal point instead ds@1952: ** of "." depending on how locale is set. But that would cause problems ds@1952: ** for SQL. So this routine always uses "." regardless of locale. ds@1952: */ ds@1952: int sqlite3AtoF(const char *z, double *pResult){ ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: int sign = 1; ds@1952: const char *zBegin = z; ds@1952: LONGDOUBLE_TYPE v1 = 0.0; ds@1952: while( isspace(*(u8*)z) ) z++; ds@1952: if( *z=='-' ){ ds@1952: sign = -1; ds@1952: z++; ds@1952: }else if( *z=='+' ){ ds@1952: z++; ds@1952: } ds@1952: while( isdigit(*(u8*)z) ){ ds@1952: v1 = v1*10.0 + (*z - '0'); ds@1952: z++; ds@1952: } ds@1952: if( *z=='.' ){ ds@1952: LONGDOUBLE_TYPE divisor = 1.0; ds@1952: z++; ds@1952: while( isdigit(*(u8*)z) ){ ds@1952: v1 = v1*10.0 + (*z - '0'); ds@1952: divisor *= 10.0; ds@1952: z++; ds@1952: } ds@1952: v1 /= divisor; ds@1952: } ds@1952: if( *z=='e' || *z=='E' ){ ds@1952: int esign = 1; ds@1952: int eval = 0; ds@1952: LONGDOUBLE_TYPE scale = 1.0; ds@1952: z++; ds@1952: if( *z=='-' ){ ds@1952: esign = -1; ds@1952: z++; ds@1952: }else if( *z=='+' ){ ds@1952: z++; ds@1952: } ds@1952: while( isdigit(*(u8*)z) ){ ds@1952: eval = eval*10 + *z - '0'; ds@1952: z++; ds@1952: } ds@1952: while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } ds@1952: while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } ds@1952: while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } ds@1952: while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } ds@1952: if( esign<0 ){ ds@1952: v1 /= scale; ds@1952: }else{ ds@1952: v1 *= scale; ds@1952: } ds@1952: } ds@1952: *pResult = sign<0 ? -v1 : v1; ds@1952: return z - zBegin; ds@1952: #else ds@1952: return sqlite3Atoi64(z, pResult); ds@1952: #endif /* SQLITE_OMIT_FLOATING_POINT */ ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compare the 19-character string zNum against the text representation ds@1952: ** value 2^63: 9223372036854775808. Return negative, zero, or positive ds@1952: ** if zNum is less than, equal to, or greater than the string. ds@1952: ** ds@1952: ** Unlike memcmp() this routine is guaranteed to return the difference ds@1952: ** in the values of the last digit if the only difference is in the ds@1952: ** last digit. So, for example, ds@1952: ** ds@1952: ** compare2pow63("9223372036854775800") ds@1952: ** ds@1952: ** will return -8. ds@1952: */ ds@1952: static int compare2pow63(const char *zNum){ ds@1952: int c; ds@1952: c = memcmp(zNum,"922337203685477580",18); ds@1952: if( c==0 ){ ds@1952: c = zNum[18] - '8'; ds@1952: } ds@1952: return c; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Return TRUE if zNum is a 64-bit signed integer and write ds@1952: ** the value of the integer into *pNum. If zNum is not an integer ds@1952: ** or is an integer that is too large to be expressed with 64 bits, ds@1952: ** then return false. ds@1952: ** ds@1952: ** When this routine was originally written it dealt with only ds@1952: ** 32-bit numbers. At that time, it was much faster than the ds@1952: ** atoi() library routine in RedHat 7.2. ds@1952: */ ds@1952: int sqlite3Atoi64(const char *zNum, i64 *pNum){ ds@1952: i64 v = 0; ds@1952: int neg; ds@1952: int i, c; ds@1952: while( isspace(*(u8*)zNum) ) zNum++; ds@1952: if( *zNum=='-' ){ ds@1952: neg = 1; ds@1952: zNum++; ds@1952: }else if( *zNum=='+' ){ ds@1952: neg = 0; ds@1952: zNum++; ds@1952: }else{ ds@1952: neg = 0; ds@1952: } ds@1952: while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */ ds@1952: for(i=0; (c=zNum[i])>='0' && c<='9'; i++){ ds@1952: v = v*10 + c - '0'; ds@1952: } ds@1952: *pNum = neg ? -v : v; ds@1952: if( c!=0 || i==0 || i>19 ){ ds@1952: /* zNum is empty or contains non-numeric text or is longer ds@1952: ** than 19 digits (thus guaranting that it is too large) */ ds@1952: return 0; ds@1952: }else if( i<19 ){ ds@1952: /* Less than 19 digits, so we know that it fits in 64 bits */ ds@1952: return 1; ds@1952: }else{ ds@1952: /* 19-digit numbers must be no larger than 9223372036854775807 if positive ds@1952: ** or 9223372036854775808 if negative. Note that 9223372036854665808 ds@1952: ** is 2^63. */ ds@1952: return compare2pow63(zNum)='0' && c<='9'; i++){} ds@1952: if( i<19 ){ ds@1952: /* Guaranteed to fit if less than 19 digits */ ds@1952: return 1; ds@1952: }else if( i>19 ){ ds@1952: /* Guaranteed to be too big if greater than 19 digits */ ds@1952: return 0; ds@1952: }else{ ds@1952: /* Compare against 2^63. */ ds@1952: return compare2pow63(zNum)=0 && c<=9; i++){ ds@1952: v = v*10 + c; ds@1952: } ds@1952: ds@1952: /* The longest decimal representation of a 32 bit integer is 10 digits: ds@1952: ** ds@1952: ** 1234567890 ds@1952: ** 2^31 -> 2147483648 ds@1952: */ ds@1952: if( i>10 ){ ds@1952: return 0; ds@1952: } ds@1952: if( v-neg>2147483647 ){ ds@1952: return 0; ds@1952: } ds@1952: if( neg ){ ds@1952: v = -v; ds@1952: } ds@1952: *pValue = (int)v; ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Check to make sure we have a valid db pointer. This test is not ds@1952: ** foolproof but it does provide some measure of protection against ds@1952: ** misuse of the interface such as passing in db pointers that are ds@1952: ** NULL or which have been previously closed. If this routine returns ds@1952: ** TRUE it means that the db pointer is invalid and should not be ds@1952: ** dereferenced for any reason. The calling function should invoke ds@1952: ** SQLITE_MISUSE immediately. ds@1952: */ ds@1952: int sqlite3SafetyCheck(sqlite3 *db){ ds@1952: int magic; ds@1952: if( db==0 ) return 1; ds@1952: magic = db->magic; ds@1952: if( magic!=SQLITE_MAGIC_CLOSED && ds@1952: magic!=SQLITE_MAGIC_OPEN && ds@1952: magic!=SQLITE_MAGIC_BUSY ) return 1; ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The variable-length integer encoding is as follows: ds@1952: ** ds@1952: ** KEY: ds@1952: ** A = 0xxxxxxx 7 bits of data and one flag bit ds@1952: ** B = 1xxxxxxx 7 bits of data and one flag bit ds@1952: ** C = xxxxxxxx 8 bits of data ds@1952: ** ds@1952: ** 7 bits - A ds@1952: ** 14 bits - BA ds@1952: ** 21 bits - BBA ds@1952: ** 28 bits - BBBA ds@1952: ** 35 bits - BBBBA ds@1952: ** 42 bits - BBBBBA ds@1952: ** 49 bits - BBBBBBA ds@1952: ** 56 bits - BBBBBBBA ds@1952: ** 64 bits - BBBBBBBBC ds@1952: */ ds@1952: ds@1952: /* ds@1952: ** Write a 64-bit variable-length integer to memory starting at p[0]. ds@1952: ** The length of data write will be between 1 and 9 bytes. The number ds@1952: ** of bytes written is returned. ds@1952: ** ds@1952: ** A variable-length integer consists of the lower 7 bits of each byte ds@1952: ** for all bytes that have the 8th bit set and one byte with the 8th ds@1952: ** bit clear. Except, if we get to the 9th byte, it stores the full ds@1952: ** 8 bits and is the last byte. ds@1952: */ ds@1952: int sqlite3PutVarint(unsigned char *p, u64 v){ ds@1952: int i, j, n; ds@1952: u8 buf[10]; ds@1952: if( v & (((u64)0xff000000)<<32) ){ ds@1952: p[8] = v; ds@1952: v >>= 8; ds@1952: for(i=7; i>=0; i--){ ds@1952: p[i] = (v & 0x7f) | 0x80; ds@1952: v >>= 7; ds@1952: } ds@1952: return 9; ds@1952: } ds@1952: n = 0; ds@1952: do{ ds@1952: buf[n++] = (v & 0x7f) | 0x80; ds@1952: v >>= 7; ds@1952: }while( v!=0 ); ds@1952: buf[0] &= 0x7f; ds@1952: assert( n<=9 ); ds@1952: for(i=0, j=n-1; j>=0; j--, i++){ ds@1952: p[i] = buf[j]; ds@1952: } ds@1952: return n; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Read a 64-bit variable-length integer from memory starting at p[0]. ds@1952: ** Return the number of bytes read. The value is stored in *v. ds@1952: */ ds@1952: int sqlite3GetVarint(const unsigned char *p, u64 *v){ ds@1952: u32 x; ds@1952: u64 x64; ds@1952: int n; ds@1952: unsigned char c; ds@1952: if( ((c = p[0]) & 0x80)==0 ){ ds@1952: *v = c; ds@1952: return 1; ds@1952: } ds@1952: x = c & 0x7f; ds@1952: if( ((c = p[1]) & 0x80)==0 ){ ds@1952: *v = (x<<7) | c; ds@1952: return 2; ds@1952: } ds@1952: x = (x<<7) | (c&0x7f); ds@1952: if( ((c = p[2]) & 0x80)==0 ){ ds@1952: *v = (x<<7) | c; ds@1952: return 3; ds@1952: } ds@1952: x = (x<<7) | (c&0x7f); ds@1952: if( ((c = p[3]) & 0x80)==0 ){ ds@1952: *v = (x<<7) | c; ds@1952: return 4; ds@1952: } ds@1952: x64 = (x<<7) | (c&0x7f); ds@1952: n = 4; ds@1952: do{ ds@1952: c = p[n++]; ds@1952: if( n==9 ){ ds@1952: x64 = (x64<<8) | c; ds@1952: break; ds@1952: } ds@1952: x64 = (x64<<7) | (c&0x7f); ds@1952: }while( (c & 0x80)!=0 ); ds@1952: *v = x64; ds@1952: return n; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Read a 32-bit variable-length integer from memory starting at p[0]. ds@1952: ** Return the number of bytes read. The value is stored in *v. ds@1952: */ ds@1952: int sqlite3GetVarint32(const unsigned char *p, u32 *v){ ds@1952: u32 x; ds@1952: int n; ds@1952: unsigned char c; ds@1952: if( ((signed char*)p)[0]>=0 ){ ds@1952: *v = p[0]; ds@1952: return 1; ds@1952: } ds@1952: x = p[0] & 0x7f; ds@1952: if( ((signed char*)p)[1]>=0 ){ ds@1952: *v = (x<<7) | p[1]; ds@1952: return 2; ds@1952: } ds@1952: x = (x<<7) | (p[1] & 0x7f); ds@1952: n = 2; ds@1952: do{ ds@1952: x = (x<<7) | ((c = p[n++])&0x7f); ds@1952: }while( (c & 0x80)!=0 && n<9 ); ds@1952: *v = x; ds@1952: return n; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the number of bytes that will be needed to store the given ds@1952: ** 64-bit integer. ds@1952: */ ds@1952: int sqlite3VarintLen(u64 v){ ds@1952: int i = 0; ds@1952: do{ ds@1952: i++; ds@1952: v >>= 7; ds@1952: }while( v!=0 && i<9 ); ds@1952: return i; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Read or write a four-byte big-endian integer value. ds@1952: */ ds@1952: u32 sqlite3Get4byte(const u8 *p){ ds@1952: return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; ds@1952: } ds@1952: void sqlite3Put4byte(unsigned char *p, u32 v){ ds@1952: p[0] = v>>24; ds@1952: p[1] = v>>16; ds@1952: p[2] = v>>8; ds@1952: p[3] = v; ds@1952: } ds@1952: ds@1952: ds@1952: ds@1952: #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \ ds@1952: || defined(SQLITE_TEST) ds@1952: /* ds@1952: ** Translate a single byte of Hex into an integer. ds@1952: */ ds@1952: static int hexToInt(int h){ ds@1952: if( h>='0' && h<='9' ){ ds@1952: return h - '0'; ds@1952: }else if( h>='a' && h<='f' ){ ds@1952: return h - 'a' + 10; ds@1952: }else{ ds@1952: assert( h>='A' && h<='F' ); ds@1952: return h - 'A' + 10; ds@1952: } ds@1952: } ds@1952: #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */ ds@1952: ds@1952: #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) ds@1952: /* ds@1952: ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary ds@1952: ** value. Return a pointer to its binary value. Space to hold the ds@1952: ** binary value has been obtained from malloc and must be freed by ds@1952: ** the calling routine. ds@1952: */ ds@1952: void *sqlite3HexToBlob(sqlite3 *db, const char *z){ ds@1952: char *zBlob; ds@1952: int i; ds@1952: int n = strlen(z); ds@1952: if( n%2 ) return 0; ds@1952: ds@1952: zBlob = (char *)sqlite3DbMallocRaw(db, n/2); ds@1952: if( zBlob ){ ds@1952: for(i=0; imagic is not a valid open value, take care not ds@1952: ** to modify the db structure at all. It could be that db is a stale ds@1952: ** pointer. In other words, it could be that there has been a prior ds@1952: ** call to sqlite3_close(db) and db has been deallocated. And we do ds@1952: ** not want to write into deallocated memory. ds@1952: */ ds@1952: int sqlite3SafetyOn(sqlite3 *db){ ds@1952: if( db->magic==SQLITE_MAGIC_OPEN ){ ds@1952: db->magic = SQLITE_MAGIC_BUSY; ds@1952: return 0; ds@1952: }else if( db->magic==SQLITE_MAGIC_BUSY ){ ds@1952: db->magic = SQLITE_MAGIC_ERROR; ds@1952: db->u1.isInterrupted = 1; ds@1952: } ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. ds@1952: ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY ds@1952: ** when this routine is called. ds@1952: */ ds@1952: int sqlite3SafetyOff(sqlite3 *db){ ds@1952: if( db->magic==SQLITE_MAGIC_BUSY ){ ds@1952: db->magic = SQLITE_MAGIC_OPEN; ds@1952: return 0; ds@1952: }else { ds@1952: db->magic = SQLITE_MAGIC_ERROR; ds@1952: db->u1.isInterrupted = 1; ds@1952: return 1; ds@1952: } ds@1952: }