000001  /*
000002  ** 2008 June 13
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** This file contains definitions of global variables and constants.
000014  */
000015  #include "sqliteInt.h"
000016  
000017  /* An array to map all upper-case characters into their corresponding
000018  ** lower-case character. 
000019  **
000020  ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
000021  ** handle case conversions for the UTF character set since the tables
000022  ** involved are nearly as big or bigger than SQLite itself.
000023  */
000024  const unsigned char sqlite3UpperToLower[] = {
000025  #ifdef SQLITE_ASCII
000026        0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
000027       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
000028       36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
000029       54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
000030      104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
000031      122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
000032      108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
000033      126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
000034      144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
000035      162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
000036      180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
000037      198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
000038      216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
000039      234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
000040      252,253,254,255
000041  #endif
000042  #ifdef SQLITE_EBCDIC
000043        0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
000044       16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
000045       32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
000046       48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
000047       64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
000048       80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
000049       96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
000050      112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
000051      128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
000052      144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
000053      160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
000054      176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
000055      192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
000056      208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
000057      224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
000058      240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
000059  #endif
000060  };
000061  
000062  /*
000063  ** The following 256 byte lookup table is used to support SQLites built-in
000064  ** equivalents to the following standard library functions:
000065  **
000066  **   isspace()                        0x01
000067  **   isalpha()                        0x02
000068  **   isdigit()                        0x04
000069  **   isalnum()                        0x06
000070  **   isxdigit()                       0x08
000071  **   toupper()                        0x20
000072  **   SQLite identifier character      0x40
000073  **   Quote character                  0x80
000074  **
000075  ** Bit 0x20 is set if the mapped character requires translation to upper
000076  ** case. i.e. if the character is a lower-case ASCII character.
000077  ** If x is a lower-case ASCII character, then its upper-case equivalent
000078  ** is (x - 0x20). Therefore toupper() can be implemented as:
000079  **
000080  **   (x & ~(map[x]&0x20))
000081  **
000082  ** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
000083  ** array. tolower() is used more often than toupper() by SQLite.
000084  **
000085  ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an 
000086  ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
000087  ** non-ASCII UTF character. Hence the test for whether or not a character is
000088  ** part of an identifier is 0x46.
000089  */
000090  #ifdef SQLITE_ASCII
000091  const unsigned char sqlite3CtypeMap[256] = {
000092    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
000093    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
000094    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
000095    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
000096    0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
000097    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
000098    0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
000099    0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
000100  
000101    0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
000102    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
000103    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
000104    0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
000105    0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
000106    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
000107    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
000108    0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
000109  
000110    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
000111    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
000112    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
000113    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
000114    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
000115    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
000116    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
000117    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
000118  
000119    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
000120    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
000121    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
000122    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
000123    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
000124    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
000125    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
000126    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
000127  };
000128  #endif
000129  
000130  /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
000131  ** compatibility for legacy applications, the URI filename capability is
000132  ** disabled by default.
000133  **
000134  ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
000135  ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
000136  **
000137  ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
000138  ** disabled. The default value may be changed by compiling with the
000139  ** SQLITE_USE_URI symbol defined.
000140  */
000141  #ifndef SQLITE_USE_URI
000142  # define  SQLITE_USE_URI 0
000143  #endif
000144  
000145  /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
000146  ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
000147  ** that compile-time option is omitted.
000148  */
000149  #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
000150  # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
000151  #endif
000152  
000153  /* The minimum PMA size is set to this value multiplied by the database
000154  ** page size in bytes.
000155  */
000156  #ifndef SQLITE_SORTER_PMASZ
000157  # define SQLITE_SORTER_PMASZ 250
000158  #endif
000159  
000160  /* Statement journals spill to disk when their size exceeds the following
000161  ** threshold (in bytes). 0 means that statement journals are created and
000162  ** written to disk immediately (the default behavior for SQLite versions
000163  ** before 3.12.0).  -1 means always keep the entire statement journal in
000164  ** memory.  (The statement journal is also always held entirely in memory
000165  ** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
000166  ** setting.)
000167  */
000168  #ifndef SQLITE_STMTJRNL_SPILL 
000169  # define SQLITE_STMTJRNL_SPILL (64*1024)
000170  #endif
000171  
000172  /*
000173  ** The following singleton contains the global configuration for
000174  ** the SQLite library.
000175  */
000176  SQLITE_WSD struct Sqlite3Config sqlite3Config = {
000177     SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
000178     1,                         /* bCoreMutex */
000179     SQLITE_THREADSAFE==1,      /* bFullMutex */
000180     SQLITE_USE_URI,            /* bOpenUri */
000181     SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
000182     0x7ffffffe,                /* mxStrlen */
000183     0,                         /* neverCorrupt */
000184     512,                       /* szLookaside */
000185     125,                       /* nLookaside */
000186     SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
000187     {0,0,0,0,0,0,0,0},         /* m */
000188     {0,0,0,0,0,0,0,0,0},       /* mutex */
000189     {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
000190     (void*)0,                  /* pHeap */
000191     0,                         /* nHeap */
000192     0, 0,                      /* mnHeap, mxHeap */
000193     SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
000194     SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
000195     (void*)0,                  /* pScratch */
000196     0,                         /* szScratch */
000197     0,                         /* nScratch */
000198     (void*)0,                  /* pPage */
000199     0,                         /* szPage */
000200     SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
000201     0,                         /* mxParserStack */
000202     0,                         /* sharedCacheEnabled */
000203     SQLITE_SORTER_PMASZ,       /* szPma */
000204     /* All the rest should always be initialized to zero */
000205     0,                         /* isInit */
000206     0,                         /* inProgress */
000207     0,                         /* isMutexInit */
000208     0,                         /* isMallocInit */
000209     0,                         /* isPCacheInit */
000210     0,                         /* nRefInitMutex */
000211     0,                         /* pInitMutex */
000212     0,                         /* xLog */
000213     0,                         /* pLogArg */
000214  #ifdef SQLITE_ENABLE_SQLLOG
000215     0,                         /* xSqllog */
000216     0,                         /* pSqllogArg */
000217  #endif
000218  #ifdef SQLITE_VDBE_COVERAGE
000219     0,                         /* xVdbeBranch */
000220     0,                         /* pVbeBranchArg */
000221  #endif
000222  #ifndef SQLITE_UNTESTABLE
000223     0,                         /* xTestCallback */
000224  #endif
000225     0,                         /* bLocaltimeFault */
000226     0x7ffffffe                 /* iOnceResetThreshold */
000227  };
000228  
000229  /*
000230  ** Hash table for global functions - functions common to all
000231  ** database connections.  After initialization, this table is
000232  ** read-only.
000233  */
000234  FuncDefHash sqlite3BuiltinFunctions;
000235  
000236  /*
000237  ** Constant tokens for values 0 and 1.
000238  */
000239  const Token sqlite3IntTokens[] = {
000240     { "0", 1 },
000241     { "1", 1 }
000242  };
000243  
000244  
000245  /*
000246  ** The value of the "pending" byte must be 0x40000000 (1 byte past the
000247  ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
000248  ** the database page that contains the pending byte.  It never attempts
000249  ** to read or write that page.  The pending byte page is set aside
000250  ** for use by the VFS layers as space for managing file locks.
000251  **
000252  ** During testing, it is often desirable to move the pending byte to
000253  ** a different position in the file.  This allows code that has to
000254  ** deal with the pending byte to run on files that are much smaller
000255  ** than 1 GiB.  The sqlite3_test_control() interface can be used to
000256  ** move the pending byte.
000257  **
000258  ** IMPORTANT:  Changing the pending byte to any value other than
000259  ** 0x40000000 results in an incompatible database file format!
000260  ** Changing the pending byte during operation will result in undefined
000261  ** and incorrect behavior.
000262  */
000263  #ifndef SQLITE_OMIT_WSD
000264  int sqlite3PendingByte = 0x40000000;
000265  #endif
000266  
000267  #include "opcodes.h"
000268  /*
000269  ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
000270  ** created by mkopcodeh.awk during compilation.  Data is obtained
000271  ** from the comments following the "case OP_xxxx:" statements in
000272  ** the vdbe.c file.  
000273  */
000274  const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
000275  
000276  /*
000277  ** Name of the default collating sequence
000278  */
000279  const char sqlite3StrBINARY[] = "BINARY";