engine/sqlite/src/util.cpp
author skip
Thu, 25 Feb 2010 14:29:19 +0000
changeset 2 29cda98b007e
permissions -rw-r--r--
Initial import of Podcatcher from the Bergamot project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     1
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     2
** 2001 September 15
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     3
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     4
** The author disclaims copyright to this source code.  In place of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     5
** a legal notice, here is a blessing:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     6
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     7
**    May you do good and not evil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     8
**    May you find forgiveness for yourself and forgive others.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     9
**    May you share freely, never taking more than you give.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    10
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    11
*************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    12
** Utility functions used throughout sqlite.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    13
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    14
** This file contains functions for allocating memory, comparing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    15
** strings, and stuff like that.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    16
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    17
** $Id: util.cpp 1282 2008-11-13 09:31:33Z LarsPson $
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    18
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    19
#include "sqliteInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
#include <stdarg.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
#include <ctype.h>
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    25
** Set the most recent error code and error string for the sqlite
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    26
** handle "db". The error code is set to "err_code".
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
** If it is not NULL, string zFormat specifies the format of the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
** error string in the style of the printf functions: The following
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
** format characters are allowed:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    32
**      %s      Insert a string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    33
**      %z      A string that should be freed after use
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
**      %d      Insert an integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
**      %T      Insert a token
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
**      %S      Insert the first element of a SrcList
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
** zFormat and any string tokens that follow it are assumed to be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
** encoded in UTF-8.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
** To clear the most recent error for sqlite handle "db", sqlite3Error
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
** should be called with err_code set to SQLITE_OK and zFormat set
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
** to NULL.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
    db->errCode = err_code;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    48
    if( zFormat ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    49
      char *z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    50
      va_list ap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    51
      va_start(ap, zFormat);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
      z = sqlite3VMPrintf(db, zFormat, ap);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
      va_end(ap);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
** Add an error message to pParse->zErrMsg and increment pParse->nErr.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
** The following formatting characters are allowed:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
**      %s      Insert a string
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
**      %z      A string that should be freed after use
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
**      %d      Insert an integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
**      %T      Insert a token
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
**      %S      Insert the first element of a SrcList
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    71
** This function should be used to report any error that occurs whilst
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    72
** compiling an SQL statement (i.e. within sqlite3_prepare()). The
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
** last thing the sqlite3_prepare() function does is copy the error
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
** stored by this function into the database handle using sqlite3Error().
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
** Function sqlite3Error() should be used during statement execution
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
** (sqlite3_step() etc.).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    79
  va_list ap;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    80
  pParse->nErr++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    81
  sqlite3_free(pParse->zErrMsg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
  va_start(ap, zFormat);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
  pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
  va_end(ap);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
  if( pParse->rc==SQLITE_OK ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    86
    pParse->rc = SQLITE_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    87
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    91
** Clear the error message in pParse, if any
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    92
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    93
void sqlite3ErrorClear(Parse *pParse){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
  sqlite3_free(pParse->zErrMsg);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
  pParse->zErrMsg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
  pParse->nErr = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    97
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    98
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    99
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   100
** Convert an SQL-style quoted string into a normal string by removing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
** the quote characters.  The conversion is done in-place.  If the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
** input does not begin with a quote character, then this routine
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
** is a no-op.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   105
** 2002-Feb-14: This routine is extended to remove MS-Access style
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   106
** brackets from around identifers.  For example:  "[a-b-c]" becomes
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
** "a-b-c".
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
void sqlite3Dequote(char *z){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   110
  int quote;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   111
  int i, j;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
  if( z==0 ) return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   113
  quote = z[0];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   114
  switch( quote ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   115
    case '\'':  break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   116
    case '"':   break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   117
    case '`':   break;                /* For MySQL compatibility */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   118
    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   119
    default:    return;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   120
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   121
  for(i=1, j=0; z[i]; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   122
    if( z[i]==quote ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   123
      if( z[i+1]==quote ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   124
        z[j++] = quote;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   125
        i++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   126
      }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   127
        z[j++] = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   128
        break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   129
      }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   130
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   131
      z[j++] = z[i];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   132
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   133
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   134
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   135
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   136
/* An array to map all upper-case characters into their corresponding
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   137
** lower-case character. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   138
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   139
const unsigned char sqlite3UpperToLower[] = {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   140
#ifdef SQLITE_ASCII
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   141
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   142
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   143
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   144
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   145
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   146
    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   147
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   148
    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   149
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   150
    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   151
    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   152
    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   153
    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   154
    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   155
    252,253,254,255
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   156
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   157
#ifdef SQLITE_EBCDIC
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   158
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   159
     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   160
     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   161
     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   162
     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   163
     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   164
     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   165
    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   166
    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   167
    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   168
    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   169
    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   170
    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   171
    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   172
    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   173
    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   174
#endif
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   175
};
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   176
#define UpperToLower sqlite3UpperToLower
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   177
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   178
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   179
** Some systems have stricmp().  Others have strcasecmp().  Because
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   180
** there is no consistency, we will define our own.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   181
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   182
int sqlite3StrICmp(const char *zLeft, const char *zRight){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   183
  register unsigned char *a, *b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   184
  a = (unsigned char *)zLeft;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   185
  b = (unsigned char *)zRight;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   186
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   187
  return UpperToLower[*a] - UpperToLower[*b];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   188
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   189
int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   190
  register unsigned char *a, *b;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   191
  a = (unsigned char *)zLeft;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   192
  b = (unsigned char *)zRight;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   193
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   194
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   195
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   196
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   197
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   198
** Return TRUE if z is a pure numeric string.  Return FALSE if the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   199
** string contains any character which is not part of a number. If
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   200
** the string is numeric and contains the '.' character, set *realnum
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   201
** to TRUE (otherwise FALSE).
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   202
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   203
** An empty string is considered non-numeric.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   204
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   205
int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   206
  int incr = (enc==SQLITE_UTF8?1:2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   207
  if( enc==SQLITE_UTF16BE ) z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   208
  if( *z=='-' || *z=='+' ) z += incr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   209
  if( !isdigit(*(u8*)z) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   210
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   211
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   212
  z += incr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   213
  if( realnum ) *realnum = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   214
  while( isdigit(*(u8*)z) ){ z += incr; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   215
  if( *z=='.' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   216
    z += incr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   217
    if( !isdigit(*(u8*)z) ) return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   218
    while( isdigit(*(u8*)z) ){ z += incr; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   219
    if( realnum ) *realnum = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   220
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   221
  if( *z=='e' || *z=='E' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   222
    z += incr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   223
    if( *z=='+' || *z=='-' ) z += incr;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   224
    if( !isdigit(*(u8*)z) ) return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   225
    while( isdigit(*(u8*)z) ){ z += incr; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   226
    if( realnum ) *realnum = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   227
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   228
  return *z==0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   229
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   230
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   231
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   232
** The string z[] is an ascii representation of a real number.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   233
** Convert this string to a double.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   234
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   235
** This routine assumes that z[] really is a valid number.  If it
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   236
** is not, the result is undefined.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   237
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   238
** This routine is used instead of the library atof() function because
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   239
** the library atof() might want to use "," as the decimal point instead
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   240
** of "." depending on how locale is set.  But that would cause problems
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   241
** for SQL.  So this routine always uses "." regardless of locale.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   242
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   243
int sqlite3AtoF(const char *z, double *pResult){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   244
#ifndef SQLITE_OMIT_FLOATING_POINT
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   245
  int sign = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   246
  const char *zBegin = z;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   247
  LONGDOUBLE_TYPE v1 = 0.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   248
  while( isspace(*(u8*)z) ) z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   249
  if( *z=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   250
    sign = -1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   251
    z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   252
  }else if( *z=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   253
    z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   254
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   255
  while( isdigit(*(u8*)z) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   256
    v1 = v1*10.0 + (*z - '0');
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   257
    z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   258
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   259
  if( *z=='.' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   260
    LONGDOUBLE_TYPE divisor = 1.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   261
    z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   262
    while( isdigit(*(u8*)z) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   263
      v1 = v1*10.0 + (*z - '0');
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   264
      divisor *= 10.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   265
      z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   266
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   267
    v1 /= divisor;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   268
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   269
  if( *z=='e' || *z=='E' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   270
    int esign = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   271
    int eval = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   272
    LONGDOUBLE_TYPE scale = 1.0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   273
    z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   274
    if( *z=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   275
      esign = -1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   276
      z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   277
    }else if( *z=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   278
      z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   279
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   280
    while( isdigit(*(u8*)z) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   281
      eval = eval*10 + *z - '0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   282
      z++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   283
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   284
    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   285
    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   286
    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   287
    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   288
    if( esign<0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   289
      v1 /= scale;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   290
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   291
      v1 *= scale;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   292
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   293
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   294
  *pResult = sign<0 ? -v1 : v1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   295
  return z - zBegin;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   296
#else
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   297
  return sqlite3Atoi64(z, pResult);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   298
#endif /* SQLITE_OMIT_FLOATING_POINT */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   299
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   300
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   301
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   302
** Compare the 19-character string zNum against the text representation
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   303
** value 2^63:  9223372036854775808.  Return negative, zero, or positive
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   304
** if zNum is less than, equal to, or greater than the string.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   305
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   306
** Unlike memcmp() this routine is guaranteed to return the difference
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   307
** in the values of the last digit if the only difference is in the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   308
** last digit.  So, for example,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   309
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   310
**      compare2pow63("9223372036854775800")
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   311
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   312
** will return -8.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   313
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   314
static int compare2pow63(const char *zNum){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   315
  int c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   316
  c = memcmp(zNum,"922337203685477580",18);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   317
  if( c==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   318
    c = zNum[18] - '8';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   319
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   320
  return c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   321
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   322
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   323
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   324
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   325
** Return TRUE if zNum is a 64-bit signed integer and write
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   326
** the value of the integer into *pNum.  If zNum is not an integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   327
** or is an integer that is too large to be expressed with 64 bits,
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   328
** then return false.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   329
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   330
** When this routine was originally written it dealt with only
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   331
** 32-bit numbers.  At that time, it was much faster than the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   332
** atoi() library routine in RedHat 7.2.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   333
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   334
int sqlite3Atoi64(const char *zNum, i64 *pNum){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   335
  i64 v = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   336
  int neg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   337
  int i, c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   338
  while( isspace(*(u8*)zNum) ) zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   339
  if( *zNum=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   340
    neg = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   341
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   342
  }else if( *zNum=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   343
    neg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   344
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   345
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   346
    neg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   347
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   348
  while( zNum[0]=='0' ){ zNum++; } /* Skip over leading zeros. Ticket #2454 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   349
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   350
    v = v*10 + c - '0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   351
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   352
  *pNum = neg ? -v : v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   353
  if( c!=0 || i==0 || i>19 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   354
    /* zNum is empty or contains non-numeric text or is longer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   355
    ** than 19 digits (thus guaranting that it is too large) */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   356
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   357
  }else if( i<19 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   358
    /* Less than 19 digits, so we know that it fits in 64 bits */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   359
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   360
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   361
    /* 19-digit numbers must be no larger than 9223372036854775807 if positive
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   362
    ** or 9223372036854775808 if negative.  Note that 9223372036854665808
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   363
    ** is 2^63. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   364
    return compare2pow63(zNum)<neg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   365
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   366
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   367
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   368
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   369
** The string zNum represents an integer.  There might be some other
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   370
** information following the integer too, but that part is ignored.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   371
** If the integer that the prefix of zNum represents will fit in a
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   372
** 64-bit signed integer, return TRUE.  Otherwise return FALSE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   373
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   374
** This routine returns FALSE for the string -9223372036854775808 even that
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   375
** that number will, in theory fit in a 64-bit integer.  Positive
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   376
** 9223373036854775808 will not fit in 64 bits.  So it seems safer to return
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   377
** false.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   378
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   379
int sqlite3FitsIn64Bits(const char *zNum, int negFlag){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   380
  int i, c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   381
  int neg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   382
  if( *zNum=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   383
    neg = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   384
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   385
  }else if( *zNum=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   386
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   387
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   388
  if( negFlag ) neg = 1-neg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   389
  while( *zNum=='0' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   390
    zNum++;   /* Skip leading zeros.  Ticket #2454 */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   391
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   392
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   393
  if( i<19 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   394
    /* Guaranteed to fit if less than 19 digits */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   395
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   396
  }else if( i>19 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   397
    /* Guaranteed to be too big if greater than 19 digits */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   398
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   399
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   400
    /* Compare against 2^63. */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   401
    return compare2pow63(zNum)<neg;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   402
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   403
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   404
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   405
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   406
** If zNum represents an integer that will fit in 32-bits, then set
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   407
** *pValue to that integer and return true.  Otherwise return false.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   408
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   409
** Any non-numeric characters that following zNum are ignored.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   410
** This is different from sqlite3Atoi64() which requires the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   411
** input number to be zero-terminated.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   412
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   413
int sqlite3GetInt32(const char *zNum, int *pValue){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   414
  sqlite_int64 v = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   415
  int i, c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   416
  int neg = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   417
  if( zNum[0]=='-' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   418
    neg = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   419
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   420
  }else if( zNum[0]=='+' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   421
    zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   422
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   423
  while( zNum[0]=='0' ) zNum++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   424
  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   425
    v = v*10 + c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   426
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   427
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   428
  /* The longest decimal representation of a 32 bit integer is 10 digits:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   429
  **
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   430
  **             1234567890
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   431
  **     2^31 -> 2147483648
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   432
  */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   433
  if( i>10 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   434
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   435
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   436
  if( v-neg>2147483647 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   437
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   438
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   439
  if( neg ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   440
    v = -v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   441
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   442
  *pValue = (int)v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   443
  return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   444
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   445
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   446
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   447
** Check to make sure we have a valid db pointer.  This test is not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   448
** foolproof but it does provide some measure of protection against
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   449
** misuse of the interface such as passing in db pointers that are
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   450
** NULL or which have been previously closed.  If this routine returns
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   451
** TRUE it means that the db pointer is invalid and should not be
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   452
** dereferenced for any reason.  The calling function should invoke
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   453
** SQLITE_MISUSE immediately.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   454
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   455
int sqlite3SafetyCheck(sqlite3 *db){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   456
  int magic;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   457
  if( db==0 ) return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   458
  magic = db->magic;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   459
  if( magic!=SQLITE_MAGIC_CLOSED &&
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   460
         magic!=SQLITE_MAGIC_OPEN &&
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   461
         magic!=SQLITE_MAGIC_BUSY ) return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   462
  return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   463
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   464
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   465
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   466
** The variable-length integer encoding is as follows:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   467
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   468
** KEY:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   469
**         A = 0xxxxxxx    7 bits of data and one flag bit
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   470
**         B = 1xxxxxxx    7 bits of data and one flag bit
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   471
**         C = xxxxxxxx    8 bits of data
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   472
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   473
**  7 bits - A
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   474
** 14 bits - BA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   475
** 21 bits - BBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   476
** 28 bits - BBBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   477
** 35 bits - BBBBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   478
** 42 bits - BBBBBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   479
** 49 bits - BBBBBBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   480
** 56 bits - BBBBBBBA
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   481
** 64 bits - BBBBBBBBC
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   482
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   483
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   484
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   485
** Write a 64-bit variable-length integer to memory starting at p[0].
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   486
** The length of data write will be between 1 and 9 bytes.  The number
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   487
** of bytes written is returned.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   488
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   489
** A variable-length integer consists of the lower 7 bits of each byte
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   490
** for all bytes that have the 8th bit set and one byte with the 8th
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   491
** bit clear.  Except, if we get to the 9th byte, it stores the full
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   492
** 8 bits and is the last byte.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   493
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   494
int sqlite3PutVarint(unsigned char *p, u64 v){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   495
  int i, j, n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   496
  u8 buf[10];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   497
  if( v & (((u64)0xff000000)<<32) ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   498
    p[8] = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   499
    v >>= 8;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   500
    for(i=7; i>=0; i--){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   501
      p[i] = (v & 0x7f) | 0x80;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   502
      v >>= 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   503
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   504
    return 9;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   505
  }    
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   506
  n = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   507
  do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   508
    buf[n++] = (v & 0x7f) | 0x80;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   509
    v >>= 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   510
  }while( v!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   511
  buf[0] &= 0x7f;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   512
  assert( n<=9 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   513
  for(i=0, j=n-1; j>=0; j--, i++){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   514
    p[i] = buf[j];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   515
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   516
  return n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   517
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   518
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   519
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   520
** Read a 64-bit variable-length integer from memory starting at p[0].
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   521
** Return the number of bytes read.  The value is stored in *v.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   522
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   523
int sqlite3GetVarint(const unsigned char *p, u64 *v){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   524
  u32 x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   525
  u64 x64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   526
  int n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   527
  unsigned char c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   528
  if( ((c = p[0]) & 0x80)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   529
    *v = c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   530
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   531
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   532
  x = c & 0x7f;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   533
  if( ((c = p[1]) & 0x80)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   534
    *v = (x<<7) | c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   535
    return 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   536
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   537
  x = (x<<7) | (c&0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   538
  if( ((c = p[2]) & 0x80)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   539
    *v = (x<<7) | c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   540
    return 3;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   541
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   542
  x = (x<<7) | (c&0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   543
  if( ((c = p[3]) & 0x80)==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   544
    *v = (x<<7) | c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   545
    return 4;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   546
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   547
  x64 = (x<<7) | (c&0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   548
  n = 4;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   549
  do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   550
    c = p[n++];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   551
    if( n==9 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   552
      x64 = (x64<<8) | c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   553
      break;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   554
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   555
    x64 = (x64<<7) | (c&0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   556
  }while( (c & 0x80)!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   557
  *v = x64;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   558
  return n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   559
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   560
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   561
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   562
** Read a 32-bit variable-length integer from memory starting at p[0].
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   563
** Return the number of bytes read.  The value is stored in *v.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   564
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   565
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   566
  u32 x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   567
  int n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   568
  unsigned char c;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   569
  if( ((signed char*)p)[0]>=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   570
    *v = p[0];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   571
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   572
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   573
  x = p[0] & 0x7f;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   574
  if( ((signed char*)p)[1]>=0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   575
    *v = (x<<7) | p[1];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   576
    return 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   577
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   578
  x = (x<<7) | (p[1] & 0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   579
  n = 2;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   580
  do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   581
    x = (x<<7) | ((c = p[n++])&0x7f);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   582
  }while( (c & 0x80)!=0 && n<9 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   583
  *v = x;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   584
  return n;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   585
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   586
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   587
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   588
** Return the number of bytes that will be needed to store the given
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   589
** 64-bit integer.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   590
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   591
int sqlite3VarintLen(u64 v){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   592
  int i = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   593
  do{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   594
    i++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   595
    v >>= 7;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   596
  }while( v!=0 && i<9 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   597
  return i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   598
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   599
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   600
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   601
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   602
** Read or write a four-byte big-endian integer value.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   603
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   604
u32 sqlite3Get4byte(const u8 *p){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   605
  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   606
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   607
void sqlite3Put4byte(unsigned char *p, u32 v){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   608
  p[0] = v>>24;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   609
  p[1] = v>>16;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   610
  p[2] = v>>8;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   611
  p[3] = v;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   612
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   613
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   614
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   615
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   616
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   617
    || defined(SQLITE_TEST)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   618
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   619
** Translate a single byte of Hex into an integer.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   620
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   621
static int hexToInt(int h){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   622
  if( h>='0' && h<='9' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   623
    return h - '0';
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   624
  }else if( h>='a' && h<='f' ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   625
    return h - 'a' + 10;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   626
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   627
    assert( h>='A' && h<='F' );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   628
    return h - 'A' + 10;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   629
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   630
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   631
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   632
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   633
#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   634
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   635
** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   636
** value.  Return a pointer to its binary value.  Space to hold the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   637
** binary value has been obtained from malloc and must be freed by
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   638
** the calling routine.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   639
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   640
void *sqlite3HexToBlob(sqlite3 *db, const char *z){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   641
  char *zBlob;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   642
  int i;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   643
  int n = strlen(z);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   644
  if( n%2 ) return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   645
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   646
  zBlob = (char *)sqlite3DbMallocRaw(db, n/2);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   647
  if( zBlob ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   648
    for(i=0; i<n; i+=2){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   649
      zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   650
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   651
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   652
  return zBlob;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   653
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   654
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   655
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   656
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   657
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   658
** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   659
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   660
** when this routine is called.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   661
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   662
** This routine is called when entering an SQLite API.  The SQLITE_MAGIC_OPEN
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   663
** value indicates that the database connection passed into the API is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   664
** open and is not being used by another thread.  By changing the value
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   665
** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   666
** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   667
** when the API exits. 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   668
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   669
** This routine is a attempt to detect if two threads use the
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   670
** same sqlite* pointer at the same time.  There is a race 
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   671
** condition so it is possible that the error is not detected.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   672
** But usually the problem will be seen.  The result will be an
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   673
** error which can be used to debug the application that is
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   674
** using SQLite incorrectly.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   675
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   676
** Ticket #202:  If db->magic is not a valid open value, take care not
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   677
** to modify the db structure at all.  It could be that db is a stale
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   678
** pointer.  In other words, it could be that there has been a prior
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   679
** call to sqlite3_close(db) and db has been deallocated.  And we do
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   680
** not want to write into deallocated memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   681
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   682
int sqlite3SafetyOn(sqlite3 *db){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   683
  if( db->magic==SQLITE_MAGIC_OPEN ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   684
    db->magic = SQLITE_MAGIC_BUSY;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   685
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   686
  }else if( db->magic==SQLITE_MAGIC_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   687
    db->magic = SQLITE_MAGIC_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   688
    db->u1.isInterrupted = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   689
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   690
  return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   691
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   692
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   693
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   694
** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   695
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   696
** when this routine is called.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   697
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   698
int sqlite3SafetyOff(sqlite3 *db){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   699
  if( db->magic==SQLITE_MAGIC_BUSY ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   700
    db->magic = SQLITE_MAGIC_OPEN;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   701
    return 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   702
  }else {
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   703
    db->magic = SQLITE_MAGIC_ERROR;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   704
    db->u1.isInterrupted = 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   705
    return 1;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   706
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   707
}