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  /* All of the upper-to-lower conversion data is above.  The following
000061  ** 18 integers are completely unrelated.  They are appended to the
000062  ** sqlite3UpperToLower[] array to avoid UBSAN warnings.  Here's what is
000063  ** going on:
000064  **
000065  ** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
000066  ** by invoking sqlite3MemCompare(A,B) which compares values A and B and
000067  ** returns negative, zero, or positive if A is less then, equal to, or
000068  ** greater than B, respectively.  Then the true false results is found by
000069  ** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or 
000070  ** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
000071  ** is negative, zero, or positive, where opcode is the specific opcode.
000072  ** The only works because the comparison opcodes are consecutive and in
000073  ** this order: NE EQ GT LE LT GE.  Various assert()s throughout the code
000074  ** ensure that is the case.
000075  **
000076  ** These elements must be appended to another array.  Otherwise the
000077  ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
000078  ** be undefined behavior.  That's goofy, but the C-standards people thought
000079  ** it was a good idea, so here we are.
000080  */
000081  /* NE  EQ  GT  LE  LT  GE  */
000082     1,  0,  0,  1,  1,  0,  /* aLTb[]: Use when compare(A,B) less than zero */
000083     0,  1,  0,  1,  0,  1,  /* aEQb[]: Use when compare(A,B) equals zero */
000084     1,  0,  1,  0,  0,  1   /* aGTb[]: Use when compare(A,B) greater than zero*/
000085  };
000086  const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
000087  const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
000088  const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
000089  
000090  /*
000091  ** The following 256 byte lookup table is used to support SQLites built-in
000092  ** equivalents to the following standard library functions:
000093  **
000094  **   isspace()                        0x01
000095  **   isalpha()                        0x02
000096  **   isdigit()                        0x04
000097  **   isalnum()                        0x06
000098  **   isxdigit()                       0x08
000099  **   toupper()                        0x20
000100  **   SQLite identifier character      0x40   $, _, or non-ascii
000101  **   Quote character                  0x80
000102  **
000103  ** Bit 0x20 is set if the mapped character requires translation to upper
000104  ** case. i.e. if the character is a lower-case ASCII character.
000105  ** If x is a lower-case ASCII character, then its upper-case equivalent
000106  ** is (x - 0x20). Therefore toupper() can be implemented as:
000107  **
000108  **   (x & ~(map[x]&0x20))
000109  **
000110  ** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
000111  ** array. tolower() is used more often than toupper() by SQLite.
000112  **
000113  ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an 
000114  ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
000115  ** non-ASCII UTF character. Hence the test for whether or not a character is
000116  ** part of an identifier is 0x46.
000117  */
000118  const unsigned char sqlite3CtypeMap[256] = {
000119    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
000120    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
000121    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
000122    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
000123    0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
000124    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
000125    0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
000126    0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
000127  
000128    0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
000129    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
000130    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
000131    0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
000132    0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
000133    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
000134    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
000135    0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
000136  
000137    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
000138    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
000139    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
000140    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
000141    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
000142    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
000143    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
000144    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
000145  
000146    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
000147    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
000148    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
000149    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
000150    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
000151    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
000152    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
000153    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
000154  };
000155  
000156  /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
000157  ** compatibility for legacy applications, the URI filename capability is
000158  ** disabled by default.
000159  **
000160  ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
000161  ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
000162  **
000163  ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
000164  ** disabled. The default value may be changed by compiling with the
000165  ** SQLITE_USE_URI symbol defined.
000166  */
000167  #ifndef SQLITE_USE_URI
000168  # define SQLITE_USE_URI 0
000169  #endif
000170  
000171  /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
000172  ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
000173  ** that compile-time option is omitted.
000174  */
000175  #if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN)
000176  # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
000177  #else
000178  # if !SQLITE_ALLOW_COVERING_INDEX_SCAN 
000179  #   error "Compile-time disabling of covering index scan using the\
000180   -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
000181   Contact SQLite developers if this is a problem for you, and\
000182   delete this #error macro to continue with your build."
000183  # endif
000184  #endif
000185  
000186  /* The minimum PMA size is set to this value multiplied by the database
000187  ** page size in bytes.
000188  */
000189  #ifndef SQLITE_SORTER_PMASZ
000190  # define SQLITE_SORTER_PMASZ 250
000191  #endif
000192  
000193  /* Statement journals spill to disk when their size exceeds the following
000194  ** threshold (in bytes). 0 means that statement journals are created and
000195  ** written to disk immediately (the default behavior for SQLite versions
000196  ** before 3.12.0).  -1 means always keep the entire statement journal in
000197  ** memory.  (The statement journal is also always held entirely in memory
000198  ** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
000199  ** setting.)
000200  */
000201  #ifndef SQLITE_STMTJRNL_SPILL 
000202  # define SQLITE_STMTJRNL_SPILL (64*1024)
000203  #endif
000204  
000205  /*
000206  ** The default lookaside-configuration, the format "SZ,N".  SZ is the
000207  ** number of bytes in each lookaside slot (should be a multiple of 8)
000208  ** and N is the number of slots.  The lookaside-configuration can be
000209  ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
000210  ** or at run-time for an individual database connection using
000211  ** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
000212  **
000213  ** With the two-size-lookaside enhancement, less lookaside is required.
000214  ** The default configuration of 1200,40 actually provides 30 1200-byte slots
000215  ** and 93 128-byte slots, which is more lookaside than is available
000216  ** using the older 1200,100 configuration without two-size-lookaside.
000217  */
000218  #ifndef SQLITE_DEFAULT_LOOKASIDE
000219  # ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000220  #   define SQLITE_DEFAULT_LOOKASIDE 1200,100  /* 120KB of memory */
000221  # else
000222  #   define SQLITE_DEFAULT_LOOKASIDE 1200,40   /* 48KB of memory */
000223  # endif
000224  #endif
000225  
000226  
000227  /* The default maximum size of an in-memory database created using
000228  ** sqlite3_deserialize()
000229  */
000230  #ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
000231  # define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
000232  #endif
000233  
000234  /*
000235  ** The following singleton contains the global configuration for
000236  ** the SQLite library.
000237  */
000238  SQLITE_WSD struct Sqlite3Config sqlite3Config = {
000239     SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
000240     1,                         /* bCoreMutex */
000241     SQLITE_THREADSAFE==1,      /* bFullMutex */
000242     SQLITE_USE_URI,            /* bOpenUri */
000243     SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
000244     0,                         /* bSmallMalloc */
000245     1,                         /* bExtraSchemaChecks */
000246     sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */
000247     0x7ffffffe,                /* mxStrlen */
000248     0,                         /* neverCorrupt */
000249     SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
000250     SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
000251     {0,0,0,0,0,0,0,0},         /* m */
000252     {0,0,0,0,0,0,0,0,0},       /* mutex */
000253     {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
000254     (void*)0,                  /* pHeap */
000255     0,                         /* nHeap */
000256     0, 0,                      /* mnHeap, mxHeap */
000257     SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
000258     SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
000259     (void*)0,                  /* pPage */
000260     0,                         /* szPage */
000261     SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
000262     0,                         /* mxParserStack */
000263     0,                         /* sharedCacheEnabled */
000264     SQLITE_SORTER_PMASZ,       /* szPma */
000265     /* All the rest should always be initialized to zero */
000266     0,                         /* isInit */
000267     0,                         /* inProgress */
000268     0,                         /* isMutexInit */
000269     0,                         /* isMallocInit */
000270     0,                         /* isPCacheInit */
000271     0,                         /* nRefInitMutex */
000272     0,                         /* pInitMutex */
000273     0,                         /* xLog */
000274     0,                         /* pLogArg */
000275  #ifdef SQLITE_ENABLE_SQLLOG
000276     0,                         /* xSqllog */
000277     0,                         /* pSqllogArg */
000278  #endif
000279  #ifdef SQLITE_VDBE_COVERAGE
000280     0,                         /* xVdbeBranch */
000281     0,                         /* pVbeBranchArg */
000282  #endif
000283  #ifndef SQLITE_OMIT_DESERIALIZE
000284     SQLITE_MEMDB_DEFAULT_MAXSIZE,   /* mxMemdbSize */
000285  #endif
000286  #ifndef SQLITE_UNTESTABLE
000287     0,                         /* xTestCallback */
000288  #endif
000289     0,                         /* bLocaltimeFault */
000290     0,                         /* xAltLocaltime */
000291     0x7ffffffe,                /* iOnceResetThreshold */
000292     SQLITE_DEFAULT_SORTERREF_SIZE,   /* szSorterRef */
000293     0,                         /* iPrngSeed */
000294  #ifdef SQLITE_DEBUG
000295     {0,0,0,0,0,0},             /* aTune */
000296  #endif
000297  };
000298  
000299  /*
000300  ** Hash table for global functions - functions common to all
000301  ** database connections.  After initialization, this table is
000302  ** read-only.
000303  */
000304  FuncDefHash sqlite3BuiltinFunctions;
000305  
000306  #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
000307  /*
000308  ** Counter used for coverage testing.  Does not come into play for
000309  ** release builds.
000310  **
000311  ** Access to this global variable is not mutex protected.  This might
000312  ** result in TSAN warnings.  But as the variable does not exist in
000313  ** release builds, that should not be a concern.
000314  */
000315  unsigned int sqlite3CoverageCounter;
000316  #endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
000317  
000318  #ifdef VDBE_PROFILE
000319  /*
000320  ** The following performance counter can be used in place of
000321  ** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
000322  */
000323  sqlite3_uint64 sqlite3NProfileCnt = 0;
000324  #endif
000325  
000326  /*
000327  ** The value of the "pending" byte must be 0x40000000 (1 byte past the
000328  ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
000329  ** the database page that contains the pending byte.  It never attempts
000330  ** to read or write that page.  The pending byte page is set aside
000331  ** for use by the VFS layers as space for managing file locks.
000332  **
000333  ** During testing, it is often desirable to move the pending byte to
000334  ** a different position in the file.  This allows code that has to
000335  ** deal with the pending byte to run on files that are much smaller
000336  ** than 1 GiB.  The sqlite3_test_control() interface can be used to
000337  ** move the pending byte.
000338  **
000339  ** IMPORTANT:  Changing the pending byte to any value other than
000340  ** 0x40000000 results in an incompatible database file format!
000341  ** Changing the pending byte during operation will result in undefined
000342  ** and incorrect behavior.
000343  */
000344  #ifndef SQLITE_OMIT_WSD
000345  int sqlite3PendingByte = 0x40000000;
000346  #endif
000347  
000348  /*
000349  ** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
000350  */
000351  u32 sqlite3TreeTrace = 0;
000352  u32 sqlite3WhereTrace = 0;
000353  
000354  #include "opcodes.h"
000355  /*
000356  ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
000357  ** created by mkopcodeh.awk during compilation.  Data is obtained
000358  ** from the comments following the "case OP_xxxx:" statements in
000359  ** the vdbe.c file.  
000360  */
000361  const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
000362  
000363  /*
000364  ** Name of the default collating sequence
000365  */
000366  const char sqlite3StrBINARY[] = "BINARY";
000367  
000368  /*
000369  ** Standard typenames.  These names must match the COLTYPE_* definitions.
000370  ** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
000371  **
000372  **    sqlite3StdType[]            The actual names of the datatypes.
000373  **
000374  **    sqlite3StdTypeLen[]         The length (in bytes) of each entry
000375  **                                in sqlite3StdType[].
000376  **
000377  **    sqlite3StdTypeAffinity[]    The affinity associated with each entry
000378  **                                in sqlite3StdType[].
000379  */
000380  const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
000381  const char sqlite3StdTypeAffinity[] = {
000382    SQLITE_AFF_NUMERIC,
000383    SQLITE_AFF_BLOB,
000384    SQLITE_AFF_INTEGER,
000385    SQLITE_AFF_INTEGER,
000386    SQLITE_AFF_REAL,
000387    SQLITE_AFF_TEXT
000388  };
000389  const char *sqlite3StdType[] = {
000390    "ANY",
000391    "BLOB",
000392    "INT",
000393    "INTEGER",
000394    "REAL",
000395    "TEXT"
000396  };