"Fossies" - the Fresh Open Source Software Archive

Member "sqlite-autoconf-3320300/shell.c" (18 Jun 2020, 602466 Bytes) of package /linux/misc/sqlite-autoconf-3320300.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "shell.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: autoconf3250300_vs_3260000.

    1 /* DO NOT EDIT!
    2 ** This file is automatically generated by the script in the canonical
    3 ** SQLite source tree at tool/mkshellc.tcl.  That script combines source
    4 ** code from various constituent source files of SQLite into this single
    5 ** "shell.c" file used to implement the SQLite command-line shell.
    6 **
    7 ** Most of the code found below comes from the "src/shell.c.in" file in
    8 ** the canonical SQLite source tree.  That main file contains "INCLUDE"
    9 ** lines that specify other files in the canonical source tree that are
   10 ** inserted to getnerate this complete program source file.
   11 **
   12 ** The code from multiple files is combined into this single "shell.c"
   13 ** source file to help make the command-line program easier to compile.
   14 **
   15 ** To modify this program, get a copy of the canonical SQLite source tree,
   16 ** edit the src/shell.c.in" and/or some of the other files that are included
   17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
   18 */
   19 /*
   20 ** 2001 September 15
   21 **
   22 ** The author disclaims copyright to this source code.  In place of
   23 ** a legal notice, here is a blessing:
   24 **
   25 **    May you do good and not evil.
   26 **    May you find forgiveness for yourself and forgive others.
   27 **    May you share freely, never taking more than you give.
   28 **
   29 *************************************************************************
   30 ** This file contains code to implement the "sqlite" command line
   31 ** utility for accessing SQLite databases.
   32 */
   33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
   34 /* This needs to come before any includes for MSVC compiler */
   35 #define _CRT_SECURE_NO_WARNINGS
   36 #endif
   37 
   38 /*
   39 ** Determine if we are dealing with WinRT, which provides only a subset of
   40 ** the full Win32 API.
   41 */
   42 #if !defined(SQLITE_OS_WINRT)
   43 # define SQLITE_OS_WINRT 0
   44 #endif
   45 
   46 /*
   47 ** Warning pragmas copied from msvc.h in the core.
   48 */
   49 #if defined(_MSC_VER)
   50 #pragma warning(disable : 4054)
   51 #pragma warning(disable : 4055)
   52 #pragma warning(disable : 4100)
   53 #pragma warning(disable : 4127)
   54 #pragma warning(disable : 4130)
   55 #pragma warning(disable : 4152)
   56 #pragma warning(disable : 4189)
   57 #pragma warning(disable : 4206)
   58 #pragma warning(disable : 4210)
   59 #pragma warning(disable : 4232)
   60 #pragma warning(disable : 4244)
   61 #pragma warning(disable : 4305)
   62 #pragma warning(disable : 4306)
   63 #pragma warning(disable : 4702)
   64 #pragma warning(disable : 4706)
   65 #endif /* defined(_MSC_VER) */
   66 
   67 /*
   68 ** No support for loadable extensions in VxWorks.
   69 */
   70 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
   71 # define SQLITE_OMIT_LOAD_EXTENSION 1
   72 #endif
   73 
   74 /*
   75 ** Enable large-file support for fopen() and friends on unix.
   76 */
   77 #ifndef SQLITE_DISABLE_LFS
   78 # define _LARGE_FILE       1
   79 # ifndef _FILE_OFFSET_BITS
   80 #   define _FILE_OFFSET_BITS 64
   81 # endif
   82 # define _LARGEFILE_SOURCE 1
   83 #endif
   84 
   85 #include <stdlib.h>
   86 #include <string.h>
   87 #include <stdio.h>
   88 #include <assert.h>
   89 #include "sqlite3.h"
   90 typedef sqlite3_int64 i64;
   91 typedef sqlite3_uint64 u64;
   92 typedef unsigned char u8;
   93 #if SQLITE_USER_AUTHENTICATION
   94 # include "sqlite3userauth.h"
   95 #endif
   96 #include <ctype.h>
   97 #include <stdarg.h>
   98 
   99 #if !defined(_WIN32) && !defined(WIN32)
  100 # include <signal.h>
  101 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
  102 #  include <pwd.h>
  103 # endif
  104 #endif
  105 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
  106 # include <unistd.h>
  107 # include <dirent.h>
  108 # define GETPID getpid
  109 # if defined(__MINGW32__)
  110 #  define DIRENT dirent
  111 #  ifndef S_ISLNK
  112 #   define S_ISLNK(mode) (0)
  113 #  endif
  114 # endif
  115 #else
  116 # define GETPID (int)GetCurrentProcessId
  117 #endif
  118 #include <sys/types.h>
  119 #include <sys/stat.h>
  120 
  121 #if HAVE_READLINE
  122 # include <readline/readline.h>
  123 # include <readline/history.h>
  124 #endif
  125 
  126 #if HAVE_EDITLINE
  127 # include <editline/readline.h>
  128 #endif
  129 
  130 #if HAVE_EDITLINE || HAVE_READLINE
  131 
  132 # define shell_add_history(X) add_history(X)
  133 # define shell_read_history(X) read_history(X)
  134 # define shell_write_history(X) write_history(X)
  135 # define shell_stifle_history(X) stifle_history(X)
  136 # define shell_readline(X) readline(X)
  137 
  138 #elif HAVE_LINENOISE
  139 
  140 # include "linenoise.h"
  141 # define shell_add_history(X) linenoiseHistoryAdd(X)
  142 # define shell_read_history(X) linenoiseHistoryLoad(X)
  143 # define shell_write_history(X) linenoiseHistorySave(X)
  144 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
  145 # define shell_readline(X) linenoise(X)
  146 
  147 #else
  148 
  149 # define shell_read_history(X)
  150 # define shell_write_history(X)
  151 # define shell_stifle_history(X)
  152 
  153 # define SHELL_USE_LOCAL_GETLINE 1
  154 #endif
  155 
  156 
  157 #if defined(_WIN32) || defined(WIN32)
  158 # if SQLITE_OS_WINRT
  159 #  define SQLITE_OMIT_POPEN 1
  160 # else
  161 #  include <io.h>
  162 #  include <fcntl.h>
  163 #  define isatty(h) _isatty(h)
  164 #  ifndef access
  165 #   define access(f,m) _access((f),(m))
  166 #  endif
  167 #  ifndef unlink
  168 #   define unlink _unlink
  169 #  endif
  170 #  ifndef strdup
  171 #   define strdup _strdup
  172 #  endif
  173 #  undef popen
  174 #  define popen _popen
  175 #  undef pclose
  176 #  define pclose _pclose
  177 # endif
  178 #else
  179  /* Make sure isatty() has a prototype. */
  180  extern int isatty(int);
  181 
  182 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
  183   /* popen and pclose are not C89 functions and so are
  184   ** sometimes omitted from the <stdio.h> header */
  185    extern FILE *popen(const char*,const char*);
  186    extern int pclose(FILE*);
  187 # else
  188 #  define SQLITE_OMIT_POPEN 1
  189 # endif
  190 #endif
  191 
  192 #if defined(_WIN32_WCE)
  193 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
  194  * thus we always assume that we have a console. That can be
  195  * overridden with the -batch command line option.
  196  */
  197 #define isatty(x) 1
  198 #endif
  199 
  200 /* ctype macros that work with signed characters */
  201 #define IsSpace(X)  isspace((unsigned char)X)
  202 #define IsDigit(X)  isdigit((unsigned char)X)
  203 #define ToLower(X)  (char)tolower((unsigned char)X)
  204 
  205 #if defined(_WIN32) || defined(WIN32)
  206 #if SQLITE_OS_WINRT
  207 #include <intrin.h>
  208 #endif
  209 #include <windows.h>
  210 
  211 /* string conversion routines only needed on Win32 */
  212 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
  213 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
  214 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
  215 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
  216 #endif
  217 
  218 /* On Windows, we normally run with output mode of TEXT so that \n characters
  219 ** are automatically translated into \r\n.  However, this behavior needs
  220 ** to be disabled in some cases (ex: when generating CSV output and when
  221 ** rendering quoted strings that contain \n characters).  The following
  222 ** routines take care of that.
  223 */
  224 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
  225 static void setBinaryMode(FILE *file, int isOutput){
  226   if( isOutput ) fflush(file);
  227   _setmode(_fileno(file), _O_BINARY);
  228 }
  229 static void setTextMode(FILE *file, int isOutput){
  230   if( isOutput ) fflush(file);
  231   _setmode(_fileno(file), _O_TEXT);
  232 }
  233 #else
  234 # define setBinaryMode(X,Y)
  235 # define setTextMode(X,Y)
  236 #endif
  237 
  238 
  239 /* True if the timer is enabled */
  240 static int enableTimer = 0;
  241 
  242 /* Return the current wall-clock time */
  243 static sqlite3_int64 timeOfDay(void){
  244   static sqlite3_vfs *clockVfs = 0;
  245   sqlite3_int64 t;
  246   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
  247   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
  248     clockVfs->xCurrentTimeInt64(clockVfs, &t);
  249   }else{
  250     double r;
  251     clockVfs->xCurrentTime(clockVfs, &r);
  252     t = (sqlite3_int64)(r*86400000.0);
  253   }
  254   return t;
  255 }
  256 
  257 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
  258 #include <sys/time.h>
  259 #include <sys/resource.h>
  260 
  261 /* VxWorks does not support getrusage() as far as we can determine */
  262 #if defined(_WRS_KERNEL) || defined(__RTP__)
  263 struct rusage {
  264   struct timeval ru_utime; /* user CPU time used */
  265   struct timeval ru_stime; /* system CPU time used */
  266 };
  267 #define getrusage(A,B) memset(B,0,sizeof(*B))
  268 #endif
  269 
  270 /* Saved resource information for the beginning of an operation */
  271 static struct rusage sBegin;  /* CPU time at start */
  272 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
  273 
  274 /*
  275 ** Begin timing an operation
  276 */
  277 static void beginTimer(void){
  278   if( enableTimer ){
  279     getrusage(RUSAGE_SELF, &sBegin);
  280     iBegin = timeOfDay();
  281   }
  282 }
  283 
  284 /* Return the difference of two time_structs in seconds */
  285 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
  286   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
  287          (double)(pEnd->tv_sec - pStart->tv_sec);
  288 }
  289 
  290 /*
  291 ** Print the timing results.
  292 */
  293 static void endTimer(void){
  294   if( enableTimer ){
  295     sqlite3_int64 iEnd = timeOfDay();
  296     struct rusage sEnd;
  297     getrusage(RUSAGE_SELF, &sEnd);
  298     printf("Run Time: real %.3f user %f sys %f\n",
  299        (iEnd - iBegin)*0.001,
  300        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
  301        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
  302   }
  303 }
  304 
  305 #define BEGIN_TIMER beginTimer()
  306 #define END_TIMER endTimer()
  307 #define HAS_TIMER 1
  308 
  309 #elif (defined(_WIN32) || defined(WIN32))
  310 
  311 /* Saved resource information for the beginning of an operation */
  312 static HANDLE hProcess;
  313 static FILETIME ftKernelBegin;
  314 static FILETIME ftUserBegin;
  315 static sqlite3_int64 ftWallBegin;
  316 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
  317                                     LPFILETIME, LPFILETIME);
  318 static GETPROCTIMES getProcessTimesAddr = NULL;
  319 
  320 /*
  321 ** Check to see if we have timer support.  Return 1 if necessary
  322 ** support found (or found previously).
  323 */
  324 static int hasTimer(void){
  325   if( getProcessTimesAddr ){
  326     return 1;
  327   } else {
  328 #if !SQLITE_OS_WINRT
  329     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
  330     ** versions. See if the version we are running on has it, and if it
  331     ** does, save off a pointer to it and the current process handle.
  332     */
  333     hProcess = GetCurrentProcess();
  334     if( hProcess ){
  335       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
  336       if( NULL != hinstLib ){
  337         getProcessTimesAddr =
  338             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
  339         if( NULL != getProcessTimesAddr ){
  340           return 1;
  341         }
  342         FreeLibrary(hinstLib);
  343       }
  344     }
  345 #endif
  346   }
  347   return 0;
  348 }
  349 
  350 /*
  351 ** Begin timing an operation
  352 */
  353 static void beginTimer(void){
  354   if( enableTimer && getProcessTimesAddr ){
  355     FILETIME ftCreation, ftExit;
  356     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
  357                         &ftKernelBegin,&ftUserBegin);
  358     ftWallBegin = timeOfDay();
  359   }
  360 }
  361 
  362 /* Return the difference of two FILETIME structs in seconds */
  363 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
  364   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
  365   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
  366   return (double) ((i64End - i64Start) / 10000000.0);
  367 }
  368 
  369 /*
  370 ** Print the timing results.
  371 */
  372 static void endTimer(void){
  373   if( enableTimer && getProcessTimesAddr){
  374     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
  375     sqlite3_int64 ftWallEnd = timeOfDay();
  376     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
  377     printf("Run Time: real %.3f user %f sys %f\n",
  378        (ftWallEnd - ftWallBegin)*0.001,
  379        timeDiff(&ftUserBegin, &ftUserEnd),
  380        timeDiff(&ftKernelBegin, &ftKernelEnd));
  381   }
  382 }
  383 
  384 #define BEGIN_TIMER beginTimer()
  385 #define END_TIMER endTimer()
  386 #define HAS_TIMER hasTimer()
  387 
  388 #else
  389 #define BEGIN_TIMER
  390 #define END_TIMER
  391 #define HAS_TIMER 0
  392 #endif
  393 
  394 /*
  395 ** Used to prevent warnings about unused parameters
  396 */
  397 #define UNUSED_PARAMETER(x) (void)(x)
  398 
  399 /*
  400 ** Number of elements in an array
  401 */
  402 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
  403 
  404 /*
  405 ** If the following flag is set, then command execution stops
  406 ** at an error if we are not interactive.
  407 */
  408 static int bail_on_error = 0;
  409 
  410 /*
  411 ** Threat stdin as an interactive input if the following variable
  412 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
  413 */
  414 static int stdin_is_interactive = 1;
  415 
  416 /*
  417 ** On Windows systems we have to know if standard output is a console
  418 ** in order to translate UTF-8 into MBCS.  The following variable is
  419 ** true if translation is required.
  420 */
  421 static int stdout_is_console = 1;
  422 
  423 /*
  424 ** The following is the open SQLite database.  We make a pointer
  425 ** to this database a static variable so that it can be accessed
  426 ** by the SIGINT handler to interrupt database processing.
  427 */
  428 static sqlite3 *globalDb = 0;
  429 
  430 /*
  431 ** True if an interrupt (Control-C) has been received.
  432 */
  433 static volatile int seenInterrupt = 0;
  434 
  435 #ifdef SQLITE_DEBUG
  436 /*
  437 ** Out-of-memory simulator variables
  438 */
  439 static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
  440 static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
  441 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
  442 #endif /* SQLITE_DEBUG */
  443 
  444 /*
  445 ** This is the name of our program. It is set in main(), used
  446 ** in a number of other places, mostly for error messages.
  447 */
  448 static char *Argv0;
  449 
  450 /*
  451 ** Prompt strings. Initialized in main. Settable with
  452 **   .prompt main continue
  453 */
  454 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
  455 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
  456 
  457 /*
  458 ** Render output like fprintf().  Except, if the output is going to the
  459 ** console and if this is running on a Windows machine, translate the
  460 ** output from UTF-8 into MBCS.
  461 */
  462 #if defined(_WIN32) || defined(WIN32)
  463 void utf8_printf(FILE *out, const char *zFormat, ...){
  464   va_list ap;
  465   va_start(ap, zFormat);
  466   if( stdout_is_console && (out==stdout || out==stderr) ){
  467     char *z1 = sqlite3_vmprintf(zFormat, ap);
  468     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
  469     sqlite3_free(z1);
  470     fputs(z2, out);
  471     sqlite3_free(z2);
  472   }else{
  473     vfprintf(out, zFormat, ap);
  474   }
  475   va_end(ap);
  476 }
  477 #elif !defined(utf8_printf)
  478 # define utf8_printf fprintf
  479 #endif
  480 
  481 /*
  482 ** Render output like fprintf().  This should not be used on anything that
  483 ** includes string formatting (e.g. "%s").
  484 */
  485 #if !defined(raw_printf)
  486 # define raw_printf fprintf
  487 #endif
  488 
  489 /* Indicate out-of-memory and exit. */
  490 static void shell_out_of_memory(void){
  491   raw_printf(stderr,"Error: out of memory\n");
  492   exit(1);
  493 }
  494 
  495 #ifdef SQLITE_DEBUG
  496 /* This routine is called when a simulated OOM occurs.  It is broken
  497 ** out as a separate routine to make it easy to set a breakpoint on
  498 ** the OOM
  499 */
  500 void shellOomFault(void){
  501   if( oomRepeat>0 ){
  502     oomRepeat--;
  503   }else{
  504     oomCounter--;
  505   }
  506 }
  507 #endif /* SQLITE_DEBUG */
  508 
  509 #ifdef SQLITE_DEBUG
  510 /* This routine is a replacement malloc() that is used to simulate
  511 ** Out-Of-Memory (OOM) errors for testing purposes.
  512 */
  513 static void *oomMalloc(int nByte){
  514   if( oomCounter ){
  515     if( oomCounter==1 ){
  516       shellOomFault();
  517       return 0;
  518     }else{
  519       oomCounter--;
  520     }
  521   }
  522   return defaultMalloc(nByte);
  523 }
  524 #endif /* SQLITE_DEBUG */
  525 
  526 #ifdef SQLITE_DEBUG
  527 /* Register the OOM simulator.  This must occur before any memory
  528 ** allocations */
  529 static void registerOomSimulator(void){
  530   sqlite3_mem_methods mem;
  531   sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
  532   defaultMalloc = mem.xMalloc;
  533   mem.xMalloc = oomMalloc;
  534   sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
  535 }
  536 #endif
  537 
  538 /*
  539 ** Write I/O traces to the following stream.
  540 */
  541 #ifdef SQLITE_ENABLE_IOTRACE
  542 static FILE *iotrace = 0;
  543 #endif
  544 
  545 /*
  546 ** This routine works like printf in that its first argument is a
  547 ** format string and subsequent arguments are values to be substituted
  548 ** in place of % fields.  The result of formatting this string
  549 ** is written to iotrace.
  550 */
  551 #ifdef SQLITE_ENABLE_IOTRACE
  552 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
  553   va_list ap;
  554   char *z;
  555   if( iotrace==0 ) return;
  556   va_start(ap, zFormat);
  557   z = sqlite3_vmprintf(zFormat, ap);
  558   va_end(ap);
  559   utf8_printf(iotrace, "%s", z);
  560   sqlite3_free(z);
  561 }
  562 #endif
  563 
  564 /*
  565 ** Output string zUtf to stream pOut as w characters.  If w is negative,
  566 ** then right-justify the text.  W is the width in UTF-8 characters, not
  567 ** in bytes.  This is different from the %*.*s specification in printf
  568 ** since with %*.*s the width is measured in bytes, not characters.
  569 */
  570 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
  571   int i;
  572   int n;
  573   int aw = w<0 ? -w : w;
  574   char zBuf[1000];
  575   if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
  576   for(i=n=0; zUtf[i]; i++){
  577     if( (zUtf[i]&0xc0)!=0x80 ){
  578       n++;
  579       if( n==aw ){
  580         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
  581         break;
  582       }
  583     }
  584   }
  585   if( n>=aw ){
  586     utf8_printf(pOut, "%.*s", i, zUtf);
  587   }else if( w<0 ){
  588     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
  589   }else{
  590     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
  591   }
  592 }
  593 
  594 
  595 /*
  596 ** Determines if a string is a number of not.
  597 */
  598 static int isNumber(const char *z, int *realnum){
  599   if( *z=='-' || *z=='+' ) z++;
  600   if( !IsDigit(*z) ){
  601     return 0;
  602   }
  603   z++;
  604   if( realnum ) *realnum = 0;
  605   while( IsDigit(*z) ){ z++; }
  606   if( *z=='.' ){
  607     z++;
  608     if( !IsDigit(*z) ) return 0;
  609     while( IsDigit(*z) ){ z++; }
  610     if( realnum ) *realnum = 1;
  611   }
  612   if( *z=='e' || *z=='E' ){
  613     z++;
  614     if( *z=='+' || *z=='-' ) z++;
  615     if( !IsDigit(*z) ) return 0;
  616     while( IsDigit(*z) ){ z++; }
  617     if( realnum ) *realnum = 1;
  618   }
  619   return *z==0;
  620 }
  621 
  622 /*
  623 ** Compute a string length that is limited to what can be stored in
  624 ** lower 30 bits of a 32-bit signed integer.
  625 */
  626 static int strlen30(const char *z){
  627   const char *z2 = z;
  628   while( *z2 ){ z2++; }
  629   return 0x3fffffff & (int)(z2 - z);
  630 }
  631 
  632 /*
  633 ** Return the length of a string in characters.  Multibyte UTF8 characters
  634 ** count as a single character.
  635 */
  636 static int strlenChar(const char *z){
  637   int n = 0;
  638   while( *z ){
  639     if( (0xc0&*(z++))!=0x80 ) n++;
  640   }
  641   return n;
  642 }
  643 
  644 /*
  645 ** This routine reads a line of text from FILE in, stores
  646 ** the text in memory obtained from malloc() and returns a pointer
  647 ** to the text.  NULL is returned at end of file, or if malloc()
  648 ** fails.
  649 **
  650 ** If zLine is not NULL then it is a malloced buffer returned from
  651 ** a previous call to this routine that may be reused.
  652 */
  653 static char *local_getline(char *zLine, FILE *in){
  654   int nLine = zLine==0 ? 0 : 100;
  655   int n = 0;
  656 
  657   while( 1 ){
  658     if( n+100>nLine ){
  659       nLine = nLine*2 + 100;
  660       zLine = realloc(zLine, nLine);
  661       if( zLine==0 ) shell_out_of_memory();
  662     }
  663     if( fgets(&zLine[n], nLine - n, in)==0 ){
  664       if( n==0 ){
  665         free(zLine);
  666         return 0;
  667       }
  668       zLine[n] = 0;
  669       break;
  670     }
  671     while( zLine[n] ) n++;
  672     if( n>0 && zLine[n-1]=='\n' ){
  673       n--;
  674       if( n>0 && zLine[n-1]=='\r' ) n--;
  675       zLine[n] = 0;
  676       break;
  677     }
  678   }
  679 #if defined(_WIN32) || defined(WIN32)
  680   /* For interactive input on Windows systems, translate the
  681   ** multi-byte characterset characters into UTF-8. */
  682   if( stdin_is_interactive && in==stdin ){
  683     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
  684     if( zTrans ){
  685       int nTrans = strlen30(zTrans)+1;
  686       if( nTrans>nLine ){
  687         zLine = realloc(zLine, nTrans);
  688         if( zLine==0 ) shell_out_of_memory();
  689       }
  690       memcpy(zLine, zTrans, nTrans);
  691       sqlite3_free(zTrans);
  692     }
  693   }
  694 #endif /* defined(_WIN32) || defined(WIN32) */
  695   return zLine;
  696 }
  697 
  698 /*
  699 ** Retrieve a single line of input text.
  700 **
  701 ** If in==0 then read from standard input and prompt before each line.
  702 ** If isContinuation is true, then a continuation prompt is appropriate.
  703 ** If isContinuation is zero, then the main prompt should be used.
  704 **
  705 ** If zPrior is not NULL then it is a buffer from a prior call to this
  706 ** routine that can be reused.
  707 **
  708 ** The result is stored in space obtained from malloc() and must either
  709 ** be freed by the caller or else passed back into this routine via the
  710 ** zPrior argument for reuse.
  711 */
  712 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
  713   char *zPrompt;
  714   char *zResult;
  715   if( in!=0 ){
  716     zResult = local_getline(zPrior, in);
  717   }else{
  718     zPrompt = isContinuation ? continuePrompt : mainPrompt;
  719 #if SHELL_USE_LOCAL_GETLINE
  720     printf("%s", zPrompt);
  721     fflush(stdout);
  722     zResult = local_getline(zPrior, stdin);
  723 #else
  724     free(zPrior);
  725     zResult = shell_readline(zPrompt);
  726     if( zResult && *zResult ) shell_add_history(zResult);
  727 #endif
  728   }
  729   return zResult;
  730 }
  731 
  732 
  733 /*
  734 ** Return the value of a hexadecimal digit.  Return -1 if the input
  735 ** is not a hex digit.
  736 */
  737 static int hexDigitValue(char c){
  738   if( c>='0' && c<='9' ) return c - '0';
  739   if( c>='a' && c<='f' ) return c - 'a' + 10;
  740   if( c>='A' && c<='F' ) return c - 'A' + 10;
  741   return -1;
  742 }
  743 
  744 /*
  745 ** Interpret zArg as an integer value, possibly with suffixes.
  746 */
  747 static sqlite3_int64 integerValue(const char *zArg){
  748   sqlite3_int64 v = 0;
  749   static const struct { char *zSuffix; int iMult; } aMult[] = {
  750     { "KiB", 1024 },
  751     { "MiB", 1024*1024 },
  752     { "GiB", 1024*1024*1024 },
  753     { "KB",  1000 },
  754     { "MB",  1000000 },
  755     { "GB",  1000000000 },
  756     { "K",   1000 },
  757     { "M",   1000000 },
  758     { "G",   1000000000 },
  759   };
  760   int i;
  761   int isNeg = 0;
  762   if( zArg[0]=='-' ){
  763     isNeg = 1;
  764     zArg++;
  765   }else if( zArg[0]=='+' ){
  766     zArg++;
  767   }
  768   if( zArg[0]=='0' && zArg[1]=='x' ){
  769     int x;
  770     zArg += 2;
  771     while( (x = hexDigitValue(zArg[0]))>=0 ){
  772       v = (v<<4) + x;
  773       zArg++;
  774     }
  775   }else{
  776     while( IsDigit(zArg[0]) ){
  777       v = v*10 + zArg[0] - '0';
  778       zArg++;
  779     }
  780   }
  781   for(i=0; i<ArraySize(aMult); i++){
  782     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
  783       v *= aMult[i].iMult;
  784       break;
  785     }
  786   }
  787   return isNeg? -v : v;
  788 }
  789 
  790 /*
  791 ** A variable length string to which one can append text.
  792 */
  793 typedef struct ShellText ShellText;
  794 struct ShellText {
  795   char *z;
  796   int n;
  797   int nAlloc;
  798 };
  799 
  800 /*
  801 ** Initialize and destroy a ShellText object
  802 */
  803 static void initText(ShellText *p){
  804   memset(p, 0, sizeof(*p));
  805 }
  806 static void freeText(ShellText *p){
  807   free(p->z);
  808   initText(p);
  809 }
  810 
  811 /* zIn is either a pointer to a NULL-terminated string in memory obtained
  812 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
  813 ** added to zIn, and the result returned in memory obtained from malloc().
  814 ** zIn, if it was not NULL, is freed.
  815 **
  816 ** If the third argument, quote, is not '\0', then it is used as a
  817 ** quote character for zAppend.
  818 */
  819 static void appendText(ShellText *p, char const *zAppend, char quote){
  820   int len;
  821   int i;
  822   int nAppend = strlen30(zAppend);
  823 
  824   len = nAppend+p->n+1;
  825   if( quote ){
  826     len += 2;
  827     for(i=0; i<nAppend; i++){
  828       if( zAppend[i]==quote ) len++;
  829     }
  830   }
  831 
  832   if( p->n+len>=p->nAlloc ){
  833     p->nAlloc = p->nAlloc*2 + len + 20;
  834     p->z = realloc(p->z, p->nAlloc);
  835     if( p->z==0 ) shell_out_of_memory();
  836   }
  837 
  838   if( quote ){
  839     char *zCsr = p->z+p->n;
  840     *zCsr++ = quote;
  841     for(i=0; i<nAppend; i++){
  842       *zCsr++ = zAppend[i];
  843       if( zAppend[i]==quote ) *zCsr++ = quote;
  844     }
  845     *zCsr++ = quote;
  846     p->n = (int)(zCsr - p->z);
  847     *zCsr = '\0';
  848   }else{
  849     memcpy(p->z+p->n, zAppend, nAppend);
  850     p->n += nAppend;
  851     p->z[p->n] = '\0';
  852   }
  853 }
  854 
  855 /*
  856 ** Attempt to determine if identifier zName needs to be quoted, either
  857 ** because it contains non-alphanumeric characters, or because it is an
  858 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
  859 ** that quoting is required.
  860 **
  861 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
  862 */
  863 static char quoteChar(const char *zName){
  864   int i;
  865   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
  866   for(i=0; zName[i]; i++){
  867     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
  868   }
  869   return sqlite3_keyword_check(zName, i) ? '"' : 0;
  870 }
  871 
  872 /*
  873 ** Construct a fake object name and column list to describe the structure
  874 ** of the view, virtual table, or table valued function zSchema.zName.
  875 */
  876 static char *shellFakeSchema(
  877   sqlite3 *db,            /* The database connection containing the vtab */
  878   const char *zSchema,    /* Schema of the database holding the vtab */
  879   const char *zName       /* The name of the virtual table */
  880 ){
  881   sqlite3_stmt *pStmt = 0;
  882   char *zSql;
  883   ShellText s;
  884   char cQuote;
  885   char *zDiv = "(";
  886   int nRow = 0;
  887 
  888   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
  889                          zSchema ? zSchema : "main", zName);
  890   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  891   sqlite3_free(zSql);
  892   initText(&s);
  893   if( zSchema ){
  894     cQuote = quoteChar(zSchema);
  895     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
  896     appendText(&s, zSchema, cQuote);
  897     appendText(&s, ".", 0);
  898   }
  899   cQuote = quoteChar(zName);
  900   appendText(&s, zName, cQuote);
  901   while( sqlite3_step(pStmt)==SQLITE_ROW ){
  902     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
  903     nRow++;
  904     appendText(&s, zDiv, 0);
  905     zDiv = ",";
  906     cQuote = quoteChar(zCol);
  907     appendText(&s, zCol, cQuote);
  908   }
  909   appendText(&s, ")", 0);
  910   sqlite3_finalize(pStmt);
  911   if( nRow==0 ){
  912     freeText(&s);
  913     s.z = 0;
  914   }
  915   return s.z;
  916 }
  917 
  918 /*
  919 ** SQL function:  shell_module_schema(X)
  920 **
  921 ** Return a fake schema for the table-valued function or eponymous virtual
  922 ** table X.
  923 */
  924 static void shellModuleSchema(
  925   sqlite3_context *pCtx,
  926   int nVal,
  927   sqlite3_value **apVal
  928 ){
  929   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
  930   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
  931   UNUSED_PARAMETER(nVal);
  932   if( zFake ){
  933     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
  934                         -1, sqlite3_free);
  935     free(zFake);
  936   }
  937 }
  938 
  939 /*
  940 ** SQL function:  shell_add_schema(S,X)
  941 **
  942 ** Add the schema name X to the CREATE statement in S and return the result.
  943 ** Examples:
  944 **
  945 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
  946 **
  947 ** Also works on
  948 **
  949 **    CREATE INDEX
  950 **    CREATE UNIQUE INDEX
  951 **    CREATE VIEW
  952 **    CREATE TRIGGER
  953 **    CREATE VIRTUAL TABLE
  954 **
  955 ** This UDF is used by the .schema command to insert the schema name of
  956 ** attached databases into the middle of the sqlite_master.sql field.
  957 */
  958 static void shellAddSchemaName(
  959   sqlite3_context *pCtx,
  960   int nVal,
  961   sqlite3_value **apVal
  962 ){
  963   static const char *aPrefix[] = {
  964      "TABLE",
  965      "INDEX",
  966      "UNIQUE INDEX",
  967      "VIEW",
  968      "TRIGGER",
  969      "VIRTUAL TABLE"
  970   };
  971   int i = 0;
  972   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
  973   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
  974   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
  975   sqlite3 *db = sqlite3_context_db_handle(pCtx);
  976   UNUSED_PARAMETER(nVal);
  977   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
  978     for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
  979       int n = strlen30(aPrefix[i]);
  980       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
  981         char *z = 0;
  982         char *zFake = 0;
  983         if( zSchema ){
  984           char cQuote = quoteChar(zSchema);
  985           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
  986             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
  987           }else{
  988             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
  989           }
  990         }
  991         if( zName
  992          && aPrefix[i][0]=='V'
  993          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
  994         ){
  995           if( z==0 ){
  996             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
  997           }else{
  998             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
  999           }
 1000           free(zFake);
 1001         }
 1002         if( z ){
 1003           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
 1004           return;
 1005         }
 1006       }
 1007     }
 1008   }
 1009   sqlite3_result_value(pCtx, apVal[0]);
 1010 }
 1011 
 1012 /*
 1013 ** The source code for several run-time loadable extensions is inserted
 1014 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
 1015 ** code, we need to override some macros to make the included program code
 1016 ** work here in the middle of this regular program.
 1017 */
 1018 #define SQLITE_EXTENSION_INIT1
 1019 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
 1020 
 1021 #if defined(_WIN32) && defined(_MSC_VER)
 1022 /************************* Begin test_windirent.h ******************/
 1023 /*
 1024 ** 2015 November 30
 1025 **
 1026 ** The author disclaims copyright to this source code.  In place of
 1027 ** a legal notice, here is a blessing:
 1028 **
 1029 **    May you do good and not evil.
 1030 **    May you find forgiveness for yourself and forgive others.
 1031 **    May you share freely, never taking more than you give.
 1032 **
 1033 *************************************************************************
 1034 ** This file contains declarations for most of the opendir() family of
 1035 ** POSIX functions on Win32 using the MSVCRT.
 1036 */
 1037 
 1038 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
 1039 #define SQLITE_WINDIRENT_H
 1040 
 1041 /*
 1042 ** We need several data types from the Windows SDK header.
 1043 */
 1044 
 1045 #ifndef WIN32_LEAN_AND_MEAN
 1046 #define WIN32_LEAN_AND_MEAN
 1047 #endif
 1048 
 1049 #include "windows.h"
 1050 
 1051 /*
 1052 ** We need several support functions from the SQLite core.
 1053 */
 1054 
 1055 /* #include "sqlite3.h" */
 1056 
 1057 /*
 1058 ** We need several things from the ANSI and MSVCRT headers.
 1059 */
 1060 
 1061 #include <stdio.h>
 1062 #include <stdlib.h>
 1063 #include <errno.h>
 1064 #include <io.h>
 1065 #include <limits.h>
 1066 #include <sys/types.h>
 1067 #include <sys/stat.h>
 1068 
 1069 /*
 1070 ** We may need several defines that should have been in "sys/stat.h".
 1071 */
 1072 
 1073 #ifndef S_ISREG
 1074 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
 1075 #endif
 1076 
 1077 #ifndef S_ISDIR
 1078 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
 1079 #endif
 1080 
 1081 #ifndef S_ISLNK
 1082 #define S_ISLNK(mode) (0)
 1083 #endif
 1084 
 1085 /*
 1086 ** We may need to provide the "mode_t" type.
 1087 */
 1088 
 1089 #ifndef MODE_T_DEFINED
 1090   #define MODE_T_DEFINED
 1091   typedef unsigned short mode_t;
 1092 #endif
 1093 
 1094 /*
 1095 ** We may need to provide the "ino_t" type.
 1096 */
 1097 
 1098 #ifndef INO_T_DEFINED
 1099   #define INO_T_DEFINED
 1100   typedef unsigned short ino_t;
 1101 #endif
 1102 
 1103 /*
 1104 ** We need to define "NAME_MAX" if it was not present in "limits.h".
 1105 */
 1106 
 1107 #ifndef NAME_MAX
 1108 #  ifdef FILENAME_MAX
 1109 #    define NAME_MAX (FILENAME_MAX)
 1110 #  else
 1111 #    define NAME_MAX (260)
 1112 #  endif
 1113 #endif
 1114 
 1115 /*
 1116 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
 1117 */
 1118 
 1119 #ifndef NULL_INTPTR_T
 1120 #  define NULL_INTPTR_T ((intptr_t)(0))
 1121 #endif
 1122 
 1123 #ifndef BAD_INTPTR_T
 1124 #  define BAD_INTPTR_T ((intptr_t)(-1))
 1125 #endif
 1126 
 1127 /*
 1128 ** We need to provide the necessary structures and related types.
 1129 */
 1130 
 1131 #ifndef DIRENT_DEFINED
 1132 #define DIRENT_DEFINED
 1133 typedef struct DIRENT DIRENT;
 1134 typedef DIRENT *LPDIRENT;
 1135 struct DIRENT {
 1136   ino_t d_ino;               /* Sequence number, do not use. */
 1137   unsigned d_attributes;     /* Win32 file attributes. */
 1138   char d_name[NAME_MAX + 1]; /* Name within the directory. */
 1139 };
 1140 #endif
 1141 
 1142 #ifndef DIR_DEFINED
 1143 #define DIR_DEFINED
 1144 typedef struct DIR DIR;
 1145 typedef DIR *LPDIR;
 1146 struct DIR {
 1147   intptr_t d_handle; /* Value returned by "_findfirst". */
 1148   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
 1149   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
 1150 };
 1151 #endif
 1152 
 1153 /*
 1154 ** Provide a macro, for use by the implementation, to determine if a
 1155 ** particular directory entry should be skipped over when searching for
 1156 ** the next directory entry that should be returned by the readdir() or
 1157 ** readdir_r() functions.
 1158 */
 1159 
 1160 #ifndef is_filtered
 1161 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
 1162 #endif
 1163 
 1164 /*
 1165 ** Provide the function prototype for the POSIX compatiable getenv()
 1166 ** function.  This function is not thread-safe.
 1167 */
 1168 
 1169 extern const char *windirent_getenv(const char *name);
 1170 
 1171 /*
 1172 ** Finally, we can provide the function prototypes for the opendir(),
 1173 ** readdir(), readdir_r(), and closedir() POSIX functions.
 1174 */
 1175 
 1176 extern LPDIR opendir(const char *dirname);
 1177 extern LPDIRENT readdir(LPDIR dirp);
 1178 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
 1179 extern INT closedir(LPDIR dirp);
 1180 
 1181 #endif /* defined(WIN32) && defined(_MSC_VER) */
 1182 
 1183 /************************* End test_windirent.h ********************/
 1184 /************************* Begin test_windirent.c ******************/
 1185 /*
 1186 ** 2015 November 30
 1187 **
 1188 ** The author disclaims copyright to this source code.  In place of
 1189 ** a legal notice, here is a blessing:
 1190 **
 1191 **    May you do good and not evil.
 1192 **    May you find forgiveness for yourself and forgive others.
 1193 **    May you share freely, never taking more than you give.
 1194 **
 1195 *************************************************************************
 1196 ** This file contains code to implement most of the opendir() family of
 1197 ** POSIX functions on Win32 using the MSVCRT.
 1198 */
 1199 
 1200 #if defined(_WIN32) && defined(_MSC_VER)
 1201 /* #include "test_windirent.h" */
 1202 
 1203 /*
 1204 ** Implementation of the POSIX getenv() function using the Win32 API.
 1205 ** This function is not thread-safe.
 1206 */
 1207 const char *windirent_getenv(
 1208   const char *name
 1209 ){
 1210   static char value[32768]; /* Maximum length, per MSDN */
 1211   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
 1212   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
 1213 
 1214   memset(value, 0, sizeof(value));
 1215   dwRet = GetEnvironmentVariableA(name, value, dwSize);
 1216   if( dwRet==0 || dwRet>dwSize ){
 1217     /*
 1218     ** The function call to GetEnvironmentVariableA() failed -OR-
 1219     ** the buffer is not large enough.  Either way, return NULL.
 1220     */
 1221     return 0;
 1222   }else{
 1223     /*
 1224     ** The function call to GetEnvironmentVariableA() succeeded
 1225     ** -AND- the buffer contains the entire value.
 1226     */
 1227     return value;
 1228   }
 1229 }
 1230 
 1231 /*
 1232 ** Implementation of the POSIX opendir() function using the MSVCRT.
 1233 */
 1234 LPDIR opendir(
 1235   const char *dirname
 1236 ){
 1237   struct _finddata_t data;
 1238   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
 1239   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
 1240 
 1241   if( dirp==NULL ) return NULL;
 1242   memset(dirp, 0, sizeof(DIR));
 1243 
 1244   /* TODO: Remove this if Unix-style root paths are not used. */
 1245   if( sqlite3_stricmp(dirname, "/")==0 ){
 1246     dirname = windirent_getenv("SystemDrive");
 1247   }
 1248 
 1249   memset(&data, 0, sizeof(struct _finddata_t));
 1250   _snprintf(data.name, namesize, "%s\\*", dirname);
 1251   dirp->d_handle = _findfirst(data.name, &data);
 1252 
 1253   if( dirp->d_handle==BAD_INTPTR_T ){
 1254     closedir(dirp);
 1255     return NULL;
 1256   }
 1257 
 1258   /* TODO: Remove this block to allow hidden and/or system files. */
 1259   if( is_filtered(data) ){
 1260 next:
 1261 
 1262     memset(&data, 0, sizeof(struct _finddata_t));
 1263     if( _findnext(dirp->d_handle, &data)==-1 ){
 1264       closedir(dirp);
 1265       return NULL;
 1266     }
 1267 
 1268     /* TODO: Remove this block to allow hidden and/or system files. */
 1269     if( is_filtered(data) ) goto next;
 1270   }
 1271 
 1272   dirp->d_first.d_attributes = data.attrib;
 1273   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
 1274   dirp->d_first.d_name[NAME_MAX] = '\0';
 1275 
 1276   return dirp;
 1277 }
 1278 
 1279 /*
 1280 ** Implementation of the POSIX readdir() function using the MSVCRT.
 1281 */
 1282 LPDIRENT readdir(
 1283   LPDIR dirp
 1284 ){
 1285   struct _finddata_t data;
 1286 
 1287   if( dirp==NULL ) return NULL;
 1288 
 1289   if( dirp->d_first.d_ino==0 ){
 1290     dirp->d_first.d_ino++;
 1291     dirp->d_next.d_ino++;
 1292 
 1293     return &dirp->d_first;
 1294   }
 1295 
 1296 next:
 1297 
 1298   memset(&data, 0, sizeof(struct _finddata_t));
 1299   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
 1300 
 1301   /* TODO: Remove this block to allow hidden and/or system files. */
 1302   if( is_filtered(data) ) goto next;
 1303 
 1304   dirp->d_next.d_ino++;
 1305   dirp->d_next.d_attributes = data.attrib;
 1306   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
 1307   dirp->d_next.d_name[NAME_MAX] = '\0';
 1308 
 1309   return &dirp->d_next;
 1310 }
 1311 
 1312 /*
 1313 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
 1314 */
 1315 INT readdir_r(
 1316   LPDIR dirp,
 1317   LPDIRENT entry,
 1318   LPDIRENT *result
 1319 ){
 1320   struct _finddata_t data;
 1321 
 1322   if( dirp==NULL ) return EBADF;
 1323 
 1324   if( dirp->d_first.d_ino==0 ){
 1325     dirp->d_first.d_ino++;
 1326     dirp->d_next.d_ino++;
 1327 
 1328     entry->d_ino = dirp->d_first.d_ino;
 1329     entry->d_attributes = dirp->d_first.d_attributes;
 1330     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
 1331     entry->d_name[NAME_MAX] = '\0';
 1332 
 1333     *result = entry;
 1334     return 0;
 1335   }
 1336 
 1337 next:
 1338 
 1339   memset(&data, 0, sizeof(struct _finddata_t));
 1340   if( _findnext(dirp->d_handle, &data)==-1 ){
 1341     *result = NULL;
 1342     return ENOENT;
 1343   }
 1344 
 1345   /* TODO: Remove this block to allow hidden and/or system files. */
 1346   if( is_filtered(data) ) goto next;
 1347 
 1348   entry->d_ino = (ino_t)-1; /* not available */
 1349   entry->d_attributes = data.attrib;
 1350   strncpy(entry->d_name, data.name, NAME_MAX);
 1351   entry->d_name[NAME_MAX] = '\0';
 1352 
 1353   *result = entry;
 1354   return 0;
 1355 }
 1356 
 1357 /*
 1358 ** Implementation of the POSIX closedir() function using the MSVCRT.
 1359 */
 1360 INT closedir(
 1361   LPDIR dirp
 1362 ){
 1363   INT result = 0;
 1364 
 1365   if( dirp==NULL ) return EINVAL;
 1366 
 1367   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
 1368     result = _findclose(dirp->d_handle);
 1369   }
 1370 
 1371   sqlite3_free(dirp);
 1372   return result;
 1373 }
 1374 
 1375 #endif /* defined(WIN32) && defined(_MSC_VER) */
 1376 
 1377 /************************* End test_windirent.c ********************/
 1378 #define dirent DIRENT
 1379 #endif
 1380 /************************* Begin ../ext/misc/shathree.c ******************/
 1381 /*
 1382 ** 2017-03-08
 1383 **
 1384 ** The author disclaims copyright to this source code.  In place of
 1385 ** a legal notice, here is a blessing:
 1386 **
 1387 **    May you do good and not evil.
 1388 **    May you find forgiveness for yourself and forgive others.
 1389 **    May you share freely, never taking more than you give.
 1390 **
 1391 ******************************************************************************
 1392 **
 1393 ** This SQLite extension implements functions that compute SHA3 hashes.
 1394 ** Two SQL functions are implemented:
 1395 **
 1396 **     sha3(X,SIZE)
 1397 **     sha3_query(Y,SIZE)
 1398 **
 1399 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
 1400 ** X is NULL.
 1401 **
 1402 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
 1403 ** and returns a hash of their results.
 1404 **
 1405 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
 1406 ** is used.  If SIZE is included it must be one of the integers 224, 256,
 1407 ** 384, or 512, to determine SHA3 hash variant that is computed.
 1408 */
 1409 /* #include "sqlite3ext.h" */
 1410 SQLITE_EXTENSION_INIT1
 1411 #include <assert.h>
 1412 #include <string.h>
 1413 #include <stdarg.h>
 1414 /* typedef sqlite3_uint64 u64; */
 1415 
 1416 /******************************************************************************
 1417 ** The Hash Engine
 1418 */
 1419 /*
 1420 ** Macros to determine whether the machine is big or little endian,
 1421 ** and whether or not that determination is run-time or compile-time.
 1422 **
 1423 ** For best performance, an attempt is made to guess at the byte-order
 1424 ** using C-preprocessor macros.  If that is unsuccessful, or if
 1425 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
 1426 ** at run-time.
 1427 */
 1428 #ifndef SHA3_BYTEORDER
 1429 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 1430      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 1431      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 1432      defined(__arm__)
 1433 #   define SHA3_BYTEORDER    1234
 1434 # elif defined(sparc)    || defined(__ppc__)
 1435 #   define SHA3_BYTEORDER    4321
 1436 # else
 1437 #   define SHA3_BYTEORDER 0
 1438 # endif
 1439 #endif
 1440 
 1441 
 1442 /*
 1443 ** State structure for a SHA3 hash in progress
 1444 */
 1445 typedef struct SHA3Context SHA3Context;
 1446 struct SHA3Context {
 1447   union {
 1448     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
 1449     unsigned char x[1600];    /* ... or 1600 bytes */
 1450   } u;
 1451   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
 1452   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
 1453   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
 1454 };
 1455 
 1456 /*
 1457 ** A single step of the Keccak mixing function for a 1600-bit state
 1458 */
 1459 static void KeccakF1600Step(SHA3Context *p){
 1460   int i;
 1461   u64 b0, b1, b2, b3, b4;
 1462   u64 c0, c1, c2, c3, c4;
 1463   u64 d0, d1, d2, d3, d4;
 1464   static const u64 RC[] = {
 1465     0x0000000000000001ULL,  0x0000000000008082ULL,
 1466     0x800000000000808aULL,  0x8000000080008000ULL,
 1467     0x000000000000808bULL,  0x0000000080000001ULL,
 1468     0x8000000080008081ULL,  0x8000000000008009ULL,
 1469     0x000000000000008aULL,  0x0000000000000088ULL,
 1470     0x0000000080008009ULL,  0x000000008000000aULL,
 1471     0x000000008000808bULL,  0x800000000000008bULL,
 1472     0x8000000000008089ULL,  0x8000000000008003ULL,
 1473     0x8000000000008002ULL,  0x8000000000000080ULL,
 1474     0x000000000000800aULL,  0x800000008000000aULL,
 1475     0x8000000080008081ULL,  0x8000000000008080ULL,
 1476     0x0000000080000001ULL,  0x8000000080008008ULL
 1477   };
 1478 # define a00 (p->u.s[0])
 1479 # define a01 (p->u.s[1])
 1480 # define a02 (p->u.s[2])
 1481 # define a03 (p->u.s[3])
 1482 # define a04 (p->u.s[4])
 1483 # define a10 (p->u.s[5])
 1484 # define a11 (p->u.s[6])
 1485 # define a12 (p->u.s[7])
 1486 # define a13 (p->u.s[8])
 1487 # define a14 (p->u.s[9])
 1488 # define a20 (p->u.s[10])
 1489 # define a21 (p->u.s[11])
 1490 # define a22 (p->u.s[12])
 1491 # define a23 (p->u.s[13])
 1492 # define a24 (p->u.s[14])
 1493 # define a30 (p->u.s[15])
 1494 # define a31 (p->u.s[16])
 1495 # define a32 (p->u.s[17])
 1496 # define a33 (p->u.s[18])
 1497 # define a34 (p->u.s[19])
 1498 # define a40 (p->u.s[20])
 1499 # define a41 (p->u.s[21])
 1500 # define a42 (p->u.s[22])
 1501 # define a43 (p->u.s[23])
 1502 # define a44 (p->u.s[24])
 1503 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
 1504 
 1505   for(i=0; i<24; i+=4){
 1506     c0 = a00^a10^a20^a30^a40;
 1507     c1 = a01^a11^a21^a31^a41;
 1508     c2 = a02^a12^a22^a32^a42;
 1509     c3 = a03^a13^a23^a33^a43;
 1510     c4 = a04^a14^a24^a34^a44;
 1511     d0 = c4^ROL64(c1, 1);
 1512     d1 = c0^ROL64(c2, 1);
 1513     d2 = c1^ROL64(c3, 1);
 1514     d3 = c2^ROL64(c4, 1);
 1515     d4 = c3^ROL64(c0, 1);
 1516 
 1517     b0 = (a00^d0);
 1518     b1 = ROL64((a11^d1), 44);
 1519     b2 = ROL64((a22^d2), 43);
 1520     b3 = ROL64((a33^d3), 21);
 1521     b4 = ROL64((a44^d4), 14);
 1522     a00 =   b0 ^((~b1)&  b2 );
 1523     a00 ^= RC[i];
 1524     a11 =   b1 ^((~b2)&  b3 );
 1525     a22 =   b2 ^((~b3)&  b4 );
 1526     a33 =   b3 ^((~b4)&  b0 );
 1527     a44 =   b4 ^((~b0)&  b1 );
 1528 
 1529     b2 = ROL64((a20^d0), 3);
 1530     b3 = ROL64((a31^d1), 45);
 1531     b4 = ROL64((a42^d2), 61);
 1532     b0 = ROL64((a03^d3), 28);
 1533     b1 = ROL64((a14^d4), 20);
 1534     a20 =   b0 ^((~b1)&  b2 );
 1535     a31 =   b1 ^((~b2)&  b3 );
 1536     a42 =   b2 ^((~b3)&  b4 );
 1537     a03 =   b3 ^((~b4)&  b0 );
 1538     a14 =   b4 ^((~b0)&  b1 );
 1539 
 1540     b4 = ROL64((a40^d0), 18);
 1541     b0 = ROL64((a01^d1), 1);
 1542     b1 = ROL64((a12^d2), 6);
 1543     b2 = ROL64((a23^d3), 25);
 1544     b3 = ROL64((a34^d4), 8);
 1545     a40 =   b0 ^((~b1)&  b2 );
 1546     a01 =   b1 ^((~b2)&  b3 );
 1547     a12 =   b2 ^((~b3)&  b4 );
 1548     a23 =   b3 ^((~b4)&  b0 );
 1549     a34 =   b4 ^((~b0)&  b1 );
 1550 
 1551     b1 = ROL64((a10^d0), 36);
 1552     b2 = ROL64((a21^d1), 10);
 1553     b3 = ROL64((a32^d2), 15);
 1554     b4 = ROL64((a43^d3), 56);
 1555     b0 = ROL64((a04^d4), 27);
 1556     a10 =   b0 ^((~b1)&  b2 );
 1557     a21 =   b1 ^((~b2)&  b3 );
 1558     a32 =   b2 ^((~b3)&  b4 );
 1559     a43 =   b3 ^((~b4)&  b0 );
 1560     a04 =   b4 ^((~b0)&  b1 );
 1561 
 1562     b3 = ROL64((a30^d0), 41);
 1563     b4 = ROL64((a41^d1), 2);
 1564     b0 = ROL64((a02^d2), 62);
 1565     b1 = ROL64((a13^d3), 55);
 1566     b2 = ROL64((a24^d4), 39);
 1567     a30 =   b0 ^((~b1)&  b2 );
 1568     a41 =   b1 ^((~b2)&  b3 );
 1569     a02 =   b2 ^((~b3)&  b4 );
 1570     a13 =   b3 ^((~b4)&  b0 );
 1571     a24 =   b4 ^((~b0)&  b1 );
 1572 
 1573     c0 = a00^a20^a40^a10^a30;
 1574     c1 = a11^a31^a01^a21^a41;
 1575     c2 = a22^a42^a12^a32^a02;
 1576     c3 = a33^a03^a23^a43^a13;
 1577     c4 = a44^a14^a34^a04^a24;
 1578     d0 = c4^ROL64(c1, 1);
 1579     d1 = c0^ROL64(c2, 1);
 1580     d2 = c1^ROL64(c3, 1);
 1581     d3 = c2^ROL64(c4, 1);
 1582     d4 = c3^ROL64(c0, 1);
 1583 
 1584     b0 = (a00^d0);
 1585     b1 = ROL64((a31^d1), 44);
 1586     b2 = ROL64((a12^d2), 43);
 1587     b3 = ROL64((a43^d3), 21);
 1588     b4 = ROL64((a24^d4), 14);
 1589     a00 =   b0 ^((~b1)&  b2 );
 1590     a00 ^= RC[i+1];
 1591     a31 =   b1 ^((~b2)&  b3 );
 1592     a12 =   b2 ^((~b3)&  b4 );
 1593     a43 =   b3 ^((~b4)&  b0 );
 1594     a24 =   b4 ^((~b0)&  b1 );
 1595 
 1596     b2 = ROL64((a40^d0), 3);
 1597     b3 = ROL64((a21^d1), 45);
 1598     b4 = ROL64((a02^d2), 61);
 1599     b0 = ROL64((a33^d3), 28);
 1600     b1 = ROL64((a14^d4), 20);
 1601     a40 =   b0 ^((~b1)&  b2 );
 1602     a21 =   b1 ^((~b2)&  b3 );
 1603     a02 =   b2 ^((~b3)&  b4 );
 1604     a33 =   b3 ^((~b4)&  b0 );
 1605     a14 =   b4 ^((~b0)&  b1 );
 1606 
 1607     b4 = ROL64((a30^d0), 18);
 1608     b0 = ROL64((a11^d1), 1);
 1609     b1 = ROL64((a42^d2), 6);
 1610     b2 = ROL64((a23^d3), 25);
 1611     b3 = ROL64((a04^d4), 8);
 1612     a30 =   b0 ^((~b1)&  b2 );
 1613     a11 =   b1 ^((~b2)&  b3 );
 1614     a42 =   b2 ^((~b3)&  b4 );
 1615     a23 =   b3 ^((~b4)&  b0 );
 1616     a04 =   b4 ^((~b0)&  b1 );
 1617 
 1618     b1 = ROL64((a20^d0), 36);
 1619     b2 = ROL64((a01^d1), 10);
 1620     b3 = ROL64((a32^d2), 15);
 1621     b4 = ROL64((a13^d3), 56);
 1622     b0 = ROL64((a44^d4), 27);
 1623     a20 =   b0 ^((~b1)&  b2 );
 1624     a01 =   b1 ^((~b2)&  b3 );
 1625     a32 =   b2 ^((~b3)&  b4 );
 1626     a13 =   b3 ^((~b4)&  b0 );
 1627     a44 =   b4 ^((~b0)&  b1 );
 1628 
 1629     b3 = ROL64((a10^d0), 41);
 1630     b4 = ROL64((a41^d1), 2);
 1631     b0 = ROL64((a22^d2), 62);
 1632     b1 = ROL64((a03^d3), 55);
 1633     b2 = ROL64((a34^d4), 39);
 1634     a10 =   b0 ^((~b1)&  b2 );
 1635     a41 =   b1 ^((~b2)&  b3 );
 1636     a22 =   b2 ^((~b3)&  b4 );
 1637     a03 =   b3 ^((~b4)&  b0 );
 1638     a34 =   b4 ^((~b0)&  b1 );
 1639 
 1640     c0 = a00^a40^a30^a20^a10;
 1641     c1 = a31^a21^a11^a01^a41;
 1642     c2 = a12^a02^a42^a32^a22;
 1643     c3 = a43^a33^a23^a13^a03;
 1644     c4 = a24^a14^a04^a44^a34;
 1645     d0 = c4^ROL64(c1, 1);
 1646     d1 = c0^ROL64(c2, 1);
 1647     d2 = c1^ROL64(c3, 1);
 1648     d3 = c2^ROL64(c4, 1);
 1649     d4 = c3^ROL64(c0, 1);
 1650 
 1651     b0 = (a00^d0);
 1652     b1 = ROL64((a21^d1), 44);
 1653     b2 = ROL64((a42^d2), 43);
 1654     b3 = ROL64((a13^d3), 21);
 1655     b4 = ROL64((a34^d4), 14);
 1656     a00 =   b0 ^((~b1)&  b2 );
 1657     a00 ^= RC[i+2];
 1658     a21 =   b1 ^((~b2)&  b3 );
 1659     a42 =   b2 ^((~b3)&  b4 );
 1660     a13 =   b3 ^((~b4)&  b0 );
 1661     a34 =   b4 ^((~b0)&  b1 );
 1662 
 1663     b2 = ROL64((a30^d0), 3);
 1664     b3 = ROL64((a01^d1), 45);
 1665     b4 = ROL64((a22^d2), 61);
 1666     b0 = ROL64((a43^d3), 28);
 1667     b1 = ROL64((a14^d4), 20);
 1668     a30 =   b0 ^((~b1)&  b2 );
 1669     a01 =   b1 ^((~b2)&  b3 );
 1670     a22 =   b2 ^((~b3)&  b4 );
 1671     a43 =   b3 ^((~b4)&  b0 );
 1672     a14 =   b4 ^((~b0)&  b1 );
 1673 
 1674     b4 = ROL64((a10^d0), 18);
 1675     b0 = ROL64((a31^d1), 1);
 1676     b1 = ROL64((a02^d2), 6);
 1677     b2 = ROL64((a23^d3), 25);
 1678     b3 = ROL64((a44^d4), 8);
 1679     a10 =   b0 ^((~b1)&  b2 );
 1680     a31 =   b1 ^((~b2)&  b3 );
 1681     a02 =   b2 ^((~b3)&  b4 );
 1682     a23 =   b3 ^((~b4)&  b0 );
 1683     a44 =   b4 ^((~b0)&  b1 );
 1684 
 1685     b1 = ROL64((a40^d0), 36);
 1686     b2 = ROL64((a11^d1), 10);
 1687     b3 = ROL64((a32^d2), 15);
 1688     b4 = ROL64((a03^d3), 56);
 1689     b0 = ROL64((a24^d4), 27);
 1690     a40 =   b0 ^((~b1)&  b2 );
 1691     a11 =   b1 ^((~b2)&  b3 );
 1692     a32 =   b2 ^((~b3)&  b4 );
 1693     a03 =   b3 ^((~b4)&  b0 );
 1694     a24 =   b4 ^((~b0)&  b1 );
 1695 
 1696     b3 = ROL64((a20^d0), 41);
 1697     b4 = ROL64((a41^d1), 2);
 1698     b0 = ROL64((a12^d2), 62);
 1699     b1 = ROL64((a33^d3), 55);
 1700     b2 = ROL64((a04^d4), 39);
 1701     a20 =   b0 ^((~b1)&  b2 );
 1702     a41 =   b1 ^((~b2)&  b3 );
 1703     a12 =   b2 ^((~b3)&  b4 );
 1704     a33 =   b3 ^((~b4)&  b0 );
 1705     a04 =   b4 ^((~b0)&  b1 );
 1706 
 1707     c0 = a00^a30^a10^a40^a20;
 1708     c1 = a21^a01^a31^a11^a41;
 1709     c2 = a42^a22^a02^a32^a12;
 1710     c3 = a13^a43^a23^a03^a33;
 1711     c4 = a34^a14^a44^a24^a04;
 1712     d0 = c4^ROL64(c1, 1);
 1713     d1 = c0^ROL64(c2, 1);
 1714     d2 = c1^ROL64(c3, 1);
 1715     d3 = c2^ROL64(c4, 1);
 1716     d4 = c3^ROL64(c0, 1);
 1717 
 1718     b0 = (a00^d0);
 1719     b1 = ROL64((a01^d1), 44);
 1720     b2 = ROL64((a02^d2), 43);
 1721     b3 = ROL64((a03^d3), 21);
 1722     b4 = ROL64((a04^d4), 14);
 1723     a00 =   b0 ^((~b1)&  b2 );
 1724     a00 ^= RC[i+3];
 1725     a01 =   b1 ^((~b2)&  b3 );
 1726     a02 =   b2 ^((~b3)&  b4 );
 1727     a03 =   b3 ^((~b4)&  b0 );
 1728     a04 =   b4 ^((~b0)&  b1 );
 1729 
 1730     b2 = ROL64((a10^d0), 3);
 1731     b3 = ROL64((a11^d1), 45);
 1732     b4 = ROL64((a12^d2), 61);
 1733     b0 = ROL64((a13^d3), 28);
 1734     b1 = ROL64((a14^d4), 20);
 1735     a10 =   b0 ^((~b1)&  b2 );
 1736     a11 =   b1 ^((~b2)&  b3 );
 1737     a12 =   b2 ^((~b3)&  b4 );
 1738     a13 =   b3 ^((~b4)&  b0 );
 1739     a14 =   b4 ^((~b0)&  b1 );
 1740 
 1741     b4 = ROL64((a20^d0), 18);
 1742     b0 = ROL64((a21^d1), 1);
 1743     b1 = ROL64((a22^d2), 6);
 1744     b2 = ROL64((a23^d3), 25);
 1745     b3 = ROL64((a24^d4), 8);
 1746     a20 =   b0 ^((~b1)&  b2 );
 1747     a21 =   b1 ^((~b2)&  b3 );
 1748     a22 =   b2 ^((~b3)&  b4 );
 1749     a23 =   b3 ^((~b4)&  b0 );
 1750     a24 =   b4 ^((~b0)&  b1 );
 1751 
 1752     b1 = ROL64((a30^d0), 36);
 1753     b2 = ROL64((a31^d1), 10);
 1754     b3 = ROL64((a32^d2), 15);
 1755     b4 = ROL64((a33^d3), 56);
 1756     b0 = ROL64((a34^d4), 27);
 1757     a30 =   b0 ^((~b1)&  b2 );
 1758     a31 =   b1 ^((~b2)&  b3 );
 1759     a32 =   b2 ^((~b3)&  b4 );
 1760     a33 =   b3 ^((~b4)&  b0 );
 1761     a34 =   b4 ^((~b0)&  b1 );
 1762 
 1763     b3 = ROL64((a40^d0), 41);
 1764     b4 = ROL64((a41^d1), 2);
 1765     b0 = ROL64((a42^d2), 62);
 1766     b1 = ROL64((a43^d3), 55);
 1767     b2 = ROL64((a44^d4), 39);
 1768     a40 =   b0 ^((~b1)&  b2 );
 1769     a41 =   b1 ^((~b2)&  b3 );
 1770     a42 =   b2 ^((~b3)&  b4 );
 1771     a43 =   b3 ^((~b4)&  b0 );
 1772     a44 =   b4 ^((~b0)&  b1 );
 1773   }
 1774 }
 1775 
 1776 /*
 1777 ** Initialize a new hash.  iSize determines the size of the hash
 1778 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
 1779 ** can be zero to use the default hash size of 256 bits.
 1780 */
 1781 static void SHA3Init(SHA3Context *p, int iSize){
 1782   memset(p, 0, sizeof(*p));
 1783   if( iSize>=128 && iSize<=512 ){
 1784     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
 1785   }else{
 1786     p->nRate = (1600 - 2*256)/8;
 1787   }
 1788 #if SHA3_BYTEORDER==1234
 1789   /* Known to be little-endian at compile-time. No-op */
 1790 #elif SHA3_BYTEORDER==4321
 1791   p->ixMask = 7;  /* Big-endian */
 1792 #else
 1793   {
 1794     static unsigned int one = 1;
 1795     if( 1==*(unsigned char*)&one ){
 1796       /* Little endian.  No byte swapping. */
 1797       p->ixMask = 0;
 1798     }else{
 1799       /* Big endian.  Byte swap. */
 1800       p->ixMask = 7;
 1801     }
 1802   }
 1803 #endif
 1804 }
 1805 
 1806 /*
 1807 ** Make consecutive calls to the SHA3Update function to add new content
 1808 ** to the hash
 1809 */
 1810 static void SHA3Update(
 1811   SHA3Context *p,
 1812   const unsigned char *aData,
 1813   unsigned int nData
 1814 ){
 1815   unsigned int i = 0;
 1816 #if SHA3_BYTEORDER==1234
 1817   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
 1818     for(; i+7<nData; i+=8){
 1819       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
 1820       p->nLoaded += 8;
 1821       if( p->nLoaded>=p->nRate ){
 1822         KeccakF1600Step(p);
 1823         p->nLoaded = 0;
 1824       }
 1825     }
 1826   }
 1827 #endif
 1828   for(; i<nData; i++){
 1829 #if SHA3_BYTEORDER==1234
 1830     p->u.x[p->nLoaded] ^= aData[i];
 1831 #elif SHA3_BYTEORDER==4321
 1832     p->u.x[p->nLoaded^0x07] ^= aData[i];
 1833 #else
 1834     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
 1835 #endif
 1836     p->nLoaded++;
 1837     if( p->nLoaded==p->nRate ){
 1838       KeccakF1600Step(p);
 1839       p->nLoaded = 0;
 1840     }
 1841   }
 1842 }
 1843 
 1844 /*
 1845 ** After all content has been added, invoke SHA3Final() to compute
 1846 ** the final hash.  The function returns a pointer to the binary
 1847 ** hash value.
 1848 */
 1849 static unsigned char *SHA3Final(SHA3Context *p){
 1850   unsigned int i;
 1851   if( p->nLoaded==p->nRate-1 ){
 1852     const unsigned char c1 = 0x86;
 1853     SHA3Update(p, &c1, 1);
 1854   }else{
 1855     const unsigned char c2 = 0x06;
 1856     const unsigned char c3 = 0x80;
 1857     SHA3Update(p, &c2, 1);
 1858     p->nLoaded = p->nRate - 1;
 1859     SHA3Update(p, &c3, 1);
 1860   }
 1861   for(i=0; i<p->nRate; i++){
 1862     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
 1863   }
 1864   return &p->u.x[p->nRate];
 1865 }
 1866 /* End of the hashing logic
 1867 *****************************************************************************/
 1868 
 1869 /*
 1870 ** Implementation of the sha3(X,SIZE) function.
 1871 **
 1872 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
 1873 ** size is 256.  If X is a BLOB, it is hashed as is.  
 1874 ** For all other non-NULL types of input, X is converted into a UTF-8 string
 1875 ** and the string is hashed without the trailing 0x00 terminator.  The hash
 1876 ** of a NULL value is NULL.
 1877 */
 1878 static void sha3Func(
 1879   sqlite3_context *context,
 1880   int argc,
 1881   sqlite3_value **argv
 1882 ){
 1883   SHA3Context cx;
 1884   int eType = sqlite3_value_type(argv[0]);
 1885   int nByte = sqlite3_value_bytes(argv[0]);
 1886   int iSize;
 1887   if( argc==1 ){
 1888     iSize = 256;
 1889   }else{
 1890     iSize = sqlite3_value_int(argv[1]);
 1891     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
 1892       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
 1893                                     "384 512", -1);
 1894       return;
 1895     }
 1896   }
 1897   if( eType==SQLITE_NULL ) return;
 1898   SHA3Init(&cx, iSize);
 1899   if( eType==SQLITE_BLOB ){
 1900     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
 1901   }else{
 1902     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
 1903   }
 1904   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
 1905 }
 1906 
 1907 /* Compute a string using sqlite3_vsnprintf() with a maximum length
 1908 ** of 50 bytes and add it to the hash.
 1909 */
 1910 static void hash_step_vformat(
 1911   SHA3Context *p,                 /* Add content to this context */
 1912   const char *zFormat,
 1913   ...
 1914 ){
 1915   va_list ap;
 1916   int n;
 1917   char zBuf[50];
 1918   va_start(ap, zFormat);
 1919   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
 1920   va_end(ap);
 1921   n = (int)strlen(zBuf);
 1922   SHA3Update(p, (unsigned char*)zBuf, n);
 1923 }
 1924 
 1925 /*
 1926 ** Implementation of the sha3_query(SQL,SIZE) function.
 1927 **
 1928 ** This function compiles and runs the SQL statement(s) given in the
 1929 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
 1930 ** size is 256.
 1931 **
 1932 ** The format of the byte stream that is hashed is summarized as follows:
 1933 **
 1934 **       S<n>:<sql>
 1935 **       R
 1936 **       N
 1937 **       I<int>
 1938 **       F<ieee-float>
 1939 **       B<size>:<bytes>
 1940 **       T<size>:<text>
 1941 **
 1942 ** <sql> is the original SQL text for each statement run and <n> is
 1943 ** the size of that text.  The SQL text is UTF-8.  A single R character
 1944 ** occurs before the start of each row.  N means a NULL value.
 1945 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
 1946 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
 1947 ** B means blobs of <size> bytes.  T means text rendered as <size>
 1948 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
 1949 ** text integers.
 1950 **
 1951 ** For each SQL statement in the X input, there is one S segment.  Each
 1952 ** S segment is followed by zero or more R segments, one for each row in the
 1953 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
 1954 ** one for each column in the result set.  Segments are concatentated directly
 1955 ** with no delimiters of any kind.
 1956 */
 1957 static void sha3QueryFunc(
 1958   sqlite3_context *context,
 1959   int argc,
 1960   sqlite3_value **argv
 1961 ){
 1962   sqlite3 *db = sqlite3_context_db_handle(context);
 1963   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
 1964   sqlite3_stmt *pStmt = 0;
 1965   int nCol;                   /* Number of columns in the result set */
 1966   int i;                      /* Loop counter */
 1967   int rc;
 1968   int n;
 1969   const char *z;
 1970   SHA3Context cx;
 1971   int iSize;
 1972 
 1973   if( argc==1 ){
 1974     iSize = 256;
 1975   }else{
 1976     iSize = sqlite3_value_int(argv[1]);
 1977     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
 1978       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
 1979                                     "384 512", -1);
 1980       return;
 1981     }
 1982   }
 1983   if( zSql==0 ) return;
 1984   SHA3Init(&cx, iSize);
 1985   while( zSql[0] ){
 1986     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
 1987     if( rc ){
 1988       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
 1989                                    zSql, sqlite3_errmsg(db));
 1990       sqlite3_finalize(pStmt);
 1991       sqlite3_result_error(context, zMsg, -1);
 1992       sqlite3_free(zMsg);
 1993       return;
 1994     }
 1995     if( !sqlite3_stmt_readonly(pStmt) ){
 1996       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
 1997       sqlite3_finalize(pStmt);
 1998       sqlite3_result_error(context, zMsg, -1);
 1999       sqlite3_free(zMsg);
 2000       return;
 2001     }
 2002     nCol = sqlite3_column_count(pStmt);
 2003     z = sqlite3_sql(pStmt);
 2004     n = (int)strlen(z);
 2005     hash_step_vformat(&cx,"S%d:",n);
 2006     SHA3Update(&cx,(unsigned char*)z,n);
 2007 
 2008     /* Compute a hash over the result of the query */
 2009     while( SQLITE_ROW==sqlite3_step(pStmt) ){
 2010       SHA3Update(&cx,(const unsigned char*)"R",1);
 2011       for(i=0; i<nCol; i++){
 2012         switch( sqlite3_column_type(pStmt,i) ){
 2013           case SQLITE_NULL: {
 2014             SHA3Update(&cx, (const unsigned char*)"N",1);
 2015             break;
 2016           }
 2017           case SQLITE_INTEGER: {
 2018             sqlite3_uint64 u;
 2019             int j;
 2020             unsigned char x[9];
 2021             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
 2022             memcpy(&u, &v, 8);
 2023             for(j=8; j>=1; j--){
 2024               x[j] = u & 0xff;
 2025               u >>= 8;
 2026             }
 2027             x[0] = 'I';
 2028             SHA3Update(&cx, x, 9);
 2029             break;
 2030           }
 2031           case SQLITE_FLOAT: {
 2032             sqlite3_uint64 u;
 2033             int j;
 2034             unsigned char x[9];
 2035             double r = sqlite3_column_double(pStmt,i);
 2036             memcpy(&u, &r, 8);
 2037             for(j=8; j>=1; j--){
 2038               x[j] = u & 0xff;
 2039               u >>= 8;
 2040             }
 2041             x[0] = 'F';
 2042             SHA3Update(&cx,x,9);
 2043             break;
 2044           }
 2045           case SQLITE_TEXT: {
 2046             int n2 = sqlite3_column_bytes(pStmt, i);
 2047             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
 2048             hash_step_vformat(&cx,"T%d:",n2);
 2049             SHA3Update(&cx, z2, n2);
 2050             break;
 2051           }
 2052           case SQLITE_BLOB: {
 2053             int n2 = sqlite3_column_bytes(pStmt, i);
 2054             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
 2055             hash_step_vformat(&cx,"B%d:",n2);
 2056             SHA3Update(&cx, z2, n2);
 2057             break;
 2058           }
 2059         }
 2060       }
 2061     }
 2062     sqlite3_finalize(pStmt);
 2063   }
 2064   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
 2065 }
 2066 
 2067 
 2068 #ifdef _WIN32
 2069 
 2070 #endif
 2071 int sqlite3_shathree_init(
 2072   sqlite3 *db,
 2073   char **pzErrMsg,
 2074   const sqlite3_api_routines *pApi
 2075 ){
 2076   int rc = SQLITE_OK;
 2077   SQLITE_EXTENSION_INIT2(pApi);
 2078   (void)pzErrMsg;  /* Unused parameter */
 2079   rc = sqlite3_create_function(db, "sha3", 1,
 2080                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 2081                       0, sha3Func, 0, 0);
 2082   if( rc==SQLITE_OK ){
 2083     rc = sqlite3_create_function(db, "sha3", 2,
 2084                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 2085                       0, sha3Func, 0, 0);
 2086   }
 2087   if( rc==SQLITE_OK ){
 2088     rc = sqlite3_create_function(db, "sha3_query", 1,
 2089                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
 2090                       0, sha3QueryFunc, 0, 0);
 2091   }
 2092   if( rc==SQLITE_OK ){
 2093     rc = sqlite3_create_function(db, "sha3_query", 2,
 2094                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
 2095                       0, sha3QueryFunc, 0, 0);
 2096   }
 2097   return rc;
 2098 }
 2099 
 2100 /************************* End ../ext/misc/shathree.c ********************/
 2101 /************************* Begin ../ext/misc/fileio.c ******************/
 2102 /*
 2103 ** 2014-06-13
 2104 **
 2105 ** The author disclaims copyright to this source code.  In place of
 2106 ** a legal notice, here is a blessing:
 2107 **
 2108 **    May you do good and not evil.
 2109 **    May you find forgiveness for yourself and forgive others.
 2110 **    May you share freely, never taking more than you give.
 2111 **
 2112 ******************************************************************************
 2113 **
 2114 ** This SQLite extension implements SQL functions readfile() and
 2115 ** writefile(), and eponymous virtual type "fsdir".
 2116 **
 2117 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
 2118 **
 2119 **   If neither of the optional arguments is present, then this UDF
 2120 **   function writes blob DATA to file FILE. If successful, the number
 2121 **   of bytes written is returned. If an error occurs, NULL is returned.
 2122 **
 2123 **   If the first option argument - MODE - is present, then it must
 2124 **   be passed an integer value that corresponds to a POSIX mode
 2125 **   value (file type + permissions, as returned in the stat.st_mode
 2126 **   field by the stat() system call). Three types of files may
 2127 **   be written/created:
 2128 **
 2129 **     regular files:  (mode & 0170000)==0100000
 2130 **     symbolic links: (mode & 0170000)==0120000
 2131 **     directories:    (mode & 0170000)==0040000
 2132 **
 2133 **   For a directory, the DATA is ignored. For a symbolic link, it is
 2134 **   interpreted as text and used as the target of the link. For a
 2135 **   regular file, it is interpreted as a blob and written into the
 2136 **   named file. Regardless of the type of file, its permissions are
 2137 **   set to (mode & 0777) before returning.
 2138 **
 2139 **   If the optional MTIME argument is present, then it is interpreted
 2140 **   as an integer - the number of seconds since the unix epoch. The
 2141 **   modification-time of the target file is set to this value before
 2142 **   returning.
 2143 **
 2144 **   If three or more arguments are passed to this function and an
 2145 **   error is encountered, an exception is raised.
 2146 **
 2147 ** READFILE(FILE):
 2148 **
 2149 **   Read and return the contents of file FILE (type blob) from disk.
 2150 **
 2151 ** FSDIR:
 2152 **
 2153 **   Used as follows:
 2154 **
 2155 **     SELECT * FROM fsdir($path [, $dir]);
 2156 **
 2157 **   Parameter $path is an absolute or relative pathname. If the file that it
 2158 **   refers to does not exist, it is an error. If the path refers to a regular
 2159 **   file or symbolic link, it returns a single row. Or, if the path refers
 2160 **   to a directory, it returns one row for the directory, and one row for each
 2161 **   file within the hierarchy rooted at $path.
 2162 **
 2163 **   Each row has the following columns:
 2164 **
 2165 **     name:  Path to file or directory (text value).
 2166 **     mode:  Value of stat.st_mode for directory entry (an integer).
 2167 **     mtime: Value of stat.st_mtime for directory entry (an integer).
 2168 **     data:  For a regular file, a blob containing the file data. For a
 2169 **            symlink, a text value containing the text of the link. For a
 2170 **            directory, NULL.
 2171 **
 2172 **   If a non-NULL value is specified for the optional $dir parameter and
 2173 **   $path is a relative path, then $path is interpreted relative to $dir. 
 2174 **   And the paths returned in the "name" column of the table are also 
 2175 **   relative to directory $dir.
 2176 */
 2177 /* #include "sqlite3ext.h" */
 2178 SQLITE_EXTENSION_INIT1
 2179 #include <stdio.h>
 2180 #include <string.h>
 2181 #include <assert.h>
 2182 
 2183 #include <sys/types.h>
 2184 #include <sys/stat.h>
 2185 #include <fcntl.h>
 2186 #if !defined(_WIN32) && !defined(WIN32)
 2187 #  include <unistd.h>
 2188 #  include <dirent.h>
 2189 #  include <utime.h>
 2190 #  include <sys/time.h>
 2191 #else
 2192 #  include "windows.h"
 2193 #  include <io.h>
 2194 #  include <direct.h>
 2195 /* #  include "test_windirent.h" */
 2196 #  define dirent DIRENT
 2197 #  ifndef chmod
 2198 #    define chmod _chmod
 2199 #  endif
 2200 #  ifndef stat
 2201 #    define stat _stat
 2202 #  endif
 2203 #  define mkdir(path,mode) _mkdir(path)
 2204 #  define lstat(path,buf) stat(path,buf)
 2205 #endif
 2206 #include <time.h>
 2207 #include <errno.h>
 2208 
 2209 
 2210 /*
 2211 ** Structure of the fsdir() table-valued function
 2212 */
 2213                  /*    0    1    2     3    4           5             */
 2214 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
 2215 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
 2216 #define FSDIR_COLUMN_MODE     1     /* Access mode */
 2217 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
 2218 #define FSDIR_COLUMN_DATA     3     /* File content */
 2219 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
 2220 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
 2221 
 2222 
 2223 /*
 2224 ** Set the result stored by context ctx to a blob containing the 
 2225 ** contents of file zName.  Or, leave the result unchanged (NULL)
 2226 ** if the file does not exist or is unreadable.
 2227 **
 2228 ** If the file exceeds the SQLite blob size limit, through an
 2229 ** SQLITE_TOOBIG error.
 2230 **
 2231 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
 2232 ** off of disk.
 2233 */
 2234 static void readFileContents(sqlite3_context *ctx, const char *zName){
 2235   FILE *in;
 2236   sqlite3_int64 nIn;
 2237   void *pBuf;
 2238   sqlite3 *db;
 2239   int mxBlob;
 2240 
 2241   in = fopen(zName, "rb");
 2242   if( in==0 ){
 2243     /* File does not exist or is unreadable. Leave the result set to NULL. */
 2244     return;
 2245   }
 2246   fseek(in, 0, SEEK_END);
 2247   nIn = ftell(in);
 2248   rewind(in);
 2249   db = sqlite3_context_db_handle(ctx);
 2250   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
 2251   if( nIn>mxBlob ){
 2252     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
 2253     fclose(in);
 2254     return;
 2255   }
 2256   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
 2257   if( pBuf==0 ){
 2258     sqlite3_result_error_nomem(ctx);
 2259     fclose(in);
 2260     return;
 2261   }
 2262   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
 2263     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
 2264   }else{
 2265     sqlite3_result_error_code(ctx, SQLITE_IOERR);
 2266     sqlite3_free(pBuf);
 2267   }
 2268   fclose(in);
 2269 }
 2270 
 2271 /*
 2272 ** Implementation of the "readfile(X)" SQL function.  The entire content
 2273 ** of the file named X is read and returned as a BLOB.  NULL is returned
 2274 ** if the file does not exist or is unreadable.
 2275 */
 2276 static void readfileFunc(
 2277   sqlite3_context *context,
 2278   int argc,
 2279   sqlite3_value **argv
 2280 ){
 2281   const char *zName;
 2282   (void)(argc);  /* Unused parameter */
 2283   zName = (const char*)sqlite3_value_text(argv[0]);
 2284   if( zName==0 ) return;
 2285   readFileContents(context, zName);
 2286 }
 2287 
 2288 /*
 2289 ** Set the error message contained in context ctx to the results of
 2290 ** vprintf(zFmt, ...).
 2291 */
 2292 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
 2293   char *zMsg = 0;
 2294   va_list ap;
 2295   va_start(ap, zFmt);
 2296   zMsg = sqlite3_vmprintf(zFmt, ap);
 2297   sqlite3_result_error(ctx, zMsg, -1);
 2298   sqlite3_free(zMsg);
 2299   va_end(ap);
 2300 }
 2301 
 2302 #if defined(_WIN32)
 2303 /*
 2304 ** This function is designed to convert a Win32 FILETIME structure into the
 2305 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
 2306 */
 2307 static sqlite3_uint64 fileTimeToUnixTime(
 2308   LPFILETIME pFileTime
 2309 ){
 2310   SYSTEMTIME epochSystemTime;
 2311   ULARGE_INTEGER epochIntervals;
 2312   FILETIME epochFileTime;
 2313   ULARGE_INTEGER fileIntervals;
 2314 
 2315   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
 2316   epochSystemTime.wYear = 1970;
 2317   epochSystemTime.wMonth = 1;
 2318   epochSystemTime.wDay = 1;
 2319   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
 2320   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
 2321   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
 2322 
 2323   fileIntervals.LowPart = pFileTime->dwLowDateTime;
 2324   fileIntervals.HighPart = pFileTime->dwHighDateTime;
 2325 
 2326   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
 2327 }
 2328 
 2329 /*
 2330 ** This function attempts to normalize the time values found in the stat()
 2331 ** buffer to UTC.  This is necessary on Win32, where the runtime library
 2332 ** appears to return these values as local times.
 2333 */
 2334 static void statTimesToUtc(
 2335   const char *zPath,
 2336   struct stat *pStatBuf
 2337 ){
 2338   HANDLE hFindFile;
 2339   WIN32_FIND_DATAW fd;
 2340   LPWSTR zUnicodeName;
 2341   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
 2342   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
 2343   if( zUnicodeName ){
 2344     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
 2345     hFindFile = FindFirstFileW(zUnicodeName, &fd);
 2346     if( hFindFile!=NULL ){
 2347       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
 2348       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
 2349       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
 2350       FindClose(hFindFile);
 2351     }
 2352     sqlite3_free(zUnicodeName);
 2353   }
 2354 }
 2355 #endif
 2356 
 2357 /*
 2358 ** This function is used in place of stat().  On Windows, special handling
 2359 ** is required in order for the included time to be returned as UTC.  On all
 2360 ** other systems, this function simply calls stat().
 2361 */
 2362 static int fileStat(
 2363   const char *zPath,
 2364   struct stat *pStatBuf
 2365 ){
 2366 #if defined(_WIN32)
 2367   int rc = stat(zPath, pStatBuf);
 2368   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
 2369   return rc;
 2370 #else
 2371   return stat(zPath, pStatBuf);
 2372 #endif
 2373 }
 2374 
 2375 /*
 2376 ** This function is used in place of lstat().  On Windows, special handling
 2377 ** is required in order for the included time to be returned as UTC.  On all
 2378 ** other systems, this function simply calls lstat().
 2379 */
 2380 static int fileLinkStat(
 2381   const char *zPath,
 2382   struct stat *pStatBuf
 2383 ){
 2384 #if defined(_WIN32)
 2385   int rc = lstat(zPath, pStatBuf);
 2386   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
 2387   return rc;
 2388 #else
 2389   return lstat(zPath, pStatBuf);
 2390 #endif
 2391 }
 2392 
 2393 /*
 2394 ** Argument zFile is the name of a file that will be created and/or written
 2395 ** by SQL function writefile(). This function ensures that the directory
 2396 ** zFile will be written to exists, creating it if required. The permissions
 2397 ** for any path components created by this function are set in accordance
 2398 ** with the current umask.
 2399 **
 2400 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
 2401 ** SQLITE_OK is returned if the directory is successfully created, or
 2402 ** SQLITE_ERROR otherwise.
 2403 */
 2404 static int makeDirectory(
 2405   const char *zFile
 2406 ){
 2407   char *zCopy = sqlite3_mprintf("%s", zFile);
 2408   int rc = SQLITE_OK;
 2409 
 2410   if( zCopy==0 ){
 2411     rc = SQLITE_NOMEM;
 2412   }else{
 2413     int nCopy = (int)strlen(zCopy);
 2414     int i = 1;
 2415 
 2416     while( rc==SQLITE_OK ){
 2417       struct stat sStat;
 2418       int rc2;
 2419 
 2420       for(; zCopy[i]!='/' && i<nCopy; i++);
 2421       if( i==nCopy ) break;
 2422       zCopy[i] = '\0';
 2423 
 2424       rc2 = fileStat(zCopy, &sStat);
 2425       if( rc2!=0 ){
 2426         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
 2427       }else{
 2428         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
 2429       }
 2430       zCopy[i] = '/';
 2431       i++;
 2432     }
 2433 
 2434     sqlite3_free(zCopy);
 2435   }
 2436 
 2437   return rc;
 2438 }
 2439 
 2440 /*
 2441 ** This function does the work for the writefile() UDF. Refer to 
 2442 ** header comments at the top of this file for details.
 2443 */
 2444 static int writeFile(
 2445   sqlite3_context *pCtx,          /* Context to return bytes written in */
 2446   const char *zFile,              /* File to write */
 2447   sqlite3_value *pData,           /* Data to write */
 2448   mode_t mode,                    /* MODE parameter passed to writefile() */
 2449   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
 2450 ){
 2451 #if !defined(_WIN32) && !defined(WIN32)
 2452   if( S_ISLNK(mode) ){
 2453     const char *zTo = (const char*)sqlite3_value_text(pData);
 2454     if( symlink(zTo, zFile)<0 ) return 1;
 2455   }else
 2456 #endif
 2457   {
 2458     if( S_ISDIR(mode) ){
 2459       if( mkdir(zFile, mode) ){
 2460         /* The mkdir() call to create the directory failed. This might not
 2461         ** be an error though - if there is already a directory at the same
 2462         ** path and either the permissions already match or can be changed
 2463         ** to do so using chmod(), it is not an error.  */
 2464         struct stat sStat;
 2465         if( errno!=EEXIST
 2466          || 0!=fileStat(zFile, &sStat)
 2467          || !S_ISDIR(sStat.st_mode)
 2468          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
 2469         ){
 2470           return 1;
 2471         }
 2472       }
 2473     }else{
 2474       sqlite3_int64 nWrite = 0;
 2475       const char *z;
 2476       int rc = 0;
 2477       FILE *out = fopen(zFile, "wb");
 2478       if( out==0 ) return 1;
 2479       z = (const char*)sqlite3_value_blob(pData);
 2480       if( z ){
 2481         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
 2482         nWrite = sqlite3_value_bytes(pData);
 2483         if( nWrite!=n ){
 2484           rc = 1;
 2485         }
 2486       }
 2487       fclose(out);
 2488       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
 2489         rc = 1;
 2490       }
 2491       if( rc ) return 2;
 2492       sqlite3_result_int64(pCtx, nWrite);
 2493     }
 2494   }
 2495 
 2496   if( mtime>=0 ){
 2497 #if defined(_WIN32)
 2498 #if !SQLITE_OS_WINRT
 2499     /* Windows */
 2500     FILETIME lastAccess;
 2501     FILETIME lastWrite;
 2502     SYSTEMTIME currentTime;
 2503     LONGLONG intervals;
 2504     HANDLE hFile;
 2505     LPWSTR zUnicodeName;
 2506     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
 2507 
 2508     GetSystemTime(&currentTime);
 2509     SystemTimeToFileTime(&currentTime, &lastAccess);
 2510     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
 2511     lastWrite.dwLowDateTime = (DWORD)intervals;
 2512     lastWrite.dwHighDateTime = intervals >> 32;
 2513     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
 2514     if( zUnicodeName==0 ){
 2515       return 1;
 2516     }
 2517     hFile = CreateFileW(
 2518       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
 2519       FILE_FLAG_BACKUP_SEMANTICS, NULL
 2520     );
 2521     sqlite3_free(zUnicodeName);
 2522     if( hFile!=INVALID_HANDLE_VALUE ){
 2523       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
 2524       CloseHandle(hFile);
 2525       return !bResult;
 2526     }else{
 2527       return 1;
 2528     }
 2529 #endif
 2530 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
 2531     /* Recent unix */
 2532     struct timespec times[2];
 2533     times[0].tv_nsec = times[1].tv_nsec = 0;
 2534     times[0].tv_sec = time(0);
 2535     times[1].tv_sec = mtime;
 2536     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
 2537       return 1;
 2538     }
 2539 #else
 2540     /* Legacy unix */
 2541     struct timeval times[2];
 2542     times[0].tv_usec = times[1].tv_usec = 0;
 2543     times[0].tv_sec = time(0);
 2544     times[1].tv_sec = mtime;
 2545     if( utimes(zFile, times) ){
 2546       return 1;
 2547     }
 2548 #endif
 2549   }
 2550 
 2551   return 0;
 2552 }
 2553 
 2554 /*
 2555 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
 2556 ** Refer to header comments at the top of this file for details.
 2557 */
 2558 static void writefileFunc(
 2559   sqlite3_context *context,
 2560   int argc,
 2561   sqlite3_value **argv
 2562 ){
 2563   const char *zFile;
 2564   mode_t mode = 0;
 2565   int res;
 2566   sqlite3_int64 mtime = -1;
 2567 
 2568   if( argc<2 || argc>4 ){
 2569     sqlite3_result_error(context, 
 2570         "wrong number of arguments to function writefile()", -1
 2571     );
 2572     return;
 2573   }
 2574 
 2575   zFile = (const char*)sqlite3_value_text(argv[0]);
 2576   if( zFile==0 ) return;
 2577   if( argc>=3 ){
 2578     mode = (mode_t)sqlite3_value_int(argv[2]);
 2579   }
 2580   if( argc==4 ){
 2581     mtime = sqlite3_value_int64(argv[3]);
 2582   }
 2583 
 2584   res = writeFile(context, zFile, argv[1], mode, mtime);
 2585   if( res==1 && errno==ENOENT ){
 2586     if( makeDirectory(zFile)==SQLITE_OK ){
 2587       res = writeFile(context, zFile, argv[1], mode, mtime);
 2588     }
 2589   }
 2590 
 2591   if( argc>2 && res!=0 ){
 2592     if( S_ISLNK(mode) ){
 2593       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
 2594     }else if( S_ISDIR(mode) ){
 2595       ctxErrorMsg(context, "failed to create directory: %s", zFile);
 2596     }else{
 2597       ctxErrorMsg(context, "failed to write file: %s", zFile);
 2598     }
 2599   }
 2600 }
 2601 
 2602 /*
 2603 ** SQL function:   lsmode(MODE)
 2604 **
 2605 ** Given a numberic st_mode from stat(), convert it into a human-readable
 2606 ** text string in the style of "ls -l".
 2607 */
 2608 static void lsModeFunc(
 2609   sqlite3_context *context,
 2610   int argc,
 2611   sqlite3_value **argv
 2612 ){
 2613   int i;
 2614   int iMode = sqlite3_value_int(argv[0]);
 2615   char z[16];
 2616   (void)argc;
 2617   if( S_ISLNK(iMode) ){
 2618     z[0] = 'l';
 2619   }else if( S_ISREG(iMode) ){
 2620     z[0] = '-';
 2621   }else if( S_ISDIR(iMode) ){
 2622     z[0] = 'd';
 2623   }else{
 2624     z[0] = '?';
 2625   }
 2626   for(i=0; i<3; i++){
 2627     int m = (iMode >> ((2-i)*3));
 2628     char *a = &z[1 + i*3];
 2629     a[0] = (m & 0x4) ? 'r' : '-';
 2630     a[1] = (m & 0x2) ? 'w' : '-';
 2631     a[2] = (m & 0x1) ? 'x' : '-';
 2632   }
 2633   z[10] = '\0';
 2634   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
 2635 }
 2636 
 2637 #ifndef SQLITE_OMIT_VIRTUALTABLE
 2638 
 2639 /* 
 2640 ** Cursor type for recursively iterating through a directory structure.
 2641 */
 2642 typedef struct fsdir_cursor fsdir_cursor;
 2643 typedef struct FsdirLevel FsdirLevel;
 2644 
 2645 struct FsdirLevel {
 2646   DIR *pDir;                 /* From opendir() */
 2647   char *zDir;                /* Name of directory (nul-terminated) */
 2648 };
 2649 
 2650 struct fsdir_cursor {
 2651   sqlite3_vtab_cursor base;  /* Base class - must be first */
 2652 
 2653   int nLvl;                  /* Number of entries in aLvl[] array */
 2654   int iLvl;                  /* Index of current entry */
 2655   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
 2656 
 2657   const char *zBase;
 2658   int nBase;
 2659 
 2660   struct stat sStat;         /* Current lstat() results */
 2661   char *zPath;               /* Path to current entry */
 2662   sqlite3_int64 iRowid;      /* Current rowid */
 2663 };
 2664 
 2665 typedef struct fsdir_tab fsdir_tab;
 2666 struct fsdir_tab {
 2667   sqlite3_vtab base;         /* Base class - must be first */
 2668 };
 2669 
 2670 /*
 2671 ** Construct a new fsdir virtual table object.
 2672 */
 2673 static int fsdirConnect(
 2674   sqlite3 *db,
 2675   void *pAux,
 2676   int argc, const char *const*argv,
 2677   sqlite3_vtab **ppVtab,
 2678   char **pzErr
 2679 ){
 2680   fsdir_tab *pNew = 0;
 2681   int rc;
 2682   (void)pAux;
 2683   (void)argc;
 2684   (void)argv;
 2685   (void)pzErr;
 2686   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
 2687   if( rc==SQLITE_OK ){
 2688     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
 2689     if( pNew==0 ) return SQLITE_NOMEM;
 2690     memset(pNew, 0, sizeof(*pNew));
 2691     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
 2692   }
 2693   *ppVtab = (sqlite3_vtab*)pNew;
 2694   return rc;
 2695 }
 2696 
 2697 /*
 2698 ** This method is the destructor for fsdir vtab objects.
 2699 */
 2700 static int fsdirDisconnect(sqlite3_vtab *pVtab){
 2701   sqlite3_free(pVtab);
 2702   return SQLITE_OK;
 2703 }
 2704 
 2705 /*
 2706 ** Constructor for a new fsdir_cursor object.
 2707 */
 2708 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
 2709   fsdir_cursor *pCur;
 2710   (void)p;
 2711   pCur = sqlite3_malloc( sizeof(*pCur) );
 2712   if( pCur==0 ) return SQLITE_NOMEM;
 2713   memset(pCur, 0, sizeof(*pCur));
 2714   pCur->iLvl = -1;
 2715   *ppCursor = &pCur->base;
 2716   return SQLITE_OK;
 2717 }
 2718 
 2719 /*
 2720 ** Reset a cursor back to the state it was in when first returned
 2721 ** by fsdirOpen().
 2722 */
 2723 static void fsdirResetCursor(fsdir_cursor *pCur){
 2724   int i;
 2725   for(i=0; i<=pCur->iLvl; i++){
 2726     FsdirLevel *pLvl = &pCur->aLvl[i];
 2727     if( pLvl->pDir ) closedir(pLvl->pDir);
 2728     sqlite3_free(pLvl->zDir);
 2729   }
 2730   sqlite3_free(pCur->zPath);
 2731   sqlite3_free(pCur->aLvl);
 2732   pCur->aLvl = 0;
 2733   pCur->zPath = 0;
 2734   pCur->zBase = 0;
 2735   pCur->nBase = 0;
 2736   pCur->nLvl = 0;
 2737   pCur->iLvl = -1;
 2738   pCur->iRowid = 1;
 2739 }
 2740 
 2741 /*
 2742 ** Destructor for an fsdir_cursor.
 2743 */
 2744 static int fsdirClose(sqlite3_vtab_cursor *cur){
 2745   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2746 
 2747   fsdirResetCursor(pCur);
 2748   sqlite3_free(pCur);
 2749   return SQLITE_OK;
 2750 }
 2751 
 2752 /*
 2753 ** Set the error message for the virtual table associated with cursor
 2754 ** pCur to the results of vprintf(zFmt, ...).
 2755 */
 2756 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
 2757   va_list ap;
 2758   va_start(ap, zFmt);
 2759   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
 2760   va_end(ap);
 2761 }
 2762 
 2763 
 2764 /*
 2765 ** Advance an fsdir_cursor to its next row of output.
 2766 */
 2767 static int fsdirNext(sqlite3_vtab_cursor *cur){
 2768   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2769   mode_t m = pCur->sStat.st_mode;
 2770 
 2771   pCur->iRowid++;
 2772   if( S_ISDIR(m) ){
 2773     /* Descend into this directory */
 2774     int iNew = pCur->iLvl + 1;
 2775     FsdirLevel *pLvl;
 2776     if( iNew>=pCur->nLvl ){
 2777       int nNew = iNew+1;
 2778       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
 2779       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
 2780       if( aNew==0 ) return SQLITE_NOMEM;
 2781       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
 2782       pCur->aLvl = aNew;
 2783       pCur->nLvl = nNew;
 2784     }
 2785     pCur->iLvl = iNew;
 2786     pLvl = &pCur->aLvl[iNew];
 2787     
 2788     pLvl->zDir = pCur->zPath;
 2789     pCur->zPath = 0;
 2790     pLvl->pDir = opendir(pLvl->zDir);
 2791     if( pLvl->pDir==0 ){
 2792       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
 2793       return SQLITE_ERROR;
 2794     }
 2795   }
 2796 
 2797   while( pCur->iLvl>=0 ){
 2798     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
 2799     struct dirent *pEntry = readdir(pLvl->pDir);
 2800     if( pEntry ){
 2801       if( pEntry->d_name[0]=='.' ){
 2802        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
 2803        if( pEntry->d_name[1]=='\0' ) continue;
 2804       }
 2805       sqlite3_free(pCur->zPath);
 2806       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
 2807       if( pCur->zPath==0 ) return SQLITE_NOMEM;
 2808       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
 2809         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
 2810         return SQLITE_ERROR;
 2811       }
 2812       return SQLITE_OK;
 2813     }
 2814     closedir(pLvl->pDir);
 2815     sqlite3_free(pLvl->zDir);
 2816     pLvl->pDir = 0;
 2817     pLvl->zDir = 0;
 2818     pCur->iLvl--;
 2819   }
 2820 
 2821   /* EOF */
 2822   sqlite3_free(pCur->zPath);
 2823   pCur->zPath = 0;
 2824   return SQLITE_OK;
 2825 }
 2826 
 2827 /*
 2828 ** Return values of columns for the row at which the series_cursor
 2829 ** is currently pointing.
 2830 */
 2831 static int fsdirColumn(
 2832   sqlite3_vtab_cursor *cur,   /* The cursor */
 2833   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
 2834   int i                       /* Which column to return */
 2835 ){
 2836   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2837   switch( i ){
 2838     case FSDIR_COLUMN_NAME: {
 2839       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
 2840       break;
 2841     }
 2842 
 2843     case FSDIR_COLUMN_MODE:
 2844       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
 2845       break;
 2846 
 2847     case FSDIR_COLUMN_MTIME:
 2848       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
 2849       break;
 2850 
 2851     case FSDIR_COLUMN_DATA: {
 2852       mode_t m = pCur->sStat.st_mode;
 2853       if( S_ISDIR(m) ){
 2854         sqlite3_result_null(ctx);
 2855 #if !defined(_WIN32) && !defined(WIN32)
 2856       }else if( S_ISLNK(m) ){
 2857         char aStatic[64];
 2858         char *aBuf = aStatic;
 2859         sqlite3_int64 nBuf = 64;
 2860         int n;
 2861 
 2862         while( 1 ){
 2863           n = readlink(pCur->zPath, aBuf, nBuf);
 2864           if( n<nBuf ) break;
 2865           if( aBuf!=aStatic ) sqlite3_free(aBuf);
 2866           nBuf = nBuf*2;
 2867           aBuf = sqlite3_malloc64(nBuf);
 2868           if( aBuf==0 ){
 2869             sqlite3_result_error_nomem(ctx);
 2870             return SQLITE_NOMEM;
 2871           }
 2872         }
 2873 
 2874         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
 2875         if( aBuf!=aStatic ) sqlite3_free(aBuf);
 2876 #endif
 2877       }else{
 2878         readFileContents(ctx, pCur->zPath);
 2879       }
 2880     }
 2881     case FSDIR_COLUMN_PATH:
 2882     default: {
 2883       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
 2884       ** always return their values as NULL */
 2885       break;
 2886     }
 2887   }
 2888   return SQLITE_OK;
 2889 }
 2890 
 2891 /*
 2892 ** Return the rowid for the current row. In this implementation, the
 2893 ** first row returned is assigned rowid value 1, and each subsequent
 2894 ** row a value 1 more than that of the previous.
 2895 */
 2896 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
 2897   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2898   *pRowid = pCur->iRowid;
 2899   return SQLITE_OK;
 2900 }
 2901 
 2902 /*
 2903 ** Return TRUE if the cursor has been moved off of the last
 2904 ** row of output.
 2905 */
 2906 static int fsdirEof(sqlite3_vtab_cursor *cur){
 2907   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2908   return (pCur->zPath==0);
 2909 }
 2910 
 2911 /*
 2912 ** xFilter callback.
 2913 **
 2914 ** idxNum==1   PATH parameter only
 2915 ** idxNum==2   Both PATH and DIR supplied
 2916 */
 2917 static int fsdirFilter(
 2918   sqlite3_vtab_cursor *cur, 
 2919   int idxNum, const char *idxStr,
 2920   int argc, sqlite3_value **argv
 2921 ){
 2922   const char *zDir = 0;
 2923   fsdir_cursor *pCur = (fsdir_cursor*)cur;
 2924   (void)idxStr;
 2925   fsdirResetCursor(pCur);
 2926 
 2927   if( idxNum==0 ){
 2928     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
 2929     return SQLITE_ERROR;
 2930   }
 2931 
 2932   assert( argc==idxNum && (argc==1 || argc==2) );
 2933   zDir = (const char*)sqlite3_value_text(argv[0]);
 2934   if( zDir==0 ){
 2935     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
 2936     return SQLITE_ERROR;
 2937   }
 2938   if( argc==2 ){
 2939     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
 2940   }
 2941   if( pCur->zBase ){
 2942     pCur->nBase = (int)strlen(pCur->zBase)+1;
 2943     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
 2944   }else{
 2945     pCur->zPath = sqlite3_mprintf("%s", zDir);
 2946   }
 2947 
 2948   if( pCur->zPath==0 ){
 2949     return SQLITE_NOMEM;
 2950   }
 2951   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
 2952     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
 2953     return SQLITE_ERROR;
 2954   }
 2955 
 2956   return SQLITE_OK;
 2957 }
 2958 
 2959 /*
 2960 ** SQLite will invoke this method one or more times while planning a query
 2961 ** that uses the generate_series virtual table.  This routine needs to create
 2962 ** a query plan for each invocation and compute an estimated cost for that
 2963 ** plan.
 2964 **
 2965 ** In this implementation idxNum is used to represent the
 2966 ** query plan.  idxStr is unused.
 2967 **
 2968 ** The query plan is represented by values of idxNum:
 2969 **
 2970 **  (1)  The path value is supplied by argv[0]
 2971 **  (2)  Path is in argv[0] and dir is in argv[1]
 2972 */
 2973 static int fsdirBestIndex(
 2974   sqlite3_vtab *tab,
 2975   sqlite3_index_info *pIdxInfo
 2976 ){
 2977   int i;                 /* Loop over constraints */
 2978   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
 2979   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
 2980   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
 2981   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
 2982   const struct sqlite3_index_constraint *pConstraint;
 2983 
 2984   (void)tab;
 2985   pConstraint = pIdxInfo->aConstraint;
 2986   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
 2987     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
 2988     switch( pConstraint->iColumn ){
 2989       case FSDIR_COLUMN_PATH: {
 2990         if( pConstraint->usable ){
 2991           idxPath = i;
 2992           seenPath = 0;
 2993         }else if( idxPath<0 ){
 2994           seenPath = 1;
 2995         }
 2996         break;
 2997       }
 2998       case FSDIR_COLUMN_DIR: {
 2999         if( pConstraint->usable ){
 3000           idxDir = i;
 3001           seenDir = 0;
 3002         }else if( idxDir<0 ){
 3003           seenDir = 1;
 3004         }
 3005         break;
 3006       }
 3007     } 
 3008   }
 3009   if( seenPath || seenDir ){
 3010     /* If input parameters are unusable, disallow this plan */
 3011     return SQLITE_CONSTRAINT;
 3012   }
 3013 
 3014   if( idxPath<0 ){
 3015     pIdxInfo->idxNum = 0;
 3016     /* The pIdxInfo->estimatedCost should have been initialized to a huge
 3017     ** number.  Leave it unchanged. */
 3018     pIdxInfo->estimatedRows = 0x7fffffff;
 3019   }else{
 3020     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
 3021     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
 3022     if( idxDir>=0 ){
 3023       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
 3024       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
 3025       pIdxInfo->idxNum = 2;
 3026       pIdxInfo->estimatedCost = 10.0;
 3027     }else{
 3028       pIdxInfo->idxNum = 1;
 3029       pIdxInfo->estimatedCost = 100.0;
 3030     }
 3031   }
 3032 
 3033   return SQLITE_OK;
 3034 }
 3035 
 3036 /*
 3037 ** Register the "fsdir" virtual table.
 3038 */
 3039 static int fsdirRegister(sqlite3 *db){
 3040   static sqlite3_module fsdirModule = {
 3041     0,                         /* iVersion */
 3042     0,                         /* xCreate */
 3043     fsdirConnect,              /* xConnect */
 3044     fsdirBestIndex,            /* xBestIndex */
 3045     fsdirDisconnect,           /* xDisconnect */
 3046     0,                         /* xDestroy */
 3047     fsdirOpen,                 /* xOpen - open a cursor */
 3048     fsdirClose,                /* xClose - close a cursor */
 3049     fsdirFilter,               /* xFilter - configure scan constraints */
 3050     fsdirNext,                 /* xNext - advance a cursor */
 3051     fsdirEof,                  /* xEof - check for end of scan */
 3052     fsdirColumn,               /* xColumn - read data */
 3053     fsdirRowid,                /* xRowid - read data */
 3054     0,                         /* xUpdate */
 3055     0,                         /* xBegin */
 3056     0,                         /* xSync */
 3057     0,                         /* xCommit */
 3058     0,                         /* xRollback */
 3059     0,                         /* xFindMethod */
 3060     0,                         /* xRename */
 3061     0,                         /* xSavepoint */
 3062     0,                         /* xRelease */
 3063     0,                         /* xRollbackTo */
 3064     0,                         /* xShadowName */
 3065   };
 3066 
 3067   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
 3068   return rc;
 3069 }
 3070 #else         /* SQLITE_OMIT_VIRTUALTABLE */
 3071 # define fsdirRegister(x) SQLITE_OK
 3072 #endif
 3073 
 3074 #ifdef _WIN32
 3075 
 3076 #endif
 3077 int sqlite3_fileio_init(
 3078   sqlite3 *db, 
 3079   char **pzErrMsg, 
 3080   const sqlite3_api_routines *pApi
 3081 ){
 3082   int rc = SQLITE_OK;
 3083   SQLITE_EXTENSION_INIT2(pApi);
 3084   (void)pzErrMsg;  /* Unused parameter */
 3085   rc = sqlite3_create_function(db, "readfile", 1, 
 3086                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
 3087                                readfileFunc, 0, 0);
 3088   if( rc==SQLITE_OK ){
 3089     rc = sqlite3_create_function(db, "writefile", -1,
 3090                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
 3091                                  writefileFunc, 0, 0);
 3092   }
 3093   if( rc==SQLITE_OK ){
 3094     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
 3095                                  lsModeFunc, 0, 0);
 3096   }
 3097   if( rc==SQLITE_OK ){
 3098     rc = fsdirRegister(db);
 3099   }
 3100   return rc;
 3101 }
 3102 
 3103 /************************* End ../ext/misc/fileio.c ********************/
 3104 /************************* Begin ../ext/misc/completion.c ******************/
 3105 /*
 3106 ** 2017-07-10
 3107 **
 3108 ** The author disclaims copyright to this source code.  In place of
 3109 ** a legal notice, here is a blessing:
 3110 **
 3111 **    May you do good and not evil.
 3112 **    May you find forgiveness for yourself and forgive others.
 3113 **    May you share freely, never taking more than you give.
 3114 **
 3115 *************************************************************************
 3116 **
 3117 ** This file implements an eponymous virtual table that returns suggested
 3118 ** completions for a partial SQL input.
 3119 **
 3120 ** Suggested usage:
 3121 **
 3122 **     SELECT DISTINCT candidate COLLATE nocase
 3123 **       FROM completion($prefix,$wholeline)
 3124 **      ORDER BY 1;
 3125 **
 3126 ** The two query parameters are optional.  $prefix is the text of the
 3127 ** current word being typed and that is to be completed.  $wholeline is
 3128 ** the complete input line, used for context.
 3129 **
 3130 ** The raw completion() table might return the same candidate multiple
 3131 ** times, for example if the same column name is used to two or more
 3132 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
 3133 ** the DISTINCT and ORDER BY are recommended.
 3134 **
 3135 ** This virtual table operates at the speed of human typing, and so there
 3136 ** is no attempt to make it fast.  Even a slow implementation will be much
 3137 ** faster than any human can type.
 3138 **
 3139 */
 3140 /* #include "sqlite3ext.h" */
 3141 SQLITE_EXTENSION_INIT1
 3142 #include <assert.h>
 3143 #include <string.h>
 3144 #include <ctype.h>
 3145 
 3146 #ifndef SQLITE_OMIT_VIRTUALTABLE
 3147 
 3148 /* completion_vtab is a subclass of sqlite3_vtab which will
 3149 ** serve as the underlying representation of a completion virtual table
 3150 */
 3151 typedef struct completion_vtab completion_vtab;
 3152 struct completion_vtab {
 3153   sqlite3_vtab base;  /* Base class - must be first */
 3154   sqlite3 *db;        /* Database connection for this completion vtab */
 3155 };
 3156 
 3157 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
 3158 ** serve as the underlying representation of a cursor that scans
 3159 ** over rows of the result
 3160 */
 3161 typedef struct completion_cursor completion_cursor;
 3162 struct completion_cursor {
 3163   sqlite3_vtab_cursor base;  /* Base class - must be first */
 3164   sqlite3 *db;               /* Database connection for this cursor */
 3165   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
 3166   char *zPrefix;             /* The prefix for the word we want to complete */
 3167   char *zLine;               /* The whole that we want to complete */
 3168   const char *zCurrentRow;   /* Current output row */
 3169   int szRow;                 /* Length of the zCurrentRow string */
 3170   sqlite3_stmt *pStmt;       /* Current statement */
 3171   sqlite3_int64 iRowid;      /* The rowid */
 3172   int ePhase;                /* Current phase */
 3173   int j;                     /* inter-phase counter */
 3174 };
 3175 
 3176 /* Values for ePhase:
 3177 */
 3178 #define COMPLETION_FIRST_PHASE   1
 3179 #define COMPLETION_KEYWORDS      1
 3180 #define COMPLETION_PRAGMAS       2
 3181 #define COMPLETION_FUNCTIONS     3
 3182 #define COMPLETION_COLLATIONS    4
 3183 #define COMPLETION_INDEXES       5
 3184 #define COMPLETION_TRIGGERS      6
 3185 #define COMPLETION_DATABASES     7
 3186 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
 3187 #define COMPLETION_COLUMNS       9
 3188 #define COMPLETION_MODULES       10
 3189 #define COMPLETION_EOF           11
 3190 
 3191 /*
 3192 ** The completionConnect() method is invoked to create a new
 3193 ** completion_vtab that describes the completion virtual table.
 3194 **
 3195 ** Think of this routine as the constructor for completion_vtab objects.
 3196 **
 3197 ** All this routine needs to do is:
 3198 **
 3199 **    (1) Allocate the completion_vtab object and initialize all fields.
 3200 **
 3201 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
 3202 **        result set of queries against completion will look like.
 3203 */
 3204 static int completionConnect(
 3205   sqlite3 *db,
 3206   void *pAux,
 3207   int argc, const char *const*argv,
 3208   sqlite3_vtab **ppVtab,
 3209   char **pzErr
 3210 ){
 3211   completion_vtab *pNew;
 3212   int rc;
 3213 
 3214   (void)(pAux);    /* Unused parameter */
 3215   (void)(argc);    /* Unused parameter */
 3216   (void)(argv);    /* Unused parameter */
 3217   (void)(pzErr);   /* Unused parameter */
 3218 
 3219 /* Column numbers */
 3220 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
 3221 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
 3222 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
 3223 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
 3224 
 3225   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
 3226   rc = sqlite3_declare_vtab(db,
 3227       "CREATE TABLE x("
 3228       "  candidate TEXT,"
 3229       "  prefix TEXT HIDDEN,"
 3230       "  wholeline TEXT HIDDEN,"
 3231       "  phase INT HIDDEN"        /* Used for debugging only */
 3232       ")");
 3233   if( rc==SQLITE_OK ){
 3234     pNew = sqlite3_malloc( sizeof(*pNew) );
 3235     *ppVtab = (sqlite3_vtab*)pNew;
 3236     if( pNew==0 ) return SQLITE_NOMEM;
 3237     memset(pNew, 0, sizeof(*pNew));
 3238     pNew->db = db;
 3239   }
 3240   return rc;
 3241 }
 3242 
 3243 /*
 3244 ** This method is the destructor for completion_cursor objects.
 3245 */
 3246 static int completionDisconnect(sqlite3_vtab *pVtab){
 3247   sqlite3_free(pVtab);
 3248   return SQLITE_OK;
 3249 }
 3250 
 3251 /*
 3252 ** Constructor for a new completion_cursor object.
 3253 */
 3254 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
 3255   completion_cursor *pCur;
 3256   pCur = sqlite3_malloc( sizeof(*pCur) );
 3257   if( pCur==0 ) return SQLITE_NOMEM;
 3258   memset(pCur, 0, sizeof(*pCur));
 3259   pCur->db = ((completion_vtab*)p)->db;
 3260   *ppCursor = &pCur->base;
 3261   return SQLITE_OK;
 3262 }
 3263 
 3264 /*
 3265 ** Reset the completion_cursor.
 3266 */
 3267 static void completionCursorReset(completion_cursor *pCur){
 3268   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
 3269   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
 3270   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
 3271   pCur->j = 0;
 3272 }
 3273 
 3274 /*
 3275 ** Destructor for a completion_cursor.
 3276 */
 3277 static int completionClose(sqlite3_vtab_cursor *cur){
 3278   completionCursorReset((completion_cursor*)cur);
 3279   sqlite3_free(cur);
 3280   return SQLITE_OK;
 3281 }
 3282 
 3283 /*
 3284 ** Advance a completion_cursor to its next row of output.
 3285 **
 3286 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
 3287 ** record the current state of the scan.  This routine sets ->zCurrentRow
 3288 ** to the current row of output and then returns.  If no more rows remain,
 3289 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
 3290 ** table that has reached the end of its scan.
 3291 **
 3292 ** The current implementation just lists potential identifiers and
 3293 ** keywords and filters them by zPrefix.  Future enhancements should
 3294 ** take zLine into account to try to restrict the set of identifiers and
 3295 ** keywords based on what would be legal at the current point of input.
 3296 */
 3297 static int completionNext(sqlite3_vtab_cursor *cur){
 3298   completion_cursor *pCur = (completion_cursor*)cur;
 3299   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
 3300   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
 3301   pCur->iRowid++;
 3302   while( pCur->ePhase!=COMPLETION_EOF ){
 3303     switch( pCur->ePhase ){
 3304       case COMPLETION_KEYWORDS: {
 3305         if( pCur->j >= sqlite3_keyword_count() ){
 3306           pCur->zCurrentRow = 0;
 3307           pCur->ePhase = COMPLETION_DATABASES;
 3308         }else{
 3309           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
 3310         }
 3311         iCol = -1;
 3312         break;
 3313       }
 3314       case COMPLETION_DATABASES: {
 3315         if( pCur->pStmt==0 ){
 3316           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
 3317                              &pCur->pStmt, 0);
 3318         }
 3319         iCol = 1;
 3320         eNextPhase = COMPLETION_TABLES;
 3321         break;
 3322       }
 3323       case COMPLETION_TABLES: {
 3324         if( pCur->pStmt==0 ){
 3325           sqlite3_stmt *pS2;
 3326           char *zSql = 0;
 3327           const char *zSep = "";
 3328           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
 3329           while( sqlite3_step(pS2)==SQLITE_ROW ){
 3330             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
 3331             zSql = sqlite3_mprintf(
 3332                "%z%s"
 3333                "SELECT name FROM \"%w\".sqlite_master",
 3334                zSql, zSep, zDb
 3335             );
 3336             if( zSql==0 ) return SQLITE_NOMEM;
 3337             zSep = " UNION ";
 3338           }
 3339           sqlite3_finalize(pS2);
 3340           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
 3341           sqlite3_free(zSql);
 3342         }
 3343         iCol = 0;
 3344         eNextPhase = COMPLETION_COLUMNS;
 3345         break;
 3346       }
 3347       case COMPLETION_COLUMNS: {
 3348         if( pCur->pStmt==0 ){
 3349           sqlite3_stmt *pS2;
 3350           char *zSql = 0;
 3351           const char *zSep = "";
 3352           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
 3353           while( sqlite3_step(pS2)==SQLITE_ROW ){
 3354             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
 3355             zSql = sqlite3_mprintf(
 3356                "%z%s"
 3357                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
 3358                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
 3359                " WHERE sm.type='table'",
 3360                zSql, zSep, zDb, zDb
 3361             );
 3362             if( zSql==0 ) return SQLITE_NOMEM;
 3363             zSep = " UNION ";
 3364           }
 3365           sqlite3_finalize(pS2);
 3366           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
 3367           sqlite3_free(zSql);
 3368         }
 3369         iCol = 0;
 3370         eNextPhase = COMPLETION_EOF;
 3371         break;
 3372       }
 3373     }
 3374     if( iCol<0 ){
 3375       /* This case is when the phase presets zCurrentRow */
 3376       if( pCur->zCurrentRow==0 ) continue;
 3377     }else{
 3378       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
 3379         /* Extract the next row of content */
 3380         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
 3381         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
 3382       }else{
 3383         /* When all rows are finished, advance to the next phase */
 3384         sqlite3_finalize(pCur->pStmt);
 3385         pCur->pStmt = 0;
 3386         pCur->ePhase = eNextPhase;
 3387         continue;
 3388       }
 3389     }
 3390     if( pCur->nPrefix==0 ) break;
 3391     if( pCur->nPrefix<=pCur->szRow
 3392      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
 3393     ){
 3394       break;
 3395     }
 3396   }
 3397 
 3398   return SQLITE_OK;
 3399 }
 3400 
 3401 /*
 3402 ** Return values of columns for the row at which the completion_cursor
 3403 ** is currently pointing.
 3404 */
 3405 static int completionColumn(
 3406   sqlite3_vtab_cursor *cur,   /* The cursor */
 3407   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
 3408   int i                       /* Which column to return */
 3409 ){
 3410   completion_cursor *pCur = (completion_cursor*)cur;
 3411   switch( i ){
 3412     case COMPLETION_COLUMN_CANDIDATE: {
 3413       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
 3414       break;
 3415     }
 3416     case COMPLETION_COLUMN_PREFIX: {
 3417       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
 3418       break;
 3419     }
 3420     case COMPLETION_COLUMN_WHOLELINE: {
 3421       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
 3422       break;
 3423     }
 3424     case COMPLETION_COLUMN_PHASE: {
 3425       sqlite3_result_int(ctx, pCur->ePhase);
 3426       break;
 3427     }
 3428   }
 3429   return SQLITE_OK;
 3430 }
 3431 
 3432 /*
 3433 ** Return the rowid for the current row.  In this implementation, the
 3434 ** rowid is the same as the output value.
 3435 */
 3436 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
 3437   completion_cursor *pCur = (completion_cursor*)cur;
 3438   *pRowid = pCur->iRowid;
 3439   return SQLITE_OK;
 3440 }
 3441 
 3442 /*
 3443 ** Return TRUE if the cursor has been moved off of the last
 3444 ** row of output.
 3445 */
 3446 static int completionEof(sqlite3_vtab_cursor *cur){
 3447   completion_cursor *pCur = (completion_cursor*)cur;
 3448   return pCur->ePhase >= COMPLETION_EOF;
 3449 }
 3450 
 3451 /*
 3452 ** This method is called to "rewind" the completion_cursor object back
 3453 ** to the first row of output.  This method is always called at least
 3454 ** once prior to any call to completionColumn() or completionRowid() or 
 3455 ** completionEof().
 3456 */
 3457 static int completionFilter(
 3458   sqlite3_vtab_cursor *pVtabCursor, 
 3459   int idxNum, const char *idxStr,
 3460   int argc, sqlite3_value **argv
 3461 ){
 3462   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
 3463   int iArg = 0;
 3464   (void)(idxStr);   /* Unused parameter */
 3465   (void)(argc);     /* Unused parameter */
 3466   completionCursorReset(pCur);
 3467   if( idxNum & 1 ){
 3468     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
 3469     if( pCur->nPrefix>0 ){
 3470       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
 3471       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
 3472     }
 3473     iArg = 1;
 3474   }
 3475   if( idxNum & 2 ){
 3476     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
 3477     if( pCur->nLine>0 ){
 3478       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
 3479       if( pCur->zLine==0 ) return SQLITE_NOMEM;
 3480     }
 3481   }
 3482   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
 3483     int i = pCur->nLine;
 3484     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
 3485       i--;
 3486     }
 3487     pCur->nPrefix = pCur->nLine - i;
 3488     if( pCur->nPrefix>0 ){
 3489       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
 3490       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
 3491     }
 3492   }
 3493   pCur->iRowid = 0;
 3494   pCur->ePhase = COMPLETION_FIRST_PHASE;
 3495   return completionNext(pVtabCursor);
 3496 }
 3497 
 3498 /*
 3499 ** SQLite will invoke this method one or more times while planning a query
 3500 ** that uses the completion virtual table.  This routine needs to create
 3501 ** a query plan for each invocation and compute an estimated cost for that
 3502 ** plan.
 3503 **
 3504 ** There are two hidden parameters that act as arguments to the table-valued
 3505 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
 3506 ** is available and bit 1 is set if "wholeline" is available.
 3507 */
 3508 static int completionBestIndex(
 3509   sqlite3_vtab *tab,
 3510   sqlite3_index_info *pIdxInfo
 3511 ){
 3512   int i;                 /* Loop over constraints */
 3513   int idxNum = 0;        /* The query plan bitmask */
 3514   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
 3515   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
 3516   int nArg = 0;          /* Number of arguments that completeFilter() expects */
 3517   const struct sqlite3_index_constraint *pConstraint;
 3518 
 3519   (void)(tab);    /* Unused parameter */
 3520   pConstraint = pIdxInfo->aConstraint;
 3521   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
 3522     if( pConstraint->usable==0 ) continue;
 3523     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
 3524     switch( pConstraint->iColumn ){
 3525       case COMPLETION_COLUMN_PREFIX:
 3526         prefixIdx = i;
 3527         idxNum |= 1;
 3528         break;
 3529       case COMPLETION_COLUMN_WHOLELINE:
 3530         wholelineIdx = i;
 3531         idxNum |= 2;
 3532         break;
 3533     }
 3534   }
 3535   if( prefixIdx>=0 ){
 3536     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
 3537     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
 3538   }
 3539   if( wholelineIdx>=0 ){
 3540     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
 3541     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
 3542   }
 3543   pIdxInfo->idxNum = idxNum;
 3544   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
 3545   pIdxInfo->estimatedRows = 500 - 100*nArg;
 3546   return SQLITE_OK;
 3547 }
 3548 
 3549 /*
 3550 ** This following structure defines all the methods for the 
 3551 ** completion virtual table.
 3552 */
 3553 static sqlite3_module completionModule = {
 3554   0,                         /* iVersion */
 3555   0,                         /* xCreate */
 3556   completionConnect,         /* xConnect */
 3557   completionBestIndex,       /* xBestIndex */
 3558   completionDisconnect,      /* xDisconnect */
 3559   0,                         /* xDestroy */
 3560   completionOpen,            /* xOpen - open a cursor */
 3561   completionClose,           /* xClose - close a cursor */
 3562   completionFilter,          /* xFilter - configure scan constraints */
 3563   completionNext,            /* xNext - advance a cursor */
 3564   completionEof,             /* xEof - check for end of scan */
 3565   completionColumn,          /* xColumn - read data */
 3566   completionRowid,           /* xRowid - read data */
 3567   0,                         /* xUpdate */
 3568   0,                         /* xBegin */
 3569   0,                         /* xSync */
 3570   0,                         /* xCommit */
 3571   0,                         /* xRollback */
 3572   0,                         /* xFindMethod */
 3573   0,                         /* xRename */
 3574   0,                         /* xSavepoint */
 3575   0,                         /* xRelease */
 3576   0,                         /* xRollbackTo */
 3577   0                          /* xShadowName */
 3578 };
 3579 
 3580 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 3581 
 3582 int sqlite3CompletionVtabInit(sqlite3 *db){
 3583   int rc = SQLITE_OK;
 3584 #ifndef SQLITE_OMIT_VIRTUALTABLE
 3585   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
 3586 #endif
 3587   return rc;
 3588 }
 3589 
 3590 #ifdef _WIN32
 3591 
 3592 #endif
 3593 int sqlite3_completion_init(
 3594   sqlite3 *db, 
 3595   char **pzErrMsg, 
 3596   const sqlite3_api_routines *pApi
 3597 ){
 3598   int rc = SQLITE_OK;
 3599   SQLITE_EXTENSION_INIT2(pApi);
 3600   (void)(pzErrMsg);  /* Unused parameter */
 3601 #ifndef SQLITE_OMIT_VIRTUALTABLE
 3602   rc = sqlite3CompletionVtabInit(db);
 3603 #endif
 3604   return rc;
 3605 }
 3606 
 3607 /************************* End ../ext/misc/completion.c ********************/
 3608 /************************* Begin ../ext/misc/appendvfs.c ******************/
 3609 /*
 3610 ** 2017-10-20
 3611 **
 3612 ** The author disclaims copyright to this source code.  In place of
 3613 ** a legal notice, here is a blessing:
 3614 **
 3615 **    May you do good and not evil.
 3616 **    May you find forgiveness for yourself and forgive others.
 3617 **    May you share freely, never taking more than you give.
 3618 **
 3619 ******************************************************************************
 3620 **
 3621 ** This file implements a VFS shim that allows an SQLite database to be
 3622 ** appended onto the end of some other file, such as an executable.
 3623 **
 3624 ** A special record must appear at the end of the file that identifies the
 3625 ** file as an appended database and provides an offset to page 1.  For
 3626 ** best performance page 1 should be located at a disk page boundary, though
 3627 ** that is not required.
 3628 **
 3629 ** When opening a database using this VFS, the connection might treat
 3630 ** the file as an ordinary SQLite database, or it might treat is as a
 3631 ** database appended onto some other file.  Here are the rules:
 3632 **
 3633 **  (1)  When opening a new empty file, that file is treated as an ordinary
 3634 **       database.
 3635 **
 3636 **  (2)  When opening a file that begins with the standard SQLite prefix
 3637 **       string "SQLite format 3", that file is treated as an ordinary
 3638 **       database.
 3639 **
 3640 **  (3)  When opening a file that ends with the appendvfs trailer string
 3641 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
 3642 **       database.
 3643 **
 3644 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
 3645 **       set, then a new database is appended to the already existing file.
 3646 **
 3647 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
 3648 **
 3649 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
 3650 ** the file containing the database is limited to 1GB.  This VFS will refuse
 3651 ** to read or write past the 1GB mark.  This restriction might be lifted in
 3652 ** future versions.  For now, if you need a large database, then keep the
 3653 ** database in a separate file.
 3654 **
 3655 ** If the file being opened is not an appended database, then this shim is
 3656 ** a pass-through into the default underlying VFS.
 3657 **/
 3658 /* #include "sqlite3ext.h" */
 3659 SQLITE_EXTENSION_INIT1
 3660 #include <string.h>
 3661 #include <assert.h>
 3662 
 3663 /* The append mark at the end of the database is:
 3664 **
 3665 **     Start-Of-SQLite3-NNNNNNNN
 3666 **     123456789 123456789 12345
 3667 **
 3668 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
 3669 ** the offset to page 1.
 3670 */
 3671 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
 3672 #define APND_MARK_PREFIX_SZ  17
 3673 #define APND_MARK_SIZE       25
 3674 
 3675 /*
 3676 ** Maximum size of the combined prefix + database + append-mark.  This
 3677 ** must be less than 0x40000000 to avoid locking issues on Windows.
 3678 */
 3679 #define APND_MAX_SIZE  (65536*15259)
 3680 
 3681 /*
 3682 ** Forward declaration of objects used by this utility
 3683 */
 3684 typedef struct sqlite3_vfs ApndVfs;
 3685 typedef struct ApndFile ApndFile;
 3686 
 3687 /* Access to a lower-level VFS that (might) implement dynamic loading,
 3688 ** access to randomness, etc.
 3689 */
 3690 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
 3691 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
 3692 
 3693 /* An open file */
 3694 struct ApndFile {
 3695   sqlite3_file base;              /* IO methods */
 3696   sqlite3_int64 iPgOne;           /* File offset to page 1 */
 3697   sqlite3_int64 iMark;            /* Start of the append-mark */
 3698 };
 3699 
 3700 /*
 3701 ** Methods for ApndFile
 3702 */
 3703 static int apndClose(sqlite3_file*);
 3704 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
 3705 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
 3706 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
 3707 static int apndSync(sqlite3_file*, int flags);
 3708 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
 3709 static int apndLock(sqlite3_file*, int);
 3710 static int apndUnlock(sqlite3_file*, int);
 3711 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
 3712 static int apndFileControl(sqlite3_file*, int op, void *pArg);
 3713 static int apndSectorSize(sqlite3_file*);
 3714 static int apndDeviceCharacteristics(sqlite3_file*);
 3715 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
 3716 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
 3717 static void apndShmBarrier(sqlite3_file*);
 3718 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
 3719 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
 3720 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
 3721 
 3722 /*
 3723 ** Methods for ApndVfs
 3724 */
 3725 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
 3726 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
 3727 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
 3728 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
 3729 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
 3730 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
 3731 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
 3732 static void apndDlClose(sqlite3_vfs*, void*);
 3733 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
 3734 static int apndSleep(sqlite3_vfs*, int microseconds);
 3735 static int apndCurrentTime(sqlite3_vfs*, double*);
 3736 static int apndGetLastError(sqlite3_vfs*, int, char *);
 3737 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
 3738 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
 3739 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
 3740 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
 3741 
 3742 static sqlite3_vfs apnd_vfs = {
 3743   3,                            /* iVersion (set when registered) */
 3744   0,                            /* szOsFile (set when registered) */
 3745   1024,                         /* mxPathname */
 3746   0,                            /* pNext */
 3747   "apndvfs",                    /* zName */
 3748   0,                            /* pAppData (set when registered) */ 
 3749   apndOpen,                     /* xOpen */
 3750   apndDelete,                   /* xDelete */
 3751   apndAccess,                   /* xAccess */
 3752   apndFullPathname,             /* xFullPathname */
 3753   apndDlOpen,                   /* xDlOpen */
 3754   apndDlError,                  /* xDlError */
 3755   apndDlSym,                    /* xDlSym */
 3756   apndDlClose,                  /* xDlClose */
 3757   apndRandomness,               /* xRandomness */
 3758   apndSleep,                    /* xSleep */
 3759   apndCurrentTime,              /* xCurrentTime */
 3760   apndGetLastError,             /* xGetLastError */
 3761   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
 3762   apndSetSystemCall,            /* xSetSystemCall */
 3763   apndGetSystemCall,            /* xGetSystemCall */
 3764   apndNextSystemCall            /* xNextSystemCall */
 3765 };
 3766 
 3767 static const sqlite3_io_methods apnd_io_methods = {
 3768   3,                              /* iVersion */
 3769   apndClose,                      /* xClose */
 3770   apndRead,                       /* xRead */
 3771   apndWrite,                      /* xWrite */
 3772   apndTruncate,                   /* xTruncate */
 3773   apndSync,                       /* xSync */
 3774   apndFileSize,                   /* xFileSize */
 3775   apndLock,                       /* xLock */
 3776   apndUnlock,                     /* xUnlock */
 3777   apndCheckReservedLock,          /* xCheckReservedLock */
 3778   apndFileControl,                /* xFileControl */
 3779   apndSectorSize,                 /* xSectorSize */
 3780   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
 3781   apndShmMap,                     /* xShmMap */
 3782   apndShmLock,                    /* xShmLock */
 3783   apndShmBarrier,                 /* xShmBarrier */
 3784   apndShmUnmap,                   /* xShmUnmap */
 3785   apndFetch,                      /* xFetch */
 3786   apndUnfetch                     /* xUnfetch */
 3787 };
 3788 
 3789 
 3790 
 3791 /*
 3792 ** Close an apnd-file.
 3793 */
 3794 static int apndClose(sqlite3_file *pFile){
 3795   pFile = ORIGFILE(pFile);
 3796   return pFile->pMethods->xClose(pFile);
 3797 }
 3798 
 3799 /*
 3800 ** Read data from an apnd-file.
 3801 */
 3802 static int apndRead(
 3803   sqlite3_file *pFile, 
 3804   void *zBuf, 
 3805   int iAmt, 
 3806   sqlite_int64 iOfst
 3807 ){
 3808   ApndFile *p = (ApndFile *)pFile;
 3809   pFile = ORIGFILE(pFile);
 3810   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
 3811 }
 3812 
 3813 /*
 3814 ** Add the append-mark onto the end of the file.
 3815 */
 3816 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
 3817   int i;
 3818   unsigned char a[APND_MARK_SIZE];
 3819   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
 3820   for(i=0; i<8; i++){
 3821     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
 3822   }
 3823   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
 3824 }
 3825 
 3826 /*
 3827 ** Write data to an apnd-file.
 3828 */
 3829 static int apndWrite(
 3830   sqlite3_file *pFile,
 3831   const void *zBuf,
 3832   int iAmt,
 3833   sqlite_int64 iOfst
 3834 ){
 3835   int rc;
 3836   ApndFile *p = (ApndFile *)pFile;
 3837   pFile = ORIGFILE(pFile);
 3838   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
 3839   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
 3840   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
 3841     sqlite3_int64 sz = 0;
 3842     rc = pFile->pMethods->xFileSize(pFile, &sz);
 3843     if( rc==SQLITE_OK ){
 3844       p->iMark = sz - APND_MARK_SIZE;
 3845       if( iOfst + iAmt + p->iPgOne > p->iMark ){
 3846         p->iMark = p->iPgOne + iOfst + iAmt;
 3847         rc = apndWriteMark(p, pFile);
 3848       }
 3849     }
 3850   }
 3851   return rc;
 3852 }
 3853 
 3854 /*
 3855 ** Truncate an apnd-file.
 3856 */
 3857 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
 3858   int rc;
 3859   ApndFile *p = (ApndFile *)pFile;
 3860   pFile = ORIGFILE(pFile);
 3861   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
 3862   if( rc==SQLITE_OK ){
 3863     p->iMark = p->iPgOne+size;
 3864     rc = apndWriteMark(p, pFile);
 3865   }
 3866   return rc;
 3867 }
 3868 
 3869 /*
 3870 ** Sync an apnd-file.
 3871 */
 3872 static int apndSync(sqlite3_file *pFile, int flags){
 3873   pFile = ORIGFILE(pFile);
 3874   return pFile->pMethods->xSync(pFile, flags);
 3875 }
 3876 
 3877 /*
 3878 ** Return the current file-size of an apnd-file.
 3879 */
 3880 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
 3881   ApndFile *p = (ApndFile *)pFile;
 3882   int rc;
 3883   pFile = ORIGFILE(p);
 3884   rc = pFile->pMethods->xFileSize(pFile, pSize);
 3885   if( rc==SQLITE_OK && p->iPgOne ){
 3886     *pSize -= p->iPgOne + APND_MARK_SIZE;
 3887   }
 3888   return rc;
 3889 }
 3890 
 3891 /*
 3892 ** Lock an apnd-file.
 3893 */
 3894 static int apndLock(sqlite3_file *pFile, int eLock){
 3895   pFile = ORIGFILE(pFile);
 3896   return pFile->pMethods->xLock(pFile, eLock);
 3897 }
 3898 
 3899 /*
 3900 ** Unlock an apnd-file.
 3901 */
 3902 static int apndUnlock(sqlite3_file *pFile, int eLock){
 3903   pFile = ORIGFILE(pFile);
 3904   return pFile->pMethods->xUnlock(pFile, eLock);
 3905 }
 3906 
 3907 /*
 3908 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
 3909 */
 3910 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
 3911   pFile = ORIGFILE(pFile);
 3912   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
 3913 }
 3914 
 3915 /*
 3916 ** File control method. For custom operations on an apnd-file.
 3917 */
 3918 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
 3919   ApndFile *p = (ApndFile *)pFile;
 3920   int rc;
 3921   pFile = ORIGFILE(pFile);
 3922   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
 3923   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
 3924     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
 3925   }
 3926   return rc;
 3927 }
 3928 
 3929 /*
 3930 ** Return the sector-size in bytes for an apnd-file.
 3931 */
 3932 static int apndSectorSize(sqlite3_file *pFile){
 3933   pFile = ORIGFILE(pFile);
 3934   return pFile->pMethods->xSectorSize(pFile);
 3935 }
 3936 
 3937 /*
 3938 ** Return the device characteristic flags supported by an apnd-file.
 3939 */
 3940 static int apndDeviceCharacteristics(sqlite3_file *pFile){
 3941   pFile = ORIGFILE(pFile);
 3942   return pFile->pMethods->xDeviceCharacteristics(pFile);
 3943 }
 3944 
 3945 /* Create a shared memory file mapping */
 3946 static int apndShmMap(
 3947   sqlite3_file *pFile,
 3948   int iPg,
 3949   int pgsz,
 3950   int bExtend,
 3951   void volatile **pp
 3952 ){
 3953   pFile = ORIGFILE(pFile);
 3954   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
 3955 }
 3956 
 3957 /* Perform locking on a shared-memory segment */
 3958 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
 3959   pFile = ORIGFILE(pFile);
 3960   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
 3961 }
 3962 
 3963 /* Memory barrier operation on shared memory */
 3964 static void apndShmBarrier(sqlite3_file *pFile){
 3965   pFile = ORIGFILE(pFile);
 3966   pFile->pMethods->xShmBarrier(pFile);
 3967 }
 3968 
 3969 /* Unmap a shared memory segment */
 3970 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
 3971   pFile = ORIGFILE(pFile);
 3972   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
 3973 }
 3974 
 3975 /* Fetch a page of a memory-mapped file */
 3976 static int apndFetch(
 3977   sqlite3_file *pFile,
 3978   sqlite3_int64 iOfst,
 3979   int iAmt,
 3980   void **pp
 3981 ){
 3982   ApndFile *p = (ApndFile *)pFile;
 3983   pFile = ORIGFILE(pFile);
 3984   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
 3985 }
 3986 
 3987 /* Release a memory-mapped page */
 3988 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
 3989   ApndFile *p = (ApndFile *)pFile;
 3990   pFile = ORIGFILE(pFile);
 3991   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
 3992 }
 3993 
 3994 /*
 3995 ** Check to see if the file is an ordinary SQLite database file.
 3996 */
 3997 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
 3998   int rc;
 3999   char zHdr[16];
 4000   static const char aSqliteHdr[] = "SQLite format 3";
 4001   if( sz<512 ) return 0;
 4002   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
 4003   if( rc ) return 0;
 4004   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
 4005 }
 4006 
 4007 /*
 4008 ** Try to read the append-mark off the end of a file.  Return the
 4009 ** start of the appended database if the append-mark is present.  If
 4010 ** there is no append-mark, return -1;
 4011 */
 4012 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
 4013   int rc, i;
 4014   sqlite3_int64 iMark;
 4015   unsigned char a[APND_MARK_SIZE];
 4016 
 4017   if( sz<=APND_MARK_SIZE ) return -1;
 4018   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
 4019   if( rc ) return -1;
 4020   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
 4021   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
 4022   for(i=1; i<8; i++){    
 4023     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
 4024   }
 4025   return iMark;
 4026 }
 4027 
 4028 /*
 4029 ** Open an apnd file handle.
 4030 */
 4031 static int apndOpen(
 4032   sqlite3_vfs *pVfs,
 4033   const char *zName,
 4034   sqlite3_file *pFile,
 4035   int flags,
 4036   int *pOutFlags
 4037 ){
 4038   ApndFile *p;
 4039   sqlite3_file *pSubFile;
 4040   sqlite3_vfs *pSubVfs;
 4041   int rc;
 4042   sqlite3_int64 sz;
 4043   pSubVfs = ORIGVFS(pVfs);
 4044   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
 4045     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
 4046   }
 4047   p = (ApndFile*)pFile;
 4048   memset(p, 0, sizeof(*p));
 4049   pSubFile = ORIGFILE(pFile);
 4050   p->base.pMethods = &apnd_io_methods;
 4051   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
 4052   if( rc ) goto apnd_open_done;
 4053   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
 4054   if( rc ){
 4055     pSubFile->pMethods->xClose(pSubFile);
 4056     goto apnd_open_done;
 4057   }
 4058   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
 4059     memmove(pFile, pSubFile, pSubVfs->szOsFile);
 4060     return SQLITE_OK;
 4061   }
 4062   p->iMark = 0;
 4063   p->iPgOne = apndReadMark(sz, pFile);
 4064   if( p->iPgOne>0 ){
 4065     return SQLITE_OK;
 4066   }
 4067   if( (flags & SQLITE_OPEN_CREATE)==0 ){
 4068     pSubFile->pMethods->xClose(pSubFile);
 4069     rc = SQLITE_CANTOPEN;
 4070   }
 4071   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
 4072 apnd_open_done:
 4073   if( rc ) pFile->pMethods = 0;
 4074   return rc;
 4075 }
 4076 
 4077 /*
 4078 ** All other VFS methods are pass-thrus.
 4079 */
 4080 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
 4081   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
 4082 }
 4083 static int apndAccess(
 4084   sqlite3_vfs *pVfs, 
 4085   const char *zPath, 
 4086   int flags, 
 4087   int *pResOut
 4088 ){
 4089   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
 4090 }
 4091 static int apndFullPathname(
 4092   sqlite3_vfs *pVfs, 
 4093   const char *zPath, 
 4094   int nOut, 
 4095   char *zOut
 4096 ){
 4097   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
 4098 }
 4099 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
 4100   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
 4101 }
 4102 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
 4103   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
 4104 }
 4105 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
 4106   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
 4107 }
 4108 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
 4109   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
 4110 }
 4111 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 4112   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
 4113 }
 4114 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
 4115   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
 4116 }
 4117 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
 4118   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
 4119 }
 4120 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
 4121   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
 4122 }
 4123 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
 4124   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
 4125 }
 4126 static int apndSetSystemCall(
 4127   sqlite3_vfs *pVfs,
 4128   const char *zName,
 4129   sqlite3_syscall_ptr pCall
 4130 ){
 4131   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
 4132 }
 4133 static sqlite3_syscall_ptr apndGetSystemCall(
 4134   sqlite3_vfs *pVfs,
 4135   const char *zName
 4136 ){
 4137   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
 4138 }
 4139 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
 4140   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
 4141 }
 4142 
 4143   
 4144 #ifdef _WIN32
 4145 
 4146 #endif
 4147 /* 
 4148 ** This routine is called when the extension is loaded.
 4149 ** Register the new VFS.
 4150 */
 4151 int sqlite3_appendvfs_init(
 4152   sqlite3 *db, 
 4153   char **pzErrMsg, 
 4154   const sqlite3_api_routines *pApi
 4155 ){
 4156   int rc = SQLITE_OK;
 4157   sqlite3_vfs *pOrig;
 4158   SQLITE_EXTENSION_INIT2(pApi);
 4159   (void)pzErrMsg;
 4160   (void)db;
 4161   pOrig = sqlite3_vfs_find(0);
 4162   apnd_vfs.iVersion = pOrig->iVersion;
 4163   apnd_vfs.pAppData = pOrig;
 4164   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
 4165   rc = sqlite3_vfs_register(&apnd_vfs, 0);
 4166 #ifdef APPENDVFS_TEST
 4167   if( rc==SQLITE_OK ){
 4168     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
 4169   }
 4170 #endif
 4171   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
 4172   return rc;
 4173 }
 4174 
 4175 /************************* End ../ext/misc/appendvfs.c ********************/
 4176 /************************* Begin ../ext/misc/memtrace.c ******************/
 4177 /*
 4178 ** 2019-01-21
 4179 **
 4180 ** The author disclaims copyright to this source code.  In place of
 4181 ** a legal notice, here is a blessing:
 4182 **
 4183 **    May you do good and not evil.
 4184 **    May you find forgiveness for yourself and forgive others.
 4185 **    May you share freely, never taking more than you give.
 4186 **
 4187 *************************************************************************
 4188 **
 4189 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
 4190 ** mechanism to add a tracing layer on top of SQLite.  If this extension
 4191 ** is registered prior to sqlite3_initialize(), it will cause all memory
 4192 ** allocation activities to be logged on standard output, or to some other
 4193 ** FILE specified by the initializer.
 4194 **
 4195 ** This file needs to be compiled into the application that uses it.
 4196 **
 4197 ** This extension is used to implement the --memtrace option of the
 4198 ** command-line shell.
 4199 */
 4200 #include <assert.h>
 4201 #include <string.h>
 4202 #include <stdio.h>
 4203 
 4204 /* The original memory allocation routines */
 4205 static sqlite3_mem_methods memtraceBase;
 4206 static FILE *memtraceOut;
 4207 
 4208 /* Methods that trace memory allocations */
 4209 static void *memtraceMalloc(int n){
 4210   if( memtraceOut ){
 4211     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
 4212             memtraceBase.xRoundup(n));
 4213   }
 4214   return memtraceBase.xMalloc(n);
 4215 }
 4216 static void memtraceFree(void *p){
 4217   if( p==0 ) return;
 4218   if( memtraceOut ){
 4219     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
 4220   }
 4221   memtraceBase.xFree(p);
 4222 }
 4223 static void *memtraceRealloc(void *p, int n){
 4224   if( p==0 ) return memtraceMalloc(n);
 4225   if( n==0 ){
 4226     memtraceFree(p);
 4227     return 0;
 4228   }
 4229   if( memtraceOut ){
 4230     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
 4231             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
 4232   }
 4233   return memtraceBase.xRealloc(p, n);
 4234 }
 4235 static int memtraceSize(void *p){
 4236   return memtraceBase.xSize(p);
 4237 }
 4238 static int memtraceRoundup(int n){
 4239   return memtraceBase.xRoundup(n);
 4240 }
 4241 static int memtraceInit(void *p){
 4242   return memtraceBase.xInit(p);
 4243 }
 4244 static void memtraceShutdown(void *p){
 4245   memtraceBase.xShutdown(p);
 4246 }
 4247 
 4248 /* The substitute memory allocator */
 4249 static sqlite3_mem_methods ersaztMethods = {
 4250   memtraceMalloc,
 4251   memtraceFree,
 4252   memtraceRealloc,
 4253   memtraceSize,
 4254   memtraceRoundup,
 4255   memtraceInit,
 4256   memtraceShutdown,
 4257   0
 4258 };
 4259 
 4260 /* Begin tracing memory allocations to out. */
 4261 int sqlite3MemTraceActivate(FILE *out){
 4262   int rc = SQLITE_OK;
 4263   if( memtraceBase.xMalloc==0 ){
 4264     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
 4265     if( rc==SQLITE_OK ){
 4266       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
 4267     }
 4268   }
 4269   memtraceOut = out;
 4270   return rc;
 4271 }
 4272 
 4273 /* Deactivate memory tracing */
 4274 int sqlite3MemTraceDeactivate(void){
 4275   int rc = SQLITE_OK;
 4276   if( memtraceBase.xMalloc!=0 ){
 4277     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
 4278     if( rc==SQLITE_OK ){
 4279       memset(&memtraceBase, 0, sizeof(memtraceBase));
 4280     }
 4281   }
 4282   memtraceOut = 0;
 4283   return rc;
 4284 }
 4285 
 4286 /************************* End ../ext/misc/memtrace.c ********************/
 4287 /************************* Begin ../ext/misc/uint.c ******************/
 4288 /*
 4289 ** 2020-04-14
 4290 **
 4291 ** The author disclaims copyright to this source code.  In place of
 4292 ** a legal notice, here is a blessing:
 4293 **
 4294 **    May you do good and not evil.
 4295 **    May you find forgiveness for yourself and forgive others.
 4296 **    May you share freely, never taking more than you give.
 4297 **
 4298 ******************************************************************************
 4299 **
 4300 ** This SQLite extension implements the UINT collating sequence.
 4301 **
 4302 ** UINT works like BINARY for text, except that embedded strings
 4303 ** of digits compare in numeric order.
 4304 **
 4305 **     *   Leading zeros are handled properly, in the sense that
 4306 **         they do not mess of the maginitude comparison of embedded
 4307 **         strings of digits.  "x00123y" is equal to "x123y".
 4308 **
 4309 **     *   Only unsigned integers are recognized.  Plus and minus
 4310 **         signs are ignored.  Decimal points and exponential notation
 4311 **         are ignored.
 4312 **
 4313 **     *   Embedded integers can be of arbitrary length.  Comparison
 4314 **         is *not* limited integers that can be expressed as a
 4315 **         64-bit machine integer.
 4316 */
 4317 /* #include "sqlite3ext.h" */
 4318 SQLITE_EXTENSION_INIT1
 4319 #include <assert.h>
 4320 #include <string.h>
 4321 #include <ctype.h>
 4322 
 4323 /*
 4324 ** Compare text in lexicographic order, except strings of digits
 4325 ** compare in numeric order.
 4326 */
 4327 static int uintCollFunc(
 4328   void *notUsed,
 4329   int nKey1, const void *pKey1,
 4330   int nKey2, const void *pKey2
 4331 ){
 4332   const unsigned char *zA = (const unsigned char*)pKey1;
 4333   const unsigned char *zB = (const unsigned char*)pKey2;
 4334   int i=0, j=0, x;
 4335   (void)notUsed;
 4336   while( i<nKey1 && j<nKey2 ){
 4337     x = zA[i] - zB[j];
 4338     if( isdigit(zA[i]) ){
 4339       int k;
 4340       if( !isdigit(zB[j]) ) return x;
 4341       while( i<nKey1 && zA[i]=='0' ){ i++; }
 4342       while( j<nKey2 && zB[j]=='0' ){ j++; }
 4343       k = 0;
 4344       while( i+k<nKey1 && isdigit(zA[i+k])
 4345              && j+k<nKey2 && isdigit(zB[j+k]) ){
 4346         k++;
 4347       }
 4348       if( i+k<nKey1 && isdigit(zA[i+k]) ){
 4349         return +1;
 4350       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
 4351         return -1;
 4352       }else{
 4353         x = memcmp(zA+i, zB+j, k);
 4354         if( x ) return x;
 4355         i += k;
 4356         j += k;
 4357       }
 4358     }else if( x ){
 4359       return x;
 4360     }else{
 4361       i++;
 4362       j++;
 4363     }
 4364   }
 4365   return (nKey1 - i) - (nKey2 - j);
 4366 }
 4367 
 4368 #ifdef _WIN32
 4369 
 4370 #endif
 4371 int sqlite3_uint_init(
 4372   sqlite3 *db, 
 4373   char **pzErrMsg, 
 4374   const sqlite3_api_routines *pApi
 4375 ){
 4376   SQLITE_EXTENSION_INIT2(pApi);
 4377   (void)pzErrMsg;  /* Unused parameter */
 4378   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
 4379 }
 4380 
 4381 /************************* End ../ext/misc/uint.c ********************/
 4382 #ifdef SQLITE_HAVE_ZLIB
 4383 /************************* Begin ../ext/misc/zipfile.c ******************/
 4384 /*
 4385 ** 2017-12-26
 4386 **
 4387 ** The author disclaims copyright to this source code.  In place of
 4388 ** a legal notice, here is a blessing:
 4389 **
 4390 **    May you do good and not evil.
 4391 **    May you find forgiveness for yourself and forgive others.
 4392 **    May you share freely, never taking more than you give.
 4393 **
 4394 ******************************************************************************
 4395 **
 4396 ** This file implements a virtual table for reading and writing ZIP archive
 4397 ** files.
 4398 **
 4399 ** Usage example:
 4400 **
 4401 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
 4402 **
 4403 ** Current limitations:
 4404 **
 4405 **    *  No support for encryption
 4406 **    *  No support for ZIP archives spanning multiple files
 4407 **    *  No support for zip64 extensions
 4408 **    *  Only the "inflate/deflate" (zlib) compression method is supported
 4409 */
 4410 /* #include "sqlite3ext.h" */
 4411 SQLITE_EXTENSION_INIT1
 4412 #include <stdio.h>
 4413 #include <string.h>
 4414 #include <assert.h>
 4415 
 4416 #include <zlib.h>
 4417 
 4418 #ifndef SQLITE_OMIT_VIRTUALTABLE
 4419 
 4420 #ifndef SQLITE_AMALGAMATION
 4421 
 4422 /* typedef sqlite3_int64 i64; */
 4423 /* typedef unsigned char u8; */
 4424 typedef unsigned short u16;
 4425 typedef unsigned long u32;
 4426 #define MIN(a,b) ((a)<(b) ? (a) : (b))
 4427 
 4428 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
 4429 # define ALWAYS(X)      (1)
 4430 # define NEVER(X)       (0)
 4431 #elif !defined(NDEBUG)
 4432 # define ALWAYS(X)      ((X)?1:(assert(0),0))
 4433 # define NEVER(X)       ((X)?(assert(0),1):0)
 4434 #else
 4435 # define ALWAYS(X)      (X)
 4436 # define NEVER(X)       (X)
 4437 #endif
 4438 
 4439 #endif   /* SQLITE_AMALGAMATION */
 4440 
 4441 /*
 4442 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
 4443 **
 4444 ** In some ways it would be better to obtain these values from system 
 4445 ** header files. But, the dependency is undesirable and (a) these
 4446 ** have been stable for decades, (b) the values are part of POSIX and
 4447 ** are also made explicit in [man stat], and (c) are part of the 
 4448 ** file format for zip archives.
 4449 */
 4450 #ifndef S_IFDIR
 4451 # define S_IFDIR 0040000
 4452 #endif
 4453 #ifndef S_IFREG
 4454 # define S_IFREG 0100000
 4455 #endif
 4456 #ifndef S_IFLNK
 4457 # define S_IFLNK 0120000
 4458 #endif
 4459 
 4460 static const char ZIPFILE_SCHEMA[] = 
 4461   "CREATE TABLE y("
 4462     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
 4463     "mode,"              /* 1: POSIX mode for file */
 4464     "mtime,"             /* 2: Last modification time (secs since 1970)*/
 4465     "sz,"                /* 3: Size of object */
 4466     "rawdata,"           /* 4: Raw data */
 4467     "data,"              /* 5: Uncompressed data */
 4468     "method,"            /* 6: Compression method (integer) */
 4469     "z HIDDEN"           /* 7: Name of zip file */
 4470   ") WITHOUT ROWID;";
 4471 
 4472 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
 4473 #define ZIPFILE_BUFFER_SIZE (64*1024)
 4474 
 4475 
 4476 /*
 4477 ** Magic numbers used to read and write zip files.
 4478 **
 4479 ** ZIPFILE_NEWENTRY_MADEBY:
 4480 **   Use this value for the "version-made-by" field in new zip file
 4481 **   entries. The upper byte indicates "unix", and the lower byte 
 4482 **   indicates that the zip file matches pkzip specification 3.0. 
 4483 **   This is what info-zip seems to do.
 4484 **
 4485 ** ZIPFILE_NEWENTRY_REQUIRED:
 4486 **   Value for "version-required-to-extract" field of new entries.
 4487 **   Version 2.0 is required to support folders and deflate compression.
 4488 **
 4489 ** ZIPFILE_NEWENTRY_FLAGS:
 4490 **   Value for "general-purpose-bit-flags" field of new entries. Bit
 4491 **   11 means "utf-8 filename and comment".
 4492 **
 4493 ** ZIPFILE_SIGNATURE_CDS:
 4494 **   First 4 bytes of a valid CDS record.
 4495 **
 4496 ** ZIPFILE_SIGNATURE_LFH:
 4497 **   First 4 bytes of a valid LFH record.
 4498 **
 4499 ** ZIPFILE_SIGNATURE_EOCD
 4500 **   First 4 bytes of a valid EOCD record.
 4501 */
 4502 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
 4503 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
 4504 #define ZIPFILE_NEWENTRY_REQUIRED 20
 4505 #define ZIPFILE_NEWENTRY_FLAGS    0x800
 4506 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
 4507 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
 4508 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
 4509 
 4510 /*
 4511 ** The sizes of the fixed-size part of each of the three main data 
 4512 ** structures in a zip archive.
 4513 */
 4514 #define ZIPFILE_LFH_FIXED_SZ      30
 4515 #define ZIPFILE_EOCD_FIXED_SZ     22
 4516 #define ZIPFILE_CDS_FIXED_SZ      46
 4517 
 4518 /*
 4519 *** 4.3.16  End of central directory record:
 4520 ***
 4521 ***   end of central dir signature    4 bytes  (0x06054b50)
 4522 ***   number of this disk             2 bytes
 4523 ***   number of the disk with the
 4524 ***   start of the central directory  2 bytes
 4525 ***   total number of entries in the
 4526 ***   central directory on this disk  2 bytes
 4527 ***   total number of entries in
 4528 ***   the central directory           2 bytes
 4529 ***   size of the central directory   4 bytes
 4530 ***   offset of start of central
 4531 ***   directory with respect to
 4532 ***   the starting disk number        4 bytes
 4533 ***   .ZIP file comment length        2 bytes
 4534 ***   .ZIP file comment       (variable size)
 4535 */
 4536 typedef struct ZipfileEOCD ZipfileEOCD;
 4537 struct ZipfileEOCD {
 4538   u16 iDisk;
 4539   u16 iFirstDisk;
 4540   u16 nEntry;
 4541   u16 nEntryTotal;
 4542   u32 nSize;
 4543   u32 iOffset;
 4544 };
 4545 
 4546 /*
 4547 *** 4.3.12  Central directory structure:
 4548 ***
 4549 *** ...
 4550 ***
 4551 ***   central file header signature   4 bytes  (0x02014b50)
 4552 ***   version made by                 2 bytes
 4553 ***   version needed to extract       2 bytes
 4554 ***   general purpose bit flag        2 bytes
 4555 ***   compression method              2 bytes
 4556 ***   last mod file time              2 bytes
 4557 ***   last mod file date              2 bytes
 4558 ***   crc-32                          4 bytes
 4559 ***   compressed size                 4 bytes
 4560 ***   uncompressed size               4 bytes
 4561 ***   file name length                2 bytes
 4562 ***   extra field length              2 bytes
 4563 ***   file comment length             2 bytes
 4564 ***   disk number start               2 bytes
 4565 ***   internal file attributes        2 bytes
 4566 ***   external file attributes        4 bytes
 4567 ***   relative offset of local header 4 bytes
 4568 */
 4569 typedef struct ZipfileCDS ZipfileCDS;
 4570 struct ZipfileCDS {
 4571   u16 iVersionMadeBy;
 4572   u16 iVersionExtract;
 4573   u16 flags;
 4574   u16 iCompression;
 4575   u16 mTime;
 4576   u16 mDate;
 4577   u32 crc32;
 4578   u32 szCompressed;
 4579   u32 szUncompressed;
 4580   u16 nFile;
 4581   u16 nExtra;
 4582   u16 nComment;
 4583   u16 iDiskStart;
 4584   u16 iInternalAttr;
 4585   u32 iExternalAttr;
 4586   u32 iOffset;
 4587   char *zFile;                    /* Filename (sqlite3_malloc()) */
 4588 };
 4589 
 4590 /*
 4591 *** 4.3.7  Local file header:
 4592 ***
 4593 ***   local file header signature     4 bytes  (0x04034b50)
 4594 ***   version needed to extract       2 bytes
 4595 ***   general purpose bit flag        2 bytes
 4596 ***   compression method              2 bytes
 4597 ***   last mod file time              2 bytes
 4598 ***   last mod file date              2 bytes
 4599 ***   crc-32                          4 bytes
 4600 ***   compressed size                 4 bytes
 4601 ***   uncompressed size               4 bytes
 4602 ***   file name length                2 bytes
 4603 ***   extra field length              2 bytes
 4604 ***   
 4605 */
 4606 typedef struct ZipfileLFH ZipfileLFH;
 4607 struct ZipfileLFH {
 4608   u16 iVersionExtract;
 4609   u16 flags;
 4610   u16 iCompression;
 4611   u16 mTime;
 4612   u16 mDate;
 4613   u32 crc32;
 4614   u32 szCompressed;
 4615   u32 szUncompressed;
 4616   u16 nFile;
 4617   u16 nExtra;
 4618 };
 4619 
 4620 typedef struct ZipfileEntry ZipfileEntry;
 4621 struct ZipfileEntry {
 4622   ZipfileCDS cds;            /* Parsed CDS record */
 4623   u32 mUnixTime;             /* Modification time, in UNIX format */
 4624   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
 4625   i64 iDataOff;              /* Offset to data in file (if aData==0) */
 4626   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
 4627   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
 4628 };
 4629 
 4630 /* 
 4631 ** Cursor type for zipfile tables.
 4632 */
 4633 typedef struct ZipfileCsr ZipfileCsr;
 4634 struct ZipfileCsr {
 4635   sqlite3_vtab_cursor base;  /* Base class - must be first */
 4636   i64 iId;                   /* Cursor ID */
 4637   u8 bEof;                   /* True when at EOF */
 4638   u8 bNoop;                  /* If next xNext() call is no-op */
 4639 
 4640   /* Used outside of write transactions */
 4641   FILE *pFile;               /* Zip file */
 4642   i64 iNextOff;              /* Offset of next record in central directory */
 4643   ZipfileEOCD eocd;          /* Parse of central directory record */
 4644 
 4645   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
 4646   ZipfileEntry *pCurrent;    /* Current entry */
 4647   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
 4648 };
 4649 
 4650 typedef struct ZipfileTab ZipfileTab;
 4651 struct ZipfileTab {
 4652   sqlite3_vtab base;         /* Base class - must be first */
 4653   char *zFile;               /* Zip file this table accesses (may be NULL) */
 4654   sqlite3 *db;               /* Host database connection */
 4655   u8 *aBuffer;               /* Temporary buffer used for various tasks */
 4656 
 4657   ZipfileCsr *pCsrList;      /* List of cursors */
 4658   i64 iNextCsrid;
 4659 
 4660   /* The following are used by write transactions only */
 4661   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
 4662   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
 4663   FILE *pWriteFd;            /* File handle open on zip archive */
 4664   i64 szCurrent;             /* Current size of zip archive */
 4665   i64 szOrig;                /* Size of archive at start of transaction */
 4666 };
 4667 
 4668 /*
 4669 ** Set the error message contained in context ctx to the results of
 4670 ** vprintf(zFmt, ...).
 4671 */
 4672 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
 4673   char *zMsg = 0;
 4674   va_list ap;
 4675   va_start(ap, zFmt);
 4676   zMsg = sqlite3_vmprintf(zFmt, ap);
 4677   sqlite3_result_error(ctx, zMsg, -1);
 4678   sqlite3_free(zMsg);
 4679   va_end(ap);
 4680 }
 4681 
 4682 /*
 4683 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
 4684 ** is not quoted, do nothing.
 4685 */
 4686 static void zipfileDequote(char *zIn){
 4687   char q = zIn[0];
 4688   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
 4689     int iIn = 1;
 4690     int iOut = 0;
 4691     if( q=='[' ) q = ']';
 4692     while( ALWAYS(zIn[iIn]) ){
 4693       char c = zIn[iIn++];
 4694       if( c==q && zIn[iIn++]!=q ) break;
 4695       zIn[iOut++] = c;
 4696     }
 4697     zIn[iOut] = '\0';
 4698   }
 4699 }
 4700 
 4701 /*
 4702 ** Construct a new ZipfileTab virtual table object.
 4703 ** 
 4704 **   argv[0]   -> module name  ("zipfile")
 4705 **   argv[1]   -> database name
 4706 **   argv[2]   -> table name
 4707 **   argv[...] -> "column name" and other module argument fields.
 4708 */
 4709 static int zipfileConnect(
 4710   sqlite3 *db,
 4711   void *pAux,
 4712   int argc, const char *const*argv,
 4713   sqlite3_vtab **ppVtab,
 4714   char **pzErr
 4715 ){
 4716   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
 4717   int nFile = 0;
 4718   const char *zFile = 0;
 4719   ZipfileTab *pNew = 0;
 4720   int rc;
 4721 
 4722   /* If the table name is not "zipfile", require that the argument be
 4723   ** specified. This stops zipfile tables from being created as:
 4724   **
 4725   **   CREATE VIRTUAL TABLE zzz USING zipfile();
 4726   **
 4727   ** It does not prevent:
 4728   **
 4729   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
 4730   */
 4731   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
 4732   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
 4733     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
 4734     return SQLITE_ERROR;
 4735   }
 4736 
 4737   if( argc>3 ){
 4738     zFile = argv[3];
 4739     nFile = (int)strlen(zFile)+1;
 4740   }
 4741 
 4742   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
 4743   if( rc==SQLITE_OK ){
 4744     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
 4745     if( pNew==0 ) return SQLITE_NOMEM;
 4746     memset(pNew, 0, nByte+nFile);
 4747     pNew->db = db;
 4748     pNew->aBuffer = (u8*)&pNew[1];
 4749     if( zFile ){
 4750       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
 4751       memcpy(pNew->zFile, zFile, nFile);
 4752       zipfileDequote(pNew->zFile);
 4753     }
 4754   }
 4755   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
 4756   *ppVtab = (sqlite3_vtab*)pNew;
 4757   return rc;
 4758 }
 4759 
 4760 /*
 4761 ** Free the ZipfileEntry structure indicated by the only argument.
 4762 */
 4763 static void zipfileEntryFree(ZipfileEntry *p){
 4764   if( p ){
 4765     sqlite3_free(p->cds.zFile);
 4766     sqlite3_free(p);
 4767   }
 4768 }
 4769 
 4770 /*
 4771 ** Release resources that should be freed at the end of a write 
 4772 ** transaction.
 4773 */
 4774 static void zipfileCleanupTransaction(ZipfileTab *pTab){
 4775   ZipfileEntry *pEntry;
 4776   ZipfileEntry *pNext;
 4777 
 4778   if( pTab->pWriteFd ){
 4779     fclose(pTab->pWriteFd);
 4780     pTab->pWriteFd = 0;
 4781   }
 4782   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
 4783     pNext = pEntry->pNext;
 4784     zipfileEntryFree(pEntry);
 4785   }
 4786   pTab->pFirstEntry = 0;
 4787   pTab->pLastEntry = 0;
 4788   pTab->szCurrent = 0;
 4789   pTab->szOrig = 0;
 4790 }
 4791 
 4792 /*
 4793 ** This method is the destructor for zipfile vtab objects.
 4794 */
 4795 static int zipfileDisconnect(sqlite3_vtab *pVtab){
 4796   zipfileCleanupTransaction((ZipfileTab*)pVtab);
 4797   sqlite3_free(pVtab);
 4798   return SQLITE_OK;
 4799 }
 4800 
 4801 /*
 4802 ** Constructor for a new ZipfileCsr object.
 4803 */
 4804 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
 4805   ZipfileTab *pTab = (ZipfileTab*)p;
 4806   ZipfileCsr *pCsr;
 4807   pCsr = sqlite3_malloc(sizeof(*pCsr));
 4808   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
 4809   if( pCsr==0 ){
 4810     return SQLITE_NOMEM;
 4811   }
 4812   memset(pCsr, 0, sizeof(*pCsr));
 4813   pCsr->iId = ++pTab->iNextCsrid;
 4814   pCsr->pCsrNext = pTab->pCsrList;
 4815   pTab->pCsrList = pCsr;
 4816   return SQLITE_OK;
 4817 }
 4818 
 4819 /*
 4820 ** Reset a cursor back to the state it was in when first returned
 4821 ** by zipfileOpen().
 4822 */
 4823 static void zipfileResetCursor(ZipfileCsr *pCsr){
 4824   ZipfileEntry *p;
 4825   ZipfileEntry *pNext;
 4826 
 4827   pCsr->bEof = 0;
 4828   if( pCsr->pFile ){
 4829     fclose(pCsr->pFile);
 4830     pCsr->pFile = 0;
 4831     zipfileEntryFree(pCsr->pCurrent);
 4832     pCsr->pCurrent = 0;
 4833   }
 4834 
 4835   for(p=pCsr->pFreeEntry; p; p=pNext){
 4836     pNext = p->pNext;
 4837     zipfileEntryFree(p);
 4838   }
 4839 }
 4840 
 4841 /*
 4842 ** Destructor for an ZipfileCsr.
 4843 */
 4844 static int zipfileClose(sqlite3_vtab_cursor *cur){
 4845   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
 4846   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
 4847   ZipfileCsr **pp;
 4848   zipfileResetCursor(pCsr);
 4849 
 4850   /* Remove this cursor from the ZipfileTab.pCsrList list. */
 4851   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
 4852   *pp = pCsr->pCsrNext;
 4853 
 4854   sqlite3_free(pCsr);
 4855   return SQLITE_OK;
 4856 }
 4857 
 4858 /*
 4859 ** Set the error message for the virtual table associated with cursor
 4860 ** pCsr to the results of vprintf(zFmt, ...).
 4861 */
 4862 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
 4863   va_list ap;
 4864   va_start(ap, zFmt);
 4865   sqlite3_free(pTab->base.zErrMsg);
 4866   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
 4867   va_end(ap);
 4868 }
 4869 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
 4870   va_list ap;
 4871   va_start(ap, zFmt);
 4872   sqlite3_free(pCsr->base.pVtab->zErrMsg);
 4873   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
 4874   va_end(ap);
 4875 }
 4876 
 4877 /*
 4878 ** Read nRead bytes of data from offset iOff of file pFile into buffer
 4879 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
 4880 ** otherwise. 
 4881 **
 4882 ** If an error does occur, output variable (*pzErrmsg) may be set to point
 4883 ** to an English language error message. It is the responsibility of the
 4884 ** caller to eventually free this buffer using
 4885 ** sqlite3_free().
 4886 */
 4887 static int zipfileReadData(
 4888   FILE *pFile,                    /* Read from this file */
 4889   u8 *aRead,                      /* Read into this buffer */
 4890   int nRead,                      /* Number of bytes to read */
 4891   i64 iOff,                       /* Offset to read from */
 4892   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
 4893 ){
 4894   size_t n;
 4895   fseek(pFile, (long)iOff, SEEK_SET);
 4896   n = fread(aRead, 1, nRead, pFile);
 4897   if( (int)n!=nRead ){
 4898     *pzErrmsg = sqlite3_mprintf("error in fread()");
 4899     return SQLITE_ERROR;
 4900   }
 4901   return SQLITE_OK;
 4902 }
 4903 
 4904 static int zipfileAppendData(
 4905   ZipfileTab *pTab,
 4906   const u8 *aWrite,
 4907   int nWrite
 4908 ){
 4909   size_t n;
 4910   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
 4911   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
 4912   if( (int)n!=nWrite ){
 4913     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
 4914     return SQLITE_ERROR;
 4915   }
 4916   pTab->szCurrent += nWrite;
 4917   return SQLITE_OK;
 4918 }
 4919 
 4920 /*
 4921 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
 4922 */
 4923 static u16 zipfileGetU16(const u8 *aBuf){
 4924   return (aBuf[1] << 8) + aBuf[0];
 4925 }
 4926 
 4927 /*
 4928 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
 4929 */
 4930 static u32 zipfileGetU32(const u8 *aBuf){
 4931   return ((u32)(aBuf[3]) << 24)
 4932        + ((u32)(aBuf[2]) << 16)
 4933        + ((u32)(aBuf[1]) <<  8)
 4934        + ((u32)(aBuf[0]) <<  0);
 4935 }
 4936 
 4937 /*
 4938 ** Write a 16-bit little endiate integer into buffer aBuf.
 4939 */
 4940 static void zipfilePutU16(u8 *aBuf, u16 val){
 4941   aBuf[0] = val & 0xFF;
 4942   aBuf[1] = (val>>8) & 0xFF;
 4943 }
 4944 
 4945 /*
 4946 ** Write a 32-bit little endiate integer into buffer aBuf.
 4947 */
 4948 static void zipfilePutU32(u8 *aBuf, u32 val){
 4949   aBuf[0] = val & 0xFF;
 4950   aBuf[1] = (val>>8) & 0xFF;
 4951   aBuf[2] = (val>>16) & 0xFF;
 4952   aBuf[3] = (val>>24) & 0xFF;
 4953 }
 4954 
 4955 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
 4956 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
 4957 
 4958 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
 4959 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
 4960 
 4961 /*
 4962 ** Magic numbers used to read CDS records.
 4963 */
 4964 #define ZIPFILE_CDS_NFILE_OFF        28
 4965 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
 4966 
 4967 /*
 4968 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
 4969 ** if the record is not well-formed, or SQLITE_OK otherwise.
 4970 */
 4971 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
 4972   u8 *aRead = aBuf;
 4973   u32 sig = zipfileRead32(aRead);
 4974   int rc = SQLITE_OK;
 4975   if( sig!=ZIPFILE_SIGNATURE_CDS ){
 4976     rc = SQLITE_ERROR;
 4977   }else{
 4978     pCDS->iVersionMadeBy = zipfileRead16(aRead);
 4979     pCDS->iVersionExtract = zipfileRead16(aRead);
 4980     pCDS->flags = zipfileRead16(aRead);
 4981     pCDS->iCompression = zipfileRead16(aRead);
 4982     pCDS->mTime = zipfileRead16(aRead);
 4983     pCDS->mDate = zipfileRead16(aRead);
 4984     pCDS->crc32 = zipfileRead32(aRead);
 4985     pCDS->szCompressed = zipfileRead32(aRead);
 4986     pCDS->szUncompressed = zipfileRead32(aRead);
 4987     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
 4988     pCDS->nFile = zipfileRead16(aRead);
 4989     pCDS->nExtra = zipfileRead16(aRead);
 4990     pCDS->nComment = zipfileRead16(aRead);
 4991     pCDS->iDiskStart = zipfileRead16(aRead);
 4992     pCDS->iInternalAttr = zipfileRead16(aRead);
 4993     pCDS->iExternalAttr = zipfileRead32(aRead);
 4994     pCDS->iOffset = zipfileRead32(aRead);
 4995     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
 4996   }
 4997 
 4998   return rc;
 4999 }
 5000 
 5001 /*
 5002 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
 5003 ** if the record is not well-formed, or SQLITE_OK otherwise.
 5004 */
 5005 static int zipfileReadLFH(
 5006   u8 *aBuffer,
 5007   ZipfileLFH *pLFH
 5008 ){
 5009   u8 *aRead = aBuffer;
 5010   int rc = SQLITE_OK;
 5011 
 5012   u32 sig = zipfileRead32(aRead);
 5013   if( sig!=ZIPFILE_SIGNATURE_LFH ){
 5014     rc = SQLITE_ERROR;
 5015   }else{
 5016     pLFH->iVersionExtract = zipfileRead16(aRead);
 5017     pLFH->flags = zipfileRead16(aRead);
 5018     pLFH->iCompression = zipfileRead16(aRead);
 5019     pLFH->mTime = zipfileRead16(aRead);
 5020     pLFH->mDate = zipfileRead16(aRead);
 5021     pLFH->crc32 = zipfileRead32(aRead);
 5022     pLFH->szCompressed = zipfileRead32(aRead);
 5023     pLFH->szUncompressed = zipfileRead32(aRead);
 5024     pLFH->nFile = zipfileRead16(aRead);
 5025     pLFH->nExtra = zipfileRead16(aRead);
 5026   }
 5027   return rc;
 5028 }
 5029 
 5030 
 5031 /*
 5032 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
 5033 ** Scan through this buffer to find an "extra-timestamp" field. If one
 5034 ** exists, extract the 32-bit modification-timestamp from it and store
 5035 ** the value in output parameter *pmTime.
 5036 **
 5037 ** Zero is returned if no extra-timestamp record could be found (and so
 5038 ** *pmTime is left unchanged), or non-zero otherwise.
 5039 **
 5040 ** The general format of an extra field is:
 5041 **
 5042 **   Header ID    2 bytes
 5043 **   Data Size    2 bytes
 5044 **   Data         N bytes
 5045 */
 5046 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
 5047   int ret = 0;
 5048   u8 *p = aExtra;
 5049   u8 *pEnd = &aExtra[nExtra];
 5050 
 5051   while( p<pEnd ){
 5052     u16 id = zipfileRead16(p);
 5053     u16 nByte = zipfileRead16(p);
 5054 
 5055     switch( id ){
 5056       case ZIPFILE_EXTRA_TIMESTAMP: {
 5057         u8 b = p[0];
 5058         if( b & 0x01 ){     /* 0x01 -> modtime is present */
 5059           *pmTime = zipfileGetU32(&p[1]);
 5060           ret = 1;
 5061         }
 5062         break;
 5063       }
 5064     }
 5065 
 5066     p += nByte;
 5067   }
 5068   return ret;
 5069 }
 5070 
 5071 /*
 5072 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
 5073 ** fields of the CDS structure passed as the only argument to a 32-bit
 5074 ** UNIX seconds-since-the-epoch timestamp. Return the result.
 5075 **
 5076 ** "Standard" MS-DOS time format:
 5077 **
 5078 **   File modification time:
 5079 **     Bits 00-04: seconds divided by 2
 5080 **     Bits 05-10: minute
 5081 **     Bits 11-15: hour
 5082 **   File modification date:
 5083 **     Bits 00-04: day
 5084 **     Bits 05-08: month (1-12)
 5085 **     Bits 09-15: years from 1980 
 5086 **
 5087 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
 5088 */
 5089 static u32 zipfileMtime(ZipfileCDS *pCDS){
 5090   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
 5091   int M = ((pCDS->mDate >> 5) & 0x0F);
 5092   int D = (pCDS->mDate & 0x1F);
 5093   int B = -13;
 5094 
 5095   int sec = (pCDS->mTime & 0x1F)*2;
 5096   int min = (pCDS->mTime >> 5) & 0x3F;
 5097   int hr = (pCDS->mTime >> 11) & 0x1F;
 5098   i64 JD;
 5099 
 5100   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
 5101 
 5102   /* Calculate the JD in seconds for noon on the day in question */
 5103   if( M<3 ){
 5104     Y = Y-1;
 5105     M = M+12;
 5106   }
 5107   JD = (i64)(24*60*60) * (
 5108       (int)(365.25 * (Y + 4716))
 5109     + (int)(30.6001 * (M + 1))
 5110     + D + B - 1524
 5111   );
 5112 
 5113   /* Correct the JD for the time within the day */
 5114   JD += (hr-12) * 3600 + min * 60 + sec;
 5115 
 5116   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
 5117   return (u32)(JD - (i64)(24405875) * 24*60*6);
 5118 }
 5119 
 5120 /*
 5121 ** The opposite of zipfileMtime(). This function populates the mTime and
 5122 ** mDate fields of the CDS structure passed as the first argument according
 5123 ** to the UNIX timestamp value passed as the second.
 5124 */
 5125 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
 5126   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
 5127   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
 5128 
 5129   int A, B, C, D, E;
 5130   int yr, mon, day;
 5131   int hr, min, sec;
 5132 
 5133   A = (int)((JD - 1867216.25)/36524.25);
 5134   A = (int)(JD + 1 + A - (A/4));
 5135   B = A + 1524;
 5136   C = (int)((B - 122.1)/365.25);
 5137   D = (36525*(C&32767))/100;
 5138   E = (int)((B-D)/30.6001);
 5139 
 5140   day = B - D - (int)(30.6001*E);
 5141   mon = (E<14 ? E-1 : E-13);
 5142   yr = mon>2 ? C-4716 : C-4715;
 5143 
 5144   hr = (mUnixTime % (24*60*60)) / (60*60);
 5145   min = (mUnixTime % (60*60)) / 60;
 5146   sec = (mUnixTime % 60);
 5147 
 5148   if( yr>=1980 ){
 5149     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
 5150     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
 5151   }else{
 5152     pCds->mDate = pCds->mTime = 0;
 5153   }
 5154 
 5155   assert( mUnixTime<315507600 
 5156        || mUnixTime==zipfileMtime(pCds) 
 5157        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
 5158        /* || (mUnixTime % 2) */
 5159   );
 5160 }
 5161 
 5162 /*
 5163 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
 5164 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
 5165 ** then pFile is a file-handle open on a zip file. In either case, this
 5166 ** function creates a ZipfileEntry object based on the zip archive entry
 5167 ** for which the CDS record is at offset iOff.
 5168 **
 5169 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
 5170 ** the new object. Otherwise, an SQLite error code is returned and the
 5171 ** final value of (*ppEntry) undefined.
 5172 */
 5173 static int zipfileGetEntry(
 5174   ZipfileTab *pTab,               /* Store any error message here */
 5175   const u8 *aBlob,                /* Pointer to in-memory file image */
 5176   int nBlob,                      /* Size of aBlob[] in bytes */
 5177   FILE *pFile,                    /* If aBlob==0, read from this file */
 5178   i64 iOff,                       /* Offset of CDS record */
 5179   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
 5180 ){
 5181   u8 *aRead;
 5182   char **pzErr = &pTab->base.zErrMsg;
 5183   int rc = SQLITE_OK;
 5184 
 5185   if( aBlob==0 ){
 5186     aRead = pTab->aBuffer;
 5187     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
 5188   }else{
 5189     aRead = (u8*)&aBlob[iOff];
 5190   }
 5191 
 5192   if( rc==SQLITE_OK ){
 5193     sqlite3_int64 nAlloc;
 5194     ZipfileEntry *pNew;
 5195 
 5196     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
 5197     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
 5198     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
 5199 
 5200     nAlloc = sizeof(ZipfileEntry) + nExtra;
 5201     if( aBlob ){
 5202       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
 5203     }
 5204 
 5205     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
 5206     if( pNew==0 ){
 5207       rc = SQLITE_NOMEM;
 5208     }else{
 5209       memset(pNew, 0, sizeof(ZipfileEntry));
 5210       rc = zipfileReadCDS(aRead, &pNew->cds);
 5211       if( rc!=SQLITE_OK ){
 5212         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
 5213       }else if( aBlob==0 ){
 5214         rc = zipfileReadData(
 5215             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
 5216         );
 5217       }else{
 5218         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
 5219       }
 5220     }
 5221 
 5222     if( rc==SQLITE_OK ){
 5223       u32 *pt = &pNew->mUnixTime;
 5224       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
 5225       pNew->aExtra = (u8*)&pNew[1];
 5226       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
 5227       if( pNew->cds.zFile==0 ){
 5228         rc = SQLITE_NOMEM;
 5229       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
 5230         pNew->mUnixTime = zipfileMtime(&pNew->cds);
 5231       }
 5232     }
 5233 
 5234     if( rc==SQLITE_OK ){
 5235       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
 5236       ZipfileLFH lfh;
 5237       if( pFile ){
 5238         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
 5239       }else{
 5240         aRead = (u8*)&aBlob[pNew->cds.iOffset];
 5241       }
 5242 
 5243       rc = zipfileReadLFH(aRead, &lfh);
 5244       if( rc==SQLITE_OK ){
 5245         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
 5246         pNew->iDataOff += lfh.nFile + lfh.nExtra;
 5247         if( aBlob && pNew->cds.szCompressed ){
 5248           pNew->aData = &pNew->aExtra[nExtra];
 5249           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
 5250         }
 5251       }else{
 5252         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
 5253             (int)pNew->cds.iOffset
 5254         );
 5255       }
 5256     }
 5257 
 5258     if( rc!=SQLITE_OK ){
 5259       zipfileEntryFree(pNew);
 5260     }else{
 5261       *ppEntry = pNew;
 5262     }
 5263   }
 5264 
 5265   return rc;
 5266 }
 5267 
 5268 /*
 5269 ** Advance an ZipfileCsr to its next row of output.
 5270 */
 5271 static int zipfileNext(sqlite3_vtab_cursor *cur){
 5272   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
 5273   int rc = SQLITE_OK;
 5274 
 5275   if( pCsr->pFile ){
 5276     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
 5277     zipfileEntryFree(pCsr->pCurrent);
 5278     pCsr->pCurrent = 0;
 5279     if( pCsr->iNextOff>=iEof ){
 5280       pCsr->bEof = 1;
 5281     }else{
 5282       ZipfileEntry *p = 0;
 5283       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
 5284       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
 5285       if( rc==SQLITE_OK ){
 5286         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
 5287         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
 5288       }
 5289       pCsr->pCurrent = p;
 5290     }
 5291   }else{
 5292     if( !pCsr->bNoop ){
 5293       pCsr->pCurrent = pCsr->pCurrent->pNext;
 5294     }
 5295     if( pCsr->pCurrent==0 ){
 5296       pCsr->bEof = 1;
 5297     }
 5298   }
 5299 
 5300   pCsr->bNoop = 0;
 5301   return rc;
 5302 }
 5303 
 5304 static void zipfileFree(void *p) { 
 5305   sqlite3_free(p); 
 5306 }
 5307 
 5308 /*
 5309 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
 5310 ** size is nOut bytes. This function uncompresses the data and sets the
 5311 ** return value in context pCtx to the result (a blob).
 5312 **
 5313 ** If an error occurs, an error code is left in pCtx instead.
 5314 */
 5315 static void zipfileInflate(
 5316   sqlite3_context *pCtx,          /* Store result here */
 5317   const u8 *aIn,                  /* Compressed data */
 5318   int nIn,                        /* Size of buffer aIn[] in bytes */
 5319   int nOut                        /* Expected output size */
 5320 ){
 5321   u8 *aRes = sqlite3_malloc(nOut);
 5322   if( aRes==0 ){
 5323     sqlite3_result_error_nomem(pCtx);
 5324   }else{
 5325     int err;
 5326     z_stream str;
 5327     memset(&str, 0, sizeof(str));
 5328 
 5329     str.next_in = (Byte*)aIn;
 5330     str.avail_in = nIn;
 5331     str.next_out = (Byte*)aRes;
 5332     str.avail_out = nOut;
 5333 
 5334     err = inflateInit2(&str, -15);
 5335     if( err!=Z_OK ){
 5336       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
 5337     }else{
 5338       err = inflate(&str, Z_NO_FLUSH);
 5339       if( err!=Z_STREAM_END ){
 5340         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
 5341       }else{
 5342         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
 5343         aRes = 0;
 5344       }
 5345     }
 5346     sqlite3_free(aRes);
 5347     inflateEnd(&str);
 5348   }
 5349 }
 5350 
 5351 /*
 5352 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
 5353 ** compresses it and sets (*ppOut) to point to a buffer containing the
 5354 ** compressed data. The caller is responsible for eventually calling
 5355 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
 5356 ** is set to the size of buffer (*ppOut) in bytes.
 5357 **
 5358 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
 5359 ** code is returned and an error message left in virtual-table handle
 5360 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
 5361 ** case.
 5362 */
 5363 static int zipfileDeflate(
 5364   const u8 *aIn, int nIn,         /* Input */
 5365   u8 **ppOut, int *pnOut,         /* Output */
 5366   char **pzErr                    /* OUT: Error message */
 5367 ){
 5368   int rc = SQLITE_OK;
 5369   sqlite3_int64 nAlloc;
 5370   z_stream str;
 5371   u8 *aOut;
 5372 
 5373   memset(&str, 0, sizeof(str));
 5374   str.next_in = (Bytef*)aIn;
 5375   str.avail_in = nIn;
 5376   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
 5377 
 5378   nAlloc = deflateBound(&str, nIn);
 5379   aOut = (u8*)sqlite3_malloc64(nAlloc);
 5380   if( aOut==0 ){
 5381     rc = SQLITE_NOMEM;
 5382   }else{
 5383     int res;
 5384     str.next_out = aOut;
 5385     str.avail_out = nAlloc;
 5386     res = deflate(&str, Z_FINISH);
 5387     if( res==Z_STREAM_END ){
 5388       *ppOut = aOut;
 5389       *pnOut = (int)str.total_out;
 5390     }else{
 5391       sqlite3_free(aOut);
 5392       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
 5393       rc = SQLITE_ERROR;
 5394     }
 5395     deflateEnd(&str);
 5396   }
 5397 
 5398   return rc;
 5399 }
 5400 
 5401 
 5402 /*
 5403 ** Return values of columns for the row at which the series_cursor
 5404 ** is currently pointing.
 5405 */
 5406 static int zipfileColumn(
 5407   sqlite3_vtab_cursor *cur,   /* The cursor */
 5408   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
 5409   int i                       /* Which column to return */
 5410 ){
 5411   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
 5412   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
 5413   int rc = SQLITE_OK;
 5414   switch( i ){
 5415     case 0:   /* name */
 5416       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
 5417       break;
 5418     case 1:   /* mode */
 5419       /* TODO: Whether or not the following is correct surely depends on
 5420       ** the platform on which the archive was created.  */
 5421       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
 5422       break;
 5423     case 2: { /* mtime */
 5424       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
 5425       break;
 5426     }
 5427     case 3: { /* sz */
 5428       if( sqlite3_vtab_nochange(ctx)==0 ){
 5429         sqlite3_result_int64(ctx, pCDS->szUncompressed);
 5430       }
 5431       break;
 5432     }
 5433     case 4:   /* rawdata */
 5434       if( sqlite3_vtab_nochange(ctx) ) break;
 5435     case 5: { /* data */
 5436       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
 5437         int sz = pCDS->szCompressed;
 5438         int szFinal = pCDS->szUncompressed;
 5439         if( szFinal>0 ){
 5440           u8 *aBuf;
 5441           u8 *aFree = 0;
 5442           if( pCsr->pCurrent->aData ){
 5443             aBuf = pCsr->pCurrent->aData;
 5444           }else{
 5445             aBuf = aFree = sqlite3_malloc64(sz);
 5446             if( aBuf==0 ){
 5447               rc = SQLITE_NOMEM;
 5448             }else{
 5449               FILE *pFile = pCsr->pFile;
 5450               if( pFile==0 ){
 5451                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
 5452               }
 5453               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
 5454                   &pCsr->base.pVtab->zErrMsg
 5455               );
 5456             }
 5457           }
 5458           if( rc==SQLITE_OK ){
 5459             if( i==5 && pCDS->iCompression ){
 5460               zipfileInflate(ctx, aBuf, sz, szFinal);
 5461             }else{
 5462               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
 5463             }
 5464           }
 5465           sqlite3_free(aFree);
 5466         }else{
 5467           /* Figure out if this is a directory or a zero-sized file. Consider
 5468           ** it to be a directory either if the mode suggests so, or if
 5469           ** the final character in the name is '/'.  */
 5470           u32 mode = pCDS->iExternalAttr >> 16;
 5471           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
 5472             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
 5473           }
 5474         }
 5475       }
 5476       break;
 5477     }
 5478     case 6:   /* method */
 5479       sqlite3_result_int(ctx, pCDS->iCompression);
 5480       break;
 5481     default:  /* z */
 5482       assert( i==7 );
 5483       sqlite3_result_int64(ctx, pCsr->iId);
 5484       break;
 5485   }
 5486 
 5487   return rc;
 5488 }
 5489 
 5490 /*
 5491 ** Return TRUE if the cursor is at EOF.
 5492 */
 5493 static int zipfileEof(sqlite3_vtab_cursor *cur){
 5494   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
 5495   return pCsr->bEof;
 5496 }
 5497 
 5498 /*
 5499 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
 5500 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
 5501 ** is guaranteed to be a file-handle open on a zip file.
 5502 **
 5503 ** This function attempts to locate the EOCD record within the zip archive
 5504 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
 5505 ** returned if successful. Otherwise, an SQLite error code is returned and
 5506 ** an English language error message may be left in virtual-table pTab.
 5507 */
 5508 static int zipfileReadEOCD(
 5509   ZipfileTab *pTab,               /* Return errors here */
 5510   const u8 *aBlob,                /* Pointer to in-memory file image */
 5511   int nBlob,                      /* Size of aBlob[] in bytes */
 5512   FILE *pFile,                    /* Read from this file if aBlob==0 */
 5513   ZipfileEOCD *pEOCD              /* Object to populate */
 5514 ){
 5515   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
 5516   int nRead;                      /* Bytes to read from file */
 5517   int rc = SQLITE_OK;
 5518 
 5519   if( aBlob==0 ){
 5520     i64 iOff;                     /* Offset to read from */
 5521     i64 szFile;                   /* Total size of file in bytes */
 5522     fseek(pFile, 0, SEEK_END);
 5523     szFile = (i64)ftell(pFile);
 5524     if( szFile==0 ){
 5525       memset(pEOCD, 0, sizeof(ZipfileEOCD));
 5526       return SQLITE_OK;
 5527     }
 5528     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
 5529     iOff = szFile - nRead;
 5530     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
 5531   }else{
 5532     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
 5533     aRead = (u8*)&aBlob[nBlob-nRead