Fossil

Check-in [ea67a544]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update the build-in SQLite to the latest 3.23.0 alpha
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:ea67a5449a6e53d09ccfb937baf46e017bc2a7119d80db01f0b17a91786477a3
User & Date: drh 2018-03-17 16:29:10
Context
2018-03-17
20:52
Update the custom MinGW makefile. check-in: 853b7c7f user: mistachkin tags: trunk
20:31
Experimental support for using 'mman.h' when compiling for POSIX. check-in: 37b69e7d user: mistachkin tags: withMman
16:29
Update the build-in SQLite to the latest 3.23.0 alpha check-in: ea67a544 user: drh tags: trunk
15:55
Add a new viewing mode called "Classic". The new "Classic" is the same as "Verbose" in the previous release. The "Verbose View" is now the same as "Compact" except that the check-in details are always expanded. check-in: b900583c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/main.mk.

   558    558                    -DSQLITE_ENABLE_FTS5 \
   559    559                    -DSQLITE_ENABLE_STMTVTAB \
   560    560                    -DSQLITE_USE_ZLIB \
   561    561                    -DSQLITE_INTROSPECTION_PRAGMAS \
   562    562                    -DSQLITE_ENABLE_DBPAGE_VTAB
   563    563   
   564    564   # Setup the options used to compile the included SQLite shell.
   565         -SHELL_OPTIONS = -Dmain=sqlite3_shell \
          565  +SHELL_OPTIONS = -DNDEBUG=1 \
          566  +                -DSQLITE_THREADSAFE=0 \
          567  +                -DSQLITE_DEFAULT_MEMSTATUS=0 \
          568  +                -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
          569  +                -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
          570  +                -DSQLITE_OMIT_DECLTYPE \
          571  +                -DSQLITE_OMIT_DEPRECATED \
          572  +                -DSQLITE_OMIT_GET_TABLE \
          573  +                -DSQLITE_OMIT_PROGRESS_CALLBACK \
          574  +                -DSQLITE_OMIT_SHARED_CACHE \
          575  +                -DSQLITE_OMIT_LOAD_EXTENSION \
          576  +                -DSQLITE_MAX_EXPR_DEPTH=0 \
          577  +                -DSQLITE_USE_ALLOCA \
          578  +                -DSQLITE_ENABLE_LOCKING_STYLE=0 \
          579  +                -DSQLITE_DEFAULT_FILE_FORMAT=4 \
          580  +                -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
          581  +                -DSQLITE_ENABLE_FTS4 \
          582  +                -DSQLITE_ENABLE_FTS3_PARENTHESIS \
          583  +                -DSQLITE_ENABLE_DBSTAT_VTAB \
          584  +                -DSQLITE_ENABLE_JSON1 \
          585  +                -DSQLITE_ENABLE_FTS5 \
          586  +                -DSQLITE_ENABLE_STMTVTAB \
          587  +                -DSQLITE_USE_ZLIB \
          588  +                -DSQLITE_INTROSPECTION_PRAGMAS \
          589  +                -DSQLITE_ENABLE_DBPAGE_VTAB \
          590  +                -Dmain=sqlite3_shell \
   566    591                   -DSQLITE_SHELL_IS_UTF8=1 \
   567    592                   -DSQLITE_OMIT_LOAD_EXTENSION=1 \
   568    593                   -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
   569    594                   -DSQLITE_SHELL_DBNAME_PROC=fossil_open
   570    595   
   571    596   # Setup the options used to compile the included miniz library.
   572    597   MINIZ_OPTIONS = -DMINIZ_NO_STDIO \

Changes to src/makemake.tcl.

   199    199   #lappend SQLITE_OPTIONS -DSQLITE_ENABLE_FTS3=1
   200    200   #lappend SQLITE_OPTIONS -DSQLITE_ENABLE_STAT4
   201    201   #lappend SQLITE_OPTIONS -DSQLITE_WIN32_NO_ANSI
   202    202   #lappend SQLITE_OPTIONS -DSQLITE_WINNT_MAX_PATH_CHARS=4096
   203    203   
   204    204   # Options used to compile the included SQLite shell.
   205    205   #
   206         -set SHELL_OPTIONS {
          206  +set SHELL_OPTIONS [concat $SQLITE_OPTIONS {
   207    207     -Dmain=sqlite3_shell
   208    208     -DSQLITE_SHELL_IS_UTF8=1
   209    209     -DSQLITE_OMIT_LOAD_EXTENSION=1
   210    210     -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE)
   211    211     -DSQLITE_SHELL_DBNAME_PROC=fossil_open
   212         -}
          212  +}]
   213    213   
   214    214   # miniz (libz drop-in alternative) precompiler flags.
   215    215   #
   216    216   set MINIZ_OPTIONS {
   217    217     -DMINIZ_NO_STDIO
   218    218     -DMINIZ_NO_TIME
   219    219     -DMINIZ_NO_ARCHIVE_APIS

Changes to src/shell.c.

   993    993   #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
   994    994   #define SQLITE_WINDIRENT_H
   995    995   
   996    996   /*
   997    997   ** We need several data types from the Windows SDK header.
   998    998   */
   999    999   
         1000  +#ifndef WIN32_LEAN_AND_MEAN
  1000   1001   #define WIN32_LEAN_AND_MEAN
         1002  +#endif
         1003  +
  1001   1004   #include "windows.h"
  1002   1005   
  1003   1006   /*
  1004   1007   ** We need several support functions from the SQLite core.
  1005   1008   */
  1006   1009   
  1007   1010   
................................................................................
  2312   2315   #if defined(_WIN32)
  2313   2316       /* Windows */
  2314   2317       FILETIME lastAccess;
  2315   2318       FILETIME lastWrite;
  2316   2319       SYSTEMTIME currentTime;
  2317   2320       LONGLONG intervals;
  2318   2321       HANDLE hFile;
         2322  +    LPWSTR zUnicodeName;
         2323  +    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
         2324  +
  2319   2325       GetSystemTime(&currentTime);
  2320   2326       SystemTimeToFileTime(&currentTime, &lastAccess);
  2321   2327       intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
  2322   2328       lastWrite.dwLowDateTime = (DWORD)intervals;
  2323   2329       lastWrite.dwHighDateTime = intervals >> 32;
  2324         -    hFile = CreateFile(
  2325         -      zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
         2330  +    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
         2331  +    hFile = CreateFileW(
         2332  +      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
  2326   2333         FILE_FLAG_BACKUP_SEMANTICS, NULL
  2327   2334       );
         2335  +    sqlite3_free(zUnicodeName);
  2328   2336       if( hFile!=INVALID_HANDLE_VALUE ){
  2329   2337         BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
  2330   2338         CloseHandle(hFile);
  2331   2339         return !bResult;
  2332   2340       }else{
  2333   2341         return 1;
  2334   2342       }
................................................................................
  2528   2536     int i;
  2529   2537     for(i=0; i<=pCur->iLvl; i++){
  2530   2538       FsdirLevel *pLvl = &pCur->aLvl[i];
  2531   2539       if( pLvl->pDir ) closedir(pLvl->pDir);
  2532   2540       sqlite3_free(pLvl->zDir);
  2533   2541     }
  2534   2542     sqlite3_free(pCur->zPath);
         2543  +  sqlite3_free(pCur->aLvl);
  2535   2544     pCur->aLvl = 0;
  2536   2545     pCur->zPath = 0;
  2537   2546     pCur->zBase = 0;
  2538   2547     pCur->nBase = 0;
         2548  +  pCur->nLvl = 0;
  2539   2549     pCur->iLvl = -1;
  2540   2550     pCur->iRowid = 1;
  2541   2551   }
  2542   2552   
  2543   2553   /*
  2544   2554   ** Destructor for an fsdir_cursor.
  2545   2555   */
  2546   2556   static int fsdirClose(sqlite3_vtab_cursor *cur){
  2547   2557     fsdir_cursor *pCur = (fsdir_cursor*)cur;
  2548   2558   
  2549   2559     fsdirResetCursor(pCur);
  2550         -  sqlite3_free(pCur->aLvl);
  2551   2560     sqlite3_free(pCur);
  2552   2561     return SQLITE_OK;
  2553   2562   }
  2554   2563   
  2555   2564   /*
  2556   2565   ** Set the error message for the virtual table associated with cursor
  2557   2566   ** pCur to the results of vprintf(zFmt, ...).
................................................................................
  3990   3999   **    *  Only the "inflate/deflate" (zlib) compression method is supported
  3991   4000   */
  3992   4001   SQLITE_EXTENSION_INIT1
  3993   4002   #include <stdio.h>
  3994   4003   #include <string.h>
  3995   4004   #include <assert.h>
  3996   4005   
  3997         -#include <sys/types.h>
  3998         -#include <sys/stat.h>
  3999         -#include <fcntl.h>
  4000         -#if !defined(_WIN32) && !defined(WIN32)
  4001         -#  include <unistd.h>
  4002         -#  include <dirent.h>
  4003         -#  include <utime.h>
  4004         -#else
  4005         -#  include <io.h>
  4006         -#endif
  4007         -#include <time.h>
  4008         -#include <errno.h>
  4009         -
  4010   4006   #include <zlib.h>
  4011   4007   
  4012   4008   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4013   4009   
  4014   4010   #ifndef SQLITE_AMALGAMATION
         4011  +
  4015   4012   /* typedef sqlite3_int64 i64; */
  4016   4013   /* typedef unsigned char u8; */
  4017   4014   typedef unsigned short u16;
  4018   4015   typedef unsigned long u32;
  4019   4016   #define MIN(a,b) ((a)<(b) ? (a) : (b))
         4017  +
         4018  +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
         4019  +# define ALWAYS(X)      (1)
         4020  +# define NEVER(X)       (0)
         4021  +#elif !defined(NDEBUG)
         4022  +# define ALWAYS(X)      ((X)?1:(assert(0),0))
         4023  +# define NEVER(X)       ((X)?(assert(0),1):0)
         4024  +#else
         4025  +# define ALWAYS(X)      (X)
         4026  +# define NEVER(X)       (X)
         4027  +#endif
         4028  +
         4029  +#endif   /* SQLITE_AMALGAMATION */
         4030  +
         4031  +/*
         4032  +** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
         4033  +**
         4034  +** In some ways it would be better to obtain these values from system 
         4035  +** header files. But, the dependency is undesirable and (a) these
         4036  +** have been stable for decades, (b) the values are part of POSIX and
         4037  +** are also made explicit in [man stat], and (c) are part of the 
         4038  +** file format for zip archives.
         4039  +*/
         4040  +#ifndef S_IFDIR
         4041  +# define S_IFDIR 0040000
         4042  +#endif
         4043  +#ifndef S_IFREG
         4044  +# define S_IFREG 0100000
         4045  +#endif
         4046  +#ifndef S_IFLNK
         4047  +# define S_IFLNK 0120000
  4020   4048   #endif
  4021   4049   
  4022   4050   static const char ZIPFILE_SCHEMA[] = 
  4023   4051     "CREATE TABLE y("
  4024   4052       "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
  4025   4053       "mode,"              /* 1: POSIX mode for file */
  4026   4054       "mtime,"             /* 2: Last modification time (secs since 1970)*/
................................................................................
  4053   4081   **   11 means "utf-8 filename and comment".
  4054   4082   **
  4055   4083   ** ZIPFILE_SIGNATURE_CDS:
  4056   4084   **   First 4 bytes of a valid CDS record.
  4057   4085   **
  4058   4086   ** ZIPFILE_SIGNATURE_LFH:
  4059   4087   **   First 4 bytes of a valid LFH record.
         4088  +**
         4089  +** ZIPFILE_SIGNATURE_EOCD
         4090  +**   First 4 bytes of a valid EOCD record.
  4060   4091   */
  4061   4092   #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
  4062   4093   #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
  4063   4094   #define ZIPFILE_NEWENTRY_REQUIRED 20
  4064   4095   #define ZIPFILE_NEWENTRY_FLAGS    0x800
  4065   4096   #define ZIPFILE_SIGNATURE_CDS     0x02014b50
  4066   4097   #define ZIPFILE_SIGNATURE_LFH     0x04034b50
  4067   4098   #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
         4099  +
         4100  +/*
         4101  +** The sizes of the fixed-size part of each of the three main data 
         4102  +** structures in a zip archive.
         4103  +*/
  4068   4104   #define ZIPFILE_LFH_FIXED_SZ      30
  4069         -
  4070         -/*
  4071         -** Set the error message contained in context ctx to the results of
  4072         -** vprintf(zFmt, ...).
  4073         -*/
  4074         -static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
  4075         -  char *zMsg = 0;
  4076         -  va_list ap;
  4077         -  va_start(ap, zFmt);
  4078         -  zMsg = sqlite3_vmprintf(zFmt, ap);
  4079         -  sqlite3_result_error(ctx, zMsg, -1);
  4080         -  sqlite3_free(zMsg);
  4081         -  va_end(ap);
  4082         -}
  4083         -
         4105  +#define ZIPFILE_EOCD_FIXED_SZ     22
         4106  +#define ZIPFILE_CDS_FIXED_SZ      46
  4084   4107   
  4085   4108   /*
  4086   4109   *** 4.3.16  End of central directory record:
  4087   4110   ***
  4088   4111   ***   end of central dir signature    4 bytes  (0x06054b50)
  4089   4112   ***   number of this disk             2 bytes
  4090   4113   ***   number of the disk with the
................................................................................
  4182   4205     u32 szUncompressed;
  4183   4206     u16 nFile;
  4184   4207     u16 nExtra;
  4185   4208   };
  4186   4209   
  4187   4210   typedef struct ZipfileEntry ZipfileEntry;
  4188   4211   struct ZipfileEntry {
  4189         -  char *zPath;               /* Path of zipfile entry */
  4190         -  u8 *aCdsEntry;             /* Buffer containing entire CDS entry */
  4191         -  int nCdsEntry;             /* Size of buffer aCdsEntry[] in bytes */
  4192         -  int bDeleted;              /* True if entry has been deleted */
         4212  +  ZipfileCDS cds;            /* Parsed CDS record */
         4213  +  u32 mUnixTime;             /* Modification time, in UNIX format */
         4214  +  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
         4215  +  i64 iDataOff;              /* Offset to data in file (if aData==0) */
         4216  +  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
  4193   4217     ZipfileEntry *pNext;       /* Next element in in-memory CDS */
  4194   4218   };
  4195   4219   
  4196   4220   /* 
  4197         -** Cursor type for recursively iterating through a directory structure.
         4221  +** Cursor type for zipfile tables.
  4198   4222   */
  4199   4223   typedef struct ZipfileCsr ZipfileCsr;
  4200   4224   struct ZipfileCsr {
  4201   4225     sqlite3_vtab_cursor base;  /* Base class - must be first */
  4202   4226     i64 iId;                   /* Cursor ID */
  4203         -  int bEof;                  /* True when at EOF */
         4227  +  u8 bEof;                   /* True when at EOF */
         4228  +  u8 bNoop;                  /* If next xNext() call is no-op */
  4204   4229   
  4205   4230     /* Used outside of write transactions */
  4206   4231     FILE *pFile;               /* Zip file */
  4207   4232     i64 iNextOff;              /* Offset of next record in central directory */
  4208   4233     ZipfileEOCD eocd;          /* Parse of central directory record */
  4209   4234   
  4210         -  /* Used inside write transactions */
  4211         -  ZipfileEntry *pCurrent;
  4212         -
  4213         -  ZipfileCDS cds;            /* Central Directory Structure */
  4214         -  ZipfileLFH lfh;            /* Local File Header for current entry */
  4215         -  i64 iDataOff;              /* Offset in zipfile to data */
  4216         -  u32 mTime;                 /* Extended mtime value */
  4217         -  int flags;                 /* Flags byte (see below for bits) */
         4235  +  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
         4236  +  ZipfileEntry *pCurrent;    /* Current entry */
  4218   4237     ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
  4219   4238   };
  4220   4239   
  4221         -/*
  4222         -** Values for ZipfileCsr.flags.
  4223         -*/
  4224         -#define ZIPFILE_MTIME_VALID 0x0001
  4225         -
  4226   4240   typedef struct ZipfileTab ZipfileTab;
  4227   4241   struct ZipfileTab {
  4228   4242     sqlite3_vtab base;         /* Base class - must be first */
  4229   4243     char *zFile;               /* Zip file this table accesses (may be NULL) */
         4244  +  sqlite3 *db;               /* Host database connection */
  4230   4245     u8 *aBuffer;               /* Temporary buffer used for various tasks */
  4231   4246   
  4232   4247     ZipfileCsr *pCsrList;      /* List of cursors */
  4233   4248     i64 iNextCsrid;
  4234   4249   
  4235   4250     /* The following are used by write transactions only */
  4236   4251     ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
  4237   4252     ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
  4238   4253     FILE *pWriteFd;            /* File handle open on zip archive */
  4239   4254     i64 szCurrent;             /* Current size of zip archive */
  4240   4255     i64 szOrig;                /* Size of archive at start of transaction */
  4241   4256   };
  4242   4257   
         4258  +/*
         4259  +** Set the error message contained in context ctx to the results of
         4260  +** vprintf(zFmt, ...).
         4261  +*/
         4262  +static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
         4263  +  char *zMsg = 0;
         4264  +  va_list ap;
         4265  +  va_start(ap, zFmt);
         4266  +  zMsg = sqlite3_vmprintf(zFmt, ap);
         4267  +  sqlite3_result_error(ctx, zMsg, -1);
         4268  +  sqlite3_free(zMsg);
         4269  +  va_end(ap);
         4270  +}
         4271  +
         4272  +/*
         4273  +** If string zIn is quoted, dequote it in place. Otherwise, if the string
         4274  +** is not quoted, do nothing.
         4275  +*/
  4243   4276   static void zipfileDequote(char *zIn){
  4244   4277     char q = zIn[0];
  4245   4278     if( q=='"' || q=='\'' || q=='`' || q=='[' ){
  4246         -    char c;
  4247   4279       int iIn = 1;
  4248   4280       int iOut = 0;
  4249   4281       if( q=='[' ) q = ']';
  4250         -    while( (c = zIn[iIn++]) ){
  4251         -      if( c==q ){
  4252         -        if( zIn[iIn++]!=q ) break;
  4253         -      }
         4282  +    while( ALWAYS(zIn[iIn]) ){
         4283  +      char c = zIn[iIn++];
         4284  +      if( c==q && zIn[iIn++]!=q ) break;
  4254   4285         zIn[iOut++] = c;
  4255   4286       }
  4256   4287       zIn[iOut] = '\0';
  4257   4288     }
  4258   4289   }
  4259   4290   
  4260   4291   /*
................................................................................
  4273   4304     char **pzErr
  4274   4305   ){
  4275   4306     int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
  4276   4307     int nFile = 0;
  4277   4308     const char *zFile = 0;
  4278   4309     ZipfileTab *pNew = 0;
  4279   4310     int rc;
         4311  +
         4312  +  /* If the table name is not "zipfile", require that the argument be
         4313  +  ** specified. This stops zipfile tables from being created as:
         4314  +  **
         4315  +  **   CREATE VIRTUAL TABLE zzz USING zipfile();
         4316  +  **
         4317  +  ** It does not prevent:
         4318  +  **
         4319  +  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
         4320  +  */
         4321  +  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
         4322  +  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
         4323  +    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
         4324  +    return SQLITE_ERROR;
         4325  +  }
  4280   4326   
  4281   4327     if( argc>3 ){
  4282   4328       zFile = argv[3];
  4283   4329       nFile = (int)strlen(zFile)+1;
  4284   4330     }
  4285   4331   
  4286   4332     rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
  4287   4333     if( rc==SQLITE_OK ){
  4288   4334       pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
  4289   4335       if( pNew==0 ) return SQLITE_NOMEM;
  4290   4336       memset(pNew, 0, nByte+nFile);
         4337  +    pNew->db = db;
  4291   4338       pNew->aBuffer = (u8*)&pNew[1];
  4292   4339       if( zFile ){
  4293   4340         pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
  4294   4341         memcpy(pNew->zFile, zFile, nFile);
  4295   4342         zipfileDequote(pNew->zFile);
  4296   4343       }
  4297   4344     }
  4298   4345     *ppVtab = (sqlite3_vtab*)pNew;
  4299   4346     return rc;
  4300   4347   }
         4348  +
         4349  +/*
         4350  +** Free the ZipfileEntry structure indicated by the only argument.
         4351  +*/
         4352  +static void zipfileEntryFree(ZipfileEntry *p){
         4353  +  if( p ){
         4354  +    sqlite3_free(p->cds.zFile);
         4355  +    sqlite3_free(p);
         4356  +  }
         4357  +}
         4358  +
         4359  +/*
         4360  +** Release resources that should be freed at the end of a write 
         4361  +** transaction.
         4362  +*/
         4363  +static void zipfileCleanupTransaction(ZipfileTab *pTab){
         4364  +  ZipfileEntry *pEntry;
         4365  +  ZipfileEntry *pNext;
         4366  +
         4367  +  if( pTab->pWriteFd ){
         4368  +    fclose(pTab->pWriteFd);
         4369  +    pTab->pWriteFd = 0;
         4370  +  }
         4371  +  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
         4372  +    pNext = pEntry->pNext;
         4373  +    zipfileEntryFree(pEntry);
         4374  +  }
         4375  +  pTab->pFirstEntry = 0;
         4376  +  pTab->pLastEntry = 0;
         4377  +  pTab->szCurrent = 0;
         4378  +  pTab->szOrig = 0;
         4379  +}
  4301   4380   
  4302   4381   /*
  4303   4382   ** This method is the destructor for zipfile vtab objects.
  4304   4383   */
  4305   4384   static int zipfileDisconnect(sqlite3_vtab *pVtab){
         4385  +  zipfileCleanupTransaction((ZipfileTab*)pVtab);
  4306   4386     sqlite3_free(pVtab);
  4307   4387     return SQLITE_OK;
  4308   4388   }
  4309   4389   
  4310   4390   /*
  4311   4391   ** Constructor for a new ZipfileCsr object.
  4312   4392   */
................................................................................
  4326   4406   }
  4327   4407   
  4328   4408   /*
  4329   4409   ** Reset a cursor back to the state it was in when first returned
  4330   4410   ** by zipfileOpen().
  4331   4411   */
  4332   4412   static void zipfileResetCursor(ZipfileCsr *pCsr){
  4333         -  sqlite3_free(pCsr->cds.zFile);
  4334         -  pCsr->cds.zFile = 0;
         4413  +  ZipfileEntry *p;
         4414  +  ZipfileEntry *pNext;
         4415  +
  4335   4416     pCsr->bEof = 0;
  4336   4417     if( pCsr->pFile ){
  4337   4418       fclose(pCsr->pFile);
  4338   4419       pCsr->pFile = 0;
         4420  +    zipfileEntryFree(pCsr->pCurrent);
         4421  +    pCsr->pCurrent = 0;
         4422  +  }
         4423  +
         4424  +  for(p=pCsr->pFreeEntry; p; p=pNext){
         4425  +    pNext = p->pNext;
         4426  +    zipfileEntryFree(p);
  4339   4427     }
  4340   4428   }
  4341   4429   
  4342   4430   /*
  4343   4431   ** Destructor for an ZipfileCsr.
  4344   4432   */
  4345   4433   static int zipfileClose(sqlite3_vtab_cursor *cur){
  4346   4434     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4347   4435     ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
  4348   4436     ZipfileCsr **pp;
  4349   4437     zipfileResetCursor(pCsr);
  4350   4438   
  4351   4439     /* Remove this cursor from the ZipfileTab.pCsrList list. */
  4352         -  for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
  4353         -    if( *pp==pCsr ){ 
  4354         -      *pp = pCsr->pCsrNext;
  4355         -      break;
  4356         -    }
  4357         -  }
         4440  +  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
         4441  +  *pp = pCsr->pCsrNext;
  4358   4442   
  4359   4443     sqlite3_free(pCsr);
  4360   4444     return SQLITE_OK;
  4361   4445   }
  4362   4446   
  4363   4447   /*
  4364   4448   ** Set the error message for the virtual table associated with cursor
  4365   4449   ** pCsr to the results of vprintf(zFmt, ...).
  4366   4450   */
  4367         -static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
         4451  +static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
         4452  +  va_list ap;
         4453  +  va_start(ap, zFmt);
         4454  +  sqlite3_free(pTab->base.zErrMsg);
         4455  +  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
         4456  +  va_end(ap);
         4457  +}
         4458  +static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
  4368   4459     va_list ap;
  4369   4460     va_start(ap, zFmt);
         4461  +  sqlite3_free(pCsr->base.pVtab->zErrMsg);
  4370   4462     pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
  4371   4463     va_end(ap);
  4372   4464   }
  4373   4465   
         4466  +/*
         4467  +** Read nRead bytes of data from offset iOff of file pFile into buffer
         4468  +** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
         4469  +** otherwise. 
         4470  +**
         4471  +** If an error does occur, output variable (*pzErrmsg) may be set to point
         4472  +** to an English language error message. It is the responsibility of the
         4473  +** caller to eventually free this buffer using
         4474  +** sqlite3_free().
         4475  +*/
  4374   4476   static int zipfileReadData(
  4375   4477     FILE *pFile,                    /* Read from this file */
  4376   4478     u8 *aRead,                      /* Read into this buffer */
  4377   4479     int nRead,                      /* Number of bytes to read */
  4378   4480     i64 iOff,                       /* Offset to read from */
  4379   4481     char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
  4380   4482   ){
................................................................................
  4400   4502       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
  4401   4503       return SQLITE_ERROR;
  4402   4504     }
  4403   4505     pTab->szCurrent += nWrite;
  4404   4506     return SQLITE_OK;
  4405   4507   }
  4406   4508   
         4509  +/*
         4510  +** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
         4511  +*/
  4407   4512   static u16 zipfileGetU16(const u8 *aBuf){
  4408   4513     return (aBuf[1] << 8) + aBuf[0];
  4409   4514   }
         4515  +
         4516  +/*
         4517  +** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
         4518  +*/
  4410   4519   static u32 zipfileGetU32(const u8 *aBuf){
  4411   4520     return ((u32)(aBuf[3]) << 24)
  4412   4521          + ((u32)(aBuf[2]) << 16)
  4413   4522          + ((u32)(aBuf[1]) <<  8)
  4414   4523          + ((u32)(aBuf[0]) <<  0);
  4415   4524   }
  4416   4525   
         4526  +/*
         4527  +** Write a 16-bit little endiate integer into buffer aBuf.
         4528  +*/
  4417   4529   static void zipfilePutU16(u8 *aBuf, u16 val){
  4418   4530     aBuf[0] = val & 0xFF;
  4419   4531     aBuf[1] = (val>>8) & 0xFF;
  4420   4532   }
         4533  +
         4534  +/*
         4535  +** Write a 32-bit little endiate integer into buffer aBuf.
         4536  +*/
  4421   4537   static void zipfilePutU32(u8 *aBuf, u32 val){
  4422   4538     aBuf[0] = val & 0xFF;
  4423   4539     aBuf[1] = (val>>8) & 0xFF;
  4424   4540     aBuf[2] = (val>>16) & 0xFF;
  4425   4541     aBuf[3] = (val>>24) & 0xFF;
  4426   4542   }
  4427   4543   
  4428   4544   #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
  4429   4545   #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
  4430   4546   
  4431   4547   #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
  4432   4548   #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
  4433   4549   
  4434         -static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
  4435         -  return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
  4436         -}
  4437         -
  4438   4550   /*
  4439   4551   ** Magic numbers used to read CDS records.
  4440   4552   */
  4441         -#define ZIPFILE_CDS_FIXED_SZ         46
  4442   4553   #define ZIPFILE_CDS_NFILE_OFF        28
         4554  +#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
  4443   4555   
  4444   4556   /*
  4445   4557   ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
  4446   4558   ** if the record is not well-formed, or SQLITE_OK otherwise.
  4447   4559   */
  4448   4560   static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
  4449   4561     u8 *aRead = aBuf;
................................................................................
  4472   4584       assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
  4473   4585     }
  4474   4586   
  4475   4587     return rc;
  4476   4588   }
  4477   4589   
  4478   4590   /*
  4479         -** Read the CDS record for the current entry from disk into pCsr->cds.
         4591  +** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
         4592  +** if the record is not well-formed, or SQLITE_OK otherwise.
  4480   4593   */
  4481         -static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
  4482         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
  4483         -  u8 *aRead;
  4484         -  int rc = SQLITE_OK;
  4485         -
  4486         -  sqlite3_free(pCsr->cds.zFile);
  4487         -  pCsr->cds.zFile = 0;
  4488         -
  4489         -  if( pCsr->pCurrent==0 ){
  4490         -    aRead = zipfileCsrBuffer(pCsr);
  4491         -    rc = zipfileReadData(
  4492         -        pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
  4493         -    );
  4494         -  }else{
  4495         -    aRead = pCsr->pCurrent->aCdsEntry;
  4496         -  }
  4497         -
  4498         -  if( rc==SQLITE_OK ){
  4499         -    rc = zipfileReadCDS(aRead, &pCsr->cds);
  4500         -    if( rc!=SQLITE_OK ){
  4501         -      assert( pCsr->pCurrent==0 );
  4502         -      zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
  4503         -    }else{
  4504         -      int nRead;
  4505         -      if( pCsr->pCurrent==0 ){
  4506         -        nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
  4507         -        aRead = zipfileCsrBuffer(pCsr);
  4508         -        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
  4509         -        rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
  4510         -      }else{
  4511         -        aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
  4512         -      }
  4513         -
  4514         -      if( rc==SQLITE_OK ){
  4515         -        pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
  4516         -        pCsr->iNextOff += pCsr->cds.nFile;
  4517         -        pCsr->iNextOff += pCsr->cds.nExtra;
  4518         -        pCsr->iNextOff += pCsr->cds.nComment;
  4519         -      }
  4520         -
  4521         -      /* Scan the cds.nExtra bytes of "extra" fields for any that can
  4522         -      ** be interpreted. The general format of an extra field is:
  4523         -      **
  4524         -      **   Header ID    2 bytes
  4525         -      **   Data Size    2 bytes
  4526         -      **   Data         N bytes
  4527         -      **
  4528         -      */
  4529         -      if( rc==SQLITE_OK ){
  4530         -        u8 *p = &aRead[pCsr->cds.nFile];
  4531         -        u8 *pEnd = &p[pCsr->cds.nExtra];
  4532         -
  4533         -        while( p<pEnd ){
  4534         -          u16 id = zipfileRead16(p);
  4535         -          u16 nByte = zipfileRead16(p);
  4536         -
  4537         -          switch( id ){
  4538         -            case ZIPFILE_EXTRA_TIMESTAMP: {
  4539         -              u8 b = p[0];
  4540         -              if( b & 0x01 ){     /* 0x01 -> modtime is present */
  4541         -                pCsr->mTime = zipfileGetU32(&p[1]);
  4542         -                pCsr->flags |= ZIPFILE_MTIME_VALID;
  4543         -              }
  4544         -              break;
  4545         -            }
  4546         -          }
  4547         -
  4548         -          p += nByte;
  4549         -        }
  4550         -      }
  4551         -    }
  4552         -  }
  4553         -
  4554         -  return rc;
  4555         -}
  4556         -
  4557         -static FILE *zipfileGetFd(ZipfileCsr *pCsr){
  4558         -  if( pCsr->pFile ) return pCsr->pFile;
  4559         -  return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
  4560         -}
  4561         -
  4562   4594   static int zipfileReadLFH(
  4563         -  FILE *pFd, 
  4564         -  i64 iOffset,
  4565         -  u8 *aTmp, 
  4566         -  ZipfileLFH *pLFH, 
  4567         -  char **pzErr
  4568         -){
  4569         -  u8 *aRead = aTmp;
  4570         -  static const int szFix = ZIPFILE_LFH_FIXED_SZ;
  4571         -  int rc;
  4572         -
  4573         -  rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
  4574         -  if( rc==SQLITE_OK ){
  4575         -    u32 sig = zipfileRead32(aRead);
  4576         -    if( sig!=ZIPFILE_SIGNATURE_LFH ){
  4577         -      *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
  4578         -      rc = SQLITE_ERROR;
  4579         -    }else{
  4580         -      pLFH->iVersionExtract = zipfileRead16(aRead);
  4581         -      pLFH->flags = zipfileRead16(aRead);
  4582         -      pLFH->iCompression = zipfileRead16(aRead);
  4583         -      pLFH->mTime = zipfileRead16(aRead);
  4584         -      pLFH->mDate = zipfileRead16(aRead);
  4585         -      pLFH->crc32 = zipfileRead32(aRead);
  4586         -      pLFH->szCompressed = zipfileRead32(aRead);
  4587         -      pLFH->szUncompressed = zipfileRead32(aRead);
  4588         -      pLFH->nFile = zipfileRead16(aRead);
  4589         -      pLFH->nExtra = zipfileRead16(aRead);
  4590         -      assert( aRead==&aTmp[szFix] );
  4591         -    }
  4592         -  }
  4593         -  return rc;
  4594         -}
  4595         -
  4596         -static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
  4597         -  FILE *pFile = zipfileGetFd(pCsr);
  4598         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
  4599         -  u8 *aRead = zipfileCsrBuffer(pCsr);
  4600         -  int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
  4601         -  pCsr->iDataOff =  pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
  4602         -  pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
  4603         -  return rc;
  4604         -}
  4605         -
  4606         -
  4607         -/*
  4608         -** Advance an ZipfileCsr to its next row of output.
  4609         -*/
  4610         -static int zipfileNext(sqlite3_vtab_cursor *cur){
  4611         -  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4612         -  int rc = SQLITE_OK;
  4613         -  pCsr->flags = 0;
  4614         -
  4615         -  if( pCsr->pCurrent==0 ){
  4616         -    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
  4617         -    if( pCsr->iNextOff>=iEof ){
  4618         -      pCsr->bEof = 1;
  4619         -    }
  4620         -  }else{
  4621         -    assert( pCsr->pFile==0 );
  4622         -    do {
  4623         -      pCsr->pCurrent = pCsr->pCurrent->pNext;
  4624         -    }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
  4625         -    if( pCsr->pCurrent==0 ){
  4626         -      pCsr->bEof = 1;
  4627         -    }
  4628         -  }
  4629         -
  4630         -  if( pCsr->bEof==0 ){
  4631         -    rc = zipfileCsrReadCDS(pCsr);
  4632         -    if( rc==SQLITE_OK ){
  4633         -      rc = zipfileCsrReadLFH(pCsr);
  4634         -    }
  4635         -  }
  4636         -
  4637         -  return rc;
  4638         -}
  4639         -
  4640         -/*
         4595  +  u8 *aBuffer,
         4596  +  ZipfileLFH *pLFH
         4597  +){
         4598  +  u8 *aRead = aBuffer;
         4599  +  int rc = SQLITE_OK;
         4600  +
         4601  +  u32 sig = zipfileRead32(aRead);
         4602  +  if( sig!=ZIPFILE_SIGNATURE_LFH ){
         4603  +    rc = SQLITE_ERROR;
         4604  +  }else{
         4605  +    pLFH->iVersionExtract = zipfileRead16(aRead);
         4606  +    pLFH->flags = zipfileRead16(aRead);
         4607  +    pLFH->iCompression = zipfileRead16(aRead);
         4608  +    pLFH->mTime = zipfileRead16(aRead);
         4609  +    pLFH->mDate = zipfileRead16(aRead);
         4610  +    pLFH->crc32 = zipfileRead32(aRead);
         4611  +    pLFH->szCompressed = zipfileRead32(aRead);
         4612  +    pLFH->szUncompressed = zipfileRead32(aRead);
         4613  +    pLFH->nFile = zipfileRead16(aRead);
         4614  +    pLFH->nExtra = zipfileRead16(aRead);
         4615  +  }
         4616  +  return rc;
         4617  +}
         4618  +
         4619  +
         4620  +/*
         4621  +** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
         4622  +** Scan through this buffer to find an "extra-timestamp" field. If one
         4623  +** exists, extract the 32-bit modification-timestamp from it and store
         4624  +** the value in output parameter *pmTime.
         4625  +**
         4626  +** Zero is returned if no extra-timestamp record could be found (and so
         4627  +** *pmTime is left unchanged), or non-zero otherwise.
         4628  +**
         4629  +** The general format of an extra field is:
         4630  +**
         4631  +**   Header ID    2 bytes
         4632  +**   Data Size    2 bytes
         4633  +**   Data         N bytes
         4634  +*/
         4635  +static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
         4636  +  int ret = 0;
         4637  +  u8 *p = aExtra;
         4638  +  u8 *pEnd = &aExtra[nExtra];
         4639  +
         4640  +  while( p<pEnd ){
         4641  +    u16 id = zipfileRead16(p);
         4642  +    u16 nByte = zipfileRead16(p);
         4643  +
         4644  +    switch( id ){
         4645  +      case ZIPFILE_EXTRA_TIMESTAMP: {
         4646  +        u8 b = p[0];
         4647  +        if( b & 0x01 ){     /* 0x01 -> modtime is present */
         4648  +          *pmTime = zipfileGetU32(&p[1]);
         4649  +          ret = 1;
         4650  +        }
         4651  +        break;
         4652  +      }
         4653  +    }
         4654  +
         4655  +    p += nByte;
         4656  +  }
         4657  +  return ret;
         4658  +}
         4659  +
         4660  +/*
         4661  +** Convert the standard MS-DOS timestamp stored in the mTime and mDate
         4662  +** fields of the CDS structure passed as the only argument to a 32-bit
         4663  +** UNIX seconds-since-the-epoch timestamp. Return the result.
         4664  +**
  4641   4665   ** "Standard" MS-DOS time format:
  4642   4666   **
  4643   4667   **   File modification time:
  4644   4668   **     Bits 00-04: seconds divided by 2
  4645   4669   **     Bits 05-10: minute
  4646   4670   **     Bits 11-15: hour
  4647   4671   **   File modification date:
  4648   4672   **     Bits 00-04: day
  4649   4673   **     Bits 05-08: month (1-12)
  4650   4674   **     Bits 09-15: years from 1980 
  4651         -*/
  4652         -static time_t zipfileMtime(ZipfileCsr *pCsr){
  4653         -  struct tm t;
  4654         -  memset(&t, 0, sizeof(t));
  4655         -  t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
  4656         -  t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
  4657         -  t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
  4658         -
  4659         -  t.tm_mday = (pCsr->cds.mDate & 0x1F);
  4660         -  t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
  4661         -  t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
  4662         -
  4663         -  return mktime(&t);
  4664         -}
  4665         -
  4666         -static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
  4667         -  time_t t = (time_t)mTime;
  4668         -  struct tm res;
  4669         -
  4670         -#if !defined(_WIN32) && !defined(WIN32)
  4671         -  localtime_r(&t, &res);
  4672         -#else
  4673         -  memcpy(&res, localtime(&t), sizeof(struct tm));
  4674         -#endif
  4675         -
  4676         -  pCds->mTime = (u16)(
  4677         -    (res.tm_sec / 2) + 
  4678         -    (res.tm_min << 5) +
  4679         -    (res.tm_hour << 11));
  4680         -
  4681         -  pCds->mDate = (u16)(
  4682         -    (res.tm_mday-1) +
  4683         -    ((res.tm_mon+1) << 5) +
  4684         -    ((res.tm_year-80) << 9));
  4685         -}
  4686         -
         4675  +**
         4676  +** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
         4677  +*/
         4678  +static u32 zipfileMtime(ZipfileCDS *pCDS){
         4679  +  int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
         4680  +  int M = ((pCDS->mDate >> 5) & 0x0F);
         4681  +  int D = (pCDS->mDate & 0x1F);
         4682  +  int B = -13;
         4683  +
         4684  +  int sec = (pCDS->mTime & 0x1F)*2;
         4685  +  int min = (pCDS->mTime >> 5) & 0x3F;
         4686  +  int hr = (pCDS->mTime >> 11) & 0x1F;
         4687  +  i64 JD;
         4688  +
         4689  +  /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
         4690  +
         4691  +  /* Calculate the JD in seconds for noon on the day in question */
         4692  +  if( M<3 ){
         4693  +    Y = Y-1;
         4694  +    M = M+12;
         4695  +  }
         4696  +  JD = (i64)(24*60*60) * (
         4697  +      (int)(365.25 * (Y + 4716))
         4698  +    + (int)(30.6001 * (M + 1))
         4699  +    + D + B - 1524
         4700  +  );
         4701  +
         4702  +  /* Correct the JD for the time within the day */
         4703  +  JD += (hr-12) * 3600 + min * 60 + sec;
         4704  +
         4705  +  /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
         4706  +  return (u32)(JD - (i64)(24405875) * 24*60*6);
         4707  +}
         4708  +
         4709  +/*
         4710  +** The opposite of zipfileMtime(). This function populates the mTime and
         4711  +** mDate fields of the CDS structure passed as the first argument according
         4712  +** to the UNIX timestamp value passed as the second.
         4713  +*/
         4714  +static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
         4715  +  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
         4716  +  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
         4717  +
         4718  +  int A, B, C, D, E;
         4719  +  int yr, mon, day;
         4720  +  int hr, min, sec;
         4721  +
         4722  +  A = (int)((JD - 1867216.25)/36524.25);
         4723  +  A = (int)(JD + 1 + A - (A/4));
         4724  +  B = A + 1524;
         4725  +  C = (int)((B - 122.1)/365.25);
         4726  +  D = (36525*(C&32767))/100;
         4727  +  E = (int)((B-D)/30.6001);
         4728  +
         4729  +  day = B - D - (int)(30.6001*E);
         4730  +  mon = (E<14 ? E-1 : E-13);
         4731  +  yr = mon>2 ? C-4716 : C-4715;
         4732  +
         4733  +  hr = (mUnixTime % (24*60*60)) / (60*60);
         4734  +  min = (mUnixTime % (60*60)) / 60;
         4735  +  sec = (mUnixTime % 60);
         4736  +
         4737  +  if( yr>=1980 ){
         4738  +    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
         4739  +    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
         4740  +  }else{
         4741  +    pCds->mDate = pCds->mTime = 0;
         4742  +  }
         4743  +
         4744  +  assert( mUnixTime<315507600 
         4745  +       || mUnixTime==zipfileMtime(pCds) 
         4746  +       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
         4747  +       /* || (mUnixTime % 2) */
         4748  +  );
         4749  +}
         4750  +
         4751  +/*
         4752  +** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
         4753  +** size) containing an entire zip archive image. Or, if aBlob is NULL,
         4754  +** then pFile is a file-handle open on a zip file. In either case, this
         4755  +** function creates a ZipfileEntry object based on the zip archive entry
         4756  +** for which the CDS record is at offset iOff.
         4757  +**
         4758  +** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
         4759  +** the new object. Otherwise, an SQLite error code is returned and the
         4760  +** final value of (*ppEntry) undefined.
         4761  +*/
         4762  +static int zipfileGetEntry(
         4763  +  ZipfileTab *pTab,               /* Store any error message here */
         4764  +  const u8 *aBlob,                /* Pointer to in-memory file image */
         4765  +  int nBlob,                      /* Size of aBlob[] in bytes */
         4766  +  FILE *pFile,                    /* If aBlob==0, read from this file */
         4767  +  i64 iOff,                       /* Offset of CDS record */
         4768  +  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
         4769  +){
         4770  +  u8 *aRead;
         4771  +  char **pzErr = &pTab->base.zErrMsg;
         4772  +  int rc = SQLITE_OK;
         4773  +
         4774  +  if( aBlob==0 ){
         4775  +    aRead = pTab->aBuffer;
         4776  +    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
         4777  +  }else{
         4778  +    aRead = (u8*)&aBlob[iOff];
         4779  +  }
         4780  +
         4781  +  if( rc==SQLITE_OK ){
         4782  +    int nAlloc;
         4783  +    ZipfileEntry *pNew;
         4784  +
         4785  +    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
         4786  +    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
         4787  +    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
         4788  +
         4789  +    nAlloc = sizeof(ZipfileEntry) + nExtra;
         4790  +    if( aBlob ){
         4791  +      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
         4792  +    }
         4793  +
         4794  +    pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
         4795  +    if( pNew==0 ){
         4796  +      rc = SQLITE_NOMEM;
         4797  +    }else{
         4798  +      memset(pNew, 0, sizeof(ZipfileEntry));
         4799  +      rc = zipfileReadCDS(aRead, &pNew->cds);
         4800  +      if( rc!=SQLITE_OK ){
         4801  +        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
         4802  +      }else if( aBlob==0 ){
         4803  +        rc = zipfileReadData(
         4804  +            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
         4805  +        );
         4806  +      }else{
         4807  +        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
         4808  +      }
         4809  +    }
         4810  +
         4811  +    if( rc==SQLITE_OK ){
         4812  +      u32 *pt = &pNew->mUnixTime;
         4813  +      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
         4814  +      pNew->aExtra = (u8*)&pNew[1];
         4815  +      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
         4816  +      if( pNew->cds.zFile==0 ){
         4817  +        rc = SQLITE_NOMEM;
         4818  +      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
         4819  +        pNew->mUnixTime = zipfileMtime(&pNew->cds);
         4820  +      }
         4821  +    }
         4822  +
         4823  +    if( rc==SQLITE_OK ){
         4824  +      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
         4825  +      ZipfileLFH lfh;
         4826  +      if( pFile ){
         4827  +        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
         4828  +      }else{
         4829  +        aRead = (u8*)&aBlob[pNew->cds.iOffset];
         4830  +      }
         4831  +
         4832  +      rc = zipfileReadLFH(aRead, &lfh);
         4833  +      if( rc==SQLITE_OK ){
         4834  +        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
         4835  +        pNew->iDataOff += lfh.nFile + lfh.nExtra;
         4836  +        if( aBlob && pNew->cds.szCompressed ){
         4837  +          pNew->aData = &pNew->aExtra[nExtra];
         4838  +          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
         4839  +        }
         4840  +      }else{
         4841  +        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
         4842  +            (int)pNew->cds.iOffset
         4843  +        );
         4844  +      }
         4845  +    }
         4846  +
         4847  +    if( rc!=SQLITE_OK ){
         4848  +      zipfileEntryFree(pNew);
         4849  +    }else{
         4850  +      *ppEntry = pNew;
         4851  +    }
         4852  +  }
         4853  +
         4854  +  return rc;
         4855  +}
         4856  +
         4857  +/*
         4858  +** Advance an ZipfileCsr to its next row of output.
         4859  +*/
         4860  +static int zipfileNext(sqlite3_vtab_cursor *cur){
         4861  +  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
         4862  +  int rc = SQLITE_OK;
         4863  +
         4864  +  if( pCsr->pFile ){
         4865  +    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
         4866  +    zipfileEntryFree(pCsr->pCurrent);
         4867  +    pCsr->pCurrent = 0;
         4868  +    if( pCsr->iNextOff>=iEof ){
         4869  +      pCsr->bEof = 1;
         4870  +    }else{
         4871  +      ZipfileEntry *p = 0;
         4872  +      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
         4873  +      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
         4874  +      if( rc==SQLITE_OK ){
         4875  +        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
         4876  +        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
         4877  +      }
         4878  +      pCsr->pCurrent = p;
         4879  +    }
         4880  +  }else{
         4881  +    if( !pCsr->bNoop ){
         4882  +      pCsr->pCurrent = pCsr->pCurrent->pNext;
         4883  +    }
         4884  +    if( pCsr->pCurrent==0 ){
         4885  +      pCsr->bEof = 1;
         4886  +    }
         4887  +  }
         4888  +
         4889  +  pCsr->bNoop = 0;
         4890  +  return rc;
         4891  +}
         4892  +
         4893  +static void zipfileFree(void *p) { 
         4894  +  sqlite3_free(p); 
         4895  +}
         4896  +
         4897  +/*
         4898  +** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
         4899  +** size is nOut bytes. This function uncompresses the data and sets the
         4900  +** return value in context pCtx to the result (a blob).
         4901  +**
         4902  +** If an error occurs, an error code is left in pCtx instead.
         4903  +*/
  4687   4904   static void zipfileInflate(
  4688         -  sqlite3_context *pCtx,          /* Store error here, if any */
         4905  +  sqlite3_context *pCtx,          /* Store result here */
  4689   4906     const u8 *aIn,                  /* Compressed data */
  4690   4907     int nIn,                        /* Size of buffer aIn[] in bytes */
  4691   4908     int nOut                        /* Expected output size */
  4692   4909   ){
  4693   4910     u8 *aRes = sqlite3_malloc(nOut);
  4694   4911     if( aRes==0 ){
  4695   4912       sqlite3_result_error_nomem(pCtx);
................................................................................
  4707   4924       if( err!=Z_OK ){
  4708   4925         zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
  4709   4926       }else{
  4710   4927         err = inflate(&str, Z_NO_FLUSH);
  4711   4928         if( err!=Z_STREAM_END ){
  4712   4929           zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
  4713   4930         }else{
  4714         -        sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
         4931  +        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
         4932  +        aRes = 0;
  4715   4933         }
  4716   4934       }
  4717   4935       sqlite3_free(aRes);
  4718   4936       inflateEnd(&str);
  4719   4937     }
  4720   4938   }
  4721   4939   
         4940  +/*
         4941  +** Buffer aIn (size nIn bytes) contains uncompressed data. This function
         4942  +** compresses it and sets (*ppOut) to point to a buffer containing the
         4943  +** compressed data. The caller is responsible for eventually calling
         4944  +** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
         4945  +** is set to the size of buffer (*ppOut) in bytes.
         4946  +**
         4947  +** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
         4948  +** code is returned and an error message left in virtual-table handle
         4949  +** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
         4950  +** case.
         4951  +*/
  4722   4952   static int zipfileDeflate(
  4723         -  ZipfileTab *pTab,               /* Set error message here */
  4724   4953     const u8 *aIn, int nIn,         /* Input */
  4725         -  u8 **ppOut, int *pnOut          /* Output */
         4954  +  u8 **ppOut, int *pnOut,         /* Output */
         4955  +  char **pzErr                    /* OUT: Error message */
  4726   4956   ){
  4727   4957     int nAlloc = (int)compressBound(nIn);
  4728   4958     u8 *aOut;
  4729   4959     int rc = SQLITE_OK;
  4730   4960   
  4731   4961     aOut = (u8*)sqlite3_malloc(nAlloc);
  4732   4962     if( aOut==0 ){
................................................................................
  4744   4974       res = deflate(&str, Z_FINISH);
  4745   4975   
  4746   4976       if( res==Z_STREAM_END ){
  4747   4977         *ppOut = aOut;
  4748   4978         *pnOut = (int)str.total_out;
  4749   4979       }else{
  4750   4980         sqlite3_free(aOut);
  4751         -      pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
         4981  +      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
  4752   4982         rc = SQLITE_ERROR;
  4753   4983       }
  4754   4984       deflateEnd(&str);
  4755   4985     }
  4756   4986   
  4757   4987     return rc;
  4758   4988   }
................................................................................
  4764   4994   */
  4765   4995   static int zipfileColumn(
  4766   4996     sqlite3_vtab_cursor *cur,   /* The cursor */
  4767   4997     sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  4768   4998     int i                       /* Which column to return */
  4769   4999   ){
  4770   5000     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
         5001  +  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
  4771   5002     int rc = SQLITE_OK;
  4772   5003     switch( i ){
  4773   5004       case 0:   /* name */
  4774         -      sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
         5005  +      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
  4775   5006         break;
  4776   5007       case 1:   /* mode */
  4777   5008         /* TODO: Whether or not the following is correct surely depends on
  4778   5009         ** the platform on which the archive was created.  */
  4779         -      sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
         5010  +      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
  4780   5011         break;
  4781   5012       case 2: { /* mtime */
  4782         -      if( pCsr->flags & ZIPFILE_MTIME_VALID ){
  4783         -        sqlite3_result_int64(ctx, pCsr->mTime);
  4784         -      }else{
  4785         -        sqlite3_result_int64(ctx, zipfileMtime(pCsr));
  4786         -      }
         5013  +      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
  4787   5014         break;
  4788   5015       }
  4789   5016       case 3: { /* sz */
  4790   5017         if( sqlite3_vtab_nochange(ctx)==0 ){
  4791         -        sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
         5018  +        sqlite3_result_int64(ctx, pCDS->szUncompressed);
  4792   5019         }
  4793   5020         break;
  4794   5021       }
  4795   5022       case 4:   /* rawdata */
  4796   5023         if( sqlite3_vtab_nochange(ctx) ) break;
  4797   5024       case 5: { /* data */
  4798         -      if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
  4799         -        int sz = pCsr->cds.szCompressed;
  4800         -        int szFinal = pCsr->cds.szUncompressed;
         5025  +      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
         5026  +        int sz = pCDS->szCompressed;
         5027  +        int szFinal = pCDS->szUncompressed;
  4801   5028           if( szFinal>0 ){
  4802         -          u8 *aBuf = sqlite3_malloc(sz);
  4803         -          if( aBuf==0 ){
  4804         -            rc = SQLITE_NOMEM;
         5029  +          u8 *aBuf;
         5030  +          u8 *aFree = 0;
         5031  +          if( pCsr->pCurrent->aData ){
         5032  +            aBuf = pCsr->pCurrent->aData;
  4805   5033             }else{
  4806         -            FILE *pFile = zipfileGetFd(pCsr);
  4807         -            rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
  4808         -                &pCsr->base.pVtab->zErrMsg
  4809         -            );
         5034  +            aBuf = aFree = sqlite3_malloc(sz);
         5035  +            if( aBuf==0 ){
         5036  +              rc = SQLITE_NOMEM;
         5037  +            }else{
         5038  +              FILE *pFile = pCsr->pFile;
         5039  +              if( pFile==0 ){
         5040  +                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
         5041  +              }
         5042  +              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
         5043  +                  &pCsr->base.pVtab->zErrMsg
         5044  +              );
         5045  +            }
  4810   5046             }
  4811   5047             if( rc==SQLITE_OK ){
  4812         -            if( i==5 && pCsr->cds.iCompression ){
         5048  +            if( i==5 && pCDS->iCompression ){
  4813   5049                 zipfileInflate(ctx, aBuf, sz, szFinal);
  4814   5050               }else{
  4815   5051                 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
  4816   5052               }
  4817         -            sqlite3_free(aBuf);
  4818   5053             }
         5054  +          sqlite3_free(aFree);
  4819   5055           }else{
  4820   5056             /* Figure out if this is a directory or a zero-sized file. Consider
  4821   5057             ** it to be a directory either if the mode suggests so, or if
  4822   5058             ** the final character in the name is '/'.  */
  4823         -          u32 mode = pCsr->cds.iExternalAttr >> 16;
  4824         -          if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
         5059  +          u32 mode = pCDS->iExternalAttr >> 16;
         5060  +          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
  4825   5061               sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
  4826   5062             }
  4827   5063           }
  4828   5064         }
  4829   5065         break;
  4830   5066       }
  4831   5067       case 6:   /* method */
  4832         -      sqlite3_result_int(ctx, pCsr->cds.iCompression);
         5068  +      sqlite3_result_int(ctx, pCDS->iCompression);
  4833   5069         break;
  4834         -    case 7:   /* z */
         5070  +    default:  /* z */
         5071  +      assert( i==7 );
  4835   5072         sqlite3_result_int64(ctx, pCsr->iId);
  4836   5073         break;
  4837   5074     }
  4838   5075   
  4839   5076     return rc;
  4840   5077   }
  4841   5078   
  4842   5079   /*
  4843         -** Return the rowid for the current row.
  4844         -*/
  4845         -static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  4846         -  assert( 0 );
  4847         -  return SQLITE_OK;
  4848         -}
  4849         -
  4850         -/*
  4851         -** Return TRUE if the cursor has been moved off of the last
  4852         -** row of output.
         5080  +** Return TRUE if the cursor is at EOF.
  4853   5081   */
  4854   5082   static int zipfileEof(sqlite3_vtab_cursor *cur){
  4855   5083     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4856   5084     return pCsr->bEof;
  4857   5085   }
  4858   5086   
  4859   5087   /*
         5088  +** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
         5089  +** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
         5090  +** is guaranteed to be a file-handle open on a zip file.
         5091  +**
         5092  +** This function attempts to locate the EOCD record within the zip archive
         5093  +** and populate *pEOCD with the results of decoding it. SQLITE_OK is
         5094  +** returned if successful. Otherwise, an SQLite error code is returned and
         5095  +** an English language error message may be left in virtual-table pTab.
  4860   5096   */
  4861   5097   static int zipfileReadEOCD(
  4862   5098     ZipfileTab *pTab,               /* Return errors here */
  4863         -  FILE *pFile,                    /* Read from this file */
         5099  +  const u8 *aBlob,                /* Pointer to in-memory file image */
         5100  +  int nBlob,                      /* Size of aBlob[] in bytes */
         5101  +  FILE *pFile,                    /* Read from this file if aBlob==0 */
  4864   5102     ZipfileEOCD *pEOCD              /* Object to populate */
  4865   5103   ){
  4866   5104     u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
  4867         -  i64 szFile;                     /* Total size of file in bytes */
  4868   5105     int nRead;                      /* Bytes to read from file */
  4869         -  i64 iOff;                       /* Offset to read from */
  4870         -  int rc;
         5106  +  int rc = SQLITE_OK;
  4871   5107   
  4872         -  fseek(pFile, 0, SEEK_END);
  4873         -  szFile = (i64)ftell(pFile);
  4874         -  if( szFile==0 ){
  4875         -    memset(pEOCD, 0, sizeof(ZipfileEOCD));
  4876         -    return SQLITE_OK;
         5108  +  if( aBlob==0 ){
         5109  +    i64 iOff;                     /* Offset to read from */
         5110  +    i64 szFile;                   /* Total size of file in bytes */
         5111  +    fseek(pFile, 0, SEEK_END);
         5112  +    szFile = (i64)ftell(pFile);
         5113  +    if( szFile==0 ){
         5114  +      memset(pEOCD, 0, sizeof(ZipfileEOCD));
         5115  +      return SQLITE_OK;
         5116  +    }
         5117  +    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
         5118  +    iOff = szFile - nRead;
         5119  +    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
         5120  +  }else{
         5121  +    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
         5122  +    aRead = (u8*)&aBlob[nBlob-nRead];
  4877   5123     }
  4878         -  nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
  4879         -  iOff = szFile - nRead;
  4880   5124   
  4881         -  rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
  4882   5125     if( rc==SQLITE_OK ){
  4883   5126       int i;
  4884   5127   
  4885   5128       /* Scan backwards looking for the signature bytes */
  4886   5129       for(i=nRead-20; i>=0; i--){
  4887   5130         if( aRead[i]==0x50 && aRead[i+1]==0x4b 
  4888   5131          && aRead[i+2]==0x05 && aRead[i+3]==0x06 
................................................................................
  4900   5143       aRead += i+4;
  4901   5144       pEOCD->iDisk = zipfileRead16(aRead);
  4902   5145       pEOCD->iFirstDisk = zipfileRead16(aRead);
  4903   5146       pEOCD->nEntry = zipfileRead16(aRead);
  4904   5147       pEOCD->nEntryTotal = zipfileRead16(aRead);
  4905   5148       pEOCD->nSize = zipfileRead32(aRead);
  4906   5149       pEOCD->iOffset = zipfileRead32(aRead);
  4907         -
  4908         -#if 0
  4909         -    printf("iDisk=%d  iFirstDisk=%d  nEntry=%d  "
  4910         -           "nEntryTotal=%d  nSize=%d  iOffset=%d", 
  4911         -           (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
  4912         -           (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
  4913         -    );
  4914         -#endif
  4915         -  }
  4916         -
  4917         -  return SQLITE_OK;
         5150  +  }
         5151  +
         5152  +  return rc;
         5153  +}
         5154  +
         5155  +/*
         5156  +** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
         5157  +** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
         5158  +** to the end of the list. Otherwise, it is added to the list immediately
         5159  +** before pBefore (which is guaranteed to be a part of said list).
         5160  +*/
         5161  +static void zipfileAddEntry(
         5162  +  ZipfileTab *pTab, 
         5163  +  ZipfileEntry *pBefore, 
         5164  +  ZipfileEntry *pNew
         5165  +){
         5166  +  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
         5167  +  assert( pNew->pNext==0 );
         5168  +  if( pBefore==0 ){
         5169  +    if( pTab->pFirstEntry==0 ){
         5170  +      pTab->pFirstEntry = pTab->pLastEntry = pNew;
         5171  +    }else{
         5172  +      assert( pTab->pLastEntry->pNext==0 );
         5173  +      pTab->pLastEntry->pNext = pNew;
         5174  +      pTab->pLastEntry = pNew;
         5175  +    }
         5176  +  }else{
         5177  +    ZipfileEntry **pp;
         5178  +    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
         5179  +    pNew->pNext = pBefore;
         5180  +    *pp = pNew;
         5181  +  }
         5182  +}
         5183  +
         5184  +static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
         5185  +  ZipfileEOCD eocd;
         5186  +  int rc;
         5187  +  int i;
         5188  +  i64 iOff;
         5189  +
         5190  +  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
         5191  +  iOff = eocd.iOffset;
         5192  +  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
         5193  +    ZipfileEntry *pNew = 0;
         5194  +    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
         5195  +
         5196  +    if( rc==SQLITE_OK ){
         5197  +      zipfileAddEntry(pTab, 0, pNew);
         5198  +      iOff += ZIPFILE_CDS_FIXED_SZ;
         5199  +      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
         5200  +    }
         5201  +  }
         5202  +  return rc;
  4918   5203   }
  4919   5204   
  4920   5205   /*
  4921   5206   ** xFilter callback.
  4922   5207   */
  4923   5208   static int zipfileFilter(
  4924   5209     sqlite3_vtab_cursor *cur, 
  4925   5210     int idxNum, const char *idxStr,
  4926   5211     int argc, sqlite3_value **argv
  4927   5212   ){
  4928   5213     ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
  4929   5214     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4930         -  const char *zFile;              /* Zip file to scan */
         5215  +  const char *zFile = 0;          /* Zip file to scan */
  4931   5216     int rc = SQLITE_OK;             /* Return Code */
         5217  +  int bInMemory = 0;              /* True for an in-memory zipfile */
  4932   5218   
  4933   5219     zipfileResetCursor(pCsr);
  4934   5220   
  4935   5221     if( pTab->zFile ){
  4936   5222       zFile = pTab->zFile;
  4937   5223     }else if( idxNum==0 ){
  4938         -    /* Error. This is an eponymous virtual table and the user has not 
  4939         -    ** supplied a file name. */
  4940         -    zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
         5224  +    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
  4941   5225       return SQLITE_ERROR;
         5226  +  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
         5227  +    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
         5228  +    int nBlob = sqlite3_value_bytes(argv[0]);
         5229  +    assert( pTab->pFirstEntry==0 );
         5230  +    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
         5231  +    pCsr->pFreeEntry = pTab->pFirstEntry;
         5232  +    pTab->pFirstEntry = pTab->pLastEntry = 0;
         5233  +    if( rc!=SQLITE_OK ) return rc;
         5234  +    bInMemory = 1;
  4942   5235     }else{
  4943   5236       zFile = (const char*)sqlite3_value_text(argv[0]);
  4944   5237     }
  4945   5238   
  4946         -  if( pTab->pWriteFd==0 ){
         5239  +  if( 0==pTab->pWriteFd && 0==bInMemory ){
  4947   5240       pCsr->pFile = fopen(zFile, "rb");
  4948   5241       if( pCsr->pFile==0 ){
  4949         -      zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
         5242  +      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
  4950   5243         rc = SQLITE_ERROR;
  4951   5244       }else{
  4952         -      rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
         5245  +      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
  4953   5246         if( rc==SQLITE_OK ){
  4954   5247           if( pCsr->eocd.nEntry==0 ){
  4955   5248             pCsr->bEof = 1;
  4956   5249           }else{
  4957   5250             pCsr->iNextOff = pCsr->eocd.iOffset;
  4958   5251             rc = zipfileNext(cur);
  4959   5252           }
  4960   5253         }
  4961   5254       }
  4962   5255     }else{
  4963         -    ZipfileEntry e;
  4964         -    memset(&e, 0, sizeof(e));
  4965         -    e.pNext = pTab->pFirstEntry;
  4966         -    pCsr->pCurrent = &e;
         5256  +    pCsr->bNoop = 1;
         5257  +    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
  4967   5258       rc = zipfileNext(cur);
  4968         -    assert( pCsr->pCurrent!=&e );
  4969   5259     }
  4970   5260   
  4971   5261     return rc;
  4972   5262   }
  4973   5263   
  4974   5264   /*
  4975   5265   ** xBestIndex callback.
................................................................................
  4997   5287       pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
  4998   5288       pIdxInfo->idxNum = 0;
  4999   5289     }
  5000   5290   
  5001   5291     return SQLITE_OK;
  5002   5292   }
  5003   5293   
  5004         -/*
  5005         -** Add object pNew to the end of the linked list that begins at
  5006         -** ZipfileTab.pFirstEntry and ends with pLastEntry.
  5007         -*/
  5008         -static void zipfileAddEntry(
  5009         -  ZipfileTab *pTab, 
  5010         -  ZipfileEntry *pBefore, 
  5011         -  ZipfileEntry *pNew
  5012         -){
  5013         -  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
  5014         -  assert( pNew->pNext==0 );
  5015         -  if( pBefore==0 ){
  5016         -    if( pTab->pFirstEntry==0 ){
  5017         -      pTab->pFirstEntry = pTab->pLastEntry = pNew;
  5018         -    }else{
  5019         -      assert( pTab->pLastEntry->pNext==0 );
  5020         -      pTab->pLastEntry->pNext = pNew;
  5021         -      pTab->pLastEntry = pNew;
  5022         -    }
  5023         -  }else{
  5024         -    ZipfileEntry **pp;
  5025         -    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
  5026         -    pNew->pNext = pBefore;
  5027         -    *pp = pNew;
  5028         -  }
  5029         -}
  5030         -
  5031         -static int zipfileLoadDirectory(ZipfileTab *pTab){
  5032         -  ZipfileEOCD eocd;
  5033         -  int rc;
  5034         -
  5035         -  rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
  5036         -  if( rc==SQLITE_OK && eocd.nEntry>0 ){
  5037         -    int i;
  5038         -    int iOff = 0;
  5039         -    u8 *aBuf = sqlite3_malloc(eocd.nSize);
  5040         -    if( aBuf==0 ){
  5041         -      rc = SQLITE_NOMEM;
  5042         -    }else{
  5043         -      rc = zipfileReadData(
  5044         -          pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
  5045         -      );
  5046         -    }
  5047         -
  5048         -    for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
  5049         -      u16 nFile;
  5050         -      u16 nExtra;
  5051         -      u16 nComment;
  5052         -      ZipfileEntry *pNew;
  5053         -      u8 *aRec = &aBuf[iOff];
  5054         -
  5055         -      nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
  5056         -      nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
  5057         -      nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
  5058         -
  5059         -      pNew = sqlite3_malloc(
  5060         -          sizeof(ZipfileEntry) 
  5061         -        + nFile+1 
  5062         -        + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
  5063         -      );
  5064         -      if( pNew==0 ){
  5065         -        rc = SQLITE_NOMEM;
  5066         -      }else{
  5067         -        memset(pNew, 0, sizeof(ZipfileEntry));
  5068         -        pNew->zPath = (char*)&pNew[1];
  5069         -        memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
  5070         -        pNew->zPath[nFile] = '\0';
  5071         -        pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
  5072         -        pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  5073         -        memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
  5074         -        zipfileAddEntry(pTab, 0, pNew);
  5075         -      }
  5076         -
  5077         -      iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  5078         -    }
  5079         -
  5080         -    sqlite3_free(aBuf);
  5081         -  }
  5082         -
  5083         -  return rc;
  5084         -}
  5085         -
  5086         -static ZipfileEntry *zipfileNewEntry(
  5087         -  ZipfileCDS *pCds,               /* Values for fixed size part of CDS */
  5088         -  const char *zPath,              /* Path for new entry */
  5089         -  int nPath,                      /* strlen(zPath) */
  5090         -  u32 mTime                       /* Modification time (or 0) */
  5091         -){
  5092         -  u8 *aWrite;
         5294  +static ZipfileEntry *zipfileNewEntry(const char *zPath){
  5093   5295     ZipfileEntry *pNew;
  5094         -  pCds->nFile = (u16)nPath;
  5095         -  pCds->nExtra = mTime ? 9 : 0;
  5096         -  pNew = (ZipfileEntry*)sqlite3_malloc(
  5097         -    sizeof(ZipfileEntry) + 
  5098         -    nPath+1 + 
  5099         -    ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
  5100         -  );
  5101         -
         5296  +  pNew = sqlite3_malloc(sizeof(ZipfileEntry));
  5102   5297     if( pNew ){
  5103   5298       memset(pNew, 0, sizeof(ZipfileEntry));
  5104         -    pNew->zPath = (char*)&pNew[1];
  5105         -    pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
  5106         -    pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
  5107         -    memcpy(pNew->zPath, zPath, nPath+1);
  5108         -
  5109         -    aWrite = pNew->aCdsEntry;
  5110         -    zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
  5111         -    zipfileWrite16(aWrite, pCds->iVersionMadeBy);
  5112         -    zipfileWrite16(aWrite, pCds->iVersionExtract);
  5113         -    zipfileWrite16(aWrite, pCds->flags);
  5114         -    zipfileWrite16(aWrite, pCds->iCompression);
  5115         -    zipfileWrite16(aWrite, pCds->mTime);
  5116         -    zipfileWrite16(aWrite, pCds->mDate);
  5117         -    zipfileWrite32(aWrite, pCds->crc32);
  5118         -    zipfileWrite32(aWrite, pCds->szCompressed);
  5119         -    zipfileWrite32(aWrite, pCds->szUncompressed);
  5120         -    zipfileWrite16(aWrite, pCds->nFile);
  5121         -    zipfileWrite16(aWrite, pCds->nExtra);
  5122         -    zipfileWrite16(aWrite, pCds->nComment);      assert( pCds->nComment==0 );
  5123         -    zipfileWrite16(aWrite, pCds->iDiskStart);
  5124         -    zipfileWrite16(aWrite, pCds->iInternalAttr);
  5125         -    zipfileWrite32(aWrite, pCds->iExternalAttr);
  5126         -    zipfileWrite32(aWrite, pCds->iOffset);
  5127         -    assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
  5128         -    memcpy(aWrite, zPath, nPath);
  5129         -    if( pCds->nExtra ){
  5130         -      aWrite += nPath;
  5131         -      zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
  5132         -      zipfileWrite16(aWrite, 5);
  5133         -      *aWrite++ = 0x01;
  5134         -      zipfileWrite32(aWrite, mTime);
         5299  +    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
         5300  +    if( pNew->cds.zFile==0 ){
         5301  +      sqlite3_free(pNew);
         5302  +      pNew = 0;
  5135   5303       }
  5136   5304     }
  5137         -
  5138   5305     return pNew;
  5139   5306   }
         5307  +
         5308  +static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
         5309  +  ZipfileCDS *pCds = &pEntry->cds;
         5310  +  u8 *a = aBuf;
         5311  +
         5312  +  pCds->nExtra = 9;
         5313  +
         5314  +  /* Write the LFH itself */
         5315  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
         5316  +  zipfileWrite16(a, pCds->iVersionExtract);
         5317  +  zipfileWrite16(a, pCds->flags);
         5318  +  zipfileWrite16(a, pCds->iCompression);
         5319  +  zipfileWrite16(a, pCds->mTime);
         5320  +  zipfileWrite16(a, pCds->mDate);
         5321  +  zipfileWrite32(a, pCds->crc32);
         5322  +  zipfileWrite32(a, pCds->szCompressed);
         5323  +  zipfileWrite32(a, pCds->szUncompressed);
         5324  +  zipfileWrite16(a, (u16)pCds->nFile);
         5325  +  zipfileWrite16(a, pCds->nExtra);
         5326  +  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
         5327  +
         5328  +  /* Add the file name */
         5329  +  memcpy(a, pCds->zFile, (int)pCds->nFile);
         5330  +  a += (int)pCds->nFile;
         5331  +
         5332  +  /* The "extra" data */
         5333  +  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         5334  +  zipfileWrite16(a, 5);
         5335  +  *a++ = 0x01;
         5336  +  zipfileWrite32(a, pEntry->mUnixTime);
         5337  +
         5338  +  return a-aBuf;
         5339  +}
  5140   5340   
  5141   5341   static int zipfileAppendEntry(
  5142   5342     ZipfileTab *pTab,
  5143         -  ZipfileCDS *pCds,
  5144         -  const char *zPath,              /* Path for new entry */
  5145         -  int nPath,                      /* strlen(zPath) */
         5343  +  ZipfileEntry *pEntry,
  5146   5344     const u8 *pData,
  5147         -  int nData,
  5148         -  u32 mTime
         5345  +  int nData
  5149   5346   ){
  5150   5347     u8 *aBuf = pTab->aBuffer;
         5348  +  int nBuf;
  5151   5349     int rc;
  5152   5350   
  5153         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
  5154         -  zipfileWrite16(aBuf, pCds->iVersionExtract);
  5155         -  zipfileWrite16(aBuf, pCds->flags);
  5156         -  zipfileWrite16(aBuf, pCds->iCompression);
  5157         -  zipfileWrite16(aBuf, pCds->mTime);
  5158         -  zipfileWrite16(aBuf, pCds->mDate);
  5159         -  zipfileWrite32(aBuf, pCds->crc32);
  5160         -  zipfileWrite32(aBuf, pCds->szCompressed);
  5161         -  zipfileWrite32(aBuf, pCds->szUncompressed);
  5162         -  zipfileWrite16(aBuf, (u16)nPath);
  5163         -  zipfileWrite16(aBuf, pCds->nExtra);
  5164         -  assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
  5165         -  rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
         5351  +  nBuf = zipfileSerializeLFH(pEntry, aBuf);
         5352  +  rc = zipfileAppendData(pTab, aBuf, nBuf);
  5166   5353     if( rc==SQLITE_OK ){
  5167         -    rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
  5168         -  }
  5169         -
  5170         -  if( rc==SQLITE_OK && pCds->nExtra ){
  5171         -    aBuf = pTab->aBuffer;
  5172         -    zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
  5173         -    zipfileWrite16(aBuf, 5);
  5174         -    *aBuf++ = 0x01;
  5175         -    zipfileWrite32(aBuf, mTime);
  5176         -    rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
  5177         -  }
  5178         -
  5179         -  if( rc==SQLITE_OK ){
         5354  +    pEntry->iDataOff = pTab->szCurrent;
  5180   5355       rc = zipfileAppendData(pTab, pData, nData);
  5181   5356     }
  5182   5357   
  5183   5358     return rc;
  5184   5359   }
  5185   5360   
  5186   5361   static int zipfileGetMode(
  5187         -  ZipfileTab *pTab, 
  5188   5362     sqlite3_value *pVal, 
  5189         -  u32 defaultMode,                /* Value to use if pVal IS NULL */
  5190         -  u32 *pMode
         5363  +  int bIsDir,                     /* If true, default to directory */
         5364  +  u32 *pMode,                     /* OUT: Mode value */
         5365  +  char **pzErr                    /* OUT: Error message */
  5191   5366   ){
  5192   5367     const char *z = (const char*)sqlite3_value_text(pVal);
  5193   5368     u32 mode = 0;
  5194   5369     if( z==0 ){
  5195         -    mode = defaultMode;
         5370  +    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
  5196   5371     }else if( z[0]>='0' && z[0]<='9' ){
  5197   5372       mode = (unsigned int)sqlite3_value_int(pVal);
  5198   5373     }else{
  5199   5374       const char zTemplate[11] = "-rwxrwxrwx";
  5200   5375       int i;
  5201   5376       if( strlen(z)!=10 ) goto parse_error;
  5202   5377       switch( z[0] ){
  5203   5378         case '-': mode |= S_IFREG; break;
  5204   5379         case 'd': mode |= S_IFDIR; break;
  5205         -#if !defined(_WIN32) && !defined(WIN32)
  5206   5380         case 'l': mode |= S_IFLNK; break;
  5207         -#endif
  5208   5381         default: goto parse_error;
  5209   5382       }
  5210   5383       for(i=1; i<10; i++){
  5211   5384         if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
  5212   5385         else if( z[i]!='-' ) goto parse_error;
  5213   5386       }
  5214   5387     }
         5388  +  if( ((mode & S_IFDIR)==0)==bIsDir ){
         5389  +    /* The "mode" attribute is a directory, but data has been specified.
         5390  +    ** Or vice-versa - no data but "mode" is a file or symlink.  */
         5391  +    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
         5392  +    return SQLITE_CONSTRAINT;
         5393  +  }
  5215   5394     *pMode = mode;
  5216   5395     return SQLITE_OK;
  5217   5396   
  5218   5397    parse_error:
  5219         -  pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
         5398  +  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
  5220   5399     return SQLITE_ERROR;
  5221   5400   }
  5222   5401   
  5223   5402   /*
  5224   5403   ** Both (const char*) arguments point to nul-terminated strings. Argument
  5225   5404   ** nB is the value of strlen(zB). This function returns 0 if the strings are
  5226   5405   ** identical, ignoring any trailing '/' character in either path.  */
................................................................................
  5227   5406   static int zipfileComparePath(const char *zA, const char *zB, int nB){
  5228   5407     int nA = (int)strlen(zA);
  5229   5408     if( zA[nA-1]=='/' ) nA--;
  5230   5409     if( zB[nB-1]=='/' ) nB--;
  5231   5410     if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
  5232   5411     return 1;
  5233   5412   }
         5413  +
         5414  +static int zipfileBegin(sqlite3_vtab *pVtab){
         5415  +  ZipfileTab *pTab = (ZipfileTab*)pVtab;
         5416  +  int rc = SQLITE_OK;
         5417  +
         5418  +  assert( pTab->pWriteFd==0 );
         5419  +
         5420  +  /* Open a write fd on the file. Also load the entire central directory
         5421  +  ** structure into memory. During the transaction any new file data is 
         5422  +  ** appended to the archive file, but the central directory is accumulated
         5423  +  ** in main-memory until the transaction is committed.  */
         5424  +  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
         5425  +  if( pTab->pWriteFd==0 ){
         5426  +    pTab->base.zErrMsg = sqlite3_mprintf(
         5427  +        "zipfile: failed to open file %s for writing", pTab->zFile
         5428  +        );
         5429  +    rc = SQLITE_ERROR;
         5430  +  }else{
         5431  +    fseek(pTab->pWriteFd, 0, SEEK_END);
         5432  +    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
         5433  +    rc = zipfileLoadDirectory(pTab, 0, 0);
         5434  +  }
         5435  +
         5436  +  if( rc!=SQLITE_OK ){
         5437  +    zipfileCleanupTransaction(pTab);
         5438  +  }
         5439  +
         5440  +  return rc;
         5441  +}
         5442  +
         5443  +/*
         5444  +** Return the current time as a 32-bit timestamp in UNIX epoch format (like
         5445  +** time(2)).
         5446  +*/
         5447  +static u32 zipfileTime(void){
         5448  +  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
         5449  +  u32 ret;
         5450  +  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
         5451  +    i64 ms;
         5452  +    pVfs->xCurrentTimeInt64(pVfs, &ms);
         5453  +    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
         5454  +  }else{
         5455  +    double day;
         5456  +    pVfs->xCurrentTime(pVfs, &day);
         5457  +    ret = (u32)((day - 2440587.5) * 86400);
         5458  +  }
         5459  +  return ret;
         5460  +}
         5461  +
         5462  +/*
         5463  +** Return a 32-bit timestamp in UNIX epoch format.
         5464  +**
         5465  +** If the value passed as the only argument is either NULL or an SQL NULL,
         5466  +** return the current time. Otherwise, return the value stored in (*pVal)
         5467  +** cast to a 32-bit unsigned integer.
         5468  +*/
         5469  +static u32 zipfileGetTime(sqlite3_value *pVal){
         5470  +  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
         5471  +    return zipfileTime();
         5472  +  }
         5473  +  return (u32)sqlite3_value_int64(pVal);
         5474  +}
  5234   5475   
  5235   5476   /*
  5236   5477   ** xUpdate method.
  5237   5478   */
  5238   5479   static int zipfileUpdate(
  5239   5480     sqlite3_vtab *pVtab, 
  5240   5481     int nVal, 
................................................................................
  5242   5483     sqlite_int64 *pRowid
  5243   5484   ){
  5244   5485     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5245   5486     int rc = SQLITE_OK;             /* Return Code */
  5246   5487     ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
  5247   5488   
  5248   5489     u32 mode = 0;                   /* Mode for new entry */
  5249         -  i64 mTime = 0;                  /* Modification time for new entry */
         5490  +  u32 mTime = 0;                  /* Modification time for new entry */
  5250   5491     i64 sz = 0;                     /* Uncompressed size */
  5251   5492     const char *zPath = 0;          /* Path for new entry */
  5252   5493     int nPath = 0;                  /* strlen(zPath) */
  5253   5494     const u8 *pData = 0;            /* Pointer to buffer containing content */
  5254   5495     int nData = 0;                  /* Size of pData buffer in bytes */
  5255   5496     int iMethod = 0;                /* Compression method for new entry */
  5256   5497     u8 *pFree = 0;                  /* Free this */
  5257   5498     char *zFree = 0;                /* Also free this */
  5258         -  ZipfileCDS cds;                 /* New Central Directory Structure entry */
  5259   5499     ZipfileEntry *pOld = 0;
  5260   5500     int bIsDir = 0;
  5261   5501     u32 iCrc32 = 0;
  5262   5502   
  5263         -  assert( pTab->zFile );
  5264         -  assert( pTab->pWriteFd );
         5503  +  if( pTab->pWriteFd==0 ){
         5504  +    rc = zipfileBegin(pVtab);
         5505  +    if( rc!=SQLITE_OK ) return rc;
         5506  +  }
  5265   5507   
         5508  +  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
  5266   5509     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  5267   5510       const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
  5268   5511       int nDelete = (int)strlen(zDelete);
  5269   5512       for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
  5270         -      if( pOld->bDeleted ) continue;
  5271         -      if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
  5272         -        pOld->bDeleted = 1;
         5513  +      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
  5273   5514           break;
  5274   5515         }
  5275   5516         assert( pOld->pNext );
  5276   5517       }
  5277         -    if( nVal==1 ) return SQLITE_OK;
  5278         -  }
  5279         -
  5280         -  /* Check that "sz" and "rawdata" are both NULL: */
  5281         -  if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
  5282         -   || sqlite3_value_type(apVal[6])!=SQLITE_NULL
  5283         -  ){
  5284         -    rc = SQLITE_CONSTRAINT;
  5285         -  }
  5286         -
  5287         -  if( rc==SQLITE_OK ){
  5288         -    if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
  5289         -      /* data=NULL. A directory */
  5290         -      bIsDir = 1;
  5291         -    }else{
  5292         -      /* Value specified for "data", and possibly "method". This must be
  5293         -      ** a regular file or a symlink. */
  5294         -      const u8 *aIn = sqlite3_value_blob(apVal[7]);
  5295         -      int nIn = sqlite3_value_bytes(apVal[7]);
  5296         -      int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
  5297         -
  5298         -      iMethod = sqlite3_value_int(apVal[8]);
  5299         -      sz = nIn;
  5300         -      pData = aIn;
  5301         -      nData = nIn;
  5302         -      if( iMethod!=0 && iMethod!=8 ){
  5303         -        rc = SQLITE_CONSTRAINT;
  5304         -      }else{
  5305         -        if( bAuto || iMethod ){
  5306         -          int nCmp;
  5307         -          rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
  5308         -          if( rc==SQLITE_OK ){
  5309         -            if( iMethod || nCmp<nIn ){
  5310         -              iMethod = 8;
  5311         -              pData = pFree;
  5312         -              nData = nCmp;
  5313         -            }
  5314         -          }
  5315         -        }
  5316         -        iCrc32 = crc32(0, aIn, nIn);
  5317         -      }
  5318         -    }
  5319         -  }
  5320         -
  5321         -  if( rc==SQLITE_OK ){
  5322         -    rc = zipfileGetMode(pTab, apVal[3], 
  5323         -        (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
  5324         -    );
  5325         -    if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
  5326         -      /* The "mode" attribute is a directory, but data has been specified.
  5327         -      ** Or vice-versa - no data but "mode" is a file or symlink.  */
         5518  +  }
         5519  +
         5520  +  if( nVal>1 ){
         5521  +    /* Check that "sz" and "rawdata" are both NULL: */
         5522  +    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
         5523  +      zipfileTableErr(pTab, "sz must be NULL");
         5524  +      rc = SQLITE_CONSTRAINT;
         5525  +    }
         5526  +    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
         5527  +      zipfileTableErr(pTab, "rawdata must be NULL"); 
  5328   5528         rc = SQLITE_CONSTRAINT;
  5329   5529       }
  5330         -  }
  5331         -
  5332         -  if( rc==SQLITE_OK ){
  5333         -    zPath = (const char*)sqlite3_value_text(apVal[2]);
  5334         -    nPath = (int)strlen(zPath);
  5335         -    if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
  5336         -      mTime = (sqlite3_int64)time(0);
  5337         -    }else{
  5338         -      mTime = sqlite3_value_int64(apVal[4]);
  5339         -    }
  5340         -  }
  5341         -
  5342         -  if( rc==SQLITE_OK && bIsDir ){
  5343         -    /* For a directory, check that the last character in the path is a
  5344         -    ** '/'. This appears to be required for compatibility with info-zip
  5345         -    ** (the unzip command on unix). It does not create directories
  5346         -    ** otherwise.  */
  5347         -    if( zPath[nPath-1]!='/' ){
  5348         -      zFree = sqlite3_mprintf("%s/", zPath);
  5349         -      if( zFree==0 ){ rc = SQLITE_NOMEM; }
  5350         -      zPath = (const char*)zFree;
  5351         -      nPath++;
  5352         -    }
  5353         -  }
  5354         -
  5355         -  /* Check that we're not inserting a duplicate entry */
  5356         -  if( rc==SQLITE_OK ){
  5357         -    ZipfileEntry *p;
  5358         -    for(p=pTab->pFirstEntry; p; p=p->pNext){
  5359         -      if( p->bDeleted ) continue;
  5360         -      if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
  5361         -        rc = SQLITE_CONSTRAINT;
  5362         -        break;
  5363         -      }
  5364         -    }
  5365         -  }
  5366         -
  5367         -  if( rc==SQLITE_OK ){
  5368         -    /* Create the new CDS record. */
  5369         -    memset(&cds, 0, sizeof(cds));
  5370         -    cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
  5371         -    cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
  5372         -    cds.flags = ZIPFILE_NEWENTRY_FLAGS;
  5373         -    cds.iCompression = (u16)iMethod;
  5374         -    zipfileMtimeToDos(&cds, (u32)mTime);
  5375         -    cds.crc32 = iCrc32;
  5376         -    cds.szCompressed = nData;
  5377         -    cds.szUncompressed = (u32)sz;
  5378         -    cds.iExternalAttr = (mode<<16);
  5379         -    cds.iOffset = (u32)pTab->szCurrent;
  5380         -    pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
  5381         -    if( pNew==0 ){
  5382         -      rc = SQLITE_NOMEM;
  5383         -    }else{
  5384         -      zipfileAddEntry(pTab, pOld, pNew);
  5385         -    }
  5386         -  }
  5387         -
  5388         -  /* Append the new header+file to the archive */
  5389         -  if( rc==SQLITE_OK ){
  5390         -    rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
  5391         -  }
  5392         -
  5393         -  if( rc!=SQLITE_OK && pOld ){
  5394         -    pOld->bDeleted = 0;
  5395         -  }
         5530  +
         5531  +    if( rc==SQLITE_OK ){
         5532  +      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
         5533  +        /* data=NULL. A directory */
         5534  +        bIsDir = 1;
         5535  +      }else{
         5536  +        /* Value specified for "data", and possibly "method". This must be
         5537  +        ** a regular file or a symlink. */
         5538  +        const u8 *aIn = sqlite3_value_blob(apVal[7]);
         5539  +        int nIn = sqlite3_value_bytes(apVal[7]);
         5540  +        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
         5541  +
         5542  +        iMethod = sqlite3_value_int(apVal[8]);
         5543  +        sz = nIn;
         5544  +        pData = aIn;
         5545  +        nData = nIn;
         5546  +        if( iMethod!=0 && iMethod!=8 ){
         5547  +          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
         5548  +          rc = SQLITE_CONSTRAINT;
         5549  +        }else{
         5550  +          if( bAuto || iMethod ){
         5551  +            int nCmp;
         5552  +            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
         5553  +            if( rc==SQLITE_OK ){
         5554  +              if( iMethod || nCmp<nIn ){
         5555  +                iMethod = 8;
         5556  +                pData = pFree;
         5557  +                nData = nCmp;
         5558  +              }
         5559  +            }
         5560  +          }
         5561  +          iCrc32 = crc32(0, aIn, nIn);
         5562  +        }
         5563  +      }
         5564  +    }
         5565  +
         5566  +    if( rc==SQLITE_OK ){
         5567  +      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
         5568  +    }
         5569  +
         5570  +    if( rc==SQLITE_OK ){
         5571  +      zPath = (const char*)sqlite3_value_text(apVal[2]);
         5572  +      nPath = (int)strlen(zPath);
         5573  +      mTime = zipfileGetTime(apVal[4]);
         5574  +    }
         5575  +
         5576  +    if( rc==SQLITE_OK && bIsDir ){
         5577  +      /* For a directory, check that the last character in the path is a
         5578  +      ** '/'. This appears to be required for compatibility with info-zip
         5579  +      ** (the unzip command on unix). It does not create directories
         5580  +      ** otherwise.  */
         5581  +      if( zPath[nPath-1]!='/' ){
         5582  +        zFree = sqlite3_mprintf("%s/", zPath);
         5583  +        if( zFree==0 ){ rc = SQLITE_NOMEM; }
         5584  +        zPath = (const char*)zFree;
         5585  +        nPath++;
         5586  +      }
         5587  +    }
         5588  +
         5589  +    /* Check that we're not inserting a duplicate entry */
         5590  +    if( pOld==0 && rc==SQLITE_OK ){
         5591  +      ZipfileEntry *p;
         5592  +      for(p=pTab->pFirstEntry; p; p=p->pNext){
         5593  +        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
         5594  +          switch( sqlite3_vtab_on_conflict(pTab->db) ){
         5595  +            case SQLITE_IGNORE: {
         5596  +              goto zipfile_update_done;
         5597  +            }
         5598  +            case SQLITE_REPLACE: {
         5599  +              pOld = p;
         5600  +              break;
         5601  +            }
         5602  +            default: {
         5603  +              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
         5604  +              rc = SQLITE_CONSTRAINT;
         5605  +              break;
         5606  +            }
         5607  +          }
         5608  +          break;
         5609  +        }
         5610  +      }
         5611  +    }
         5612  +
         5613  +    if( rc==SQLITE_OK ){
         5614  +      /* Create the new CDS record. */
         5615  +      pNew = zipfileNewEntry(zPath);
         5616  +      if( pNew==0 ){
         5617  +        rc = SQLITE_NOMEM;
         5618  +      }else{
         5619  +        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         5620  +        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         5621  +        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         5622  +        pNew->cds.iCompression = (u16)iMethod;
         5623  +        zipfileMtimeToDos(&pNew->cds, mTime);
         5624  +        pNew->cds.crc32 = iCrc32;
         5625  +        pNew->cds.szCompressed = nData;
         5626  +        pNew->cds.szUncompressed = (u32)sz;
         5627  +        pNew->cds.iExternalAttr = (mode<<16);
         5628  +        pNew->cds.iOffset = (u32)pTab->szCurrent;
         5629  +        pNew->cds.nFile = (u16)nPath;
         5630  +        pNew->mUnixTime = (u32)mTime;
         5631  +        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
         5632  +        zipfileAddEntry(pTab, pOld, pNew);
         5633  +      }
         5634  +    }
         5635  +  }
         5636  +
         5637  +  if( rc==SQLITE_OK && pOld ){
         5638  +    ZipfileEntry **pp;
         5639  +    ZipfileCsr *pCsr;
         5640  +    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
         5641  +      if( pCsr->pCurrent==pOld ){
         5642  +        pCsr->pCurrent = pOld->pNext;
         5643  +        pCsr->bNoop = 1;
         5644  +      }
         5645  +    }
         5646  +    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
         5647  +    *pp = (*pp)->pNext;
         5648  +    zipfileEntryFree(pOld);
         5649  +  }
         5650  +
         5651  +zipfile_update_done:
  5396   5652     sqlite3_free(pFree);
  5397   5653     sqlite3_free(zFree);
  5398   5654     return rc;
  5399   5655   }
         5656  +
         5657  +static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
         5658  +  u8 *a = aBuf;
         5659  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
         5660  +  zipfileWrite16(a, p->iDisk);
         5661  +  zipfileWrite16(a, p->iFirstDisk);
         5662  +  zipfileWrite16(a, p->nEntry);
         5663  +  zipfileWrite16(a, p->nEntryTotal);
         5664  +  zipfileWrite32(a, p->nSize);
         5665  +  zipfileWrite32(a, p->iOffset);
         5666  +  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
         5667  +
         5668  +  return a-aBuf;
         5669  +}
  5400   5670   
  5401   5671   static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
  5402         -  u8 *aBuf = pTab->aBuffer;
  5403         -
  5404         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
  5405         -  zipfileWrite16(aBuf, p->iDisk);
  5406         -  zipfileWrite16(aBuf, p->iFirstDisk);
  5407         -  zipfileWrite16(aBuf, p->nEntry);
  5408         -  zipfileWrite16(aBuf, p->nEntryTotal);
  5409         -  zipfileWrite32(aBuf, p->nSize);
  5410         -  zipfileWrite32(aBuf, p->iOffset);
  5411         -  zipfileWrite16(aBuf, 0);        /* Size of trailing comment in bytes*/
  5412         -
  5413         -  assert( (aBuf-pTab->aBuffer)==22 );
  5414         -  return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
  5415         -}
  5416         -
  5417         -static void zipfileCleanupTransaction(ZipfileTab *pTab){
  5418         -  ZipfileEntry *pEntry;
  5419         -  ZipfileEntry *pNext;
  5420         -
  5421         -  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
  5422         -    pNext = pEntry->pNext;
  5423         -    sqlite3_free(pEntry);
  5424         -  }
  5425         -  pTab->pFirstEntry = 0;
  5426         -  pTab->pLastEntry = 0;
  5427         -  fclose(pTab->pWriteFd);
  5428         -  pTab->pWriteFd = 0;
  5429         -  pTab->szCurrent = 0;
  5430         -  pTab->szOrig = 0;
  5431         -}
  5432         -
  5433         -static int zipfileBegin(sqlite3_vtab *pVtab){
  5434         -  ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5435         -  int rc = SQLITE_OK;
  5436         -
  5437         -  assert( pTab->pWriteFd==0 );
  5438         -
  5439         -  /* This table is only writable if a default archive path was specified 
  5440         -  ** as part of the CREATE VIRTUAL TABLE statement. */
  5441         -  if( pTab->zFile==0 ){
  5442         -    pTab->base.zErrMsg = sqlite3_mprintf(
  5443         -        "zipfile: writing requires a default archive"
  5444         -    );
  5445         -    return SQLITE_ERROR;
  5446         -  }
  5447         -
  5448         -  /* Open a write fd on the file. Also load the entire central directory
  5449         -  ** structure into memory. During the transaction any new file data is 
  5450         -  ** appended to the archive file, but the central directory is accumulated
  5451         -  ** in main-memory until the transaction is committed.  */
  5452         -  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
  5453         -  if( pTab->pWriteFd==0 ){
  5454         -    pTab->base.zErrMsg = sqlite3_mprintf(
  5455         -        "zipfile: failed to open file %s for writing", pTab->zFile
  5456         -    );
  5457         -    rc = SQLITE_ERROR;
  5458         -  }else{
  5459         -    fseek(pTab->pWriteFd, 0, SEEK_END);
  5460         -    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
  5461         -    rc = zipfileLoadDirectory(pTab);
  5462         -  }
  5463         -
  5464         -  if( rc!=SQLITE_OK ){
  5465         -    zipfileCleanupTransaction(pTab);
  5466         -  }
  5467         -
  5468         -  return rc;
         5672  +  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
         5673  +  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
         5674  +  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
         5675  +}
         5676  +
         5677  +/*
         5678  +** Serialize the CDS structure into buffer aBuf[]. Return the number
         5679  +** of bytes written.
         5680  +*/
         5681  +static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
         5682  +  u8 *a = aBuf;
         5683  +  ZipfileCDS *pCDS = &pEntry->cds;
         5684  +
         5685  +  if( pEntry->aExtra==0 ){
         5686  +    pCDS->nExtra = 9;
         5687  +  }
         5688  +
         5689  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
         5690  +  zipfileWrite16(a, pCDS->iVersionMadeBy);
         5691  +  zipfileWrite16(a, pCDS->iVersionExtract);
         5692  +  zipfileWrite16(a, pCDS->flags);
         5693  +  zipfileWrite16(a, pCDS->iCompression);
         5694  +  zipfileWrite16(a, pCDS->mTime);
         5695  +  zipfileWrite16(a, pCDS->mDate);
         5696  +  zipfileWrite32(a, pCDS->crc32);
         5697  +  zipfileWrite32(a, pCDS->szCompressed);
         5698  +  zipfileWrite32(a, pCDS->szUncompressed);
         5699  +  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
         5700  +  zipfileWrite16(a, pCDS->nFile);
         5701  +  zipfileWrite16(a, pCDS->nExtra);
         5702  +  zipfileWrite16(a, pCDS->nComment);
         5703  +  zipfileWrite16(a, pCDS->iDiskStart);
         5704  +  zipfileWrite16(a, pCDS->iInternalAttr);
         5705  +  zipfileWrite32(a, pCDS->iExternalAttr);
         5706  +  zipfileWrite32(a, pCDS->iOffset);
         5707  +
         5708  +  memcpy(a, pCDS->zFile, pCDS->nFile);
         5709  +  a += pCDS->nFile;
         5710  +
         5711  +  if( pEntry->aExtra ){
         5712  +    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
         5713  +    memcpy(a, pEntry->aExtra, n);
         5714  +    a += n;
         5715  +  }else{
         5716  +    assert( pCDS->nExtra==9 );
         5717  +    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         5718  +    zipfileWrite16(a, 5);
         5719  +    *a++ = 0x01;
         5720  +    zipfileWrite32(a, pEntry->mUnixTime);
         5721  +  }
         5722  +
         5723  +  return a-aBuf;
  5469   5724   }
  5470   5725   
  5471   5726   static int zipfileCommit(sqlite3_vtab *pVtab){
  5472   5727     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5473   5728     int rc = SQLITE_OK;
  5474   5729     if( pTab->pWriteFd ){
  5475   5730       i64 iOffset = pTab->szCurrent;
  5476   5731       ZipfileEntry *p;
  5477   5732       ZipfileEOCD eocd;
  5478   5733       int nEntry = 0;
  5479   5734   
  5480         -    /* Write out all undeleted entries */
         5735  +    /* Write out all entries */
  5481   5736       for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
  5482         -      if( p->bDeleted ) continue;
  5483         -      rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
         5737  +      int n = zipfileSerializeCDS(p, pTab->aBuffer);
         5738  +      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
  5484   5739         nEntry++;
  5485   5740       }
  5486   5741   
  5487   5742       /* Write out the EOCD record */
  5488   5743       eocd.iDisk = 0;
  5489   5744       eocd.iFirstDisk = 0;
  5490   5745       eocd.nEntry = (u16)nEntry;
................................................................................
  5517   5772   ){
  5518   5773     ZipfileCsr *pCsr;
  5519   5774     ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
  5520   5775     assert( argc>0 );
  5521   5776   
  5522   5777     pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
  5523   5778     if( pCsr ){
  5524         -    ZipfileCDS *p = &pCsr->cds;
         5779  +    ZipfileCDS *p = &pCsr->pCurrent->cds;
  5525   5780       char *zRes = sqlite3_mprintf("{"
  5526   5781           "\"version-made-by\" : %u, "
  5527   5782           "\"version-to-extract\" : %u, "
  5528   5783           "\"flags\" : %u, "
  5529   5784           "\"compression\" : %u, "
  5530   5785           "\"time\" : %u, "
  5531   5786           "\"date\" : %u, "
................................................................................
  5554   5809       }else{
  5555   5810         sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
  5556   5811         sqlite3_free(zRes);
  5557   5812       }
  5558   5813     }
  5559   5814   }
  5560   5815   
  5561         -
  5562   5816   /*
  5563   5817   ** xFindFunction method.
  5564   5818   */
  5565   5819   static int zipfileFindFunction(
  5566   5820     sqlite3_vtab *pVtab,            /* Virtual table handle */
  5567   5821     int nArg,                       /* Number of SQL function arguments */
  5568   5822     const char *zName,              /* Name of SQL function */
  5569   5823     void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
  5570   5824     void **ppArg                    /* OUT: User data for *pxFunc */
  5571   5825   ){
  5572         -  if( nArg>0 ){
  5573         -    if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
  5574         -      *pxFunc = zipfileFunctionCds;
  5575         -      *ppArg = (void*)pVtab;
  5576         -      return 1;
  5577         -    }
  5578         -  }
  5579         -
  5580         -  return 0;
  5581         -}
         5826  +  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
         5827  +    *pxFunc = zipfileFunctionCds;
         5828  +    *ppArg = (void*)pVtab;
         5829  +    return 1;
         5830  +  }
         5831  +  return 0;
         5832  +}
         5833  +
         5834  +typedef struct ZipfileBuffer ZipfileBuffer;
         5835  +struct ZipfileBuffer {
         5836  +  u8 *a;                          /* Pointer to buffer */
         5837  +  int n;                          /* Size of buffer in bytes */
         5838  +  int nAlloc;                     /* Byte allocated at a[] */
         5839  +};
         5840  +
         5841  +typedef struct ZipfileCtx ZipfileCtx;
         5842  +struct ZipfileCtx {
         5843  +  int nEntry;
         5844  +  ZipfileBuffer body;
         5845  +  ZipfileBuffer cds;
         5846  +};
         5847  +
         5848  +static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
         5849  +  if( pBuf->n+nByte>pBuf->nAlloc ){
         5850  +    u8 *aNew;
         5851  +    int nNew = pBuf->n ? pBuf->n*2 : 512;
         5852  +    int nReq = pBuf->n + nByte;
         5853  +
         5854  +    while( nNew<nReq ) nNew = nNew*2;
         5855  +    aNew = sqlite3_realloc(pBuf->a, nNew);
         5856  +    if( aNew==0 ) return SQLITE_NOMEM;
         5857  +    pBuf->a = aNew;
         5858  +    pBuf->nAlloc = nNew;
         5859  +  }
         5860  +  return SQLITE_OK;
         5861  +}
         5862  +
         5863  +/*
         5864  +** xStep() callback for the zipfile() aggregate. This can be called in
         5865  +** any of the following ways:
         5866  +**
         5867  +**   SELECT zipfile(name,data) ...
         5868  +**   SELECT zipfile(name,mode,mtime,data) ...
         5869  +**   SELECT zipfile(name,mode,mtime,data,method) ...
         5870  +*/
         5871  +void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
         5872  +  ZipfileCtx *p;                  /* Aggregate function context */
         5873  +  ZipfileEntry e;                 /* New entry to add to zip archive */
         5874  +
         5875  +  sqlite3_value *pName = 0;
         5876  +  sqlite3_value *pMode = 0;
         5877  +  sqlite3_value *pMtime = 0;
         5878  +  sqlite3_value *pData = 0;
         5879  +  sqlite3_value *pMethod = 0;
         5880  +
         5881  +  int bIsDir = 0;
         5882  +  u32 mode;
         5883  +  int rc = SQLITE_OK;
         5884  +  char *zErr = 0;
         5885  +
         5886  +  int iMethod = -1;               /* Compression method to use (0 or 8) */
         5887  +
         5888  +  const u8 *aData = 0;            /* Possibly compressed data for new entry */
         5889  +  int nData = 0;                  /* Size of aData[] in bytes */
         5890  +  int szUncompressed = 0;         /* Size of data before compression */
         5891  +  u8 *aFree = 0;                  /* Free this before returning */
         5892  +  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
         5893  +
         5894  +  char *zName = 0;                /* Path (name) of new entry */
         5895  +  int nName = 0;                  /* Size of zName in bytes */
         5896  +  char *zFree = 0;                /* Free this before returning */
         5897  +  int nByte;
         5898  +
         5899  +  memset(&e, 0, sizeof(e));
         5900  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         5901  +  if( p==0 ) return;
         5902  +
         5903  +  /* Martial the arguments into stack variables */
         5904  +  if( nVal!=2 && nVal!=4 && nVal!=5 ){
         5905  +    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
         5906  +    rc = SQLITE_ERROR;
         5907  +    goto zipfile_step_out;
         5908  +  }
         5909  +  pName = apVal[0];
         5910  +  if( nVal==2 ){
         5911  +    pData = apVal[1];
         5912  +  }else{
         5913  +    pMode = apVal[1];
         5914  +    pMtime = apVal[2];
         5915  +    pData = apVal[3];
         5916  +    if( nVal==5 ){
         5917  +      pMethod = apVal[4];
         5918  +    }
         5919  +  }
         5920  +
         5921  +  /* Check that the 'name' parameter looks ok. */
         5922  +  zName = (char*)sqlite3_value_text(pName);
         5923  +  nName = sqlite3_value_bytes(pName);
         5924  +  if( zName==0 ){
         5925  +    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
         5926  +    rc = SQLITE_ERROR;
         5927  +    goto zipfile_step_out;
         5928  +  }
         5929  +
         5930  +  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
         5931  +  ** deflate compression) or NULL (choose automatically).  */
         5932  +  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
         5933  +    iMethod = (int)sqlite3_value_int64(pMethod);
         5934  +    if( iMethod!=0 && iMethod!=8 ){
         5935  +      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
         5936  +      rc = SQLITE_ERROR;
         5937  +      goto zipfile_step_out;
         5938  +    }
         5939  +  }
         5940  +
         5941  +  /* Now inspect the data. If this is NULL, then the new entry must be a
         5942  +  ** directory.  Otherwise, figure out whether or not the data should
         5943  +  ** be deflated or simply stored in the zip archive. */
         5944  +  if( sqlite3_value_type(pData)==SQLITE_NULL ){
         5945  +    bIsDir = 1;
         5946  +    iMethod = 0;
         5947  +  }else{
         5948  +    aData = sqlite3_value_blob(pData);
         5949  +    szUncompressed = nData = sqlite3_value_bytes(pData);
         5950  +    iCrc32 = crc32(0, aData, nData);
         5951  +    if( iMethod<0 || iMethod==8 ){
         5952  +      int nOut = 0;
         5953  +      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
         5954  +      if( rc!=SQLITE_OK ){
         5955  +        goto zipfile_step_out;
         5956  +      }
         5957  +      if( iMethod==8 || nOut<nData ){
         5958  +        aData = aFree;
         5959  +        nData = nOut;
         5960  +        iMethod = 8;
         5961  +      }else{
         5962  +        iMethod = 0;
         5963  +      }
         5964  +    }
         5965  +  }
         5966  +
         5967  +  /* Decode the "mode" argument. */
         5968  +  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
         5969  +  if( rc ) goto zipfile_step_out;
         5970  +
         5971  +  /* Decode the "mtime" argument. */
         5972  +  e.mUnixTime = zipfileGetTime(pMtime);
         5973  +
         5974  +  /* If this is a directory entry, ensure that there is exactly one '/'
         5975  +  ** at the end of the path. Or, if this is not a directory and the path
         5976  +  ** ends in '/' it is an error. */
         5977  +  if( bIsDir==0 ){
         5978  +    if( zName[nName-1]=='/' ){
         5979  +      zErr = sqlite3_mprintf("non-directory name must not end with /");
         5980  +      rc = SQLITE_ERROR;
         5981  +      goto zipfile_step_out;
         5982  +    }
         5983  +  }else{
         5984  +    if( zName[nName-1]!='/' ){
         5985  +      zName = zFree = sqlite3_mprintf("%s/", zName);
         5986  +      nName++;
         5987  +      if( zName==0 ){
         5988  +        rc = SQLITE_NOMEM;
         5989  +        goto zipfile_step_out;
         5990  +      }
         5991  +    }else{
         5992  +      while( nName>1 && zName[nName-2]=='/' ) nName--;
         5993  +    }
         5994  +  }
         5995  +
         5996  +  /* Assemble the ZipfileEntry object for the new zip archive entry */
         5997  +  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         5998  +  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         5999  +  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         6000  +  e.cds.iCompression = (u16)iMethod;
         6001  +  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
         6002  +  e.cds.crc32 = iCrc32;
         6003  +  e.cds.szCompressed = nData;
         6004  +  e.cds.szUncompressed = szUncompressed;
         6005  +  e.cds.iExternalAttr = (mode<<16);
         6006  +  e.cds.iOffset = p->body.n;
         6007  +  e.cds.nFile = (u16)nName;
         6008  +  e.cds.zFile = zName;
         6009  +
         6010  +  /* Append the LFH to the body of the new archive */
         6011  +  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
         6012  +  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
         6013  +  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
         6014  +
         6015  +  /* Append the data to the body of the new archive */
         6016  +  if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
         6017  +  memcpy(&p->body.a[p->body.n], aData, nData);
         6018  +  p->body.n += nData;
         6019  +
         6020  +  /* Append the CDS record to the directory of the new archive */
         6021  +  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
         6022  +  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
         6023  +  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
         6024  +
         6025  +  /* Increment the count of entries in the archive */
         6026  +  p->nEntry++;
         6027  +
         6028  + zipfile_step_out:
         6029  +  sqlite3_free(aFree);
         6030  +  sqlite3_free(zFree);
         6031  +  if( rc ){
         6032  +    if( zErr ){
         6033  +      sqlite3_result_error(pCtx, zErr, -1);
         6034  +    }else{
         6035  +      sqlite3_result_error_code(pCtx, rc);
         6036  +    }
         6037  +  }
         6038  +  sqlite3_free(zErr);
         6039  +}
         6040  +
         6041  +/*
         6042  +** xFinalize() callback for zipfile aggregate function.
         6043  +*/
         6044  +void zipfileFinal(sqlite3_context *pCtx){
         6045  +  ZipfileCtx *p;
         6046  +  ZipfileEOCD eocd;
         6047  +  int nZip;
         6048  +  u8 *aZip;
         6049  +
         6050  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         6051  +  if( p==0 ) return;
         6052  +  if( p->nEntry>0 ){
         6053  +    memset(&eocd, 0, sizeof(eocd));
         6054  +    eocd.nEntry = (u16)p->nEntry;
         6055  +    eocd.nEntryTotal = (u16)p->nEntry;
         6056  +    eocd.nSize = p->cds.n;
         6057  +    eocd.iOffset = p->body.n;
         6058  +
         6059  +    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
         6060  +    aZip = (u8*)sqlite3_malloc(nZip);
         6061  +    if( aZip==0 ){
         6062  +      sqlite3_result_error_nomem(pCtx);
         6063  +    }else{
         6064  +      memcpy(aZip, p->body.a, p->body.n);
         6065  +      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
         6066  +      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
         6067  +      sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
         6068  +    }
         6069  +  }
         6070  +
         6071  +  sqlite3_free(p->body.a);
         6072  +  sqlite3_free(p->cds.a);
         6073  +}
         6074  +
  5582   6075   
  5583   6076   /*
  5584   6077   ** Register the "zipfile" virtual table.
  5585   6078   */
  5586   6079   static int zipfileRegister(sqlite3 *db){
  5587   6080     static sqlite3_module zipfileModule = {
  5588   6081       1,                         /* iVersion */
................................................................................
  5593   6086       zipfileDisconnect,         /* xDestroy */
  5594   6087       zipfileOpen,               /* xOpen - open a cursor */
  5595   6088       zipfileClose,              /* xClose - close a cursor */
  5596   6089       zipfileFilter,             /* xFilter - configure scan constraints */
  5597   6090       zipfileNext,               /* xNext - advance a cursor */
  5598   6091       zipfileEof,                /* xEof - check for end of scan */
  5599   6092       zipfileColumn,             /* xColumn - read data */
  5600         -    zipfileRowid,              /* xRowid - read data */
         6093  +    0,                         /* xRowid - read data */
  5601   6094       zipfileUpdate,             /* xUpdate */
  5602   6095       zipfileBegin,              /* xBegin */
  5603   6096       0,                         /* xSync */
  5604   6097       zipfileCommit,             /* xCommit */
  5605   6098       zipfileRollback,           /* xRollback */
  5606   6099       zipfileFindFunction,       /* xFindMethod */
  5607   6100       0,                         /* xRename */
  5608   6101     };
  5609   6102   
  5610   6103     int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
         6104  +  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
  5611   6105     if( rc==SQLITE_OK ){
  5612         -    rc = sqlite3_overload_function(db, "zipfile_cds", -1);
         6106  +    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
         6107  +        zipfileStep, zipfileFinal
         6108  +    );
  5613   6109     }
  5614   6110     return rc;
  5615   6111   }
  5616   6112   #else         /* SQLITE_OMIT_VIRTUALTABLE */
  5617   6113   # define zipfileRegister(x) SQLITE_OK
  5618   6114   #endif
  5619   6115   
................................................................................
  7974   8470   
  7975   8471   /* Allowed values for ShellState.openMode
  7976   8472   */
  7977   8473   #define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
  7978   8474   #define SHELL_OPEN_NORMAL     1      /* Normal database file */
  7979   8475   #define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
  7980   8476   #define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
         8477  +#define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
  7981   8478   
  7982   8479   /*
  7983   8480   ** These are the allowed shellFlgs values
  7984   8481   */
  7985   8482   #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
  7986   8483   #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
  7987   8484   #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
................................................................................
  8080   8577   **     (4) Delete the temporary file
  8081   8578   **
  8082   8579   ** If the EDITOR argument is omitted, use the value in the VISUAL
  8083   8580   ** environment variable.  If still there is no EDITOR, through an error.
  8084   8581   **
  8085   8582   ** Also throw an error if the EDITOR program returns a non-zero exit code.
  8086   8583   */
         8584  +#ifndef SQLITE_NOHAVE_SYSTEM
  8087   8585   static void editFunc(
  8088   8586     sqlite3_context *context,
  8089   8587     int argc,
  8090   8588     sqlite3_value **argv
  8091   8589   ){
  8092   8590     const char *zEditor;
  8093   8591     char *zTempFile = 0;
................................................................................
  8177   8675     fclose(f);
  8178   8676     f = 0;
  8179   8677     if( x!=sz ){
  8180   8678       sqlite3_result_error(context, "could not read back the whole file", -1);
  8181   8679       goto edit_func_end;
  8182   8680     }
  8183   8681     if( bBin ){
  8184         -    sqlite3_result_blob(context, p, sz, sqlite3_free);
         8682  +    sqlite3_result_blob64(context, p, sz, sqlite3_free);
  8185   8683     }else{
  8186         -    sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
         8684  +    sqlite3_result_text64(context, (const char*)p, sz,
         8685  +                          sqlite3_free, SQLITE_UTF8);
  8187   8686     }
  8188   8687     p = 0;
  8189   8688   
  8190   8689   edit_func_end:
  8191   8690     if( f ) fclose(f);
  8192   8691     unlink(zTempFile);
  8193   8692     sqlite3_free(zTempFile);
  8194   8693     sqlite3_free(p);
  8195   8694   }
         8695  +#endif /* SQLITE_NOHAVE_SYSTEM */
  8196   8696   
  8197   8697   /*
  8198   8698   ** Save or restore the current output mode
  8199   8699   */
  8200   8700   static void outputModePush(ShellState *p){
  8201   8701     p->modePrior = p->mode;
  8202   8702     memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
................................................................................
  9174   9674   static int display_stats(
  9175   9675     sqlite3 *db,                /* Database to query */
  9176   9676     ShellState *pArg,           /* Pointer to ShellState */
  9177   9677     int bReset                  /* True to reset the stats */
  9178   9678   ){
  9179   9679     int iCur;
  9180   9680     int iHiwtr;
  9181         -
  9182         -  if( pArg && pArg->out ){
  9183         -    displayStatLine(pArg, "Memory Used:",
  9184         -       "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
  9185         -    displayStatLine(pArg, "Number of Outstanding Allocations:",
  9186         -       "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
  9187         -    if( pArg->shellFlgs & SHFLG_Pagecache ){
  9188         -      displayStatLine(pArg, "Number of Pcache Pages Used:",
  9189         -         "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
  9190         -    }
  9191         -    displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
  9192         -       "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
  9193         -    displayStatLine(pArg, "Largest Allocation:",
  9194         -       "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
  9195         -    displayStatLine(pArg, "Largest Pcache Allocation:",
  9196         -       "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
         9681  +  FILE *out;
         9682  +  if( pArg==0 || pArg->out==0 ) return 0;
         9683  +  out = pArg->out;
         9684  +
         9685  +  if( pArg->pStmt && (pArg->statsOn & 2) ){
         9686  +    int nCol, i, x;
         9687  +    sqlite3_stmt *pStmt = pArg->pStmt;
         9688  +    char z[100];
         9689  +    nCol = sqlite3_column_count(pStmt);
         9690  +    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
         9691  +    for(i=0; i<nCol; i++){
         9692  +      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
         9693  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
         9694  +#ifndef SQLITE_OMIT_DECLTYPE
         9695  +      sqlite3_snprintf(30, z+x, "declared type:");
         9696  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
         9697  +#endif
         9698  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
         9699  +      sqlite3_snprintf(30, z+x, "database name:");
         9700  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
         9701  +      sqlite3_snprintf(30, z+x, "table name:");
         9702  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
         9703  +      sqlite3_snprintf(30, z+x, "origin name:");
         9704  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
         9705  +#endif
         9706  +    }
         9707  +  }
         9708  +
         9709  +  displayStatLine(pArg, "Memory Used:",
         9710  +     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
         9711  +  displayStatLine(pArg, "Number of Outstanding Allocations:",
         9712  +     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
         9713  +  if( pArg->shellFlgs & SHFLG_Pagecache ){
         9714  +    displayStatLine(pArg, "Number of Pcache Pages Used:",
         9715  +       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
         9716  +  }
         9717  +  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
         9718  +     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
         9719  +  displayStatLine(pArg, "Largest Allocation:",
         9720  +     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
         9721  +  displayStatLine(pArg, "Largest Pcache Allocation:",
         9722  +     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
  9197   9723   #ifdef YYTRACKMAXSTACKDEPTH
  9198         -    displayStatLine(pArg, "Deepest Parser Stack:",
  9199         -       "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
         9724  +  displayStatLine(pArg, "Deepest Parser Stack:",
         9725  +     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
  9200   9726   #endif
  9201         -  }
  9202   9727   
  9203         -  if( pArg && pArg->out && db ){
         9728  +  if( db ){
  9204   9729       if( pArg->shellFlgs & SHFLG_Lookaside ){
  9205   9730         iHiwtr = iCur = -1;
  9206   9731         sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
  9207   9732                           &iCur, &iHiwtr, bReset);
  9208   9733         raw_printf(pArg->out,
  9209   9734                 "Lookaside Slots Used:                %d (max %d)\n",
  9210   9735                 iCur, iHiwtr);
................................................................................
  9231   9756       iHiwtr = iCur = -1;
  9232   9757       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
  9233   9758       raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
  9234   9759       iHiwtr = iCur = -1;
  9235   9760       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
  9236   9761       raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
  9237   9762       iHiwtr = iCur = -1;
         9763  +    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
         9764  +    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
         9765  +    iHiwtr = iCur = -1;
  9238   9766       sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
  9239   9767       raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
  9240   9768               iCur);
  9241   9769       iHiwtr = iCur = -1;
  9242   9770       sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
  9243   9771       raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
  9244   9772               iCur);
  9245   9773     }
  9246   9774   
  9247         -  if( pArg && pArg->out && db && pArg->pStmt ){
         9775  +  if( pArg->pStmt ){
  9248   9776       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
  9249   9777                                  bReset);
  9250   9778       raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
  9251   9779       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
  9252   9780       raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
  9253   9781       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
  9254   9782       raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
  9255   9783       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
  9256   9784       raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
         9785  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
         9786  +    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
         9787  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
         9788  +    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
         9789  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
         9790  +    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
  9257   9791     }
  9258   9792   
  9259   9793   #ifdef __linux__
  9260   9794     displayLinuxIoStats(pArg->out);
  9261   9795   #endif
  9262   9796   
  9263   9797     /* Do not remove this machine readable comment: extra-stats-output-here */
................................................................................
  9465   9999   }
  9466  10000   
  9467  10001   /*
  9468  10002   ** Run a prepared statement
  9469  10003   */
  9470  10004   static void exec_prepared_stmt(
  9471  10005     ShellState *pArg,                                /* Pointer to ShellState */
  9472         -  sqlite3_stmt *pStmt,                             /* Statment to run */
  9473         -  int (*xCallback)(void*,int,char**,char**,int*)   /* Callback function */
        10006  +  sqlite3_stmt *pStmt                              /* Statment to run */
  9474  10007   ){
  9475  10008     int rc;
  9476  10009   
  9477  10010     /* perform the first step.  this will tell us if we
  9478  10011     ** have a result set or not and how wide it is.
  9479  10012     */
  9480  10013     rc = sqlite3_step(pStmt);
  9481  10014     /* if we have a result set... */
  9482  10015     if( SQLITE_ROW == rc ){
  9483         -    /* if we have a callback... */
  9484         -    if( xCallback ){
  9485         -      /* allocate space for col name ptr, value ptr, and type */
  9486         -      int nCol = sqlite3_column_count(pStmt);
  9487         -      void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
  9488         -      if( !pData ){
  9489         -        rc = SQLITE_NOMEM;
  9490         -      }else{
  9491         -        char **azCols = (char **)pData;      /* Names of result columns */
  9492         -        char **azVals = &azCols[nCol];       /* Results */
  9493         -        int *aiTypes = (int *)&azVals[nCol]; /* Result types */
  9494         -        int i, x;
  9495         -        assert(sizeof(int) <= sizeof(char *));
  9496         -        /* save off ptrs to column names */
        10016  +    /* allocate space for col name ptr, value ptr, and type */
        10017  +    int nCol = sqlite3_column_count(pStmt);
        10018  +    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
        10019  +    if( !pData ){
        10020  +      rc = SQLITE_NOMEM;
        10021  +    }else{
        10022  +      char **azCols = (char **)pData;      /* Names of result columns */
        10023  +      char **azVals = &azCols[nCol];       /* Results */
        10024  +      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
        10025  +      int i, x;
        10026  +      assert(sizeof(int) <= sizeof(char *));
        10027  +      /* save off ptrs to column names */
        10028  +      for(i=0; i<nCol; i++){
        10029  +        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
        10030  +      }
        10031  +      do{
        10032  +        /* extract the data and data types */
  9497  10033           for(i=0; i<nCol; i++){
  9498         -          azCols[i] = (char *)sqlite3_column_name(pStmt, i);
  9499         -        }
  9500         -        do{
  9501         -          /* extract the data and data types */
  9502         -          for(i=0; i<nCol; i++){
  9503         -            aiTypes[i] = x = sqlite3_column_type(pStmt, i);
  9504         -            if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
  9505         -              azVals[i] = "";
  9506         -            }else{
  9507         -              azVals[i] = (char*)sqlite3_column_text(pStmt, i);
  9508         -            }
  9509         -            if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
  9510         -              rc = SQLITE_NOMEM;
  9511         -              break; /* from for */
  9512         -            }
  9513         -          } /* end for */
  9514         -
  9515         -          /* if data and types extracted successfully... */
  9516         -          if( SQLITE_ROW == rc ){
  9517         -            /* call the supplied callback with the result row data */
  9518         -            if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
  9519         -              rc = SQLITE_ABORT;
  9520         -            }else{
  9521         -              rc = sqlite3_step(pStmt);
  9522         -            }
  9523         -          }
  9524         -        } while( SQLITE_ROW == rc );
  9525         -        sqlite3_free(pData);
  9526         -      }
  9527         -    }else{
  9528         -      do{
  9529         -        rc = sqlite3_step(pStmt);
  9530         -      } while( rc == SQLITE_ROW );
        10034  +          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
        10035  +          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
        10036  +            azVals[i] = "";
        10037  +          }else{
        10038  +            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
        10039  +          }
        10040  +          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
        10041  +            rc = SQLITE_NOMEM;
        10042  +            break; /* from for */
        10043  +          }
        10044  +        } /* end for */
        10045  +
        10046  +        /* if data and types extracted successfully... */
        10047  +        if( SQLITE_ROW == rc ){
        10048  +          /* call the supplied callback with the result row data */
        10049  +          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
        10050  +            rc = SQLITE_ABORT;
        10051  +          }else{
        10052  +            rc = sqlite3_step(pStmt);
        10053  +          }
        10054  +        }
        10055  +      } while( SQLITE_ROW == rc );
        10056  +      sqlite3_free(pData);
  9531  10057       }
  9532  10058     }
  9533  10059   }
  9534  10060   
  9535  10061   #ifndef SQLITE_OMIT_VIRTUALTABLE
  9536  10062   /*
  9537  10063   ** This function is called to process SQL if the previous shell command
................................................................................
  9669  10195   ** set via the supplied callback.
  9670  10196   **
  9671  10197   ** This is very similar to SQLite's built-in sqlite3_exec()
  9672  10198   ** function except it takes a slightly different callback
  9673  10199   ** and callback data argument.
  9674  10200   */
  9675  10201   static int shell_exec(
  9676         -  sqlite3 *db,                              /* An open database */
  9677         -  const char *zSql,                         /* SQL to be evaluated */
  9678         -  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
  9679         -                                            /* (not the same as sqlite3_exec) */
  9680  10202     ShellState *pArg,                         /* Pointer to ShellState */
        10203  +  const char *zSql,                         /* SQL to be evaluated */
  9681  10204     char **pzErrMsg                           /* Error msg written here */
  9682  10205   ){
  9683  10206     sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  9684  10207     int rc = SQLITE_OK;             /* Return Code */
  9685  10208     int rc2;
  9686  10209     const char *zLeftover;          /* Tail of unprocessed SQL */
        10210  +  sqlite3 *db = pArg->db;
  9687  10211   
  9688  10212     if( pzErrMsg ){
  9689  10213       *pzErrMsg = NULL;
  9690  10214     }
  9691  10215   
  9692  10216   #ifndef SQLITE_OMIT_VIRTUALTABLE
  9693  10217     if( pArg->expert.pExpert ){
................................................................................
  9750  10274           if( pArg->autoEQP>=AUTOEQP_full ){
  9751  10275             /* Also do an EXPLAIN for ".eqp full" mode */
  9752  10276             zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
  9753  10277             rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  9754  10278             if( rc==SQLITE_OK ){
  9755  10279               pArg->cMode = MODE_Explain;
  9756  10280               explain_data_prepare(pArg, pExplain);
  9757         -            exec_prepared_stmt(pArg, pExplain, xCallback);
        10281  +            exec_prepared_stmt(pArg, pExplain);
  9758  10282               explain_data_delete(pArg);
  9759  10283             }
  9760  10284             sqlite3_finalize(pExplain);
  9761  10285             sqlite3_free(zEQP);
  9762  10286           }
  9763  10287           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
  9764  10288           restore_debug_trace_modes();
................................................................................
  9776  10300           /* If the shell is currently in ".explain" mode, gather the extra
  9777  10301           ** data required to add indents to the output.*/
  9778  10302           if( pArg->cMode==MODE_Explain ){
  9779  10303             explain_data_prepare(pArg, pStmt);
  9780  10304           }
  9781  10305         }
  9782  10306   
  9783         -      exec_prepared_stmt(pArg, pStmt, xCallback);
        10307  +      exec_prepared_stmt(pArg, pStmt);
  9784  10308         explain_data_delete(pArg);
  9785  10309   
  9786  10310         /* print usage stats if stats on */
  9787  10311         if( pArg && pArg->statsOn ){
  9788  10312           display_stats(db, pArg, 0);
  9789  10313         }
  9790  10314   
................................................................................
 10039  10563       appendText(&sSelect, " FROM ", 0);
 10040  10564       appendText(&sSelect, zTable, quoteChar(zTable));
 10041  10565   
 10042  10566       savedDestTable = p->zDestTable;
 10043  10567       savedMode = p->mode;
 10044  10568       p->zDestTable = sTable.z;
 10045  10569       p->mode = p->cMode = MODE_Insert;
 10046         -    rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
        10570  +    rc = shell_exec(p, sSelect.z, 0);
 10047  10571       if( (rc&0xff)==SQLITE_CORRUPT ){
 10048  10572         raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
 10049  10573         toggleSelectOrder(p->db);
 10050         -      shell_exec(p->db, sSelect.z, shell_callback, p, 0);
        10574  +      shell_exec(p, sSelect.z, 0);
 10051  10575         toggleSelectOrder(p->db);
 10052  10576       }
 10053  10577       p->zDestTable = savedDestTable;
 10054  10578       p->mode = savedMode;
 10055  10579       freeText(&sTable);
 10056  10580       freeText(&sSelect);
 10057  10581       if( rc ) p->nErr++;
................................................................................
 10160  10684     "                         tcl      TCL list elements\n"
 10161  10685     ".nullvalue STRING      Use STRING in place of NULL values\n"
 10162  10686     ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
 10163  10687     "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
 10164  10688     "                         on the output.\n"
 10165  10689     ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
 10166  10690     "                         The --new option starts with an empty file\n"
        10691  +  "                         Other options: --readonly --append --zip\n"
 10167  10692     ".output ?FILE?         Send output to FILE or stdout\n"
 10168  10693     ".print STRING...       Print literal STRING\n"
 10169  10694     ".prompt MAIN CONTINUE  Replace the standard prompts\n"
 10170  10695     ".quit                  Exit this program\n"
 10171  10696     ".read FILENAME         Execute SQL in FILENAME\n"
 10172  10697     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
 10173  10698     ".save FILE             Write in-memory database into FILE\n"
................................................................................
 10177  10702     ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
 10178  10703     ".separator COL ?ROW?   Change the column separator and optionally the row\n"
 10179  10704     "                         separator for both the output mode and .import\n"
 10180  10705   #if defined(SQLITE_ENABLE_SESSION)
 10181  10706     ".session CMD ...       Create or control sessions\n"
 10182  10707   #endif
 10183  10708     ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
        10709  +#ifndef SQLITE_NOHAVE_SYSTEM
 10184  10710     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
        10711  +#endif
 10185  10712     ".show                  Show the current values for various settings\n"
 10186  10713     ".stats ?on|off?        Show stats or turn stats on or off\n"
        10714  +#ifndef SQLITE_NOHAVE_SYSTEM
 10187  10715     ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
        10716  +#endif
 10188  10717     ".tables ?TABLE?        List names of tables\n"
 10189  10718     "                         If TABLE specified, only list tables matching\n"
 10190  10719     "                         LIKE pattern TABLE.\n"
 10191  10720     ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
 10192  10721     ".timeout MS            Try opening locked tables for MS milliseconds\n"
 10193  10722     ".timer on|off          Turn SQL timer on or off\n"
 10194  10723     ".trace FILE|off        Output each SQL statement as it is run\n"
................................................................................
 10309  10838     return 1;
 10310  10839   }
 10311  10840   #endif
 10312  10841   
 10313  10842   /*
 10314  10843   ** Try to deduce the type of file for zName based on its content.  Return
 10315  10844   ** one of the SHELL_OPEN_* constants.
        10845  +**
        10846  +** If the file does not exist or is empty but its name looks like a ZIP
        10847  +** archive and the dfltZip flag is true, then assume it is a ZIP archive.
        10848  +** Otherwise, assume an ordinary database regardless of the filename if
        10849  +** the type cannot be determined from content.
 10316  10850   */
 10317         -static int deduceDatabaseType(const char *zName){
        10851  +static int deduceDatabaseType(const char *zName, int dfltZip){
 10318  10852     FILE *f = fopen(zName, "rb");
 10319  10853     size_t n;
 10320  10854     int rc = SHELL_OPEN_UNSPEC;
 10321  10855     char zBuf[100];
 10322         -  if( f==0 ) return SHELL_OPEN_NORMAL;
        10856  +  if( f==0 ){
        10857  +    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
        10858  +    return SHELL_OPEN_NORMAL;
        10859  +  }
 10323  10860     fseek(f, -25, SEEK_END);
 10324  10861     n = fread(zBuf, 25, 1, f);
 10325  10862     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
 10326  10863       rc = SHELL_OPEN_APPENDVFS;
 10327  10864     }else{
 10328  10865       fseek(f, -22, SEEK_END);
 10329  10866       n = fread(zBuf, 22, 1, f);
 10330  10867       if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
 10331  10868          && zBuf[3]==0x06 ){
 10332  10869         rc = SHELL_OPEN_ZIPFILE;
        10870  +    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
        10871  +      return SHELL_OPEN_ZIPFILE;
 10333  10872       }
 10334  10873     }
 10335  10874     fclose(f);
 10336  10875     return rc;  
 10337  10876   }
 10338  10877   
 10339  10878   /*
................................................................................
 10340  10879   ** Make sure the database is open.  If it is not, then open it.  If
 10341  10880   ** the database fails to open, print an error message and exit.
 10342  10881   */
 10343  10882   static void open_db(ShellState *p, int keepAlive){
 10344  10883     if( p->db==0 ){
 10345  10884       sqlite3_initialize();
 10346  10885       if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
 10347         -      p->openMode = deduceDatabaseType(p->zDbFilename);
        10886  +      p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
 10348  10887       }
 10349  10888       switch( p->openMode ){
 10350  10889         case SHELL_OPEN_APPENDVFS: {
 10351  10890           sqlite3_open_v2(p->zDbFilename, &p->db, 
 10352  10891              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
 10353  10892           break;
 10354  10893         }
 10355  10894         case SHELL_OPEN_ZIPFILE: {
 10356  10895           sqlite3_open(":memory:", &p->db);
 10357  10896           break;
        10897  +      }
        10898  +      case SHELL_OPEN_READONLY: {
        10899  +        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
        10900  +        break;
 10358  10901         }
 10359  10902         case SHELL_OPEN_UNSPEC:
 10360  10903         case SHELL_OPEN_NORMAL: {
 10361  10904           sqlite3_open(p->zDbFilename, &p->db);
 10362  10905           break;
 10363  10906         }
 10364  10907       }
................................................................................
 10381  10924   #endif
 10382  10925       sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
 10383  10926                               shellAddSchemaName, 0, 0);
 10384  10927       sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
 10385  10928                               shellModuleSchema, 0, 0);
 10386  10929       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
 10387  10930                               shellPutsFunc, 0, 0);
        10931  +#ifndef SQLITE_NOHAVE_SYSTEM
 10388  10932       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
 10389  10933                               editFunc, 0, 0);
 10390  10934       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
 10391  10935                               editFunc, 0, 0);
        10936  +#endif
 10392  10937       if( p->openMode==SHELL_OPEN_ZIPFILE ){
 10393  10938         char *zSql = sqlite3_mprintf(
 10394  10939            "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
 10395  10940         sqlite3_exec(p->db, zSql, 0, 0, 0);
 10396  10941         sqlite3_free(zSql);
 10397  10942       }
 10398  10943     }
................................................................................
 10999  11544   static void output_reset(ShellState *p){
 11000  11545     if( p->outfile[0]=='|' ){
 11001  11546   #ifndef SQLITE_OMIT_POPEN
 11002  11547       pclose(p->out);
 11003  11548   #endif
 11004  11549     }else{
 11005  11550       output_file_close(p->out);
        11551  +#ifndef SQLITE_NOHAVE_SYSTEM
 11006  11552       if( p->doXdgOpen ){
 11007  11553         const char *zXdgOpenCmd =
 11008  11554   #if defined(_WIN32)
 11009  11555         "start";
 11010  11556   #elif defined(__APPLE__)
 11011  11557         "open";
 11012  11558   #else
................................................................................
 11017  11563         if( system(zCmd) ){
 11018  11564           utf8_printf(stderr, "Failed: [%s]\n", zCmd);
 11019  11565         }
 11020  11566         sqlite3_free(zCmd);
 11021  11567         outputModePop(p);
 11022  11568         p->doXdgOpen = 0;
 11023  11569       }
        11570  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
 11024  11571     }
 11025  11572     p->outfile[0] = 0;
 11026  11573     p->out = stdout;
 11027  11574   }
 11028  11575   
 11029  11576   /*
 11030  11577   ** Run an SQL command and return the single integer result.
................................................................................
 12127  12674         "  name TEXT PRIMARY KEY,  -- name of the file\n"
 12128  12675         "  mode INT,               -- access permissions\n"
 12129  12676         "  mtime INT,              -- last modification time\n"
 12130  12677         "  sz INT,                 -- original file size\n"
 12131  12678         "  data BLOB               -- compressed content\n"
 12132  12679         ")";
 12133  12680     const char *zDrop = "DROP TABLE IF EXISTS sqlar";
 12134         -  const char *zInsertFmt = 
 12135         -     "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
        12681  +  const char *zInsertFmt[2] = {
        12682  +     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
 12136  12683        "  SELECT\n"
 12137  12684        "    %s,\n"
 12138  12685        "    mode,\n"
 12139  12686        "    mtime,\n"
 12140  12687        "    CASE substr(lsmode(mode),1,1)\n"
 12141  12688        "      WHEN '-' THEN length(data)\n"
 12142  12689        "      WHEN 'd' THEN 0\n"
 12143  12690        "      ELSE -1 END,\n"
 12144         -     "    CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
        12691  +     "    sqlar_compress(data)\n"
        12692  +     "  FROM fsdir(%Q,%Q)\n"
        12693  +     "  WHERE lsmode(mode) NOT LIKE '?%%';",
        12694  +     "REPLACE INTO %s(name,mode,mtime,data)\n"
        12695  +     "  SELECT\n"
        12696  +     "    %s,\n"
        12697  +     "    mode,\n"
        12698  +     "    mtime,\n"
        12699  +     "    data\n"
 12145  12700        "  FROM fsdir(%Q,%Q)\n"
 12146         -     "  WHERE lsmode(mode) NOT LIKE '?%%';";
        12701  +     "  WHERE lsmode(mode) NOT LIKE '?%%';"
        12702  +  };
 12147  12703     int i;                          /* For iterating through azFile[] */
 12148  12704     int rc;                         /* Return code */
        12705  +  const char *zTab = 0;           /* SQL table into which to insert */
        12706  +  char *zSql;
        12707  +  char zTemp[50];
 12149  12708   
        12709  +  arExecSql(pAr, "PRAGMA page_size=512");
 12150  12710     rc = arExecSql(pAr, "SAVEPOINT ar;");
 12151  12711     if( rc!=SQLITE_OK ) return rc;
 12152         -  if( bUpdate==0 ){
 12153         -    rc = arExecSql(pAr, zDrop);
 12154         -    if( rc!=SQLITE_OK ) return rc;
        12712  +  zTemp[0] = 0; 
        12713  +  if( pAr->bZip ){
        12714  +    /* Initialize the zipfile virtual table, if necessary */
        12715  +    if( pAr->zFile ){
        12716  +      sqlite3_uint64 r;
        12717  +      sqlite3_randomness(sizeof(r),&r);
        12718  +      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
        12719  +      zTab = zTemp;
        12720  +      zSql = sqlite3_mprintf(
        12721  +         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
        12722  +         zTab, pAr->zFile
        12723  +      );
        12724  +      rc = arExecSql(pAr, zSql);
        12725  +      sqlite3_free(zSql);
        12726  +    }else{
        12727  +      zTab = "zip";
        12728  +    }
        12729  +  }else{
        12730  +    /* Initialize the table for an SQLAR */
        12731  +    zTab = "sqlar";
        12732  +    if( bUpdate==0 ){
        12733  +      rc = arExecSql(pAr, zDrop);
        12734  +      if( rc!=SQLITE_OK ) goto end_ar_transaction;
        12735  +    }
        12736  +    rc = arExecSql(pAr, zCreate);
 12155  12737     }
 12156         -  rc = arExecSql(pAr, zCreate);
 12157  12738     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
 12158         -    char *zSql = sqlite3_mprintf(zInsertFmt,
        12739  +    char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
 12159  12740           pAr->bVerbose ? "shell_putsnl(name)" : "name",
 12160  12741           pAr->azArg[i], pAr->zDir);
 12161  12742       rc = arExecSql(pAr, zSql);
 12162  12743       sqlite3_free(zSql);
 12163  12744     }
        12745  +end_ar_transaction:
 12164  12746     if( rc!=SQLITE_OK ){
 12165  12747       arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
 12166  12748     }else{
 12167  12749       rc = arExecSql(pAr, "RELEASE ar;");
        12750  +    if( pAr->bZip && pAr->zFile ){
        12751  +      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
        12752  +      arExecSql(pAr, zSql);
        12753  +      sqlite3_free(zSql);
        12754  +    }
 12168  12755     }
 12169  12756     return rc;
 12170  12757   }
 12171  12758   
 12172  12759   /*
 12173  12760   ** Implementation of ".ar" dot command.
 12174  12761   */
................................................................................
 12182  12769     memset(&cmd, 0, sizeof(cmd));
 12183  12770     rc = arParseCommand(azArg, nArg, &cmd);
 12184  12771     if( rc==SQLITE_OK ){
 12185  12772       int eDbType = SHELL_OPEN_UNSPEC;
 12186  12773       cmd.p = pState;
 12187  12774       cmd.db = pState->db;
 12188  12775       if( cmd.zFile ){
 12189         -      eDbType = deduceDatabaseType(cmd.zFile);
        12776  +      eDbType = deduceDatabaseType(cmd.zFile, 1);
 12190  12777       }else{
 12191  12778         eDbType = pState->openMode;
 12192  12779       }
 12193  12780       if( eDbType==SHELL_OPEN_ZIPFILE ){
 12194         -      if( cmd.zFile==0 ){
 12195         -        cmd.zSrcTable = sqlite3_mprintf("zip");
 12196         -      }else{
 12197         -        cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
 12198         -      }
 12199         -      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
 12200         -        utf8_printf(stderr, "zip archives are read-only\n");
 12201         -        rc = SQLITE_ERROR;
 12202         -        goto end_ar_command;
        12781  +      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
        12782  +        if( cmd.zFile==0 ){
        12783  +          cmd.zSrcTable = sqlite3_mprintf("zip");
        12784  +        }else{
        12785  +          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
        12786  +        }
 12203  12787         }
 12204  12788         cmd.bZip = 1;
 12205  12789       }else if( cmd.zFile ){
 12206  12790         int flags;
 12207  12791         if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
 12208  12792         if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
 12209  12793           flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
................................................................................
 12220  12804         if( rc!=SQLITE_OK ){
 12221  12805           utf8_printf(stderr, "cannot open file: %s (%s)\n", 
 12222  12806               cmd.zFile, sqlite3_errmsg(cmd.db)
 12223  12807           );
 12224  12808           goto end_ar_command;
 12225  12809         }
 12226  12810         sqlite3_fileio_init(cmd.db, 0, 0);
 12227         -#ifdef SQLITE_HAVE_ZLIB
 12228  12811         sqlite3_sqlar_init(cmd.db, 0, 0);
 12229         -#endif
 12230  12812         sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
 12231  12813                                 shellPutsFunc, 0, 0);
 12232  12814   
 12233  12815       }
 12234         -    if( cmd.zSrcTable==0 ){
        12816  +    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
 12235  12817         if( cmd.eCmd!=AR_CMD_CREATE
 12236  12818          && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
 12237  12819         ){
 12238  12820           utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
 12239  12821           rc = SQLITE_ERROR;
 12240  12822           goto end_ar_command;
 12241  12823         }
................................................................................
 12620  13202     if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
 12621  13203       if( nArg==2 ){
 12622  13204         if( strcmp(azArg[1],"full")==0 ){
 12623  13205           p->autoEQP = AUTOEQP_full;
 12624  13206         }else if( strcmp(azArg[1],"trigger")==0 ){
 12625  13207           p->autoEQP = AUTOEQP_trigger;
 12626  13208         }else{
 12627         -        p->autoEQP = booleanValue(azArg[1]);
        13209  +        p->autoEQP = (u8)booleanValue(azArg[1]);
 12628  13210         }
 12629  13211       }else{
 12630  13212         raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
 12631  13213         rc = 1;
 12632  13214       }
 12633  13215     }else
 12634  13216   
................................................................................
 12707  13289         raw_printf(p->out, "/* No STAT tables available */\n");
 12708  13290       }else{
 12709  13291         raw_printf(p->out, "ANALYZE sqlite_master;\n");
 12710  13292         sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
 12711  13293                      callback, &data, &zErrMsg);
 12712  13294         data.cMode = data.mode = MODE_Insert;
 12713  13295         data.zDestTable = "sqlite_stat1";
 12714         -      shell_exec(p->db, "SELECT * FROM sqlite_stat1",
 12715         -                 shell_callback, &data,&zErrMsg);
        13296  +      shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
 12716  13297         data.zDestTable = "sqlite_stat3";
 12717         -      shell_exec(p->db, "SELECT * FROM sqlite_stat3",
 12718         -                 shell_callback, &data,&zErrMsg);
        13298  +      shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
 12719  13299         data.zDestTable = "sqlite_stat4";
 12720         -      shell_exec(p->db, "SELECT * FROM sqlite_stat4",
 12721         -                 shell_callback, &data, &zErrMsg);
        13300  +      shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
 12722  13301         raw_printf(p->out, "ANALYZE sqlite_master;\n");
 12723  13302       }
 12724  13303     }else
 12725  13304   
 12726  13305     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
 12727  13306       if( nArg==2 ){
 12728  13307         p->showHeader = booleanValue(azArg[1]);
................................................................................
 13195  13774       p->zFreeOnClose = 0;
 13196  13775       p->openMode = SHELL_OPEN_UNSPEC;
 13197  13776       /* Check for command-line arguments */
 13198  13777       for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
 13199  13778         const char *z = azArg[iName];
 13200  13779         if( optionMatch(z,"new") ){
 13201  13780           newFlag = 1;
 13202         -#ifdef SQLITE_HAVE_ZIP
        13781  +#ifdef SQLITE_HAVE_ZLIB
 13203  13782         }else if( optionMatch(z, "zip") ){
 13204  13783           p->openMode = SHELL_OPEN_ZIPFILE;
 13205  13784   #endif
 13206  13785         }else if( optionMatch(z, "append") ){
 13207  13786           p->openMode = SHELL_OPEN_APPENDVFS;
        13787  +      }else if( optionMatch(z, "readonly") ){
        13788  +        p->openMode = SHELL_OPEN_READONLY;
 13208  13789         }else if( z[0]=='-' ){
 13209  13790           utf8_printf(stderr, "unknown option: %s\n", z);
 13210  13791           rc = 1;
 13211  13792           goto meta_command_exit;
 13212  13793         }
 13213  13794       }
 13214  13795       /* If a filename is specified, try to open it first */
................................................................................
 13257  13838         }
 13258  13839         p->outCount = 2;
 13259  13840       }else{
 13260  13841         p->outCount = 0;
 13261  13842       }
 13262  13843       output_reset(p);
 13263  13844       if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
        13845  +#ifndef SQLITE_NOHAVE_SYSTEM
 13264  13846       if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
 13265  13847         p->doXdgOpen = 1;
 13266  13848         outputModePush(p);
 13267  13849         if( zFile[1]=='x' ){
 13268  13850           newTempFile(p, "csv");
 13269  13851           p->mode = MODE_Csv;
 13270  13852           sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
................................................................................
 13271  13853           sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
 13272  13854         }else{
 13273  13855           newTempFile(p, "txt");
 13274  13856           bTxtMode = 1;
 13275  13857         }
 13276  13858         zFile = p->zTempFile;
 13277  13859       }
        13860  +#endif /* SQLITE_NOHAVE_SYSTEM */
 13278  13861       if( zFile[0]=='|' ){
 13279  13862   #ifdef SQLITE_OMIT_POPEN
 13280  13863         raw_printf(stderr, "Error: pipes are not supported in this OS\n");
 13281  13864         rc = 1;
 13282  13865         p->out = stdout;
 13283  13866   #else
 13284  13867         p->out = popen(zFile + 1, "w");
................................................................................
 13390  13973       }else{
 13391  13974         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
 13392  13975         rc = 1;
 13393  13976       }
 13394  13977       sqlite3_close(pSrc);
 13395  13978     }else
 13396  13979   
 13397         -
 13398  13980     if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
 13399  13981       if( nArg==2 ){
 13400         -      p->scanstatsOn = booleanValue(azArg[1]);
        13982  +      p->scanstatsOn = (u8)booleanValue(azArg[1]);
 13401  13983   #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
 13402  13984         raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
 13403  13985   #endif
 13404  13986       }else{
 13405  13987         raw_printf(stderr, "Usage: .scanstats on|off\n");
 13406  13988         rc = 1;
 13407  13989       }
................................................................................
 13981  14563             sSql.z, iSize);
 13982  14564       }
 13983  14565       freeText(&sQuery);
 13984  14566       freeText(&sSql);
 13985  14567       if( bDebug ){
 13986  14568         utf8_printf(p->out, "%s\n", zSql);
 13987  14569       }else{
 13988         -      shell_exec(p->db, zSql, shell_callback, p, 0);
        14570  +      shell_exec(p, zSql, 0);
 13989  14571       }
 13990  14572       sqlite3_free(zSql);
 13991  14573     }else
 13992  14574   
        14575  +#ifndef SQLITE_NOHAVE_SYSTEM
 13993  14576     if( c=='s'
 13994  14577      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
 13995  14578     ){
 13996  14579       char *zCmd;
 13997  14580       int i, x;
 13998  14581       if( nArg<2 ){
 13999  14582         raw_printf(stderr, "Usage: .system COMMAND\n");
................................................................................
 14005  14588         zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
 14006  14589                                zCmd, azArg[i]);
 14007  14590       }
 14008  14591       x = system(zCmd);
 14009  14592       sqlite3_free(zCmd);
 14010  14593       if( x ) raw_printf(stderr, "System command returns %d\n", x);
 14011  14594     }else
        14595  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
 14012  14596   
 14013  14597     if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
 14014  14598       static const char *azBool[] = { "off", "on", "trigger", "full"};
 14015  14599       int i;
 14016  14600       if( nArg!=1 ){
 14017  14601         raw_printf(stderr, "Usage: .show\n");
 14018  14602         rc = 1;
................................................................................
 14044  14628       raw_printf(p->out, "\n");
 14045  14629       utf8_printf(p->out, "%12.12s: %s\n", "filename",
 14046  14630                   p->zDbFilename ? p->zDbFilename : "");
 14047  14631     }else
 14048  14632   
 14049  14633     if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
 14050  14634       if( nArg==2 ){
 14051         -      p->statsOn = booleanValue(azArg[1]);
        14635  +      p->statsOn = (u8)booleanValue(azArg[1]);
 14052  14636       }else if( nArg==1 ){
 14053  14637         display_stats(p->db, p, 0);
 14054  14638       }else{
 14055  14639         raw_printf(stderr, "Usage: .stats ?on|off?\n");
 14056  14640         rc = 1;
 14057  14641       }
 14058  14642     }else
................................................................................
 14584  15168     if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
 14585  15169            && _all_whitespace(&zLine[2]) ){
 14586  15170       return 1;  /* SQL Server */
 14587  15171     }
 14588  15172     return 0;
 14589  15173   }
 14590  15174   
        15175  +/*
        15176  +** We need a default sqlite3_complete() implementation to use in case
        15177  +** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
        15178  +** any arbitrary text is a complete SQL statement.  This is not very
        15179  +** user-friendly, but it does seem to work.
        15180  +*/
        15181  +#ifdef SQLITE_OMIT_COMPLETE
        15182  +int sqlite3_complete(const char *zSql){ return 1; }
        15183  +#endif
        15184  +
 14591  15185   /*
 14592  15186   ** Return true if zSql is a complete SQL statement.  Return false if it
 14593  15187   ** ends in the middle of a string literal or C-style comment.
 14594  15188   */
 14595  15189   static int line_is_complete(char *zSql, int nSql){
 14596  15190     int rc;
 14597  15191     if( zSql==0 ) return 1;
................................................................................
 14608  15202   static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
 14609  15203     int rc;
 14610  15204     char *zErrMsg = 0;
 14611  15205   
 14612  15206     open_db(p, 0);
 14613  15207     if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
 14614  15208     BEGIN_TIMER;
 14615         -  rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
        15209  +  rc = shell_exec(p, zSql, &zErrMsg);
 14616  15210     END_TIMER;
 14617  15211     if( rc || zErrMsg ){
 14618  15212       char zPrefix[100];
 14619  15213       if( in!=0 || !stdin_is_interactive ){
 14620  15214         sqlite3_snprintf(sizeof(zPrefix), zPrefix,
 14621  15215                          "Error: near line %d:", startline);
 14622  15216       }else{
................................................................................
 14840  15434     sqlite3_free(zBuf);
 14841  15435   }
 14842  15436   
 14843  15437   /*
 14844  15438   ** Show available command line options
 14845  15439   */
 14846  15440   static const char zOptions[] =
        15441  +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
        15442  +  "   -A ARGS...           run \".archive ARGS\" and exit\n"
        15443  +#endif
        15444  +  "   -append              append the database to the end of the file\n"
 14847  15445     "   -ascii               set output mode to 'ascii'\n"
 14848  15446     "   -bail                stop after hitting an error\n"
 14849  15447     "   -batch               force batch I/O\n"
 14850  15448     "   -column              set output mode to 'column'\n"
 14851  15449     "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
 14852  15450     "   -csv                 set output mode to 'csv'\n"
 14853  15451     "   -echo                print commands before execution\n"
................................................................................
 14866  15464   #ifdef SQLITE_ENABLE_MULTIPLEX
 14867  15465     "   -multiplex           enable the multiplexor VFS\n"
 14868  15466   #endif
 14869  15467     "   -newline SEP         set output row separator. Default: '\\n'\n"
 14870  15468     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
 14871  15469     "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
 14872  15470     "   -quote               set output mode to 'quote'\n"
        15471  +  "   -readonly            open the database read-only\n"
 14873  15472     "   -separator SEP       set output column separator. Default: '|'\n"
 14874  15473     "   -stats               print memory stats before each finalize\n"
 14875  15474     "   -version             show SQLite version\n"
 14876  15475     "   -vfs NAME            use NAME as the default VFS\n"
 14877  15476   #ifdef SQLITE_ENABLE_VFSTRACE
 14878  15477     "   -vfstrace            enable tracing of all VFS calls\n"
 14879  15478   #endif
        15479  +#ifdef SQLITE_HAVE_ZLIB
        15480  +  "   -zip                 open the file as a ZIP Archive\n"
        15481  +#endif
 14880  15482   ;
 14881  15483   static void usage(int showDetail){
 14882  15484     utf8_printf(stderr,
 14883  15485         "Usage: %s [OPTIONS] FILENAME [SQL]\n"
 14884  15486         "FILENAME is the name of an SQLite database. A new database is created\n"
 14885  15487         "if the file does not previously exist.\n", Argv0);
 14886  15488     if( showDetail ){
................................................................................
 14975  15577     if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
 14976  15578       utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
 14977  15579               sqlite3_sourceid(), SQLITE_SOURCE_ID);
 14978  15580       exit(1);
 14979  15581     }
 14980  15582   #endif
 14981  15583     main_init(&data);
        15584  +
        15585  +  /* On Windows, we must translate command-line arguments into UTF-8.
        15586  +  ** The SQLite memory allocator subsystem has to be enabled in order to
        15587  +  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
        15588  +  ** subsequent sqlite3_config() calls will work.  So copy all results into
        15589  +  ** memory that does not come from the SQLite memory allocator.
        15590  +  */
 14982  15591   #if !SQLITE_SHELL_IS_UTF8
 14983  15592     sqlite3_initialize();
 14984         -  argv = sqlite3_malloc64(sizeof(argv[0])*argc);
        15593  +  argv = malloc(sizeof(argv[0])*argc);
 14985  15594     if( argv==0 ){
 14986  15595       raw_printf(stderr, "out of memory\n");
 14987  15596       exit(1);
 14988  15597     }
 14989  15598     for(i=0; i<argc; i++){
 14990         -    argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
        15599  +    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
        15600  +    int n;
        15601  +    if( z==0 ){
        15602  +      raw_printf(stderr, "out of memory\n");
        15603  +      exit(1);
        15604  +    }
        15605  +    n = (int)strlen(z);
        15606  +    argv[i] = malloc( n+1 );
 14991  15607       if( argv[i]==0 ){
 14992  15608         raw_printf(stderr, "out of memory\n");
 14993  15609         exit(1);
 14994  15610       }
        15611  +    memcpy(argv[i], z, n+1);
        15612  +    sqlite3_free(z);
 14995  15613     }
        15614  +  sqlite3_shutdown();
 14996  15615   #endif
        15616  +
 14997  15617     assert( argc>=1 && argv && argv[0] );
 14998  15618     Argv0 = argv[0];
 14999  15619   
 15000  15620     /* Make sure we have a valid signal handler early, before anything
 15001  15621     ** else is done.
 15002  15622     */
 15003  15623   #ifdef SIGINT
................................................................................
 15109  15729         sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
 15110  15730         if( pVfs ){
 15111  15731           sqlite3_vfs_register(pVfs, 1);
 15112  15732         }else{
 15113  15733           utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
 15114  15734           exit(1);
 15115  15735         }
 15116         -#ifdef SQLITE_HAVE_ZIP
        15736  +#ifdef SQLITE_HAVE_ZLIB
 15117  15737       }else if( strcmp(z,"-zip")==0 ){
 15118  15738         data.openMode = SHELL_OPEN_ZIPFILE;
 15119  15739   #endif
 15120  15740       }else if( strcmp(z,"-append")==0 ){
 15121  15741         data.openMode = SHELL_OPEN_APPENDVFS;
        15742  +    }else if( strcmp(z,"-readonly")==0 ){
        15743  +      data.openMode = SHELL_OPEN_READONLY;
        15744  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
        15745  +    }else if( strncmp(z, "-A",2)==0 ){
        15746  +      /* All remaining command-line arguments are passed to the ".archive"
        15747  +      ** command, so ignore them */
        15748  +      break;
        15749  +#endif
 15122  15750       }
 15123  15751     }
 15124  15752     if( data.zDbFilename==0 ){
 15125  15753   #ifndef SQLITE_OMIT_MEMORYDB
 15126  15754       data.zDbFilename = ":memory:";
 15127  15755       warnInmemoryDb = argc==1;
 15128  15756   #else
................................................................................
 15168  15796       }else if( strcmp(z,"-line")==0 ){
 15169  15797         data.mode = MODE_Line;
 15170  15798       }else if( strcmp(z,"-column")==0 ){
 15171  15799         data.mode = MODE_Column;
 15172  15800       }else if( strcmp(z,"-csv")==0 ){
 15173  15801         data.mode = MODE_Csv;
 15174  15802         memcpy(data.colSeparator,",",2);
 15175         -#ifdef SQLITE_HAVE_ZIP
        15803  +#ifdef SQLITE_HAVE_ZLIB
 15176  15804       }else if( strcmp(z,"-zip")==0 ){
 15177  15805         data.openMode = SHELL_OPEN_ZIPFILE;
 15178  15806   #endif
 15179  15807       }else if( strcmp(z,"-append")==0 ){
 15180  15808         data.openMode = SHELL_OPEN_APPENDVFS;
 15181  15809       }else if( strcmp(z,"-ascii")==0 ){
 15182  15810         data.mode = MODE_Ascii;
................................................................................
 15251  15879         if( i==argc-1 ) break;
 15252  15880         z = cmdline_option_value(argc,argv,++i);
 15253  15881         if( z[0]=='.' ){
 15254  15882           rc = do_meta_command(z, &data);
 15255  15883           if( rc && bail_on_error ) return rc==2 ? 0 : rc;
 15256  15884         }else{
 15257  15885           open_db(&data, 0);
 15258         -        rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
        15886  +        rc = shell_exec(&data, z, &zErrMsg);
 15259  15887           if( zErrMsg!=0 ){
 15260  15888             utf8_printf(stderr,"Error: %s\n", zErrMsg);
 15261  15889             if( bail_on_error ) return rc!=0 ? rc : 1;
 15262  15890           }else if( rc!=0 ){
 15263  15891             utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
 15264  15892             if( bail_on_error ) return rc;
 15265  15893           }
 15266  15894         }
        15895  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
        15896  +    }else if( strncmp(z, "-A", 2)==0 ){
        15897  +      if( nCmd>0 ){
        15898  +        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
        15899  +                            " with \"%s\"\n", z);
        15900  +        return 1;
        15901  +      }
        15902  +      open_db(&data, 0);
        15903  +      if( z[2] ){
        15904  +        argv[i] = &z[2];
        15905  +        arDotCommand(&data, argv+(i-1), argc-(i-1));
        15906  +      }else{
        15907  +        arDotCommand(&data, argv+i, argc-i);
        15908  +      }
        15909  +      readStdin = 0;
        15910  +      break;
        15911  +#endif
 15267  15912       }else{
 15268  15913         utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
 15269  15914         raw_printf(stderr,"Use -help for a list of options.\n");
 15270  15915         return 1;
 15271  15916       }
 15272  15917       data.cMode = data.mode;
 15273  15918     }
................................................................................
 15279  15924       */
 15280  15925       for(i=0; i<nCmd; i++){
 15281  15926         if( azCmd[i][0]=='.' ){
 15282  15927           rc = do_meta_command(azCmd[i], &data);
 15283  15928           if( rc ) return rc==2 ? 0 : rc;
 15284  15929         }else{
 15285  15930           open_db(&data, 0);
 15286         -        rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
        15931  +        rc = shell_exec(&data, azCmd[i], &zErrMsg);
 15287  15932           if( zErrMsg!=0 ){
 15288  15933             utf8_printf(stderr,"Error: %s\n", zErrMsg);
 15289  15934             return rc!=0 ? rc : 1;
 15290  15935           }else if( rc!=0 ){
 15291  15936             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
 15292  15937             return rc;
 15293  15938           }
................................................................................
 15342  15987     }
 15343  15988     sqlite3_free(data.zFreeOnClose);
 15344  15989     find_home_dir(1);
 15345  15990     output_reset(&data);
 15346  15991     data.doXdgOpen = 0;
 15347  15992     clearTempFile(&data);
 15348  15993   #if !SQLITE_SHELL_IS_UTF8
 15349         -  for(i=0; i<argc; i++) sqlite3_free(argv[i]);
 15350         -  sqlite3_free(argv);
        15994  +  for(i=0; i<argc; i++) free(argv[i]);
        15995  +  free(argv);
 15351  15996   #endif
 15352  15997     return rc;
 15353  15998   }

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.22.0.  By combining all the individual C code files into this
            3  +** version 3.23.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
  1143   1143   ** been edited in any way since it was last checked in, then the last
  1144   1144   ** four hexadecimal digits of the hash may be modified.
  1145   1145   **
  1146   1146   ** See also: [sqlite3_libversion()],
  1147   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1149   */
  1150         -#define SQLITE_VERSION        "3.22.0"
  1151         -#define SQLITE_VERSION_NUMBER 3022000
  1152         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
         1150  +#define SQLITE_VERSION        "3.23.0"
         1151  +#define SQLITE_VERSION_NUMBER 3023000
         1152  +#define SQLITE_SOURCE_ID      "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  3516   3516   SQLITE_API void sqlite3_free_table(char **result);
  3517   3517   
  3518   3518   /*
  3519   3519   ** CAPI3REF: Formatted String Printing Functions
  3520   3520   **
  3521   3521   ** These routines are work-alikes of the "printf()" family of functions
  3522   3522   ** from the standard C library.
  3523         -** These routines understand most of the common K&R formatting options,
  3524         -** plus some additional non-standard formats, detailed below.
  3525         -** Note that some of the more obscure formatting options from recent
  3526         -** C-library standards are omitted from this implementation.
         3523  +** These routines understand most of the common formatting options from
         3524  +** the standard library printf() 
         3525  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         3526  +** See the [built-in printf()] documentation for details.
  3527   3527   **
  3528   3528   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  3529         -** results into memory obtained from [sqlite3_malloc()].
         3529  +** results into memory obtained from [sqlite3_malloc64()].
  3530   3530   ** The strings returned by these two routines should be
  3531   3531   ** released by [sqlite3_free()].  ^Both routines return a
  3532         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         3532  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  3533   3533   ** memory to hold the resulting string.
  3534   3534   **
  3535   3535   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  3536   3536   ** the standard C library.  The result is written into the
  3537   3537   ** buffer supplied as the second parameter whose size is given by
  3538   3538   ** the first parameter. Note that the order of the
  3539   3539   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  3549   3549   ** guarantees that the buffer is always zero-terminated.  ^The first
  3550   3550   ** parameter "n" is the total size of the buffer, including space for
  3551   3551   ** the zero terminator.  So the longest string that can be completely
  3552   3552   ** written will be n-1 characters.
  3553   3553   **
  3554   3554   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  3555   3555   **
  3556         -** These routines all implement some additional formatting
  3557         -** options that are useful for constructing SQL statements.
  3558         -** All of the usual printf() formatting options apply.  In addition, there
  3559         -** is are "%q", "%Q", "%w" and "%z" options.
  3560         -**
  3561         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  3562         -** string from the argument list.  But %q also doubles every '\'' character.
  3563         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  3564         -** character it escapes that character and allows it to be inserted into
  3565         -** the string.
  3566         -**
  3567         -** For example, assume the string variable zText contains text as follows:
  3568         -**
  3569         -** <blockquote><pre>
  3570         -**  char *zText = "It's a happy day!";
  3571         -** </pre></blockquote>
  3572         -**
  3573         -** One can use this text in an SQL statement as follows:
  3574         -**
  3575         -** <blockquote><pre>
  3576         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  3577         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3578         -**  sqlite3_free(zSQL);
  3579         -** </pre></blockquote>
  3580         -**
  3581         -** Because the %q format string is used, the '\'' character in zText
  3582         -** is escaped and the SQL generated is as follows:
  3583         -**
  3584         -** <blockquote><pre>
  3585         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  3586         -** </pre></blockquote>
  3587         -**
  3588         -** This is correct.  Had we used %s instead of %q, the generated SQL
  3589         -** would have looked like this:
  3590         -**
  3591         -** <blockquote><pre>
  3592         -**  INSERT INTO table1 VALUES('It's a happy day!');
  3593         -** </pre></blockquote>
  3594         -**
  3595         -** This second example is an SQL syntax error.  As a general rule you should
  3596         -** always use %q instead of %s when inserting text into a string literal.
  3597         -**
  3598         -** ^(The %Q option works like %q except it also adds single quotes around
  3599         -** the outside of the total string.  Additionally, if the parameter in the
  3600         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  3601         -** single quotes).)^  So, for example, one could say:
  3602         -**
  3603         -** <blockquote><pre>
  3604         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  3605         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3606         -**  sqlite3_free(zSQL);
  3607         -** </pre></blockquote>
  3608         -**
  3609         -** The code above will render a correct SQL statement in the zSQL
  3610         -** variable even if the zText variable is a NULL pointer.
  3611         -**
  3612         -** ^(The "%w" formatting option is like "%q" except that it expects to
  3613         -** be contained within double-quotes instead of single quotes, and it
  3614         -** escapes the double-quote character instead of the single-quote
  3615         -** character.)^  The "%w" formatting option is intended for safely inserting
  3616         -** table and column names into a constructed SQL statement.
  3617         -**
  3618         -** ^(The "%z" formatting option works like "%s" but with the
  3619         -** addition that after the string has been read and copied into
  3620         -** the result, [sqlite3_free()] is called on the input string.)^
         3556  +** See also:  [built-in printf()], [printf() SQL function]
  3621   3557   */
  3622   3558   SQLITE_API char *sqlite3_mprintf(const char*,...);
  3623   3559   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  3624   3560   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  3625   3561   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  3626   3562   
  3627   3563   /*
................................................................................
  4679   4615   ** a schema change, on the first  [sqlite3_step()] call following any change
  4680   4616   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  4681   4617   ** ^The specific value of WHERE-clause [parameter] might influence the 
  4682   4618   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  4683   4619   ** or [GLOB] operator or if the parameter is compared to an indexed column
  4684   4620   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  4685   4621   ** </li>
         4622  +** </ol>
  4686   4623   **
  4687   4624   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  4688   4625   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  4689   4626   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  4690   4627   ** sqlite3_prepare_v2() interface works exactly the same as
  4691   4628   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  4692         -** </ol>
  4693   4629   */
  4694   4630   SQLITE_API int sqlite3_prepare(
  4695   4631     sqlite3 *db,            /* Database handle */
  4696   4632     const char *zSql,       /* SQL statement, UTF-8 encoded */
  4697   4633     int nByte,              /* Maximum length of zSql in bytes. */
  4698   4634     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  4699   4635     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  8313   8249   ** wal file in wal mode databases, or the number of pages written to the
  8314   8250   ** database file in rollback mode databases. Any pages written as part of
  8315   8251   ** transaction rollback or database recovery operations are not included.
  8316   8252   ** If an IO or other error occurs while writing a page to disk, the effect
  8317   8253   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  8318   8254   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  8319   8255   ** </dd>
         8256  +**
         8257  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         8258  +** <dd>This parameter returns the number of dirty cache entries that have
         8259  +** been written to disk in the middle of a transaction due to the page
         8260  +** cache overflowing. Transactions are more efficient if they are written
         8261  +** to disk all at once. When pages spill mid-transaction, that introduces
         8262  +** additional overhead. This parameter can be used help identify
         8263  +** inefficiencies that can be resolve by increasing the cache size.
         8264  +** </dd>
  8320   8265   **
  8321   8266   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  8322   8267   ** <dd>This parameter returns zero for the current value if and only if
  8323   8268   ** all foreign key constraints (deferred or immediate) have been
  8324   8269   ** resolved.)^  ^The highwater mark is always 0.
  8325   8270   ** </dd>
  8326   8271   ** </dl>
................................................................................
  8333   8278   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  8334   8279   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  8335   8280   #define SQLITE_DBSTATUS_CACHE_HIT            7
  8336   8281   #define SQLITE_DBSTATUS_CACHE_MISS           8
  8337   8282   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  8338   8283   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  8339   8284   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  8340         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         8285  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         8286  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  8341   8287   
  8342   8288   
  8343   8289   /*
  8344   8290   ** CAPI3REF: Prepared Statement Status
  8345   8291   ** METHOD: sqlite3_stmt
  8346   8292   **
  8347   8293   ** ^(Each prepared statement maintains various
................................................................................
  9813   9759   ** transaction open on the database, or if the database is not a wal mode
  9814   9760   ** database.
  9815   9761   **
  9816   9762   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  9817   9763   */
  9818   9764   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  9819   9765   
         9766  +/*
         9767  +** CAPI3REF: Serialize a database
         9768  +**
         9769  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         9770  +** that is a serialization of the S database on [database connection] D.
         9771  +** If P is not a NULL pointer, then the size of the database in bytes
         9772  +** is written into *P.
         9773  +**
         9774  +** For an ordinary on-disk database file, the serialization is just a
         9775  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         9776  +** the serialization is the same sequence of bytes which would be written
         9777  +** to disk if that database where backed up to disk.
         9778  +**
         9779  +** The usual case is that sqlite3_serialize() copies the serialization of
         9780  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         9781  +** a pointer to that memory.  The caller is responsible for freeing the
         9782  +** returned value to avoid a memory leak.  However, if the F argument
         9783  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         9784  +** are made, and the sqlite3_serialize() function will return a pointer
         9785  +** to the contiguous memory representation of the database that SQLite
         9786  +** is currently using for that database, or NULL if the no such contiguous
         9787  +** memory representation of the database exists.  A contiguous memory
         9788  +** representation of the database will usually only exist if there has
         9789  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         9790  +** values of D and S.
         9791  +** The size of the database is written into *P even if the 
         9792  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         9793  +** of the database exists.
         9794  +**
         9795  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         9796  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         9797  +** allocation error occurs.
         9798  +**
         9799  +** This interface is only available if SQLite is compiled with the
         9800  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9801  +*/
         9802  +SQLITE_API unsigned char *sqlite3_serialize(
         9803  +  sqlite3 *db,           /* The database connection */
         9804  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         9805  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         9806  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         9807  +);
         9808  +
         9809  +/*
         9810  +** CAPI3REF: Flags for sqlite3_serialize
         9811  +**
         9812  +** Zero or more of the following constants can be OR-ed together for
         9813  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         9814  +**
         9815  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         9816  +** a pointer to contiguous in-memory database that it is currently using,
         9817  +** without making a copy of the database.  If SQLite is not currently using
         9818  +** a contiguous in-memory database, then this option causes
         9819  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         9820  +** using a contiguous in-memory database if it has been initialized by a
         9821  +** prior call to [sqlite3_deserialize()].
         9822  +*/
         9823  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         9824  +
         9825  +/*
         9826  +** CAPI3REF: Deserialize a database
         9827  +**
         9828  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         9829  +** [database connection] D to disconnection from database S and then
         9830  +** reopen S as an in-memory database based on the serialization contained
         9831  +** in P.  The serialized database P is N bytes in size.  M is the size of
         9832  +** the buffer P, which might be larger than N.  If M is larger than N, and
         9833  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         9834  +** permitted to add content to the in-memory database as long as the total
         9835  +** size does not exceed M bytes.
         9836  +**
         9837  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         9838  +** invoke sqlite3_free() on the serialization buffer when the database
         9839  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         9840  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         9841  +** if writes on the database cause it to grow larger than M bytes.
         9842  +**
         9843  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         9844  +** database is currently in a read transaction or is involved in a backup
         9845  +** operation.
         9846  +**
         9847  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         9848  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         9849  +** [sqlite3_free()] is invoked on argument P prior to returning.
         9850  +**
         9851  +** This interface is only available if SQLite is compiled with the
         9852  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9853  +*/
         9854  +SQLITE_API int sqlite3_deserialize(
         9855  +  sqlite3 *db,            /* The database connection */
         9856  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         9857  +  unsigned char *pData,   /* The serialized database content */
         9858  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         9859  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         9860  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         9861  +);
         9862  +
         9863  +/*
         9864  +** CAPI3REF: Flags for sqlite3_deserialize()
         9865  +**
         9866  +** The following are allowed values for 6th argument (the F argument) to
         9867  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         9868  +**
         9869  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         9870  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         9871  +** and that SQLite should take ownership of this memory and automatically
         9872  +** free it when it has finished using it.  Without this flag, the caller
         9873  +** is resposible for freeing any dynamically allocated memory.
         9874  +**
         9875  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         9876  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
         9877  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         9878  +** Without this flag, the deserialized database cannot increase in size beyond
         9879  +** the number of bytes specified by the M parameter.
         9880  +**
         9881  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         9882  +** should be treated as read-only.
         9883  +*/
         9884  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         9885  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         9886  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         9887  +
  9820   9888   /*
  9821   9889   ** Undo the hack that converts floating point types to integer for
  9822   9890   ** builds on processors without floating point support.
  9823   9891   */
  9824   9892   #ifdef SQLITE_OMIT_FLOATING_POINT
  9825   9893   # undef double
  9826   9894   #endif
................................................................................
  9960  10028   #if 0
  9961  10029   extern "C" {
  9962  10030   #endif
  9963  10031   
  9964  10032   
  9965  10033   /*
  9966  10034   ** CAPI3REF: Session Object Handle
        10035  +**
        10036  +** An instance of this object is a [session] that can be used to
        10037  +** record changes to a database.
  9967  10038   */
  9968  10039   typedef struct sqlite3_session sqlite3_session;
  9969  10040   
  9970  10041   /*
  9971  10042   ** CAPI3REF: Changeset Iterator Handle
        10043  +**
        10044  +** An instance of this object acts as a cursor for iterating
        10045  +** over the elements of a [changeset] or [patchset].
  9972  10046   */
  9973  10047   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  9974  10048   
  9975  10049   /*
  9976  10050   ** CAPI3REF: Create A New Session Object
        10051  +** CONSTRUCTOR: sqlite3_session
  9977  10052   **
  9978  10053   ** Create a new session object attached to database handle db. If successful,
  9979  10054   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  9980  10055   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  9981  10056   ** error code (e.g. SQLITE_NOMEM) is returned.
  9982  10057   **
  9983  10058   ** It is possible to create multiple session objects attached to a single
................................................................................
 10006  10081     sqlite3 *db,                    /* Database handle */
 10007  10082     const char *zDb,                /* Name of db (e.g. "main") */
 10008  10083     sqlite3_session **ppSession     /* OUT: New session object */
 10009  10084   );
 10010  10085   
 10011  10086   /*
 10012  10087   ** CAPI3REF: Delete A Session Object
        10088  +** DESTRUCTOR: sqlite3_session
 10013  10089   **
 10014  10090   ** Delete a session object previously allocated using 
 10015  10091   ** [sqlite3session_create()]. Once a session object has been deleted, the
 10016  10092   ** results of attempting to use pSession with any other session module
 10017  10093   ** function are undefined.
 10018  10094   **
 10019  10095   ** Session objects must be deleted before the database handle to which they
................................................................................
 10021  10097   ** [sqlite3session_create()] for details.
 10022  10098   */
 10023  10099   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
 10024  10100   
 10025  10101   
 10026  10102   /*
 10027  10103   ** CAPI3REF: Enable Or Disable A Session Object
        10104  +** METHOD: sqlite3_session
 10028  10105   **
 10029  10106   ** Enable or disable the recording of changes by a session object. When
 10030  10107   ** enabled, a session object records changes made to the database. When
 10031  10108   ** disabled - it does not. A newly created session object is enabled.
 10032  10109   ** Refer to the documentation for [sqlite3session_changeset()] for further
 10033  10110   ** details regarding how enabling and disabling a session object affects
 10034  10111   ** the eventual changesets.
................................................................................
 10040  10117   ** The return value indicates the final state of the session object: 0 if 
 10041  10118   ** the session is disabled, or 1 if it is enabled.
 10042  10119   */
 10043  10120   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
 10044  10121   
 10045  10122   /*
 10046  10123   ** CAPI3REF: Set Or Clear the Indirect Change Flag
        10124  +** METHOD: sqlite3_session
 10047  10125   **
 10048  10126   ** Each change recorded by a session object is marked as either direct or
 10049  10127   ** indirect. A change is marked as indirect if either:
 10050  10128   **
 10051  10129   ** <ul>
 10052  10130   **   <li> The session object "indirect" flag is set when the change is
 10053  10131   **        made, or
................................................................................
 10069  10147   ** The return value indicates the final state of the indirect flag: 0 if 
 10070  10148   ** it is clear, or 1 if it is set.
 10071  10149   */
 10072  10150   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
 10073  10151   
 10074  10152   /*
 10075  10153   ** CAPI3REF: Attach A Table To A Session Object
        10154  +** METHOD: sqlite3_session
 10076  10155   **
 10077  10156   ** If argument zTab is not NULL, then it is the name of a table to attach
 10078  10157   ** to the session object passed as the first argument. All subsequent changes 
 10079  10158   ** made to the table while the session object is enabled will be recorded. See 
 10080  10159   ** documentation for [sqlite3session_changeset()] for further details.
 10081  10160   **
 10082  10161   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
 10131  10210   SQLITE_API int sqlite3session_attach(
 10132  10211     sqlite3_session *pSession,      /* Session object */
 10133  10212     const char *zTab                /* Table name */
 10134  10213   );
 10135  10214   
 10136  10215   /*
 10137  10216   ** CAPI3REF: Set a table filter on a Session Object.
        10217  +** METHOD: sqlite3_session
 10138  10218   **
 10139  10219   ** The second argument (xFilter) is the "filter callback". For changes to rows 
 10140  10220   ** in tables that are not attached to the Session object, the filter is called
 10141  10221   ** to determine whether changes to the table's rows should be tracked or not. 
 10142  10222   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
 10143  10223   ** attached, xFilter will not be called again.
 10144  10224   */
................................................................................
 10149  10229       const char *zTab              /* Table name */
 10150  10230     ),
 10151  10231     void *pCtx                      /* First argument passed to xFilter */
 10152  10232   );
 10153  10233   
 10154  10234   /*
 10155  10235   ** CAPI3REF: Generate A Changeset From A Session Object
        10236  +** METHOD: sqlite3_session
 10156  10237   **
 10157  10238   ** Obtain a changeset containing changes to the tables attached to the 
 10158  10239   ** session object passed as the first argument. If successful, 
 10159  10240   ** set *ppChangeset to point to a buffer containing the changeset 
 10160  10241   ** and *pnChangeset to the size of the changeset in bytes before returning
 10161  10242   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
 10162  10243   ** zero and return an SQLite error code.
................................................................................
 10258  10339   SQLITE_API int sqlite3session_changeset(
 10259  10340     sqlite3_session *pSession,      /* Session object */
 10260  10341     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
 10261  10342     void **ppChangeset              /* OUT: Buffer containing changeset */
 10262  10343   );
 10263  10344   
 10264  10345   /*
 10265         -** CAPI3REF: Load The Difference Between Tables Into A Session 
        10346  +** CAPI3REF: Load The Difference Between Tables Into A Session
        10347  +** METHOD: sqlite3_session
 10266  10348   **
 10267  10349   ** If it is not already attached to the session object passed as the first
 10268  10350   ** argument, this function attaches table zTbl in the same manner as the
 10269  10351   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
 10270  10352   ** does not have a primary key, this function is a no-op (but does not return
 10271  10353   ** an error).
 10272  10354   **
................................................................................
 10323  10405     const char *zTbl,
 10324  10406     char **pzErrMsg
 10325  10407   );
 10326  10408   
 10327  10409   
 10328  10410   /*
 10329  10411   ** CAPI3REF: Generate A Patchset From A Session Object
        10412  +** METHOD: sqlite3_session
 10330  10413   **
 10331  10414   ** The differences between a patchset and a changeset are that:
 10332  10415   **
 10333  10416   ** <ul>
 10334  10417   **   <li> DELETE records consist of the primary key fields only. The 
 10335  10418   **        original values of other fields are omitted.
 10336  10419   **   <li> The original values of any modified fields are omitted from 
................................................................................
 10374  10457   ** guaranteed that a call to sqlite3session_changeset() will return a 
 10375  10458   ** changeset containing zero changes.
 10376  10459   */
 10377  10460   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
 10378  10461   
 10379  10462   /*
 10380  10463   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
        10464  +** CONSTRUCTOR: sqlite3_changeset_iter
 10381  10465   **
 10382  10466   ** Create an iterator used to iterate through the contents of a changeset.
 10383  10467   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
 10384  10468   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
 10385  10469   ** SQLite error code is returned.
 10386  10470   **
 10387  10471   ** The following functions can be used to advance and query a changeset 
................................................................................
 10414  10498     int nChangeset,                 /* Size of changeset blob in bytes */
 10415  10499     void *pChangeset                /* Pointer to blob containing changeset */
 10416  10500   );
 10417  10501   
 10418  10502   
 10419  10503   /*
 10420  10504   ** CAPI3REF: Advance A Changeset Iterator
        10505  +** METHOD: sqlite3_changeset_iter
 10421  10506   **
 10422  10507   ** This function may only be used with iterators created by function
 10423  10508   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
 10424  10509   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
 10425  10510   ** is returned and the call has no effect.
 10426  10511   **
 10427  10512   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
 10438  10523   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
 10439  10524   ** SQLITE_NOMEM.
 10440  10525   */
 10441  10526   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
 10442  10527   
 10443  10528   /*
 10444  10529   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
        10530  +** METHOD: sqlite3_changeset_iter
 10445  10531   **
 10446  10532   ** The pIter argument passed to this function may either be an iterator
 10447  10533   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10448  10534   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10449  10535   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
 10450  10536   ** is not the case, this function returns [SQLITE_MISUSE].
 10451  10537   **
................................................................................
 10472  10558     int *pnCol,                     /* OUT: Number of columns in table */
 10473  10559     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
 10474  10560     int *pbIndirect                 /* OUT: True for an 'indirect' change */
 10475  10561   );
 10476  10562   
 10477  10563   /*
 10478  10564   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
        10565  +** METHOD: sqlite3_changeset_iter
 10479  10566   **
 10480  10567   ** For each modified table, a changeset includes the following:
 10481  10568   **
 10482  10569   ** <ul>
 10483  10570   **   <li> The number of columns in the table, and
 10484  10571   **   <li> Which of those columns make up the tables PRIMARY KEY.
 10485  10572   ** </ul>
................................................................................
 10503  10590     sqlite3_changeset_iter *pIter,  /* Iterator object */
 10504  10591     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
 10505  10592     int *pnCol                      /* OUT: Number of entries in output array */
 10506  10593   );
 10507  10594   
 10508  10595   /*
 10509  10596   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
        10597  +** METHOD: sqlite3_changeset_iter
 10510  10598   **
 10511  10599   ** The pIter argument passed to this function may either be an iterator
 10512  10600   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10513  10601   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10514  10602   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10515  10603   ** Furthermore, it may only be called if the type of change that the iterator
 10516  10604   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
 10533  10621     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10534  10622     int iVal,                       /* Column number */
 10535  10623     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
 10536  10624   );
 10537  10625   
 10538  10626   /*
 10539  10627   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
        10628  +** METHOD: sqlite3_changeset_iter
 10540  10629   **
 10541  10630   ** The pIter argument passed to this function may either be an iterator
 10542  10631   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10543  10632   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10544  10633   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10545  10634   ** Furthermore, it may only be called if the type of change that the iterator
 10546  10635   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
 10566  10655     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10567  10656     int iVal,                       /* Column number */
 10568  10657     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
 10569  10658   );
 10570  10659   
 10571  10660   /*
 10572  10661   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
        10662  +** METHOD: sqlite3_changeset_iter
 10573  10663   **
 10574  10664   ** This function should only be used with iterator objects passed to a
 10575  10665   ** conflict-handler callback by [sqlite3changeset_apply()] with either
 10576  10666   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
 10577  10667   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
 10578  10668   ** is set to NULL.
 10579  10669   **
................................................................................
 10593  10683     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10594  10684     int iVal,                       /* Column number */
 10595  10685     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
 10596  10686   );
 10597  10687   
 10598  10688   /*
 10599  10689   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
        10690  +** METHOD: sqlite3_changeset_iter
 10600  10691   **
 10601  10692   ** This function may only be called with an iterator passed to an
 10602  10693   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
 10603  10694   ** it sets the output variable to the total number of known foreign key
 10604  10695   ** violations in the destination database and returns SQLITE_OK.
 10605  10696   **
 10606  10697   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
 10609  10700     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10610  10701     int *pnOut                      /* OUT: Number of FK violations */
 10611  10702   );
 10612  10703   
 10613  10704   
 10614  10705   /*
 10615  10706   ** CAPI3REF: Finalize A Changeset Iterator
        10707  +** METHOD: sqlite3_changeset_iter
 10616  10708   **
 10617  10709   ** This function is used to finalize an iterator allocated with
 10618  10710   ** [sqlite3changeset_start()].
 10619  10711   **
 10620  10712   ** This function should only be called on iterators created using the
 10621  10713   ** [sqlite3changeset_start()] function. If an application calls this
 10622  10714   ** function with an iterator passed to a conflict-handler by
................................................................................
 10625  10717   **
 10626  10718   ** If an error was encountered within a call to an sqlite3changeset_xxx()
 10627  10719   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
 10628  10720   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
 10629  10721   ** to that error is returned by this function. Otherwise, SQLITE_OK is
 10630  10722   ** returned. This is to allow the following pattern (pseudo-code):
 10631  10723   **
        10724  +** <pre>
 10632  10725   **   sqlite3changeset_start();
 10633  10726   **   while( SQLITE_ROW==sqlite3changeset_next() ){
 10634  10727   **     // Do something with change.
 10635  10728   **   }
 10636  10729   **   rc = sqlite3changeset_finalize();
 10637  10730   **   if( rc!=SQLITE_OK ){
 10638  10731   **     // An error has occurred 
 10639  10732   **   }
        10733  +** </pre>
 10640  10734   */
 10641  10735   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
 10642  10736   
 10643  10737   /*
 10644  10738   ** CAPI3REF: Invert A Changeset
 10645  10739   **
 10646  10740   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
 10680  10774   ** single changeset. The result is a changeset equivalent to applying
 10681  10775   ** changeset A followed by changeset B. 
 10682  10776   **
 10683  10777   ** This function combines the two input changesets using an 
 10684  10778   ** sqlite3_changegroup object. Calling it produces similar results as the
 10685  10779   ** following code fragment:
 10686  10780   **
        10781  +** <pre>
 10687  10782   **   sqlite3_changegroup *pGrp;
 10688  10783   **   rc = sqlite3_changegroup_new(&pGrp);
 10689  10784   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
 10690  10785   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
 10691  10786   **   if( rc==SQLITE_OK ){
 10692  10787   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
 10693  10788   **   }else{
 10694  10789   **     *ppOut = 0;
 10695  10790   **     *pnOut = 0;
 10696  10791   **   }
        10792  +** </pre>
 10697  10793   **
 10698  10794   ** Refer to the sqlite3_changegroup documentation below for details.
 10699  10795   */
 10700  10796   SQLITE_API int sqlite3changeset_concat(
 10701  10797     int nA,                         /* Number of bytes in buffer pA */
 10702  10798     void *pA,                       /* Pointer to buffer containing changeset A */
 10703  10799     int nB,                         /* Number of bytes in buffer pB */
................................................................................
 10705  10801     int *pnOut,                     /* OUT: Number of bytes in output changeset */
 10706  10802     void **ppOut                    /* OUT: Buffer containing output changeset */
 10707  10803   );
 10708  10804   
 10709  10805   
 10710  10806   /*
 10711  10807   ** CAPI3REF: Changegroup Handle
        10808  +**
        10809  +** A changegroup is an object used to combine two or more 
        10810  +** [changesets] or [patchsets]
 10712  10811   */
 10713  10812   typedef struct sqlite3_changegroup sqlite3_changegroup;
 10714  10813   
 10715  10814   /*
 10716  10815   ** CAPI3REF: Create A New Changegroup Object
        10816  +** CONSTRUCTOR: sqlite3_changegroup
 10717  10817   **
 10718  10818   ** An sqlite3_changegroup object is used to combine two or more changesets
 10719  10819   ** (or patchsets) into a single changeset (or patchset). A single changegroup
 10720  10820   ** object may combine changesets or patchsets, but not both. The output is
 10721  10821   ** always in the same format as the input.
 10722  10822   **
 10723  10823   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
 10747  10847   ** sqlite3changegroup_output() functions, also available are the streaming
 10748  10848   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
 10749  10849   */
 10750  10850   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
 10751  10851   
 10752  10852   /*
 10753  10853   ** CAPI3REF: Add A Changeset To A Changegroup
        10854  +** METHOD: sqlite3_changegroup
 10754  10855   **
 10755  10856   ** Add all changes within the changeset (or patchset) in buffer pData (size
 10756  10857   ** nData bytes) to the changegroup. 
 10757  10858   **
 10758  10859   ** If the buffer contains a patchset, then all prior calls to this function
 10759  10860   ** on the same changegroup object must also have specified patchsets. Or, if
 10760  10861   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
 10824  10925   **
 10825  10926   ** If no error occurs, SQLITE_OK is returned.
 10826  10927   */
 10827  10928   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 10828  10929   
 10829  10930   /*
 10830  10931   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
        10932  +** METHOD: sqlite3_changegroup
 10831  10933   **
 10832  10934   ** Obtain a buffer containing a changeset (or patchset) representing the
 10833  10935   ** current contents of the changegroup. If the inputs to the changegroup
 10834  10936   ** were themselves changesets, the output is a changeset. Or, if the
 10835  10937   ** inputs were patchsets, the output is also a patchset.
 10836  10938   **
 10837  10939   ** As with the output of the sqlite3session_changeset() and
................................................................................
 10854  10956     sqlite3_changegroup*,
 10855  10957     int *pnData,                    /* OUT: Size of output buffer in bytes */
 10856  10958     void **ppData                   /* OUT: Pointer to output buffer */
 10857  10959   );
 10858  10960   
 10859  10961   /*
 10860  10962   ** CAPI3REF: Delete A Changegroup Object
        10963  +** DESTRUCTOR: sqlite3_changegroup
 10861  10964   */
 10862  10965   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 10863  10966   
 10864  10967   /*
 10865  10968   ** CAPI3REF: Apply A Changeset To A Database
 10866  10969   **
 10867  10970   ** Apply a changeset to a database. This function attempts to update the
................................................................................
 12669  12772   #define TK_CASE                           136
 12670  12773   #define TK_WHEN                           137
 12671  12774   #define TK_THEN                           138
 12672  12775   #define TK_ELSE                           139
 12673  12776   #define TK_INDEX                          140
 12674  12777   #define TK_ALTER                          141
 12675  12778   #define TK_ADD                            142
 12676         -#define TK_ISNOT                          143
 12677         -#define TK_FUNCTION                       144
 12678         -#define TK_COLUMN                         145
 12679         -#define TK_AGG_FUNCTION                   146
 12680         -#define TK_AGG_COLUMN                     147
 12681         -#define TK_UMINUS                         148
 12682         -#define TK_UPLUS                          149
 12683         -#define TK_REGISTER                       150
 12684         -#define TK_VECTOR                         151
 12685         -#define TK_SELECT_COLUMN                  152
 12686         -#define TK_IF_NULL_ROW                    153
 12687         -#define TK_ASTERISK                       154
 12688         -#define TK_SPAN                           155
 12689         -#define TK_END_OF_FILE                    156
 12690         -#define TK_UNCLOSED_STRING                157
 12691         -#define TK_SPACE                          158
 12692         -#define TK_ILLEGAL                        159
        12779  +#define TK_TRUEFALSE                      143
        12780  +#define TK_ISNOT                          144
        12781  +#define TK_FUNCTION                       145
        12782  +#define TK_COLUMN                         146
        12783  +#define TK_AGG_FUNCTION                   147
        12784  +#define TK_AGG_COLUMN                     148
        12785  +#define TK_UMINUS                         149
        12786  +#define TK_UPLUS                          150
        12787  +#define TK_TRUTH                          151
        12788  +#define TK_REGISTER                       152
        12789  +#define TK_VECTOR                         153
        12790  +#define TK_SELECT_COLUMN                  154
        12791  +#define TK_IF_NULL_ROW                    155
        12792  +#define TK_ASTERISK                       156
        12793  +#define TK_SPAN                           157
        12794  +#define TK_END_OF_FILE                    158
        12795  +#define TK_UNCLOSED_STRING                159
        12796  +#define TK_SPACE                          160
        12797  +#define TK_ILLEGAL                        161
 12693  12798   
 12694  12799   /* The token codes above must all fit in 8 bits */
 12695  12800   #define TKFLG_MASK           0xff  
 12696  12801   
 12697  12802   /* Flags that can be added to a token code when it is not
 12698  12803   ** being stored in a u8: */
 12699  12804   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13931  14036   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13932  14037   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 13933  14038   #define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 13934  14039   #define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 13935  14040   #define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 13936  14041   #define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 13937  14042   #define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 13938         -#define OP_Offset         96 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14043  +#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 13939  14044   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 13940         -#define OP_Column         98 /* synopsis: r[P3]=PX                         */
 13941         -#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
 13942         -#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 13943         -#define OP_Count         101 /* synopsis: r[P2]=count()                    */
 13944         -#define OP_ReadCookie    102
 13945         -#define OP_SetCookie     103
 13946         -#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
 13947         -#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
 13948         -#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
 13949         -#define OP_OpenDup       107
 13950         -#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2                       */
 13951         -#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2                       */
 13952         -#define OP_SorterOpen    110
 13953         -#define OP_SequenceTest  111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 13954         -#define OP_OpenPseudo    112 /* synopsis: P3 columns in r[P2]              */
 13955         -#define OP_Close         113
 13956         -#define OP_ColumnsUsed   114
 13957         -#define OP_Sequence      115 /* synopsis: r[P2]=cursor[P1].ctr++           */
 13958         -#define OP_NewRowid      116 /* synopsis: r[P2]=rowid                      */
 13959         -#define OP_Insert        117 /* synopsis: intkey=r[P3] data=r[P2]          */
 13960         -#define OP_InsertInt     118 /* synopsis: intkey=P3 data=r[P2]             */
 13961         -#define OP_Delete        119
 13962         -#define OP_ResetCount    120
 13963         -#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13964         -#define OP_SorterData    122 /* synopsis: r[P2]=data                       */
 13965         -#define OP_RowData       123 /* synopsis: r[P2]=data                       */
 13966         -#define OP_Rowid         124 /* synopsis: r[P2]=rowid                      */
 13967         -#define OP_NullRow       125
 13968         -#define OP_SeekEnd       126
 13969         -#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
 13970         -#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
 13971         -#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
 13972         -#define OP_DeferredSeek  130 /* synopsis: Move P3 to P1.rowid if needed    */
 13973         -#define OP_IdxRowid      131 /* synopsis: r[P2]=rowid                      */
        14045  +#define OP_Offset         98 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14046  +#define OP_Column         99 /* synopsis: r[P3]=PX                         */
        14047  +#define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
        14048  +#define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14049  +#define OP_Count         102 /* synopsis: r[P2]=count()                    */
        14050  +#define OP_ReadCookie    103
        14051  +#define OP_SetCookie     104
        14052  +#define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
        14053  +#define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
        14054  +#define OP_OpenWrite     107 /* synopsis: root=P2 iDb=P3                   */
        14055  +#define OP_OpenDup       108
        14056  +#define OP_OpenAutoindex 109 /* synopsis: nColumn=P2                       */
        14057  +#define OP_OpenEphemeral 110 /* synopsis: nColumn=P2                       */
        14058  +#define OP_SorterOpen    111
        14059  +#define OP_SequenceTest  112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        14060  +#define OP_OpenPseudo    113 /* synopsis: P3 columns in r[P2]              */
        14061  +#define OP_Close         114
        14062  +#define OP_ColumnsUsed   115
        14063  +#define OP_Sequence      116 /* synopsis: r[P2]=cursor[P1].ctr++           */
        14064  +#define OP_NewRowid      117 /* synopsis: r[P2]=rowid                      */
        14065  +#define OP_Insert        118 /* synopsis: intkey=r[P3] data=r[P2]          */
        14066  +#define OP_InsertInt     119 /* synopsis: intkey=P3 data=r[P2]             */
        14067  +#define OP_Delete        120
        14068  +#define OP_ResetCount    121
        14069  +#define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        14070  +#define OP_SorterData    123 /* synopsis: r[P2]=data                       */
        14071  +#define OP_RowData       124 /* synopsis: r[P2]=data                       */
        14072  +#define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
        14073  +#define OP_NullRow       126
        14074  +#define OP_SeekEnd       127
        14075  +#define OP_SorterInsert  128 /* synopsis: key=r[P2]                        */
        14076  +#define OP_IdxInsert     129 /* synopsis: key=r[P2]                        */
        14077  +#define OP_IdxDelete     130 /* synopsis: key=r[P2@P3]                     */
        14078  +#define OP_DeferredSeek  131 /* synopsis: Move P3 to P1.rowid if needed    */
 13974  14079   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13975         -#define OP_Destroy       133
 13976         -#define OP_Clear         134
 13977         -#define OP_ResetSorter   135
 13978         -#define OP_CreateBtree   136 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13979         -#define OP_SqlExec       137
 13980         -#define OP_ParseSchema   138
 13981         -#define OP_LoadAnalysis  139
 13982         -#define OP_DropTable     140
 13983         -#define OP_DropIndex     141
 13984         -#define OP_DropTrigger   142
 13985         -#define OP_IntegrityCk   143
 13986         -#define OP_RowSetAdd     144 /* synopsis: rowset(P1)=r[P2]                 */
 13987         -#define OP_Param         145
 13988         -#define OP_FkCounter     146 /* synopsis: fkctr[P1]+=P2                    */
 13989         -#define OP_MemMax        147 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 13990         -#define OP_OffsetLimit   148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 13991         -#define OP_AggStep0      149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13992         -#define OP_AggStep       150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13993         -#define OP_AggFinal      151 /* synopsis: accum=r[P1] N=P2                 */
 13994         -#define OP_Expire        152
 13995         -#define OP_TableLock     153 /* synopsis: iDb=P1 root=P2 write=P3          */
 13996         -#define OP_VBegin        154
 13997         -#define OP_VCreate       155
 13998         -#define OP_VDestroy      156
 13999         -#define OP_VOpen         157
 14000         -#define OP_VColumn       158 /* synopsis: r[P3]=vcolumn(P2)                */
 14001         -#define OP_VRename       159
 14002         -#define OP_Pagecount     160
 14003         -#define OP_MaxPgcnt      161
 14004         -#define OP_PureFunc0     162
 14005         -#define OP_Function0     163 /* synopsis: r[P3]=func(r[P2@P5])             */
 14006         -#define OP_PureFunc      164
 14007         -#define OP_Function      165 /* synopsis: r[P3]=func(r[P2@P5])             */
 14008         -#define OP_Trace         166
 14009         -#define OP_CursorHint    167
 14010         -#define OP_Noop          168
 14011         -#define OP_Explain       169
        14080  +#define OP_IdxRowid      133 /* synopsis: r[P2]=rowid                      */
        14081  +#define OP_Destroy       134
        14082  +#define OP_Clear         135
        14083  +#define OP_ResetSorter   136
        14084  +#define OP_CreateBtree   137 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        14085  +#define OP_SqlExec       138
        14086  +#define OP_ParseSchema   139
        14087  +#define OP_LoadAnalysis  140
        14088  +#define OP_DropTable     141
        14089  +#define OP_DropIndex     142
        14090  +#define OP_DropTrigger   143
        14091  +#define OP_IntegrityCk   144
        14092  +#define OP_RowSetAdd     145 /* synopsis: rowset(P1)=r[P2]                 */
        14093  +#define OP_Param         146
        14094  +#define OP_FkCounter     147 /* synopsis: fkctr[P1]+=P2                    */
        14095  +#define OP_MemMax        148 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        14096  +#define OP_OffsetLimit   149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        14097  +#define OP_AggStep0      150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14098  +#define OP_AggStep       151 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14099  +#define OP_AggFinal      152 /* synopsis: accum=r[P1] N=P2                 */
        14100  +#define OP_Expire        153
        14101  +#define OP_TableLock     154 /* synopsis: iDb=P1 root=P2 write=P3          */
        14102  +#define OP_VBegin        155
        14103  +#define OP_VCreate       156
        14104  +#define OP_VDestroy      157
        14105  +#define OP_VOpen         158
        14106  +#define OP_VColumn       159 /* synopsis: r[P3]=vcolumn(P2)                */
        14107  +#define OP_VRename       160
        14108  +#define OP_Pagecount     161
        14109  +#define OP_MaxPgcnt      162
        14110  +#define OP_PureFunc0     163
        14111  +#define OP_Function0     164 /* synopsis: r[P3]=func(r[P2@P5])             */
        14112  +#define OP_PureFunc      165
        14113  +#define OP_Function      166 /* synopsis: r[P3]=func(r[P2@P5])             */
        14114  +#define OP_Trace         167
        14115  +#define OP_CursorHint    168
        14116  +#define OP_Noop          169
        14117  +#define OP_Explain       170
 14012  14118   
 14013  14119   /* Properties such as "out2" or "jump" that are specified in
 14014  14120   ** comments following the "case" for each opcode in the vdbe.c
 14015  14121   ** are encoded into bitvectors as follows:
 14016  14122   */
 14017  14123   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14018  14124   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14029  14135   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14030  14136   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14031  14137   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14032  14138   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14033  14139   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14034  14140   /*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14035  14141   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14036         -/*  96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        14142  +/*  96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
 14037  14143   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14038         -/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
 14039         -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
 14040         -/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 14041         -/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14042         -/* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
        14144  +/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        14145  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
        14146  +/* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
        14147  +/* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14148  +/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 14043  14149   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14044         -/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14045         -/* 168 */ 0x00, 0x00,}
        14150  +/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14151  +/* 168 */ 0x00, 0x00, 0x00,}
 14046  14152   
 14047  14153   /* The sqlite3P2Values() routine is able to run faster if it knows
 14048  14154   ** the value of the largest JUMP opcode.  The smaller the maximum
 14049  14155   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14050  14156   ** generated this include file strives to group all JUMP opcodes
 14051  14157   ** together near the beginning of the list.
 14052  14158   */
................................................................................
 15244  15350     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15245  15351     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15246  15352     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 15247  15353     struct sqlite3InitInfo {      /* Information used during initialization */
 15248  15354       int newTnum;                /* Rootpage of table being initialized */
 15249  15355       u8 iDb;                     /* Which db file is being initialized */
 15250  15356       u8 busy;                    /* TRUE if currently initializing */
 15251         -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 15252         -    u8 imposterTable;           /* Building an imposter table */
        15357  +    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
        15358  +    unsigned imposterTable : 1; /* Building an imposter table */
        15359  +    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
 15253  15360     } init;
 15254  15361     int nVdbeActive;              /* Number of VDBEs currently running */
 15255  15362     int nVdbeRead;                /* Number of active VDBEs that read or write */
 15256  15363     int nVdbeWrite;               /* Number of active VDBEs that read and write */
 15257  15364     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 15258  15365     int nVDestroy;                /* Number of active OP_VDestroy operations */
 15259  15366     int nExtension;               /* Number of loaded extensions */
................................................................................
 15633  15740   };
 15634  15741   
 15635  15742   /* Allowed values for Column.colFlags:
 15636  15743   */
 15637  15744   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 15638  15745   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 15639  15746   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
        15747  +#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
 15640  15748   
 15641  15749   /*
 15642  15750   ** A "Collating Sequence" is defined by an instance of the following
 15643  15751   ** structure. Conceptually, a collating sequence consists of a name and
 15644  15752   ** a comparison routine that defines the order of that sequence.
 15645  15753   **
 15646  15754   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 17717  17825   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 17718  17826   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 17719  17827   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 17720  17828   SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
 17721  17829   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 17722  17830   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 17723  17831   SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
        17832  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
        17833  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
 17724  17834   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 17725  17835   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 17726  17836   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
 17727  17837   SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
 17728  17838   SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
 17729  17839   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 17730  17840   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
................................................................................
 17898  18008   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 17899  18009   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 17900  18010   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 17901  18011   
 17902  18012   #if defined(SQLITE_NEED_ERR_NAME)
 17903  18013   SQLITE_PRIVATE const char *sqlite3ErrName(int);
 17904  18014   #endif
        18015  +
        18016  +#ifdef SQLITE_ENABLE_DESERIALIZE
        18017  +SQLITE_PRIVATE int sqlite3MemdbInit(void);
        18018  +#endif
 17905  18019   
 17906  18020   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 17907  18021   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 17908  18022   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 17909  18023   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 17910  18024   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 17911  18025   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
................................................................................
 17947  18061   SQLITE_PRIVATE const Token sqlite3IntTokens[];
 17948  18062   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 17949  18063   SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
 17950  18064   #ifndef SQLITE_OMIT_WSD
 17951  18065   SQLITE_PRIVATE int sqlite3PendingByte;
 17952  18066   #endif
 17953  18067   #endif
        18068  +#ifdef VDBE_PROFILE
        18069  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
        18070  +#endif
 17954  18071   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 17955  18072   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 17956  18073   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 17957  18074   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 17958  18075   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 17959  18076   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 17960  18077   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
................................................................................
 18570  18687   ** Constant tokens for values 0 and 1.
 18571  18688   */
 18572  18689   SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 18573  18690      { "0", 1 },
 18574  18691      { "1", 1 }
 18575  18692   };
 18576  18693   
        18694  +#ifdef VDBE_PROFILE
        18695  +/*
        18696  +** The following performance counter can be used in place of
        18697  +** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
        18698  +*/
        18699  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
        18700  +#endif
 18577  18701   
 18578  18702   /*
 18579  18703   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 18580  18704   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 18581  18705   ** the database page that contains the pending byte.  It never attempts
 18582  18706   ** to read or write that page.  The pending byte page is set aside
 18583  18707   ** for use by the VFS layers as space for managing file locks.
................................................................................
 18853  18977   
 18854  18978   /* One or more of the following flags are set to indicate the validOK
 18855  18979   ** representations of the value stored in the Mem struct.
 18856  18980   **
 18857  18981   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 18858  18982   ** For a pointer type created using sqlite3_bind_pointer() or
 18859  18983   ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
 18860         -** If both MEM_Null and MEM_Zero are set, that means that the value is
 18861         -** an unchanging column value from VColumn.
 18862  18984   **
 18863  18985   ** If the MEM_Str flag is set then Mem.z points at a string representation.
 18864  18986   ** Usually this is encoded in the same unicode encoding as the main
 18865  18987   ** database (see below for exceptions). If the MEM_Term flag is also
 18866  18988   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 18867  18989   ** flags may coexist with the MEM_Str flag.
 18868  18990   */
................................................................................
 18948  19070     Mem *pOut;              /* The return value is stored here */
 18949  19071     FuncDef *pFunc;         /* Pointer to function information */
 18950  19072     Mem *pMem;              /* Memory cell used to store aggregate context */
 18951  19073     Vdbe *pVdbe;            /* The VM that owns this context */
 18952  19074     int iOp;                /* Instruction number of OP_Function */
 18953  19075     int isError;            /* Error code returned by the function. */
 18954  19076     u8 skipFlag;            /* Skip accumulator loading if true */
 18955         -  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
 18956  19077     u8 argc;                /* Number of arguments */
 18957  19078     sqlite3_value *argv[1]; /* Argument set */
 18958  19079   };
 18959  19080   
 18960  19081   /* A bitfield type for use inside of structures.  Always follow with :N where
 18961  19082   ** N is the number of bits.
 18962  19083   */
................................................................................
 19118  19239   SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 19119  19240   SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 19120  19241   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 19121  19242   SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
 19122  19243   SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 19123  19244   SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 19124  19245   SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
        19246  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
 19125  19247   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 19126  19248   SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 19127  19249   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 19128  19250   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 19129  19251   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 19130  19252   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 19131  19253   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
................................................................................
 19511  19633       }
 19512  19634   
 19513  19635       /*
 19514  19636       ** Set *pCurrent to the total cache hits or misses encountered by all
 19515  19637       ** pagers the database handle is connected to. *pHighwater is always set 
 19516  19638       ** to zero.
 19517  19639       */
        19640  +    case SQLITE_DBSTATUS_CACHE_SPILL:
        19641  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
        19642  +      /* Fall through into the next case */
 19518  19643       case SQLITE_DBSTATUS_CACHE_HIT:
 19519  19644       case SQLITE_DBSTATUS_CACHE_MISS:
 19520  19645       case SQLITE_DBSTATUS_CACHE_WRITE:{
 19521  19646         int i;
 19522  19647         int nRet = 0;
 19523  19648         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 19524  19649         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
................................................................................
 24104  24229   #if SQLITE_MUTEX_NREF
 24105  24230     volatile int nRef;         /* Number of entrances */
 24106  24231     volatile pthread_t owner;  /* Thread that is within this mutex */
 24107  24232     int trace;                 /* True to trace changes */
 24108  24233   #endif
 24109  24234   };
 24110  24235   #if SQLITE_MUTEX_NREF
 24111         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
        24236  +# define SQLITE3_MUTEX_INITIALIZER(id) \
        24237  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
 24112  24238   #elif defined(SQLITE_ENABLE_API_ARMOR)
 24113         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
        24239  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
 24114  24240   #else
 24115         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
        24241  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
 24116  24242   #endif
 24117  24243   
 24118  24244   /*
 24119  24245   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24120  24246   ** intended for use only inside assert() statements.  On some platforms,
 24121  24247   ** there might be race conditions that can cause these routines to
 24122  24248   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
 24205  24331   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 24206  24332   ** returns a different mutex on every call.  But for the static 
 24207  24333   ** mutex types, the same mutex is returned on every call that has
 24208  24334   ** the same type number.
 24209  24335   */
 24210  24336   static sqlite3_mutex *pthreadMutexAlloc(int iType){
 24211  24337     static sqlite3_mutex staticMutexes[] = {
 24212         -    SQLITE3_MUTEX_INITIALIZER,
 24213         -    SQLITE3_MUTEX_INITIALIZER,
 24214         -    SQLITE3_MUTEX_INITIALIZER,
 24215         -    SQLITE3_MUTEX_INITIALIZER,
 24216         -    SQLITE3_MUTEX_INITIALIZER,
 24217         -    SQLITE3_MUTEX_INITIALIZER,
 24218         -    SQLITE3_MUTEX_INITIALIZER,
 24219         -    SQLITE3_MUTEX_INITIALIZER,
 24220         -    SQLITE3_MUTEX_INITIALIZER,
 24221         -    SQLITE3_MUTEX_INITIALIZER,
 24222         -    SQLITE3_MUTEX_INITIALIZER,
 24223         -    SQLITE3_MUTEX_INITIALIZER
        24338  +    SQLITE3_MUTEX_INITIALIZER(2),
        24339  +    SQLITE3_MUTEX_INITIALIZER(3),
        24340  +    SQLITE3_MUTEX_INITIALIZER(4),
        24341  +    SQLITE3_MUTEX_INITIALIZER(5),
        24342  +    SQLITE3_MUTEX_INITIALIZER(6),
        24343  +    SQLITE3_MUTEX_INITIALIZER(7),
        24344  +    SQLITE3_MUTEX_INITIALIZER(8),
        24345  +    SQLITE3_MUTEX_INITIALIZER(9),
        24346  +    SQLITE3_MUTEX_INITIALIZER(10),
        24347  +    SQLITE3_MUTEX_INITIALIZER(11),
        24348  +    SQLITE3_MUTEX_INITIALIZER(12),
        24349  +    SQLITE3_MUTEX_INITIALIZER(13)
 24224  24350     };
 24225  24351     sqlite3_mutex *p;
 24226  24352     switch( iType ){
 24227  24353       case SQLITE_MUTEX_RECURSIVE: {
 24228  24354         p = sqlite3MallocZero( sizeof(*p) );
 24229  24355         if( p ){
 24230  24356   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
 24235  24361           /* Use a recursive mutex if it is available */
 24236  24362           pthread_mutexattr_t recursiveAttr;
 24237  24363           pthread_mutexattr_init(&recursiveAttr);
 24238  24364           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 24239  24365           pthread_mutex_init(&p->mutex, &recursiveAttr);
 24240  24366           pthread_mutexattr_destroy(&recursiveAttr);
 24241  24367   #endif
        24368  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24369  +        p->id = SQLITE_MUTEX_RECURSIVE;
        24370  +#endif
 24242  24371         }
 24243  24372         break;
 24244  24373       }
 24245  24374       case SQLITE_MUTEX_FAST: {
 24246  24375         p = sqlite3MallocZero( sizeof(*p) );
 24247  24376         if( p ){
 24248  24377           pthread_mutex_init(&p->mutex, 0);
        24378  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24379  +        p->id = SQLITE_MUTEX_FAST;
        24380  +#endif
 24249  24381         }
 24250  24382         break;
 24251  24383       }
 24252  24384       default: {
 24253  24385   #ifdef SQLITE_ENABLE_API_ARMOR
 24254  24386         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
 24255  24387           (void)SQLITE_MISUSE_BKPT;
................................................................................
 24257  24389         }
 24258  24390   #endif
 24259  24391         p = &staticMutexes[iType-2];
 24260  24392         break;
 24261  24393       }
 24262  24394     }
 24263  24395   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
 24264         -  if( p ) p->id = iType;
        24396  +  assert( p==0 || p->id==iType );
 24265  24397   #endif
 24266  24398     return p;
 24267  24399   }
 24268  24400   
 24269  24401   
 24270  24402   /*
 24271  24403   ** This routine deallocates a previously
................................................................................
 24774  24906   */
 24775  24907   struct sqlite3_mutex {
 24776  24908     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 24777  24909     int id;                    /* Mutex type */
 24778  24910   #ifdef SQLITE_DEBUG
 24779  24911     volatile int nRef;         /* Number of enterances */
 24780  24912     volatile DWORD owner;      /* Thread holding this mutex */
 24781         -  volatile int trace;        /* True to trace changes */
        24913  +  volatile LONG trace;       /* True to trace changes */
 24782  24914   #endif
 24783  24915   };
 24784  24916   
 24785  24917   /*
 24786  24918   ** These are the initializer values used when declaring a "static" mutex
 24787  24919   ** on Win32.  It should be noted that all mutexes require initialization
 24788  24920   ** on the Win32 platform.
 24789  24921   */
 24790  24922   #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 24791  24923   
 24792  24924   #ifdef SQLITE_DEBUG
 24793         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
        24925  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
 24794  24926                                       0L, (DWORD)0, 0 }
 24795  24927   #else
 24796         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
        24928  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
 24797  24929   #endif
 24798  24930   
 24799  24931   #ifdef SQLITE_DEBUG
 24800  24932   /*
 24801  24933   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24802  24934   ** intended for use only inside assert() statements.
 24803  24935   */
................................................................................
 24832  24964   #endif
 24833  24965   }
 24834  24966   
 24835  24967   /*
 24836  24968   ** Initialize and deinitialize the mutex subsystem.
 24837  24969   */
 24838  24970   static sqlite3_mutex winMutex_staticMutexes[] = {
 24839         -  SQLITE3_MUTEX_INITIALIZER,
 24840         -  SQLITE3_MUTEX_INITIALIZER,
 24841         -  SQLITE3_MUTEX_INITIALIZER,
 24842         -  SQLITE3_MUTEX_INITIALIZER,
 24843         -  SQLITE3_MUTEX_INITIALIZER,
 24844         -  SQLITE3_MUTEX_INITIALIZER,
 24845         -  SQLITE3_MUTEX_INITIALIZER,
 24846         -  SQLITE3_MUTEX_INITIALIZER,
 24847         -  SQLITE3_MUTEX_INITIALIZER,
 24848         -  SQLITE3_MUTEX_INITIALIZER,
 24849         -  SQLITE3_MUTEX_INITIALIZER,
 24850         -  SQLITE3_MUTEX_INITIALIZER
        24971  +  SQLITE3_MUTEX_INITIALIZER(2),
        24972  +  SQLITE3_MUTEX_INITIALIZER(3),
        24973  +  SQLITE3_MUTEX_INITIALIZER(4),
        24974  +  SQLITE3_MUTEX_INITIALIZER(5),
        24975  +  SQLITE3_MUTEX_INITIALIZER(6),
        24976  +  SQLITE3_MUTEX_INITIALIZER(7),
        24977  +  SQLITE3_MUTEX_INITIALIZER(8),
        24978  +  SQLITE3_MUTEX_INITIALIZER(9),
        24979  +  SQLITE3_MUTEX_INITIALIZER(10),
        24980  +  SQLITE3_MUTEX_INITIALIZER(11),
        24981  +  SQLITE3_MUTEX_INITIALIZER(12),
        24982  +  SQLITE3_MUTEX_INITIALIZER(13)
 24851  24983   };
 24852  24984   
 24853  24985   static int winMutex_isInit = 0;
 24854  24986   static int winMutex_isNt = -1; /* <0 means "need to query" */
 24855  24987   
 24856  24988   /* As the winMutexInit() and winMutexEnd() functions are called as part
 24857  24989   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
................................................................................
 24973  25105   #ifdef SQLITE_ENABLE_API_ARMOR
 24974  25106         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
 24975  25107           (void)SQLITE_MISUSE_BKPT;
 24976  25108           return 0;
 24977  25109         }
 24978  25110   #endif
 24979  25111         p = &winMutex_staticMutexes[iType-2];
 24980         -      p->id = iType;
 24981  25112   #ifdef SQLITE_DEBUG
 24982  25113   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
 24983         -      p->trace = 1;
        25114  +      InterlockedCompareExchange(&p->trace, 1, 0);
 24984  25115   #endif
 24985  25116   #endif
 24986  25117         break;
 24987  25118       }
 24988  25119     }
        25120  +  assert( p==0 || p->id==iType );
 24989  25121     return p;
 24990  25122   }
 24991  25123   
 24992  25124   
 24993  25125   /*
 24994  25126   ** This routine deallocates a previously
 24995  25127   ** allocated mutex.  SQLite is careful to deallocate every
................................................................................
 26060  26192     double rounder;            /* Used for rounding floating point values */
 26061  26193     etByte flag_dp;            /* True if decimal point should be shown */
 26062  26194     etByte flag_rtz;           /* True if trailing zeros should be removed */
 26063  26195   #endif
 26064  26196     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 26065  26197     char buf[etBUFSIZE];       /* Conversion buffer */
 26066  26198   
        26199  +  /* pAccum never starts out with an empty buffer that was obtained from 
        26200  +  ** malloc().  This precondition is required by the mprintf("%z...")
        26201  +  ** optimization. */
        26202  +  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26203  +
 26067  26204     bufpt = 0;
 26068  26205     if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
 26069  26206       pArgList = va_arg(ap, PrintfArguments*);
 26070  26207       bArgList = 1;
 26071  26208     }else{
 26072  26209       bArgList = 0;
 26073  26210     }
................................................................................
 26478  26615           buf[0] = '%';
 26479  26616           bufpt = buf;
 26480  26617           length = 1;
 26481  26618           break;
 26482  26619         case etCHARX:
 26483  26620           if( bArgList ){
 26484  26621             bufpt = getTextArg(pArgList);
 26485         -          c = bufpt ? bufpt[0] : 0;
        26622  +          length = 1;
        26623  +          if( bufpt ){
        26624  +            buf[0] = c = *(bufpt++);
        26625  +            if( (c&0xc0)==0xc0 ){
        26626  +              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
        26627  +                buf[length++] = *(bufpt++);
        26628  +              }
        26629  +            }
        26630  +          }else{
        26631  +            buf[0] = 0;
        26632  +          }
 26486  26633           }else{
 26487         -          c = va_arg(ap,int);
        26634  +          unsigned int ch = va_arg(ap,unsigned int);
        26635  +          if( ch<0x00080 ){
        26636  +            buf[0] = ch & 0xff;
        26637  +            length = 1;
        26638  +          }else if( ch<0x00800 ){
        26639  +            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
        26640  +            buf[1] = 0x80 + (u8)(ch & 0x3f);
        26641  +            length = 2;
        26642  +          }else if( ch<0x10000 ){
        26643  +            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
        26644  +            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
        26645  +            buf[2] = 0x80 + (u8)(ch & 0x3f);
        26646  +            length = 3;
        26647  +          }else{
        26648  +            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
        26649  +            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
        26650  +            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
        26651  +            buf[3] = 0x80 + (u8)(ch & 0x3f);
        26652  +            length = 4;
        26653  +          }
 26488  26654           }
 26489  26655           if( precision>1 ){
 26490  26656             width -= precision-1;
 26491  26657             if( width>1 && !flag_leftjustify ){
 26492  26658               sqlite3AppendChar(pAccum, width-1, ' ');
 26493  26659               width = 0;
 26494  26660             }
 26495         -          sqlite3AppendChar(pAccum, precision-1, c);
        26661  +          while( precision-- > 1 ){
        26662  +            sqlite3StrAccumAppend(pAccum, buf, length);
        26663  +          }
 26496  26664           }
 26497         -        length = 1;
 26498         -        buf[0] = c;
 26499  26665           bufpt = buf;
 26500         -        break;
        26666  +        flag_altform2 = 1;
        26667  +        goto adjust_width_for_utf8;
 26501  26668         case etSTRING:
 26502  26669         case etDYNSTRING:
 26503  26670           if( bArgList ){
 26504  26671             bufpt = getTextArg(pArgList);
 26505  26672             xtype = etSTRING;
 26506  26673           }else{
 26507  26674             bufpt = va_arg(ap,char*);
 26508  26675           }
 26509  26676           if( bufpt==0 ){
 26510  26677             bufpt = "";
 26511  26678           }else if( xtype==etDYNSTRING ){
        26679  +          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
        26680  +            /* Special optimization for sqlite3_mprintf("%z..."):
        26681  +            ** Extend an existing memory allocation rather than creating
        26682  +            ** a new one. */
        26683  +            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26684  +            pAccum->zText = bufpt;
        26685  +            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
        26686  +            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
        26687  +            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
        26688  +            length = 0;
        26689  +            break;
        26690  +          }
 26512  26691             zExtra = bufpt;
 26513  26692           }
 26514  26693           if( precision>=0 ){
 26515         -          for(length=0; length<precision && bufpt[length]; length++){}
        26694  +          if( flag_altform2 ){
        26695  +            /* Set length to the number of bytes needed in order to display
        26696  +            ** precision characters */
        26697  +            unsigned char *z = (unsigned char*)bufpt;
        26698  +            while( precision-- > 0 && z[0] ){
        26699  +              SQLITE_SKIP_UTF8(z);
        26700  +            }
        26701  +            length = (int)(z - (unsigned char*)bufpt);
        26702  +          }else{
        26703  +            for(length=0; length<precision && bufpt[length]; length++){}
        26704  +          }
 26516  26705           }else{
 26517  26706             length = 0x7fffffff & (int)strlen(bufpt);
 26518  26707           }
        26708  +      adjust_width_for_utf8:
        26709  +        if( flag_altform2 && width>0 ){
        26710  +          /* Adjust width to account for extra bytes in UTF-8 characters */
        26711  +          int ii = length - 1;
        26712  +          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
        26713  +        }
 26519  26714           break;
 26520         -      case etSQLESCAPE:           /* Escape ' characters */
 26521         -      case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
 26522         -      case etSQLESCAPE3: {        /* Escape " characters */
        26715  +      case etSQLESCAPE:           /* %q: Escape ' characters */
        26716  +      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
        26717  +      case etSQLESCAPE3: {        /* %w: Escape " characters */
 26523  26718           int i, j, k, n, isnull;
 26524  26719           int needQuote;
 26525  26720           char ch;
 26526  26721           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 26527  26722           char *escarg;
 26528  26723   
 26529  26724           if( bArgList ){
 26530  26725             escarg = getTextArg(pArgList);
 26531  26726           }else{
 26532  26727             escarg = va_arg(ap,char*);
 26533  26728           }
 26534  26729           isnull = escarg==0;
 26535  26730           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
        26731  +        /* For %q, %Q, and %w, the precision is the number of byte (or
        26732  +        ** characters if the ! flags is present) to use from the input.
        26733  +        ** Because of the extra quoting characters inserted, the number
        26734  +        ** of output characters may be larger than the precision.
        26735  +        */
 26536  26736           k = precision;
 26537  26737           for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 26538  26738             if( ch==q )  n++;
        26739  +          if( flag_altform2 && (ch&0xc0)==0xc0 ){
        26740  +            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
        26741  +          }
 26539  26742           }
 26540  26743           needQuote = !isnull && xtype==etSQLESCAPE2;
 26541  26744           n += i + 3;
 26542  26745           if( n>etBUFSIZE ){
 26543  26746             bufpt = zExtra = sqlite3Malloc( n );
 26544  26747             if( bufpt==0 ){
 26545  26748               setStrAccumError(pAccum, STRACCUM_NOMEM);
................................................................................
 26554  26757           for(i=0; i<k; i++){
 26555  26758             bufpt[j++] = ch = escarg[i];
 26556  26759             if( ch==q ) bufpt[j++] = ch;
 26557  26760           }
 26558  26761           if( needQuote ) bufpt[j++] = q;
 26559  26762           bufpt[j] = 0;
 26560  26763           length = j;
 26561         -        /* The precision in %q and %Q means how many input characters to
 26562         -        ** consume, not the length of the output...
 26563         -        ** if( precision>=0 && precision<length ) length = precision; */
 26564         -        break;
        26764  +        goto adjust_width_for_utf8;
 26565  26765         }
 26566  26766         case etTOKEN: {
 26567  26767           Token *pToken;
 26568  26768           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 26569  26769           pToken = va_arg(ap, Token*);
 26570  26770           assert( bArgList==0 );
 26571  26771           if( pToken && pToken->n ){
................................................................................
 26596  26796           assert( xtype==etINVALID );
 26597  26797           return;
 26598  26798         }
 26599  26799       }/* End switch over the format type */
 26600  26800       /*
 26601  26801       ** The text of the conversion is pointed to by "bufpt" and is
 26602  26802       ** "length" characters long.  The field width is "width".  Do
 26603         -    ** the output.
        26803  +    ** the output.  Both length and width are in bytes, not characters,
        26804  +    ** at this point.  If the "!" flag was present on string conversions
        26805  +    ** indicating that width and precision should be expressed in characters,
        26806  +    ** then the values have been translated prior to reaching this point.
 26604  26807       */
 26605  26808       width -= length;
 26606  26809       if( width>0 ){
 26607  26810         if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26608  26811         sqlite3StrAccumAppend(pAccum, bufpt, length);
 26609  26812         if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26610  26813       }else{
................................................................................
 27265  27468       case TK_STRING: {
 27266  27469         sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
 27267  27470         break;
 27268  27471       }
 27269  27472       case TK_NULL: {
 27270  27473         sqlite3TreeViewLine(pView,"NULL");
 27271  27474         break;
        27475  +    }
        27476  +    case TK_TRUEFALSE: {
        27477  +      sqlite3TreeViewLine(pView,
        27478  +         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
        27479  +      break;
 27272  27480       }
 27273  27481   #ifndef SQLITE_OMIT_BLOB_LITERAL
 27274  27482       case TK_BLOB: {
 27275  27483         sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 27276  27484         break;
 27277  27485       }
 27278  27486   #endif
................................................................................
 27321  27529   
 27322  27530       case TK_UMINUS:  zUniOp = "UMINUS"; break;
 27323  27531       case TK_UPLUS:   zUniOp = "UPLUS";  break;
 27324  27532       case TK_BITNOT:  zUniOp = "BITNOT"; break;
 27325  27533       case TK_NOT:     zUniOp = "NOT";    break;
 27326  27534       case TK_ISNULL:  zUniOp = "ISNULL"; break;
 27327  27535       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
        27536  +
        27537  +    case TK_TRUTH: {
        27538  +      int x;
        27539  +      const char *azOp[] = {
        27540  +         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
        27541  +      };
        27542  +      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
        27543  +      assert( pExpr->pRight );
        27544  +      assert( pExpr->pRight->op==TK_TRUEFALSE );
        27545  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
        27546  +      zUniOp = azOp[x];
        27547  +      break;
        27548  +    }
 27328  27549   
 27329  27550       case TK_SPAN: {
 27330  27551         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
 27331  27552         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 27332  27553         break;
 27333  27554       }
 27334  27555   
................................................................................
 29059  29280   /*
 29060  29281   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
 29061  29282   ** routine does *not* accept hexadecimal notation.
 29062  29283   **
 29063  29284   ** Returns:
 29064  29285   **
 29065  29286   **     0    Successful transformation.  Fits in a 64-bit signed integer.
 29066         -**     1    Excess text after the integer value
        29287  +**     1    Excess non-space text after the integer value
 29067  29288   **     2    Integer too large for a 64-bit signed integer or is malformed
 29068  29289   **     3    Special case of 9223372036854775808
 29069  29290   **
 29070  29291   ** length is the number of bytes in the string (bytes, not characters).
 29071  29292   ** The string is not necessarily zero-terminated.  The encoding is
 29072  29293   ** given by enc.
 29073  29294   */
................................................................................
 29102  29323       }
 29103  29324     }
 29104  29325     zStart = zNum;
 29105  29326     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 29106  29327     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 29107  29328       u = u*10 + c - '0';
 29108  29329     }
        29330  +  testcase( i==18*incr );
        29331  +  testcase( i==19*incr );
        29332  +  testcase( i==20*incr );
 29109  29333     if( u>LARGEST_INT64 ){
        29334  +    /* This test and assignment is needed only to suppress UB warnings
        29335  +    ** from clang and -fsanitize=undefined.  This test and assignment make
        29336  +    ** the code a little larger and slower, and no harm comes from omitting
        29337  +    ** them, but we must appaise the undefined-behavior pharisees. */
 29110  29338       *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
 29111  29339     }else if( neg ){
 29112  29340       *pNum = -(i64)u;
 29113  29341     }else{
 29114  29342       *pNum = (i64)u;
 29115  29343     }
 29116         -  testcase( i==18 );
 29117         -  testcase( i==19 );
 29118         -  testcase( i==20 );
 29119         -  if( &zNum[i]<zEnd              /* Extra bytes at the end */
 29120         -   || (i==0 && zStart==zNum)     /* No digits */
        29344  +  rc = 0;
        29345  +  if( (i==0 && zStart==zNum)     /* No digits */
 29121  29346      || nonNum                     /* UTF16 with high-order bytes non-zero */
 29122  29347     ){
 29123  29348       rc = 1;
 29124         -  }else{
 29125         -    rc = 0;
        29349  +  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
        29350  +    int jj = i;
        29351  +    do{
        29352  +      if( !sqlite3Isspace(zNum[jj]) ){
        29353  +        rc = 1;          /* Extra non-space text after the integer */
        29354  +        break;
        29355  +      }
        29356  +      jj += incr;
        29357  +    }while( &zNum[jj]<zEnd );
 29126  29358     }
 29127         -  if( i>19*incr ){                /* Too many digits */
 29128         -    /* zNum is empty or contains non-numeric text or is longer
 29129         -    ** than 19 digits (thus guaranteeing that it is too large) */
 29130         -    return 2;
 29131         -  }else if( i<19*incr ){
        29359  +  if( i<19*incr ){
 29132  29360       /* Less than 19 digits, so we know that it fits in 64 bits */
 29133  29361       assert( u<=LARGEST_INT64 );
 29134  29362       return rc;
 29135  29363     }else{
 29136  29364       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 29137         -    c = compare2pow63(zNum, incr);
        29365  +    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
 29138  29366       if( c<0 ){
 29139  29367         /* zNum is less than 9223372036854775808 so it fits */
 29140  29368         assert( u<=LARGEST_INT64 );
 29141  29369         return rc;
 29142         -    }else if( c>0 ){
 29143         -      /* zNum is greater than 9223372036854775808 so it overflows */
 29144         -      return 2;
 29145  29370       }else{
 29146         -      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 29147         -      ** special case 2 overflow if positive */
 29148         -      assert( u-1==LARGEST_INT64 );
 29149         -      return neg ? rc : 3;
        29371  +      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
        29372  +      if( c>0 ){
        29373  +        /* zNum is greater than 9223372036854775808 so it overflows */
        29374  +        return 2;
        29375  +      }else{
        29376  +        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
        29377  +        ** special case 2 overflow if positive */
        29378  +        assert( u-1==LARGEST_INT64 );
        29379  +        return neg ? rc : 3;
        29380  +      }
 29150  29381       }
 29151  29382     }
 29152  29383   }
 29153  29384   
 29154  29385   /*
 29155  29386   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
 29156  29387   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
................................................................................
 30459  30690       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30460  30691       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30461  30692       /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30462  30693       /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30463  30694       /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30464  30695       /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30465  30696       /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30466         -    /*  96 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30697  +    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 30467  30698       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30468         -    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
 30469         -    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30470         -    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30471         -    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
 30472         -    /* 102 */ "ReadCookie"       OpHelp(""),
 30473         -    /* 103 */ "SetCookie"        OpHelp(""),
 30474         -    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30475         -    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 30476         -    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 30477         -    /* 107 */ "OpenDup"          OpHelp(""),
 30478         -    /* 108 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 30479         -    /* 109 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 30480         -    /* 110 */ "SorterOpen"       OpHelp(""),
 30481         -    /* 111 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 30482         -    /* 112 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 30483         -    /* 113 */ "Close"            OpHelp(""),
 30484         -    /* 114 */ "ColumnsUsed"      OpHelp(""),
 30485         -    /* 115 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 30486         -    /* 116 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 30487         -    /* 117 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 30488         -    /* 118 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 30489         -    /* 119 */ "Delete"           OpHelp(""),
 30490         -    /* 120 */ "ResetCount"       OpHelp(""),
 30491         -    /* 121 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30492         -    /* 122 */ "SorterData"       OpHelp("r[P2]=data"),
 30493         -    /* 123 */ "RowData"          OpHelp("r[P2]=data"),
 30494         -    /* 124 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30495         -    /* 125 */ "NullRow"          OpHelp(""),
 30496         -    /* 126 */ "SeekEnd"          OpHelp(""),
 30497         -    /* 127 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30498         -    /* 128 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30499         -    /* 129 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30500         -    /* 130 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30501         -    /* 131 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30699  +    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30700  +    /*  99 */ "Column"           OpHelp("r[P3]=PX"),
        30701  +    /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        30702  +    /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        30703  +    /* 102 */ "Count"            OpHelp("r[P2]=count()"),
        30704  +    /* 103 */ "ReadCookie"       OpHelp(""),
        30705  +    /* 104 */ "SetCookie"        OpHelp(""),
        30706  +    /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        30707  +    /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        30708  +    /* 107 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        30709  +    /* 108 */ "OpenDup"          OpHelp(""),
        30710  +    /* 109 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        30711  +    /* 110 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        30712  +    /* 111 */ "SorterOpen"       OpHelp(""),
        30713  +    /* 112 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        30714  +    /* 113 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        30715  +    /* 114 */ "Close"            OpHelp(""),
        30716  +    /* 115 */ "ColumnsUsed"      OpHelp(""),
        30717  +    /* 116 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        30718  +    /* 117 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        30719  +    /* 118 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        30720  +    /* 119 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        30721  +    /* 120 */ "Delete"           OpHelp(""),
        30722  +    /* 121 */ "ResetCount"       OpHelp(""),
        30723  +    /* 122 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        30724  +    /* 123 */ "SorterData"       OpHelp("r[P2]=data"),
        30725  +    /* 124 */ "RowData"          OpHelp("r[P2]=data"),
        30726  +    /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
        30727  +    /* 126 */ "NullRow"          OpHelp(""),
        30728  +    /* 127 */ "SeekEnd"          OpHelp(""),
        30729  +    /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30730  +    /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30731  +    /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        30732  +    /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30502  30733       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30503         -    /* 133 */ "Destroy"          OpHelp(""),
 30504         -    /* 134 */ "Clear"            OpHelp(""),
 30505         -    /* 135 */ "ResetSorter"      OpHelp(""),
 30506         -    /* 136 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30507         -    /* 137 */ "SqlExec"          OpHelp(""),
 30508         -    /* 138 */ "ParseSchema"      OpHelp(""),
 30509         -    /* 139 */ "LoadAnalysis"     OpHelp(""),
 30510         -    /* 140 */ "DropTable"        OpHelp(""),
 30511         -    /* 141 */ "DropIndex"        OpHelp(""),
 30512         -    /* 142 */ "DropTrigger"      OpHelp(""),
 30513         -    /* 143 */ "IntegrityCk"      OpHelp(""),
 30514         -    /* 144 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 30515         -    /* 145 */ "Param"            OpHelp(""),
 30516         -    /* 146 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 30517         -    /* 147 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 30518         -    /* 148 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 30519         -    /* 149 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 30520         -    /* 150 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 30521         -    /* 151 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 30522         -    /* 152 */ "Expire"           OpHelp(""),
 30523         -    /* 153 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 30524         -    /* 154 */ "VBegin"           OpHelp(""),
 30525         -    /* 155 */ "VCreate"          OpHelp(""),
 30526         -    /* 156 */ "VDestroy"         OpHelp(""),
 30527         -    /* 157 */ "VOpen"            OpHelp(""),
 30528         -    /* 158 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 30529         -    /* 159 */ "VRename"          OpHelp(""),
 30530         -    /* 160 */ "Pagecount"        OpHelp(""),
 30531         -    /* 161 */ "MaxPgcnt"         OpHelp(""),
 30532         -    /* 162 */ "PureFunc0"        OpHelp(""),
 30533         -    /* 163 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 30534         -    /* 164 */ "PureFunc"         OpHelp(""),
 30535         -    /* 165 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 30536         -    /* 166 */ "Trace"            OpHelp(""),
 30537         -    /* 167 */ "CursorHint"       OpHelp(""),
 30538         -    /* 168 */ "Noop"             OpHelp(""),
 30539         -    /* 169 */ "Explain"          OpHelp(""),
        30734  +    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30735  +    /* 134 */ "Destroy"          OpHelp(""),
        30736  +    /* 135 */ "Clear"            OpHelp(""),
        30737  +    /* 136 */ "ResetSorter"      OpHelp(""),
        30738  +    /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        30739  +    /* 138 */ "SqlExec"          OpHelp(""),
        30740  +    /* 139 */ "ParseSchema"      OpHelp(""),
        30741  +    /* 140 */ "LoadAnalysis"     OpHelp(""),
        30742  +    /* 141 */ "DropTable"        OpHelp(""),
        30743  +    /* 142 */ "DropIndex"        OpHelp(""),
        30744  +    /* 143 */ "DropTrigger"      OpHelp(""),
        30745  +    /* 144 */ "IntegrityCk"      OpHelp(""),
        30746  +    /* 145 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        30747  +    /* 146 */ "Param"            OpHelp(""),
        30748  +    /* 147 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        30749  +    /* 148 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        30750  +    /* 149 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        30751  +    /* 150 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        30752  +    /* 151 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        30753  +    /* 152 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        30754  +    /* 153 */ "Expire"           OpHelp(""),
        30755  +    /* 154 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        30756  +    /* 155 */ "VBegin"           OpHelp(""),
        30757  +    /* 156 */ "VCreate"          OpHelp(""),
        30758  +    /* 157 */ "VDestroy"         OpHelp(""),
        30759  +    /* 158 */ "VOpen"            OpHelp(""),
        30760  +    /* 159 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        30761  +    /* 160 */ "VRename"          OpHelp(""),
        30762  +    /* 161 */ "Pagecount"        OpHelp(""),
        30763  +    /* 162 */ "MaxPgcnt"         OpHelp(""),
        30764  +    /* 163 */ "PureFunc0"        OpHelp(""),
        30765  +    /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        30766  +    /* 165 */ "PureFunc"         OpHelp(""),
        30767  +    /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        30768  +    /* 167 */ "Trace"            OpHelp(""),
        30769  +    /* 168 */ "CursorHint"       OpHelp(""),
        30770  +    /* 169 */ "Noop"             OpHelp(""),
        30771  +    /* 170 */ "Explain"          OpHelp(""),
 30540  30772     };
 30541  30773     return azName[i];
 30542  30774   }
 30543  30775   #endif
 30544  30776   
 30545  30777   /************** End of opcodes.c *********************************************/
 30546  30778   /************** Begin file os_unix.c *****************************************/
................................................................................
 31208  31440   #if defined(HAVE_FCHOWN)
 31209  31441     { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
 31210  31442   #else
 31211  31443     { "fchown",       (sqlite3_syscall_ptr)0,               0 },
 31212  31444   #endif
 31213  31445   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 31214  31446   
        31447  +#if defined(HAVE_FCHOWN)
 31215  31448     { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
        31449  +#else
        31450  +  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
        31451  +#endif
 31216  31452   #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
 31217  31453   
 31218  31454   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 31219  31455     { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
 31220  31456   #else
 31221  31457     { "mmap",         (sqlite3_syscall_ptr)0,               0 },
 31222  31458   #endif
................................................................................
 31436  31672   ** is held when required. This function is only used as part of assert() 
 31437  31673   ** statements. e.g.
 31438  31674   **
 31439  31675   **   unixEnterMutex()
 31440  31676   **     assert( unixMutexHeld() );
 31441  31677   **   unixEnterLeave()
 31442  31678   */
        31679  +static sqlite3_mutex *unixBigLock = 0;
 31443  31680   static void unixEnterMutex(void){
 31444         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31681  +  sqlite3_mutex_enter(unixBigLock);
 31445  31682   }
 31446  31683   static void unixLeaveMutex(void){
 31447         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31684  +  sqlite3_mutex_leave(unixBigLock);
 31448  31685   }
 31449  31686   #ifdef SQLITE_DEBUG
 31450  31687   static int unixMutexHeld(void) {
 31451         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31688  +  return sqlite3_mutex_held(unixBigLock);
 31452  31689   }
 31453  31690   #endif
 31454  31691   
 31455  31692   
 31456  31693   #ifdef SQLITE_HAVE_OS_TRACE
 31457  31694   /*
 31458  31695   ** Helper function for printing out trace information from debugging
................................................................................
 34915  35152     assert( n==1 || lockType!=F_RDLCK );
 34916  35153   
 34917  35154     /* Locks are within range */
 34918  35155     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 34919  35156   
 34920  35157     if( pShmNode->h>=0 ){
 34921  35158       /* Initialize the locking parameters */
 34922         -    memset(&f, 0, sizeof(f));
 34923  35159       f.l_type = lockType;
 34924  35160       f.l_whence = SEEK_SET;
 34925  35161       f.l_start = ofst;
 34926  35162       f.l_len = n;
 34927         -
 34928  35163       rc = osFcntl(pShmNode->h, F_SETLK, &f);
 34929  35164       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 34930  35165     }
 34931  35166   
 34932  35167     /* Update the global lock state and do debug tracing */
 34933  35168   #ifdef SQLITE_DEBUG
 34934  35169     { u16 mask;
................................................................................
 36586  36821     ** the same instant might all reset the PRNG.  But multiple resets
 36587  36822     ** are harmless.
 36588  36823     */
 36589  36824     if( randomnessPid!=osGetpid(0) ){
 36590  36825       randomnessPid = osGetpid(0);
 36591  36826       sqlite3_randomness(0,0);
 36592  36827     }
 36593         -
 36594  36828     memset(p, 0, sizeof(unixFile));
 36595  36829   
 36596  36830     if( eType==SQLITE_OPEN_MAIN_DB ){
 36597  36831       UnixUnusedFd *pUnused;
 36598  36832       pUnused = findReusableFd(zName, flags);
 36599  36833       if( pUnused ){
 36600  36834         fd = pUnused->fd;
................................................................................
 38461  38695     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 38462  38696     assert( ArraySize(aSyscall)==29 );
 38463  38697   
 38464  38698     /* Register all VFSes defined in the aVfs[] array */
 38465  38699     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 38466  38700       sqlite3_vfs_register(&aVfs[i], i==0);
 38467  38701     }
        38702  +  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
 38468  38703     return SQLITE_OK; 
 38469  38704   }
 38470  38705   
 38471  38706   /*
 38472  38707   ** Shutdown the operating system interface.
 38473  38708   **
 38474  38709   ** Some operating systems might need to do some cleanup in this routine,
 38475  38710   ** to release dynamically allocated objects.  But not on unix.
 38476  38711   ** This routine is a no-op for unix.
 38477  38712   */
 38478  38713   SQLITE_API int sqlite3_os_end(void){ 
        38714  +  unixBigLock = 0;
 38479  38715     return SQLITE_OK; 
 38480  38716   }
 38481  38717    
 38482  38718   #endif /* SQLITE_OS_UNIX */
 38483  38719   
 38484  38720   /************** End of os_unix.c *********************************************/
 38485  38721   /************** Begin file os_win.c ******************************************/
................................................................................
 42310  42546   ** is held when required. This function is only used as part of assert()
 42311  42547   ** statements. e.g.
 42312  42548   **
 42313  42549   **   winShmEnterMutex()
 42314  42550   **     assert( winShmMutexHeld() );
 42315  42551   **   winShmLeaveMutex()
 42316  42552   */
        42553  +static sqlite3_mutex *winBigLock = 0;
 42317  42554   static void winShmEnterMutex(void){
 42318         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42555  +  sqlite3_mutex_enter(winBigLock);
 42319  42556   }
 42320  42557   static void winShmLeaveMutex(void){
 42321         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42558  +  sqlite3_mutex_leave(winBigLock);
 42322  42559   }
 42323  42560   #ifndef NDEBUG
 42324  42561   static int winShmMutexHeld(void) {
 42325         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42562  +  return sqlite3_mutex_held(winBigLock);
 42326  42563   }
 42327  42564   #endif
 42328  42565   
 42329  42566   /*
 42330  42567   ** Object used to represent a single file opened and mmapped to provide
 42331  42568   ** shared memory.  When multiple threads all reference the same
 42332  42569   ** log-summary, each thread has its own winFile object, but they all
................................................................................
 44740  44977   #endif
 44741  44978   
 44742  44979     sqlite3_vfs_register(&winNolockVfs, 0);
 44743  44980   
 44744  44981   #if defined(SQLITE_WIN32_HAS_WIDE)
 44745  44982     sqlite3_vfs_register(&winLongPathNolockVfs, 0);
 44746  44983   #endif
        44984  +
        44985  +#ifndef SQLITE_OMIT_WAL
        44986  +  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
        44987  +#endif
 44747  44988   
 44748  44989     return SQLITE_OK;
 44749  44990   }
 44750  44991   
 44751  44992   SQLITE_API int sqlite3_os_end(void){
 44752  44993   #if SQLITE_OS_WINRT
 44753  44994     if( sleepObj!=NULL ){
 44754  44995       osCloseHandle(sleepObj);
 44755  44996       sleepObj = NULL;
 44756  44997     }
 44757  44998   #endif
        44999  +
        45000  +#ifndef SQLITE_OMIT_WAL
        45001  +  winBigLock = 0;
        45002  +#endif
        45003  +
 44758  45004     return SQLITE_OK;
 44759  45005   }
 44760  45006   
 44761  45007   #endif /* SQLITE_OS_WIN */
 44762  45008   
 44763  45009   /************** End of os_win.c **********************************************/
        45010  +/************** Begin file memdb.c *******************************************/
        45011  +/*
        45012  +** 2016-09-07
        45013  +**
        45014  +** The author disclaims copyright to this source code.  In place of
        45015  +** a legal notice, here is a blessing:
        45016  +**
        45017  +**    May you do good and not evil.
        45018  +**    May you find forgiveness for yourself and forgive others.
        45019  +**    May you share freely, never taking more than you give.
        45020  +**
        45021  +******************************************************************************
        45022  +**
        45023  +** This file implements in-memory VFS.  A database is held as a contiguous
        45024  +** block of memory.
        45025  +**
        45026  +** This file also implements interface sqlite3_serialize() and
        45027  +** sqlite3_deserialize().
        45028  +*/
        45029  +#ifdef SQLITE_ENABLE_DESERIALIZE
        45030  +/* #include "sqliteInt.h" */
        45031  +
        45032  +/*
        45033  +** Forward declaration of objects used by this utility
        45034  +*/
        45035  +typedef struct sqlite3_vfs MemVfs;
        45036  +typedef struct MemFile MemFile;
        45037  +
        45038  +/* Access to a lower-level VFS that (might) implement dynamic loading,
        45039  +** access to randomness, etc.
        45040  +*/
        45041  +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
        45042  +
        45043  +/* An open file */
        45044  +struct MemFile {
        45045  +  sqlite3_file base;              /* IO methods */
        45046  +  sqlite3_int64 sz;               /* Size of the file */
        45047  +  sqlite3_int64 szMax;            /* Space allocated to aData */
        45048  +  unsigned char *aData;           /* content of the file */
        45049  +  int nMmap;                      /* Number of memory mapped pages */
        45050  +  unsigned mFlags;                /* Flags */
        45051  +  int eLock;                      /* Most recent lock against this file */
        45052  +};
        45053  +
        45054  +/*
        45055  +** Methods for MemFile
        45056  +*/
        45057  +static int memdbClose(sqlite3_file*);
        45058  +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
        45059  +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
        45060  +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
        45061  +static int memdbSync(sqlite3_file*, int flags);
        45062  +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
        45063  +static int memdbLock(sqlite3_file*, int);
        45064  +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
        45065  +static int memdbFileControl(sqlite3_file*, int op, void *pArg);
        45066  +/* static int memdbSectorSize(sqlite3_file*); // not used */
        45067  +static int memdbDeviceCharacteristics(sqlite3_file*);
        45068  +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
        45069  +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
        45070  +
        45071  +/*
        45072  +** Methods for MemVfs
        45073  +*/
        45074  +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
        45075  +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
        45076  +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
        45077  +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
        45078  +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
        45079  +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
        45080  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
        45081  +static void memdbDlClose(sqlite3_vfs*, void*);
        45082  +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
        45083  +static int memdbSleep(sqlite3_vfs*, int microseconds);
        45084  +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
        45085  +static int memdbGetLastError(sqlite3_vfs*, int, char *);
        45086  +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
        45087  +
        45088  +static sqlite3_vfs memdb_vfs = {
        45089  +  2,                           /* iVersion */
        45090  +  0,                           /* szOsFile (set when registered) */
        45091  +  1024,                        /* mxPathname */
        45092  +  0,                           /* pNext */
        45093  +  "memdb",                     /* zName */
        45094  +  0,                           /* pAppData (set when registered) */ 
        45095  +  memdbOpen,                   /* xOpen */
        45096  +  0, /* memdbDelete, */        /* xDelete */
        45097  +  memdbAccess,                 /* xAccess */
        45098  +  memdbFullPathname,           /* xFullPathname */
        45099  +  memdbDlOpen,                 /* xDlOpen */
        45100  +  memdbDlError,                /* xDlError */
        45101  +  memdbDlSym,                  /* xDlSym */
        45102  +  memdbDlClose,                /* xDlClose */
        45103  +  memdbRandomness,             /* xRandomness */
        45104  +  memdbSleep,                  /* xSleep */
        45105  +  0, /* memdbCurrentTime, */   /* xCurrentTime */
        45106  +  memdbGetLastError,           /* xGetLastError */
        45107  +  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
        45108  +};
        45109  +
        45110  +static const sqlite3_io_methods memdb_io_methods = {
        45111  +  3,                              /* iVersion */
        45112  +  memdbClose,                      /* xClose */
        45113  +  memdbRead,                       /* xRead */
        45114  +  memdbWrite,                      /* xWrite */
        45115  +  memdbTruncate,                   /* xTruncate */
        45116  +  memdbSync,                       /* xSync */
        45117  +  memdbFileSize,                   /* xFileSize */
        45118  +  memdbLock,                       /* xLock */
        45119  +  memdbLock,                       /* xUnlock - same as xLock in this case */ 
        45120  +  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
        45121  +  memdbFileControl,                /* xFileControl */
        45122  +  0, /* memdbSectorSize,*/         /* xSectorSize */
        45123  +  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
        45124  +  0,                               /* xShmMap */
        45125  +  0,                               /* xShmLock */
        45126  +  0,                               /* xShmBarrier */
        45127  +  0,                               /* xShmUnmap */
        45128  +  memdbFetch,                      /* xFetch */
        45129  +  memdbUnfetch                     /* xUnfetch */
        45130  +};
        45131  +
        45132  +
        45133  +
        45134  +/*
        45135  +** Close an memdb-file.
        45136  +**
        45137  +** The pData pointer is owned by the application, so there is nothing
        45138  +** to free.
        45139  +*/
        45140  +static int memdbClose(sqlite3_file *pFile){
        45141  +  MemFile *p = (MemFile *)pFile;
        45142  +  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
        45143  +  return SQLITE_OK;
        45144  +}
        45145  +
        45146  +/*
        45147  +** Read data from an memdb-file.
        45148  +*/
        45149  +static int memdbRead(
        45150  +  sqlite3_file *pFile, 
        45151  +  void *zBuf, 
        45152  +  int iAmt, 
        45153  +  sqlite_int64 iOfst
        45154  +){
        45155  +  MemFile *p = (MemFile *)pFile;
        45156  +  if( iOfst+iAmt>p->sz ){
        45157  +    memset(zBuf, 0, iAmt);
        45158  +    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
        45159  +    return SQLITE_IOERR_SHORT_READ;
        45160  +  }
        45161  +  memcpy(zBuf, p->aData+iOfst, iAmt);
        45162  +  return SQLITE_OK;
        45163  +}
        45164  +
        45165  +/*
        45166  +** Try to enlarge the memory allocation to hold at least sz bytes
        45167  +*/
        45168  +static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
        45169  +  unsigned char *pNew;
        45170  +  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
        45171  +    return SQLITE_FULL;
        45172  +  }
        45173  +  pNew = sqlite3_realloc64(p->aData, newSz);
        45174  +  if( pNew==0 ) return SQLITE_NOMEM;
        45175  +  p->aData = pNew;
        45176  +  p->szMax = newSz;
        45177  +  return SQLITE_OK;
        45178  +}
        45179  +
        45180  +/*
        45181  +** Write data to an memdb-file.
        45182  +*/
        45183  +static int memdbWrite(
        45184  +  sqlite3_file *pFile,
        45185  +  const void *z,
        45186  +  int iAmt,
        45187  +  sqlite_int64 iOfst
        45188  +){
        45189  +  MemFile *p = (MemFile *)pFile;
        45190  +  if( iOfst+iAmt>p->sz ){
        45191  +    int rc;
        45192  +    if( iOfst+iAmt>p->szMax
        45193  +     && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
        45194  +    ){
        45195  +      return rc;
        45196  +    }
        45197  +    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
        45198  +    p->sz = iOfst+iAmt;
        45199  +  }
        45200  +  memcpy(p->aData+iOfst, z, iAmt);
        45201  +  return SQLITE_OK;
        45202  +}
        45203  +
        45204  +/*
        45205  +** Truncate an memdb-file.
        45206  +**
        45207  +** In rollback mode (which is always the case for memdb, as it does not
        45208  +** support WAL mode) the truncate() method is only used to reduce
        45209  +** the size of a file, never to increase the size.
        45210  +*/
        45211  +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
        45212  +  MemFile *p = (MemFile *)pFile;
        45213  +  if( NEVER(size>p->sz) ) return SQLITE_FULL;
        45214  +  p->sz = size; 
        45215  +  return SQLITE_OK;
        45216  +}
        45217  +
        45218  +/*
        45219  +** Sync an memdb-file.
        45220  +*/
        45221  +static int memdbSync(sqlite3_file *pFile, int flags){
        45222  +  return SQLITE_OK;
        45223  +}
        45224  +
        45225  +/*
        45226  +** Return the current file-size of an memdb-file.
        45227  +*/
        45228  +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
        45229  +  MemFile *p = (MemFile *)pFile;
        45230  +  *pSize = p->sz;
        45231  +  return SQLITE_OK;
        45232  +}
        45233  +
        45234  +/*
        45235  +** Lock an memdb-file.
        45236  +*/
        45237  +static int memdbLock(sqlite3_file *pFile, int eLock){
        45238  +  MemFile *p = (MemFile *)pFile;
        45239  +  p->eLock = eLock;
        45240  +  return SQLITE_OK;
        45241  +}
        45242  +
        45243  +#if 0 /* Never used because memdbAccess() always returns false */
        45244  +/*
        45245  +** Check if another file-handle holds a RESERVED lock on an memdb-file.
        45246  +*/
        45247  +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
        45248  +  *pResOut = 0;
        45249  +  return SQLITE_OK;
        45250  +}
        45251  +#endif
        45252  +
        45253  +/*
        45254  +** File control method. For custom operations on an memdb-file.
        45255  +*/
        45256  +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
        45257  +  MemFile *p = (MemFile *)pFile;
        45258  +  int rc = SQLITE_NOTFOUND;
        45259  +  if( op==SQLITE_FCNTL_VFSNAME ){
        45260  +    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
        45261  +    rc = SQLITE_OK;
        45262  +  }
        45263  +  return rc;
        45264  +}
        45265  +
        45266  +#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
        45267  +/*
        45268  +** Return the sector-size in bytes for an memdb-file.
        45269  +*/
        45270  +static int memdbSectorSize(sqlite3_file *pFile){
        45271  +  return 1024;
        45272  +}
        45273  +#endif
        45274  +
        45275  +/*
        45276  +** Return the device characteristic flags supported by an memdb-file.
        45277  +*/
        45278  +static int memdbDeviceCharacteristics(sqlite3_file *pFile){
        45279  +  return SQLITE_IOCAP_ATOMIC | 
        45280  +         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
        45281  +         SQLITE_IOCAP_SAFE_APPEND |
        45282  +         SQLITE_IOCAP_SEQUENTIAL;
        45283  +}
        45284  +
        45285  +/* Fetch a page of a memory-mapped file */
        45286  +static int memdbFetch(
        45287  +  sqlite3_file *pFile,
        45288  +  sqlite3_int64 iOfst,
        45289  +  int iAmt,
        45290  +  void **pp
        45291  +){
        45292  +  MemFile *p = (MemFile *)pFile;
        45293  +  p->nMmap++;
        45294  +  *pp = (void*)(p->aData + iOfst);
        45295  +  return SQLITE_OK;
        45296  +}
        45297  +
        45298  +/* Release a memory-mapped page */
        45299  +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
        45300  +  MemFile *p = (MemFile *)pFile;
        45301  +  p->nMmap--;
        45302  +  return SQLITE_OK;
        45303  +}
        45304  +
        45305  +/*
        45306  +** Open an mem file handle.
        45307  +*/
        45308  +static int memdbOpen(
        45309  +  sqlite3_vfs *pVfs,
        45310  +  const char *zName,
        45311  +  sqlite3_file *pFile,
        45312  +  int flags,
        45313  +  int *pOutFlags
        45314  +){
        45315  +  MemFile *p = (MemFile*)pFile;
        45316  +  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
        45317  +    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
        45318  +  }
        45319  +  memset(p, 0, sizeof(*p));
        45320  +  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
        45321  +  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
        45322  +  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
        45323  +  p->base.pMethods = &memdb_io_methods;
        45324  +  return SQLITE_OK;
        45325  +}
        45326  +
        45327  +#if 0 /* Only used to delete rollback journals, master journals, and WAL
        45328  +      ** files, none of which exist in memdb.  So this routine is never used */
        45329  +/*
        45330  +** Delete the file located at zPath. If the dirSync argument is true,
        45331  +** ensure the file-system modifications are synced to disk before
        45332  +** returning.
        45333  +*/
        45334  +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
        45335  +  return SQLITE_IOERR_DELETE;
        45336  +}
        45337  +#endif
        45338  +
        45339  +/*
        45340  +** Test for access permissions. Return true if the requested permission
        45341  +** is available, or false otherwise.
        45342  +**
        45343  +** With memdb, no files ever exist on disk.  So always return false.
        45344  +*/
        45345  +static int memdbAccess(
        45346  +  sqlite3_vfs *pVfs, 
        45347  +  const char *zPath, 
        45348  +  int flags, 
        45349  +  int *pResOut
        45350  +){
        45351  +  *pResOut = 0;
        45352  +  return SQLITE_OK;
        45353  +}
        45354  +
        45355  +/*
        45356  +** Populate buffer zOut with the full canonical pathname corresponding
        45357  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
        45358  +** of at least (INST_MAX_PATHNAME+1) bytes.
        45359  +*/
        45360  +static int memdbFullPathname(
        45361  +  sqlite3_vfs *pVfs, 
        45362  +  const char *zPath, 
        45363  +  int nOut, 
        45364  +  char *zOut
        45365  +){
        45366  +  sqlite3_snprintf(nOut, zOut, "%s", zPath);
        45367  +  return SQLITE_OK;
        45368  +}
        45369  +
        45370  +/*
        45371  +** Open the dynamic library located at zPath and return a handle.
        45372  +*/
        45373  +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
        45374  +  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
        45375  +}
        45376  +
        45377  +/*
        45378  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
        45379  +** utf-8 string describing the most recent error encountered associated 
        45380  +** with dynamic libraries.
        45381  +*/
        45382  +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
        45383  +  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
        45384  +}
        45385  +
        45386  +/*
        45387  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
        45388  +*/
        45389  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
        45390  +  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
        45391  +}
        45392  +
        45393  +/*
        45394  +** Close the dynamic library handle pHandle.
        45395  +*/
        45396  +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
        45397  +  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
        45398  +}
        45399  +
        45400  +/*
        45401  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
        45402  +** random data.
        45403  +*/
        45404  +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
        45405  +  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
        45406  +}
        45407  +
        45408  +/*
        45409  +** Sleep for nMicro microseconds. Return the number of microseconds 
        45410  +** actually slept.
        45411  +*/
        45412  +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
        45413  +  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
        45414  +}
        45415  +
        45416  +#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
        45417  +/*
        45418  +** Return the current time as a Julian Day number in *pTimeOut.
        45419  +*/
        45420  +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
        45421  +  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
        45422  +}
        45423  +#endif
        45424  +
        45425  +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
        45426  +  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
        45427  +}
        45428  +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
        45429  +  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
        45430  +}
        45431  +
        45432  +/*
        45433  +** Translate a database connection pointer and schema name into a
        45434  +** MemFile pointer.
        45435  +*/
        45436  +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
        45437  +  MemFile *p = 0;
        45438  +  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
        45439  +  if( rc ) return 0;
        45440  +  if( p->base.pMethods!=&memdb_io_methods ) return 0;
        45441  +  return p;
        45442  +}
        45443  +
        45444  +/*
        45445  +** Return the serialization of a database
        45446  +*/
        45447  +SQLITE_API unsigned char *sqlite3_serialize(
        45448  +  sqlite3 *db,              /* The database connection */
        45449  +  const char *zSchema,      /* Which database within the connection */
        45450  +  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
        45451  +  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
        45452  +){
        45453  +  MemFile *p;
        45454  +  int iDb;
        45455  +  Btree *pBt;
        45456  +  sqlite3_int64 sz;
        45457  +  int szPage = 0;
        45458  +  sqlite3_stmt *pStmt = 0;
        45459  +  unsigned char *pOut;
        45460  +  char *zSql;
        45461  +  int rc;
        45462  +
        45463  +#ifdef SQLITE_ENABLE_API_ARMOR
        45464  +  if( !sqlite3SafetyCheckOk(db) ){
        45465  +    (void)SQLITE_MISUSE_BKPT;
        45466  +    return 0;
        45467  +  }
        45468  +#endif
        45469  +
        45470  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45471  +  p = memdbFromDbSchema(db, zSchema);
        45472  +  iDb = sqlite3FindDbName(db, zSchema);
        45473  +  if( piSize ) *piSize = -1;
        45474  +  if( iDb<0 ) return 0;
        45475  +  if( p ){
        45476  +    if( piSize ) *piSize = p->sz;
        45477  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45478  +      pOut = p->aData;
        45479  +    }else{
        45480  +      pOut = sqlite3_malloc64( p->sz );
        45481  +      if( pOut ) memcpy(pOut, p->aData, p->sz);
        45482  +    }
        45483  +    return pOut;
        45484  +  }
        45485  +  pBt = db->aDb[iDb].pBt;
        45486  +  if( pBt==0 ) return 0;
        45487  +  szPage = sqlite3BtreeGetPageSize(pBt);
        45488  +  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
        45489  +  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
        45490  +  sqlite3_free(zSql);
        45491  +  if( rc ) return 0;
        45492  +  rc = sqlite3_step(pStmt);
        45493  +  if( rc!=SQLITE_ROW ){
        45494  +    pOut = 0;
        45495  +  }else{
        45496  +    sz = sqlite3_column_int64(pStmt, 0)*szPage;
        45497  +    if( piSize ) *piSize = sz;
        45498  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45499  +      pOut = 0;
        45500  +    }else{
        45501  +      pOut = sqlite3_malloc64( sz );
        45502  +      if( pOut ){
        45503  +        int nPage = sqlite3_column_int(pStmt, 0);
        45504  +        Pager *pPager = sqlite3BtreePager(pBt);
        45505  +        int pgno;
        45506  +        for(pgno=1; pgno<=nPage; pgno++){
        45507  +          DbPage *pPage = 0;
        45508  +          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
        45509  +          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
        45510  +          if( rc==SQLITE_OK ){
        45511  +            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
        45512  +          }else{
        45513  +            memset(pTo, 0, szPage);
        45514  +          }
        45515  +          sqlite3PagerUnref(pPage);       
        45516  +        }
        45517  +      }
        45518  +    }
        45519  +  }
        45520  +  sqlite3_finalize(pStmt);
        45521  +  return pOut;
        45522  +}
        45523  +
        45524  +/* Convert zSchema to a MemDB and initialize its content.
        45525  +*/
        45526  +SQLITE_API int sqlite3_deserialize(
        45527  +  sqlite3 *db,            /* The database connection */
        45528  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
        45529  +  unsigned char *pData,   /* The serialized database content */
        45530  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
        45531  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
        45532  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
        45533  +){
        45534  +  MemFile *p;
        45535  +  char *zSql;
        45536  +  sqlite3_stmt *pStmt = 0;
        45537  +  int rc;
        45538  +  int iDb;
        45539  +
        45540  +#ifdef SQLITE_ENABLE_API_ARMOR
        45541  +  if( !sqlite3SafetyCheckOk(db) ){
        45542  +    return SQLITE_MISUSE_BKPT;
        45543  +  }
        45544  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
        45545  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
        45546  +#endif
        45547  +
        45548  +  sqlite3_mutex_enter(db->mutex);
        45549  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45550  +  iDb = sqlite3FindDbName(db, zSchema);
        45551  +  if( iDb<0 ){
        45552  +    rc = SQLITE_ERROR;
        45553  +    goto end_deserialize;
        45554  +  }    
        45555  +  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
        45556  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
        45557  +  sqlite3_free(zSql);
        45558  +  if( rc ) goto end_deserialize;
        45559  +  db->init.iDb = (u8)iDb;
        45560  +  db->init.reopenMemdb = 1;
        45561  +  rc = sqlite3_step(pStmt);
        45562  +  db->init.reopenMemdb = 0;
        45563  +  if( rc!=SQLITE_DONE ){
        45564  +    rc = SQLITE_ERROR;
        45565  +    goto end_deserialize;
        45566  +  }
        45567  +  p = memdbFromDbSchema(db, zSchema);
        45568  +  if( p==0 ){
        45569  +    rc = SQLITE_ERROR;
        45570  +  }else{
        45571  +    p->aData = pData;
        45572  +    p->sz = szDb;
        45573  +    p->szMax = szBuf;
        45574  +    p->mFlags = mFlags;
        45575  +    rc = SQLITE_OK;
        45576  +  }
        45577  +
        45578  +end_deserialize:
        45579  +  sqlite3_finalize(pStmt);
        45580  +  sqlite3_mutex_leave(db->mutex);
        45581  +  return rc;
        45582  +}
        45583  +
        45584  +/* 
        45585  +** This routine is called when the extension is loaded.
        45586  +** Register the new VFS.
        45587  +*/
        45588  +SQLITE_PRIVATE int sqlite3MemdbInit(void){
        45589  +  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
        45590  +  int sz = pLower->szOsFile;
        45591  +  memdb_vfs.pAppData = pLower;
        45592  +  /* In all known configurations of SQLite, the size of a default
        45593  +  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
        45594  +  ** Should that ever change, remove the following NEVER() */
        45595  +  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
        45596  +  memdb_vfs.szOsFile = sz;
        45597  +  return sqlite3_vfs_register(&memdb_vfs, 0);
        45598  +}
        45599  +#endif /* SQLITE_ENABLE_DESERIALIZE */
        45600  +
        45601  +/************** End of memdb.c ***********************************************/
 44764  45602   /************** Begin file bitvec.c ******************************************/
 44765  45603   /*
 44766  45604   ** 2008 February 16
 44767  45605   **
 44768  45606   ** The author disclaims copyright to this source code.  In place of
 44769  45607   ** a legal notice, here is a blessing:
 44770  45608   **
................................................................................
 45605  46443       }
 45606  46444       if( pPg ){
 45607  46445         int rc;
 45608  46446   #ifdef SQLITE_LOG_CACHE_SPILL
 45609  46447         sqlite3_log(SQLITE_FULL, 
 45610  46448                     "spill page %d making room for %d - cache used: %d/%d",
 45611  46449                     pPg->pgno, pgno,
 45612         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
        46450  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
 45613  46451                   numberOfCachePages(pCache));
 45614  46452   #endif
 45615  46453         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
 45616  46454         rc = pCache->xStress(pCache->pStress, pPg);
 45617  46455         pcacheDump(pCache);
 45618  46456         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 45619  46457           return rc;
................................................................................
 48661  49499     int pageSize;               /* Number of bytes in a page */
 48662  49500     Pgno mxPgno;                /* Maximum allowed size of the database */
 48663  49501     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 48664  49502     char *zFilename;            /* Name of the database file */
 48665  49503     char *zJournal;             /* Name of the journal file */
 48666  49504     int (*xBusyHandler)(void*); /* Function to call when busy */
 48667  49505     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 48668         -  int aStat[3];               /* Total cache hits, misses and writes */
        49506  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
 48669  49507   #ifdef SQLITE_TEST
 48670  49508     int nRead;                  /* Database pages read */
 48671  49509   #endif
 48672  49510     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 48673  49511     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
 48674  49512   #ifdef SQLITE_HAS_CODEC
 48675  49513     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
 48689  49527   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 48690  49528   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 48691  49529   ** or CACHE_WRITE to sqlite3_db_status().
 48692  49530   */
 48693  49531   #define PAGER_STAT_HIT   0
 48694  49532   #define PAGER_STAT_MISS  1
 48695  49533   #define PAGER_STAT_WRITE 2
        49534  +#define PAGER_STAT_SPILL 3
 48696  49535   
 48697  49536   /*
 48698  49537   ** The following global variables hold counters used for
 48699  49538   ** testing purposes only.  These variables do not exist in
 48700  49539   ** a non-testing build.  These variables are not thread-safe.
 48701  49540   */
 48702  49541   #ifdef SQLITE_TEST
................................................................................
 49175  50014     assert( isOpen(pPager->fd) );
 49176  50015     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 49177  50016   #else
 49178  50017     UNUSED_PARAMETER(pPager);
 49179  50018   #endif
 49180  50019   
 49181  50020   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 49182         -  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
        50021  +  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
 49183  50022       return -1;
 49184  50023     }
 49185  50024   #endif
 49186  50025   
 49187  50026   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 49188  50027     {
 49189  50028       int nSector = pPager->sectorSize;
................................................................................
 52064  52903     PgHdr *pNext;
 52065  52904     for(p=pPager->pMmapFreelist; p; p=pNext){
 52066  52905       pNext = p->pDirty;
 52067  52906       sqlite3_free(p);
 52068  52907     }
 52069  52908   }
 52070  52909   
        52910  +/* Verify that the database file has not be deleted or renamed out from
        52911  +** under the pager.  Return SQLITE_OK if the database is still where it ought
        52912  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
        52913  +** code from sqlite3OsAccess()) if the database has gone missing.
        52914  +*/
        52915  +static int databaseIsUnmoved(Pager *pPager){
        52916  +  int bHasMoved = 0;
        52917  +  int rc;
        52918  +
        52919  +  if( pPager->tempFile ) return SQLITE_OK;
        52920  +  if( pPager->dbSize==0 ) return SQLITE_OK;
        52921  +  assert( pPager->zFilename && pPager->zFilename[0] );
        52922  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
        52923  +  if( rc==SQLITE_NOTFOUND ){
        52924  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
        52925  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
        52926  +    ** version 3.8.3, it never checked */
        52927  +    rc = SQLITE_OK;
        52928  +  }else if( rc==SQLITE_OK && bHasMoved ){
        52929  +    rc = SQLITE_READONLY_DBMOVED;
        52930  +  }
        52931  +  return rc;
        52932  +}
        52933  +
 52071  52934   
 52072  52935   /*
 52073  52936   ** Shutdown the page cache.  Free all memory and close all files.
 52074  52937   **
 52075  52938   ** If a transaction was in progress when this routine is called, that
 52076  52939   ** transaction is rolled back.  All outstanding pages are invalidated
 52077  52940   ** and their memory is freed.  Any attempt to use a page associated
................................................................................
 52080  52943   **
 52081  52944   ** This function always succeeds. If a transaction is active an attempt
 52082  52945   ** is made to roll it back. If an error occurs during the rollback 
 52083  52946   ** a hot journal may be left in the filesystem but no error is returned
 52084  52947   ** to the caller.
 52085  52948   */
 52086  52949   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
 52087         -  u8 *pTmp = (u8 *)pPager->pTmpSpace;
 52088         -
        52950  +  u8 *pTmp = (u8*)pPager->pTmpSpace;
 52089  52951     assert( db || pagerUseWal(pPager)==0 );
 52090  52952     assert( assert_pager_state(pPager) );
 52091  52953     disable_simulated_io_errors();
 52092  52954     sqlite3BeginBenignMalloc();
 52093  52955     pagerFreeMapHdrs(pPager);
 52094  52956     /* pPager->errCode = 0; */
 52095  52957     pPager->exclusiveMode = 0;
 52096  52958   #ifndef SQLITE_OMIT_WAL
 52097         -  assert( db || pPager->pWal==0 );
 52098         -  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
 52099         -      (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
 52100         -  );
 52101         -  pPager->pWal = 0;
        52959  +  {
        52960  +    u8 *a = 0;
        52961  +    assert( db || pPager->pWal==0 );
        52962  +    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
        52963  +     && SQLITE_OK==databaseIsUnmoved(pPager)
        52964  +    ){
        52965  +      a = pTmp;
        52966  +    }
        52967  +    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
        52968  +    pPager->pWal = 0;
        52969  +  }
 52102  52970   #endif
 52103  52971     pager_reset(pPager);
 52104  52972     if( MEMDB ){
 52105  52973       pager_unlock(pPager);
 52106  52974     }else{
 52107  52975       /* If it is open, sync the journal file before calling UnlockAndRollback.
 52108  52976       ** If this is not done, then an unsynced portion of the open journal 
................................................................................
 52551  53419     if( pPager->doNotSpill
 52552  53420      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 52553  53421         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 52554  53422     ){
 52555  53423       return SQLITE_OK;
 52556  53424     }
 52557  53425   
        53426  +  pPager->aStat[PAGER_STAT_SPILL]++;
 52558  53427     pPg->pDirty = 0;
 52559  53428     if( pagerUseWal(pPager) ){
 52560  53429       /* Write a single frame for this page to the log. */
 52561  53430       rc = subjournalPageIfRequired(pPg); 
 52562  53431       if( rc==SQLITE_OK ){
 52563  53432         rc = pagerWalFrames(pPager, pPg, 0, 0);
 52564  53433       }
................................................................................
 52656  53525     void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 52657  53526   ){
 52658  53527     u8 *pPtr;
 52659  53528     Pager *pPager = 0;       /* Pager object to allocate and return */
 52660  53529     int rc = SQLITE_OK;      /* Return code */
 52661  53530     int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 52662  53531     int memDb = 0;           /* True if this is an in-memory file */
        53532  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53533  +  int memJM = 0;           /* Memory journal mode */
        53534  +#else
        53535  +# define memJM 0
        53536  +#endif
 52663  53537     int readOnly = 0;        /* True if this is a read-only file */
 52664  53538     int journalFileSize;     /* Bytes to allocate for each journal fd */
 52665  53539     char *zPathname = 0;     /* Full path to database file */
 52666  53540     int nPathname = 0;       /* Number of bytes in zPathname */
 52667  53541     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 52668  53542     int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 52669  53543     u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
................................................................................
 52783  53657   
 52784  53658     /* Open the pager file.
 52785  53659     */
 52786  53660     if( zFilename && zFilename[0] ){
 52787  53661       int fout = 0;                    /* VFS flags returned by xOpen() */
 52788  53662       rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 52789  53663       assert( !memDb );
 52790         -    readOnly = (fout&SQLITE_OPEN_READONLY);
        53664  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53665  +    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
        53666  +#endif
        53667  +    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
 52791  53668   
 52792  53669       /* If the file was successfully opened for read/write access,
 52793  53670       ** choose a default page size in case we have to create the
 52794  53671       ** database file. The default page size is the maximum of:
 52795  53672       **
 52796  53673       **    + SQLITE_DEFAULT_PAGE_SIZE,
 52797  53674       **    + The value returned by sqlite3OsSectorSize()
................................................................................
 52914  53791     /* pPager->pLast = 0; */
 52915  53792     pPager->nExtra = (u16)nExtra;
 52916  53793     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 52917  53794     assert( isOpen(pPager->fd) || tempFile );
 52918  53795     setSectorSize(pPager);
 52919  53796     if( !useJournal ){
 52920  53797       pPager->journalMode = PAGER_JOURNALMODE_OFF;
 52921         -  }else if( memDb ){
        53798  +  }else if( memDb || memJM ){
 52922  53799       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 52923  53800     }
 52924  53801     /* pPager->xBusyHandler = 0; */
 52925  53802     /* pPager->pBusyHandlerArg = 0; */
 52926  53803     pPager->xReiniter = xReinit;
 52927  53804     setGetterMethod(pPager);
 52928  53805     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
................................................................................
 52929  53806     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
 52930  53807   
 52931  53808     *ppPager = pPager;
 52932  53809     return SQLITE_OK;
 52933  53810   }
 52934  53811   
 52935  53812   
 52936         -/* Verify that the database file has not be deleted or renamed out from
 52937         -** under the pager.  Return SQLITE_OK if the database is still were it ought
 52938         -** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
 52939         -** code from sqlite3OsAccess()) if the database has gone missing.
 52940         -*/
 52941         -static int databaseIsUnmoved(Pager *pPager){
 52942         -  int bHasMoved = 0;
 52943         -  int rc;
 52944         -
 52945         -  if( pPager->tempFile ) return SQLITE_OK;
 52946         -  if( pPager->dbSize==0 ) return SQLITE_OK;
 52947         -  assert( pPager->zFilename && pPager->zFilename[0] );
 52948         -  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
 52949         -  if( rc==SQLITE_NOTFOUND ){
 52950         -    /* If the HAS_MOVED file-control is unimplemented, assume that the file
 52951         -    ** has not been moved.  That is the historical behavior of SQLite: prior to
 52952         -    ** version 3.8.3, it never checked */
 52953         -    rc = SQLITE_OK;
 52954         -  }else if( rc==SQLITE_OK && bHasMoved ){
 52955         -    rc = SQLITE_READONLY_DBMOVED;
 52956         -  }
 52957         -  return rc;
 52958         -}
 52959         -
 52960  53813   
 52961  53814   /*
 52962  53815   ** This function is called after transitioning from PAGER_UNLOCK to
 52963  53816   ** PAGER_SHARED state. It tests if there is a hot journal present in
 52964  53817   ** the file-system for the given pager. A hot journal is one that 
 52965  53818   ** needs to be played back. According to this function, a hot-journal
 52966  53819   ** file exists if the following criteria are met:
................................................................................
 54461  55314           rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
 54462  55315           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 54463  55316         }
 54464  55317         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 54465  55318         if( bBatch ){
 54466  55319           if( rc==SQLITE_OK ){
 54467  55320             rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
 54468         -        }else{
 54469         -          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
        55321  +        }
        55322  +        if( rc!=SQLITE_OK ){
        55323  +          sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
 54470  55324           }
 54471  55325         }
 54472  55326   
 54473  55327         if( rc!=SQLITE_OK ){
 54474  55328           assert( rc!=SQLITE_IOERR_BLOCKED );
 54475  55329           goto commit_phase_one_exit;
 54476  55330         }
................................................................................
 54686  55540     a[9] = pPager->nRead;
 54687  55541     a[10] = pPager->aStat[PAGER_STAT_WRITE];
 54688  55542     return a;
 54689  55543   }
 54690  55544   #endif
 54691  55545   
 54692  55546   /*
 54693         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 54694         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        55547  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
        55548  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
        55549  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
        55550  +** it was added later.
        55551  +**
        55552  +** Before returning, *pnVal is incremented by the
 54695  55553   ** current cache hit or miss count, according to the value of eStat. If the 
 54696  55554   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 54697  55555   ** returning.
 54698  55556   */
 54699  55557   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 54700  55558   
 54701  55559     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 54702  55560          || eStat==SQLITE_DBSTATUS_CACHE_MISS
 54703  55561          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
        55562  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
 54704  55563     );
 54705  55564   
 54706  55565     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 54707  55566     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 54708         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        55567  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
        55568  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
 54709  55569   
 54710         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
        55570  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
        55571  +  *pnVal += pPager->aStat[eStat];
 54711  55572     if( reset ){
 54712         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
        55573  +    pPager->aStat[eStat] = 0;
 54713  55574     }
 54714  55575   }
 54715  55576   
 54716  55577   /*
 54717  55578   ** Return true if this is an in-memory or temp-file backed pager.
 54718  55579   */
 54719  55580   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 56151  57012   ** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
 56152  57013   ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
 56153  57014   **
 56154  57015   ** If this call is successful, *ppPage is set to point to the wal-index
 56155  57016   ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 56156  57017   ** then an SQLite error code is returned and *ppPage is set to 0.
 56157  57018   */
 56158         -static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
        57019  +static SQLITE_NOINLINE int walIndexPageRealloc(
        57020  +  Wal *pWal,               /* The WAL context */
        57021  +  int iPage,               /* The page we seek */
        57022  +  volatile u32 **ppPage    /* Write the page pointer here */
        57023  +){
 56159  57024     int rc = SQLITE_OK;
 56160  57025   
 56161  57026     /* Enlarge the pWal->apWiData[] array if required */
 56162  57027     if( pWal->nWiData<=iPage ){
 56163  57028       int nByte = sizeof(u32*)*(iPage+1);
 56164  57029       volatile u32 **apNew;
 56165  57030       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
................................................................................
 56170  57035       memset((void*)&apNew[pWal->nWiData], 0,
 56171  57036              sizeof(u32*)*(iPage+1-pWal->nWiData));
 56172  57037       pWal->apWiData = apNew;
 56173  57038       pWal->nWiData = iPage+1;
 56174  57039     }
 56175  57040   
 56176  57041     /* Request a pointer to the required page from the VFS */
 56177         -  if( pWal->apWiData[iPage]==0 ){
 56178         -    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 56179         -      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 56180         -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
 56181         -    }else{
 56182         -      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 56183         -          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 56184         -      );
 56185         -      assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
 56186         -      testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
 56187         -      if( (rc&0xff)==SQLITE_READONLY ){
 56188         -        pWal->readOnly |= WAL_SHM_RDONLY;
 56189         -        if( rc==SQLITE_READONLY ){
 56190         -          rc = SQLITE_OK;
 56191         -        }
        57042  +  assert( pWal->apWiData[iPage]==0 );
        57043  +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
        57044  +    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
        57045  +    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
        57046  +  }else{
        57047  +    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
        57048  +        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
        57049  +    );
        57050  +    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
        57051  +    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
        57052  +    if( (rc&0xff)==SQLITE_READONLY ){
        57053  +      pWal->readOnly |= WAL_SHM_RDONLY;
        57054  +      if( rc==SQLITE_READONLY ){
        57055  +        rc = SQLITE_OK;
 56192  57056         }
 56193  57057       }
 56194  57058     }
 56195  57059   
 56196  57060     *ppPage = pWal->apWiData[iPage];
 56197  57061     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 56198  57062     return rc;
        57063  +}
        57064  +static int walIndexPage(
        57065  +  Wal *pWal,               /* The WAL context */
        57066  +  int iPage,               /* The page we seek */
        57067  +  volatile u32 **ppPage    /* Write the page pointer here */
        57068  +){
        57069  +  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
        57070  +    return walIndexPageRealloc(pWal, iPage, ppPage);
        57071  +  }
        57072  +  return SQLITE_OK;
 56199  57073   }
 56200  57074   
 56201  57075   /*
 56202  57076   ** Return a pointer to the WalCkptInfo structure in the wal-index.
 56203  57077   */
 56204  57078   static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 56205  57079     assert( pWal->nWiData>0 && pWal->apWiData[0] );
................................................................................
 57169  58043   */
 57170  58044   static void walIteratorFree(WalIterator *p){
 57171  58045     sqlite3_free(p);
 57172  58046   }
 57173  58047   
 57174  58048   /*
 57175  58049   ** Construct a WalInterator object that can be used to loop over all 
 57176         -** pages in the WAL in ascending order. The caller must hold the checkpoint
 57177         -** lock.
        58050  +** pages in the WAL following frame nBackfill in ascending order. Frames
        58051  +** nBackfill or earlier may be included - excluding them is an optimization
        58052  +** only. The caller must hold the checkpoint lock.
 57178  58053   **
 57179  58054   ** On success, make *pp point to the newly allocated WalInterator object
 57180  58055   ** return SQLITE_OK. Otherwise, return an error code. If this routine
 57181  58056   ** returns an error, the value of *pp is undefined.
 57182  58057   **
 57183  58058   ** The calling routine should invoke walIteratorFree() to destroy the
 57184  58059   ** WalIterator object when it has finished with it.
 57185  58060   */
 57186         -static int walIteratorInit(Wal *pWal, WalIterator **pp){
        58061  +static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
 57187  58062     WalIterator *p;                 /* Return value */
 57188  58063     int nSegment;                   /* Number of segments to merge */
 57189  58064     u32 iLast;                      /* Last frame in log */
 57190  58065     int nByte;                      /* Number of bytes to allocate */
 57191  58066     int i;                          /* Iterator variable */
 57192  58067     ht_slot *aTmp;                  /* Temp space used by merge-sort */
 57193  58068     int rc = SQLITE_OK;             /* Return Code */
................................................................................
 57216  58091     aTmp = (ht_slot *)sqlite3_malloc64(
 57217  58092         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 57218  58093     );
 57219  58094     if( !aTmp ){
 57220  58095       rc = SQLITE_NOMEM_BKPT;
 57221  58096     }
 57222  58097   
 57223         -  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
        58098  +  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
 57224  58099       volatile ht_slot *aHash;
 57225  58100       u32 iZero;
 57226  58101       volatile u32 *aPgno;
 57227  58102   
 57228  58103       rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 57229  58104       if( rc==SQLITE_OK ){
 57230  58105         int j;                      /* Counter variable */
................................................................................
 57250  58125         p->aSegment[i].aPgno = (u32 *)aPgno;
 57251  58126       }
 57252  58127     }
 57253  58128     sqlite3_free(aTmp);
 57254  58129   
 57255  58130     if( rc!=SQLITE_OK ){
 57256  58131       walIteratorFree(p);
        58132  +    p = 0;
 57257  58133     }
 57258  58134     *pp = p;
 57259  58135     return rc;
 57260  58136   }
 57261  58137   
 57262  58138   /*
 57263  58139   ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
................................................................................
 57372  58248   
 57373  58249     szPage = walPagesize(pWal);
 57374  58250     testcase( szPage<=32768 );
 57375  58251     testcase( szPage>=65536 );
 57376  58252     pInfo = walCkptInfo(pWal);
 57377  58253     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 57378  58254   
 57379         -    /* Allocate the iterator */
 57380         -    rc = walIteratorInit(pWal, &pIter);
 57381         -    if( rc!=SQLITE_OK ){
 57382         -      return rc;
 57383         -    }
 57384         -    assert( pIter );
 57385         -
 57386  58255       /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
 57387  58256       ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
 57388  58257       assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
 57389  58258   
 57390  58259       /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 57391  58260       ** safe to write into the database.  Frames beyond mxSafeFrame might
 57392  58261       ** overwrite database pages that are in use by active readers and thus
................................................................................
 57415  58284             xBusy = 0;
 57416  58285           }else{
 57417  58286             goto walcheckpoint_out;
 57418  58287           }
 57419  58288         }
 57420  58289       }
 57421  58290   
 57422         -    if( pInfo->nBackfill<mxSafeFrame
        58291  +    /* Allocate the iterator */
        58292  +    if( pInfo->nBackfill<mxSafeFrame ){
        58293  +      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
        58294  +      assert( rc==SQLITE_OK || pIter==0 );
        58295  +    }
        58296  +
        58297  +    if( pIter
 57423  58298        && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
 57424  58299       ){
 57425  58300         i64 nSize;                    /* Current size of database file */
 57426  58301         u32 nBackfill = pInfo->nBackfill;
 57427  58302   
 57428  58303         pInfo->nBackfillAttempted = mxSafeFrame;
 57429  58304   
................................................................................
 58465  59340     **     This condition filters out normal hash-table collisions.
 58466  59341     **
 58467  59342     **   (iFrame<=iLast): 
 58468  59343     **     This condition filters out entries that were added to the hash
 58469  59344     **     table after the current read-transaction had started.
 58470  59345     */
 58471  59346     iMinHash = walFramePage(pWal->minFrame);
 58472         -  for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
        59347  +  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
 58473  59348       volatile ht_slot *aHash;      /* Pointer to hash table */
 58474  59349       volatile u32 *aPgno;          /* Pointer to array of page numbers */
 58475  59350       u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 58476  59351       int iKey;                     /* Hash slot index */
 58477  59352       int nCollide;                 /* Number of hash collisions remaining */
 58478  59353       int rc;                       /* Error code */
 58479  59354   
................................................................................
 58488  59363           assert( iFrame>iRead || CORRUPT_DB );
 58489  59364           iRead = iFrame;
 58490  59365         }
 58491  59366         if( (nCollide--)==0 ){
 58492  59367           return SQLITE_CORRUPT_BKPT;
 58493  59368         }
 58494  59369       }
        59370  +    if( iRead ) break;
 58495  59371     }
 58496  59372   
 58497  59373   #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 58498  59374     /* If expensive assert() statements are available, do a linear search
 58499  59375     ** of the wal-index file content. Make sure the results agree with the
 58500  59376     ** result obtained using the hash indexes above.  */
 58501  59377     {
................................................................................
 59902  60778   **    eState==FAULT:                   Cursor fault with skipNext as error code.
 59903  60779   */
 59904  60780   struct BtCursor {
 59905  60781     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 59906  60782     u8 curFlags;              /* zero or more BTCF_* flags defined below */
 59907  60783     u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
 59908  60784     u8 hints;                 /* As configured by CursorSetHints() */
 59909         -  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 59910         -  Btree *pBtree;            /* The Btree to which this cursor belongs */
 59911         -  BtShared *pBt;            /* The BtShared this cursor points to */
 59912         -  BtCursor *pNext;          /* Forms a linked list of all cursors */
 59913         -  Pgno *aOverflow;          /* Cache of overflow page locations */
 59914         -  CellInfo info;            /* A parse of the cell we are pointing at */
 59915         -  i64 nKey;                 /* Size of pKey, or last integer key */
 59916         -  void *pKey;               /* Saved key that was cursor last known position */
 59917         -  Pgno pgnoRoot;            /* The root page of this tree */
 59918  60785     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
 59919  60786                      ** Error code if eState==CURSOR_FAULT */
        60787  +  Btree *pBtree;            /* The Btree to which this cursor belongs */
        60788  +  Pgno *aOverflow;          /* Cache of overflow page locations */
        60789  +  void *pKey;               /* Saved key that was cursor last known position */
 59920  60790     /* All fields above are zeroed when the cursor is allocated.  See
 59921  60791     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
 59922  60792     ** initialized. */
        60793  +#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
        60794  +  BtShared *pBt;            /* The BtShared this cursor points to */
        60795  +  BtCursor *pNext;          /* Forms a linked list of all cursors */
        60796  +  CellInfo info;            /* A parse of the cell we are pointing at */
        60797  +  i64 nKey;                 /* Size of pKey, or last integer key */
        60798  +  Pgno pgnoRoot;            /* The root page of this tree */
 59923  60799     i8 iPage;                 /* Index of current page in apPage */
 59924  60800     u8 curIntKey;             /* Value of apPage[0]->intKey */
 59925  60801     u16 ix;                   /* Current index for apPage[iPage] */
 59926  60802     u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
 59927  60803     struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
 59928  60804     MemPage *pPage;                        /* Current page */
 59929  60805     MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
................................................................................
 59965  60841   ** CURSOR_FAULT:
 59966  60842   **   An unrecoverable error (an I/O error or a malloc failure) has occurred
 59967  60843   **   on a different connection that shares the BtShared cache with this
 59968  60844   **   cursor.  The error has left the cache in an inconsistent state.
 59969  60845   **   Do nothing else with this cursor.  Any attempt to use the cursor
 59970  60846   **   should return the error code stored in BtCursor.skipNext
 59971  60847   */
 59972         -#define CURSOR_INVALID           0
 59973         -#define CURSOR_VALID             1
        60848  +#define CURSOR_VALID             0
        60849  +#define CURSOR_INVALID           1
 59974  60850   #define CURSOR_SKIPNEXT          2
 59975  60851   #define CURSOR_REQUIRESEEK       3
 59976  60852   #define CURSOR_FAULT             4
 59977  60853   
 59978  60854   /* 
 59979  60855   ** The database page the PENDING_BYTE occupies. This page is never used.
 59980  60856   */
................................................................................
 64744  65620   **
 64745  65621   ** The simple approach here would be to memset() the entire object
 64746  65622   ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 64747  65623   ** do not need to be zeroed and they are large, so we can save a lot
 64748  65624   ** of run-time by skipping the initialization of those elements.
 64749  65625   */
 64750  65626   SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 64751         -  memset(p, 0, offsetof(BtCursor, iPage));
        65627  +  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
 64752  65628   }
 64753  65629   
 64754  65630   /*
 64755  65631   ** Close a cursor.  The read lock on the database file is released
 64756  65632   ** when the last cursor is closed.
 64757  65633   */
 64758  65634   SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
................................................................................
 64787  65663   ** BtCursor.info structure.  If it is not already valid, call
 64788  65664   ** btreeParseCell() to fill it in.
 64789  65665   **
 64790  65666   ** BtCursor.info is a cache of the information in the current cell.
 64791  65667   ** Using this cache reduces the number of calls to btreeParseCell().
 64792  65668   */
 64793  65669   #ifndef NDEBUG
        65670  +  static int cellInfoEqual(CellInfo *a, CellInfo *b){
        65671  +    if( a->nKey!=b->nKey ) return 0;
        65672  +    if( a->pPayload!=b->pPayload ) return 0;
        65673  +    if( a->nPayload!=b->nPayload ) return 0;
        65674  +    if( a->nLocal!=b->nLocal ) return 0;
        65675  +    if( a->nSize!=b->nSize ) return 0;
        65676  +    return 1;
        65677  +  }
 64794  65678     static void assertCellInfo(BtCursor *pCur){
 64795  65679       CellInfo info;
 64796  65680       memset(&info, 0, sizeof(info));
 64797  65681       btreeParseCell(pCur->pPage, pCur->ix, &info);
 64798         -    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
        65682  +    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
 64799  65683     }
 64800  65684   #else
 64801  65685     #define assertCellInfo(x)
 64802  65686   #endif
 64803  65687   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
 64804  65688     if( pCur->info.nSize==0 ){
 64805  65689       pCur->curFlags |= BTCF_ValidNKey;
................................................................................
 65067  65951       ** The aOverflow[] array is sized at one entry for each overflow page
 65068  65952       ** in the overflow chain. The page number of the first overflow page is
 65069  65953       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
 65070  65954       ** means "not yet known" (the cache is lazily populated).
 65071  65955       */
 65072  65956       if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 65073  65957         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 65074         -      if( nOvfl>pCur->nOvflAlloc ){
        65958  +      if( pCur->aOverflow==0
        65959  +       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
        65960  +      ){
 65075  65961           Pgno *aNew = (Pgno*)sqlite3Realloc(
 65076  65962               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
 65077  65963           );
 65078  65964           if( aNew==0 ){
 65079  65965             return SQLITE_NOMEM_BKPT;
 65080  65966           }else{
 65081         -          pCur->nOvflAlloc = nOvfl*2;
 65082  65967             pCur->aOverflow = aNew;
 65083  65968           }
 65084  65969         }
 65085  65970         memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
 65086  65971         pCur->curFlags |= BTCF_ValidOvfl;
 65087  65972       }else{
 65088  65973         /* If the overflow page-list cache has been allocated and the
................................................................................
 66588  67473   static void freePage(MemPage *pPage, int *pRC){
 66589  67474     if( (*pRC)==SQLITE_OK ){
 66590  67475       *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 66591  67476     }
 66592  67477   }
 66593  67478   
 66594  67479   /*
 66595         -** Free any overflow pages associated with the given Cell.  Write the
 66596         -** local Cell size (the number of bytes on the original page, omitting
 66597         -** overflow) into *pnSize.
        67480  +** Free any overflow pages associated with the given Cell.  Store
        67481  +** size information about the cell in pInfo.
 66598  67482   */
 66599  67483   static int clearCell(
 66600  67484     MemPage *pPage,          /* The page that contains the Cell */
 66601  67485     unsigned char *pCell,    /* First byte of the Cell */
 66602  67486     CellInfo *pInfo          /* Size information about the cell */
 66603  67487   ){
 66604  67488     BtShared *pBt;
................................................................................
 67794  68678       */
 67795  68679       if( pOld->aData[0]!=apOld[0]->aData[0] ){
 67796  68680         rc = SQLITE_CORRUPT_BKPT;
 67797  68681         goto balance_cleanup;
 67798  68682       }
 67799  68683   
 67800  68684       /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
 67801         -    ** constains overflow cells, include them in the b.apCell[] array
        68685  +    ** contains overflow cells, include them in the b.apCell[] array
 67802  68686       ** in the correct spot.
 67803  68687       **
 67804  68688       ** Note that when there are multiple overflow cells, it is always the
 67805  68689       ** case that they are sequential and adjacent.  This invariant arises
 67806  68690       ** because multiple overflows can only occurs when inserting divider
 67807  68691       ** cells into a parent on a prior balance, and divider cells are always
 67808  68692       ** adjacent and are inserted in order.  There is an assert() tagged
................................................................................
 71279  72163         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
 71280  72164       );
 71281  72165     }
 71282  72166     return 1;
 71283  72167   }
 71284  72168   #endif
 71285  72169   
        72170  +#ifdef SQLITE_DEBUG
        72171  +/*
        72172  +** Check that string value of pMem agrees with its integer or real value.
        72173  +**
        72174  +** A single int or real value always converts to the same strings.  But
        72175  +** many different strings can be converted into the same int or real.
        72176  +** If a table contains a numeric value and an index is based on the
        72177  +** corresponding string value, then it is important that the string be
        72178  +** derived from the numeric value, not the other way around, to ensure
        72179  +** that the index and table are consistent.  See ticket
        72180  +** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
        72181  +** an example.
        72182  +**
        72183  +** This routine looks at pMem to verify that if it has both a numeric
        72184  +** representation and a string representation then the string rep has
        72185  +** been derived from the numeric and not the other way around.  It returns
        72186  +** true if everything is ok and false if there is a problem.
        72187  +**
        72188  +** This routine is for use inside of assert() statements only.
        72189  +*/
        72190  +SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
        72191  +  char zBuf[100];
        72192  +  char *z;
        72193  +  int i, j, incr;
        72194  +  if( (p->flags & MEM_Str)==0 ) return 1;
        72195  +  if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
        72196  +  if( p->flags & MEM_Int ){
        72197  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
        72198  +  }else{
        72199  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
        72200  +  }
        72201  +  z = p->z;
        72202  +  i = j = 0;
        72203  +  incr = 1;
        72204  +  if( p->enc!=SQLITE_UTF8 ){
        72205  +    incr = 2;
        72206  +    if( p->enc==SQLITE_UTF16BE ) z++;
        72207  +  }
        72208  +  while( zBuf[j] ){
        72209  +    if( zBuf[j++]!=z[i] ) return 0;
        72210  +    i += incr;
        72211  +  }
        72212  +  return 1;
        72213  +}
        72214  +#endif /* SQLITE_DEBUG */
 71286  72215   
 71287  72216   /*
 71288  72217   ** If pMem is an object with a valid string representation, this routine
 71289  72218   ** ensures the internal encoding for the string representation is
 71290  72219   ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 71291  72220   **
 71292  72221   ** If pMem is not a string object, or the encoding of the string
................................................................................
 71712  72641     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 71713  72642       return memRealValue(pMem);
 71714  72643     }else{
 71715  72644       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 71716  72645       return (double)0;
 71717  72646     }
 71718  72647   }
        72648  +
        72649  +/*
        72650  +** Return 1 if pMem represents true, and return 0 if pMem represents false.
        72651  +** Return the value ifNull if pMem is NULL.  
        72652  +*/
        72653  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
        72654  +  if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
        72655  +  if( pMem->flags & MEM_Null ) return ifNull;
        72656  +  return sqlite3VdbeRealValue(pMem)!=0.0;
        72657  +}
 71719  72658   
 71720  72659   /*
 71721  72660   ** The MEM structure is already a MEM_Real.  Try to also make it a
 71722  72661   ** MEM_Int if we can.
 71723  72662   */
 71724  72663   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 71725  72664     i64 ix;
................................................................................
 71767  72706     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71768  72707     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 71769  72708   
 71770  72709     pMem->u.r = sqlite3VdbeRealValue(pMem);
 71771  72710     MemSetTypeFlag(pMem, MEM_Real);
 71772  72711     return SQLITE_OK;
 71773  72712   }
        72713  +
        72714  +/* Compare a floating point value to an integer.  Return true if the two
        72715  +** values are the same within the precision of the floating point value.
        72716  +**
        72717  +** For some versions of GCC on 32-bit machines, if you do the more obvious
        72718  +** comparison of "r1==(double)i" you sometimes get an answer of false even
        72719  +** though the r1 and (double)i values are bit-for-bit the same.
        72720  +*/
        72721  +static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
        72722  +  double r2 = (double)i;
        72723  +  return memcmp(&r1, &r2, sizeof(r1))==0;
        72724  +}
 71774  72725   
 71775  72726   /*
 71776  72727   ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 71777  72728   ** Invalidate any prior representations.
 71778  72729   **
 71779  72730   ** Every effort is made to force the conversion, even if the input
 71780  72731   ** is a string that does not look completely like a number.  Convert
................................................................................
 71787  72738       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71788  72739       rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
 71789  72740       if( rc==0 ){
 71790  72741         MemSetTypeFlag(pMem, MEM_Int);
 71791  72742       }else{
 71792  72743         i64 i = pMem->u.i;
 71793  72744         sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
 71794         -      if( rc==1 && pMem->u.r==(double)i ){
        72745  +      if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
 71795  72746           pMem->u.i = i;
 71796  72747           MemSetTypeFlag(pMem, MEM_Int);
 71797  72748         }else{
 71798  72749           MemSetTypeFlag(pMem, MEM_Real);
 71799  72750         }
 71800  72751       }
 71801  72752     }
................................................................................
 72270  73221     }else{
 72271  73222       sqlite3VdbeMemStringify(pVal, enc, 0);
 72272  73223       assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 72273  73224     }
 72274  73225     assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 72275  73226                 || pVal->db->mallocFailed );
 72276  73227     if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
        73228  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72277  73229       return pVal->z;
 72278  73230     }else{
 72279  73231       return 0;
 72280  73232     }
 72281  73233   }
 72282  73234   
 72283  73235   /* This function is only available internally, it is not part of the
................................................................................
 72292  73244   */
 72293  73245   SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 72294  73246     if( !pVal ) return 0;
 72295  73247     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 72296  73248     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 72297  73249     assert( (pVal->flags & MEM_RowSet)==0 );
 72298  73250     if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
        73251  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72299  73252       return pVal->z;
 72300  73253     }
 72301  73254     if( pVal->flags&MEM_Null ){
 72302  73255       return 0;
 72303  73256     }
 72304  73257     return valueToText(pVal, enc);
 72305  73258   }
................................................................................
 78073  79026   SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 78074  79027     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78075  79028     sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
 78076  79029   }
 78077  79030   SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 78078  79031     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78079  79032     pCtx->isError = SQLITE_ERROR;
 78080         -  pCtx->fErrorOrAux = 1;
 78081  79033     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 78082  79034   }
 78083  79035   #ifndef SQLITE_OMIT_UTF16
 78084  79036   SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 78085  79037     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78086  79038     pCtx->isError = SQLITE_ERROR;
 78087         -  pCtx->fErrorOrAux = 1;
 78088  79039     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 78089  79040   }
 78090  79041   #endif
 78091  79042   SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 78092  79043     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78093  79044     sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
 78094  79045   }
................................................................................
 78186  79137     if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
 78187  79138       return SQLITE_TOOBIG;
 78188  79139     }
 78189  79140     sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
 78190  79141     return SQLITE_OK;
 78191  79142   }
 78192  79143   SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 78193         -  pCtx->isError = errCode;
 78194         -  pCtx->fErrorOrAux = 1;
        79144  +  pCtx->isError = errCode ? errCode : -1;
 78195  79145   #ifdef SQLITE_DEBUG
 78196  79146     if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
 78197  79147   #endif
 78198  79148     if( pCtx->pOut->flags & MEM_Null ){
 78199  79149       sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 
 78200  79150                            SQLITE_UTF8, SQLITE_STATIC);
 78201  79151     }
 78202  79152   }
 78203  79153   
 78204  79154   /* Force an SQLITE_TOOBIG error. */
 78205  79155   SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 78206  79156     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78207  79157     pCtx->isError = SQLITE_TOOBIG;
 78208         -  pCtx->fErrorOrAux = 1;
 78209  79158     sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
 78210  79159                          SQLITE_UTF8, SQLITE_STATIC);
 78211  79160   }
 78212  79161   
 78213  79162   /* An SQLITE_NOMEM error. */
 78214  79163   SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 78215  79164     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78216  79165     sqlite3VdbeMemSetNull(pCtx->pOut);
 78217  79166     pCtx->isError = SQLITE_NOMEM_BKPT;
 78218         -  pCtx->fErrorOrAux = 1;
 78219  79167     sqlite3OomFault(pCtx->pOut->db);
 78220  79168   }
 78221  79169   
 78222  79170   /*
 78223  79171   ** This function is called after a transaction has been committed. It 
 78224  79172   ** invokes callbacks registered with sqlite3_wal_hook() as required.
 78225  79173   */
................................................................................
 78618  79566     if( pAuxData==0 ){
 78619  79567       pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
 78620  79568       if( !pAuxData ) goto failed;
 78621  79569       pAuxData->iAuxOp = pCtx->iOp;
 78622  79570       pAuxData->iAuxArg = iArg;
 78623  79571       pAuxData->pNextAux = pVdbe->pAuxData;
 78624  79572       pVdbe->pAuxData = pAuxData;
 78625         -    if( pCtx->fErrorOrAux==0 ){
 78626         -      pCtx->isError = 0;
 78627         -      pCtx->fErrorOrAux = 1;
 78628         -    }
        79573  +    if( pCtx->isError==0 ) pCtx->isError = -1;
 78629  79574     }else if( pAuxData->xDeleteAux ){
 78630  79575       pAuxData->xDeleteAux(pAuxData->pAux);
 78631  79576     }
 78632  79577   
 78633  79578     pAuxData->pAux = pAux;
 78634  79579     pAuxData->xDeleteAux = xDelete;
 78635  79580     return;
................................................................................
 79377  80322   /*
 79378  80323   ** Return the value of a status counter for a prepared statement
 79379  80324   */
 79380  80325   SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 79381  80326     Vdbe *pVdbe = (Vdbe*)pStmt;
 79382  80327     u32 v;
 79383  80328   #ifdef SQLITE_ENABLE_API_ARMOR
 79384         -  if( !pStmt ){
        80329  +  if( !pStmt 
        80330  +   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
        80331  +  ){
 79385  80332       (void)SQLITE_MISUSE_BKPT;
 79386  80333       return 0;
 79387  80334     }
 79388  80335   #endif
 79389  80336     if( op==SQLITE_STMTSTATUS_MEMUSED ){
 79390  80337       sqlite3 *db = pVdbe->db;
 79391  80338       sqlite3_mutex_enter(db->mutex);
................................................................................
 80151  81098       pRec->u.i = iValue;
 80152  81099       pRec->flags |= MEM_Int;
 80153  81100     }else{
 80154  81101       pRec->u.r = rValue;
 80155  81102       pRec->flags |= MEM_Real;
 80156  81103       if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
 80157  81104     }
        81105  +  /* TEXT->NUMERIC is many->one.  Hence, it is important to invalidate the
        81106  +  ** string representation after computing a numeric equivalent, because the
        81107  +  ** string representation might not be the canonical representation for the
        81108  +  ** numeric value.  Ticket [343634942dd54ab57b7024] 2018-01-31. */
        81109  +  pRec->flags &= ~MEM_Str;
 80158  81110   }
 80159  81111   
 80160  81112   /*
 80161  81113   ** Processing is determine by the affinity parameter:
 80162  81114   **
 80163  81115   ** SQLITE_AFF_INTEGER:
 80164  81116   ** SQLITE_AFF_REAL:
................................................................................
 80619  81571     for(pOp=&aOp[p->pc]; 1; pOp++){
 80620  81572       /* Errors are detected by individual opcodes, with an immediate
 80621  81573       ** jumps to abort_due_to_error. */
 80622  81574       assert( rc==SQLITE_OK );
 80623  81575   
 80624  81576       assert( pOp>=aOp && pOp<&aOp[p->nOp]);
 80625  81577   #ifdef VDBE_PROFILE
 80626         -    start = sqlite3Hwtime();
        81578  +    start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 80627  81579   #endif
 80628  81580       nVmStep++;
 80629  81581   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 80630  81582       if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
 80631  81583   #endif
 80632  81584   
 80633  81585       /* Only allow tracing if SQLITE_DEBUG is defined.
................................................................................
 82143  83095   ** give a NULL output.
 82144  83096   */
 82145  83097   case OP_And:              /* same as TK_AND, in1, in2, out3 */
 82146  83098   case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 82147  83099     int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82148  83100     int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82149  83101   
 82150         -  pIn1 = &aMem[pOp->p1];
 82151         -  if( pIn1->flags & MEM_Null ){
 82152         -    v1 = 2;
 82153         -  }else{
 82154         -    v1 = sqlite3VdbeIntValue(pIn1)!=0;
 82155         -  }
 82156         -  pIn2 = &aMem[pOp->p2];
 82157         -  if( pIn2->flags & MEM_Null ){
 82158         -    v2 = 2;
 82159         -  }else{
 82160         -    v2 = sqlite3VdbeIntValue(pIn2)!=0;
 82161         -  }
        83102  +  v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
        83103  +  v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
 82162  83104     if( pOp->opcode==OP_And ){
 82163  83105       static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 82164  83106       v1 = and_logic[v1*3+v2];
 82165  83107     }else{
 82166  83108       static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 82167  83109       v1 = or_logic[v1*3+v2];
 82168  83110     }
................................................................................
 82171  83113       MemSetTypeFlag(pOut, MEM_Null);
 82172  83114     }else{
 82173  83115       pOut->u.i = v1;
 82174  83116       MemSetTypeFlag(pOut, MEM_Int);
 82175  83117     }
 82176  83118     break;
 82177  83119   }
        83120  +
        83121  +/* Opcode: IsTrue P1 P2 P3 P4 *
        83122  +** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
        83123  +**
        83124  +** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
        83125  +** IS NOT FALSE operators.
        83126  +**
        83127  +** Interpret the value in register P1 as a boolean value.  Store that
        83128  +** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is 
        83129  +** NULL, then the P3 is stored in register P2.  Invert the answer if P4
        83130  +** is 1.
        83131  +**
        83132  +** The logic is summarized like this:
        83133  +**
        83134  +** <ul> 
        83135  +** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
        83136  +** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
        83137  +** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
        83138  +** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
        83139  +** </ul>
        83140  +*/
        83141  +case OP_IsTrue: {               /* in1, out2 */
        83142  +  assert( pOp->p4type==P4_INT32 );
        83143  +  assert( pOp->p4.i==0 || pOp->p4.i==1 );
        83144  +  assert( pOp->p3==0 || pOp->p3==1 );
        83145  +  sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
        83146  +      sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
        83147  +  break;
        83148  +}
 82178  83149   
 82179  83150   /* Opcode: Not P1 P2 * * *
 82180  83151   ** Synopsis: r[P2]= !r[P1]
 82181  83152   **
 82182  83153   ** Interpret the value in register P1 as a boolean value.  Store the
 82183  83154   ** boolean complement in register P2.  If the value in register P1 is 
 82184  83155   ** NULL, then a NULL is stored in P2.
 82185  83156   */
 82186  83157   case OP_Not: {                /* same as TK_NOT, in1, out2 */
 82187  83158     pIn1 = &aMem[pOp->p1];
 82188  83159     pOut = &aMem[pOp->p2];
 82189         -  sqlite3VdbeMemSetNull(pOut);
 82190  83160     if( (pIn1->flags & MEM_Null)==0 ){
 82191         -    pOut->flags = MEM_Int;
 82192         -    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
        83161  +    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
        83162  +  }else{
        83163  +    sqlite3VdbeMemSetNull(pOut);
 82193  83164     }
 82194  83165     break;
 82195  83166   }
 82196  83167   
 82197  83168   /* Opcode: BitNot P1 P2 * * *
 82198  83169   ** Synopsis: r[P1]= ~r[P1]
 82199  83170   **
................................................................................
 82252  83223   
 82253  83224   /* Opcode: If P1 P2 P3 * *
 82254  83225   **
 82255  83226   ** Jump to P2 if the value in register P1 is true.  The value
 82256  83227   ** is considered true if it is numeric and non-zero.  If the value
 82257  83228   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82258  83229   */
        83230  +case OP_If:  {               /* jump, in1 */
        83231  +  int c;
        83232  +  c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
        83233  +  VdbeBranchTaken(c!=0, 2);
        83234  +  if( c ) goto jump_to_p2;
        83235  +  break;
        83236  +}
        83237  +
 82259  83238   /* Opcode: IfNot P1 P2 P3 * *
 82260  83239   **
 82261  83240   ** Jump to P2 if the value in register P1 is False.  The value
 82262  83241   ** is considered false if it has a numeric value of zero.  If the value
 82263  83242   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82264  83243   */
 82265         -case OP_If:                 /* jump, in1 */
 82266  83244   case OP_IfNot: {            /* jump, in1 */
 82267  83245     int c;
 82268         -  pIn1 = &aMem[pOp->p1];
 82269         -  if( pIn1->flags & MEM_Null ){
 82270         -    c = pOp->p3;
 82271         -  }else{
 82272         -#ifdef SQLITE_OMIT_FLOATING_POINT
 82273         -    c = sqlite3VdbeIntValue(pIn1)!=0;
 82274         -#else
 82275         -    c = sqlite3VdbeRealValue(pIn1)!=0.0;
 82276         -#endif
 82277         -    if( pOp->opcode==OP_IfNot ) c = !c;
 82278         -  }
        83246  +  c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
 82279  83247     VdbeBranchTaken(c!=0, 2);
 82280         -  if( c ){
 82281         -    goto jump_to_p2;
 82282         -  }
        83248  +  if( c ) goto jump_to_p2;
 82283  83249     break;
 82284  83250   }
 82285  83251   
 82286  83252   /* Opcode: IsNull P1 P2 * * *
 82287  83253   ** Synopsis: if r[P1]==NULL goto P2
 82288  83254   **
 82289  83255   ** Jump to P2 if the value in register P1 is NULL.
................................................................................
 82336  83302   ** Store in register r[P3] the byte offset into the database file that is the
 82337  83303   ** start of the payload for the record at which that cursor P1 is currently
 82338  83304   ** pointing.
 82339  83305   **
 82340  83306   ** P2 is the column number for the argument to the sqlite_offset() function.
 82341  83307   ** This opcode does not use P2 itself, but the P2 value is used by the
 82342  83308   ** code generator.  The P1, P2, and P3 operands to this opcode are the
 82343         -** as as for OP_Column.
        83309  +** same as for OP_Column.
 82344  83310   **
 82345  83311   ** This opcode is only available if SQLite is compiled with the
 82346  83312   ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
 82347  83313   */
 82348  83314   case OP_Offset: {          /* out3 */
 82349  83315     VdbeCursor *pC;    /* The VDBE cursor */
 82350  83316     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
................................................................................
 84244  85210     VdbeFrame *pFrame;     /* Root frame of VDBE */
 84245  85211   
 84246  85212     v = 0;
 84247  85213     res = 0;
 84248  85214     pOut = out2Prerelease(p, pOp);
 84249  85215     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 84250  85216     pC = p->apCsr[pOp->p1];
        85217  +  if( !pC->isTable ){
        85218  +    rc = SQLITE_CORRUPT_BKPT;
        85219  +    goto abort_due_to_error;
        85220  +  }
 84251  85221     assert( pC!=0 );
 84252  85222     assert( pC->eCurType==CURTYPE_BTREE );
 84253  85223     assert( pC->uc.pCursor!=0 );
 84254  85224     {
 84255  85225       /* The next rowid or record number (different terms for the same
 84256  85226       ** thing) is obtained in a two-step algorithm.
 84257  85227       **
................................................................................
 86180  87150     sqlite3_context *pCtx;
 86181  87151   
 86182  87152     assert( pOp->p4type==P4_FUNCDEF );
 86183  87153     n = pOp->p5;
 86184  87154     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
 86185  87155     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
 86186  87156     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86187         -  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
        87157  +  pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
        87158  +               (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
 86188  87159     if( pCtx==0 ) goto no_mem;
 86189  87160     pCtx->pMem = 0;
        87161  +  pCtx->pOut = (Mem*)&(pCtx->argv[n]);
        87162  +  sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
 86190  87163     pCtx->pFunc = pOp->p4.pFunc;
 86191  87164     pCtx->iOp = (int)(pOp - aOp);
 86192  87165     pCtx->pVdbe = p;
        87166  +  pCtx->skipFlag = 0;
        87167  +  pCtx->isError = 0;
 86193  87168     pCtx->argc = n;
 86194  87169     pOp->p4type = P4_FUNCCTX;
 86195  87170     pOp->p4.pCtx = pCtx;
 86196  87171     pOp->opcode = OP_AggStep;
 86197  87172     /* Fall through into OP_AggStep */
 86198  87173   }
 86199  87174   case OP_AggStep: {
 86200  87175     int i;
 86201  87176     sqlite3_context *pCtx;
 86202  87177     Mem *pMem;
 86203         -  Mem t;
 86204  87178   
 86205  87179     assert( pOp->p4type==P4_FUNCCTX );
 86206  87180     pCtx = pOp->p4.pCtx;
 86207  87181     pMem = &aMem[pOp->p3];
 86208  87182   
 86209  87183     /* If this function is inside of a trigger, the register array in aMem[]
 86210  87184     ** might change from one evaluation to the next.  The next block of code
................................................................................
 86219  87193     for(i=0; i<pCtx->argc; i++){
 86220  87194       assert( memIsValid(pCtx->argv[i]) );
 86221  87195       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 86222  87196     }
 86223  87197   #endif
 86224  87198   
 86225  87199     pMem->n++;
 86226         -  sqlite3VdbeMemInit(&t, db, MEM_Null);
 86227         -  pCtx->pOut = &t;
 86228         -  pCtx->fErrorOrAux = 0;
 86229         -  pCtx->skipFlag = 0;
        87200  +  assert( pCtx->pOut->flags==MEM_Null );
        87201  +  assert( pCtx->isError==0 );
        87202  +  assert( pCtx->skipFlag==0 );
 86230  87203     (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
 86231         -  if( pCtx->fErrorOrAux ){
 86232         -    if( pCtx->isError ){
 86233         -      sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
        87204  +  if( pCtx->isError ){
        87205  +    if( pCtx->isError>0 ){
        87206  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 86234  87207         rc = pCtx->isError;
 86235  87208       }
 86236         -    sqlite3VdbeMemRelease(&t);
        87209  +    if( pCtx->skipFlag ){
        87210  +      assert( pOp[-1].opcode==OP_CollSeq );
        87211  +      i = pOp[-1].p1;
        87212  +      if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
        87213  +      pCtx->skipFlag = 0;
        87214  +    }
        87215  +    sqlite3VdbeMemRelease(pCtx->pOut);
        87216  +    pCtx->pOut->flags = MEM_Null;
        87217  +    pCtx->isError = 0;
 86237  87218       if( rc ) goto abort_due_to_error;
 86238         -  }else{
 86239         -    assert( t.flags==MEM_Null );
 86240  87219     }
 86241         -  if( pCtx->skipFlag ){
 86242         -    assert( pOp[-1].opcode==OP_CollSeq );
 86243         -    i = pOp[-1].p1;
 86244         -    if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
 86245         -  }
        87220  +  assert( pCtx->pOut->flags==MEM_Null );
        87221  +  assert( pCtx->skipFlag==0 );
 86246  87222     break;
 86247  87223   }
 86248  87224   
 86249  87225   /* Opcode: AggFinal P1 P2 * P4 *
 86250  87226   ** Synopsis: accum=r[P1] N=P2
 86251  87227   **
 86252  87228   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
 86725  87701       pDest->flags = MEM_Null|MEM_Zero;
 86726  87702       pDest->u.nZero = 0;
 86727  87703     }else{
 86728  87704       MemSetTypeFlag(pDest, MEM_Null);
 86729  87705     }
 86730  87706     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
 86731  87707     sqlite3VtabImportErrmsg(p, pVtab);
 86732         -  if( sContext.isError ){
        87708  +  if( sContext.isError>0 ){
        87709  +    sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
 86733  87710       rc = sContext.isError;
 86734  87711     }
 86735  87712     sqlite3VdbeChangeEncoding(pDest, encoding);
 86736  87713     REGISTER_TRACE(pOp->p3, pDest);
 86737  87714     UPDATE_MAX_BLOBSIZE(pDest);
 86738  87715   
 86739  87716     if( sqlite3VdbeMemTooBig(pDest) ){
................................................................................
 86990  87967     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86991  87968     pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
 86992  87969     if( pCtx==0 ) goto no_mem;
 86993  87970     pCtx->pOut = 0;
 86994  87971     pCtx->pFunc = pOp->p4.pFunc;
 86995  87972     pCtx->iOp = (int)(pOp - aOp);
 86996  87973     pCtx->pVdbe = p;
        87974  +  pCtx->isError = 0;
 86997  87975     pCtx->argc = n;
 86998  87976     pOp->p4type = P4_FUNCCTX;
 86999  87977     pOp->p4.pCtx = pCtx;
 87000  87978     assert( OP_PureFunc == OP_PureFunc0+2 );
 87001  87979     assert( OP_Function == OP_Function0+2 );
 87002  87980     pOp->opcode += 2;
 87003  87981     /* Fall through into OP_Function */
................................................................................
 87024  88002   #ifdef SQLITE_DEBUG
 87025  88003     for(i=0; i<pCtx->argc; i++){
 87026  88004       assert( memIsValid(pCtx->argv[i]) );
 87027  88005       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 87028  88006     }
 87029  88007   #endif
 87030  88008     MemSetTypeFlag(pOut, MEM_Null);
 87031         -  pCtx->fErrorOrAux = 0;
        88009  +  assert( pCtx->isError==0 );
 87032  88010     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 87033  88011   
 87034  88012     /* If the function returned an error, throw an exception */
 87035         -  if( pCtx->fErrorOrAux ){
 87036         -    if( pCtx->isError ){
        88013  +  if( pCtx->isError ){
        88014  +    if( pCtx->isError>0 ){
 87037  88015         sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
 87038  88016         rc = pCtx->isError;
 87039  88017       }
 87040  88018       sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
        88019  +    pCtx->isError = 0;
 87041  88020       if( rc ) goto abort_due_to_error;
 87042  88021     }
 87043  88022   
 87044  88023     /* Copy the result of the function into register P3 */
 87045  88024     if( pOut->flags & (MEM_Str|MEM_Blob) ){
 87046  88025       sqlite3VdbeChangeEncoding(pOut, encoding);
 87047  88026       if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
................................................................................
 87075  88054   ** first time they are evaluated for this run.
 87076  88055   **
 87077  88056   ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
 87078  88057   ** error is encountered.
 87079  88058   */
 87080  88059   case OP_Trace:
 87081  88060   case OP_Init: {          /* jump */
        88061  +  int i;
        88062  +#ifndef SQLITE_OMIT_TRACE
 87082  88063     char *zTrace;
 87083         -  int i;
        88064  +#endif
 87084  88065   
 87085  88066     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 87086  88067     ** The "--" string is broken up to prevent false-positives with srcck1.c.
 87087  88068     **
 87088  88069     ** This assert() provides evidence for:
 87089  88070     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
 87090  88071     ** would have been returned by the legacy sqlite3_trace() interface by
................................................................................
 87193  88174   ** readability.  From this point on down, the normal indentation rules are
 87194  88175   ** restored.
 87195  88176   *****************************************************************************/
 87196  88177       }
 87197  88178   
 87198  88179   #ifdef VDBE_PROFILE
 87199  88180       {
 87200         -      u64 endTime = sqlite3Hwtime();
        88181  +      u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 87201  88182         if( endTime>start ) pOrigOp->cycles += endTime - start;
 87202  88183         pOrigOp->cnt++;
 87203  88184       }
 87204  88185   #endif
 87205  88186   
 87206  88187       /* The following code adds nothing to the actual functionality
 87207  88188       ** of the program.  It is only here for testing and debugging.
................................................................................
 91580  92561     ** Z is a string literal if it doesn't match any column names.  In that
 91581  92562     ** case, we need to return right away and not make any changes to
 91582  92563     ** pExpr.
 91583  92564     **
 91584  92565     ** Because no reference was made to outer contexts, the pNC->nRef
 91585  92566     ** fields are not changed in any context.
 91586  92567     */
 91587         -  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 91588         -    pExpr->op = TK_STRING;
 91589         -    pExpr->pTab = 0;
 91590         -    return WRC_Prune;
        92568  +  if( cnt==0 && zTab==0 ){
        92569  +    assert( pExpr->op==TK_ID );
        92570  +    if( ExprHasProperty(pExpr,EP_DblQuoted) ){
        92571  +      pExpr->op = TK_STRING;
        92572  +      pExpr->pTab = 0;
        92573  +      return WRC_Prune;
        92574  +    }
        92575  +    if( sqlite3ExprIdToTrueFalse(pExpr) ){
        92576  +      return WRC_Prune;
        92577  +    }
 91591  92578     }
 91592  92579   
 91593  92580     /*
 91594  92581     ** cnt==0 means there was not match.  cnt>1 means there were two or
 91595  92582     ** more matches.  Either way, we have an error.
 91596  92583     */
 91597  92584     if( cnt!=1 ){
................................................................................
 91932  92919         }
 91933  92920         break;
 91934  92921       }
 91935  92922       case TK_VARIABLE: {
 91936  92923         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
 91937  92924         break;
 91938  92925       }
        92926  +    case TK_IS:
        92927  +    case TK_ISNOT: {
        92928  +      Expr *pRight;
        92929  +      assert( !ExprHasProperty(pExpr, EP_Reduced) );
        92930  +      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
        92931  +      ** and "x IS NOT FALSE". */
        92932  +      if( (pRight = pExpr->pRight)->op==TK_ID ){
        92933  +        int rc = resolveExprStep(pWalker, pRight);
        92934  +        if( rc==WRC_Abort ) return WRC_Abort;
        92935  +        if( pRight->op==TK_TRUEFALSE ){
        92936  +          pExpr->op2 = pExpr->op;
        92937  +          pExpr->op = TK_TRUTH;
        92938  +          return WRC_Continue;
        92939  +        }
        92940  +      }
        92941  +      /* Fall thru */
        92942  +    }
 91939  92943       case TK_BETWEEN:
 91940  92944       case TK_EQ:
 91941  92945       case TK_NE:
 91942  92946       case TK_LT:
 91943  92947       case TK_LE:
 91944  92948       case TK_GT:
 91945         -    case TK_GE:
 91946         -    case TK_IS:
 91947         -    case TK_ISNOT: {
        92949  +    case TK_GE: {
 91948  92950         int nLeft, nRight;
 91949  92951         if( pParse->db->mallocFailed ) break;
 91950  92952         assert( pExpr->pLeft!=0 );
 91951  92953         nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
 91952  92954         if( pExpr->op==TK_BETWEEN ){
 91953  92955           nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
 91954  92956           if( nRight==nLeft ){
................................................................................
 94414  95416   ** This callback is used by multiple expression walkers.
 94415  95417   */
 94416  95418   SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
 94417  95419     UNUSED_PARAMETER(NotUsed);
 94418  95420     pWalker->eCode = 0;
 94419  95421     return WRC_Abort;
 94420  95422   }
        95423  +
        95424  +/*
        95425  +** If the input expression is an ID with the name "true" or "false"
        95426  +** then convert it into an TK_TRUEFALSE term.  Return non-zero if
        95427  +** the conversion happened, and zero if the expression is unaltered.
        95428  +*/
        95429  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
        95430  +  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
        95431  +  if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
        95432  +   || sqlite3StrICmp(pExpr->u.zToken, "false")==0
        95433  +  ){
        95434  +    pExpr->op = TK_TRUEFALSE;
        95435  +    return 1;
        95436  +  }
        95437  +  return 0;
        95438  +}
        95439  +
        95440  +/*
        95441  +** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
        95442  +** and 0 if it is FALSE.
        95443  +*/
        95444  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
        95445  +  assert( pExpr->op==TK_TRUEFALSE );
        95446  +  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
        95447  +       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
        95448  +  return pExpr->u.zToken[4]==0;
        95449  +}
        95450  +
 94421  95451   
 94422  95452   /*
 94423  95453   ** These routines are Walker callbacks used to check expressions to
 94424  95454   ** see if they are "constant" for some definition of constant.  The
 94425  95455   ** Walker.eCode value determines the type of "constant" we are looking
 94426  95456   ** for.
 94427  95457   **
................................................................................
 94462  95492         if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
 94463  95493           return WRC_Continue;
 94464  95494         }else{
 94465  95495           pWalker->eCode = 0;
 94466  95496           return WRC_Abort;
 94467  95497         }
 94468  95498       case TK_ID:
        95499  +      /* Convert "true" or "false" in a DEFAULT clause into the
        95500  +      ** appropriate TK_TRUEFALSE operator */
        95501  +      if( sqlite3ExprIdToTrueFalse(pExpr) ){
        95502  +        return WRC_Prune;
        95503  +      }
        95504  +      /* Fall thru */
 94469  95505       case TK_COLUMN:
 94470  95506       case TK_AGG_FUNCTION:
 94471  95507       case TK_AGG_COLUMN:
 94472  95508         testcase( pExpr->op==TK_ID );
 94473  95509         testcase( pExpr->op==TK_COLUMN );
 94474  95510         testcase( pExpr->op==TK_AGG_FUNCTION );
 94475  95511         testcase( pExpr->op==TK_AGG_COLUMN );
................................................................................
 96225  97261         return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 96226  97262                                  pExpr->iColumn, iTab, target,
 96227  97263                                  pExpr->op2);
 96228  97264       }
 96229  97265       case TK_INTEGER: {
 96230  97266         codeInteger(pParse, pExpr, 0, target);
 96231  97267         return target;
        97268  +    }
        97269  +    case TK_TRUEFALSE: {
        97270  +      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
        97271  +      return target;
 96232  97272       }
 96233  97273   #ifndef SQLITE_OMIT_FLOATING_POINT
 96234  97274       case TK_FLOAT: {
 96235  97275         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 96236  97276         codeReal(v, pExpr->u.zToken, 0, target);
 96237  97277         return target;
 96238  97278       }
................................................................................
 96380  97420       case TK_NOT: {
 96381  97421         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 96382  97422         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 96383  97423         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 96384  97424         testcase( regFree1==0 );
 96385  97425         sqlite3VdbeAddOp2(v, op, r1, inReg);
 96386  97426         break;
        97427  +    }
        97428  +    case TK_TRUTH: {
        97429  +      int isTrue;    /* IS TRUE or IS NOT TRUE */
        97430  +      int bNormal;   /* IS TRUE or IS FALSE */
        97431  +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
        97432  +      testcase( regFree1==0 );
        97433  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        97434  +      bNormal = pExpr->op2==TK_IS;
        97435  +      testcase( isTrue && bNormal);
        97436  +      testcase( !isTrue && bNormal);
        97437  +      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
        97438  +      break;
 96387  97439       }
 96388  97440       case TK_ISNULL:
 96389  97441       case TK_NOTNULL: {
 96390  97442         int addr;
 96391  97443         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
 96392  97444         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 96393  97445         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
................................................................................
 97155  98207         sqlite3ExprCachePop(pParse);
 97156  98208         break;
 97157  98209       }
 97158  98210       case TK_NOT: {
 97159  98211         testcase( jumpIfNull==0 );
 97160  98212         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 97161  98213         break;
        98214  +    }
        98215  +    case TK_TRUTH: {
        98216  +      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
        98217  +      int isTrue;     /* IS TRUE or IS NOT TRUE */
        98218  +      testcase( jumpIfNull==0 );
        98219  +      isNot = pExpr->op2==TK_ISNOT;
        98220  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98221  +      testcase( isTrue && isNot );
        98222  +      testcase( !isTrue && isNot );
        98223  +      if( isTrue ^ isNot ){
        98224  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98225  +                          isNot ? SQLITE_JUMPIFNULL : 0);
        98226  +      }else{
        98227  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98228  +                           isNot ? SQLITE_JUMPIFNULL : 0);
        98229  +      }
        98230  +      break;
 97162  98231       }
 97163  98232       case TK_IS:
 97164  98233       case TK_ISNOT:
 97165  98234         testcase( op==TK_IS );
 97166  98235         testcase( op==TK_ISNOT );
 97167  98236         op = (op==TK_IS) ? TK_EQ : TK_NE;
 97168  98237         jumpIfNull = SQLITE_NULLEQ;
................................................................................
 97309  98378         sqlite3ExprCachePop(pParse);
 97310  98379         break;
 97311  98380       }
 97312  98381       case TK_NOT: {
 97313  98382         testcase( jumpIfNull==0 );
 97314  98383         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 97315  98384         break;
        98385  +    }
        98386  +    case TK_TRUTH: {
        98387  +      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
        98388  +      int isTrue;  /* IS TRUE or IS NOT TRUE */
        98389  +      testcase( jumpIfNull==0 );
        98390  +      isNot = pExpr->op2==TK_ISNOT;
        98391  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98392  +      testcase( isTrue && isNot );
        98393  +      testcase( !isTrue && isNot );
        98394  +      if( isTrue ^ isNot ){
        98395  +        /* IS TRUE and IS NOT FALSE */
        98396  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98397  +                           isNot ? 0 : SQLITE_JUMPIFNULL);
        98398  +
        98399  +      }else{
        98400  +        /* IS FALSE and IS NOT TRUE */
        98401  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98402  +                          isNot ? 0 : SQLITE_JUMPIFNULL);
        98403  +      }
        98404  +      break;
 97316  98405       }
 97317  98406       case TK_IS:
 97318  98407       case TK_ISNOT:
 97319  98408         testcase( pExpr->op==TK_IS );
 97320  98409         testcase( pExpr->op==TK_ISNOT );
 97321  98410         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 97322  98411         jumpIfNull = SQLITE_NULLEQ;
................................................................................
100820 101909   **
100821 101910   **     ATTACH DATABASE x AS y KEY z
100822 101911   **
100823 101912   **     SELECT sqlite_attach(x, y, z)
100824 101913   **
100825 101914   ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
100826 101915   ** third argument.
       101916  +**
       101917  +** If the db->init.reopenMemdb flags is set, then instead of attaching a
       101918  +** new database, close the database on db->init.iDb and reopen it as an
       101919  +** empty MemDB.
100827 101920   */
100828 101921   static void attachFunc(
100829 101922     sqlite3_context *context,
100830 101923     int NotUsed,
100831 101924     sqlite3_value **argv
100832 101925   ){
100833 101926     int i;
................................................................................
100840 101933     unsigned int flags;
100841 101934     Db *aNew;                 /* New array of Db pointers */
100842 101935     Db *pNew;                 /* Db object for the newly attached database */
100843 101936     char *zErrDyn = 0;
100844 101937     sqlite3_vfs *pVfs;
100845 101938   
100846 101939     UNUSED_PARAMETER(NotUsed);
100847         -
100848 101940     zFile = (const char *)sqlite3_value_text(argv[0]);
100849 101941     zName = (const char *)sqlite3_value_text(argv[1]);
100850 101942     if( zFile==0 ) zFile = "";
100851 101943     if( zName==0 ) zName = "";
100852 101944   
100853         -  /* Check for the following errors:
100854         -  **
100855         -  **     * Too many attached databases,
100856         -  **     * Transaction currently open
100857         -  **     * Specified database name already being used.
100858         -  */
100859         -  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100860         -    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
100861         -      db->aLimit[SQLITE_LIMIT_ATTACHED]
100862         -    );
100863         -    goto attach_error;
100864         -  }
100865         -  for(i=0; i<db->nDb; i++){
100866         -    char *z = db->aDb[i].zDbSName;
100867         -    assert( z && zName );
100868         -    if( sqlite3StrICmp(z, zName)==0 ){
100869         -      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       101945  +#ifdef SQLITE_ENABLE_DESERIALIZE
       101946  +# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
       101947  +#else
       101948  +# define REOPEN_AS_MEMDB(db)  (0)
       101949  +#endif
       101950  +
       101951  +  if( REOPEN_AS_MEMDB(db) ){
       101952  +    /* This is not a real ATTACH.  Instead, this routine is being called
       101953  +    ** from sqlite3_deserialize() to close database db->init.iDb and
       101954  +    ** reopen it as a MemDB */
       101955  +    pVfs = sqlite3_vfs_find("memdb");
       101956  +    if( pVfs==0 ) return;
       101957  +    pNew = &db->aDb[db->init.iDb];
       101958  +    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
       101959  +    pNew->pBt = 0;
       101960  +    pNew->pSchema = 0;
       101961  +    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       101962  +  }else{
       101963  +    /* This is a real ATTACH
       101964  +    **
       101965  +    ** Check for the following errors:
       101966  +    **
       101967  +    **     * Too many attached databases,
       101968  +    **     * Transaction currently open
       101969  +    **     * Specified database name already being used.
       101970  +    */
       101971  +    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
       101972  +      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
       101973  +        db->aLimit[SQLITE_LIMIT_ATTACHED]
       101974  +      );
100870 101975         goto attach_error;
100871 101976       }
100872         -  }
100873         -
100874         -  /* Allocate the new entry in the db->aDb[] array and initialize the schema
100875         -  ** hash tables.
100876         -  */
100877         -  if( db->aDb==db->aDbStatic ){
100878         -    aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100879         -    if( aNew==0 ) return;
100880         -    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100881         -  }else{
100882         -    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100883         -    if( aNew==0 ) return;
100884         -  }
100885         -  db->aDb = aNew;
100886         -  pNew = &db->aDb[db->nDb];
100887         -  memset(pNew, 0, sizeof(*pNew));
100888         -
100889         -  /* Open the database file. If the btree is successfully opened, use
100890         -  ** it to obtain the database schema. At this point the schema may
100891         -  ** or may not be initialized.
100892         -  */
100893         -  flags = db->openFlags;
100894         -  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100895         -  if( rc!=SQLITE_OK ){
100896         -    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
100897         -    sqlite3_result_error(context, zErr, -1);
100898         -    sqlite3_free(zErr);
100899         -    return;
100900         -  }
100901         -  assert( pVfs );
100902         -  flags |= SQLITE_OPEN_MAIN_DB;
100903         -  rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100904         -  sqlite3_free( zPath );
100905         -  db->nDb++;
       101977  +    for(i=0; i<db->nDb; i++){
       101978  +      char *z = db->aDb[i].zDbSName;
       101979  +      assert( z && zName );
       101980  +      if( sqlite3StrICmp(z, zName)==0 ){
       101981  +        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       101982  +        goto attach_error;
       101983  +      }
       101984  +    }
       101985  +  
       101986  +    /* Allocate the new entry in the db->aDb[] array and initialize the schema
       101987  +    ** hash tables.
       101988  +    */
       101989  +    if( db->aDb==db->aDbStatic ){
       101990  +      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
       101991  +      if( aNew==0 ) return;
       101992  +      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
       101993  +    }else{
       101994  +      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
       101995  +      if( aNew==0 ) return;
       101996  +    }
       101997  +    db->aDb = aNew;
       101998  +    pNew = &db->aDb[db->nDb];
       101999  +    memset(pNew, 0, sizeof(*pNew));
       102000  +  
       102001  +    /* Open the database file. If the btree is successfully opened, use
       102002  +    ** it to obtain the database schema. At this point the schema may
       102003  +    ** or may not be initialized.
       102004  +    */
       102005  +    flags = db->openFlags;
       102006  +    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
       102007  +    if( rc!=SQLITE_OK ){
       102008  +      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
       102009  +      sqlite3_result_error(context, zErr, -1);
       102010  +      sqlite3_free(zErr);
       102011  +      return;
       102012  +    }
       102013  +    assert( pVfs );
       102014  +    flags |= SQLITE_OPEN_MAIN_DB;
       102015  +    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
       102016  +    sqlite3_free( zPath );
       102017  +    db->nDb++;
       102018  +  }
100906 102019     db->skipBtreeMutex = 0;
100907 102020     if( rc==SQLITE_CONSTRAINT ){
100908 102021       rc = SQLITE_ERROR;
100909 102022       zErrDyn = sqlite3MPrintf(db, "database is already attached");
100910 102023     }else if( rc==SQLITE_OK ){
100911 102024       Pager *pPager;
100912 102025       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
................................................................................
100925 102038   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
100926 102039       sqlite3BtreeSetPagerFlags(pNew->pBt,
100927 102040                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100928 102041   #endif
100929 102042       sqlite3BtreeLeave(pNew->pBt);
100930 102043     }
100931 102044     pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100932         -  pNew->zDbSName = sqlite3DbStrDup(db, zName);
       102045  +  if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
100933 102046     if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100934 102047       rc = SQLITE_NOMEM_BKPT;
100935 102048     }
100936 102049   
100937 102050   
100938 102051   #ifdef SQLITE_HAS_CODEC
100939 102052     if( rc==SQLITE_OK ){
................................................................................
100965 102078           break;
100966 102079       }
100967 102080     }
100968 102081   #endif
100969 102082   
100970 102083     /* If the file was opened successfully, read the schema for the new database.
100971 102084     ** If this fails, or if opening the file failed, then close the file and 
100972         -  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100973         -  ** we found it.
       102085  +  ** remove the entry from the db->aDb[] array. i.e. put everything back the
       102086  +  ** way we found it.
100974 102087     */
100975 102088     if( rc==SQLITE_OK ){
100976 102089       sqlite3BtreeEnterAll(db);
       102090  +    db->init.iDb = 0;
100977 102091       rc = sqlite3Init(db, &zErrDyn);
100978 102092       sqlite3BtreeLeaveAll(db);
       102093  +    assert( zErrDyn==0 || rc!=SQLITE_OK );
100979 102094     }
100980 102095   #ifdef SQLITE_USER_AUTHENTICATION
100981 102096     if( rc==SQLITE_OK ){
100982 102097       u8 newAuth = 0;
100983 102098       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
100984 102099       if( newAuth<db->auth.authLevel ){
100985 102100         rc = SQLITE_AUTH_USER;
100986 102101       }
100987 102102     }
100988 102103   #endif
100989 102104     if( rc ){
100990         -    int iDb = db->nDb - 1;
100991         -    assert( iDb>=2 );
100992         -    if( db->aDb[iDb].pBt ){
100993         -      sqlite3BtreeClose(db->aDb[iDb].pBt);
100994         -      db->aDb[iDb].pBt = 0;
100995         -      db->aDb[iDb].pSchema = 0;
100996         -    }
100997         -    sqlite3ResetAllSchemasOfConnection(db);
100998         -    db->nDb = iDb;
100999         -    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
101000         -      sqlite3OomFault(db);
101001         -      sqlite3DbFree(db, zErrDyn);
101002         -      zErrDyn = sqlite3MPrintf(db, "out of memory");
101003         -    }else if( zErrDyn==0 ){
101004         -      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102105  +    if( !REOPEN_AS_MEMDB(db) ){
       102106  +      int iDb = db->nDb - 1;
       102107  +      assert( iDb>=2 );
       102108  +      if( db->aDb[iDb].pBt ){
       102109  +        sqlite3BtreeClose(db->aDb[iDb].pBt);
       102110  +        db->aDb[iDb].pBt = 0;
       102111  +        db->aDb[iDb].pSchema = 0;
       102112  +      }
       102113  +      sqlite3ResetAllSchemasOfConnection(db);
       102114  +      db->nDb = iDb;
       102115  +      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       102116  +        sqlite3OomFault(db);
       102117  +        sqlite3DbFree(db, zErrDyn);
       102118  +        zErrDyn = sqlite3MPrintf(db, "out of memory");
       102119  +      }else if( zErrDyn==0 ){
       102120  +        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102121  +      }
101005 102122       }
101006 102123       goto attach_error;
101007 102124     }
101008 102125     
101009 102126     return;
101010 102127   
101011 102128   attach_error:
................................................................................
101268 102385         return 1;
101269 102386       }
101270 102387       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101271 102388         return 1;
101272 102389       }
101273 102390       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101274 102391         return 1;
       102392  +    }
       102393  +    if( pSelect->pWith ){
       102394  +      int i;
       102395  +      for(i=0; i<pSelect->pWith->nCte; i++){
       102396  +        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
       102397  +          return 1;
       102398  +        }
       102399  +      }
101275 102400       }
101276 102401       pSelect = pSelect->pPrior;
101277 102402     }
101278 102403     return 0;
101279 102404   }
101280 102405   SQLITE_PRIVATE int sqlite3FixExpr(
101281 102406     DbFixer *pFix,     /* Context of the fixation */
................................................................................
102732 103857   ** This routine is called by the parser while in the middle of
102733 103858   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
102734 103859   ** been seen on a column.  This routine sets the notNull flag on
102735 103860   ** the column currently under construction.
102736 103861   */
102737 103862   SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
102738 103863     Table *p;
       103864  +  Column *pCol;
102739 103865     p = pParse->pNewTable;
102740 103866     if( p==0 || NEVER(p->nCol<1) ) return;
102741         -  p->aCol[p->nCol-1].notNull = (u8)onError;
       103867  +  pCol = &p->aCol[p->nCol-1];
       103868  +  pCol->notNull = (u8)onError;
102742 103869     p->tabFlags |= TF_HasNotNull;
       103870  +
       103871  +  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
       103872  +  ** on this column.  */
       103873  +  if( pCol->colFlags & COLFLAG_UNIQUE ){
       103874  +    Index *pIdx;
       103875  +    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
       103876  +      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
       103877  +      if( pIdx->aiColumn[0]==p->nCol-1 ){
       103878  +        pIdx->uniqNotNull = 1;
       103879  +      }
       103880  +    }
       103881  +  }
102743 103882   }
102744 103883   
102745 103884   /*
102746 103885   ** Scan the column type name zType (length nType) and return the
102747 103886   ** associated affinity type.
102748 103887   **
102749 103888   ** This routine does a case-independent search of zType for the 
................................................................................
103470 104609     if( pEnd==0 && pSelect==0 ){
103471 104610       return;
103472 104611     }
103473 104612     assert( !db->mallocFailed );
103474 104613     p = pParse->pNewTable;
103475 104614     if( p==0 ) return;
103476 104615   
103477         -  assert( !db->init.busy || !pSelect );
103478         -
103479 104616     /* If the db->init.busy is 1 it means we are reading the SQL off the
103480 104617     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
103481 104618     ** So do not write to the disk again.  Extract the root page number
103482 104619     ** for the table from the db->init.newTnum field.  (The page number
103483 104620     ** should have been put there by the sqliteOpenCb routine.)
103484 104621     **
103485 104622     ** If the root page number is 1, that means this is the sqlite_master
103486 104623     ** table itself.  So mark it read-only.
103487 104624     */
103488 104625     if( db->init.busy ){
       104626  +    if( pSelect ){
       104627  +      sqlite3ErrorMsg(pParse, "");
       104628  +      return;
       104629  +    }
103489 104630       p->tnum = db->init.newTnum;
103490 104631       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103491 104632     }
103492 104633   
103493 104634     /* Special processing for WITHOUT ROWID Tables */
103494 104635     if( tabOpts & TF_WithoutRowid ){
103495 104636       if( (p->tabFlags & TF_Autoincrement) ){
................................................................................
104699 105840   
104700 105841     /* If pList==0, it means this routine was called to make a primary
104701 105842     ** key out of the last column added to the table under construction.
104702 105843     ** So create a fake list to simulate this.
104703 105844     */
104704 105845     if( pList==0 ){
104705 105846       Token prevCol;
104706         -    sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
       105847  +    Column *pCol = &pTab->aCol[pTab->nCol-1];
       105848  +    pCol->colFlags |= COLFLAG_UNIQUE;
       105849  +    sqlite3TokenInit(&prevCol, pCol->zName);
104707 105850       pList = sqlite3ExprListAppend(pParse, 0,
104708 105851                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
104709 105852       if( pList==0 ) goto exit_create_index;
104710 105853       assert( pList->nExpr==1 );
104711 105854       sqlite3ExprListSetSortOrder(pList, sortOrder);
104712 105855     }else{
104713 105856       sqlite3ExprListCheckLength(pParse, pList, "index");
................................................................................
107544 108687   }
107545 108688   
107546 108689   /*
107547 108690   ** Indicate that the accumulator load should be skipped on this
107548 108691   ** iteration of the aggregate loop.
107549 108692   */
107550 108693   static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
       108694  +  assert( context->isError<=0 );
       108695  +  context->isError = -1;
107551 108696     context->skipFlag = 1;
107552 108697   }
107553 108698   
107554 108699   /*
107555 108700   ** Implementation of the non-aggregate min() and max() functions
107556 108701   */
107557 108702   static void minmaxFunc(
................................................................................
107610 108755   ** Implementation of the length() function
107611 108756   */
107612 108757   static void lengthFunc(
107613 108758     sqlite3_context *context,
107614 108759     int argc,
107615 108760     sqlite3_value **argv
107616 108761   ){
107617         -  int len;
107618         -
107619 108762     assert( argc==1 );
107620 108763     UNUSED_PARAMETER(argc);
107621 108764     switch( sqlite3_value_type(argv[0]) ){
107622 108765       case SQLITE_BLOB:
107623 108766       case SQLITE_INTEGER:
107624 108767       case SQLITE_FLOAT: {
107625 108768         sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
107626 108769         break;
107627 108770       }
107628 108771       case SQLITE_TEXT: {
107629 108772         const unsigned char *z = sqlite3_value_text(argv[0]);
       108773  +      const unsigned char *z0;
       108774  +      unsigned char c;
107630 108775         if( z==0 ) return;
107631         -      len = 0;
107632         -      while( *z ){
107633         -        len++;
107634         -        SQLITE_SKIP_UTF8(z);
       108776  +      z0 = z;
       108777  +      while( (c = *z)!=0 ){
       108778  +        z++;
       108779  +        if( c>=0xc0 ){
       108780  +          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
       108781  +        }
107635 108782         }
107636         -      sqlite3_result_int(context, len);
       108783  +      sqlite3_result_int(context, (int)(z-z0));
107637 108784         break;
107638 108785       }
107639 108786       default: {
107640 108787         sqlite3_result_null(context);
107641 108788         break;
107642 108789       }
107643 108790     }
................................................................................
108704 109851     unsigned char *zOut;              /* The output */
108705 109852     int nStr;                /* Size of zStr */
108706 109853     int nPattern;            /* Size of zPattern */
108707 109854     int nRep;                /* Size of zRep */
108708 109855     i64 nOut;                /* Maximum size of zOut */
108709 109856     int loopLimit;           /* Last zStr[] that might match zPattern[] */
108710 109857     int i, j;                /* Loop counters */
       109858  +  unsigned cntExpand;      /* Number zOut expansions */
       109859  +  sqlite3 *db = sqlite3_context_db_handle(context);
108711 109860   
108712 109861     assert( argc==3 );
108713 109862     UNUSED_PARAMETER(argc);
108714 109863     zStr = sqlite3_value_text(argv[0]);
108715 109864     if( zStr==0 ) return;
108716 109865     nStr = sqlite3_value_bytes(argv[0]);
108717 109866     assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
................................................................................
108735 109884     nOut = nStr + 1;
108736 109885     assert( nOut<SQLITE_MAX_LENGTH );
108737 109886     zOut = contextMalloc(context, (i64)nOut);
108738 109887     if( zOut==0 ){
108739 109888       return;
108740 109889     }
108741 109890     loopLimit = nStr - nPattern;  
       109891  +  cntExpand = 0;
108742 109892     for(i=j=0; i<=loopLimit; i++){
108743 109893       if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108744 109894         zOut[j++] = zStr[i];
108745 109895       }else{
108746         -      u8 *zOld;
108747         -      sqlite3 *db = sqlite3_context_db_handle(context);
108748         -      nOut += nRep - nPattern;
108749         -      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108750         -      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108751         -      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108752         -        sqlite3_result_error_toobig(context);
108753         -        sqlite3_free(zOut);
108754         -        return;
108755         -      }
108756         -      zOld = zOut;
108757         -      zOut = sqlite3_realloc64(zOut, (int)nOut);
108758         -      if( zOut==0 ){
108759         -        sqlite3_result_error_nomem(context);
108760         -        sqlite3_free(zOld);
108761         -        return;
       109896  +      if( nRep>nPattern ){
       109897  +        nOut += nRep - nPattern;
       109898  +        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
       109899  +        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
       109900  +        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       109901  +          sqlite3_result_error_toobig(context);
       109902  +          sqlite3_free(zOut);
       109903  +          return;
       109904  +        }
       109905  +        cntExpand++;
       109906  +        if( (cntExpand&(cntExpand-1))==0 ){
       109907  +          /* Grow the size of the output buffer only on substitutions
       109908  +          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
       109909  +          u8 *zOld;
       109910  +          zOld = zOut;
       109911  +          zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
       109912  +          if( zOut==0 ){
       109913  +            sqlite3_result_error_nomem(context);
       109914  +            sqlite3_free(zOld);
       109915  +            return;
       109916  +          }
       109917  +        }
108762 109918         }
108763 109919         memcpy(&zOut[j], zRep, nRep);
108764 109920         j += nRep;
108765 109921         i += nPattern-1;
108766 109922       }
108767 109923     }
108768         -  assert( j+nStr-i+1==nOut );
       109924  +  assert( j+nStr-i+1<=nOut );
108769 109925     memcpy(&zOut[j], &zStr[i], nStr-i);
108770 109926     j += nStr - i;
108771 109927     assert( j<=nOut );
108772 109928     zOut[j] = 0;
108773 109929     sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
108774 109930   }
108775 109931   
................................................................................
113824 114980   #define sqlite3_prepare_v3             sqlite3_api->prepare_v3
113825 114981   #define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
113826 114982   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
113827 114983   #define sqlite3_result_pointer         sqlite3_api->result_pointer
113828 114984   #define sqlite3_value_pointer          sqlite3_api->value_pointer
113829 114985   /* Version 3.22.0 and later */
113830 114986   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
113831         -#define sqlite3_value_nochange         sqltie3_api->value_nochange
113832         -#define sqlite3_vtab_collation         sqltie3_api->vtab_collation
       114987  +#define sqlite3_value_nochange         sqlite3_api->value_nochange
       114988  +#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
113833 114989   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
113834 114990   
113835 114991   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
113836 114992     /* This case when the file really is being compiled as a loadable 
113837 114993     ** extension */
113838 114994   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
113839 114995   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
117809 118965     const char *zExtra   /* Error information */
117810 118966   ){
117811 118967     sqlite3 *db = pData->db;
117812 118968     if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117813 118969       char *z;
117814 118970       if( zObj==0 ) zObj = "?";
117815 118971       z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
117816         -    if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
       118972  +    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117817 118973       sqlite3DbFree(db, *pData->pzErrMsg);
117818 118974       *pData->pzErrMsg = z;
117819 118975     }
117820 118976     pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117821 118977   }
117822 118978   
117823 118979   /*
................................................................................
119948 121104       bSeq = 0;
119949 121105     }else{
119950 121106       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
119951 121107       codeOffset(v, p->iOffset, addrContinue);
119952 121108       iSortTab = iTab;
119953 121109       bSeq = 1;
119954 121110     }
119955         -  for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
       121111  +  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
       121112  +    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
       121113  +  }
       121114  +  for(i=nSortData-1; i>=0; i--){
119956 121115       int iRead;
119957 121116       if( aOutEx[i].u.x.iOrderByCol ){
119958 121117         iRead = aOutEx[i].u.x.iOrderByCol-1;
119959 121118       }else{
119960         -      iRead = iCol++;
       121119  +      iRead = iCol--;
119961 121120       }
119962 121121       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
119963 121122       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
119964 121123     }
119965 121124     switch( eDest ){
119966 121125       case SRT_Table:
119967 121126       case SRT_EphemTab: {
................................................................................
127020 128179   
127021 128180     /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
127022 128181     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127023 128182     if( rc!=SQLITE_OK ) return rc;
127024 128183     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
127025 128184       const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
127026 128185       assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
127027         -    if( zSubSql ){
127028         -      assert( zSubSql[0]!='S' );
       128186  +    assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
       128187  +    if( zSubSql && zSubSql[0]!='S' ){
127029 128188         rc = execSql(db, pzErrMsg, zSubSql);
127030 128189         if( rc!=SQLITE_OK ) break;
127031 128190       }
127032 128191     }
127033 128192     assert( rc!=SQLITE_ROW );
127034 128193     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127035 128194     if( rc ){
................................................................................
130559 131718         testcase( pStart->wtFlags & TERM_VIRTUAL );
130560 131719         pX = pStart->pExpr;
130561 131720         assert( pX!=0 );
130562 131721         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130563 131722         if( sqlite3ExprIsVector(pX->pRight) ){
130564 131723           r1 = rTemp = sqlite3GetTempReg(pParse);
130565 131724           codeExprOrVector(pParse, pX->pRight, r1, 1);
130566         -        op = aMoveOp[(pX->op - TK_GT) | 0x0001];
       131725  +        testcase( pX->op==TK_GT );
       131726  +        testcase( pX->op==TK_GE );
       131727  +        testcase( pX->op==TK_LT );
       131728  +        testcase( pX->op==TK_LE );
       131729  +        op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
       131730  +        assert( pX->op!=TK_GT || op==OP_SeekGE );
       131731  +        assert( pX->op!=TK_GE || op==OP_SeekGE );
       131732  +        assert( pX->op!=TK_LT || op==OP_SeekLE );
       131733  +        assert( pX->op!=TK_LE || op==OP_SeekLE );
130567 131734         }else{
130568 131735           r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130569 131736           disableTerm(pLevel, pStart);
130570 131737           op = aMoveOp[(pX->op - TK_GT)];
130571 131738         }
130572 131739         sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
130573 131740         VdbeComment((v, "pk"));
................................................................................
131334 132501       pE = pTerm->pExpr;
131335 132502       assert( !ExprHasProperty(pE, EP_FromJoin) );
131336 132503       assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131337 132504       pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131338 132505                       WO_EQ|WO_IN|WO_IS, 0);
131339 132506       if( pAlt==0 ) continue;
131340 132507       if( pAlt->wtFlags & (TERM_CODED) ) continue;
       132508  +    if( (pAlt->eOperator & WO_IN) 
       132509  +     && (pAlt->pExpr->flags & EP_xIsSelect)
       132510  +     && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
       132511  +    ){
       132512  +      continue;
       132513  +    }
131341 132514       testcase( pAlt->eOperator & WO_EQ );
131342 132515       testcase( pAlt->eOperator & WO_IS );
131343 132516       testcase( pAlt->eOperator & WO_IN );
131344 132517       VdbeModuleComment((v, "begin transitive constraint"));
131345 132518       sEAlt = *pAlt->pExpr;
131346 132519       sEAlt.pLeft = pE->pLeft;
131347 132520       sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
................................................................................
132248 133421       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
132249 133422       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
132250 133423       if( ALWAYS(pSrc!=0) ){
132251 133424         int i;
132252 133425         for(i=0; i<pSrc->nSrc; i++){
132253 133426           mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132254 133427           mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
       133428  +        if( pSrc->a[i].fg.isTabFunc ){
       133429  +          mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
       133430  +        }
132255 133431         }
132256 133432       }
132257 133433       pS = pS->pPrior;
132258 133434     }
132259 133435     return mask;
132260 133436   }
132261 133437   
................................................................................
132660 133836   
132661 133837         pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
132662 133838         transferJoinMarkings(pNew, pExpr);
132663 133839         idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
132664 133840         exprAnalyze(pSrc, pWC, idxNew);
132665 133841       }
132666 133842       pTerm = &pWC->a[idxTerm];
132667         -    pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
       133843  +    pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
132668 133844       pTerm->eOperator = 0;
132669 133845     }
132670 133846   
132671 133847     /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132672 133848     ** a virtual term for each vector component. The expression object
132673 133849     ** used by each such virtual term is pExpr (the full vector IN(...) 
132674 133850     ** expression). The WhereTerm.iField variable identifies the index within
................................................................................
135385 136561       }else if( eOp & (WO_EQ|WO_IS) ){
135386 136562         int iCol = pProbe->aiColumn[saved_nEq];
135387 136563         pNew->wsFlags |= WHERE_COLUMN_EQ;
135388 136564         assert( saved_nEq==pNew->u.btree.nEq );
135389 136565         if( iCol==XN_ROWID 
135390 136566          || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135391 136567         ){
135392         -        if( iCol>=0 && pProbe->uniqNotNull==0 ){
135393         -          pNew->wsFlags |= WHERE_UNQ_WANTED;
135394         -        }else{
       136568  +        if( iCol==XN_ROWID || pProbe->uniqNotNull 
       136569  +         || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ) 
       136570  +        ){
135395 136571             pNew->wsFlags |= WHERE_ONEROW;
       136572  +        }else{
       136573  +          pNew->wsFlags |= WHERE_UNQ_WANTED;
135396 136574           }
135397 136575         }
135398 136576       }else if( eOp & WO_ISNULL ){
135399 136577         pNew->wsFlags |= WHERE_COLUMN_NULL;
135400 136578       }else if( eOp & (WO_GT|WO_GE) ){
135401 136579         testcase( eOp & WO_GT );
135402 136580         testcase( eOp & WO_GE );
................................................................................
137535 138713     ** preserves SQLite's legacy behaviour in the following two cases:
137536 138714     **
137537 138715     **   FROM ... WHERE random()>0;           -- eval random() once per row
137538 138716     **   FROM ... WHERE (SELECT random())>0;  -- eval random() once overall
137539 138717     */
137540 138718     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137541 138719       WhereTerm *pT = &sWLB.pWC->a[ii];
       138720  +    if( pT->wtFlags & TERM_VIRTUAL ) continue;
137542 138721       if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137543 138722         sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137544 138723         pT->wtFlags |= TERM_CODED;
137545 138724       }
137546 138725     }
137547 138726   
137548 138727     if( wctrlFlags & WHERE_WANT_DISTINCT ){
................................................................................
139993 141172     if( stateno>YY_MAX_SHIFT ) return stateno;
139994 141173     assert( stateno <= YY_SHIFT_COUNT );
139995 141174   #if defined(YYCOVERAGE)
139996 141175     yycoverage[stateno][iLookAhead] = 1;
139997 141176   #endif
139998 141177     do{
139999 141178       i = yy_shift_ofst[stateno];
140000         -    assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
       141179  +    assert( i>=0 );
       141180  +    assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
140001 141181       assert( iLookAhead!=YYNOCODE );
140002 141182       assert( iLookAhead < YYNTOKEN );
140003 141183       i += iLookAhead;
140004 141184       if( yy_lookahead[i]!=iLookAhead ){
140005 141185   #ifdef YYFALLBACK
140006 141186         YYCODETYPE iFallback;            /* Fallback token */
140007 141187         if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
................................................................................
140707 141887     Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy314, 0);
140708 141888     sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy336);
140709 141889   }
140710 141890           break;
140711 141891         case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
140712 141892   {
140713 141893     Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
       141894  +  if( p ){
       141895  +    sqlite3ExprIdToTrueFalse(p);
       141896  +    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
       141897  +  }
140714 141898     sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
140715 141899   }
140716 141900           break;
140717 141901         case 35: /* ccons ::= NOT NULL onconf */
140718 141902   {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
140719 141903           break;
140720 141904         case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
................................................................................
143600 144784       if( sqlite3GlobalConfig.isPCacheInit==0 ){
143601 144785         rc = sqlite3PcacheInitialize();
143602 144786       }
143603 144787       if( rc==SQLITE_OK ){
143604 144788         sqlite3GlobalConfig.isPCacheInit = 1;
143605 144789         rc = sqlite3OsInit();
143606 144790       }
       144791  +#ifdef SQLITE_ENABLE_DESERIALIZE
       144792  +    if( rc==SQLITE_OK ){
       144793  +      rc = sqlite3MemdbInit();
       144794  +    }
       144795  +#endif
143607 144796       if( rc==SQLITE_OK ){
143608 144797         sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
143609 144798             sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
143610 144799         sqlite3GlobalConfig.isInit = 1;
143611 144800   #ifdef SQLITE_EXTRA_INIT
143612 144801         bRunExtraInit = 1;
143613 144802   #endif
................................................................................
143632 144821     ** been compiled correctly.  It is important to run this code, but
143633 144822     ** we don't want to run it too often and soak up CPU cycles for no
143634 144823     ** reason.  So we run it once during initialization.
143635 144824     */
143636 144825   #ifndef NDEBUG
143637 144826   #ifndef SQLITE_OMIT_FLOATING_POINT
143638 144827     /* This section of code's only "output" is via assert() statements. */
143639         -  if ( rc==SQLITE_OK ){
       144828  +  if( rc==SQLITE_OK ){
143640 144829       u64 x = (((u64)1)<<63)-1;
143641 144830       double y;
143642 144831       assert(sizeof(x)==8);
143643 144832       assert(sizeof(x)==sizeof(y));
143644 144833       memcpy(&y, &x, 8);
143645 144834       assert( sqlite3IsNaN(y) );
143646 144835     }
................................................................................
144799 145988   #else
144800 145989       /* SQLITE_NOLFS       */ 0,
144801 145990   #endif
144802 145991       /* SQLITE_AUTH        */ "authorization denied",
144803 145992       /* SQLITE_FORMAT      */ 0,
144804 145993       /* SQLITE_RANGE       */ "column index out of range",
144805 145994       /* SQLITE_NOTADB      */ "file is not a database",
       145995  +    /* SQLITE_NOTICE      */ "notification message",
       145996  +    /* SQLITE_WARNING     */ "warning message",
144806 145997     };
144807 145998     const char *zErr = "unknown error";
144808 145999     switch( rc ){
144809 146000       case SQLITE_ABORT_ROLLBACK: {
144810 146001         zErr = "abort due to ROLLBACK";
144811 146002         break;
       146003  +    }
       146004  +    case SQLITE_ROW: {
       146005  +      zErr = "another row available";
       146006  +      break;
       146007  +    }
       146008  +    case SQLITE_DONE: {
       146009  +      zErr = "no more rows available";
       146010  +      break;
144812 146011       }
144813 146012       default: {
144814 146013         rc &= 0xff;
144815 146014         if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
144816 146015           zErr = aMsg[rc];
144817 146016         }
144818 146017         break;
................................................................................
160792 161991     sqlite3_stmt *pStmt;
160793 161992     int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
160794 161993     if( rc==SQLITE_OK ){
160795 161994       sqlite3_bind_int64(pStmt, 1, iBlock);
160796 161995       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
160797 161996       sqlite3_step(pStmt);
160798 161997       rc = sqlite3_reset(pStmt);
       161998  +    sqlite3_bind_null(pStmt, 2);
160799 161999     }
160800 162000     return rc;
160801 162001   }
160802 162002   
160803 162003   /*
160804 162004   ** Find the largest relative level number in the table. If successful, set
160805 162005   ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
................................................................................
160848 162048         char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
160849 162049         if( !zEnd ) return SQLITE_NOMEM;
160850 162050         sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160851 162051       }
160852 162052       sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
160853 162053       sqlite3_step(pStmt);
160854 162054       rc = sqlite3_reset(pStmt);
       162055  +    sqlite3_bind_null(pStmt, 6);
160855 162056     }
160856 162057     return rc;
160857 162058   }
160858 162059   
160859 162060   /*
160860 162061   ** Return the size of the common prefix (if any) shared by zPrev and
160861 162062   ** zNext, in bytes. For example, 
................................................................................
162327 163528       *pRC = rc;
162328 163529       return;
162329 163530     }
162330 163531     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
162331 163532     sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
162332 163533     sqlite3_step(pStmt);
162333 163534     *pRC = sqlite3_reset(pStmt);
       163535  +  sqlite3_bind_null(pStmt, 2);
162334 163536     sqlite3_free(a);
162335 163537   }
162336 163538   
162337 163539   /*
162338 163540   ** Merge the entire database so that there is one segment for each 
162339 163541   ** iIndex/iLangid combination.
162340 163542   */
................................................................................
163515 164717       if( rc==SQLITE_OK ){
163516 164718         sqlite3_bind_int64(pChomp, 1, iNewStart);
163517 164719         sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
163518 164720         sqlite3_bind_int64(pChomp, 3, iAbsLevel);
163519 164721         sqlite3_bind_int(pChomp, 4, iIdx);
163520 164722         sqlite3_step(pChomp);
163521 164723         rc = sqlite3_reset(pChomp);
       164724  +      sqlite3_bind_null(pChomp, 2);
163522 164725       }
163523 164726     }
163524 164727   
163525 164728     sqlite3_free(root.a);
163526 164729     sqlite3_free(block.a);
163527 164730     return rc;
163528 164731   }
................................................................................
163594 164797   
163595 164798     rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
163596 164799     if( rc==SQLITE_OK ){
163597 164800       sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
163598 164801       sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163599 164802       sqlite3_step(pReplace);
163600 164803       rc = sqlite3_reset(pReplace);
       164804  +    sqlite3_bind_null(pReplace, 2);
163601 164805     }
163602 164806   
163603 164807     return rc;
163604 164808   }
163605 164809   
163606 164810   /*
163607 164811   ** Load an incr-merge hint from the database. The incr-merge hint, if one 
................................................................................
164408 165612     sqlite3_vtab *pVtab,            /* FTS3 vtab object */
164409 165613     int nArg,                       /* Size of argument array */
164410 165614     sqlite3_value **apVal,          /* Array of arguments */
164411 165615     sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
164412 165616   ){
164413 165617     Fts3Table *p = (Fts3Table *)pVtab;
164414 165618     int rc = SQLITE_OK;             /* Return Code */
164415         -  int isRemove = 0;               /* True for an UPDATE or DELETE */
164416 165619     u32 *aSzIns = 0;                /* Sizes of inserted documents */
164417 165620     u32 *aSzDel = 0;                /* Sizes of deleted documents */
164418 165621     int nChng = 0;                  /* Net change in number of documents */
164419 165622     int bInsertDone = 0;
164420 165623   
164421 165624     /* At this point it must be known if the %_stat table exists or not.
164422 165625     ** So bHasStat may not be 2.  */
................................................................................
164506 165709       goto update_out;
164507 165710     }
164508 165711   
164509 165712     /* If this is a DELETE or UPDATE operation, remove the old record. */
164510 165713     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
164511 165714       assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
164512 165715       rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
164513         -    isRemove = 1;
164514 165716     }
164515 165717     
164516 165718     /* If this is an INSERT or UPDATE operation, insert the new record. */
164517 165719     if( nArg>1 && rc==SQLITE_OK ){
164518 165720       int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
164519 165721       if( bInsertDone==0 ){
164520 165722         rc = fts3InsertData(p, apVal, pRowid);
164521 165723         if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164522 165724           rc = FTS_CORRUPT_VTAB;
164523 165725         }
164524 165726       }
164525         -    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
       165727  +    if( rc==SQLITE_OK ){
164526 165728         rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
164527 165729       }
164528 165730       if( rc==SQLITE_OK ){
164529 165731         assert( p->iPrevDocid==*pRowid );
164530 165732         rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
164531 165733       }
164532 165734       if( p->bHasDocsize ){
................................................................................
167826 169028       }else{
167827 169029         sqlite3_bind_null(p, 1);
167828 169030       }
167829 169031       sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167830 169032       sqlite3_step(p);
167831 169033       pNode->isDirty = 0;
167832 169034       rc = sqlite3_reset(p);
       169035  +    sqlite3_bind_null(p, 2);
167833 169036       if( pNode->iNode==0 && rc==SQLITE_OK ){
167834 169037         pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167835 169038         nodeHashInsert(pRtree, pNode);
167836 169039       }
167837 169040     }
167838 169041     return rc;
167839 169042   }
................................................................................
179821 181024     int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
179822 181025     sqlite3_session *pSession,      /* Session object pTab is attached to */
179823 181026     SessionTable *pTab              /* Table that change applies to */
179824 181027   ){
179825 181028     int iHash; 
179826 181029     int bNull = 0; 
179827 181030     int rc = SQLITE_OK;
179828         -  SessionStat1Ctx stat1;
       181031  +  SessionStat1Ctx stat1 = {0};
179829 181032   
179830 181033     if( pSession->rc ) return;
179831 181034   
179832 181035     /* Load table details if required */
179833 181036     if( sessionInitTable(pSession, pTab) ) return;
179834 181037   
179835 181038     /* Check the number of columns in this xPreUpdate call matches the 
................................................................................
181426 182629     int rc = SQLITE_OK;
181427 182630   
181428 182631     for(i=0; i<nCol && rc==SQLITE_OK; i++){
181429 182632       int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
181430 182633       if( abPK && abPK[i]==0 ) continue;
181431 182634       rc = sessionInputBuffer(pIn, 9);
181432 182635       if( rc==SQLITE_OK ){
181433         -      eType = pIn->aData[pIn->iNext++];
181434         -    }
181435         -
181436         -    assert( apOut[i]==0 );
181437         -    if( eType ){
181438         -      apOut[i] = sqlite3ValueNew(0);
181439         -      if( !apOut[i] ) rc = SQLITE_NOMEM;
       182636  +      if( pIn->iNext>=pIn->nData ){
       182637  +        rc = SQLITE_CORRUPT_BKPT;
       182638  +      }else{
       182639  +        eType = pIn->aData[pIn->iNext++];
       182640  +        assert( apOut[i]==0 );
       182641  +        if( eType ){
       182642  +          apOut[i] = sqlite3ValueNew(0);
       182643  +          if( !apOut[i] ) rc = SQLITE_NOMEM;
       182644  +        }
       182645  +      }
181440 182646       }
181441 182647   
181442 182648       if( rc==SQLITE_OK ){
181443 182649         u8 *aVal = &pIn->aData[pIn->iNext];
181444 182650         if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
181445 182651           int nByte;
181446 182652           pIn->iNext += sessionVarintGet(aVal, &nByte);
181447 182653           rc = sessionInputBuffer(pIn, nByte);
181448 182654           if( rc==SQLITE_OK ){
181449         -          u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
181450         -          rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       182655  +          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
       182656  +            rc = SQLITE_CORRUPT_BKPT;
       182657  +          }else{
       182658  +            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
       182659  +            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       182660  +            pIn->iNext += nByte;
       182661  +          }
181451 182662           }
181452         -        pIn->iNext += nByte;
181453 182663         }
181454 182664         if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
181455 182665           sqlite3_int64 v = sessionGetI64(aVal);
181456 182666           if( eType==SQLITE_INTEGER ){
181457 182667             sqlite3VdbeMemSetInt64(apOut[i], v);
181458 182668           }else{
181459 182669             double d;
................................................................................
181485 182695     int rc = SQLITE_OK;
181486 182696     int nCol = 0;
181487 182697     int nRead = 0;
181488 182698   
181489 182699     rc = sessionInputBuffer(pIn, 9);
181490 182700     if( rc==SQLITE_OK ){
181491 182701       nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181492         -    rc = sessionInputBuffer(pIn, nRead+nCol+100);
181493         -    nRead += nCol;
       182702  +    /* The hard upper limit for the number of columns in an SQLite
       182703  +    ** database table is, according to sqliteLimit.h, 32676. So 
       182704  +    ** consider any table-header that purports to have more than 65536 
       182705  +    ** columns to be corrupt. This is convenient because otherwise, 
       182706  +    ** if the (nCol>65536) condition below were omitted, a sufficiently 
       182707  +    ** large value for nCol may cause nRead to wrap around and become 
       182708  +    ** negative. Leading to a crash. */
       182709  +    if( nCol<0 || nCol>65536 ){
       182710  +      rc = SQLITE_CORRUPT_BKPT;
       182711  +    }else{
       182712  +      rc = sessionInputBuffer(pIn, nRead+nCol+100);
       182713  +      nRead += nCol;
       182714  +    }
181494 182715     }
181495 182716   
181496 182717     while( rc==SQLITE_OK ){
181497 182718       while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181498 182719         nRead++;
181499 182720       }
181500 182721       if( (pIn->iNext + nRead)<pIn->nData ) break;
................................................................................
181563 182784     assert( p->rc==SQLITE_OK );
181564 182785   
181565 182786     rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181566 182787     if( rc==SQLITE_OK ){
181567 182788       int nByte;
181568 182789       int nVarint;
181569 182790       nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181570         -    nCopy -= nVarint;
181571         -    p->in.iNext += nVarint;
181572         -    nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181573         -    p->tblhdr.nBuf = 0;
181574         -    sessionBufferGrow(&p->tblhdr, nByte, &rc);
       182791  +    if( p->nCol>0 ){
       182792  +      nCopy -= nVarint;
       182793  +      p->in.iNext += nVarint;
       182794  +      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
       182795  +      p->tblhdr.nBuf = 0;
       182796  +      sessionBufferGrow(&p->tblhdr, nByte, &rc);
       182797  +    }else{
       182798  +      rc = SQLITE_CORRUPT_BKPT;
       182799  +    }
181575 182800     }
181576 182801   
181577 182802     if( rc==SQLITE_OK ){
181578 182803       int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181579 182804       memset(p->tblhdr.aBuf, 0, iPK);
181580 182805       memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
181581 182806       p->in.iNext += nCopy;
................................................................................
181644 182869       p->bPatchset = (op=='P');
181645 182870       if( sessionChangesetReadTblhdr(p) ) return p->rc;
181646 182871       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181647 182872       p->in.iCurrent = p->in.iNext;
181648 182873       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181649 182874       op = p->in.aData[p->in.iNext++];
181650 182875     }
       182876  +
       182877  +  if( p->zTab==0 ){
       182878  +    /* The first record in the changeset is not a table header. Must be a
       182879  +    ** corrupt changeset. */
       182880  +    assert( p->in.iNext==1 );
       182881  +    return (p->rc = SQLITE_CORRUPT_BKPT);
       182882  +  }
181651 182883   
181652 182884     p->op = op;
181653 182885     p->bIndirect = p->in.aData[p->in.iNext++];
181654 182886     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181655 182887       return (p->rc = SQLITE_CORRUPT_BKPT);
181656 182888     }
181657 182889   
................................................................................
181687 182919       if( p->bPatchset && p->op==SQLITE_UPDATE ){
181688 182920         /* If this is an UPDATE that is part of a patchset, then all PK and
181689 182921         ** modified fields are present in the new.* record. The old.* record
181690 182922         ** is currently completely empty. This block shifts the PK fields from
181691 182923         ** new.* to old.*, to accommodate the code that reads these arrays.  */
181692 182924         for(i=0; i<p->nCol; i++){
181693 182925           assert( p->apValue[i]==0 );
181694         -        assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181695 182926           if( p->abPK[i] ){
181696 182927             p->apValue[i] = p->apValue[i+p->nCol];
       182928  +          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
181697 182929             p->apValue[i+p->nCol] = 0;
181698 182930           }
181699 182931         }
181700 182932       }
181701 182933     }
181702 182934   
181703 182935     return SQLITE_ROW;
................................................................................
182414 183646     ** in the code below. */
182415 183647     assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
182416 183648   
182417 183649     for(i=0; rc==SQLITE_OK && i<nCol; i++){
182418 183650       if( !abPK || abPK[i] ){
182419 183651         sqlite3_value *pVal;
182420 183652         (void)xValue(pIter, i, &pVal);
182421         -      rc = sessionBindValue(pStmt, i+1, pVal);
       183653  +      if( pVal==0 ){
       183654  +        /* The value in the changeset was "undefined". This indicates a
       183655  +        ** corrupt changeset blob.  */
       183656  +        rc = SQLITE_CORRUPT_BKPT;
       183657  +      }else{
       183658  +        rc = sessionBindValue(pStmt, i+1, pVal);
       183659  +      }
182422 183660       }
182423 183661     }
182424 183662     return rc;
182425 183663   }
182426 183664   
182427 183665   /*
182428 183666   ** SQL statement pSelect is as generated by the sessionSelectRow() function.
................................................................................
188051 189289     if( stateno>fts5YY_MAX_SHIFT ) return stateno;
188052 189290     assert( stateno <= fts5YY_SHIFT_COUNT );
188053 189291   #if defined(fts5YYCOVERAGE)
188054 189292     fts5yycoverage[stateno][iLookAhead] = 1;
188055 189293   #endif
188056 189294     do{
188057 189295       i = fts5yy_shift_ofst[stateno];
188058         -    assert( i>=0 && i+fts5YYNFTS5TOKEN<=sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
       189296  +    assert( i>=0 );
       189297  +    assert( i+fts5YYNFTS5TOKEN<=(int)sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
188059 189298       assert( iLookAhead!=fts5YYNOCODE );
188060 189299       assert( iLookAhead < fts5YYNFTS5TOKEN );
188061 189300       i += iLookAhead;
188062 189301       if( fts5yy_lookahead[i]!=iLookAhead ){
188063 189302   #ifdef fts5YYFALLBACK
188064 189303         fts5YYCODETYPE iFallback;            /* Fallback token */
188065 189304         if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
................................................................................
192493 193732       }
192494 193733   
192495 193734       if( sCtx.pPhrase==0 ){
192496 193735         /* This happens when parsing a token or quoted phrase that contains
192497 193736         ** no token characters at all. (e.g ... MATCH '""'). */
192498 193737         sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
192499 193738       }else if( sCtx.pPhrase->nTerm ){
192500         -      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
       193739  +      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
192501 193740       }
192502 193741       pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
192503 193742     }
192504 193743   
192505 193744     return sCtx.pPhrase;
192506 193745   }
192507 193746   
................................................................................
194956 196195       if( p->rc ) return;
194957 196196     }
194958 196197   
194959 196198     sqlite3_bind_int64(p->pWriter, 1, iRowid);
194960 196199     sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
194961 196200     sqlite3_step(p->pWriter);
194962 196201     p->rc = sqlite3_reset(p->pWriter);
       196202  +  sqlite3_bind_null(p->pWriter, 2);
194963 196203   }
194964 196204   
194965 196205   /*
194966 196206   ** Execute the following SQL:
194967 196207   **
194968 196208   **     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
194969 196209   */
................................................................................
196584 197824     sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
196585 197825     if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
196586 197826       i64 val = sqlite3_column_int(pIdxSelect, 0);
196587 197827       iPg = (int)(val>>1);
196588 197828       bDlidx = (val & 0x0001);
196589 197829     }
196590 197830     p->rc = sqlite3_reset(pIdxSelect);
       197831  +  sqlite3_bind_null(pIdxSelect, 2);
196591 197832   
196592 197833     if( iPg<pSeg->pgnoFirst ){
196593 197834       iPg = pSeg->pgnoFirst;
196594 197835       bDlidx = 0;
196595 197836     }
196596 197837   
196597 197838     pIter->iLeafPgno = iPg - 1;
................................................................................
197796 199037           sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
197797 199038           if( p->rc==SQLITE_OK ){
197798 199039             u8 aBlob[2] = {0xff, 0xff};
197799 199040             sqlite3_bind_int(pIdxSelect, 1, iSegid);
197800 199041             sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
197801 199042             assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
197802 199043             p->rc = sqlite3_reset(pIdxSelect);
       199044  +          sqlite3_bind_null(pIdxSelect, 2);
197803 199045           }
197804 199046         }
197805 199047   #endif
197806 199048       }
197807 199049     }
197808 199050   
197809 199051     return iSegid;
................................................................................
197922 199164       const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
197923 199165       /* The following was already done in fts5WriteInit(): */
197924 199166       /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
197925 199167       sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
197926 199168       sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
197927 199169       sqlite3_step(p->pIdxWriter);
197928 199170       p->rc = sqlite3_reset(p->pIdxWriter);
       199171  +    sqlite3_bind_null(p->pIdxWriter, 2);
197929 199172     }
197930 199173     pWriter->iBtPage = 0;
197931 199174   }
197932 199175   
197933 199176   /*
197934 199177   ** This is called once for each leaf page except the first that contains
197935 199178   ** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
................................................................................
203329 204572   static void fts5SourceIdFunc(
203330 204573     sqlite3_context *pCtx,          /* Function call context */
203331 204574     int nArg,                       /* Number of args */
203332 204575     sqlite3_value **apUnused        /* Function arguments */
203333 204576   ){
203334 204577     assert( nArg==0 );
203335 204578     UNUSED_PARAM2(nArg, apUnused);
203336         -  sqlite3_result_text(pCtx, "fts5: 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d", -1, SQLITE_TRANSIENT);
       204579  +  sqlite3_result_text(pCtx, "fts5: 2018-03-16 20:23:01 d75e67654aa9620b9617786553a002f54e8c6dcbbcc58948a06bd98a0916d75a", -1, SQLITE_TRANSIENT);
203337 204580   }
203338 204581   
203339 204582   static int fts5Init(sqlite3 *db){
203340 204583     static const sqlite3_module fts5Mod = {
203341 204584       /* iVersion      */ 2,
203342 204585       /* xCreate       */ fts5CreateMethod,
203343 204586       /* xConnect      */ fts5ConnectMethod,
................................................................................
203905 205148       sqlite3_stmt *pReplace = 0;
203906 205149       rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
203907 205150       if( rc==SQLITE_OK ){
203908 205151         sqlite3_bind_int64(pReplace, 1, iRowid);
203909 205152         sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
203910 205153         sqlite3_step(pReplace);
203911 205154         rc = sqlite3_reset(pReplace);
       205155  +      sqlite3_bind_null(pReplace, 2);
203912 205156       }
203913 205157     }
203914 205158     return rc;
203915 205159   }
203916 205160   
203917 205161   /*
203918 205162   ** Load the contents of the "averages" record from disk into the 
................................................................................
204565 205809       if( pVal ){
204566 205810         sqlite3_bind_value(pReplace, 2, pVal);
204567 205811       }else{
204568 205812         sqlite3_bind_int(pReplace, 2, iVal);
204569 205813       }
204570 205814       sqlite3_step(pReplace);
204571 205815       rc = sqlite3_reset(pReplace);
       205816  +    sqlite3_bind_null(pReplace, 1);
204572 205817     }
204573 205818     if( rc==SQLITE_OK && pVal ){
204574 205819       int iNew = p->pConfig->iCookie + 1;
204575 205820       rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
204576 205821       if( rc==SQLITE_OK ){
204577 205822         p->pConfig->iCookie = iNew;
204578 205823       }
................................................................................
207597 208842   #endif
207598 208843     return rc;
207599 208844   }
207600 208845   #endif /* SQLITE_CORE */
207601 208846   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207602 208847   
207603 208848   /************** End of stmt.c ************************************************/
207604         -#if __LINE__!=207604
       208849  +#if __LINE__!=208849
207605 208850   #undef SQLITE_SOURCE_ID
207606         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt2"
       208851  +#define SQLITE_SOURCE_ID      "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec47703alt2"
207607 208852   #endif
207608 208853   /* Return the source-id for this library */
207609 208854   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207610 208855   /************************** End of sqlite3.c ******************************/

Changes to src/sqlite3.h.

   119    119   ** been edited in any way since it was last checked in, then the last
   120    120   ** four hexadecimal digits of the hash may be modified.
   121    121   **
   122    122   ** See also: [sqlite3_libversion()],
   123    123   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   124    124   ** [sqlite_version()] and [sqlite_source_id()].
   125    125   */
   126         -#define SQLITE_VERSION        "3.22.0"
   127         -#define SQLITE_VERSION_NUMBER 3022000
   128         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
          126  +#define SQLITE_VERSION        "3.23.0"
          127  +#define SQLITE_VERSION_NUMBER 3023000
          128  +#define SQLITE_SOURCE_ID      "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
   129    129   
   130    130   /*
   131    131   ** CAPI3REF: Run-Time Library Version Numbers
   132    132   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   133    133   **
   134    134   ** These interfaces provide the same information as the [SQLITE_VERSION],
   135    135   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2492   2492   SQLITE_API void sqlite3_free_table(char **result);
  2493   2493   
  2494   2494   /*
  2495   2495   ** CAPI3REF: Formatted String Printing Functions
  2496   2496   **
  2497   2497   ** These routines are work-alikes of the "printf()" family of functions
  2498   2498   ** from the standard C library.
  2499         -** These routines understand most of the common K&R formatting options,
  2500         -** plus some additional non-standard formats, detailed below.
  2501         -** Note that some of the more obscure formatting options from recent
  2502         -** C-library standards are omitted from this implementation.
         2499  +** These routines understand most of the common formatting options from
         2500  +** the standard library printf() 
         2501  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         2502  +** See the [built-in printf()] documentation for details.
  2503   2503   **
  2504   2504   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2505         -** results into memory obtained from [sqlite3_malloc()].
         2505  +** results into memory obtained from [sqlite3_malloc64()].
  2506   2506   ** The strings returned by these two routines should be
  2507   2507   ** released by [sqlite3_free()].  ^Both routines return a
  2508         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         2508  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  2509   2509   ** memory to hold the resulting string.
  2510   2510   **
  2511   2511   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2512   2512   ** the standard C library.  The result is written into the
  2513   2513   ** buffer supplied as the second parameter whose size is given by
  2514   2514   ** the first parameter. Note that the order of the
  2515   2515   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  2525   2525   ** guarantees that the buffer is always zero-terminated.  ^The first
  2526   2526   ** parameter "n" is the total size of the buffer, including space for
  2527   2527   ** the zero terminator.  So the longest string that can be completely
  2528   2528   ** written will be n-1 characters.
  2529   2529   **
  2530   2530   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2531   2531   **
  2532         -** These routines all implement some additional formatting
  2533         -** options that are useful for constructing SQL statements.
  2534         -** All of the usual printf() formatting options apply.  In addition, there
  2535         -** is are "%q", "%Q", "%w" and "%z" options.
  2536         -**
  2537         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  2538         -** string from the argument list.  But %q also doubles every '\'' character.
  2539         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  2540         -** character it escapes that character and allows it to be inserted into
  2541         -** the string.
  2542         -**
  2543         -** For example, assume the string variable zText contains text as follows:
  2544         -**
  2545         -** <blockquote><pre>
  2546         -**  char *zText = "It's a happy day!";
  2547         -** </pre></blockquote>
  2548         -**
  2549         -** One can use this text in an SQL statement as follows:
  2550         -**
  2551         -** <blockquote><pre>
  2552         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2553         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2554         -**  sqlite3_free(zSQL);
  2555         -** </pre></blockquote>
  2556         -**
  2557         -** Because the %q format string is used, the '\'' character in zText
  2558         -** is escaped and the SQL generated is as follows:
  2559         -**
  2560         -** <blockquote><pre>
  2561         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  2562         -** </pre></blockquote>
  2563         -**
  2564         -** This is correct.  Had we used %s instead of %q, the generated SQL
  2565         -** would have looked like this:
  2566         -**
  2567         -** <blockquote><pre>
  2568         -**  INSERT INTO table1 VALUES('It's a happy day!');
  2569         -** </pre></blockquote>
  2570         -**
  2571         -** This second example is an SQL syntax error.  As a general rule you should
  2572         -** always use %q instead of %s when inserting text into a string literal.
  2573         -**
  2574         -** ^(The %Q option works like %q except it also adds single quotes around
  2575         -** the outside of the total string.  Additionally, if the parameter in the
  2576         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2577         -** single quotes).)^  So, for example, one could say:
  2578         -**
  2579         -** <blockquote><pre>
  2580         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2581         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2582         -**  sqlite3_free(zSQL);
  2583         -** </pre></blockquote>
  2584         -**
  2585         -** The code above will render a correct SQL statement in the zSQL
  2586         -** variable even if the zText variable is a NULL pointer.
  2587         -**
  2588         -** ^(The "%w" formatting option is like "%q" except that it expects to
  2589         -** be contained within double-quotes instead of single quotes, and it
  2590         -** escapes the double-quote character instead of the single-quote
  2591         -** character.)^  The "%w" formatting option is intended for safely inserting
  2592         -** table and column names into a constructed SQL statement.
  2593         -**
  2594         -** ^(The "%z" formatting option works like "%s" but with the
  2595         -** addition that after the string has been read and copied into
  2596         -** the result, [sqlite3_free()] is called on the input string.)^
         2532  +** See also:  [built-in printf()], [printf() SQL function]
  2597   2533   */
  2598   2534   SQLITE_API char *sqlite3_mprintf(const char*,...);
  2599   2535   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2600   2536   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2601   2537   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2602   2538   
  2603   2539   /*
................................................................................
  3655   3591   ** a schema change, on the first  [sqlite3_step()] call following any change
  3656   3592   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3657   3593   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3658   3594   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3659   3595   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3660   3596   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3661   3597   ** </li>
         3598  +** </ol>
  3662   3599   **
  3663   3600   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3664   3601   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3665   3602   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3666   3603   ** sqlite3_prepare_v2() interface works exactly the same as
  3667   3604   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  3668         -** </ol>
  3669   3605   */
  3670   3606   SQLITE_API int sqlite3_prepare(
  3671   3607     sqlite3 *db,            /* Database handle */
  3672   3608     const char *zSql,       /* SQL statement, UTF-8 encoded */
  3673   3609     int nByte,              /* Maximum length of zSql in bytes. */
  3674   3610     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3675   3611     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  7289   7225   ** wal file in wal mode databases, or the number of pages written to the
  7290   7226   ** database file in rollback mode databases. Any pages written as part of
  7291   7227   ** transaction rollback or database recovery operations are not included.
  7292   7228   ** If an IO or other error occurs while writing a page to disk, the effect
  7293   7229   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  7294   7230   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  7295   7231   ** </dd>
         7232  +**
         7233  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         7234  +** <dd>This parameter returns the number of dirty cache entries that have
         7235  +** been written to disk in the middle of a transaction due to the page
         7236  +** cache overflowing. Transactions are more efficient if they are written
         7237  +** to disk all at once. When pages spill mid-transaction, that introduces
         7238  +** additional overhead. This parameter can be used help identify
         7239  +** inefficiencies that can be resolve by increasing the cache size.
         7240  +** </dd>
  7296   7241   **
  7297   7242   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  7298   7243   ** <dd>This parameter returns zero for the current value if and only if
  7299   7244   ** all foreign key constraints (deferred or immediate) have been
  7300   7245   ** resolved.)^  ^The highwater mark is always 0.
  7301   7246   ** </dd>
  7302   7247   ** </dl>
................................................................................
  7309   7254   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7310   7255   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7311   7256   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7312   7257   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7313   7258   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7314   7259   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7315   7260   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  7316         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         7261  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         7262  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  7317   7263   
  7318   7264   
  7319   7265   /*
  7320   7266   ** CAPI3REF: Prepared Statement Status
  7321   7267   ** METHOD: sqlite3_stmt
  7322   7268   **
  7323   7269   ** ^(Each prepared statement maintains various
................................................................................
  8789   8735   ** transaction open on the database, or if the database is not a wal mode
  8790   8736   ** database.
  8791   8737   **
  8792   8738   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  8793   8739   */
  8794   8740   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  8795   8741   
         8742  +/*
         8743  +** CAPI3REF: Serialize a database
         8744  +**
         8745  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         8746  +** that is a serialization of the S database on [database connection] D.
         8747  +** If P is not a NULL pointer, then the size of the database in bytes
         8748  +** is written into *P.
         8749  +**
         8750  +** For an ordinary on-disk database file, the serialization is just a
         8751  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         8752  +** the serialization is the same sequence of bytes which would be written
         8753  +** to disk if that database where backed up to disk.
         8754  +**
         8755  +** The usual case is that sqlite3_serialize() copies the serialization of
         8756  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         8757  +** a pointer to that memory.  The caller is responsible for freeing the
         8758  +** returned value to avoid a memory leak.  However, if the F argument
         8759  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         8760  +** are made, and the sqlite3_serialize() function will return a pointer
         8761  +** to the contiguous memory representation of the database that SQLite
         8762  +** is currently using for that database, or NULL if the no such contiguous
         8763  +** memory representation of the database exists.  A contiguous memory
         8764  +** representation of the database will usually only exist if there has
         8765  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         8766  +** values of D and S.
         8767  +** The size of the database is written into *P even if the 
         8768  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         8769  +** of the database exists.
         8770  +**
         8771  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         8772  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         8773  +** allocation error occurs.
         8774  +**
         8775  +** This interface is only available if SQLite is compiled with the
         8776  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8777  +*/
         8778  +SQLITE_API unsigned char *sqlite3_serialize(
         8779  +  sqlite3 *db,           /* The database connection */
         8780  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         8781  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         8782  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         8783  +);
         8784  +
         8785  +/*
         8786  +** CAPI3REF: Flags for sqlite3_serialize
         8787  +**
         8788  +** Zero or more of the following constants can be OR-ed together for
         8789  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         8790  +**
         8791  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         8792  +** a pointer to contiguous in-memory database that it is currently using,
         8793  +** without making a copy of the database.  If SQLite is not currently using
         8794  +** a contiguous in-memory database, then this option causes
         8795  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         8796  +** using a contiguous in-memory database if it has been initialized by a
         8797  +** prior call to [sqlite3_deserialize()].
         8798  +*/
         8799  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         8800  +
         8801  +/*
         8802  +** CAPI3REF: Deserialize a database
         8803  +**
         8804  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         8805  +** [database connection] D to disconnection from database S and then
         8806  +** reopen S as an in-memory database based on the serialization contained
         8807  +** in P.  The serialized database P is N bytes in size.  M is the size of
         8808  +** the buffer P, which might be larger than N.  If M is larger than N, and
         8809  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         8810  +** permitted to add content to the in-memory database as long as the total
         8811  +** size does not exceed M bytes.
         8812  +**
         8813  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         8814  +** invoke sqlite3_free() on the serialization buffer when the database
         8815  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         8816  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         8817  +** if writes on the database cause it to grow larger than M bytes.
         8818  +**
         8819  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         8820  +** database is currently in a read transaction or is involved in a backup
         8821  +** operation.
         8822  +**
         8823  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         8824  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         8825  +** [sqlite3_free()] is invoked on argument P prior to returning.
         8826  +**
         8827  +** This interface is only available if SQLite is compiled with the
         8828  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8829  +*/
         8830  +SQLITE_API int sqlite3_deserialize(
         8831  +  sqlite3 *db,            /* The database connection */
         8832  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         8833  +  unsigned char *pData,   /* The serialized database content */
         8834  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         8835  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         8836  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         8837  +);
         8838  +
         8839  +/*
         8840  +** CAPI3REF: Flags for sqlite3_deserialize()
         8841  +**
         8842  +** The following are allowed values for 6th argument (the F argument) to
         8843  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         8844  +**
         8845  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         8846  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         8847  +** and that SQLite should take ownership of this memory and automatically
         8848  +** free it when it has finished using it.  Without this flag, the caller
         8849  +** is resposible for freeing any dynamically allocated memory.
         8850  +**
         8851  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         8852  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
         8853  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         8854  +** Without this flag, the deserialized database cannot increase in size beyond
         8855  +** the number of bytes specified by the M parameter.
         8856  +**
         8857  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         8858  +** should be treated as read-only.
         8859  +*/
         8860  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         8861  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         8862  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         8863  +
  8796   8864   /*
  8797   8865   ** Undo the hack that converts floating point types to integer for
  8798   8866   ** builds on processors without floating point support.
  8799   8867   */
  8800   8868   #ifdef SQLITE_OMIT_FLOATING_POINT
  8801   8869   # undef double
  8802   8870   #endif
................................................................................
  8936   9004   #ifdef __cplusplus
  8937   9005   extern "C" {
  8938   9006   #endif
  8939   9007   
  8940   9008   
  8941   9009   /*
  8942   9010   ** CAPI3REF: Session Object Handle
         9011  +**
         9012  +** An instance of this object is a [session] that can be used to
         9013  +** record changes to a database.
  8943   9014   */
  8944   9015   typedef struct sqlite3_session sqlite3_session;
  8945   9016   
  8946   9017   /*
  8947   9018   ** CAPI3REF: Changeset Iterator Handle
         9019  +**
         9020  +** An instance of this object acts as a cursor for iterating
         9021  +** over the elements of a [changeset] or [patchset].
  8948   9022   */
  8949   9023   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  8950   9024   
  8951   9025   /*
  8952   9026   ** CAPI3REF: Create A New Session Object
         9027  +** CONSTRUCTOR: sqlite3_session
  8953   9028   **
  8954   9029   ** Create a new session object attached to database handle db. If successful,
  8955   9030   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  8956   9031   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  8957   9032   ** error code (e.g. SQLITE_NOMEM) is returned.
  8958   9033   **
  8959   9034   ** It is possible to create multiple session objects attached to a single
................................................................................
  8982   9057     sqlite3 *db,                    /* Database handle */
  8983   9058     const char *zDb,                /* Name of db (e.g. "main") */
  8984   9059     sqlite3_session **ppSession     /* OUT: New session object */
  8985   9060   );
  8986   9061   
  8987   9062   /*
  8988   9063   ** CAPI3REF: Delete A Session Object
         9064  +** DESTRUCTOR: sqlite3_session
  8989   9065   **
  8990   9066   ** Delete a session object previously allocated using 
  8991   9067   ** [sqlite3session_create()]. Once a session object has been deleted, the
  8992   9068   ** results of attempting to use pSession with any other session module
  8993   9069   ** function are undefined.
  8994   9070   **
  8995   9071   ** Session objects must be deleted before the database handle to which they
................................................................................
  8997   9073   ** [sqlite3session_create()] for details.
  8998   9074   */
  8999   9075   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  9000   9076   
  9001   9077   
  9002   9078   /*
  9003   9079   ** CAPI3REF: Enable Or Disable A Session Object
         9080  +** METHOD: sqlite3_session
  9004   9081   **
  9005   9082   ** Enable or disable the recording of changes by a session object. When
  9006   9083   ** enabled, a session object records changes made to the database. When
  9007   9084   ** disabled - it does not. A newly created session object is enabled.
  9008   9085   ** Refer to the documentation for [sqlite3session_changeset()] for further
  9009   9086   ** details regarding how enabling and disabling a session object affects
  9010   9087   ** the eventual changesets.
................................................................................
  9016   9093   ** The return value indicates the final state of the session object: 0 if 
  9017   9094   ** the session is disabled, or 1 if it is enabled.
  9018   9095   */
  9019   9096   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  9020   9097   
  9021   9098   /*
  9022   9099   ** CAPI3REF: Set Or Clear the Indirect Change Flag
         9100  +** METHOD: sqlite3_session
  9023   9101   **
  9024   9102   ** Each change recorded by a session object is marked as either direct or
  9025   9103   ** indirect. A change is marked as indirect if either:
  9026   9104   **
  9027   9105   ** <ul>
  9028   9106   **   <li> The session object "indirect" flag is set when the change is
  9029   9107   **        made, or
................................................................................
  9045   9123   ** The return value indicates the final state of the indirect flag: 0 if 
  9046   9124   ** it is clear, or 1 if it is set.
  9047   9125   */
  9048   9126   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  9049   9127   
  9050   9128   /*
  9051   9129   ** CAPI3REF: Attach A Table To A Session Object
         9130  +** METHOD: sqlite3_session
  9052   9131   **
  9053   9132   ** If argument zTab is not NULL, then it is the name of a table to attach
  9054   9133   ** to the session object passed as the first argument. All subsequent changes 
  9055   9134   ** made to the table while the session object is enabled will be recorded. See 
  9056   9135   ** documentation for [sqlite3session_changeset()] for further details.
  9057   9136   **
  9058   9137   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
  9107   9186   SQLITE_API int sqlite3session_attach(
  9108   9187     sqlite3_session *pSession,      /* Session object */
  9109   9188     const char *zTab                /* Table name */
  9110   9189   );
  9111   9190   
  9112   9191   /*
  9113   9192   ** CAPI3REF: Set a table filter on a Session Object.
         9193  +** METHOD: sqlite3_session
  9114   9194   **
  9115   9195   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  9116   9196   ** in tables that are not attached to the Session object, the filter is called
  9117   9197   ** to determine whether changes to the table's rows should be tracked or not. 
  9118   9198   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  9119   9199   ** attached, xFilter will not be called again.
  9120   9200   */
................................................................................
  9125   9205       const char *zTab              /* Table name */
  9126   9206     ),
  9127   9207     void *pCtx                      /* First argument passed to xFilter */
  9128   9208   );
  9129   9209   
  9130   9210   /*
  9131   9211   ** CAPI3REF: Generate A Changeset From A Session Object
         9212  +** METHOD: sqlite3_session
  9132   9213   **
  9133   9214   ** Obtain a changeset containing changes to the tables attached to the 
  9134   9215   ** session object passed as the first argument. If successful, 
  9135   9216   ** set *ppChangeset to point to a buffer containing the changeset 
  9136   9217   ** and *pnChangeset to the size of the changeset in bytes before returning
  9137   9218   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
  9138   9219   ** zero and return an SQLite error code.
................................................................................
  9234   9315   SQLITE_API int sqlite3session_changeset(
  9235   9316     sqlite3_session *pSession,      /* Session object */
  9236   9317     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  9237   9318     void **ppChangeset              /* OUT: Buffer containing changeset */
  9238   9319   );
  9239   9320   
  9240   9321   /*
  9241         -** CAPI3REF: Load The Difference Between Tables Into A Session 
         9322  +** CAPI3REF: Load The Difference Between Tables Into A Session
         9323  +** METHOD: sqlite3_session
  9242   9324   **
  9243   9325   ** If it is not already attached to the session object passed as the first
  9244   9326   ** argument, this function attaches table zTbl in the same manner as the
  9245   9327   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
  9246   9328   ** does not have a primary key, this function is a no-op (but does not return
  9247   9329   ** an error).
  9248   9330   **
................................................................................
  9299   9381     const char *zTbl,
  9300   9382     char **pzErrMsg
  9301   9383   );
  9302   9384   
  9303   9385   
  9304   9386   /*
  9305   9387   ** CAPI3REF: Generate A Patchset From A Session Object
         9388  +** METHOD: sqlite3_session
  9306   9389   **
  9307   9390   ** The differences between a patchset and a changeset are that:
  9308   9391   **
  9309   9392   ** <ul>
  9310   9393   **   <li> DELETE records consist of the primary key fields only. The 
  9311   9394   **        original values of other fields are omitted.
  9312   9395   **   <li> The original values of any modified fields are omitted from 
................................................................................
  9350   9433   ** guaranteed that a call to sqlite3session_changeset() will return a 
  9351   9434   ** changeset containing zero changes.
  9352   9435   */
  9353   9436   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  9354   9437   
  9355   9438   /*
  9356   9439   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
         9440  +** CONSTRUCTOR: sqlite3_changeset_iter
  9357   9441   **
  9358   9442   ** Create an iterator used to iterate through the contents of a changeset.
  9359   9443   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  9360   9444   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
  9361   9445   ** SQLite error code is returned.
  9362   9446   **
  9363   9447   ** The following functions can be used to advance and query a changeset 
................................................................................
  9390   9474     int nChangeset,                 /* Size of changeset blob in bytes */
  9391   9475     void *pChangeset                /* Pointer to blob containing changeset */
  9392   9476   );
  9393   9477   
  9394   9478   
  9395   9479   /*
  9396   9480   ** CAPI3REF: Advance A Changeset Iterator
         9481  +** METHOD: sqlite3_changeset_iter
  9397   9482   **
  9398   9483   ** This function may only be used with iterators created by function
  9399   9484   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
  9400   9485   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
  9401   9486   ** is returned and the call has no effect.
  9402   9487   **
  9403   9488   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
  9414   9499   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9415   9500   ** SQLITE_NOMEM.
  9416   9501   */
  9417   9502   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9418   9503   
  9419   9504   /*
  9420   9505   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
         9506  +** METHOD: sqlite3_changeset_iter
  9421   9507   **
  9422   9508   ** The pIter argument passed to this function may either be an iterator
  9423   9509   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9424   9510   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9425   9511   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
  9426   9512   ** is not the case, this function returns [SQLITE_MISUSE].
  9427   9513   **
................................................................................
  9448   9534     int *pnCol,                     /* OUT: Number of columns in table */
  9449   9535     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9450   9536     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9451   9537   );
  9452   9538   
  9453   9539   /*
  9454   9540   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
         9541  +** METHOD: sqlite3_changeset_iter
  9455   9542   **
  9456   9543   ** For each modified table, a changeset includes the following:
  9457   9544   **
  9458   9545   ** <ul>
  9459   9546   **   <li> The number of columns in the table, and
  9460   9547   **   <li> Which of those columns make up the tables PRIMARY KEY.
  9461   9548   ** </ul>
................................................................................
  9479   9566     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9480   9567     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9481   9568     int *pnCol                      /* OUT: Number of entries in output array */
  9482   9569   );
  9483   9570   
  9484   9571   /*
  9485   9572   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
         9573  +** METHOD: sqlite3_changeset_iter
  9486   9574   **
  9487   9575   ** The pIter argument passed to this function may either be an iterator
  9488   9576   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9489   9577   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9490   9578   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9491   9579   ** Furthermore, it may only be called if the type of change that the iterator
  9492   9580   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
  9509   9597     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9510   9598     int iVal,                       /* Column number */
  9511   9599     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9512   9600   );
  9513   9601   
  9514   9602   /*
  9515   9603   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
         9604  +** METHOD: sqlite3_changeset_iter
  9516   9605   **
  9517   9606   ** The pIter argument passed to this function may either be an iterator
  9518   9607   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9519   9608   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9520   9609   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9521   9610   ** Furthermore, it may only be called if the type of change that the iterator
  9522   9611   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
  9542   9631     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9543   9632     int iVal,                       /* Column number */
  9544   9633     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9545   9634   );
  9546   9635   
  9547   9636   /*
  9548   9637   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
         9638  +** METHOD: sqlite3_changeset_iter
  9549   9639   **
  9550   9640   ** This function should only be used with iterator objects passed to a
  9551   9641   ** conflict-handler callback by [sqlite3changeset_apply()] with either
  9552   9642   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  9553   9643   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  9554   9644   ** is set to NULL.
  9555   9645   **
................................................................................
  9569   9659     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9570   9660     int iVal,                       /* Column number */
  9571   9661     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9572   9662   );
  9573   9663   
  9574   9664   /*
  9575   9665   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
         9666  +** METHOD: sqlite3_changeset_iter
  9576   9667   **
  9577   9668   ** This function may only be called with an iterator passed to an
  9578   9669   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9579   9670   ** it sets the output variable to the total number of known foreign key
  9580   9671   ** violations in the destination database and returns SQLITE_OK.
  9581   9672   **
  9582   9673   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
  9585   9676     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9586   9677     int *pnOut                      /* OUT: Number of FK violations */
  9587   9678   );
  9588   9679   
  9589   9680   
  9590   9681   /*
  9591   9682   ** CAPI3REF: Finalize A Changeset Iterator
         9683  +** METHOD: sqlite3_changeset_iter
  9592   9684   **
  9593   9685   ** This function is used to finalize an iterator allocated with
  9594   9686   ** [sqlite3changeset_start()].
  9595   9687   **
  9596   9688   ** This function should only be called on iterators created using the
  9597   9689   ** [sqlite3changeset_start()] function. If an application calls this
  9598   9690   ** function with an iterator passed to a conflict-handler by
................................................................................
  9601   9693   **
  9602   9694   ** If an error was encountered within a call to an sqlite3changeset_xxx()
  9603   9695   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
  9604   9696   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  9605   9697   ** to that error is returned by this function. Otherwise, SQLITE_OK is
  9606   9698   ** returned. This is to allow the following pattern (pseudo-code):
  9607   9699   **
         9700  +** <pre>
  9608   9701   **   sqlite3changeset_start();
  9609   9702   **   while( SQLITE_ROW==sqlite3changeset_next() ){
  9610   9703   **     // Do something with change.
  9611   9704   **   }
  9612   9705   **   rc = sqlite3changeset_finalize();
  9613   9706   **   if( rc!=SQLITE_OK ){
  9614   9707   **     // An error has occurred 
  9615   9708   **   }
         9709  +** </pre>
  9616   9710   */
  9617   9711   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9618   9712   
  9619   9713   /*
  9620   9714   ** CAPI3REF: Invert A Changeset
  9621   9715   **
  9622   9716   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
  9656   9750   ** single changeset. The result is a changeset equivalent to applying
  9657   9751   ** changeset A followed by changeset B. 
  9658   9752   **
  9659   9753   ** This function combines the two input changesets using an 
  9660   9754   ** sqlite3_changegroup object. Calling it produces similar results as the
  9661   9755   ** following code fragment:
  9662   9756   **
         9757  +** <pre>
  9663   9758   **   sqlite3_changegroup *pGrp;
  9664   9759   **   rc = sqlite3_changegroup_new(&pGrp);
  9665   9760   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  9666   9761   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  9667   9762   **   if( rc==SQLITE_OK ){
  9668   9763   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  9669   9764   **   }else{
  9670   9765   **     *ppOut = 0;
  9671   9766   **     *pnOut = 0;
  9672   9767   **   }
         9768  +** </pre>
  9673   9769   **
  9674   9770   ** Refer to the sqlite3_changegroup documentation below for details.
  9675   9771   */
  9676   9772   SQLITE_API int sqlite3changeset_concat(
  9677   9773     int nA,                         /* Number of bytes in buffer pA */
  9678   9774     void *pA,                       /* Pointer to buffer containing changeset A */
  9679   9775     int nB,                         /* Number of bytes in buffer pB */
................................................................................
  9681   9777     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9682   9778     void **ppOut                    /* OUT: Buffer containing output changeset */
  9683   9779   );
  9684   9780   
  9685   9781   
  9686   9782   /*
  9687   9783   ** CAPI3REF: Changegroup Handle
         9784  +**
         9785  +** A changegroup is an object used to combine two or more 
         9786  +** [changesets] or [patchsets]
  9688   9787   */
  9689   9788   typedef struct sqlite3_changegroup sqlite3_changegroup;
  9690   9789   
  9691   9790   /*
  9692   9791   ** CAPI3REF: Create A New Changegroup Object
         9792  +** CONSTRUCTOR: sqlite3_changegroup
  9693   9793   **
  9694   9794   ** An sqlite3_changegroup object is used to combine two or more changesets
  9695   9795   ** (or patchsets) into a single changeset (or patchset). A single changegroup
  9696   9796   ** object may combine changesets or patchsets, but not both. The output is
  9697   9797   ** always in the same format as the input.
  9698   9798   **
  9699   9799   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
  9723   9823   ** sqlite3changegroup_output() functions, also available are the streaming
  9724   9824   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  9725   9825   */
  9726   9826   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
  9727   9827   
  9728   9828   /*
  9729   9829   ** CAPI3REF: Add A Changeset To A Changegroup
         9830  +** METHOD: sqlite3_changegroup
  9730   9831   **
  9731   9832   ** Add all changes within the changeset (or patchset) in buffer pData (size
  9732   9833   ** nData bytes) to the changegroup. 
  9733   9834   **
  9734   9835   ** If the buffer contains a patchset, then all prior calls to this function
  9735   9836   ** on the same changegroup object must also have specified patchsets. Or, if
  9736   9837   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
  9800   9901   **
  9801   9902   ** If no error occurs, SQLITE_OK is returned.
  9802   9903   */
  9803   9904   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  9804   9905   
  9805   9906   /*
  9806   9907   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
         9908  +** METHOD: sqlite3_changegroup
  9807   9909   **
  9808   9910   ** Obtain a buffer containing a changeset (or patchset) representing the
  9809   9911   ** current contents of the changegroup. If the inputs to the changegroup
  9810   9912   ** were themselves changesets, the output is a changeset. Or, if the
  9811   9913   ** inputs were patchsets, the output is also a patchset.
  9812   9914   **
  9813   9915   ** As with the output of the sqlite3session_changeset() and
................................................................................
  9830   9932     sqlite3_changegroup*,
  9831   9933     int *pnData,                    /* OUT: Size of output buffer in bytes */
  9832   9934     void **ppData                   /* OUT: Pointer to output buffer */
  9833   9935   );
  9834   9936   
  9835   9937   /*
  9836   9938   ** CAPI3REF: Delete A Changegroup Object
         9939  +** DESTRUCTOR: sqlite3_changegroup
  9837   9940   */
  9838   9941   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
  9839   9942   
  9840   9943   /*
  9841   9944   ** CAPI3REF: Apply A Changeset To A Database
  9842   9945   **
  9843   9946   ** Apply a changeset to a database. This function attempts to update the

Changes to win/Makefile.PellesCGMake.

    87     87   SQLITEOBJ=$(foreach sf,$(SQLITESRC),$(sf:.c=.obj))
    88     88   SQLITEDEFINES=-DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_WIN32_NO_ANSI
    89     89   
    90     90   # define the SQLite shell files, which need special flags on compile
    91     91   SQLITESHELLSRC=shell.c
    92     92   ORIGSQLITESHELLSRC=$(foreach sf,$(SQLITESHELLSRC),$(SRCDIR)$(sf))
    93     93   SQLITESHELLOBJ=$(foreach sf,$(SQLITESHELLSRC),$(sf:.c=.obj))
    94         -SQLITESHELLDEFINES=-Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
           94  +SQLITESHELLDEFINES=-DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
    95     95   
    96     96   # define the th scripting files, which need special flags on compile
    97     97   THSRC=th.c th_lang.c
    98     98   ORIGTHSRC=$(foreach sf,$(THSRC),$(SRCDIR)$(sf))
    99     99   THOBJ=$(foreach sf,$(THSRC),$(sf:.c=.obj))
   100    100   
   101    101   # define the zlib files, needed by this compile

Changes to win/Makefile.dmc.

    24     24   CFLAGS = -o
    25     25   BCC    = $(DMDIR)\bin\dmc $(CFLAGS)
    26     26   TCC    = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
    27     27   LIBS   = $(DMDIR)\extra\lib\ zlib wsock32 advapi32
    28     28   
    29     29   SQLITE_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB
    30     30   
    31         -SHELL_OPTIONS = -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
           31  +SHELL_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
    32     32   
    33     33   SRC   = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c builtin_.c bundle_.c cache_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c cookies_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c dispatch_.c doc_.c encode_.c etag_.c event_.c export_.c file_.c finfo_.c foci_.c fshell_.c fusefs_.c glob_.c graph_.c gzip_.c hname_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c json_.c json_artifact_.c json_branch_.c json_config_.c json_diff_.c json_dir_.c json_finfo_.c json_login_.c json_query_.c json_report_.c json_status_.c json_tag_.c json_timeline_.c json_user_.c json_wiki_.c leaf_.c loadctrl_.c login_.c lookslike_.c main_.c manifest_.c markdown_.c markdown_html_.c md5_.c merge_.c merge3_.c moderate_.c name_.c path_.c piechart_.c pivot_.c popen_.c pqueue_.c printf_.c publish_.c purge_.c rebuild_.c regexp_.c report_.c rss_.c schema_.c search_.c security_audit_.c setup_.c sha1_.c sha1hard_.c sha3_.c shun_.c sitemap_.c skins_.c sqlcmd_.c stash_.c stat_.c statrep_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c unicode_.c unversioned_.c update_.c url_.c user_.c utf8_.c util_.c verify_.c vfile_.c wiki_.c wikiformat_.c winfile_.c winhttp_.c wysiwyg_.c xfer_.c xfersetup_.c zip_.c
    34     34   
    35     35   OBJ   = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\builtin$O $(OBJDIR)\bundle$O $(OBJDIR)\cache$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\cookies$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\dispatch$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\etag$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\foci$O $(OBJDIR)\fshell$O $(OBJDIR)\fusefs$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\hname$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\json$O $(OBJDIR)\json_artifact$O $(OBJDIR)\json_branch$O $(OBJDIR)\json_config$O $(OBJDIR)\json_diff$O $(OBJDIR)\json_dir$O $(OBJDIR)\json_finfo$O $(OBJDIR)\json_login$O $(OBJDIR)\json_query$O $(OBJDIR)\json_report$O $(OBJDIR)\json_status$O $(OBJDIR)\json_tag$O $(OBJDIR)\json_timeline$O $(OBJDIR)\json_user$O $(OBJDIR)\json_wiki$O $(OBJDIR)\leaf$O $(OBJDIR)\loadctrl$O $(OBJDIR)\login$O $(OBJDIR)\lookslike$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\markdown$O $(OBJDIR)\markdown_html$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\moderate$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\piechart$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\publish$O $(OBJDIR)\purge$O $(OBJDIR)\rebuild$O $(OBJDIR)\regexp$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\security_audit$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\sha1hard$O $(OBJDIR)\sha3$O $(OBJDIR)\shun$O $(OBJDIR)\sitemap$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\statrep$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\unicode$O $(OBJDIR)\unversioned$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\utf8$O $(OBJDIR)\util$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winfile$O $(OBJDIR)\winhttp$O $(OBJDIR)\wysiwyg$O $(OBJDIR)\xfer$O $(OBJDIR)\xfersetup$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
    36     36   
    37     37   
    38     38   RC=$(DMDIR)\bin\rcc

Changes to win/Makefile.mingw.

  2293   2293                    -DSQLITE_INTROSPECTION_PRAGMAS \
  2294   2294                    -DSQLITE_ENABLE_DBPAGE_VTAB \
  2295   2295                    -DSQLITE_WIN32_NO_ANSI \
  2296   2296                    $(MINGW_OPTIONS) \
  2297   2297                    -DSQLITE_USE_MALLOC_H \
  2298   2298                    -DSQLITE_USE_MSIZE
  2299   2299   
  2300         -SHELL_OPTIONS = -Dmain=sqlite3_shell \
         2300  +SHELL_OPTIONS = -DNDEBUG=1 \
         2301  +                -DSQLITE_THREADSAFE=0 \
         2302  +                -DSQLITE_DEFAULT_MEMSTATUS=0 \
         2303  +                -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
         2304  +                -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
         2305  +                -DSQLITE_OMIT_DECLTYPE \
         2306  +                -DSQLITE_OMIT_DEPRECATED \
         2307  +                -DSQLITE_OMIT_GET_TABLE \
         2308  +                -DSQLITE_OMIT_PROGRESS_CALLBACK \
         2309  +                -DSQLITE_OMIT_SHARED_CACHE \
         2310  +                -DSQLITE_OMIT_LOAD_EXTENSION \
         2311  +                -DSQLITE_MAX_EXPR_DEPTH=0 \
         2312  +                -DSQLITE_USE_ALLOCA \
         2313  +                -DSQLITE_ENABLE_LOCKING_STYLE=0 \
         2314  +                -DSQLITE_DEFAULT_FILE_FORMAT=4 \
         2315  +                -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
         2316  +                -DSQLITE_ENABLE_FTS4 \
         2317  +                -DSQLITE_ENABLE_FTS3_PARENTHESIS \
         2318  +                -DSQLITE_ENABLE_DBSTAT_VTAB \
         2319  +                -DSQLITE_ENABLE_JSON1 \
         2320  +                -DSQLITE_ENABLE_FTS5 \
         2321  +                -DSQLITE_ENABLE_STMTVTAB \
         2322  +                -DSQLITE_USE_ZLIB \
         2323  +                -DSQLITE_INTROSPECTION_PRAGMAS \
         2324  +                -DSQLITE_ENABLE_DBPAGE_VTAB \
         2325  +                -Dmain=sqlite3_shell \
  2301   2326                   -DSQLITE_SHELL_IS_UTF8=1 \
  2302   2327                   -DSQLITE_OMIT_LOAD_EXTENSION=1 \
  2303   2328                   -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
  2304   2329                   -DSQLITE_SHELL_DBNAME_PROC=fossil_open \
  2305   2330                   -Daccess=file_access \
  2306   2331                   -Dsystem=fossil_system \
  2307   2332                   -Dgetenv=fossil_getenv \

Changes to win/Makefile.msc.

   336    336                    /DSQLITE_ENABLE_FTS5 \
   337    337                    /DSQLITE_ENABLE_STMTVTAB \
   338    338                    /DSQLITE_USE_ZLIB \
   339    339                    /DSQLITE_INTROSPECTION_PRAGMAS \
   340    340                    /DSQLITE_ENABLE_DBPAGE_VTAB \
   341    341                    /DSQLITE_WIN32_NO_ANSI
   342    342   
   343         -SHELL_OPTIONS = /Dmain=sqlite3_shell \
          343  +SHELL_OPTIONS = /DNDEBUG=1 \
          344  +                /DSQLITE_THREADSAFE=0 \
          345  +                /DSQLITE_DEFAULT_MEMSTATUS=0 \
          346  +                /DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
          347  +                /DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
          348  +                /DSQLITE_OMIT_DECLTYPE \
          349  +                /DSQLITE_OMIT_DEPRECATED \
          350  +                /DSQLITE_OMIT_GET_TABLE \
          351  +                /DSQLITE_OMIT_PROGRESS_CALLBACK \
          352  +                /DSQLITE_OMIT_SHARED_CACHE \
          353  +                /DSQLITE_OMIT_LOAD_EXTENSION \
          354  +                /DSQLITE_MAX_EXPR_DEPTH=0 \
          355  +                /DSQLITE_USE_ALLOCA \
          356  +                /DSQLITE_ENABLE_LOCKING_STYLE=0 \
          357  +                /DSQLITE_DEFAULT_FILE_FORMAT=4 \
          358  +                /DSQLITE_ENABLE_EXPLAIN_COMMENTS \
          359  +                /DSQLITE_ENABLE_FTS4 \
          360  +                /DSQLITE_ENABLE_FTS3_PARENTHESIS \
          361  +                /DSQLITE_ENABLE_DBSTAT_VTAB \
          362  +                /DSQLITE_ENABLE_JSON1 \
          363  +                /DSQLITE_ENABLE_FTS5 \
          364  +                /DSQLITE_ENABLE_STMTVTAB \
          365  +                /DSQLITE_USE_ZLIB \
          366  +                /DSQLITE_INTROSPECTION_PRAGMAS \
          367  +                /DSQLITE_ENABLE_DBPAGE_VTAB \
          368  +                /Dmain=sqlite3_shell \
   344    369                   /DSQLITE_SHELL_IS_UTF8=1 \
   345    370                   /DSQLITE_OMIT_LOAD_EXTENSION=1 \
   346    371                   /DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
   347    372                   /DSQLITE_SHELL_DBNAME_PROC=fossil_open \
   348    373                   /Daccess=file_access \
   349    374                   /Dsystem=fossil_system \
   350    375                   /Dgetenv=fossil_getenv \