~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/sqlite/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
** $Id: util.c,v 1.132 2005/03/18 14:03:15 drh Exp $
 
18
*/
 
19
#include "sqliteInt.h"
 
20
#include <stdarg.h>
 
21
#include <ctype.h>
 
22
 
 
23
#if SQLITE_MEMDEBUG>2 && defined(__GLIBC__)
 
24
#include <execinfo.h>
 
25
void print_stack_trace(){
 
26
  void *bt[30];
 
27
  int i;
 
28
  int n = backtrace(bt, 30);
 
29
 
 
30
  fprintf(stderr, "STACK: ");
 
31
  for(i=0; i<n;i++){
 
32
    fprintf(stderr, "%p ", bt[i]);
 
33
  }
 
34
  fprintf(stderr, "\n");
 
35
}
 
36
#else
 
37
#define print_stack_trace()
 
38
#endif
 
39
 
 
40
/*
 
41
** If malloc() ever fails, this global variable gets set to 1.
 
42
** This causes the library to abort and never again function.
 
43
*/
 
44
int sqlite3_malloc_failed = 0;
 
45
 
 
46
/*
 
47
** If SQLITE_MEMDEBUG is defined, then use versions of malloc() and
 
48
** free() that track memory usage and check for buffer overruns.
 
49
*/
 
50
#ifdef SQLITE_MEMDEBUG
 
51
 
 
52
/*
 
53
** For keeping track of the number of mallocs and frees.   This
 
54
** is used to check for memory leaks.  The iMallocFail and iMallocReset
 
55
** values are used to simulate malloc() failures during testing in 
 
56
** order to verify that the library correctly handles an out-of-memory
 
57
** condition.
 
58
*/
 
59
int sqlite3_nMalloc;         /* Number of sqliteMalloc() calls */
 
60
int sqlite3_nFree;           /* Number of sqliteFree() calls */
 
61
int sqlite3_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
 
62
int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
 
63
#if SQLITE_MEMDEBUG>1
 
64
static int memcnt = 0;
 
65
#endif
 
66
 
 
67
/*
 
68
** Number of 32-bit guard words
 
69
*/
 
70
#define N_GUARD 1
 
71
 
 
72
/*
 
73
** Allocate new memory and set it to zero.  Return NULL if
 
74
** no memory is available.
 
75
*/
 
76
void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){
 
77
  void *p;
 
78
  int *pi;
 
79
  int i, k;
 
80
  if( sqlite3_iMallocFail>=0 ){
 
81
    sqlite3_iMallocFail--;
 
82
    if( sqlite3_iMallocFail==0 ){
 
83
      sqlite3_malloc_failed++;
 
84
#if SQLITE_MEMDEBUG>1
 
85
      fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n",
 
86
              n, zFile,line);
 
87
#endif
 
88
      sqlite3_iMallocFail = sqlite3_iMallocReset;
 
89
      return 0;
 
90
    }
 
91
  }
 
92
  if( n==0 ) return 0;
 
93
  k = (n+sizeof(int)-1)/sizeof(int);
 
94
  pi = malloc( (N_GUARD*2+1+k)*sizeof(int));
 
95
  if( pi==0 ){
 
96
    if( n>0 ) sqlite3_malloc_failed++;
 
97
    return 0;
 
98
  }
 
99
  sqlite3_nMalloc++;
 
100
  for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
 
101
  pi[N_GUARD] = n;
 
102
  for(i=0; i<N_GUARD; i++) pi[k+1+N_GUARD+i] = 0xdead3344;
 
103
  p = &pi[N_GUARD+1];
 
104
  memset(p, bZero==0, n);
 
105
#if SQLITE_MEMDEBUG>1
 
106
  print_stack_trace();
 
107
  fprintf(stderr,"%06d malloc %d bytes at 0x%x from %s:%d\n",
 
108
      ++memcnt, n, (int)p, zFile,line);
 
109
#endif
 
110
  return p;
 
111
}
 
112
 
 
113
/*
 
114
** Check to see if the given pointer was obtained from sqliteMalloc()
 
115
** and is able to hold at least N bytes.  Raise an exception if this
 
116
** is not the case.
 
117
**
 
118
** This routine is used for testing purposes only.
 
119
*/
 
120
void sqlite3CheckMemory(void *p, int N){
 
121
  int *pi = p;
 
122
  int n, i, k;
 
123
  pi -= N_GUARD+1;
 
124
  for(i=0; i<N_GUARD; i++){
 
125
    assert( pi[i]==0xdead1122 );
 
126
  }
 
127
  n = pi[N_GUARD];
 
128
  assert( N>=0 && N<n );
 
129
  k = (n+sizeof(int)-1)/sizeof(int);
 
130
  for(i=0; i<N_GUARD; i++){
 
131
    assert( pi[k+N_GUARD+1+i]==0xdead3344 );
 
132
  }
 
133
}
 
134
 
 
135
/*
 
136
** Free memory previously obtained from sqliteMalloc()
 
137
*/
 
138
void sqlite3Free_(void *p, char *zFile, int line){
 
139
  if( p ){
 
140
    int *pi, i, k, n;
 
141
    pi = p;
 
142
    pi -= N_GUARD+1;
 
143
    sqlite3_nFree++;
 
144
    for(i=0; i<N_GUARD; i++){
 
145
      if( pi[i]!=0xdead1122 ){
 
146
        fprintf(stderr,"Low-end memory corruption at 0x%x\n", (int)p);
 
147
        return;
 
148
      }
 
149
    }
 
150
    n = pi[N_GUARD];
 
151
    k = (n+sizeof(int)-1)/sizeof(int);
 
152
    for(i=0; i<N_GUARD; i++){
 
153
      if( pi[k+N_GUARD+1+i]!=0xdead3344 ){
 
154
        fprintf(stderr,"High-end memory corruption at 0x%x\n", (int)p);
 
155
        return;
 
156
      }
 
157
    }
 
158
    memset(pi, 0xff, (k+N_GUARD*2+1)*sizeof(int));
 
159
#if SQLITE_MEMDEBUG>1
 
160
    fprintf(stderr,"%06d free %d bytes at 0x%x from %s:%d\n",
 
161
         ++memcnt, n, (int)p, zFile,line);
 
162
#endif
 
163
    free(pi);
 
164
  }
 
165
}
 
166
 
 
167
/*
 
168
** Resize a prior allocation.  If p==0, then this routine
 
169
** works just like sqliteMalloc().  If n==0, then this routine
 
170
** works just like sqliteFree().
 
171
*/
 
172
void *sqlite3Realloc_(void *oldP, int n, char *zFile, int line){
 
173
  int *oldPi, *pi, i, k, oldN, oldK;
 
174
  void *p;
 
175
  if( oldP==0 ){
 
176
    return sqlite3Malloc_(n,1,zFile,line);
 
177
  }
 
178
  if( n==0 ){
 
179
    sqlite3Free_(oldP,zFile,line);
 
180
    return 0;
 
181
  }
 
182
  oldPi = oldP;
 
183
  oldPi -= N_GUARD+1;
 
184
  if( oldPi[0]!=0xdead1122 ){
 
185
    fprintf(stderr,"Low-end memory corruption in realloc at 0x%x\n", (int)oldP);
 
186
    return 0;
 
187
  }
 
188
  oldN = oldPi[N_GUARD];
 
189
  oldK = (oldN+sizeof(int)-1)/sizeof(int);
 
190
  for(i=0; i<N_GUARD; i++){
 
191
    if( oldPi[oldK+N_GUARD+1+i]!=0xdead3344 ){
 
192
      fprintf(stderr,"High-end memory corruption in realloc at 0x%x\n",
 
193
              (int)oldP);
 
194
      return 0;
 
195
    }
 
196
  }
 
197
  k = (n + sizeof(int) - 1)/sizeof(int);
 
198
  pi = malloc( (k+N_GUARD*2+1)*sizeof(int) );
 
199
  if( pi==0 ){
 
200
    if( n>0 ) sqlite3_malloc_failed++;
 
201
    return 0;
 
202
  }
 
203
  for(i=0; i<N_GUARD; i++) pi[i] = 0xdead1122;
 
204
  pi[N_GUARD] = n;
 
205
  for(i=0; i<N_GUARD; i++) pi[k+N_GUARD+1+i] = 0xdead3344;
 
206
  p = &pi[N_GUARD+1];
 
207
  memcpy(p, oldP, n>oldN ? oldN : n);
 
208
  if( n>oldN ){
 
209
    memset(&((char*)p)[oldN], 0x55, n-oldN);
 
210
  }
 
211
  memset(oldPi, 0xab, (oldK+N_GUARD+2)*sizeof(int));
 
212
  free(oldPi);
 
213
#if SQLITE_MEMDEBUG>1
 
214
  print_stack_trace();
 
215
  fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
 
216
    ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
 
217
#endif
 
218
  return p;
 
219
}
 
220
 
 
221
/*
 
222
** Make a copy of a string in memory obtained from sqliteMalloc()
 
223
*/
 
224
char *sqlite3StrDup_(const char *z, char *zFile, int line){
 
225
  char *zNew;
 
226
  if( z==0 ) return 0;
 
227
  zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
 
228
  if( zNew ) strcpy(zNew, z);
 
229
  return zNew;
 
230
}
 
231
char *sqlite3StrNDup_(const char *z, int n, char *zFile, int line){
 
232
  char *zNew;
 
233
  if( z==0 ) return 0;
 
234
  zNew = sqlite3Malloc_(n+1, 0, zFile, line);
 
235
  if( zNew ){
 
236
    memcpy(zNew, z, n);
 
237
    zNew[n] = 0;
 
238
  }
 
239
  return zNew;
 
240
}
 
241
 
 
242
/*
 
243
** A version of sqliteFree that is always a function, not a macro.
 
244
*/
 
245
void sqlite3FreeX(void *p){
 
246
  sqliteFree(p);
 
247
}
 
248
#endif /* SQLITE_MEMDEBUG */
 
249
 
 
250
/*
 
251
** The following versions of malloc() and free() are for use in a
 
252
** normal build.
 
253
*/
 
254
#if !defined(SQLITE_MEMDEBUG)
 
255
 
 
256
/*
 
257
** Allocate new memory and set it to zero.  Return NULL if
 
258
** no memory is available.  See also sqliteMallocRaw().
 
259
*/
 
260
void *sqlite3Malloc(int n){
 
261
  void *p;
 
262
  if( (p = malloc(n))==0 ){
 
263
    if( n>0 ) sqlite3_malloc_failed++;
 
264
  }else{
 
265
    memset(p, 0, n);
 
266
  }
 
267
  return p;
 
268
}
 
269
 
 
270
/*
 
271
** Allocate new memory but do not set it to zero.  Return NULL if
 
272
** no memory is available.  See also sqliteMalloc().
 
273
*/
 
274
void *sqlite3MallocRaw(int n){
 
275
  void *p;
 
276
  if( (p = malloc(n))==0 ){
 
277
    if( n>0 ) sqlite3_malloc_failed++;
 
278
  }
 
279
  return p;
 
280
}
 
281
 
 
282
/*
 
283
** Free memory previously obtained from sqliteMalloc()
 
284
*/
 
285
void sqlite3FreeX(void *p){
 
286
  if( p ){
 
287
    free(p);
 
288
  }
 
289
}
 
290
 
 
291
/*
 
292
** Resize a prior allocation.  If p==0, then this routine
 
293
** works just like sqliteMalloc().  If n==0, then this routine
 
294
** works just like sqliteFree().
 
295
*/
 
296
void *sqlite3Realloc(void *p, int n){
 
297
  void *p2;
 
298
  if( p==0 ){
 
299
    return sqliteMalloc(n);
 
300
  }
 
301
  if( n==0 ){
 
302
    sqliteFree(p);
 
303
    return 0;
 
304
  }
 
305
  p2 = realloc(p, n);
 
306
  if( p2==0 ){
 
307
    if( n>0 ) sqlite3_malloc_failed++;
 
308
  }
 
309
  return p2;
 
310
}
 
311
 
 
312
/*
 
313
** Make a copy of a string in memory obtained from sqliteMalloc()
 
314
*/
 
315
char *sqlite3StrDup(const char *z){
 
316
  char *zNew;
 
317
  if( z==0 ) return 0;
 
318
  zNew = sqliteMallocRaw(strlen(z)+1);
 
319
  if( zNew ) strcpy(zNew, z);
 
320
  return zNew;
 
321
}
 
322
char *sqlite3StrNDup(const char *z, int n){
 
323
  char *zNew;
 
324
  if( z==0 ) return 0;
 
325
  zNew = sqliteMallocRaw(n+1);
 
326
  if( zNew ){
 
327
    memcpy(zNew, z, n);
 
328
    zNew[n] = 0;
 
329
  }
 
330
  return zNew;
 
331
}
 
332
#endif /* !defined(SQLITE_MEMDEBUG) */
 
333
 
 
334
/*
 
335
** Create a string from the 2nd and subsequent arguments (up to the
 
336
** first NULL argument), store the string in memory obtained from
 
337
** sqliteMalloc() and make the pointer indicated by the 1st argument
 
338
** point to that string.  The 1st argument must either be NULL or 
 
339
** point to memory obtained from sqliteMalloc().
 
340
*/
 
341
void sqlite3SetString(char **pz, const char *zFirst, ...){
 
342
  va_list ap;
 
343
  int nByte;
 
344
  const char *z;
 
345
  char *zResult;
 
346
 
 
347
  if( pz==0 ) return;
 
348
  nByte = strlen(zFirst) + 1;
 
349
  va_start(ap, zFirst);
 
350
  while( (z = va_arg(ap, const char*))!=0 ){
 
351
    nByte += strlen(z);
 
352
  }
 
353
  va_end(ap);
 
354
  sqliteFree(*pz);
 
355
  *pz = zResult = sqliteMallocRaw( nByte );
 
356
  if( zResult==0 ){
 
357
    return;
 
358
  }
 
359
  strcpy(zResult, zFirst);
 
360
  zResult += strlen(zResult);
 
361
  va_start(ap, zFirst);
 
362
  while( (z = va_arg(ap, const char*))!=0 ){
 
363
    strcpy(zResult, z);
 
364
    zResult += strlen(zResult);
 
365
  }
 
366
  va_end(ap);
 
367
#ifdef SQLITE_DEBUG
 
368
#if SQLITE_DEBUG>1
 
369
  fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
 
370
#endif
 
371
#endif
 
372
}
 
373
 
 
374
/*
 
375
** Set the most recent error code and error string for the sqlite
 
376
** handle "db". The error code is set to "err_code".
 
377
**
 
378
** If it is not NULL, string zFormat specifies the format of the
 
379
** error string in the style of the printf functions: The following
 
380
** format characters are allowed:
 
381
**
 
382
**      %s      Insert a string
 
383
**      %z      A string that should be freed after use
 
384
**      %d      Insert an integer
 
385
**      %T      Insert a token
 
386
**      %S      Insert the first element of a SrcList
 
387
**
 
388
** zFormat and any string tokens that follow it are assumed to be
 
389
** encoded in UTF-8.
 
390
**
 
391
** To clear the most recent error for slqite handle "db", sqlite3Error
 
392
** should be called with err_code set to SQLITE_OK and zFormat set
 
393
** to NULL.
 
394
*/
 
395
void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
 
396
  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())) ){
 
397
    db->errCode = err_code;
 
398
    if( zFormat ){
 
399
      char *z;
 
400
      va_list ap;
 
401
      va_start(ap, zFormat);
 
402
      z = sqlite3VMPrintf(zFormat, ap);
 
403
      va_end(ap);
 
404
      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
 
405
    }else{
 
406
      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
 
407
    }
 
408
  }
 
409
}
 
410
 
 
411
/*
 
412
** Add an error message to pParse->zErrMsg and increment pParse->nErr.
 
413
** The following formatting characters are allowed:
 
414
**
 
415
**      %s      Insert a string
 
416
**      %z      A string that should be freed after use
 
417
**      %d      Insert an integer
 
418
**      %T      Insert a token
 
419
**      %S      Insert the first element of a SrcList
 
420
**
 
421
** This function should be used to report any error that occurs whilst
 
422
** compiling an SQL statement (i.e. within sqlite3_prepare()). The
 
423
** last thing the sqlite3_prepare() function does is copy the error
 
424
** stored by this function into the database handle using sqlite3Error().
 
425
** Function sqlite3Error() should be used during statement execution
 
426
** (sqlite3_step() etc.).
 
427
*/
 
428
void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
 
429
  va_list ap;
 
430
  pParse->nErr++;
 
431
  sqliteFree(pParse->zErrMsg);
 
432
  va_start(ap, zFormat);
 
433
  pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
 
434
  va_end(ap);
 
435
}
 
436
 
 
437
/*
 
438
** Convert an SQL-style quoted string into a normal string by removing
 
439
** the quote characters.  The conversion is done in-place.  If the
 
440
** input does not begin with a quote character, then this routine
 
441
** is a no-op.
 
442
**
 
443
** 2002-Feb-14: This routine is extended to remove MS-Access style
 
444
** brackets from around identifers.  For example:  "[a-b-c]" becomes
 
445
** "a-b-c".
 
446
*/
 
447
void sqlite3Dequote(char *z){
 
448
  int quote;
 
449
  int i, j;
 
450
  if( z==0 ) return;
 
451
  quote = z[0];
 
452
  switch( quote ){
 
453
    case '\'':  break;
 
454
    case '"':   break;
 
455
    case '[':   quote = ']';  break;
 
456
    default:    return;
 
457
  }
 
458
  for(i=1, j=0; z[i]; i++){
 
459
    if( z[i]==quote ){
 
460
      if( z[i+1]==quote ){
 
461
        z[j++] = quote;
 
462
        i++;
 
463
      }else{
 
464
        z[j++] = 0;
 
465
        break;
 
466
      }
 
467
    }else{
 
468
      z[j++] = z[i];
 
469
    }
 
470
  }
 
471
}
 
472
 
 
473
/* An array to map all upper-case characters into their corresponding
 
474
** lower-case character. 
 
475
*/
 
476
const unsigned char sqlite3UpperToLower[] = {
 
477
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
 
478
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 
479
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 
480
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
 
481
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
 
482
    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
 
483
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
 
484
    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
485
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
 
486
    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
 
487
    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
 
488
    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
 
489
    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
 
490
    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
 
491
    252,253,254,255
 
492
};
 
493
#define UpperToLower sqlite3UpperToLower
 
494
 
 
495
/*
 
496
** Some systems have stricmp().  Others have strcasecmp().  Because
 
497
** there is no consistency, we will define our own.
 
498
*/
 
499
int sqlite3StrICmp(const char *zLeft, const char *zRight){
 
500
  register unsigned char *a, *b;
 
501
  a = (unsigned char *)zLeft;
 
502
  b = (unsigned char *)zRight;
 
503
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 
504
  return UpperToLower[*a] - UpperToLower[*b];
 
505
}
 
506
int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
 
507
  register unsigned char *a, *b;
 
508
  a = (unsigned char *)zLeft;
 
509
  b = (unsigned char *)zRight;
 
510
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 
511
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
 
512
}
 
513
 
 
514
/*
 
515
** Return TRUE if z is a pure numeric string.  Return FALSE if the
 
516
** string contains any character which is not part of a number. If
 
517
** the string is numeric and contains the '.' character, set *realnum
 
518
** to TRUE (otherwise FALSE).
 
519
**
 
520
** An empty string is considered non-numeric.
 
521
*/
 
522
int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
 
523
  int incr = (enc==SQLITE_UTF8?1:2);
 
524
  if( enc==SQLITE_UTF16BE ) z++;
 
525
  if( *z=='-' || *z=='+' ) z += incr;
 
526
  if( !isdigit(*(u8*)z) ){
 
527
    return 0;
 
528
  }
 
529
  z += incr;
 
530
  if( realnum ) *realnum = 0;
 
531
  while( isdigit(*(u8*)z) ){ z += incr; }
 
532
  if( *z=='.' ){
 
533
    z += incr;
 
534
    if( !isdigit(*(u8*)z) ) return 0;
 
535
    while( isdigit(*(u8*)z) ){ z += incr; }
 
536
    if( realnum ) *realnum = 1;
 
537
  }
 
538
  if( *z=='e' || *z=='E' ){
 
539
    z += incr;
 
540
    if( *z=='+' || *z=='-' ) z += incr;
 
541
    if( !isdigit(*(u8*)z) ) return 0;
 
542
    while( isdigit(*(u8*)z) ){ z += incr; }
 
543
    if( realnum ) *realnum = 1;
 
544
  }
 
545
  return *z==0;
 
546
}
 
547
 
 
548
/*
 
549
** The string z[] is an ascii representation of a real number.
 
550
** Convert this string to a double.
 
551
**
 
552
** This routine assumes that z[] really is a valid number.  If it
 
553
** is not, the result is undefined.
 
554
**
 
555
** This routine is used instead of the library atof() function because
 
556
** the library atof() might want to use "," as the decimal point instead
 
557
** of "." depending on how locale is set.  But that would cause problems
 
558
** for SQL.  So this routine always uses "." regardless of locale.
 
559
*/
 
560
double sqlite3AtoF(const char *z, const char **pzEnd){
 
561
  int sign = 1;
 
562
  LONGDOUBLE_TYPE v1 = 0.0;
 
563
  if( *z=='-' ){
 
564
    sign = -1;
 
565
    z++;
 
566
  }else if( *z=='+' ){
 
567
    z++;
 
568
  }
 
569
  while( isdigit(*(u8*)z) ){
 
570
    v1 = v1*10.0 + (*z - '0');
 
571
    z++;
 
572
  }
 
573
  if( *z=='.' ){
 
574
    LONGDOUBLE_TYPE divisor = 1.0;
 
575
    z++;
 
576
    while( isdigit(*(u8*)z) ){
 
577
      v1 = v1*10.0 + (*z - '0');
 
578
      divisor *= 10.0;
 
579
      z++;
 
580
    }
 
581
    v1 /= divisor;
 
582
  }
 
583
  if( *z=='e' || *z=='E' ){
 
584
    int esign = 1;
 
585
    int eval = 0;
 
586
    LONGDOUBLE_TYPE scale = 1.0;
 
587
    z++;
 
588
    if( *z=='-' ){
 
589
      esign = -1;
 
590
      z++;
 
591
    }else if( *z=='+' ){
 
592
      z++;
 
593
    }
 
594
    while( isdigit(*(u8*)z) ){
 
595
      eval = eval*10 + *z - '0';
 
596
      z++;
 
597
    }
 
598
    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
 
599
    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
 
600
    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
 
601
    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
 
602
    if( esign<0 ){
 
603
      v1 /= scale;
 
604
    }else{
 
605
      v1 *= scale;
 
606
    }
 
607
  }
 
608
  if( pzEnd ) *pzEnd = z;
 
609
  return sign<0 ? -v1 : v1;
 
610
}
 
611
 
 
612
/*
 
613
** Return TRUE if zNum is a 64-bit signed integer and write
 
614
** the value of the integer into *pNum.  If zNum is not an integer
 
615
** or is an integer that is too large to be expressed with 64 bits,
 
616
** then return false.  If n>0 and the integer is string is not
 
617
** exactly n bytes long, return false.
 
618
**
 
619
** When this routine was originally written it dealt with only
 
620
** 32-bit numbers.  At that time, it was much faster than the
 
621
** atoi() library routine in RedHat 7.2.
 
622
*/
 
623
int sqlite3atoi64(const char *zNum, i64 *pNum){
 
624
  i64 v = 0;
 
625
  int neg;
 
626
  int i, c;
 
627
  if( *zNum=='-' ){
 
628
    neg = 1;
 
629
    zNum++;
 
630
  }else if( *zNum=='+' ){
 
631
    neg = 0;
 
632
    zNum++;
 
633
  }else{
 
634
    neg = 0;
 
635
  }
 
636
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
 
637
    v = v*10 + c - '0';
 
638
  }
 
639
  *pNum = neg ? -v : v;
 
640
  return c==0 && i>0 && 
 
641
      (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
 
642
}
 
643
 
 
644
/*
 
645
** The string zNum represents an integer.  There might be some other
 
646
** information following the integer too, but that part is ignored.
 
647
** If the integer that the prefix of zNum represents will fit in a
 
648
** 32-bit signed integer, return TRUE.  Otherwise return FALSE.
 
649
**
 
650
** This routine returns FALSE for the string -2147483648 even that
 
651
** that number will in fact fit in a 32-bit integer.  But positive
 
652
** 2147483648 will not fit in 32 bits.  So it seems safer to return
 
653
** false.
 
654
*/
 
655
static int sqlite3FitsIn32Bits(const char *zNum){
 
656
  int i, c;
 
657
  if( *zNum=='-' || *zNum=='+' ) zNum++;
 
658
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
 
659
  return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
 
660
}
 
661
 
 
662
/*
 
663
** If zNum represents an integer that will fit in 32-bits, then set
 
664
** *pValue to that integer and return true.  Otherwise return false.
 
665
*/
 
666
int sqlite3GetInt32(const char *zNum, int *pValue){
 
667
  if( sqlite3FitsIn32Bits(zNum) ){
 
668
    *pValue = atoi(zNum);
 
669
    return 1;
 
670
  }
 
671
  return 0;
 
672
}
 
673
 
 
674
/*
 
675
** The string zNum represents an integer.  There might be some other
 
676
** information following the integer too, but that part is ignored.
 
677
** If the integer that the prefix of zNum represents will fit in a
 
678
** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
 
679
**
 
680
** This routine returns FALSE for the string -9223372036854775808 even that
 
681
** that number will, in theory fit in a 64-bit integer.  Positive
 
682
** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
 
683
** false.
 
684
*/
 
685
int sqlite3FitsIn64Bits(const char *zNum){
 
686
  int i, c;
 
687
  if( *zNum=='-' || *zNum=='+' ) zNum++;
 
688
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
 
689
  return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
 
690
}
 
691
 
 
692
 
 
693
/*
 
694
** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
 
695
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
 
696
** when this routine is called.
 
697
**
 
698
** This routine is a attempt to detect if two threads use the
 
699
** same sqlite* pointer at the same time.  There is a race 
 
700
** condition so it is possible that the error is not detected.
 
701
** But usually the problem will be seen.  The result will be an
 
702
** error which can be used to debug the application that is
 
703
** using SQLite incorrectly.
 
704
**
 
705
** Ticket #202:  If db->magic is not a valid open value, take care not
 
706
** to modify the db structure at all.  It could be that db is a stale
 
707
** pointer.  In other words, it could be that there has been a prior
 
708
** call to sqlite3_close(db) and db has been deallocated.  And we do
 
709
** not want to write into deallocated memory.
 
710
*/
 
711
int sqlite3SafetyOn(sqlite3 *db){
 
712
  if( db->magic==SQLITE_MAGIC_OPEN ){
 
713
    db->magic = SQLITE_MAGIC_BUSY;
 
714
    return 0;
 
715
  }else if( db->magic==SQLITE_MAGIC_BUSY || db->magic==SQLITE_MAGIC_ERROR ){
 
716
    db->magic = SQLITE_MAGIC_ERROR;
 
717
    db->flags |= SQLITE_Interrupt;
 
718
  }
 
719
  return 1;
 
720
}
 
721
 
 
722
/*
 
723
** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
 
724
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
 
725
** when this routine is called.
 
726
*/
 
727
int sqlite3SafetyOff(sqlite3 *db){
 
728
  if( db->magic==SQLITE_MAGIC_BUSY ){
 
729
    db->magic = SQLITE_MAGIC_OPEN;
 
730
    return 0;
 
731
  }else if( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ERROR ){
 
732
    db->magic = SQLITE_MAGIC_ERROR;
 
733
    db->flags |= SQLITE_Interrupt;
 
734
  }
 
735
  return 1;
 
736
}
 
737
 
 
738
/*
 
739
** Check to make sure we have a valid db pointer.  This test is not
 
740
** foolproof but it does provide some measure of protection against
 
741
** misuse of the interface such as passing in db pointers that are
 
742
** NULL or which have been previously closed.  If this routine returns
 
743
** TRUE it means that the db pointer is invalid and should not be
 
744
** dereferenced for any reason.  The calling function should invoke
 
745
** SQLITE_MISUSE immediately.
 
746
*/
 
747
int sqlite3SafetyCheck(sqlite3 *db){
 
748
  int magic;
 
749
  if( db==0 ) return 1;
 
750
  magic = db->magic;
 
751
  if( magic!=SQLITE_MAGIC_CLOSED &&
 
752
         magic!=SQLITE_MAGIC_OPEN &&
 
753
         magic!=SQLITE_MAGIC_BUSY ) return 1;
 
754
  return 0;
 
755
}
 
756
 
 
757
/*
 
758
** The variable-length integer encoding is as follows:
 
759
**
 
760
** KEY:
 
761
**         A = 0xxxxxxx    7 bits of data and one flag bit
 
762
**         B = 1xxxxxxx    7 bits of data and one flag bit
 
763
**         C = xxxxxxxx    8 bits of data
 
764
**
 
765
**  7 bits - A
 
766
** 14 bits - BA
 
767
** 21 bits - BBA
 
768
** 28 bits - BBBA
 
769
** 35 bits - BBBBA
 
770
** 42 bits - BBBBBA
 
771
** 49 bits - BBBBBBA
 
772
** 56 bits - BBBBBBBA
 
773
** 64 bits - BBBBBBBBC
 
774
*/
 
775
 
 
776
/*
 
777
** Write a 64-bit variable-length integer to memory starting at p[0].
 
778
** The length of data write will be between 1 and 9 bytes.  The number
 
779
** of bytes written is returned.
 
780
**
 
781
** A variable-length integer consists of the lower 7 bits of each byte
 
782
** for all bytes that have the 8th bit set and one byte with the 8th
 
783
** bit clear.  Except, if we get to the 9th byte, it stores the full
 
784
** 8 bits and is the last byte.
 
785
*/
 
786
int sqlite3PutVarint(unsigned char *p, u64 v){
 
787
  int i, j, n;
 
788
  u8 buf[10];
 
789
  if( v & (((u64)0xff000000)<<32) ){
 
790
    p[8] = v;
 
791
    v >>= 8;
 
792
    for(i=7; i>=0; i--){
 
793
      p[i] = (v & 0x7f) | 0x80;
 
794
      v >>= 7;
 
795
    }
 
796
    return 9;
 
797
  }    
 
798
  n = 0;
 
799
  do{
 
800
    buf[n++] = (v & 0x7f) | 0x80;
 
801
    v >>= 7;
 
802
  }while( v!=0 );
 
803
  buf[0] &= 0x7f;
 
804
  assert( n<=9 );
 
805
  for(i=0, j=n-1; j>=0; j--, i++){
 
806
    p[i] = buf[j];
 
807
  }
 
808
  return n;
 
809
}
 
810
 
 
811
/*
 
812
** Read a 64-bit variable-length integer from memory starting at p[0].
 
813
** Return the number of bytes read.  The value is stored in *v.
 
814
*/
 
815
int sqlite3GetVarint(const unsigned char *p, u64 *v){
 
816
  u32 x;
 
817
  u64 x64;
 
818
  int n;
 
819
  unsigned char c;
 
820
  if( ((c = p[0]) & 0x80)==0 ){
 
821
    *v = c;
 
822
    return 1;
 
823
  }
 
824
  x = c & 0x7f;
 
825
  if( ((c = p[1]) & 0x80)==0 ){
 
826
    *v = (x<<7) | c;
 
827
    return 2;
 
828
  }
 
829
  x = (x<<7) | (c&0x7f);
 
830
  if( ((c = p[2]) & 0x80)==0 ){
 
831
    *v = (x<<7) | c;
 
832
    return 3;
 
833
  }
 
834
  x = (x<<7) | (c&0x7f);
 
835
  if( ((c = p[3]) & 0x80)==0 ){
 
836
    *v = (x<<7) | c;
 
837
    return 4;
 
838
  }
 
839
  x64 = (x<<7) | (c&0x7f);
 
840
  n = 4;
 
841
  do{
 
842
    c = p[n++];
 
843
    if( n==9 ){
 
844
      x64 = (x64<<8) | c;
 
845
      break;
 
846
    }
 
847
    x64 = (x64<<7) | (c&0x7f);
 
848
  }while( (c & 0x80)!=0 );
 
849
  *v = x64;
 
850
  return n;
 
851
}
 
852
 
 
853
/*
 
854
** Read a 32-bit variable-length integer from memory starting at p[0].
 
855
** Return the number of bytes read.  The value is stored in *v.
 
856
*/
 
857
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
 
858
  u32 x;
 
859
  int n;
 
860
  unsigned char c;
 
861
  if( ((signed char*)p)[0]>=0 ){
 
862
    *v = p[0];
 
863
    return 1;
 
864
  }
 
865
  x = p[0] & 0x7f;
 
866
  if( ((signed char*)p)[1]>=0 ){
 
867
    *v = (x<<7) | p[1];
 
868
    return 2;
 
869
  }
 
870
  x = (x<<7) | (p[1] & 0x7f);
 
871
  n = 2;
 
872
  do{
 
873
    x = (x<<7) | ((c = p[n++])&0x7f);
 
874
  }while( (c & 0x80)!=0 && n<9 );
 
875
  *v = x;
 
876
  return n;
 
877
}
 
878
 
 
879
/*
 
880
** Return the number of bytes that will be needed to store the given
 
881
** 64-bit integer.
 
882
*/
 
883
int sqlite3VarintLen(u64 v){
 
884
  int i = 0;
 
885
  do{
 
886
    i++;
 
887
    v >>= 7;
 
888
  }while( v!=0 && i<9 );
 
889
  return i;
 
890
}
 
891
 
 
892
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
 
893
    || defined(SQLITE_TEST)
 
894
/*
 
895
** Translate a single byte of Hex into an integer.
 
896
*/
 
897
static int hexToInt(int h){
 
898
  if( h>='0' && h<='9' ){
 
899
    return h - '0';
 
900
  }else if( h>='a' && h<='f' ){
 
901
    return h - 'a' + 10;
 
902
  }else{
 
903
    assert( h>='A' && h<='F' );
 
904
    return h - 'A' + 10;
 
905
  }
 
906
}
 
907
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
 
908
 
 
909
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
 
910
/*
 
911
** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
 
912
** value.  Return a pointer to its binary value.  Space to hold the
 
913
** binary value has been obtained from malloc and must be freed by
 
914
** the calling routine.
 
915
*/
 
916
void *sqlite3HexToBlob(const char *z){
 
917
  char *zBlob;
 
918
  int i;
 
919
  int n = strlen(z);
 
920
  if( n%2 ) return 0;
 
921
 
 
922
  zBlob = (char *)sqliteMalloc(n/2);
 
923
  for(i=0; i<n; i+=2){
 
924
    zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
 
925
  }
 
926
  return zBlob;
 
927
}
 
928
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
 
929
 
 
930
#if defined(SQLITE_TEST)
 
931
/*
 
932
** Convert text generated by the "%p" conversion format back into
 
933
** a pointer.
 
934
*/
 
935
void *sqlite3TextToPtr(const char *z){
 
936
  void *p;
 
937
  u64 v;
 
938
  u32 v2;
 
939
  if( z[0]=='0' && z[1]=='x' ){
 
940
    z += 2;
 
941
  }
 
942
  v = 0;
 
943
  while( *z ){
 
944
    v = (v<<4) + hexToInt(*z);
 
945
    z++;
 
946
  }
 
947
  if( sizeof(p)==sizeof(v) ){
 
948
    p = *(void**)&v;
 
949
  }else{
 
950
    assert( sizeof(p)==sizeof(v2) );
 
951
    v2 = (u32)v;
 
952
    p = *(void**)&v2;
 
953
  }
 
954
  return p;
 
955
}
 
956
#endif