shell.c revision 369950
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"
90typedef sqlite3_int64 i64;
91typedef sqlite3_uint64 u64;
92typedef 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 */
212extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
213extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
214extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
215extern 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
225static void setBinaryMode(FILE *file, int isOutput){
226  if( isOutput ) fflush(file);
227  _setmode(_fileno(file), _O_BINARY);
228}
229static 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 */
240static int enableTimer = 0;
241
242/* Return the current wall-clock time */
243static 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__)
263struct 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 */
271static struct rusage sBegin;  /* CPU time at start */
272static sqlite3_int64 iBegin;  /* Wall-clock time at start */
273
274/*
275** Begin timing an operation
276*/
277static 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 */
285static 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*/
293static 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 */
312static HANDLE hProcess;
313static FILETIME ftKernelBegin;
314static FILETIME ftUserBegin;
315static sqlite3_int64 ftWallBegin;
316typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
317                                    LPFILETIME, LPFILETIME);
318static 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*/
324static 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*/
353static 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 */
363static 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*/
372static 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*/
408static 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*/
414static 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*/
421static 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*/
428static sqlite3 *globalDb = 0;
429
430/*
431** True if an interrupt (Control-C) has been received.
432*/
433static volatile int seenInterrupt = 0;
434
435#ifdef SQLITE_DEBUG
436/*
437** Out-of-memory simulator variables
438*/
439static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
440static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
441static 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*/
448static char *Argv0;
449
450/*
451** Prompt strings. Initialized in main. Settable with
452**   .prompt main continue
453*/
454static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
455static 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)
463void 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. */
490static 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*/
500void 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*/
513static 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 */
529static 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
542static 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
552static 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*/
570static 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  for(i=n=0; zUtf[i]; i++){
575    if( (zUtf[i]&0xc0)!=0x80 ){
576      n++;
577      if( n==aw ){
578        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
579        break;
580      }
581    }
582  }
583  if( n>=aw ){
584    utf8_printf(pOut, "%.*s", i, zUtf);
585  }else if( w<0 ){
586    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
587  }else{
588    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
589  }
590}
591
592
593/*
594** Determines if a string is a number of not.
595*/
596static int isNumber(const char *z, int *realnum){
597  if( *z=='-' || *z=='+' ) z++;
598  if( !IsDigit(*z) ){
599    return 0;
600  }
601  z++;
602  if( realnum ) *realnum = 0;
603  while( IsDigit(*z) ){ z++; }
604  if( *z=='.' ){
605    z++;
606    if( !IsDigit(*z) ) return 0;
607    while( IsDigit(*z) ){ z++; }
608    if( realnum ) *realnum = 1;
609  }
610  if( *z=='e' || *z=='E' ){
611    z++;
612    if( *z=='+' || *z=='-' ) z++;
613    if( !IsDigit(*z) ) return 0;
614    while( IsDigit(*z) ){ z++; }
615    if( realnum ) *realnum = 1;
616  }
617  return *z==0;
618}
619
620/*
621** Compute a string length that is limited to what can be stored in
622** lower 30 bits of a 32-bit signed integer.
623*/
624static int strlen30(const char *z){
625  const char *z2 = z;
626  while( *z2 ){ z2++; }
627  return 0x3fffffff & (int)(z2 - z);
628}
629
630/*
631** Return the length of a string in characters.  Multibyte UTF8 characters
632** count as a single character.
633*/
634static int strlenChar(const char *z){
635  int n = 0;
636  while( *z ){
637    if( (0xc0&*(z++))!=0x80 ) n++;
638  }
639  return n;
640}
641
642/*
643** Return true if zFile does not exist or if it is not an ordinary file.
644*/
645#ifdef _WIN32
646# define notNormalFile(X) 0
647#else
648static int notNormalFile(const char *zFile){
649  struct stat x;
650  int rc;
651  memset(&x, 0, sizeof(x));
652  rc = stat(zFile, &x);
653  return rc || !S_ISREG(x.st_mode);
654}
655#endif
656
657/*
658** This routine reads a line of text from FILE in, stores
659** the text in memory obtained from malloc() and returns a pointer
660** to the text.  NULL is returned at end of file, or if malloc()
661** fails.
662**
663** If zLine is not NULL then it is a malloced buffer returned from
664** a previous call to this routine that may be reused.
665*/
666static char *local_getline(char *zLine, FILE *in){
667  int nLine = zLine==0 ? 0 : 100;
668  int n = 0;
669
670  while( 1 ){
671    if( n+100>nLine ){
672      nLine = nLine*2 + 100;
673      zLine = realloc(zLine, nLine);
674      if( zLine==0 ) shell_out_of_memory();
675    }
676    if( fgets(&zLine[n], nLine - n, in)==0 ){
677      if( n==0 ){
678        free(zLine);
679        return 0;
680      }
681      zLine[n] = 0;
682      break;
683    }
684    while( zLine[n] ) n++;
685    if( n>0 && zLine[n-1]=='\n' ){
686      n--;
687      if( n>0 && zLine[n-1]=='\r' ) n--;
688      zLine[n] = 0;
689      break;
690    }
691  }
692#if defined(_WIN32) || defined(WIN32)
693  /* For interactive input on Windows systems, translate the
694  ** multi-byte characterset characters into UTF-8. */
695  if( stdin_is_interactive && in==stdin ){
696    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
697    if( zTrans ){
698      int nTrans = strlen30(zTrans)+1;
699      if( nTrans>nLine ){
700        zLine = realloc(zLine, nTrans);
701        if( zLine==0 ) shell_out_of_memory();
702      }
703      memcpy(zLine, zTrans, nTrans);
704      sqlite3_free(zTrans);
705    }
706  }
707#endif /* defined(_WIN32) || defined(WIN32) */
708  return zLine;
709}
710
711/*
712** Retrieve a single line of input text.
713**
714** If in==0 then read from standard input and prompt before each line.
715** If isContinuation is true, then a continuation prompt is appropriate.
716** If isContinuation is zero, then the main prompt should be used.
717**
718** If zPrior is not NULL then it is a buffer from a prior call to this
719** routine that can be reused.
720**
721** The result is stored in space obtained from malloc() and must either
722** be freed by the caller or else passed back into this routine via the
723** zPrior argument for reuse.
724*/
725static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
726  char *zPrompt;
727  char *zResult;
728  if( in!=0 ){
729    zResult = local_getline(zPrior, in);
730  }else{
731    zPrompt = isContinuation ? continuePrompt : mainPrompt;
732#if SHELL_USE_LOCAL_GETLINE
733    printf("%s", zPrompt);
734    fflush(stdout);
735    zResult = local_getline(zPrior, stdin);
736#else
737    free(zPrior);
738    zResult = shell_readline(zPrompt);
739    if( zResult && *zResult ) shell_add_history(zResult);
740#endif
741  }
742  return zResult;
743}
744
745
746/*
747** Return the value of a hexadecimal digit.  Return -1 if the input
748** is not a hex digit.
749*/
750static int hexDigitValue(char c){
751  if( c>='0' && c<='9' ) return c - '0';
752  if( c>='a' && c<='f' ) return c - 'a' + 10;
753  if( c>='A' && c<='F' ) return c - 'A' + 10;
754  return -1;
755}
756
757/*
758** Interpret zArg as an integer value, possibly with suffixes.
759*/
760static sqlite3_int64 integerValue(const char *zArg){
761  sqlite3_int64 v = 0;
762  static const struct { char *zSuffix; int iMult; } aMult[] = {
763    { "KiB", 1024 },
764    { "MiB", 1024*1024 },
765    { "GiB", 1024*1024*1024 },
766    { "KB",  1000 },
767    { "MB",  1000000 },
768    { "GB",  1000000000 },
769    { "K",   1000 },
770    { "M",   1000000 },
771    { "G",   1000000000 },
772  };
773  int i;
774  int isNeg = 0;
775  if( zArg[0]=='-' ){
776    isNeg = 1;
777    zArg++;
778  }else if( zArg[0]=='+' ){
779    zArg++;
780  }
781  if( zArg[0]=='0' && zArg[1]=='x' ){
782    int x;
783    zArg += 2;
784    while( (x = hexDigitValue(zArg[0]))>=0 ){
785      v = (v<<4) + x;
786      zArg++;
787    }
788  }else{
789    while( IsDigit(zArg[0]) ){
790      v = v*10 + zArg[0] - '0';
791      zArg++;
792    }
793  }
794  for(i=0; i<ArraySize(aMult); i++){
795    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
796      v *= aMult[i].iMult;
797      break;
798    }
799  }
800  return isNeg? -v : v;
801}
802
803/*
804** A variable length string to which one can append text.
805*/
806typedef struct ShellText ShellText;
807struct ShellText {
808  char *z;
809  int n;
810  int nAlloc;
811};
812
813/*
814** Initialize and destroy a ShellText object
815*/
816static void initText(ShellText *p){
817  memset(p, 0, sizeof(*p));
818}
819static void freeText(ShellText *p){
820  free(p->z);
821  initText(p);
822}
823
824/* zIn is either a pointer to a NULL-terminated string in memory obtained
825** from malloc(), or a NULL pointer. The string pointed to by zAppend is
826** added to zIn, and the result returned in memory obtained from malloc().
827** zIn, if it was not NULL, is freed.
828**
829** If the third argument, quote, is not '\0', then it is used as a
830** quote character for zAppend.
831*/
832static void appendText(ShellText *p, char const *zAppend, char quote){
833  int len;
834  int i;
835  int nAppend = strlen30(zAppend);
836
837  len = nAppend+p->n+1;
838  if( quote ){
839    len += 2;
840    for(i=0; i<nAppend; i++){
841      if( zAppend[i]==quote ) len++;
842    }
843  }
844
845  if( p->n+len>=p->nAlloc ){
846    p->nAlloc = p->nAlloc*2 + len + 20;
847    p->z = realloc(p->z, p->nAlloc);
848    if( p->z==0 ) shell_out_of_memory();
849  }
850
851  if( quote ){
852    char *zCsr = p->z+p->n;
853    *zCsr++ = quote;
854    for(i=0; i<nAppend; i++){
855      *zCsr++ = zAppend[i];
856      if( zAppend[i]==quote ) *zCsr++ = quote;
857    }
858    *zCsr++ = quote;
859    p->n = (int)(zCsr - p->z);
860    *zCsr = '\0';
861  }else{
862    memcpy(p->z+p->n, zAppend, nAppend);
863    p->n += nAppend;
864    p->z[p->n] = '\0';
865  }
866}
867
868/*
869** Attempt to determine if identifier zName needs to be quoted, either
870** because it contains non-alphanumeric characters, or because it is an
871** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
872** that quoting is required.
873**
874** Return '"' if quoting is required.  Return 0 if no quoting is required.
875*/
876static char quoteChar(const char *zName){
877  int i;
878  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
879  for(i=0; zName[i]; i++){
880    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
881  }
882  return sqlite3_keyword_check(zName, i) ? '"' : 0;
883}
884
885/*
886** Construct a fake object name and column list to describe the structure
887** of the view, virtual table, or table valued function zSchema.zName.
888*/
889static char *shellFakeSchema(
890  sqlite3 *db,            /* The database connection containing the vtab */
891  const char *zSchema,    /* Schema of the database holding the vtab */
892  const char *zName       /* The name of the virtual table */
893){
894  sqlite3_stmt *pStmt = 0;
895  char *zSql;
896  ShellText s;
897  char cQuote;
898  char *zDiv = "(";
899  int nRow = 0;
900
901  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
902                         zSchema ? zSchema : "main", zName);
903  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
904  sqlite3_free(zSql);
905  initText(&s);
906  if( zSchema ){
907    cQuote = quoteChar(zSchema);
908    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
909    appendText(&s, zSchema, cQuote);
910    appendText(&s, ".", 0);
911  }
912  cQuote = quoteChar(zName);
913  appendText(&s, zName, cQuote);
914  while( sqlite3_step(pStmt)==SQLITE_ROW ){
915    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
916    nRow++;
917    appendText(&s, zDiv, 0);
918    zDiv = ",";
919    cQuote = quoteChar(zCol);
920    appendText(&s, zCol, cQuote);
921  }
922  appendText(&s, ")", 0);
923  sqlite3_finalize(pStmt);
924  if( nRow==0 ){
925    freeText(&s);
926    s.z = 0;
927  }
928  return s.z;
929}
930
931/*
932** SQL function:  shell_module_schema(X)
933**
934** Return a fake schema for the table-valued function or eponymous virtual
935** table X.
936*/
937static void shellModuleSchema(
938  sqlite3_context *pCtx,
939  int nVal,
940  sqlite3_value **apVal
941){
942  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
943  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
944  UNUSED_PARAMETER(nVal);
945  if( zFake ){
946    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
947                        -1, sqlite3_free);
948    free(zFake);
949  }
950}
951
952/*
953** SQL function:  shell_add_schema(S,X)
954**
955** Add the schema name X to the CREATE statement in S and return the result.
956** Examples:
957**
958**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
959**
960** Also works on
961**
962**    CREATE INDEX
963**    CREATE UNIQUE INDEX
964**    CREATE VIEW
965**    CREATE TRIGGER
966**    CREATE VIRTUAL TABLE
967**
968** This UDF is used by the .schema command to insert the schema name of
969** attached databases into the middle of the sqlite_schema.sql field.
970*/
971static void shellAddSchemaName(
972  sqlite3_context *pCtx,
973  int nVal,
974  sqlite3_value **apVal
975){
976  static const char *aPrefix[] = {
977     "TABLE",
978     "INDEX",
979     "UNIQUE INDEX",
980     "VIEW",
981     "TRIGGER",
982     "VIRTUAL TABLE"
983  };
984  int i = 0;
985  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
986  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
987  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
988  sqlite3 *db = sqlite3_context_db_handle(pCtx);
989  UNUSED_PARAMETER(nVal);
990  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
991    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
992      int n = strlen30(aPrefix[i]);
993      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
994        char *z = 0;
995        char *zFake = 0;
996        if( zSchema ){
997          char cQuote = quoteChar(zSchema);
998          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
999            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1000          }else{
1001            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1002          }
1003        }
1004        if( zName
1005         && aPrefix[i][0]=='V'
1006         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1007        ){
1008          if( z==0 ){
1009            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1010          }else{
1011            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1012          }
1013          free(zFake);
1014        }
1015        if( z ){
1016          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1017          return;
1018        }
1019      }
1020    }
1021  }
1022  sqlite3_result_value(pCtx, apVal[0]);
1023}
1024
1025/*
1026** The source code for several run-time loadable extensions is inserted
1027** below by the ../tool/mkshellc.tcl script.  Before processing that included
1028** code, we need to override some macros to make the included program code
1029** work here in the middle of this regular program.
1030*/
1031#define SQLITE_EXTENSION_INIT1
1032#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1033
1034#if defined(_WIN32) && defined(_MSC_VER)
1035/************************* Begin test_windirent.h ******************/
1036/*
1037** 2015 November 30
1038**
1039** The author disclaims copyright to this source code.  In place of
1040** a legal notice, here is a blessing:
1041**
1042**    May you do good and not evil.
1043**    May you find forgiveness for yourself and forgive others.
1044**    May you share freely, never taking more than you give.
1045**
1046*************************************************************************
1047** This file contains declarations for most of the opendir() family of
1048** POSIX functions on Win32 using the MSVCRT.
1049*/
1050
1051#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1052#define SQLITE_WINDIRENT_H
1053
1054/*
1055** We need several data types from the Windows SDK header.
1056*/
1057
1058#ifndef WIN32_LEAN_AND_MEAN
1059#define WIN32_LEAN_AND_MEAN
1060#endif
1061
1062#include "windows.h"
1063
1064/*
1065** We need several support functions from the SQLite core.
1066*/
1067
1068/* #include "sqlite3.h" */
1069
1070/*
1071** We need several things from the ANSI and MSVCRT headers.
1072*/
1073
1074#include <stdio.h>
1075#include <stdlib.h>
1076#include <errno.h>
1077#include <io.h>
1078#include <limits.h>
1079#include <sys/types.h>
1080#include <sys/stat.h>
1081
1082/*
1083** We may need several defines that should have been in "sys/stat.h".
1084*/
1085
1086#ifndef S_ISREG
1087#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1088#endif
1089
1090#ifndef S_ISDIR
1091#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1092#endif
1093
1094#ifndef S_ISLNK
1095#define S_ISLNK(mode) (0)
1096#endif
1097
1098/*
1099** We may need to provide the "mode_t" type.
1100*/
1101
1102#ifndef MODE_T_DEFINED
1103  #define MODE_T_DEFINED
1104  typedef unsigned short mode_t;
1105#endif
1106
1107/*
1108** We may need to provide the "ino_t" type.
1109*/
1110
1111#ifndef INO_T_DEFINED
1112  #define INO_T_DEFINED
1113  typedef unsigned short ino_t;
1114#endif
1115
1116/*
1117** We need to define "NAME_MAX" if it was not present in "limits.h".
1118*/
1119
1120#ifndef NAME_MAX
1121#  ifdef FILENAME_MAX
1122#    define NAME_MAX (FILENAME_MAX)
1123#  else
1124#    define NAME_MAX (260)
1125#  endif
1126#endif
1127
1128/*
1129** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1130*/
1131
1132#ifndef NULL_INTPTR_T
1133#  define NULL_INTPTR_T ((intptr_t)(0))
1134#endif
1135
1136#ifndef BAD_INTPTR_T
1137#  define BAD_INTPTR_T ((intptr_t)(-1))
1138#endif
1139
1140/*
1141** We need to provide the necessary structures and related types.
1142*/
1143
1144#ifndef DIRENT_DEFINED
1145#define DIRENT_DEFINED
1146typedef struct DIRENT DIRENT;
1147typedef DIRENT *LPDIRENT;
1148struct DIRENT {
1149  ino_t d_ino;               /* Sequence number, do not use. */
1150  unsigned d_attributes;     /* Win32 file attributes. */
1151  char d_name[NAME_MAX + 1]; /* Name within the directory. */
1152};
1153#endif
1154
1155#ifndef DIR_DEFINED
1156#define DIR_DEFINED
1157typedef struct DIR DIR;
1158typedef DIR *LPDIR;
1159struct DIR {
1160  intptr_t d_handle; /* Value returned by "_findfirst". */
1161  DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1162  DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1163};
1164#endif
1165
1166/*
1167** Provide a macro, for use by the implementation, to determine if a
1168** particular directory entry should be skipped over when searching for
1169** the next directory entry that should be returned by the readdir() or
1170** readdir_r() functions.
1171*/
1172
1173#ifndef is_filtered
1174#  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1175#endif
1176
1177/*
1178** Provide the function prototype for the POSIX compatiable getenv()
1179** function.  This function is not thread-safe.
1180*/
1181
1182extern const char *windirent_getenv(const char *name);
1183
1184/*
1185** Finally, we can provide the function prototypes for the opendir(),
1186** readdir(), readdir_r(), and closedir() POSIX functions.
1187*/
1188
1189extern LPDIR opendir(const char *dirname);
1190extern LPDIRENT readdir(LPDIR dirp);
1191extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1192extern INT closedir(LPDIR dirp);
1193
1194#endif /* defined(WIN32) && defined(_MSC_VER) */
1195
1196/************************* End test_windirent.h ********************/
1197/************************* Begin test_windirent.c ******************/
1198/*
1199** 2015 November 30
1200**
1201** The author disclaims copyright to this source code.  In place of
1202** a legal notice, here is a blessing:
1203**
1204**    May you do good and not evil.
1205**    May you find forgiveness for yourself and forgive others.
1206**    May you share freely, never taking more than you give.
1207**
1208*************************************************************************
1209** This file contains code to implement most of the opendir() family of
1210** POSIX functions on Win32 using the MSVCRT.
1211*/
1212
1213#if defined(_WIN32) && defined(_MSC_VER)
1214/* #include "test_windirent.h" */
1215
1216/*
1217** Implementation of the POSIX getenv() function using the Win32 API.
1218** This function is not thread-safe.
1219*/
1220const char *windirent_getenv(
1221  const char *name
1222){
1223  static char value[32768]; /* Maximum length, per MSDN */
1224  DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1225  DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1226
1227  memset(value, 0, sizeof(value));
1228  dwRet = GetEnvironmentVariableA(name, value, dwSize);
1229  if( dwRet==0 || dwRet>dwSize ){
1230    /*
1231    ** The function call to GetEnvironmentVariableA() failed -OR-
1232    ** the buffer is not large enough.  Either way, return NULL.
1233    */
1234    return 0;
1235  }else{
1236    /*
1237    ** The function call to GetEnvironmentVariableA() succeeded
1238    ** -AND- the buffer contains the entire value.
1239    */
1240    return value;
1241  }
1242}
1243
1244/*
1245** Implementation of the POSIX opendir() function using the MSVCRT.
1246*/
1247LPDIR opendir(
1248  const char *dirname
1249){
1250  struct _finddata_t data;
1251  LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1252  SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1253
1254  if( dirp==NULL ) return NULL;
1255  memset(dirp, 0, sizeof(DIR));
1256
1257  /* TODO: Remove this if Unix-style root paths are not used. */
1258  if( sqlite3_stricmp(dirname, "/")==0 ){
1259    dirname = windirent_getenv("SystemDrive");
1260  }
1261
1262  memset(&data, 0, sizeof(struct _finddata_t));
1263  _snprintf(data.name, namesize, "%s\\*", dirname);
1264  dirp->d_handle = _findfirst(data.name, &data);
1265
1266  if( dirp->d_handle==BAD_INTPTR_T ){
1267    closedir(dirp);
1268    return NULL;
1269  }
1270
1271  /* TODO: Remove this block to allow hidden and/or system files. */
1272  if( is_filtered(data) ){
1273next:
1274
1275    memset(&data, 0, sizeof(struct _finddata_t));
1276    if( _findnext(dirp->d_handle, &data)==-1 ){
1277      closedir(dirp);
1278      return NULL;
1279    }
1280
1281    /* TODO: Remove this block to allow hidden and/or system files. */
1282    if( is_filtered(data) ) goto next;
1283  }
1284
1285  dirp->d_first.d_attributes = data.attrib;
1286  strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1287  dirp->d_first.d_name[NAME_MAX] = '\0';
1288
1289  return dirp;
1290}
1291
1292/*
1293** Implementation of the POSIX readdir() function using the MSVCRT.
1294*/
1295LPDIRENT readdir(
1296  LPDIR dirp
1297){
1298  struct _finddata_t data;
1299
1300  if( dirp==NULL ) return NULL;
1301
1302  if( dirp->d_first.d_ino==0 ){
1303    dirp->d_first.d_ino++;
1304    dirp->d_next.d_ino++;
1305
1306    return &dirp->d_first;
1307  }
1308
1309next:
1310
1311  memset(&data, 0, sizeof(struct _finddata_t));
1312  if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1313
1314  /* TODO: Remove this block to allow hidden and/or system files. */
1315  if( is_filtered(data) ) goto next;
1316
1317  dirp->d_next.d_ino++;
1318  dirp->d_next.d_attributes = data.attrib;
1319  strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1320  dirp->d_next.d_name[NAME_MAX] = '\0';
1321
1322  return &dirp->d_next;
1323}
1324
1325/*
1326** Implementation of the POSIX readdir_r() function using the MSVCRT.
1327*/
1328INT readdir_r(
1329  LPDIR dirp,
1330  LPDIRENT entry,
1331  LPDIRENT *result
1332){
1333  struct _finddata_t data;
1334
1335  if( dirp==NULL ) return EBADF;
1336
1337  if( dirp->d_first.d_ino==0 ){
1338    dirp->d_first.d_ino++;
1339    dirp->d_next.d_ino++;
1340
1341    entry->d_ino = dirp->d_first.d_ino;
1342    entry->d_attributes = dirp->d_first.d_attributes;
1343    strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1344    entry->d_name[NAME_MAX] = '\0';
1345
1346    *result = entry;
1347    return 0;
1348  }
1349
1350next:
1351
1352  memset(&data, 0, sizeof(struct _finddata_t));
1353  if( _findnext(dirp->d_handle, &data)==-1 ){
1354    *result = NULL;
1355    return ENOENT;
1356  }
1357
1358  /* TODO: Remove this block to allow hidden and/or system files. */
1359  if( is_filtered(data) ) goto next;
1360
1361  entry->d_ino = (ino_t)-1; /* not available */
1362  entry->d_attributes = data.attrib;
1363  strncpy(entry->d_name, data.name, NAME_MAX);
1364  entry->d_name[NAME_MAX] = '\0';
1365
1366  *result = entry;
1367  return 0;
1368}
1369
1370/*
1371** Implementation of the POSIX closedir() function using the MSVCRT.
1372*/
1373INT closedir(
1374  LPDIR dirp
1375){
1376  INT result = 0;
1377
1378  if( dirp==NULL ) return EINVAL;
1379
1380  if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1381    result = _findclose(dirp->d_handle);
1382  }
1383
1384  sqlite3_free(dirp);
1385  return result;
1386}
1387
1388#endif /* defined(WIN32) && defined(_MSC_VER) */
1389
1390/************************* End test_windirent.c ********************/
1391#define dirent DIRENT
1392#endif
1393/************************* Begin ../ext/misc/shathree.c ******************/
1394/*
1395** 2017-03-08
1396**
1397** The author disclaims copyright to this source code.  In place of
1398** a legal notice, here is a blessing:
1399**
1400**    May you do good and not evil.
1401**    May you find forgiveness for yourself and forgive others.
1402**    May you share freely, never taking more than you give.
1403**
1404******************************************************************************
1405**
1406** This SQLite extension implements functions that compute SHA3 hashes.
1407** Two SQL functions are implemented:
1408**
1409**     sha3(X,SIZE)
1410**     sha3_query(Y,SIZE)
1411**
1412** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1413** X is NULL.
1414**
1415** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1416** and returns a hash of their results.
1417**
1418** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1419** is used.  If SIZE is included it must be one of the integers 224, 256,
1420** 384, or 512, to determine SHA3 hash variant that is computed.
1421*/
1422/* #include "sqlite3ext.h" */
1423SQLITE_EXTENSION_INIT1
1424#include <assert.h>
1425#include <string.h>
1426#include <stdarg.h>
1427/* typedef sqlite3_uint64 u64; */
1428
1429/******************************************************************************
1430** The Hash Engine
1431*/
1432/*
1433** Macros to determine whether the machine is big or little endian,
1434** and whether or not that determination is run-time or compile-time.
1435**
1436** For best performance, an attempt is made to guess at the byte-order
1437** using C-preprocessor macros.  If that is unsuccessful, or if
1438** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1439** at run-time.
1440*/
1441#ifndef SHA3_BYTEORDER
1442# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1443     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1444     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1445     defined(__arm__)
1446#   define SHA3_BYTEORDER    1234
1447# elif defined(sparc)    || defined(__ppc__)
1448#   define SHA3_BYTEORDER    4321
1449# else
1450#   define SHA3_BYTEORDER 0
1451# endif
1452#endif
1453
1454
1455/*
1456** State structure for a SHA3 hash in progress
1457*/
1458typedef struct SHA3Context SHA3Context;
1459struct SHA3Context {
1460  union {
1461    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1462    unsigned char x[1600];    /* ... or 1600 bytes */
1463  } u;
1464  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1465  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1466  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1467};
1468
1469/*
1470** A single step of the Keccak mixing function for a 1600-bit state
1471*/
1472static void KeccakF1600Step(SHA3Context *p){
1473  int i;
1474  u64 b0, b1, b2, b3, b4;
1475  u64 c0, c1, c2, c3, c4;
1476  u64 d0, d1, d2, d3, d4;
1477  static const u64 RC[] = {
1478    0x0000000000000001ULL,  0x0000000000008082ULL,
1479    0x800000000000808aULL,  0x8000000080008000ULL,
1480    0x000000000000808bULL,  0x0000000080000001ULL,
1481    0x8000000080008081ULL,  0x8000000000008009ULL,
1482    0x000000000000008aULL,  0x0000000000000088ULL,
1483    0x0000000080008009ULL,  0x000000008000000aULL,
1484    0x000000008000808bULL,  0x800000000000008bULL,
1485    0x8000000000008089ULL,  0x8000000000008003ULL,
1486    0x8000000000008002ULL,  0x8000000000000080ULL,
1487    0x000000000000800aULL,  0x800000008000000aULL,
1488    0x8000000080008081ULL,  0x8000000000008080ULL,
1489    0x0000000080000001ULL,  0x8000000080008008ULL
1490  };
1491# define a00 (p->u.s[0])
1492# define a01 (p->u.s[1])
1493# define a02 (p->u.s[2])
1494# define a03 (p->u.s[3])
1495# define a04 (p->u.s[4])
1496# define a10 (p->u.s[5])
1497# define a11 (p->u.s[6])
1498# define a12 (p->u.s[7])
1499# define a13 (p->u.s[8])
1500# define a14 (p->u.s[9])
1501# define a20 (p->u.s[10])
1502# define a21 (p->u.s[11])
1503# define a22 (p->u.s[12])
1504# define a23 (p->u.s[13])
1505# define a24 (p->u.s[14])
1506# define a30 (p->u.s[15])
1507# define a31 (p->u.s[16])
1508# define a32 (p->u.s[17])
1509# define a33 (p->u.s[18])
1510# define a34 (p->u.s[19])
1511# define a40 (p->u.s[20])
1512# define a41 (p->u.s[21])
1513# define a42 (p->u.s[22])
1514# define a43 (p->u.s[23])
1515# define a44 (p->u.s[24])
1516# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1517
1518  for(i=0; i<24; i+=4){
1519    c0 = a00^a10^a20^a30^a40;
1520    c1 = a01^a11^a21^a31^a41;
1521    c2 = a02^a12^a22^a32^a42;
1522    c3 = a03^a13^a23^a33^a43;
1523    c4 = a04^a14^a24^a34^a44;
1524    d0 = c4^ROL64(c1, 1);
1525    d1 = c0^ROL64(c2, 1);
1526    d2 = c1^ROL64(c3, 1);
1527    d3 = c2^ROL64(c4, 1);
1528    d4 = c3^ROL64(c0, 1);
1529
1530    b0 = (a00^d0);
1531    b1 = ROL64((a11^d1), 44);
1532    b2 = ROL64((a22^d2), 43);
1533    b3 = ROL64((a33^d3), 21);
1534    b4 = ROL64((a44^d4), 14);
1535    a00 =   b0 ^((~b1)&  b2 );
1536    a00 ^= RC[i];
1537    a11 =   b1 ^((~b2)&  b3 );
1538    a22 =   b2 ^((~b3)&  b4 );
1539    a33 =   b3 ^((~b4)&  b0 );
1540    a44 =   b4 ^((~b0)&  b1 );
1541
1542    b2 = ROL64((a20^d0), 3);
1543    b3 = ROL64((a31^d1), 45);
1544    b4 = ROL64((a42^d2), 61);
1545    b0 = ROL64((a03^d3), 28);
1546    b1 = ROL64((a14^d4), 20);
1547    a20 =   b0 ^((~b1)&  b2 );
1548    a31 =   b1 ^((~b2)&  b3 );
1549    a42 =   b2 ^((~b3)&  b4 );
1550    a03 =   b3 ^((~b4)&  b0 );
1551    a14 =   b4 ^((~b0)&  b1 );
1552
1553    b4 = ROL64((a40^d0), 18);
1554    b0 = ROL64((a01^d1), 1);
1555    b1 = ROL64((a12^d2), 6);
1556    b2 = ROL64((a23^d3), 25);
1557    b3 = ROL64((a34^d4), 8);
1558    a40 =   b0 ^((~b1)&  b2 );
1559    a01 =   b1 ^((~b2)&  b3 );
1560    a12 =   b2 ^((~b3)&  b4 );
1561    a23 =   b3 ^((~b4)&  b0 );
1562    a34 =   b4 ^((~b0)&  b1 );
1563
1564    b1 = ROL64((a10^d0), 36);
1565    b2 = ROL64((a21^d1), 10);
1566    b3 = ROL64((a32^d2), 15);
1567    b4 = ROL64((a43^d3), 56);
1568    b0 = ROL64((a04^d4), 27);
1569    a10 =   b0 ^((~b1)&  b2 );
1570    a21 =   b1 ^((~b2)&  b3 );
1571    a32 =   b2 ^((~b3)&  b4 );
1572    a43 =   b3 ^((~b4)&  b0 );
1573    a04 =   b4 ^((~b0)&  b1 );
1574
1575    b3 = ROL64((a30^d0), 41);
1576    b4 = ROL64((a41^d1), 2);
1577    b0 = ROL64((a02^d2), 62);
1578    b1 = ROL64((a13^d3), 55);
1579    b2 = ROL64((a24^d4), 39);
1580    a30 =   b0 ^((~b1)&  b2 );
1581    a41 =   b1 ^((~b2)&  b3 );
1582    a02 =   b2 ^((~b3)&  b4 );
1583    a13 =   b3 ^((~b4)&  b0 );
1584    a24 =   b4 ^((~b0)&  b1 );
1585
1586    c0 = a00^a20^a40^a10^a30;
1587    c1 = a11^a31^a01^a21^a41;
1588    c2 = a22^a42^a12^a32^a02;
1589    c3 = a33^a03^a23^a43^a13;
1590    c4 = a44^a14^a34^a04^a24;
1591    d0 = c4^ROL64(c1, 1);
1592    d1 = c0^ROL64(c2, 1);
1593    d2 = c1^ROL64(c3, 1);
1594    d3 = c2^ROL64(c4, 1);
1595    d4 = c3^ROL64(c0, 1);
1596
1597    b0 = (a00^d0);
1598    b1 = ROL64((a31^d1), 44);
1599    b2 = ROL64((a12^d2), 43);
1600    b3 = ROL64((a43^d3), 21);
1601    b4 = ROL64((a24^d4), 14);
1602    a00 =   b0 ^((~b1)&  b2 );
1603    a00 ^= RC[i+1];
1604    a31 =   b1 ^((~b2)&  b3 );
1605    a12 =   b2 ^((~b3)&  b4 );
1606    a43 =   b3 ^((~b4)&  b0 );
1607    a24 =   b4 ^((~b0)&  b1 );
1608
1609    b2 = ROL64((a40^d0), 3);
1610    b3 = ROL64((a21^d1), 45);
1611    b4 = ROL64((a02^d2), 61);
1612    b0 = ROL64((a33^d3), 28);
1613    b1 = ROL64((a14^d4), 20);
1614    a40 =   b0 ^((~b1)&  b2 );
1615    a21 =   b1 ^((~b2)&  b3 );
1616    a02 =   b2 ^((~b3)&  b4 );
1617    a33 =   b3 ^((~b4)&  b0 );
1618    a14 =   b4 ^((~b0)&  b1 );
1619
1620    b4 = ROL64((a30^d0), 18);
1621    b0 = ROL64((a11^d1), 1);
1622    b1 = ROL64((a42^d2), 6);
1623    b2 = ROL64((a23^d3), 25);
1624    b3 = ROL64((a04^d4), 8);
1625    a30 =   b0 ^((~b1)&  b2 );
1626    a11 =   b1 ^((~b2)&  b3 );
1627    a42 =   b2 ^((~b3)&  b4 );
1628    a23 =   b3 ^((~b4)&  b0 );
1629    a04 =   b4 ^((~b0)&  b1 );
1630
1631    b1 = ROL64((a20^d0), 36);
1632    b2 = ROL64((a01^d1), 10);
1633    b3 = ROL64((a32^d2), 15);
1634    b4 = ROL64((a13^d3), 56);
1635    b0 = ROL64((a44^d4), 27);
1636    a20 =   b0 ^((~b1)&  b2 );
1637    a01 =   b1 ^((~b2)&  b3 );
1638    a32 =   b2 ^((~b3)&  b4 );
1639    a13 =   b3 ^((~b4)&  b0 );
1640    a44 =   b4 ^((~b0)&  b1 );
1641
1642    b3 = ROL64((a10^d0), 41);
1643    b4 = ROL64((a41^d1), 2);
1644    b0 = ROL64((a22^d2), 62);
1645    b1 = ROL64((a03^d3), 55);
1646    b2 = ROL64((a34^d4), 39);
1647    a10 =   b0 ^((~b1)&  b2 );
1648    a41 =   b1 ^((~b2)&  b3 );
1649    a22 =   b2 ^((~b3)&  b4 );
1650    a03 =   b3 ^((~b4)&  b0 );
1651    a34 =   b4 ^((~b0)&  b1 );
1652
1653    c0 = a00^a40^a30^a20^a10;
1654    c1 = a31^a21^a11^a01^a41;
1655    c2 = a12^a02^a42^a32^a22;
1656    c3 = a43^a33^a23^a13^a03;
1657    c4 = a24^a14^a04^a44^a34;
1658    d0 = c4^ROL64(c1, 1);
1659    d1 = c0^ROL64(c2, 1);
1660    d2 = c1^ROL64(c3, 1);
1661    d3 = c2^ROL64(c4, 1);
1662    d4 = c3^ROL64(c0, 1);
1663
1664    b0 = (a00^d0);
1665    b1 = ROL64((a21^d1), 44);
1666    b2 = ROL64((a42^d2), 43);
1667    b3 = ROL64((a13^d3), 21);
1668    b4 = ROL64((a34^d4), 14);
1669    a00 =   b0 ^((~b1)&  b2 );
1670    a00 ^= RC[i+2];
1671    a21 =   b1 ^((~b2)&  b3 );
1672    a42 =   b2 ^((~b3)&  b4 );
1673    a13 =   b3 ^((~b4)&  b0 );
1674    a34 =   b4 ^((~b0)&  b1 );
1675
1676    b2 = ROL64((a30^d0), 3);
1677    b3 = ROL64((a01^d1), 45);
1678    b4 = ROL64((a22^d2), 61);
1679    b0 = ROL64((a43^d3), 28);
1680    b1 = ROL64((a14^d4), 20);
1681    a30 =   b0 ^((~b1)&  b2 );
1682    a01 =   b1 ^((~b2)&  b3 );
1683    a22 =   b2 ^((~b3)&  b4 );
1684    a43 =   b3 ^((~b4)&  b0 );
1685    a14 =   b4 ^((~b0)&  b1 );
1686
1687    b4 = ROL64((a10^d0), 18);
1688    b0 = ROL64((a31^d1), 1);
1689    b1 = ROL64((a02^d2), 6);
1690    b2 = ROL64((a23^d3), 25);
1691    b3 = ROL64((a44^d4), 8);
1692    a10 =   b0 ^((~b1)&  b2 );
1693    a31 =   b1 ^((~b2)&  b3 );
1694    a02 =   b2 ^((~b3)&  b4 );
1695    a23 =   b3 ^((~b4)&  b0 );
1696    a44 =   b4 ^((~b0)&  b1 );
1697
1698    b1 = ROL64((a40^d0), 36);
1699    b2 = ROL64((a11^d1), 10);
1700    b3 = ROL64((a32^d2), 15);
1701    b4 = ROL64((a03^d3), 56);
1702    b0 = ROL64((a24^d4), 27);
1703    a40 =   b0 ^((~b1)&  b2 );
1704    a11 =   b1 ^((~b2)&  b3 );
1705    a32 =   b2 ^((~b3)&  b4 );
1706    a03 =   b3 ^((~b4)&  b0 );
1707    a24 =   b4 ^((~b0)&  b1 );
1708
1709    b3 = ROL64((a20^d0), 41);
1710    b4 = ROL64((a41^d1), 2);
1711    b0 = ROL64((a12^d2), 62);
1712    b1 = ROL64((a33^d3), 55);
1713    b2 = ROL64((a04^d4), 39);
1714    a20 =   b0 ^((~b1)&  b2 );
1715    a41 =   b1 ^((~b2)&  b3 );
1716    a12 =   b2 ^((~b3)&  b4 );
1717    a33 =   b3 ^((~b4)&  b0 );
1718    a04 =   b4 ^((~b0)&  b1 );
1719
1720    c0 = a00^a30^a10^a40^a20;
1721    c1 = a21^a01^a31^a11^a41;
1722    c2 = a42^a22^a02^a32^a12;
1723    c3 = a13^a43^a23^a03^a33;
1724    c4 = a34^a14^a44^a24^a04;
1725    d0 = c4^ROL64(c1, 1);
1726    d1 = c0^ROL64(c2, 1);
1727    d2 = c1^ROL64(c3, 1);
1728    d3 = c2^ROL64(c4, 1);
1729    d4 = c3^ROL64(c0, 1);
1730
1731    b0 = (a00^d0);
1732    b1 = ROL64((a01^d1), 44);
1733    b2 = ROL64((a02^d2), 43);
1734    b3 = ROL64((a03^d3), 21);
1735    b4 = ROL64((a04^d4), 14);
1736    a00 =   b0 ^((~b1)&  b2 );
1737    a00 ^= RC[i+3];
1738    a01 =   b1 ^((~b2)&  b3 );
1739    a02 =   b2 ^((~b3)&  b4 );
1740    a03 =   b3 ^((~b4)&  b0 );
1741    a04 =   b4 ^((~b0)&  b1 );
1742
1743    b2 = ROL64((a10^d0), 3);
1744    b3 = ROL64((a11^d1), 45);
1745    b4 = ROL64((a12^d2), 61);
1746    b0 = ROL64((a13^d3), 28);
1747    b1 = ROL64((a14^d4), 20);
1748    a10 =   b0 ^((~b1)&  b2 );
1749    a11 =   b1 ^((~b2)&  b3 );
1750    a12 =   b2 ^((~b3)&  b4 );
1751    a13 =   b3 ^((~b4)&  b0 );
1752    a14 =   b4 ^((~b0)&  b1 );
1753
1754    b4 = ROL64((a20^d0), 18);
1755    b0 = ROL64((a21^d1), 1);
1756    b1 = ROL64((a22^d2), 6);
1757    b2 = ROL64((a23^d3), 25);
1758    b3 = ROL64((a24^d4), 8);
1759    a20 =   b0 ^((~b1)&  b2 );
1760    a21 =   b1 ^((~b2)&  b3 );
1761    a22 =   b2 ^((~b3)&  b4 );
1762    a23 =   b3 ^((~b4)&  b0 );
1763    a24 =   b4 ^((~b0)&  b1 );
1764
1765    b1 = ROL64((a30^d0), 36);
1766    b2 = ROL64((a31^d1), 10);
1767    b3 = ROL64((a32^d2), 15);
1768    b4 = ROL64((a33^d3), 56);
1769    b0 = ROL64((a34^d4), 27);
1770    a30 =   b0 ^((~b1)&  b2 );
1771    a31 =   b1 ^((~b2)&  b3 );
1772    a32 =   b2 ^((~b3)&  b4 );
1773    a33 =   b3 ^((~b4)&  b0 );
1774    a34 =   b4 ^((~b0)&  b1 );
1775
1776    b3 = ROL64((a40^d0), 41);
1777    b4 = ROL64((a41^d1), 2);
1778    b0 = ROL64((a42^d2), 62);
1779    b1 = ROL64((a43^d3), 55);
1780    b2 = ROL64((a44^d4), 39);
1781    a40 =   b0 ^((~b1)&  b2 );
1782    a41 =   b1 ^((~b2)&  b3 );
1783    a42 =   b2 ^((~b3)&  b4 );
1784    a43 =   b3 ^((~b4)&  b0 );
1785    a44 =   b4 ^((~b0)&  b1 );
1786  }
1787}
1788
1789/*
1790** Initialize a new hash.  iSize determines the size of the hash
1791** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1792** can be zero to use the default hash size of 256 bits.
1793*/
1794static void SHA3Init(SHA3Context *p, int iSize){
1795  memset(p, 0, sizeof(*p));
1796  if( iSize>=128 && iSize<=512 ){
1797    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1798  }else{
1799    p->nRate = (1600 - 2*256)/8;
1800  }
1801#if SHA3_BYTEORDER==1234
1802  /* Known to be little-endian at compile-time. No-op */
1803#elif SHA3_BYTEORDER==4321
1804  p->ixMask = 7;  /* Big-endian */
1805#else
1806  {
1807    static unsigned int one = 1;
1808    if( 1==*(unsigned char*)&one ){
1809      /* Little endian.  No byte swapping. */
1810      p->ixMask = 0;
1811    }else{
1812      /* Big endian.  Byte swap. */
1813      p->ixMask = 7;
1814    }
1815  }
1816#endif
1817}
1818
1819/*
1820** Make consecutive calls to the SHA3Update function to add new content
1821** to the hash
1822*/
1823static void SHA3Update(
1824  SHA3Context *p,
1825  const unsigned char *aData,
1826  unsigned int nData
1827){
1828  unsigned int i = 0;
1829#if SHA3_BYTEORDER==1234
1830  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1831    for(; i+7<nData; i+=8){
1832      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1833      p->nLoaded += 8;
1834      if( p->nLoaded>=p->nRate ){
1835        KeccakF1600Step(p);
1836        p->nLoaded = 0;
1837      }
1838    }
1839  }
1840#endif
1841  for(; i<nData; i++){
1842#if SHA3_BYTEORDER==1234
1843    p->u.x[p->nLoaded] ^= aData[i];
1844#elif SHA3_BYTEORDER==4321
1845    p->u.x[p->nLoaded^0x07] ^= aData[i];
1846#else
1847    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1848#endif
1849    p->nLoaded++;
1850    if( p->nLoaded==p->nRate ){
1851      KeccakF1600Step(p);
1852      p->nLoaded = 0;
1853    }
1854  }
1855}
1856
1857/*
1858** After all content has been added, invoke SHA3Final() to compute
1859** the final hash.  The function returns a pointer to the binary
1860** hash value.
1861*/
1862static unsigned char *SHA3Final(SHA3Context *p){
1863  unsigned int i;
1864  if( p->nLoaded==p->nRate-1 ){
1865    const unsigned char c1 = 0x86;
1866    SHA3Update(p, &c1, 1);
1867  }else{
1868    const unsigned char c2 = 0x06;
1869    const unsigned char c3 = 0x80;
1870    SHA3Update(p, &c2, 1);
1871    p->nLoaded = p->nRate - 1;
1872    SHA3Update(p, &c3, 1);
1873  }
1874  for(i=0; i<p->nRate; i++){
1875    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1876  }
1877  return &p->u.x[p->nRate];
1878}
1879/* End of the hashing logic
1880*****************************************************************************/
1881
1882/*
1883** Implementation of the sha3(X,SIZE) function.
1884**
1885** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1886** size is 256.  If X is a BLOB, it is hashed as is.
1887** For all other non-NULL types of input, X is converted into a UTF-8 string
1888** and the string is hashed without the trailing 0x00 terminator.  The hash
1889** of a NULL value is NULL.
1890*/
1891static void sha3Func(
1892  sqlite3_context *context,
1893  int argc,
1894  sqlite3_value **argv
1895){
1896  SHA3Context cx;
1897  int eType = sqlite3_value_type(argv[0]);
1898  int nByte = sqlite3_value_bytes(argv[0]);
1899  int iSize;
1900  if( argc==1 ){
1901    iSize = 256;
1902  }else{
1903    iSize = sqlite3_value_int(argv[1]);
1904    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1905      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1906                                    "384 512", -1);
1907      return;
1908    }
1909  }
1910  if( eType==SQLITE_NULL ) return;
1911  SHA3Init(&cx, iSize);
1912  if( eType==SQLITE_BLOB ){
1913    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1914  }else{
1915    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1916  }
1917  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1918}
1919
1920/* Compute a string using sqlite3_vsnprintf() with a maximum length
1921** of 50 bytes and add it to the hash.
1922*/
1923static void hash_step_vformat(
1924  SHA3Context *p,                 /* Add content to this context */
1925  const char *zFormat,
1926  ...
1927){
1928  va_list ap;
1929  int n;
1930  char zBuf[50];
1931  va_start(ap, zFormat);
1932  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1933  va_end(ap);
1934  n = (int)strlen(zBuf);
1935  SHA3Update(p, (unsigned char*)zBuf, n);
1936}
1937
1938/*
1939** Implementation of the sha3_query(SQL,SIZE) function.
1940**
1941** This function compiles and runs the SQL statement(s) given in the
1942** argument. The results are hashed using a SIZE-bit SHA3.  The default
1943** size is 256.
1944**
1945** The format of the byte stream that is hashed is summarized as follows:
1946**
1947**       S<n>:<sql>
1948**       R
1949**       N
1950**       I<int>
1951**       F<ieee-float>
1952**       B<size>:<bytes>
1953**       T<size>:<text>
1954**
1955** <sql> is the original SQL text for each statement run and <n> is
1956** the size of that text.  The SQL text is UTF-8.  A single R character
1957** occurs before the start of each row.  N means a NULL value.
1958** I mean an 8-byte little-endian integer <int>.  F is a floating point
1959** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1960** B means blobs of <size> bytes.  T means text rendered as <size>
1961** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1962** text integers.
1963**
1964** For each SQL statement in the X input, there is one S segment.  Each
1965** S segment is followed by zero or more R segments, one for each row in the
1966** result set.  After each R, there are one or more N, I, F, B, or T segments,
1967** one for each column in the result set.  Segments are concatentated directly
1968** with no delimiters of any kind.
1969*/
1970static void sha3QueryFunc(
1971  sqlite3_context *context,
1972  int argc,
1973  sqlite3_value **argv
1974){
1975  sqlite3 *db = sqlite3_context_db_handle(context);
1976  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1977  sqlite3_stmt *pStmt = 0;
1978  int nCol;                   /* Number of columns in the result set */
1979  int i;                      /* Loop counter */
1980  int rc;
1981  int n;
1982  const char *z;
1983  SHA3Context cx;
1984  int iSize;
1985
1986  if( argc==1 ){
1987    iSize = 256;
1988  }else{
1989    iSize = sqlite3_value_int(argv[1]);
1990    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1991      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1992                                    "384 512", -1);
1993      return;
1994    }
1995  }
1996  if( zSql==0 ) return;
1997  SHA3Init(&cx, iSize);
1998  while( zSql[0] ){
1999    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2000    if( rc ){
2001      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2002                                   zSql, sqlite3_errmsg(db));
2003      sqlite3_finalize(pStmt);
2004      sqlite3_result_error(context, zMsg, -1);
2005      sqlite3_free(zMsg);
2006      return;
2007    }
2008    if( !sqlite3_stmt_readonly(pStmt) ){
2009      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2010      sqlite3_finalize(pStmt);
2011      sqlite3_result_error(context, zMsg, -1);
2012      sqlite3_free(zMsg);
2013      return;
2014    }
2015    nCol = sqlite3_column_count(pStmt);
2016    z = sqlite3_sql(pStmt);
2017    if( z ){
2018      n = (int)strlen(z);
2019      hash_step_vformat(&cx,"S%d:",n);
2020      SHA3Update(&cx,(unsigned char*)z,n);
2021    }
2022
2023    /* Compute a hash over the result of the query */
2024    while( SQLITE_ROW==sqlite3_step(pStmt) ){
2025      SHA3Update(&cx,(const unsigned char*)"R",1);
2026      for(i=0; i<nCol; i++){
2027        switch( sqlite3_column_type(pStmt,i) ){
2028          case SQLITE_NULL: {
2029            SHA3Update(&cx, (const unsigned char*)"N",1);
2030            break;
2031          }
2032          case SQLITE_INTEGER: {
2033            sqlite3_uint64 u;
2034            int j;
2035            unsigned char x[9];
2036            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2037            memcpy(&u, &v, 8);
2038            for(j=8; j>=1; j--){
2039              x[j] = u & 0xff;
2040              u >>= 8;
2041            }
2042            x[0] = 'I';
2043            SHA3Update(&cx, x, 9);
2044            break;
2045          }
2046          case SQLITE_FLOAT: {
2047            sqlite3_uint64 u;
2048            int j;
2049            unsigned char x[9];
2050            double r = sqlite3_column_double(pStmt,i);
2051            memcpy(&u, &r, 8);
2052            for(j=8; j>=1; j--){
2053              x[j] = u & 0xff;
2054              u >>= 8;
2055            }
2056            x[0] = 'F';
2057            SHA3Update(&cx,x,9);
2058            break;
2059          }
2060          case SQLITE_TEXT: {
2061            int n2 = sqlite3_column_bytes(pStmt, i);
2062            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2063            hash_step_vformat(&cx,"T%d:",n2);
2064            SHA3Update(&cx, z2, n2);
2065            break;
2066          }
2067          case SQLITE_BLOB: {
2068            int n2 = sqlite3_column_bytes(pStmt, i);
2069            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2070            hash_step_vformat(&cx,"B%d:",n2);
2071            SHA3Update(&cx, z2, n2);
2072            break;
2073          }
2074        }
2075      }
2076    }
2077    sqlite3_finalize(pStmt);
2078  }
2079  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2080}
2081
2082
2083#ifdef _WIN32
2084
2085#endif
2086int sqlite3_shathree_init(
2087  sqlite3 *db,
2088  char **pzErrMsg,
2089  const sqlite3_api_routines *pApi
2090){
2091  int rc = SQLITE_OK;
2092  SQLITE_EXTENSION_INIT2(pApi);
2093  (void)pzErrMsg;  /* Unused parameter */
2094  rc = sqlite3_create_function(db, "sha3", 1,
2095                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2096                      0, sha3Func, 0, 0);
2097  if( rc==SQLITE_OK ){
2098    rc = sqlite3_create_function(db, "sha3", 2,
2099                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2100                      0, sha3Func, 0, 0);
2101  }
2102  if( rc==SQLITE_OK ){
2103    rc = sqlite3_create_function(db, "sha3_query", 1,
2104                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
2105                      0, sha3QueryFunc, 0, 0);
2106  }
2107  if( rc==SQLITE_OK ){
2108    rc = sqlite3_create_function(db, "sha3_query", 2,
2109                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
2110                      0, sha3QueryFunc, 0, 0);
2111  }
2112  return rc;
2113}
2114
2115/************************* End ../ext/misc/shathree.c ********************/
2116/************************* Begin ../ext/misc/fileio.c ******************/
2117/*
2118** 2014-06-13
2119**
2120** The author disclaims copyright to this source code.  In place of
2121** a legal notice, here is a blessing:
2122**
2123**    May you do good and not evil.
2124**    May you find forgiveness for yourself and forgive others.
2125**    May you share freely, never taking more than you give.
2126**
2127******************************************************************************
2128**
2129** This SQLite extension implements SQL functions readfile() and
2130** writefile(), and eponymous virtual type "fsdir".
2131**
2132** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2133**
2134**   If neither of the optional arguments is present, then this UDF
2135**   function writes blob DATA to file FILE. If successful, the number
2136**   of bytes written is returned. If an error occurs, NULL is returned.
2137**
2138**   If the first option argument - MODE - is present, then it must
2139**   be passed an integer value that corresponds to a POSIX mode
2140**   value (file type + permissions, as returned in the stat.st_mode
2141**   field by the stat() system call). Three types of files may
2142**   be written/created:
2143**
2144**     regular files:  (mode & 0170000)==0100000
2145**     symbolic links: (mode & 0170000)==0120000
2146**     directories:    (mode & 0170000)==0040000
2147**
2148**   For a directory, the DATA is ignored. For a symbolic link, it is
2149**   interpreted as text and used as the target of the link. For a
2150**   regular file, it is interpreted as a blob and written into the
2151**   named file. Regardless of the type of file, its permissions are
2152**   set to (mode & 0777) before returning.
2153**
2154**   If the optional MTIME argument is present, then it is interpreted
2155**   as an integer - the number of seconds since the unix epoch. The
2156**   modification-time of the target file is set to this value before
2157**   returning.
2158**
2159**   If three or more arguments are passed to this function and an
2160**   error is encountered, an exception is raised.
2161**
2162** READFILE(FILE):
2163**
2164**   Read and return the contents of file FILE (type blob) from disk.
2165**
2166** FSDIR:
2167**
2168**   Used as follows:
2169**
2170**     SELECT * FROM fsdir($path [, $dir]);
2171**
2172**   Parameter $path is an absolute or relative pathname. If the file that it
2173**   refers to does not exist, it is an error. If the path refers to a regular
2174**   file or symbolic link, it returns a single row. Or, if the path refers
2175**   to a directory, it returns one row for the directory, and one row for each
2176**   file within the hierarchy rooted at $path.
2177**
2178**   Each row has the following columns:
2179**
2180**     name:  Path to file or directory (text value).
2181**     mode:  Value of stat.st_mode for directory entry (an integer).
2182**     mtime: Value of stat.st_mtime for directory entry (an integer).
2183**     data:  For a regular file, a blob containing the file data. For a
2184**            symlink, a text value containing the text of the link. For a
2185**            directory, NULL.
2186**
2187**   If a non-NULL value is specified for the optional $dir parameter and
2188**   $path is a relative path, then $path is interpreted relative to $dir.
2189**   And the paths returned in the "name" column of the table are also
2190**   relative to directory $dir.
2191*/
2192/* #include "sqlite3ext.h" */
2193SQLITE_EXTENSION_INIT1
2194#include <stdio.h>
2195#include <string.h>
2196#include <assert.h>
2197
2198#include <sys/types.h>
2199#include <sys/stat.h>
2200#include <fcntl.h>
2201#if !defined(_WIN32) && !defined(WIN32)
2202#  include <unistd.h>
2203#  include <dirent.h>
2204#  include <utime.h>
2205#  include <sys/time.h>
2206#else
2207#  include "windows.h"
2208#  include <io.h>
2209#  include <direct.h>
2210/* #  include "test_windirent.h" */
2211#  define dirent DIRENT
2212#  ifndef chmod
2213#    define chmod _chmod
2214#  endif
2215#  ifndef stat
2216#    define stat _stat
2217#  endif
2218#  define mkdir(path,mode) _mkdir(path)
2219#  define lstat(path,buf) stat(path,buf)
2220#endif
2221#include <time.h>
2222#include <errno.h>
2223
2224
2225/*
2226** Structure of the fsdir() table-valued function
2227*/
2228                 /*    0    1    2     3    4           5             */
2229#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2230#define FSDIR_COLUMN_NAME     0     /* Name of the file */
2231#define FSDIR_COLUMN_MODE     1     /* Access mode */
2232#define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2233#define FSDIR_COLUMN_DATA     3     /* File content */
2234#define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2235#define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2236
2237
2238/*
2239** Set the result stored by context ctx to a blob containing the
2240** contents of file zName.  Or, leave the result unchanged (NULL)
2241** if the file does not exist or is unreadable.
2242**
2243** If the file exceeds the SQLite blob size limit, through an
2244** SQLITE_TOOBIG error.
2245**
2246** Throw an SQLITE_IOERR if there are difficulties pulling the file
2247** off of disk.
2248*/
2249static void readFileContents(sqlite3_context *ctx, const char *zName){
2250  FILE *in;
2251  sqlite3_int64 nIn;
2252  void *pBuf;
2253  sqlite3 *db;
2254  int mxBlob;
2255
2256  in = fopen(zName, "rb");
2257  if( in==0 ){
2258    /* File does not exist or is unreadable. Leave the result set to NULL. */
2259    return;
2260  }
2261  fseek(in, 0, SEEK_END);
2262  nIn = ftell(in);
2263  rewind(in);
2264  db = sqlite3_context_db_handle(ctx);
2265  mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2266  if( nIn>mxBlob ){
2267    sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2268    fclose(in);
2269    return;
2270  }
2271  pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2272  if( pBuf==0 ){
2273    sqlite3_result_error_nomem(ctx);
2274    fclose(in);
2275    return;
2276  }
2277  if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2278    sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2279  }else{
2280    sqlite3_result_error_code(ctx, SQLITE_IOERR);
2281    sqlite3_free(pBuf);
2282  }
2283  fclose(in);
2284}
2285
2286/*
2287** Implementation of the "readfile(X)" SQL function.  The entire content
2288** of the file named X is read and returned as a BLOB.  NULL is returned
2289** if the file does not exist or is unreadable.
2290*/
2291static void readfileFunc(
2292  sqlite3_context *context,
2293  int argc,
2294  sqlite3_value **argv
2295){
2296  const char *zName;
2297  (void)(argc);  /* Unused parameter */
2298  zName = (const char*)sqlite3_value_text(argv[0]);
2299  if( zName==0 ) return;
2300  readFileContents(context, zName);
2301}
2302
2303/*
2304** Set the error message contained in context ctx to the results of
2305** vprintf(zFmt, ...).
2306*/
2307static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2308  char *zMsg = 0;
2309  va_list ap;
2310  va_start(ap, zFmt);
2311  zMsg = sqlite3_vmprintf(zFmt, ap);
2312  sqlite3_result_error(ctx, zMsg, -1);
2313  sqlite3_free(zMsg);
2314  va_end(ap);
2315}
2316
2317#if defined(_WIN32)
2318/*
2319** This function is designed to convert a Win32 FILETIME structure into the
2320** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2321*/
2322static sqlite3_uint64 fileTimeToUnixTime(
2323  LPFILETIME pFileTime
2324){
2325  SYSTEMTIME epochSystemTime;
2326  ULARGE_INTEGER epochIntervals;
2327  FILETIME epochFileTime;
2328  ULARGE_INTEGER fileIntervals;
2329
2330  memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2331  epochSystemTime.wYear = 1970;
2332  epochSystemTime.wMonth = 1;
2333  epochSystemTime.wDay = 1;
2334  SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2335  epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2336  epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2337
2338  fileIntervals.LowPart = pFileTime->dwLowDateTime;
2339  fileIntervals.HighPart = pFileTime->dwHighDateTime;
2340
2341  return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2342}
2343
2344/*
2345** This function attempts to normalize the time values found in the stat()
2346** buffer to UTC.  This is necessary on Win32, where the runtime library
2347** appears to return these values as local times.
2348*/
2349static void statTimesToUtc(
2350  const char *zPath,
2351  struct stat *pStatBuf
2352){
2353  HANDLE hFindFile;
2354  WIN32_FIND_DATAW fd;
2355  LPWSTR zUnicodeName;
2356  extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2357  zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2358  if( zUnicodeName ){
2359    memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2360    hFindFile = FindFirstFileW(zUnicodeName, &fd);
2361    if( hFindFile!=NULL ){
2362      pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2363      pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2364      pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2365      FindClose(hFindFile);
2366    }
2367    sqlite3_free(zUnicodeName);
2368  }
2369}
2370#endif
2371
2372/*
2373** This function is used in place of stat().  On Windows, special handling
2374** is required in order for the included time to be returned as UTC.  On all
2375** other systems, this function simply calls stat().
2376*/
2377static int fileStat(
2378  const char *zPath,
2379  struct stat *pStatBuf
2380){
2381#if defined(_WIN32)
2382  int rc = stat(zPath, pStatBuf);
2383  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2384  return rc;
2385#else
2386  return stat(zPath, pStatBuf);
2387#endif
2388}
2389
2390/*
2391** This function is used in place of lstat().  On Windows, special handling
2392** is required in order for the included time to be returned as UTC.  On all
2393** other systems, this function simply calls lstat().
2394*/
2395static int fileLinkStat(
2396  const char *zPath,
2397  struct stat *pStatBuf
2398){
2399#if defined(_WIN32)
2400  int rc = lstat(zPath, pStatBuf);
2401  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2402  return rc;
2403#else
2404  return lstat(zPath, pStatBuf);
2405#endif
2406}
2407
2408/*
2409** Argument zFile is the name of a file that will be created and/or written
2410** by SQL function writefile(). This function ensures that the directory
2411** zFile will be written to exists, creating it if required. The permissions
2412** for any path components created by this function are set in accordance
2413** with the current umask.
2414**
2415** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2416** SQLITE_OK is returned if the directory is successfully created, or
2417** SQLITE_ERROR otherwise.
2418*/
2419static int makeDirectory(
2420  const char *zFile
2421){
2422  char *zCopy = sqlite3_mprintf("%s", zFile);
2423  int rc = SQLITE_OK;
2424
2425  if( zCopy==0 ){
2426    rc = SQLITE_NOMEM;
2427  }else{
2428    int nCopy = (int)strlen(zCopy);
2429    int i = 1;
2430
2431    while( rc==SQLITE_OK ){
2432      struct stat sStat;
2433      int rc2;
2434
2435      for(; zCopy[i]!='/' && i<nCopy; i++);
2436      if( i==nCopy ) break;
2437      zCopy[i] = '\0';
2438
2439      rc2 = fileStat(zCopy, &sStat);
2440      if( rc2!=0 ){
2441        if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2442      }else{
2443        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2444      }
2445      zCopy[i] = '/';
2446      i++;
2447    }
2448
2449    sqlite3_free(zCopy);
2450  }
2451
2452  return rc;
2453}
2454
2455/*
2456** This function does the work for the writefile() UDF. Refer to
2457** header comments at the top of this file for details.
2458*/
2459static int writeFile(
2460  sqlite3_context *pCtx,          /* Context to return bytes written in */
2461  const char *zFile,              /* File to write */
2462  sqlite3_value *pData,           /* Data to write */
2463  mode_t mode,                    /* MODE parameter passed to writefile() */
2464  sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2465){
2466#if !defined(_WIN32) && !defined(WIN32)
2467  if( S_ISLNK(mode) ){
2468    const char *zTo = (const char*)sqlite3_value_text(pData);
2469    if( symlink(zTo, zFile)<0 ) return 1;
2470  }else
2471#endif
2472  {
2473    if( S_ISDIR(mode) ){
2474      if( mkdir(zFile, mode) ){
2475        /* The mkdir() call to create the directory failed. This might not
2476        ** be an error though - if there is already a directory at the same
2477        ** path and either the permissions already match or can be changed
2478        ** to do so using chmod(), it is not an error.  */
2479        struct stat sStat;
2480        if( errno!=EEXIST
2481         || 0!=fileStat(zFile, &sStat)
2482         || !S_ISDIR(sStat.st_mode)
2483         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2484        ){
2485          return 1;
2486        }
2487      }
2488    }else{
2489      sqlite3_int64 nWrite = 0;
2490      const char *z;
2491      int rc = 0;
2492      FILE *out = fopen(zFile, "wb");
2493      if( out==0 ) return 1;
2494      z = (const char*)sqlite3_value_blob(pData);
2495      if( z ){
2496        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2497        nWrite = sqlite3_value_bytes(pData);
2498        if( nWrite!=n ){
2499          rc = 1;
2500        }
2501      }
2502      fclose(out);
2503      if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2504        rc = 1;
2505      }
2506      if( rc ) return 2;
2507      sqlite3_result_int64(pCtx, nWrite);
2508    }
2509  }
2510
2511  if( mtime>=0 ){
2512#if defined(_WIN32)
2513#if !SQLITE_OS_WINRT
2514    /* Windows */
2515    FILETIME lastAccess;
2516    FILETIME lastWrite;
2517    SYSTEMTIME currentTime;
2518    LONGLONG intervals;
2519    HANDLE hFile;
2520    LPWSTR zUnicodeName;
2521    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2522
2523    GetSystemTime(&currentTime);
2524    SystemTimeToFileTime(&currentTime, &lastAccess);
2525    intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2526    lastWrite.dwLowDateTime = (DWORD)intervals;
2527    lastWrite.dwHighDateTime = intervals >> 32;
2528    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2529    if( zUnicodeName==0 ){
2530      return 1;
2531    }
2532    hFile = CreateFileW(
2533      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2534      FILE_FLAG_BACKUP_SEMANTICS, NULL
2535    );
2536    sqlite3_free(zUnicodeName);
2537    if( hFile!=INVALID_HANDLE_VALUE ){
2538      BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2539      CloseHandle(hFile);
2540      return !bResult;
2541    }else{
2542      return 1;
2543    }
2544#endif
2545#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2546    /* Recent unix */
2547    struct timespec times[2];
2548    times[0].tv_nsec = times[1].tv_nsec = 0;
2549    times[0].tv_sec = time(0);
2550    times[1].tv_sec = mtime;
2551    if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2552      return 1;
2553    }
2554#else
2555    /* Legacy unix */
2556    struct timeval times[2];
2557    times[0].tv_usec = times[1].tv_usec = 0;
2558    times[0].tv_sec = time(0);
2559    times[1].tv_sec = mtime;
2560    if( utimes(zFile, times) ){
2561      return 1;
2562    }
2563#endif
2564  }
2565
2566  return 0;
2567}
2568
2569/*
2570** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2571** Refer to header comments at the top of this file for details.
2572*/
2573static void writefileFunc(
2574  sqlite3_context *context,
2575  int argc,
2576  sqlite3_value **argv
2577){
2578  const char *zFile;
2579  mode_t mode = 0;
2580  int res;
2581  sqlite3_int64 mtime = -1;
2582
2583  if( argc<2 || argc>4 ){
2584    sqlite3_result_error(context,
2585        "wrong number of arguments to function writefile()", -1
2586    );
2587    return;
2588  }
2589
2590  zFile = (const char*)sqlite3_value_text(argv[0]);
2591  if( zFile==0 ) return;
2592  if( argc>=3 ){
2593    mode = (mode_t)sqlite3_value_int(argv[2]);
2594  }
2595  if( argc==4 ){
2596    mtime = sqlite3_value_int64(argv[3]);
2597  }
2598
2599  res = writeFile(context, zFile, argv[1], mode, mtime);
2600  if( res==1 && errno==ENOENT ){
2601    if( makeDirectory(zFile)==SQLITE_OK ){
2602      res = writeFile(context, zFile, argv[1], mode, mtime);
2603    }
2604  }
2605
2606  if( argc>2 && res!=0 ){
2607    if( S_ISLNK(mode) ){
2608      ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2609    }else if( S_ISDIR(mode) ){
2610      ctxErrorMsg(context, "failed to create directory: %s", zFile);
2611    }else{
2612      ctxErrorMsg(context, "failed to write file: %s", zFile);
2613    }
2614  }
2615}
2616
2617/*
2618** SQL function:   lsmode(MODE)
2619**
2620** Given a numberic st_mode from stat(), convert it into a human-readable
2621** text string in the style of "ls -l".
2622*/
2623static void lsModeFunc(
2624  sqlite3_context *context,
2625  int argc,
2626  sqlite3_value **argv
2627){
2628  int i;
2629  int iMode = sqlite3_value_int(argv[0]);
2630  char z[16];
2631  (void)argc;
2632  if( S_ISLNK(iMode) ){
2633    z[0] = 'l';
2634  }else if( S_ISREG(iMode) ){
2635    z[0] = '-';
2636  }else if( S_ISDIR(iMode) ){
2637    z[0] = 'd';
2638  }else{
2639    z[0] = '?';
2640  }
2641  for(i=0; i<3; i++){
2642    int m = (iMode >> ((2-i)*3));
2643    char *a = &z[1 + i*3];
2644    a[0] = (m & 0x4) ? 'r' : '-';
2645    a[1] = (m & 0x2) ? 'w' : '-';
2646    a[2] = (m & 0x1) ? 'x' : '-';
2647  }
2648  z[10] = '\0';
2649  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2650}
2651
2652#ifndef SQLITE_OMIT_VIRTUALTABLE
2653
2654/*
2655** Cursor type for recursively iterating through a directory structure.
2656*/
2657typedef struct fsdir_cursor fsdir_cursor;
2658typedef struct FsdirLevel FsdirLevel;
2659
2660struct FsdirLevel {
2661  DIR *pDir;                 /* From opendir() */
2662  char *zDir;                /* Name of directory (nul-terminated) */
2663};
2664
2665struct fsdir_cursor {
2666  sqlite3_vtab_cursor base;  /* Base class - must be first */
2667
2668  int nLvl;                  /* Number of entries in aLvl[] array */
2669  int iLvl;                  /* Index of current entry */
2670  FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2671
2672  const char *zBase;
2673  int nBase;
2674
2675  struct stat sStat;         /* Current lstat() results */
2676  char *zPath;               /* Path to current entry */
2677  sqlite3_int64 iRowid;      /* Current rowid */
2678};
2679
2680typedef struct fsdir_tab fsdir_tab;
2681struct fsdir_tab {
2682  sqlite3_vtab base;         /* Base class - must be first */
2683};
2684
2685/*
2686** Construct a new fsdir virtual table object.
2687*/
2688static int fsdirConnect(
2689  sqlite3 *db,
2690  void *pAux,
2691  int argc, const char *const*argv,
2692  sqlite3_vtab **ppVtab,
2693  char **pzErr
2694){
2695  fsdir_tab *pNew = 0;
2696  int rc;
2697  (void)pAux;
2698  (void)argc;
2699  (void)argv;
2700  (void)pzErr;
2701  rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2702  if( rc==SQLITE_OK ){
2703    pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2704    if( pNew==0 ) return SQLITE_NOMEM;
2705    memset(pNew, 0, sizeof(*pNew));
2706    sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2707  }
2708  *ppVtab = (sqlite3_vtab*)pNew;
2709  return rc;
2710}
2711
2712/*
2713** This method is the destructor for fsdir vtab objects.
2714*/
2715static int fsdirDisconnect(sqlite3_vtab *pVtab){
2716  sqlite3_free(pVtab);
2717  return SQLITE_OK;
2718}
2719
2720/*
2721** Constructor for a new fsdir_cursor object.
2722*/
2723static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2724  fsdir_cursor *pCur;
2725  (void)p;
2726  pCur = sqlite3_malloc( sizeof(*pCur) );
2727  if( pCur==0 ) return SQLITE_NOMEM;
2728  memset(pCur, 0, sizeof(*pCur));
2729  pCur->iLvl = -1;
2730  *ppCursor = &pCur->base;
2731  return SQLITE_OK;
2732}
2733
2734/*
2735** Reset a cursor back to the state it was in when first returned
2736** by fsdirOpen().
2737*/
2738static void fsdirResetCursor(fsdir_cursor *pCur){
2739  int i;
2740  for(i=0; i<=pCur->iLvl; i++){
2741    FsdirLevel *pLvl = &pCur->aLvl[i];
2742    if( pLvl->pDir ) closedir(pLvl->pDir);
2743    sqlite3_free(pLvl->zDir);
2744  }
2745  sqlite3_free(pCur->zPath);
2746  sqlite3_free(pCur->aLvl);
2747  pCur->aLvl = 0;
2748  pCur->zPath = 0;
2749  pCur->zBase = 0;
2750  pCur->nBase = 0;
2751  pCur->nLvl = 0;
2752  pCur->iLvl = -1;
2753  pCur->iRowid = 1;
2754}
2755
2756/*
2757** Destructor for an fsdir_cursor.
2758*/
2759static int fsdirClose(sqlite3_vtab_cursor *cur){
2760  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2761
2762  fsdirResetCursor(pCur);
2763  sqlite3_free(pCur);
2764  return SQLITE_OK;
2765}
2766
2767/*
2768** Set the error message for the virtual table associated with cursor
2769** pCur to the results of vprintf(zFmt, ...).
2770*/
2771static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2772  va_list ap;
2773  va_start(ap, zFmt);
2774  pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2775  va_end(ap);
2776}
2777
2778
2779/*
2780** Advance an fsdir_cursor to its next row of output.
2781*/
2782static int fsdirNext(sqlite3_vtab_cursor *cur){
2783  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2784  mode_t m = pCur->sStat.st_mode;
2785
2786  pCur->iRowid++;
2787  if( S_ISDIR(m) ){
2788    /* Descend into this directory */
2789    int iNew = pCur->iLvl + 1;
2790    FsdirLevel *pLvl;
2791    if( iNew>=pCur->nLvl ){
2792      int nNew = iNew+1;
2793      sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2794      FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2795      if( aNew==0 ) return SQLITE_NOMEM;
2796      memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2797      pCur->aLvl = aNew;
2798      pCur->nLvl = nNew;
2799    }
2800    pCur->iLvl = iNew;
2801    pLvl = &pCur->aLvl[iNew];
2802
2803    pLvl->zDir = pCur->zPath;
2804    pCur->zPath = 0;
2805    pLvl->pDir = opendir(pLvl->zDir);
2806    if( pLvl->pDir==0 ){
2807      fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2808      return SQLITE_ERROR;
2809    }
2810  }
2811
2812  while( pCur->iLvl>=0 ){
2813    FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2814    struct dirent *pEntry = readdir(pLvl->pDir);
2815    if( pEntry ){
2816      if( pEntry->d_name[0]=='.' ){
2817       if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2818       if( pEntry->d_name[1]=='\0' ) continue;
2819      }
2820      sqlite3_free(pCur->zPath);
2821      pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2822      if( pCur->zPath==0 ) return SQLITE_NOMEM;
2823      if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2824        fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2825        return SQLITE_ERROR;
2826      }
2827      return SQLITE_OK;
2828    }
2829    closedir(pLvl->pDir);
2830    sqlite3_free(pLvl->zDir);
2831    pLvl->pDir = 0;
2832    pLvl->zDir = 0;
2833    pCur->iLvl--;
2834  }
2835
2836  /* EOF */
2837  sqlite3_free(pCur->zPath);
2838  pCur->zPath = 0;
2839  return SQLITE_OK;
2840}
2841
2842/*
2843** Return values of columns for the row at which the series_cursor
2844** is currently pointing.
2845*/
2846static int fsdirColumn(
2847  sqlite3_vtab_cursor *cur,   /* The cursor */
2848  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2849  int i                       /* Which column to return */
2850){
2851  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2852  switch( i ){
2853    case FSDIR_COLUMN_NAME: {
2854      sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2855      break;
2856    }
2857
2858    case FSDIR_COLUMN_MODE:
2859      sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2860      break;
2861
2862    case FSDIR_COLUMN_MTIME:
2863      sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2864      break;
2865
2866    case FSDIR_COLUMN_DATA: {
2867      mode_t m = pCur->sStat.st_mode;
2868      if( S_ISDIR(m) ){
2869        sqlite3_result_null(ctx);
2870#if !defined(_WIN32) && !defined(WIN32)
2871      }else if( S_ISLNK(m) ){
2872        char aStatic[64];
2873        char *aBuf = aStatic;
2874        sqlite3_int64 nBuf = 64;
2875        int n;
2876
2877        while( 1 ){
2878          n = readlink(pCur->zPath, aBuf, nBuf);
2879          if( n<nBuf ) break;
2880          if( aBuf!=aStatic ) sqlite3_free(aBuf);
2881          nBuf = nBuf*2;
2882          aBuf = sqlite3_malloc64(nBuf);
2883          if( aBuf==0 ){
2884            sqlite3_result_error_nomem(ctx);
2885            return SQLITE_NOMEM;
2886          }
2887        }
2888
2889        sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2890        if( aBuf!=aStatic ) sqlite3_free(aBuf);
2891#endif
2892      }else{
2893        readFileContents(ctx, pCur->zPath);
2894      }
2895    }
2896    case FSDIR_COLUMN_PATH:
2897    default: {
2898      /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2899      ** always return their values as NULL */
2900      break;
2901    }
2902  }
2903  return SQLITE_OK;
2904}
2905
2906/*
2907** Return the rowid for the current row. In this implementation, the
2908** first row returned is assigned rowid value 1, and each subsequent
2909** row a value 1 more than that of the previous.
2910*/
2911static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2912  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2913  *pRowid = pCur->iRowid;
2914  return SQLITE_OK;
2915}
2916
2917/*
2918** Return TRUE if the cursor has been moved off of the last
2919** row of output.
2920*/
2921static int fsdirEof(sqlite3_vtab_cursor *cur){
2922  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2923  return (pCur->zPath==0);
2924}
2925
2926/*
2927** xFilter callback.
2928**
2929** idxNum==1   PATH parameter only
2930** idxNum==2   Both PATH and DIR supplied
2931*/
2932static int fsdirFilter(
2933  sqlite3_vtab_cursor *cur,
2934  int idxNum, const char *idxStr,
2935  int argc, sqlite3_value **argv
2936){
2937  const char *zDir = 0;
2938  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2939  (void)idxStr;
2940  fsdirResetCursor(pCur);
2941
2942  if( idxNum==0 ){
2943    fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2944    return SQLITE_ERROR;
2945  }
2946
2947  assert( argc==idxNum && (argc==1 || argc==2) );
2948  zDir = (const char*)sqlite3_value_text(argv[0]);
2949  if( zDir==0 ){
2950    fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2951    return SQLITE_ERROR;
2952  }
2953  if( argc==2 ){
2954    pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2955  }
2956  if( pCur->zBase ){
2957    pCur->nBase = (int)strlen(pCur->zBase)+1;
2958    pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2959  }else{
2960    pCur->zPath = sqlite3_mprintf("%s", zDir);
2961  }
2962
2963  if( pCur->zPath==0 ){
2964    return SQLITE_NOMEM;
2965  }
2966  if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2967    fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2968    return SQLITE_ERROR;
2969  }
2970
2971  return SQLITE_OK;
2972}
2973
2974/*
2975** SQLite will invoke this method one or more times while planning a query
2976** that uses the generate_series virtual table.  This routine needs to create
2977** a query plan for each invocation and compute an estimated cost for that
2978** plan.
2979**
2980** In this implementation idxNum is used to represent the
2981** query plan.  idxStr is unused.
2982**
2983** The query plan is represented by values of idxNum:
2984**
2985**  (1)  The path value is supplied by argv[0]
2986**  (2)  Path is in argv[0] and dir is in argv[1]
2987*/
2988static int fsdirBestIndex(
2989  sqlite3_vtab *tab,
2990  sqlite3_index_info *pIdxInfo
2991){
2992  int i;                 /* Loop over constraints */
2993  int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2994  int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2995  int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
2996  int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
2997  const struct sqlite3_index_constraint *pConstraint;
2998
2999  (void)tab;
3000  pConstraint = pIdxInfo->aConstraint;
3001  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3002    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3003    switch( pConstraint->iColumn ){
3004      case FSDIR_COLUMN_PATH: {
3005        if( pConstraint->usable ){
3006          idxPath = i;
3007          seenPath = 0;
3008        }else if( idxPath<0 ){
3009          seenPath = 1;
3010        }
3011        break;
3012      }
3013      case FSDIR_COLUMN_DIR: {
3014        if( pConstraint->usable ){
3015          idxDir = i;
3016          seenDir = 0;
3017        }else if( idxDir<0 ){
3018          seenDir = 1;
3019        }
3020        break;
3021      }
3022    }
3023  }
3024  if( seenPath || seenDir ){
3025    /* If input parameters are unusable, disallow this plan */
3026    return SQLITE_CONSTRAINT;
3027  }
3028
3029  if( idxPath<0 ){
3030    pIdxInfo->idxNum = 0;
3031    /* The pIdxInfo->estimatedCost should have been initialized to a huge
3032    ** number.  Leave it unchanged. */
3033    pIdxInfo->estimatedRows = 0x7fffffff;
3034  }else{
3035    pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3036    pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3037    if( idxDir>=0 ){
3038      pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3039      pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3040      pIdxInfo->idxNum = 2;
3041      pIdxInfo->estimatedCost = 10.0;
3042    }else{
3043      pIdxInfo->idxNum = 1;
3044      pIdxInfo->estimatedCost = 100.0;
3045    }
3046  }
3047
3048  return SQLITE_OK;
3049}
3050
3051/*
3052** Register the "fsdir" virtual table.
3053*/
3054static int fsdirRegister(sqlite3 *db){
3055  static sqlite3_module fsdirModule = {
3056    0,                         /* iVersion */
3057    0,                         /* xCreate */
3058    fsdirConnect,              /* xConnect */
3059    fsdirBestIndex,            /* xBestIndex */
3060    fsdirDisconnect,           /* xDisconnect */
3061    0,                         /* xDestroy */
3062    fsdirOpen,                 /* xOpen - open a cursor */
3063    fsdirClose,                /* xClose - close a cursor */
3064    fsdirFilter,               /* xFilter - configure scan constraints */
3065    fsdirNext,                 /* xNext - advance a cursor */
3066    fsdirEof,                  /* xEof - check for end of scan */
3067    fsdirColumn,               /* xColumn - read data */
3068    fsdirRowid,                /* xRowid - read data */
3069    0,                         /* xUpdate */
3070    0,                         /* xBegin */
3071    0,                         /* xSync */
3072    0,                         /* xCommit */
3073    0,                         /* xRollback */
3074    0,                         /* xFindMethod */
3075    0,                         /* xRename */
3076    0,                         /* xSavepoint */
3077    0,                         /* xRelease */
3078    0,                         /* xRollbackTo */
3079    0,                         /* xShadowName */
3080  };
3081
3082  int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3083  return rc;
3084}
3085#else         /* SQLITE_OMIT_VIRTUALTABLE */
3086# define fsdirRegister(x) SQLITE_OK
3087#endif
3088
3089#ifdef _WIN32
3090
3091#endif
3092int sqlite3_fileio_init(
3093  sqlite3 *db,
3094  char **pzErrMsg,
3095  const sqlite3_api_routines *pApi
3096){
3097  int rc = SQLITE_OK;
3098  SQLITE_EXTENSION_INIT2(pApi);
3099  (void)pzErrMsg;  /* Unused parameter */
3100  rc = sqlite3_create_function(db, "readfile", 1,
3101                               SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3102                               readfileFunc, 0, 0);
3103  if( rc==SQLITE_OK ){
3104    rc = sqlite3_create_function(db, "writefile", -1,
3105                                 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3106                                 writefileFunc, 0, 0);
3107  }
3108  if( rc==SQLITE_OK ){
3109    rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3110                                 lsModeFunc, 0, 0);
3111  }
3112  if( rc==SQLITE_OK ){
3113    rc = fsdirRegister(db);
3114  }
3115  return rc;
3116}
3117
3118/************************* End ../ext/misc/fileio.c ********************/
3119/************************* Begin ../ext/misc/completion.c ******************/
3120/*
3121** 2017-07-10
3122**
3123** The author disclaims copyright to this source code.  In place of
3124** a legal notice, here is a blessing:
3125**
3126**    May you do good and not evil.
3127**    May you find forgiveness for yourself and forgive others.
3128**    May you share freely, never taking more than you give.
3129**
3130*************************************************************************
3131**
3132** This file implements an eponymous virtual table that returns suggested
3133** completions for a partial SQL input.
3134**
3135** Suggested usage:
3136**
3137**     SELECT DISTINCT candidate COLLATE nocase
3138**       FROM completion($prefix,$wholeline)
3139**      ORDER BY 1;
3140**
3141** The two query parameters are optional.  $prefix is the text of the
3142** current word being typed and that is to be completed.  $wholeline is
3143** the complete input line, used for context.
3144**
3145** The raw completion() table might return the same candidate multiple
3146** times, for example if the same column name is used to two or more
3147** tables.  And the candidates are returned in an arbitrary order.  Hence,
3148** the DISTINCT and ORDER BY are recommended.
3149**
3150** This virtual table operates at the speed of human typing, and so there
3151** is no attempt to make it fast.  Even a slow implementation will be much
3152** faster than any human can type.
3153**
3154*/
3155/* #include "sqlite3ext.h" */
3156SQLITE_EXTENSION_INIT1
3157#include <assert.h>
3158#include <string.h>
3159#include <ctype.h>
3160
3161#ifndef SQLITE_OMIT_VIRTUALTABLE
3162
3163/* completion_vtab is a subclass of sqlite3_vtab which will
3164** serve as the underlying representation of a completion virtual table
3165*/
3166typedef struct completion_vtab completion_vtab;
3167struct completion_vtab {
3168  sqlite3_vtab base;  /* Base class - must be first */
3169  sqlite3 *db;        /* Database connection for this completion vtab */
3170};
3171
3172/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3173** serve as the underlying representation of a cursor that scans
3174** over rows of the result
3175*/
3176typedef struct completion_cursor completion_cursor;
3177struct completion_cursor {
3178  sqlite3_vtab_cursor base;  /* Base class - must be first */
3179  sqlite3 *db;               /* Database connection for this cursor */
3180  int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3181  char *zPrefix;             /* The prefix for the word we want to complete */
3182  char *zLine;               /* The whole that we want to complete */
3183  const char *zCurrentRow;   /* Current output row */
3184  int szRow;                 /* Length of the zCurrentRow string */
3185  sqlite3_stmt *pStmt;       /* Current statement */
3186  sqlite3_int64 iRowid;      /* The rowid */
3187  int ePhase;                /* Current phase */
3188  int j;                     /* inter-phase counter */
3189};
3190
3191/* Values for ePhase:
3192*/
3193#define COMPLETION_FIRST_PHASE   1
3194#define COMPLETION_KEYWORDS      1
3195#define COMPLETION_PRAGMAS       2
3196#define COMPLETION_FUNCTIONS     3
3197#define COMPLETION_COLLATIONS    4
3198#define COMPLETION_INDEXES       5
3199#define COMPLETION_TRIGGERS      6
3200#define COMPLETION_DATABASES     7
3201#define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3202#define COMPLETION_COLUMNS       9
3203#define COMPLETION_MODULES       10
3204#define COMPLETION_EOF           11
3205
3206/*
3207** The completionConnect() method is invoked to create a new
3208** completion_vtab that describes the completion virtual table.
3209**
3210** Think of this routine as the constructor for completion_vtab objects.
3211**
3212** All this routine needs to do is:
3213**
3214**    (1) Allocate the completion_vtab object and initialize all fields.
3215**
3216**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3217**        result set of queries against completion will look like.
3218*/
3219static int completionConnect(
3220  sqlite3 *db,
3221  void *pAux,
3222  int argc, const char *const*argv,
3223  sqlite3_vtab **ppVtab,
3224  char **pzErr
3225){
3226  completion_vtab *pNew;
3227  int rc;
3228
3229  (void)(pAux);    /* Unused parameter */
3230  (void)(argc);    /* Unused parameter */
3231  (void)(argv);    /* Unused parameter */
3232  (void)(pzErr);   /* Unused parameter */
3233
3234/* Column numbers */
3235#define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3236#define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3237#define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3238#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3239
3240  sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3241  rc = sqlite3_declare_vtab(db,
3242      "CREATE TABLE x("
3243      "  candidate TEXT,"
3244      "  prefix TEXT HIDDEN,"
3245      "  wholeline TEXT HIDDEN,"
3246      "  phase INT HIDDEN"        /* Used for debugging only */
3247      ")");
3248  if( rc==SQLITE_OK ){
3249    pNew = sqlite3_malloc( sizeof(*pNew) );
3250    *ppVtab = (sqlite3_vtab*)pNew;
3251    if( pNew==0 ) return SQLITE_NOMEM;
3252    memset(pNew, 0, sizeof(*pNew));
3253    pNew->db = db;
3254  }
3255  return rc;
3256}
3257
3258/*
3259** This method is the destructor for completion_cursor objects.
3260*/
3261static int completionDisconnect(sqlite3_vtab *pVtab){
3262  sqlite3_free(pVtab);
3263  return SQLITE_OK;
3264}
3265
3266/*
3267** Constructor for a new completion_cursor object.
3268*/
3269static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3270  completion_cursor *pCur;
3271  pCur = sqlite3_malloc( sizeof(*pCur) );
3272  if( pCur==0 ) return SQLITE_NOMEM;
3273  memset(pCur, 0, sizeof(*pCur));
3274  pCur->db = ((completion_vtab*)p)->db;
3275  *ppCursor = &pCur->base;
3276  return SQLITE_OK;
3277}
3278
3279/*
3280** Reset the completion_cursor.
3281*/
3282static void completionCursorReset(completion_cursor *pCur){
3283  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3284  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3285  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3286  pCur->j = 0;
3287}
3288
3289/*
3290** Destructor for a completion_cursor.
3291*/
3292static int completionClose(sqlite3_vtab_cursor *cur){
3293  completionCursorReset((completion_cursor*)cur);
3294  sqlite3_free(cur);
3295  return SQLITE_OK;
3296}
3297
3298/*
3299** Advance a completion_cursor to its next row of output.
3300**
3301** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3302** record the current state of the scan.  This routine sets ->zCurrentRow
3303** to the current row of output and then returns.  If no more rows remain,
3304** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3305** table that has reached the end of its scan.
3306**
3307** The current implementation just lists potential identifiers and
3308** keywords and filters them by zPrefix.  Future enhancements should
3309** take zLine into account to try to restrict the set of identifiers and
3310** keywords based on what would be legal at the current point of input.
3311*/
3312static int completionNext(sqlite3_vtab_cursor *cur){
3313  completion_cursor *pCur = (completion_cursor*)cur;
3314  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3315  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3316  pCur->iRowid++;
3317  while( pCur->ePhase!=COMPLETION_EOF ){
3318    switch( pCur->ePhase ){
3319      case COMPLETION_KEYWORDS: {
3320        if( pCur->j >= sqlite3_keyword_count() ){
3321          pCur->zCurrentRow = 0;
3322          pCur->ePhase = COMPLETION_DATABASES;
3323        }else{
3324          sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3325        }
3326        iCol = -1;
3327        break;
3328      }
3329      case COMPLETION_DATABASES: {
3330        if( pCur->pStmt==0 ){
3331          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3332                             &pCur->pStmt, 0);
3333        }
3334        iCol = 1;
3335        eNextPhase = COMPLETION_TABLES;
3336        break;
3337      }
3338      case COMPLETION_TABLES: {
3339        if( pCur->pStmt==0 ){
3340          sqlite3_stmt *pS2;
3341          char *zSql = 0;
3342          const char *zSep = "";
3343          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3344          while( sqlite3_step(pS2)==SQLITE_ROW ){
3345            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3346            zSql = sqlite3_mprintf(
3347               "%z%s"
3348               "SELECT name FROM \"%w\".sqlite_schema",
3349               zSql, zSep, zDb
3350            );
3351            if( zSql==0 ) return SQLITE_NOMEM;
3352            zSep = " UNION ";
3353          }
3354          sqlite3_finalize(pS2);
3355          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3356          sqlite3_free(zSql);
3357        }
3358        iCol = 0;
3359        eNextPhase = COMPLETION_COLUMNS;
3360        break;
3361      }
3362      case COMPLETION_COLUMNS: {
3363        if( pCur->pStmt==0 ){
3364          sqlite3_stmt *pS2;
3365          char *zSql = 0;
3366          const char *zSep = "";
3367          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3368          while( sqlite3_step(pS2)==SQLITE_ROW ){
3369            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3370            zSql = sqlite3_mprintf(
3371               "%z%s"
3372               "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3373                       " JOIN pragma_table_info(sm.name,%Q) AS pti"
3374               " WHERE sm.type='table'",
3375               zSql, zSep, zDb, zDb
3376            );
3377            if( zSql==0 ) return SQLITE_NOMEM;
3378            zSep = " UNION ";
3379          }
3380          sqlite3_finalize(pS2);
3381          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3382          sqlite3_free(zSql);
3383        }
3384        iCol = 0;
3385        eNextPhase = COMPLETION_EOF;
3386        break;
3387      }
3388    }
3389    if( iCol<0 ){
3390      /* This case is when the phase presets zCurrentRow */
3391      if( pCur->zCurrentRow==0 ) continue;
3392    }else{
3393      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3394        /* Extract the next row of content */
3395        pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3396        pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3397      }else{
3398        /* When all rows are finished, advance to the next phase */
3399        sqlite3_finalize(pCur->pStmt);
3400        pCur->pStmt = 0;
3401        pCur->ePhase = eNextPhase;
3402        continue;
3403      }
3404    }
3405    if( pCur->nPrefix==0 ) break;
3406    if( pCur->nPrefix<=pCur->szRow
3407     && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3408    ){
3409      break;
3410    }
3411  }
3412
3413  return SQLITE_OK;
3414}
3415
3416/*
3417** Return values of columns for the row at which the completion_cursor
3418** is currently pointing.
3419*/
3420static int completionColumn(
3421  sqlite3_vtab_cursor *cur,   /* The cursor */
3422  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3423  int i                       /* Which column to return */
3424){
3425  completion_cursor *pCur = (completion_cursor*)cur;
3426  switch( i ){
3427    case COMPLETION_COLUMN_CANDIDATE: {
3428      sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3429      break;
3430    }
3431    case COMPLETION_COLUMN_PREFIX: {
3432      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3433      break;
3434    }
3435    case COMPLETION_COLUMN_WHOLELINE: {
3436      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3437      break;
3438    }
3439    case COMPLETION_COLUMN_PHASE: {
3440      sqlite3_result_int(ctx, pCur->ePhase);
3441      break;
3442    }
3443  }
3444  return SQLITE_OK;
3445}
3446
3447/*
3448** Return the rowid for the current row.  In this implementation, the
3449** rowid is the same as the output value.
3450*/
3451static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3452  completion_cursor *pCur = (completion_cursor*)cur;
3453  *pRowid = pCur->iRowid;
3454  return SQLITE_OK;
3455}
3456
3457/*
3458** Return TRUE if the cursor has been moved off of the last
3459** row of output.
3460*/
3461static int completionEof(sqlite3_vtab_cursor *cur){
3462  completion_cursor *pCur = (completion_cursor*)cur;
3463  return pCur->ePhase >= COMPLETION_EOF;
3464}
3465
3466/*
3467** This method is called to "rewind" the completion_cursor object back
3468** to the first row of output.  This method is always called at least
3469** once prior to any call to completionColumn() or completionRowid() or
3470** completionEof().
3471*/
3472static int completionFilter(
3473  sqlite3_vtab_cursor *pVtabCursor,
3474  int idxNum, const char *idxStr,
3475  int argc, sqlite3_value **argv
3476){
3477  completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3478  int iArg = 0;
3479  (void)(idxStr);   /* Unused parameter */
3480  (void)(argc);     /* Unused parameter */
3481  completionCursorReset(pCur);
3482  if( idxNum & 1 ){
3483    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3484    if( pCur->nPrefix>0 ){
3485      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3486      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3487    }
3488    iArg = 1;
3489  }
3490  if( idxNum & 2 ){
3491    pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3492    if( pCur->nLine>0 ){
3493      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3494      if( pCur->zLine==0 ) return SQLITE_NOMEM;
3495    }
3496  }
3497  if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3498    int i = pCur->nLine;
3499    while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3500      i--;
3501    }
3502    pCur->nPrefix = pCur->nLine - i;
3503    if( pCur->nPrefix>0 ){
3504      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3505      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3506    }
3507  }
3508  pCur->iRowid = 0;
3509  pCur->ePhase = COMPLETION_FIRST_PHASE;
3510  return completionNext(pVtabCursor);
3511}
3512
3513/*
3514** SQLite will invoke this method one or more times while planning a query
3515** that uses the completion virtual table.  This routine needs to create
3516** a query plan for each invocation and compute an estimated cost for that
3517** plan.
3518**
3519** There are two hidden parameters that act as arguments to the table-valued
3520** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3521** is available and bit 1 is set if "wholeline" is available.
3522*/
3523static int completionBestIndex(
3524  sqlite3_vtab *tab,
3525  sqlite3_index_info *pIdxInfo
3526){
3527  int i;                 /* Loop over constraints */
3528  int idxNum = 0;        /* The query plan bitmask */
3529  int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3530  int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3531  int nArg = 0;          /* Number of arguments that completeFilter() expects */
3532  const struct sqlite3_index_constraint *pConstraint;
3533
3534  (void)(tab);    /* Unused parameter */
3535  pConstraint = pIdxInfo->aConstraint;
3536  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3537    if( pConstraint->usable==0 ) continue;
3538    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3539    switch( pConstraint->iColumn ){
3540      case COMPLETION_COLUMN_PREFIX:
3541        prefixIdx = i;
3542        idxNum |= 1;
3543        break;
3544      case COMPLETION_COLUMN_WHOLELINE:
3545        wholelineIdx = i;
3546        idxNum |= 2;
3547        break;
3548    }
3549  }
3550  if( prefixIdx>=0 ){
3551    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3552    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3553  }
3554  if( wholelineIdx>=0 ){
3555    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3556    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3557  }
3558  pIdxInfo->idxNum = idxNum;
3559  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3560  pIdxInfo->estimatedRows = 500 - 100*nArg;
3561  return SQLITE_OK;
3562}
3563
3564/*
3565** This following structure defines all the methods for the
3566** completion virtual table.
3567*/
3568static sqlite3_module completionModule = {
3569  0,                         /* iVersion */
3570  0,                         /* xCreate */
3571  completionConnect,         /* xConnect */
3572  completionBestIndex,       /* xBestIndex */
3573  completionDisconnect,      /* xDisconnect */
3574  0,                         /* xDestroy */
3575  completionOpen,            /* xOpen - open a cursor */
3576  completionClose,           /* xClose - close a cursor */
3577  completionFilter,          /* xFilter - configure scan constraints */
3578  completionNext,            /* xNext - advance a cursor */
3579  completionEof,             /* xEof - check for end of scan */
3580  completionColumn,          /* xColumn - read data */
3581  completionRowid,           /* xRowid - read data */
3582  0,                         /* xUpdate */
3583  0,                         /* xBegin */
3584  0,                         /* xSync */
3585  0,                         /* xCommit */
3586  0,                         /* xRollback */
3587  0,                         /* xFindMethod */
3588  0,                         /* xRename */
3589  0,                         /* xSavepoint */
3590  0,                         /* xRelease */
3591  0,                         /* xRollbackTo */
3592  0                          /* xShadowName */
3593};
3594
3595#endif /* SQLITE_OMIT_VIRTUALTABLE */
3596
3597int sqlite3CompletionVtabInit(sqlite3 *db){
3598  int rc = SQLITE_OK;
3599#ifndef SQLITE_OMIT_VIRTUALTABLE
3600  rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3601#endif
3602  return rc;
3603}
3604
3605#ifdef _WIN32
3606
3607#endif
3608int sqlite3_completion_init(
3609  sqlite3 *db,
3610  char **pzErrMsg,
3611  const sqlite3_api_routines *pApi
3612){
3613  int rc = SQLITE_OK;
3614  SQLITE_EXTENSION_INIT2(pApi);
3615  (void)(pzErrMsg);  /* Unused parameter */
3616#ifndef SQLITE_OMIT_VIRTUALTABLE
3617  rc = sqlite3CompletionVtabInit(db);
3618#endif
3619  return rc;
3620}
3621
3622/************************* End ../ext/misc/completion.c ********************/
3623/************************* Begin ../ext/misc/appendvfs.c ******************/
3624/*
3625** 2017-10-20
3626**
3627** The author disclaims copyright to this source code.  In place of
3628** a legal notice, here is a blessing:
3629**
3630**    May you do good and not evil.
3631**    May you find forgiveness for yourself and forgive others.
3632**    May you share freely, never taking more than you give.
3633**
3634******************************************************************************
3635**
3636** This file implements a VFS shim that allows an SQLite database to be
3637** appended onto the end of some other file, such as an executable.
3638**
3639** A special record must appear at the end of the file that identifies the
3640** file as an appended database and provides an offset to page 1.  For
3641** best performance page 1 should be located at a disk page boundary, though
3642** that is not required.
3643**
3644** When opening a database using this VFS, the connection might treat
3645** the file as an ordinary SQLite database, or it might treat is as a
3646** database appended onto some other file.  Here are the rules:
3647**
3648**  (1)  When opening a new empty file, that file is treated as an ordinary
3649**       database.
3650**
3651**  (2)  When opening a file that begins with the standard SQLite prefix
3652**       string "SQLite format 3", that file is treated as an ordinary
3653**       database.
3654**
3655**  (3)  When opening a file that ends with the appendvfs trailer string
3656**       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3657**       database.
3658**
3659**  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3660**       set, then a new database is appended to the already existing file.
3661**
3662**  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3663**
3664** To avoid unnecessary complications with the PENDING_BYTE, the size of
3665** the file containing the database is limited to 1GB.  This VFS will refuse
3666** to read or write past the 1GB mark.  This restriction might be lifted in
3667** future versions.  For now, if you need a large database, then keep the
3668** database in a separate file.
3669**
3670** If the file being opened is not an appended database, then this shim is
3671** a pass-through into the default underlying VFS.
3672**/
3673/* #include "sqlite3ext.h" */
3674SQLITE_EXTENSION_INIT1
3675#include <string.h>
3676#include <assert.h>
3677
3678/* The append mark at the end of the database is:
3679**
3680**     Start-Of-SQLite3-NNNNNNNN
3681**     123456789 123456789 12345
3682**
3683** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3684** the offset to page 1.
3685*/
3686#define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3687#define APND_MARK_PREFIX_SZ  17
3688#define APND_MARK_SIZE       25
3689
3690/*
3691** Maximum size of the combined prefix + database + append-mark.  This
3692** must be less than 0x40000000 to avoid locking issues on Windows.
3693*/
3694#define APND_MAX_SIZE  (65536*15259)
3695
3696/*
3697** Forward declaration of objects used by this utility
3698*/
3699typedef struct sqlite3_vfs ApndVfs;
3700typedef struct ApndFile ApndFile;
3701
3702/* Access to a lower-level VFS that (might) implement dynamic loading,
3703** access to randomness, etc.
3704*/
3705#define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3706#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3707
3708/* An open file */
3709struct ApndFile {
3710  sqlite3_file base;              /* IO methods */
3711  sqlite3_int64 iPgOne;           /* File offset to page 1 */
3712  sqlite3_int64 iMark;            /* Start of the append-mark */
3713};
3714
3715/*
3716** Methods for ApndFile
3717*/
3718static int apndClose(sqlite3_file*);
3719static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3720static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3721static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3722static int apndSync(sqlite3_file*, int flags);
3723static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3724static int apndLock(sqlite3_file*, int);
3725static int apndUnlock(sqlite3_file*, int);
3726static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3727static int apndFileControl(sqlite3_file*, int op, void *pArg);
3728static int apndSectorSize(sqlite3_file*);
3729static int apndDeviceCharacteristics(sqlite3_file*);
3730static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3731static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3732static void apndShmBarrier(sqlite3_file*);
3733static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3734static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3735static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3736
3737/*
3738** Methods for ApndVfs
3739*/
3740static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3741static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3742static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3743static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3744static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3745static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3746static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3747static void apndDlClose(sqlite3_vfs*, void*);
3748static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3749static int apndSleep(sqlite3_vfs*, int microseconds);
3750static int apndCurrentTime(sqlite3_vfs*, double*);
3751static int apndGetLastError(sqlite3_vfs*, int, char *);
3752static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3753static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3754static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3755static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3756
3757static sqlite3_vfs apnd_vfs = {
3758  3,                            /* iVersion (set when registered) */
3759  0,                            /* szOsFile (set when registered) */
3760  1024,                         /* mxPathname */
3761  0,                            /* pNext */
3762  "apndvfs",                    /* zName */
3763  0,                            /* pAppData (set when registered) */
3764  apndOpen,                     /* xOpen */
3765  apndDelete,                   /* xDelete */
3766  apndAccess,                   /* xAccess */
3767  apndFullPathname,             /* xFullPathname */
3768  apndDlOpen,                   /* xDlOpen */
3769  apndDlError,                  /* xDlError */
3770  apndDlSym,                    /* xDlSym */
3771  apndDlClose,                  /* xDlClose */
3772  apndRandomness,               /* xRandomness */
3773  apndSleep,                    /* xSleep */
3774  apndCurrentTime,              /* xCurrentTime */
3775  apndGetLastError,             /* xGetLastError */
3776  apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3777  apndSetSystemCall,            /* xSetSystemCall */
3778  apndGetSystemCall,            /* xGetSystemCall */
3779  apndNextSystemCall            /* xNextSystemCall */
3780};
3781
3782static const sqlite3_io_methods apnd_io_methods = {
3783  3,                              /* iVersion */
3784  apndClose,                      /* xClose */
3785  apndRead,                       /* xRead */
3786  apndWrite,                      /* xWrite */
3787  apndTruncate,                   /* xTruncate */
3788  apndSync,                       /* xSync */
3789  apndFileSize,                   /* xFileSize */
3790  apndLock,                       /* xLock */
3791  apndUnlock,                     /* xUnlock */
3792  apndCheckReservedLock,          /* xCheckReservedLock */
3793  apndFileControl,                /* xFileControl */
3794  apndSectorSize,                 /* xSectorSize */
3795  apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3796  apndShmMap,                     /* xShmMap */
3797  apndShmLock,                    /* xShmLock */
3798  apndShmBarrier,                 /* xShmBarrier */
3799  apndShmUnmap,                   /* xShmUnmap */
3800  apndFetch,                      /* xFetch */
3801  apndUnfetch                     /* xUnfetch */
3802};
3803
3804
3805
3806/*
3807** Close an apnd-file.
3808*/
3809static int apndClose(sqlite3_file *pFile){
3810  pFile = ORIGFILE(pFile);
3811  return pFile->pMethods->xClose(pFile);
3812}
3813
3814/*
3815** Read data from an apnd-file.
3816*/
3817static int apndRead(
3818  sqlite3_file *pFile,
3819  void *zBuf,
3820  int iAmt,
3821  sqlite_int64 iOfst
3822){
3823  ApndFile *p = (ApndFile *)pFile;
3824  pFile = ORIGFILE(pFile);
3825  return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3826}
3827
3828/*
3829** Add the append-mark onto the end of the file.
3830*/
3831static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3832  int i;
3833  unsigned char a[APND_MARK_SIZE];
3834  memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3835  for(i=0; i<8; i++){
3836    a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3837  }
3838  return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3839}
3840
3841/*
3842** Write data to an apnd-file.
3843*/
3844static int apndWrite(
3845  sqlite3_file *pFile,
3846  const void *zBuf,
3847  int iAmt,
3848  sqlite_int64 iOfst
3849){
3850  int rc;
3851  ApndFile *p = (ApndFile *)pFile;
3852  pFile = ORIGFILE(pFile);
3853  if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3854  rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3855  if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3856    sqlite3_int64 sz = 0;
3857    rc = pFile->pMethods->xFileSize(pFile, &sz);
3858    if( rc==SQLITE_OK ){
3859      p->iMark = sz - APND_MARK_SIZE;
3860      if( iOfst + iAmt + p->iPgOne > p->iMark ){
3861        p->iMark = p->iPgOne + iOfst + iAmt;
3862        rc = apndWriteMark(p, pFile);
3863      }
3864    }
3865  }
3866  return rc;
3867}
3868
3869/*
3870** Truncate an apnd-file.
3871*/
3872static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3873  int rc;
3874  ApndFile *p = (ApndFile *)pFile;
3875  pFile = ORIGFILE(pFile);
3876  rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3877  if( rc==SQLITE_OK ){
3878    p->iMark = p->iPgOne+size;
3879    rc = apndWriteMark(p, pFile);
3880  }
3881  return rc;
3882}
3883
3884/*
3885** Sync an apnd-file.
3886*/
3887static int apndSync(sqlite3_file *pFile, int flags){
3888  pFile = ORIGFILE(pFile);
3889  return pFile->pMethods->xSync(pFile, flags);
3890}
3891
3892/*
3893** Return the current file-size of an apnd-file.
3894*/
3895static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3896  ApndFile *p = (ApndFile *)pFile;
3897  int rc;
3898  pFile = ORIGFILE(p);
3899  rc = pFile->pMethods->xFileSize(pFile, pSize);
3900  if( rc==SQLITE_OK && p->iPgOne ){
3901    *pSize -= p->iPgOne + APND_MARK_SIZE;
3902  }
3903  return rc;
3904}
3905
3906/*
3907** Lock an apnd-file.
3908*/
3909static int apndLock(sqlite3_file *pFile, int eLock){
3910  pFile = ORIGFILE(pFile);
3911  return pFile->pMethods->xLock(pFile, eLock);
3912}
3913
3914/*
3915** Unlock an apnd-file.
3916*/
3917static int apndUnlock(sqlite3_file *pFile, int eLock){
3918  pFile = ORIGFILE(pFile);
3919  return pFile->pMethods->xUnlock(pFile, eLock);
3920}
3921
3922/*
3923** Check if another file-handle holds a RESERVED lock on an apnd-file.
3924*/
3925static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3926  pFile = ORIGFILE(pFile);
3927  return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3928}
3929
3930/*
3931** File control method. For custom operations on an apnd-file.
3932*/
3933static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3934  ApndFile *p = (ApndFile *)pFile;
3935  int rc;
3936  pFile = ORIGFILE(pFile);
3937  rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3938  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3939    *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3940  }
3941  return rc;
3942}
3943
3944/*
3945** Return the sector-size in bytes for an apnd-file.
3946*/
3947static int apndSectorSize(sqlite3_file *pFile){
3948  pFile = ORIGFILE(pFile);
3949  return pFile->pMethods->xSectorSize(pFile);
3950}
3951
3952/*
3953** Return the device characteristic flags supported by an apnd-file.
3954*/
3955static int apndDeviceCharacteristics(sqlite3_file *pFile){
3956  pFile = ORIGFILE(pFile);
3957  return pFile->pMethods->xDeviceCharacteristics(pFile);
3958}
3959
3960/* Create a shared memory file mapping */
3961static int apndShmMap(
3962  sqlite3_file *pFile,
3963  int iPg,
3964  int pgsz,
3965  int bExtend,
3966  void volatile **pp
3967){
3968  pFile = ORIGFILE(pFile);
3969  return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3970}
3971
3972/* Perform locking on a shared-memory segment */
3973static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3974  pFile = ORIGFILE(pFile);
3975  return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3976}
3977
3978/* Memory barrier operation on shared memory */
3979static void apndShmBarrier(sqlite3_file *pFile){
3980  pFile = ORIGFILE(pFile);
3981  pFile->pMethods->xShmBarrier(pFile);
3982}
3983
3984/* Unmap a shared memory segment */
3985static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3986  pFile = ORIGFILE(pFile);
3987  return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3988}
3989
3990/* Fetch a page of a memory-mapped file */
3991static int apndFetch(
3992  sqlite3_file *pFile,
3993  sqlite3_int64 iOfst,
3994  int iAmt,
3995  void **pp
3996){
3997  ApndFile *p = (ApndFile *)pFile;
3998  pFile = ORIGFILE(pFile);
3999  return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4000}
4001
4002/* Release a memory-mapped page */
4003static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4004  ApndFile *p = (ApndFile *)pFile;
4005  pFile = ORIGFILE(pFile);
4006  return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4007}
4008
4009/*
4010** Check to see if the file is an ordinary SQLite database file.
4011*/
4012static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4013  int rc;
4014  char zHdr[16];
4015  static const char aSqliteHdr[] = "SQLite format 3";
4016  if( sz<512 ) return 0;
4017  rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
4018  if( rc ) return 0;
4019  return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
4020}
4021
4022/*
4023** Try to read the append-mark off the end of a file.  Return the
4024** start of the appended database if the append-mark is present.  If
4025** there is no append-mark, return -1;
4026*/
4027static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4028  int rc, i;
4029  sqlite3_int64 iMark;
4030  unsigned char a[APND_MARK_SIZE];
4031
4032  if( sz<=APND_MARK_SIZE ) return -1;
4033  rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4034  if( rc ) return -1;
4035  if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4036  iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
4037  for(i=1; i<8; i++){
4038    iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
4039  }
4040  return iMark;
4041}
4042
4043/*
4044** Open an apnd file handle.
4045*/
4046static int apndOpen(
4047  sqlite3_vfs *pVfs,
4048  const char *zName,
4049  sqlite3_file *pFile,
4050  int flags,
4051  int *pOutFlags
4052){
4053  ApndFile *p;
4054  sqlite3_file *pSubFile;
4055  sqlite3_vfs *pSubVfs;
4056  int rc;
4057  sqlite3_int64 sz;
4058  pSubVfs = ORIGVFS(pVfs);
4059  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4060    return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4061  }
4062  p = (ApndFile*)pFile;
4063  memset(p, 0, sizeof(*p));
4064  pSubFile = ORIGFILE(pFile);
4065  pFile->pMethods = &apnd_io_methods;
4066  rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4067  if( rc ) goto apnd_open_done;
4068  rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4069  if( rc ){
4070    pSubFile->pMethods->xClose(pSubFile);
4071    goto apnd_open_done;
4072  }
4073  if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4074    memmove(pFile, pSubFile, pSubVfs->szOsFile);
4075    return SQLITE_OK;
4076  }
4077  p->iMark = 0;
4078  p->iPgOne = apndReadMark(sz, pFile);
4079  if( p->iPgOne>0 ){
4080    return SQLITE_OK;
4081  }
4082  if( (flags & SQLITE_OPEN_CREATE)==0 ){
4083    pSubFile->pMethods->xClose(pSubFile);
4084    rc = SQLITE_CANTOPEN;
4085  }
4086  p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4087apnd_open_done:
4088  if( rc ) pFile->pMethods = 0;
4089  return rc;
4090}
4091
4092/*
4093** All other VFS methods are pass-thrus.
4094*/
4095static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4096  return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4097}
4098static int apndAccess(
4099  sqlite3_vfs *pVfs,
4100  const char *zPath,
4101  int flags,
4102  int *pResOut
4103){
4104  return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4105}
4106static int apndFullPathname(
4107  sqlite3_vfs *pVfs,
4108  const char *zPath,
4109  int nOut,
4110  char *zOut
4111){
4112  return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4113}
4114static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4115  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4116}
4117static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4118  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4119}
4120static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4121  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4122}
4123static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4124  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4125}
4126static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4127  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4128}
4129static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4130  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4131}
4132static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4133  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4134}
4135static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4136  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4137}
4138static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4139  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4140}
4141static int apndSetSystemCall(
4142  sqlite3_vfs *pVfs,
4143  const char *zName,
4144  sqlite3_syscall_ptr pCall
4145){
4146  return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4147}
4148static sqlite3_syscall_ptr apndGetSystemCall(
4149  sqlite3_vfs *pVfs,
4150  const char *zName
4151){
4152  return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4153}
4154static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4155  return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4156}
4157
4158
4159#ifdef _WIN32
4160
4161#endif
4162/*
4163** This routine is called when the extension is loaded.
4164** Register the new VFS.
4165*/
4166int sqlite3_appendvfs_init(
4167  sqlite3 *db,
4168  char **pzErrMsg,
4169  const sqlite3_api_routines *pApi
4170){
4171  int rc = SQLITE_OK;
4172  sqlite3_vfs *pOrig;
4173  SQLITE_EXTENSION_INIT2(pApi);
4174  (void)pzErrMsg;
4175  (void)db;
4176  pOrig = sqlite3_vfs_find(0);
4177  apnd_vfs.iVersion = pOrig->iVersion;
4178  apnd_vfs.pAppData = pOrig;
4179  apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4180  rc = sqlite3_vfs_register(&apnd_vfs, 0);
4181#ifdef APPENDVFS_TEST
4182  if( rc==SQLITE_OK ){
4183    rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4184  }
4185#endif
4186  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4187  return rc;
4188}
4189
4190/************************* End ../ext/misc/appendvfs.c ********************/
4191/************************* Begin ../ext/misc/memtrace.c ******************/
4192/*
4193** 2019-01-21
4194**
4195** The author disclaims copyright to this source code.  In place of
4196** a legal notice, here is a blessing:
4197**
4198**    May you do good and not evil.
4199**    May you find forgiveness for yourself and forgive others.
4200**    May you share freely, never taking more than you give.
4201**
4202*************************************************************************
4203**
4204** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4205** mechanism to add a tracing layer on top of SQLite.  If this extension
4206** is registered prior to sqlite3_initialize(), it will cause all memory
4207** allocation activities to be logged on standard output, or to some other
4208** FILE specified by the initializer.
4209**
4210** This file needs to be compiled into the application that uses it.
4211**
4212** This extension is used to implement the --memtrace option of the
4213** command-line shell.
4214*/
4215#include <assert.h>
4216#include <string.h>
4217#include <stdio.h>
4218
4219/* The original memory allocation routines */
4220static sqlite3_mem_methods memtraceBase;
4221static FILE *memtraceOut;
4222
4223/* Methods that trace memory allocations */
4224static void *memtraceMalloc(int n){
4225  if( memtraceOut ){
4226    fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4227            memtraceBase.xRoundup(n));
4228  }
4229  return memtraceBase.xMalloc(n);
4230}
4231static void memtraceFree(void *p){
4232  if( p==0 ) return;
4233  if( memtraceOut ){
4234    fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4235  }
4236  memtraceBase.xFree(p);
4237}
4238static void *memtraceRealloc(void *p, int n){
4239  if( p==0 ) return memtraceMalloc(n);
4240  if( n==0 ){
4241    memtraceFree(p);
4242    return 0;
4243  }
4244  if( memtraceOut ){
4245    fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4246            memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4247  }
4248  return memtraceBase.xRealloc(p, n);
4249}
4250static int memtraceSize(void *p){
4251  return memtraceBase.xSize(p);
4252}
4253static int memtraceRoundup(int n){
4254  return memtraceBase.xRoundup(n);
4255}
4256static int memtraceInit(void *p){
4257  return memtraceBase.xInit(p);
4258}
4259static void memtraceShutdown(void *p){
4260  memtraceBase.xShutdown(p);
4261}
4262
4263/* The substitute memory allocator */
4264static sqlite3_mem_methods ersaztMethods = {
4265  memtraceMalloc,
4266  memtraceFree,
4267  memtraceRealloc,
4268  memtraceSize,
4269  memtraceRoundup,
4270  memtraceInit,
4271  memtraceShutdown,
4272  0
4273};
4274
4275/* Begin tracing memory allocations to out. */
4276int sqlite3MemTraceActivate(FILE *out){
4277  int rc = SQLITE_OK;
4278  if( memtraceBase.xMalloc==0 ){
4279    rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4280    if( rc==SQLITE_OK ){
4281      rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4282    }
4283  }
4284  memtraceOut = out;
4285  return rc;
4286}
4287
4288/* Deactivate memory tracing */
4289int sqlite3MemTraceDeactivate(void){
4290  int rc = SQLITE_OK;
4291  if( memtraceBase.xMalloc!=0 ){
4292    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4293    if( rc==SQLITE_OK ){
4294      memset(&memtraceBase, 0, sizeof(memtraceBase));
4295    }
4296  }
4297  memtraceOut = 0;
4298  return rc;
4299}
4300
4301/************************* End ../ext/misc/memtrace.c ********************/
4302/************************* Begin ../ext/misc/uint.c ******************/
4303/*
4304** 2020-04-14
4305**
4306** The author disclaims copyright to this source code.  In place of
4307** a legal notice, here is a blessing:
4308**
4309**    May you do good and not evil.
4310**    May you find forgiveness for yourself and forgive others.
4311**    May you share freely, never taking more than you give.
4312**
4313******************************************************************************
4314**
4315** This SQLite extension implements the UINT collating sequence.
4316**
4317** UINT works like BINARY for text, except that embedded strings
4318** of digits compare in numeric order.
4319**
4320**     *   Leading zeros are handled properly, in the sense that
4321**         they do not mess of the maginitude comparison of embedded
4322**         strings of digits.  "x00123y" is equal to "x123y".
4323**
4324**     *   Only unsigned integers are recognized.  Plus and minus
4325**         signs are ignored.  Decimal points and exponential notation
4326**         are ignored.
4327**
4328**     *   Embedded integers can be of arbitrary length.  Comparison
4329**         is *not* limited integers that can be expressed as a
4330**         64-bit machine integer.
4331*/
4332/* #include "sqlite3ext.h" */
4333SQLITE_EXTENSION_INIT1
4334#include <assert.h>
4335#include <string.h>
4336#include <ctype.h>
4337
4338/*
4339** Compare text in lexicographic order, except strings of digits
4340** compare in numeric order.
4341*/
4342static int uintCollFunc(
4343  void *notUsed,
4344  int nKey1, const void *pKey1,
4345  int nKey2, const void *pKey2
4346){
4347  const unsigned char *zA = (const unsigned char*)pKey1;
4348  const unsigned char *zB = (const unsigned char*)pKey2;
4349  int i=0, j=0, x;
4350  (void)notUsed;
4351  while( i<nKey1 && j<nKey2 ){
4352    x = zA[i] - zB[j];
4353    if( isdigit(zA[i]) ){
4354      int k;
4355      if( !isdigit(zB[j]) ) return x;
4356      while( i<nKey1 && zA[i]=='0' ){ i++; }
4357      while( j<nKey2 && zB[j]=='0' ){ j++; }
4358      k = 0;
4359      while( i+k<nKey1 && isdigit(zA[i+k])
4360             && j+k<nKey2 && isdigit(zB[j+k]) ){
4361        k++;
4362      }
4363      if( i+k<nKey1 && isdigit(zA[i+k]) ){
4364        return +1;
4365      }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4366        return -1;
4367      }else{
4368        x = memcmp(zA+i, zB+j, k);
4369        if( x ) return x;
4370        i += k;
4371        j += k;
4372      }
4373    }else if( x ){
4374      return x;
4375    }else{
4376      i++;
4377      j++;
4378    }
4379  }
4380  return (nKey1 - i) - (nKey2 - j);
4381}
4382
4383#ifdef _WIN32
4384
4385#endif
4386int sqlite3_uint_init(
4387  sqlite3 *db,
4388  char **pzErrMsg,
4389  const sqlite3_api_routines *pApi
4390){
4391  SQLITE_EXTENSION_INIT2(pApi);
4392  (void)pzErrMsg;  /* Unused parameter */
4393  return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4394}
4395
4396/************************* End ../ext/misc/uint.c ********************/
4397/************************* Begin ../ext/misc/decimal.c ******************/
4398/*
4399** 2020-06-22
4400**
4401** The author disclaims copyright to this source code.  In place of
4402** a legal notice, here is a blessing:
4403**
4404**    May you do good and not evil.
4405**    May you find forgiveness for yourself and forgive others.
4406**    May you share freely, never taking more than you give.
4407**
4408******************************************************************************
4409**
4410** Routines to implement arbitrary-precision decimal math.
4411**
4412** The focus here is on simplicity and correctness, not performance.
4413*/
4414/* #include "sqlite3ext.h" */
4415SQLITE_EXTENSION_INIT1
4416#include <assert.h>
4417#include <string.h>
4418#include <ctype.h>
4419#include <stdlib.h>
4420
4421/* Mark a function parameter as unused, to suppress nuisance compiler
4422** warnings. */
4423#ifndef UNUSED_PARAMETER
4424# define UNUSED_PARAMETER(X)  (void)(X)
4425#endif
4426
4427
4428/* A decimal object */
4429typedef struct Decimal Decimal;
4430struct Decimal {
4431  char sign;        /* 0 for positive, 1 for negative */
4432  char oom;         /* True if an OOM is encountered */
4433  char isNull;      /* True if holds a NULL rather than a number */
4434  char isInit;      /* True upon initialization */
4435  int nDigit;       /* Total number of digits */
4436  int nFrac;        /* Number of digits to the right of the decimal point */
4437  signed char *a;   /* Array of digits.  Most significant first. */
4438};
4439
4440/*
4441** Release memory held by a Decimal, but do not free the object itself.
4442*/
4443static void decimal_clear(Decimal *p){
4444  sqlite3_free(p->a);
4445}
4446
4447/*
4448** Destroy a Decimal object
4449*/
4450static void decimal_free(Decimal *p){
4451  if( p ){
4452    decimal_clear(p);
4453    sqlite3_free(p);
4454  }
4455}
4456
4457/*
4458** Allocate a new Decimal object.  Initialize it to the number given
4459** by the input string.
4460*/
4461static Decimal *decimal_new(
4462  sqlite3_context *pCtx,
4463  sqlite3_value *pIn,
4464  int nAlt,
4465  const unsigned char *zAlt
4466){
4467  Decimal *p;
4468  int n, i;
4469  const unsigned char *zIn;
4470  int iExp = 0;
4471  p = sqlite3_malloc( sizeof(*p) );
4472  if( p==0 ) goto new_no_mem;
4473  p->sign = 0;
4474  p->oom = 0;
4475  p->isInit = 1;
4476  p->isNull = 0;
4477  p->nDigit = 0;
4478  p->nFrac = 0;
4479  if( zAlt ){
4480    n = nAlt,
4481    zIn = zAlt;
4482  }else{
4483    if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4484      p->a = 0;
4485      p->isNull = 1;
4486      return p;
4487    }
4488    n = sqlite3_value_bytes(pIn);
4489    zIn = sqlite3_value_text(pIn);
4490  }
4491  p->a = sqlite3_malloc64( n+1 );
4492  if( p->a==0 ) goto new_no_mem;
4493  for(i=0; isspace(zIn[i]); i++){}
4494  if( zIn[i]=='-' ){
4495    p->sign = 1;
4496    i++;
4497  }else if( zIn[i]=='+' ){
4498    i++;
4499  }
4500  while( i<n && zIn[i]=='0' ) i++;
4501  while( i<n ){
4502    char c = zIn[i];
4503    if( c>='0' && c<='9' ){
4504      p->a[p->nDigit++] = c - '0';
4505    }else if( c=='.' ){
4506      p->nFrac = p->nDigit + 1;
4507    }else if( c=='e' || c=='E' ){
4508      int j = i+1;
4509      int neg = 0;
4510      if( j>=n ) break;
4511      if( zIn[j]=='-' ){
4512        neg = 1;
4513        j++;
4514      }else if( zIn[j]=='+' ){
4515        j++;
4516      }
4517      while( j<n && iExp<1000000 ){
4518        if( zIn[j]>='0' && zIn[j]<='9' ){
4519          iExp = iExp*10 + zIn[j] - '0';
4520        }
4521        j++;
4522      }
4523      if( neg ) iExp = -iExp;
4524      break;
4525    }
4526    i++;
4527  }
4528  if( p->nFrac ){
4529    p->nFrac = p->nDigit - (p->nFrac - 1);
4530  }
4531  if( iExp>0 ){
4532    if( p->nFrac>0 ){
4533      if( iExp<=p->nFrac ){
4534        p->nFrac -= iExp;
4535        iExp = 0;
4536      }else{
4537        iExp -= p->nFrac;
4538        p->nFrac = 0;
4539      }
4540    }
4541    if( iExp>0 ){
4542      p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4543      if( p->a==0 ) goto new_no_mem;
4544      memset(p->a+p->nDigit, 0, iExp);
4545      p->nDigit += iExp;
4546    }
4547  }else if( iExp<0 ){
4548    int nExtra;
4549    iExp = -iExp;
4550    nExtra = p->nDigit - p->nFrac - 1;
4551    if( nExtra ){
4552      if( nExtra>=iExp ){
4553        p->nFrac += iExp;
4554        iExp  = 0;
4555      }else{
4556        iExp -= nExtra;
4557        p->nFrac = p->nDigit - 1;
4558      }
4559    }
4560    if( iExp>0 ){
4561      p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4562      if( p->a==0 ) goto new_no_mem;
4563      memmove(p->a+iExp, p->a, p->nDigit);
4564      memset(p->a, 0, iExp);
4565      p->nDigit += iExp;
4566      p->nFrac += iExp;
4567    }
4568  }
4569  return p;
4570
4571new_no_mem:
4572  if( pCtx ) sqlite3_result_error_nomem(pCtx);
4573  sqlite3_free(p);
4574  return 0;
4575}
4576
4577/*
4578** Make the given Decimal the result.
4579*/
4580static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4581  char *z;
4582  int i, j;
4583  int n;
4584  if( p==0 || p->oom ){
4585    sqlite3_result_error_nomem(pCtx);
4586    return;
4587  }
4588  if( p->isNull ){
4589    sqlite3_result_null(pCtx);
4590    return;
4591  }
4592  z = sqlite3_malloc( p->nDigit+4 );
4593  if( z==0 ){
4594    sqlite3_result_error_nomem(pCtx);
4595    return;
4596  }
4597  i = 0;
4598  if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4599    p->sign = 0;
4600  }
4601  if( p->sign ){
4602    z[0] = '-';
4603    i = 1;
4604  }
4605  n = p->nDigit - p->nFrac;
4606  if( n<=0 ){
4607    z[i++] = '0';
4608  }
4609  j = 0;
4610  while( n>1 && p->a[j]==0 ){
4611    j++;
4612    n--;
4613  }
4614  while( n>0  ){
4615    z[i++] = p->a[j] + '0';
4616    j++;
4617    n--;
4618  }
4619  if( p->nFrac ){
4620    z[i++] = '.';
4621    do{
4622      z[i++] = p->a[j] + '0';
4623      j++;
4624    }while( j<p->nDigit );
4625  }
4626  z[i] = 0;
4627  sqlite3_result_text(pCtx, z, i, sqlite3_free);
4628}
4629
4630/*
4631** SQL Function:   decimal(X)
4632**
4633** Convert input X into decimal and then back into text
4634*/
4635static void decimalFunc(
4636  sqlite3_context *context,
4637  int argc,
4638  sqlite3_value **argv
4639){
4640  Decimal *p = decimal_new(context, argv[0], 0, 0);
4641  UNUSED_PARAMETER(argc);
4642  decimal_result(context, p);
4643  decimal_free(p);
4644}
4645
4646/*
4647** Compare to Decimal objects.  Return negative, 0, or positive if the
4648** first object is less than, equal to, or greater than the second.
4649**
4650** Preconditions for this routine:
4651**
4652**    pA!=0
4653**    pA->isNull==0
4654**    pB!=0
4655**    pB->isNull==0
4656*/
4657static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4658  int nASig, nBSig, rc, n;
4659  if( pA->sign!=pB->sign ){
4660    return pA->sign ? -1 : +1;
4661  }
4662  if( pA->sign ){
4663    const Decimal *pTemp = pA;
4664    pA = pB;
4665    pB = pTemp;
4666  }
4667  nASig = pA->nDigit - pA->nFrac;
4668  nBSig = pB->nDigit - pB->nFrac;
4669  if( nASig!=nBSig ){
4670    return nASig - nBSig;
4671  }
4672  n = pA->nDigit;
4673  if( n>pB->nDigit ) n = pB->nDigit;
4674  rc = memcmp(pA->a, pB->a, n);
4675  if( rc==0 ){
4676    rc = pA->nDigit - pB->nDigit;
4677  }
4678  return rc;
4679}
4680
4681/*
4682** SQL Function:   decimal_cmp(X, Y)
4683**
4684** Return negative, zero, or positive if X is less then, equal to, or
4685** greater than Y.
4686*/
4687static void decimalCmpFunc(
4688  sqlite3_context *context,
4689  int argc,
4690  sqlite3_value **argv
4691){
4692  Decimal *pA = 0, *pB = 0;
4693  int rc;
4694
4695  UNUSED_PARAMETER(argc);
4696  pA = decimal_new(context, argv[0], 0, 0);
4697  if( pA==0 || pA->isNull ) goto cmp_done;
4698  pB = decimal_new(context, argv[1], 0, 0);
4699  if( pB==0 || pB->isNull ) goto cmp_done;
4700  rc = decimal_cmp(pA, pB);
4701  if( rc<0 ) rc = -1;
4702  else if( rc>0 ) rc = +1;
4703  sqlite3_result_int(context, rc);
4704cmp_done:
4705  decimal_free(pA);
4706  decimal_free(pB);
4707}
4708
4709/*
4710** Expand the Decimal so that it has a least nDigit digits and nFrac
4711** digits to the right of the decimal point.
4712*/
4713static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4714  int nAddSig;
4715  int nAddFrac;
4716  if( p==0 ) return;
4717  nAddFrac = nFrac - p->nFrac;
4718  nAddSig = (nDigit - p->nDigit) - nAddFrac;
4719  if( nAddFrac==0 && nAddSig==0 ) return;
4720  p->a = sqlite3_realloc64(p->a, nDigit+1);
4721  if( p->a==0 ){
4722    p->oom = 1;
4723    return;
4724  }
4725  if( nAddSig ){
4726    memmove(p->a+nAddSig, p->a, p->nDigit);
4727    memset(p->a, 0, nAddSig);
4728    p->nDigit += nAddSig;
4729  }
4730  if( nAddFrac ){
4731    memset(p->a+p->nDigit, 0, nAddFrac);
4732    p->nDigit += nAddFrac;
4733    p->nFrac += nAddFrac;
4734  }
4735}
4736
4737/*
4738** Add the value pB into pA.
4739**
4740** Both pA and pB might become denormalized by this routine.
4741*/
4742static void decimal_add(Decimal *pA, Decimal *pB){
4743  int nSig, nFrac, nDigit;
4744  int i, rc;
4745  if( pA==0 ){
4746    return;
4747  }
4748  if( pA->oom || pB==0 || pB->oom ){
4749    pA->oom = 1;
4750    return;
4751  }
4752  if( pA->isNull || pB->isNull ){
4753    pA->isNull = 1;
4754    return;
4755  }
4756  nSig = pA->nDigit - pA->nFrac;
4757  if( nSig && pA->a[0]==0 ) nSig--;
4758  if( nSig<pB->nDigit-pB->nFrac ){
4759    nSig = pB->nDigit - pB->nFrac;
4760  }
4761  nFrac = pA->nFrac;
4762  if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4763  nDigit = nSig + nFrac + 1;
4764  decimal_expand(pA, nDigit, nFrac);
4765  decimal_expand(pB, nDigit, nFrac);
4766  if( pA->oom || pB->oom ){
4767    pA->oom = 1;
4768  }else{
4769    if( pA->sign==pB->sign ){
4770      int carry = 0;
4771      for(i=nDigit-1; i>=0; i--){
4772        int x = pA->a[i] + pB->a[i] + carry;
4773        if( x>=10 ){
4774          carry = 1;
4775          pA->a[i] = x - 10;
4776        }else{
4777          carry = 0;
4778          pA->a[i] = x;
4779        }
4780      }
4781    }else{
4782      signed char *aA, *aB;
4783      int borrow = 0;
4784      rc = memcmp(pA->a, pB->a, nDigit);
4785      if( rc<0 ){
4786        aA = pB->a;
4787        aB = pA->a;
4788        pA->sign = !pA->sign;
4789      }else{
4790        aA = pA->a;
4791        aB = pB->a;
4792      }
4793      for(i=nDigit-1; i>=0; i--){
4794        int x = aA[i] - aB[i] - borrow;
4795        if( x<0 ){
4796          pA->a[i] = x+10;
4797          borrow = 1;
4798        }else{
4799          pA->a[i] = x;
4800          borrow = 0;
4801        }
4802      }
4803    }
4804  }
4805}
4806
4807/*
4808** Compare text in decimal order.
4809*/
4810static int decimalCollFunc(
4811  void *notUsed,
4812  int nKey1, const void *pKey1,
4813  int nKey2, const void *pKey2
4814){
4815  const unsigned char *zA = (const unsigned char*)pKey1;
4816  const unsigned char *zB = (const unsigned char*)pKey2;
4817  Decimal *pA = decimal_new(0, 0, nKey1, zA);
4818  Decimal *pB = decimal_new(0, 0, nKey2, zB);
4819  int rc;
4820  UNUSED_PARAMETER(notUsed);
4821  if( pA==0 || pB==0 ){
4822    rc = 0;
4823  }else{
4824    rc = decimal_cmp(pA, pB);
4825  }
4826  decimal_free(pA);
4827  decimal_free(pB);
4828  return rc;
4829}
4830
4831
4832/*
4833** SQL Function:   decimal_add(X, Y)
4834**                 decimal_sub(X, Y)
4835**
4836** Return the sum or difference of X and Y.
4837*/
4838static void decimalAddFunc(
4839  sqlite3_context *context,
4840  int argc,
4841  sqlite3_value **argv
4842){
4843  Decimal *pA = decimal_new(context, argv[0], 0, 0);
4844  Decimal *pB = decimal_new(context, argv[1], 0, 0);
4845  UNUSED_PARAMETER(argc);
4846  decimal_add(pA, pB);
4847  decimal_result(context, pA);
4848  decimal_free(pA);
4849  decimal_free(pB);
4850}
4851static void decimalSubFunc(
4852  sqlite3_context *context,
4853  int argc,
4854  sqlite3_value **argv
4855){
4856  Decimal *pA = decimal_new(context, argv[0], 0, 0);
4857  Decimal *pB = decimal_new(context, argv[1], 0, 0);
4858  UNUSED_PARAMETER(argc);
4859  if( pB==0 ) return;
4860  pB->sign = !pB->sign;
4861  decimal_add(pA, pB);
4862  decimal_result(context, pA);
4863  decimal_free(pA);
4864  decimal_free(pB);
4865}
4866
4867/* Aggregate funcion:   decimal_sum(X)
4868**
4869** Works like sum() except that it uses decimal arithmetic for unlimited
4870** precision.
4871*/
4872static void decimalSumStep(
4873  sqlite3_context *context,
4874  int argc,
4875  sqlite3_value **argv
4876){
4877  Decimal *p;
4878  Decimal *pArg;
4879  UNUSED_PARAMETER(argc);
4880  p = sqlite3_aggregate_context(context, sizeof(*p));
4881  if( p==0 ) return;
4882  if( !p->isInit ){
4883    p->isInit = 1;
4884    p->a = sqlite3_malloc(2);
4885    if( p->a==0 ){
4886      p->oom = 1;
4887    }else{
4888      p->a[0] = 0;
4889    }
4890    p->nDigit = 1;
4891    p->nFrac = 0;
4892  }
4893  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4894  pArg = decimal_new(context, argv[0], 0, 0);
4895  decimal_add(p, pArg);
4896  decimal_free(pArg);
4897}
4898static void decimalSumInverse(
4899  sqlite3_context *context,
4900  int argc,
4901  sqlite3_value **argv
4902){
4903  Decimal *p;
4904  Decimal *pArg;
4905  UNUSED_PARAMETER(argc);
4906  p = sqlite3_aggregate_context(context, sizeof(*p));
4907  if( p==0 ) return;
4908  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4909  pArg = decimal_new(context, argv[0], 0, 0);
4910  if( pArg ) pArg->sign = !pArg->sign;
4911  decimal_add(p, pArg);
4912  decimal_free(pArg);
4913}
4914static void decimalSumValue(sqlite3_context *context){
4915  Decimal *p = sqlite3_aggregate_context(context, 0);
4916  if( p==0 ) return;
4917  decimal_result(context, p);
4918}
4919static void decimalSumFinalize(sqlite3_context *context){
4920  Decimal *p = sqlite3_aggregate_context(context, 0);
4921  if( p==0 ) return;
4922  decimal_result(context, p);
4923  decimal_clear(p);
4924}
4925
4926/*
4927** SQL Function:   decimal_mul(X, Y)
4928**
4929** Return the product of X and Y.
4930**
4931** All significant digits after the decimal point are retained.
4932** Trailing zeros after the decimal point are omitted as long as
4933** the number of digits after the decimal point is no less than
4934** either the number of digits in either input.
4935*/
4936static void decimalMulFunc(
4937  sqlite3_context *context,
4938  int argc,
4939  sqlite3_value **argv
4940){
4941  Decimal *pA = decimal_new(context, argv[0], 0, 0);
4942  Decimal *pB = decimal_new(context, argv[1], 0, 0);
4943  signed char *acc = 0;
4944  int i, j, k;
4945  int minFrac;
4946  UNUSED_PARAMETER(argc);
4947  if( pA==0 || pA->oom || pA->isNull
4948   || pB==0 || pB->oom || pB->isNull
4949  ){
4950    goto mul_end;
4951  }
4952  acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4953  if( acc==0 ){
4954    sqlite3_result_error_nomem(context);
4955    goto mul_end;
4956  }
4957  memset(acc, 0, pA->nDigit + pB->nDigit + 2);
4958  minFrac = pA->nFrac;
4959  if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
4960  for(i=pA->nDigit-1; i>=0; i--){
4961    signed char f = pA->a[i];
4962    int carry = 0, x;
4963    for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
4964      x = acc[k] + f*pB->a[j] + carry;
4965      acc[k] = x%10;
4966      carry = x/10;
4967    }
4968    x = acc[k] + carry;
4969    acc[k] = x%10;
4970    acc[k-1] += x/10;
4971  }
4972  sqlite3_free(pA->a);
4973  pA->a = acc;
4974  acc = 0;
4975  pA->nDigit += pB->nDigit + 2;
4976  pA->nFrac += pB->nFrac;
4977  pA->sign ^= pB->sign;
4978  while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
4979    pA->nFrac--;
4980    pA->nDigit--;
4981  }
4982  decimal_result(context, pA);
4983
4984mul_end:
4985  sqlite3_free(acc);
4986  decimal_free(pA);
4987  decimal_free(pB);
4988}
4989
4990#ifdef _WIN32
4991
4992#endif
4993int sqlite3_decimal_init(
4994  sqlite3 *db,
4995  char **pzErrMsg,
4996  const sqlite3_api_routines *pApi
4997){
4998  int rc = SQLITE_OK;
4999  static const struct {
5000    const char *zFuncName;
5001    int nArg;
5002    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5003  } aFunc[] = {
5004    { "decimal",       1,   decimalFunc        },
5005    { "decimal_cmp",   2,   decimalCmpFunc     },
5006    { "decimal_add",   2,   decimalAddFunc     },
5007    { "decimal_sub",   2,   decimalSubFunc     },
5008    { "decimal_mul",   2,   decimalMulFunc     },
5009  };
5010  unsigned int i;
5011  (void)pzErrMsg;  /* Unused parameter */
5012
5013  SQLITE_EXTENSION_INIT2(pApi);
5014
5015  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5016    rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5017                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5018                   0, aFunc[i].xFunc, 0, 0);
5019  }
5020  if( rc==SQLITE_OK ){
5021    rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5022                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5023                   decimalSumStep, decimalSumFinalize,
5024                   decimalSumValue, decimalSumInverse, 0);
5025  }
5026  if( rc==SQLITE_OK ){
5027    rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5028                                  0, decimalCollFunc);
5029  }
5030  return rc;
5031}
5032
5033/************************* End ../ext/misc/decimal.c ********************/
5034/************************* Begin ../ext/misc/ieee754.c ******************/
5035/*
5036** 2013-04-17
5037**
5038** The author disclaims copyright to this source code.  In place of
5039** a legal notice, here is a blessing:
5040**
5041**    May you do good and not evil.
5042**    May you find forgiveness for yourself and forgive others.
5043**    May you share freely, never taking more than you give.
5044**
5045******************************************************************************
5046**
5047** This SQLite extension implements functions for the exact display
5048** and input of IEEE754 Binary64 floating-point numbers.
5049**
5050**   ieee754(X)
5051**   ieee754(Y,Z)
5052**
5053** In the first form, the value X should be a floating-point number.
5054** The function will return a string of the form 'ieee754(Y,Z)' where
5055** Y and Z are integers such that X==Y*pow(2,Z).
5056**
5057** In the second form, Y and Z are integers which are the mantissa and
5058** base-2 exponent of a new floating point number.  The function returns
5059** a floating-point value equal to Y*pow(2,Z).
5060**
5061** Examples:
5062**
5063**     ieee754(2.0)             ->     'ieee754(2,0)'
5064**     ieee754(45.25)           ->     'ieee754(181,-2)'
5065**     ieee754(2, 0)            ->     2.0
5066**     ieee754(181, -2)         ->     45.25
5067**
5068** Two additional functions break apart the one-argument ieee754()
5069** result into separate integer values:
5070**
5071**     ieee754_mantissa(45.25)  ->     181
5072**     ieee754_exponent(45.25)  ->     -2
5073**
5074** These functions convert binary64 numbers into blobs and back again.
5075**
5076**     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5077**     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5078**
5079** In all single-argument functions, if the argument is an 8-byte blob
5080** then that blob is interpreted as a big-endian binary64 value.
5081**
5082**
5083** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5084** -----------------------------------------------
5085**
5086** This extension in combination with the separate 'decimal' extension
5087** can be used to compute the exact decimal representation of binary64
5088** values.  To begin, first compute a table of exponent values:
5089**
5090**    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5091**    WITH RECURSIVE c(x,v) AS (
5092**      VALUES(0,'1')
5093**      UNION ALL
5094**      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5095**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5096**    WITH RECURSIVE c(x,v) AS (
5097**      VALUES(-1,'0.5')
5098**      UNION ALL
5099**      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5100**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5101**
5102** Then, to compute the exact decimal representation of a floating
5103** point value (the value 47.49 is used in the example) do:
5104**
5105**    WITH c(n) AS (VALUES(47.49))
5106**          ---------------^^^^^---- Replace with whatever you want
5107**    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5108**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5109**
5110** Here is a query to show various boundry values for the binary64
5111** number format:
5112**
5113**    WITH c(name,bin) AS (VALUES
5114**       ('minimum positive value',        x'0000000000000001'),
5115**       ('maximum subnormal value',       x'000fffffffffffff'),
5116**       ('mininum positive nornal value', x'0010000000000000'),
5117**       ('maximum value',                 x'7fefffffffffffff'))
5118**    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5119**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5120**
5121*/
5122/* #include "sqlite3ext.h" */
5123SQLITE_EXTENSION_INIT1
5124#include <assert.h>
5125#include <string.h>
5126
5127/* Mark a function parameter as unused, to suppress nuisance compiler
5128** warnings. */
5129#ifndef UNUSED_PARAMETER
5130# define UNUSED_PARAMETER(X)  (void)(X)
5131#endif
5132
5133/*
5134** Implementation of the ieee754() function
5135*/
5136static void ieee754func(
5137  sqlite3_context *context,
5138  int argc,
5139  sqlite3_value **argv
5140){
5141  if( argc==1 ){
5142    sqlite3_int64 m, a;
5143    double r;
5144    int e;
5145    int isNeg;
5146    char zResult[100];
5147    assert( sizeof(m)==sizeof(r) );
5148    if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5149     && sqlite3_value_bytes(argv[0])==sizeof(r)
5150    ){
5151      const unsigned char *x = sqlite3_value_blob(argv[0]);
5152      unsigned int i;
5153      sqlite3_uint64 v = 0;
5154      for(i=0; i<sizeof(r); i++){
5155        v = (v<<8) | x[i];
5156      }
5157      memcpy(&r, &v, sizeof(r));
5158    }else{
5159      r = sqlite3_value_double(argv[0]);
5160    }
5161    if( r<0.0 ){
5162      isNeg = 1;
5163      r = -r;
5164    }else{
5165      isNeg = 0;
5166    }
5167    memcpy(&a,&r,sizeof(a));
5168    if( a==0 ){
5169      e = 0;
5170      m = 0;
5171    }else{
5172      e = a>>52;
5173      m = a & ((((sqlite3_int64)1)<<52)-1);
5174      if( e==0 ){
5175        m <<= 1;
5176      }else{
5177        m |= ((sqlite3_int64)1)<<52;
5178      }
5179      while( e<1075 && m>0 && (m&1)==0 ){
5180        m >>= 1;
5181        e++;
5182      }
5183      if( isNeg ) m = -m;
5184    }
5185    switch( *(int*)sqlite3_user_data(context) ){
5186      case 0:
5187        sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5188                         m, e-1075);
5189        sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5190        break;
5191      case 1:
5192        sqlite3_result_int64(context, m);
5193        break;
5194      case 2:
5195        sqlite3_result_int(context, e-1075);
5196        break;
5197    }
5198  }else{
5199    sqlite3_int64 m, e, a;
5200    double r;
5201    int isNeg = 0;
5202    m = sqlite3_value_int64(argv[0]);
5203    e = sqlite3_value_int64(argv[1]);
5204    if( m<0 ){
5205      isNeg = 1;
5206      m = -m;
5207      if( m<0 ) return;
5208    }else if( m==0 && e>-1000 && e<1000 ){
5209      sqlite3_result_double(context, 0.0);
5210      return;
5211    }
5212    while( (m>>32)&0xffe00000 ){
5213      m >>= 1;
5214      e++;
5215    }
5216    while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5217      m <<= 1;
5218      e--;
5219    }
5220    e += 1075;
5221    if( e<=0 ){
5222      /* Subnormal */
5223      m >>= 1-e;
5224      e = 0;
5225    }else if( e>0x7ff ){
5226      e = 0x7ff;
5227    }
5228    a = m & ((((sqlite3_int64)1)<<52)-1);
5229    a |= e<<52;
5230    if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5231    memcpy(&r, &a, sizeof(r));
5232    sqlite3_result_double(context, r);
5233  }
5234}
5235
5236/*
5237** Functions to convert between blobs and floats.
5238*/
5239static void ieee754func_from_blob(
5240  sqlite3_context *context,
5241  int argc,
5242  sqlite3_value **argv
5243){
5244  UNUSED_PARAMETER(argc);
5245  if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5246   && sqlite3_value_bytes(argv[0])==sizeof(double)
5247  ){
5248    double r;
5249    const unsigned char *x = sqlite3_value_blob(argv[0]);
5250    unsigned int i;
5251    sqlite3_uint64 v = 0;
5252    for(i=0; i<sizeof(r); i++){
5253      v = (v<<8) | x[i];
5254    }
5255    memcpy(&r, &v, sizeof(r));
5256    sqlite3_result_double(context, r);
5257  }
5258}
5259static void ieee754func_to_blob(
5260  sqlite3_context *context,
5261  int argc,
5262  sqlite3_value **argv
5263){
5264  UNUSED_PARAMETER(argc);
5265  if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5266   || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5267  ){
5268    double r = sqlite3_value_double(argv[0]);
5269    sqlite3_uint64 v;
5270    unsigned char a[sizeof(r)];
5271    unsigned int i;
5272    memcpy(&v, &r, sizeof(r));
5273    for(i=1; i<=sizeof(r); i++){
5274      a[sizeof(r)-i] = v&0xff;
5275      v >>= 8;
5276    }
5277    sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5278  }
5279}
5280
5281
5282#ifdef _WIN32
5283
5284#endif
5285int sqlite3_ieee_init(
5286  sqlite3 *db,
5287  char **pzErrMsg,
5288  const sqlite3_api_routines *pApi
5289){
5290  static const struct {
5291    char *zFName;
5292    int nArg;
5293    int iAux;
5294    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5295  } aFunc[] = {
5296    { "ieee754",           1,   0, ieee754func },
5297    { "ieee754",           2,   0, ieee754func },
5298    { "ieee754_mantissa",  1,   1, ieee754func },
5299    { "ieee754_exponent",  1,   2, ieee754func },
5300    { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5301    { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5302
5303  };
5304  unsigned int i;
5305  int rc = SQLITE_OK;
5306  SQLITE_EXTENSION_INIT2(pApi);
5307  (void)pzErrMsg;  /* Unused parameter */
5308  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5309    rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5310                               SQLITE_UTF8|SQLITE_INNOCUOUS,
5311                               (void*)&aFunc[i].iAux,
5312                               aFunc[i].xFunc, 0, 0);
5313  }
5314  return rc;
5315}
5316
5317/************************* End ../ext/misc/ieee754.c ********************/
5318/************************* Begin ../ext/misc/series.c ******************/
5319/*
5320** 2015-08-18
5321**
5322** The author disclaims copyright to this source code.  In place of
5323** a legal notice, here is a blessing:
5324**
5325**    May you do good and not evil.
5326**    May you find forgiveness for yourself and forgive others.
5327**    May you share freely, never taking more than you give.
5328**
5329*************************************************************************
5330**
5331** This file demonstrates how to create a table-valued-function using
5332** a virtual table.  This demo implements the generate_series() function
5333** which gives similar results to the eponymous function in PostgreSQL.
5334** Examples:
5335**
5336**      SELECT * FROM generate_series(0,100,5);
5337**
5338** The query above returns integers from 0 through 100 counting by steps
5339** of 5.
5340**
5341**      SELECT * FROM generate_series(0,100);
5342**
5343** Integers from 0 through 100 with a step size of 1.
5344**
5345**      SELECT * FROM generate_series(20) LIMIT 10;
5346**
5347** Integers 20 through 29.
5348**
5349** HOW IT WORKS
5350**
5351** The generate_series "function" is really a virtual table with the
5352** following schema:
5353**
5354**     CREATE TABLE generate_series(
5355**       value,
5356**       start HIDDEN,
5357**       stop HIDDEN,
5358**       step HIDDEN
5359**     );
5360**
5361** Function arguments in queries against this virtual table are translated
5362** into equality constraints against successive hidden columns.  In other
5363** words, the following pairs of queries are equivalent to each other:
5364**
5365**    SELECT * FROM generate_series(0,100,5);
5366**    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5367**
5368**    SELECT * FROM generate_series(0,100);
5369**    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5370**
5371**    SELECT * FROM generate_series(20) LIMIT 10;
5372**    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5373**
5374** The generate_series virtual table implementation leaves the xCreate method
5375** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5376** TABLE command with "generate_series" as the USING argument.  Instead, there
5377** is a single generate_series virtual table that is always available without
5378** having to be created first.
5379**
5380** The xBestIndex method looks for equality constraints against the hidden
5381** start, stop, and step columns, and if present, it uses those constraints
5382** to bound the sequence of generated values.  If the equality constraints
5383** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5384** xBestIndex returns a small cost when both start and stop are available,
5385** and a very large cost if either start or stop are unavailable.  This
5386** encourages the query planner to order joins such that the bounds of the
5387** series are well-defined.
5388*/
5389/* #include "sqlite3ext.h" */
5390SQLITE_EXTENSION_INIT1
5391#include <assert.h>
5392#include <string.h>
5393
5394#ifndef SQLITE_OMIT_VIRTUALTABLE
5395
5396
5397/* series_cursor is a subclass of sqlite3_vtab_cursor which will
5398** serve as the underlying representation of a cursor that scans
5399** over rows of the result
5400*/
5401typedef struct series_cursor series_cursor;
5402struct series_cursor {
5403  sqlite3_vtab_cursor base;  /* Base class - must be first */
5404  int isDesc;                /* True to count down rather than up */
5405  sqlite3_int64 iRowid;      /* The rowid */
5406  sqlite3_int64 iValue;      /* Current value ("value") */
5407  sqlite3_int64 mnValue;     /* Mimimum value ("start") */
5408  sqlite3_int64 mxValue;     /* Maximum value ("stop") */
5409  sqlite3_int64 iStep;       /* Increment ("step") */
5410};
5411
5412/*
5413** The seriesConnect() method is invoked to create a new
5414** series_vtab that describes the generate_series virtual table.
5415**
5416** Think of this routine as the constructor for series_vtab objects.
5417**
5418** All this routine needs to do is:
5419**
5420**    (1) Allocate the series_vtab object and initialize all fields.
5421**
5422**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5423**        result set of queries against generate_series will look like.
5424*/
5425static int seriesConnect(
5426  sqlite3 *db,
5427  void *pUnused,
5428  int argcUnused, const char *const*argvUnused,
5429  sqlite3_vtab **ppVtab,
5430  char **pzErrUnused
5431){
5432  sqlite3_vtab *pNew;
5433  int rc;
5434
5435/* Column numbers */
5436#define SERIES_COLUMN_VALUE 0
5437#define SERIES_COLUMN_START 1
5438#define SERIES_COLUMN_STOP  2
5439#define SERIES_COLUMN_STEP  3
5440
5441  (void)pUnused;
5442  (void)argcUnused;
5443  (void)argvUnused;
5444  (void)pzErrUnused;
5445  rc = sqlite3_declare_vtab(db,
5446     "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5447  if( rc==SQLITE_OK ){
5448    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5449    if( pNew==0 ) return SQLITE_NOMEM;
5450    memset(pNew, 0, sizeof(*pNew));
5451    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5452  }
5453  return rc;
5454}
5455
5456/*
5457** This method is the destructor for series_cursor objects.
5458*/
5459static int seriesDisconnect(sqlite3_vtab *pVtab){
5460  sqlite3_free(pVtab);
5461  return SQLITE_OK;
5462}
5463
5464/*
5465** Constructor for a new series_cursor object.
5466*/
5467static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5468  series_cursor *pCur;
5469  (void)pUnused;
5470  pCur = sqlite3_malloc( sizeof(*pCur) );
5471  if( pCur==0 ) return SQLITE_NOMEM;
5472  memset(pCur, 0, sizeof(*pCur));
5473  *ppCursor = &pCur->base;
5474  return SQLITE_OK;
5475}
5476
5477/*
5478** Destructor for a series_cursor.
5479*/
5480static int seriesClose(sqlite3_vtab_cursor *cur){
5481  sqlite3_free(cur);
5482  return SQLITE_OK;
5483}
5484
5485
5486/*
5487** Advance a series_cursor to its next row of output.
5488*/
5489static int seriesNext(sqlite3_vtab_cursor *cur){
5490  series_cursor *pCur = (series_cursor*)cur;
5491  if( pCur->isDesc ){
5492    pCur->iValue -= pCur->iStep;
5493  }else{
5494    pCur->iValue += pCur->iStep;
5495  }
5496  pCur->iRowid++;
5497  return SQLITE_OK;
5498}
5499
5500/*
5501** Return values of columns for the row at which the series_cursor
5502** is currently pointing.
5503*/
5504static int seriesColumn(
5505  sqlite3_vtab_cursor *cur,   /* The cursor */
5506  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5507  int i                       /* Which column to return */
5508){
5509  series_cursor *pCur = (series_cursor*)cur;
5510  sqlite3_int64 x = 0;
5511  switch( i ){
5512    case SERIES_COLUMN_START:  x = pCur->mnValue; break;
5513    case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;
5514    case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;
5515    default:                   x = pCur->iValue;  break;
5516  }
5517  sqlite3_result_int64(ctx, x);
5518  return SQLITE_OK;
5519}
5520
5521/*
5522** Return the rowid for the current row. In this implementation, the
5523** first row returned is assigned rowid value 1, and each subsequent
5524** row a value 1 more than that of the previous.
5525*/
5526static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5527  series_cursor *pCur = (series_cursor*)cur;
5528  *pRowid = pCur->iRowid;
5529  return SQLITE_OK;
5530}
5531
5532/*
5533** Return TRUE if the cursor has been moved off of the last
5534** row of output.
5535*/
5536static int seriesEof(sqlite3_vtab_cursor *cur){
5537  series_cursor *pCur = (series_cursor*)cur;
5538  if( pCur->isDesc ){
5539    return pCur->iValue < pCur->mnValue;
5540  }else{
5541    return pCur->iValue > pCur->mxValue;
5542  }
5543}
5544
5545/* True to cause run-time checking of the start=, stop=, and/or step=
5546** parameters.  The only reason to do this is for testing the
5547** constraint checking logic for virtual tables in the SQLite core.
5548*/
5549#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5550# define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5551#endif
5552
5553/*
5554** This method is called to "rewind" the series_cursor object back
5555** to the first row of output.  This method is always called at least
5556** once prior to any call to seriesColumn() or seriesRowid() or
5557** seriesEof().
5558**
5559** The query plan selected by seriesBestIndex is passed in the idxNum
5560** parameter.  (idxStr is not used in this implementation.)  idxNum
5561** is a bitmask showing which constraints are available:
5562**
5563**    1:    start=VALUE
5564**    2:    stop=VALUE
5565**    4:    step=VALUE
5566**
5567** Also, if bit 8 is set, that means that the series should be output
5568** in descending order rather than in ascending order.
5569**
5570** This routine should initialize the cursor and position it so that it
5571** is pointing at the first row, or pointing off the end of the table
5572** (so that seriesEof() will return true) if the table is empty.
5573*/
5574static int seriesFilter(
5575  sqlite3_vtab_cursor *pVtabCursor,
5576  int idxNum, const char *idxStrUnused,
5577  int argc, sqlite3_value **argv
5578){
5579  series_cursor *pCur = (series_cursor *)pVtabCursor;
5580  int i = 0;
5581  (void)idxStrUnused;
5582  if( idxNum & 1 ){
5583    pCur->mnValue = sqlite3_value_int64(argv[i++]);
5584  }else{
5585    pCur->mnValue = 0;
5586  }
5587  if( idxNum & 2 ){
5588    pCur->mxValue = sqlite3_value_int64(argv[i++]);
5589  }else{
5590    pCur->mxValue = 0xffffffff;
5591  }
5592  if( idxNum & 4 ){
5593    pCur->iStep = sqlite3_value_int64(argv[i++]);
5594    if( pCur->iStep<1 ) pCur->iStep = 1;
5595  }else{
5596    pCur->iStep = 1;
5597  }
5598  for(i=0; i<argc; i++){
5599    if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5600      /* If any of the constraints have a NULL value, then return no rows.
5601      ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5602      pCur->mnValue = 1;
5603      pCur->mxValue = 0;
5604      break;
5605    }
5606  }
5607  if( idxNum & 8 ){
5608    pCur->isDesc = 1;
5609    pCur->iValue = pCur->mxValue;
5610    if( pCur->iStep>0 ){
5611      pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5612    }
5613  }else{
5614    pCur->isDesc = 0;
5615    pCur->iValue = pCur->mnValue;
5616  }
5617  pCur->iRowid = 1;
5618  return SQLITE_OK;
5619}
5620
5621/*
5622** SQLite will invoke this method one or more times while planning a query
5623** that uses the generate_series virtual table.  This routine needs to create
5624** a query plan for each invocation and compute an estimated cost for that
5625** plan.
5626**
5627** In this implementation idxNum is used to represent the
5628** query plan.  idxStr is unused.
5629**
5630** The query plan is represented by bits in idxNum:
5631**
5632**  (1)  start = $value  -- constraint exists
5633**  (2)  stop = $value   -- constraint exists
5634**  (4)  step = $value   -- constraint exists
5635**  (8)  output in descending order
5636*/
5637static int seriesBestIndex(
5638  sqlite3_vtab *tabUnused,
5639  sqlite3_index_info *pIdxInfo
5640){
5641  int i, j;              /* Loop over constraints */
5642  int idxNum = 0;        /* The query plan bitmask */
5643  int unusableMask = 0;  /* Mask of unusable constraints */
5644  int nArg = 0;          /* Number of arguments that seriesFilter() expects */
5645  int aIdx[3];           /* Constraints on start, stop, and step */
5646  const struct sqlite3_index_constraint *pConstraint;
5647
5648  /* This implementation assumes that the start, stop, and step columns
5649  ** are the last three columns in the virtual table. */
5650  assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5651  assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5652  (void)tabUnused;
5653  aIdx[0] = aIdx[1] = aIdx[2] = -1;
5654  pConstraint = pIdxInfo->aConstraint;
5655  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5656    int iCol;    /* 0 for start, 1 for stop, 2 for step */
5657    int iMask;   /* bitmask for those column */
5658    if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5659    iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5660    assert( iCol>=0 && iCol<=2 );
5661    iMask = 1 << iCol;
5662    if( pConstraint->usable==0 ){
5663      unusableMask |=  iMask;
5664      continue;
5665    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5666      idxNum |= iMask;
5667      aIdx[iCol] = i;
5668    }
5669  }
5670  for(i=0; i<3; i++){
5671    if( (j = aIdx[i])>=0 ){
5672      pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5673      pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5674    }
5675  }
5676  if( (unusableMask & ~idxNum)!=0 ){
5677    /* The start, stop, and step columns are inputs.  Therefore if there
5678    ** are unusable constraints on any of start, stop, or step then
5679    ** this plan is unusable */
5680    return SQLITE_CONSTRAINT;
5681  }
5682  if( (idxNum & 3)==3 ){
5683    /* Both start= and stop= boundaries are available.  This is the
5684    ** the preferred case */
5685    pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5686    pIdxInfo->estimatedRows = 1000;
5687    if( pIdxInfo->nOrderBy==1 ){
5688      if( pIdxInfo->aOrderBy[0].desc ) idxNum |= 8;
5689      pIdxInfo->orderByConsumed = 1;
5690    }
5691  }else{
5692    /* If either boundary is missing, we have to generate a huge span
5693    ** of numbers.  Make this case very expensive so that the query
5694    ** planner will work hard to avoid it. */
5695    pIdxInfo->estimatedRows = 2147483647;
5696  }
5697  pIdxInfo->idxNum = idxNum;
5698  return SQLITE_OK;
5699}
5700
5701/*
5702** This following structure defines all the methods for the
5703** generate_series virtual table.
5704*/
5705static sqlite3_module seriesModule = {
5706  0,                         /* iVersion */
5707  0,                         /* xCreate */
5708  seriesConnect,             /* xConnect */
5709  seriesBestIndex,           /* xBestIndex */
5710  seriesDisconnect,          /* xDisconnect */
5711  0,                         /* xDestroy */
5712  seriesOpen,                /* xOpen - open a cursor */
5713  seriesClose,               /* xClose - close a cursor */
5714  seriesFilter,              /* xFilter - configure scan constraints */
5715  seriesNext,                /* xNext - advance a cursor */
5716  seriesEof,                 /* xEof - check for end of scan */
5717  seriesColumn,              /* xColumn - read data */
5718  seriesRowid,               /* xRowid - read data */
5719  0,                         /* xUpdate */
5720  0,                         /* xBegin */
5721  0,                         /* xSync */
5722  0,                         /* xCommit */
5723  0,                         /* xRollback */
5724  0,                         /* xFindMethod */
5725  0,                         /* xRename */
5726  0,                         /* xSavepoint */
5727  0,                         /* xRelease */
5728  0,                         /* xRollbackTo */
5729  0                          /* xShadowName */
5730};
5731
5732#endif /* SQLITE_OMIT_VIRTUALTABLE */
5733
5734#ifdef _WIN32
5735
5736#endif
5737int sqlite3_series_init(
5738  sqlite3 *db,
5739  char **pzErrMsg,
5740  const sqlite3_api_routines *pApi
5741){
5742  int rc = SQLITE_OK;
5743  SQLITE_EXTENSION_INIT2(pApi);
5744#ifndef SQLITE_OMIT_VIRTUALTABLE
5745  if( sqlite3_libversion_number()<3008012 ){
5746    *pzErrMsg = sqlite3_mprintf(
5747        "generate_series() requires SQLite 3.8.12 or later");
5748    return SQLITE_ERROR;
5749  }
5750  rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5751#endif
5752  return rc;
5753}
5754
5755/************************* End ../ext/misc/series.c ********************/
5756#ifdef SQLITE_HAVE_ZLIB
5757/************************* Begin ../ext/misc/zipfile.c ******************/
5758/*
5759** 2017-12-26
5760**
5761** The author disclaims copyright to this source code.  In place of
5762** a legal notice, here is a blessing:
5763**
5764**    May you do good and not evil.
5765**    May you find forgiveness for yourself and forgive others.
5766**    May you share freely, never taking more than you give.
5767**
5768******************************************************************************
5769**
5770** This file implements a virtual table for reading and writing ZIP archive
5771** files.
5772**
5773** Usage example:
5774**
5775**     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
5776**
5777** Current limitations:
5778**
5779**    *  No support for encryption
5780**    *  No support for ZIP archives spanning multiple files
5781**    *  No support for zip64 extensions
5782**    *  Only the "inflate/deflate" (zlib) compression method is supported
5783*/
5784/* #include "sqlite3ext.h" */
5785SQLITE_EXTENSION_INIT1
5786#include <stdio.h>
5787#include <string.h>
5788#include <assert.h>
5789
5790#include <zlib.h>
5791
5792#ifndef SQLITE_OMIT_VIRTUALTABLE
5793
5794#ifndef SQLITE_AMALGAMATION
5795
5796/* typedef sqlite3_int64 i64; */
5797/* typedef unsigned char u8; */
5798typedef unsigned short u16;
5799typedef unsigned long u32;
5800#define MIN(a,b) ((a)<(b) ? (a) : (b))
5801
5802#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
5803# define ALWAYS(X)      (1)
5804# define NEVER(X)       (0)
5805#elif !defined(NDEBUG)
5806# define ALWAYS(X)      ((X)?1:(assert(0),0))
5807# define NEVER(X)       ((X)?(assert(0),1):0)
5808#else
5809# define ALWAYS(X)      (X)
5810# define NEVER(X)       (X)
5811#endif
5812
5813#endif   /* SQLITE_AMALGAMATION */
5814
5815/*
5816** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
5817**
5818** In some ways it would be better to obtain these values from system
5819** header files. But, the dependency is undesirable and (a) these
5820** have been stable for decades, (b) the values are part of POSIX and
5821** are also made explicit in [man stat], and (c) are part of the
5822** file format for zip archives.
5823*/
5824#ifndef S_IFDIR
5825# define S_IFDIR 0040000
5826#endif
5827#ifndef S_IFREG
5828# define S_IFREG 0100000
5829#endif
5830#ifndef S_IFLNK
5831# define S_IFLNK 0120000
5832#endif
5833
5834static const char ZIPFILE_SCHEMA[] =
5835  "CREATE TABLE y("
5836    "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
5837    "mode,"              /* 1: POSIX mode for file */
5838    "mtime,"             /* 2: Last modification time (secs since 1970)*/
5839    "sz,"                /* 3: Size of object */
5840    "rawdata,"           /* 4: Raw data */
5841    "data,"              /* 5: Uncompressed data */
5842    "method,"            /* 6: Compression method (integer) */
5843    "z HIDDEN"           /* 7: Name of zip file */
5844  ") WITHOUT ROWID;";
5845
5846#define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
5847#define ZIPFILE_BUFFER_SIZE (64*1024)
5848
5849
5850/*
5851** Magic numbers used to read and write zip files.
5852**
5853** ZIPFILE_NEWENTRY_MADEBY:
5854**   Use this value for the "version-made-by" field in new zip file
5855**   entries. The upper byte indicates "unix", and the lower byte
5856**   indicates that the zip file matches pkzip specification 3.0.
5857**   This is what info-zip seems to do.
5858**
5859** ZIPFILE_NEWENTRY_REQUIRED:
5860**   Value for "version-required-to-extract" field of new entries.
5861**   Version 2.0 is required to support folders and deflate compression.
5862**
5863** ZIPFILE_NEWENTRY_FLAGS:
5864**   Value for "general-purpose-bit-flags" field of new entries. Bit
5865**   11 means "utf-8 filename and comment".
5866**
5867** ZIPFILE_SIGNATURE_CDS:
5868**   First 4 bytes of a valid CDS record.
5869**
5870** ZIPFILE_SIGNATURE_LFH:
5871**   First 4 bytes of a valid LFH record.
5872**
5873** ZIPFILE_SIGNATURE_EOCD
5874**   First 4 bytes of a valid EOCD record.
5875*/
5876#define ZIPFILE_EXTRA_TIMESTAMP   0x5455
5877#define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
5878#define ZIPFILE_NEWENTRY_REQUIRED 20
5879#define ZIPFILE_NEWENTRY_FLAGS    0x800
5880#define ZIPFILE_SIGNATURE_CDS     0x02014b50
5881#define ZIPFILE_SIGNATURE_LFH     0x04034b50
5882#define ZIPFILE_SIGNATURE_EOCD    0x06054b50
5883
5884/*
5885** The sizes of the fixed-size part of each of the three main data
5886** structures in a zip archive.
5887*/
5888#define ZIPFILE_LFH_FIXED_SZ      30
5889#define ZIPFILE_EOCD_FIXED_SZ     22
5890#define ZIPFILE_CDS_FIXED_SZ      46
5891
5892/*
5893*** 4.3.16  End of central directory record:
5894***
5895***   end of central dir signature    4 bytes  (0x06054b50)
5896***   number of this disk             2 bytes
5897***   number of the disk with the
5898***   start of the central directory  2 bytes
5899***   total number of entries in the
5900***   central directory on this disk  2 bytes
5901***   total number of entries in
5902***   the central directory           2 bytes
5903***   size of the central directory   4 bytes
5904***   offset of start of central
5905***   directory with respect to
5906***   the starting disk number        4 bytes
5907***   .ZIP file comment length        2 bytes
5908***   .ZIP file comment       (variable size)
5909*/
5910typedef struct ZipfileEOCD ZipfileEOCD;
5911struct ZipfileEOCD {
5912  u16 iDisk;
5913  u16 iFirstDisk;
5914  u16 nEntry;
5915  u16 nEntryTotal;
5916  u32 nSize;
5917  u32 iOffset;
5918};
5919
5920/*
5921*** 4.3.12  Central directory structure:
5922***
5923*** ...
5924***
5925***   central file header signature   4 bytes  (0x02014b50)
5926***   version made by                 2 bytes
5927***   version needed to extract       2 bytes
5928***   general purpose bit flag        2 bytes
5929***   compression method              2 bytes
5930***   last mod file time              2 bytes
5931***   last mod file date              2 bytes
5932***   crc-32                          4 bytes
5933***   compressed size                 4 bytes
5934***   uncompressed size               4 bytes
5935***   file name length                2 bytes
5936***   extra field length              2 bytes
5937***   file comment length             2 bytes
5938***   disk number start               2 bytes
5939***   internal file attributes        2 bytes
5940***   external file attributes        4 bytes
5941***   relative offset of local header 4 bytes
5942*/
5943typedef struct ZipfileCDS ZipfileCDS;
5944struct ZipfileCDS {
5945  u16 iVersionMadeBy;
5946  u16 iVersionExtract;
5947  u16 flags;
5948  u16 iCompression;
5949  u16 mTime;
5950  u16 mDate;
5951  u32 crc32;
5952  u32 szCompressed;
5953  u32 szUncompressed;
5954  u16 nFile;
5955  u16 nExtra;
5956  u16 nComment;
5957  u16 iDiskStart;
5958  u16 iInternalAttr;
5959  u32 iExternalAttr;
5960  u32 iOffset;
5961  char *zFile;                    /* Filename (sqlite3_malloc()) */
5962};
5963
5964/*
5965*** 4.3.7  Local file header:
5966***
5967***   local file header signature     4 bytes  (0x04034b50)
5968***   version needed to extract       2 bytes
5969***   general purpose bit flag        2 bytes
5970***   compression method              2 bytes
5971***   last mod file time              2 bytes
5972***   last mod file date              2 bytes
5973***   crc-32                          4 bytes
5974***   compressed size                 4 bytes
5975***   uncompressed size               4 bytes
5976***   file name length                2 bytes
5977***   extra field length              2 bytes
5978***
5979*/
5980typedef struct ZipfileLFH ZipfileLFH;
5981struct ZipfileLFH {
5982  u16 iVersionExtract;
5983  u16 flags;
5984  u16 iCompression;
5985  u16 mTime;
5986  u16 mDate;
5987  u32 crc32;
5988  u32 szCompressed;
5989  u32 szUncompressed;
5990  u16 nFile;
5991  u16 nExtra;
5992};
5993
5994typedef struct ZipfileEntry ZipfileEntry;
5995struct ZipfileEntry {
5996  ZipfileCDS cds;            /* Parsed CDS record */
5997  u32 mUnixTime;             /* Modification time, in UNIX format */
5998  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
5999  i64 iDataOff;              /* Offset to data in file (if aData==0) */
6000  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
6001  ZipfileEntry *pNext;       /* Next element in in-memory CDS */
6002};
6003
6004/*
6005** Cursor type for zipfile tables.
6006*/
6007typedef struct ZipfileCsr ZipfileCsr;
6008struct ZipfileCsr {
6009  sqlite3_vtab_cursor base;  /* Base class - must be first */
6010  i64 iId;                   /* Cursor ID */
6011  u8 bEof;                   /* True when at EOF */
6012  u8 bNoop;                  /* If next xNext() call is no-op */
6013
6014  /* Used outside of write transactions */
6015  FILE *pFile;               /* Zip file */
6016  i64 iNextOff;              /* Offset of next record in central directory */
6017  ZipfileEOCD eocd;          /* Parse of central directory record */
6018
6019  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
6020  ZipfileEntry *pCurrent;    /* Current entry */
6021  ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
6022};
6023
6024typedef struct ZipfileTab ZipfileTab;
6025struct ZipfileTab {
6026  sqlite3_vtab base;         /* Base class - must be first */
6027  char *zFile;               /* Zip file this table accesses (may be NULL) */
6028  sqlite3 *db;               /* Host database connection */
6029  u8 *aBuffer;               /* Temporary buffer used for various tasks */
6030
6031  ZipfileCsr *pCsrList;      /* List of cursors */
6032  i64 iNextCsrid;
6033
6034  /* The following are used by write transactions only */
6035  ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6036  ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
6037  FILE *pWriteFd;            /* File handle open on zip archive */
6038  i64 szCurrent;             /* Current size of zip archive */
6039  i64 szOrig;                /* Size of archive at start of transaction */
6040};
6041
6042/*
6043** Set the error message contained in context ctx to the results of
6044** vprintf(zFmt, ...).
6045*/
6046static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6047  char *zMsg = 0;
6048  va_list ap;
6049  va_start(ap, zFmt);
6050  zMsg = sqlite3_vmprintf(zFmt, ap);
6051  sqlite3_result_error(ctx, zMsg, -1);
6052  sqlite3_free(zMsg);
6053  va_end(ap);
6054}
6055
6056/*
6057** If string zIn is quoted, dequote it in place. Otherwise, if the string
6058** is not quoted, do nothing.
6059*/
6060static void zipfileDequote(char *zIn){
6061  char q = zIn[0];
6062  if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6063    int iIn = 1;
6064    int iOut = 0;
6065    if( q=='[' ) q = ']';
6066    while( ALWAYS(zIn[iIn]) ){
6067      char c = zIn[iIn++];
6068      if( c==q && zIn[iIn++]!=q ) break;
6069      zIn[iOut++] = c;
6070    }
6071    zIn[iOut] = '\0';
6072  }
6073}
6074
6075/*
6076** Construct a new ZipfileTab virtual table object.
6077**
6078**   argv[0]   -> module name  ("zipfile")
6079**   argv[1]   -> database name
6080**   argv[2]   -> table name
6081**   argv[...] -> "column name" and other module argument fields.
6082*/
6083static int zipfileConnect(
6084  sqlite3 *db,
6085  void *pAux,
6086  int argc, const char *const*argv,
6087  sqlite3_vtab **ppVtab,
6088  char **pzErr
6089){
6090  int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
6091  int nFile = 0;
6092  const char *zFile = 0;
6093  ZipfileTab *pNew = 0;
6094  int rc;
6095
6096  /* If the table name is not "zipfile", require that the argument be
6097  ** specified. This stops zipfile tables from being created as:
6098  **
6099  **   CREATE VIRTUAL TABLE zzz USING zipfile();
6100  **
6101  ** It does not prevent:
6102  **
6103  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
6104  */
6105  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
6106  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
6107    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
6108    return SQLITE_ERROR;
6109  }
6110
6111  if( argc>3 ){
6112    zFile = argv[3];
6113    nFile = (int)strlen(zFile)+1;
6114  }
6115
6116  rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
6117  if( rc==SQLITE_OK ){
6118    pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
6119    if( pNew==0 ) return SQLITE_NOMEM;
6120    memset(pNew, 0, nByte+nFile);
6121    pNew->db = db;
6122    pNew->aBuffer = (u8*)&pNew[1];
6123    if( zFile ){
6124      pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
6125      memcpy(pNew->zFile, zFile, nFile);
6126      zipfileDequote(pNew->zFile);
6127    }
6128  }
6129  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
6130  *ppVtab = (sqlite3_vtab*)pNew;
6131  return rc;
6132}
6133
6134/*
6135** Free the ZipfileEntry structure indicated by the only argument.
6136*/
6137static void zipfileEntryFree(ZipfileEntry *p){
6138  if( p ){
6139    sqlite3_free(p->cds.zFile);
6140    sqlite3_free(p);
6141  }
6142}
6143
6144/*
6145** Release resources that should be freed at the end of a write
6146** transaction.
6147*/
6148static void zipfileCleanupTransaction(ZipfileTab *pTab){
6149  ZipfileEntry *pEntry;
6150  ZipfileEntry *pNext;
6151
6152  if( pTab->pWriteFd ){
6153    fclose(pTab->pWriteFd);
6154    pTab->pWriteFd = 0;
6155  }
6156  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
6157    pNext = pEntry->pNext;
6158    zipfileEntryFree(pEntry);
6159  }
6160  pTab->pFirstEntry = 0;
6161  pTab->pLastEntry = 0;
6162  pTab->szCurrent = 0;
6163  pTab->szOrig = 0;
6164}
6165
6166/*
6167** This method is the destructor for zipfile vtab objects.
6168*/
6169static int zipfileDisconnect(sqlite3_vtab *pVtab){
6170  zipfileCleanupTransaction((ZipfileTab*)pVtab);
6171  sqlite3_free(pVtab);
6172  return SQLITE_OK;
6173}
6174
6175/*
6176** Constructor for a new ZipfileCsr object.
6177*/
6178static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
6179  ZipfileTab *pTab = (ZipfileTab*)p;
6180  ZipfileCsr *pCsr;
6181  pCsr = sqlite3_malloc(sizeof(*pCsr));
6182  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
6183  if( pCsr==0 ){
6184    return SQLITE_NOMEM;
6185  }
6186  memset(pCsr, 0, sizeof(*pCsr));
6187  pCsr->iId = ++pTab->iNextCsrid;
6188  pCsr->pCsrNext = pTab->pCsrList;
6189  pTab->pCsrList = pCsr;
6190  return SQLITE_OK;
6191}
6192
6193/*
6194** Reset a cursor back to the state it was in when first returned
6195** by zipfileOpen().
6196*/
6197static void zipfileResetCursor(ZipfileCsr *pCsr){
6198  ZipfileEntry *p;
6199  ZipfileEntry *pNext;
6200
6201  pCsr->bEof = 0;
6202  if( pCsr->pFile ){
6203    fclose(pCsr->pFile);
6204    pCsr->pFile = 0;
6205    zipfileEntryFree(pCsr->pCurrent);
6206    pCsr->pCurrent = 0;
6207  }
6208
6209  for(p=pCsr->pFreeEntry; p; p=pNext){
6210    pNext = p->pNext;
6211    zipfileEntryFree(p);
6212  }
6213}
6214
6215/*
6216** Destructor for an ZipfileCsr.
6217*/
6218static int zipfileClose(sqlite3_vtab_cursor *cur){
6219  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6220  ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
6221  ZipfileCsr **pp;
6222  zipfileResetCursor(pCsr);
6223
6224  /* Remove this cursor from the ZipfileTab.pCsrList list. */
6225  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
6226  *pp = pCsr->pCsrNext;
6227
6228  sqlite3_free(pCsr);
6229  return SQLITE_OK;
6230}
6231
6232/*
6233** Set the error message for the virtual table associated with cursor
6234** pCsr to the results of vprintf(zFmt, ...).
6235*/
6236static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
6237  va_list ap;
6238  va_start(ap, zFmt);
6239  sqlite3_free(pTab->base.zErrMsg);
6240  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
6241  va_end(ap);
6242}
6243static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
6244  va_list ap;
6245  va_start(ap, zFmt);
6246  sqlite3_free(pCsr->base.pVtab->zErrMsg);
6247  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
6248  va_end(ap);
6249}
6250
6251/*
6252** Read nRead bytes of data from offset iOff of file pFile into buffer
6253** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
6254** otherwise.
6255**
6256** If an error does occur, output variable (*pzErrmsg) may be set to point
6257** to an English language error message. It is the responsibility of the
6258** caller to eventually free this buffer using
6259** sqlite3_free().
6260*/
6261static int zipfileReadData(
6262  FILE *pFile,                    /* Read from this file */
6263  u8 *aRead,                      /* Read into this buffer */
6264  int nRead,                      /* Number of bytes to read */
6265  i64 iOff,                       /* Offset to read from */
6266  char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
6267){
6268  size_t n;
6269  fseek(pFile, (long)iOff, SEEK_SET);
6270  n = fread(aRead, 1, nRead, pFile);
6271  if( (int)n!=nRead ){
6272    *pzErrmsg = sqlite3_mprintf("error in fread()");
6273    return SQLITE_ERROR;
6274  }
6275  return SQLITE_OK;
6276}
6277
6278static int zipfileAppendData(
6279  ZipfileTab *pTab,
6280  const u8 *aWrite,
6281  int nWrite
6282){
6283  if( nWrite>0 ){
6284    size_t n = nWrite;
6285    fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
6286    n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
6287    if( (int)n!=nWrite ){
6288      pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
6289      return SQLITE_ERROR;
6290    }
6291    pTab->szCurrent += nWrite;
6292  }
6293  return SQLITE_OK;
6294}
6295
6296/*
6297** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
6298*/
6299static u16 zipfileGetU16(const u8 *aBuf){
6300  return (aBuf[1] << 8) + aBuf[0];
6301}
6302
6303/*
6304** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
6305*/
6306static u32 zipfileGetU32(const u8 *aBuf){
6307  return ((u32)(aBuf[3]) << 24)
6308       + ((u32)(aBuf[2]) << 16)
6309       + ((u32)(aBuf[1]) <<  8)
6310       + ((u32)(aBuf[0]) <<  0);
6311}
6312
6313/*
6314** Write a 16-bit little endiate integer into buffer aBuf.
6315*/
6316static void zipfilePutU16(u8 *aBuf, u16 val){
6317  aBuf[0] = val & 0xFF;
6318  aBuf[1] = (val>>8) & 0xFF;
6319}
6320
6321/*
6322** Write a 32-bit little endiate integer into buffer aBuf.
6323*/
6324static void zipfilePutU32(u8 *aBuf, u32 val){
6325  aBuf[0] = val & 0xFF;
6326  aBuf[1] = (val>>8) & 0xFF;
6327  aBuf[2] = (val>>16) & 0xFF;
6328  aBuf[3] = (val>>24) & 0xFF;
6329}
6330
6331#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
6332#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
6333
6334#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
6335#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
6336
6337/*
6338** Magic numbers used to read CDS records.
6339*/
6340#define ZIPFILE_CDS_NFILE_OFF        28
6341#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
6342
6343/*
6344** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
6345** if the record is not well-formed, or SQLITE_OK otherwise.
6346*/
6347static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
6348  u8 *aRead = aBuf;
6349  u32 sig = zipfileRead32(aRead);
6350  int rc = SQLITE_OK;
6351  if( sig!=ZIPFILE_SIGNATURE_CDS ){
6352    rc = SQLITE_ERROR;
6353  }else{
6354    pCDS->iVersionMadeBy = zipfileRead16(aRead);
6355    pCDS->iVersionExtract = zipfileRead16(aRead);
6356    pCDS->flags = zipfileRead16(aRead);
6357    pCDS->iCompression = zipfileRead16(aRead);
6358    pCDS->mTime = zipfileRead16(aRead);
6359    pCDS->mDate = zipfileRead16(aRead);
6360    pCDS->crc32 = zipfileRead32(aRead);
6361    pCDS->szCompressed = zipfileRead32(aRead);
6362    pCDS->szUncompressed = zipfileRead32(aRead);
6363    assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6364    pCDS->nFile = zipfileRead16(aRead);
6365    pCDS->nExtra = zipfileRead16(aRead);
6366    pCDS->nComment = zipfileRead16(aRead);
6367    pCDS->iDiskStart = zipfileRead16(aRead);
6368    pCDS->iInternalAttr = zipfileRead16(aRead);
6369    pCDS->iExternalAttr = zipfileRead32(aRead);
6370    pCDS->iOffset = zipfileRead32(aRead);
6371    assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
6372  }
6373
6374  return rc;
6375}
6376
6377/*
6378** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
6379** if the record is not well-formed, or SQLITE_OK otherwise.
6380*/
6381static int zipfileReadLFH(
6382  u8 *aBuffer,
6383  ZipfileLFH *pLFH
6384){
6385  u8 *aRead = aBuffer;
6386  int rc = SQLITE_OK;
6387
6388  u32 sig = zipfileRead32(aRead);
6389  if( sig!=ZIPFILE_SIGNATURE_LFH ){
6390    rc = SQLITE_ERROR;
6391  }else{
6392    pLFH->iVersionExtract = zipfileRead16(aRead);
6393    pLFH->flags = zipfileRead16(aRead);
6394    pLFH->iCompression = zipfileRead16(aRead);
6395    pLFH->mTime = zipfileRead16(aRead);
6396    pLFH->mDate = zipfileRead16(aRead);
6397    pLFH->crc32 = zipfileRead32(aRead);
6398    pLFH->szCompressed = zipfileRead32(aRead);
6399    pLFH->szUncompressed = zipfileRead32(aRead);
6400    pLFH->nFile = zipfileRead16(aRead);
6401    pLFH->nExtra = zipfileRead16(aRead);
6402  }
6403  return rc;
6404}
6405
6406
6407/*
6408** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
6409** Scan through this buffer to find an "extra-timestamp" field. If one
6410** exists, extract the 32-bit modification-timestamp from it and store
6411** the value in output parameter *pmTime.
6412**
6413** Zero is returned if no extra-timestamp record could be found (and so
6414** *pmTime is left unchanged), or non-zero otherwise.
6415**
6416** The general format of an extra field is:
6417**
6418**   Header ID    2 bytes
6419**   Data Size    2 bytes
6420**   Data         N bytes
6421*/
6422static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
6423  int ret = 0;
6424  u8 *p = aExtra;
6425  u8 *pEnd = &aExtra[nExtra];
6426
6427  while( p<pEnd ){
6428    u16 id = zipfileRead16(p);
6429    u16 nByte = zipfileRead16(p);
6430
6431    switch( id ){
6432      case ZIPFILE_EXTRA_TIMESTAMP: {
6433        u8 b = p[0];
6434        if( b & 0x01 ){     /* 0x01 -> modtime is present */
6435          *pmTime = zipfileGetU32(&p[1]);
6436          ret = 1;
6437        }
6438        break;
6439      }
6440    }
6441
6442    p += nByte;
6443  }
6444  return ret;
6445}
6446
6447/*
6448** Convert the standard MS-DOS timestamp stored in the mTime and mDate
6449** fields of the CDS structure passed as the only argument to a 32-bit
6450** UNIX seconds-since-the-epoch timestamp. Return the result.
6451**
6452** "Standard" MS-DOS time format:
6453**
6454**   File modification time:
6455**     Bits 00-04: seconds divided by 2
6456**     Bits 05-10: minute
6457**     Bits 11-15: hour
6458**   File modification date:
6459**     Bits 00-04: day
6460**     Bits 05-08: month (1-12)
6461**     Bits 09-15: years from 1980
6462**
6463** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
6464*/
6465static u32 zipfileMtime(ZipfileCDS *pCDS){
6466  int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
6467  int M = ((pCDS->mDate >> 5) & 0x0F);
6468  int D = (pCDS->mDate & 0x1F);
6469  int B = -13;
6470
6471  int sec = (pCDS->mTime & 0x1F)*2;
6472  int min = (pCDS->mTime >> 5) & 0x3F;
6473  int hr = (pCDS->mTime >> 11) & 0x1F;
6474  i64 JD;
6475
6476  /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
6477
6478  /* Calculate the JD in seconds for noon on the day in question */
6479  if( M<3 ){
6480    Y = Y-1;
6481    M = M+12;
6482  }
6483  JD = (i64)(24*60*60) * (
6484      (int)(365.25 * (Y + 4716))
6485    + (int)(30.6001 * (M + 1))
6486    + D + B - 1524
6487  );
6488
6489  /* Correct the JD for the time within the day */
6490  JD += (hr-12) * 3600 + min * 60 + sec;
6491
6492  /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
6493  return (u32)(JD - (i64)(24405875) * 24*60*6);
6494}
6495
6496/*
6497** The opposite of zipfileMtime(). This function populates the mTime and
6498** mDate fields of the CDS structure passed as the first argument according
6499** to the UNIX timestamp value passed as the second.
6500*/
6501static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
6502  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
6503  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
6504
6505  int A, B, C, D, E;
6506  int yr, mon, day;
6507  int hr, min, sec;
6508
6509  A = (int)((JD - 1867216.25)/36524.25);
6510  A = (int)(JD + 1 + A - (A/4));
6511  B = A + 1524;
6512  C = (int)((B - 122.1)/365.25);
6513  D = (36525*(C&32767))/100;
6514  E = (int)((B-D)/30.6001);
6515
6516  day = B - D - (int)(30.6001*E);
6517  mon = (E<14 ? E-1 : E-13);
6518  yr = mon>2 ? C-4716 : C-4715;
6519
6520  hr = (mUnixTime % (24*60*60)) / (60*60);
6521  min = (mUnixTime % (60*60)) / 60;
6522  sec = (mUnixTime % 60);
6523
6524  if( yr>=1980 ){
6525    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
6526    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
6527  }else{
6528    pCds->mDate = pCds->mTime = 0;
6529  }
6530
6531  assert( mUnixTime<315507600
6532       || mUnixTime==zipfileMtime(pCds)
6533       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
6534       /* || (mUnixTime % 2) */
6535  );
6536}
6537
6538/*
6539** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
6540** size) containing an entire zip archive image. Or, if aBlob is NULL,
6541** then pFile is a file-handle open on a zip file. In either case, this
6542** function creates a ZipfileEntry object based on the zip archive entry
6543** for which the CDS record is at offset iOff.
6544**
6545** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
6546** the new object. Otherwise, an SQLite error code is returned and the
6547** final value of (*ppEntry) undefined.
6548*/
6549static int zipfileGetEntry(
6550  ZipfileTab *pTab,               /* Store any error message here */
6551  const u8 *aBlob,                /* Pointer to in-memory file image */
6552  int nBlob,                      /* Size of aBlob[] in bytes */
6553  FILE *pFile,                    /* If aBlob==0, read from this file */
6554  i64 iOff,                       /* Offset of CDS record */
6555  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
6556){
6557  u8 *aRead;
6558  char **pzErr = &pTab->base.zErrMsg;
6559  int rc = SQLITE_OK;
6560
6561  if( aBlob==0 ){
6562    aRead = pTab->aBuffer;
6563    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
6564  }else{
6565    aRead = (u8*)&aBlob[iOff];
6566  }
6567
6568  if( rc==SQLITE_OK ){
6569    sqlite3_int64 nAlloc;
6570    ZipfileEntry *pNew;
6571
6572    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
6573    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
6574    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
6575
6576    nAlloc = sizeof(ZipfileEntry) + nExtra;
6577    if( aBlob ){
6578      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
6579    }
6580
6581    pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
6582    if( pNew==0 ){
6583      rc = SQLITE_NOMEM;
6584    }else{
6585      memset(pNew, 0, sizeof(ZipfileEntry));
6586      rc = zipfileReadCDS(aRead, &pNew->cds);
6587      if( rc!=SQLITE_OK ){
6588        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
6589      }else if( aBlob==0 ){
6590        rc = zipfileReadData(
6591            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
6592        );
6593      }else{
6594        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
6595      }
6596    }
6597
6598    if( rc==SQLITE_OK ){
6599      u32 *pt = &pNew->mUnixTime;
6600      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
6601      pNew->aExtra = (u8*)&pNew[1];
6602      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
6603      if( pNew->cds.zFile==0 ){
6604        rc = SQLITE_NOMEM;
6605      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
6606        pNew->mUnixTime = zipfileMtime(&pNew->cds);
6607      }
6608    }
6609
6610    if( rc==SQLITE_OK ){
6611      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
6612      ZipfileLFH lfh;
6613      if( pFile ){
6614        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
6615      }else{
6616        aRead = (u8*)&aBlob[pNew->cds.iOffset];
6617      }
6618
6619      rc = zipfileReadLFH(aRead, &lfh);
6620      if( rc==SQLITE_OK ){
6621        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
6622        pNew->iDataOff += lfh.nFile + lfh.nExtra;
6623        if( aBlob && pNew->cds.szCompressed ){
6624          pNew->aData = &pNew->aExtra[nExtra];
6625          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
6626        }
6627      }else{
6628        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
6629            (int)pNew->cds.iOffset
6630        );
6631      }
6632    }
6633
6634    if( rc!=SQLITE_OK ){
6635      zipfileEntryFree(pNew);
6636    }else{
6637      *ppEntry = pNew;
6638    }
6639  }
6640
6641  return rc;
6642}
6643
6644/*
6645** Advance an ZipfileCsr to its next row of output.
6646*/
6647static int zipfileNext(sqlite3_vtab_cursor *cur){
6648  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6649  int rc = SQLITE_OK;
6650
6651  if( pCsr->pFile ){
6652    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
6653    zipfileEntryFree(pCsr->pCurrent);
6654    pCsr->pCurrent = 0;
6655    if( pCsr->iNextOff>=iEof ){
6656      pCsr->bEof = 1;
6657    }else{
6658      ZipfileEntry *p = 0;
6659      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
6660      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
6661      if( rc==SQLITE_OK ){
6662        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
6663        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
6664      }
6665      pCsr->pCurrent = p;
6666    }
6667  }else{
6668    if( !pCsr->bNoop ){
6669      pCsr->pCurrent = pCsr->pCurrent->pNext;
6670    }
6671    if( pCsr->pCurrent==0 ){
6672      pCsr->bEof = 1;
6673    }
6674  }
6675
6676  pCsr->bNoop = 0;
6677  return rc;
6678}
6679
6680static void zipfileFree(void *p) {
6681  sqlite3_free(p);
6682}
6683
6684/*
6685** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
6686** size is nOut bytes. This function uncompresses the data and sets the
6687** return value in context pCtx to the result (a blob).
6688**
6689** If an error occurs, an error code is left in pCtx instead.
6690*/
6691static void zipfileInflate(
6692  sqlite3_context *pCtx,          /* Store result here */
6693  const u8 *aIn,                  /* Compressed data */
6694  int nIn,                        /* Size of buffer aIn[] in bytes */
6695  int nOut                        /* Expected output size */
6696){
6697  u8 *aRes = sqlite3_malloc(nOut);
6698  if( aRes==0 ){
6699    sqlite3_result_error_nomem(pCtx);
6700  }else{
6701    int err;
6702    z_stream str;
6703    memset(&str, 0, sizeof(str));
6704
6705    str.next_in = (Byte*)aIn;
6706    str.avail_in = nIn;
6707    str.next_out = (Byte*)aRes;
6708    str.avail_out = nOut;
6709
6710    err = inflateInit2(&str, -15);
6711    if( err!=Z_OK ){
6712      zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
6713    }else{
6714      err = inflate(&str, Z_NO_FLUSH);
6715      if( err!=Z_STREAM_END ){
6716        zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
6717      }else{
6718        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
6719        aRes = 0;
6720      }
6721    }
6722    sqlite3_free(aRes);
6723    inflateEnd(&str);
6724  }
6725}
6726
6727/*
6728** Buffer aIn (size nIn bytes) contains uncompressed data. This function
6729** compresses it and sets (*ppOut) to point to a buffer containing the
6730** compressed data. The caller is responsible for eventually calling
6731** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
6732** is set to the size of buffer (*ppOut) in bytes.
6733**
6734** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
6735** code is returned and an error message left in virtual-table handle
6736** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
6737** case.
6738*/
6739static int zipfileDeflate(
6740  const u8 *aIn, int nIn,         /* Input */
6741  u8 **ppOut, int *pnOut,         /* Output */
6742  char **pzErr                    /* OUT: Error message */
6743){
6744  int rc = SQLITE_OK;
6745  sqlite3_int64 nAlloc;
6746  z_stream str;
6747  u8 *aOut;
6748
6749  memset(&str, 0, sizeof(str));
6750  str.next_in = (Bytef*)aIn;
6751  str.avail_in = nIn;
6752  deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
6753
6754  nAlloc = deflateBound(&str, nIn);
6755  aOut = (u8*)sqlite3_malloc64(nAlloc);
6756  if( aOut==0 ){
6757    rc = SQLITE_NOMEM;
6758  }else{
6759    int res;
6760    str.next_out = aOut;
6761    str.avail_out = nAlloc;
6762    res = deflate(&str, Z_FINISH);
6763    if( res==Z_STREAM_END ){
6764      *ppOut = aOut;
6765      *pnOut = (int)str.total_out;
6766    }else{
6767      sqlite3_free(aOut);
6768      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
6769      rc = SQLITE_ERROR;
6770    }
6771    deflateEnd(&str);
6772  }
6773
6774  return rc;
6775}
6776
6777
6778/*
6779** Return values of columns for the row at which the series_cursor
6780** is currently pointing.
6781*/
6782static int zipfileColumn(
6783  sqlite3_vtab_cursor *cur,   /* The cursor */
6784  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
6785  int i                       /* Which column to return */
6786){
6787  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6788  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
6789  int rc = SQLITE_OK;
6790  switch( i ){
6791    case 0:   /* name */
6792      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
6793      break;
6794    case 1:   /* mode */
6795      /* TODO: Whether or not the following is correct surely depends on
6796      ** the platform on which the archive was created.  */
6797      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
6798      break;
6799    case 2: { /* mtime */
6800      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
6801      break;
6802    }
6803    case 3: { /* sz */
6804      if( sqlite3_vtab_nochange(ctx)==0 ){
6805        sqlite3_result_int64(ctx, pCDS->szUncompressed);
6806      }
6807      break;
6808    }
6809    case 4:   /* rawdata */
6810      if( sqlite3_vtab_nochange(ctx) ) break;
6811    case 5: { /* data */
6812      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
6813        int sz = pCDS->szCompressed;
6814        int szFinal = pCDS->szUncompressed;
6815        if( szFinal>0 ){
6816          u8 *aBuf;
6817          u8 *aFree = 0;
6818          if( pCsr->pCurrent->aData ){
6819            aBuf = pCsr->pCurrent->aData;
6820          }else{
6821            aBuf = aFree = sqlite3_malloc64(sz);
6822            if( aBuf==0 ){
6823              rc = SQLITE_NOMEM;
6824            }else{
6825              FILE *pFile = pCsr->pFile;
6826              if( pFile==0 ){
6827                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
6828              }
6829              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
6830                  &pCsr->base.pVtab->zErrMsg
6831              );
6832            }
6833          }
6834          if( rc==SQLITE_OK ){
6835            if( i==5 && pCDS->iCompression ){
6836              zipfileInflate(ctx, aBuf, sz, szFinal);
6837            }else{
6838              sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
6839            }
6840          }
6841          sqlite3_free(aFree);
6842        }else{
6843          /* Figure out if this is a directory or a zero-sized file. Consider
6844          ** it to be a directory either if the mode suggests so, or if
6845          ** the final character in the name is '/'.  */
6846          u32 mode = pCDS->iExternalAttr >> 16;
6847          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
6848            sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
6849          }
6850        }
6851      }
6852      break;
6853    }
6854    case 6:   /* method */
6855      sqlite3_result_int(ctx, pCDS->iCompression);
6856      break;
6857    default:  /* z */
6858      assert( i==7 );
6859      sqlite3_result_int64(ctx, pCsr->iId);
6860      break;
6861  }
6862
6863  return rc;
6864}
6865
6866/*
6867** Return TRUE if the cursor is at EOF.
6868*/
6869static int zipfileEof(sqlite3_vtab_cursor *cur){
6870  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6871  return pCsr->bEof;
6872}
6873
6874/*
6875** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
6876** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
6877** is guaranteed to be a file-handle open on a zip file.
6878**
6879** This function attempts to locate the EOCD record within the zip archive
6880** and populate *pEOCD with the results of decoding it. SQLITE_OK is
6881** returned if successful. Otherwise, an SQLite error code is returned and
6882** an English language error message may be left in virtual-table pTab.
6883*/
6884static int zipfileReadEOCD(
6885  ZipfileTab *pTab,               /* Return errors here */
6886  const u8 *aBlob,                /* Pointer to in-memory file image */
6887  int nBlob,                      /* Size of aBlob[] in bytes */
6888  FILE *pFile,                    /* Read from this file if aBlob==0 */
6889  ZipfileEOCD *pEOCD              /* Object to populate */
6890){
6891  u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
6892  int nRead;                      /* Bytes to read from file */
6893  int rc = SQLITE_OK;
6894
6895  if( aBlob==0 ){
6896    i64 iOff;                     /* Offset to read from */
6897    i64 szFile;                   /* Total size of file in bytes */
6898    fseek(pFile, 0, SEEK_END);
6899    szFile = (i64)ftell(pFile);
6900    if( szFile==0 ){
6901      memset(pEOCD, 0, sizeof(ZipfileEOCD));
6902      return SQLITE_OK;
6903    }
6904    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
6905    iOff = szFile - nRead;
6906    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
6907  }else{
6908    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
6909    aRead = (u8*)&aBlob[nBlob-nRead];
6910  }
6911
6912  if( rc==SQLITE_OK ){
6913    int i;
6914
6915    /* Scan backwards looking for the signature bytes */
6916    for(i=nRead-20; i>=0; i--){
6917      if( aRead[i]==0x50 && aRead[i+1]==0x4b
6918       && aRead[i+2]==0x05 && aRead[i+3]==0x06
6919      ){
6920        break;
6921      }
6922    }
6923    if( i<0 ){
6924      pTab->base.zErrMsg = sqlite3_mprintf(
6925          "cannot find end of central directory record"
6926      );
6927      return SQLITE_ERROR;
6928    }
6929
6930    aRead += i+4;
6931    pEOCD->iDisk = zipfileRead16(aRead);
6932    pEOCD->iFirstDisk = zipfileRead16(aRead);
6933    pEOCD->nEntry = zipfileRead16(aRead);
6934    pEOCD->nEntryTotal = zipfileRead16(aRead);
6935    pEOCD->nSize = zipfileRead32(aRead);
6936    pEOCD->iOffset = zipfileRead32(aRead);
6937  }
6938
6939  return rc;
6940}
6941
6942/*
6943** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
6944** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
6945** to the end of the list. Otherwise, it is added to the list immediately
6946** before pBefore (which is guaranteed to be a part of said list).
6947*/
6948static void zipfileAddEntry(
6949  ZipfileTab *pTab,
6950  ZipfileEntry *pBefore,
6951  ZipfileEntry *pNew
6952){
6953  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
6954  assert( pNew->pNext==0 );
6955  if( pBefore==0 ){
6956    if( pTab->pFirstEntry==0 ){
6957      pTab->pFirstEntry = pTab->pLastEntry = pNew;
6958    }else{
6959      assert( pTab->pLastEntry->pNext==0 );
6960      pTab->pLastEntry->pNext = pNew;
6961      pTab->pLastEntry = pNew;
6962    }
6963  }else{
6964    ZipfileEntry **pp;
6965    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
6966    pNew->pNext = pBefore;
6967    *pp = pNew;
6968  }
6969}
6970
6971static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
6972  ZipfileEOCD eocd;
6973  int rc;
6974  int i;
6975  i64 iOff;
6976
6977  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
6978  iOff = eocd.iOffset;
6979  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
6980    ZipfileEntry *pNew = 0;
6981    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
6982
6983    if( rc==SQLITE_OK ){
6984      zipfileAddEntry(pTab, 0, pNew);
6985      iOff += ZIPFILE_CDS_FIXED_SZ;
6986      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
6987    }
6988  }
6989  return rc;
6990}
6991
6992/*
6993** xFilter callback.
6994*/
6995static int zipfileFilter(
6996  sqlite3_vtab_cursor *cur,
6997  int idxNum, const char *idxStr,
6998  int argc, sqlite3_value **argv
6999){
7000  ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7001  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7002  const char *zFile = 0;          /* Zip file to scan */
7003  int rc = SQLITE_OK;             /* Return Code */
7004  int bInMemory = 0;              /* True for an in-memory zipfile */
7005
7006  zipfileResetCursor(pCsr);
7007
7008  if( pTab->zFile ){
7009    zFile = pTab->zFile;
7010  }else if( idxNum==0 ){
7011    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7012    return SQLITE_ERROR;
7013  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7014    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7015    int nBlob = sqlite3_value_bytes(argv[0]);
7016    assert( pTab->pFirstEntry==0 );
7017    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7018    pCsr->pFreeEntry = pTab->pFirstEntry;
7019    pTab->pFirstEntry = pTab->pLastEntry = 0;
7020    if( rc!=SQLITE_OK ) return rc;
7021    bInMemory = 1;
7022  }else{
7023    zFile = (const char*)sqlite3_value_text(argv[0]);
7024  }
7025
7026  if( 0==pTab->pWriteFd && 0==bInMemory ){
7027    pCsr->pFile = fopen(zFile, "rb");
7028    if( pCsr->pFile==0 ){
7029      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7030      rc = SQLITE_ERROR;
7031    }else{
7032      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7033      if( rc==SQLITE_OK ){
7034        if( pCsr->eocd.nEntry==0 ){
7035          pCsr->bEof = 1;
7036        }else{
7037          pCsr->iNextOff = pCsr->eocd.iOffset;
7038          rc = zipfileNext(cur);
7039        }
7040      }
7041    }
7042  }else{
7043    pCsr->bNoop = 1;
7044    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7045    rc = zipfileNext(cur);
7046  }
7047
7048  return rc;
7049}
7050
7051/*
7052** xBestIndex callback.
7053*/
7054static int zipfileBestIndex(
7055  sqlite3_vtab *tab,
7056  sqlite3_index_info *pIdxInfo
7057){
7058  int i;
7059  int idx = -1;
7060  int unusable = 0;
7061
7062  for(i=0; i<pIdxInfo->nConstraint; i++){
7063    const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7064    if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7065    if( pCons->usable==0 ){
7066      unusable = 1;
7067    }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7068      idx = i;
7069    }
7070  }
7071  pIdxInfo->estimatedCost = 1000.0;
7072  if( idx>=0 ){
7073    pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7074    pIdxInfo->aConstraintUsage[idx].omit = 1;
7075    pIdxInfo->idxNum = 1;
7076  }else if( unusable ){
7077    return SQLITE_CONSTRAINT;
7078  }
7079  return SQLITE_OK;
7080}
7081
7082static ZipfileEntry *zipfileNewEntry(const char *zPath){
7083  ZipfileEntry *pNew;
7084  pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7085  if( pNew ){
7086    memset(pNew, 0, sizeof(ZipfileEntry));
7087    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7088    if( pNew->cds.zFile==0 ){
7089      sqlite3_free(pNew);
7090      pNew = 0;
7091    }
7092  }
7093  return pNew;
7094}
7095
7096static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
7097  ZipfileCDS *pCds = &pEntry->cds;
7098  u8 *a = aBuf;
7099
7100  pCds->nExtra = 9;
7101
7102  /* Write the LFH itself */
7103  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
7104  zipfileWrite16(a, pCds->iVersionExtract);
7105  zipfileWrite16(a, pCds->flags);
7106  zipfileWrite16(a, pCds->iCompression);
7107  zipfileWrite16(a, pCds->mTime);
7108  zipfileWrite16(a, pCds->mDate);
7109  zipfileWrite32(a, pCds->crc32);
7110  zipfileWrite32(a, pCds->szCompressed);
7111  zipfileWrite32(a, pCds->szUncompressed);
7112  zipfileWrite16(a, (u16)pCds->nFile);
7113  zipfileWrite16(a, pCds->nExtra);
7114  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
7115
7116  /* Add the file name */
7117  memcpy(a, pCds->zFile, (int)pCds->nFile);
7118  a += (int)pCds->nFile;
7119
7120  /* The "extra" data */
7121  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7122  zipfileWrite16(a, 5);
7123  *a++ = 0x01;
7124  zipfileWrite32(a, pEntry->mUnixTime);
7125
7126  return a-aBuf;
7127}
7128
7129static int zipfileAppendEntry(
7130  ZipfileTab *pTab,
7131  ZipfileEntry *pEntry,
7132  const u8 *pData,
7133  int nData
7134){
7135  u8 *aBuf = pTab->aBuffer;
7136  int nBuf;
7137  int rc;
7138
7139  nBuf = zipfileSerializeLFH(pEntry, aBuf);
7140  rc = zipfileAppendData(pTab, aBuf, nBuf);
7141  if( rc==SQLITE_OK ){
7142    pEntry->iDataOff = pTab->szCurrent;
7143    rc = zipfileAppendData(pTab, pData, nData);
7144  }
7145
7146  return rc;
7147}
7148
7149static int zipfileGetMode(
7150  sqlite3_value *pVal,
7151  int bIsDir,                     /* If true, default to directory */
7152  u32 *pMode,                     /* OUT: Mode value */
7153  char **pzErr                    /* OUT: Error message */
7154){
7155  const char *z = (const char*)sqlite3_value_text(pVal);
7156  u32 mode = 0;
7157  if( z==0 ){
7158    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
7159  }else if( z[0]>='0' && z[0]<='9' ){
7160    mode = (unsigned int)sqlite3_value_int(pVal);
7161  }else{
7162    const char zTemplate[11] = "-rwxrwxrwx";
7163    int i;
7164    if( strlen(z)!=10 ) goto parse_error;
7165    switch( z[0] ){
7166      case '-': mode |= S_IFREG; break;
7167      case 'd': mode |= S_IFDIR; break;
7168      case 'l': mode |= S_IFLNK; break;
7169      default: goto parse_error;
7170    }
7171    for(i=1; i<10; i++){
7172      if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
7173      else if( z[i]!='-' ) goto parse_error;
7174    }
7175  }
7176  if( ((mode & S_IFDIR)==0)==bIsDir ){
7177    /* The "mode" attribute is a directory, but data has been specified.
7178    ** Or vice-versa - no data but "mode" is a file or symlink.  */
7179    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
7180    return SQLITE_CONSTRAINT;
7181  }
7182  *pMode = mode;
7183  return SQLITE_OK;
7184
7185 parse_error:
7186  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
7187  return SQLITE_ERROR;
7188}
7189
7190/*
7191** Both (const char*) arguments point to nul-terminated strings. Argument
7192** nB is the value of strlen(zB). This function returns 0 if the strings are
7193** identical, ignoring any trailing '/' character in either path.  */
7194static int zipfileComparePath(const char *zA, const char *zB, int nB){
7195  int nA = (int)strlen(zA);
7196  if( nA>0 && zA[nA-1]=='/' ) nA--;
7197  if( nB>0 && zB[nB-1]=='/' ) nB--;
7198  if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
7199  return 1;
7200}
7201
7202static int zipfileBegin(sqlite3_vtab *pVtab){
7203  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7204  int rc = SQLITE_OK;
7205
7206  assert( pTab->pWriteFd==0 );
7207  if( pTab->zFile==0 || pTab->zFile[0]==0 ){
7208    pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
7209    return SQLITE_ERROR;
7210  }
7211
7212  /* Open a write fd on the file. Also load the entire central directory
7213  ** structure into memory. During the transaction any new file data is
7214  ** appended to the archive file, but the central directory is accumulated
7215  ** in main-memory until the transaction is committed.  */
7216  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
7217  if( pTab->pWriteFd==0 ){
7218    pTab->base.zErrMsg = sqlite3_mprintf(
7219        "zipfile: failed to open file %s for writing", pTab->zFile
7220        );
7221    rc = SQLITE_ERROR;
7222  }else{
7223    fseek(pTab->pWriteFd, 0, SEEK_END);
7224    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
7225    rc = zipfileLoadDirectory(pTab, 0, 0);
7226  }
7227
7228  if( rc!=SQLITE_OK ){
7229    zipfileCleanupTransaction(pTab);
7230  }
7231
7232  return rc;
7233}
7234
7235/*
7236** Return the current time as a 32-bit timestamp in UNIX epoch format (like
7237** time(2)).
7238*/
7239static u32 zipfileTime(void){
7240  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
7241  u32 ret;
7242  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
7243    i64 ms;
7244    pVfs->xCurrentTimeInt64(pVfs, &ms);
7245    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
7246  }else{
7247    double day;
7248    pVfs->xCurrentTime(pVfs, &day);
7249    ret = (u32)((day - 2440587.5) * 86400);
7250  }
7251  return ret;
7252}
7253
7254/*
7255** Return a 32-bit timestamp in UNIX epoch format.
7256**
7257** If the value passed as the only argument is either NULL or an SQL NULL,
7258** return the current time. Otherwise, return the value stored in (*pVal)
7259** cast to a 32-bit unsigned integer.
7260*/
7261static u32 zipfileGetTime(sqlite3_value *pVal){
7262  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
7263    return zipfileTime();
7264  }
7265  return (u32)sqlite3_value_int64(pVal);
7266}
7267
7268/*
7269** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
7270** linked list.  Remove it from the list and free the object.
7271*/
7272static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
7273  if( pOld ){
7274    ZipfileEntry **pp;
7275    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
7276    *pp = (*pp)->pNext;
7277    zipfileEntryFree(pOld);
7278  }
7279}
7280
7281/*
7282** xUpdate method.
7283*/
7284static int zipfileUpdate(
7285  sqlite3_vtab *pVtab,
7286  int nVal,
7287  sqlite3_value **apVal,
7288  sqlite_int64 *pRowid
7289){
7290  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7291  int rc = SQLITE_OK;             /* Return Code */
7292  ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
7293
7294  u32 mode = 0;                   /* Mode for new entry */
7295  u32 mTime = 0;                  /* Modification time for new entry */
7296  i64 sz = 0;                     /* Uncompressed size */
7297  const char *zPath = 0;          /* Path for new entry */
7298  int nPath = 0;                  /* strlen(zPath) */
7299  const u8 *pData = 0;            /* Pointer to buffer containing content */
7300  int nData = 0;                  /* Size of pData buffer in bytes */
7301  int iMethod = 0;                /* Compression method for new entry */
7302  u8 *pFree = 0;                  /* Free this */
7303  char *zFree = 0;                /* Also free this */
7304  ZipfileEntry *pOld = 0;
7305  ZipfileEntry *pOld2 = 0;
7306  int bUpdate = 0;                /* True for an update that modifies "name" */
7307  int bIsDir = 0;
7308  u32 iCrc32 = 0;
7309
7310  if( pTab->pWriteFd==0 ){
7311    rc = zipfileBegin(pVtab);
7312    if( rc!=SQLITE_OK ) return rc;
7313  }
7314
7315  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
7316  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
7317    const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
7318    int nDelete = (int)strlen(zDelete);
7319    if( nVal>1 ){
7320      const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
7321      if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
7322        bUpdate = 1;
7323      }
7324    }
7325    for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
7326      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
7327        break;
7328      }
7329      assert( pOld->pNext );
7330    }
7331  }
7332
7333  if( nVal>1 ){
7334    /* Check that "sz" and "rawdata" are both NULL: */
7335    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
7336      zipfileTableErr(pTab, "sz must be NULL");
7337      rc = SQLITE_CONSTRAINT;
7338    }
7339    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
7340      zipfileTableErr(pTab, "rawdata must be NULL");
7341      rc = SQLITE_CONSTRAINT;
7342    }
7343
7344    if( rc==SQLITE_OK ){
7345      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
7346        /* data=NULL. A directory */
7347        bIsDir = 1;
7348      }else{
7349        /* Value specified for "data", and possibly "method". This must be
7350        ** a regular file or a symlink. */
7351        const u8 *aIn = sqlite3_value_blob(apVal[7]);
7352        int nIn = sqlite3_value_bytes(apVal[7]);
7353        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
7354
7355        iMethod = sqlite3_value_int(apVal[8]);
7356        sz = nIn;
7357        pData = aIn;
7358        nData = nIn;
7359        if( iMethod!=0 && iMethod!=8 ){
7360          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
7361          rc = SQLITE_CONSTRAINT;
7362        }else{
7363          if( bAuto || iMethod ){
7364            int nCmp;
7365            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
7366            if( rc==SQLITE_OK ){
7367              if( iMethod || nCmp<nIn ){
7368                iMethod = 8;
7369                pData = pFree;
7370                nData = nCmp;
7371              }
7372            }
7373          }
7374          iCrc32 = crc32(0, aIn, nIn);
7375        }
7376      }
7377    }
7378
7379    if( rc==SQLITE_OK ){
7380      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
7381    }
7382
7383    if( rc==SQLITE_OK ){
7384      zPath = (const char*)sqlite3_value_text(apVal[2]);
7385      if( zPath==0 ) zPath = "";
7386      nPath = (int)strlen(zPath);
7387      mTime = zipfileGetTime(apVal[4]);
7388    }
7389
7390    if( rc==SQLITE_OK && bIsDir ){
7391      /* For a directory, check that the last character in the path is a
7392      ** '/'. This appears to be required for compatibility with info-zip
7393      ** (the unzip command on unix). It does not create directories
7394      ** otherwise.  */
7395      if( nPath<=0 || zPath[nPath-1]!='/' ){
7396        zFree = sqlite3_mprintf("%s/", zPath);
7397        zPath = (const char*)zFree;
7398        if( zFree==0 ){
7399          rc = SQLITE_NOMEM;
7400          nPath = 0;
7401        }else{
7402          nPath = (int)strlen(zPath);
7403        }
7404      }
7405    }
7406
7407    /* Check that we're not inserting a duplicate entry -OR- updating an
7408    ** entry with a path, thereby making it into a duplicate. */
7409    if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
7410      ZipfileEntry *p;
7411      for(p=pTab->pFirstEntry; p; p=p->pNext){
7412        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
7413          switch( sqlite3_vtab_on_conflict(pTab->db) ){
7414            case SQLITE_IGNORE: {
7415              goto zipfile_update_done;
7416            }
7417            case SQLITE_REPLACE: {
7418              pOld2 = p;
7419              break;
7420            }
7421            default: {
7422              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
7423              rc = SQLITE_CONSTRAINT;
7424              break;
7425            }
7426          }
7427          break;
7428        }
7429      }
7430    }
7431
7432    if( rc==SQLITE_OK ){
7433      /* Create the new CDS record. */
7434      pNew = zipfileNewEntry(zPath);
7435      if( pNew==0 ){
7436        rc = SQLITE_NOMEM;
7437      }else{
7438        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7439        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7440        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7441        pNew->cds.iCompression = (u16)iMethod;
7442        zipfileMtimeToDos(&pNew->cds, mTime);
7443        pNew->cds.crc32 = iCrc32;
7444        pNew->cds.szCompressed = nData;
7445        pNew->cds.szUncompressed = (u32)sz;
7446        pNew->cds.iExternalAttr = (mode<<16);
7447        pNew->cds.iOffset = (u32)pTab->szCurrent;
7448        pNew->cds.nFile = (u16)nPath;
7449        pNew->mUnixTime = (u32)mTime;
7450        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
7451        zipfileAddEntry(pTab, pOld, pNew);
7452      }
7453    }
7454  }
7455
7456  if( rc==SQLITE_OK && (pOld || pOld2) ){
7457    ZipfileCsr *pCsr;
7458    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7459      if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
7460        pCsr->pCurrent = pCsr->pCurrent->pNext;
7461        pCsr->bNoop = 1;
7462      }
7463    }
7464
7465    zipfileRemoveEntryFromList(pTab, pOld);
7466    zipfileRemoveEntryFromList(pTab, pOld2);
7467  }
7468
7469zipfile_update_done:
7470  sqlite3_free(pFree);
7471  sqlite3_free(zFree);
7472  return rc;
7473}
7474
7475static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
7476  u8 *a = aBuf;
7477  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
7478  zipfileWrite16(a, p->iDisk);
7479  zipfileWrite16(a, p->iFirstDisk);
7480  zipfileWrite16(a, p->nEntry);
7481  zipfileWrite16(a, p->nEntryTotal);
7482  zipfileWrite32(a, p->nSize);
7483  zipfileWrite32(a, p->iOffset);
7484  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
7485
7486  return a-aBuf;
7487}
7488
7489static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
7490  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
7491  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
7492  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
7493}
7494
7495/*
7496** Serialize the CDS structure into buffer aBuf[]. Return the number
7497** of bytes written.
7498*/
7499static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
7500  u8 *a = aBuf;
7501  ZipfileCDS *pCDS = &pEntry->cds;
7502
7503  if( pEntry->aExtra==0 ){
7504    pCDS->nExtra = 9;
7505  }
7506
7507  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
7508  zipfileWrite16(a, pCDS->iVersionMadeBy);
7509  zipfileWrite16(a, pCDS->iVersionExtract);
7510  zipfileWrite16(a, pCDS->flags);
7511  zipfileWrite16(a, pCDS->iCompression);
7512  zipfileWrite16(a, pCDS->mTime);
7513  zipfileWrite16(a, pCDS->mDate);
7514  zipfileWrite32(a, pCDS->crc32);
7515  zipfileWrite32(a, pCDS->szCompressed);
7516  zipfileWrite32(a, pCDS->szUncompressed);
7517  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7518  zipfileWrite16(a, pCDS->nFile);
7519  zipfileWrite16(a, pCDS->nExtra);
7520  zipfileWrite16(a, pCDS->nComment);
7521  zipfileWrite16(a, pCDS->iDiskStart);
7522  zipfileWrite16(a, pCDS->iInternalAttr);
7523  zipfileWrite32(a, pCDS->iExternalAttr);
7524  zipfileWrite32(a, pCDS->iOffset);
7525
7526  memcpy(a, pCDS->zFile, pCDS->nFile);
7527  a += pCDS->nFile;
7528
7529  if( pEntry->aExtra ){
7530    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
7531    memcpy(a, pEntry->aExtra, n);
7532    a += n;
7533  }else{
7534    assert( pCDS->nExtra==9 );
7535    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7536    zipfileWrite16(a, 5);
7537    *a++ = 0x01;
7538    zipfileWrite32(a, pEntry->mUnixTime);
7539  }
7540
7541  return a-aBuf;
7542}
7543
7544static int zipfileCommit(sqlite3_vtab *pVtab){
7545  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7546  int rc = SQLITE_OK;
7547  if( pTab->pWriteFd ){
7548    i64 iOffset = pTab->szCurrent;
7549    ZipfileEntry *p;
7550    ZipfileEOCD eocd;
7551    int nEntry = 0;
7552
7553    /* Write out all entries */
7554    for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
7555      int n = zipfileSerializeCDS(p, pTab->aBuffer);
7556      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
7557      nEntry++;
7558    }
7559
7560    /* Write out the EOCD record */
7561    eocd.iDisk = 0;
7562    eocd.iFirstDisk = 0;
7563    eocd.nEntry = (u16)nEntry;
7564    eocd.nEntryTotal = (u16)nEntry;
7565    eocd.nSize = (u32)(pTab->szCurrent - iOffset);
7566    eocd.iOffset = (u32)iOffset;
7567    rc = zipfileAppendEOCD(pTab, &eocd);
7568
7569    zipfileCleanupTransaction(pTab);
7570  }
7571  return rc;
7572}
7573
7574static int zipfileRollback(sqlite3_vtab *pVtab){
7575  return zipfileCommit(pVtab);
7576}
7577
7578static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
7579  ZipfileCsr *pCsr;
7580  for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7581    if( iId==pCsr->iId ) break;
7582  }
7583  return pCsr;
7584}
7585
7586static void zipfileFunctionCds(
7587  sqlite3_context *context,
7588  int argc,
7589  sqlite3_value **argv
7590){
7591  ZipfileCsr *pCsr;
7592  ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
7593  assert( argc>0 );
7594
7595  pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
7596  if( pCsr ){
7597    ZipfileCDS *p = &pCsr->pCurrent->cds;
7598    char *zRes = sqlite3_mprintf("{"
7599        "\"version-made-by\" : %u, "
7600        "\"version-to-extract\" : %u, "
7601        "\"flags\" : %u, "
7602        "\"compression\" : %u, "
7603        "\"time\" : %u, "
7604        "\"date\" : %u, "
7605        "\"crc32\" : %u, "
7606        "\"compressed-size\" : %u, "
7607        "\"uncompressed-size\" : %u, "
7608        "\"file-name-length\" : %u, "
7609        "\"extra-field-length\" : %u, "
7610        "\"file-comment-length\" : %u, "
7611        "\"disk-number-start\" : %u, "
7612        "\"internal-attr\" : %u, "
7613        "\"external-attr\" : %u, "
7614        "\"offset\" : %u }",
7615        (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
7616        (u32)p->flags, (u32)p->iCompression,
7617        (u32)p->mTime, (u32)p->mDate,
7618        (u32)p->crc32, (u32)p->szCompressed,
7619        (u32)p->szUncompressed, (u32)p->nFile,
7620        (u32)p->nExtra, (u32)p->nComment,
7621        (u32)p->iDiskStart, (u32)p->iInternalAttr,
7622        (u32)p->iExternalAttr, (u32)p->iOffset
7623    );
7624
7625    if( zRes==0 ){
7626      sqlite3_result_error_nomem(context);
7627    }else{
7628      sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
7629      sqlite3_free(zRes);
7630    }
7631  }
7632}
7633
7634/*
7635** xFindFunction method.
7636*/
7637static int zipfileFindFunction(
7638  sqlite3_vtab *pVtab,            /* Virtual table handle */
7639  int nArg,                       /* Number of SQL function arguments */
7640  const char *zName,              /* Name of SQL function */
7641  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
7642  void **ppArg                    /* OUT: User data for *pxFunc */
7643){
7644  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
7645    *pxFunc = zipfileFunctionCds;
7646    *ppArg = (void*)pVtab;
7647    return 1;
7648  }
7649  return 0;
7650}
7651
7652typedef struct ZipfileBuffer ZipfileBuffer;
7653struct ZipfileBuffer {
7654  u8 *a;                          /* Pointer to buffer */
7655  int n;                          /* Size of buffer in bytes */
7656  int nAlloc;                     /* Byte allocated at a[] */
7657};
7658
7659typedef struct ZipfileCtx ZipfileCtx;
7660struct ZipfileCtx {
7661  int nEntry;
7662  ZipfileBuffer body;
7663  ZipfileBuffer cds;
7664};
7665
7666static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
7667  if( pBuf->n+nByte>pBuf->nAlloc ){
7668    u8 *aNew;
7669    sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
7670    int nReq = pBuf->n + nByte;
7671
7672    while( nNew<nReq ) nNew = nNew*2;
7673    aNew = sqlite3_realloc64(pBuf->a, nNew);
7674    if( aNew==0 ) return SQLITE_NOMEM;
7675    pBuf->a = aNew;
7676    pBuf->nAlloc = (int)nNew;
7677  }
7678  return SQLITE_OK;
7679}
7680
7681/*
7682** xStep() callback for the zipfile() aggregate. This can be called in
7683** any of the following ways:
7684**
7685**   SELECT zipfile(name,data) ...
7686**   SELECT zipfile(name,mode,mtime,data) ...
7687**   SELECT zipfile(name,mode,mtime,data,method) ...
7688*/
7689void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
7690  ZipfileCtx *p;                  /* Aggregate function context */
7691  ZipfileEntry e;                 /* New entry to add to zip archive */
7692
7693  sqlite3_value *pName = 0;
7694  sqlite3_value *pMode = 0;
7695  sqlite3_value *pMtime = 0;
7696  sqlite3_value *pData = 0;
7697  sqlite3_value *pMethod = 0;
7698
7699  int bIsDir = 0;
7700  u32 mode;
7701  int rc = SQLITE_OK;
7702  char *zErr = 0;
7703
7704  int iMethod = -1;               /* Compression method to use (0 or 8) */
7705
7706  const u8 *aData = 0;            /* Possibly compressed data for new entry */
7707  int nData = 0;                  /* Size of aData[] in bytes */
7708  int szUncompressed = 0;         /* Size of data before compression */
7709  u8 *aFree = 0;                  /* Free this before returning */
7710  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
7711
7712  char *zName = 0;                /* Path (name) of new entry */
7713  int nName = 0;                  /* Size of zName in bytes */
7714  char *zFree = 0;                /* Free this before returning */
7715  int nByte;
7716
7717  memset(&e, 0, sizeof(e));
7718  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7719  if( p==0 ) return;
7720
7721  /* Martial the arguments into stack variables */
7722  if( nVal!=2 && nVal!=4 && nVal!=5 ){
7723    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
7724    rc = SQLITE_ERROR;
7725    goto zipfile_step_out;
7726  }
7727  pName = apVal[0];
7728  if( nVal==2 ){
7729    pData = apVal[1];
7730  }else{
7731    pMode = apVal[1];
7732    pMtime = apVal[2];
7733    pData = apVal[3];
7734    if( nVal==5 ){
7735      pMethod = apVal[4];
7736    }
7737  }
7738
7739  /* Check that the 'name' parameter looks ok. */
7740  zName = (char*)sqlite3_value_text(pName);
7741  nName = sqlite3_value_bytes(pName);
7742  if( zName==0 ){
7743    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
7744    rc = SQLITE_ERROR;
7745    goto zipfile_step_out;
7746  }
7747
7748  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
7749  ** deflate compression) or NULL (choose automatically).  */
7750  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
7751    iMethod = (int)sqlite3_value_int64(pMethod);
7752    if( iMethod!=0 && iMethod!=8 ){
7753      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
7754      rc = SQLITE_ERROR;
7755      goto zipfile_step_out;
7756    }
7757  }
7758
7759  /* Now inspect the data. If this is NULL, then the new entry must be a
7760  ** directory.  Otherwise, figure out whether or not the data should
7761  ** be deflated or simply stored in the zip archive. */
7762  if( sqlite3_value_type(pData)==SQLITE_NULL ){
7763    bIsDir = 1;
7764    iMethod = 0;
7765  }else{
7766    aData = sqlite3_value_blob(pData);
7767    szUncompressed = nData = sqlite3_value_bytes(pData);
7768    iCrc32 = crc32(0, aData, nData);
7769    if( iMethod<0 || iMethod==8 ){
7770      int nOut = 0;
7771      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
7772      if( rc!=SQLITE_OK ){
7773        goto zipfile_step_out;
7774      }
7775      if( iMethod==8 || nOut<nData ){
7776        aData = aFree;
7777        nData = nOut;
7778        iMethod = 8;
7779      }else{
7780        iMethod = 0;
7781      }
7782    }
7783  }
7784
7785  /* Decode the "mode" argument. */
7786  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
7787  if( rc ) goto zipfile_step_out;
7788
7789  /* Decode the "mtime" argument. */
7790  e.mUnixTime = zipfileGetTime(pMtime);
7791
7792  /* If this is a directory entry, ensure that there is exactly one '/'
7793  ** at the end of the path. Or, if this is not a directory and the path
7794  ** ends in '/' it is an error. */
7795  if( bIsDir==0 ){
7796    if( nName>0 && zName[nName-1]=='/' ){
7797      zErr = sqlite3_mprintf("non-directory name must not end with /");
7798      rc = SQLITE_ERROR;
7799      goto zipfile_step_out;
7800    }
7801  }else{
7802    if( nName==0 || zName[nName-1]!='/' ){
7803      zName = zFree = sqlite3_mprintf("%s/", zName);
7804      if( zName==0 ){
7805        rc = SQLITE_NOMEM;
7806        goto zipfile_step_out;
7807      }
7808      nName = (int)strlen(zName);
7809    }else{
7810      while( nName>1 && zName[nName-2]=='/' ) nName--;
7811    }
7812  }
7813
7814  /* Assemble the ZipfileEntry object for the new zip archive entry */
7815  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7816  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7817  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7818  e.cds.iCompression = (u16)iMethod;
7819  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
7820  e.cds.crc32 = iCrc32;
7821  e.cds.szCompressed = nData;
7822  e.cds.szUncompressed = szUncompressed;
7823  e.cds.iExternalAttr = (mode<<16);
7824  e.cds.iOffset = p->body.n;
7825  e.cds.nFile = (u16)nName;
7826  e.cds.zFile = zName;
7827
7828  /* Append the LFH to the body of the new archive */
7829  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
7830  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
7831  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
7832
7833  /* Append the data to the body of the new archive */
7834  if( nData>0 ){
7835    if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
7836    memcpy(&p->body.a[p->body.n], aData, nData);
7837    p->body.n += nData;
7838  }
7839
7840  /* Append the CDS record to the directory of the new archive */
7841  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
7842  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
7843  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
7844
7845  /* Increment the count of entries in the archive */
7846  p->nEntry++;
7847
7848 zipfile_step_out:
7849  sqlite3_free(aFree);
7850  sqlite3_free(zFree);
7851  if( rc ){
7852    if( zErr ){
7853      sqlite3_result_error(pCtx, zErr, -1);
7854    }else{
7855      sqlite3_result_error_code(pCtx, rc);
7856    }
7857  }
7858  sqlite3_free(zErr);
7859}
7860
7861/*
7862** xFinalize() callback for zipfile aggregate function.
7863*/
7864void zipfileFinal(sqlite3_context *pCtx){
7865  ZipfileCtx *p;
7866  ZipfileEOCD eocd;
7867  sqlite3_int64 nZip;
7868  u8 *aZip;
7869
7870  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7871  if( p==0 ) return;
7872  if( p->nEntry>0 ){
7873    memset(&eocd, 0, sizeof(eocd));
7874    eocd.nEntry = (u16)p->nEntry;
7875    eocd.nEntryTotal = (u16)p->nEntry;
7876    eocd.nSize = p->cds.n;
7877    eocd.iOffset = p->body.n;
7878
7879    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
7880    aZip = (u8*)sqlite3_malloc64(nZip);
7881    if( aZip==0 ){
7882      sqlite3_result_error_nomem(pCtx);
7883    }else{
7884      memcpy(aZip, p->body.a, p->body.n);
7885      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
7886      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
7887      sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
7888    }
7889  }
7890
7891  sqlite3_free(p->body.a);
7892  sqlite3_free(p->cds.a);
7893}
7894
7895
7896/*
7897** Register the "zipfile" virtual table.
7898*/
7899static int zipfileRegister(sqlite3 *db){
7900  static sqlite3_module zipfileModule = {
7901    1,                         /* iVersion */
7902    zipfileConnect,            /* xCreate */
7903    zipfileConnect,            /* xConnect */
7904    zipfileBestIndex,          /* xBestIndex */
7905    zipfileDisconnect,         /* xDisconnect */
7906    zipfileDisconnect,         /* xDestroy */
7907    zipfileOpen,               /* xOpen - open a cursor */
7908    zipfileClose,              /* xClose - close a cursor */
7909    zipfileFilter,             /* xFilter - configure scan constraints */
7910    zipfileNext,               /* xNext - advance a cursor */
7911    zipfileEof,                /* xEof - check for end of scan */
7912    zipfileColumn,             /* xColumn - read data */
7913    0,                         /* xRowid - read data */
7914    zipfileUpdate,             /* xUpdate */
7915    zipfileBegin,              /* xBegin */
7916    0,                         /* xSync */
7917    zipfileCommit,             /* xCommit */
7918    zipfileRollback,           /* xRollback */
7919    zipfileFindFunction,       /* xFindMethod */
7920    0,                         /* xRename */
7921  };
7922
7923  int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
7924  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
7925  if( rc==SQLITE_OK ){
7926    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
7927        zipfileStep, zipfileFinal
7928    );
7929  }
7930  return rc;
7931}
7932#else         /* SQLITE_OMIT_VIRTUALTABLE */
7933# define zipfileRegister(x) SQLITE_OK
7934#endif
7935
7936#ifdef _WIN32
7937
7938#endif
7939int sqlite3_zipfile_init(
7940  sqlite3 *db,
7941  char **pzErrMsg,
7942  const sqlite3_api_routines *pApi
7943){
7944  SQLITE_EXTENSION_INIT2(pApi);
7945  (void)pzErrMsg;  /* Unused parameter */
7946  return zipfileRegister(db);
7947}
7948
7949/************************* End ../ext/misc/zipfile.c ********************/
7950/************************* Begin ../ext/misc/sqlar.c ******************/
7951/*
7952** 2017-12-17
7953**
7954** The author disclaims copyright to this source code.  In place of
7955** a legal notice, here is a blessing:
7956**
7957**    May you do good and not evil.
7958**    May you find forgiveness for yourself and forgive others.
7959**    May you share freely, never taking more than you give.
7960**
7961******************************************************************************
7962**
7963** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
7964** for working with sqlar archives and used by the shell tool's built-in
7965** sqlar support.
7966*/
7967/* #include "sqlite3ext.h" */
7968SQLITE_EXTENSION_INIT1
7969#include <zlib.h>
7970#include <assert.h>
7971
7972/*
7973** Implementation of the "sqlar_compress(X)" SQL function.
7974**
7975** If the type of X is SQLITE_BLOB, and compressing that blob using
7976** zlib utility function compress() yields a smaller blob, return the
7977** compressed blob. Otherwise, return a copy of X.
7978**
7979** SQLar uses the "zlib format" for compressed content.  The zlib format
7980** contains a two-byte identification header and a four-byte checksum at
7981** the end.  This is different from ZIP which uses the raw deflate format.
7982**
7983** Future enhancements to SQLar might add support for new compression formats.
7984** If so, those new formats will be identified by alternative headers in the
7985** compressed data.
7986*/
7987static void sqlarCompressFunc(
7988  sqlite3_context *context,
7989  int argc,
7990  sqlite3_value **argv
7991){
7992  assert( argc==1 );
7993  if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7994    const Bytef *pData = sqlite3_value_blob(argv[0]);
7995    uLong nData = sqlite3_value_bytes(argv[0]);
7996    uLongf nOut = compressBound(nData);
7997    Bytef *pOut;
7998
7999    pOut = (Bytef*)sqlite3_malloc(nOut);
8000    if( pOut==0 ){
8001      sqlite3_result_error_nomem(context);
8002      return;
8003    }else{
8004      if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8005        sqlite3_result_error(context, "error in compress()", -1);
8006      }else if( nOut<nData ){
8007        sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8008      }else{
8009        sqlite3_result_value(context, argv[0]);
8010      }
8011      sqlite3_free(pOut);
8012    }
8013  }else{
8014    sqlite3_result_value(context, argv[0]);
8015  }
8016}
8017
8018/*
8019** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8020**
8021** Parameter SZ is interpreted as an integer. If it is less than or
8022** equal to zero, then this function returns a copy of X. Or, if
8023** SZ is equal to the size of X when interpreted as a blob, also
8024** return a copy of X. Otherwise, decompress blob X using zlib
8025** utility function uncompress() and return the results (another
8026** blob).
8027*/
8028static void sqlarUncompressFunc(
8029  sqlite3_context *context,
8030  int argc,
8031  sqlite3_value **argv
8032){
8033  uLong nData;
8034  uLongf sz;
8035
8036  assert( argc==2 );
8037  sz = sqlite3_value_int(argv[1]);
8038
8039  if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8040    sqlite3_result_value(context, argv[0]);
8041  }else{
8042    const Bytef *pData= sqlite3_value_blob(argv[0]);
8043    Bytef *pOut = sqlite3_malloc(sz);
8044    if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8045      sqlite3_result_error(context, "error in uncompress()", -1);
8046    }else{
8047      sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8048    }
8049    sqlite3_free(pOut);
8050  }
8051}
8052
8053
8054#ifdef _WIN32
8055
8056#endif
8057int sqlite3_sqlar_init(
8058  sqlite3 *db,
8059  char **pzErrMsg,
8060  const sqlite3_api_routines *pApi
8061){
8062  int rc = SQLITE_OK;
8063  SQLITE_EXTENSION_INIT2(pApi);
8064  (void)pzErrMsg;  /* Unused parameter */
8065  rc = sqlite3_create_function(db, "sqlar_compress", 1,
8066                               SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8067                               sqlarCompressFunc, 0, 0);
8068  if( rc==SQLITE_OK ){
8069    rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8070                                 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8071                                 sqlarUncompressFunc, 0, 0);
8072  }
8073  return rc;
8074}
8075
8076/************************* End ../ext/misc/sqlar.c ********************/
8077#endif
8078/************************* Begin ../ext/expert/sqlite3expert.h ******************/
8079/*
8080** 2017 April 07
8081**
8082** The author disclaims copyright to this source code.  In place of
8083** a legal notice, here is a blessing:
8084**
8085**    May you do good and not evil.
8086**    May you find forgiveness for yourself and forgive others.
8087**    May you share freely, never taking more than you give.
8088**
8089*************************************************************************
8090*/
8091#if !defined(SQLITEEXPERT_H)
8092#define SQLITEEXPERT_H 1
8093/* #include "sqlite3.h" */
8094
8095typedef struct sqlite3expert sqlite3expert;
8096
8097/*
8098** Create a new sqlite3expert object.
8099**
8100** If successful, a pointer to the new object is returned and (*pzErr) set
8101** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
8102** an English-language error message. In this case it is the responsibility
8103** of the caller to eventually free the error message buffer using
8104** sqlite3_free().
8105*/
8106sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
8107
8108/*
8109** Configure an sqlite3expert object.
8110**
8111** EXPERT_CONFIG_SAMPLE:
8112**   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
8113**   each candidate index. This involves scanning and sorting the entire
8114**   contents of each user database table once for each candidate index
8115**   associated with the table. For large databases, this can be
8116**   prohibitively slow. This option allows the sqlite3expert object to
8117**   be configured so that sqlite_stat1 data is instead generated based on a
8118**   subset of each table, or so that no sqlite_stat1 data is used at all.
8119**
8120**   A single integer argument is passed to this option. If the value is less
8121**   than or equal to zero, then no sqlite_stat1 data is generated or used by
8122**   the analysis - indexes are recommended based on the database schema only.
8123**   Or, if the value is 100 or greater, complete sqlite_stat1 data is
8124**   generated for each candidate index (this is the default). Finally, if the
8125**   value falls between 0 and 100, then it represents the percentage of user
8126**   table rows that should be considered when generating sqlite_stat1 data.
8127**
8128**   Examples:
8129**
8130**     // Do not generate any sqlite_stat1 data
8131**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
8132**
8133**     // Generate sqlite_stat1 data based on 10% of the rows in each table.
8134**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
8135*/
8136int sqlite3_expert_config(sqlite3expert *p, int op, ...);
8137
8138#define EXPERT_CONFIG_SAMPLE 1    /* int */
8139
8140/*
8141** Specify zero or more SQL statements to be included in the analysis.
8142**
8143** Buffer zSql must contain zero or more complete SQL statements. This
8144** function parses all statements contained in the buffer and adds them
8145** to the internal list of statements to analyze. If successful, SQLITE_OK
8146** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
8147** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
8148** may be set to point to an English language error message. In this case
8149** the caller is responsible for eventually freeing the error message buffer
8150** using sqlite3_free().
8151**
8152** If an error does occur while processing one of the statements in the
8153** buffer passed as the second argument, none of the statements in the
8154** buffer are added to the analysis.
8155**
8156** This function must be called before sqlite3_expert_analyze(). If a call
8157** to this function is made on an sqlite3expert object that has already
8158** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
8159** immediately and no statements are added to the analysis.
8160*/
8161int sqlite3_expert_sql(
8162  sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
8163  const char *zSql,               /* SQL statement(s) to add */
8164  char **pzErr                    /* OUT: Error message (if any) */
8165);
8166
8167
8168/*
8169** This function is called after the sqlite3expert object has been configured
8170** with all SQL statements using sqlite3_expert_sql() to actually perform
8171** the analysis. Once this function has been called, it is not possible to
8172** add further SQL statements to the analysis.
8173**
8174** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
8175** an error occurs, an SQLite error code is returned and (*pzErr) set to
8176** point to a buffer containing an English language error message. In this
8177** case it is the responsibility of the caller to eventually free the buffer
8178** using sqlite3_free().
8179**
8180** If an error does occur within this function, the sqlite3expert object
8181** is no longer useful for any purpose. At that point it is no longer
8182** possible to add further SQL statements to the object or to re-attempt
8183** the analysis. The sqlite3expert object must still be freed using a call
8184** sqlite3_expert_destroy().
8185*/
8186int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
8187
8188/*
8189** Return the total number of statements loaded using sqlite3_expert_sql().
8190** The total number of SQL statements may be different from the total number
8191** to calls to sqlite3_expert_sql().
8192*/
8193int sqlite3_expert_count(sqlite3expert*);
8194
8195/*
8196** Return a component of the report.
8197**
8198** This function is called after sqlite3_expert_analyze() to extract the
8199** results of the analysis. Each call to this function returns either a
8200** NULL pointer or a pointer to a buffer containing a nul-terminated string.
8201** The value passed as the third argument must be one of the EXPERT_REPORT_*
8202** #define constants defined below.
8203**
8204** For some EXPERT_REPORT_* parameters, the buffer returned contains
8205** information relating to a specific SQL statement. In these cases that
8206** SQL statement is identified by the value passed as the second argument.
8207** SQL statements are numbered from 0 in the order in which they are parsed.
8208** If an out-of-range value (less than zero or equal to or greater than the
8209** value returned by sqlite3_expert_count()) is passed as the second argument
8210** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
8211**
8212** EXPERT_REPORT_SQL:
8213**   Return the text of SQL statement iStmt.
8214**
8215** EXPERT_REPORT_INDEXES:
8216**   Return a buffer containing the CREATE INDEX statements for all recommended
8217**   indexes for statement iStmt. If there are no new recommeded indexes, NULL
8218**   is returned.
8219**
8220** EXPERT_REPORT_PLAN:
8221**   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
8222**   iStmt after the proposed indexes have been added to the database schema.
8223**
8224** EXPERT_REPORT_CANDIDATES:
8225**   Return a pointer to a buffer containing the CREATE INDEX statements
8226**   for all indexes that were tested (for all SQL statements). The iStmt
8227**   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
8228*/
8229const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
8230
8231/*
8232** Values for the third argument passed to sqlite3_expert_report().
8233*/
8234#define EXPERT_REPORT_SQL        1
8235#define EXPERT_REPORT_INDEXES    2
8236#define EXPERT_REPORT_PLAN       3
8237#define EXPERT_REPORT_CANDIDATES 4
8238
8239/*
8240** Free an (sqlite3expert*) handle and all associated resources. There
8241** should be one call to this function for each successful call to
8242** sqlite3-expert_new().
8243*/
8244void sqlite3_expert_destroy(sqlite3expert*);
8245
8246#endif  /* !defined(SQLITEEXPERT_H) */
8247
8248/************************* End ../ext/expert/sqlite3expert.h ********************/
8249/************************* Begin ../ext/expert/sqlite3expert.c ******************/
8250/*
8251** 2017 April 09
8252**
8253** The author disclaims copyright to this source code.  In place of
8254** a legal notice, here is a blessing:
8255**
8256**    May you do good and not evil.
8257**    May you find forgiveness for yourself and forgive others.
8258**    May you share freely, never taking more than you give.
8259**
8260*************************************************************************
8261*/
8262/* #include "sqlite3expert.h" */
8263#include <assert.h>
8264#include <string.h>
8265#include <stdio.h>
8266
8267#ifndef SQLITE_OMIT_VIRTUALTABLE
8268
8269/* typedef sqlite3_int64 i64; */
8270/* typedef sqlite3_uint64 u64; */
8271
8272typedef struct IdxColumn IdxColumn;
8273typedef struct IdxConstraint IdxConstraint;
8274typedef struct IdxScan IdxScan;
8275typedef struct IdxStatement IdxStatement;
8276typedef struct IdxTable IdxTable;
8277typedef struct IdxWrite IdxWrite;
8278
8279#define STRLEN  (int)strlen
8280
8281/*
8282** A temp table name that we assume no user database will actually use.
8283** If this assumption proves incorrect triggers on the table with the
8284** conflicting name will be ignored.
8285*/
8286#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
8287
8288/*
8289** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
8290** any other type of single-ended range constraint on a column).
8291**
8292** pLink:
8293**   Used to temporarily link IdxConstraint objects into lists while
8294**   creating candidate indexes.
8295*/
8296struct IdxConstraint {
8297  char *zColl;                    /* Collation sequence */
8298  int bRange;                     /* True for range, false for eq */
8299  int iCol;                       /* Constrained table column */
8300  int bFlag;                      /* Used by idxFindCompatible() */
8301  int bDesc;                      /* True if ORDER BY <expr> DESC */
8302  IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
8303  IdxConstraint *pLink;           /* See above */
8304};
8305
8306/*
8307** A single scan of a single table.
8308*/
8309struct IdxScan {
8310  IdxTable *pTab;                 /* Associated table object */
8311  int iDb;                        /* Database containing table zTable */
8312  i64 covering;                   /* Mask of columns required for cov. index */
8313  IdxConstraint *pOrder;          /* ORDER BY columns */
8314  IdxConstraint *pEq;             /* List of == constraints */
8315  IdxConstraint *pRange;          /* List of < constraints */
8316  IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
8317};
8318
8319/*
8320** Information regarding a single database table. Extracted from
8321** "PRAGMA table_info" by function idxGetTableInfo().
8322*/
8323struct IdxColumn {
8324  char *zName;
8325  char *zColl;
8326  int iPk;
8327};
8328struct IdxTable {
8329  int nCol;
8330  char *zName;                    /* Table name */
8331  IdxColumn *aCol;
8332  IdxTable *pNext;                /* Next table in linked list of all tables */
8333};
8334
8335/*
8336** An object of the following type is created for each unique table/write-op
8337** seen. The objects are stored in a singly-linked list beginning at
8338** sqlite3expert.pWrite.
8339*/
8340struct IdxWrite {
8341  IdxTable *pTab;
8342  int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
8343  IdxWrite *pNext;
8344};
8345
8346/*
8347** Each statement being analyzed is represented by an instance of this
8348** structure.
8349*/
8350struct IdxStatement {
8351  int iId;                        /* Statement number */
8352  char *zSql;                     /* SQL statement */
8353  char *zIdx;                     /* Indexes */
8354  char *zEQP;                     /* Plan */
8355  IdxStatement *pNext;
8356};
8357
8358
8359/*
8360** A hash table for storing strings. With space for a payload string
8361** with each entry. Methods are:
8362**
8363**   idxHashInit()
8364**   idxHashClear()
8365**   idxHashAdd()
8366**   idxHashSearch()
8367*/
8368#define IDX_HASH_SIZE 1023
8369typedef struct IdxHashEntry IdxHashEntry;
8370typedef struct IdxHash IdxHash;
8371struct IdxHashEntry {
8372  char *zKey;                     /* nul-terminated key */
8373  char *zVal;                     /* nul-terminated value string */
8374  char *zVal2;                    /* nul-terminated value string 2 */
8375  IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
8376  IdxHashEntry *pNext;            /* Next entry in hash */
8377};
8378struct IdxHash {
8379  IdxHashEntry *pFirst;
8380  IdxHashEntry *aHash[IDX_HASH_SIZE];
8381};
8382
8383/*
8384** sqlite3expert object.
8385*/
8386struct sqlite3expert {
8387  int iSample;                    /* Percentage of tables to sample for stat1 */
8388  sqlite3 *db;                    /* User database */
8389  sqlite3 *dbm;                   /* In-memory db for this analysis */
8390  sqlite3 *dbv;                   /* Vtab schema for this analysis */
8391  IdxTable *pTable;               /* List of all IdxTable objects */
8392  IdxScan *pScan;                 /* List of scan objects */
8393  IdxWrite *pWrite;               /* List of write objects */
8394  IdxStatement *pStatement;       /* List of IdxStatement objects */
8395  int bRun;                       /* True once analysis has run */
8396  char **pzErrmsg;
8397  int rc;                         /* Error code from whereinfo hook */
8398  IdxHash hIdx;                   /* Hash containing all candidate indexes */
8399  char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
8400};
8401
8402
8403/*
8404** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
8405** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
8406*/
8407static void *idxMalloc(int *pRc, int nByte){
8408  void *pRet;
8409  assert( *pRc==SQLITE_OK );
8410  assert( nByte>0 );
8411  pRet = sqlite3_malloc(nByte);
8412  if( pRet ){
8413    memset(pRet, 0, nByte);
8414  }else{
8415    *pRc = SQLITE_NOMEM;
8416  }
8417  return pRet;
8418}
8419
8420/*
8421** Initialize an IdxHash hash table.
8422*/
8423static void idxHashInit(IdxHash *pHash){
8424  memset(pHash, 0, sizeof(IdxHash));
8425}
8426
8427/*
8428** Reset an IdxHash hash table.
8429*/
8430static void idxHashClear(IdxHash *pHash){
8431  int i;
8432  for(i=0; i<IDX_HASH_SIZE; i++){
8433    IdxHashEntry *pEntry;
8434    IdxHashEntry *pNext;
8435    for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
8436      pNext = pEntry->pHashNext;
8437      sqlite3_free(pEntry->zVal2);
8438      sqlite3_free(pEntry);
8439    }
8440  }
8441  memset(pHash, 0, sizeof(IdxHash));
8442}
8443
8444/*
8445** Return the index of the hash bucket that the string specified by the
8446** arguments to this function belongs.
8447*/
8448static int idxHashString(const char *z, int n){
8449  unsigned int ret = 0;
8450  int i;
8451  for(i=0; i<n; i++){
8452    ret += (ret<<3) + (unsigned char)(z[i]);
8453  }
8454  return (int)(ret % IDX_HASH_SIZE);
8455}
8456
8457/*
8458** If zKey is already present in the hash table, return non-zero and do
8459** nothing. Otherwise, add an entry with key zKey and payload string zVal to
8460** the hash table passed as the second argument.
8461*/
8462static int idxHashAdd(
8463  int *pRc,
8464  IdxHash *pHash,
8465  const char *zKey,
8466  const char *zVal
8467){
8468  int nKey = STRLEN(zKey);
8469  int iHash = idxHashString(zKey, nKey);
8470  int nVal = (zVal ? STRLEN(zVal) : 0);
8471  IdxHashEntry *pEntry;
8472  assert( iHash>=0 );
8473  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8474    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8475      return 1;
8476    }
8477  }
8478  pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
8479  if( pEntry ){
8480    pEntry->zKey = (char*)&pEntry[1];
8481    memcpy(pEntry->zKey, zKey, nKey);
8482    if( zVal ){
8483      pEntry->zVal = &pEntry->zKey[nKey+1];
8484      memcpy(pEntry->zVal, zVal, nVal);
8485    }
8486    pEntry->pHashNext = pHash->aHash[iHash];
8487    pHash->aHash[iHash] = pEntry;
8488
8489    pEntry->pNext = pHash->pFirst;
8490    pHash->pFirst = pEntry;
8491  }
8492  return 0;
8493}
8494
8495/*
8496** If zKey/nKey is present in the hash table, return a pointer to the
8497** hash-entry object.
8498*/
8499static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
8500  int iHash;
8501  IdxHashEntry *pEntry;
8502  if( nKey<0 ) nKey = STRLEN(zKey);
8503  iHash = idxHashString(zKey, nKey);
8504  assert( iHash>=0 );
8505  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8506    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8507      return pEntry;
8508    }
8509  }
8510  return 0;
8511}
8512
8513/*
8514** If the hash table contains an entry with a key equal to the string
8515** passed as the final two arguments to this function, return a pointer
8516** to the payload string. Otherwise, if zKey/nKey is not present in the
8517** hash table, return NULL.
8518*/
8519static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
8520  IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
8521  if( pEntry ) return pEntry->zVal;
8522  return 0;
8523}
8524
8525/*
8526** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
8527** variable to point to a copy of nul-terminated string zColl.
8528*/
8529static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
8530  IdxConstraint *pNew;
8531  int nColl = STRLEN(zColl);
8532
8533  assert( *pRc==SQLITE_OK );
8534  pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
8535  if( pNew ){
8536    pNew->zColl = (char*)&pNew[1];
8537    memcpy(pNew->zColl, zColl, nColl+1);
8538  }
8539  return pNew;
8540}
8541
8542/*
8543** An error associated with database handle db has just occurred. Pass
8544** the error message to callback function xOut.
8545*/
8546static void idxDatabaseError(
8547  sqlite3 *db,                    /* Database handle */
8548  char **pzErrmsg                 /* Write error here */
8549){
8550  *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
8551}
8552
8553/*
8554** Prepare an SQL statement.
8555*/
8556static int idxPrepareStmt(
8557  sqlite3 *db,                    /* Database handle to compile against */
8558  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
8559  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
8560  const char *zSql                /* SQL statement to compile */
8561){
8562  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
8563  if( rc!=SQLITE_OK ){
8564    *ppStmt = 0;
8565    idxDatabaseError(db, pzErrmsg);
8566  }
8567  return rc;
8568}
8569
8570/*
8571** Prepare an SQL statement using the results of a printf() formatting.
8572*/
8573static int idxPrintfPrepareStmt(
8574  sqlite3 *db,                    /* Database handle to compile against */
8575  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
8576  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
8577  const char *zFmt,               /* printf() format of SQL statement */
8578  ...                             /* Trailing printf() arguments */
8579){
8580  va_list ap;
8581  int rc;
8582  char *zSql;
8583  va_start(ap, zFmt);
8584  zSql = sqlite3_vmprintf(zFmt, ap);
8585  if( zSql==0 ){
8586    rc = SQLITE_NOMEM;
8587  }else{
8588    rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
8589    sqlite3_free(zSql);
8590  }
8591  va_end(ap);
8592  return rc;
8593}
8594
8595
8596/*************************************************************************
8597** Beginning of virtual table implementation.
8598*/
8599typedef struct ExpertVtab ExpertVtab;
8600struct ExpertVtab {
8601  sqlite3_vtab base;
8602  IdxTable *pTab;
8603  sqlite3expert *pExpert;
8604};
8605
8606typedef struct ExpertCsr ExpertCsr;
8607struct ExpertCsr {
8608  sqlite3_vtab_cursor base;
8609  sqlite3_stmt *pData;
8610};
8611
8612static char *expertDequote(const char *zIn){
8613  int n = STRLEN(zIn);
8614  char *zRet = sqlite3_malloc(n);
8615
8616  assert( zIn[0]=='\'' );
8617  assert( zIn[n-1]=='\'' );
8618
8619  if( zRet ){
8620    int iOut = 0;
8621    int iIn = 0;
8622    for(iIn=1; iIn<(n-1); iIn++){
8623      if( zIn[iIn]=='\'' ){
8624        assert( zIn[iIn+1]=='\'' );
8625        iIn++;
8626      }
8627      zRet[iOut++] = zIn[iIn];
8628    }
8629    zRet[iOut] = '\0';
8630  }
8631
8632  return zRet;
8633}
8634
8635/*
8636** This function is the implementation of both the xConnect and xCreate
8637** methods of the r-tree virtual table.
8638**
8639**   argv[0]   -> module name
8640**   argv[1]   -> database name
8641**   argv[2]   -> table name
8642**   argv[...] -> column names...
8643*/
8644static int expertConnect(
8645  sqlite3 *db,
8646  void *pAux,
8647  int argc, const char *const*argv,
8648  sqlite3_vtab **ppVtab,
8649  char **pzErr
8650){
8651  sqlite3expert *pExpert = (sqlite3expert*)pAux;
8652  ExpertVtab *p = 0;
8653  int rc;
8654
8655  if( argc!=4 ){
8656    *pzErr = sqlite3_mprintf("internal error!");
8657    rc = SQLITE_ERROR;
8658  }else{
8659    char *zCreateTable = expertDequote(argv[3]);
8660    if( zCreateTable ){
8661      rc = sqlite3_declare_vtab(db, zCreateTable);
8662      if( rc==SQLITE_OK ){
8663        p = idxMalloc(&rc, sizeof(ExpertVtab));
8664      }
8665      if( rc==SQLITE_OK ){
8666        p->pExpert = pExpert;
8667        p->pTab = pExpert->pTable;
8668        assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
8669      }
8670      sqlite3_free(zCreateTable);
8671    }else{
8672      rc = SQLITE_NOMEM;
8673    }
8674  }
8675
8676  *ppVtab = (sqlite3_vtab*)p;
8677  return rc;
8678}
8679
8680static int expertDisconnect(sqlite3_vtab *pVtab){
8681  ExpertVtab *p = (ExpertVtab*)pVtab;
8682  sqlite3_free(p);
8683  return SQLITE_OK;
8684}
8685
8686static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
8687  ExpertVtab *p = (ExpertVtab*)pVtab;
8688  int rc = SQLITE_OK;
8689  int n = 0;
8690  IdxScan *pScan;
8691  const int opmask =
8692    SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
8693    SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
8694    SQLITE_INDEX_CONSTRAINT_LE;
8695
8696  pScan = idxMalloc(&rc, sizeof(IdxScan));
8697  if( pScan ){
8698    int i;
8699
8700    /* Link the new scan object into the list */
8701    pScan->pTab = p->pTab;
8702    pScan->pNextScan = p->pExpert->pScan;
8703    p->pExpert->pScan = pScan;
8704
8705    /* Add the constraints to the IdxScan object */
8706    for(i=0; i<pIdxInfo->nConstraint; i++){
8707      struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8708      if( pCons->usable
8709       && pCons->iColumn>=0
8710       && p->pTab->aCol[pCons->iColumn].iPk==0
8711       && (pCons->op & opmask)
8712      ){
8713        IdxConstraint *pNew;
8714        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
8715        pNew = idxNewConstraint(&rc, zColl);
8716        if( pNew ){
8717          pNew->iCol = pCons->iColumn;
8718          if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8719            pNew->pNext = pScan->pEq;
8720            pScan->pEq = pNew;
8721          }else{
8722            pNew->bRange = 1;
8723            pNew->pNext = pScan->pRange;
8724            pScan->pRange = pNew;
8725          }
8726        }
8727        n++;
8728        pIdxInfo->aConstraintUsage[i].argvIndex = n;
8729      }
8730    }
8731
8732    /* Add the ORDER BY to the IdxScan object */
8733    for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
8734      int iCol = pIdxInfo->aOrderBy[i].iColumn;
8735      if( iCol>=0 ){
8736        IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
8737        if( pNew ){
8738          pNew->iCol = iCol;
8739          pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
8740          pNew->pNext = pScan->pOrder;
8741          pNew->pLink = pScan->pOrder;
8742          pScan->pOrder = pNew;
8743          n++;
8744        }
8745      }
8746    }
8747  }
8748
8749  pIdxInfo->estimatedCost = 1000000.0 / (n+1);
8750  return rc;
8751}
8752
8753static int expertUpdate(
8754  sqlite3_vtab *pVtab,
8755  int nData,
8756  sqlite3_value **azData,
8757  sqlite_int64 *pRowid
8758){
8759  (void)pVtab;
8760  (void)nData;
8761  (void)azData;
8762  (void)pRowid;
8763  return SQLITE_OK;
8764}
8765
8766/*
8767** Virtual table module xOpen method.
8768*/
8769static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8770  int rc = SQLITE_OK;
8771  ExpertCsr *pCsr;
8772  (void)pVTab;
8773  pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
8774  *ppCursor = (sqlite3_vtab_cursor*)pCsr;
8775  return rc;
8776}
8777
8778/*
8779** Virtual table module xClose method.
8780*/
8781static int expertClose(sqlite3_vtab_cursor *cur){
8782  ExpertCsr *pCsr = (ExpertCsr*)cur;
8783  sqlite3_finalize(pCsr->pData);
8784  sqlite3_free(pCsr);
8785  return SQLITE_OK;
8786}
8787
8788/*
8789** Virtual table module xEof method.
8790**
8791** Return non-zero if the cursor does not currently point to a valid
8792** record (i.e if the scan has finished), or zero otherwise.
8793*/
8794static int expertEof(sqlite3_vtab_cursor *cur){
8795  ExpertCsr *pCsr = (ExpertCsr*)cur;
8796  return pCsr->pData==0;
8797}
8798
8799/*
8800** Virtual table module xNext method.
8801*/
8802static int expertNext(sqlite3_vtab_cursor *cur){
8803  ExpertCsr *pCsr = (ExpertCsr*)cur;
8804  int rc = SQLITE_OK;
8805
8806  assert( pCsr->pData );
8807  rc = sqlite3_step(pCsr->pData);
8808  if( rc!=SQLITE_ROW ){
8809    rc = sqlite3_finalize(pCsr->pData);
8810    pCsr->pData = 0;
8811  }else{
8812    rc = SQLITE_OK;
8813  }
8814
8815  return rc;
8816}
8817
8818/*
8819** Virtual table module xRowid method.
8820*/
8821static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8822  (void)cur;
8823  *pRowid = 0;
8824  return SQLITE_OK;
8825}
8826
8827/*
8828** Virtual table module xColumn method.
8829*/
8830static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
8831  ExpertCsr *pCsr = (ExpertCsr*)cur;
8832  sqlite3_value *pVal;
8833  pVal = sqlite3_column_value(pCsr->pData, i);
8834  if( pVal ){
8835    sqlite3_result_value(ctx, pVal);
8836  }
8837  return SQLITE_OK;
8838}
8839
8840/*
8841** Virtual table module xFilter method.
8842*/
8843static int expertFilter(
8844  sqlite3_vtab_cursor *cur,
8845  int idxNum, const char *idxStr,
8846  int argc, sqlite3_value **argv
8847){
8848  ExpertCsr *pCsr = (ExpertCsr*)cur;
8849  ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
8850  sqlite3expert *pExpert = pVtab->pExpert;
8851  int rc;
8852
8853  (void)idxNum;
8854  (void)idxStr;
8855  (void)argc;
8856  (void)argv;
8857  rc = sqlite3_finalize(pCsr->pData);
8858  pCsr->pData = 0;
8859  if( rc==SQLITE_OK ){
8860    rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
8861        "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
8862    );
8863  }
8864
8865  if( rc==SQLITE_OK ){
8866    rc = expertNext(cur);
8867  }
8868  return rc;
8869}
8870
8871static int idxRegisterVtab(sqlite3expert *p){
8872  static sqlite3_module expertModule = {
8873    2,                            /* iVersion */
8874    expertConnect,                /* xCreate - create a table */
8875    expertConnect,                /* xConnect - connect to an existing table */
8876    expertBestIndex,              /* xBestIndex - Determine search strategy */
8877    expertDisconnect,             /* xDisconnect - Disconnect from a table */
8878    expertDisconnect,             /* xDestroy - Drop a table */
8879    expertOpen,                   /* xOpen - open a cursor */
8880    expertClose,                  /* xClose - close a cursor */
8881    expertFilter,                 /* xFilter - configure scan constraints */
8882    expertNext,                   /* xNext - advance a cursor */
8883    expertEof,                    /* xEof */
8884    expertColumn,                 /* xColumn - read data */
8885    expertRowid,                  /* xRowid - read data */
8886    expertUpdate,                 /* xUpdate - write data */
8887    0,                            /* xBegin - begin transaction */
8888    0,                            /* xSync - sync transaction */
8889    0,                            /* xCommit - commit transaction */
8890    0,                            /* xRollback - rollback transaction */
8891    0,                            /* xFindFunction - function overloading */
8892    0,                            /* xRename - rename the table */
8893    0,                            /* xSavepoint */
8894    0,                            /* xRelease */
8895    0,                            /* xRollbackTo */
8896    0,                            /* xShadowName */
8897  };
8898
8899  return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
8900}
8901/*
8902** End of virtual table implementation.
8903*************************************************************************/
8904/*
8905** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
8906** is called, set it to the return value of sqlite3_finalize() before
8907** returning. Otherwise, discard the sqlite3_finalize() return value.
8908*/
8909static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
8910  int rc = sqlite3_finalize(pStmt);
8911  if( *pRc==SQLITE_OK ) *pRc = rc;
8912}
8913
8914/*
8915** Attempt to allocate an IdxTable structure corresponding to table zTab
8916** in the main database of connection db. If successful, set (*ppOut) to
8917** point to the new object and return SQLITE_OK. Otherwise, return an
8918** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
8919** set to point to an error string.
8920**
8921** It is the responsibility of the caller to eventually free either the
8922** IdxTable object or error message using sqlite3_free().
8923*/
8924static int idxGetTableInfo(
8925  sqlite3 *db,                    /* Database connection to read details from */
8926  const char *zTab,               /* Table name */
8927  IdxTable **ppOut,               /* OUT: New object (if successful) */
8928  char **pzErrmsg                 /* OUT: Error message (if not) */
8929){
8930  sqlite3_stmt *p1 = 0;
8931  int nCol = 0;
8932  int nTab = STRLEN(zTab);
8933  int nByte = sizeof(IdxTable) + nTab + 1;
8934  IdxTable *pNew = 0;
8935  int rc, rc2;
8936  char *pCsr = 0;
8937  int nPk = 0;
8938
8939  rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
8940  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8941    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8942    nByte += 1 + STRLEN(zCol);
8943    rc = sqlite3_table_column_metadata(
8944        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8945    );
8946    nByte += 1 + STRLEN(zCol);
8947    nCol++;
8948    nPk += (sqlite3_column_int(p1, 5)>0);
8949  }
8950  rc2 = sqlite3_reset(p1);
8951  if( rc==SQLITE_OK ) rc = rc2;
8952
8953  nByte += sizeof(IdxColumn) * nCol;
8954  if( rc==SQLITE_OK ){
8955    pNew = idxMalloc(&rc, nByte);
8956  }
8957  if( rc==SQLITE_OK ){
8958    pNew->aCol = (IdxColumn*)&pNew[1];
8959    pNew->nCol = nCol;
8960    pCsr = (char*)&pNew->aCol[nCol];
8961  }
8962
8963  nCol = 0;
8964  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8965    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8966    int nCopy = STRLEN(zCol) + 1;
8967    pNew->aCol[nCol].zName = pCsr;
8968    pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
8969    memcpy(pCsr, zCol, nCopy);
8970    pCsr += nCopy;
8971
8972    rc = sqlite3_table_column_metadata(
8973        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8974    );
8975    if( rc==SQLITE_OK ){
8976      nCopy = STRLEN(zCol) + 1;
8977      pNew->aCol[nCol].zColl = pCsr;
8978      memcpy(pCsr, zCol, nCopy);
8979      pCsr += nCopy;
8980    }
8981
8982    nCol++;
8983  }
8984  idxFinalize(&rc, p1);
8985
8986  if( rc!=SQLITE_OK ){
8987    sqlite3_free(pNew);
8988    pNew = 0;
8989  }else{
8990    pNew->zName = pCsr;
8991    memcpy(pNew->zName, zTab, nTab+1);
8992  }
8993
8994  *ppOut = pNew;
8995  return rc;
8996}
8997
8998/*
8999** This function is a no-op if *pRc is set to anything other than
9000** SQLITE_OK when it is called.
9001**
9002** If *pRc is initially set to SQLITE_OK, then the text specified by
9003** the printf() style arguments is appended to zIn and the result returned
9004** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9005** zIn before returning.
9006*/
9007static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9008  va_list ap;
9009  char *zAppend = 0;
9010  char *zRet = 0;
9011  int nIn = zIn ? STRLEN(zIn) : 0;
9012  int nAppend = 0;
9013  va_start(ap, zFmt);
9014  if( *pRc==SQLITE_OK ){
9015    zAppend = sqlite3_vmprintf(zFmt, ap);
9016    if( zAppend ){
9017      nAppend = STRLEN(zAppend);
9018      zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9019    }
9020    if( zAppend && zRet ){
9021      if( nIn ) memcpy(zRet, zIn, nIn);
9022      memcpy(&zRet[nIn], zAppend, nAppend+1);
9023    }else{
9024      sqlite3_free(zRet);
9025      zRet = 0;
9026      *pRc = SQLITE_NOMEM;
9027    }
9028    sqlite3_free(zAppend);
9029    sqlite3_free(zIn);
9030  }
9031  va_end(ap);
9032  return zRet;
9033}
9034
9035/*
9036** Return true if zId must be quoted in order to use it as an SQL
9037** identifier, or false otherwise.
9038*/
9039static int idxIdentifierRequiresQuotes(const char *zId){
9040  int i;
9041  for(i=0; zId[i]; i++){
9042    if( !(zId[i]=='_')
9043     && !(zId[i]>='0' && zId[i]<='9')
9044     && !(zId[i]>='a' && zId[i]<='z')
9045     && !(zId[i]>='A' && zId[i]<='Z')
9046    ){
9047      return 1;
9048    }
9049  }
9050  return 0;
9051}
9052
9053/*
9054** This function appends an index column definition suitable for constraint
9055** pCons to the string passed as zIn and returns the result.
9056*/
9057static char *idxAppendColDefn(
9058  int *pRc,                       /* IN/OUT: Error code */
9059  char *zIn,                      /* Column defn accumulated so far */
9060  IdxTable *pTab,                 /* Table index will be created on */
9061  IdxConstraint *pCons
9062){
9063  char *zRet = zIn;
9064  IdxColumn *p = &pTab->aCol[pCons->iCol];
9065  if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9066
9067  if( idxIdentifierRequiresQuotes(p->zName) ){
9068    zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9069  }else{
9070    zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9071  }
9072
9073  if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9074    if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9075      zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9076    }else{
9077      zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9078    }
9079  }
9080
9081  if( pCons->bDesc ){
9082    zRet = idxAppendText(pRc, zRet, " DESC");
9083  }
9084  return zRet;
9085}
9086
9087/*
9088** Search database dbm for an index compatible with the one idxCreateFromCons()
9089** would create from arguments pScan, pEq and pTail. If no error occurs and
9090** such an index is found, return non-zero. Or, if no such index is found,
9091** return zero.
9092**
9093** If an error occurs, set *pRc to an SQLite error code and return zero.
9094*/
9095static int idxFindCompatible(
9096  int *pRc,                       /* OUT: Error code */
9097  sqlite3* dbm,                   /* Database to search */
9098  IdxScan *pScan,                 /* Scan for table to search for index on */
9099  IdxConstraint *pEq,             /* List of == constraints */
9100  IdxConstraint *pTail            /* List of range constraints */
9101){
9102  const char *zTbl = pScan->pTab->zName;
9103  sqlite3_stmt *pIdxList = 0;
9104  IdxConstraint *pIter;
9105  int nEq = 0;                    /* Number of elements in pEq */
9106  int rc;
9107
9108  /* Count the elements in list pEq */
9109  for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
9110
9111  rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
9112  while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
9113    int bMatch = 1;
9114    IdxConstraint *pT = pTail;
9115    sqlite3_stmt *pInfo = 0;
9116    const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
9117
9118    /* Zero the IdxConstraint.bFlag values in the pEq list */
9119    for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
9120
9121    rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
9122    while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
9123      int iIdx = sqlite3_column_int(pInfo, 0);
9124      int iCol = sqlite3_column_int(pInfo, 1);
9125      const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
9126
9127      if( iIdx<nEq ){
9128        for(pIter=pEq; pIter; pIter=pIter->pLink){
9129          if( pIter->bFlag ) continue;
9130          if( pIter->iCol!=iCol ) continue;
9131          if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
9132          pIter->bFlag = 1;
9133          break;
9134        }
9135        if( pIter==0 ){
9136          bMatch = 0;
9137          break;
9138        }
9139      }else{
9140        if( pT ){
9141          if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
9142            bMatch = 0;
9143            break;
9144          }
9145          pT = pT->pLink;
9146        }
9147      }
9148    }
9149    idxFinalize(&rc, pInfo);
9150
9151    if( rc==SQLITE_OK && bMatch ){
9152      sqlite3_finalize(pIdxList);
9153      return 1;
9154    }
9155  }
9156  idxFinalize(&rc, pIdxList);
9157
9158  *pRc = rc;
9159  return 0;
9160}
9161
9162static int idxCreateFromCons(
9163  sqlite3expert *p,
9164  IdxScan *pScan,
9165  IdxConstraint *pEq,
9166  IdxConstraint *pTail
9167){
9168  sqlite3 *dbm = p->dbm;
9169  int rc = SQLITE_OK;
9170  if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
9171    IdxTable *pTab = pScan->pTab;
9172    char *zCols = 0;
9173    char *zIdx = 0;
9174    IdxConstraint *pCons;
9175    unsigned int h = 0;
9176    const char *zFmt;
9177
9178    for(pCons=pEq; pCons; pCons=pCons->pLink){
9179      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9180    }
9181    for(pCons=pTail; pCons; pCons=pCons->pLink){
9182      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9183    }
9184
9185    if( rc==SQLITE_OK ){
9186      /* Hash the list of columns to come up with a name for the index */
9187      const char *zTable = pScan->pTab->zName;
9188      char *zName;                /* Index name */
9189      int i;
9190      for(i=0; zCols[i]; i++){
9191        h += ((h<<3) + zCols[i]);
9192      }
9193      zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
9194      if( zName==0 ){
9195        rc = SQLITE_NOMEM;
9196      }else{
9197        if( idxIdentifierRequiresQuotes(zTable) ){
9198          zFmt = "CREATE INDEX '%q' ON %Q(%s)";
9199        }else{
9200          zFmt = "CREATE INDEX %s ON %s(%s)";
9201        }
9202        zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
9203        if( !zIdx ){
9204          rc = SQLITE_NOMEM;
9205        }else{
9206          rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
9207          idxHashAdd(&rc, &p->hIdx, zName, zIdx);
9208        }
9209        sqlite3_free(zName);
9210        sqlite3_free(zIdx);
9211      }
9212    }
9213
9214    sqlite3_free(zCols);
9215  }
9216  return rc;
9217}
9218
9219/*
9220** Return true if list pList (linked by IdxConstraint.pLink) contains
9221** a constraint compatible with *p. Otherwise return false.
9222*/
9223static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
9224  IdxConstraint *pCmp;
9225  for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
9226    if( p->iCol==pCmp->iCol ) return 1;
9227  }
9228  return 0;
9229}
9230
9231static int idxCreateFromWhere(
9232  sqlite3expert *p,
9233  IdxScan *pScan,                 /* Create indexes for this scan */
9234  IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
9235){
9236  IdxConstraint *p1 = 0;
9237  IdxConstraint *pCon;
9238  int rc;
9239
9240  /* Gather up all the == constraints. */
9241  for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
9242    if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9243      pCon->pLink = p1;
9244      p1 = pCon;
9245    }
9246  }
9247
9248  /* Create an index using the == constraints collected above. And the
9249  ** range constraint/ORDER BY terms passed in by the caller, if any. */
9250  rc = idxCreateFromCons(p, pScan, p1, pTail);
9251
9252  /* If no range/ORDER BY passed by the caller, create a version of the
9253  ** index for each range constraint.  */
9254  if( pTail==0 ){
9255    for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
9256      assert( pCon->pLink==0 );
9257      if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9258        rc = idxCreateFromCons(p, pScan, p1, pCon);
9259      }
9260    }
9261  }
9262
9263  return rc;
9264}
9265
9266/*
9267** Create candidate indexes in database [dbm] based on the data in
9268** linked-list pScan.
9269*/
9270static int idxCreateCandidates(sqlite3expert *p){
9271  int rc = SQLITE_OK;
9272  IdxScan *pIter;
9273
9274  for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
9275    rc = idxCreateFromWhere(p, pIter, 0);
9276    if( rc==SQLITE_OK && pIter->pOrder ){
9277      rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
9278    }
9279  }
9280
9281  return rc;
9282}
9283
9284/*
9285** Free all elements of the linked list starting at pConstraint.
9286*/
9287static void idxConstraintFree(IdxConstraint *pConstraint){
9288  IdxConstraint *pNext;
9289  IdxConstraint *p;
9290
9291  for(p=pConstraint; p; p=pNext){
9292    pNext = p->pNext;
9293    sqlite3_free(p);
9294  }
9295}
9296
9297/*
9298** Free all elements of the linked list starting from pScan up until pLast
9299** (pLast is not freed).
9300*/
9301static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
9302  IdxScan *p;
9303  IdxScan *pNext;
9304  for(p=pScan; p!=pLast; p=pNext){
9305    pNext = p->pNextScan;
9306    idxConstraintFree(p->pOrder);
9307    idxConstraintFree(p->pEq);
9308    idxConstraintFree(p->pRange);
9309    sqlite3_free(p);
9310  }
9311}
9312
9313/*
9314** Free all elements of the linked list starting from pStatement up
9315** until pLast (pLast is not freed).
9316*/
9317static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
9318  IdxStatement *p;
9319  IdxStatement *pNext;
9320  for(p=pStatement; p!=pLast; p=pNext){
9321    pNext = p->pNext;
9322    sqlite3_free(p->zEQP);
9323    sqlite3_free(p->zIdx);
9324    sqlite3_free(p);
9325  }
9326}
9327
9328/*
9329** Free the linked list of IdxTable objects starting at pTab.
9330*/
9331static void idxTableFree(IdxTable *pTab){
9332  IdxTable *pIter;
9333  IdxTable *pNext;
9334  for(pIter=pTab; pIter; pIter=pNext){
9335    pNext = pIter->pNext;
9336    sqlite3_free(pIter);
9337  }
9338}
9339
9340/*
9341** Free the linked list of IdxWrite objects starting at pTab.
9342*/
9343static void idxWriteFree(IdxWrite *pTab){
9344  IdxWrite *pIter;
9345  IdxWrite *pNext;
9346  for(pIter=pTab; pIter; pIter=pNext){
9347    pNext = pIter->pNext;
9348    sqlite3_free(pIter);
9349  }
9350}
9351
9352
9353
9354/*
9355** This function is called after candidate indexes have been created. It
9356** runs all the queries to see which indexes they prefer, and populates
9357** IdxStatement.zIdx and IdxStatement.zEQP with the results.
9358*/
9359int idxFindIndexes(
9360  sqlite3expert *p,
9361  char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
9362){
9363  IdxStatement *pStmt;
9364  sqlite3 *dbm = p->dbm;
9365  int rc = SQLITE_OK;
9366
9367  IdxHash hIdx;
9368  idxHashInit(&hIdx);
9369
9370  for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
9371    IdxHashEntry *pEntry;
9372    sqlite3_stmt *pExplain = 0;
9373    idxHashClear(&hIdx);
9374    rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
9375        "EXPLAIN QUERY PLAN %s", pStmt->zSql
9376    );
9377    while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
9378      /* int iId = sqlite3_column_int(pExplain, 0); */
9379      /* int iParent = sqlite3_column_int(pExplain, 1); */
9380      /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
9381      const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
9382      int nDetail;
9383      int i;
9384
9385      if( !zDetail ) continue;
9386      nDetail = STRLEN(zDetail);
9387
9388      for(i=0; i<nDetail; i++){
9389        const char *zIdx = 0;
9390        if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
9391          zIdx = &zDetail[i+13];
9392        }else if( i+22<nDetail
9393            && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
9394        ){
9395          zIdx = &zDetail[i+22];
9396        }
9397        if( zIdx ){
9398          const char *zSql;
9399          int nIdx = 0;
9400          while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
9401            nIdx++;
9402          }
9403          zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
9404          if( zSql ){
9405            idxHashAdd(&rc, &hIdx, zSql, 0);
9406            if( rc ) goto find_indexes_out;
9407          }
9408          break;
9409        }
9410      }
9411
9412      if( zDetail[0]!='-' ){
9413        pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
9414      }
9415    }
9416
9417    for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
9418      pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
9419    }
9420
9421    idxFinalize(&rc, pExplain);
9422  }
9423
9424 find_indexes_out:
9425  idxHashClear(&hIdx);
9426  return rc;
9427}
9428
9429static int idxAuthCallback(
9430  void *pCtx,
9431  int eOp,
9432  const char *z3,
9433  const char *z4,
9434  const char *zDb,
9435  const char *zTrigger
9436){
9437  int rc = SQLITE_OK;
9438  (void)z4;
9439  (void)zTrigger;
9440  if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
9441    if( sqlite3_stricmp(zDb, "main")==0 ){
9442      sqlite3expert *p = (sqlite3expert*)pCtx;
9443      IdxTable *pTab;
9444      for(pTab=p->pTable; pTab; pTab=pTab->pNext){
9445        if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
9446      }
9447      if( pTab ){
9448        IdxWrite *pWrite;
9449        for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
9450          if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
9451        }
9452        if( pWrite==0 ){
9453          pWrite = idxMalloc(&rc, sizeof(IdxWrite));
9454          if( rc==SQLITE_OK ){
9455            pWrite->pTab = pTab;
9456            pWrite->eOp = eOp;
9457            pWrite->pNext = p->pWrite;
9458            p->pWrite = pWrite;
9459          }
9460        }
9461      }
9462    }
9463  }
9464  return rc;
9465}
9466
9467static int idxProcessOneTrigger(
9468  sqlite3expert *p,
9469  IdxWrite *pWrite,
9470  char **pzErr
9471){
9472  static const char *zInt = UNIQUE_TABLE_NAME;
9473  static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
9474  IdxTable *pTab = pWrite->pTab;
9475  const char *zTab = pTab->zName;
9476  const char *zSql =
9477    "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
9478    "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
9479    "ORDER BY type;";
9480  sqlite3_stmt *pSelect = 0;
9481  int rc = SQLITE_OK;
9482  char *zWrite = 0;
9483
9484  /* Create the table and its triggers in the temp schema */
9485  rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
9486  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
9487    const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
9488    rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
9489  }
9490  idxFinalize(&rc, pSelect);
9491
9492  /* Rename the table in the temp schema to zInt */
9493  if( rc==SQLITE_OK ){
9494    char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
9495    if( z==0 ){
9496      rc = SQLITE_NOMEM;
9497    }else{
9498      rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
9499      sqlite3_free(z);
9500    }
9501  }
9502
9503  switch( pWrite->eOp ){
9504    case SQLITE_INSERT: {
9505      int i;
9506      zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
9507      for(i=0; i<pTab->nCol; i++){
9508        zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
9509      }
9510      zWrite = idxAppendText(&rc, zWrite, ")");
9511      break;
9512    }
9513    case SQLITE_UPDATE: {
9514      int i;
9515      zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
9516      for(i=0; i<pTab->nCol; i++){
9517        zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
9518            pTab->aCol[i].zName
9519        );
9520      }
9521      break;
9522    }
9523    default: {
9524      assert( pWrite->eOp==SQLITE_DELETE );
9525      if( rc==SQLITE_OK ){
9526        zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
9527        if( zWrite==0 ) rc = SQLITE_NOMEM;
9528      }
9529    }
9530  }
9531
9532  if( rc==SQLITE_OK ){
9533    sqlite3_stmt *pX = 0;
9534    rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
9535    idxFinalize(&rc, pX);
9536    if( rc!=SQLITE_OK ){
9537      idxDatabaseError(p->dbv, pzErr);
9538    }
9539  }
9540  sqlite3_free(zWrite);
9541
9542  if( rc==SQLITE_OK ){
9543    rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
9544  }
9545
9546  return rc;
9547}
9548
9549static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
9550  int rc = SQLITE_OK;
9551  IdxWrite *pEnd = 0;
9552  IdxWrite *pFirst = p->pWrite;
9553
9554  while( rc==SQLITE_OK && pFirst!=pEnd ){
9555    IdxWrite *pIter;
9556    for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
9557      rc = idxProcessOneTrigger(p, pIter, pzErr);
9558    }
9559    pEnd = pFirst;
9560    pFirst = p->pWrite;
9561  }
9562
9563  return rc;
9564}
9565
9566
9567static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9568  int rc = idxRegisterVtab(p);
9569  sqlite3_stmt *pSchema = 0;
9570
9571  /* For each table in the main db schema:
9572  **
9573  **   1) Add an entry to the p->pTable list, and
9574  **   2) Create the equivalent virtual table in dbv.
9575  */
9576  rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9577      "SELECT type, name, sql, 1 FROM sqlite_schema "
9578      "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9579      " UNION ALL "
9580      "SELECT type, name, sql, 2 FROM sqlite_schema "
9581      "WHERE type = 'trigger'"
9582      "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9583      "ORDER BY 4, 1"
9584  );
9585  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9586    const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9587    const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9588    const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9589
9590    if( zType[0]=='v' || zType[1]=='r' ){
9591      rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9592    }else{
9593      IdxTable *pTab;
9594      rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9595      if( rc==SQLITE_OK ){
9596        int i;
9597        char *zInner = 0;
9598        char *zOuter = 0;
9599        pTab->pNext = p->pTable;
9600        p->pTable = pTab;
9601
9602        /* The statement the vtab will pass to sqlite3_declare_vtab() */
9603        zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9604        for(i=0; i<pTab->nCol; i++){
9605          zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9606              (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9607          );
9608        }
9609        zInner = idxAppendText(&rc, zInner, ")");
9610
9611        /* The CVT statement to create the vtab */
9612        zOuter = idxAppendText(&rc, 0,
9613            "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9614        );
9615        if( rc==SQLITE_OK ){
9616          rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9617        }
9618        sqlite3_free(zInner);
9619        sqlite3_free(zOuter);
9620      }
9621    }
9622  }
9623  idxFinalize(&rc, pSchema);
9624  return rc;
9625}
9626
9627struct IdxSampleCtx {
9628  int iTarget;
9629  double target;                  /* Target nRet/nRow value */
9630  double nRow;                    /* Number of rows seen */
9631  double nRet;                    /* Number of rows returned */
9632};
9633
9634static void idxSampleFunc(
9635  sqlite3_context *pCtx,
9636  int argc,
9637  sqlite3_value **argv
9638){
9639  struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
9640  int bRet;
9641
9642  (void)argv;
9643  assert( argc==0 );
9644  if( p->nRow==0.0 ){
9645    bRet = 1;
9646  }else{
9647    bRet = (p->nRet / p->nRow) <= p->target;
9648    if( bRet==0 ){
9649      unsigned short rnd;
9650      sqlite3_randomness(2, (void*)&rnd);
9651      bRet = ((int)rnd % 100) <= p->iTarget;
9652    }
9653  }
9654
9655  sqlite3_result_int(pCtx, bRet);
9656  p->nRow += 1.0;
9657  p->nRet += (double)bRet;
9658}
9659
9660struct IdxRemCtx {
9661  int nSlot;
9662  struct IdxRemSlot {
9663    int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
9664    i64 iVal;                     /* SQLITE_INTEGER value */
9665    double rVal;                  /* SQLITE_FLOAT value */
9666    int nByte;                    /* Bytes of space allocated at z */
9667    int n;                        /* Size of buffer z */
9668    char *z;                      /* SQLITE_TEXT/BLOB value */
9669  } aSlot[1];
9670};
9671
9672/*
9673** Implementation of scalar function rem().
9674*/
9675static void idxRemFunc(
9676  sqlite3_context *pCtx,
9677  int argc,
9678  sqlite3_value **argv
9679){
9680  struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
9681  struct IdxRemSlot *pSlot;
9682  int iSlot;
9683  assert( argc==2 );
9684
9685  iSlot = sqlite3_value_int(argv[0]);
9686  assert( iSlot<=p->nSlot );
9687  pSlot = &p->aSlot[iSlot];
9688
9689  switch( pSlot->eType ){
9690    case SQLITE_NULL:
9691      /* no-op */
9692      break;
9693
9694    case SQLITE_INTEGER:
9695      sqlite3_result_int64(pCtx, pSlot->iVal);
9696      break;
9697
9698    case SQLITE_FLOAT:
9699      sqlite3_result_double(pCtx, pSlot->rVal);
9700      break;
9701
9702    case SQLITE_BLOB:
9703      sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9704      break;
9705
9706    case SQLITE_TEXT:
9707      sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9708      break;
9709  }
9710
9711  pSlot->eType = sqlite3_value_type(argv[1]);
9712  switch( pSlot->eType ){
9713    case SQLITE_NULL:
9714      /* no-op */
9715      break;
9716
9717    case SQLITE_INTEGER:
9718      pSlot->iVal = sqlite3_value_int64(argv[1]);
9719      break;
9720
9721    case SQLITE_FLOAT:
9722      pSlot->rVal = sqlite3_value_double(argv[1]);
9723      break;
9724
9725    case SQLITE_BLOB:
9726    case SQLITE_TEXT: {
9727      int nByte = sqlite3_value_bytes(argv[1]);
9728      if( nByte>pSlot->nByte ){
9729        char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
9730        if( zNew==0 ){
9731          sqlite3_result_error_nomem(pCtx);
9732          return;
9733        }
9734        pSlot->nByte = nByte*2;
9735        pSlot->z = zNew;
9736      }
9737      pSlot->n = nByte;
9738      if( pSlot->eType==SQLITE_BLOB ){
9739        memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
9740      }else{
9741        memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
9742      }
9743      break;
9744    }
9745  }
9746}
9747
9748static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
9749  int rc = SQLITE_OK;
9750  const char *zMax =
9751    "SELECT max(i.seqno) FROM "
9752    "  sqlite_schema AS s, "
9753    "  pragma_index_list(s.name) AS l, "
9754    "  pragma_index_info(l.name) AS i "
9755    "WHERE s.type = 'table'";
9756  sqlite3_stmt *pMax = 0;
9757
9758  *pnMax = 0;
9759  rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
9760  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
9761    *pnMax = sqlite3_column_int(pMax, 0) + 1;
9762  }
9763  idxFinalize(&rc, pMax);
9764
9765  return rc;
9766}
9767
9768static int idxPopulateOneStat1(
9769  sqlite3expert *p,
9770  sqlite3_stmt *pIndexXInfo,
9771  sqlite3_stmt *pWriteStat,
9772  const char *zTab,
9773  const char *zIdx,
9774  char **pzErr
9775){
9776  char *zCols = 0;
9777  char *zOrder = 0;
9778  char *zQuery = 0;
9779  int nCol = 0;
9780  int i;
9781  sqlite3_stmt *pQuery = 0;
9782  int *aStat = 0;
9783  int rc = SQLITE_OK;
9784
9785  assert( p->iSample>0 );
9786
9787  /* Formulate the query text */
9788  sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
9789  while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
9790    const char *zComma = zCols==0 ? "" : ", ";
9791    const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
9792    const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
9793    zCols = idxAppendText(&rc, zCols,
9794        "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
9795    );
9796    zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
9797  }
9798  sqlite3_reset(pIndexXInfo);
9799  if( rc==SQLITE_OK ){
9800    if( p->iSample==100 ){
9801      zQuery = sqlite3_mprintf(
9802          "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
9803      );
9804    }else{
9805      zQuery = sqlite3_mprintf(
9806          "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
9807      );
9808    }
9809  }
9810  sqlite3_free(zCols);
9811  sqlite3_free(zOrder);
9812
9813  /* Formulate the query text */
9814  if( rc==SQLITE_OK ){
9815    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9816    rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
9817  }
9818  sqlite3_free(zQuery);
9819
9820  if( rc==SQLITE_OK ){
9821    aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
9822  }
9823  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9824    IdxHashEntry *pEntry;
9825    char *zStat = 0;
9826    for(i=0; i<=nCol; i++) aStat[i] = 1;
9827    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9828      aStat[0]++;
9829      for(i=0; i<nCol; i++){
9830        if( sqlite3_column_int(pQuery, i)==0 ) break;
9831      }
9832      for(/*no-op*/; i<nCol; i++){
9833        aStat[i+1]++;
9834      }
9835    }
9836
9837    if( rc==SQLITE_OK ){
9838      int s0 = aStat[0];
9839      zStat = sqlite3_mprintf("%d", s0);
9840      if( zStat==0 ) rc = SQLITE_NOMEM;
9841      for(i=1; rc==SQLITE_OK && i<=nCol; i++){
9842        zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
9843      }
9844    }
9845
9846    if( rc==SQLITE_OK ){
9847      sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
9848      sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
9849      sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
9850      sqlite3_step(pWriteStat);
9851      rc = sqlite3_reset(pWriteStat);
9852    }
9853
9854    pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
9855    if( pEntry ){
9856      assert( pEntry->zVal2==0 );
9857      pEntry->zVal2 = zStat;
9858    }else{
9859      sqlite3_free(zStat);
9860    }
9861  }
9862  sqlite3_free(aStat);
9863  idxFinalize(&rc, pQuery);
9864
9865  return rc;
9866}
9867
9868static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
9869  int rc;
9870  char *zSql;
9871
9872  rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9873  if( rc!=SQLITE_OK ) return rc;
9874
9875  zSql = sqlite3_mprintf(
9876      "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
9877  );
9878  if( zSql==0 ) return SQLITE_NOMEM;
9879  rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
9880  sqlite3_free(zSql);
9881
9882  return rc;
9883}
9884
9885/*
9886** This function is called as part of sqlite3_expert_analyze(). Candidate
9887** indexes have already been created in database sqlite3expert.dbm, this
9888** function populates sqlite_stat1 table in the same database.
9889**
9890** The stat1 data is generated by querying the
9891*/
9892static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
9893  int rc = SQLITE_OK;
9894  int nMax =0;
9895  struct IdxRemCtx *pCtx = 0;
9896  struct IdxSampleCtx samplectx;
9897  int i;
9898  i64 iPrev = -100000;
9899  sqlite3_stmt *pAllIndex = 0;
9900  sqlite3_stmt *pIndexXInfo = 0;
9901  sqlite3_stmt *pWrite = 0;
9902
9903  const char *zAllIndex =
9904    "SELECT s.rowid, s.name, l.name FROM "
9905    "  sqlite_schema AS s, "
9906    "  pragma_index_list(s.name) AS l "
9907    "WHERE s.type = 'table'";
9908  const char *zIndexXInfo =
9909    "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
9910  const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
9911
9912  /* If iSample==0, no sqlite_stat1 data is required. */
9913  if( p->iSample==0 ) return SQLITE_OK;
9914
9915  rc = idxLargestIndex(p->dbm, &nMax, pzErr);
9916  if( nMax<=0 || rc!=SQLITE_OK ) return rc;
9917
9918  rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
9919
9920  if( rc==SQLITE_OK ){
9921    int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
9922    pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
9923  }
9924
9925  if( rc==SQLITE_OK ){
9926    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9927    rc = sqlite3_create_function(
9928        dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
9929    );
9930  }
9931  if( rc==SQLITE_OK ){
9932    rc = sqlite3_create_function(
9933        p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
9934    );
9935  }
9936
9937  if( rc==SQLITE_OK ){
9938    pCtx->nSlot = nMax+1;
9939    rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
9940  }
9941  if( rc==SQLITE_OK ){
9942    rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
9943  }
9944  if( rc==SQLITE_OK ){
9945    rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
9946  }
9947
9948  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
9949    i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
9950    const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
9951    const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
9952    if( p->iSample<100 && iPrev!=iRowid ){
9953      samplectx.target = (double)p->iSample / 100.0;
9954      samplectx.iTarget = p->iSample;
9955      samplectx.nRow = 0.0;
9956      samplectx.nRet = 0.0;
9957      rc = idxBuildSampleTable(p, zTab);
9958      if( rc!=SQLITE_OK ) break;
9959    }
9960    rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
9961    iPrev = iRowid;
9962  }
9963  if( rc==SQLITE_OK && p->iSample<100 ){
9964    rc = sqlite3_exec(p->dbv,
9965        "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
9966    );
9967  }
9968
9969  idxFinalize(&rc, pAllIndex);
9970  idxFinalize(&rc, pIndexXInfo);
9971  idxFinalize(&rc, pWrite);
9972
9973  if( pCtx ){
9974    for(i=0; i<pCtx->nSlot; i++){
9975      sqlite3_free(pCtx->aSlot[i].z);
9976    }
9977    sqlite3_free(pCtx);
9978  }
9979
9980  if( rc==SQLITE_OK ){
9981    rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
9982  }
9983
9984  sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9985  return rc;
9986}
9987
9988/*
9989** Allocate a new sqlite3expert object.
9990*/
9991sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
9992  int rc = SQLITE_OK;
9993  sqlite3expert *pNew;
9994
9995  pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
9996
9997  /* Open two in-memory databases to work with. The "vtab database" (dbv)
9998  ** will contain a virtual table corresponding to each real table in
9999  ** the user database schema, and a copy of each view. It is used to
10000  ** collect information regarding the WHERE, ORDER BY and other clauses
10001  ** of the user's query.
10002  */
10003  if( rc==SQLITE_OK ){
10004    pNew->db = db;
10005    pNew->iSample = 100;
10006    rc = sqlite3_open(":memory:", &pNew->dbv);
10007  }
10008  if( rc==SQLITE_OK ){
10009    rc = sqlite3_open(":memory:", &pNew->dbm);
10010    if( rc==SQLITE_OK ){
10011      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10012    }
10013  }
10014
10015
10016  /* Copy the entire schema of database [db] into [dbm]. */
10017  if( rc==SQLITE_OK ){
10018    sqlite3_stmt *pSql;
10019    rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10020        "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10021        " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10022    );
10023    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10024      const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10025      rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10026    }
10027    idxFinalize(&rc, pSql);
10028  }
10029
10030  /* Create the vtab schema */
10031  if( rc==SQLITE_OK ){
10032    rc = idxCreateVtabSchema(pNew, pzErrmsg);
10033  }
10034
10035  /* Register the auth callback with dbv */
10036  if( rc==SQLITE_OK ){
10037    sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10038  }
10039
10040  /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10041  ** return the new sqlite3expert handle.  */
10042  if( rc!=SQLITE_OK ){
10043    sqlite3_expert_destroy(pNew);
10044    pNew = 0;
10045  }
10046  return pNew;
10047}
10048
10049/*
10050** Configure an sqlite3expert object.
10051*/
10052int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10053  int rc = SQLITE_OK;
10054  va_list ap;
10055  va_start(ap, op);
10056  switch( op ){
10057    case EXPERT_CONFIG_SAMPLE: {
10058      int iVal = va_arg(ap, int);
10059      if( iVal<0 ) iVal = 0;
10060      if( iVal>100 ) iVal = 100;
10061      p->iSample = iVal;
10062      break;
10063    }
10064    default:
10065      rc = SQLITE_NOTFOUND;
10066      break;
10067  }
10068
10069  va_end(ap);
10070  return rc;
10071}
10072
10073/*
10074** Add an SQL statement to the analysis.
10075*/
10076int sqlite3_expert_sql(
10077  sqlite3expert *p,               /* From sqlite3_expert_new() */
10078  const char *zSql,               /* SQL statement to add */
10079  char **pzErr                    /* OUT: Error message (if any) */
10080){
10081  IdxScan *pScanOrig = p->pScan;
10082  IdxStatement *pStmtOrig = p->pStatement;
10083  int rc = SQLITE_OK;
10084  const char *zStmt = zSql;
10085
10086  if( p->bRun ) return SQLITE_MISUSE;
10087
10088  while( rc==SQLITE_OK && zStmt && zStmt[0] ){
10089    sqlite3_stmt *pStmt = 0;
10090    rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
10091    if( rc==SQLITE_OK ){
10092      if( pStmt ){
10093        IdxStatement *pNew;
10094        const char *z = sqlite3_sql(pStmt);
10095        int n = STRLEN(z);
10096        pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
10097        if( rc==SQLITE_OK ){
10098          pNew->zSql = (char*)&pNew[1];
10099          memcpy(pNew->zSql, z, n+1);
10100          pNew->pNext = p->pStatement;
10101          if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
10102          p->pStatement = pNew;
10103        }
10104        sqlite3_finalize(pStmt);
10105      }
10106    }else{
10107      idxDatabaseError(p->dbv, pzErr);
10108    }
10109  }
10110
10111  if( rc!=SQLITE_OK ){
10112    idxScanFree(p->pScan, pScanOrig);
10113    idxStatementFree(p->pStatement, pStmtOrig);
10114    p->pScan = pScanOrig;
10115    p->pStatement = pStmtOrig;
10116  }
10117
10118  return rc;
10119}
10120
10121int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
10122  int rc;
10123  IdxHashEntry *pEntry;
10124
10125  /* Do trigger processing to collect any extra IdxScan structures */
10126  rc = idxProcessTriggers(p, pzErr);
10127
10128  /* Create candidate indexes within the in-memory database file */
10129  if( rc==SQLITE_OK ){
10130    rc = idxCreateCandidates(p);
10131  }
10132
10133  /* Generate the stat1 data */
10134  if( rc==SQLITE_OK ){
10135    rc = idxPopulateStat1(p, pzErr);
10136  }
10137
10138  /* Formulate the EXPERT_REPORT_CANDIDATES text */
10139  for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10140    p->zCandidates = idxAppendText(&rc, p->zCandidates,
10141        "%s;%s%s\n", pEntry->zVal,
10142        pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
10143    );
10144  }
10145
10146  /* Figure out which of the candidate indexes are preferred by the query
10147  ** planner and report the results to the user.  */
10148  if( rc==SQLITE_OK ){
10149    rc = idxFindIndexes(p, pzErr);
10150  }
10151
10152  if( rc==SQLITE_OK ){
10153    p->bRun = 1;
10154  }
10155  return rc;
10156}
10157
10158/*
10159** Return the total number of statements that have been added to this
10160** sqlite3expert using sqlite3_expert_sql().
10161*/
10162int sqlite3_expert_count(sqlite3expert *p){
10163  int nRet = 0;
10164  if( p->pStatement ) nRet = p->pStatement->iId+1;
10165  return nRet;
10166}
10167
10168/*
10169** Return a component of the report.
10170*/
10171const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
10172  const char *zRet = 0;
10173  IdxStatement *pStmt;
10174
10175  if( p->bRun==0 ) return 0;
10176  for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
10177  switch( eReport ){
10178    case EXPERT_REPORT_SQL:
10179      if( pStmt ) zRet = pStmt->zSql;
10180      break;
10181    case EXPERT_REPORT_INDEXES:
10182      if( pStmt ) zRet = pStmt->zIdx;
10183      break;
10184    case EXPERT_REPORT_PLAN:
10185      if( pStmt ) zRet = pStmt->zEQP;
10186      break;
10187    case EXPERT_REPORT_CANDIDATES:
10188      zRet = p->zCandidates;
10189      break;
10190  }
10191  return zRet;
10192}
10193
10194/*
10195** Free an sqlite3expert object.
10196*/
10197void sqlite3_expert_destroy(sqlite3expert *p){
10198  if( p ){
10199    sqlite3_close(p->dbm);
10200    sqlite3_close(p->dbv);
10201    idxScanFree(p->pScan, 0);
10202    idxStatementFree(p->pStatement, 0);
10203    idxTableFree(p->pTable);
10204    idxWriteFree(p->pWrite);
10205    idxHashClear(&p->hIdx);
10206    sqlite3_free(p->zCandidates);
10207    sqlite3_free(p);
10208  }
10209}
10210
10211#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10212
10213/************************* End ../ext/expert/sqlite3expert.c ********************/
10214
10215#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
10216/************************* Begin ../ext/misc/dbdata.c ******************/
10217/*
10218** 2019-04-17
10219**
10220** The author disclaims copyright to this source code.  In place of
10221** a legal notice, here is a blessing:
10222**
10223**    May you do good and not evil.
10224**    May you find forgiveness for yourself and forgive others.
10225**    May you share freely, never taking more than you give.
10226**
10227******************************************************************************
10228**
10229** This file contains an implementation of two eponymous virtual tables,
10230** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
10231** "sqlite_dbpage" eponymous virtual table be available.
10232**
10233** SQLITE_DBDATA:
10234**   sqlite_dbdata is used to extract data directly from a database b-tree
10235**   page and its associated overflow pages, bypassing the b-tree layer.
10236**   The table schema is equivalent to:
10237**
10238**     CREATE TABLE sqlite_dbdata(
10239**       pgno INTEGER,
10240**       cell INTEGER,
10241**       field INTEGER,
10242**       value ANY,
10243**       schema TEXT HIDDEN
10244**     );
10245**
10246**   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
10247**   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
10248**   "schema".
10249**
10250**   Each page of the database is inspected. If it cannot be interpreted as
10251**   a b-tree page, or if it is a b-tree page containing 0 entries, the
10252**   sqlite_dbdata table contains no rows for that page.  Otherwise, the
10253**   table contains one row for each field in the record associated with
10254**   each cell on the page. For intkey b-trees, the key value is stored in
10255**   field -1.
10256**
10257**   For example, for the database:
10258**
10259**     CREATE TABLE t1(a, b);     -- root page is page 2
10260**     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
10261**     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
10262**
10263**   the sqlite_dbdata table contains, as well as from entries related to
10264**   page 1, content equivalent to:
10265**
10266**     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
10267**         (2, 0, -1, 5     ),
10268**         (2, 0,  0, 'v'   ),
10269**         (2, 0,  1, 'five'),
10270**         (2, 1, -1, 10    ),
10271**         (2, 1,  0, 'x'   ),
10272**         (2, 1,  1, 'ten' );
10273**
10274**   If database corruption is encountered, this module does not report an
10275**   error. Instead, it attempts to extract as much data as possible and
10276**   ignores the corruption.
10277**
10278** SQLITE_DBPTR:
10279**   The sqlite_dbptr table has the following schema:
10280**
10281**     CREATE TABLE sqlite_dbptr(
10282**       pgno INTEGER,
10283**       child INTEGER,
10284**       schema TEXT HIDDEN
10285**     );
10286**
10287**   It contains one entry for each b-tree pointer between a parent and
10288**   child page in the database.
10289*/
10290#if !defined(SQLITEINT_H)
10291/* #include "sqlite3ext.h" */
10292
10293/* typedef unsigned char u8; */
10294
10295#endif
10296SQLITE_EXTENSION_INIT1
10297#include <string.h>
10298#include <assert.h>
10299
10300#define DBDATA_PADDING_BYTES 100
10301
10302typedef struct DbdataTable DbdataTable;
10303typedef struct DbdataCursor DbdataCursor;
10304
10305/* Cursor object */
10306struct DbdataCursor {
10307  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
10308  sqlite3_stmt *pStmt;            /* For fetching database pages */
10309
10310  int iPgno;                      /* Current page number */
10311  u8 *aPage;                      /* Buffer containing page */
10312  int nPage;                      /* Size of aPage[] in bytes */
10313  int nCell;                      /* Number of cells on aPage[] */
10314  int iCell;                      /* Current cell number */
10315  int bOnePage;                   /* True to stop after one page */
10316  int szDb;
10317  sqlite3_int64 iRowid;
10318
10319  /* Only for the sqlite_dbdata table */
10320  u8 *pRec;                       /* Buffer containing current record */
10321  int nRec;                       /* Size of pRec[] in bytes */
10322  int nHdr;                       /* Size of header in bytes */
10323  int iField;                     /* Current field number */
10324  u8 *pHdrPtr;
10325  u8 *pPtr;
10326
10327  sqlite3_int64 iIntkey;          /* Integer key value */
10328};
10329
10330/* Table object */
10331struct DbdataTable {
10332  sqlite3_vtab base;              /* Base class.  Must be first */
10333  sqlite3 *db;                    /* The database connection */
10334  sqlite3_stmt *pStmt;            /* For fetching database pages */
10335  int bPtr;                       /* True for sqlite3_dbptr table */
10336};
10337
10338/* Column and schema definitions for sqlite_dbdata */
10339#define DBDATA_COLUMN_PGNO        0
10340#define DBDATA_COLUMN_CELL        1
10341#define DBDATA_COLUMN_FIELD       2
10342#define DBDATA_COLUMN_VALUE       3
10343#define DBDATA_COLUMN_SCHEMA      4
10344#define DBDATA_SCHEMA             \
10345      "CREATE TABLE x("           \
10346      "  pgno INTEGER,"           \
10347      "  cell INTEGER,"           \
10348      "  field INTEGER,"          \
10349      "  value ANY,"              \
10350      "  schema TEXT HIDDEN"      \
10351      ")"
10352
10353/* Column and schema definitions for sqlite_dbptr */
10354#define DBPTR_COLUMN_PGNO         0
10355#define DBPTR_COLUMN_CHILD        1
10356#define DBPTR_COLUMN_SCHEMA       2
10357#define DBPTR_SCHEMA              \
10358      "CREATE TABLE x("           \
10359      "  pgno INTEGER,"           \
10360      "  child INTEGER,"          \
10361      "  schema TEXT HIDDEN"      \
10362      ")"
10363
10364/*
10365** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
10366** table.
10367*/
10368static int dbdataConnect(
10369  sqlite3 *db,
10370  void *pAux,
10371  int argc, const char *const*argv,
10372  sqlite3_vtab **ppVtab,
10373  char **pzErr
10374){
10375  DbdataTable *pTab = 0;
10376  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
10377
10378  if( rc==SQLITE_OK ){
10379    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
10380    if( pTab==0 ){
10381      rc = SQLITE_NOMEM;
10382    }else{
10383      memset(pTab, 0, sizeof(DbdataTable));
10384      pTab->db = db;
10385      pTab->bPtr = (pAux!=0);
10386    }
10387  }
10388
10389  *ppVtab = (sqlite3_vtab*)pTab;
10390  return rc;
10391}
10392
10393/*
10394** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
10395*/
10396static int dbdataDisconnect(sqlite3_vtab *pVtab){
10397  DbdataTable *pTab = (DbdataTable*)pVtab;
10398  if( pTab ){
10399    sqlite3_finalize(pTab->pStmt);
10400    sqlite3_free(pVtab);
10401  }
10402  return SQLITE_OK;
10403}
10404
10405/*
10406** This function interprets two types of constraints:
10407**
10408**       schema=?
10409**       pgno=?
10410**
10411** If neither are present, idxNum is set to 0. If schema=? is present,
10412** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
10413** in idxNum is set.
10414**
10415** If both parameters are present, schema is in position 0 and pgno in
10416** position 1.
10417*/
10418static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
10419  DbdataTable *pTab = (DbdataTable*)tab;
10420  int i;
10421  int iSchema = -1;
10422  int iPgno = -1;
10423  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
10424
10425  for(i=0; i<pIdx->nConstraint; i++){
10426    struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
10427    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10428      if( p->iColumn==colSchema ){
10429        if( p->usable==0 ) return SQLITE_CONSTRAINT;
10430        iSchema = i;
10431      }
10432      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
10433        iPgno = i;
10434      }
10435    }
10436  }
10437
10438  if( iSchema>=0 ){
10439    pIdx->aConstraintUsage[iSchema].argvIndex = 1;
10440    pIdx->aConstraintUsage[iSchema].omit = 1;
10441  }
10442  if( iPgno>=0 ){
10443    pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
10444    pIdx->aConstraintUsage[iPgno].omit = 1;
10445    pIdx->estimatedCost = 100;
10446    pIdx->estimatedRows =  50;
10447
10448    if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
10449      int iCol = pIdx->aOrderBy[0].iColumn;
10450      if( pIdx->nOrderBy==1 ){
10451        pIdx->orderByConsumed = (iCol==0 || iCol==1);
10452      }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
10453        pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
10454      }
10455    }
10456
10457  }else{
10458    pIdx->estimatedCost = 100000000;
10459    pIdx->estimatedRows = 1000000000;
10460  }
10461  pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
10462  return SQLITE_OK;
10463}
10464
10465/*
10466** Open a new sqlite_dbdata or sqlite_dbptr cursor.
10467*/
10468static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
10469  DbdataCursor *pCsr;
10470
10471  pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
10472  if( pCsr==0 ){
10473    return SQLITE_NOMEM;
10474  }else{
10475    memset(pCsr, 0, sizeof(DbdataCursor));
10476    pCsr->base.pVtab = pVTab;
10477  }
10478
10479  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
10480  return SQLITE_OK;
10481}
10482
10483/*
10484** Restore a cursor object to the state it was in when first allocated
10485** by dbdataOpen().
10486*/
10487static void dbdataResetCursor(DbdataCursor *pCsr){
10488  DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
10489  if( pTab->pStmt==0 ){
10490    pTab->pStmt = pCsr->pStmt;
10491  }else{
10492    sqlite3_finalize(pCsr->pStmt);
10493  }
10494  pCsr->pStmt = 0;
10495  pCsr->iPgno = 1;
10496  pCsr->iCell = 0;
10497  pCsr->iField = 0;
10498  pCsr->bOnePage = 0;
10499  sqlite3_free(pCsr->aPage);
10500  sqlite3_free(pCsr->pRec);
10501  pCsr->pRec = 0;
10502  pCsr->aPage = 0;
10503}
10504
10505/*
10506** Close an sqlite_dbdata or sqlite_dbptr cursor.
10507*/
10508static int dbdataClose(sqlite3_vtab_cursor *pCursor){
10509  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10510  dbdataResetCursor(pCsr);
10511  sqlite3_free(pCsr);
10512  return SQLITE_OK;
10513}
10514
10515/*
10516** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
10517*/
10518static unsigned int get_uint16(unsigned char *a){
10519  return (a[0]<<8)|a[1];
10520}
10521static unsigned int get_uint32(unsigned char *a){
10522  return ((unsigned int)a[0]<<24)
10523       | ((unsigned int)a[1]<<16)
10524       | ((unsigned int)a[2]<<8)
10525       | ((unsigned int)a[3]);
10526}
10527
10528/*
10529** Load page pgno from the database via the sqlite_dbpage virtual table.
10530** If successful, set (*ppPage) to point to a buffer containing the page
10531** data, (*pnPage) to the size of that buffer in bytes and return
10532** SQLITE_OK. In this case it is the responsibility of the caller to
10533** eventually free the buffer using sqlite3_free().
10534**
10535** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
10536** return an SQLite error code.
10537*/
10538static int dbdataLoadPage(
10539  DbdataCursor *pCsr,             /* Cursor object */
10540  unsigned int pgno,              /* Page number of page to load */
10541  u8 **ppPage,                    /* OUT: pointer to page buffer */
10542  int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
10543){
10544  int rc2;
10545  int rc = SQLITE_OK;
10546  sqlite3_stmt *pStmt = pCsr->pStmt;
10547
10548  *ppPage = 0;
10549  *pnPage = 0;
10550  sqlite3_bind_int64(pStmt, 2, pgno);
10551  if( SQLITE_ROW==sqlite3_step(pStmt) ){
10552    int nCopy = sqlite3_column_bytes(pStmt, 0);
10553    if( nCopy>0 ){
10554      u8 *pPage;
10555      pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
10556      if( pPage==0 ){
10557        rc = SQLITE_NOMEM;
10558      }else{
10559        const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
10560        memcpy(pPage, pCopy, nCopy);
10561        memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
10562      }
10563      *ppPage = pPage;
10564      *pnPage = nCopy;
10565    }
10566  }
10567  rc2 = sqlite3_reset(pStmt);
10568  if( rc==SQLITE_OK ) rc = rc2;
10569
10570  return rc;
10571}
10572
10573/*
10574** Read a varint.  Put the value in *pVal and return the number of bytes.
10575*/
10576static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
10577  sqlite3_int64 v = 0;
10578  int i;
10579  for(i=0; i<8; i++){
10580    v = (v<<7) + (z[i]&0x7f);
10581    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
10582  }
10583  v = (v<<8) + (z[i]&0xff);
10584  *pVal = v;
10585  return 9;
10586}
10587
10588/*
10589** Return the number of bytes of space used by an SQLite value of type
10590** eType.
10591*/
10592static int dbdataValueBytes(int eType){
10593  switch( eType ){
10594    case 0: case 8: case 9:
10595    case 10: case 11:
10596      return 0;
10597    case 1:
10598      return 1;
10599    case 2:
10600      return 2;
10601    case 3:
10602      return 3;
10603    case 4:
10604      return 4;
10605    case 5:
10606      return 6;
10607    case 6:
10608    case 7:
10609      return 8;
10610    default:
10611      if( eType>0 ){
10612        return ((eType-12) / 2);
10613      }
10614      return 0;
10615  }
10616}
10617
10618/*
10619** Load a value of type eType from buffer pData and use it to set the
10620** result of context object pCtx.
10621*/
10622static void dbdataValue(
10623  sqlite3_context *pCtx,
10624  int eType,
10625  u8 *pData,
10626  int nData
10627){
10628  if( eType>=0 && dbdataValueBytes(eType)<=nData ){
10629    switch( eType ){
10630      case 0:
10631      case 10:
10632      case 11:
10633        sqlite3_result_null(pCtx);
10634        break;
10635
10636      case 8:
10637        sqlite3_result_int(pCtx, 0);
10638        break;
10639      case 9:
10640        sqlite3_result_int(pCtx, 1);
10641        break;
10642
10643      case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
10644        sqlite3_uint64 v = (signed char)pData[0];
10645        pData++;
10646        switch( eType ){
10647          case 7:
10648          case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
10649          case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
10650          case 4:  v = (v<<8) + pData[0];  pData++;
10651          case 3:  v = (v<<8) + pData[0];  pData++;
10652          case 2:  v = (v<<8) + pData[0];  pData++;
10653        }
10654
10655        if( eType==7 ){
10656          double r;
10657          memcpy(&r, &v, sizeof(r));
10658          sqlite3_result_double(pCtx, r);
10659        }else{
10660          sqlite3_result_int64(pCtx, (sqlite3_int64)v);
10661        }
10662        break;
10663      }
10664
10665      default: {
10666        int n = ((eType-12) / 2);
10667        if( eType % 2 ){
10668          sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
10669        }else{
10670          sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
10671        }
10672      }
10673    }
10674  }
10675}
10676
10677/*
10678** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
10679*/
10680static int dbdataNext(sqlite3_vtab_cursor *pCursor){
10681  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10682  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10683
10684  pCsr->iRowid++;
10685  while( 1 ){
10686    int rc;
10687    int iOff = (pCsr->iPgno==1 ? 100 : 0);
10688    int bNextPage = 0;
10689
10690    if( pCsr->aPage==0 ){
10691      while( 1 ){
10692        if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
10693        rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
10694        if( rc!=SQLITE_OK ) return rc;
10695        if( pCsr->aPage ) break;
10696        pCsr->iPgno++;
10697      }
10698      pCsr->iCell = pTab->bPtr ? -2 : 0;
10699      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
10700    }
10701
10702    if( pTab->bPtr ){
10703      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
10704        pCsr->iCell = pCsr->nCell;
10705      }
10706      pCsr->iCell++;
10707      if( pCsr->iCell>=pCsr->nCell ){
10708        sqlite3_free(pCsr->aPage);
10709        pCsr->aPage = 0;
10710        if( pCsr->bOnePage ) return SQLITE_OK;
10711        pCsr->iPgno++;
10712      }else{
10713        return SQLITE_OK;
10714      }
10715    }else{
10716      /* If there is no record loaded, load it now. */
10717      if( pCsr->pRec==0 ){
10718        int bHasRowid = 0;
10719        int nPointer = 0;
10720        sqlite3_int64 nPayload = 0;
10721        sqlite3_int64 nHdr = 0;
10722        int iHdr;
10723        int U, X;
10724        int nLocal;
10725
10726        switch( pCsr->aPage[iOff] ){
10727          case 0x02:
10728            nPointer = 4;
10729            break;
10730          case 0x0a:
10731            break;
10732          case 0x0d:
10733            bHasRowid = 1;
10734            break;
10735          default:
10736            /* This is not a b-tree page with records on it. Continue. */
10737            pCsr->iCell = pCsr->nCell;
10738            break;
10739        }
10740
10741        if( pCsr->iCell>=pCsr->nCell ){
10742          bNextPage = 1;
10743        }else{
10744
10745          iOff += 8 + nPointer + pCsr->iCell*2;
10746          if( iOff>pCsr->nPage ){
10747            bNextPage = 1;
10748          }else{
10749            iOff = get_uint16(&pCsr->aPage[iOff]);
10750          }
10751
10752          /* For an interior node cell, skip past the child-page number */
10753          iOff += nPointer;
10754
10755          /* Load the "byte of payload including overflow" field */
10756          if( bNextPage || iOff>pCsr->nPage ){
10757            bNextPage = 1;
10758          }else{
10759            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
10760          }
10761
10762          /* If this is a leaf intkey cell, load the rowid */
10763          if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
10764            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
10765          }
10766
10767          /* Figure out how much data to read from the local page */
10768          U = pCsr->nPage;
10769          if( bHasRowid ){
10770            X = U-35;
10771          }else{
10772            X = ((U-12)*64/255)-23;
10773          }
10774          if( nPayload<=X ){
10775            nLocal = nPayload;
10776          }else{
10777            int M, K;
10778            M = ((U-12)*32/255)-23;
10779            K = M+((nPayload-M)%(U-4));
10780            if( K<=X ){
10781              nLocal = K;
10782            }else{
10783              nLocal = M;
10784            }
10785          }
10786
10787          if( bNextPage || nLocal+iOff>pCsr->nPage ){
10788            bNextPage = 1;
10789          }else{
10790
10791            /* Allocate space for payload. And a bit more to catch small buffer
10792            ** overruns caused by attempting to read a varint or similar from
10793            ** near the end of a corrupt record.  */
10794            pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
10795            if( pCsr->pRec==0 ) return SQLITE_NOMEM;
10796            memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
10797            pCsr->nRec = nPayload;
10798
10799            /* Load the nLocal bytes of payload */
10800            memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
10801            iOff += nLocal;
10802
10803            /* Load content from overflow pages */
10804            if( nPayload>nLocal ){
10805              sqlite3_int64 nRem = nPayload - nLocal;
10806              unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
10807              while( nRem>0 ){
10808                u8 *aOvfl = 0;
10809                int nOvfl = 0;
10810                int nCopy;
10811                rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
10812                assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
10813                if( rc!=SQLITE_OK ) return rc;
10814                if( aOvfl==0 ) break;
10815
10816                nCopy = U-4;
10817                if( nCopy>nRem ) nCopy = nRem;
10818                memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
10819                nRem -= nCopy;
10820
10821                pgnoOvfl = get_uint32(aOvfl);
10822                sqlite3_free(aOvfl);
10823              }
10824            }
10825
10826            iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
10827            pCsr->nHdr = nHdr;
10828            pCsr->pHdrPtr = &pCsr->pRec[iHdr];
10829            pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
10830            pCsr->iField = (bHasRowid ? -1 : 0);
10831          }
10832        }
10833      }else{
10834        pCsr->iField++;
10835        if( pCsr->iField>0 ){
10836          sqlite3_int64 iType;
10837          if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
10838            bNextPage = 1;
10839          }else{
10840            pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
10841            pCsr->pPtr += dbdataValueBytes(iType);
10842          }
10843        }
10844      }
10845
10846      if( bNextPage ){
10847        sqlite3_free(pCsr->aPage);
10848        sqlite3_free(pCsr->pRec);
10849        pCsr->aPage = 0;
10850        pCsr->pRec = 0;
10851        if( pCsr->bOnePage ) return SQLITE_OK;
10852        pCsr->iPgno++;
10853      }else{
10854        if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
10855          return SQLITE_OK;
10856        }
10857
10858        /* Advance to the next cell. The next iteration of the loop will load
10859        ** the record and so on. */
10860        sqlite3_free(pCsr->pRec);
10861        pCsr->pRec = 0;
10862        pCsr->iCell++;
10863      }
10864    }
10865  }
10866
10867  assert( !"can't get here" );
10868  return SQLITE_OK;
10869}
10870
10871/*
10872** Return true if the cursor is at EOF.
10873*/
10874static int dbdataEof(sqlite3_vtab_cursor *pCursor){
10875  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10876  return pCsr->aPage==0;
10877}
10878
10879/*
10880** Determine the size in pages of database zSchema (where zSchema is
10881** "main", "temp" or the name of an attached database) and set
10882** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
10883** an SQLite error code.
10884*/
10885static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
10886  DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
10887  char *zSql = 0;
10888  int rc, rc2;
10889  sqlite3_stmt *pStmt = 0;
10890
10891  zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
10892  if( zSql==0 ) return SQLITE_NOMEM;
10893  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
10894  sqlite3_free(zSql);
10895  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
10896    pCsr->szDb = sqlite3_column_int(pStmt, 0);
10897  }
10898  rc2 = sqlite3_finalize(pStmt);
10899  if( rc==SQLITE_OK ) rc = rc2;
10900  return rc;
10901}
10902
10903/*
10904** xFilter method for sqlite_dbdata and sqlite_dbptr.
10905*/
10906static int dbdataFilter(
10907  sqlite3_vtab_cursor *pCursor,
10908  int idxNum, const char *idxStr,
10909  int argc, sqlite3_value **argv
10910){
10911  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10912  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10913  int rc = SQLITE_OK;
10914  const char *zSchema = "main";
10915
10916  dbdataResetCursor(pCsr);
10917  assert( pCsr->iPgno==1 );
10918  if( idxNum & 0x01 ){
10919    zSchema = (const char*)sqlite3_value_text(argv[0]);
10920  }
10921  if( idxNum & 0x02 ){
10922    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
10923    pCsr->bOnePage = 1;
10924  }else{
10925    pCsr->nPage = dbdataDbsize(pCsr, zSchema);
10926    rc = dbdataDbsize(pCsr, zSchema);
10927  }
10928
10929  if( rc==SQLITE_OK ){
10930    if( pTab->pStmt ){
10931      pCsr->pStmt = pTab->pStmt;
10932      pTab->pStmt = 0;
10933    }else{
10934      rc = sqlite3_prepare_v2(pTab->db,
10935          "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
10936          &pCsr->pStmt, 0
10937      );
10938    }
10939  }
10940  if( rc==SQLITE_OK ){
10941    rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
10942  }else{
10943    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
10944  }
10945  if( rc==SQLITE_OK ){
10946    rc = dbdataNext(pCursor);
10947  }
10948  return rc;
10949}
10950
10951/*
10952** Return a column for the sqlite_dbdata or sqlite_dbptr table.
10953*/
10954static int dbdataColumn(
10955  sqlite3_vtab_cursor *pCursor,
10956  sqlite3_context *ctx,
10957  int i
10958){
10959  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10960  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10961  if( pTab->bPtr ){
10962    switch( i ){
10963      case DBPTR_COLUMN_PGNO:
10964        sqlite3_result_int64(ctx, pCsr->iPgno);
10965        break;
10966      case DBPTR_COLUMN_CHILD: {
10967        int iOff = pCsr->iPgno==1 ? 100 : 0;
10968        if( pCsr->iCell<0 ){
10969          iOff += 8;
10970        }else{
10971          iOff += 12 + pCsr->iCell*2;
10972          if( iOff>pCsr->nPage ) return SQLITE_OK;
10973          iOff = get_uint16(&pCsr->aPage[iOff]);
10974        }
10975        if( iOff<=pCsr->nPage ){
10976          sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
10977        }
10978        break;
10979      }
10980    }
10981  }else{
10982    switch( i ){
10983      case DBDATA_COLUMN_PGNO:
10984        sqlite3_result_int64(ctx, pCsr->iPgno);
10985        break;
10986      case DBDATA_COLUMN_CELL:
10987        sqlite3_result_int(ctx, pCsr->iCell);
10988        break;
10989      case DBDATA_COLUMN_FIELD:
10990        sqlite3_result_int(ctx, pCsr->iField);
10991        break;
10992      case DBDATA_COLUMN_VALUE: {
10993        if( pCsr->iField<0 ){
10994          sqlite3_result_int64(ctx, pCsr->iIntkey);
10995        }else{
10996          sqlite3_int64 iType;
10997          dbdataGetVarint(pCsr->pHdrPtr, &iType);
10998          dbdataValue(
10999              ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11000          );
11001        }
11002        break;
11003      }
11004    }
11005  }
11006  return SQLITE_OK;
11007}
11008
11009/*
11010** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11011*/
11012static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11013  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11014  *pRowid = pCsr->iRowid;
11015  return SQLITE_OK;
11016}
11017
11018
11019/*
11020** Invoke this routine to register the "sqlite_dbdata" virtual table module
11021*/
11022static int sqlite3DbdataRegister(sqlite3 *db){
11023  static sqlite3_module dbdata_module = {
11024    0,                            /* iVersion */
11025    0,                            /* xCreate */
11026    dbdataConnect,                /* xConnect */
11027    dbdataBestIndex,              /* xBestIndex */
11028    dbdataDisconnect,             /* xDisconnect */
11029    0,                            /* xDestroy */
11030    dbdataOpen,                   /* xOpen - open a cursor */
11031    dbdataClose,                  /* xClose - close a cursor */
11032    dbdataFilter,                 /* xFilter - configure scan constraints */
11033    dbdataNext,                   /* xNext - advance a cursor */
11034    dbdataEof,                    /* xEof - check for end of scan */
11035    dbdataColumn,                 /* xColumn - read data */
11036    dbdataRowid,                  /* xRowid - read data */
11037    0,                            /* xUpdate */
11038    0,                            /* xBegin */
11039    0,                            /* xSync */
11040    0,                            /* xCommit */
11041    0,                            /* xRollback */
11042    0,                            /* xFindMethod */
11043    0,                            /* xRename */
11044    0,                            /* xSavepoint */
11045    0,                            /* xRelease */
11046    0,                            /* xRollbackTo */
11047    0                             /* xShadowName */
11048  };
11049
11050  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11051  if( rc==SQLITE_OK ){
11052    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11053  }
11054  return rc;
11055}
11056
11057#ifdef _WIN32
11058
11059#endif
11060int sqlite3_dbdata_init(
11061  sqlite3 *db,
11062  char **pzErrMsg,
11063  const sqlite3_api_routines *pApi
11064){
11065  SQLITE_EXTENSION_INIT2(pApi);
11066  return sqlite3DbdataRegister(db);
11067}
11068
11069/************************* End ../ext/misc/dbdata.c ********************/
11070#endif
11071
11072#if defined(SQLITE_ENABLE_SESSION)
11073/*
11074** State information for a single open session
11075*/
11076typedef struct OpenSession OpenSession;
11077struct OpenSession {
11078  char *zName;             /* Symbolic name for this session */
11079  int nFilter;             /* Number of xFilter rejection GLOB patterns */
11080  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
11081  sqlite3_session *p;      /* The open session */
11082};
11083#endif
11084
11085typedef struct ExpertInfo ExpertInfo;
11086struct ExpertInfo {
11087  sqlite3expert *pExpert;
11088  int bVerbose;
11089};
11090
11091/* A single line in the EQP output */
11092typedef struct EQPGraphRow EQPGraphRow;
11093struct EQPGraphRow {
11094  int iEqpId;           /* ID for this row */
11095  int iParentId;        /* ID of the parent row */
11096  EQPGraphRow *pNext;   /* Next row in sequence */
11097  char zText[1];        /* Text to display for this row */
11098};
11099
11100/* All EQP output is collected into an instance of the following */
11101typedef struct EQPGraph EQPGraph;
11102struct EQPGraph {
11103  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
11104  EQPGraphRow *pLast;   /* Last element of the pRow list */
11105  char zPrefix[100];    /* Graph prefix */
11106};
11107
11108/*
11109** State information about the database connection is contained in an
11110** instance of the following structure.
11111*/
11112typedef struct ShellState ShellState;
11113struct ShellState {
11114  sqlite3 *db;           /* The database */
11115  u8 autoExplain;        /* Automatically turn on .explain mode */
11116  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
11117  u8 autoEQPtest;        /* autoEQP is in test mode */
11118  u8 autoEQPtrace;       /* autoEQP is in trace mode */
11119  u8 statsOn;            /* True to display memory stats before each finalize */
11120  u8 scanstatsOn;        /* True to display scan stats before each finalize */
11121  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
11122  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
11123  u8 nEqpLevel;          /* Depth of the EQP output graph */
11124  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
11125  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
11126  int outCount;          /* Revert to stdout when reaching zero */
11127  int cnt;               /* Number of records displayed so far */
11128  int lineno;            /* Line number of last line read from in */
11129  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
11130  FILE *in;              /* Read commands from this stream */
11131  FILE *out;             /* Write results here */
11132  FILE *traceOut;        /* Output for sqlite3_trace() */
11133  int nErr;              /* Number of errors seen */
11134  int mode;              /* An output mode setting */
11135  int modePrior;         /* Saved mode */
11136  int cMode;             /* temporary output mode for the current query */
11137  int normalMode;        /* Output mode before ".explain on" */
11138  int writableSchema;    /* True if PRAGMA writable_schema=ON */
11139  int showHeader;        /* True to show column names in List or Column mode */
11140  int nCheck;            /* Number of ".check" commands run */
11141  unsigned nProgress;    /* Number of progress callbacks encountered */
11142  unsigned mxProgress;   /* Maximum progress callbacks before failing */
11143  unsigned flgProgress;  /* Flags for the progress callback */
11144  unsigned shellFlgs;    /* Various flags */
11145  unsigned priorShFlgs;  /* Saved copy of flags */
11146  sqlite3_int64 szMax;   /* --maxsize argument to .open */
11147  char *zDestTable;      /* Name of destination table when MODE_Insert */
11148  char *zTempFile;       /* Temporary file that might need deleting */
11149  char zTestcase[30];    /* Name of current test case */
11150  char colSeparator[20]; /* Column separator character for several modes */
11151  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
11152  char colSepPrior[20];  /* Saved column separator */
11153  char rowSepPrior[20];  /* Saved row separator */
11154  int *colWidth;         /* Requested width of each column in columnar modes */
11155  int *actualWidth;      /* Actual width of each column */
11156  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
11157  char nullValue[20];    /* The text to print when a NULL comes back from
11158                         ** the database */
11159  char outfile[FILENAME_MAX]; /* Filename for *out */
11160  const char *zDbFilename;    /* name of the database file */
11161  char *zFreeOnClose;         /* Filename to free when closing */
11162  const char *zVfs;           /* Name of VFS to use */
11163  sqlite3_stmt *pStmt;   /* Current statement if any. */
11164  FILE *pLog;            /* Write log output here */
11165  int *aiIndent;         /* Array of indents used in MODE_Explain */
11166  int nIndent;           /* Size of array aiIndent[] */
11167  int iIndent;           /* Index of current op in aiIndent[] */
11168  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
11169#if defined(SQLITE_ENABLE_SESSION)
11170  int nSession;             /* Number of active sessions */
11171  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
11172#endif
11173  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
11174};
11175
11176
11177/* Allowed values for ShellState.autoEQP
11178*/
11179#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
11180#define AUTOEQP_on       1           /* Automatic EQP is on */
11181#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
11182#define AUTOEQP_full     3           /* Show full EXPLAIN */
11183
11184/* Allowed values for ShellState.openMode
11185*/
11186#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
11187#define SHELL_OPEN_NORMAL      1      /* Normal database file */
11188#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
11189#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
11190#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
11191#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
11192#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
11193
11194/* Allowed values for ShellState.eTraceType
11195*/
11196#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
11197#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
11198#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
11199
11200/* Bits in the ShellState.flgProgress variable */
11201#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
11202#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
11203                                   ** callback limit is reached, and for each
11204                                   ** top-level SQL statement */
11205#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
11206
11207/*
11208** These are the allowed shellFlgs values
11209*/
11210#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
11211#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
11212#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
11213#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
11214#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
11215#define SHFLG_CountChanges   0x00000020 /* .changes setting */
11216#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
11217#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
11218#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
11219#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
11220
11221/*
11222** Macros for testing and setting shellFlgs
11223*/
11224#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
11225#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
11226#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
11227
11228/*
11229** These are the allowed modes.
11230*/
11231#define MODE_Line     0  /* One column per line.  Blank line between records */
11232#define MODE_Column   1  /* One record per line in neat columns */
11233#define MODE_List     2  /* One record per line with a separator */
11234#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
11235#define MODE_Html     4  /* Generate an XHTML table */
11236#define MODE_Insert   5  /* Generate SQL "insert" statements */
11237#define MODE_Quote    6  /* Quote values as for SQL */
11238#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
11239#define MODE_Csv      8  /* Quote strings, numbers are plain */
11240#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
11241#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
11242#define MODE_Pretty  11  /* Pretty-print schemas */
11243#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
11244#define MODE_Json    13  /* Output JSON */
11245#define MODE_Markdown 14 /* Markdown formatting */
11246#define MODE_Table   15  /* MySQL-style table formatting */
11247#define MODE_Box     16  /* Unicode box-drawing characters */
11248
11249static const char *modeDescr[] = {
11250  "line",
11251  "column",
11252  "list",
11253  "semi",
11254  "html",
11255  "insert",
11256  "quote",
11257  "tcl",
11258  "csv",
11259  "explain",
11260  "ascii",
11261  "prettyprint",
11262  "eqp",
11263  "json",
11264  "markdown",
11265  "table",
11266  "box"
11267};
11268
11269/*
11270** These are the column/row/line separators used by the various
11271** import/export modes.
11272*/
11273#define SEP_Column    "|"
11274#define SEP_Row       "\n"
11275#define SEP_Tab       "\t"
11276#define SEP_Space     " "
11277#define SEP_Comma     ","
11278#define SEP_CrLf      "\r\n"
11279#define SEP_Unit      "\x1F"
11280#define SEP_Record    "\x1E"
11281
11282/*
11283** A callback for the sqlite3_log() interface.
11284*/
11285static void shellLog(void *pArg, int iErrCode, const char *zMsg){
11286  ShellState *p = (ShellState*)pArg;
11287  if( p->pLog==0 ) return;
11288  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
11289  fflush(p->pLog);
11290}
11291
11292/*
11293** SQL function:  shell_putsnl(X)
11294**
11295** Write the text X to the screen (or whatever output is being directed)
11296** adding a newline at the end, and then return X.
11297*/
11298static void shellPutsFunc(
11299  sqlite3_context *pCtx,
11300  int nVal,
11301  sqlite3_value **apVal
11302){
11303  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
11304  (void)nVal;
11305  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
11306  sqlite3_result_value(pCtx, apVal[0]);
11307}
11308
11309/*
11310** SQL function:   edit(VALUE)
11311**                 edit(VALUE,EDITOR)
11312**
11313** These steps:
11314**
11315**     (1) Write VALUE into a temporary file.
11316**     (2) Run program EDITOR on that temporary file.
11317**     (3) Read the temporary file back and return its content as the result.
11318**     (4) Delete the temporary file
11319**
11320** If the EDITOR argument is omitted, use the value in the VISUAL
11321** environment variable.  If still there is no EDITOR, through an error.
11322**
11323** Also throw an error if the EDITOR program returns a non-zero exit code.
11324*/
11325#ifndef SQLITE_NOHAVE_SYSTEM
11326static void editFunc(
11327  sqlite3_context *context,
11328  int argc,
11329  sqlite3_value **argv
11330){
11331  const char *zEditor;
11332  char *zTempFile = 0;
11333  sqlite3 *db;
11334  char *zCmd = 0;
11335  int bBin;
11336  int rc;
11337  int hasCRNL = 0;
11338  FILE *f = 0;
11339  sqlite3_int64 sz;
11340  sqlite3_int64 x;
11341  unsigned char *p = 0;
11342
11343  if( argc==2 ){
11344    zEditor = (const char*)sqlite3_value_text(argv[1]);
11345  }else{
11346    zEditor = getenv("VISUAL");
11347  }
11348  if( zEditor==0 ){
11349    sqlite3_result_error(context, "no editor for edit()", -1);
11350    return;
11351  }
11352  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
11353    sqlite3_result_error(context, "NULL input to edit()", -1);
11354    return;
11355  }
11356  db = sqlite3_context_db_handle(context);
11357  zTempFile = 0;
11358  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
11359  if( zTempFile==0 ){
11360    sqlite3_uint64 r = 0;
11361    sqlite3_randomness(sizeof(r), &r);
11362    zTempFile = sqlite3_mprintf("temp%llx", r);
11363    if( zTempFile==0 ){
11364      sqlite3_result_error_nomem(context);
11365      return;
11366    }
11367  }
11368  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
11369  /* When writing the file to be edited, do \n to \r\n conversions on systems
11370  ** that want \r\n line endings */
11371  f = fopen(zTempFile, bBin ? "wb" : "w");
11372  if( f==0 ){
11373    sqlite3_result_error(context, "edit() cannot open temp file", -1);
11374    goto edit_func_end;
11375  }
11376  sz = sqlite3_value_bytes(argv[0]);
11377  if( bBin ){
11378    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
11379  }else{
11380    const char *z = (const char*)sqlite3_value_text(argv[0]);
11381    /* Remember whether or not the value originally contained \r\n */
11382    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
11383    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
11384  }
11385  fclose(f);
11386  f = 0;
11387  if( x!=sz ){
11388    sqlite3_result_error(context, "edit() could not write the whole file", -1);
11389    goto edit_func_end;
11390  }
11391  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
11392  if( zCmd==0 ){
11393    sqlite3_result_error_nomem(context);
11394    goto edit_func_end;
11395  }
11396  rc = system(zCmd);
11397  sqlite3_free(zCmd);
11398  if( rc ){
11399    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
11400    goto edit_func_end;
11401  }
11402  f = fopen(zTempFile, "rb");
11403  if( f==0 ){
11404    sqlite3_result_error(context,
11405      "edit() cannot reopen temp file after edit", -1);
11406    goto edit_func_end;
11407  }
11408  fseek(f, 0, SEEK_END);
11409  sz = ftell(f);
11410  rewind(f);
11411  p = sqlite3_malloc64( sz+1 );
11412  if( p==0 ){
11413    sqlite3_result_error_nomem(context);
11414    goto edit_func_end;
11415  }
11416  x = fread(p, 1, (size_t)sz, f);
11417  fclose(f);
11418  f = 0;
11419  if( x!=sz ){
11420    sqlite3_result_error(context, "could not read back the whole file", -1);
11421    goto edit_func_end;
11422  }
11423  if( bBin ){
11424    sqlite3_result_blob64(context, p, sz, sqlite3_free);
11425  }else{
11426    sqlite3_int64 i, j;
11427    if( hasCRNL ){
11428      /* If the original contains \r\n then do no conversions back to \n */
11429      j = sz;
11430    }else{
11431      /* If the file did not originally contain \r\n then convert any new
11432      ** \r\n back into \n */
11433      for(i=j=0; i<sz; i++){
11434        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
11435        p[j++] = p[i];
11436      }
11437      sz = j;
11438      p[sz] = 0;
11439    }
11440    sqlite3_result_text64(context, (const char*)p, sz,
11441                          sqlite3_free, SQLITE_UTF8);
11442  }
11443  p = 0;
11444
11445edit_func_end:
11446  if( f ) fclose(f);
11447  unlink(zTempFile);
11448  sqlite3_free(zTempFile);
11449  sqlite3_free(p);
11450}
11451#endif /* SQLITE_NOHAVE_SYSTEM */
11452
11453/*
11454** Save or restore the current output mode
11455*/
11456static void outputModePush(ShellState *p){
11457  p->modePrior = p->mode;
11458  p->priorShFlgs = p->shellFlgs;
11459  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
11460  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
11461}
11462static void outputModePop(ShellState *p){
11463  p->mode = p->modePrior;
11464  p->shellFlgs = p->priorShFlgs;
11465  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
11466  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
11467}
11468
11469/*
11470** Output the given string as a hex-encoded blob (eg. X'1234' )
11471*/
11472static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
11473  int i;
11474  char *zBlob = (char *)pBlob;
11475  raw_printf(out,"X'");
11476  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
11477  raw_printf(out,"'");
11478}
11479
11480/*
11481** Find a string that is not found anywhere in z[].  Return a pointer
11482** to that string.
11483**
11484** Try to use zA and zB first.  If both of those are already found in z[]
11485** then make up some string and store it in the buffer zBuf.
11486*/
11487static const char *unused_string(
11488  const char *z,                    /* Result must not appear anywhere in z */
11489  const char *zA, const char *zB,   /* Try these first */
11490  char *zBuf                        /* Space to store a generated string */
11491){
11492  unsigned i = 0;
11493  if( strstr(z, zA)==0 ) return zA;
11494  if( strstr(z, zB)==0 ) return zB;
11495  do{
11496    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
11497  }while( strstr(z,zBuf)!=0 );
11498  return zBuf;
11499}
11500
11501/*
11502** Output the given string as a quoted string using SQL quoting conventions.
11503**
11504** See also: output_quoted_escaped_string()
11505*/
11506static void output_quoted_string(FILE *out, const char *z){
11507  int i;
11508  char c;
11509  setBinaryMode(out, 1);
11510  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11511  if( c==0 ){
11512    utf8_printf(out,"'%s'",z);
11513  }else{
11514    raw_printf(out, "'");
11515    while( *z ){
11516      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11517      if( c=='\'' ) i++;
11518      if( i ){
11519        utf8_printf(out, "%.*s", i, z);
11520        z += i;
11521      }
11522      if( c=='\'' ){
11523        raw_printf(out, "'");
11524        continue;
11525      }
11526      if( c==0 ){
11527        break;
11528      }
11529      z++;
11530    }
11531    raw_printf(out, "'");
11532  }
11533  setTextMode(out, 1);
11534}
11535
11536/*
11537** Output the given string as a quoted string using SQL quoting conventions.
11538** Additionallly , escape the "\n" and "\r" characters so that they do not
11539** get corrupted by end-of-line translation facilities in some operating
11540** systems.
11541**
11542** This is like output_quoted_string() but with the addition of the \r\n
11543** escape mechanism.
11544*/
11545static void output_quoted_escaped_string(FILE *out, const char *z){
11546  int i;
11547  char c;
11548  setBinaryMode(out, 1);
11549  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
11550  if( c==0 ){
11551    utf8_printf(out,"'%s'",z);
11552  }else{
11553    const char *zNL = 0;
11554    const char *zCR = 0;
11555    int nNL = 0;
11556    int nCR = 0;
11557    char zBuf1[20], zBuf2[20];
11558    for(i=0; z[i]; i++){
11559      if( z[i]=='\n' ) nNL++;
11560      if( z[i]=='\r' ) nCR++;
11561    }
11562    if( nNL ){
11563      raw_printf(out, "replace(");
11564      zNL = unused_string(z, "\\n", "\\012", zBuf1);
11565    }
11566    if( nCR ){
11567      raw_printf(out, "replace(");
11568      zCR = unused_string(z, "\\r", "\\015", zBuf2);
11569    }
11570    raw_printf(out, "'");
11571    while( *z ){
11572      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
11573      if( c=='\'' ) i++;
11574      if( i ){
11575        utf8_printf(out, "%.*s", i, z);
11576        z += i;
11577      }
11578      if( c=='\'' ){
11579        raw_printf(out, "'");
11580        continue;
11581      }
11582      if( c==0 ){
11583        break;
11584      }
11585      z++;
11586      if( c=='\n' ){
11587        raw_printf(out, "%s", zNL);
11588        continue;
11589      }
11590      raw_printf(out, "%s", zCR);
11591    }
11592    raw_printf(out, "'");
11593    if( nCR ){
11594      raw_printf(out, ",'%s',char(13))", zCR);
11595    }
11596    if( nNL ){
11597      raw_printf(out, ",'%s',char(10))", zNL);
11598    }
11599  }
11600  setTextMode(out, 1);
11601}
11602
11603/*
11604** Output the given string as a quoted according to C or TCL quoting rules.
11605*/
11606static void output_c_string(FILE *out, const char *z){
11607  unsigned int c;
11608  fputc('"', out);
11609  while( (c = *(z++))!=0 ){
11610    if( c=='\\' ){
11611      fputc(c, out);
11612      fputc(c, out);
11613    }else if( c=='"' ){
11614      fputc('\\', out);
11615      fputc('"', out);
11616    }else if( c=='\t' ){
11617      fputc('\\', out);
11618      fputc('t', out);
11619    }else if( c=='\n' ){
11620      fputc('\\', out);
11621      fputc('n', out);
11622    }else if( c=='\r' ){
11623      fputc('\\', out);
11624      fputc('r', out);
11625    }else if( !isprint(c&0xff) ){
11626      raw_printf(out, "\\%03o", c&0xff);
11627    }else{
11628      fputc(c, out);
11629    }
11630  }
11631  fputc('"', out);
11632}
11633
11634/*
11635** Output the given string as a quoted according to JSON quoting rules.
11636*/
11637static void output_json_string(FILE *out, const char *z, int n){
11638  unsigned int c;
11639  if( n<0 ) n = (int)strlen(z);
11640  fputc('"', out);
11641  while( n-- ){
11642    c = *(z++);
11643    if( c=='\\' || c=='"' ){
11644      fputc('\\', out);
11645      fputc(c, out);
11646    }else if( c<=0x1f ){
11647      fputc('\\', out);
11648      if( c=='\b' ){
11649        fputc('b', out);
11650      }else if( c=='\f' ){
11651        fputc('f', out);
11652      }else if( c=='\n' ){
11653        fputc('n', out);
11654      }else if( c=='\r' ){
11655        fputc('r', out);
11656      }else if( c=='\t' ){
11657        fputc('t', out);
11658      }else{
11659         raw_printf(out, "u%04x",c);
11660      }
11661    }else{
11662      fputc(c, out);
11663    }
11664  }
11665  fputc('"', out);
11666}
11667
11668/*
11669** Output the given string with characters that are special to
11670** HTML escaped.
11671*/
11672static void output_html_string(FILE *out, const char *z){
11673  int i;
11674  if( z==0 ) z = "";
11675  while( *z ){
11676    for(i=0;   z[i]
11677            && z[i]!='<'
11678            && z[i]!='&'
11679            && z[i]!='>'
11680            && z[i]!='\"'
11681            && z[i]!='\'';
11682        i++){}
11683    if( i>0 ){
11684      utf8_printf(out,"%.*s",i,z);
11685    }
11686    if( z[i]=='<' ){
11687      raw_printf(out,"&lt;");
11688    }else if( z[i]=='&' ){
11689      raw_printf(out,"&amp;");
11690    }else if( z[i]=='>' ){
11691      raw_printf(out,"&gt;");
11692    }else if( z[i]=='\"' ){
11693      raw_printf(out,"&quot;");
11694    }else if( z[i]=='\'' ){
11695      raw_printf(out,"&#39;");
11696    }else{
11697      break;
11698    }
11699    z += i + 1;
11700  }
11701}
11702
11703/*
11704** If a field contains any character identified by a 1 in the following
11705** array, then the string must be quoted for CSV.
11706*/
11707static const char needCsvQuote[] = {
11708  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11709  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11710  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
11711  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11712  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11713  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11714  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11715  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
11716  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11717  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11718  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11719  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11720  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11721  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11722  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11723  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11724};
11725
11726/*
11727** Output a single term of CSV.  Actually, p->colSeparator is used for
11728** the separator, which may or may not be a comma.  p->nullValue is
11729** the null value.  Strings are quoted if necessary.  The separator
11730** is only issued if bSep is true.
11731*/
11732static void output_csv(ShellState *p, const char *z, int bSep){
11733  FILE *out = p->out;
11734  if( z==0 ){
11735    utf8_printf(out,"%s",p->nullValue);
11736  }else{
11737    int i;
11738    int nSep = strlen30(p->colSeparator);
11739    for(i=0; z[i]; i++){
11740      if( needCsvQuote[((unsigned char*)z)[i]]
11741         || (z[i]==p->colSeparator[0] &&
11742             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
11743        i = 0;
11744        break;
11745      }
11746    }
11747    if( i==0 ){
11748      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
11749      utf8_printf(out, "%s", zQuoted);
11750      sqlite3_free(zQuoted);
11751    }else{
11752      utf8_printf(out, "%s", z);
11753    }
11754  }
11755  if( bSep ){
11756    utf8_printf(p->out, "%s", p->colSeparator);
11757  }
11758}
11759
11760/*
11761** This routine runs when the user presses Ctrl-C
11762*/
11763static void interrupt_handler(int NotUsed){
11764  UNUSED_PARAMETER(NotUsed);
11765  seenInterrupt++;
11766  if( seenInterrupt>2 ) exit(1);
11767  if( globalDb ) sqlite3_interrupt(globalDb);
11768}
11769
11770#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11771/*
11772** This routine runs for console events (e.g. Ctrl-C) on Win32
11773*/
11774static BOOL WINAPI ConsoleCtrlHandler(
11775  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
11776){
11777  if( dwCtrlType==CTRL_C_EVENT ){
11778    interrupt_handler(0);
11779    return TRUE;
11780  }
11781  return FALSE;
11782}
11783#endif
11784
11785#ifndef SQLITE_OMIT_AUTHORIZATION
11786/*
11787** When the ".auth ON" is set, the following authorizer callback is
11788** invoked.  It always returns SQLITE_OK.
11789*/
11790static int shellAuth(
11791  void *pClientData,
11792  int op,
11793  const char *zA1,
11794  const char *zA2,
11795  const char *zA3,
11796  const char *zA4
11797){
11798  ShellState *p = (ShellState*)pClientData;
11799  static const char *azAction[] = { 0,
11800     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
11801     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
11802     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
11803     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
11804     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
11805     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
11806     "PRAGMA",               "READ",                 "SELECT",
11807     "TRANSACTION",          "UPDATE",               "ATTACH",
11808     "DETACH",               "ALTER_TABLE",          "REINDEX",
11809     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
11810     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
11811  };
11812  int i;
11813  const char *az[4];
11814  az[0] = zA1;
11815  az[1] = zA2;
11816  az[2] = zA3;
11817  az[3] = zA4;
11818  utf8_printf(p->out, "authorizer: %s", azAction[op]);
11819  for(i=0; i<4; i++){
11820    raw_printf(p->out, " ");
11821    if( az[i] ){
11822      output_c_string(p->out, az[i]);
11823    }else{
11824      raw_printf(p->out, "NULL");
11825    }
11826  }
11827  raw_printf(p->out, "\n");
11828  return SQLITE_OK;
11829}
11830#endif
11831
11832/*
11833** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
11834**
11835** This routine converts some CREATE TABLE statements for shadow tables
11836** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
11837*/
11838static void printSchemaLine(FILE *out, const char *z, const char *zTail){
11839  if( z==0 ) return;
11840  if( zTail==0 ) return;
11841  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
11842    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
11843  }else{
11844    utf8_printf(out, "%s%s", z, zTail);
11845  }
11846}
11847static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
11848  char c = z[n];
11849  z[n] = 0;
11850  printSchemaLine(out, z, zTail);
11851  z[n] = c;
11852}
11853
11854/*
11855** Return true if string z[] has nothing but whitespace and comments to the
11856** end of the first line.
11857*/
11858static int wsToEol(const char *z){
11859  int i;
11860  for(i=0; z[i]; i++){
11861    if( z[i]=='\n' ) return 1;
11862    if( IsSpace(z[i]) ) continue;
11863    if( z[i]=='-' && z[i+1]=='-' ) return 1;
11864    return 0;
11865  }
11866  return 1;
11867}
11868
11869/*
11870** Add a new entry to the EXPLAIN QUERY PLAN data
11871*/
11872static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
11873  EQPGraphRow *pNew;
11874  int nText = strlen30(zText);
11875  if( p->autoEQPtest ){
11876    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
11877  }
11878  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
11879  if( pNew==0 ) shell_out_of_memory();
11880  pNew->iEqpId = iEqpId;
11881  pNew->iParentId = p2;
11882  memcpy(pNew->zText, zText, nText+1);
11883  pNew->pNext = 0;
11884  if( p->sGraph.pLast ){
11885    p->sGraph.pLast->pNext = pNew;
11886  }else{
11887    p->sGraph.pRow = pNew;
11888  }
11889  p->sGraph.pLast = pNew;
11890}
11891
11892/*
11893** Free and reset the EXPLAIN QUERY PLAN data that has been collected
11894** in p->sGraph.
11895*/
11896static void eqp_reset(ShellState *p){
11897  EQPGraphRow *pRow, *pNext;
11898  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
11899    pNext = pRow->pNext;
11900    sqlite3_free(pRow);
11901  }
11902  memset(&p->sGraph, 0, sizeof(p->sGraph));
11903}
11904
11905/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
11906** pOld, or return the first such line if pOld is NULL
11907*/
11908static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
11909  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
11910  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
11911  return pRow;
11912}
11913
11914/* Render a single level of the graph that has iEqpId as its parent.  Called
11915** recursively to render sublevels.
11916*/
11917static void eqp_render_level(ShellState *p, int iEqpId){
11918  EQPGraphRow *pRow, *pNext;
11919  int n = strlen30(p->sGraph.zPrefix);
11920  char *z;
11921  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
11922    pNext = eqp_next_row(p, iEqpId, pRow);
11923    z = pRow->zText;
11924    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
11925                pNext ? "|--" : "`--", z);
11926    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
11927      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
11928      eqp_render_level(p, pRow->iEqpId);
11929      p->sGraph.zPrefix[n] = 0;
11930    }
11931  }
11932}
11933
11934/*
11935** Display and reset the EXPLAIN QUERY PLAN data
11936*/
11937static void eqp_render(ShellState *p){
11938  EQPGraphRow *pRow = p->sGraph.pRow;
11939  if( pRow ){
11940    if( pRow->zText[0]=='-' ){
11941      if( pRow->pNext==0 ){
11942        eqp_reset(p);
11943        return;
11944      }
11945      utf8_printf(p->out, "%s\n", pRow->zText+3);
11946      p->sGraph.pRow = pRow->pNext;
11947      sqlite3_free(pRow);
11948    }else{
11949      utf8_printf(p->out, "QUERY PLAN\n");
11950    }
11951    p->sGraph.zPrefix[0] = 0;
11952    eqp_render_level(p, 0);
11953    eqp_reset(p);
11954  }
11955}
11956
11957#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11958/*
11959** Progress handler callback.
11960*/
11961static int progress_handler(void *pClientData) {
11962  ShellState *p = (ShellState*)pClientData;
11963  p->nProgress++;
11964  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
11965    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
11966    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11967    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
11968    return 1;
11969  }
11970  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
11971    raw_printf(p->out, "Progress %u\n", p->nProgress);
11972  }
11973  return 0;
11974}
11975#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
11976
11977/*
11978** Print N dashes
11979*/
11980static void print_dashes(FILE *out, int N){
11981  const char zDash[] = "--------------------------------------------------";
11982  const int nDash = sizeof(zDash) - 1;
11983  while( N>nDash ){
11984    fputs(zDash, out);
11985    N -= nDash;
11986  }
11987  raw_printf(out, "%.*s", N, zDash);
11988}
11989
11990/*
11991** Print a markdown or table-style row separator using ascii-art
11992*/
11993static void print_row_separator(
11994  ShellState *p,
11995  int nArg,
11996  const char *zSep
11997){
11998  int i;
11999  if( nArg>0 ){
12000    fputs(zSep, p->out);
12001    print_dashes(p->out, p->actualWidth[0]+2);
12002    for(i=1; i<nArg; i++){
12003      fputs(zSep, p->out);
12004      print_dashes(p->out, p->actualWidth[i]+2);
12005    }
12006    fputs(zSep, p->out);
12007  }
12008  fputs("\n", p->out);
12009}
12010
12011/*
12012** This is the callback routine that the shell
12013** invokes for each row of a query result.
12014*/
12015static int shell_callback(
12016  void *pArg,
12017  int nArg,        /* Number of result columns */
12018  char **azArg,    /* Text of each result column */
12019  char **azCol,    /* Column names */
12020  int *aiType      /* Column types.  Might be NULL */
12021){
12022  int i;
12023  ShellState *p = (ShellState*)pArg;
12024
12025  if( azArg==0 ) return 0;
12026  switch( p->cMode ){
12027    case MODE_Line: {
12028      int w = 5;
12029      if( azArg==0 ) break;
12030      for(i=0; i<nArg; i++){
12031        int len = strlen30(azCol[i] ? azCol[i] : "");
12032        if( len>w ) w = len;
12033      }
12034      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12035      for(i=0; i<nArg; i++){
12036        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12037                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12038      }
12039      break;
12040    }
12041    case MODE_Explain: {
12042      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12043      if( nArg>ArraySize(aExplainWidth) ){
12044        nArg = ArraySize(aExplainWidth);
12045      }
12046      if( p->cnt++==0 ){
12047        for(i=0; i<nArg; i++){
12048          int w = aExplainWidth[i];
12049          utf8_width_print(p->out, w, azCol[i]);
12050          fputs(i==nArg-1 ? "\n" : "  ", p->out);
12051        }
12052        for(i=0; i<nArg; i++){
12053          int w = aExplainWidth[i];
12054          print_dashes(p->out, w);
12055          fputs(i==nArg-1 ? "\n" : "  ", p->out);
12056        }
12057      }
12058      if( azArg==0 ) break;
12059      for(i=0; i<nArg; i++){
12060        int w = aExplainWidth[i];
12061        if( azArg[i] && strlenChar(azArg[i])>w ){
12062          w = strlenChar(azArg[i]);
12063        }
12064        if( i==1 && p->aiIndent && p->pStmt ){
12065          if( p->iIndent<p->nIndent ){
12066            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12067          }
12068          p->iIndent++;
12069        }
12070        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12071        fputs(i==nArg-1 ? "\n" : "  ", p->out);
12072      }
12073      break;
12074    }
12075    case MODE_Semi: {   /* .schema and .fullschema output */
12076      printSchemaLine(p->out, azArg[0], ";\n");
12077      break;
12078    }
12079    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
12080      char *z;
12081      int j;
12082      int nParen = 0;
12083      char cEnd = 0;
12084      char c;
12085      int nLine = 0;
12086      assert( nArg==1 );
12087      if( azArg[0]==0 ) break;
12088      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
12089       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
12090      ){
12091        utf8_printf(p->out, "%s;\n", azArg[0]);
12092        break;
12093      }
12094      z = sqlite3_mprintf("%s", azArg[0]);
12095      j = 0;
12096      for(i=0; IsSpace(z[i]); i++){}
12097      for(; (c = z[i])!=0; i++){
12098        if( IsSpace(c) ){
12099          if( z[j-1]=='\r' ) z[j-1] = '\n';
12100          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
12101        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
12102          j--;
12103        }
12104        z[j++] = c;
12105      }
12106      while( j>0 && IsSpace(z[j-1]) ){ j--; }
12107      z[j] = 0;
12108      if( strlen30(z)>=79 ){
12109        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
12110          if( c==cEnd ){
12111            cEnd = 0;
12112          }else if( c=='"' || c=='\'' || c=='`' ){
12113            cEnd = c;
12114          }else if( c=='[' ){
12115            cEnd = ']';
12116          }else if( c=='-' && z[i+1]=='-' ){
12117            cEnd = '\n';
12118          }else if( c=='(' ){
12119            nParen++;
12120          }else if( c==')' ){
12121            nParen--;
12122            if( nLine>0 && nParen==0 && j>0 ){
12123              printSchemaLineN(p->out, z, j, "\n");
12124              j = 0;
12125            }
12126          }
12127          z[j++] = c;
12128          if( nParen==1 && cEnd==0
12129           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
12130          ){
12131            if( c=='\n' ) j--;
12132            printSchemaLineN(p->out, z, j, "\n  ");
12133            j = 0;
12134            nLine++;
12135            while( IsSpace(z[i+1]) ){ i++; }
12136          }
12137        }
12138        z[j] = 0;
12139      }
12140      printSchemaLine(p->out, z, ";\n");
12141      sqlite3_free(z);
12142      break;
12143    }
12144    case MODE_List: {
12145      if( p->cnt++==0 && p->showHeader ){
12146        for(i=0; i<nArg; i++){
12147          utf8_printf(p->out,"%s%s",azCol[i],
12148                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
12149        }
12150      }
12151      if( azArg==0 ) break;
12152      for(i=0; i<nArg; i++){
12153        char *z = azArg[i];
12154        if( z==0 ) z = p->nullValue;
12155        utf8_printf(p->out, "%s", z);
12156        if( i<nArg-1 ){
12157          utf8_printf(p->out, "%s", p->colSeparator);
12158        }else{
12159          utf8_printf(p->out, "%s", p->rowSeparator);
12160        }
12161      }
12162      break;
12163    }
12164    case MODE_Html: {
12165      if( p->cnt++==0 && p->showHeader ){
12166        raw_printf(p->out,"<TR>");
12167        for(i=0; i<nArg; i++){
12168          raw_printf(p->out,"<TH>");
12169          output_html_string(p->out, azCol[i]);
12170          raw_printf(p->out,"</TH>\n");
12171        }
12172        raw_printf(p->out,"</TR>\n");
12173      }
12174      if( azArg==0 ) break;
12175      raw_printf(p->out,"<TR>");
12176      for(i=0; i<nArg; i++){
12177        raw_printf(p->out,"<TD>");
12178        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12179        raw_printf(p->out,"</TD>\n");
12180      }
12181      raw_printf(p->out,"</TR>\n");
12182      break;
12183    }
12184    case MODE_Tcl: {
12185      if( p->cnt++==0 && p->showHeader ){
12186        for(i=0; i<nArg; i++){
12187          output_c_string(p->out,azCol[i] ? azCol[i] : "");
12188          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12189        }
12190        utf8_printf(p->out, "%s", p->rowSeparator);
12191      }
12192      if( azArg==0 ) break;
12193      for(i=0; i<nArg; i++){
12194        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12195        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12196      }
12197      utf8_printf(p->out, "%s", p->rowSeparator);
12198      break;
12199    }
12200    case MODE_Csv: {
12201      setBinaryMode(p->out, 1);
12202      if( p->cnt++==0 && p->showHeader ){
12203        for(i=0; i<nArg; i++){
12204          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
12205        }
12206        utf8_printf(p->out, "%s", p->rowSeparator);
12207      }
12208      if( nArg>0 ){
12209        for(i=0; i<nArg; i++){
12210          output_csv(p, azArg[i], i<nArg-1);
12211        }
12212        utf8_printf(p->out, "%s", p->rowSeparator);
12213      }
12214      setTextMode(p->out, 1);
12215      break;
12216    }
12217    case MODE_Insert: {
12218      if( azArg==0 ) break;
12219      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
12220      if( p->showHeader ){
12221        raw_printf(p->out,"(");
12222        for(i=0; i<nArg; i++){
12223          if( i>0 ) raw_printf(p->out, ",");
12224          if( quoteChar(azCol[i]) ){
12225            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
12226            utf8_printf(p->out, "%s", z);
12227            sqlite3_free(z);
12228          }else{
12229            raw_printf(p->out, "%s", azCol[i]);
12230          }
12231        }
12232        raw_printf(p->out,")");
12233      }
12234      p->cnt++;
12235      for(i=0; i<nArg; i++){
12236        raw_printf(p->out, i>0 ? "," : " VALUES(");
12237        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12238          utf8_printf(p->out,"NULL");
12239        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12240          if( ShellHasFlag(p, SHFLG_Newlines) ){
12241            output_quoted_string(p->out, azArg[i]);
12242          }else{
12243            output_quoted_escaped_string(p->out, azArg[i]);
12244          }
12245        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12246          utf8_printf(p->out,"%s", azArg[i]);
12247        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12248          char z[50];
12249          double r = sqlite3_column_double(p->pStmt, i);
12250          sqlite3_uint64 ur;
12251          memcpy(&ur,&r,sizeof(r));
12252          if( ur==0x7ff0000000000000LL ){
12253            raw_printf(p->out, "1e999");
12254          }else if( ur==0xfff0000000000000LL ){
12255            raw_printf(p->out, "-1e999");
12256          }else{
12257            sqlite3_snprintf(50,z,"%!.20g", r);
12258            raw_printf(p->out, "%s", z);
12259          }
12260        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12261          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12262          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12263          output_hex_blob(p->out, pBlob, nBlob);
12264        }else if( isNumber(azArg[i], 0) ){
12265          utf8_printf(p->out,"%s", azArg[i]);
12266        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
12267          output_quoted_string(p->out, azArg[i]);
12268        }else{
12269          output_quoted_escaped_string(p->out, azArg[i]);
12270        }
12271      }
12272      raw_printf(p->out,");\n");
12273      break;
12274    }
12275    case MODE_Json: {
12276      if( azArg==0 ) break;
12277      if( p->cnt==0 ){
12278        fputs("[{", p->out);
12279      }else{
12280        fputs(",\n{", p->out);
12281      }
12282      p->cnt++;
12283      for(i=0; i<nArg; i++){
12284        output_json_string(p->out, azCol[i], -1);
12285        putc(':', p->out);
12286        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12287          fputs("null",p->out);
12288        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12289          char z[50];
12290          double r = sqlite3_column_double(p->pStmt, i);
12291          sqlite3_uint64 ur;
12292          memcpy(&ur,&r,sizeof(r));
12293          if( ur==0x7ff0000000000000LL ){
12294            raw_printf(p->out, "1e999");
12295          }else if( ur==0xfff0000000000000LL ){
12296            raw_printf(p->out, "-1e999");
12297          }else{
12298            sqlite3_snprintf(50,z,"%!.20g", r);
12299            raw_printf(p->out, "%s", z);
12300          }
12301        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12302          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12303          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12304          output_json_string(p->out, pBlob, nBlob);
12305        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12306          output_json_string(p->out, azArg[i], -1);
12307        }else{
12308          utf8_printf(p->out,"%s", azArg[i]);
12309        }
12310        if( i<nArg-1 ){
12311          putc(',', p->out);
12312        }
12313      }
12314      putc('}', p->out);
12315      break;
12316    }
12317    case MODE_Quote: {
12318      if( azArg==0 ) break;
12319      if( p->cnt==0 && p->showHeader ){
12320        for(i=0; i<nArg; i++){
12321          if( i>0 ) fputs(p->colSeparator, p->out);
12322          output_quoted_string(p->out, azCol[i]);
12323        }
12324        fputs(p->rowSeparator, p->out);
12325      }
12326      p->cnt++;
12327      for(i=0; i<nArg; i++){
12328        if( i>0 ) fputs(p->colSeparator, p->out);
12329        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12330          utf8_printf(p->out,"NULL");
12331        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12332          output_quoted_string(p->out, azArg[i]);
12333        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12334          utf8_printf(p->out,"%s", azArg[i]);
12335        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12336          char z[50];
12337          double r = sqlite3_column_double(p->pStmt, i);
12338          sqlite3_snprintf(50,z,"%!.20g", r);
12339          raw_printf(p->out, "%s", z);
12340        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12341          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12342          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12343          output_hex_blob(p->out, pBlob, nBlob);
12344        }else if( isNumber(azArg[i], 0) ){
12345          utf8_printf(p->out,"%s", azArg[i]);
12346        }else{
12347          output_quoted_string(p->out, azArg[i]);
12348        }
12349      }
12350      fputs(p->rowSeparator, p->out);
12351      break;
12352    }
12353    case MODE_Ascii: {
12354      if( p->cnt++==0 && p->showHeader ){
12355        for(i=0; i<nArg; i++){
12356          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12357          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
12358        }
12359        utf8_printf(p->out, "%s", p->rowSeparator);
12360      }
12361      if( azArg==0 ) break;
12362      for(i=0; i<nArg; i++){
12363        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12364        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
12365      }
12366      utf8_printf(p->out, "%s", p->rowSeparator);
12367      break;
12368    }
12369    case MODE_EQP: {
12370      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
12371      break;
12372    }
12373  }
12374  return 0;
12375}
12376
12377/*
12378** This is the callback routine that the SQLite library
12379** invokes for each row of a query result.
12380*/
12381static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12382  /* since we don't have type info, call the shell_callback with a NULL value */
12383  return shell_callback(pArg, nArg, azArg, azCol, NULL);
12384}
12385
12386/*
12387** This is the callback routine from sqlite3_exec() that appends all
12388** output onto the end of a ShellText object.
12389*/
12390static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
12391  ShellText *p = (ShellText*)pArg;
12392  int i;
12393  UNUSED_PARAMETER(az);
12394  if( azArg==0 ) return 0;
12395  if( p->n ) appendText(p, "|", 0);
12396  for(i=0; i<nArg; i++){
12397    if( i ) appendText(p, ",", 0);
12398    if( azArg[i] ) appendText(p, azArg[i], 0);
12399  }
12400  return 0;
12401}
12402
12403/*
12404** Generate an appropriate SELFTEST table in the main database.
12405*/
12406static void createSelftestTable(ShellState *p){
12407  char *zErrMsg = 0;
12408  sqlite3_exec(p->db,
12409    "SAVEPOINT selftest_init;\n"
12410    "CREATE TABLE IF NOT EXISTS selftest(\n"
12411    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
12412    "  op TEXT,\n"                   /* Operator:  memo run */
12413    "  cmd TEXT,\n"                  /* Command text */
12414    "  ans TEXT\n"                   /* Desired answer */
12415    ");"
12416    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
12417    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
12418    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
12419    "         'memo','Tests generated by --init');\n"
12420    "INSERT INTO [_shell$self]\n"
12421    "  SELECT 'run',\n"
12422    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
12423                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
12424    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
12425                          "FROM sqlite_schema ORDER BY 2',224));\n"
12426    "INSERT INTO [_shell$self]\n"
12427    "  SELECT 'run',"
12428    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
12429    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
12430    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
12431    "  FROM (\n"
12432    "    SELECT name FROM sqlite_schema\n"
12433    "     WHERE type='table'\n"
12434    "       AND name<>'selftest'\n"
12435    "       AND coalesce(rootpage,0)>0\n"
12436    "  )\n"
12437    " ORDER BY name;\n"
12438    "INSERT INTO [_shell$self]\n"
12439    "  VALUES('run','PRAGMA integrity_check','ok');\n"
12440    "INSERT INTO selftest(tno,op,cmd,ans)"
12441    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
12442    "DROP TABLE [_shell$self];"
12443    ,0,0,&zErrMsg);
12444  if( zErrMsg ){
12445    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
12446    sqlite3_free(zErrMsg);
12447  }
12448  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
12449}
12450
12451
12452/*
12453** Set the destination table field of the ShellState structure to
12454** the name of the table given.  Escape any quote characters in the
12455** table name.
12456*/
12457static void set_table_name(ShellState *p, const char *zName){
12458  int i, n;
12459  char cQuote;
12460  char *z;
12461
12462  if( p->zDestTable ){
12463    free(p->zDestTable);
12464    p->zDestTable = 0;
12465  }
12466  if( zName==0 ) return;
12467  cQuote = quoteChar(zName);
12468  n = strlen30(zName);
12469  if( cQuote ) n += n+2;
12470  z = p->zDestTable = malloc( n+1 );
12471  if( z==0 ) shell_out_of_memory();
12472  n = 0;
12473  if( cQuote ) z[n++] = cQuote;
12474  for(i=0; zName[i]; i++){
12475    z[n++] = zName[i];
12476    if( zName[i]==cQuote ) z[n++] = cQuote;
12477  }
12478  if( cQuote ) z[n++] = cQuote;
12479  z[n] = 0;
12480}
12481
12482
12483/*
12484** Execute a query statement that will generate SQL output.  Print
12485** the result columns, comma-separated, on a line and then add a
12486** semicolon terminator to the end of that line.
12487**
12488** If the number of columns is 1 and that column contains text "--"
12489** then write the semicolon on a separate line.  That way, if a
12490** "--" comment occurs at the end of the statement, the comment
12491** won't consume the semicolon terminator.
12492*/
12493static int run_table_dump_query(
12494  ShellState *p,           /* Query context */
12495  const char *zSelect      /* SELECT statement to extract content */
12496){
12497  sqlite3_stmt *pSelect;
12498  int rc;
12499  int nResult;
12500  int i;
12501  const char *z;
12502  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
12503  if( rc!=SQLITE_OK || !pSelect ){
12504    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12505                sqlite3_errmsg(p->db));
12506    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12507    return rc;
12508  }
12509  rc = sqlite3_step(pSelect);
12510  nResult = sqlite3_column_count(pSelect);
12511  while( rc==SQLITE_ROW ){
12512    z = (const char*)sqlite3_column_text(pSelect, 0);
12513    utf8_printf(p->out, "%s", z);
12514    for(i=1; i<nResult; i++){
12515      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
12516    }
12517    if( z==0 ) z = "";
12518    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
12519    if( z[0] ){
12520      raw_printf(p->out, "\n;\n");
12521    }else{
12522      raw_printf(p->out, ";\n");
12523    }
12524    rc = sqlite3_step(pSelect);
12525  }
12526  rc = sqlite3_finalize(pSelect);
12527  if( rc!=SQLITE_OK ){
12528    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12529                sqlite3_errmsg(p->db));
12530    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12531  }
12532  return rc;
12533}
12534
12535/*
12536** Allocate space and save off current error string.
12537*/
12538static char *save_err_msg(
12539  sqlite3 *db            /* Database to query */
12540){
12541  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
12542  char *zErrMsg = sqlite3_malloc64(nErrMsg);
12543  if( zErrMsg ){
12544    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
12545  }
12546  return zErrMsg;
12547}
12548
12549#ifdef __linux__
12550/*
12551** Attempt to display I/O stats on Linux using /proc/PID/io
12552*/
12553static void displayLinuxIoStats(FILE *out){
12554  FILE *in;
12555  char z[200];
12556  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
12557  in = fopen(z, "rb");
12558  if( in==0 ) return;
12559  while( fgets(z, sizeof(z), in)!=0 ){
12560    static const struct {
12561      const char *zPattern;
12562      const char *zDesc;
12563    } aTrans[] = {
12564      { "rchar: ",                  "Bytes received by read():" },
12565      { "wchar: ",                  "Bytes sent to write():"    },
12566      { "syscr: ",                  "Read() system calls:"      },
12567      { "syscw: ",                  "Write() system calls:"     },
12568      { "read_bytes: ",             "Bytes read from storage:"  },
12569      { "write_bytes: ",            "Bytes written to storage:" },
12570      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
12571    };
12572    int i;
12573    for(i=0; i<ArraySize(aTrans); i++){
12574      int n = strlen30(aTrans[i].zPattern);
12575      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
12576        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
12577        break;
12578      }
12579    }
12580  }
12581  fclose(in);
12582}
12583#endif
12584
12585/*
12586** Display a single line of status using 64-bit values.
12587*/
12588static void displayStatLine(
12589  ShellState *p,            /* The shell context */
12590  char *zLabel,             /* Label for this one line */
12591  char *zFormat,            /* Format for the result */
12592  int iStatusCtrl,          /* Which status to display */
12593  int bReset                /* True to reset the stats */
12594){
12595  sqlite3_int64 iCur = -1;
12596  sqlite3_int64 iHiwtr = -1;
12597  int i, nPercent;
12598  char zLine[200];
12599  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
12600  for(i=0, nPercent=0; zFormat[i]; i++){
12601    if( zFormat[i]=='%' ) nPercent++;
12602  }
12603  if( nPercent>1 ){
12604    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
12605  }else{
12606    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
12607  }
12608  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
12609}
12610
12611/*
12612** Display memory stats.
12613*/
12614static int display_stats(
12615  sqlite3 *db,                /* Database to query */
12616  ShellState *pArg,           /* Pointer to ShellState */
12617  int bReset                  /* True to reset the stats */
12618){
12619  int iCur;
12620  int iHiwtr;
12621  FILE *out;
12622  if( pArg==0 || pArg->out==0 ) return 0;
12623  out = pArg->out;
12624
12625  if( pArg->pStmt && (pArg->statsOn & 2) ){
12626    int nCol, i, x;
12627    sqlite3_stmt *pStmt = pArg->pStmt;
12628    char z[100];
12629    nCol = sqlite3_column_count(pStmt);
12630    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
12631    for(i=0; i<nCol; i++){
12632      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
12633      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
12634#ifndef SQLITE_OMIT_DECLTYPE
12635      sqlite3_snprintf(30, z+x, "declared type:");
12636      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
12637#endif
12638#ifdef SQLITE_ENABLE_COLUMN_METADATA
12639      sqlite3_snprintf(30, z+x, "database name:");
12640      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
12641      sqlite3_snprintf(30, z+x, "table name:");
12642      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
12643      sqlite3_snprintf(30, z+x, "origin name:");
12644      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
12645#endif
12646    }
12647  }
12648
12649  displayStatLine(pArg, "Memory Used:",
12650     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
12651  displayStatLine(pArg, "Number of Outstanding Allocations:",
12652     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
12653  if( pArg->shellFlgs & SHFLG_Pagecache ){
12654    displayStatLine(pArg, "Number of Pcache Pages Used:",
12655       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
12656  }
12657  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
12658     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
12659  displayStatLine(pArg, "Largest Allocation:",
12660     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
12661  displayStatLine(pArg, "Largest Pcache Allocation:",
12662     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
12663#ifdef YYTRACKMAXSTACKDEPTH
12664  displayStatLine(pArg, "Deepest Parser Stack:",
12665     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
12666#endif
12667
12668  if( db ){
12669    if( pArg->shellFlgs & SHFLG_Lookaside ){
12670      iHiwtr = iCur = -1;
12671      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
12672                        &iCur, &iHiwtr, bReset);
12673      raw_printf(pArg->out,
12674              "Lookaside Slots Used:                %d (max %d)\n",
12675              iCur, iHiwtr);
12676      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
12677                        &iCur, &iHiwtr, bReset);
12678      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
12679              iHiwtr);
12680      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
12681                        &iCur, &iHiwtr, bReset);
12682      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
12683              iHiwtr);
12684      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
12685                        &iCur, &iHiwtr, bReset);
12686      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
12687              iHiwtr);
12688    }
12689    iHiwtr = iCur = -1;
12690    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
12691    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
12692            iCur);
12693    iHiwtr = iCur = -1;
12694    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
12695    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
12696    iHiwtr = iCur = -1;
12697    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
12698    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
12699    iHiwtr = iCur = -1;
12700    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
12701    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
12702    iHiwtr = iCur = -1;
12703    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
12704    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
12705    iHiwtr = iCur = -1;
12706    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
12707    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
12708            iCur);
12709    iHiwtr = iCur = -1;
12710    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
12711    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
12712            iCur);
12713  }
12714
12715  if( pArg->pStmt ){
12716    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
12717                               bReset);
12718    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
12719    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
12720    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
12721    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
12722    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
12723    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12724    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
12725    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
12726    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
12727    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
12728    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
12729    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
12730    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
12731  }
12732
12733#ifdef __linux__
12734  displayLinuxIoStats(pArg->out);
12735#endif
12736
12737  /* Do not remove this machine readable comment: extra-stats-output-here */
12738
12739  return 0;
12740}
12741
12742/*
12743** Display scan stats.
12744*/
12745static void display_scanstats(
12746  sqlite3 *db,                    /* Database to query */
12747  ShellState *pArg                /* Pointer to ShellState */
12748){
12749#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
12750  UNUSED_PARAMETER(db);
12751  UNUSED_PARAMETER(pArg);
12752#else
12753  int i, k, n, mx;
12754  raw_printf(pArg->out, "-------- scanstats --------\n");
12755  mx = 0;
12756  for(k=0; k<=mx; k++){
12757    double rEstLoop = 1.0;
12758    for(i=n=0; 1; i++){
12759      sqlite3_stmt *p = pArg->pStmt;
12760      sqlite3_int64 nLoop, nVisit;
12761      double rEst;
12762      int iSid;
12763      const char *zExplain;
12764      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
12765        break;
12766      }
12767      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
12768      if( iSid>mx ) mx = iSid;
12769      if( iSid!=k ) continue;
12770      if( n==0 ){
12771        rEstLoop = (double)nLoop;
12772        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
12773      }
12774      n++;
12775      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
12776      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
12777      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
12778      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
12779      rEstLoop *= rEst;
12780      raw_printf(pArg->out,
12781          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
12782          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
12783      );
12784    }
12785  }
12786  raw_printf(pArg->out, "---------------------------\n");
12787#endif
12788}
12789
12790/*
12791** Parameter azArray points to a zero-terminated array of strings. zStr
12792** points to a single nul-terminated string. Return non-zero if zStr
12793** is equal, according to strcmp(), to any of the strings in the array.
12794** Otherwise, return zero.
12795*/
12796static int str_in_array(const char *zStr, const char **azArray){
12797  int i;
12798  for(i=0; azArray[i]; i++){
12799    if( 0==strcmp(zStr, azArray[i]) ) return 1;
12800  }
12801  return 0;
12802}
12803
12804/*
12805** If compiled statement pSql appears to be an EXPLAIN statement, allocate
12806** and populate the ShellState.aiIndent[] array with the number of
12807** spaces each opcode should be indented before it is output.
12808**
12809** The indenting rules are:
12810**
12811**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
12812**       all opcodes that occur between the p2 jump destination and the opcode
12813**       itself by 2 spaces.
12814**
12815**     * For each "Goto", if the jump destination is earlier in the program
12816**       and ends on one of:
12817**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
12818**       or if the P1 parameter is one instead of zero,
12819**       then indent all opcodes between the earlier instruction
12820**       and "Goto" by 2 spaces.
12821*/
12822static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12823  const char *zSql;               /* The text of the SQL statement */
12824  const char *z;                  /* Used to check if this is an EXPLAIN */
12825  int *abYield = 0;               /* True if op is an OP_Yield */
12826  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
12827  int iOp;                        /* Index of operation in p->aiIndent[] */
12828
12829  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
12830  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
12831                            "Rewind", 0 };
12832  const char *azGoto[] = { "Goto", 0 };
12833
12834  /* Try to figure out if this is really an EXPLAIN statement. If this
12835  ** cannot be verified, return early.  */
12836  if( sqlite3_column_count(pSql)!=8 ){
12837    p->cMode = p->mode;
12838    return;
12839  }
12840  zSql = sqlite3_sql(pSql);
12841  if( zSql==0 ) return;
12842  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
12843  if( sqlite3_strnicmp(z, "explain", 7) ){
12844    p->cMode = p->mode;
12845    return;
12846  }
12847
12848  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12849    int i;
12850    int iAddr = sqlite3_column_int(pSql, 0);
12851    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
12852
12853    /* Set p2 to the P2 field of the current opcode. Then, assuming that
12854    ** p2 is an instruction address, set variable p2op to the index of that
12855    ** instruction in the aiIndent[] array. p2 and p2op may be different if
12856    ** the current instruction is part of a sub-program generated by an
12857    ** SQL trigger or foreign key.  */
12858    int p2 = sqlite3_column_int(pSql, 3);
12859    int p2op = (p2 + (iOp-iAddr));
12860
12861    /* Grow the p->aiIndent array as required */
12862    if( iOp>=nAlloc ){
12863      if( iOp==0 ){
12864        /* Do further verfication that this is explain output.  Abort if
12865        ** it is not */
12866        static const char *explainCols[] = {
12867           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
12868        int jj;
12869        for(jj=0; jj<ArraySize(explainCols); jj++){
12870          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
12871            p->cMode = p->mode;
12872            sqlite3_reset(pSql);
12873            return;
12874          }
12875        }
12876      }
12877      nAlloc += 100;
12878      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
12879      if( p->aiIndent==0 ) shell_out_of_memory();
12880      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
12881      if( abYield==0 ) shell_out_of_memory();
12882    }
12883    abYield[iOp] = str_in_array(zOp, azYield);
12884    p->aiIndent[iOp] = 0;
12885    p->nIndent = iOp+1;
12886
12887    if( str_in_array(zOp, azNext) ){
12888      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12889    }
12890    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
12891     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
12892    ){
12893      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12894    }
12895  }
12896
12897  p->iIndent = 0;
12898  sqlite3_free(abYield);
12899  sqlite3_reset(pSql);
12900}
12901
12902/*
12903** Free the array allocated by explain_data_prepare().
12904*/
12905static void explain_data_delete(ShellState *p){
12906  sqlite3_free(p->aiIndent);
12907  p->aiIndent = 0;
12908  p->nIndent = 0;
12909  p->iIndent = 0;
12910}
12911
12912/*
12913** Disable and restore .wheretrace and .selecttrace settings.
12914*/
12915#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12916extern unsigned int sqlite3_unsupported_selecttrace;
12917static int savedSelectTrace;
12918#endif
12919#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12920extern int sqlite3WhereTrace;
12921static int savedWhereTrace;
12922#endif
12923static void disable_debug_trace_modes(void){
12924#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12925  savedSelectTrace = sqlite3_unsupported_selecttrace;
12926  sqlite3_unsupported_selecttrace = 0;
12927#endif
12928#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12929  savedWhereTrace = sqlite3WhereTrace;
12930  sqlite3WhereTrace = 0;
12931#endif
12932}
12933static void restore_debug_trace_modes(void){
12934#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12935  sqlite3_unsupported_selecttrace = savedSelectTrace;
12936#endif
12937#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12938  sqlite3WhereTrace = savedWhereTrace;
12939#endif
12940}
12941
12942/* Create the TEMP table used to store parameter bindings */
12943static void bind_table_init(ShellState *p){
12944  int wrSchema = 0;
12945  int defensiveMode = 0;
12946  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
12947  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
12948  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
12949  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
12950  sqlite3_exec(p->db,
12951    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
12952    "  key TEXT PRIMARY KEY,\n"
12953    "  value ANY\n"
12954    ") WITHOUT ROWID;",
12955    0, 0, 0);
12956  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
12957  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
12958}
12959
12960/*
12961** Bind parameters on a prepared statement.
12962**
12963** Parameter bindings are taken from a TEMP table of the form:
12964**
12965**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
12966**    WITHOUT ROWID;
12967**
12968** No bindings occur if this table does not exist.  The name of the table
12969** begins with "sqlite_" so that it will not collide with ordinary application
12970** tables.  The table must be in the TEMP schema.
12971*/
12972static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
12973  int nVar;
12974  int i;
12975  int rc;
12976  sqlite3_stmt *pQ = 0;
12977
12978  nVar = sqlite3_bind_parameter_count(pStmt);
12979  if( nVar==0 ) return;  /* Nothing to do */
12980  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
12981                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
12982    return; /* Parameter table does not exist */
12983  }
12984  rc = sqlite3_prepare_v2(pArg->db,
12985          "SELECT value FROM temp.sqlite_parameters"
12986          " WHERE key=?1", -1, &pQ, 0);
12987  if( rc || pQ==0 ) return;
12988  for(i=1; i<=nVar; i++){
12989    char zNum[30];
12990    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
12991    if( zVar==0 ){
12992      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
12993      zVar = zNum;
12994    }
12995    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
12996    if( sqlite3_step(pQ)==SQLITE_ROW ){
12997      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
12998    }else{
12999      sqlite3_bind_null(pStmt, i);
13000    }
13001    sqlite3_reset(pQ);
13002  }
13003  sqlite3_finalize(pQ);
13004}
13005
13006/*
13007** UTF8 box-drawing characters.  Imagine box lines like this:
13008**
13009**           1
13010**           |
13011**       4 --+-- 2
13012**           |
13013**           3
13014**
13015** Each box characters has between 2 and 4 of the lines leading from
13016** the center.  The characters are here identified by the numbers of
13017** their corresponding lines.
13018*/
13019#define BOX_24   "\342\224\200"  /* U+2500 --- */
13020#define BOX_13   "\342\224\202"  /* U+2502  |  */
13021#define BOX_23   "\342\224\214"  /* U+250c  ,- */
13022#define BOX_34   "\342\224\220"  /* U+2510 -,  */
13023#define BOX_12   "\342\224\224"  /* U+2514  '- */
13024#define BOX_14   "\342\224\230"  /* U+2518 -'  */
13025#define BOX_123  "\342\224\234"  /* U+251c  |- */
13026#define BOX_134  "\342\224\244"  /* U+2524 -|  */
13027#define BOX_234  "\342\224\254"  /* U+252c -,- */
13028#define BOX_124  "\342\224\264"  /* U+2534 -'- */
13029#define BOX_1234 "\342\224\274"  /* U+253c -|- */
13030
13031/* Draw horizontal line N characters long using unicode box
13032** characters
13033*/
13034static void print_box_line(FILE *out, int N){
13035  const char zDash[] =
13036      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13037      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13038  const int nDash = sizeof(zDash) - 1;
13039  N *= 3;
13040  while( N>nDash ){
13041    utf8_printf(out, zDash);
13042    N -= nDash;
13043  }
13044  utf8_printf(out, "%.*s", N, zDash);
13045}
13046
13047/*
13048** Draw a horizontal separator for a MODE_Box table.
13049*/
13050static void print_box_row_separator(
13051  ShellState *p,
13052  int nArg,
13053  const char *zSep1,
13054  const char *zSep2,
13055  const char *zSep3
13056){
13057  int i;
13058  if( nArg>0 ){
13059    utf8_printf(p->out, "%s", zSep1);
13060    print_box_line(p->out, p->actualWidth[0]+2);
13061    for(i=1; i<nArg; i++){
13062      utf8_printf(p->out, "%s", zSep2);
13063      print_box_line(p->out, p->actualWidth[i]+2);
13064    }
13065    utf8_printf(p->out, "%s", zSep3);
13066  }
13067  fputs("\n", p->out);
13068}
13069
13070
13071
13072/*
13073** Run a prepared statement and output the result in one of the
13074** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13075** or MODE_Box.
13076**
13077** This is different from ordinary exec_prepared_stmt() in that
13078** it has to run the entire query and gather the results into memory
13079** first, in order to determine column widths, before providing
13080** any output.
13081*/
13082static void exec_prepared_stmt_columnar(
13083  ShellState *p,                        /* Pointer to ShellState */
13084  sqlite3_stmt *pStmt                   /* Statment to run */
13085){
13086  sqlite3_int64 nRow = 0;
13087  int nColumn = 0;
13088  char **azData = 0;
13089  sqlite3_int64 nAlloc = 0;
13090  const char *z;
13091  int rc;
13092  sqlite3_int64 i, nData;
13093  int j, nTotal, w, n;
13094  const char *colSep = 0;
13095  const char *rowSep = 0;
13096
13097  rc = sqlite3_step(pStmt);
13098  if( rc!=SQLITE_ROW ) return;
13099  nColumn = sqlite3_column_count(pStmt);
13100  nAlloc = nColumn*4;
13101  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
13102  if( azData==0 ) shell_out_of_memory();
13103  for(i=0; i<nColumn; i++){
13104    azData[i] = strdup(sqlite3_column_name(pStmt,i));
13105  }
13106  do{
13107    if( (nRow+2)*nColumn >= nAlloc ){
13108      nAlloc *= 2;
13109      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
13110      if( azData==0 ) shell_out_of_memory();
13111    }
13112    nRow++;
13113    for(i=0; i<nColumn; i++){
13114      z = (const char*)sqlite3_column_text(pStmt,i);
13115      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
13116    }
13117  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
13118  if( nColumn>p->nWidth ){
13119    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
13120    if( p->colWidth==0 ) shell_out_of_memory();
13121    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
13122    p->nWidth = nColumn;
13123    p->actualWidth = &p->colWidth[nColumn];
13124  }
13125  memset(p->actualWidth, 0, nColumn*sizeof(int));
13126  for(i=0; i<nColumn; i++){
13127    w = p->colWidth[i];
13128    if( w<0 ) w = -w;
13129    p->actualWidth[i] = w;
13130  }
13131  nTotal = nColumn*(nRow+1);
13132  for(i=0; i<nTotal; i++){
13133    z = azData[i];
13134    if( z==0 ) z = p->nullValue;
13135    n = strlenChar(z);
13136    j = i%nColumn;
13137    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
13138  }
13139  if( seenInterrupt ) goto columnar_end;
13140  switch( p->cMode ){
13141    case MODE_Column: {
13142      colSep = "  ";
13143      rowSep = "\n";
13144      if( p->showHeader ){
13145        for(i=0; i<nColumn; i++){
13146          w = p->actualWidth[i];
13147          if( p->colWidth[i]<0 ) w = -w;
13148          utf8_width_print(p->out, w, azData[i]);
13149          fputs(i==nColumn-1?"\n":"  ", p->out);
13150        }
13151        for(i=0; i<nColumn; i++){
13152          print_dashes(p->out, p->actualWidth[i]);
13153          fputs(i==nColumn-1?"\n":"  ", p->out);
13154        }
13155      }
13156      break;
13157    }
13158    case MODE_Table: {
13159      colSep = " | ";
13160      rowSep = " |\n";
13161      print_row_separator(p, nColumn, "+");
13162      fputs("| ", p->out);
13163      for(i=0; i<nColumn; i++){
13164        w = p->actualWidth[i];
13165        n = strlenChar(azData[i]);
13166        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13167        fputs(i==nColumn-1?" |\n":" | ", p->out);
13168      }
13169      print_row_separator(p, nColumn, "+");
13170      break;
13171    }
13172    case MODE_Markdown: {
13173      colSep = " | ";
13174      rowSep = " |\n";
13175      fputs("| ", p->out);
13176      for(i=0; i<nColumn; i++){
13177        w = p->actualWidth[i];
13178        n = strlenChar(azData[i]);
13179        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13180        fputs(i==nColumn-1?" |\n":" | ", p->out);
13181      }
13182      print_row_separator(p, nColumn, "|");
13183      break;
13184    }
13185    case MODE_Box: {
13186      colSep = " " BOX_13 " ";
13187      rowSep = " " BOX_13 "\n";
13188      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
13189      utf8_printf(p->out, BOX_13 " ");
13190      for(i=0; i<nColumn; i++){
13191        w = p->actualWidth[i];
13192        n = strlenChar(azData[i]);
13193        utf8_printf(p->out, "%*s%s%*s%s",
13194            (w-n)/2, "", azData[i], (w-n+1)/2, "",
13195            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
13196      }
13197      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
13198      break;
13199    }
13200  }
13201  for(i=nColumn, j=0; i<nTotal; i++, j++){
13202    if( j==0 && p->cMode!=MODE_Column ){
13203      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
13204    }
13205    z = azData[i];
13206    if( z==0 ) z = p->nullValue;
13207    w = p->actualWidth[j];
13208    if( p->colWidth[j]<0 ) w = -w;
13209    utf8_width_print(p->out, w, z);
13210    if( j==nColumn-1 ){
13211      utf8_printf(p->out, "%s", rowSep);
13212      j = -1;
13213      if( seenInterrupt ) goto columnar_end;
13214    }else{
13215      utf8_printf(p->out, "%s", colSep);
13216    }
13217  }
13218  if( p->cMode==MODE_Table ){
13219    print_row_separator(p, nColumn, "+");
13220  }else if( p->cMode==MODE_Box ){
13221    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
13222  }
13223columnar_end:
13224  if( seenInterrupt ){
13225    utf8_printf(p->out, "Interrupt\n");
13226  }
13227  nData = (nRow+1)*nColumn;
13228  for(i=0; i<nData; i++) free(azData[i]);
13229  sqlite3_free(azData);
13230}
13231
13232/*
13233** Run a prepared statement
13234*/
13235static void exec_prepared_stmt(
13236  ShellState *pArg,                                /* Pointer to ShellState */
13237  sqlite3_stmt *pStmt                              /* Statment to run */
13238){
13239  int rc;
13240
13241  if( pArg->cMode==MODE_Column
13242   || pArg->cMode==MODE_Table
13243   || pArg->cMode==MODE_Box
13244   || pArg->cMode==MODE_Markdown
13245  ){
13246    exec_prepared_stmt_columnar(pArg, pStmt);
13247    return;
13248  }
13249
13250  /* perform the first step.  this will tell us if we
13251  ** have a result set or not and how wide it is.
13252  */
13253  rc = sqlite3_step(pStmt);
13254  /* if we have a result set... */
13255  if( SQLITE_ROW == rc ){
13256    /* allocate space for col name ptr, value ptr, and type */
13257    int nCol = sqlite3_column_count(pStmt);
13258    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
13259    if( !pData ){
13260      rc = SQLITE_NOMEM;
13261    }else{
13262      char **azCols = (char **)pData;      /* Names of result columns */
13263      char **azVals = &azCols[nCol];       /* Results */
13264      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
13265      int i, x;
13266      assert(sizeof(int) <= sizeof(char *));
13267      /* save off ptrs to column names */
13268      for(i=0; i<nCol; i++){
13269        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13270      }
13271      do{
13272        /* extract the data and data types */
13273        for(i=0; i<nCol; i++){
13274          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
13275          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
13276            azVals[i] = "";
13277          }else{
13278            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
13279          }
13280          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
13281            rc = SQLITE_NOMEM;
13282            break; /* from for */
13283          }
13284        } /* end for */
13285
13286        /* if data and types extracted successfully... */
13287        if( SQLITE_ROW == rc ){
13288          /* call the supplied callback with the result row data */
13289          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
13290            rc = SQLITE_ABORT;
13291          }else{
13292            rc = sqlite3_step(pStmt);
13293          }
13294        }
13295      } while( SQLITE_ROW == rc );
13296      sqlite3_free(pData);
13297      if( pArg->cMode==MODE_Json ){
13298        fputs("]\n", pArg->out);
13299      }
13300    }
13301  }
13302}
13303
13304#ifndef SQLITE_OMIT_VIRTUALTABLE
13305/*
13306** This function is called to process SQL if the previous shell command
13307** was ".expert". It passes the SQL in the second argument directly to
13308** the sqlite3expert object.
13309**
13310** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13311** code. In this case, (*pzErr) may be set to point to a buffer containing
13312** an English language error message. It is the responsibility of the
13313** caller to eventually free this buffer using sqlite3_free().
13314*/
13315static int expertHandleSQL(
13316  ShellState *pState,
13317  const char *zSql,
13318  char **pzErr
13319){
13320  assert( pState->expert.pExpert );
13321  assert( pzErr==0 || *pzErr==0 );
13322  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
13323}
13324
13325/*
13326** This function is called either to silently clean up the object
13327** created by the ".expert" command (if bCancel==1), or to generate a
13328** report from it and then clean it up (if bCancel==0).
13329**
13330** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13331** code. In this case, (*pzErr) may be set to point to a buffer containing
13332** an English language error message. It is the responsibility of the
13333** caller to eventually free this buffer using sqlite3_free().
13334*/
13335static int expertFinish(
13336  ShellState *pState,
13337  int bCancel,
13338  char **pzErr
13339){
13340  int rc = SQLITE_OK;
13341  sqlite3expert *p = pState->expert.pExpert;
13342  assert( p );
13343  assert( bCancel || pzErr==0 || *pzErr==0 );
13344  if( bCancel==0 ){
13345    FILE *out = pState->out;
13346    int bVerbose = pState->expert.bVerbose;
13347
13348    rc = sqlite3_expert_analyze(p, pzErr);
13349    if( rc==SQLITE_OK ){
13350      int nQuery = sqlite3_expert_count(p);
13351      int i;
13352
13353      if( bVerbose ){
13354        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
13355        raw_printf(out, "-- Candidates -----------------------------\n");
13356        raw_printf(out, "%s\n", zCand);
13357      }
13358      for(i=0; i<nQuery; i++){
13359        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
13360        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
13361        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
13362        if( zIdx==0 ) zIdx = "(no new indexes)\n";
13363        if( bVerbose ){
13364          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
13365          raw_printf(out, "%s\n\n", zSql);
13366        }
13367        raw_printf(out, "%s\n", zIdx);
13368        raw_printf(out, "%s\n", zEQP);
13369      }
13370    }
13371  }
13372  sqlite3_expert_destroy(p);
13373  pState->expert.pExpert = 0;
13374  return rc;
13375}
13376
13377/*
13378** Implementation of ".expert" dot command.
13379*/
13380static int expertDotCommand(
13381  ShellState *pState,             /* Current shell tool state */
13382  char **azArg,                   /* Array of arguments passed to dot command */
13383  int nArg                        /* Number of entries in azArg[] */
13384){
13385  int rc = SQLITE_OK;
13386  char *zErr = 0;
13387  int i;
13388  int iSample = 0;
13389
13390  assert( pState->expert.pExpert==0 );
13391  memset(&pState->expert, 0, sizeof(ExpertInfo));
13392
13393  for(i=1; rc==SQLITE_OK && i<nArg; i++){
13394    char *z = azArg[i];
13395    int n;
13396    if( z[0]=='-' && z[1]=='-' ) z++;
13397    n = strlen30(z);
13398    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
13399      pState->expert.bVerbose = 1;
13400    }
13401    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
13402      if( i==(nArg-1) ){
13403        raw_printf(stderr, "option requires an argument: %s\n", z);
13404        rc = SQLITE_ERROR;
13405      }else{
13406        iSample = (int)integerValue(azArg[++i]);
13407        if( iSample<0 || iSample>100 ){
13408          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
13409          rc = SQLITE_ERROR;
13410        }
13411      }
13412    }
13413    else{
13414      raw_printf(stderr, "unknown option: %s\n", z);
13415      rc = SQLITE_ERROR;
13416    }
13417  }
13418
13419  if( rc==SQLITE_OK ){
13420    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
13421    if( pState->expert.pExpert==0 ){
13422      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
13423      rc = SQLITE_ERROR;
13424    }else{
13425      sqlite3_expert_config(
13426          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
13427      );
13428    }
13429  }
13430
13431  return rc;
13432}
13433#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13434
13435/*
13436** Execute a statement or set of statements.  Print
13437** any result rows/columns depending on the current mode
13438** set via the supplied callback.
13439**
13440** This is very similar to SQLite's built-in sqlite3_exec()
13441** function except it takes a slightly different callback
13442** and callback data argument.
13443*/
13444static int shell_exec(
13445  ShellState *pArg,                         /* Pointer to ShellState */
13446  const char *zSql,                         /* SQL to be evaluated */
13447  char **pzErrMsg                           /* Error msg written here */
13448){
13449  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
13450  int rc = SQLITE_OK;             /* Return Code */
13451  int rc2;
13452  const char *zLeftover;          /* Tail of unprocessed SQL */
13453  sqlite3 *db = pArg->db;
13454
13455  if( pzErrMsg ){
13456    *pzErrMsg = NULL;
13457  }
13458
13459#ifndef SQLITE_OMIT_VIRTUALTABLE
13460  if( pArg->expert.pExpert ){
13461    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
13462    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
13463  }
13464#endif
13465
13466  while( zSql[0] && (SQLITE_OK == rc) ){
13467    static const char *zStmtSql;
13468    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13469    if( SQLITE_OK != rc ){
13470      if( pzErrMsg ){
13471        *pzErrMsg = save_err_msg(db);
13472      }
13473    }else{
13474      if( !pStmt ){
13475        /* this happens for a comment or white-space */
13476        zSql = zLeftover;
13477        while( IsSpace(zSql[0]) ) zSql++;
13478        continue;
13479      }
13480      zStmtSql = sqlite3_sql(pStmt);
13481      if( zStmtSql==0 ) zStmtSql = "";
13482      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
13483
13484      /* save off the prepared statment handle and reset row count */
13485      if( pArg ){
13486        pArg->pStmt = pStmt;
13487        pArg->cnt = 0;
13488      }
13489
13490      /* echo the sql statement if echo on */
13491      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
13492        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13493      }
13494
13495      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
13496      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
13497        sqlite3_stmt *pExplain;
13498        char *zEQP;
13499        int triggerEQP = 0;
13500        disable_debug_trace_modes();
13501        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
13502        if( pArg->autoEQP>=AUTOEQP_trigger ){
13503          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
13504        }
13505        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
13506        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13507        if( rc==SQLITE_OK ){
13508          while( sqlite3_step(pExplain)==SQLITE_ROW ){
13509            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
13510            int iEqpId = sqlite3_column_int(pExplain, 0);
13511            int iParentId = sqlite3_column_int(pExplain, 1);
13512            if( zEQPLine==0 ) zEQPLine = "";
13513            if( zEQPLine[0]=='-' ) eqp_render(pArg);
13514            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
13515          }
13516          eqp_render(pArg);
13517        }
13518        sqlite3_finalize(pExplain);
13519        sqlite3_free(zEQP);
13520        if( pArg->autoEQP>=AUTOEQP_full ){
13521          /* Also do an EXPLAIN for ".eqp full" mode */
13522          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
13523          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13524          if( rc==SQLITE_OK ){
13525            pArg->cMode = MODE_Explain;
13526            explain_data_prepare(pArg, pExplain);
13527            exec_prepared_stmt(pArg, pExplain);
13528            explain_data_delete(pArg);
13529          }
13530          sqlite3_finalize(pExplain);
13531          sqlite3_free(zEQP);
13532        }
13533        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
13534          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
13535          /* Reprepare pStmt before reactiving trace modes */
13536          sqlite3_finalize(pStmt);
13537          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
13538          if( pArg ) pArg->pStmt = pStmt;
13539        }
13540        restore_debug_trace_modes();
13541      }
13542
13543      if( pArg ){
13544        pArg->cMode = pArg->mode;
13545        if( pArg->autoExplain ){
13546          if( sqlite3_stmt_isexplain(pStmt)==1 ){
13547            pArg->cMode = MODE_Explain;
13548          }
13549          if( sqlite3_stmt_isexplain(pStmt)==2 ){
13550            pArg->cMode = MODE_EQP;
13551          }
13552        }
13553
13554        /* If the shell is currently in ".explain" mode, gather the extra
13555        ** data required to add indents to the output.*/
13556        if( pArg->cMode==MODE_Explain ){
13557          explain_data_prepare(pArg, pStmt);
13558        }
13559      }
13560
13561      bind_prepared_stmt(pArg, pStmt);
13562      exec_prepared_stmt(pArg, pStmt);
13563      explain_data_delete(pArg);
13564      eqp_render(pArg);
13565
13566      /* print usage stats if stats on */
13567      if( pArg && pArg->statsOn ){
13568        display_stats(db, pArg, 0);
13569      }
13570
13571      /* print loop-counters if required */
13572      if( pArg && pArg->scanstatsOn ){
13573        display_scanstats(db, pArg);
13574      }
13575
13576      /* Finalize the statement just executed. If this fails, save a
13577      ** copy of the error message. Otherwise, set zSql to point to the
13578      ** next statement to execute. */
13579      rc2 = sqlite3_finalize(pStmt);
13580      if( rc!=SQLITE_NOMEM ) rc = rc2;
13581      if( rc==SQLITE_OK ){
13582        zSql = zLeftover;
13583        while( IsSpace(zSql[0]) ) zSql++;
13584      }else if( pzErrMsg ){
13585        *pzErrMsg = save_err_msg(db);
13586      }
13587
13588      /* clear saved stmt handle */
13589      if( pArg ){
13590        pArg->pStmt = NULL;
13591      }
13592    }
13593  } /* end while */
13594
13595  return rc;
13596}
13597
13598/*
13599** Release memory previously allocated by tableColumnList().
13600*/
13601static void freeColumnList(char **azCol){
13602  int i;
13603  for(i=1; azCol[i]; i++){
13604    sqlite3_free(azCol[i]);
13605  }
13606  /* azCol[0] is a static string */
13607  sqlite3_free(azCol);
13608}
13609
13610/*
13611** Return a list of pointers to strings which are the names of all
13612** columns in table zTab.   The memory to hold the names is dynamically
13613** allocated and must be released by the caller using a subsequent call
13614** to freeColumnList().
13615**
13616** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
13617** value that needs to be preserved, then azCol[0] is filled in with the
13618** name of the rowid column.
13619**
13620** The first regular column in the table is azCol[1].  The list is terminated
13621** by an entry with azCol[i]==0.
13622*/
13623static char **tableColumnList(ShellState *p, const char *zTab){
13624  char **azCol = 0;
13625  sqlite3_stmt *pStmt;
13626  char *zSql;
13627  int nCol = 0;
13628  int nAlloc = 0;
13629  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
13630  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
13631  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
13632  int rc;
13633
13634  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
13635  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13636  sqlite3_free(zSql);
13637  if( rc ) return 0;
13638  while( sqlite3_step(pStmt)==SQLITE_ROW ){
13639    if( nCol>=nAlloc-2 ){
13640      nAlloc = nAlloc*2 + nCol + 10;
13641      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
13642      if( azCol==0 ) shell_out_of_memory();
13643    }
13644    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
13645    if( sqlite3_column_int(pStmt, 5) ){
13646      nPK++;
13647      if( nPK==1
13648       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
13649                          "INTEGER")==0
13650      ){
13651        isIPK = 1;
13652      }else{
13653        isIPK = 0;
13654      }
13655    }
13656  }
13657  sqlite3_finalize(pStmt);
13658  if( azCol==0 ) return 0;
13659  azCol[0] = 0;
13660  azCol[nCol+1] = 0;
13661
13662  /* The decision of whether or not a rowid really needs to be preserved
13663  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
13664  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
13665  ** rowids on tables where the rowid is inaccessible because there are other
13666  ** columns in the table named "rowid", "_rowid_", and "oid".
13667  */
13668  if( preserveRowid && isIPK ){
13669    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
13670    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
13671    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
13672    ** ROWID aliases.  To distinguish these cases, check to see if
13673    ** there is a "pk" entry in "PRAGMA index_list".  There will be
13674    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
13675    */
13676    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
13677                           " WHERE origin='pk'", zTab);
13678    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13679    sqlite3_free(zSql);
13680    if( rc ){
13681      freeColumnList(azCol);
13682      return 0;
13683    }
13684    rc = sqlite3_step(pStmt);
13685    sqlite3_finalize(pStmt);
13686    preserveRowid = rc==SQLITE_ROW;
13687  }
13688  if( preserveRowid ){
13689    /* Only preserve the rowid if we can find a name to use for the
13690    ** rowid */
13691    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
13692    int i, j;
13693    for(j=0; j<3; j++){
13694      for(i=1; i<=nCol; i++){
13695        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
13696      }
13697      if( i>nCol ){
13698        /* At this point, we know that azRowid[j] is not the name of any
13699        ** ordinary column in the table.  Verify that azRowid[j] is a valid
13700        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
13701        ** tables will fail this last check */
13702        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
13703        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
13704        break;
13705      }
13706    }
13707  }
13708  return azCol;
13709}
13710
13711/*
13712** Toggle the reverse_unordered_selects setting.
13713*/
13714static void toggleSelectOrder(sqlite3 *db){
13715  sqlite3_stmt *pStmt = 0;
13716  int iSetting = 0;
13717  char zStmt[100];
13718  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
13719  if( sqlite3_step(pStmt)==SQLITE_ROW ){
13720    iSetting = sqlite3_column_int(pStmt, 0);
13721  }
13722  sqlite3_finalize(pStmt);
13723  sqlite3_snprintf(sizeof(zStmt), zStmt,
13724       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
13725  sqlite3_exec(db, zStmt, 0, 0, 0);
13726}
13727
13728/*
13729** This is a different callback routine used for dumping the database.
13730** Each row received by this callback consists of a table name,
13731** the table type ("index" or "table") and SQL to create the table.
13732** This routine should print text sufficient to recreate the table.
13733*/
13734static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
13735  int rc;
13736  const char *zTable;
13737  const char *zType;
13738  const char *zSql;
13739  ShellState *p = (ShellState *)pArg;
13740  int dataOnly;
13741  int noSys;
13742
13743  UNUSED_PARAMETER(azNotUsed);
13744  if( nArg!=3 || azArg==0 ) return 0;
13745  zTable = azArg[0];
13746  zType = azArg[1];
13747  zSql = azArg[2];
13748  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
13749  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
13750
13751  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
13752    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
13753  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
13754    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
13755  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
13756    return 0;
13757  }else if( dataOnly ){
13758    /* no-op */
13759  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
13760    char *zIns;
13761    if( !p->writableSchema ){
13762      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
13763      p->writableSchema = 1;
13764    }
13765    zIns = sqlite3_mprintf(
13766       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
13767       "VALUES('table','%q','%q',0,'%q');",
13768       zTable, zTable, zSql);
13769    utf8_printf(p->out, "%s\n", zIns);
13770    sqlite3_free(zIns);
13771    return 0;
13772  }else{
13773    printSchemaLine(p->out, zSql, ";\n");
13774  }
13775
13776  if( strcmp(zType, "table")==0 ){
13777    ShellText sSelect;
13778    ShellText sTable;
13779    char **azCol;
13780    int i;
13781    char *savedDestTable;
13782    int savedMode;
13783
13784    azCol = tableColumnList(p, zTable);
13785    if( azCol==0 ){
13786      p->nErr++;
13787      return 0;
13788    }
13789
13790    /* Always quote the table name, even if it appears to be pure ascii,
13791    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
13792    initText(&sTable);
13793    appendText(&sTable, zTable, quoteChar(zTable));
13794    /* If preserving the rowid, add a column list after the table name.
13795    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
13796    ** instead of the usual "INSERT INTO tab VALUES(...)".
13797    */
13798    if( azCol[0] ){
13799      appendText(&sTable, "(", 0);
13800      appendText(&sTable, azCol[0], 0);
13801      for(i=1; azCol[i]; i++){
13802        appendText(&sTable, ",", 0);
13803        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
13804      }
13805      appendText(&sTable, ")", 0);
13806    }
13807
13808    /* Build an appropriate SELECT statement */
13809    initText(&sSelect);
13810    appendText(&sSelect, "SELECT ", 0);
13811    if( azCol[0] ){
13812      appendText(&sSelect, azCol[0], 0);
13813      appendText(&sSelect, ",", 0);
13814    }
13815    for(i=1; azCol[i]; i++){
13816      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
13817      if( azCol[i+1] ){
13818        appendText(&sSelect, ",", 0);
13819      }
13820    }
13821    freeColumnList(azCol);
13822    appendText(&sSelect, " FROM ", 0);
13823    appendText(&sSelect, zTable, quoteChar(zTable));
13824
13825    savedDestTable = p->zDestTable;
13826    savedMode = p->mode;
13827    p->zDestTable = sTable.z;
13828    p->mode = p->cMode = MODE_Insert;
13829    rc = shell_exec(p, sSelect.z, 0);
13830    if( (rc&0xff)==SQLITE_CORRUPT ){
13831      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13832      toggleSelectOrder(p->db);
13833      shell_exec(p, sSelect.z, 0);
13834      toggleSelectOrder(p->db);
13835    }
13836    p->zDestTable = savedDestTable;
13837    p->mode = savedMode;
13838    freeText(&sTable);
13839    freeText(&sSelect);
13840    if( rc ) p->nErr++;
13841  }
13842  return 0;
13843}
13844
13845/*
13846** Run zQuery.  Use dump_callback() as the callback routine so that
13847** the contents of the query are output as SQL statements.
13848**
13849** If we get a SQLITE_CORRUPT error, rerun the query after appending
13850** "ORDER BY rowid DESC" to the end.
13851*/
13852static int run_schema_dump_query(
13853  ShellState *p,
13854  const char *zQuery
13855){
13856  int rc;
13857  char *zErr = 0;
13858  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
13859  if( rc==SQLITE_CORRUPT ){
13860    char *zQ2;
13861    int len = strlen30(zQuery);
13862    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13863    if( zErr ){
13864      utf8_printf(p->out, "/****** %s ******/\n", zErr);
13865      sqlite3_free(zErr);
13866      zErr = 0;
13867    }
13868    zQ2 = malloc( len+100 );
13869    if( zQ2==0 ) return rc;
13870    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
13871    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
13872    if( rc ){
13873      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
13874    }else{
13875      rc = SQLITE_CORRUPT;
13876    }
13877    sqlite3_free(zErr);
13878    free(zQ2);
13879  }
13880  return rc;
13881}
13882
13883/*
13884** Text of help messages.
13885**
13886** The help text for each individual command begins with a line that starts
13887** with ".".  Subsequent lines are supplimental information.
13888**
13889** There must be two or more spaces between the end of the command and the
13890** start of the description of what that command does.
13891*/
13892static const char *(azHelp[]) = {
13893#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
13894  ".archive ...             Manage SQL archives",
13895  "   Each command must have exactly one of the following options:",
13896  "     -c, --create               Create a new archive",
13897  "     -u, --update               Add or update files with changed mtime",
13898  "     -i, --insert               Like -u but always add even if unchanged",
13899  "     -t, --list                 List contents of archive",
13900  "     -x, --extract              Extract files from archive",
13901  "   Optional arguments:",
13902  "     -v, --verbose              Print each filename as it is processed",
13903  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
13904  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
13905  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
13906  "     -n, --dryrun               Show the SQL that would have occurred",
13907  "   Examples:",
13908  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
13909  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
13910  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
13911  "   See also:",
13912  "      http://sqlite.org/cli.html#sqlar_archive_support",
13913#endif
13914#ifndef SQLITE_OMIT_AUTHORIZATION
13915  ".auth ON|OFF             Show authorizer callbacks",
13916#endif
13917  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
13918  "       --append            Use the appendvfs",
13919  "       --async             Write to FILE without journal and fsync()",
13920  ".bail on|off             Stop after hitting an error.  Default OFF",
13921  ".binary on|off           Turn binary output on or off.  Default OFF",
13922  ".cd DIRECTORY            Change the working directory to DIRECTORY",
13923  ".changes on|off          Show number of rows changed by SQL",
13924  ".check GLOB              Fail if output since .testcase does not match",
13925  ".clone NEWDB             Clone data into NEWDB from the existing database",
13926  ".databases               List names and files of attached databases",
13927  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
13928  ".dbinfo ?DB?             Show status information about the database",
13929  ".dump ?TABLE?            Render database content as SQL",
13930  "   Options:",
13931  "     --data-only            Output only INSERT statements",
13932  "     --newlines             Allow unescaped newline characters in output",
13933  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
13934  "     --preserve-rowids      Include ROWID values in the output",
13935  "   TABLE is a LIKE pattern for the tables to dump",
13936  "   Additional LIKE patterns can be given in subsequent arguments",
13937  ".echo on|off             Turn command echo on or off",
13938  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
13939  "   Other Modes:",
13940#ifdef SQLITE_DEBUG
13941  "      test                  Show raw EXPLAIN QUERY PLAN output",
13942  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
13943#endif
13944  "      trigger               Like \"full\" but also show trigger bytecode",
13945  ".excel                   Display the output of next command in spreadsheet",
13946  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
13947  ".exit ?CODE?             Exit this program with return-code CODE",
13948  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
13949  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
13950  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
13951  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
13952  "   --help                  Show CMD details",
13953  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
13954  ".headers on|off          Turn display of headers on or off",
13955  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
13956  ".import FILE TABLE       Import data from FILE into TABLE",
13957  "   Options:",
13958  "     --ascii               Use \\037 and \\036 as column and row separators",
13959  "     --csv                 Use , and \\n as column and row separators",
13960  "     --skip N              Skip the first N rows of input",
13961  "     -v                    \"Verbose\" - increase auxiliary output",
13962  "   Notes:",
13963  "     *  If TABLE does not exist, it is created.  The first row of input",
13964  "        determines the column names.",
13965  "     *  If neither --csv or --ascii are used, the input mode is derived",
13966  "        from the \".mode\" output mode",
13967  "     *  If FILE begins with \"|\" then it is a command that generates the",
13968  "        input text.",
13969#ifndef SQLITE_OMIT_TEST_CONTROL
13970  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
13971#endif
13972  ".indexes ?TABLE?         Show names of indexes",
13973  "                           If TABLE is specified, only show indexes for",
13974  "                           tables matching TABLE using the LIKE operator.",
13975#ifdef SQLITE_ENABLE_IOTRACE
13976  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
13977#endif
13978  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
13979  ".lint OPTIONS            Report potential schema issues.",
13980  "     Options:",
13981  "        fkey-indexes     Find missing foreign key indexes",
13982#ifndef SQLITE_OMIT_LOAD_EXTENSION
13983  ".load FILE ?ENTRY?       Load an extension library",
13984#endif
13985  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
13986  ".mode MODE ?TABLE?       Set output mode",
13987  "   MODE is one of:",
13988  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
13989  "     box       Tables using unicode box-drawing characters",
13990  "     csv       Comma-separated values",
13991  "     column    Output in columns.  (See .width)",
13992  "     html      HTML <table> code",
13993  "     insert    SQL insert statements for TABLE",
13994  "     json      Results in a JSON array",
13995  "     line      One value per line",
13996  "     list      Values delimited by \"|\"",
13997  "     markdown  Markdown table format",
13998  "     quote     Escape answers as for SQL",
13999  "     table     ASCII-art table",
14000  "     tabs      Tab-separated values",
14001  "     tcl       TCL list elements",
14002  ".nullvalue STRING        Use STRING in place of NULL values",
14003  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
14004  "     If FILE begins with '|' then open as a pipe",
14005  "       --bom  Put a UTF8 byte-order mark at the beginning",
14006  "       -e     Send output to the system text editor",
14007  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
14008#ifdef SQLITE_DEBUG
14009  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
14010#endif
14011  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
14012  "     Options:",
14013  "        --append        Use appendvfs to append database to the end of FILE",
14014#ifdef SQLITE_ENABLE_DESERIALIZE
14015  "        --deserialize   Load into memory useing sqlite3_deserialize()",
14016  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
14017  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
14018#endif
14019  "        --new           Initialize FILE to an empty database",
14020  "        --nofollow      Do not follow symbolic links",
14021  "        --readonly      Open FILE readonly",
14022  "        --zip           FILE is a ZIP archive",
14023  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
14024  "   If FILE begins with '|' then open it as a pipe.",
14025  "   Options:",
14026  "     --bom                 Prefix output with a UTF8 byte-order mark",
14027  "     -e                    Send output to the system text editor",
14028  "     -x                    Send output as CSV to a spreadsheet",
14029  ".parameter CMD ...       Manage SQL parameter bindings",
14030  "   clear                   Erase all bindings",
14031  "   init                    Initialize the TEMP table that holds bindings",
14032  "   list                    List the current parameter bindings",
14033  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
14034  "                           PARAMETER should start with one of: $ : @ ?",
14035  "   unset PARAMETER         Remove PARAMETER from the binding table",
14036  ".print STRING...         Print literal STRING",
14037#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14038  ".progress N              Invoke progress handler after every N opcodes",
14039  "   --limit N                 Interrupt after N progress callbacks",
14040  "   --once                    Do no more than one progress interrupt",
14041  "   --quiet|-q                No output except at interrupts",
14042  "   --reset                   Reset the count for each input and interrupt",
14043#endif
14044  ".prompt MAIN CONTINUE    Replace the standard prompts",
14045  ".quit                    Exit this program",
14046  ".read FILE               Read input from FILE",
14047#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14048  ".recover                 Recover as much data as possible from corrupt db.",
14049  "   --freelist-corrupt       Assume the freelist is corrupt",
14050  "   --recovery-db NAME       Store recovery metadata in database file NAME",
14051  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
14052  "   --no-rowids              Do not attempt to recover rowid values",
14053  "                            that are not also INTEGER PRIMARY KEYs",
14054#endif
14055  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
14056  ".save FILE               Write in-memory database into FILE",
14057  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
14058  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
14059  "   Options:",
14060  "      --indent             Try to pretty-print the schema",
14061  "      --nosys              Omit objects whose names start with \"sqlite_\"",
14062  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
14063  "    Options:",
14064  "       --init               Create a new SELFTEST table",
14065  "       -v                   Verbose output",
14066  ".separator COL ?ROW?     Change the column and row separators",
14067#if defined(SQLITE_ENABLE_SESSION)
14068  ".session ?NAME? CMD ...  Create or control sessions",
14069  "   Subcommands:",
14070  "     attach TABLE             Attach TABLE",
14071  "     changeset FILE           Write a changeset into FILE",
14072  "     close                    Close one session",
14073  "     enable ?BOOLEAN?         Set or query the enable bit",
14074  "     filter GLOB...           Reject tables matching GLOBs",
14075  "     indirect ?BOOLEAN?       Mark or query the indirect status",
14076  "     isempty                  Query whether the session is empty",
14077  "     list                     List currently open session names",
14078  "     open DB NAME             Open a new session on DB",
14079  "     patchset FILE            Write a patchset into FILE",
14080  "   If ?NAME? is omitted, the first defined session is used.",
14081#endif
14082  ".sha3sum ...             Compute a SHA3 hash of database content",
14083  "    Options:",
14084  "      --schema              Also hash the sqlite_schema table",
14085  "      --sha3-224            Use the sha3-224 algorithm",
14086  "      --sha3-256            Use the sha3-256 algorithm (default)",
14087  "      --sha3-384            Use the sha3-384 algorithm",
14088  "      --sha3-512            Use the sha3-512 algorithm",
14089  "    Any other argument is a LIKE pattern for tables to hash",
14090#ifndef SQLITE_NOHAVE_SYSTEM
14091  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
14092#endif
14093  ".show                    Show the current values for various settings",
14094  ".stats ?on|off?          Show stats or turn stats on or off",
14095#ifndef SQLITE_NOHAVE_SYSTEM
14096  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
14097#endif
14098  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
14099  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
14100  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
14101  "                           Run \".testctrl\" with no arguments for details",
14102  ".timeout MS              Try opening locked tables for MS milliseconds",
14103  ".timer on|off            Turn SQL timer on or off",
14104#ifndef SQLITE_OMIT_TRACE
14105  ".trace ?OPTIONS?         Output each SQL statement as it is run",
14106  "    FILE                    Send output to FILE",
14107  "    stdout                  Send output to stdout",
14108  "    stderr                  Send output to stderr",
14109  "    off                     Disable tracing",
14110  "    --expanded              Expand query parameters",
14111#ifdef SQLITE_ENABLE_NORMALIZE
14112  "    --normalized            Normal the SQL statements",
14113#endif
14114  "    --plain                 Show SQL as it is input",
14115  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
14116  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
14117  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
14118  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
14119#endif /* SQLITE_OMIT_TRACE */
14120#ifdef SQLITE_DEBUG
14121  ".unmodule NAME ...       Unregister virtual table modules",
14122  "    --allexcept             Unregister everything except those named",
14123#endif
14124  ".vfsinfo ?AUX?           Information about the top-level VFS",
14125  ".vfslist                 List all available VFSes",
14126  ".vfsname ?AUX?           Print the name of the VFS stack",
14127  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
14128  "     Negative values right-justify",
14129};
14130
14131/*
14132** Output help text.
14133**
14134** zPattern describes the set of commands for which help text is provided.
14135** If zPattern is NULL, then show all commands, but only give a one-line
14136** description of each.
14137**
14138** Return the number of matches.
14139*/
14140static int showHelp(FILE *out, const char *zPattern){
14141  int i = 0;
14142  int j = 0;
14143  int n = 0;
14144  char *zPat;
14145  if( zPattern==0
14146   || zPattern[0]=='0'
14147   || strcmp(zPattern,"-a")==0
14148   || strcmp(zPattern,"-all")==0
14149   || strcmp(zPattern,"--all")==0
14150  ){
14151    /* Show all commands, but only one line per command */
14152    if( zPattern==0 ) zPattern = "";
14153    for(i=0; i<ArraySize(azHelp); i++){
14154      if( azHelp[i][0]=='.' || zPattern[0] ){
14155        utf8_printf(out, "%s\n", azHelp[i]);
14156        n++;
14157      }
14158    }
14159  }else{
14160    /* Look for commands that for which zPattern is an exact prefix */
14161    zPat = sqlite3_mprintf(".%s*", zPattern);
14162    for(i=0; i<ArraySize(azHelp); i++){
14163      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
14164        utf8_printf(out, "%s\n", azHelp[i]);
14165        j = i+1;
14166        n++;
14167      }
14168    }
14169    sqlite3_free(zPat);
14170    if( n ){
14171      if( n==1 ){
14172        /* when zPattern is a prefix of exactly one command, then include the
14173        ** details of that command, which should begin at offset j */
14174        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
14175          utf8_printf(out, "%s\n", azHelp[j]);
14176          j++;
14177        }
14178      }
14179      return n;
14180    }
14181    /* Look for commands that contain zPattern anywhere.  Show the complete
14182    ** text of all commands that match. */
14183    zPat = sqlite3_mprintf("%%%s%%", zPattern);
14184    for(i=0; i<ArraySize(azHelp); i++){
14185      if( azHelp[i][0]=='.' ) j = i;
14186      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
14187        utf8_printf(out, "%s\n", azHelp[j]);
14188        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
14189          j++;
14190          utf8_printf(out, "%s\n", azHelp[j]);
14191        }
14192        i = j;
14193        n++;
14194      }
14195    }
14196    sqlite3_free(zPat);
14197  }
14198  return n;
14199}
14200
14201/* Forward reference */
14202static int process_input(ShellState *p);
14203
14204/*
14205** Read the content of file zName into memory obtained from sqlite3_malloc64()
14206** and return a pointer to the buffer. The caller is responsible for freeing
14207** the memory.
14208**
14209** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
14210** read.
14211**
14212** For convenience, a nul-terminator byte is always appended to the data read
14213** from the file before the buffer is returned. This byte is not included in
14214** the final value of (*pnByte), if applicable.
14215**
14216** NULL is returned if any error is encountered. The final value of *pnByte
14217** is undefined in this case.
14218*/
14219static char *readFile(const char *zName, int *pnByte){
14220  FILE *in = fopen(zName, "rb");
14221  long nIn;
14222  size_t nRead;
14223  char *pBuf;
14224  if( in==0 ) return 0;
14225  fseek(in, 0, SEEK_END);
14226  nIn = ftell(in);
14227  rewind(in);
14228  pBuf = sqlite3_malloc64( nIn+1 );
14229  if( pBuf==0 ){ fclose(in); return 0; }
14230  nRead = fread(pBuf, nIn, 1, in);
14231  fclose(in);
14232  if( nRead!=1 ){
14233    sqlite3_free(pBuf);
14234    return 0;
14235  }
14236  pBuf[nIn] = 0;
14237  if( pnByte ) *pnByte = nIn;
14238  return pBuf;
14239}
14240
14241#if defined(SQLITE_ENABLE_SESSION)
14242/*
14243** Close a single OpenSession object and release all of its associated
14244** resources.
14245*/
14246static void session_close(OpenSession *pSession){
14247  int i;
14248  sqlite3session_delete(pSession->p);
14249  sqlite3_free(pSession->zName);
14250  for(i=0; i<pSession->nFilter; i++){
14251    sqlite3_free(pSession->azFilter[i]);
14252  }
14253  sqlite3_free(pSession->azFilter);
14254  memset(pSession, 0, sizeof(OpenSession));
14255}
14256#endif
14257
14258/*
14259** Close all OpenSession objects and release all associated resources.
14260*/
14261#if defined(SQLITE_ENABLE_SESSION)
14262static void session_close_all(ShellState *p){
14263  int i;
14264  for(i=0; i<p->nSession; i++){
14265    session_close(&p->aSession[i]);
14266  }
14267  p->nSession = 0;
14268}
14269#else
14270# define session_close_all(X)
14271#endif
14272
14273/*
14274** Implementation of the xFilter function for an open session.  Omit
14275** any tables named by ".session filter" but let all other table through.
14276*/
14277#if defined(SQLITE_ENABLE_SESSION)
14278static int session_filter(void *pCtx, const char *zTab){
14279  OpenSession *pSession = (OpenSession*)pCtx;
14280  int i;
14281  for(i=0; i<pSession->nFilter; i++){
14282    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
14283  }
14284  return 1;
14285}
14286#endif
14287
14288/*
14289** Try to deduce the type of file for zName based on its content.  Return
14290** one of the SHELL_OPEN_* constants.
14291**
14292** If the file does not exist or is empty but its name looks like a ZIP
14293** archive and the dfltZip flag is true, then assume it is a ZIP archive.
14294** Otherwise, assume an ordinary database regardless of the filename if
14295** the type cannot be determined from content.
14296*/
14297int deduceDatabaseType(const char *zName, int dfltZip){
14298  FILE *f = fopen(zName, "rb");
14299  size_t n;
14300  int rc = SHELL_OPEN_UNSPEC;
14301  char zBuf[100];
14302  if( f==0 ){
14303    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14304       return SHELL_OPEN_ZIPFILE;
14305    }else{
14306       return SHELL_OPEN_NORMAL;
14307    }
14308  }
14309  n = fread(zBuf, 16, 1, f);
14310  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
14311    fclose(f);
14312    return SHELL_OPEN_NORMAL;
14313  }
14314  fseek(f, -25, SEEK_END);
14315  n = fread(zBuf, 25, 1, f);
14316  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
14317    rc = SHELL_OPEN_APPENDVFS;
14318  }else{
14319    fseek(f, -22, SEEK_END);
14320    n = fread(zBuf, 22, 1, f);
14321    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
14322       && zBuf[3]==0x06 ){
14323      rc = SHELL_OPEN_ZIPFILE;
14324    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14325      rc = SHELL_OPEN_ZIPFILE;
14326    }
14327  }
14328  fclose(f);
14329  return rc;
14330}
14331
14332#ifdef SQLITE_ENABLE_DESERIALIZE
14333/*
14334** Reconstruct an in-memory database using the output from the "dbtotxt"
14335** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
14336** is 0, then read from standard input.
14337*/
14338static unsigned char *readHexDb(ShellState *p, int *pnData){
14339  unsigned char *a = 0;
14340  int nLine;
14341  int n = 0;
14342  int pgsz = 0;
14343  int iOffset = 0;
14344  int j, k;
14345  int rc;
14346  FILE *in;
14347  unsigned int x[16];
14348  char zLine[1000];
14349  if( p->zDbFilename ){
14350    in = fopen(p->zDbFilename, "r");
14351    if( in==0 ){
14352      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
14353      return 0;
14354    }
14355    nLine = 0;
14356  }else{
14357    in = p->in;
14358    nLine = p->lineno;
14359    if( in==0 ) in = stdin;
14360  }
14361  *pnData = 0;
14362  nLine++;
14363  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
14364  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
14365  if( rc!=2 ) goto readHexDb_error;
14366  if( n<0 ) goto readHexDb_error;
14367  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
14368  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
14369  a = sqlite3_malloc( n ? n : 1 );
14370  if( a==0 ){
14371    utf8_printf(stderr, "Out of memory!\n");
14372    goto readHexDb_error;
14373  }
14374  memset(a, 0, n);
14375  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
14376    utf8_printf(stderr, "invalid pagesize\n");
14377    goto readHexDb_error;
14378  }
14379  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
14380    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
14381    if( rc==2 ){
14382      iOffset = k;
14383      continue;
14384    }
14385    if( strncmp(zLine, "| end ", 6)==0 ){
14386      break;
14387    }
14388    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
14389                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
14390                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
14391    if( rc==17 ){
14392      k = iOffset+j;
14393      if( k+16<=n ){
14394        int ii;
14395        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
14396      }
14397    }
14398  }
14399  *pnData = n;
14400  if( in!=p->in ){
14401    fclose(in);
14402  }else{
14403    p->lineno = nLine;
14404  }
14405  return a;
14406
14407readHexDb_error:
14408  if( in!=p->in ){
14409    fclose(in);
14410  }else{
14411    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
14412      nLine++;
14413      if(strncmp(zLine, "| end ", 6)==0 ) break;
14414    }
14415    p->lineno = nLine;
14416  }
14417  sqlite3_free(a);
14418  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
14419  return 0;
14420}
14421#endif /* SQLITE_ENABLE_DESERIALIZE */
14422
14423/*
14424** Scalar function "shell_int32". The first argument to this function
14425** must be a blob. The second a non-negative integer. This function
14426** reads and returns a 32-bit big-endian integer from byte
14427** offset (4*<arg2>) of the blob.
14428*/
14429static void shellInt32(
14430  sqlite3_context *context,
14431  int argc,
14432  sqlite3_value **argv
14433){
14434  const unsigned char *pBlob;
14435  int nBlob;
14436  int iInt;
14437
14438  UNUSED_PARAMETER(argc);
14439  nBlob = sqlite3_value_bytes(argv[0]);
14440  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
14441  iInt = sqlite3_value_int(argv[1]);
14442
14443  if( iInt>=0 && (iInt+1)*4<=nBlob ){
14444    const unsigned char *a = &pBlob[iInt*4];
14445    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
14446                       + ((sqlite3_int64)a[1]<<16)
14447                       + ((sqlite3_int64)a[2]<< 8)
14448                       + ((sqlite3_int64)a[3]<< 0);
14449    sqlite3_result_int64(context, iVal);
14450  }
14451}
14452
14453/*
14454** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
14455** using "..." with internal double-quote characters doubled.
14456*/
14457static void shellIdQuote(
14458  sqlite3_context *context,
14459  int argc,
14460  sqlite3_value **argv
14461){
14462  const char *zName = (const char*)sqlite3_value_text(argv[0]);
14463  UNUSED_PARAMETER(argc);
14464  if( zName ){
14465    char *z = sqlite3_mprintf("\"%w\"", zName);
14466    sqlite3_result_text(context, z, -1, sqlite3_free);
14467  }
14468}
14469
14470/*
14471** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
14472*/
14473static void shellUSleepFunc(
14474  sqlite3_context *context,
14475  int argcUnused,
14476  sqlite3_value **argv
14477){
14478  int sleep = sqlite3_value_int(argv[0]);
14479  (void)argcUnused;
14480  sqlite3_sleep(sleep/1000);
14481  sqlite3_result_int(context, sleep);
14482}
14483
14484/*
14485** Scalar function "shell_escape_crnl" used by the .recover command.
14486** The argument passed to this function is the output of built-in
14487** function quote(). If the first character of the input is "'",
14488** indicating that the value passed to quote() was a text value,
14489** then this function searches the input for "\n" and "\r" characters
14490** and adds a wrapper similar to the following:
14491**
14492**   replace(replace(<input>, '\n', char(10), '\r', char(13));
14493**
14494** Or, if the first character of the input is not "'", then a copy
14495** of the input is returned.
14496*/
14497static void shellEscapeCrnl(
14498  sqlite3_context *context,
14499  int argc,
14500  sqlite3_value **argv
14501){
14502  const char *zText = (const char*)sqlite3_value_text(argv[0]);
14503  UNUSED_PARAMETER(argc);
14504  if( zText[0]=='\'' ){
14505    int nText = sqlite3_value_bytes(argv[0]);
14506    int i;
14507    char zBuf1[20];
14508    char zBuf2[20];
14509    const char *zNL = 0;
14510    const char *zCR = 0;
14511    int nCR = 0;
14512    int nNL = 0;
14513
14514    for(i=0; zText[i]; i++){
14515      if( zNL==0 && zText[i]=='\n' ){
14516        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
14517        nNL = (int)strlen(zNL);
14518      }
14519      if( zCR==0 && zText[i]=='\r' ){
14520        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
14521        nCR = (int)strlen(zCR);
14522      }
14523    }
14524
14525    if( zNL || zCR ){
14526      int iOut = 0;
14527      i64 nMax = (nNL > nCR) ? nNL : nCR;
14528      i64 nAlloc = nMax * nText + (nMax+64)*2;
14529      char *zOut = (char*)sqlite3_malloc64(nAlloc);
14530      if( zOut==0 ){
14531        sqlite3_result_error_nomem(context);
14532        return;
14533      }
14534
14535      if( zNL && zCR ){
14536        memcpy(&zOut[iOut], "replace(replace(", 16);
14537        iOut += 16;
14538      }else{
14539        memcpy(&zOut[iOut], "replace(", 8);
14540        iOut += 8;
14541      }
14542      for(i=0; zText[i]; i++){
14543        if( zText[i]=='\n' ){
14544          memcpy(&zOut[iOut], zNL, nNL);
14545          iOut += nNL;
14546        }else if( zText[i]=='\r' ){
14547          memcpy(&zOut[iOut], zCR, nCR);
14548          iOut += nCR;
14549        }else{
14550          zOut[iOut] = zText[i];
14551          iOut++;
14552        }
14553      }
14554
14555      if( zNL ){
14556        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14557        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
14558        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
14559      }
14560      if( zCR ){
14561        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14562        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
14563        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
14564      }
14565
14566      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
14567      sqlite3_free(zOut);
14568      return;
14569    }
14570  }
14571
14572  sqlite3_result_value(context, argv[0]);
14573}
14574
14575/* Flags for open_db().
14576**
14577** The default behavior of open_db() is to exit(1) if the database fails to
14578** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
14579** but still returns without calling exit.
14580**
14581** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
14582** ZIP archive if the file does not exist or is empty and its name matches
14583** the *.zip pattern.
14584*/
14585#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
14586#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
14587
14588/*
14589** Make sure the database is open.  If it is not, then open it.  If
14590** the database fails to open, print an error message and exit.
14591*/
14592static void open_db(ShellState *p, int openFlags){
14593  if( p->db==0 ){
14594    if( p->openMode==SHELL_OPEN_UNSPEC ){
14595      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
14596        p->openMode = SHELL_OPEN_NORMAL;
14597      }else{
14598        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
14599                             (openFlags & OPEN_DB_ZIPFILE)!=0);
14600      }
14601    }
14602    switch( p->openMode ){
14603      case SHELL_OPEN_APPENDVFS: {
14604        sqlite3_open_v2(p->zDbFilename, &p->db,
14605           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
14606        break;
14607      }
14608      case SHELL_OPEN_HEXDB:
14609      case SHELL_OPEN_DESERIALIZE: {
14610        sqlite3_open(0, &p->db);
14611        break;
14612      }
14613      case SHELL_OPEN_ZIPFILE: {
14614        sqlite3_open(":memory:", &p->db);
14615        break;
14616      }
14617      case SHELL_OPEN_READONLY: {
14618        sqlite3_open_v2(p->zDbFilename, &p->db,
14619            SQLITE_OPEN_READONLY|p->openFlags, 0);
14620        break;
14621      }
14622      case SHELL_OPEN_UNSPEC:
14623      case SHELL_OPEN_NORMAL: {
14624        sqlite3_open_v2(p->zDbFilename, &p->db,
14625           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
14626        break;
14627      }
14628    }
14629    globalDb = p->db;
14630    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
14631      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
14632          p->zDbFilename, sqlite3_errmsg(p->db));
14633      if( openFlags & OPEN_DB_KEEPALIVE ){
14634        sqlite3_open(":memory:", &p->db);
14635        return;
14636      }
14637      exit(1);
14638    }
14639#ifndef SQLITE_OMIT_LOAD_EXTENSION
14640    sqlite3_enable_load_extension(p->db, 1);
14641#endif
14642    sqlite3_fileio_init(p->db, 0, 0);
14643    sqlite3_shathree_init(p->db, 0, 0);
14644    sqlite3_completion_init(p->db, 0, 0);
14645    sqlite3_uint_init(p->db, 0, 0);
14646    sqlite3_decimal_init(p->db, 0, 0);
14647    sqlite3_ieee_init(p->db, 0, 0);
14648    sqlite3_series_init(p->db, 0, 0);
14649#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14650    sqlite3_dbdata_init(p->db, 0, 0);
14651#endif
14652#ifdef SQLITE_HAVE_ZLIB
14653    sqlite3_zipfile_init(p->db, 0, 0);
14654    sqlite3_sqlar_init(p->db, 0, 0);
14655#endif
14656    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
14657                            shellAddSchemaName, 0, 0);
14658    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
14659                            shellModuleSchema, 0, 0);
14660    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
14661                            shellPutsFunc, 0, 0);
14662    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
14663                            shellEscapeCrnl, 0, 0);
14664    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
14665                            shellInt32, 0, 0);
14666    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
14667                            shellIdQuote, 0, 0);
14668    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
14669                            shellUSleepFunc, 0, 0);
14670#ifndef SQLITE_NOHAVE_SYSTEM
14671    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
14672                            editFunc, 0, 0);
14673    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
14674                            editFunc, 0, 0);
14675#endif
14676    if( p->openMode==SHELL_OPEN_ZIPFILE ){
14677      char *zSql = sqlite3_mprintf(
14678         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
14679      sqlite3_exec(p->db, zSql, 0, 0, 0);
14680      sqlite3_free(zSql);
14681    }
14682#ifdef SQLITE_ENABLE_DESERIALIZE
14683    else
14684    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
14685      int rc;
14686      int nData = 0;
14687      unsigned char *aData;
14688      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
14689        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
14690      }else{
14691        aData = readHexDb(p, &nData);
14692        if( aData==0 ){
14693          return;
14694        }
14695      }
14696      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
14697                   SQLITE_DESERIALIZE_RESIZEABLE |
14698                   SQLITE_DESERIALIZE_FREEONCLOSE);
14699      if( rc ){
14700        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
14701      }
14702      if( p->szMax>0 ){
14703        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
14704      }
14705    }
14706#endif
14707  }
14708}
14709
14710/*
14711** Attempt to close the databaes connection.  Report errors.
14712*/
14713void close_db(sqlite3 *db){
14714  int rc = sqlite3_close(db);
14715  if( rc ){
14716    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
14717        rc, sqlite3_errmsg(db));
14718  }
14719}
14720
14721#if HAVE_READLINE || HAVE_EDITLINE
14722/*
14723** Readline completion callbacks
14724*/
14725static char *readline_completion_generator(const char *text, int state){
14726  static sqlite3_stmt *pStmt = 0;
14727  char *zRet;
14728  if( state==0 ){
14729    char *zSql;
14730    sqlite3_finalize(pStmt);
14731    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14732                           "  FROM completion(%Q) ORDER BY 1", text);
14733    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14734    sqlite3_free(zSql);
14735  }
14736  if( sqlite3_step(pStmt)==SQLITE_ROW ){
14737    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
14738  }else{
14739    sqlite3_finalize(pStmt);
14740    pStmt = 0;
14741    zRet = 0;
14742  }
14743  return zRet;
14744}
14745static char **readline_completion(const char *zText, int iStart, int iEnd){
14746  rl_attempted_completion_over = 1;
14747  return rl_completion_matches(zText, readline_completion_generator);
14748}
14749
14750#elif HAVE_LINENOISE
14751/*
14752** Linenoise completion callback
14753*/
14754static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
14755  int nLine = strlen30(zLine);
14756  int i, iStart;
14757  sqlite3_stmt *pStmt = 0;
14758  char *zSql;
14759  char zBuf[1000];
14760
14761  if( nLine>sizeof(zBuf)-30 ) return;
14762  if( zLine[0]=='.' || zLine[0]=='#') return;
14763  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
14764  if( i==nLine-1 ) return;
14765  iStart = i+1;
14766  memcpy(zBuf, zLine, iStart);
14767  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14768                         "  FROM completion(%Q,%Q) ORDER BY 1",
14769                         &zLine[iStart], zLine);
14770  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14771  sqlite3_free(zSql);
14772  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
14773  while( sqlite3_step(pStmt)==SQLITE_ROW ){
14774    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
14775    int nCompletion = sqlite3_column_bytes(pStmt, 0);
14776    if( iStart+nCompletion < sizeof(zBuf)-1 ){
14777      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
14778      linenoiseAddCompletion(lc, zBuf);
14779    }
14780  }
14781  sqlite3_finalize(pStmt);
14782}
14783#endif
14784
14785/*
14786** Do C-language style dequoting.
14787**
14788**    \a    -> alarm
14789**    \b    -> backspace
14790**    \t    -> tab
14791**    \n    -> newline
14792**    \v    -> vertical tab
14793**    \f    -> form feed
14794**    \r    -> carriage return
14795**    \s    -> space
14796**    \"    -> "
14797**    \'    -> '
14798**    \\    -> backslash
14799**    \NNN  -> ascii character NNN in octal
14800*/
14801static void resolve_backslashes(char *z){
14802  int i, j;
14803  char c;
14804  while( *z && *z!='\\' ) z++;
14805  for(i=j=0; (c = z[i])!=0; i++, j++){
14806    if( c=='\\' && z[i+1]!=0 ){
14807      c = z[++i];
14808      if( c=='a' ){
14809        c = '\a';
14810      }else if( c=='b' ){
14811        c = '\b';
14812      }else if( c=='t' ){
14813        c = '\t';
14814      }else if( c=='n' ){
14815        c = '\n';
14816      }else if( c=='v' ){
14817        c = '\v';
14818      }else if( c=='f' ){
14819        c = '\f';
14820      }else if( c=='r' ){
14821        c = '\r';
14822      }else if( c=='"' ){
14823        c = '"';
14824      }else if( c=='\'' ){
14825        c = '\'';
14826      }else if( c=='\\' ){
14827        c = '\\';
14828      }else if( c>='0' && c<='7' ){
14829        c -= '0';
14830        if( z[i+1]>='0' && z[i+1]<='7' ){
14831          i++;
14832          c = (c<<3) + z[i] - '0';
14833          if( z[i+1]>='0' && z[i+1]<='7' ){
14834            i++;
14835            c = (c<<3) + z[i] - '0';
14836          }
14837        }
14838      }
14839    }
14840    z[j] = c;
14841  }
14842  if( j<i ) z[j] = 0;
14843}
14844
14845/*
14846** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
14847** for TRUE and FALSE.  Return the integer value if appropriate.
14848*/
14849static int booleanValue(const char *zArg){
14850  int i;
14851  if( zArg[0]=='0' && zArg[1]=='x' ){
14852    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
14853  }else{
14854    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
14855  }
14856  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
14857  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
14858    return 1;
14859  }
14860  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
14861    return 0;
14862  }
14863  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
14864          zArg);
14865  return 0;
14866}
14867
14868/*
14869** Set or clear a shell flag according to a boolean value.
14870*/
14871static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
14872  if( booleanValue(zArg) ){
14873    ShellSetFlag(p, mFlag);
14874  }else{
14875    ShellClearFlag(p, mFlag);
14876  }
14877}
14878
14879/*
14880** Close an output file, assuming it is not stderr or stdout
14881*/
14882static void output_file_close(FILE *f){
14883  if( f && f!=stdout && f!=stderr ) fclose(f);
14884}
14885
14886/*
14887** Try to open an output file.   The names "stdout" and "stderr" are
14888** recognized and do the right thing.  NULL is returned if the output
14889** filename is "off".
14890*/
14891static FILE *output_file_open(const char *zFile, int bTextMode){
14892  FILE *f;
14893  if( strcmp(zFile,"stdout")==0 ){
14894    f = stdout;
14895  }else if( strcmp(zFile, "stderr")==0 ){
14896    f = stderr;
14897  }else if( strcmp(zFile, "off")==0 ){
14898    f = 0;
14899  }else{
14900    f = fopen(zFile, bTextMode ? "w" : "wb");
14901    if( f==0 ){
14902      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14903    }
14904  }
14905  return f;
14906}
14907
14908#ifndef SQLITE_OMIT_TRACE
14909/*
14910** A routine for handling output from sqlite3_trace().
14911*/
14912static int sql_trace_callback(
14913  unsigned mType,         /* The trace type */
14914  void *pArg,             /* The ShellState pointer */
14915  void *pP,               /* Usually a pointer to sqlite_stmt */
14916  void *pX                /* Auxiliary output */
14917){
14918  ShellState *p = (ShellState*)pArg;
14919  sqlite3_stmt *pStmt;
14920  const char *zSql;
14921  int nSql;
14922  if( p->traceOut==0 ) return 0;
14923  if( mType==SQLITE_TRACE_CLOSE ){
14924    utf8_printf(p->traceOut, "-- closing database connection\n");
14925    return 0;
14926  }
14927  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
14928    zSql = (const char*)pX;
14929  }else{
14930    pStmt = (sqlite3_stmt*)pP;
14931    switch( p->eTraceType ){
14932      case SHELL_TRACE_EXPANDED: {
14933        zSql = sqlite3_expanded_sql(pStmt);
14934        break;
14935      }
14936#ifdef SQLITE_ENABLE_NORMALIZE
14937      case SHELL_TRACE_NORMALIZED: {
14938        zSql = sqlite3_normalized_sql(pStmt);
14939        break;
14940      }
14941#endif
14942      default: {
14943        zSql = sqlite3_sql(pStmt);
14944        break;
14945      }
14946    }
14947  }
14948  if( zSql==0 ) return 0;
14949  nSql = strlen30(zSql);
14950  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
14951  switch( mType ){
14952    case SQLITE_TRACE_ROW:
14953    case SQLITE_TRACE_STMT: {
14954      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
14955      break;
14956    }
14957    case SQLITE_TRACE_PROFILE: {
14958      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
14959      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
14960      break;
14961    }
14962  }
14963  return 0;
14964}
14965#endif
14966
14967/*
14968** A no-op routine that runs with the ".breakpoint" doc-command.  This is
14969** a useful spot to set a debugger breakpoint.
14970*/
14971static void test_breakpoint(void){
14972  static int nCall = 0;
14973  nCall++;
14974}
14975
14976/*
14977** An object used to read a CSV and other files for import.
14978*/
14979typedef struct ImportCtx ImportCtx;
14980struct ImportCtx {
14981  const char *zFile;  /* Name of the input file */
14982  FILE *in;           /* Read the CSV text from this input stream */
14983  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
14984  char *z;            /* Accumulated text for a field */
14985  int n;              /* Number of bytes in z */
14986  int nAlloc;         /* Space allocated for z[] */
14987  int nLine;          /* Current line number */
14988  int nRow;           /* Number of rows imported */
14989  int nErr;           /* Number of errors encountered */
14990  int bNotFirst;      /* True if one or more bytes already read */
14991  int cTerm;          /* Character that terminated the most recent field */
14992  int cColSep;        /* The column separator character.  (Usually ",") */
14993  int cRowSep;        /* The row separator character.  (Usually "\n") */
14994};
14995
14996/* Clean up resourced used by an ImportCtx */
14997static void import_cleanup(ImportCtx *p){
14998  if( p->in!=0 && p->xCloser!=0 ){
14999    p->xCloser(p->in);
15000    p->in = 0;
15001  }
15002  sqlite3_free(p->z);
15003  p->z = 0;
15004}
15005
15006/* Append a single byte to z[] */
15007static void import_append_char(ImportCtx *p, int c){
15008  if( p->n+1>=p->nAlloc ){
15009    p->nAlloc += p->nAlloc + 100;
15010    p->z = sqlite3_realloc64(p->z, p->nAlloc);
15011    if( p->z==0 ) shell_out_of_memory();
15012  }
15013  p->z[p->n++] = (char)c;
15014}
15015
15016/* Read a single field of CSV text.  Compatible with rfc4180 and extended
15017** with the option of having a separator other than ",".
15018**
15019**   +  Input comes from p->in.
15020**   +  Store results in p->z of length p->n.  Space to hold p->z comes
15021**      from sqlite3_malloc64().
15022**   +  Use p->cSep as the column separator.  The default is ",".
15023**   +  Use p->rSep as the row separator.  The default is "\n".
15024**   +  Keep track of the line number in p->nLine.
15025**   +  Store the character that terminates the field in p->cTerm.  Store
15026**      EOF on end-of-file.
15027**   +  Report syntax errors on stderr
15028*/
15029static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15030  int c;
15031  int cSep = p->cColSep;
15032  int rSep = p->cRowSep;
15033  p->n = 0;
15034  c = fgetc(p->in);
15035  if( c==EOF || seenInterrupt ){
15036    p->cTerm = EOF;
15037    return 0;
15038  }
15039  if( c=='"' ){
15040    int pc, ppc;
15041    int startLine = p->nLine;
15042    int cQuote = c;
15043    pc = ppc = 0;
15044    while( 1 ){
15045      c = fgetc(p->in);
15046      if( c==rSep ) p->nLine++;
15047      if( c==cQuote ){
15048        if( pc==cQuote ){
15049          pc = 0;
15050          continue;
15051        }
15052      }
15053      if( (c==cSep && pc==cQuote)
15054       || (c==rSep && pc==cQuote)
15055       || (c==rSep && pc=='\r' && ppc==cQuote)
15056       || (c==EOF && pc==cQuote)
15057      ){
15058        do{ p->n--; }while( p->z[p->n]!=cQuote );
15059        p->cTerm = c;
15060        break;
15061      }
15062      if( pc==cQuote && c!='\r' ){
15063        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15064                p->zFile, p->nLine, cQuote);
15065      }
15066      if( c==EOF ){
15067        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15068                p->zFile, startLine, cQuote);
15069        p->cTerm = c;
15070        break;
15071      }
15072      import_append_char(p, c);
15073      ppc = pc;
15074      pc = c;
15075    }
15076  }else{
15077    /* If this is the first field being parsed and it begins with the
15078    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
15079    if( (c&0xff)==0xef && p->bNotFirst==0 ){
15080      import_append_char(p, c);
15081      c = fgetc(p->in);
15082      if( (c&0xff)==0xbb ){
15083        import_append_char(p, c);
15084        c = fgetc(p->in);
15085        if( (c&0xff)==0xbf ){
15086          p->bNotFirst = 1;
15087          p->n = 0;
15088          return csv_read_one_field(p);
15089        }
15090      }
15091    }
15092    while( c!=EOF && c!=cSep && c!=rSep ){
15093      import_append_char(p, c);
15094      c = fgetc(p->in);
15095    }
15096    if( c==rSep ){
15097      p->nLine++;
15098      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
15099    }
15100    p->cTerm = c;
15101  }
15102  if( p->z ) p->z[p->n] = 0;
15103  p->bNotFirst = 1;
15104  return p->z;
15105}
15106
15107/* Read a single field of ASCII delimited text.
15108**
15109**   +  Input comes from p->in.
15110**   +  Store results in p->z of length p->n.  Space to hold p->z comes
15111**      from sqlite3_malloc64().
15112**   +  Use p->cSep as the column separator.  The default is "\x1F".
15113**   +  Use p->rSep as the row separator.  The default is "\x1E".
15114**   +  Keep track of the row number in p->nLine.
15115**   +  Store the character that terminates the field in p->cTerm.  Store
15116**      EOF on end-of-file.
15117**   +  Report syntax errors on stderr
15118*/
15119static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
15120  int c;
15121  int cSep = p->cColSep;
15122  int rSep = p->cRowSep;
15123  p->n = 0;
15124  c = fgetc(p->in);
15125  if( c==EOF || seenInterrupt ){
15126    p->cTerm = EOF;
15127    return 0;
15128  }
15129  while( c!=EOF && c!=cSep && c!=rSep ){
15130    import_append_char(p, c);
15131    c = fgetc(p->in);
15132  }
15133  if( c==rSep ){
15134    p->nLine++;
15135  }
15136  p->cTerm = c;
15137  if( p->z ) p->z[p->n] = 0;
15138  return p->z;
15139}
15140
15141/*
15142** Try to transfer data for table zTable.  If an error is seen while
15143** moving forward, try to go backwards.  The backwards movement won't
15144** work for WITHOUT ROWID tables.
15145*/
15146static void tryToCloneData(
15147  ShellState *p,
15148  sqlite3 *newDb,
15149  const char *zTable
15150){
15151  sqlite3_stmt *pQuery = 0;
15152  sqlite3_stmt *pInsert = 0;
15153  char *zQuery = 0;
15154  char *zInsert = 0;
15155  int rc;
15156  int i, j, n;
15157  int nTable = strlen30(zTable);
15158  int k = 0;
15159  int cnt = 0;
15160  const int spinRate = 10000;
15161
15162  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
15163  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15164  if( rc ){
15165    utf8_printf(stderr, "Error %d: %s on [%s]\n",
15166            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15167            zQuery);
15168    goto end_data_xfer;
15169  }
15170  n = sqlite3_column_count(pQuery);
15171  zInsert = sqlite3_malloc64(200 + nTable + n*3);
15172  if( zInsert==0 ) shell_out_of_memory();
15173  sqlite3_snprintf(200+nTable,zInsert,
15174                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
15175  i = strlen30(zInsert);
15176  for(j=1; j<n; j++){
15177    memcpy(zInsert+i, ",?", 2);
15178    i += 2;
15179  }
15180  memcpy(zInsert+i, ");", 3);
15181  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
15182  if( rc ){
15183    utf8_printf(stderr, "Error %d: %s on [%s]\n",
15184            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
15185            zQuery);
15186    goto end_data_xfer;
15187  }
15188  for(k=0; k<2; k++){
15189    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15190      for(i=0; i<n; i++){
15191        switch( sqlite3_column_type(pQuery, i) ){
15192          case SQLITE_NULL: {
15193            sqlite3_bind_null(pInsert, i+1);
15194            break;
15195          }
15196          case SQLITE_INTEGER: {
15197            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
15198            break;
15199          }
15200          case SQLITE_FLOAT: {
15201            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
15202            break;
15203          }
15204          case SQLITE_TEXT: {
15205            sqlite3_bind_text(pInsert, i+1,
15206                             (const char*)sqlite3_column_text(pQuery,i),
15207                             -1, SQLITE_STATIC);
15208            break;
15209          }
15210          case SQLITE_BLOB: {
15211            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
15212                                            sqlite3_column_bytes(pQuery,i),
15213                                            SQLITE_STATIC);
15214            break;
15215          }
15216        }
15217      } /* End for */
15218      rc = sqlite3_step(pInsert);
15219      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
15220        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
15221                        sqlite3_errmsg(newDb));
15222      }
15223      sqlite3_reset(pInsert);
15224      cnt++;
15225      if( (cnt%spinRate)==0 ){
15226        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
15227        fflush(stdout);
15228      }
15229    } /* End while */
15230    if( rc==SQLITE_DONE ) break;
15231    sqlite3_finalize(pQuery);
15232    sqlite3_free(zQuery);
15233    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
15234                             zTable);
15235    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15236    if( rc ){
15237      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
15238      break;
15239    }
15240  } /* End for(k=0...) */
15241
15242end_data_xfer:
15243  sqlite3_finalize(pQuery);
15244  sqlite3_finalize(pInsert);
15245  sqlite3_free(zQuery);
15246  sqlite3_free(zInsert);
15247}
15248
15249
15250/*
15251** Try to transfer all rows of the schema that match zWhere.  For
15252** each row, invoke xForEach() on the object defined by that row.
15253** If an error is encountered while moving forward through the
15254** sqlite_schema table, try again moving backwards.
15255*/
15256static void tryToCloneSchema(
15257  ShellState *p,
15258  sqlite3 *newDb,
15259  const char *zWhere,
15260  void (*xForEach)(ShellState*,sqlite3*,const char*)
15261){
15262  sqlite3_stmt *pQuery = 0;
15263  char *zQuery = 0;
15264  int rc;
15265  const unsigned char *zName;
15266  const unsigned char *zSql;
15267  char *zErrMsg = 0;
15268
15269  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15270                           " WHERE %s", zWhere);
15271  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15272  if( rc ){
15273    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15274                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15275                    zQuery);
15276    goto end_schema_xfer;
15277  }
15278  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15279    zName = sqlite3_column_text(pQuery, 0);
15280    zSql = sqlite3_column_text(pQuery, 1);
15281    printf("%s... ", zName); fflush(stdout);
15282    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15283    if( zErrMsg ){
15284      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15285      sqlite3_free(zErrMsg);
15286      zErrMsg = 0;
15287    }
15288    if( xForEach ){
15289      xForEach(p, newDb, (const char*)zName);
15290    }
15291    printf("done\n");
15292  }
15293  if( rc!=SQLITE_DONE ){
15294    sqlite3_finalize(pQuery);
15295    sqlite3_free(zQuery);
15296    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15297                             " WHERE %s ORDER BY rowid DESC", zWhere);
15298    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15299    if( rc ){
15300      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15301                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15302                      zQuery);
15303      goto end_schema_xfer;
15304    }
15305    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15306      zName = sqlite3_column_text(pQuery, 0);
15307      zSql = sqlite3_column_text(pQuery, 1);
15308      printf("%s... ", zName); fflush(stdout);
15309      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15310      if( zErrMsg ){
15311        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15312        sqlite3_free(zErrMsg);
15313        zErrMsg = 0;
15314      }
15315      if( xForEach ){
15316        xForEach(p, newDb, (const char*)zName);
15317      }
15318      printf("done\n");
15319    }
15320  }
15321end_schema_xfer:
15322  sqlite3_finalize(pQuery);
15323  sqlite3_free(zQuery);
15324}
15325
15326/*
15327** Open a new database file named "zNewDb".  Try to recover as much information
15328** as possible out of the main database (which might be corrupt) and write it
15329** into zNewDb.
15330*/
15331static void tryToClone(ShellState *p, const char *zNewDb){
15332  int rc;
15333  sqlite3 *newDb = 0;
15334  if( access(zNewDb,0)==0 ){
15335    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
15336    return;
15337  }
15338  rc = sqlite3_open(zNewDb, &newDb);
15339  if( rc ){
15340    utf8_printf(stderr, "Cannot create output database: %s\n",
15341            sqlite3_errmsg(newDb));
15342  }else{
15343    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
15344    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
15345    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
15346    tryToCloneSchema(p, newDb, "type!='table'", 0);
15347    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
15348    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15349  }
15350  close_db(newDb);
15351}
15352
15353/*
15354** Change the output file back to stdout.
15355**
15356** If the p->doXdgOpen flag is set, that means the output was being
15357** redirected to a temporary file named by p->zTempFile.  In that case,
15358** launch start/open/xdg-open on that temporary file.
15359*/
15360static void output_reset(ShellState *p){
15361  if( p->outfile[0]=='|' ){
15362#ifndef SQLITE_OMIT_POPEN
15363    pclose(p->out);
15364#endif
15365  }else{
15366    output_file_close(p->out);
15367#ifndef SQLITE_NOHAVE_SYSTEM
15368    if( p->doXdgOpen ){
15369      const char *zXdgOpenCmd =
15370#if defined(_WIN32)
15371      "start";
15372#elif defined(__APPLE__)
15373      "open";
15374#else
15375      "xdg-open";
15376#endif
15377      char *zCmd;
15378      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
15379      if( system(zCmd) ){
15380        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
15381      }else{
15382        /* Give the start/open/xdg-open command some time to get
15383        ** going before we continue, and potential delete the
15384        ** p->zTempFile data file out from under it */
15385        sqlite3_sleep(2000);
15386      }
15387      sqlite3_free(zCmd);
15388      outputModePop(p);
15389      p->doXdgOpen = 0;
15390    }
15391#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15392  }
15393  p->outfile[0] = 0;
15394  p->out = stdout;
15395}
15396
15397/*
15398** Run an SQL command and return the single integer result.
15399*/
15400static int db_int(ShellState *p, const char *zSql){
15401  sqlite3_stmt *pStmt;
15402  int res = 0;
15403  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15404  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
15405    res = sqlite3_column_int(pStmt,0);
15406  }
15407  sqlite3_finalize(pStmt);
15408  return res;
15409}
15410
15411/*
15412** Convert a 2-byte or 4-byte big-endian integer into a native integer
15413*/
15414static unsigned int get2byteInt(unsigned char *a){
15415  return (a[0]<<8) + a[1];
15416}
15417static unsigned int get4byteInt(unsigned char *a){
15418  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
15419}
15420
15421/*
15422** Implementation of the ".dbinfo" command.
15423**
15424** Return 1 on error, 2 to exit, and 0 otherwise.
15425*/
15426static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
15427  static const struct { const char *zName; int ofst; } aField[] = {
15428     { "file change counter:",  24  },
15429     { "database page count:",  28  },
15430     { "freelist page count:",  36  },
15431     { "schema cookie:",        40  },
15432     { "schema format:",        44  },
15433     { "default cache size:",   48  },
15434     { "autovacuum top root:",  52  },
15435     { "incremental vacuum:",   64  },
15436     { "text encoding:",        56  },
15437     { "user version:",         60  },
15438     { "application id:",       68  },
15439     { "software version:",     96  },
15440  };
15441  static const struct { const char *zName; const char *zSql; } aQuery[] = {
15442     { "number of tables:",
15443       "SELECT count(*) FROM %s WHERE type='table'" },
15444     { "number of indexes:",
15445       "SELECT count(*) FROM %s WHERE type='index'" },
15446     { "number of triggers:",
15447       "SELECT count(*) FROM %s WHERE type='trigger'" },
15448     { "number of views:",
15449       "SELECT count(*) FROM %s WHERE type='view'" },
15450     { "schema size:",
15451       "SELECT total(length(sql)) FROM %s" },
15452  };
15453  int i, rc;
15454  unsigned iDataVersion;
15455  char *zSchemaTab;
15456  char *zDb = nArg>=2 ? azArg[1] : "main";
15457  sqlite3_stmt *pStmt = 0;
15458  unsigned char aHdr[100];
15459  open_db(p, 0);
15460  if( p->db==0 ) return 1;
15461  rc = sqlite3_prepare_v2(p->db,
15462             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
15463             -1, &pStmt, 0);
15464  if( rc ){
15465    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
15466    sqlite3_finalize(pStmt);
15467    return 1;
15468  }
15469  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
15470  if( sqlite3_step(pStmt)==SQLITE_ROW
15471   && sqlite3_column_bytes(pStmt,0)>100
15472  ){
15473    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
15474    sqlite3_finalize(pStmt);
15475  }else{
15476    raw_printf(stderr, "unable to read database header\n");
15477    sqlite3_finalize(pStmt);
15478    return 1;
15479  }
15480  i = get2byteInt(aHdr+16);
15481  if( i==1 ) i = 65536;
15482  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
15483  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
15484  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
15485  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
15486  for(i=0; i<ArraySize(aField); i++){
15487    int ofst = aField[i].ofst;
15488    unsigned int val = get4byteInt(aHdr + ofst);
15489    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
15490    switch( ofst ){
15491      case 56: {
15492        if( val==1 ) raw_printf(p->out, " (utf8)");
15493        if( val==2 ) raw_printf(p->out, " (utf16le)");
15494        if( val==3 ) raw_printf(p->out, " (utf16be)");
15495      }
15496    }
15497    raw_printf(p->out, "\n");
15498  }
15499  if( zDb==0 ){
15500    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
15501  }else if( strcmp(zDb,"temp")==0 ){
15502    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
15503  }else{
15504    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
15505  }
15506  for(i=0; i<ArraySize(aQuery); i++){
15507    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
15508    int val = db_int(p, zSql);
15509    sqlite3_free(zSql);
15510    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
15511  }
15512  sqlite3_free(zSchemaTab);
15513  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
15514  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
15515  return 0;
15516}
15517
15518/*
15519** Print the current sqlite3_errmsg() value to stderr and return 1.
15520*/
15521static int shellDatabaseError(sqlite3 *db){
15522  const char *zErr = sqlite3_errmsg(db);
15523  utf8_printf(stderr, "Error: %s\n", zErr);
15524  return 1;
15525}
15526
15527/*
15528** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
15529** if they match and FALSE (0) if they do not match.
15530**
15531** Globbing rules:
15532**
15533**      '*'       Matches any sequence of zero or more characters.
15534**
15535**      '?'       Matches exactly one character.
15536**
15537**     [...]      Matches one character from the enclosed list of
15538**                characters.
15539**
15540**     [^...]     Matches one character not in the enclosed list.
15541**
15542**      '#'       Matches any sequence of one or more digits with an
15543**                optional + or - sign in front
15544**
15545**      ' '       Any span of whitespace matches any other span of
15546**                whitespace.
15547**
15548** Extra whitespace at the end of z[] is ignored.
15549*/
15550static int testcase_glob(const char *zGlob, const char *z){
15551  int c, c2;
15552  int invert;
15553  int seen;
15554
15555  while( (c = (*(zGlob++)))!=0 ){
15556    if( IsSpace(c) ){
15557      if( !IsSpace(*z) ) return 0;
15558      while( IsSpace(*zGlob) ) zGlob++;
15559      while( IsSpace(*z) ) z++;
15560    }else if( c=='*' ){
15561      while( (c=(*(zGlob++))) == '*' || c=='?' ){
15562        if( c=='?' && (*(z++))==0 ) return 0;
15563      }
15564      if( c==0 ){
15565        return 1;
15566      }else if( c=='[' ){
15567        while( *z && testcase_glob(zGlob-1,z)==0 ){
15568          z++;
15569        }
15570        return (*z)!=0;
15571      }
15572      while( (c2 = (*(z++)))!=0 ){
15573        while( c2!=c ){
15574          c2 = *(z++);
15575          if( c2==0 ) return 0;
15576        }
15577        if( testcase_glob(zGlob,z) ) return 1;
15578      }
15579      return 0;
15580    }else if( c=='?' ){
15581      if( (*(z++))==0 ) return 0;
15582    }else if( c=='[' ){
15583      int prior_c = 0;
15584      seen = 0;
15585      invert = 0;
15586      c = *(z++);
15587      if( c==0 ) return 0;
15588      c2 = *(zGlob++);
15589      if( c2=='^' ){
15590        invert = 1;
15591        c2 = *(zGlob++);
15592      }
15593      if( c2==']' ){
15594        if( c==']' ) seen = 1;
15595        c2 = *(zGlob++);
15596      }
15597      while( c2 && c2!=']' ){
15598        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
15599          c2 = *(zGlob++);
15600          if( c>=prior_c && c<=c2 ) seen = 1;
15601          prior_c = 0;
15602        }else{
15603          if( c==c2 ){
15604            seen = 1;
15605          }
15606          prior_c = c2;
15607        }
15608        c2 = *(zGlob++);
15609      }
15610      if( c2==0 || (seen ^ invert)==0 ) return 0;
15611    }else if( c=='#' ){
15612      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
15613      if( !IsDigit(z[0]) ) return 0;
15614      z++;
15615      while( IsDigit(z[0]) ){ z++; }
15616    }else{
15617      if( c!=(*(z++)) ) return 0;
15618    }
15619  }
15620  while( IsSpace(*z) ){ z++; }
15621  return *z==0;
15622}
15623
15624
15625/*
15626** Compare the string as a command-line option with either one or two
15627** initial "-" characters.
15628*/
15629static int optionMatch(const char *zStr, const char *zOpt){
15630  if( zStr[0]!='-' ) return 0;
15631  zStr++;
15632  if( zStr[0]=='-' ) zStr++;
15633  return strcmp(zStr, zOpt)==0;
15634}
15635
15636/*
15637** Delete a file.
15638*/
15639int shellDeleteFile(const char *zFilename){
15640  int rc;
15641#ifdef _WIN32
15642  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
15643  rc = _wunlink(z);
15644  sqlite3_free(z);
15645#else
15646  rc = unlink(zFilename);
15647#endif
15648  return rc;
15649}
15650
15651/*
15652** Try to delete the temporary file (if there is one) and free the
15653** memory used to hold the name of the temp file.
15654*/
15655static void clearTempFile(ShellState *p){
15656  if( p->zTempFile==0 ) return;
15657  if( p->doXdgOpen ) return;
15658  if( shellDeleteFile(p->zTempFile) ) return;
15659  sqlite3_free(p->zTempFile);
15660  p->zTempFile = 0;
15661}
15662
15663/*
15664** Create a new temp file name with the given suffix.
15665*/
15666static void newTempFile(ShellState *p, const char *zSuffix){
15667  clearTempFile(p);
15668  sqlite3_free(p->zTempFile);
15669  p->zTempFile = 0;
15670  if( p->db ){
15671    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
15672  }
15673  if( p->zTempFile==0 ){
15674    /* If p->db is an in-memory database then the TEMPFILENAME file-control
15675    ** will not work and we will need to fallback to guessing */
15676    char *zTemp;
15677    sqlite3_uint64 r;
15678    sqlite3_randomness(sizeof(r), &r);
15679    zTemp = getenv("TEMP");
15680    if( zTemp==0 ) zTemp = getenv("TMP");
15681    if( zTemp==0 ){
15682#ifdef _WIN32
15683      zTemp = "\\tmp";
15684#else
15685      zTemp = "/tmp";
15686#endif
15687    }
15688    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
15689  }else{
15690    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
15691  }
15692  if( p->zTempFile==0 ){
15693    raw_printf(stderr, "out of memory\n");
15694    exit(1);
15695  }
15696}
15697
15698
15699/*
15700** The implementation of SQL scalar function fkey_collate_clause(), used
15701** by the ".lint fkey-indexes" command. This scalar function is always
15702** called with four arguments - the parent table name, the parent column name,
15703** the child table name and the child column name.
15704**
15705**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
15706**
15707** If either of the named tables or columns do not exist, this function
15708** returns an empty string. An empty string is also returned if both tables
15709** and columns exist but have the same default collation sequence. Or,
15710** if both exist but the default collation sequences are different, this
15711** function returns the string " COLLATE <parent-collation>", where
15712** <parent-collation> is the default collation sequence of the parent column.
15713*/
15714static void shellFkeyCollateClause(
15715  sqlite3_context *pCtx,
15716  int nVal,
15717  sqlite3_value **apVal
15718){
15719  sqlite3 *db = sqlite3_context_db_handle(pCtx);
15720  const char *zParent;
15721  const char *zParentCol;
15722  const char *zParentSeq;
15723  const char *zChild;
15724  const char *zChildCol;
15725  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
15726  int rc;
15727
15728  assert( nVal==4 );
15729  zParent = (const char*)sqlite3_value_text(apVal[0]);
15730  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
15731  zChild = (const char*)sqlite3_value_text(apVal[2]);
15732  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
15733
15734  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
15735  rc = sqlite3_table_column_metadata(
15736      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
15737  );
15738  if( rc==SQLITE_OK ){
15739    rc = sqlite3_table_column_metadata(
15740        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
15741    );
15742  }
15743
15744  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
15745    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
15746    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
15747    sqlite3_free(z);
15748  }
15749}
15750
15751
15752/*
15753** The implementation of dot-command ".lint fkey-indexes".
15754*/
15755static int lintFkeyIndexes(
15756  ShellState *pState,             /* Current shell tool state */
15757  char **azArg,                   /* Array of arguments passed to dot command */
15758  int nArg                        /* Number of entries in azArg[] */
15759){
15760  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
15761  FILE *out = pState->out;        /* Stream to write non-error output to */
15762  int bVerbose = 0;               /* If -verbose is present */
15763  int bGroupByParent = 0;         /* If -groupbyparent is present */
15764  int i;                          /* To iterate through azArg[] */
15765  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
15766  int rc;                         /* Return code */
15767  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
15768
15769  /*
15770  ** This SELECT statement returns one row for each foreign key constraint
15771  ** in the schema of the main database. The column values are:
15772  **
15773  ** 0. The text of an SQL statement similar to:
15774  **
15775  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
15776  **
15777  **    This SELECT is similar to the one that the foreign keys implementation
15778  **    needs to run internally on child tables. If there is an index that can
15779  **    be used to optimize this query, then it can also be used by the FK
15780  **    implementation to optimize DELETE or UPDATE statements on the parent
15781  **    table.
15782  **
15783  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
15784  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
15785  **    contains an index that can be used to optimize the query.
15786  **
15787  ** 2. Human readable text that describes the child table and columns. e.g.
15788  **
15789  **       "child_table(child_key1, child_key2)"
15790  **
15791  ** 3. Human readable text that describes the parent table and columns. e.g.
15792  **
15793  **       "parent_table(parent_key1, parent_key2)"
15794  **
15795  ** 4. A full CREATE INDEX statement for an index that could be used to
15796  **    optimize DELETE or UPDATE statements on the parent table. e.g.
15797  **
15798  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
15799  **
15800  ** 5. The name of the parent table.
15801  **
15802  ** These six values are used by the C logic below to generate the report.
15803  */
15804  const char *zSql =
15805  "SELECT "
15806    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
15807    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
15808    "  || fkey_collate_clause("
15809    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
15810    ", "
15811    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
15812    "  || group_concat('*=?', ' AND ') || ')'"
15813    ", "
15814    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
15815    ", "
15816    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
15817    ", "
15818    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
15819    "  || ' ON ' || quote(s.name) || '('"
15820    "  || group_concat(quote(f.[from]) ||"
15821    "        fkey_collate_clause("
15822    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
15823    "  || ');'"
15824    ", "
15825    "     f.[table] "
15826    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
15827    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
15828    "GROUP BY s.name, f.id "
15829    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
15830  ;
15831  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
15832
15833  for(i=2; i<nArg; i++){
15834    int n = strlen30(azArg[i]);
15835    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
15836      bVerbose = 1;
15837    }
15838    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
15839      bGroupByParent = 1;
15840      zIndent = "    ";
15841    }
15842    else{
15843      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
15844          azArg[0], azArg[1]
15845      );
15846      return SQLITE_ERROR;
15847    }
15848  }
15849
15850  /* Register the fkey_collate_clause() SQL function */
15851  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
15852      0, shellFkeyCollateClause, 0, 0
15853  );
15854
15855
15856  if( rc==SQLITE_OK ){
15857    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
15858  }
15859  if( rc==SQLITE_OK ){
15860    sqlite3_bind_int(pSql, 1, bGroupByParent);
15861  }
15862
15863  if( rc==SQLITE_OK ){
15864    int rc2;
15865    char *zPrev = 0;
15866    while( SQLITE_ROW==sqlite3_step(pSql) ){
15867      int res = -1;
15868      sqlite3_stmt *pExplain = 0;
15869      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
15870      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
15871      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
15872      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
15873      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
15874      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
15875
15876      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
15877      if( rc!=SQLITE_OK ) break;
15878      if( SQLITE_ROW==sqlite3_step(pExplain) ){
15879        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
15880        res = (
15881              0==sqlite3_strglob(zGlob, zPlan)
15882           || 0==sqlite3_strglob(zGlobIPK, zPlan)
15883        );
15884      }
15885      rc = sqlite3_finalize(pExplain);
15886      if( rc!=SQLITE_OK ) break;
15887
15888      if( res<0 ){
15889        raw_printf(stderr, "Error: internal error");
15890        break;
15891      }else{
15892        if( bGroupByParent
15893        && (bVerbose || res==0)
15894        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
15895        ){
15896          raw_printf(out, "-- Parent table %s\n", zParent);
15897          sqlite3_free(zPrev);
15898          zPrev = sqlite3_mprintf("%s", zParent);
15899        }
15900
15901        if( res==0 ){
15902          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
15903        }else if( bVerbose ){
15904          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
15905              zIndent, zFrom, zTarget
15906          );
15907        }
15908      }
15909    }
15910    sqlite3_free(zPrev);
15911
15912    if( rc!=SQLITE_OK ){
15913      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15914    }
15915
15916    rc2 = sqlite3_finalize(pSql);
15917    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
15918      rc = rc2;
15919      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15920    }
15921  }else{
15922    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15923  }
15924
15925  return rc;
15926}
15927
15928/*
15929** Implementation of ".lint" dot command.
15930*/
15931static int lintDotCommand(
15932  ShellState *pState,             /* Current shell tool state */
15933  char **azArg,                   /* Array of arguments passed to dot command */
15934  int nArg                        /* Number of entries in azArg[] */
15935){
15936  int n;
15937  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
15938  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
15939  return lintFkeyIndexes(pState, azArg, nArg);
15940
15941 usage:
15942  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
15943  raw_printf(stderr, "Where sub-commands are:\n");
15944  raw_printf(stderr, "    fkey-indexes\n");
15945  return SQLITE_ERROR;
15946}
15947
15948#if !defined SQLITE_OMIT_VIRTUALTABLE
15949static void shellPrepare(
15950  sqlite3 *db,
15951  int *pRc,
15952  const char *zSql,
15953  sqlite3_stmt **ppStmt
15954){
15955  *ppStmt = 0;
15956  if( *pRc==SQLITE_OK ){
15957    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
15958    if( rc!=SQLITE_OK ){
15959      raw_printf(stderr, "sql error: %s (%d)\n",
15960          sqlite3_errmsg(db), sqlite3_errcode(db)
15961      );
15962      *pRc = rc;
15963    }
15964  }
15965}
15966
15967/*
15968** Create a prepared statement using printf-style arguments for the SQL.
15969**
15970** This routine is could be marked "static".  But it is not always used,
15971** depending on compile-time options.  By omitting the "static", we avoid
15972** nuisance compiler warnings about "defined but not used".
15973*/
15974void shellPreparePrintf(
15975  sqlite3 *db,
15976  int *pRc,
15977  sqlite3_stmt **ppStmt,
15978  const char *zFmt,
15979  ...
15980){
15981  *ppStmt = 0;
15982  if( *pRc==SQLITE_OK ){
15983    va_list ap;
15984    char *z;
15985    va_start(ap, zFmt);
15986    z = sqlite3_vmprintf(zFmt, ap);
15987    va_end(ap);
15988    if( z==0 ){
15989      *pRc = SQLITE_NOMEM;
15990    }else{
15991      shellPrepare(db, pRc, z, ppStmt);
15992      sqlite3_free(z);
15993    }
15994  }
15995}
15996
15997/* Finalize the prepared statement created using shellPreparePrintf().
15998**
15999** This routine is could be marked "static".  But it is not always used,
16000** depending on compile-time options.  By omitting the "static", we avoid
16001** nuisance compiler warnings about "defined but not used".
16002*/
16003void shellFinalize(
16004  int *pRc,
16005  sqlite3_stmt *pStmt
16006){
16007  if( pStmt ){
16008    sqlite3 *db = sqlite3_db_handle(pStmt);
16009    int rc = sqlite3_finalize(pStmt);
16010    if( *pRc==SQLITE_OK ){
16011      if( rc!=SQLITE_OK ){
16012        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16013      }
16014      *pRc = rc;
16015    }
16016  }
16017}
16018
16019/* Reset the prepared statement created using shellPreparePrintf().
16020**
16021** This routine is could be marked "static".  But it is not always used,
16022** depending on compile-time options.  By omitting the "static", we avoid
16023** nuisance compiler warnings about "defined but not used".
16024*/
16025void shellReset(
16026  int *pRc,
16027  sqlite3_stmt *pStmt
16028){
16029  int rc = sqlite3_reset(pStmt);
16030  if( *pRc==SQLITE_OK ){
16031    if( rc!=SQLITE_OK ){
16032      sqlite3 *db = sqlite3_db_handle(pStmt);
16033      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16034    }
16035    *pRc = rc;
16036  }
16037}
16038#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16039
16040#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16041/******************************************************************************
16042** The ".archive" or ".ar" command.
16043*/
16044/*
16045** Structure representing a single ".ar" command.
16046*/
16047typedef struct ArCommand ArCommand;
16048struct ArCommand {
16049  u8 eCmd;                        /* An AR_CMD_* value */
16050  u8 bVerbose;                    /* True if --verbose */
16051  u8 bZip;                        /* True if the archive is a ZIP */
16052  u8 bDryRun;                     /* True if --dry-run */
16053  u8 bAppend;                     /* True if --append */
16054  u8 fromCmdLine;                 /* Run from -A instead of .archive */
16055  int nArg;                       /* Number of command arguments */
16056  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
16057  const char *zFile;              /* --file argument, or NULL */
16058  const char *zDir;               /* --directory argument, or NULL */
16059  char **azArg;                   /* Array of command arguments */
16060  ShellState *p;                  /* Shell state */
16061  sqlite3 *db;                    /* Database containing the archive */
16062};
16063
16064/*
16065** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16066*/
16067static int arUsage(FILE *f){
16068  showHelp(f,"archive");
16069  return SQLITE_ERROR;
16070}
16071
16072/*
16073** Print an error message for the .ar command to stderr and return
16074** SQLITE_ERROR.
16075*/
16076static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16077  va_list ap;
16078  char *z;
16079  va_start(ap, zFmt);
16080  z = sqlite3_vmprintf(zFmt, ap);
16081  va_end(ap);
16082  utf8_printf(stderr, "Error: %s\n", z);
16083  if( pAr->fromCmdLine ){
16084    utf8_printf(stderr, "Use \"-A\" for more help\n");
16085  }else{
16086    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
16087  }
16088  sqlite3_free(z);
16089  return SQLITE_ERROR;
16090}
16091
16092/*
16093** Values for ArCommand.eCmd.
16094*/
16095#define AR_CMD_CREATE       1
16096#define AR_CMD_UPDATE       2
16097#define AR_CMD_INSERT       3
16098#define AR_CMD_EXTRACT      4
16099#define AR_CMD_LIST         5
16100#define AR_CMD_HELP         6
16101
16102/*
16103** Other (non-command) switches.
16104*/
16105#define AR_SWITCH_VERBOSE     7
16106#define AR_SWITCH_FILE        8
16107#define AR_SWITCH_DIRECTORY   9
16108#define AR_SWITCH_APPEND     10
16109#define AR_SWITCH_DRYRUN     11
16110
16111static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
16112  switch( eSwitch ){
16113    case AR_CMD_CREATE:
16114    case AR_CMD_EXTRACT:
16115    case AR_CMD_LIST:
16116    case AR_CMD_UPDATE:
16117    case AR_CMD_INSERT:
16118    case AR_CMD_HELP:
16119      if( pAr->eCmd ){
16120        return arErrorMsg(pAr, "multiple command options");
16121      }
16122      pAr->eCmd = eSwitch;
16123      break;
16124
16125    case AR_SWITCH_DRYRUN:
16126      pAr->bDryRun = 1;
16127      break;
16128    case AR_SWITCH_VERBOSE:
16129      pAr->bVerbose = 1;
16130      break;
16131    case AR_SWITCH_APPEND:
16132      pAr->bAppend = 1;
16133      /* Fall thru into --file */
16134    case AR_SWITCH_FILE:
16135      pAr->zFile = zArg;
16136      break;
16137    case AR_SWITCH_DIRECTORY:
16138      pAr->zDir = zArg;
16139      break;
16140  }
16141
16142  return SQLITE_OK;
16143}
16144
16145/*
16146** Parse the command line for an ".ar" command. The results are written into
16147** structure (*pAr). SQLITE_OK is returned if the command line is parsed
16148** successfully, otherwise an error message is written to stderr and
16149** SQLITE_ERROR returned.
16150*/
16151static int arParseCommand(
16152  char **azArg,                   /* Array of arguments passed to dot command */
16153  int nArg,                       /* Number of entries in azArg[] */
16154  ArCommand *pAr                  /* Populate this object */
16155){
16156  struct ArSwitch {
16157    const char *zLong;
16158    char cShort;
16159    u8 eSwitch;
16160    u8 bArg;
16161  } aSwitch[] = {
16162    { "create",    'c', AR_CMD_CREATE,       0 },
16163    { "extract",   'x', AR_CMD_EXTRACT,      0 },
16164    { "insert",    'i', AR_CMD_INSERT,       0 },
16165    { "list",      't', AR_CMD_LIST,         0 },
16166    { "update",    'u', AR_CMD_UPDATE,       0 },
16167    { "help",      'h', AR_CMD_HELP,         0 },
16168    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
16169    { "file",      'f', AR_SWITCH_FILE,      1 },
16170    { "append",    'a', AR_SWITCH_APPEND,    1 },
16171    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
16172    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
16173  };
16174  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
16175  struct ArSwitch *pEnd = &aSwitch[nSwitch];
16176
16177  if( nArg<=1 ){
16178    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
16179    return arUsage(stderr);
16180  }else{
16181    char *z = azArg[1];
16182    if( z[0]!='-' ){
16183      /* Traditional style [tar] invocation */
16184      int i;
16185      int iArg = 2;
16186      for(i=0; z[i]; i++){
16187        const char *zArg = 0;
16188        struct ArSwitch *pOpt;
16189        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16190          if( z[i]==pOpt->cShort ) break;
16191        }
16192        if( pOpt==pEnd ){
16193          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16194        }
16195        if( pOpt->bArg ){
16196          if( iArg>=nArg ){
16197            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
16198          }
16199          zArg = azArg[iArg++];
16200        }
16201        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16202      }
16203      pAr->nArg = nArg-iArg;
16204      if( pAr->nArg>0 ){
16205        pAr->azArg = &azArg[iArg];
16206      }
16207    }else{
16208      /* Non-traditional invocation */
16209      int iArg;
16210      for(iArg=1; iArg<nArg; iArg++){
16211        int n;
16212        z = azArg[iArg];
16213        if( z[0]!='-' ){
16214          /* All remaining command line words are command arguments. */
16215          pAr->azArg = &azArg[iArg];
16216          pAr->nArg = nArg-iArg;
16217          break;
16218        }
16219        n = strlen30(z);
16220
16221        if( z[1]!='-' ){
16222          int i;
16223          /* One or more short options */
16224          for(i=1; i<n; i++){
16225            const char *zArg = 0;
16226            struct ArSwitch *pOpt;
16227            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16228              if( z[i]==pOpt->cShort ) break;
16229            }
16230            if( pOpt==pEnd ){
16231              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16232            }
16233            if( pOpt->bArg ){
16234              if( i<(n-1) ){
16235                zArg = &z[i+1];
16236                i = n;
16237              }else{
16238                if( iArg>=(nArg-1) ){
16239                  return arErrorMsg(pAr, "option requires an argument: %c",
16240                                    z[i]);
16241                }
16242                zArg = azArg[++iArg];
16243              }
16244            }
16245            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16246          }
16247        }else if( z[2]=='\0' ){
16248          /* A -- option, indicating that all remaining command line words
16249          ** are command arguments.  */
16250          pAr->azArg = &azArg[iArg+1];
16251          pAr->nArg = nArg-iArg-1;
16252          break;
16253        }else{
16254          /* A long option */
16255          const char *zArg = 0;             /* Argument for option, if any */
16256          struct ArSwitch *pMatch = 0;      /* Matching option */
16257          struct ArSwitch *pOpt;            /* Iterator */
16258          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16259            const char *zLong = pOpt->zLong;
16260            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
16261              if( pMatch ){
16262                return arErrorMsg(pAr, "ambiguous option: %s",z);
16263              }else{
16264                pMatch = pOpt;
16265              }
16266            }
16267          }
16268
16269          if( pMatch==0 ){
16270            return arErrorMsg(pAr, "unrecognized option: %s", z);
16271          }
16272          if( pMatch->bArg ){
16273            if( iArg>=(nArg-1) ){
16274              return arErrorMsg(pAr, "option requires an argument: %s", z);
16275            }
16276            zArg = azArg[++iArg];
16277          }
16278          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
16279        }
16280      }
16281    }
16282  }
16283
16284  return SQLITE_OK;
16285}
16286
16287/*
16288** This function assumes that all arguments within the ArCommand.azArg[]
16289** array refer to archive members, as for the --extract or --list commands.
16290** It checks that each of them are present. If any specified file is not
16291** present in the archive, an error is printed to stderr and an error
16292** code returned. Otherwise, if all specified arguments are present in
16293** the archive, SQLITE_OK is returned.
16294**
16295** This function strips any trailing '/' characters from each argument.
16296** This is consistent with the way the [tar] command seems to work on
16297** Linux.
16298*/
16299static int arCheckEntries(ArCommand *pAr){
16300  int rc = SQLITE_OK;
16301  if( pAr->nArg ){
16302    int i, j;
16303    sqlite3_stmt *pTest = 0;
16304
16305    shellPreparePrintf(pAr->db, &rc, &pTest,
16306        "SELECT name FROM %s WHERE name=$name",
16307        pAr->zSrcTable
16308    );
16309    j = sqlite3_bind_parameter_index(pTest, "$name");
16310    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16311      char *z = pAr->azArg[i];
16312      int n = strlen30(z);
16313      int bOk = 0;
16314      while( n>0 && z[n-1]=='/' ) n--;
16315      z[n] = '\0';
16316      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
16317      if( SQLITE_ROW==sqlite3_step(pTest) ){
16318        bOk = 1;
16319      }
16320      shellReset(&rc, pTest);
16321      if( rc==SQLITE_OK && bOk==0 ){
16322        utf8_printf(stderr, "not found in archive: %s\n", z);
16323        rc = SQLITE_ERROR;
16324      }
16325    }
16326    shellFinalize(&rc, pTest);
16327  }
16328  return rc;
16329}
16330
16331/*
16332** Format a WHERE clause that can be used against the "sqlar" table to
16333** identify all archive members that match the command arguments held
16334** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
16335** The caller is responsible for eventually calling sqlite3_free() on
16336** any non-NULL (*pzWhere) value.
16337*/
16338static void arWhereClause(
16339  int *pRc,
16340  ArCommand *pAr,
16341  char **pzWhere                  /* OUT: New WHERE clause */
16342){
16343  char *zWhere = 0;
16344  if( *pRc==SQLITE_OK ){
16345    if( pAr->nArg==0 ){
16346      zWhere = sqlite3_mprintf("1");
16347    }else{
16348      int i;
16349      const char *zSep = "";
16350      for(i=0; i<pAr->nArg; i++){
16351        const char *z = pAr->azArg[i];
16352        zWhere = sqlite3_mprintf(
16353          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
16354          zWhere, zSep, z, strlen30(z)+1, z
16355        );
16356        if( zWhere==0 ){
16357          *pRc = SQLITE_NOMEM;
16358          break;
16359        }
16360        zSep = " OR ";
16361      }
16362    }
16363  }
16364  *pzWhere = zWhere;
16365}
16366
16367/*
16368** Implementation of .ar "lisT" command.
16369*/
16370static int arListCommand(ArCommand *pAr){
16371  const char *zSql = "SELECT %s FROM %s WHERE %s";
16372  const char *azCols[] = {
16373    "name",
16374    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
16375  };
16376
16377  char *zWhere = 0;
16378  sqlite3_stmt *pSql = 0;
16379  int rc;
16380
16381  rc = arCheckEntries(pAr);
16382  arWhereClause(&rc, pAr, &zWhere);
16383
16384  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
16385                     pAr->zSrcTable, zWhere);
16386  if( pAr->bDryRun ){
16387    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16388  }else{
16389    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16390      if( pAr->bVerbose ){
16391        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
16392            sqlite3_column_text(pSql, 0),
16393            sqlite3_column_int(pSql, 1),
16394            sqlite3_column_text(pSql, 2),
16395            sqlite3_column_text(pSql, 3)
16396        );
16397      }else{
16398        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16399      }
16400    }
16401  }
16402  shellFinalize(&rc, pSql);
16403  sqlite3_free(zWhere);
16404  return rc;
16405}
16406
16407
16408/*
16409** Implementation of .ar "eXtract" command.
16410*/
16411static int arExtractCommand(ArCommand *pAr){
16412  const char *zSql1 =
16413    "SELECT "
16414    " ($dir || name),"
16415    " writefile(($dir || name), %s, mode, mtime) "
16416    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
16417    " AND name NOT GLOB '*..[/\\]*'";
16418
16419  const char *azExtraArg[] = {
16420    "sqlar_uncompress(data, sz)",
16421    "data"
16422  };
16423
16424  sqlite3_stmt *pSql = 0;
16425  int rc = SQLITE_OK;
16426  char *zDir = 0;
16427  char *zWhere = 0;
16428  int i, j;
16429
16430  /* If arguments are specified, check that they actually exist within
16431  ** the archive before proceeding. And formulate a WHERE clause to
16432  ** match them.  */
16433  rc = arCheckEntries(pAr);
16434  arWhereClause(&rc, pAr, &zWhere);
16435
16436  if( rc==SQLITE_OK ){
16437    if( pAr->zDir ){
16438      zDir = sqlite3_mprintf("%s/", pAr->zDir);
16439    }else{
16440      zDir = sqlite3_mprintf("");
16441    }
16442    if( zDir==0 ) rc = SQLITE_NOMEM;
16443  }
16444
16445  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
16446      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
16447  );
16448
16449  if( rc==SQLITE_OK ){
16450    j = sqlite3_bind_parameter_index(pSql, "$dir");
16451    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
16452
16453    /* Run the SELECT statement twice. The first time, writefile() is called
16454    ** for all archive members that should be extracted. The second time,
16455    ** only for the directories. This is because the timestamps for
16456    ** extracted directories must be reset after they are populated (as
16457    ** populating them changes the timestamp).  */
16458    for(i=0; i<2; i++){
16459      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
16460      sqlite3_bind_int(pSql, j, i);
16461      if( pAr->bDryRun ){
16462        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16463      }else{
16464        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16465          if( i==0 && pAr->bVerbose ){
16466            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16467          }
16468        }
16469      }
16470      shellReset(&rc, pSql);
16471    }
16472    shellFinalize(&rc, pSql);
16473  }
16474
16475  sqlite3_free(zDir);
16476  sqlite3_free(zWhere);
16477  return rc;
16478}
16479
16480/*
16481** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
16482*/
16483static int arExecSql(ArCommand *pAr, const char *zSql){
16484  int rc;
16485  if( pAr->bDryRun ){
16486    utf8_printf(pAr->p->out, "%s\n", zSql);
16487    rc = SQLITE_OK;
16488  }else{
16489    char *zErr = 0;
16490    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
16491    if( zErr ){
16492      utf8_printf(stdout, "ERROR: %s\n", zErr);
16493      sqlite3_free(zErr);
16494    }
16495  }
16496  return rc;
16497}
16498
16499
16500/*
16501** Implementation of .ar "create", "insert", and "update" commands.
16502**
16503**     create    ->     Create a new SQL archive
16504**     insert    ->     Insert or reinsert all files listed
16505**     update    ->     Insert files that have changed or that were not
16506**                      previously in the archive
16507**
16508** Create the "sqlar" table in the database if it does not already exist.
16509** Then add each file in the azFile[] array to the archive. Directories
16510** are added recursively. If argument bVerbose is non-zero, a message is
16511** printed on stdout for each file archived.
16512**
16513** The create command is the same as update, except that it drops
16514** any existing "sqlar" table before beginning.  The "insert" command
16515** always overwrites every file named on the command-line, where as
16516** "update" only overwrites if the size or mtime or mode has changed.
16517*/
16518static int arCreateOrUpdateCommand(
16519  ArCommand *pAr,                 /* Command arguments and options */
16520  int bUpdate,                    /* true for a --create. */
16521  int bOnlyIfChanged              /* Only update if file has changed */
16522){
16523  const char *zCreate =
16524      "CREATE TABLE IF NOT EXISTS sqlar(\n"
16525      "  name TEXT PRIMARY KEY,  -- name of the file\n"
16526      "  mode INT,               -- access permissions\n"
16527      "  mtime INT,              -- last modification time\n"
16528      "  sz INT,                 -- original file size\n"
16529      "  data BLOB               -- compressed content\n"
16530      ")";
16531  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
16532  const char *zInsertFmt[2] = {
16533     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
16534     "  SELECT\n"
16535     "    %s,\n"
16536     "    mode,\n"
16537     "    mtime,\n"
16538     "    CASE substr(lsmode(mode),1,1)\n"
16539     "      WHEN '-' THEN length(data)\n"
16540     "      WHEN 'd' THEN 0\n"
16541     "      ELSE -1 END,\n"
16542     "    sqlar_compress(data)\n"
16543     "  FROM fsdir(%Q,%Q) AS disk\n"
16544     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16545     ,
16546     "REPLACE INTO %s(name,mode,mtime,data)\n"
16547     "  SELECT\n"
16548     "    %s,\n"
16549     "    mode,\n"
16550     "    mtime,\n"
16551     "    data\n"
16552     "  FROM fsdir(%Q,%Q) AS disk\n"
16553     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16554  };
16555  int i;                          /* For iterating through azFile[] */
16556  int rc;                         /* Return code */
16557  const char *zTab = 0;           /* SQL table into which to insert */
16558  char *zSql;
16559  char zTemp[50];
16560  char *zExists = 0;
16561
16562  arExecSql(pAr, "PRAGMA page_size=512");
16563  rc = arExecSql(pAr, "SAVEPOINT ar;");
16564  if( rc!=SQLITE_OK ) return rc;
16565  zTemp[0] = 0;
16566  if( pAr->bZip ){
16567    /* Initialize the zipfile virtual table, if necessary */
16568    if( pAr->zFile ){
16569      sqlite3_uint64 r;
16570      sqlite3_randomness(sizeof(r),&r);
16571      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
16572      zTab = zTemp;
16573      zSql = sqlite3_mprintf(
16574         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
16575         zTab, pAr->zFile
16576      );
16577      rc = arExecSql(pAr, zSql);
16578      sqlite3_free(zSql);
16579    }else{
16580      zTab = "zip";
16581    }
16582  }else{
16583    /* Initialize the table for an SQLAR */
16584    zTab = "sqlar";
16585    if( bUpdate==0 ){
16586      rc = arExecSql(pAr, zDrop);
16587      if( rc!=SQLITE_OK ) goto end_ar_transaction;
16588    }
16589    rc = arExecSql(pAr, zCreate);
16590  }
16591  if( bOnlyIfChanged ){
16592    zExists = sqlite3_mprintf(
16593      " AND NOT EXISTS("
16594          "SELECT 1 FROM %s AS mem"
16595          " WHERE mem.name=disk.name"
16596          " AND mem.mtime=disk.mtime"
16597          " AND mem.mode=disk.mode)", zTab);
16598  }else{
16599    zExists = sqlite3_mprintf("");
16600  }
16601  if( zExists==0 ) rc = SQLITE_NOMEM;
16602  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16603    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
16604        pAr->bVerbose ? "shell_putsnl(name)" : "name",
16605        pAr->azArg[i], pAr->zDir, zExists);
16606    rc = arExecSql(pAr, zSql2);
16607    sqlite3_free(zSql2);
16608  }
16609end_ar_transaction:
16610  if( rc!=SQLITE_OK ){
16611    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
16612  }else{
16613    rc = arExecSql(pAr, "RELEASE ar;");
16614    if( pAr->bZip && pAr->zFile ){
16615      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
16616      arExecSql(pAr, zSql);
16617      sqlite3_free(zSql);
16618    }
16619  }
16620  sqlite3_free(zExists);
16621  return rc;
16622}
16623
16624/*
16625** Implementation of ".ar" dot command.
16626*/
16627static int arDotCommand(
16628  ShellState *pState,          /* Current shell tool state */
16629  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
16630  char **azArg,                /* Array of arguments passed to dot command */
16631  int nArg                     /* Number of entries in azArg[] */
16632){
16633  ArCommand cmd;
16634  int rc;
16635  memset(&cmd, 0, sizeof(cmd));
16636  cmd.fromCmdLine = fromCmdLine;
16637  rc = arParseCommand(azArg, nArg, &cmd);
16638  if( rc==SQLITE_OK ){
16639    int eDbType = SHELL_OPEN_UNSPEC;
16640    cmd.p = pState;
16641    cmd.db = pState->db;
16642    if( cmd.zFile ){
16643      eDbType = deduceDatabaseType(cmd.zFile, 1);
16644    }else{
16645      eDbType = pState->openMode;
16646    }
16647    if( eDbType==SHELL_OPEN_ZIPFILE ){
16648      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
16649        if( cmd.zFile==0 ){
16650          cmd.zSrcTable = sqlite3_mprintf("zip");
16651        }else{
16652          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
16653        }
16654      }
16655      cmd.bZip = 1;
16656    }else if( cmd.zFile ){
16657      int flags;
16658      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
16659      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
16660           || cmd.eCmd==AR_CMD_UPDATE ){
16661        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
16662      }else{
16663        flags = SQLITE_OPEN_READONLY;
16664      }
16665      cmd.db = 0;
16666      if( cmd.bDryRun ){
16667        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
16668             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
16669      }
16670      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
16671             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
16672      if( rc!=SQLITE_OK ){
16673        utf8_printf(stderr, "cannot open file: %s (%s)\n",
16674            cmd.zFile, sqlite3_errmsg(cmd.db)
16675        );
16676        goto end_ar_command;
16677      }
16678      sqlite3_fileio_init(cmd.db, 0, 0);
16679      sqlite3_sqlar_init(cmd.db, 0, 0);
16680      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
16681                              shellPutsFunc, 0, 0);
16682
16683    }
16684    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
16685      if( cmd.eCmd!=AR_CMD_CREATE
16686       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
16687      ){
16688        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
16689        rc = SQLITE_ERROR;
16690        goto end_ar_command;
16691      }
16692      cmd.zSrcTable = sqlite3_mprintf("sqlar");
16693    }
16694
16695    switch( cmd.eCmd ){
16696      case AR_CMD_CREATE:
16697        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
16698        break;
16699
16700      case AR_CMD_EXTRACT:
16701        rc = arExtractCommand(&cmd);
16702        break;
16703
16704      case AR_CMD_LIST:
16705        rc = arListCommand(&cmd);
16706        break;
16707
16708      case AR_CMD_HELP:
16709        arUsage(pState->out);
16710        break;
16711
16712      case AR_CMD_INSERT:
16713        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
16714        break;
16715
16716      default:
16717        assert( cmd.eCmd==AR_CMD_UPDATE );
16718        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
16719        break;
16720    }
16721  }
16722end_ar_command:
16723  if( cmd.db!=pState->db ){
16724    close_db(cmd.db);
16725  }
16726  sqlite3_free(cmd.zSrcTable);
16727
16728  return rc;
16729}
16730/* End of the ".archive" or ".ar" command logic
16731*******************************************************************************/
16732#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
16733
16734#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16735/*
16736** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
16737** Otherwise, the SQL statement or statements in zSql are executed using
16738** database connection db and the error code written to *pRc before
16739** this function returns.
16740*/
16741static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
16742  int rc = *pRc;
16743  if( rc==SQLITE_OK ){
16744    char *zErr = 0;
16745    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16746    if( rc!=SQLITE_OK ){
16747      raw_printf(stderr, "SQL error: %s\n", zErr);
16748    }
16749    *pRc = rc;
16750  }
16751}
16752
16753/*
16754** Like shellExec(), except that zFmt is a printf() style format string.
16755*/
16756static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
16757  char *z = 0;
16758  if( *pRc==SQLITE_OK ){
16759    va_list ap;
16760    va_start(ap, zFmt);
16761    z = sqlite3_vmprintf(zFmt, ap);
16762    va_end(ap);
16763    if( z==0 ){
16764      *pRc = SQLITE_NOMEM;
16765    }else{
16766      shellExec(db, pRc, z);
16767    }
16768    sqlite3_free(z);
16769  }
16770}
16771
16772/*
16773** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16774** Otherwise, an attempt is made to allocate, zero and return a pointer
16775** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
16776** to SQLITE_NOMEM and NULL returned.
16777*/
16778static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
16779  void *pRet = 0;
16780  if( *pRc==SQLITE_OK ){
16781    pRet = sqlite3_malloc64(nByte);
16782    if( pRet==0 ){
16783      *pRc = SQLITE_NOMEM;
16784    }else{
16785      memset(pRet, 0, nByte);
16786    }
16787  }
16788  return pRet;
16789}
16790
16791/*
16792** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16793** Otherwise, zFmt is treated as a printf() style string. The result of
16794** formatting it along with any trailing arguments is written into a
16795** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
16796** It is the responsibility of the caller to eventually free this buffer
16797** using a call to sqlite3_free().
16798**
16799** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
16800** pointer returned.
16801*/
16802static char *shellMPrintf(int *pRc, const char *zFmt, ...){
16803  char *z = 0;
16804  if( *pRc==SQLITE_OK ){
16805    va_list ap;
16806    va_start(ap, zFmt);
16807    z = sqlite3_vmprintf(zFmt, ap);
16808    va_end(ap);
16809    if( z==0 ){
16810      *pRc = SQLITE_NOMEM;
16811    }
16812  }
16813  return z;
16814}
16815
16816/*
16817** When running the ".recover" command, each output table, and the special
16818** orphaned row table if it is required, is represented by an instance
16819** of the following struct.
16820*/
16821typedef struct RecoverTable RecoverTable;
16822struct RecoverTable {
16823  char *zQuoted;                  /* Quoted version of table name */
16824  int nCol;                       /* Number of columns in table */
16825  char **azlCol;                  /* Array of column lists */
16826  int iPk;                        /* Index of IPK column */
16827};
16828
16829/*
16830** Free a RecoverTable object allocated by recoverFindTable() or
16831** recoverOrphanTable().
16832*/
16833static void recoverFreeTable(RecoverTable *pTab){
16834  if( pTab ){
16835    sqlite3_free(pTab->zQuoted);
16836    if( pTab->azlCol ){
16837      int i;
16838      for(i=0; i<=pTab->nCol; i++){
16839        sqlite3_free(pTab->azlCol[i]);
16840      }
16841      sqlite3_free(pTab->azlCol);
16842    }
16843    sqlite3_free(pTab);
16844  }
16845}
16846
16847/*
16848** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
16849** Otherwise, it allocates and returns a RecoverTable object based on the
16850** final four arguments passed to this function. It is the responsibility
16851** of the caller to eventually free the returned object using
16852** recoverFreeTable().
16853*/
16854static RecoverTable *recoverNewTable(
16855  int *pRc,                       /* IN/OUT: Error code */
16856  const char *zName,              /* Name of table */
16857  const char *zSql,               /* CREATE TABLE statement */
16858  int bIntkey,
16859  int nCol
16860){
16861  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
16862  int rc = *pRc;
16863  RecoverTable *pTab = 0;
16864
16865  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
16866  if( rc==SQLITE_OK ){
16867    int nSqlCol = 0;
16868    int bSqlIntkey = 0;
16869    sqlite3_stmt *pStmt = 0;
16870
16871    rc = sqlite3_open("", &dbtmp);
16872    if( rc==SQLITE_OK ){
16873      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
16874                              shellIdQuote, 0, 0);
16875    }
16876    if( rc==SQLITE_OK ){
16877      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
16878    }
16879    if( rc==SQLITE_OK ){
16880      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
16881      if( rc==SQLITE_ERROR ){
16882        rc = SQLITE_OK;
16883        goto finished;
16884      }
16885    }
16886    shellPreparePrintf(dbtmp, &rc, &pStmt,
16887        "SELECT count(*) FROM pragma_table_info(%Q)", zName
16888    );
16889    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16890      nSqlCol = sqlite3_column_int(pStmt, 0);
16891    }
16892    shellFinalize(&rc, pStmt);
16893
16894    if( rc!=SQLITE_OK || nSqlCol<nCol ){
16895      goto finished;
16896    }
16897
16898    shellPreparePrintf(dbtmp, &rc, &pStmt,
16899      "SELECT ("
16900      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
16901      ") FROM sqlite_schema WHERE name = %Q", zName
16902    );
16903    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16904      bSqlIntkey = sqlite3_column_int(pStmt, 0);
16905    }
16906    shellFinalize(&rc, pStmt);
16907
16908    if( bIntkey==bSqlIntkey ){
16909      int i;
16910      const char *zPk = "_rowid_";
16911      sqlite3_stmt *pPkFinder = 0;
16912
16913      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
16914      ** set zPk to the name of the PK column, and pTab->iPk to the index
16915      ** of the column, where columns are 0-numbered from left to right.
16916      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
16917      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
16918      pTab->iPk = -2;
16919      if( bIntkey ){
16920        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
16921          "SELECT cid, name FROM pragma_table_info(%Q) "
16922          "  WHERE pk=1 AND type='integer' COLLATE nocase"
16923          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
16924          , zName, zName
16925        );
16926        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
16927          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
16928          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
16929        }
16930      }
16931
16932      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
16933      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
16934      pTab->nCol = nSqlCol;
16935
16936      if( bIntkey ){
16937        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
16938      }else{
16939        pTab->azlCol[0] = shellMPrintf(&rc, "");
16940      }
16941      i = 1;
16942      shellPreparePrintf(dbtmp, &rc, &pStmt,
16943          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
16944          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
16945          "FROM pragma_table_info(%Q)",
16946          bIntkey ? ", " : "", pTab->iPk,
16947          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
16948          zName
16949      );
16950      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16951        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
16952        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
16953        i++;
16954      }
16955      shellFinalize(&rc, pStmt);
16956
16957      shellFinalize(&rc, pPkFinder);
16958    }
16959  }
16960
16961 finished:
16962  sqlite3_close(dbtmp);
16963  *pRc = rc;
16964  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
16965    recoverFreeTable(pTab);
16966    pTab = 0;
16967  }
16968  return pTab;
16969}
16970
16971/*
16972** This function is called to search the schema recovered from the
16973** sqlite_schema table of the (possibly) corrupt database as part
16974** of a ".recover" command. Specifically, for a table with root page
16975** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
16976** table must be a WITHOUT ROWID table, or if non-zero, not one of
16977** those.
16978**
16979** If a table is found, a (RecoverTable*) object is returned. Or, if
16980** no such table is found, but bIntkey is false and iRoot is the
16981** root page of an index in the recovered schema, then (*pbNoop) is
16982** set to true and NULL returned. Or, if there is no such table or
16983** index, NULL is returned and (*pbNoop) set to 0, indicating that
16984** the caller should write data to the orphans table.
16985*/
16986static RecoverTable *recoverFindTable(
16987  ShellState *pState,             /* Shell state object */
16988  int *pRc,                       /* IN/OUT: Error code */
16989  int iRoot,                      /* Root page of table */
16990  int bIntkey,                    /* True for an intkey table */
16991  int nCol,                       /* Number of columns in table */
16992  int *pbNoop                     /* OUT: True if iRoot is root of index */
16993){
16994  sqlite3_stmt *pStmt = 0;
16995  RecoverTable *pRet = 0;
16996  int bNoop = 0;
16997  const char *zSql = 0;
16998  const char *zName = 0;
16999
17000  /* Search the recovered schema for an object with root page iRoot. */
17001  shellPreparePrintf(pState->db, pRc, &pStmt,
17002      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17003  );
17004  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17005    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17006    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17007      bNoop = 1;
17008      break;
17009    }
17010    if( sqlite3_stricmp(zType, "table")==0 ){
17011      zName = (const char*)sqlite3_column_text(pStmt, 1);
17012      zSql = (const char*)sqlite3_column_text(pStmt, 2);
17013      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17014      break;
17015    }
17016  }
17017
17018  shellFinalize(pRc, pStmt);
17019  *pbNoop = bNoop;
17020  return pRet;
17021}
17022
17023/*
17024** Return a RecoverTable object representing the orphans table.
17025*/
17026static RecoverTable *recoverOrphanTable(
17027  ShellState *pState,             /* Shell state object */
17028  int *pRc,                       /* IN/OUT: Error code */
17029  const char *zLostAndFound,      /* Base name for orphans table */
17030  int nCol                        /* Number of user data columns */
17031){
17032  RecoverTable *pTab = 0;
17033  if( nCol>=0 && *pRc==SQLITE_OK ){
17034    int i;
17035
17036    /* This block determines the name of the orphan table. The prefered
17037    ** name is zLostAndFound. But if that clashes with another name
17038    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17039    ** and so on until a non-clashing name is found.  */
17040    int iTab = 0;
17041    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17042    sqlite3_stmt *pTest = 0;
17043    shellPrepare(pState->db, pRc,
17044        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17045    );
17046    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17047    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17048      shellReset(pRc, pTest);
17049      sqlite3_free(zTab);
17050      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17051      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17052    }
17053    shellFinalize(pRc, pTest);
17054
17055    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17056    if( pTab ){
17057      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17058      pTab->nCol = nCol;
17059      pTab->iPk = -2;
17060      if( nCol>0 ){
17061        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17062        if( pTab->azlCol ){
17063          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17064          for(i=nCol-1; i>=0; i--){
17065            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17066          }
17067        }
17068      }
17069
17070      if( *pRc!=SQLITE_OK ){
17071        recoverFreeTable(pTab);
17072        pTab = 0;
17073      }else{
17074        raw_printf(pState->out,
17075            "CREATE TABLE %s(rootpgno INTEGER, "
17076            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17077        );
17078        for(i=0; i<nCol; i++){
17079          raw_printf(pState->out, ", c%d", i);
17080        }
17081        raw_printf(pState->out, ");\n");
17082      }
17083    }
17084    sqlite3_free(zTab);
17085  }
17086  return pTab;
17087}
17088
17089/*
17090** This function is called to recover data from the database. A script
17091** to construct a new database containing all recovered data is output
17092** on stream pState->out.
17093*/
17094static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
17095  int rc = SQLITE_OK;
17096  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
17097  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
17098  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
17099  const char *zRecoveryDb = "";   /* Name of "recovery" database */
17100  const char *zLostAndFound = "lost_and_found";
17101  int i;
17102  int nOrphan = -1;
17103  RecoverTable *pOrphan = 0;
17104
17105  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
17106  int bRowids = 1;                /* 0 if --no-rowids */
17107  for(i=1; i<nArg; i++){
17108    char *z = azArg[i];
17109    int n;
17110    if( z[0]=='-' && z[1]=='-' ) z++;
17111    n = strlen30(z);
17112    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
17113      bFreelist = 0;
17114    }else
17115    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
17116      i++;
17117      zRecoveryDb = azArg[i];
17118    }else
17119    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
17120      i++;
17121      zLostAndFound = azArg[i];
17122    }else
17123    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
17124      bRowids = 0;
17125    }
17126    else{
17127      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
17128      showHelp(pState->out, azArg[0]);
17129      return 1;
17130    }
17131  }
17132
17133  shellExecPrintf(pState->db, &rc,
17134    /* Attach an in-memory database named 'recovery'. Create an indexed
17135    ** cache of the sqlite_dbptr virtual table. */
17136    "PRAGMA writable_schema = on;"
17137    "ATTACH %Q AS recovery;"
17138    "DROP TABLE IF EXISTS recovery.dbptr;"
17139    "DROP TABLE IF EXISTS recovery.freelist;"
17140    "DROP TABLE IF EXISTS recovery.map;"
17141    "DROP TABLE IF EXISTS recovery.schema;"
17142    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
17143  );
17144
17145  if( bFreelist ){
17146    shellExec(pState->db, &rc,
17147      "WITH trunk(pgno) AS ("
17148      "  SELECT shell_int32("
17149      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
17150      "      WHERE x>0"
17151      "    UNION"
17152      "  SELECT shell_int32("
17153      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
17154      "      FROM trunk WHERE x>0"
17155      "),"
17156      "freelist(data, n, freepgno) AS ("
17157      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
17158      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
17159      "    UNION ALL"
17160      "  SELECT data, n-1, shell_int32(data, 2+n) "
17161      "      FROM freelist WHERE n>=0"
17162      ")"
17163      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
17164    );
17165  }
17166
17167  /* If this is an auto-vacuum database, add all pointer-map pages to
17168  ** the freelist table. Do this regardless of whether or not
17169  ** --freelist-corrupt was specified.  */
17170  shellExec(pState->db, &rc,
17171    "WITH ptrmap(pgno) AS ("
17172    "  SELECT 2 WHERE shell_int32("
17173    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
17174    "  )"
17175    "    UNION ALL "
17176    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
17177    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
17178    ")"
17179    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
17180  );
17181
17182  shellExec(pState->db, &rc,
17183    "CREATE TABLE recovery.dbptr("
17184    "      pgno, child, PRIMARY KEY(child, pgno)"
17185    ") WITHOUT ROWID;"
17186    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
17187    "    SELECT * FROM sqlite_dbptr"
17188    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
17189
17190    /* Delete any pointer to page 1. This ensures that page 1 is considered
17191    ** a root page, regardless of how corrupt the db is. */
17192    "DELETE FROM recovery.dbptr WHERE child = 1;"
17193
17194    /* Delete all pointers to any pages that have more than one pointer
17195    ** to them. Such pages will be treated as root pages when recovering
17196    ** data.  */
17197    "DELETE FROM recovery.dbptr WHERE child IN ("
17198    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
17199    ");"
17200
17201    /* Create the "map" table that will (eventually) contain instructions
17202    ** for dealing with each page in the db that contains one or more
17203    ** records. */
17204    "CREATE TABLE recovery.map("
17205      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
17206    ");"
17207
17208    /* Populate table [map]. If there are circular loops of pages in the
17209    ** database, the following adds all pages in such a loop to the map
17210    ** as individual root pages. This could be handled better.  */
17211    "WITH pages(i, maxlen) AS ("
17212    "  SELECT page_count, ("
17213    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
17214    "  ) FROM pragma_page_count WHERE page_count>0"
17215    "    UNION ALL"
17216    "  SELECT i-1, ("
17217    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
17218    "  ) FROM pages WHERE i>=2"
17219    ")"
17220    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
17221    "  SELECT i, maxlen, NULL, ("
17222    "    WITH p(orig, pgno, parent) AS ("
17223    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
17224    "        UNION "
17225    "      SELECT i, p.parent, "
17226    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
17227    "    )"
17228    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
17229    ") "
17230    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
17231    "UPDATE recovery.map AS o SET intkey = ("
17232    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
17233    ");"
17234
17235    /* Extract data from page 1 and any linked pages into table
17236    ** recovery.schema. With the same schema as an sqlite_schema table.  */
17237    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
17238    "INSERT INTO recovery.schema SELECT "
17239    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
17240    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
17241    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
17242    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
17243    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
17244    "FROM sqlite_dbdata WHERE pgno IN ("
17245    "  SELECT pgno FROM recovery.map WHERE root=1"
17246    ")"
17247    "GROUP BY pgno, cell;"
17248    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
17249  );
17250
17251  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
17252  ** CREATE TABLE statements that extracted from the existing schema.  */
17253  if( rc==SQLITE_OK ){
17254    sqlite3_stmt *pStmt = 0;
17255    /* ".recover" might output content in an order which causes immediate
17256    ** foreign key constraints to be violated. So disable foreign-key
17257    ** constraint enforcement to prevent problems when running the output
17258    ** script. */
17259    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
17260    raw_printf(pState->out, "BEGIN;\n");
17261    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
17262    shellPrepare(pState->db, &rc,
17263        "SELECT sql FROM recovery.schema "
17264        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
17265    );
17266    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17267      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
17268      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
17269          &zCreateTable[12]
17270      );
17271    }
17272    shellFinalize(&rc, pStmt);
17273  }
17274
17275  /* Figure out if an orphan table will be required. And if so, how many
17276  ** user columns it should contain */
17277  shellPrepare(pState->db, &rc,
17278      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
17279      , &pLoop
17280  );
17281  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17282    nOrphan = sqlite3_column_int(pLoop, 0);
17283  }
17284  shellFinalize(&rc, pLoop);
17285  pLoop = 0;
17286
17287  shellPrepare(pState->db, &rc,
17288      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
17289  );
17290
17291  shellPrepare(pState->db, &rc,
17292      "SELECT max(field), group_concat(shell_escape_crnl(quote"
17293      "(case when (? AND field<0) then NULL else value end)"
17294      "), ', ')"
17295      ", min(field) "
17296      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
17297      "GROUP BY cell", &pCells
17298  );
17299
17300  /* Loop through each root page. */
17301  shellPrepare(pState->db, &rc,
17302      "SELECT root, intkey, max(maxlen) FROM recovery.map"
17303      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
17304      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
17305      ")", &pLoop
17306  );
17307  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17308    int iRoot = sqlite3_column_int(pLoop, 0);
17309    int bIntkey = sqlite3_column_int(pLoop, 1);
17310    int nCol = sqlite3_column_int(pLoop, 2);
17311    int bNoop = 0;
17312    RecoverTable *pTab;
17313
17314    assert( bIntkey==0 || bIntkey==1 );
17315    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
17316    if( bNoop || rc ) continue;
17317    if( pTab==0 ){
17318      if( pOrphan==0 ){
17319        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17320      }
17321      pTab = pOrphan;
17322      if( pTab==0 ) break;
17323    }
17324
17325    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
17326      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
17327    }
17328    sqlite3_bind_int(pPages, 1, iRoot);
17329    if( bRowids==0 && pTab->iPk<0 ){
17330      sqlite3_bind_int(pCells, 1, 1);
17331    }else{
17332      sqlite3_bind_int(pCells, 1, 0);
17333    }
17334    sqlite3_bind_int(pCells, 3, pTab->iPk);
17335
17336    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
17337      int iPgno = sqlite3_column_int(pPages, 0);
17338      sqlite3_bind_int(pCells, 2, iPgno);
17339      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
17340        int nField = sqlite3_column_int(pCells, 0);
17341        int iMin = sqlite3_column_int(pCells, 2);
17342        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
17343
17344        RecoverTable *pTab2 = pTab;
17345        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
17346          if( pOrphan==0 ){
17347            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17348          }
17349          pTab2 = pOrphan;
17350          if( pTab2==0 ) break;
17351        }
17352
17353        nField = nField+1;
17354        if( pTab2==pOrphan ){
17355          raw_printf(pState->out,
17356              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
17357              pTab2->zQuoted, iRoot, iPgno, nField,
17358              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
17359          );
17360        }else{
17361          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
17362              pTab2->zQuoted, pTab2->azlCol[nField], zVal
17363          );
17364        }
17365      }
17366      shellReset(&rc, pCells);
17367    }
17368    shellReset(&rc, pPages);
17369    if( pTab!=pOrphan ) recoverFreeTable(pTab);
17370  }
17371  shellFinalize(&rc, pLoop);
17372  shellFinalize(&rc, pPages);
17373  shellFinalize(&rc, pCells);
17374  recoverFreeTable(pOrphan);
17375
17376  /* The rest of the schema */
17377  if( rc==SQLITE_OK ){
17378    sqlite3_stmt *pStmt = 0;
17379    shellPrepare(pState->db, &rc,
17380        "SELECT sql, name FROM recovery.schema "
17381        "WHERE sql NOT LIKE 'create table%'", &pStmt
17382    );
17383    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17384      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
17385      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
17386        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
17387        char *zPrint = shellMPrintf(&rc,
17388          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
17389          zName, zName, zSql
17390        );
17391        raw_printf(pState->out, "%s;\n", zPrint);
17392        sqlite3_free(zPrint);
17393      }else{
17394        raw_printf(pState->out, "%s;\n", zSql);
17395      }
17396    }
17397    shellFinalize(&rc, pStmt);
17398  }
17399
17400  if( rc==SQLITE_OK ){
17401    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
17402    raw_printf(pState->out, "COMMIT;\n");
17403  }
17404  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
17405  return rc;
17406}
17407#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17408
17409
17410/*
17411** If an input line begins with "." then invoke this routine to
17412** process that line.
17413**
17414** Return 1 on error, 2 to exit, and 0 otherwise.
17415*/
17416static int do_meta_command(char *zLine, ShellState *p){
17417  int h = 1;
17418  int nArg = 0;
17419  int n, c;
17420  int rc = 0;
17421  char *azArg[52];
17422
17423#ifndef SQLITE_OMIT_VIRTUALTABLE
17424  if( p->expert.pExpert ){
17425    expertFinish(p, 1, 0);
17426  }
17427#endif
17428
17429  /* Parse the input line into tokens.
17430  */
17431  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
17432    while( IsSpace(zLine[h]) ){ h++; }
17433    if( zLine[h]==0 ) break;
17434    if( zLine[h]=='\'' || zLine[h]=='"' ){
17435      int delim = zLine[h++];
17436      azArg[nArg++] = &zLine[h];
17437      while( zLine[h] && zLine[h]!=delim ){
17438        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
17439        h++;
17440      }
17441      if( zLine[h]==delim ){
17442        zLine[h++] = 0;
17443      }
17444      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
17445    }else{
17446      azArg[nArg++] = &zLine[h];
17447      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
17448      if( zLine[h] ) zLine[h++] = 0;
17449      resolve_backslashes(azArg[nArg-1]);
17450    }
17451  }
17452  azArg[nArg] = 0;
17453
17454  /* Process the input line.
17455  */
17456  if( nArg==0 ) return 0; /* no tokens, no error */
17457  n = strlen30(azArg[0]);
17458  c = azArg[0][0];
17459  clearTempFile(p);
17460
17461#ifndef SQLITE_OMIT_AUTHORIZATION
17462  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
17463    if( nArg!=2 ){
17464      raw_printf(stderr, "Usage: .auth ON|OFF\n");
17465      rc = 1;
17466      goto meta_command_exit;
17467    }
17468    open_db(p, 0);
17469    if( booleanValue(azArg[1]) ){
17470      sqlite3_set_authorizer(p->db, shellAuth, p);
17471    }else{
17472      sqlite3_set_authorizer(p->db, 0, 0);
17473    }
17474  }else
17475#endif
17476
17477#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17478  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
17479    open_db(p, 0);
17480    rc = arDotCommand(p, 0, azArg, nArg);
17481  }else
17482#endif
17483
17484  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
17485   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
17486  ){
17487    const char *zDestFile = 0;
17488    const char *zDb = 0;
17489    sqlite3 *pDest;
17490    sqlite3_backup *pBackup;
17491    int j;
17492    int bAsync = 0;
17493    const char *zVfs = 0;
17494    for(j=1; j<nArg; j++){
17495      const char *z = azArg[j];
17496      if( z[0]=='-' ){
17497        if( z[1]=='-' ) z++;
17498        if( strcmp(z, "-append")==0 ){
17499          zVfs = "apndvfs";
17500        }else
17501        if( strcmp(z, "-async")==0 ){
17502          bAsync = 1;
17503        }else
17504        {
17505          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
17506          return 1;
17507        }
17508      }else if( zDestFile==0 ){
17509        zDestFile = azArg[j];
17510      }else if( zDb==0 ){
17511        zDb = zDestFile;
17512        zDestFile = azArg[j];
17513      }else{
17514        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
17515        return 1;
17516      }
17517    }
17518    if( zDestFile==0 ){
17519      raw_printf(stderr, "missing FILENAME argument on .backup\n");
17520      return 1;
17521    }
17522    if( zDb==0 ) zDb = "main";
17523    rc = sqlite3_open_v2(zDestFile, &pDest,
17524                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
17525    if( rc!=SQLITE_OK ){
17526      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
17527      close_db(pDest);
17528      return 1;
17529    }
17530    if( bAsync ){
17531      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
17532                   0, 0, 0);
17533    }
17534    open_db(p, 0);
17535    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
17536    if( pBackup==0 ){
17537      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17538      close_db(pDest);
17539      return 1;
17540    }
17541    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
17542    sqlite3_backup_finish(pBackup);
17543    if( rc==SQLITE_DONE ){
17544      rc = 0;
17545    }else{
17546      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17547      rc = 1;
17548    }
17549    close_db(pDest);
17550  }else
17551
17552  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
17553    if( nArg==2 ){
17554      bail_on_error = booleanValue(azArg[1]);
17555    }else{
17556      raw_printf(stderr, "Usage: .bail on|off\n");
17557      rc = 1;
17558    }
17559  }else
17560
17561  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
17562    if( nArg==2 ){
17563      if( booleanValue(azArg[1]) ){
17564        setBinaryMode(p->out, 1);
17565      }else{
17566        setTextMode(p->out, 1);
17567      }
17568    }else{
17569      raw_printf(stderr, "Usage: .binary on|off\n");
17570      rc = 1;
17571    }
17572  }else
17573
17574  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
17575    if( nArg==2 ){
17576#if defined(_WIN32) || defined(WIN32)
17577      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
17578      rc = !SetCurrentDirectoryW(z);
17579      sqlite3_free(z);
17580#else
17581      rc = chdir(azArg[1]);
17582#endif
17583      if( rc ){
17584        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
17585        rc = 1;
17586      }
17587    }else{
17588      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
17589      rc = 1;
17590    }
17591  }else
17592
17593  /* The undocumented ".breakpoint" command causes a call to the no-op
17594  ** routine named test_breakpoint().
17595  */
17596  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
17597    test_breakpoint();
17598  }else
17599
17600  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
17601    if( nArg==2 ){
17602      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
17603    }else{
17604      raw_printf(stderr, "Usage: .changes on|off\n");
17605      rc = 1;
17606    }
17607  }else
17608
17609  /* Cancel output redirection, if it is currently set (by .testcase)
17610  ** Then read the content of the testcase-out.txt file and compare against
17611  ** azArg[1].  If there are differences, report an error and exit.
17612  */
17613  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
17614    char *zRes = 0;
17615    output_reset(p);
17616    if( nArg!=2 ){
17617      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
17618      rc = 2;
17619    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
17620      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
17621      rc = 2;
17622    }else if( testcase_glob(azArg[1],zRes)==0 ){
17623      utf8_printf(stderr,
17624                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
17625                 p->zTestcase, azArg[1], zRes);
17626      rc = 1;
17627    }else{
17628      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
17629      p->nCheck++;
17630    }
17631    sqlite3_free(zRes);
17632  }else
17633
17634  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
17635    if( nArg==2 ){
17636      tryToClone(p, azArg[1]);
17637    }else{
17638      raw_printf(stderr, "Usage: .clone FILENAME\n");
17639      rc = 1;
17640    }
17641  }else
17642
17643  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
17644    char **azName = 0;
17645    int nName = 0;
17646    sqlite3_stmt *pStmt;
17647    int i;
17648    open_db(p, 0);
17649    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17650    if( rc ){
17651      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17652      rc = 1;
17653    }else{
17654      while( sqlite3_step(pStmt)==SQLITE_ROW ){
17655        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
17656        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
17657        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
17658        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
17659        azName[nName*2] = strdup(zSchema);
17660        azName[nName*2+1] = strdup(zFile);
17661        nName++;
17662      }
17663    }
17664    sqlite3_finalize(pStmt);
17665    for(i=0; i<nName; i++){
17666      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
17667      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
17668      const char *z = azName[i*2+1];
17669      utf8_printf(p->out, "%s: %s %s%s\n",
17670         azName[i*2],
17671         z && z[0] ? z : "\"\"",
17672         bRdonly ? "r/o" : "r/w",
17673         eTxn==SQLITE_TXN_NONE ? "" :
17674            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
17675      free(azName[i*2]);
17676      free(azName[i*2+1]);
17677    }
17678    sqlite3_free(azName);
17679  }else
17680
17681  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
17682    static const struct DbConfigChoices {
17683      const char *zName;
17684      int op;
17685    } aDbConfig[] = {
17686        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
17687        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
17688        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
17689        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
17690        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
17691        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
17692        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
17693        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
17694        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
17695        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
17696        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
17697        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
17698        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
17699        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
17700        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
17701        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
17702    };
17703    int ii, v;
17704    open_db(p, 0);
17705    for(ii=0; ii<ArraySize(aDbConfig); ii++){
17706      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
17707      if( nArg>=3 ){
17708        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
17709      }
17710      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
17711      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
17712      if( nArg>1 ) break;
17713    }
17714    if( nArg>1 && ii==ArraySize(aDbConfig) ){
17715      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
17716      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
17717    }
17718  }else
17719
17720  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
17721    rc = shell_dbinfo_command(p, nArg, azArg);
17722  }else
17723
17724#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17725  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
17726    open_db(p, 0);
17727    rc = recoverDatabaseCmd(p, nArg, azArg);
17728  }else
17729#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17730
17731  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
17732    char *zLike = 0;
17733    char *zSql;
17734    int i;
17735    int savedShowHeader = p->showHeader;
17736    int savedShellFlags = p->shellFlgs;
17737    ShellClearFlag(p,
17738       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
17739       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
17740    for(i=1; i<nArg; i++){
17741      if( azArg[i][0]=='-' ){
17742        const char *z = azArg[i]+1;
17743        if( z[0]=='-' ) z++;
17744        if( strcmp(z,"preserve-rowids")==0 ){
17745#ifdef SQLITE_OMIT_VIRTUALTABLE
17746          raw_printf(stderr, "The --preserve-rowids option is not compatible"
17747                             " with SQLITE_OMIT_VIRTUALTABLE\n");
17748          rc = 1;
17749          sqlite3_free(zLike);
17750          goto meta_command_exit;
17751#else
17752          ShellSetFlag(p, SHFLG_PreserveRowid);
17753#endif
17754        }else
17755        if( strcmp(z,"newlines")==0 ){
17756          ShellSetFlag(p, SHFLG_Newlines);
17757        }else
17758        if( strcmp(z,"data-only")==0 ){
17759          ShellSetFlag(p, SHFLG_DumpDataOnly);
17760        }else
17761        if( strcmp(z,"nosys")==0 ){
17762          ShellSetFlag(p, SHFLG_DumpNoSys);
17763        }else
17764        {
17765          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
17766          rc = 1;
17767          sqlite3_free(zLike);
17768          goto meta_command_exit;
17769        }
17770      }else if( zLike ){
17771        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
17772                zLike, azArg[i]);
17773      }else{
17774        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
17775      }
17776    }
17777
17778    open_db(p, 0);
17779
17780    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17781      /* When playing back a "dump", the content might appear in an order
17782      ** which causes immediate foreign key constraints to be violated.
17783      ** So disable foreign-key constraint enforcement to prevent problems. */
17784      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
17785      raw_printf(p->out, "BEGIN TRANSACTION;\n");
17786    }
17787    p->writableSchema = 0;
17788    p->showHeader = 0;
17789    /* Set writable_schema=ON since doing so forces SQLite to initialize
17790    ** as much of the schema as it can even if the sqlite_schema table is
17791    ** corrupt. */
17792    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
17793    p->nErr = 0;
17794    if( zLike==0 ) zLike = sqlite3_mprintf("true");
17795    zSql = sqlite3_mprintf(
17796      "SELECT name, type, sql FROM sqlite_schema "
17797      "WHERE (%s) AND type=='table'"
17798      "  AND sql NOT NULL"
17799      " ORDER BY tbl_name='sqlite_sequence', rowid",
17800      zLike
17801    );
17802    run_schema_dump_query(p,zSql);
17803    sqlite3_free(zSql);
17804    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17805      zSql = sqlite3_mprintf(
17806        "SELECT sql FROM sqlite_schema "
17807        "WHERE (%s) AND sql NOT NULL"
17808        "  AND type IN ('index','trigger','view')",
17809        zLike
17810      );
17811      run_table_dump_query(p, zSql);
17812      sqlite3_free(zSql);
17813    }
17814    sqlite3_free(zLike);
17815    if( p->writableSchema ){
17816      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
17817      p->writableSchema = 0;
17818    }
17819    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
17820    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
17821    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17822      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
17823    }
17824    p->showHeader = savedShowHeader;
17825    p->shellFlgs = savedShellFlags;
17826  }else
17827
17828  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
17829    if( nArg==2 ){
17830      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
17831    }else{
17832      raw_printf(stderr, "Usage: .echo on|off\n");
17833      rc = 1;
17834    }
17835  }else
17836
17837  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
17838    if( nArg==2 ){
17839      p->autoEQPtest = 0;
17840      if( p->autoEQPtrace ){
17841        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
17842        p->autoEQPtrace = 0;
17843      }
17844      if( strcmp(azArg[1],"full")==0 ){
17845        p->autoEQP = AUTOEQP_full;
17846      }else if( strcmp(azArg[1],"trigger")==0 ){
17847        p->autoEQP = AUTOEQP_trigger;
17848#ifdef SQLITE_DEBUG
17849      }else if( strcmp(azArg[1],"test")==0 ){
17850        p->autoEQP = AUTOEQP_on;
17851        p->autoEQPtest = 1;
17852      }else if( strcmp(azArg[1],"trace")==0 ){
17853        p->autoEQP = AUTOEQP_full;
17854        p->autoEQPtrace = 1;
17855        open_db(p, 0);
17856        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
17857        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
17858#endif
17859      }else{
17860        p->autoEQP = (u8)booleanValue(azArg[1]);
17861      }
17862    }else{
17863      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
17864      rc = 1;
17865    }
17866  }else
17867
17868  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
17869    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
17870    rc = 2;
17871  }else
17872
17873  /* The ".explain" command is automatic now.  It is largely pointless.  It
17874  ** retained purely for backwards compatibility */
17875  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
17876    int val = 1;
17877    if( nArg>=2 ){
17878      if( strcmp(azArg[1],"auto")==0 ){
17879        val = 99;
17880      }else{
17881        val =  booleanValue(azArg[1]);
17882      }
17883    }
17884    if( val==1 && p->mode!=MODE_Explain ){
17885      p->normalMode = p->mode;
17886      p->mode = MODE_Explain;
17887      p->autoExplain = 0;
17888    }else if( val==0 ){
17889      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17890      p->autoExplain = 0;
17891    }else if( val==99 ){
17892      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17893      p->autoExplain = 1;
17894    }
17895  }else
17896
17897#ifndef SQLITE_OMIT_VIRTUALTABLE
17898  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
17899    open_db(p, 0);
17900    expertDotCommand(p, azArg, nArg);
17901  }else
17902#endif
17903
17904  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
17905    static const struct {
17906       const char *zCtrlName;   /* Name of a test-control option */
17907       int ctrlCode;            /* Integer code for that option */
17908       const char *zUsage;      /* Usage notes */
17909    } aCtrl[] = {
17910      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
17911      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
17912   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
17913      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
17914      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
17915   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
17916      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
17917      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
17918      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
17919      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
17920    };
17921    int filectrl = -1;
17922    int iCtrl = -1;
17923    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
17924    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
17925    int n2, i;
17926    const char *zCmd = 0;
17927    const char *zSchema = 0;
17928
17929    open_db(p, 0);
17930    zCmd = nArg>=2 ? azArg[1] : "help";
17931
17932    if( zCmd[0]=='-'
17933     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
17934     && nArg>=4
17935    ){
17936      zSchema = azArg[2];
17937      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
17938      nArg -= 2;
17939      zCmd = azArg[1];
17940    }
17941
17942    /* The argument can optionally begin with "-" or "--" */
17943    if( zCmd[0]=='-' && zCmd[1] ){
17944      zCmd++;
17945      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
17946    }
17947
17948    /* --help lists all file-controls */
17949    if( strcmp(zCmd,"help")==0 ){
17950      utf8_printf(p->out, "Available file-controls:\n");
17951      for(i=0; i<ArraySize(aCtrl); i++){
17952        utf8_printf(p->out, "  .filectrl %s %s\n",
17953                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
17954      }
17955      rc = 1;
17956      goto meta_command_exit;
17957    }
17958
17959    /* convert filectrl text option to value. allow any unique prefix
17960    ** of the option name, or a numerical value. */
17961    n2 = strlen30(zCmd);
17962    for(i=0; i<ArraySize(aCtrl); i++){
17963      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
17964        if( filectrl<0 ){
17965          filectrl = aCtrl[i].ctrlCode;
17966          iCtrl = i;
17967        }else{
17968          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
17969                              "Use \".filectrl --help\" for help\n", zCmd);
17970          rc = 1;
17971          goto meta_command_exit;
17972        }
17973      }
17974    }
17975    if( filectrl<0 ){
17976      utf8_printf(stderr,"Error: unknown file-control: %s\n"
17977                         "Use \".filectrl --help\" for help\n", zCmd);
17978    }else{
17979      switch(filectrl){
17980        case SQLITE_FCNTL_SIZE_LIMIT: {
17981          if( nArg!=2 && nArg!=3 ) break;
17982          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
17983          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
17984          isOk = 1;
17985          break;
17986        }
17987        case SQLITE_FCNTL_LOCK_TIMEOUT:
17988        case SQLITE_FCNTL_CHUNK_SIZE: {
17989          int x;
17990          if( nArg!=3 ) break;
17991          x = (int)integerValue(azArg[2]);
17992          sqlite3_file_control(p->db, zSchema, filectrl, &x);
17993          isOk = 2;
17994          break;
17995        }
17996        case SQLITE_FCNTL_PERSIST_WAL:
17997        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
17998          int x;
17999          if( nArg!=2 && nArg!=3 ) break;
18000          x = nArg==3 ? booleanValue(azArg[2]) : -1;
18001          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18002          iRes = x;
18003          isOk = 1;
18004          break;
18005        }
18006        case SQLITE_FCNTL_HAS_MOVED: {
18007          int x;
18008          if( nArg!=2 ) break;
18009          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18010          iRes = x;
18011          isOk = 1;
18012          break;
18013        }
18014        case SQLITE_FCNTL_TEMPFILENAME: {
18015          char *z = 0;
18016          if( nArg!=2 ) break;
18017          sqlite3_file_control(p->db, zSchema, filectrl, &z);
18018          if( z ){
18019            utf8_printf(p->out, "%s\n", z);
18020            sqlite3_free(z);
18021          }
18022          isOk = 2;
18023          break;
18024        }
18025        case SQLITE_FCNTL_RESERVE_BYTES: {
18026          int x;
18027          if( nArg>=3 ){
18028            x = atoi(azArg[2]);
18029            sqlite3_file_control(p->db, zSchema, filectrl, &x);
18030          }
18031          x = -1;
18032          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18033          utf8_printf(p->out,"%d\n", x);
18034          isOk = 2;
18035          break;
18036        }
18037      }
18038    }
18039    if( isOk==0 && iCtrl>=0 ){
18040      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18041      rc = 1;
18042    }else if( isOk==1 ){
18043      char zBuf[100];
18044      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18045      raw_printf(p->out, "%s\n", zBuf);
18046    }
18047  }else
18048
18049  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18050    ShellState data;
18051    char *zErrMsg = 0;
18052    int doStats = 0;
18053    memcpy(&data, p, sizeof(data));
18054    data.showHeader = 0;
18055    data.cMode = data.mode = MODE_Semi;
18056    if( nArg==2 && optionMatch(azArg[1], "indent") ){
18057      data.cMode = data.mode = MODE_Pretty;
18058      nArg = 1;
18059    }
18060    if( nArg!=1 ){
18061      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18062      rc = 1;
18063      goto meta_command_exit;
18064    }
18065    open_db(p, 0);
18066    rc = sqlite3_exec(p->db,
18067       "SELECT sql FROM"
18068       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
18069       "     FROM sqlite_schema UNION ALL"
18070       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
18071       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
18072       "ORDER BY rowid",
18073       callback, &data, &zErrMsg
18074    );
18075    if( rc==SQLITE_OK ){
18076      sqlite3_stmt *pStmt;
18077      rc = sqlite3_prepare_v2(p->db,
18078               "SELECT rowid FROM sqlite_schema"
18079               " WHERE name GLOB 'sqlite_stat[134]'",
18080               -1, &pStmt, 0);
18081      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
18082      sqlite3_finalize(pStmt);
18083    }
18084    if( doStats==0 ){
18085      raw_printf(p->out, "/* No STAT tables available */\n");
18086    }else{
18087      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18088      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
18089                   callback, &data, &zErrMsg);
18090      data.cMode = data.mode = MODE_Insert;
18091      data.zDestTable = "sqlite_stat1";
18092      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
18093      data.zDestTable = "sqlite_stat4";
18094      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
18095      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18096    }
18097  }else
18098
18099  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
18100    if( nArg==2 ){
18101      p->showHeader = booleanValue(azArg[1]);
18102      p->shellFlgs |= SHFLG_HeaderSet;
18103    }else{
18104      raw_printf(stderr, "Usage: .headers on|off\n");
18105      rc = 1;
18106    }
18107  }else
18108
18109  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
18110    if( nArg>=2 ){
18111      n = showHelp(p->out, azArg[1]);
18112      if( n==0 ){
18113        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
18114      }
18115    }else{
18116      showHelp(p->out, 0);
18117    }
18118  }else
18119
18120  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
18121    char *zTable = 0;           /* Insert data into this table */
18122    char *zFile = 0;            /* Name of file to extra content from */
18123    sqlite3_stmt *pStmt = NULL; /* A statement */
18124    int nCol;                   /* Number of columns in the table */
18125    int nByte;                  /* Number of bytes in an SQL string */
18126    int i, j;                   /* Loop counters */
18127    int needCommit;             /* True to COMMIT or ROLLBACK at end */
18128    int nSep;                   /* Number of bytes in p->colSeparator[] */
18129    char *zSql;                 /* An SQL statement */
18130    ImportCtx sCtx;             /* Reader context */
18131    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
18132    int eVerbose = 0;           /* Larger for more console output */
18133    int nSkip = 0;              /* Initial lines to skip */
18134    int useOutputMode = 1;      /* Use output mode to determine separators */
18135
18136    memset(&sCtx, 0, sizeof(sCtx));
18137    if( p->mode==MODE_Ascii ){
18138      xRead = ascii_read_one_field;
18139    }else{
18140      xRead = csv_read_one_field;
18141    }
18142    for(i=1; i<nArg; i++){
18143      char *z = azArg[i];
18144      if( z[0]=='-' && z[1]=='-' ) z++;
18145      if( z[0]!='-' ){
18146        if( zFile==0 ){
18147          zFile = z;
18148        }else if( zTable==0 ){
18149          zTable = z;
18150        }else{
18151          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
18152          showHelp(p->out, "import");
18153          rc = 1;
18154          goto meta_command_exit;
18155        }
18156      }else if( strcmp(z,"-v")==0 ){
18157        eVerbose++;
18158      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
18159        nSkip = integerValue(azArg[++i]);
18160      }else if( strcmp(z,"-ascii")==0 ){
18161        sCtx.cColSep = SEP_Unit[0];
18162        sCtx.cRowSep = SEP_Record[0];
18163        xRead = ascii_read_one_field;
18164        useOutputMode = 0;
18165      }else if( strcmp(z,"-csv")==0 ){
18166        sCtx.cColSep = ',';
18167        sCtx.cRowSep = '\n';
18168        xRead = csv_read_one_field;
18169        useOutputMode = 0;
18170      }else{
18171        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
18172        showHelp(p->out, "import");
18173        rc = 1;
18174        goto meta_command_exit;
18175      }
18176    }
18177    if( zTable==0 ){
18178      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
18179                  zFile==0 ? "FILE" : "TABLE");
18180      showHelp(p->out, "import");
18181      rc = 1;
18182      goto meta_command_exit;
18183    }
18184    seenInterrupt = 0;
18185    open_db(p, 0);
18186    if( useOutputMode ){
18187      /* If neither the --csv or --ascii options are specified, then set
18188      ** the column and row separator characters from the output mode. */
18189      nSep = strlen30(p->colSeparator);
18190      if( nSep==0 ){
18191        raw_printf(stderr,
18192                   "Error: non-null column separator required for import\n");
18193        rc = 1;
18194        goto meta_command_exit;
18195      }
18196      if( nSep>1 ){
18197        raw_printf(stderr,
18198              "Error: multi-character column separators not allowed"
18199              " for import\n");
18200        rc = 1;
18201        goto meta_command_exit;
18202      }
18203      nSep = strlen30(p->rowSeparator);
18204      if( nSep==0 ){
18205        raw_printf(stderr,
18206            "Error: non-null row separator required for import\n");
18207        rc = 1;
18208        goto meta_command_exit;
18209      }
18210      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
18211        /* When importing CSV (only), if the row separator is set to the
18212        ** default output row separator, change it to the default input
18213        ** row separator.  This avoids having to maintain different input
18214        ** and output row separators. */
18215        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18216        nSep = strlen30(p->rowSeparator);
18217      }
18218      if( nSep>1 ){
18219        raw_printf(stderr, "Error: multi-character row separators not allowed"
18220                           " for import\n");
18221        rc = 1;
18222        goto meta_command_exit;
18223      }
18224      sCtx.cColSep = p->colSeparator[0];
18225      sCtx.cRowSep = p->rowSeparator[0];
18226    }
18227    sCtx.zFile = zFile;
18228    sCtx.nLine = 1;
18229    if( sCtx.zFile[0]=='|' ){
18230#ifdef SQLITE_OMIT_POPEN
18231      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18232      rc = 1;
18233      goto meta_command_exit;
18234#else
18235      sCtx.in = popen(sCtx.zFile+1, "r");
18236      sCtx.zFile = "<pipe>";
18237      sCtx.xCloser = pclose;
18238#endif
18239    }else{
18240      sCtx.in = fopen(sCtx.zFile, "rb");
18241      sCtx.xCloser = fclose;
18242    }
18243    if( sCtx.in==0 ){
18244      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
18245      rc = 1;
18246      goto meta_command_exit;
18247    }
18248    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
18249      char zSep[2];
18250      zSep[1] = 0;
18251      zSep[0] = sCtx.cColSep;
18252      utf8_printf(p->out, "Column separator ");
18253      output_c_string(p->out, zSep);
18254      utf8_printf(p->out, ", row separator ");
18255      zSep[0] = sCtx.cRowSep;
18256      output_c_string(p->out, zSep);
18257      utf8_printf(p->out, "\n");
18258    }
18259    while( (nSkip--)>0 ){
18260      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
18261    }
18262    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
18263    if( zSql==0 ){
18264      import_cleanup(&sCtx);
18265      shell_out_of_memory();
18266    }
18267    nByte = strlen30(zSql);
18268    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18269    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
18270    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
18271      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
18272      char cSep = '(';
18273      while( xRead(&sCtx) ){
18274        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
18275        cSep = ',';
18276        if( sCtx.cTerm!=sCtx.cColSep ) break;
18277      }
18278      if( cSep=='(' ){
18279        sqlite3_free(zCreate);
18280        import_cleanup(&sCtx);
18281        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
18282        rc = 1;
18283        goto meta_command_exit;
18284      }
18285      zCreate = sqlite3_mprintf("%z\n)", zCreate);
18286      if( eVerbose>=1 ){
18287        utf8_printf(p->out, "%s\n", zCreate);
18288      }
18289      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
18290      sqlite3_free(zCreate);
18291      if( rc ){
18292        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
18293                sqlite3_errmsg(p->db));
18294        import_cleanup(&sCtx);
18295        rc = 1;
18296        goto meta_command_exit;
18297      }
18298      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18299    }
18300    sqlite3_free(zSql);
18301    if( rc ){
18302      if (pStmt) sqlite3_finalize(pStmt);
18303      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
18304      import_cleanup(&sCtx);
18305      rc = 1;
18306      goto meta_command_exit;
18307    }
18308    nCol = sqlite3_column_count(pStmt);
18309    sqlite3_finalize(pStmt);
18310    pStmt = 0;
18311    if( nCol==0 ) return 0; /* no columns, no error */
18312    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
18313    if( zSql==0 ){
18314      import_cleanup(&sCtx);
18315      shell_out_of_memory();
18316    }
18317    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
18318    j = strlen30(zSql);
18319    for(i=1; i<nCol; i++){
18320      zSql[j++] = ',';
18321      zSql[j++] = '?';
18322    }
18323    zSql[j++] = ')';
18324    zSql[j] = 0;
18325    if( eVerbose>=2 ){
18326      utf8_printf(p->out, "Insert using: %s\n", zSql);
18327    }
18328    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18329    sqlite3_free(zSql);
18330    if( rc ){
18331      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18332      if (pStmt) sqlite3_finalize(pStmt);
18333      import_cleanup(&sCtx);
18334      rc = 1;
18335      goto meta_command_exit;
18336    }
18337    needCommit = sqlite3_get_autocommit(p->db);
18338    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
18339    do{
18340      int startLine = sCtx.nLine;
18341      for(i=0; i<nCol; i++){
18342        char *z = xRead(&sCtx);
18343        /*
18344        ** Did we reach end-of-file before finding any columns?
18345        ** If so, stop instead of NULL filling the remaining columns.
18346        */
18347        if( z==0 && i==0 ) break;
18348        /*
18349        ** Did we reach end-of-file OR end-of-line before finding any
18350        ** columns in ASCII mode?  If so, stop instead of NULL filling
18351        ** the remaining columns.
18352        */
18353        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
18354        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
18355        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
18356          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18357                          "filling the rest with NULL\n",
18358                          sCtx.zFile, startLine, nCol, i+1);
18359          i += 2;
18360          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
18361        }
18362      }
18363      if( sCtx.cTerm==sCtx.cColSep ){
18364        do{
18365          xRead(&sCtx);
18366          i++;
18367        }while( sCtx.cTerm==sCtx.cColSep );
18368        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18369                        "extras ignored\n",
18370                        sCtx.zFile, startLine, nCol, i);
18371      }
18372      if( i>=nCol ){
18373        sqlite3_step(pStmt);
18374        rc = sqlite3_reset(pStmt);
18375        if( rc!=SQLITE_OK ){
18376          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
18377                      startLine, sqlite3_errmsg(p->db));
18378          sCtx.nErr++;
18379        }else{
18380          sCtx.nRow++;
18381        }
18382      }
18383    }while( sCtx.cTerm!=EOF );
18384
18385    import_cleanup(&sCtx);
18386    sqlite3_finalize(pStmt);
18387    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
18388    if( eVerbose>0 ){
18389      utf8_printf(p->out,
18390          "Added %d rows with %d errors using %d lines of input\n",
18391          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
18392    }
18393  }else
18394
18395#ifndef SQLITE_UNTESTABLE
18396  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
18397    char *zSql;
18398    char *zCollist = 0;
18399    sqlite3_stmt *pStmt;
18400    int tnum = 0;
18401    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
18402    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
18403    int i;
18404    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
18405      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
18406                          "       .imposter off\n");
18407      /* Also allowed, but not documented:
18408      **
18409      **    .imposter TABLE IMPOSTER
18410      **
18411      ** where TABLE is a WITHOUT ROWID table.  In that case, the
18412      ** imposter is another WITHOUT ROWID table with the columns in
18413      ** storage order. */
18414      rc = 1;
18415      goto meta_command_exit;
18416    }
18417    open_db(p, 0);
18418    if( nArg==2 ){
18419      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
18420      goto meta_command_exit;
18421    }
18422    zSql = sqlite3_mprintf(
18423      "SELECT rootpage, 0 FROM sqlite_schema"
18424      " WHERE name='%q' AND type='index'"
18425      "UNION ALL "
18426      "SELECT rootpage, 1 FROM sqlite_schema"
18427      " WHERE name='%q' AND type='table'"
18428      "   AND sql LIKE '%%without%%rowid%%'",
18429      azArg[1], azArg[1]
18430    );
18431    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18432    sqlite3_free(zSql);
18433    if( sqlite3_step(pStmt)==SQLITE_ROW ){
18434      tnum = sqlite3_column_int(pStmt, 0);
18435      isWO = sqlite3_column_int(pStmt, 1);
18436    }
18437    sqlite3_finalize(pStmt);
18438    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
18439    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18440    sqlite3_free(zSql);
18441    i = 0;
18442    while( sqlite3_step(pStmt)==SQLITE_ROW ){
18443      char zLabel[20];
18444      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
18445      i++;
18446      if( zCol==0 ){
18447        if( sqlite3_column_int(pStmt,1)==-1 ){
18448          zCol = "_ROWID_";
18449        }else{
18450          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
18451          zCol = zLabel;
18452        }
18453      }
18454      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
18455        lenPK = (int)strlen(zCollist);
18456      }
18457      if( zCollist==0 ){
18458        zCollist = sqlite3_mprintf("\"%w\"", zCol);
18459      }else{
18460        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
18461      }
18462    }
18463    sqlite3_finalize(pStmt);
18464    if( i==0 || tnum==0 ){
18465      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
18466      rc = 1;
18467      sqlite3_free(zCollist);
18468      goto meta_command_exit;
18469    }
18470    if( lenPK==0 ) lenPK = 100000;
18471    zSql = sqlite3_mprintf(
18472          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
18473          azArg[2], zCollist, lenPK, zCollist);
18474    sqlite3_free(zCollist);
18475    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
18476    if( rc==SQLITE_OK ){
18477      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
18478      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
18479      if( rc ){
18480        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
18481      }else{
18482        utf8_printf(stdout, "%s;\n", zSql);
18483        raw_printf(stdout,
18484          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
18485          azArg[1], isWO ? "table" : "index"
18486        );
18487      }
18488    }else{
18489      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
18490      rc = 1;
18491    }
18492    sqlite3_free(zSql);
18493  }else
18494#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
18495
18496#ifdef SQLITE_ENABLE_IOTRACE
18497  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
18498    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
18499    if( iotrace && iotrace!=stdout ) fclose(iotrace);
18500    iotrace = 0;
18501    if( nArg<2 ){
18502      sqlite3IoTrace = 0;
18503    }else if( strcmp(azArg[1], "-")==0 ){
18504      sqlite3IoTrace = iotracePrintf;
18505      iotrace = stdout;
18506    }else{
18507      iotrace = fopen(azArg[1], "w");
18508      if( iotrace==0 ){
18509        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
18510        sqlite3IoTrace = 0;
18511        rc = 1;
18512      }else{
18513        sqlite3IoTrace = iotracePrintf;
18514      }
18515    }
18516  }else
18517#endif
18518
18519  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
18520    static const struct {
18521       const char *zLimitName;   /* Name of a limit */
18522       int limitCode;            /* Integer code for that limit */
18523    } aLimit[] = {
18524      { "length",                SQLITE_LIMIT_LENGTH                    },
18525      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
18526      { "column",                SQLITE_LIMIT_COLUMN                    },
18527      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
18528      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
18529      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
18530      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
18531      { "attached",              SQLITE_LIMIT_ATTACHED                  },
18532      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
18533      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
18534      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
18535      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
18536    };
18537    int i, n2;
18538    open_db(p, 0);
18539    if( nArg==1 ){
18540      for(i=0; i<ArraySize(aLimit); i++){
18541        printf("%20s %d\n", aLimit[i].zLimitName,
18542               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
18543      }
18544    }else if( nArg>3 ){
18545      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
18546      rc = 1;
18547      goto meta_command_exit;
18548    }else{
18549      int iLimit = -1;
18550      n2 = strlen30(azArg[1]);
18551      for(i=0; i<ArraySize(aLimit); i++){
18552        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
18553          if( iLimit<0 ){
18554            iLimit = i;
18555          }else{
18556            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
18557            rc = 1;
18558            goto meta_command_exit;
18559          }
18560        }
18561      }
18562      if( iLimit<0 ){
18563        utf8_printf(stderr, "unknown limit: \"%s\"\n"
18564                        "enter \".limits\" with no arguments for a list.\n",
18565                         azArg[1]);
18566        rc = 1;
18567        goto meta_command_exit;
18568      }
18569      if( nArg==3 ){
18570        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
18571                      (int)integerValue(azArg[2]));
18572      }
18573      printf("%20s %d\n", aLimit[iLimit].zLimitName,
18574             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
18575    }
18576  }else
18577
18578  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
18579    open_db(p, 0);
18580    lintDotCommand(p, azArg, nArg);
18581  }else
18582
18583#ifndef SQLITE_OMIT_LOAD_EXTENSION
18584  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
18585    const char *zFile, *zProc;
18586    char *zErrMsg = 0;
18587    if( nArg<2 ){
18588      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
18589      rc = 1;
18590      goto meta_command_exit;
18591    }
18592    zFile = azArg[1];
18593    zProc = nArg>=3 ? azArg[2] : 0;
18594    open_db(p, 0);
18595    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
18596    if( rc!=SQLITE_OK ){
18597      utf8_printf(stderr, "Error: %s\n", zErrMsg);
18598      sqlite3_free(zErrMsg);
18599      rc = 1;
18600    }
18601  }else
18602#endif
18603
18604  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
18605    if( nArg!=2 ){
18606      raw_printf(stderr, "Usage: .log FILENAME\n");
18607      rc = 1;
18608    }else{
18609      const char *zFile = azArg[1];
18610      output_file_close(p->pLog);
18611      p->pLog = output_file_open(zFile, 0);
18612    }
18613  }else
18614
18615  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
18616    const char *zMode = nArg>=2 ? azArg[1] : "";
18617    int n2 = strlen30(zMode);
18618    int c2 = zMode[0];
18619    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
18620      p->mode = MODE_Line;
18621      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18622    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
18623      p->mode = MODE_Column;
18624      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
18625        p->showHeader = 1;
18626      }
18627      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18628    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
18629      p->mode = MODE_List;
18630      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
18631      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18632    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
18633      p->mode = MODE_Html;
18634    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
18635      p->mode = MODE_Tcl;
18636      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
18637      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18638    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
18639      p->mode = MODE_Csv;
18640      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18641      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18642    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
18643      p->mode = MODE_List;
18644      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
18645    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
18646      p->mode = MODE_Insert;
18647      set_table_name(p, nArg>=3 ? azArg[2] : "table");
18648    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
18649      p->mode = MODE_Quote;
18650      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18651      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18652    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
18653      p->mode = MODE_Ascii;
18654      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
18655      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
18656    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
18657      p->mode = MODE_Markdown;
18658    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
18659      p->mode = MODE_Table;
18660    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
18661      p->mode = MODE_Box;
18662    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
18663      p->mode = MODE_Json;
18664    }else if( nArg==1 ){
18665      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
18666    }else{
18667      raw_printf(stderr, "Error: mode should be one of: "
18668         "ascii box column csv html insert json line list markdown "
18669         "quote table tabs tcl\n");
18670      rc = 1;
18671    }
18672    p->cMode = p->mode;
18673  }else
18674
18675  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
18676    if( nArg==2 ){
18677      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
18678                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
18679    }else{
18680      raw_printf(stderr, "Usage: .nullvalue STRING\n");
18681      rc = 1;
18682    }
18683  }else
18684
18685#ifdef SQLITE_DEBUG
18686  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
18687    int i;
18688    for(i=1; i<nArg; i++){
18689      const char *z = azArg[i];
18690      if( z[0]=='-' && z[1]=='-' ) z++;
18691      if( strcmp(z,"-repeat")==0 ){
18692        if( i==nArg-1 ){
18693          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
18694          rc = 1;
18695        }else{
18696          oomRepeat = (int)integerValue(azArg[++i]);
18697        }
18698      }else if( IsDigit(z[0]) ){
18699        oomCounter = (int)integerValue(azArg[i]);
18700      }else{
18701        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
18702        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
18703        rc = 1;
18704      }
18705    }
18706    if( rc==0 ){
18707      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
18708      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
18709    }
18710  }else
18711#endif /* SQLITE_DEBUG */
18712
18713  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
18714    char *zNewFilename;  /* Name of the database file to open */
18715    int iName = 1;       /* Index in azArg[] of the filename */
18716    int newFlag = 0;     /* True to delete file before opening */
18717    /* Close the existing database */
18718    session_close_all(p);
18719    close_db(p->db);
18720    p->db = 0;
18721    p->zDbFilename = 0;
18722    sqlite3_free(p->zFreeOnClose);
18723    p->zFreeOnClose = 0;
18724    p->openMode = SHELL_OPEN_UNSPEC;
18725    p->openFlags = 0;
18726    p->szMax = 0;
18727    /* Check for command-line arguments */
18728    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
18729      const char *z = azArg[iName];
18730      if( optionMatch(z,"new") ){
18731        newFlag = 1;
18732#ifdef SQLITE_HAVE_ZLIB
18733      }else if( optionMatch(z, "zip") ){
18734        p->openMode = SHELL_OPEN_ZIPFILE;
18735#endif
18736      }else if( optionMatch(z, "append") ){
18737        p->openMode = SHELL_OPEN_APPENDVFS;
18738      }else if( optionMatch(z, "readonly") ){
18739        p->openMode = SHELL_OPEN_READONLY;
18740      }else if( optionMatch(z, "nofollow") ){
18741        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
18742#ifdef SQLITE_ENABLE_DESERIALIZE
18743      }else if( optionMatch(z, "deserialize") ){
18744        p->openMode = SHELL_OPEN_DESERIALIZE;
18745      }else if( optionMatch(z, "hexdb") ){
18746        p->openMode = SHELL_OPEN_HEXDB;
18747      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
18748        p->szMax = integerValue(azArg[++iName]);
18749#endif /* SQLITE_ENABLE_DESERIALIZE */
18750      }else if( z[0]=='-' ){
18751        utf8_printf(stderr, "unknown option: %s\n", z);
18752        rc = 1;
18753        goto meta_command_exit;
18754      }
18755    }
18756    /* If a filename is specified, try to open it first */
18757    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
18758    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
18759      if( newFlag ) shellDeleteFile(zNewFilename);
18760      p->zDbFilename = zNewFilename;
18761      open_db(p, OPEN_DB_KEEPALIVE);
18762      if( p->db==0 ){
18763        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
18764        sqlite3_free(zNewFilename);
18765      }else{
18766        p->zFreeOnClose = zNewFilename;
18767      }
18768    }
18769    if( p->db==0 ){
18770      /* As a fall-back open a TEMP database */
18771      p->zDbFilename = 0;
18772      open_db(p, 0);
18773    }
18774  }else
18775
18776  if( (c=='o'
18777        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
18778   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
18779  ){
18780    const char *zFile = 0;
18781    int bTxtMode = 0;
18782    int i;
18783    int eMode = 0;
18784    int bBOM = 0;
18785    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
18786
18787    if( c=='e' ){
18788      eMode = 'x';
18789      bOnce = 2;
18790    }else if( strncmp(azArg[0],"once",n)==0 ){
18791      bOnce = 1;
18792    }
18793    for(i=1; i<nArg; i++){
18794      char *z = azArg[i];
18795      if( z[0]=='-' ){
18796        if( z[1]=='-' ) z++;
18797        if( strcmp(z,"-bom")==0 ){
18798          bBOM = 1;
18799        }else if( c!='e' && strcmp(z,"-x")==0 ){
18800          eMode = 'x';  /* spreadsheet */
18801        }else if( c!='e' && strcmp(z,"-e")==0 ){
18802          eMode = 'e';  /* text editor */
18803        }else{
18804          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
18805                      azArg[i]);
18806          showHelp(p->out, azArg[0]);
18807          rc = 1;
18808          goto meta_command_exit;
18809        }
18810      }else if( zFile==0 ){
18811        zFile = z;
18812      }else{
18813        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
18814                    azArg[i]);
18815        showHelp(p->out, azArg[0]);
18816        rc = 1;
18817        goto meta_command_exit;
18818      }
18819    }
18820    if( zFile==0 ) zFile = "stdout";
18821    if( bOnce ){
18822      p->outCount = 2;
18823    }else{
18824      p->outCount = 0;
18825    }
18826    output_reset(p);
18827#ifndef SQLITE_NOHAVE_SYSTEM
18828    if( eMode=='e' || eMode=='x' ){
18829      p->doXdgOpen = 1;
18830      outputModePush(p);
18831      if( eMode=='x' ){
18832        /* spreadsheet mode.  Output as CSV. */
18833        newTempFile(p, "csv");
18834        ShellClearFlag(p, SHFLG_Echo);
18835        p->mode = MODE_Csv;
18836        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18837        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18838      }else{
18839        /* text editor mode */
18840        newTempFile(p, "txt");
18841        bTxtMode = 1;
18842      }
18843      zFile = p->zTempFile;
18844    }
18845#endif /* SQLITE_NOHAVE_SYSTEM */
18846    if( zFile[0]=='|' ){
18847#ifdef SQLITE_OMIT_POPEN
18848      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18849      rc = 1;
18850      p->out = stdout;
18851#else
18852      p->out = popen(zFile + 1, "w");
18853      if( p->out==0 ){
18854        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
18855        p->out = stdout;
18856        rc = 1;
18857      }else{
18858        if( bBOM ) fprintf(p->out,"\357\273\277");
18859        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18860      }
18861#endif
18862    }else{
18863      p->out = output_file_open(zFile, bTxtMode);
18864      if( p->out==0 ){
18865        if( strcmp(zFile,"off")!=0 ){
18866          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
18867        }
18868        p->out = stdout;
18869        rc = 1;
18870      } else {
18871        if( bBOM ) fprintf(p->out,"\357\273\277");
18872        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18873      }
18874    }
18875  }else
18876
18877  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
18878    open_db(p,0);
18879    if( nArg<=1 ) goto parameter_syntax_error;
18880
18881    /* .parameter clear
18882    ** Clear all bind parameters by dropping the TEMP table that holds them.
18883    */
18884    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
18885      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
18886                   0, 0, 0);
18887    }else
18888
18889    /* .parameter list
18890    ** List all bind parameters.
18891    */
18892    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
18893      sqlite3_stmt *pStmt = 0;
18894      int rx;
18895      int len = 0;
18896      rx = sqlite3_prepare_v2(p->db,
18897             "SELECT max(length(key)) "
18898             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18899      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
18900        len = sqlite3_column_int(pStmt, 0);
18901        if( len>40 ) len = 40;
18902      }
18903      sqlite3_finalize(pStmt);
18904      pStmt = 0;
18905      if( len ){
18906        rx = sqlite3_prepare_v2(p->db,
18907             "SELECT key, quote(value) "
18908             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18909        while( sqlite3_step(pStmt)==SQLITE_ROW ){
18910          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
18911                      sqlite3_column_text(pStmt,1));
18912        }
18913        sqlite3_finalize(pStmt);
18914      }
18915    }else
18916
18917    /* .parameter init
18918    ** Make sure the TEMP table used to hold bind parameters exists.
18919    ** Create it if necessary.
18920    */
18921    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
18922      bind_table_init(p);
18923    }else
18924
18925    /* .parameter set NAME VALUE
18926    ** Set or reset a bind parameter.  NAME should be the full parameter
18927    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
18928    ** VALUE can be in either SQL literal notation, or if not it will be
18929    ** understood to be a text string.
18930    */
18931    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
18932      int rx;
18933      char *zSql;
18934      sqlite3_stmt *pStmt;
18935      const char *zKey = azArg[2];
18936      const char *zValue = azArg[3];
18937      bind_table_init(p);
18938      zSql = sqlite3_mprintf(
18939                  "REPLACE INTO temp.sqlite_parameters(key,value)"
18940                  "VALUES(%Q,%s);", zKey, zValue);
18941      if( zSql==0 ) shell_out_of_memory();
18942      pStmt = 0;
18943      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18944      sqlite3_free(zSql);
18945      if( rx!=SQLITE_OK ){
18946        sqlite3_finalize(pStmt);
18947        pStmt = 0;
18948        zSql = sqlite3_mprintf(
18949                   "REPLACE INTO temp.sqlite_parameters(key,value)"
18950                   "VALUES(%Q,%Q);", zKey, zValue);
18951        if( zSql==0 ) shell_out_of_memory();
18952        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18953        sqlite3_free(zSql);
18954        if( rx!=SQLITE_OK ){
18955          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
18956          sqlite3_finalize(pStmt);
18957          pStmt = 0;
18958          rc = 1;
18959        }
18960      }
18961      sqlite3_step(pStmt);
18962      sqlite3_finalize(pStmt);
18963    }else
18964
18965    /* .parameter unset NAME
18966    ** Remove the NAME binding from the parameter binding table, if it
18967    ** exists.
18968    */
18969    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
18970      char *zSql = sqlite3_mprintf(
18971          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
18972      if( zSql==0 ) shell_out_of_memory();
18973      sqlite3_exec(p->db, zSql, 0, 0, 0);
18974      sqlite3_free(zSql);
18975    }else
18976    /* If no command name matches, show a syntax error */
18977    parameter_syntax_error:
18978    showHelp(p->out, "parameter");
18979  }else
18980
18981  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
18982    int i;
18983    for(i=1; i<nArg; i++){
18984      if( i>1 ) raw_printf(p->out, " ");
18985      utf8_printf(p->out, "%s", azArg[i]);
18986    }
18987    raw_printf(p->out, "\n");
18988  }else
18989
18990#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
18991  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
18992    int i;
18993    int nn = 0;
18994    p->flgProgress = 0;
18995    p->mxProgress = 0;
18996    p->nProgress = 0;
18997    for(i=1; i<nArg; i++){
18998      const char *z = azArg[i];
18999      if( z[0]=='-' ){
19000        z++;
19001        if( z[0]=='-' ) z++;
19002        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19003          p->flgProgress |= SHELL_PROGRESS_QUIET;
19004          continue;
19005        }
19006        if( strcmp(z,"reset")==0 ){
19007          p->flgProgress |= SHELL_PROGRESS_RESET;
19008          continue;
19009        }
19010        if( strcmp(z,"once")==0 ){
19011          p->flgProgress |= SHELL_PROGRESS_ONCE;
19012          continue;
19013        }
19014        if( strcmp(z,"limit")==0 ){
19015          if( i+1>=nArg ){
19016            utf8_printf(stderr, "Error: missing argument on --limit\n");
19017            rc = 1;
19018            goto meta_command_exit;
19019          }else{
19020            p->mxProgress = (int)integerValue(azArg[++i]);
19021          }
19022          continue;
19023        }
19024        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19025        rc = 1;
19026        goto meta_command_exit;
19027      }else{
19028        nn = (int)integerValue(z);
19029      }
19030    }
19031    open_db(p, 0);
19032    sqlite3_progress_handler(p->db, nn, progress_handler, p);
19033  }else
19034#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19035
19036  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19037    if( nArg >= 2) {
19038      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19039    }
19040    if( nArg >= 3) {
19041      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19042    }
19043  }else
19044
19045  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19046    rc = 2;
19047  }else
19048
19049  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19050    FILE *inSaved = p->in;
19051    int savedLineno = p->lineno;
19052    if( nArg!=2 ){
19053      raw_printf(stderr, "Usage: .read FILE\n");
19054      rc = 1;
19055      goto meta_command_exit;
19056    }
19057    if( azArg[1][0]=='|' ){
19058      p->in = popen(azArg[1]+1, "r");
19059      if( p->in==0 ){
19060        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19061        rc = 1;
19062      }else{
19063        rc = process_input(p);
19064        pclose(p->in);
19065      }
19066    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
19067      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
19068      rc = 1;
19069    }else{
19070      rc = process_input(p);
19071      fclose(p->in);
19072    }
19073    p->in = inSaved;
19074    p->lineno = savedLineno;
19075  }else
19076
19077  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
19078    const char *zSrcFile;
19079    const char *zDb;
19080    sqlite3 *pSrc;
19081    sqlite3_backup *pBackup;
19082    int nTimeout = 0;
19083
19084    if( nArg==2 ){
19085      zSrcFile = azArg[1];
19086      zDb = "main";
19087    }else if( nArg==3 ){
19088      zSrcFile = azArg[2];
19089      zDb = azArg[1];
19090    }else{
19091      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
19092      rc = 1;
19093      goto meta_command_exit;
19094    }
19095    rc = sqlite3_open(zSrcFile, &pSrc);
19096    if( rc!=SQLITE_OK ){
19097      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
19098      close_db(pSrc);
19099      return 1;
19100    }
19101    open_db(p, 0);
19102    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
19103    if( pBackup==0 ){
19104      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19105      close_db(pSrc);
19106      return 1;
19107    }
19108    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
19109          || rc==SQLITE_BUSY  ){
19110      if( rc==SQLITE_BUSY ){
19111        if( nTimeout++ >= 3 ) break;
19112        sqlite3_sleep(100);
19113      }
19114    }
19115    sqlite3_backup_finish(pBackup);
19116    if( rc==SQLITE_DONE ){
19117      rc = 0;
19118    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
19119      raw_printf(stderr, "Error: source database is busy\n");
19120      rc = 1;
19121    }else{
19122      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19123      rc = 1;
19124    }
19125    close_db(pSrc);
19126  }else
19127
19128  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
19129    if( nArg==2 ){
19130      p->scanstatsOn = (u8)booleanValue(azArg[1]);
19131#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19132      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
19133#endif
19134    }else{
19135      raw_printf(stderr, "Usage: .scanstats on|off\n");
19136      rc = 1;
19137    }
19138  }else
19139
19140  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
19141    ShellText sSelect;
19142    ShellState data;
19143    char *zErrMsg = 0;
19144    const char *zDiv = "(";
19145    const char *zName = 0;
19146    int iSchema = 0;
19147    int bDebug = 0;
19148    int bNoSystemTabs = 0;
19149    int ii;
19150
19151    open_db(p, 0);
19152    memcpy(&data, p, sizeof(data));
19153    data.showHeader = 0;
19154    data.cMode = data.mode = MODE_Semi;
19155    initText(&sSelect);
19156    for(ii=1; ii<nArg; ii++){
19157      if( optionMatch(azArg[ii],"indent") ){
19158        data.cMode = data.mode = MODE_Pretty;
19159      }else if( optionMatch(azArg[ii],"debug") ){
19160        bDebug = 1;
19161      }else if( optionMatch(azArg[ii],"nosys") ){
19162        bNoSystemTabs = 1;
19163      }else if( azArg[ii][0]=='-' ){
19164        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
19165        rc = 1;
19166        goto meta_command_exit;
19167      }else if( zName==0 ){
19168        zName = azArg[ii];
19169      }else{
19170        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
19171        rc = 1;
19172        goto meta_command_exit;
19173      }
19174    }
19175    if( zName!=0 ){
19176      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
19177                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
19178                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
19179                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
19180      if( isSchema ){
19181        char *new_argv[2], *new_colv[2];
19182        new_argv[0] = sqlite3_mprintf(
19183                      "CREATE TABLE %s (\n"
19184                      "  type text,\n"
19185                      "  name text,\n"
19186                      "  tbl_name text,\n"
19187                      "  rootpage integer,\n"
19188                      "  sql text\n"
19189                      ")", zName);
19190        new_argv[1] = 0;
19191        new_colv[0] = "sql";
19192        new_colv[1] = 0;
19193        callback(&data, 1, new_argv, new_colv);
19194        sqlite3_free(new_argv[0]);
19195      }
19196    }
19197    if( zDiv ){
19198      sqlite3_stmt *pStmt = 0;
19199      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
19200                              -1, &pStmt, 0);
19201      if( rc ){
19202        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19203        sqlite3_finalize(pStmt);
19204        rc = 1;
19205        goto meta_command_exit;
19206      }
19207      appendText(&sSelect, "SELECT sql FROM", 0);
19208      iSchema = 0;
19209      while( sqlite3_step(pStmt)==SQLITE_ROW ){
19210        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
19211        char zScNum[30];
19212        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
19213        appendText(&sSelect, zDiv, 0);
19214        zDiv = " UNION ALL ";
19215        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
19216        if( sqlite3_stricmp(zDb, "main")!=0 ){
19217          appendText(&sSelect, zDb, '\'');
19218        }else{
19219          appendText(&sSelect, "NULL", 0);
19220        }
19221        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
19222        appendText(&sSelect, zScNum, 0);
19223        appendText(&sSelect, " AS snum, ", 0);
19224        appendText(&sSelect, zDb, '\'');
19225        appendText(&sSelect, " AS sname FROM ", 0);
19226        appendText(&sSelect, zDb, quoteChar(zDb));
19227        appendText(&sSelect, ".sqlite_schema", 0);
19228      }
19229      sqlite3_finalize(pStmt);
19230#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
19231      if( zName ){
19232        appendText(&sSelect,
19233           " UNION ALL SELECT shell_module_schema(name),"
19234           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
19235        0);
19236      }
19237#endif
19238      appendText(&sSelect, ") WHERE ", 0);
19239      if( zName ){
19240        char *zQarg = sqlite3_mprintf("%Q", zName);
19241        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
19242                    strchr(zName, '[') != 0;
19243        if( strchr(zName, '.') ){
19244          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
19245        }else{
19246          appendText(&sSelect, "lower(tbl_name)", 0);
19247        }
19248        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
19249        appendText(&sSelect, zQarg, 0);
19250        if( !bGlob ){
19251          appendText(&sSelect, " ESCAPE '\\' ", 0);
19252        }
19253        appendText(&sSelect, " AND ", 0);
19254        sqlite3_free(zQarg);
19255      }
19256      if( bNoSystemTabs ){
19257        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
19258      }
19259      appendText(&sSelect, "sql IS NOT NULL"
19260                           " ORDER BY snum, rowid", 0);
19261      if( bDebug ){
19262        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
19263      }else{
19264        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
19265      }
19266      freeText(&sSelect);
19267    }
19268    if( zErrMsg ){
19269      utf8_printf(stderr,"Error: %s\n", zErrMsg);
19270      sqlite3_free(zErrMsg);
19271      rc = 1;
19272    }else if( rc != SQLITE_OK ){
19273      raw_printf(stderr,"Error: querying schema information\n");
19274      rc = 1;
19275    }else{
19276      rc = 0;
19277    }
19278  }else
19279
19280#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
19281  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
19282    sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
19283  }else
19284#endif
19285
19286#if defined(SQLITE_ENABLE_SESSION)
19287  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
19288    OpenSession *pSession = &p->aSession[0];
19289    char **azCmd = &azArg[1];
19290    int iSes = 0;
19291    int nCmd = nArg - 1;
19292    int i;
19293    if( nArg<=1 ) goto session_syntax_error;
19294    open_db(p, 0);
19295    if( nArg>=3 ){
19296      for(iSes=0; iSes<p->nSession; iSes++){
19297        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
19298      }
19299      if( iSes<p->nSession ){
19300        pSession = &p->aSession[iSes];
19301        azCmd++;
19302        nCmd--;
19303      }else{
19304        pSession = &p->aSession[0];
19305        iSes = 0;
19306      }
19307    }
19308
19309    /* .session attach TABLE
19310    ** Invoke the sqlite3session_attach() interface to attach a particular
19311    ** table so that it is never filtered.
19312    */
19313    if( strcmp(azCmd[0],"attach")==0 ){
19314      if( nCmd!=2 ) goto session_syntax_error;
19315      if( pSession->p==0 ){
19316        session_not_open:
19317        raw_printf(stderr, "ERROR: No sessions are open\n");
19318      }else{
19319        rc = sqlite3session_attach(pSession->p, azCmd[1]);
19320        if( rc ){
19321          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
19322          rc = 0;
19323        }
19324      }
19325    }else
19326
19327    /* .session changeset FILE
19328    ** .session patchset FILE
19329    ** Write a changeset or patchset into a file.  The file is overwritten.
19330    */
19331    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
19332      FILE *out = 0;
19333      if( nCmd!=2 ) goto session_syntax_error;
19334      if( pSession->p==0 ) goto session_not_open;
19335      out = fopen(azCmd[1], "wb");
19336      if( out==0 ){
19337        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
19338                    azCmd[1]);
19339      }else{
19340        int szChng;
19341        void *pChng;
19342        if( azCmd[0][0]=='c' ){
19343          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
19344        }else{
19345          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
19346        }
19347        if( rc ){
19348          printf("Error: error code %d\n", rc);
19349          rc = 0;
19350        }
19351        if( pChng
19352          && fwrite(pChng, szChng, 1, out)!=1 ){
19353          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
19354                  szChng);
19355        }
19356        sqlite3_free(pChng);
19357        fclose(out);
19358      }
19359    }else
19360
19361    /* .session close
19362    ** Close the identified session
19363    */
19364    if( strcmp(azCmd[0], "close")==0 ){
19365      if( nCmd!=1 ) goto session_syntax_error;
19366      if( p->nSession ){
19367        session_close(pSession);
19368        p->aSession[iSes] = p->aSession[--p->nSession];
19369      }
19370    }else
19371
19372    /* .session enable ?BOOLEAN?
19373    ** Query or set the enable flag
19374    */
19375    if( strcmp(azCmd[0], "enable")==0 ){
19376      int ii;
19377      if( nCmd>2 ) goto session_syntax_error;
19378      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19379      if( p->nSession ){
19380        ii = sqlite3session_enable(pSession->p, ii);
19381        utf8_printf(p->out, "session %s enable flag = %d\n",
19382                    pSession->zName, ii);
19383      }
19384    }else
19385
19386    /* .session filter GLOB ....
19387    ** Set a list of GLOB patterns of table names to be excluded.
19388    */
19389    if( strcmp(azCmd[0], "filter")==0 ){
19390      int ii, nByte;
19391      if( nCmd<2 ) goto session_syntax_error;
19392      if( p->nSession ){
19393        for(ii=0; ii<pSession->nFilter; ii++){
19394          sqlite3_free(pSession->azFilter[ii]);
19395        }
19396        sqlite3_free(pSession->azFilter);
19397        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
19398        pSession->azFilter = sqlite3_malloc( nByte );
19399        if( pSession->azFilter==0 ){
19400          raw_printf(stderr, "Error: out or memory\n");
19401          exit(1);
19402        }
19403        for(ii=1; ii<nCmd; ii++){
19404          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
19405        }
19406        pSession->nFilter = ii-1;
19407      }
19408    }else
19409
19410    /* .session indirect ?BOOLEAN?
19411    ** Query or set the indirect flag
19412    */
19413    if( strcmp(azCmd[0], "indirect")==0 ){
19414      int ii;
19415      if( nCmd>2 ) goto session_syntax_error;
19416      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19417      if( p->nSession ){
19418        ii = sqlite3session_indirect(pSession->p, ii);
19419        utf8_printf(p->out, "session %s indirect flag = %d\n",
19420                    pSession->zName, ii);
19421      }
19422    }else
19423
19424    /* .session isempty
19425    ** Determine if the session is empty
19426    */
19427    if( strcmp(azCmd[0], "isempty")==0 ){
19428      int ii;
19429      if( nCmd!=1 ) goto session_syntax_error;
19430      if( p->nSession ){
19431        ii = sqlite3session_isempty(pSession->p);
19432        utf8_printf(p->out, "session %s isempty flag = %d\n",
19433                    pSession->zName, ii);
19434      }
19435    }else
19436
19437    /* .session list
19438    ** List all currently open sessions
19439    */
19440    if( strcmp(azCmd[0],"list")==0 ){
19441      for(i=0; i<p->nSession; i++){
19442        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
19443      }
19444    }else
19445
19446    /* .session open DB NAME
19447    ** Open a new session called NAME on the attached database DB.
19448    ** DB is normally "main".
19449    */
19450    if( strcmp(azCmd[0],"open")==0 ){
19451      char *zName;
19452      if( nCmd!=3 ) goto session_syntax_error;
19453      zName = azCmd[2];
19454      if( zName[0]==0 ) goto session_syntax_error;
19455      for(i=0; i<p->nSession; i++){
19456        if( strcmp(p->aSession[i].zName,zName)==0 ){
19457          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
19458          goto meta_command_exit;
19459        }
19460      }
19461      if( p->nSession>=ArraySize(p->aSession) ){
19462        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
19463        goto meta_command_exit;
19464      }
19465      pSession = &p->aSession[p->nSession];
19466      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
19467      if( rc ){
19468        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
19469        rc = 0;
19470        goto meta_command_exit;
19471      }
19472      pSession->nFilter = 0;
19473      sqlite3session_table_filter(pSession->p, session_filter, pSession);
19474      p->nSession++;
19475      pSession->zName = sqlite3_mprintf("%s", zName);
19476    }else
19477    /* If no command name matches, show a syntax error */
19478    session_syntax_error:
19479    showHelp(p->out, "session");
19480  }else
19481#endif
19482
19483#ifdef SQLITE_DEBUG
19484  /* Undocumented commands for internal testing.  Subject to change
19485  ** without notice. */
19486  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
19487    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
19488      int i, v;
19489      for(i=1; i<nArg; i++){
19490        v = booleanValue(azArg[i]);
19491        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
19492      }
19493    }
19494    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
19495      int i; sqlite3_int64 v;
19496      for(i=1; i<nArg; i++){
19497        char zBuf[200];
19498        v = integerValue(azArg[i]);
19499        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
19500        utf8_printf(p->out, "%s", zBuf);
19501      }
19502    }
19503  }else
19504#endif
19505
19506  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
19507    int bIsInit = 0;         /* True to initialize the SELFTEST table */
19508    int bVerbose = 0;        /* Verbose output */
19509    int bSelftestExists;     /* True if SELFTEST already exists */
19510    int i, k;                /* Loop counters */
19511    int nTest = 0;           /* Number of tests runs */
19512    int nErr = 0;            /* Number of errors seen */
19513    ShellText str;           /* Answer for a query */
19514    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
19515
19516    open_db(p,0);
19517    for(i=1; i<nArg; i++){
19518      const char *z = azArg[i];
19519      if( z[0]=='-' && z[1]=='-' ) z++;
19520      if( strcmp(z,"-init")==0 ){
19521        bIsInit = 1;
19522      }else
19523      if( strcmp(z,"-v")==0 ){
19524        bVerbose++;
19525      }else
19526      {
19527        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19528                    azArg[i], azArg[0]);
19529        raw_printf(stderr, "Should be one of: --init -v\n");
19530        rc = 1;
19531        goto meta_command_exit;
19532      }
19533    }
19534    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
19535           != SQLITE_OK ){
19536      bSelftestExists = 0;
19537    }else{
19538      bSelftestExists = 1;
19539    }
19540    if( bIsInit ){
19541      createSelftestTable(p);
19542      bSelftestExists = 1;
19543    }
19544    initText(&str);
19545    appendText(&str, "x", 0);
19546    for(k=bSelftestExists; k>=0; k--){
19547      if( k==1 ){
19548        rc = sqlite3_prepare_v2(p->db,
19549            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
19550            -1, &pStmt, 0);
19551      }else{
19552        rc = sqlite3_prepare_v2(p->db,
19553          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
19554          "      (1,'run','PRAGMA integrity_check','ok')",
19555          -1, &pStmt, 0);
19556      }
19557      if( rc ){
19558        raw_printf(stderr, "Error querying the selftest table\n");
19559        rc = 1;
19560        sqlite3_finalize(pStmt);
19561        goto meta_command_exit;
19562      }
19563      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
19564        int tno = sqlite3_column_int(pStmt, 0);
19565        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
19566        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
19567        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
19568
19569        k = 0;
19570        if( bVerbose>0 ){
19571          char *zQuote = sqlite3_mprintf("%q", zSql);
19572          printf("%d: %s %s\n", tno, zOp, zSql);
19573          sqlite3_free(zQuote);
19574        }
19575        if( strcmp(zOp,"memo")==0 ){
19576          utf8_printf(p->out, "%s\n", zSql);
19577        }else
19578        if( strcmp(zOp,"run")==0 ){
19579          char *zErrMsg = 0;
19580          str.n = 0;
19581          str.z[0] = 0;
19582          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
19583          nTest++;
19584          if( bVerbose ){
19585            utf8_printf(p->out, "Result: %s\n", str.z);
19586          }
19587          if( rc || zErrMsg ){
19588            nErr++;
19589            rc = 1;
19590            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
19591            sqlite3_free(zErrMsg);
19592          }else if( strcmp(zAns,str.z)!=0 ){
19593            nErr++;
19594            rc = 1;
19595            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
19596            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
19597          }
19598        }else
19599        {
19600          utf8_printf(stderr,
19601            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
19602          rc = 1;
19603          break;
19604        }
19605      } /* End loop over rows of content from SELFTEST */
19606      sqlite3_finalize(pStmt);
19607    } /* End loop over k */
19608    freeText(&str);
19609    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
19610  }else
19611
19612  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
19613    if( nArg<2 || nArg>3 ){
19614      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
19615      rc = 1;
19616    }
19617    if( nArg>=2 ){
19618      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
19619                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
19620    }
19621    if( nArg>=3 ){
19622      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
19623                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
19624    }
19625  }else
19626
19627  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
19628    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
19629    int i;                   /* Loop counter */
19630    int bSchema = 0;         /* Also hash the schema */
19631    int bSeparate = 0;       /* Hash each table separately */
19632    int iSize = 224;         /* Hash algorithm to use */
19633    int bDebug = 0;          /* Only show the query that would have run */
19634    sqlite3_stmt *pStmt;     /* For querying tables names */
19635    char *zSql;              /* SQL to be run */
19636    char *zSep;              /* Separator */
19637    ShellText sSql;          /* Complete SQL for the query to run the hash */
19638    ShellText sQuery;        /* Set of queries used to read all content */
19639    open_db(p, 0);
19640    for(i=1; i<nArg; i++){
19641      const char *z = azArg[i];
19642      if( z[0]=='-' ){
19643        z++;
19644        if( z[0]=='-' ) z++;
19645        if( strcmp(z,"schema")==0 ){
19646          bSchema = 1;
19647        }else
19648        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
19649         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
19650        ){
19651          iSize = atoi(&z[5]);
19652        }else
19653        if( strcmp(z,"debug")==0 ){
19654          bDebug = 1;
19655        }else
19656        {
19657          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19658                      azArg[i], azArg[0]);
19659          showHelp(p->out, azArg[0]);
19660          rc = 1;
19661          goto meta_command_exit;
19662        }
19663      }else if( zLike ){
19664        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
19665        rc = 1;
19666        goto meta_command_exit;
19667      }else{
19668        zLike = z;
19669        bSeparate = 1;
19670        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
19671      }
19672    }
19673    if( bSchema ){
19674      zSql = "SELECT lower(name) FROM sqlite_schema"
19675             " WHERE type='table' AND coalesce(rootpage,0)>1"
19676             " UNION ALL SELECT 'sqlite_schema'"
19677             " ORDER BY 1 collate nocase";
19678    }else{
19679      zSql = "SELECT lower(name) FROM sqlite_schema"
19680             " WHERE type='table' AND coalesce(rootpage,0)>1"
19681             " AND name NOT LIKE 'sqlite_%'"
19682             " ORDER BY 1 collate nocase";
19683    }
19684    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19685    initText(&sQuery);
19686    initText(&sSql);
19687    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
19688    zSep = "VALUES(";
19689    while( SQLITE_ROW==sqlite3_step(pStmt) ){
19690      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
19691      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
19692      if( strncmp(zTab, "sqlite_",7)!=0 ){
19693        appendText(&sQuery,"SELECT * FROM ", 0);
19694        appendText(&sQuery,zTab,'"');
19695        appendText(&sQuery," NOT INDEXED;", 0);
19696      }else if( strcmp(zTab, "sqlite_schema")==0 ){
19697        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
19698                           " ORDER BY name;", 0);
19699      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
19700        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
19701                           " ORDER BY name;", 0);
19702      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
19703        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
19704                           " ORDER BY tbl,idx;", 0);
19705      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
19706        appendText(&sQuery, "SELECT * FROM ", 0);
19707        appendText(&sQuery, zTab, 0);
19708        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
19709      }
19710      appendText(&sSql, zSep, 0);
19711      appendText(&sSql, sQuery.z, '\'');
19712      sQuery.n = 0;
19713      appendText(&sSql, ",", 0);
19714      appendText(&sSql, zTab, '\'');
19715      zSep = "),(";
19716    }
19717    sqlite3_finalize(pStmt);
19718    if( bSeparate ){
19719      zSql = sqlite3_mprintf(
19720          "%s))"
19721          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
19722          "   FROM [sha3sum$query]",
19723          sSql.z, iSize);
19724    }else{
19725      zSql = sqlite3_mprintf(
19726          "%s))"
19727          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
19728          "   FROM [sha3sum$query]",
19729          sSql.z, iSize);
19730    }
19731    freeText(&sQuery);
19732    freeText(&sSql);
19733    if( bDebug ){
19734      utf8_printf(p->out, "%s\n", zSql);
19735    }else{
19736      shell_exec(p, zSql, 0);
19737    }
19738    sqlite3_free(zSql);
19739  }else
19740
19741#ifndef SQLITE_NOHAVE_SYSTEM
19742  if( c=='s'
19743   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
19744  ){
19745    char *zCmd;
19746    int i, x;
19747    if( nArg<2 ){
19748      raw_printf(stderr, "Usage: .system COMMAND\n");
19749      rc = 1;
19750      goto meta_command_exit;
19751    }
19752    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
19753    for(i=2; i<nArg; i++){
19754      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
19755                             zCmd, azArg[i]);
19756    }
19757    x = system(zCmd);
19758    sqlite3_free(zCmd);
19759    if( x ) raw_printf(stderr, "System command returns %d\n", x);
19760  }else
19761#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
19762
19763  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
19764    static const char *azBool[] = { "off", "on", "trigger", "full"};
19765    int i;
19766    if( nArg!=1 ){
19767      raw_printf(stderr, "Usage: .show\n");
19768      rc = 1;
19769      goto meta_command_exit;
19770    }
19771    utf8_printf(p->out, "%12.12s: %s\n","echo",
19772                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
19773    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
19774    utf8_printf(p->out, "%12.12s: %s\n","explain",
19775         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
19776    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
19777    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
19778    utf8_printf(p->out, "%12.12s: ", "nullvalue");
19779      output_c_string(p->out, p->nullValue);
19780      raw_printf(p->out, "\n");
19781    utf8_printf(p->out,"%12.12s: %s\n","output",
19782            strlen30(p->outfile) ? p->outfile : "stdout");
19783    utf8_printf(p->out,"%12.12s: ", "colseparator");
19784      output_c_string(p->out, p->colSeparator);
19785      raw_printf(p->out, "\n");
19786    utf8_printf(p->out,"%12.12s: ", "rowseparator");
19787      output_c_string(p->out, p->rowSeparator);
19788      raw_printf(p->out, "\n");
19789    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
19790    utf8_printf(p->out, "%12.12s: ", "width");
19791    for (i=0;i<p->nWidth;i++) {
19792      raw_printf(p->out, "%d ", p->colWidth[i]);
19793    }
19794    raw_printf(p->out, "\n");
19795    utf8_printf(p->out, "%12.12s: %s\n", "filename",
19796                p->zDbFilename ? p->zDbFilename : "");
19797  }else
19798
19799  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
19800    if( nArg==2 ){
19801      p->statsOn = (u8)booleanValue(azArg[1]);
19802    }else if( nArg==1 ){
19803      display_stats(p->db, p, 0);
19804    }else{
19805      raw_printf(stderr, "Usage: .stats ?on|off?\n");
19806      rc = 1;
19807    }
19808  }else
19809
19810  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
19811   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
19812                 || strncmp(azArg[0], "indexes", n)==0) )
19813  ){
19814    sqlite3_stmt *pStmt;
19815    char **azResult;
19816    int nRow, nAlloc;
19817    int ii;
19818    ShellText s;
19819    initText(&s);
19820    open_db(p, 0);
19821    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19822    if( rc ){
19823      sqlite3_finalize(pStmt);
19824      return shellDatabaseError(p->db);
19825    }
19826
19827    if( nArg>2 && c=='i' ){
19828      /* It is an historical accident that the .indexes command shows an error
19829      ** when called with the wrong number of arguments whereas the .tables
19830      ** command does not. */
19831      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
19832      rc = 1;
19833      sqlite3_finalize(pStmt);
19834      goto meta_command_exit;
19835    }
19836    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
19837      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
19838      if( zDbName==0 ) continue;
19839      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
19840      if( sqlite3_stricmp(zDbName, "main")==0 ){
19841        appendText(&s, "SELECT name FROM ", 0);
19842      }else{
19843        appendText(&s, "SELECT ", 0);
19844        appendText(&s, zDbName, '\'');
19845        appendText(&s, "||'.'||name FROM ", 0);
19846      }
19847      appendText(&s, zDbName, '"');
19848      appendText(&s, ".sqlite_schema ", 0);
19849      if( c=='t' ){
19850        appendText(&s," WHERE type IN ('table','view')"
19851                      "   AND name NOT LIKE 'sqlite_%'"
19852                      "   AND name LIKE ?1", 0);
19853      }else{
19854        appendText(&s," WHERE type='index'"
19855                      "   AND tbl_name LIKE ?1", 0);
19856      }
19857    }
19858    rc = sqlite3_finalize(pStmt);
19859    appendText(&s, " ORDER BY 1", 0);
19860    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
19861    freeText(&s);
19862    if( rc ) return shellDatabaseError(p->db);
19863
19864    /* Run the SQL statement prepared by the above block. Store the results
19865    ** as an array of nul-terminated strings in azResult[].  */
19866    nRow = nAlloc = 0;
19867    azResult = 0;
19868    if( nArg>1 ){
19869      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
19870    }else{
19871      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
19872    }
19873    while( sqlite3_step(pStmt)==SQLITE_ROW ){
19874      if( nRow>=nAlloc ){
19875        char **azNew;
19876        int n2 = nAlloc*2 + 10;
19877        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
19878        if( azNew==0 ) shell_out_of_memory();
19879        nAlloc = n2;
19880        azResult = azNew;
19881      }
19882      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
19883      if( 0==azResult[nRow] ) shell_out_of_memory();
19884      nRow++;
19885    }
19886    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
19887      rc = shellDatabaseError(p->db);
19888    }
19889
19890    /* Pretty-print the contents of array azResult[] to the output */
19891    if( rc==0 && nRow>0 ){
19892      int len, maxlen = 0;
19893      int i, j;
19894      int nPrintCol, nPrintRow;
19895      for(i=0; i<nRow; i++){
19896        len = strlen30(azResult[i]);
19897        if( len>maxlen ) maxlen = len;
19898      }
19899      nPrintCol = 80/(maxlen+2);
19900      if( nPrintCol<1 ) nPrintCol = 1;
19901      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
19902      for(i=0; i<nPrintRow; i++){
19903        for(j=i; j<nRow; j+=nPrintRow){
19904          char *zSp = j<nPrintRow ? "" : "  ";
19905          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
19906                      azResult[j] ? azResult[j]:"");
19907        }
19908        raw_printf(p->out, "\n");
19909      }
19910    }
19911
19912    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
19913    sqlite3_free(azResult);
19914  }else
19915
19916  /* Begin redirecting output to the file "testcase-out.txt" */
19917  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
19918    output_reset(p);
19919    p->out = output_file_open("testcase-out.txt", 0);
19920    if( p->out==0 ){
19921      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
19922    }
19923    if( nArg>=2 ){
19924      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
19925    }else{
19926      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
19927    }
19928  }else
19929
19930#ifndef SQLITE_UNTESTABLE
19931  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
19932    static const struct {
19933       const char *zCtrlName;   /* Name of a test-control option */
19934       int ctrlCode;            /* Integer code for that option */
19935       const char *zUsage;      /* Usage notes */
19936    } aCtrl[] = {
19937      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
19938      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
19939    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
19940    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
19941      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
19942      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
19943    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
19944      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
19945      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
19946      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
19947      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
19948      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
19949#ifdef YYCOVERAGE
19950      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
19951#endif
19952      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
19953      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
19954      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
19955      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
19956      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
19957    };
19958    int testctrl = -1;
19959    int iCtrl = -1;
19960    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
19961    int isOk = 0;
19962    int i, n2;
19963    const char *zCmd = 0;
19964
19965    open_db(p, 0);
19966    zCmd = nArg>=2 ? azArg[1] : "help";
19967
19968    /* The argument can optionally begin with "-" or "--" */
19969    if( zCmd[0]=='-' && zCmd[1] ){
19970      zCmd++;
19971      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19972    }
19973
19974    /* --help lists all test-controls */
19975    if( strcmp(zCmd,"help")==0 ){
19976      utf8_printf(p->out, "Available test-controls:\n");
19977      for(i=0; i<ArraySize(aCtrl); i++){
19978        utf8_printf(p->out, "  .testctrl %s %s\n",
19979                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19980      }
19981      rc = 1;
19982      goto meta_command_exit;
19983    }
19984
19985    /* convert testctrl text option to value. allow any unique prefix
19986    ** of the option name, or a numerical value. */
19987    n2 = strlen30(zCmd);
19988    for(i=0; i<ArraySize(aCtrl); i++){
19989      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19990        if( testctrl<0 ){
19991          testctrl = aCtrl[i].ctrlCode;
19992          iCtrl = i;
19993        }else{
19994          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
19995                              "Use \".testctrl --help\" for help\n", zCmd);
19996          rc = 1;
19997          goto meta_command_exit;
19998        }
19999      }
20000    }
20001    if( testctrl<0 ){
20002      utf8_printf(stderr,"Error: unknown test-control: %s\n"
20003                         "Use \".testctrl --help\" for help\n", zCmd);
20004    }else{
20005      switch(testctrl){
20006
20007        /* sqlite3_test_control(int, db, int) */
20008        case SQLITE_TESTCTRL_OPTIMIZATIONS:
20009          if( nArg==3 ){
20010            int opt = (int)strtol(azArg[2], 0, 0);
20011            rc2 = sqlite3_test_control(testctrl, p->db, opt);
20012            isOk = 3;
20013          }
20014          break;
20015
20016        /* sqlite3_test_control(int) */
20017        case SQLITE_TESTCTRL_PRNG_SAVE:
20018        case SQLITE_TESTCTRL_PRNG_RESTORE:
20019        case SQLITE_TESTCTRL_BYTEORDER:
20020          if( nArg==2 ){
20021            rc2 = sqlite3_test_control(testctrl);
20022            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20023          }
20024          break;
20025
20026        /* sqlite3_test_control(int, uint) */
20027        case SQLITE_TESTCTRL_PENDING_BYTE:
20028          if( nArg==3 ){
20029            unsigned int opt = (unsigned int)integerValue(azArg[2]);
20030            rc2 = sqlite3_test_control(testctrl, opt);
20031            isOk = 3;
20032          }
20033          break;
20034
20035        /* sqlite3_test_control(int, int, sqlite3*) */
20036        case SQLITE_TESTCTRL_PRNG_SEED:
20037          if( nArg==3 || nArg==4 ){
20038            int ii = (int)integerValue(azArg[2]);
20039            sqlite3 *db;
20040            if( ii==0 && strcmp(azArg[2],"random")==0 ){
20041              sqlite3_randomness(sizeof(ii),&ii);
20042              printf("-- random seed: %d\n", ii);
20043            }
20044            if( nArg==3 ){
20045              db = 0;
20046            }else{
20047              db = p->db;
20048              /* Make sure the schema has been loaded */
20049              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
20050            }
20051            rc2 = sqlite3_test_control(testctrl, ii, db);
20052            isOk = 3;
20053          }
20054          break;
20055
20056        /* sqlite3_test_control(int, int) */
20057        case SQLITE_TESTCTRL_ASSERT:
20058        case SQLITE_TESTCTRL_ALWAYS:
20059          if( nArg==3 ){
20060            int opt = booleanValue(azArg[2]);
20061            rc2 = sqlite3_test_control(testctrl, opt);
20062            isOk = 1;
20063          }
20064          break;
20065
20066        /* sqlite3_test_control(int, int) */
20067        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
20068        case SQLITE_TESTCTRL_NEVER_CORRUPT:
20069          if( nArg==3 ){
20070            int opt = booleanValue(azArg[2]);
20071            rc2 = sqlite3_test_control(testctrl, opt);
20072            isOk = 3;
20073          }
20074          break;
20075
20076        /* sqlite3_test_control(sqlite3*) */
20077        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
20078          rc2 = sqlite3_test_control(testctrl, p->db);
20079          isOk = 3;
20080          break;
20081
20082        case SQLITE_TESTCTRL_IMPOSTER:
20083          if( nArg==5 ){
20084            rc2 = sqlite3_test_control(testctrl, p->db,
20085                          azArg[2],
20086                          integerValue(azArg[3]),
20087                          integerValue(azArg[4]));
20088            isOk = 3;
20089          }
20090          break;
20091
20092        case SQLITE_TESTCTRL_SEEK_COUNT: {
20093          u64 x = 0;
20094          rc2 = sqlite3_test_control(testctrl, p->db, &x);
20095          utf8_printf(p->out, "%llu\n", x);
20096          isOk = 3;
20097          break;
20098        }
20099
20100#ifdef YYCOVERAGE
20101        case SQLITE_TESTCTRL_PARSER_COVERAGE:
20102          if( nArg==2 ){
20103            sqlite3_test_control(testctrl, p->out);
20104            isOk = 3;
20105          }
20106#endif
20107      }
20108    }
20109    if( isOk==0 && iCtrl>=0 ){
20110      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
20111      rc = 1;
20112    }else if( isOk==1 ){
20113      raw_printf(p->out, "%d\n", rc2);
20114    }else if( isOk==2 ){
20115      raw_printf(p->out, "0x%08x\n", rc2);
20116    }
20117  }else
20118#endif /* !defined(SQLITE_UNTESTABLE) */
20119
20120  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
20121    open_db(p, 0);
20122    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
20123  }else
20124
20125  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
20126    if( nArg==2 ){
20127      enableTimer = booleanValue(azArg[1]);
20128      if( enableTimer && !HAS_TIMER ){
20129        raw_printf(stderr, "Error: timer not available on this system.\n");
20130        enableTimer = 0;
20131      }
20132    }else{
20133      raw_printf(stderr, "Usage: .timer on|off\n");
20134      rc = 1;
20135    }
20136  }else
20137
20138#ifndef SQLITE_OMIT_TRACE
20139  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
20140    int mType = 0;
20141    int jj;
20142    open_db(p, 0);
20143    for(jj=1; jj<nArg; jj++){
20144      const char *z = azArg[jj];
20145      if( z[0]=='-' ){
20146        if( optionMatch(z, "expanded") ){
20147          p->eTraceType = SHELL_TRACE_EXPANDED;
20148        }
20149#ifdef SQLITE_ENABLE_NORMALIZE
20150        else if( optionMatch(z, "normalized") ){
20151          p->eTraceType = SHELL_TRACE_NORMALIZED;
20152        }
20153#endif
20154        else if( optionMatch(z, "plain") ){
20155          p->eTraceType = SHELL_TRACE_PLAIN;
20156        }
20157        else if( optionMatch(z, "profile") ){
20158          mType |= SQLITE_TRACE_PROFILE;
20159        }
20160        else if( optionMatch(z, "row") ){
20161          mType |= SQLITE_TRACE_ROW;
20162        }
20163        else if( optionMatch(z, "stmt") ){
20164          mType |= SQLITE_TRACE_STMT;
20165        }
20166        else if( optionMatch(z, "close") ){
20167          mType |= SQLITE_TRACE_CLOSE;
20168        }
20169        else {
20170          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
20171          rc = 1;
20172          goto meta_command_exit;
20173        }
20174      }else{
20175        output_file_close(p->traceOut);
20176        p->traceOut = output_file_open(azArg[1], 0);
20177      }
20178    }
20179    if( p->traceOut==0 ){
20180      sqlite3_trace_v2(p->db, 0, 0, 0);
20181    }else{
20182      if( mType==0 ) mType = SQLITE_TRACE_STMT;
20183      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
20184    }
20185  }else
20186#endif /* !defined(SQLITE_OMIT_TRACE) */
20187
20188#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20189  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
20190    int ii;
20191    int lenOpt;
20192    char *zOpt;
20193    if( nArg<2 ){
20194      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
20195      rc = 1;
20196      goto meta_command_exit;
20197    }
20198    open_db(p, 0);
20199    zOpt = azArg[1];
20200    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
20201    lenOpt = (int)strlen(zOpt);
20202    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
20203      assert( azArg[nArg]==0 );
20204      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
20205    }else{
20206      for(ii=1; ii<nArg; ii++){
20207        sqlite3_create_module(p->db, azArg[ii], 0, 0);
20208      }
20209    }
20210  }else
20211#endif
20212
20213#if SQLITE_USER_AUTHENTICATION
20214  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
20215    if( nArg<2 ){
20216      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
20217      rc = 1;
20218      goto meta_command_exit;
20219    }
20220    open_db(p, 0);
20221    if( strcmp(azArg[1],"login")==0 ){
20222      if( nArg!=4 ){
20223        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
20224        rc = 1;
20225        goto meta_command_exit;
20226      }
20227      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
20228                                     strlen30(azArg[3]));
20229      if( rc ){
20230        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
20231        rc = 1;
20232      }
20233    }else if( strcmp(azArg[1],"add")==0 ){
20234      if( nArg!=5 ){
20235        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
20236        rc = 1;
20237        goto meta_command_exit;
20238      }
20239      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20240                            booleanValue(azArg[4]));
20241      if( rc ){
20242        raw_printf(stderr, "User-Add failed: %d\n", rc);
20243        rc = 1;
20244      }
20245    }else if( strcmp(azArg[1],"edit")==0 ){
20246      if( nArg!=5 ){
20247        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
20248        rc = 1;
20249        goto meta_command_exit;
20250      }
20251      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20252                              booleanValue(azArg[4]));
20253      if( rc ){
20254        raw_printf(stderr, "User-Edit failed: %d\n", rc);
20255        rc = 1;
20256      }
20257    }else if( strcmp(azArg[1],"delete")==0 ){
20258      if( nArg!=3 ){
20259        raw_printf(stderr, "Usage: .user delete USER\n");
20260        rc = 1;
20261        goto meta_command_exit;
20262      }
20263      rc = sqlite3_user_delete(p->db, azArg[2]);
20264      if( rc ){
20265        raw_printf(stderr, "User-Delete failed: %d\n", rc);
20266        rc = 1;
20267      }
20268    }else{
20269      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
20270      rc = 1;
20271      goto meta_command_exit;
20272    }
20273  }else
20274#endif /* SQLITE_USER_AUTHENTICATION */
20275
20276  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
20277    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
20278        sqlite3_libversion(), sqlite3_sourceid());
20279#if SQLITE_HAVE_ZLIB
20280    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
20281#endif
20282#define CTIMEOPT_VAL_(opt) #opt
20283#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20284#if defined(__clang__) && defined(__clang_major__)
20285    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
20286                    CTIMEOPT_VAL(__clang_minor__) "."
20287                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
20288#elif defined(_MSC_VER)
20289    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
20290#elif defined(__GNUC__) && defined(__VERSION__)
20291    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
20292#endif
20293  }else
20294
20295  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
20296    const char *zDbName = nArg==2 ? azArg[1] : "main";
20297    sqlite3_vfs *pVfs = 0;
20298    if( p->db ){
20299      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
20300      if( pVfs ){
20301        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
20302        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
20303        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
20304        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20305      }
20306    }
20307  }else
20308
20309  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
20310    sqlite3_vfs *pVfs;
20311    sqlite3_vfs *pCurrent = 0;
20312    if( p->db ){
20313      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
20314    }
20315    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
20316      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
20317           pVfs==pCurrent ? "  <--- CURRENT" : "");
20318      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
20319      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
20320      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20321      if( pVfs->pNext ){
20322        raw_printf(p->out, "-----------------------------------\n");
20323      }
20324    }
20325  }else
20326
20327  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
20328    const char *zDbName = nArg==2 ? azArg[1] : "main";
20329    char *zVfsName = 0;
20330    if( p->db ){
20331      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
20332      if( zVfsName ){
20333        utf8_printf(p->out, "%s\n", zVfsName);
20334        sqlite3_free(zVfsName);
20335      }
20336    }
20337  }else
20338
20339#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
20340  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
20341    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
20342  }else
20343#endif
20344
20345  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
20346    int j;
20347    assert( nArg<=ArraySize(azArg) );
20348    p->nWidth = nArg-1;
20349    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
20350    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
20351    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
20352    for(j=1; j<nArg; j++){
20353      p->colWidth[j-1] = (int)integerValue(azArg[j]);
20354    }
20355  }else
20356
20357  {
20358    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
20359      " \"%s\". Enter \".help\" for help\n", azArg[0]);
20360    rc = 1;
20361  }
20362
20363meta_command_exit:
20364  if( p->outCount ){
20365    p->outCount--;
20366    if( p->outCount==0 ) output_reset(p);
20367  }
20368  return rc;
20369}
20370
20371/*
20372** Return TRUE if a semicolon occurs anywhere in the first N characters
20373** of string z[].
20374*/
20375static int line_contains_semicolon(const char *z, int N){
20376  int i;
20377  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
20378  return 0;
20379}
20380
20381/*
20382** Test to see if a line consists entirely of whitespace.
20383*/
20384static int _all_whitespace(const char *z){
20385  for(; *z; z++){
20386    if( IsSpace(z[0]) ) continue;
20387    if( *z=='/' && z[1]=='*' ){
20388      z += 2;
20389      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
20390      if( *z==0 ) return 0;
20391      z++;
20392      continue;
20393    }
20394    if( *z=='-' && z[1]=='-' ){
20395      z += 2;
20396      while( *z && *z!='\n' ){ z++; }
20397      if( *z==0 ) return 1;
20398      continue;
20399    }
20400    return 0;
20401  }
20402  return 1;
20403}
20404
20405/*
20406** Return TRUE if the line typed in is an SQL command terminator other
20407** than a semi-colon.  The SQL Server style "go" command is understood
20408** as is the Oracle "/".
20409*/
20410static int line_is_command_terminator(const char *zLine){
20411  while( IsSpace(zLine[0]) ){ zLine++; };
20412  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
20413    return 1;  /* Oracle */
20414  }
20415  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
20416         && _all_whitespace(&zLine[2]) ){
20417    return 1;  /* SQL Server */
20418  }
20419  return 0;
20420}
20421
20422/*
20423** We need a default sqlite3_complete() implementation to use in case
20424** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
20425** any arbitrary text is a complete SQL statement.  This is not very
20426** user-friendly, but it does seem to work.
20427*/
20428#ifdef SQLITE_OMIT_COMPLETE
20429#define sqlite3_complete(x) 1
20430#endif
20431
20432/*
20433** Return true if zSql is a complete SQL statement.  Return false if it
20434** ends in the middle of a string literal or C-style comment.
20435*/
20436static int line_is_complete(char *zSql, int nSql){
20437  int rc;
20438  if( zSql==0 ) return 1;
20439  zSql[nSql] = ';';
20440  zSql[nSql+1] = 0;
20441  rc = sqlite3_complete(zSql);
20442  zSql[nSql] = 0;
20443  return rc;
20444}
20445
20446/*
20447** Run a single line of SQL.  Return the number of errors.
20448*/
20449static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
20450  int rc;
20451  char *zErrMsg = 0;
20452
20453  open_db(p, 0);
20454  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
20455  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
20456  BEGIN_TIMER;
20457  rc = shell_exec(p, zSql, &zErrMsg);
20458  END_TIMER;
20459  if( rc || zErrMsg ){
20460    char zPrefix[100];
20461    if( in!=0 || !stdin_is_interactive ){
20462      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
20463                       "Error: near line %d:", startline);
20464    }else{
20465      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
20466    }
20467    if( zErrMsg!=0 ){
20468      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
20469      sqlite3_free(zErrMsg);
20470      zErrMsg = 0;
20471    }else{
20472      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
20473    }
20474    return 1;
20475  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
20476    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
20477            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
20478  }
20479  return 0;
20480}
20481
20482
20483/*
20484** Read input from *in and process it.  If *in==0 then input
20485** is interactive - the user is typing it it.  Otherwise, input
20486** is coming from a file or device.  A prompt is issued and history
20487** is saved only if input is interactive.  An interrupt signal will
20488** cause this routine to exit immediately, unless input is interactive.
20489**
20490** Return the number of errors.
20491*/
20492static int process_input(ShellState *p){
20493  char *zLine = 0;          /* A single input line */
20494  char *zSql = 0;           /* Accumulated SQL text */
20495  int nLine;                /* Length of current line */
20496  int nSql = 0;             /* Bytes of zSql[] used */
20497  int nAlloc = 0;           /* Allocated zSql[] space */
20498  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
20499  int rc;                   /* Error code */
20500  int errCnt = 0;           /* Number of errors seen */
20501  int startline = 0;        /* Line number for start of current input */
20502
20503  p->lineno = 0;
20504  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
20505    fflush(p->out);
20506    zLine = one_input_line(p->in, zLine, nSql>0);
20507    if( zLine==0 ){
20508      /* End of input */
20509      if( p->in==0 && stdin_is_interactive ) printf("\n");
20510      break;
20511    }
20512    if( seenInterrupt ){
20513      if( p->in!=0 ) break;
20514      seenInterrupt = 0;
20515    }
20516    p->lineno++;
20517    if( nSql==0 && _all_whitespace(zLine) ){
20518      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20519      continue;
20520    }
20521    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
20522      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20523      if( zLine[0]=='.' ){
20524        rc = do_meta_command(zLine, p);
20525        if( rc==2 ){ /* exit requested */
20526          break;
20527        }else if( rc ){
20528          errCnt++;
20529        }
20530      }
20531      continue;
20532    }
20533    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
20534      memcpy(zLine,";",2);
20535    }
20536    nLine = strlen30(zLine);
20537    if( nSql+nLine+2>=nAlloc ){
20538      nAlloc = nSql+nLine+100;
20539      zSql = realloc(zSql, nAlloc);
20540      if( zSql==0 ) shell_out_of_memory();
20541    }
20542    nSqlPrior = nSql;
20543    if( nSql==0 ){
20544      int i;
20545      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
20546      assert( nAlloc>0 && zSql!=0 );
20547      memcpy(zSql, zLine+i, nLine+1-i);
20548      startline = p->lineno;
20549      nSql = nLine-i;
20550    }else{
20551      zSql[nSql++] = '\n';
20552      memcpy(zSql+nSql, zLine, nLine+1);
20553      nSql += nLine;
20554    }
20555    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
20556                && sqlite3_complete(zSql) ){
20557      errCnt += runOneSqlLine(p, zSql, p->in, startline);
20558      nSql = 0;
20559      if( p->outCount ){
20560        output_reset(p);
20561        p->outCount = 0;
20562      }else{
20563        clearTempFile(p);
20564      }
20565    }else if( nSql && _all_whitespace(zSql) ){
20566      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
20567      nSql = 0;
20568    }
20569  }
20570  if( nSql && !_all_whitespace(zSql) ){
20571    errCnt += runOneSqlLine(p, zSql, p->in, startline);
20572  }
20573  free(zSql);
20574  free(zLine);
20575  return errCnt>0;
20576}
20577
20578/*
20579** Return a pathname which is the user's home directory.  A
20580** 0 return indicates an error of some kind.
20581*/
20582static char *find_home_dir(int clearFlag){
20583  static char *home_dir = NULL;
20584  if( clearFlag ){
20585    free(home_dir);
20586    home_dir = 0;
20587    return 0;
20588  }
20589  if( home_dir ) return home_dir;
20590
20591#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
20592     && !defined(__RTP__) && !defined(_WRS_KERNEL)
20593  {
20594    struct passwd *pwent;
20595    uid_t uid = getuid();
20596    if( (pwent=getpwuid(uid)) != NULL) {
20597      home_dir = pwent->pw_dir;
20598    }
20599  }
20600#endif
20601
20602#if defined(_WIN32_WCE)
20603  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
20604   */
20605  home_dir = "/";
20606#else
20607
20608#if defined(_WIN32) || defined(WIN32)
20609  if (!home_dir) {
20610    home_dir = getenv("USERPROFILE");
20611  }
20612#endif
20613
20614  if (!home_dir) {
20615    home_dir = getenv("HOME");
20616  }
20617
20618#if defined(_WIN32) || defined(WIN32)
20619  if (!home_dir) {
20620    char *zDrive, *zPath;
20621    int n;
20622    zDrive = getenv("HOMEDRIVE");
20623    zPath = getenv("HOMEPATH");
20624    if( zDrive && zPath ){
20625      n = strlen30(zDrive) + strlen30(zPath) + 1;
20626      home_dir = malloc( n );
20627      if( home_dir==0 ) return 0;
20628      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
20629      return home_dir;
20630    }
20631    home_dir = "c:\\";
20632  }
20633#endif
20634
20635#endif /* !_WIN32_WCE */
20636
20637  if( home_dir ){
20638    int n = strlen30(home_dir) + 1;
20639    char *z = malloc( n );
20640    if( z ) memcpy(z, home_dir, n);
20641    home_dir = z;
20642  }
20643
20644  return home_dir;
20645}
20646
20647/*
20648** Read input from the file given by sqliterc_override.  Or if that
20649** parameter is NULL, take input from ~/.sqliterc
20650**
20651** Returns the number of errors.
20652*/
20653static void process_sqliterc(
20654  ShellState *p,                  /* Configuration data */
20655  const char *sqliterc_override   /* Name of config file. NULL to use default */
20656){
20657  char *home_dir = NULL;
20658  const char *sqliterc = sqliterc_override;
20659  char *zBuf = 0;
20660  FILE *inSaved = p->in;
20661  int savedLineno = p->lineno;
20662
20663  if (sqliterc == NULL) {
20664    home_dir = find_home_dir(0);
20665    if( home_dir==0 ){
20666      raw_printf(stderr, "-- warning: cannot find home directory;"
20667                      " cannot read ~/.sqliterc\n");
20668      return;
20669    }
20670    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
20671    sqliterc = zBuf;
20672  }
20673  p->in = fopen(sqliterc,"rb");
20674  if( p->in ){
20675    if( stdin_is_interactive ){
20676      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
20677    }
20678    if( process_input(p) && bail_on_error ) exit(1);
20679    fclose(p->in);
20680  }else if( sqliterc_override!=0 ){
20681    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
20682    if( bail_on_error ) exit(1);
20683  }
20684  p->in = inSaved;
20685  p->lineno = savedLineno;
20686  sqlite3_free(zBuf);
20687}
20688
20689/*
20690** Show available command line options
20691*/
20692static const char zOptions[] =
20693#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20694  "   -A ARGS...           run \".archive ARGS\" and exit\n"
20695#endif
20696  "   -append              append the database to the end of the file\n"
20697  "   -ascii               set output mode to 'ascii'\n"
20698  "   -bail                stop after hitting an error\n"
20699  "   -batch               force batch I/O\n"
20700  "   -box                 set output mode to 'box'\n"
20701  "   -column              set output mode to 'column'\n"
20702  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
20703  "   -csv                 set output mode to 'csv'\n"
20704#if defined(SQLITE_ENABLE_DESERIALIZE)
20705  "   -deserialize         open the database using sqlite3_deserialize()\n"
20706#endif
20707  "   -echo                print commands before execution\n"
20708  "   -init FILENAME       read/process named file\n"
20709  "   -[no]header          turn headers on or off\n"
20710#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20711  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
20712#endif
20713  "   -help                show this message\n"
20714  "   -html                set output mode to HTML\n"
20715  "   -interactive         force interactive I/O\n"
20716  "   -json                set output mode to 'json'\n"
20717  "   -line                set output mode to 'line'\n"
20718  "   -list                set output mode to 'list'\n"
20719  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
20720  "   -markdown            set output mode to 'markdown'\n"
20721#if defined(SQLITE_ENABLE_DESERIALIZE)
20722  "   -maxsize N           maximum size for a --deserialize database\n"
20723#endif
20724  "   -memtrace            trace all memory allocations and deallocations\n"
20725  "   -mmap N              default mmap size set to N\n"
20726#ifdef SQLITE_ENABLE_MULTIPLEX
20727  "   -multiplex           enable the multiplexor VFS\n"
20728#endif
20729  "   -newline SEP         set output row separator. Default: '\\n'\n"
20730  "   -nofollow            refuse to open symbolic links to database files\n"
20731  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
20732  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
20733  "   -quote               set output mode to 'quote'\n"
20734  "   -readonly            open the database read-only\n"
20735  "   -separator SEP       set output column separator. Default: '|'\n"
20736#ifdef SQLITE_ENABLE_SORTER_REFERENCES
20737  "   -sorterref SIZE      sorter references threshold size\n"
20738#endif
20739  "   -stats               print memory stats before each finalize\n"
20740  "   -table               set output mode to 'table'\n"
20741  "   -tabs                set output mode to 'tabs'\n"
20742  "   -version             show SQLite version\n"
20743  "   -vfs NAME            use NAME as the default VFS\n"
20744#ifdef SQLITE_ENABLE_VFSTRACE
20745  "   -vfstrace            enable tracing of all VFS calls\n"
20746#endif
20747#ifdef SQLITE_HAVE_ZLIB
20748  "   -zip                 open the file as a ZIP Archive\n"
20749#endif
20750;
20751static void usage(int showDetail){
20752  utf8_printf(stderr,
20753      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
20754      "FILENAME is the name of an SQLite database. A new database is created\n"
20755      "if the file does not previously exist.\n", Argv0);
20756  if( showDetail ){
20757    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
20758  }else{
20759    raw_printf(stderr, "Use the -help option for additional information\n");
20760  }
20761  exit(1);
20762}
20763
20764/*
20765** Internal check:  Verify that the SQLite is uninitialized.  Print a
20766** error message if it is initialized.
20767*/
20768static void verify_uninitialized(void){
20769  if( sqlite3_config(-1)==SQLITE_MISUSE ){
20770    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
20771                        " initialization.\n");
20772  }
20773}
20774
20775/*
20776** Initialize the state information in data
20777*/
20778static void main_init(ShellState *data) {
20779  memset(data, 0, sizeof(*data));
20780  data->normalMode = data->cMode = data->mode = MODE_List;
20781  data->autoExplain = 1;
20782  memcpy(data->colSeparator,SEP_Column, 2);
20783  memcpy(data->rowSeparator,SEP_Row, 2);
20784  data->showHeader = 0;
20785  data->shellFlgs = SHFLG_Lookaside;
20786  verify_uninitialized();
20787  sqlite3_config(SQLITE_CONFIG_URI, 1);
20788  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
20789  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
20790  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
20791  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
20792}
20793
20794/*
20795** Output text to the console in a font that attracts extra attention.
20796*/
20797#ifdef _WIN32
20798static void printBold(const char *zText){
20799#if !SQLITE_OS_WINRT
20800  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
20801  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
20802  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
20803  SetConsoleTextAttribute(out,
20804         FOREGROUND_RED|FOREGROUND_INTENSITY
20805  );
20806#endif
20807  printf("%s", zText);
20808#if !SQLITE_OS_WINRT
20809  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
20810#endif
20811}
20812#else
20813static void printBold(const char *zText){
20814  printf("\033[1m%s\033[0m", zText);
20815}
20816#endif
20817
20818/*
20819** Get the argument to an --option.  Throw an error and die if no argument
20820** is available.
20821*/
20822static char *cmdline_option_value(int argc, char **argv, int i){
20823  if( i==argc ){
20824    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
20825            argv[0], argv[argc-1]);
20826    exit(1);
20827  }
20828  return argv[i];
20829}
20830
20831#ifndef SQLITE_SHELL_IS_UTF8
20832#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
20833#    define SQLITE_SHELL_IS_UTF8          (0)
20834#  else
20835#    define SQLITE_SHELL_IS_UTF8          (1)
20836#  endif
20837#endif
20838
20839#if SQLITE_SHELL_IS_UTF8
20840int SQLITE_CDECL main(int argc, char **argv){
20841#else
20842int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
20843  char **argv;
20844#endif
20845  char *zErrMsg = 0;
20846  ShellState data;
20847  const char *zInitFile = 0;
20848  int i;
20849  int rc = 0;
20850  int warnInmemoryDb = 0;
20851  int readStdin = 1;
20852  int nCmd = 0;
20853  char **azCmd = 0;
20854  const char *zVfs = 0;           /* Value of -vfs command-line option */
20855#if !SQLITE_SHELL_IS_UTF8
20856  char **argvToFree = 0;
20857  int argcToFree = 0;
20858#endif
20859
20860  setBinaryMode(stdin, 0);
20861  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
20862  stdin_is_interactive = isatty(0);
20863  stdout_is_console = isatty(1);
20864
20865#ifdef SQLITE_DEBUG
20866  registerOomSimulator();
20867#endif
20868
20869#if !defined(_WIN32_WCE)
20870  if( getenv("SQLITE_DEBUG_BREAK") ){
20871    if( isatty(0) && isatty(2) ){
20872      fprintf(stderr,
20873          "attach debugger to process %d and press any key to continue.\n",
20874          GETPID());
20875      fgetc(stdin);
20876    }else{
20877#if defined(_WIN32) || defined(WIN32)
20878#if SQLITE_OS_WINRT
20879      __debugbreak();
20880#else
20881      DebugBreak();
20882#endif
20883#elif defined(SIGTRAP)
20884      raise(SIGTRAP);
20885#endif
20886    }
20887  }
20888#endif
20889
20890#if USE_SYSTEM_SQLITE+0!=1
20891  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
20892    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
20893            sqlite3_sourceid(), SQLITE_SOURCE_ID);
20894    exit(1);
20895  }
20896#endif
20897  main_init(&data);
20898
20899  /* On Windows, we must translate command-line arguments into UTF-8.
20900  ** The SQLite memory allocator subsystem has to be enabled in order to
20901  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
20902  ** subsequent sqlite3_config() calls will work.  So copy all results into
20903  ** memory that does not come from the SQLite memory allocator.
20904  */
20905#if !SQLITE_SHELL_IS_UTF8
20906  sqlite3_initialize();
20907  argvToFree = malloc(sizeof(argv[0])*argc*2);
20908  argcToFree = argc;
20909  argv = argvToFree + argc;
20910  if( argv==0 ) shell_out_of_memory();
20911  for(i=0; i<argc; i++){
20912    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
20913    int n;
20914    if( z==0 ) shell_out_of_memory();
20915    n = (int)strlen(z);
20916    argv[i] = malloc( n+1 );
20917    if( argv[i]==0 ) shell_out_of_memory();
20918    memcpy(argv[i], z, n+1);
20919    argvToFree[i] = argv[i];
20920    sqlite3_free(z);
20921  }
20922  sqlite3_shutdown();
20923#endif
20924
20925  assert( argc>=1 && argv && argv[0] );
20926  Argv0 = argv[0];
20927
20928  /* Make sure we have a valid signal handler early, before anything
20929  ** else is done.
20930  */
20931#ifdef SIGINT
20932  signal(SIGINT, interrupt_handler);
20933#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
20934  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
20935#endif
20936
20937#ifdef SQLITE_SHELL_DBNAME_PROC
20938  {
20939    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
20940    ** of a C-function that will provide the name of the database file.  Use
20941    ** this compile-time option to embed this shell program in larger
20942    ** applications. */
20943    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
20944    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
20945    warnInmemoryDb = 0;
20946  }
20947#endif
20948
20949  /* Do an initial pass through the command-line argument to locate
20950  ** the name of the database file, the name of the initialization file,
20951  ** the size of the alternative malloc heap,
20952  ** and the first command to execute.
20953  */
20954  verify_uninitialized();
20955  for(i=1; i<argc; i++){
20956    char *z;
20957    z = argv[i];
20958    if( z[0]!='-' ){
20959      if( data.zDbFilename==0 ){
20960        data.zDbFilename = z;
20961      }else{
20962        /* Excesss arguments are interpreted as SQL (or dot-commands) and
20963        ** mean that nothing is read from stdin */
20964        readStdin = 0;
20965        nCmd++;
20966        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
20967        if( azCmd==0 ) shell_out_of_memory();
20968        azCmd[nCmd-1] = z;
20969      }
20970    }
20971    if( z[1]=='-' ) z++;
20972    if( strcmp(z,"-separator")==0
20973     || strcmp(z,"-nullvalue")==0
20974     || strcmp(z,"-newline")==0
20975     || strcmp(z,"-cmd")==0
20976    ){
20977      (void)cmdline_option_value(argc, argv, ++i);
20978    }else if( strcmp(z,"-init")==0 ){
20979      zInitFile = cmdline_option_value(argc, argv, ++i);
20980    }else if( strcmp(z,"-batch")==0 ){
20981      /* Need to check for batch mode here to so we can avoid printing
20982      ** informational messages (like from process_sqliterc) before
20983      ** we do the actual processing of arguments later in a second pass.
20984      */
20985      stdin_is_interactive = 0;
20986    }else if( strcmp(z,"-heap")==0 ){
20987#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20988      const char *zSize;
20989      sqlite3_int64 szHeap;
20990
20991      zSize = cmdline_option_value(argc, argv, ++i);
20992      szHeap = integerValue(zSize);
20993      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
20994      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
20995#else
20996      (void)cmdline_option_value(argc, argv, ++i);
20997#endif
20998    }else if( strcmp(z,"-pagecache")==0 ){
20999      sqlite3_int64 n, sz;
21000      sz = integerValue(cmdline_option_value(argc,argv,++i));
21001      if( sz>70000 ) sz = 70000;
21002      if( sz<0 ) sz = 0;
21003      n = integerValue(cmdline_option_value(argc,argv,++i));
21004      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21005        n = 0xffffffffffffLL/sz;
21006      }
21007      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
21008                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
21009      data.shellFlgs |= SHFLG_Pagecache;
21010    }else if( strcmp(z,"-lookaside")==0 ){
21011      int n, sz;
21012      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
21013      if( sz<0 ) sz = 0;
21014      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
21015      if( n<0 ) n = 0;
21016      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
21017      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
21018#ifdef SQLITE_ENABLE_VFSTRACE
21019    }else if( strcmp(z,"-vfstrace")==0 ){
21020      extern int vfstrace_register(
21021         const char *zTraceName,
21022         const char *zOldVfsName,
21023         int (*xOut)(const char*,void*),
21024         void *pOutArg,
21025         int makeDefault
21026      );
21027      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
21028#endif
21029#ifdef SQLITE_ENABLE_MULTIPLEX
21030    }else if( strcmp(z,"-multiplex")==0 ){
21031      extern int sqlite3_multiple_initialize(const char*,int);
21032      sqlite3_multiplex_initialize(0, 1);
21033#endif
21034    }else if( strcmp(z,"-mmap")==0 ){
21035      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21036      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
21037#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21038    }else if( strcmp(z,"-sorterref")==0 ){
21039      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21040      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
21041#endif
21042    }else if( strcmp(z,"-vfs")==0 ){
21043      zVfs = cmdline_option_value(argc, argv, ++i);
21044#ifdef SQLITE_HAVE_ZLIB
21045    }else if( strcmp(z,"-zip")==0 ){
21046      data.openMode = SHELL_OPEN_ZIPFILE;
21047#endif
21048    }else if( strcmp(z,"-append")==0 ){
21049      data.openMode = SHELL_OPEN_APPENDVFS;
21050#ifdef SQLITE_ENABLE_DESERIALIZE
21051    }else if( strcmp(z,"-deserialize")==0 ){
21052      data.openMode = SHELL_OPEN_DESERIALIZE;
21053    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21054      data.szMax = integerValue(argv[++i]);
21055#endif
21056    }else if( strcmp(z,"-readonly")==0 ){
21057      data.openMode = SHELL_OPEN_READONLY;
21058    }else if( strcmp(z,"-nofollow")==0 ){
21059      data.openFlags = SQLITE_OPEN_NOFOLLOW;
21060#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21061    }else if( strncmp(z, "-A",2)==0 ){
21062      /* All remaining command-line arguments are passed to the ".archive"
21063      ** command, so ignore them */
21064      break;
21065#endif
21066    }else if( strcmp(z, "-memtrace")==0 ){
21067      sqlite3MemTraceActivate(stderr);
21068    }else if( strcmp(z,"-bail")==0 ){
21069      bail_on_error = 1;
21070    }
21071  }
21072  verify_uninitialized();
21073
21074
21075#ifdef SQLITE_SHELL_INIT_PROC
21076  {
21077    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
21078    ** of a C-function that will perform initialization actions on SQLite that
21079    ** occur just before or after sqlite3_initialize(). Use this compile-time
21080    ** option to embed this shell program in larger applications. */
21081    extern void SQLITE_SHELL_INIT_PROC(void);
21082    SQLITE_SHELL_INIT_PROC();
21083  }
21084#else
21085  /* All the sqlite3_config() calls have now been made. So it is safe
21086  ** to call sqlite3_initialize() and process any command line -vfs option. */
21087  sqlite3_initialize();
21088#endif
21089
21090  if( zVfs ){
21091    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
21092    if( pVfs ){
21093      sqlite3_vfs_register(pVfs, 1);
21094    }else{
21095      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
21096      exit(1);
21097    }
21098  }
21099
21100  if( data.zDbFilename==0 ){
21101#ifndef SQLITE_OMIT_MEMORYDB
21102    data.zDbFilename = ":memory:";
21103    warnInmemoryDb = argc==1;
21104#else
21105    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
21106    return 1;
21107#endif
21108  }
21109  data.out = stdout;
21110  sqlite3_appendvfs_init(0,0,0);
21111
21112  /* Go ahead and open the database file if it already exists.  If the
21113  ** file does not exist, delay opening it.  This prevents empty database
21114  ** files from being created if a user mistypes the database name argument
21115  ** to the sqlite command-line tool.
21116  */
21117  if( access(data.zDbFilename, 0)==0 ){
21118    open_db(&data, 0);
21119  }
21120
21121  /* Process the initialization file if there is one.  If no -init option
21122  ** is given on the command line, look for a file named ~/.sqliterc and
21123  ** try to process it.
21124  */
21125  process_sqliterc(&data,zInitFile);
21126
21127  /* Make a second pass through the command-line argument and set
21128  ** options.  This second pass is delayed until after the initialization
21129  ** file is processed so that the command-line arguments will override
21130  ** settings in the initialization file.
21131  */
21132  for(i=1; i<argc; i++){
21133    char *z = argv[i];
21134    if( z[0]!='-' ) continue;
21135    if( z[1]=='-' ){ z++; }
21136    if( strcmp(z,"-init")==0 ){
21137      i++;
21138    }else if( strcmp(z,"-html")==0 ){
21139      data.mode = MODE_Html;
21140    }else if( strcmp(z,"-list")==0 ){
21141      data.mode = MODE_List;
21142    }else if( strcmp(z,"-quote")==0 ){
21143      data.mode = MODE_Quote;
21144      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
21145      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21146    }else if( strcmp(z,"-line")==0 ){
21147      data.mode = MODE_Line;
21148    }else if( strcmp(z,"-column")==0 ){
21149      data.mode = MODE_Column;
21150    }else if( strcmp(z,"-json")==0 ){
21151      data.mode = MODE_Json;
21152    }else if( strcmp(z,"-markdown")==0 ){
21153      data.mode = MODE_Markdown;
21154    }else if( strcmp(z,"-table")==0 ){
21155      data.mode = MODE_Table;
21156    }else if( strcmp(z,"-box")==0 ){
21157      data.mode = MODE_Box;
21158    }else if( strcmp(z,"-csv")==0 ){
21159      data.mode = MODE_Csv;
21160      memcpy(data.colSeparator,",",2);
21161#ifdef SQLITE_HAVE_ZLIB
21162    }else if( strcmp(z,"-zip")==0 ){
21163      data.openMode = SHELL_OPEN_ZIPFILE;
21164#endif
21165    }else if( strcmp(z,"-append")==0 ){
21166      data.openMode = SHELL_OPEN_APPENDVFS;
21167#ifdef SQLITE_ENABLE_DESERIALIZE
21168    }else if( strcmp(z,"-deserialize")==0 ){
21169      data.openMode = SHELL_OPEN_DESERIALIZE;
21170    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21171      data.szMax = integerValue(argv[++i]);
21172#endif
21173    }else if( strcmp(z,"-readonly")==0 ){
21174      data.openMode = SHELL_OPEN_READONLY;
21175    }else if( strcmp(z,"-nofollow")==0 ){
21176      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
21177    }else if( strcmp(z,"-ascii")==0 ){
21178      data.mode = MODE_Ascii;
21179      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
21180      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
21181    }else if( strcmp(z,"-tabs")==0 ){
21182      data.mode = MODE_List;
21183      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
21184      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21185    }else if( strcmp(z,"-separator")==0 ){
21186      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
21187                       "%s",cmdline_option_value(argc,argv,++i));
21188    }else if( strcmp(z,"-newline")==0 ){
21189      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
21190                       "%s",cmdline_option_value(argc,argv,++i));
21191    }else if( strcmp(z,"-nullvalue")==0 ){
21192      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
21193                       "%s",cmdline_option_value(argc,argv,++i));
21194    }else if( strcmp(z,"-header")==0 ){
21195      data.showHeader = 1;
21196    }else if( strcmp(z,"-noheader")==0 ){
21197      data.showHeader = 0;
21198    }else if( strcmp(z,"-echo")==0 ){
21199      ShellSetFlag(&data, SHFLG_Echo);
21200    }else if( strcmp(z,"-eqp")==0 ){
21201      data.autoEQP = AUTOEQP_on;
21202    }else if( strcmp(z,"-eqpfull")==0 ){
21203      data.autoEQP = AUTOEQP_full;
21204    }else if( strcmp(z,"-stats")==0 ){
21205      data.statsOn = 1;
21206    }else if( strcmp(z,"-scanstats")==0 ){
21207      data.scanstatsOn = 1;
21208    }else if( strcmp(z,"-backslash")==0 ){
21209      /* Undocumented command-line option: -backslash
21210      ** Causes C-style backslash escapes to be evaluated in SQL statements
21211      ** prior to sending the SQL into SQLite.  Useful for injecting
21212      ** crazy bytes in the middle of SQL statements for testing and debugging.
21213      */
21214      ShellSetFlag(&data, SHFLG_Backslash);
21215    }else if( strcmp(z,"-bail")==0 ){
21216      /* No-op.  The bail_on_error flag should already be set. */
21217    }else if( strcmp(z,"-version")==0 ){
21218      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
21219      return 0;
21220    }else if( strcmp(z,"-interactive")==0 ){
21221      stdin_is_interactive = 1;
21222    }else if( strcmp(z,"-batch")==0 ){
21223      stdin_is_interactive = 0;
21224    }else if( strcmp(z,"-heap")==0 ){
21225      i++;
21226    }else if( strcmp(z,"-pagecache")==0 ){
21227      i+=2;
21228    }else if( strcmp(z,"-lookaside")==0 ){
21229      i+=2;
21230    }else if( strcmp(z,"-mmap")==0 ){
21231      i++;
21232    }else if( strcmp(z,"-memtrace")==0 ){
21233      i++;
21234#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21235    }else if( strcmp(z,"-sorterref")==0 ){
21236      i++;
21237#endif
21238    }else if( strcmp(z,"-vfs")==0 ){
21239      i++;
21240#ifdef SQLITE_ENABLE_VFSTRACE
21241    }else if( strcmp(z,"-vfstrace")==0 ){
21242      i++;
21243#endif
21244#ifdef SQLITE_ENABLE_MULTIPLEX
21245    }else if( strcmp(z,"-multiplex")==0 ){
21246      i++;
21247#endif
21248    }else if( strcmp(z,"-help")==0 ){
21249      usage(1);
21250    }else if( strcmp(z,"-cmd")==0 ){
21251      /* Run commands that follow -cmd first and separately from commands
21252      ** that simply appear on the command-line.  This seems goofy.  It would
21253      ** be better if all commands ran in the order that they appear.  But
21254      ** we retain the goofy behavior for historical compatibility. */
21255      if( i==argc-1 ) break;
21256      z = cmdline_option_value(argc,argv,++i);
21257      if( z[0]=='.' ){
21258        rc = do_meta_command(z, &data);
21259        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
21260      }else{
21261        open_db(&data, 0);
21262        rc = shell_exec(&data, z, &zErrMsg);
21263        if( zErrMsg!=0 ){
21264          utf8_printf(stderr,"Error: %s\n", zErrMsg);
21265          if( bail_on_error ) return rc!=0 ? rc : 1;
21266        }else if( rc!=0 ){
21267          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
21268          if( bail_on_error ) return rc;
21269        }
21270      }
21271#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21272    }else if( strncmp(z, "-A", 2)==0 ){
21273      if( nCmd>0 ){
21274        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
21275                            " with \"%s\"\n", z);
21276        return 1;
21277      }
21278      open_db(&data, OPEN_DB_ZIPFILE);
21279      if( z[2] ){
21280        argv[i] = &z[2];
21281        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
21282      }else{
21283        arDotCommand(&data, 1, argv+i, argc-i);
21284      }
21285      readStdin = 0;
21286      break;
21287#endif
21288    }else{
21289      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
21290      raw_printf(stderr,"Use -help for a list of options.\n");
21291      return 1;
21292    }
21293    data.cMode = data.mode;
21294  }
21295
21296  if( !readStdin ){
21297    /* Run all arguments that do not begin with '-' as if they were separate
21298    ** command-line inputs, except for the argToSkip argument which contains
21299    ** the database filename.
21300    */
21301    for(i=0; i<nCmd; i++){
21302      if( azCmd[i][0]=='.' ){
21303        rc = do_meta_command(azCmd[i], &data);
21304        if( rc ){
21305          free(azCmd);
21306          return rc==2 ? 0 : rc;
21307        }
21308      }else{
21309        open_db(&data, 0);
21310        rc = shell_exec(&data, azCmd[i], &zErrMsg);
21311        if( zErrMsg || rc ){
21312          if( zErrMsg!=0 ){
21313            utf8_printf(stderr,"Error: %s\n", zErrMsg);
21314          }else{
21315            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
21316          }
21317          sqlite3_free(zErrMsg);
21318          free(azCmd);
21319          return rc!=0 ? rc : 1;
21320        }
21321      }
21322    }
21323  }else{
21324    /* Run commands received from standard input
21325    */
21326    if( stdin_is_interactive ){
21327      char *zHome;
21328      char *zHistory;
21329      int nHistory;
21330      printf(
21331        "SQLite version %s %.19s\n" /*extra-version-info*/
21332        "Enter \".help\" for usage hints.\n",
21333        sqlite3_libversion(), sqlite3_sourceid()
21334      );
21335      if( warnInmemoryDb ){
21336        printf("Connected to a ");
21337        printBold("transient in-memory database");
21338        printf(".\nUse \".open FILENAME\" to reopen on a "
21339               "persistent database.\n");
21340      }
21341      zHistory = getenv("SQLITE_HISTORY");
21342      if( zHistory ){
21343        zHistory = strdup(zHistory);
21344      }else if( (zHome = find_home_dir(0))!=0 ){
21345        nHistory = strlen30(zHome) + 20;
21346        if( (zHistory = malloc(nHistory))!=0 ){
21347          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
21348        }
21349      }
21350      if( zHistory ){ shell_read_history(zHistory); }
21351#if HAVE_READLINE || HAVE_EDITLINE
21352      rl_attempted_completion_function = readline_completion;
21353#elif HAVE_LINENOISE
21354      linenoiseSetCompletionCallback(linenoise_completion);
21355#endif
21356      data.in = 0;
21357      rc = process_input(&data);
21358      if( zHistory ){
21359        shell_stifle_history(2000);
21360        shell_write_history(zHistory);
21361        free(zHistory);
21362      }
21363    }else{
21364      data.in = stdin;
21365      rc = process_input(&data);
21366    }
21367  }
21368  free(azCmd);
21369  set_table_name(&data, 0);
21370  if( data.db ){
21371    session_close_all(&data);
21372    close_db(data.db);
21373  }
21374  sqlite3_free(data.zFreeOnClose);
21375  find_home_dir(1);
21376  output_reset(&data);
21377  data.doXdgOpen = 0;
21378  clearTempFile(&data);
21379#if !SQLITE_SHELL_IS_UTF8
21380  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
21381  free(argvToFree);
21382#endif
21383  free(data.colWidth);
21384  /* Clear the global data structure so that valgrind will detect memory
21385  ** leaks */
21386  memset(&data, 0, sizeof(data));
21387  return rc;
21388}
21389