xref: /freebsd/contrib/sqlite3/shell.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 ** Optionally #include a user-defined header, whereby compilation options
40 ** may be set prior to where they take effect, but after platform setup.
41 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
42 ** file. Note that this macro has a like effect on sqlite3.c compilation.
43 */
44 # define SHELL_STRINGIFY_(f) #f
45 # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
46 #ifdef SQLITE_CUSTOM_INCLUDE
47 # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
48 #endif
49 
50 /*
51 ** Determine if we are dealing with WinRT, which provides only a subset of
52 ** the full Win32 API.
53 */
54 #if !defined(SQLITE_OS_WINRT)
55 # define SQLITE_OS_WINRT 0
56 #endif
57 
58 /*
59 ** Warning pragmas copied from msvc.h in the core.
60 */
61 #if defined(_MSC_VER)
62 #pragma warning(disable : 4054)
63 #pragma warning(disable : 4055)
64 #pragma warning(disable : 4100)
65 #pragma warning(disable : 4127)
66 #pragma warning(disable : 4130)
67 #pragma warning(disable : 4152)
68 #pragma warning(disable : 4189)
69 #pragma warning(disable : 4206)
70 #pragma warning(disable : 4210)
71 #pragma warning(disable : 4232)
72 #pragma warning(disable : 4244)
73 #pragma warning(disable : 4305)
74 #pragma warning(disable : 4306)
75 #pragma warning(disable : 4702)
76 #pragma warning(disable : 4706)
77 #endif /* defined(_MSC_VER) */
78 
79 /*
80 ** No support for loadable extensions in VxWorks.
81 */
82 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
83 # define SQLITE_OMIT_LOAD_EXTENSION 1
84 #endif
85 
86 /*
87 ** Enable large-file support for fopen() and friends on unix.
88 */
89 #ifndef SQLITE_DISABLE_LFS
90 # define _LARGE_FILE       1
91 # ifndef _FILE_OFFSET_BITS
92 #   define _FILE_OFFSET_BITS 64
93 # endif
94 # define _LARGEFILE_SOURCE 1
95 #endif
96 
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdio.h>
100 #include <assert.h>
101 #include "sqlite3.h"
102 typedef sqlite3_int64 i64;
103 typedef sqlite3_uint64 u64;
104 typedef unsigned char u8;
105 #if SQLITE_USER_AUTHENTICATION
106 # include "sqlite3userauth.h"
107 #endif
108 #include <ctype.h>
109 #include <stdarg.h>
110 
111 #if !defined(_WIN32) && !defined(WIN32)
112 # include <signal.h>
113 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
114 #  include <pwd.h>
115 # endif
116 #endif
117 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
118 # include <unistd.h>
119 # include <dirent.h>
120 # define GETPID getpid
121 # if defined(__MINGW32__)
122 #  define DIRENT dirent
123 #  ifndef S_ISLNK
124 #   define S_ISLNK(mode) (0)
125 #  endif
126 # endif
127 #else
128 # define GETPID (int)GetCurrentProcessId
129 #endif
130 #include <sys/types.h>
131 #include <sys/stat.h>
132 
133 #if HAVE_READLINE
134 # include <readline/readline.h>
135 # include <readline/history.h>
136 #endif
137 
138 #if HAVE_EDITLINE
139 # include <editline/readline.h>
140 #endif
141 
142 #if HAVE_EDITLINE || HAVE_READLINE
143 
144 # define shell_add_history(X) add_history(X)
145 # define shell_read_history(X) read_history(X)
146 # define shell_write_history(X) write_history(X)
147 # define shell_stifle_history(X) stifle_history(X)
148 # define shell_readline(X) readline(X)
149 
150 #elif HAVE_LINENOISE
151 
152 # include "linenoise.h"
153 # define shell_add_history(X) linenoiseHistoryAdd(X)
154 # define shell_read_history(X) linenoiseHistoryLoad(X)
155 # define shell_write_history(X) linenoiseHistorySave(X)
156 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
157 # define shell_readline(X) linenoise(X)
158 
159 #else
160 
161 # define shell_read_history(X)
162 # define shell_write_history(X)
163 # define shell_stifle_history(X)
164 
165 # define SHELL_USE_LOCAL_GETLINE 1
166 #endif
167 
168 
169 #if defined(_WIN32) || defined(WIN32)
170 # if SQLITE_OS_WINRT
171 #  define SQLITE_OMIT_POPEN 1
172 # else
173 #  include <io.h>
174 #  include <fcntl.h>
175 #  define isatty(h) _isatty(h)
176 #  ifndef access
177 #   define access(f,m) _access((f),(m))
178 #  endif
179 #  ifndef unlink
180 #   define unlink _unlink
181 #  endif
182 #  ifndef strdup
183 #   define strdup _strdup
184 #  endif
185 #  undef popen
186 #  define popen _popen
187 #  undef pclose
188 #  define pclose _pclose
189 # endif
190 #else
191  /* Make sure isatty() has a prototype. */
192  extern int isatty(int);
193 
194 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
195   /* popen and pclose are not C89 functions and so are
196   ** sometimes omitted from the <stdio.h> header */
197    extern FILE *popen(const char*,const char*);
198    extern int pclose(FILE*);
199 # else
200 #  define SQLITE_OMIT_POPEN 1
201 # endif
202 #endif
203 
204 #if defined(_WIN32_WCE)
205 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
206  * thus we always assume that we have a console. That can be
207  * overridden with the -batch command line option.
208  */
209 #define isatty(x) 1
210 #endif
211 
212 /* ctype macros that work with signed characters */
213 #define IsSpace(X)  isspace((unsigned char)X)
214 #define IsDigit(X)  isdigit((unsigned char)X)
215 #define ToLower(X)  (char)tolower((unsigned char)X)
216 
217 #if defined(_WIN32) || defined(WIN32)
218 #if SQLITE_OS_WINRT
219 #include <intrin.h>
220 #endif
221 #include <windows.h>
222 
223 /* string conversion routines only needed on Win32 */
224 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
225 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
226 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
227 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
228 #endif
229 
230 /* On Windows, we normally run with output mode of TEXT so that \n characters
231 ** are automatically translated into \r\n.  However, this behavior needs
232 ** to be disabled in some cases (ex: when generating CSV output and when
233 ** rendering quoted strings that contain \n characters).  The following
234 ** routines take care of that.
235 */
236 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
237 static void setBinaryMode(FILE *file, int isOutput){
238   if( isOutput ) fflush(file);
239   _setmode(_fileno(file), _O_BINARY);
240 }
241 static void setTextMode(FILE *file, int isOutput){
242   if( isOutput ) fflush(file);
243   _setmode(_fileno(file), _O_TEXT);
244 }
245 #else
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
248 #endif
249 
250 
251 /* True if the timer is enabled */
252 static int enableTimer = 0;
253 
254 /* Return the current wall-clock time */
255 static sqlite3_int64 timeOfDay(void){
256   static sqlite3_vfs *clockVfs = 0;
257   sqlite3_int64 t;
258   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
259   if( clockVfs==0 ) return 0;  /* Never actually happens */
260   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
261     clockVfs->xCurrentTimeInt64(clockVfs, &t);
262   }else{
263     double r;
264     clockVfs->xCurrentTime(clockVfs, &r);
265     t = (sqlite3_int64)(r*86400000.0);
266   }
267   return t;
268 }
269 
270 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
271 #include <sys/time.h>
272 #include <sys/resource.h>
273 
274 /* VxWorks does not support getrusage() as far as we can determine */
275 #if defined(_WRS_KERNEL) || defined(__RTP__)
276 struct rusage {
277   struct timeval ru_utime; /* user CPU time used */
278   struct timeval ru_stime; /* system CPU time used */
279 };
280 #define getrusage(A,B) memset(B,0,sizeof(*B))
281 #endif
282 
283 /* Saved resource information for the beginning of an operation */
284 static struct rusage sBegin;  /* CPU time at start */
285 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
286 
287 /*
288 ** Begin timing an operation
289 */
290 static void beginTimer(void){
291   if( enableTimer ){
292     getrusage(RUSAGE_SELF, &sBegin);
293     iBegin = timeOfDay();
294   }
295 }
296 
297 /* Return the difference of two time_structs in seconds */
298 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
299   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
300          (double)(pEnd->tv_sec - pStart->tv_sec);
301 }
302 
303 /*
304 ** Print the timing results.
305 */
306 static void endTimer(void){
307   if( enableTimer ){
308     sqlite3_int64 iEnd = timeOfDay();
309     struct rusage sEnd;
310     getrusage(RUSAGE_SELF, &sEnd);
311     printf("Run Time: real %.3f user %f sys %f\n",
312        (iEnd - iBegin)*0.001,
313        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
314        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
315   }
316 }
317 
318 #define BEGIN_TIMER beginTimer()
319 #define END_TIMER endTimer()
320 #define HAS_TIMER 1
321 
322 #elif (defined(_WIN32) || defined(WIN32))
323 
324 /* Saved resource information for the beginning of an operation */
325 static HANDLE hProcess;
326 static FILETIME ftKernelBegin;
327 static FILETIME ftUserBegin;
328 static sqlite3_int64 ftWallBegin;
329 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
330                                     LPFILETIME, LPFILETIME);
331 static GETPROCTIMES getProcessTimesAddr = NULL;
332 
333 /*
334 ** Check to see if we have timer support.  Return 1 if necessary
335 ** support found (or found previously).
336 */
337 static int hasTimer(void){
338   if( getProcessTimesAddr ){
339     return 1;
340   } else {
341 #if !SQLITE_OS_WINRT
342     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
343     ** versions. See if the version we are running on has it, and if it
344     ** does, save off a pointer to it and the current process handle.
345     */
346     hProcess = GetCurrentProcess();
347     if( hProcess ){
348       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
349       if( NULL != hinstLib ){
350         getProcessTimesAddr =
351             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
352         if( NULL != getProcessTimesAddr ){
353           return 1;
354         }
355         FreeLibrary(hinstLib);
356       }
357     }
358 #endif
359   }
360   return 0;
361 }
362 
363 /*
364 ** Begin timing an operation
365 */
366 static void beginTimer(void){
367   if( enableTimer && getProcessTimesAddr ){
368     FILETIME ftCreation, ftExit;
369     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
370                         &ftKernelBegin,&ftUserBegin);
371     ftWallBegin = timeOfDay();
372   }
373 }
374 
375 /* Return the difference of two FILETIME structs in seconds */
376 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
377   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
378   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
379   return (double) ((i64End - i64Start) / 10000000.0);
380 }
381 
382 /*
383 ** Print the timing results.
384 */
385 static void endTimer(void){
386   if( enableTimer && getProcessTimesAddr){
387     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
388     sqlite3_int64 ftWallEnd = timeOfDay();
389     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
390     printf("Run Time: real %.3f user %f sys %f\n",
391        (ftWallEnd - ftWallBegin)*0.001,
392        timeDiff(&ftUserBegin, &ftUserEnd),
393        timeDiff(&ftKernelBegin, &ftKernelEnd));
394   }
395 }
396 
397 #define BEGIN_TIMER beginTimer()
398 #define END_TIMER endTimer()
399 #define HAS_TIMER hasTimer()
400 
401 #else
402 #define BEGIN_TIMER
403 #define END_TIMER
404 #define HAS_TIMER 0
405 #endif
406 
407 /*
408 ** Used to prevent warnings about unused parameters
409 */
410 #define UNUSED_PARAMETER(x) (void)(x)
411 
412 /*
413 ** Number of elements in an array
414 */
415 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
416 
417 /*
418 ** If the following flag is set, then command execution stops
419 ** at an error if we are not interactive.
420 */
421 static int bail_on_error = 0;
422 
423 /*
424 ** Threat stdin as an interactive input if the following variable
425 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
426 */
427 static int stdin_is_interactive = 1;
428 
429 /*
430 ** On Windows systems we have to know if standard output is a console
431 ** in order to translate UTF-8 into MBCS.  The following variable is
432 ** true if translation is required.
433 */
434 static int stdout_is_console = 1;
435 
436 /*
437 ** The following is the open SQLite database.  We make a pointer
438 ** to this database a static variable so that it can be accessed
439 ** by the SIGINT handler to interrupt database processing.
440 */
441 static sqlite3 *globalDb = 0;
442 
443 /*
444 ** True if an interrupt (Control-C) has been received.
445 */
446 static volatile int seenInterrupt = 0;
447 
448 /*
449 ** This is the name of our program. It is set in main(), used
450 ** in a number of other places, mostly for error messages.
451 */
452 static char *Argv0;
453 
454 /*
455 ** Prompt strings. Initialized in main. Settable with
456 **   .prompt main continue
457 */
458 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
459 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
460 
461 /*
462 ** Render output like fprintf().  Except, if the output is going to the
463 ** console and if this is running on a Windows machine, translate the
464 ** output from UTF-8 into MBCS.
465 */
466 #if defined(_WIN32) || defined(WIN32)
467 void utf8_printf(FILE *out, const char *zFormat, ...){
468   va_list ap;
469   va_start(ap, zFormat);
470   if( stdout_is_console && (out==stdout || out==stderr) ){
471     char *z1 = sqlite3_vmprintf(zFormat, ap);
472     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
473     sqlite3_free(z1);
474     fputs(z2, out);
475     sqlite3_free(z2);
476   }else{
477     vfprintf(out, zFormat, ap);
478   }
479   va_end(ap);
480 }
481 #elif !defined(utf8_printf)
482 # define utf8_printf fprintf
483 #endif
484 
485 /*
486 ** Render output like fprintf().  This should not be used on anything that
487 ** includes string formatting (e.g. "%s").
488 */
489 #if !defined(raw_printf)
490 # define raw_printf fprintf
491 #endif
492 
493 /* Indicate out-of-memory and exit. */
494 static void shell_out_of_memory(void){
495   raw_printf(stderr,"Error: out of memory\n");
496   exit(1);
497 }
498 
499 /* Check a pointer to see if it is NULL.  If it is NULL, exit with an
500 ** out-of-memory error.
501 */
502 static void shell_check_oom(void *p){
503   if( p==0 ) shell_out_of_memory();
504 }
505 
506 /*
507 ** Write I/O traces to the following stream.
508 */
509 #ifdef SQLITE_ENABLE_IOTRACE
510 static FILE *iotrace = 0;
511 #endif
512 
513 /*
514 ** This routine works like printf in that its first argument is a
515 ** format string and subsequent arguments are values to be substituted
516 ** in place of % fields.  The result of formatting this string
517 ** is written to iotrace.
518 */
519 #ifdef SQLITE_ENABLE_IOTRACE
520 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
521   va_list ap;
522   char *z;
523   if( iotrace==0 ) return;
524   va_start(ap, zFormat);
525   z = sqlite3_vmprintf(zFormat, ap);
526   va_end(ap);
527   utf8_printf(iotrace, "%s", z);
528   sqlite3_free(z);
529 }
530 #endif
531 
532 /*
533 ** Output string zUtf to stream pOut as w characters.  If w is negative,
534 ** then right-justify the text.  W is the width in UTF-8 characters, not
535 ** in bytes.  This is different from the %*.*s specification in printf
536 ** since with %*.*s the width is measured in bytes, not characters.
537 */
538 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
539   int i;
540   int n;
541   int aw = w<0 ? -w : w;
542   for(i=n=0; zUtf[i]; i++){
543     if( (zUtf[i]&0xc0)!=0x80 ){
544       n++;
545       if( n==aw ){
546         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
547         break;
548       }
549     }
550   }
551   if( n>=aw ){
552     utf8_printf(pOut, "%.*s", i, zUtf);
553   }else if( w<0 ){
554     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
555   }else{
556     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
557   }
558 }
559 
560 
561 /*
562 ** Determines if a string is a number of not.
563 */
564 static int isNumber(const char *z, int *realnum){
565   if( *z=='-' || *z=='+' ) z++;
566   if( !IsDigit(*z) ){
567     return 0;
568   }
569   z++;
570   if( realnum ) *realnum = 0;
571   while( IsDigit(*z) ){ z++; }
572   if( *z=='.' ){
573     z++;
574     if( !IsDigit(*z) ) return 0;
575     while( IsDigit(*z) ){ z++; }
576     if( realnum ) *realnum = 1;
577   }
578   if( *z=='e' || *z=='E' ){
579     z++;
580     if( *z=='+' || *z=='-' ) z++;
581     if( !IsDigit(*z) ) return 0;
582     while( IsDigit(*z) ){ z++; }
583     if( realnum ) *realnum = 1;
584   }
585   return *z==0;
586 }
587 
588 /*
589 ** Compute a string length that is limited to what can be stored in
590 ** lower 30 bits of a 32-bit signed integer.
591 */
592 static int strlen30(const char *z){
593   const char *z2 = z;
594   while( *z2 ){ z2++; }
595   return 0x3fffffff & (int)(z2 - z);
596 }
597 
598 /*
599 ** Return the length of a string in characters.  Multibyte UTF8 characters
600 ** count as a single character.
601 */
602 static int strlenChar(const char *z){
603   int n = 0;
604   while( *z ){
605     if( (0xc0&*(z++))!=0x80 ) n++;
606   }
607   return n;
608 }
609 
610 /*
611 ** Return open FILE * if zFile exists, can be opened for read
612 ** and is an ordinary file or a character stream source.
613 ** Otherwise return 0.
614 */
615 static FILE * openChrSource(const char *zFile){
616 #ifdef _WIN32
617   struct _stat x = {0};
618 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
619   /* On Windows, open first, then check the stream nature. This order
620   ** is necessary because _stat() and sibs, when checking a named pipe,
621   ** effectively break the pipe as its supplier sees it. */
622   FILE *rv = fopen(zFile, "rb");
623   if( rv==0 ) return 0;
624   if( _fstat(_fileno(rv), &x) != 0
625       || !STAT_CHR_SRC(x.st_mode)){
626     fclose(rv);
627     rv = 0;
628   }
629   return rv;
630 #else
631   struct stat x = {0};
632   int rc = stat(zFile, &x);
633 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
634   if( rc!=0 ) return 0;
635   if( STAT_CHR_SRC(x.st_mode) ){
636     return fopen(zFile, "rb");
637   }else{
638     return 0;
639   }
640 #endif
641 #undef STAT_CHR_SRC
642 }
643 
644 /*
645 ** This routine reads a line of text from FILE in, stores
646 ** the text in memory obtained from malloc() and returns a pointer
647 ** to the text.  NULL is returned at end of file, or if malloc()
648 ** fails.
649 **
650 ** If zLine is not NULL then it is a malloced buffer returned from
651 ** a previous call to this routine that may be reused.
652 */
653 static char *local_getline(char *zLine, FILE *in){
654   int nLine = zLine==0 ? 0 : 100;
655   int n = 0;
656 
657   while( 1 ){
658     if( n+100>nLine ){
659       nLine = nLine*2 + 100;
660       zLine = realloc(zLine, nLine);
661       shell_check_oom(zLine);
662     }
663     if( fgets(&zLine[n], nLine - n, in)==0 ){
664       if( n==0 ){
665         free(zLine);
666         return 0;
667       }
668       zLine[n] = 0;
669       break;
670     }
671     while( zLine[n] ) n++;
672     if( n>0 && zLine[n-1]=='\n' ){
673       n--;
674       if( n>0 && zLine[n-1]=='\r' ) n--;
675       zLine[n] = 0;
676       break;
677     }
678   }
679 #if defined(_WIN32) || defined(WIN32)
680   /* For interactive input on Windows systems, translate the
681   ** multi-byte characterset characters into UTF-8. */
682   if( stdin_is_interactive && in==stdin ){
683     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
684     if( zTrans ){
685       int nTrans = strlen30(zTrans)+1;
686       if( nTrans>nLine ){
687         zLine = realloc(zLine, nTrans);
688         shell_check_oom(zLine);
689       }
690       memcpy(zLine, zTrans, nTrans);
691       sqlite3_free(zTrans);
692     }
693   }
694 #endif /* defined(_WIN32) || defined(WIN32) */
695   return zLine;
696 }
697 
698 /*
699 ** Retrieve a single line of input text.
700 **
701 ** If in==0 then read from standard input and prompt before each line.
702 ** If isContinuation is true, then a continuation prompt is appropriate.
703 ** If isContinuation is zero, then the main prompt should be used.
704 **
705 ** If zPrior is not NULL then it is a buffer from a prior call to this
706 ** routine that can be reused.
707 **
708 ** The result is stored in space obtained from malloc() and must either
709 ** be freed by the caller or else passed back into this routine via the
710 ** zPrior argument for reuse.
711 */
712 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
713   char *zPrompt;
714   char *zResult;
715   if( in!=0 ){
716     zResult = local_getline(zPrior, in);
717   }else{
718     zPrompt = isContinuation ? continuePrompt : mainPrompt;
719 #if SHELL_USE_LOCAL_GETLINE
720     printf("%s", zPrompt);
721     fflush(stdout);
722     zResult = local_getline(zPrior, stdin);
723 #else
724     free(zPrior);
725     zResult = shell_readline(zPrompt);
726     if( zResult && *zResult ) shell_add_history(zResult);
727 #endif
728   }
729   return zResult;
730 }
731 
732 
733 /*
734 ** Return the value of a hexadecimal digit.  Return -1 if the input
735 ** is not a hex digit.
736 */
737 static int hexDigitValue(char c){
738   if( c>='0' && c<='9' ) return c - '0';
739   if( c>='a' && c<='f' ) return c - 'a' + 10;
740   if( c>='A' && c<='F' ) return c - 'A' + 10;
741   return -1;
742 }
743 
744 /*
745 ** Interpret zArg as an integer value, possibly with suffixes.
746 */
747 static sqlite3_int64 integerValue(const char *zArg){
748   sqlite3_int64 v = 0;
749   static const struct { char *zSuffix; int iMult; } aMult[] = {
750     { "KiB", 1024 },
751     { "MiB", 1024*1024 },
752     { "GiB", 1024*1024*1024 },
753     { "KB",  1000 },
754     { "MB",  1000000 },
755     { "GB",  1000000000 },
756     { "K",   1000 },
757     { "M",   1000000 },
758     { "G",   1000000000 },
759   };
760   int i;
761   int isNeg = 0;
762   if( zArg[0]=='-' ){
763     isNeg = 1;
764     zArg++;
765   }else if( zArg[0]=='+' ){
766     zArg++;
767   }
768   if( zArg[0]=='0' && zArg[1]=='x' ){
769     int x;
770     zArg += 2;
771     while( (x = hexDigitValue(zArg[0]))>=0 ){
772       v = (v<<4) + x;
773       zArg++;
774     }
775   }else{
776     while( IsDigit(zArg[0]) ){
777       v = v*10 + zArg[0] - '0';
778       zArg++;
779     }
780   }
781   for(i=0; i<ArraySize(aMult); i++){
782     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
783       v *= aMult[i].iMult;
784       break;
785     }
786   }
787   return isNeg? -v : v;
788 }
789 
790 /*
791 ** A variable length string to which one can append text.
792 */
793 typedef struct ShellText ShellText;
794 struct ShellText {
795   char *z;
796   int n;
797   int nAlloc;
798 };
799 
800 /*
801 ** Initialize and destroy a ShellText object
802 */
803 static void initText(ShellText *p){
804   memset(p, 0, sizeof(*p));
805 }
806 static void freeText(ShellText *p){
807   free(p->z);
808   initText(p);
809 }
810 
811 /* zIn is either a pointer to a NULL-terminated string in memory obtained
812 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
813 ** added to zIn, and the result returned in memory obtained from malloc().
814 ** zIn, if it was not NULL, is freed.
815 **
816 ** If the third argument, quote, is not '\0', then it is used as a
817 ** quote character for zAppend.
818 */
819 static void appendText(ShellText *p, char const *zAppend, char quote){
820   int len;
821   int i;
822   int nAppend = strlen30(zAppend);
823 
824   len = nAppend+p->n+1;
825   if( quote ){
826     len += 2;
827     for(i=0; i<nAppend; i++){
828       if( zAppend[i]==quote ) len++;
829     }
830   }
831 
832   if( p->z==0 || p->n+len>=p->nAlloc ){
833     p->nAlloc = p->nAlloc*2 + len + 20;
834     p->z = realloc(p->z, p->nAlloc);
835     shell_check_oom(p->z);
836   }
837 
838   if( quote ){
839     char *zCsr = p->z+p->n;
840     *zCsr++ = quote;
841     for(i=0; i<nAppend; i++){
842       *zCsr++ = zAppend[i];
843       if( zAppend[i]==quote ) *zCsr++ = quote;
844     }
845     *zCsr++ = quote;
846     p->n = (int)(zCsr - p->z);
847     *zCsr = '\0';
848   }else{
849     memcpy(p->z+p->n, zAppend, nAppend);
850     p->n += nAppend;
851     p->z[p->n] = '\0';
852   }
853 }
854 
855 /*
856 ** Attempt to determine if identifier zName needs to be quoted, either
857 ** because it contains non-alphanumeric characters, or because it is an
858 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
859 ** that quoting is required.
860 **
861 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
862 */
863 static char quoteChar(const char *zName){
864   int i;
865   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
866   for(i=0; zName[i]; i++){
867     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
868   }
869   return sqlite3_keyword_check(zName, i) ? '"' : 0;
870 }
871 
872 /*
873 ** Construct a fake object name and column list to describe the structure
874 ** of the view, virtual table, or table valued function zSchema.zName.
875 */
876 static char *shellFakeSchema(
877   sqlite3 *db,            /* The database connection containing the vtab */
878   const char *zSchema,    /* Schema of the database holding the vtab */
879   const char *zName       /* The name of the virtual table */
880 ){
881   sqlite3_stmt *pStmt = 0;
882   char *zSql;
883   ShellText s;
884   char cQuote;
885   char *zDiv = "(";
886   int nRow = 0;
887 
888   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
889                          zSchema ? zSchema : "main", zName);
890   shell_check_oom(zSql);
891   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
892   sqlite3_free(zSql);
893   initText(&s);
894   if( zSchema ){
895     cQuote = quoteChar(zSchema);
896     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
897     appendText(&s, zSchema, cQuote);
898     appendText(&s, ".", 0);
899   }
900   cQuote = quoteChar(zName);
901   appendText(&s, zName, cQuote);
902   while( sqlite3_step(pStmt)==SQLITE_ROW ){
903     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
904     nRow++;
905     appendText(&s, zDiv, 0);
906     zDiv = ",";
907     if( zCol==0 ) zCol = "";
908     cQuote = quoteChar(zCol);
909     appendText(&s, zCol, cQuote);
910   }
911   appendText(&s, ")", 0);
912   sqlite3_finalize(pStmt);
913   if( nRow==0 ){
914     freeText(&s);
915     s.z = 0;
916   }
917   return s.z;
918 }
919 
920 /*
921 ** SQL function:  shell_module_schema(X)
922 **
923 ** Return a fake schema for the table-valued function or eponymous virtual
924 ** table X.
925 */
926 static void shellModuleSchema(
927   sqlite3_context *pCtx,
928   int nVal,
929   sqlite3_value **apVal
930 ){
931   const char *zName;
932   char *zFake;
933   UNUSED_PARAMETER(nVal);
934   zName = (const char*)sqlite3_value_text(apVal[0]);
935   zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
936   if( zFake ){
937     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
938                         -1, sqlite3_free);
939     free(zFake);
940   }
941 }
942 
943 /*
944 ** SQL function:  shell_add_schema(S,X)
945 **
946 ** Add the schema name X to the CREATE statement in S and return the result.
947 ** Examples:
948 **
949 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
950 **
951 ** Also works on
952 **
953 **    CREATE INDEX
954 **    CREATE UNIQUE INDEX
955 **    CREATE VIEW
956 **    CREATE TRIGGER
957 **    CREATE VIRTUAL TABLE
958 **
959 ** This UDF is used by the .schema command to insert the schema name of
960 ** attached databases into the middle of the sqlite_schema.sql field.
961 */
962 static void shellAddSchemaName(
963   sqlite3_context *pCtx,
964   int nVal,
965   sqlite3_value **apVal
966 ){
967   static const char *aPrefix[] = {
968      "TABLE",
969      "INDEX",
970      "UNIQUE INDEX",
971      "VIEW",
972      "TRIGGER",
973      "VIRTUAL TABLE"
974   };
975   int i = 0;
976   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
977   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
978   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
979   sqlite3 *db = sqlite3_context_db_handle(pCtx);
980   UNUSED_PARAMETER(nVal);
981   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
982     for(i=0; i<ArraySize(aPrefix); i++){
983       int n = strlen30(aPrefix[i]);
984       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
985         char *z = 0;
986         char *zFake = 0;
987         if( zSchema ){
988           char cQuote = quoteChar(zSchema);
989           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
990             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
991           }else{
992             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
993           }
994         }
995         if( zName
996          && aPrefix[i][0]=='V'
997          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
998         ){
999           if( z==0 ){
1000             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1001           }else{
1002             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1003           }
1004           free(zFake);
1005         }
1006         if( z ){
1007           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1008           return;
1009         }
1010       }
1011     }
1012   }
1013   sqlite3_result_value(pCtx, apVal[0]);
1014 }
1015 
1016 /*
1017 ** The source code for several run-time loadable extensions is inserted
1018 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
1019 ** code, we need to override some macros to make the included program code
1020 ** work here in the middle of this regular program.
1021 */
1022 #define SQLITE_EXTENSION_INIT1
1023 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1024 
1025 #if defined(_WIN32) && defined(_MSC_VER)
1026 /************************* Begin test_windirent.h ******************/
1027 /*
1028 ** 2015 November 30
1029 **
1030 ** The author disclaims copyright to this source code.  In place of
1031 ** a legal notice, here is a blessing:
1032 **
1033 **    May you do good and not evil.
1034 **    May you find forgiveness for yourself and forgive others.
1035 **    May you share freely, never taking more than you give.
1036 **
1037 *************************************************************************
1038 ** This file contains declarations for most of the opendir() family of
1039 ** POSIX functions on Win32 using the MSVCRT.
1040 */
1041 
1042 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1043 #define SQLITE_WINDIRENT_H
1044 
1045 /*
1046 ** We need several data types from the Windows SDK header.
1047 */
1048 
1049 #ifndef WIN32_LEAN_AND_MEAN
1050 #define WIN32_LEAN_AND_MEAN
1051 #endif
1052 
1053 #include "windows.h"
1054 
1055 /*
1056 ** We need several support functions from the SQLite core.
1057 */
1058 
1059 /* #include "sqlite3.h" */
1060 
1061 /*
1062 ** We need several things from the ANSI and MSVCRT headers.
1063 */
1064 
1065 #include <stdio.h>
1066 #include <stdlib.h>
1067 #include <errno.h>
1068 #include <io.h>
1069 #include <limits.h>
1070 #include <sys/types.h>
1071 #include <sys/stat.h>
1072 
1073 /*
1074 ** We may need several defines that should have been in "sys/stat.h".
1075 */
1076 
1077 #ifndef S_ISREG
1078 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1079 #endif
1080 
1081 #ifndef S_ISDIR
1082 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1083 #endif
1084 
1085 #ifndef S_ISLNK
1086 #define S_ISLNK(mode) (0)
1087 #endif
1088 
1089 /*
1090 ** We may need to provide the "mode_t" type.
1091 */
1092 
1093 #ifndef MODE_T_DEFINED
1094   #define MODE_T_DEFINED
1095   typedef unsigned short mode_t;
1096 #endif
1097 
1098 /*
1099 ** We may need to provide the "ino_t" type.
1100 */
1101 
1102 #ifndef INO_T_DEFINED
1103   #define INO_T_DEFINED
1104   typedef unsigned short ino_t;
1105 #endif
1106 
1107 /*
1108 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1109 */
1110 
1111 #ifndef NAME_MAX
1112 #  ifdef FILENAME_MAX
1113 #    define NAME_MAX (FILENAME_MAX)
1114 #  else
1115 #    define NAME_MAX (260)
1116 #  endif
1117 #endif
1118 
1119 /*
1120 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1121 */
1122 
1123 #ifndef NULL_INTPTR_T
1124 #  define NULL_INTPTR_T ((intptr_t)(0))
1125 #endif
1126 
1127 #ifndef BAD_INTPTR_T
1128 #  define BAD_INTPTR_T ((intptr_t)(-1))
1129 #endif
1130 
1131 /*
1132 ** We need to provide the necessary structures and related types.
1133 */
1134 
1135 #ifndef DIRENT_DEFINED
1136 #define DIRENT_DEFINED
1137 typedef struct DIRENT DIRENT;
1138 typedef DIRENT *LPDIRENT;
1139 struct DIRENT {
1140   ino_t d_ino;               /* Sequence number, do not use. */
1141   unsigned d_attributes;     /* Win32 file attributes. */
1142   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1143 };
1144 #endif
1145 
1146 #ifndef DIR_DEFINED
1147 #define DIR_DEFINED
1148 typedef struct DIR DIR;
1149 typedef DIR *LPDIR;
1150 struct DIR {
1151   intptr_t d_handle; /* Value returned by "_findfirst". */
1152   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1153   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1154 };
1155 #endif
1156 
1157 /*
1158 ** Provide a macro, for use by the implementation, to determine if a
1159 ** particular directory entry should be skipped over when searching for
1160 ** the next directory entry that should be returned by the readdir() or
1161 ** readdir_r() functions.
1162 */
1163 
1164 #ifndef is_filtered
1165 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1166 #endif
1167 
1168 /*
1169 ** Provide the function prototype for the POSIX compatiable getenv()
1170 ** function.  This function is not thread-safe.
1171 */
1172 
1173 extern const char *windirent_getenv(const char *name);
1174 
1175 /*
1176 ** Finally, we can provide the function prototypes for the opendir(),
1177 ** readdir(), readdir_r(), and closedir() POSIX functions.
1178 */
1179 
1180 extern LPDIR opendir(const char *dirname);
1181 extern LPDIRENT readdir(LPDIR dirp);
1182 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1183 extern INT closedir(LPDIR dirp);
1184 
1185 #endif /* defined(WIN32) && defined(_MSC_VER) */
1186 
1187 /************************* End test_windirent.h ********************/
1188 /************************* Begin test_windirent.c ******************/
1189 /*
1190 ** 2015 November 30
1191 **
1192 ** The author disclaims copyright to this source code.  In place of
1193 ** a legal notice, here is a blessing:
1194 **
1195 **    May you do good and not evil.
1196 **    May you find forgiveness for yourself and forgive others.
1197 **    May you share freely, never taking more than you give.
1198 **
1199 *************************************************************************
1200 ** This file contains code to implement most of the opendir() family of
1201 ** POSIX functions on Win32 using the MSVCRT.
1202 */
1203 
1204 #if defined(_WIN32) && defined(_MSC_VER)
1205 /* #include "test_windirent.h" */
1206 
1207 /*
1208 ** Implementation of the POSIX getenv() function using the Win32 API.
1209 ** This function is not thread-safe.
1210 */
1211 const char *windirent_getenv(
1212   const char *name
1213 ){
1214   static char value[32768]; /* Maximum length, per MSDN */
1215   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1216   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1217 
1218   memset(value, 0, sizeof(value));
1219   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1220   if( dwRet==0 || dwRet>dwSize ){
1221     /*
1222     ** The function call to GetEnvironmentVariableA() failed -OR-
1223     ** the buffer is not large enough.  Either way, return NULL.
1224     */
1225     return 0;
1226   }else{
1227     /*
1228     ** The function call to GetEnvironmentVariableA() succeeded
1229     ** -AND- the buffer contains the entire value.
1230     */
1231     return value;
1232   }
1233 }
1234 
1235 /*
1236 ** Implementation of the POSIX opendir() function using the MSVCRT.
1237 */
1238 LPDIR opendir(
1239   const char *dirname
1240 ){
1241   struct _finddata_t data;
1242   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1243   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1244 
1245   if( dirp==NULL ) return NULL;
1246   memset(dirp, 0, sizeof(DIR));
1247 
1248   /* TODO: Remove this if Unix-style root paths are not used. */
1249   if( sqlite3_stricmp(dirname, "/")==0 ){
1250     dirname = windirent_getenv("SystemDrive");
1251   }
1252 
1253   memset(&data, 0, sizeof(struct _finddata_t));
1254   _snprintf(data.name, namesize, "%s\\*", dirname);
1255   dirp->d_handle = _findfirst(data.name, &data);
1256 
1257   if( dirp->d_handle==BAD_INTPTR_T ){
1258     closedir(dirp);
1259     return NULL;
1260   }
1261 
1262   /* TODO: Remove this block to allow hidden and/or system files. */
1263   if( is_filtered(data) ){
1264 next:
1265 
1266     memset(&data, 0, sizeof(struct _finddata_t));
1267     if( _findnext(dirp->d_handle, &data)==-1 ){
1268       closedir(dirp);
1269       return NULL;
1270     }
1271 
1272     /* TODO: Remove this block to allow hidden and/or system files. */
1273     if( is_filtered(data) ) goto next;
1274   }
1275 
1276   dirp->d_first.d_attributes = data.attrib;
1277   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1278   dirp->d_first.d_name[NAME_MAX] = '\0';
1279 
1280   return dirp;
1281 }
1282 
1283 /*
1284 ** Implementation of the POSIX readdir() function using the MSVCRT.
1285 */
1286 LPDIRENT readdir(
1287   LPDIR dirp
1288 ){
1289   struct _finddata_t data;
1290 
1291   if( dirp==NULL ) return NULL;
1292 
1293   if( dirp->d_first.d_ino==0 ){
1294     dirp->d_first.d_ino++;
1295     dirp->d_next.d_ino++;
1296 
1297     return &dirp->d_first;
1298   }
1299 
1300 next:
1301 
1302   memset(&data, 0, sizeof(struct _finddata_t));
1303   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1304 
1305   /* TODO: Remove this block to allow hidden and/or system files. */
1306   if( is_filtered(data) ) goto next;
1307 
1308   dirp->d_next.d_ino++;
1309   dirp->d_next.d_attributes = data.attrib;
1310   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1311   dirp->d_next.d_name[NAME_MAX] = '\0';
1312 
1313   return &dirp->d_next;
1314 }
1315 
1316 /*
1317 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1318 */
1319 INT readdir_r(
1320   LPDIR dirp,
1321   LPDIRENT entry,
1322   LPDIRENT *result
1323 ){
1324   struct _finddata_t data;
1325 
1326   if( dirp==NULL ) return EBADF;
1327 
1328   if( dirp->d_first.d_ino==0 ){
1329     dirp->d_first.d_ino++;
1330     dirp->d_next.d_ino++;
1331 
1332     entry->d_ino = dirp->d_first.d_ino;
1333     entry->d_attributes = dirp->d_first.d_attributes;
1334     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1335     entry->d_name[NAME_MAX] = '\0';
1336 
1337     *result = entry;
1338     return 0;
1339   }
1340 
1341 next:
1342 
1343   memset(&data, 0, sizeof(struct _finddata_t));
1344   if( _findnext(dirp->d_handle, &data)==-1 ){
1345     *result = NULL;
1346     return ENOENT;
1347   }
1348 
1349   /* TODO: Remove this block to allow hidden and/or system files. */
1350   if( is_filtered(data) ) goto next;
1351 
1352   entry->d_ino = (ino_t)-1; /* not available */
1353   entry->d_attributes = data.attrib;
1354   strncpy(entry->d_name, data.name, NAME_MAX);
1355   entry->d_name[NAME_MAX] = '\0';
1356 
1357   *result = entry;
1358   return 0;
1359 }
1360 
1361 /*
1362 ** Implementation of the POSIX closedir() function using the MSVCRT.
1363 */
1364 INT closedir(
1365   LPDIR dirp
1366 ){
1367   INT result = 0;
1368 
1369   if( dirp==NULL ) return EINVAL;
1370 
1371   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1372     result = _findclose(dirp->d_handle);
1373   }
1374 
1375   sqlite3_free(dirp);
1376   return result;
1377 }
1378 
1379 #endif /* defined(WIN32) && defined(_MSC_VER) */
1380 
1381 /************************* End test_windirent.c ********************/
1382 #define dirent DIRENT
1383 #endif
1384 /************************* Begin ../ext/misc/shathree.c ******************/
1385 /*
1386 ** 2017-03-08
1387 **
1388 ** The author disclaims copyright to this source code.  In place of
1389 ** a legal notice, here is a blessing:
1390 **
1391 **    May you do good and not evil.
1392 **    May you find forgiveness for yourself and forgive others.
1393 **    May you share freely, never taking more than you give.
1394 **
1395 ******************************************************************************
1396 **
1397 ** This SQLite extension implements functions that compute SHA3 hashes.
1398 ** Two SQL functions are implemented:
1399 **
1400 **     sha3(X,SIZE)
1401 **     sha3_query(Y,SIZE)
1402 **
1403 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1404 ** X is NULL.
1405 **
1406 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1407 ** and returns a hash of their results.
1408 **
1409 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1410 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1411 ** 384, or 512, to determine SHA3 hash variant that is computed.
1412 */
1413 /* #include "sqlite3ext.h" */
1414 SQLITE_EXTENSION_INIT1
1415 #include <assert.h>
1416 #include <string.h>
1417 #include <stdarg.h>
1418 
1419 #ifndef SQLITE_AMALGAMATION
1420 /* typedef sqlite3_uint64 u64; */
1421 #endif /* SQLITE_AMALGAMATION */
1422 
1423 /******************************************************************************
1424 ** The Hash Engine
1425 */
1426 /*
1427 ** Macros to determine whether the machine is big or little endian,
1428 ** and whether or not that determination is run-time or compile-time.
1429 **
1430 ** For best performance, an attempt is made to guess at the byte-order
1431 ** using C-preprocessor macros.  If that is unsuccessful, or if
1432 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1433 ** at run-time.
1434 */
1435 #ifndef SHA3_BYTEORDER
1436 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1437      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1438      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1439      defined(__arm__)
1440 #   define SHA3_BYTEORDER    1234
1441 # elif defined(sparc)    || defined(__ppc__)
1442 #   define SHA3_BYTEORDER    4321
1443 # else
1444 #   define SHA3_BYTEORDER 0
1445 # endif
1446 #endif
1447 
1448 
1449 /*
1450 ** State structure for a SHA3 hash in progress
1451 */
1452 typedef struct SHA3Context SHA3Context;
1453 struct SHA3Context {
1454   union {
1455     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1456     unsigned char x[1600];    /* ... or 1600 bytes */
1457   } u;
1458   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1459   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1460   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1461 };
1462 
1463 /*
1464 ** A single step of the Keccak mixing function for a 1600-bit state
1465 */
1466 static void KeccakF1600Step(SHA3Context *p){
1467   int i;
1468   u64 b0, b1, b2, b3, b4;
1469   u64 c0, c1, c2, c3, c4;
1470   u64 d0, d1, d2, d3, d4;
1471   static const u64 RC[] = {
1472     0x0000000000000001ULL,  0x0000000000008082ULL,
1473     0x800000000000808aULL,  0x8000000080008000ULL,
1474     0x000000000000808bULL,  0x0000000080000001ULL,
1475     0x8000000080008081ULL,  0x8000000000008009ULL,
1476     0x000000000000008aULL,  0x0000000000000088ULL,
1477     0x0000000080008009ULL,  0x000000008000000aULL,
1478     0x000000008000808bULL,  0x800000000000008bULL,
1479     0x8000000000008089ULL,  0x8000000000008003ULL,
1480     0x8000000000008002ULL,  0x8000000000000080ULL,
1481     0x000000000000800aULL,  0x800000008000000aULL,
1482     0x8000000080008081ULL,  0x8000000000008080ULL,
1483     0x0000000080000001ULL,  0x8000000080008008ULL
1484   };
1485 # define a00 (p->u.s[0])
1486 # define a01 (p->u.s[1])
1487 # define a02 (p->u.s[2])
1488 # define a03 (p->u.s[3])
1489 # define a04 (p->u.s[4])
1490 # define a10 (p->u.s[5])
1491 # define a11 (p->u.s[6])
1492 # define a12 (p->u.s[7])
1493 # define a13 (p->u.s[8])
1494 # define a14 (p->u.s[9])
1495 # define a20 (p->u.s[10])
1496 # define a21 (p->u.s[11])
1497 # define a22 (p->u.s[12])
1498 # define a23 (p->u.s[13])
1499 # define a24 (p->u.s[14])
1500 # define a30 (p->u.s[15])
1501 # define a31 (p->u.s[16])
1502 # define a32 (p->u.s[17])
1503 # define a33 (p->u.s[18])
1504 # define a34 (p->u.s[19])
1505 # define a40 (p->u.s[20])
1506 # define a41 (p->u.s[21])
1507 # define a42 (p->u.s[22])
1508 # define a43 (p->u.s[23])
1509 # define a44 (p->u.s[24])
1510 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1511 
1512   for(i=0; i<24; i+=4){
1513     c0 = a00^a10^a20^a30^a40;
1514     c1 = a01^a11^a21^a31^a41;
1515     c2 = a02^a12^a22^a32^a42;
1516     c3 = a03^a13^a23^a33^a43;
1517     c4 = a04^a14^a24^a34^a44;
1518     d0 = c4^ROL64(c1, 1);
1519     d1 = c0^ROL64(c2, 1);
1520     d2 = c1^ROL64(c3, 1);
1521     d3 = c2^ROL64(c4, 1);
1522     d4 = c3^ROL64(c0, 1);
1523 
1524     b0 = (a00^d0);
1525     b1 = ROL64((a11^d1), 44);
1526     b2 = ROL64((a22^d2), 43);
1527     b3 = ROL64((a33^d3), 21);
1528     b4 = ROL64((a44^d4), 14);
1529     a00 =   b0 ^((~b1)&  b2 );
1530     a00 ^= RC[i];
1531     a11 =   b1 ^((~b2)&  b3 );
1532     a22 =   b2 ^((~b3)&  b4 );
1533     a33 =   b3 ^((~b4)&  b0 );
1534     a44 =   b4 ^((~b0)&  b1 );
1535 
1536     b2 = ROL64((a20^d0), 3);
1537     b3 = ROL64((a31^d1), 45);
1538     b4 = ROL64((a42^d2), 61);
1539     b0 = ROL64((a03^d3), 28);
1540     b1 = ROL64((a14^d4), 20);
1541     a20 =   b0 ^((~b1)&  b2 );
1542     a31 =   b1 ^((~b2)&  b3 );
1543     a42 =   b2 ^((~b3)&  b4 );
1544     a03 =   b3 ^((~b4)&  b0 );
1545     a14 =   b4 ^((~b0)&  b1 );
1546 
1547     b4 = ROL64((a40^d0), 18);
1548     b0 = ROL64((a01^d1), 1);
1549     b1 = ROL64((a12^d2), 6);
1550     b2 = ROL64((a23^d3), 25);
1551     b3 = ROL64((a34^d4), 8);
1552     a40 =   b0 ^((~b1)&  b2 );
1553     a01 =   b1 ^((~b2)&  b3 );
1554     a12 =   b2 ^((~b3)&  b4 );
1555     a23 =   b3 ^((~b4)&  b0 );
1556     a34 =   b4 ^((~b0)&  b1 );
1557 
1558     b1 = ROL64((a10^d0), 36);
1559     b2 = ROL64((a21^d1), 10);
1560     b3 = ROL64((a32^d2), 15);
1561     b4 = ROL64((a43^d3), 56);
1562     b0 = ROL64((a04^d4), 27);
1563     a10 =   b0 ^((~b1)&  b2 );
1564     a21 =   b1 ^((~b2)&  b3 );
1565     a32 =   b2 ^((~b3)&  b4 );
1566     a43 =   b3 ^((~b4)&  b0 );
1567     a04 =   b4 ^((~b0)&  b1 );
1568 
1569     b3 = ROL64((a30^d0), 41);
1570     b4 = ROL64((a41^d1), 2);
1571     b0 = ROL64((a02^d2), 62);
1572     b1 = ROL64((a13^d3), 55);
1573     b2 = ROL64((a24^d4), 39);
1574     a30 =   b0 ^((~b1)&  b2 );
1575     a41 =   b1 ^((~b2)&  b3 );
1576     a02 =   b2 ^((~b3)&  b4 );
1577     a13 =   b3 ^((~b4)&  b0 );
1578     a24 =   b4 ^((~b0)&  b1 );
1579 
1580     c0 = a00^a20^a40^a10^a30;
1581     c1 = a11^a31^a01^a21^a41;
1582     c2 = a22^a42^a12^a32^a02;
1583     c3 = a33^a03^a23^a43^a13;
1584     c4 = a44^a14^a34^a04^a24;
1585     d0 = c4^ROL64(c1, 1);
1586     d1 = c0^ROL64(c2, 1);
1587     d2 = c1^ROL64(c3, 1);
1588     d3 = c2^ROL64(c4, 1);
1589     d4 = c3^ROL64(c0, 1);
1590 
1591     b0 = (a00^d0);
1592     b1 = ROL64((a31^d1), 44);
1593     b2 = ROL64((a12^d2), 43);
1594     b3 = ROL64((a43^d3), 21);
1595     b4 = ROL64((a24^d4), 14);
1596     a00 =   b0 ^((~b1)&  b2 );
1597     a00 ^= RC[i+1];
1598     a31 =   b1 ^((~b2)&  b3 );
1599     a12 =   b2 ^((~b3)&  b4 );
1600     a43 =   b3 ^((~b4)&  b0 );
1601     a24 =   b4 ^((~b0)&  b1 );
1602 
1603     b2 = ROL64((a40^d0), 3);
1604     b3 = ROL64((a21^d1), 45);
1605     b4 = ROL64((a02^d2), 61);
1606     b0 = ROL64((a33^d3), 28);
1607     b1 = ROL64((a14^d4), 20);
1608     a40 =   b0 ^((~b1)&  b2 );
1609     a21 =   b1 ^((~b2)&  b3 );
1610     a02 =   b2 ^((~b3)&  b4 );
1611     a33 =   b3 ^((~b4)&  b0 );
1612     a14 =   b4 ^((~b0)&  b1 );
1613 
1614     b4 = ROL64((a30^d0), 18);
1615     b0 = ROL64((a11^d1), 1);
1616     b1 = ROL64((a42^d2), 6);
1617     b2 = ROL64((a23^d3), 25);
1618     b3 = ROL64((a04^d4), 8);
1619     a30 =   b0 ^((~b1)&  b2 );
1620     a11 =   b1 ^((~b2)&  b3 );
1621     a42 =   b2 ^((~b3)&  b4 );
1622     a23 =   b3 ^((~b4)&  b0 );
1623     a04 =   b4 ^((~b0)&  b1 );
1624 
1625     b1 = ROL64((a20^d0), 36);
1626     b2 = ROL64((a01^d1), 10);
1627     b3 = ROL64((a32^d2), 15);
1628     b4 = ROL64((a13^d3), 56);
1629     b0 = ROL64((a44^d4), 27);
1630     a20 =   b0 ^((~b1)&  b2 );
1631     a01 =   b1 ^((~b2)&  b3 );
1632     a32 =   b2 ^((~b3)&  b4 );
1633     a13 =   b3 ^((~b4)&  b0 );
1634     a44 =   b4 ^((~b0)&  b1 );
1635 
1636     b3 = ROL64((a10^d0), 41);
1637     b4 = ROL64((a41^d1), 2);
1638     b0 = ROL64((a22^d2), 62);
1639     b1 = ROL64((a03^d3), 55);
1640     b2 = ROL64((a34^d4), 39);
1641     a10 =   b0 ^((~b1)&  b2 );
1642     a41 =   b1 ^((~b2)&  b3 );
1643     a22 =   b2 ^((~b3)&  b4 );
1644     a03 =   b3 ^((~b4)&  b0 );
1645     a34 =   b4 ^((~b0)&  b1 );
1646 
1647     c0 = a00^a40^a30^a20^a10;
1648     c1 = a31^a21^a11^a01^a41;
1649     c2 = a12^a02^a42^a32^a22;
1650     c3 = a43^a33^a23^a13^a03;
1651     c4 = a24^a14^a04^a44^a34;
1652     d0 = c4^ROL64(c1, 1);
1653     d1 = c0^ROL64(c2, 1);
1654     d2 = c1^ROL64(c3, 1);
1655     d3 = c2^ROL64(c4, 1);
1656     d4 = c3^ROL64(c0, 1);
1657 
1658     b0 = (a00^d0);
1659     b1 = ROL64((a21^d1), 44);
1660     b2 = ROL64((a42^d2), 43);
1661     b3 = ROL64((a13^d3), 21);
1662     b4 = ROL64((a34^d4), 14);
1663     a00 =   b0 ^((~b1)&  b2 );
1664     a00 ^= RC[i+2];
1665     a21 =   b1 ^((~b2)&  b3 );
1666     a42 =   b2 ^((~b3)&  b4 );
1667     a13 =   b3 ^((~b4)&  b0 );
1668     a34 =   b4 ^((~b0)&  b1 );
1669 
1670     b2 = ROL64((a30^d0), 3);
1671     b3 = ROL64((a01^d1), 45);
1672     b4 = ROL64((a22^d2), 61);
1673     b0 = ROL64((a43^d3), 28);
1674     b1 = ROL64((a14^d4), 20);
1675     a30 =   b0 ^((~b1)&  b2 );
1676     a01 =   b1 ^((~b2)&  b3 );
1677     a22 =   b2 ^((~b3)&  b4 );
1678     a43 =   b3 ^((~b4)&  b0 );
1679     a14 =   b4 ^((~b0)&  b1 );
1680 
1681     b4 = ROL64((a10^d0), 18);
1682     b0 = ROL64((a31^d1), 1);
1683     b1 = ROL64((a02^d2), 6);
1684     b2 = ROL64((a23^d3), 25);
1685     b3 = ROL64((a44^d4), 8);
1686     a10 =   b0 ^((~b1)&  b2 );
1687     a31 =   b1 ^((~b2)&  b3 );
1688     a02 =   b2 ^((~b3)&  b4 );
1689     a23 =   b3 ^((~b4)&  b0 );
1690     a44 =   b4 ^((~b0)&  b1 );
1691 
1692     b1 = ROL64((a40^d0), 36);
1693     b2 = ROL64((a11^d1), 10);
1694     b3 = ROL64((a32^d2), 15);
1695     b4 = ROL64((a03^d3), 56);
1696     b0 = ROL64((a24^d4), 27);
1697     a40 =   b0 ^((~b1)&  b2 );
1698     a11 =   b1 ^((~b2)&  b3 );
1699     a32 =   b2 ^((~b3)&  b4 );
1700     a03 =   b3 ^((~b4)&  b0 );
1701     a24 =   b4 ^((~b0)&  b1 );
1702 
1703     b3 = ROL64((a20^d0), 41);
1704     b4 = ROL64((a41^d1), 2);
1705     b0 = ROL64((a12^d2), 62);
1706     b1 = ROL64((a33^d3), 55);
1707     b2 = ROL64((a04^d4), 39);
1708     a20 =   b0 ^((~b1)&  b2 );
1709     a41 =   b1 ^((~b2)&  b3 );
1710     a12 =   b2 ^((~b3)&  b4 );
1711     a33 =   b3 ^((~b4)&  b0 );
1712     a04 =   b4 ^((~b0)&  b1 );
1713 
1714     c0 = a00^a30^a10^a40^a20;
1715     c1 = a21^a01^a31^a11^a41;
1716     c2 = a42^a22^a02^a32^a12;
1717     c3 = a13^a43^a23^a03^a33;
1718     c4 = a34^a14^a44^a24^a04;
1719     d0 = c4^ROL64(c1, 1);
1720     d1 = c0^ROL64(c2, 1);
1721     d2 = c1^ROL64(c3, 1);
1722     d3 = c2^ROL64(c4, 1);
1723     d4 = c3^ROL64(c0, 1);
1724 
1725     b0 = (a00^d0);
1726     b1 = ROL64((a01^d1), 44);
1727     b2 = ROL64((a02^d2), 43);
1728     b3 = ROL64((a03^d3), 21);
1729     b4 = ROL64((a04^d4), 14);
1730     a00 =   b0 ^((~b1)&  b2 );
1731     a00 ^= RC[i+3];
1732     a01 =   b1 ^((~b2)&  b3 );
1733     a02 =   b2 ^((~b3)&  b4 );
1734     a03 =   b3 ^((~b4)&  b0 );
1735     a04 =   b4 ^((~b0)&  b1 );
1736 
1737     b2 = ROL64((a10^d0), 3);
1738     b3 = ROL64((a11^d1), 45);
1739     b4 = ROL64((a12^d2), 61);
1740     b0 = ROL64((a13^d3), 28);
1741     b1 = ROL64((a14^d4), 20);
1742     a10 =   b0 ^((~b1)&  b2 );
1743     a11 =   b1 ^((~b2)&  b3 );
1744     a12 =   b2 ^((~b3)&  b4 );
1745     a13 =   b3 ^((~b4)&  b0 );
1746     a14 =   b4 ^((~b0)&  b1 );
1747 
1748     b4 = ROL64((a20^d0), 18);
1749     b0 = ROL64((a21^d1), 1);
1750     b1 = ROL64((a22^d2), 6);
1751     b2 = ROL64((a23^d3), 25);
1752     b3 = ROL64((a24^d4), 8);
1753     a20 =   b0 ^((~b1)&  b2 );
1754     a21 =   b1 ^((~b2)&  b3 );
1755     a22 =   b2 ^((~b3)&  b4 );
1756     a23 =   b3 ^((~b4)&  b0 );
1757     a24 =   b4 ^((~b0)&  b1 );
1758 
1759     b1 = ROL64((a30^d0), 36);
1760     b2 = ROL64((a31^d1), 10);
1761     b3 = ROL64((a32^d2), 15);
1762     b4 = ROL64((a33^d3), 56);
1763     b0 = ROL64((a34^d4), 27);
1764     a30 =   b0 ^((~b1)&  b2 );
1765     a31 =   b1 ^((~b2)&  b3 );
1766     a32 =   b2 ^((~b3)&  b4 );
1767     a33 =   b3 ^((~b4)&  b0 );
1768     a34 =   b4 ^((~b0)&  b1 );
1769 
1770     b3 = ROL64((a40^d0), 41);
1771     b4 = ROL64((a41^d1), 2);
1772     b0 = ROL64((a42^d2), 62);
1773     b1 = ROL64((a43^d3), 55);
1774     b2 = ROL64((a44^d4), 39);
1775     a40 =   b0 ^((~b1)&  b2 );
1776     a41 =   b1 ^((~b2)&  b3 );
1777     a42 =   b2 ^((~b3)&  b4 );
1778     a43 =   b3 ^((~b4)&  b0 );
1779     a44 =   b4 ^((~b0)&  b1 );
1780   }
1781 }
1782 
1783 /*
1784 ** Initialize a new hash.  iSize determines the size of the hash
1785 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1786 ** can be zero to use the default hash size of 256 bits.
1787 */
1788 static void SHA3Init(SHA3Context *p, int iSize){
1789   memset(p, 0, sizeof(*p));
1790   if( iSize>=128 && iSize<=512 ){
1791     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1792   }else{
1793     p->nRate = (1600 - 2*256)/8;
1794   }
1795 #if SHA3_BYTEORDER==1234
1796   /* Known to be little-endian at compile-time. No-op */
1797 #elif SHA3_BYTEORDER==4321
1798   p->ixMask = 7;  /* Big-endian */
1799 #else
1800   {
1801     static unsigned int one = 1;
1802     if( 1==*(unsigned char*)&one ){
1803       /* Little endian.  No byte swapping. */
1804       p->ixMask = 0;
1805     }else{
1806       /* Big endian.  Byte swap. */
1807       p->ixMask = 7;
1808     }
1809   }
1810 #endif
1811 }
1812 
1813 /*
1814 ** Make consecutive calls to the SHA3Update function to add new content
1815 ** to the hash
1816 */
1817 static void SHA3Update(
1818   SHA3Context *p,
1819   const unsigned char *aData,
1820   unsigned int nData
1821 ){
1822   unsigned int i = 0;
1823   if( aData==0 ) return;
1824 #if SHA3_BYTEORDER==1234
1825   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1826     for(; i+7<nData; i+=8){
1827       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1828       p->nLoaded += 8;
1829       if( p->nLoaded>=p->nRate ){
1830         KeccakF1600Step(p);
1831         p->nLoaded = 0;
1832       }
1833     }
1834   }
1835 #endif
1836   for(; i<nData; i++){
1837 #if SHA3_BYTEORDER==1234
1838     p->u.x[p->nLoaded] ^= aData[i];
1839 #elif SHA3_BYTEORDER==4321
1840     p->u.x[p->nLoaded^0x07] ^= aData[i];
1841 #else
1842     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1843 #endif
1844     p->nLoaded++;
1845     if( p->nLoaded==p->nRate ){
1846       KeccakF1600Step(p);
1847       p->nLoaded = 0;
1848     }
1849   }
1850 }
1851 
1852 /*
1853 ** After all content has been added, invoke SHA3Final() to compute
1854 ** the final hash.  The function returns a pointer to the binary
1855 ** hash value.
1856 */
1857 static unsigned char *SHA3Final(SHA3Context *p){
1858   unsigned int i;
1859   if( p->nLoaded==p->nRate-1 ){
1860     const unsigned char c1 = 0x86;
1861     SHA3Update(p, &c1, 1);
1862   }else{
1863     const unsigned char c2 = 0x06;
1864     const unsigned char c3 = 0x80;
1865     SHA3Update(p, &c2, 1);
1866     p->nLoaded = p->nRate - 1;
1867     SHA3Update(p, &c3, 1);
1868   }
1869   for(i=0; i<p->nRate; i++){
1870     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1871   }
1872   return &p->u.x[p->nRate];
1873 }
1874 /* End of the hashing logic
1875 *****************************************************************************/
1876 
1877 /*
1878 ** Implementation of the sha3(X,SIZE) function.
1879 **
1880 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1881 ** size is 256.  If X is a BLOB, it is hashed as is.
1882 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1883 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1884 ** of a NULL value is NULL.
1885 */
1886 static void sha3Func(
1887   sqlite3_context *context,
1888   int argc,
1889   sqlite3_value **argv
1890 ){
1891   SHA3Context cx;
1892   int eType = sqlite3_value_type(argv[0]);
1893   int nByte = sqlite3_value_bytes(argv[0]);
1894   int iSize;
1895   if( argc==1 ){
1896     iSize = 256;
1897   }else{
1898     iSize = sqlite3_value_int(argv[1]);
1899     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1900       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1901                                     "384 512", -1);
1902       return;
1903     }
1904   }
1905   if( eType==SQLITE_NULL ) return;
1906   SHA3Init(&cx, iSize);
1907   if( eType==SQLITE_BLOB ){
1908     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1909   }else{
1910     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1911   }
1912   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1913 }
1914 
1915 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1916 ** of 50 bytes and add it to the hash.
1917 */
1918 static void hash_step_vformat(
1919   SHA3Context *p,                 /* Add content to this context */
1920   const char *zFormat,
1921   ...
1922 ){
1923   va_list ap;
1924   int n;
1925   char zBuf[50];
1926   va_start(ap, zFormat);
1927   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1928   va_end(ap);
1929   n = (int)strlen(zBuf);
1930   SHA3Update(p, (unsigned char*)zBuf, n);
1931 }
1932 
1933 /*
1934 ** Implementation of the sha3_query(SQL,SIZE) function.
1935 **
1936 ** This function compiles and runs the SQL statement(s) given in the
1937 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1938 ** size is 256.
1939 **
1940 ** The format of the byte stream that is hashed is summarized as follows:
1941 **
1942 **       S<n>:<sql>
1943 **       R
1944 **       N
1945 **       I<int>
1946 **       F<ieee-float>
1947 **       B<size>:<bytes>
1948 **       T<size>:<text>
1949 **
1950 ** <sql> is the original SQL text for each statement run and <n> is
1951 ** the size of that text.  The SQL text is UTF-8.  A single R character
1952 ** occurs before the start of each row.  N means a NULL value.
1953 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1954 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1955 ** B means blobs of <size> bytes.  T means text rendered as <size>
1956 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1957 ** text integers.
1958 **
1959 ** For each SQL statement in the X input, there is one S segment.  Each
1960 ** S segment is followed by zero or more R segments, one for each row in the
1961 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1962 ** one for each column in the result set.  Segments are concatentated directly
1963 ** with no delimiters of any kind.
1964 */
1965 static void sha3QueryFunc(
1966   sqlite3_context *context,
1967   int argc,
1968   sqlite3_value **argv
1969 ){
1970   sqlite3 *db = sqlite3_context_db_handle(context);
1971   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1972   sqlite3_stmt *pStmt = 0;
1973   int nCol;                   /* Number of columns in the result set */
1974   int i;                      /* Loop counter */
1975   int rc;
1976   int n;
1977   const char *z;
1978   SHA3Context cx;
1979   int iSize;
1980 
1981   if( argc==1 ){
1982     iSize = 256;
1983   }else{
1984     iSize = sqlite3_value_int(argv[1]);
1985     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1986       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1987                                     "384 512", -1);
1988       return;
1989     }
1990   }
1991   if( zSql==0 ) return;
1992   SHA3Init(&cx, iSize);
1993   while( zSql[0] ){
1994     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1995     if( rc ){
1996       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1997                                    zSql, sqlite3_errmsg(db));
1998       sqlite3_finalize(pStmt);
1999       sqlite3_result_error(context, zMsg, -1);
2000       sqlite3_free(zMsg);
2001       return;
2002     }
2003     if( !sqlite3_stmt_readonly(pStmt) ){
2004       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2005       sqlite3_finalize(pStmt);
2006       sqlite3_result_error(context, zMsg, -1);
2007       sqlite3_free(zMsg);
2008       return;
2009     }
2010     nCol = sqlite3_column_count(pStmt);
2011     z = sqlite3_sql(pStmt);
2012     if( z ){
2013       n = (int)strlen(z);
2014       hash_step_vformat(&cx,"S%d:",n);
2015       SHA3Update(&cx,(unsigned char*)z,n);
2016     }
2017 
2018     /* Compute a hash over the result of the query */
2019     while( SQLITE_ROW==sqlite3_step(pStmt) ){
2020       SHA3Update(&cx,(const unsigned char*)"R",1);
2021       for(i=0; i<nCol; i++){
2022         switch( sqlite3_column_type(pStmt,i) ){
2023           case SQLITE_NULL: {
2024             SHA3Update(&cx, (const unsigned char*)"N",1);
2025             break;
2026           }
2027           case SQLITE_INTEGER: {
2028             sqlite3_uint64 u;
2029             int j;
2030             unsigned char x[9];
2031             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2032             memcpy(&u, &v, 8);
2033             for(j=8; j>=1; j--){
2034               x[j] = u & 0xff;
2035               u >>= 8;
2036             }
2037             x[0] = 'I';
2038             SHA3Update(&cx, x, 9);
2039             break;
2040           }
2041           case SQLITE_FLOAT: {
2042             sqlite3_uint64 u;
2043             int j;
2044             unsigned char x[9];
2045             double r = sqlite3_column_double(pStmt,i);
2046             memcpy(&u, &r, 8);
2047             for(j=8; j>=1; j--){
2048               x[j] = u & 0xff;
2049               u >>= 8;
2050             }
2051             x[0] = 'F';
2052             SHA3Update(&cx,x,9);
2053             break;
2054           }
2055           case SQLITE_TEXT: {
2056             int n2 = sqlite3_column_bytes(pStmt, i);
2057             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2058             hash_step_vformat(&cx,"T%d:",n2);
2059             SHA3Update(&cx, z2, n2);
2060             break;
2061           }
2062           case SQLITE_BLOB: {
2063             int n2 = sqlite3_column_bytes(pStmt, i);
2064             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2065             hash_step_vformat(&cx,"B%d:",n2);
2066             SHA3Update(&cx, z2, n2);
2067             break;
2068           }
2069         }
2070       }
2071     }
2072     sqlite3_finalize(pStmt);
2073   }
2074   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2075 }
2076 
2077 
2078 #ifdef _WIN32
2079 
2080 #endif
2081 int sqlite3_shathree_init(
2082   sqlite3 *db,
2083   char **pzErrMsg,
2084   const sqlite3_api_routines *pApi
2085 ){
2086   int rc = SQLITE_OK;
2087   SQLITE_EXTENSION_INIT2(pApi);
2088   (void)pzErrMsg;  /* Unused parameter */
2089   rc = sqlite3_create_function(db, "sha3", 1,
2090                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2091                       0, sha3Func, 0, 0);
2092   if( rc==SQLITE_OK ){
2093     rc = sqlite3_create_function(db, "sha3", 2,
2094                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2095                       0, sha3Func, 0, 0);
2096   }
2097   if( rc==SQLITE_OK ){
2098     rc = sqlite3_create_function(db, "sha3_query", 1,
2099                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2100                       0, sha3QueryFunc, 0, 0);
2101   }
2102   if( rc==SQLITE_OK ){
2103     rc = sqlite3_create_function(db, "sha3_query", 2,
2104                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2105                       0, sha3QueryFunc, 0, 0);
2106   }
2107   return rc;
2108 }
2109 
2110 /************************* End ../ext/misc/shathree.c ********************/
2111 /************************* Begin ../ext/misc/fileio.c ******************/
2112 /*
2113 ** 2014-06-13
2114 **
2115 ** The author disclaims copyright to this source code.  In place of
2116 ** a legal notice, here is a blessing:
2117 **
2118 **    May you do good and not evil.
2119 **    May you find forgiveness for yourself and forgive others.
2120 **    May you share freely, never taking more than you give.
2121 **
2122 ******************************************************************************
2123 **
2124 ** This SQLite extension implements SQL functions readfile() and
2125 ** writefile(), and eponymous virtual type "fsdir".
2126 **
2127 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2128 **
2129 **   If neither of the optional arguments is present, then this UDF
2130 **   function writes blob DATA to file FILE. If successful, the number
2131 **   of bytes written is returned. If an error occurs, NULL is returned.
2132 **
2133 **   If the first option argument - MODE - is present, then it must
2134 **   be passed an integer value that corresponds to a POSIX mode
2135 **   value (file type + permissions, as returned in the stat.st_mode
2136 **   field by the stat() system call). Three types of files may
2137 **   be written/created:
2138 **
2139 **     regular files:  (mode & 0170000)==0100000
2140 **     symbolic links: (mode & 0170000)==0120000
2141 **     directories:    (mode & 0170000)==0040000
2142 **
2143 **   For a directory, the DATA is ignored. For a symbolic link, it is
2144 **   interpreted as text and used as the target of the link. For a
2145 **   regular file, it is interpreted as a blob and written into the
2146 **   named file. Regardless of the type of file, its permissions are
2147 **   set to (mode & 0777) before returning.
2148 **
2149 **   If the optional MTIME argument is present, then it is interpreted
2150 **   as an integer - the number of seconds since the unix epoch. The
2151 **   modification-time of the target file is set to this value before
2152 **   returning.
2153 **
2154 **   If three or more arguments are passed to this function and an
2155 **   error is encountered, an exception is raised.
2156 **
2157 ** READFILE(FILE):
2158 **
2159 **   Read and return the contents of file FILE (type blob) from disk.
2160 **
2161 ** FSDIR:
2162 **
2163 **   Used as follows:
2164 **
2165 **     SELECT * FROM fsdir($path [, $dir]);
2166 **
2167 **   Parameter $path is an absolute or relative pathname. If the file that it
2168 **   refers to does not exist, it is an error. If the path refers to a regular
2169 **   file or symbolic link, it returns a single row. Or, if the path refers
2170 **   to a directory, it returns one row for the directory, and one row for each
2171 **   file within the hierarchy rooted at $path.
2172 **
2173 **   Each row has the following columns:
2174 **
2175 **     name:  Path to file or directory (text value).
2176 **     mode:  Value of stat.st_mode for directory entry (an integer).
2177 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2178 **     data:  For a regular file, a blob containing the file data. For a
2179 **            symlink, a text value containing the text of the link. For a
2180 **            directory, NULL.
2181 **
2182 **   If a non-NULL value is specified for the optional $dir parameter and
2183 **   $path is a relative path, then $path is interpreted relative to $dir.
2184 **   And the paths returned in the "name" column of the table are also
2185 **   relative to directory $dir.
2186 **
2187 ** Notes on building this extension for Windows:
2188 **   Unless linked statically with the SQLite library, a preprocessor
2189 **   symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2190 **   DLL form of this extension for WIN32. See its use below for details.
2191 */
2192 /* #include "sqlite3ext.h" */
2193 SQLITE_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 */
2249 static 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 */
2291 static 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 */
2307 static 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 */
2322 static 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 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
2346 #  /* To allow a standalone DLL, use this next replacement function: */
2347 #  undef sqlite3_win32_utf8_to_unicode
2348 #  define sqlite3_win32_utf8_to_unicode utf8_to_utf16
2349 #
2350 LPWSTR utf8_to_utf16(const char *z){
2351   int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
2352   LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
2353   if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
2354     return rv;
2355   sqlite3_free(rv);
2356   return 0;
2357 }
2358 #endif
2359 
2360 /*
2361 ** This function attempts to normalize the time values found in the stat()
2362 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2363 ** appears to return these values as local times.
2364 */
2365 static void statTimesToUtc(
2366   const char *zPath,
2367   struct stat *pStatBuf
2368 ){
2369   HANDLE hFindFile;
2370   WIN32_FIND_DATAW fd;
2371   LPWSTR zUnicodeName;
2372   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2373   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2374   if( zUnicodeName ){
2375     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2376     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2377     if( hFindFile!=NULL ){
2378       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2379       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2380       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2381       FindClose(hFindFile);
2382     }
2383     sqlite3_free(zUnicodeName);
2384   }
2385 }
2386 #endif
2387 
2388 /*
2389 ** This function is used in place of stat().  On Windows, special handling
2390 ** is required in order for the included time to be returned as UTC.  On all
2391 ** other systems, this function simply calls stat().
2392 */
2393 static int fileStat(
2394   const char *zPath,
2395   struct stat *pStatBuf
2396 ){
2397 #if defined(_WIN32)
2398   int rc = stat(zPath, pStatBuf);
2399   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2400   return rc;
2401 #else
2402   return stat(zPath, pStatBuf);
2403 #endif
2404 }
2405 
2406 /*
2407 ** This function is used in place of lstat().  On Windows, special handling
2408 ** is required in order for the included time to be returned as UTC.  On all
2409 ** other systems, this function simply calls lstat().
2410 */
2411 static int fileLinkStat(
2412   const char *zPath,
2413   struct stat *pStatBuf
2414 ){
2415 #if defined(_WIN32)
2416   int rc = lstat(zPath, pStatBuf);
2417   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2418   return rc;
2419 #else
2420   return lstat(zPath, pStatBuf);
2421 #endif
2422 }
2423 
2424 /*
2425 ** Argument zFile is the name of a file that will be created and/or written
2426 ** by SQL function writefile(). This function ensures that the directory
2427 ** zFile will be written to exists, creating it if required. The permissions
2428 ** for any path components created by this function are set in accordance
2429 ** with the current umask.
2430 **
2431 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2432 ** SQLITE_OK is returned if the directory is successfully created, or
2433 ** SQLITE_ERROR otherwise.
2434 */
2435 static int makeDirectory(
2436   const char *zFile
2437 ){
2438   char *zCopy = sqlite3_mprintf("%s", zFile);
2439   int rc = SQLITE_OK;
2440 
2441   if( zCopy==0 ){
2442     rc = SQLITE_NOMEM;
2443   }else{
2444     int nCopy = (int)strlen(zCopy);
2445     int i = 1;
2446 
2447     while( rc==SQLITE_OK ){
2448       struct stat sStat;
2449       int rc2;
2450 
2451       for(; zCopy[i]!='/' && i<nCopy; i++);
2452       if( i==nCopy ) break;
2453       zCopy[i] = '\0';
2454 
2455       rc2 = fileStat(zCopy, &sStat);
2456       if( rc2!=0 ){
2457         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2458       }else{
2459         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2460       }
2461       zCopy[i] = '/';
2462       i++;
2463     }
2464 
2465     sqlite3_free(zCopy);
2466   }
2467 
2468   return rc;
2469 }
2470 
2471 /*
2472 ** This function does the work for the writefile() UDF. Refer to
2473 ** header comments at the top of this file for details.
2474 */
2475 static int writeFile(
2476   sqlite3_context *pCtx,          /* Context to return bytes written in */
2477   const char *zFile,              /* File to write */
2478   sqlite3_value *pData,           /* Data to write */
2479   mode_t mode,                    /* MODE parameter passed to writefile() */
2480   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2481 ){
2482   if( zFile==0 ) return 1;
2483 #if !defined(_WIN32) && !defined(WIN32)
2484   if( S_ISLNK(mode) ){
2485     const char *zTo = (const char*)sqlite3_value_text(pData);
2486     if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
2487   }else
2488 #endif
2489   {
2490     if( S_ISDIR(mode) ){
2491       if( mkdir(zFile, mode) ){
2492         /* The mkdir() call to create the directory failed. This might not
2493         ** be an error though - if there is already a directory at the same
2494         ** path and either the permissions already match or can be changed
2495         ** to do so using chmod(), it is not an error.  */
2496         struct stat sStat;
2497         if( errno!=EEXIST
2498          || 0!=fileStat(zFile, &sStat)
2499          || !S_ISDIR(sStat.st_mode)
2500          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2501         ){
2502           return 1;
2503         }
2504       }
2505     }else{
2506       sqlite3_int64 nWrite = 0;
2507       const char *z;
2508       int rc = 0;
2509       FILE *out = fopen(zFile, "wb");
2510       if( out==0 ) return 1;
2511       z = (const char*)sqlite3_value_blob(pData);
2512       if( z ){
2513         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2514         nWrite = sqlite3_value_bytes(pData);
2515         if( nWrite!=n ){
2516           rc = 1;
2517         }
2518       }
2519       fclose(out);
2520       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2521         rc = 1;
2522       }
2523       if( rc ) return 2;
2524       sqlite3_result_int64(pCtx, nWrite);
2525     }
2526   }
2527 
2528   if( mtime>=0 ){
2529 #if defined(_WIN32)
2530 #if !SQLITE_OS_WINRT
2531     /* Windows */
2532     FILETIME lastAccess;
2533     FILETIME lastWrite;
2534     SYSTEMTIME currentTime;
2535     LONGLONG intervals;
2536     HANDLE hFile;
2537     LPWSTR zUnicodeName;
2538     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2539 
2540     GetSystemTime(&currentTime);
2541     SystemTimeToFileTime(&currentTime, &lastAccess);
2542     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2543     lastWrite.dwLowDateTime = (DWORD)intervals;
2544     lastWrite.dwHighDateTime = intervals >> 32;
2545     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2546     if( zUnicodeName==0 ){
2547       return 1;
2548     }
2549     hFile = CreateFileW(
2550       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2551       FILE_FLAG_BACKUP_SEMANTICS, NULL
2552     );
2553     sqlite3_free(zUnicodeName);
2554     if( hFile!=INVALID_HANDLE_VALUE ){
2555       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2556       CloseHandle(hFile);
2557       return !bResult;
2558     }else{
2559       return 1;
2560     }
2561 #endif
2562 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2563     /* Recent unix */
2564     struct timespec times[2];
2565     times[0].tv_nsec = times[1].tv_nsec = 0;
2566     times[0].tv_sec = time(0);
2567     times[1].tv_sec = mtime;
2568     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2569       return 1;
2570     }
2571 #else
2572     /* Legacy unix */
2573     struct timeval times[2];
2574     times[0].tv_usec = times[1].tv_usec = 0;
2575     times[0].tv_sec = time(0);
2576     times[1].tv_sec = mtime;
2577     if( utimes(zFile, times) ){
2578       return 1;
2579     }
2580 #endif
2581   }
2582 
2583   return 0;
2584 }
2585 
2586 /*
2587 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2588 ** Refer to header comments at the top of this file for details.
2589 */
2590 static void writefileFunc(
2591   sqlite3_context *context,
2592   int argc,
2593   sqlite3_value **argv
2594 ){
2595   const char *zFile;
2596   mode_t mode = 0;
2597   int res;
2598   sqlite3_int64 mtime = -1;
2599 
2600   if( argc<2 || argc>4 ){
2601     sqlite3_result_error(context,
2602         "wrong number of arguments to function writefile()", -1
2603     );
2604     return;
2605   }
2606 
2607   zFile = (const char*)sqlite3_value_text(argv[0]);
2608   if( zFile==0 ) return;
2609   if( argc>=3 ){
2610     mode = (mode_t)sqlite3_value_int(argv[2]);
2611   }
2612   if( argc==4 ){
2613     mtime = sqlite3_value_int64(argv[3]);
2614   }
2615 
2616   res = writeFile(context, zFile, argv[1], mode, mtime);
2617   if( res==1 && errno==ENOENT ){
2618     if( makeDirectory(zFile)==SQLITE_OK ){
2619       res = writeFile(context, zFile, argv[1], mode, mtime);
2620     }
2621   }
2622 
2623   if( argc>2 && res!=0 ){
2624     if( S_ISLNK(mode) ){
2625       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2626     }else if( S_ISDIR(mode) ){
2627       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2628     }else{
2629       ctxErrorMsg(context, "failed to write file: %s", zFile);
2630     }
2631   }
2632 }
2633 
2634 /*
2635 ** SQL function:   lsmode(MODE)
2636 **
2637 ** Given a numberic st_mode from stat(), convert it into a human-readable
2638 ** text string in the style of "ls -l".
2639 */
2640 static void lsModeFunc(
2641   sqlite3_context *context,
2642   int argc,
2643   sqlite3_value **argv
2644 ){
2645   int i;
2646   int iMode = sqlite3_value_int(argv[0]);
2647   char z[16];
2648   (void)argc;
2649   if( S_ISLNK(iMode) ){
2650     z[0] = 'l';
2651   }else if( S_ISREG(iMode) ){
2652     z[0] = '-';
2653   }else if( S_ISDIR(iMode) ){
2654     z[0] = 'd';
2655   }else{
2656     z[0] = '?';
2657   }
2658   for(i=0; i<3; i++){
2659     int m = (iMode >> ((2-i)*3));
2660     char *a = &z[1 + i*3];
2661     a[0] = (m & 0x4) ? 'r' : '-';
2662     a[1] = (m & 0x2) ? 'w' : '-';
2663     a[2] = (m & 0x1) ? 'x' : '-';
2664   }
2665   z[10] = '\0';
2666   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2667 }
2668 
2669 #ifndef SQLITE_OMIT_VIRTUALTABLE
2670 
2671 /*
2672 ** Cursor type for recursively iterating through a directory structure.
2673 */
2674 typedef struct fsdir_cursor fsdir_cursor;
2675 typedef struct FsdirLevel FsdirLevel;
2676 
2677 struct FsdirLevel {
2678   DIR *pDir;                 /* From opendir() */
2679   char *zDir;                /* Name of directory (nul-terminated) */
2680 };
2681 
2682 struct fsdir_cursor {
2683   sqlite3_vtab_cursor base;  /* Base class - must be first */
2684 
2685   int nLvl;                  /* Number of entries in aLvl[] array */
2686   int iLvl;                  /* Index of current entry */
2687   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2688 
2689   const char *zBase;
2690   int nBase;
2691 
2692   struct stat sStat;         /* Current lstat() results */
2693   char *zPath;               /* Path to current entry */
2694   sqlite3_int64 iRowid;      /* Current rowid */
2695 };
2696 
2697 typedef struct fsdir_tab fsdir_tab;
2698 struct fsdir_tab {
2699   sqlite3_vtab base;         /* Base class - must be first */
2700 };
2701 
2702 /*
2703 ** Construct a new fsdir virtual table object.
2704 */
2705 static int fsdirConnect(
2706   sqlite3 *db,
2707   void *pAux,
2708   int argc, const char *const*argv,
2709   sqlite3_vtab **ppVtab,
2710   char **pzErr
2711 ){
2712   fsdir_tab *pNew = 0;
2713   int rc;
2714   (void)pAux;
2715   (void)argc;
2716   (void)argv;
2717   (void)pzErr;
2718   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2719   if( rc==SQLITE_OK ){
2720     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2721     if( pNew==0 ) return SQLITE_NOMEM;
2722     memset(pNew, 0, sizeof(*pNew));
2723     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2724   }
2725   *ppVtab = (sqlite3_vtab*)pNew;
2726   return rc;
2727 }
2728 
2729 /*
2730 ** This method is the destructor for fsdir vtab objects.
2731 */
2732 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2733   sqlite3_free(pVtab);
2734   return SQLITE_OK;
2735 }
2736 
2737 /*
2738 ** Constructor for a new fsdir_cursor object.
2739 */
2740 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2741   fsdir_cursor *pCur;
2742   (void)p;
2743   pCur = sqlite3_malloc( sizeof(*pCur) );
2744   if( pCur==0 ) return SQLITE_NOMEM;
2745   memset(pCur, 0, sizeof(*pCur));
2746   pCur->iLvl = -1;
2747   *ppCursor = &pCur->base;
2748   return SQLITE_OK;
2749 }
2750 
2751 /*
2752 ** Reset a cursor back to the state it was in when first returned
2753 ** by fsdirOpen().
2754 */
2755 static void fsdirResetCursor(fsdir_cursor *pCur){
2756   int i;
2757   for(i=0; i<=pCur->iLvl; i++){
2758     FsdirLevel *pLvl = &pCur->aLvl[i];
2759     if( pLvl->pDir ) closedir(pLvl->pDir);
2760     sqlite3_free(pLvl->zDir);
2761   }
2762   sqlite3_free(pCur->zPath);
2763   sqlite3_free(pCur->aLvl);
2764   pCur->aLvl = 0;
2765   pCur->zPath = 0;
2766   pCur->zBase = 0;
2767   pCur->nBase = 0;
2768   pCur->nLvl = 0;
2769   pCur->iLvl = -1;
2770   pCur->iRowid = 1;
2771 }
2772 
2773 /*
2774 ** Destructor for an fsdir_cursor.
2775 */
2776 static int fsdirClose(sqlite3_vtab_cursor *cur){
2777   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2778 
2779   fsdirResetCursor(pCur);
2780   sqlite3_free(pCur);
2781   return SQLITE_OK;
2782 }
2783 
2784 /*
2785 ** Set the error message for the virtual table associated with cursor
2786 ** pCur to the results of vprintf(zFmt, ...).
2787 */
2788 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2789   va_list ap;
2790   va_start(ap, zFmt);
2791   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2792   va_end(ap);
2793 }
2794 
2795 
2796 /*
2797 ** Advance an fsdir_cursor to its next row of output.
2798 */
2799 static int fsdirNext(sqlite3_vtab_cursor *cur){
2800   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2801   mode_t m = pCur->sStat.st_mode;
2802 
2803   pCur->iRowid++;
2804   if( S_ISDIR(m) ){
2805     /* Descend into this directory */
2806     int iNew = pCur->iLvl + 1;
2807     FsdirLevel *pLvl;
2808     if( iNew>=pCur->nLvl ){
2809       int nNew = iNew+1;
2810       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2811       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2812       if( aNew==0 ) return SQLITE_NOMEM;
2813       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2814       pCur->aLvl = aNew;
2815       pCur->nLvl = nNew;
2816     }
2817     pCur->iLvl = iNew;
2818     pLvl = &pCur->aLvl[iNew];
2819 
2820     pLvl->zDir = pCur->zPath;
2821     pCur->zPath = 0;
2822     pLvl->pDir = opendir(pLvl->zDir);
2823     if( pLvl->pDir==0 ){
2824       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2825       return SQLITE_ERROR;
2826     }
2827   }
2828 
2829   while( pCur->iLvl>=0 ){
2830     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2831     struct dirent *pEntry = readdir(pLvl->pDir);
2832     if( pEntry ){
2833       if( pEntry->d_name[0]=='.' ){
2834        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2835        if( pEntry->d_name[1]=='\0' ) continue;
2836       }
2837       sqlite3_free(pCur->zPath);
2838       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2839       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2840       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2841         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2842         return SQLITE_ERROR;
2843       }
2844       return SQLITE_OK;
2845     }
2846     closedir(pLvl->pDir);
2847     sqlite3_free(pLvl->zDir);
2848     pLvl->pDir = 0;
2849     pLvl->zDir = 0;
2850     pCur->iLvl--;
2851   }
2852 
2853   /* EOF */
2854   sqlite3_free(pCur->zPath);
2855   pCur->zPath = 0;
2856   return SQLITE_OK;
2857 }
2858 
2859 /*
2860 ** Return values of columns for the row at which the series_cursor
2861 ** is currently pointing.
2862 */
2863 static int fsdirColumn(
2864   sqlite3_vtab_cursor *cur,   /* The cursor */
2865   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2866   int i                       /* Which column to return */
2867 ){
2868   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2869   switch( i ){
2870     case FSDIR_COLUMN_NAME: {
2871       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2872       break;
2873     }
2874 
2875     case FSDIR_COLUMN_MODE:
2876       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2877       break;
2878 
2879     case FSDIR_COLUMN_MTIME:
2880       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2881       break;
2882 
2883     case FSDIR_COLUMN_DATA: {
2884       mode_t m = pCur->sStat.st_mode;
2885       if( S_ISDIR(m) ){
2886         sqlite3_result_null(ctx);
2887 #if !defined(_WIN32) && !defined(WIN32)
2888       }else if( S_ISLNK(m) ){
2889         char aStatic[64];
2890         char *aBuf = aStatic;
2891         sqlite3_int64 nBuf = 64;
2892         int n;
2893 
2894         while( 1 ){
2895           n = readlink(pCur->zPath, aBuf, nBuf);
2896           if( n<nBuf ) break;
2897           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2898           nBuf = nBuf*2;
2899           aBuf = sqlite3_malloc64(nBuf);
2900           if( aBuf==0 ){
2901             sqlite3_result_error_nomem(ctx);
2902             return SQLITE_NOMEM;
2903           }
2904         }
2905 
2906         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2907         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2908 #endif
2909       }else{
2910         readFileContents(ctx, pCur->zPath);
2911       }
2912     }
2913     case FSDIR_COLUMN_PATH:
2914     default: {
2915       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2916       ** always return their values as NULL */
2917       break;
2918     }
2919   }
2920   return SQLITE_OK;
2921 }
2922 
2923 /*
2924 ** Return the rowid for the current row. In this implementation, the
2925 ** first row returned is assigned rowid value 1, and each subsequent
2926 ** row a value 1 more than that of the previous.
2927 */
2928 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2929   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2930   *pRowid = pCur->iRowid;
2931   return SQLITE_OK;
2932 }
2933 
2934 /*
2935 ** Return TRUE if the cursor has been moved off of the last
2936 ** row of output.
2937 */
2938 static int fsdirEof(sqlite3_vtab_cursor *cur){
2939   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2940   return (pCur->zPath==0);
2941 }
2942 
2943 /*
2944 ** xFilter callback.
2945 **
2946 ** idxNum==1   PATH parameter only
2947 ** idxNum==2   Both PATH and DIR supplied
2948 */
2949 static int fsdirFilter(
2950   sqlite3_vtab_cursor *cur,
2951   int idxNum, const char *idxStr,
2952   int argc, sqlite3_value **argv
2953 ){
2954   const char *zDir = 0;
2955   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2956   (void)idxStr;
2957   fsdirResetCursor(pCur);
2958 
2959   if( idxNum==0 ){
2960     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2961     return SQLITE_ERROR;
2962   }
2963 
2964   assert( argc==idxNum && (argc==1 || argc==2) );
2965   zDir = (const char*)sqlite3_value_text(argv[0]);
2966   if( zDir==0 ){
2967     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2968     return SQLITE_ERROR;
2969   }
2970   if( argc==2 ){
2971     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2972   }
2973   if( pCur->zBase ){
2974     pCur->nBase = (int)strlen(pCur->zBase)+1;
2975     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2976   }else{
2977     pCur->zPath = sqlite3_mprintf("%s", zDir);
2978   }
2979 
2980   if( pCur->zPath==0 ){
2981     return SQLITE_NOMEM;
2982   }
2983   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2984     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2985     return SQLITE_ERROR;
2986   }
2987 
2988   return SQLITE_OK;
2989 }
2990 
2991 /*
2992 ** SQLite will invoke this method one or more times while planning a query
2993 ** that uses the generate_series virtual table.  This routine needs to create
2994 ** a query plan for each invocation and compute an estimated cost for that
2995 ** plan.
2996 **
2997 ** In this implementation idxNum is used to represent the
2998 ** query plan.  idxStr is unused.
2999 **
3000 ** The query plan is represented by values of idxNum:
3001 **
3002 **  (1)  The path value is supplied by argv[0]
3003 **  (2)  Path is in argv[0] and dir is in argv[1]
3004 */
3005 static int fsdirBestIndex(
3006   sqlite3_vtab *tab,
3007   sqlite3_index_info *pIdxInfo
3008 ){
3009   int i;                 /* Loop over constraints */
3010   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
3011   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
3012   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
3013   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
3014   const struct sqlite3_index_constraint *pConstraint;
3015 
3016   (void)tab;
3017   pConstraint = pIdxInfo->aConstraint;
3018   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3019     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3020     switch( pConstraint->iColumn ){
3021       case FSDIR_COLUMN_PATH: {
3022         if( pConstraint->usable ){
3023           idxPath = i;
3024           seenPath = 0;
3025         }else if( idxPath<0 ){
3026           seenPath = 1;
3027         }
3028         break;
3029       }
3030       case FSDIR_COLUMN_DIR: {
3031         if( pConstraint->usable ){
3032           idxDir = i;
3033           seenDir = 0;
3034         }else if( idxDir<0 ){
3035           seenDir = 1;
3036         }
3037         break;
3038       }
3039     }
3040   }
3041   if( seenPath || seenDir ){
3042     /* If input parameters are unusable, disallow this plan */
3043     return SQLITE_CONSTRAINT;
3044   }
3045 
3046   if( idxPath<0 ){
3047     pIdxInfo->idxNum = 0;
3048     /* The pIdxInfo->estimatedCost should have been initialized to a huge
3049     ** number.  Leave it unchanged. */
3050     pIdxInfo->estimatedRows = 0x7fffffff;
3051   }else{
3052     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3053     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3054     if( idxDir>=0 ){
3055       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3056       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3057       pIdxInfo->idxNum = 2;
3058       pIdxInfo->estimatedCost = 10.0;
3059     }else{
3060       pIdxInfo->idxNum = 1;
3061       pIdxInfo->estimatedCost = 100.0;
3062     }
3063   }
3064 
3065   return SQLITE_OK;
3066 }
3067 
3068 /*
3069 ** Register the "fsdir" virtual table.
3070 */
3071 static int fsdirRegister(sqlite3 *db){
3072   static sqlite3_module fsdirModule = {
3073     0,                         /* iVersion */
3074     0,                         /* xCreate */
3075     fsdirConnect,              /* xConnect */
3076     fsdirBestIndex,            /* xBestIndex */
3077     fsdirDisconnect,           /* xDisconnect */
3078     0,                         /* xDestroy */
3079     fsdirOpen,                 /* xOpen - open a cursor */
3080     fsdirClose,                /* xClose - close a cursor */
3081     fsdirFilter,               /* xFilter - configure scan constraints */
3082     fsdirNext,                 /* xNext - advance a cursor */
3083     fsdirEof,                  /* xEof - check for end of scan */
3084     fsdirColumn,               /* xColumn - read data */
3085     fsdirRowid,                /* xRowid - read data */
3086     0,                         /* xUpdate */
3087     0,                         /* xBegin */
3088     0,                         /* xSync */
3089     0,                         /* xCommit */
3090     0,                         /* xRollback */
3091     0,                         /* xFindMethod */
3092     0,                         /* xRename */
3093     0,                         /* xSavepoint */
3094     0,                         /* xRelease */
3095     0,                         /* xRollbackTo */
3096     0,                         /* xShadowName */
3097   };
3098 
3099   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3100   return rc;
3101 }
3102 #else         /* SQLITE_OMIT_VIRTUALTABLE */
3103 # define fsdirRegister(x) SQLITE_OK
3104 #endif
3105 
3106 #ifdef _WIN32
3107 
3108 #endif
3109 int sqlite3_fileio_init(
3110   sqlite3 *db,
3111   char **pzErrMsg,
3112   const sqlite3_api_routines *pApi
3113 ){
3114   int rc = SQLITE_OK;
3115   SQLITE_EXTENSION_INIT2(pApi);
3116   (void)pzErrMsg;  /* Unused parameter */
3117   rc = sqlite3_create_function(db, "readfile", 1,
3118                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3119                                readfileFunc, 0, 0);
3120   if( rc==SQLITE_OK ){
3121     rc = sqlite3_create_function(db, "writefile", -1,
3122                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3123                                  writefileFunc, 0, 0);
3124   }
3125   if( rc==SQLITE_OK ){
3126     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3127                                  lsModeFunc, 0, 0);
3128   }
3129   if( rc==SQLITE_OK ){
3130     rc = fsdirRegister(db);
3131   }
3132   return rc;
3133 }
3134 
3135 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
3136 /* To allow a standalone DLL, make test_windirent.c use the same
3137  * redefined SQLite API calls as the above extension code does.
3138  * Just pull in this .c to accomplish this. As a beneficial side
3139  * effect, this extension becomes a single translation unit. */
3140 #  include "test_windirent.c"
3141 #endif
3142 
3143 /************************* End ../ext/misc/fileio.c ********************/
3144 /************************* Begin ../ext/misc/completion.c ******************/
3145 /*
3146 ** 2017-07-10
3147 **
3148 ** The author disclaims copyright to this source code.  In place of
3149 ** a legal notice, here is a blessing:
3150 **
3151 **    May you do good and not evil.
3152 **    May you find forgiveness for yourself and forgive others.
3153 **    May you share freely, never taking more than you give.
3154 **
3155 *************************************************************************
3156 **
3157 ** This file implements an eponymous virtual table that returns suggested
3158 ** completions for a partial SQL input.
3159 **
3160 ** Suggested usage:
3161 **
3162 **     SELECT DISTINCT candidate COLLATE nocase
3163 **       FROM completion($prefix,$wholeline)
3164 **      ORDER BY 1;
3165 **
3166 ** The two query parameters are optional.  $prefix is the text of the
3167 ** current word being typed and that is to be completed.  $wholeline is
3168 ** the complete input line, used for context.
3169 **
3170 ** The raw completion() table might return the same candidate multiple
3171 ** times, for example if the same column name is used to two or more
3172 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3173 ** the DISTINCT and ORDER BY are recommended.
3174 **
3175 ** This virtual table operates at the speed of human typing, and so there
3176 ** is no attempt to make it fast.  Even a slow implementation will be much
3177 ** faster than any human can type.
3178 **
3179 */
3180 /* #include "sqlite3ext.h" */
3181 SQLITE_EXTENSION_INIT1
3182 #include <assert.h>
3183 #include <string.h>
3184 #include <ctype.h>
3185 
3186 #ifndef SQLITE_OMIT_VIRTUALTABLE
3187 
3188 /* completion_vtab is a subclass of sqlite3_vtab which will
3189 ** serve as the underlying representation of a completion virtual table
3190 */
3191 typedef struct completion_vtab completion_vtab;
3192 struct completion_vtab {
3193   sqlite3_vtab base;  /* Base class - must be first */
3194   sqlite3 *db;        /* Database connection for this completion vtab */
3195 };
3196 
3197 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3198 ** serve as the underlying representation of a cursor that scans
3199 ** over rows of the result
3200 */
3201 typedef struct completion_cursor completion_cursor;
3202 struct completion_cursor {
3203   sqlite3_vtab_cursor base;  /* Base class - must be first */
3204   sqlite3 *db;               /* Database connection for this cursor */
3205   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3206   char *zPrefix;             /* The prefix for the word we want to complete */
3207   char *zLine;               /* The whole that we want to complete */
3208   const char *zCurrentRow;   /* Current output row */
3209   int szRow;                 /* Length of the zCurrentRow string */
3210   sqlite3_stmt *pStmt;       /* Current statement */
3211   sqlite3_int64 iRowid;      /* The rowid */
3212   int ePhase;                /* Current phase */
3213   int j;                     /* inter-phase counter */
3214 };
3215 
3216 /* Values for ePhase:
3217 */
3218 #define COMPLETION_FIRST_PHASE   1
3219 #define COMPLETION_KEYWORDS      1
3220 #define COMPLETION_PRAGMAS       2
3221 #define COMPLETION_FUNCTIONS     3
3222 #define COMPLETION_COLLATIONS    4
3223 #define COMPLETION_INDEXES       5
3224 #define COMPLETION_TRIGGERS      6
3225 #define COMPLETION_DATABASES     7
3226 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3227 #define COMPLETION_COLUMNS       9
3228 #define COMPLETION_MODULES       10
3229 #define COMPLETION_EOF           11
3230 
3231 /*
3232 ** The completionConnect() method is invoked to create a new
3233 ** completion_vtab that describes the completion virtual table.
3234 **
3235 ** Think of this routine as the constructor for completion_vtab objects.
3236 **
3237 ** All this routine needs to do is:
3238 **
3239 **    (1) Allocate the completion_vtab object and initialize all fields.
3240 **
3241 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3242 **        result set of queries against completion will look like.
3243 */
3244 static int completionConnect(
3245   sqlite3 *db,
3246   void *pAux,
3247   int argc, const char *const*argv,
3248   sqlite3_vtab **ppVtab,
3249   char **pzErr
3250 ){
3251   completion_vtab *pNew;
3252   int rc;
3253 
3254   (void)(pAux);    /* Unused parameter */
3255   (void)(argc);    /* Unused parameter */
3256   (void)(argv);    /* Unused parameter */
3257   (void)(pzErr);   /* Unused parameter */
3258 
3259 /* Column numbers */
3260 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3261 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3262 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3263 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3264 
3265   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3266   rc = sqlite3_declare_vtab(db,
3267       "CREATE TABLE x("
3268       "  candidate TEXT,"
3269       "  prefix TEXT HIDDEN,"
3270       "  wholeline TEXT HIDDEN,"
3271       "  phase INT HIDDEN"        /* Used for debugging only */
3272       ")");
3273   if( rc==SQLITE_OK ){
3274     pNew = sqlite3_malloc( sizeof(*pNew) );
3275     *ppVtab = (sqlite3_vtab*)pNew;
3276     if( pNew==0 ) return SQLITE_NOMEM;
3277     memset(pNew, 0, sizeof(*pNew));
3278     pNew->db = db;
3279   }
3280   return rc;
3281 }
3282 
3283 /*
3284 ** This method is the destructor for completion_cursor objects.
3285 */
3286 static int completionDisconnect(sqlite3_vtab *pVtab){
3287   sqlite3_free(pVtab);
3288   return SQLITE_OK;
3289 }
3290 
3291 /*
3292 ** Constructor for a new completion_cursor object.
3293 */
3294 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3295   completion_cursor *pCur;
3296   pCur = sqlite3_malloc( sizeof(*pCur) );
3297   if( pCur==0 ) return SQLITE_NOMEM;
3298   memset(pCur, 0, sizeof(*pCur));
3299   pCur->db = ((completion_vtab*)p)->db;
3300   *ppCursor = &pCur->base;
3301   return SQLITE_OK;
3302 }
3303 
3304 /*
3305 ** Reset the completion_cursor.
3306 */
3307 static void completionCursorReset(completion_cursor *pCur){
3308   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3309   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3310   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3311   pCur->j = 0;
3312 }
3313 
3314 /*
3315 ** Destructor for a completion_cursor.
3316 */
3317 static int completionClose(sqlite3_vtab_cursor *cur){
3318   completionCursorReset((completion_cursor*)cur);
3319   sqlite3_free(cur);
3320   return SQLITE_OK;
3321 }
3322 
3323 /*
3324 ** Advance a completion_cursor to its next row of output.
3325 **
3326 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3327 ** record the current state of the scan.  This routine sets ->zCurrentRow
3328 ** to the current row of output and then returns.  If no more rows remain,
3329 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3330 ** table that has reached the end of its scan.
3331 **
3332 ** The current implementation just lists potential identifiers and
3333 ** keywords and filters them by zPrefix.  Future enhancements should
3334 ** take zLine into account to try to restrict the set of identifiers and
3335 ** keywords based on what would be legal at the current point of input.
3336 */
3337 static int completionNext(sqlite3_vtab_cursor *cur){
3338   completion_cursor *pCur = (completion_cursor*)cur;
3339   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3340   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3341   pCur->iRowid++;
3342   while( pCur->ePhase!=COMPLETION_EOF ){
3343     switch( pCur->ePhase ){
3344       case COMPLETION_KEYWORDS: {
3345         if( pCur->j >= sqlite3_keyword_count() ){
3346           pCur->zCurrentRow = 0;
3347           pCur->ePhase = COMPLETION_DATABASES;
3348         }else{
3349           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3350         }
3351         iCol = -1;
3352         break;
3353       }
3354       case COMPLETION_DATABASES: {
3355         if( pCur->pStmt==0 ){
3356           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3357                              &pCur->pStmt, 0);
3358         }
3359         iCol = 1;
3360         eNextPhase = COMPLETION_TABLES;
3361         break;
3362       }
3363       case COMPLETION_TABLES: {
3364         if( pCur->pStmt==0 ){
3365           sqlite3_stmt *pS2;
3366           char *zSql = 0;
3367           const char *zSep = "";
3368           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3369           while( sqlite3_step(pS2)==SQLITE_ROW ){
3370             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3371             zSql = sqlite3_mprintf(
3372                "%z%s"
3373                "SELECT name FROM \"%w\".sqlite_schema",
3374                zSql, zSep, zDb
3375             );
3376             if( zSql==0 ) return SQLITE_NOMEM;
3377             zSep = " UNION ";
3378           }
3379           sqlite3_finalize(pS2);
3380           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3381           sqlite3_free(zSql);
3382         }
3383         iCol = 0;
3384         eNextPhase = COMPLETION_COLUMNS;
3385         break;
3386       }
3387       case COMPLETION_COLUMNS: {
3388         if( pCur->pStmt==0 ){
3389           sqlite3_stmt *pS2;
3390           char *zSql = 0;
3391           const char *zSep = "";
3392           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3393           while( sqlite3_step(pS2)==SQLITE_ROW ){
3394             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3395             zSql = sqlite3_mprintf(
3396                "%z%s"
3397                "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3398                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3399                " WHERE sm.type='table'",
3400                zSql, zSep, zDb, zDb
3401             );
3402             if( zSql==0 ) return SQLITE_NOMEM;
3403             zSep = " UNION ";
3404           }
3405           sqlite3_finalize(pS2);
3406           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3407           sqlite3_free(zSql);
3408         }
3409         iCol = 0;
3410         eNextPhase = COMPLETION_EOF;
3411         break;
3412       }
3413     }
3414     if( iCol<0 ){
3415       /* This case is when the phase presets zCurrentRow */
3416       if( pCur->zCurrentRow==0 ) continue;
3417     }else{
3418       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3419         /* Extract the next row of content */
3420         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3421         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3422       }else{
3423         /* When all rows are finished, advance to the next phase */
3424         sqlite3_finalize(pCur->pStmt);
3425         pCur->pStmt = 0;
3426         pCur->ePhase = eNextPhase;
3427         continue;
3428       }
3429     }
3430     if( pCur->nPrefix==0 ) break;
3431     if( pCur->nPrefix<=pCur->szRow
3432      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3433     ){
3434       break;
3435     }
3436   }
3437 
3438   return SQLITE_OK;
3439 }
3440 
3441 /*
3442 ** Return values of columns for the row at which the completion_cursor
3443 ** is currently pointing.
3444 */
3445 static int completionColumn(
3446   sqlite3_vtab_cursor *cur,   /* The cursor */
3447   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3448   int i                       /* Which column to return */
3449 ){
3450   completion_cursor *pCur = (completion_cursor*)cur;
3451   switch( i ){
3452     case COMPLETION_COLUMN_CANDIDATE: {
3453       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3454       break;
3455     }
3456     case COMPLETION_COLUMN_PREFIX: {
3457       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3458       break;
3459     }
3460     case COMPLETION_COLUMN_WHOLELINE: {
3461       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3462       break;
3463     }
3464     case COMPLETION_COLUMN_PHASE: {
3465       sqlite3_result_int(ctx, pCur->ePhase);
3466       break;
3467     }
3468   }
3469   return SQLITE_OK;
3470 }
3471 
3472 /*
3473 ** Return the rowid for the current row.  In this implementation, the
3474 ** rowid is the same as the output value.
3475 */
3476 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3477   completion_cursor *pCur = (completion_cursor*)cur;
3478   *pRowid = pCur->iRowid;
3479   return SQLITE_OK;
3480 }
3481 
3482 /*
3483 ** Return TRUE if the cursor has been moved off of the last
3484 ** row of output.
3485 */
3486 static int completionEof(sqlite3_vtab_cursor *cur){
3487   completion_cursor *pCur = (completion_cursor*)cur;
3488   return pCur->ePhase >= COMPLETION_EOF;
3489 }
3490 
3491 /*
3492 ** This method is called to "rewind" the completion_cursor object back
3493 ** to the first row of output.  This method is always called at least
3494 ** once prior to any call to completionColumn() or completionRowid() or
3495 ** completionEof().
3496 */
3497 static int completionFilter(
3498   sqlite3_vtab_cursor *pVtabCursor,
3499   int idxNum, const char *idxStr,
3500   int argc, sqlite3_value **argv
3501 ){
3502   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3503   int iArg = 0;
3504   (void)(idxStr);   /* Unused parameter */
3505   (void)(argc);     /* Unused parameter */
3506   completionCursorReset(pCur);
3507   if( idxNum & 1 ){
3508     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3509     if( pCur->nPrefix>0 ){
3510       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3511       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3512     }
3513     iArg = 1;
3514   }
3515   if( idxNum & 2 ){
3516     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3517     if( pCur->nLine>0 ){
3518       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3519       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3520     }
3521   }
3522   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3523     int i = pCur->nLine;
3524     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3525       i--;
3526     }
3527     pCur->nPrefix = pCur->nLine - i;
3528     if( pCur->nPrefix>0 ){
3529       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3530       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3531     }
3532   }
3533   pCur->iRowid = 0;
3534   pCur->ePhase = COMPLETION_FIRST_PHASE;
3535   return completionNext(pVtabCursor);
3536 }
3537 
3538 /*
3539 ** SQLite will invoke this method one or more times while planning a query
3540 ** that uses the completion virtual table.  This routine needs to create
3541 ** a query plan for each invocation and compute an estimated cost for that
3542 ** plan.
3543 **
3544 ** There are two hidden parameters that act as arguments to the table-valued
3545 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3546 ** is available and bit 1 is set if "wholeline" is available.
3547 */
3548 static int completionBestIndex(
3549   sqlite3_vtab *tab,
3550   sqlite3_index_info *pIdxInfo
3551 ){
3552   int i;                 /* Loop over constraints */
3553   int idxNum = 0;        /* The query plan bitmask */
3554   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3555   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3556   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3557   const struct sqlite3_index_constraint *pConstraint;
3558 
3559   (void)(tab);    /* Unused parameter */
3560   pConstraint = pIdxInfo->aConstraint;
3561   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3562     if( pConstraint->usable==0 ) continue;
3563     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3564     switch( pConstraint->iColumn ){
3565       case COMPLETION_COLUMN_PREFIX:
3566         prefixIdx = i;
3567         idxNum |= 1;
3568         break;
3569       case COMPLETION_COLUMN_WHOLELINE:
3570         wholelineIdx = i;
3571         idxNum |= 2;
3572         break;
3573     }
3574   }
3575   if( prefixIdx>=0 ){
3576     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3577     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3578   }
3579   if( wholelineIdx>=0 ){
3580     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3581     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3582   }
3583   pIdxInfo->idxNum = idxNum;
3584   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3585   pIdxInfo->estimatedRows = 500 - 100*nArg;
3586   return SQLITE_OK;
3587 }
3588 
3589 /*
3590 ** This following structure defines all the methods for the
3591 ** completion virtual table.
3592 */
3593 static sqlite3_module completionModule = {
3594   0,                         /* iVersion */
3595   0,                         /* xCreate */
3596   completionConnect,         /* xConnect */
3597   completionBestIndex,       /* xBestIndex */
3598   completionDisconnect,      /* xDisconnect */
3599   0,                         /* xDestroy */
3600   completionOpen,            /* xOpen - open a cursor */
3601   completionClose,           /* xClose - close a cursor */
3602   completionFilter,          /* xFilter - configure scan constraints */
3603   completionNext,            /* xNext - advance a cursor */
3604   completionEof,             /* xEof - check for end of scan */
3605   completionColumn,          /* xColumn - read data */
3606   completionRowid,           /* xRowid - read data */
3607   0,                         /* xUpdate */
3608   0,                         /* xBegin */
3609   0,                         /* xSync */
3610   0,                         /* xCommit */
3611   0,                         /* xRollback */
3612   0,                         /* xFindMethod */
3613   0,                         /* xRename */
3614   0,                         /* xSavepoint */
3615   0,                         /* xRelease */
3616   0,                         /* xRollbackTo */
3617   0                          /* xShadowName */
3618 };
3619 
3620 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3621 
3622 int sqlite3CompletionVtabInit(sqlite3 *db){
3623   int rc = SQLITE_OK;
3624 #ifndef SQLITE_OMIT_VIRTUALTABLE
3625   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3626 #endif
3627   return rc;
3628 }
3629 
3630 #ifdef _WIN32
3631 
3632 #endif
3633 int sqlite3_completion_init(
3634   sqlite3 *db,
3635   char **pzErrMsg,
3636   const sqlite3_api_routines *pApi
3637 ){
3638   int rc = SQLITE_OK;
3639   SQLITE_EXTENSION_INIT2(pApi);
3640   (void)(pzErrMsg);  /* Unused parameter */
3641 #ifndef SQLITE_OMIT_VIRTUALTABLE
3642   rc = sqlite3CompletionVtabInit(db);
3643 #endif
3644   return rc;
3645 }
3646 
3647 /************************* End ../ext/misc/completion.c ********************/
3648 /************************* Begin ../ext/misc/appendvfs.c ******************/
3649 /*
3650 ** 2017-10-20
3651 **
3652 ** The author disclaims copyright to this source code.  In place of
3653 ** a legal notice, here is a blessing:
3654 **
3655 **    May you do good and not evil.
3656 **    May you find forgiveness for yourself and forgive others.
3657 **    May you share freely, never taking more than you give.
3658 **
3659 ******************************************************************************
3660 **
3661 ** This file implements a VFS shim that allows an SQLite database to be
3662 ** appended onto the end of some other file, such as an executable.
3663 **
3664 ** A special record must appear at the end of the file that identifies the
3665 ** file as an appended database and provides the offset to the first page
3666 ** of the exposed content. (Or, it is the length of the content prefix.)
3667 ** For best performance page 1 should be located at a disk page boundary,
3668 ** though that is not required.
3669 **
3670 ** When opening a database using this VFS, the connection might treat
3671 ** the file as an ordinary SQLite database, or it might treat it as a
3672 ** database appended onto some other file.  The decision is made by
3673 ** applying the following rules in order:
3674 **
3675 **  (1)  An empty file is an ordinary database.
3676 **
3677 **  (2)  If the file ends with the appendvfs trailer string
3678 **       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3679 **
3680 **  (3)  If the file begins with the standard SQLite prefix string
3681 **       "SQLite format 3", that file is an ordinary database.
3682 **
3683 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3684 **       set, then a new database is appended to the already existing file.
3685 **
3686 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3687 **
3688 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3689 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3690 ** This VFS will not read or write past the 1GiB mark.  This restriction
3691 ** might be lifted in future versions.  For now, if you need a larger
3692 ** database, then keep it in a separate file.
3693 **
3694 ** If the file being opened is a plain database (not an appended one), then
3695 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3696 **/
3697 /* #include "sqlite3ext.h" */
3698 SQLITE_EXTENSION_INIT1
3699 #include <string.h>
3700 #include <assert.h>
3701 
3702 /* The append mark at the end of the database is:
3703 **
3704 **     Start-Of-SQLite3-NNNNNNNN
3705 **     123456789 123456789 12345
3706 **
3707 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3708 ** the offset to page 1, and also the length of the prefix content.
3709 */
3710 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3711 #define APND_MARK_PREFIX_SZ  17
3712 #define APND_MARK_FOS_SZ      8
3713 #define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3714 
3715 /*
3716 ** Maximum size of the combined prefix + database + append-mark.  This
3717 ** must be less than 0x40000000 to avoid locking issues on Windows.
3718 */
3719 #define APND_MAX_SIZE  (0x40000000)
3720 
3721 /*
3722 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3723 */
3724 #ifndef APND_ROUNDUP
3725 #define APND_ROUNDUP 4096
3726 #endif
3727 #define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
3728 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3729 
3730 /*
3731 ** Forward declaration of objects used by this utility
3732 */
3733 typedef struct sqlite3_vfs ApndVfs;
3734 typedef struct ApndFile ApndFile;
3735 
3736 /* Access to a lower-level VFS that (might) implement dynamic loading,
3737 ** access to randomness, etc.
3738 */
3739 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3740 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3741 
3742 /* An open appendvfs file
3743 **
3744 ** An instance of this structure describes the appended database file.
3745 ** A separate sqlite3_file object is always appended. The appended
3746 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3747 ** the entire file, including the prefix, the database, and the
3748 ** append-mark.
3749 **
3750 ** The structure of an AppendVFS database is like this:
3751 **
3752 **   +-------------+---------+----------+-------------+
3753 **   | prefix-file | padding | database | append-mark |
3754 **   +-------------+---------+----------+-------------+
3755 **                           ^          ^
3756 **                           |          |
3757 **                         iPgOne      iMark
3758 **
3759 **
3760 ** "prefix file" -  file onto which the database has been appended.
3761 ** "padding"     -  zero or more bytes inserted so that "database"
3762 **                  starts on an APND_ROUNDUP boundary
3763 ** "database"    -  The SQLite database file
3764 ** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3765 **                  the offset from the start of prefix-file to the start
3766 **                  of "database".
3767 **
3768 ** The size of the database is iMark - iPgOne.
3769 **
3770 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3771 ** of iPgOne stored as a big-ending 64-bit integer.
3772 **
3773 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3774 ** Or, iMark is -1 to indicate that it has not yet been written.
3775 */
3776 struct ApndFile {
3777   sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
3778   sqlite3_int64 iPgOne;     /* Offset to the start of the database */
3779   sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
3780   /* Always followed by another sqlite3_file that describes the whole file */
3781 };
3782 
3783 /*
3784 ** Methods for ApndFile
3785 */
3786 static int apndClose(sqlite3_file*);
3787 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3788 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3789 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3790 static int apndSync(sqlite3_file*, int flags);
3791 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3792 static int apndLock(sqlite3_file*, int);
3793 static int apndUnlock(sqlite3_file*, int);
3794 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3795 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3796 static int apndSectorSize(sqlite3_file*);
3797 static int apndDeviceCharacteristics(sqlite3_file*);
3798 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3799 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3800 static void apndShmBarrier(sqlite3_file*);
3801 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3802 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3803 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3804 
3805 /*
3806 ** Methods for ApndVfs
3807 */
3808 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3809 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3810 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3811 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3812 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3813 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3814 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3815 static void apndDlClose(sqlite3_vfs*, void*);
3816 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3817 static int apndSleep(sqlite3_vfs*, int microseconds);
3818 static int apndCurrentTime(sqlite3_vfs*, double*);
3819 static int apndGetLastError(sqlite3_vfs*, int, char *);
3820 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3821 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3822 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3823 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3824 
3825 static sqlite3_vfs apnd_vfs = {
3826   3,                            /* iVersion (set when registered) */
3827   0,                            /* szOsFile (set when registered) */
3828   1024,                         /* mxPathname */
3829   0,                            /* pNext */
3830   "apndvfs",                    /* zName */
3831   0,                            /* pAppData (set when registered) */
3832   apndOpen,                     /* xOpen */
3833   apndDelete,                   /* xDelete */
3834   apndAccess,                   /* xAccess */
3835   apndFullPathname,             /* xFullPathname */
3836   apndDlOpen,                   /* xDlOpen */
3837   apndDlError,                  /* xDlError */
3838   apndDlSym,                    /* xDlSym */
3839   apndDlClose,                  /* xDlClose */
3840   apndRandomness,               /* xRandomness */
3841   apndSleep,                    /* xSleep */
3842   apndCurrentTime,              /* xCurrentTime */
3843   apndGetLastError,             /* xGetLastError */
3844   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3845   apndSetSystemCall,            /* xSetSystemCall */
3846   apndGetSystemCall,            /* xGetSystemCall */
3847   apndNextSystemCall            /* xNextSystemCall */
3848 };
3849 
3850 static const sqlite3_io_methods apnd_io_methods = {
3851   3,                              /* iVersion */
3852   apndClose,                      /* xClose */
3853   apndRead,                       /* xRead */
3854   apndWrite,                      /* xWrite */
3855   apndTruncate,                   /* xTruncate */
3856   apndSync,                       /* xSync */
3857   apndFileSize,                   /* xFileSize */
3858   apndLock,                       /* xLock */
3859   apndUnlock,                     /* xUnlock */
3860   apndCheckReservedLock,          /* xCheckReservedLock */
3861   apndFileControl,                /* xFileControl */
3862   apndSectorSize,                 /* xSectorSize */
3863   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3864   apndShmMap,                     /* xShmMap */
3865   apndShmLock,                    /* xShmLock */
3866   apndShmBarrier,                 /* xShmBarrier */
3867   apndShmUnmap,                   /* xShmUnmap */
3868   apndFetch,                      /* xFetch */
3869   apndUnfetch                     /* xUnfetch */
3870 };
3871 
3872 /*
3873 ** Close an apnd-file.
3874 */
3875 static int apndClose(sqlite3_file *pFile){
3876   pFile = ORIGFILE(pFile);
3877   return pFile->pMethods->xClose(pFile);
3878 }
3879 
3880 /*
3881 ** Read data from an apnd-file.
3882 */
3883 static int apndRead(
3884   sqlite3_file *pFile,
3885   void *zBuf,
3886   int iAmt,
3887   sqlite_int64 iOfst
3888 ){
3889   ApndFile *paf = (ApndFile *)pFile;
3890   pFile = ORIGFILE(pFile);
3891   return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3892 }
3893 
3894 /*
3895 ** Add the append-mark onto what should become the end of the file.
3896 *  If and only if this succeeds, internal ApndFile.iMark is updated.
3897 *  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3898 */
3899 static int apndWriteMark(
3900   ApndFile *paf,
3901   sqlite3_file *pFile,
3902   sqlite_int64 iWriteEnd
3903 ){
3904   sqlite_int64 iPgOne = paf->iPgOne;
3905   unsigned char a[APND_MARK_SIZE];
3906   int i = APND_MARK_FOS_SZ;
3907   int rc;
3908   assert(pFile == ORIGFILE(paf));
3909   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3910   while( --i >= 0 ){
3911     a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3912     iPgOne >>= 8;
3913   }
3914   iWriteEnd += paf->iPgOne;
3915   if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3916                   (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3917     paf->iMark = iWriteEnd;
3918   }
3919   return rc;
3920 }
3921 
3922 /*
3923 ** Write data to an apnd-file.
3924 */
3925 static int apndWrite(
3926   sqlite3_file *pFile,
3927   const void *zBuf,
3928   int iAmt,
3929   sqlite_int64 iOfst
3930 ){
3931   ApndFile *paf = (ApndFile *)pFile;
3932   sqlite_int64 iWriteEnd = iOfst + iAmt;
3933   if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3934   pFile = ORIGFILE(pFile);
3935   /* If append-mark is absent or will be overwritten, write it. */
3936   if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3937     int rc = apndWriteMark(paf, pFile, iWriteEnd);
3938     if( SQLITE_OK!=rc ) return rc;
3939   }
3940   return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3941 }
3942 
3943 /*
3944 ** Truncate an apnd-file.
3945 */
3946 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3947   ApndFile *paf = (ApndFile *)pFile;
3948   pFile = ORIGFILE(pFile);
3949   /* The append mark goes out first so truncate failure does not lose it. */
3950   if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3951   /* Truncate underlying file just past append mark */
3952   return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3953 }
3954 
3955 /*
3956 ** Sync an apnd-file.
3957 */
3958 static int apndSync(sqlite3_file *pFile, int flags){
3959   pFile = ORIGFILE(pFile);
3960   return pFile->pMethods->xSync(pFile, flags);
3961 }
3962 
3963 /*
3964 ** Return the current file-size of an apnd-file.
3965 ** If the append mark is not yet there, the file-size is 0.
3966 */
3967 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3968   ApndFile *paf = (ApndFile *)pFile;
3969   *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3970   return SQLITE_OK;
3971 }
3972 
3973 /*
3974 ** Lock an apnd-file.
3975 */
3976 static int apndLock(sqlite3_file *pFile, int eLock){
3977   pFile = ORIGFILE(pFile);
3978   return pFile->pMethods->xLock(pFile, eLock);
3979 }
3980 
3981 /*
3982 ** Unlock an apnd-file.
3983 */
3984 static int apndUnlock(sqlite3_file *pFile, int eLock){
3985   pFile = ORIGFILE(pFile);
3986   return pFile->pMethods->xUnlock(pFile, eLock);
3987 }
3988 
3989 /*
3990 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3991 */
3992 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3993   pFile = ORIGFILE(pFile);
3994   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3995 }
3996 
3997 /*
3998 ** File control method. For custom operations on an apnd-file.
3999 */
4000 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
4001   ApndFile *paf = (ApndFile *)pFile;
4002   int rc;
4003   pFile = ORIGFILE(pFile);
4004   if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
4005   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
4006   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4007     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
4008   }
4009   return rc;
4010 }
4011 
4012 /*
4013 ** Return the sector-size in bytes for an apnd-file.
4014 */
4015 static int apndSectorSize(sqlite3_file *pFile){
4016   pFile = ORIGFILE(pFile);
4017   return pFile->pMethods->xSectorSize(pFile);
4018 }
4019 
4020 /*
4021 ** Return the device characteristic flags supported by an apnd-file.
4022 */
4023 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4024   pFile = ORIGFILE(pFile);
4025   return pFile->pMethods->xDeviceCharacteristics(pFile);
4026 }
4027 
4028 /* Create a shared memory file mapping */
4029 static int apndShmMap(
4030   sqlite3_file *pFile,
4031   int iPg,
4032   int pgsz,
4033   int bExtend,
4034   void volatile **pp
4035 ){
4036   pFile = ORIGFILE(pFile);
4037   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4038 }
4039 
4040 /* Perform locking on a shared-memory segment */
4041 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4042   pFile = ORIGFILE(pFile);
4043   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4044 }
4045 
4046 /* Memory barrier operation on shared memory */
4047 static void apndShmBarrier(sqlite3_file *pFile){
4048   pFile = ORIGFILE(pFile);
4049   pFile->pMethods->xShmBarrier(pFile);
4050 }
4051 
4052 /* Unmap a shared memory segment */
4053 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4054   pFile = ORIGFILE(pFile);
4055   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4056 }
4057 
4058 /* Fetch a page of a memory-mapped file */
4059 static int apndFetch(
4060   sqlite3_file *pFile,
4061   sqlite3_int64 iOfst,
4062   int iAmt,
4063   void **pp
4064 ){
4065   ApndFile *p = (ApndFile *)pFile;
4066   if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4067     return SQLITE_IOERR; /* Cannot read what is not yet there. */
4068   }
4069   pFile = ORIGFILE(pFile);
4070   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4071 }
4072 
4073 /* Release a memory-mapped page */
4074 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4075   ApndFile *p = (ApndFile *)pFile;
4076   pFile = ORIGFILE(pFile);
4077   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4078 }
4079 
4080 /*
4081 ** Try to read the append-mark off the end of a file.  Return the
4082 ** start of the appended database if the append-mark is present.
4083 ** If there is no valid append-mark, return -1;
4084 **
4085 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4086 ** indicates that the appended database contains at least one page.  The
4087 ** start-of-database value must be a multiple of 512.
4088 */
4089 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4090   int rc, i;
4091   sqlite3_int64 iMark;
4092   int msbs = 8 * (APND_MARK_FOS_SZ-1);
4093   unsigned char a[APND_MARK_SIZE];
4094 
4095   if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4096   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4097   if( rc ) return -1;
4098   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4099   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4100   for(i=1; i<8; i++){
4101     msbs -= 8;
4102     iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4103   }
4104   if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4105   if( iMark & 0x1ff ) return -1;
4106   return iMark;
4107 }
4108 
4109 static const char apvfsSqliteHdr[] = "SQLite format 3";
4110 /*
4111 ** Check to see if the file is an appendvfs SQLite database file.
4112 ** Return true iff it is such. Parameter sz is the file's size.
4113 */
4114 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4115   int rc;
4116   char zHdr[16];
4117   sqlite3_int64 iMark = apndReadMark(sz, pFile);
4118   if( iMark>=0 ){
4119     /* If file has the correct end-marker, the expected odd size, and the
4120     ** SQLite DB type marker where the end-marker puts it, then it
4121     ** is an appendvfs database.
4122     */
4123     rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4124     if( SQLITE_OK==rc
4125      && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4126      && (sz & 0x1ff) == APND_MARK_SIZE
4127      && sz>=512+APND_MARK_SIZE
4128     ){
4129       return 1; /* It's an appendvfs database */
4130     }
4131   }
4132   return 0;
4133 }
4134 
4135 /*
4136 ** Check to see if the file is an ordinary SQLite database file.
4137 ** Return true iff so. Parameter sz is the file's size.
4138 */
4139 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4140   char zHdr[16];
4141   if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4142    || (sz & 0x1ff) != 0
4143    || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4144    || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4145   ){
4146     return 0;
4147   }else{
4148     return 1;
4149   }
4150 }
4151 
4152 /*
4153 ** Open an apnd file handle.
4154 */
4155 static int apndOpen(
4156   sqlite3_vfs *pApndVfs,
4157   const char *zName,
4158   sqlite3_file *pFile,
4159   int flags,
4160   int *pOutFlags
4161 ){
4162   ApndFile *pApndFile = (ApndFile*)pFile;
4163   sqlite3_file *pBaseFile = ORIGFILE(pFile);
4164   sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4165   int rc;
4166   sqlite3_int64 sz = 0;
4167   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4168     /* The appendvfs is not to be used for transient or temporary databases.
4169     ** Just use the base VFS open to initialize the given file object and
4170     ** open the underlying file. (Appendvfs is then unused for this file.)
4171     */
4172     return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4173   }
4174   memset(pApndFile, 0, sizeof(ApndFile));
4175   pFile->pMethods = &apnd_io_methods;
4176   pApndFile->iMark = -1;    /* Append mark not yet written */
4177 
4178   rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4179   if( rc==SQLITE_OK ){
4180     rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4181     if( rc ){
4182       pBaseFile->pMethods->xClose(pBaseFile);
4183     }
4184   }
4185   if( rc ){
4186     pFile->pMethods = 0;
4187     return rc;
4188   }
4189   if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4190     /* The file being opened appears to be just an ordinary DB. Copy
4191     ** the base dispatch-table so this instance mimics the base VFS.
4192     */
4193     memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4194     return SQLITE_OK;
4195   }
4196   pApndFile->iPgOne = apndReadMark(sz, pFile);
4197   if( pApndFile->iPgOne>=0 ){
4198     pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4199     return SQLITE_OK;
4200   }
4201   if( (flags & SQLITE_OPEN_CREATE)==0 ){
4202     pBaseFile->pMethods->xClose(pBaseFile);
4203     rc = SQLITE_CANTOPEN;
4204     pFile->pMethods = 0;
4205   }else{
4206     /* Round newly added appendvfs location to #define'd page boundary.
4207     ** Note that nothing has yet been written to the underlying file.
4208     ** The append mark will be written along with first content write.
4209     ** Until then, paf->iMark value indicates it is not yet written.
4210     */
4211     pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4212   }
4213   return rc;
4214 }
4215 
4216 /*
4217 ** Delete an apnd file.
4218 ** For an appendvfs, this could mean delete the appendvfs portion,
4219 ** leaving the appendee as it was before it gained an appendvfs.
4220 ** For now, this code deletes the underlying file too.
4221 */
4222 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4223   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4224 }
4225 
4226 /*
4227 ** All other VFS methods are pass-thrus.
4228 */
4229 static int apndAccess(
4230   sqlite3_vfs *pVfs,
4231   const char *zPath,
4232   int flags,
4233   int *pResOut
4234 ){
4235   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4236 }
4237 static int apndFullPathname(
4238   sqlite3_vfs *pVfs,
4239   const char *zPath,
4240   int nOut,
4241   char *zOut
4242 ){
4243   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4244 }
4245 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4246   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4247 }
4248 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4249   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4250 }
4251 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4252   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4253 }
4254 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4255   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4256 }
4257 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4258   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4259 }
4260 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4261   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4262 }
4263 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4264   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4265 }
4266 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4267   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4268 }
4269 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4270   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4271 }
4272 static int apndSetSystemCall(
4273   sqlite3_vfs *pVfs,
4274   const char *zName,
4275   sqlite3_syscall_ptr pCall
4276 ){
4277   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4278 }
4279 static sqlite3_syscall_ptr apndGetSystemCall(
4280   sqlite3_vfs *pVfs,
4281   const char *zName
4282 ){
4283   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4284 }
4285 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4286   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4287 }
4288 
4289 
4290 #ifdef _WIN32
4291 
4292 #endif
4293 /*
4294 ** This routine is called when the extension is loaded.
4295 ** Register the new VFS.
4296 */
4297 int sqlite3_appendvfs_init(
4298   sqlite3 *db,
4299   char **pzErrMsg,
4300   const sqlite3_api_routines *pApi
4301 ){
4302   int rc = SQLITE_OK;
4303   sqlite3_vfs *pOrig;
4304   SQLITE_EXTENSION_INIT2(pApi);
4305   (void)pzErrMsg;
4306   (void)db;
4307   pOrig = sqlite3_vfs_find(0);
4308   if( pOrig==0 ) return SQLITE_ERROR;
4309   apnd_vfs.iVersion = pOrig->iVersion;
4310   apnd_vfs.pAppData = pOrig;
4311   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4312   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4313 #ifdef APPENDVFS_TEST
4314   if( rc==SQLITE_OK ){
4315     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4316   }
4317 #endif
4318   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4319   return rc;
4320 }
4321 
4322 /************************* End ../ext/misc/appendvfs.c ********************/
4323 /************************* Begin ../ext/misc/memtrace.c ******************/
4324 /*
4325 ** 2019-01-21
4326 **
4327 ** The author disclaims copyright to this source code.  In place of
4328 ** a legal notice, here is a blessing:
4329 **
4330 **    May you do good and not evil.
4331 **    May you find forgiveness for yourself and forgive others.
4332 **    May you share freely, never taking more than you give.
4333 **
4334 *************************************************************************
4335 **
4336 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4337 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4338 ** is registered prior to sqlite3_initialize(), it will cause all memory
4339 ** allocation activities to be logged on standard output, or to some other
4340 ** FILE specified by the initializer.
4341 **
4342 ** This file needs to be compiled into the application that uses it.
4343 **
4344 ** This extension is used to implement the --memtrace option of the
4345 ** command-line shell.
4346 */
4347 #include <assert.h>
4348 #include <string.h>
4349 #include <stdio.h>
4350 
4351 /* The original memory allocation routines */
4352 static sqlite3_mem_methods memtraceBase;
4353 static FILE *memtraceOut;
4354 
4355 /* Methods that trace memory allocations */
4356 static void *memtraceMalloc(int n){
4357   if( memtraceOut ){
4358     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4359             memtraceBase.xRoundup(n));
4360   }
4361   return memtraceBase.xMalloc(n);
4362 }
4363 static void memtraceFree(void *p){
4364   if( p==0 ) return;
4365   if( memtraceOut ){
4366     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4367   }
4368   memtraceBase.xFree(p);
4369 }
4370 static void *memtraceRealloc(void *p, int n){
4371   if( p==0 ) return memtraceMalloc(n);
4372   if( n==0 ){
4373     memtraceFree(p);
4374     return 0;
4375   }
4376   if( memtraceOut ){
4377     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4378             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4379   }
4380   return memtraceBase.xRealloc(p, n);
4381 }
4382 static int memtraceSize(void *p){
4383   return memtraceBase.xSize(p);
4384 }
4385 static int memtraceRoundup(int n){
4386   return memtraceBase.xRoundup(n);
4387 }
4388 static int memtraceInit(void *p){
4389   return memtraceBase.xInit(p);
4390 }
4391 static void memtraceShutdown(void *p){
4392   memtraceBase.xShutdown(p);
4393 }
4394 
4395 /* The substitute memory allocator */
4396 static sqlite3_mem_methods ersaztMethods = {
4397   memtraceMalloc,
4398   memtraceFree,
4399   memtraceRealloc,
4400   memtraceSize,
4401   memtraceRoundup,
4402   memtraceInit,
4403   memtraceShutdown,
4404   0
4405 };
4406 
4407 /* Begin tracing memory allocations to out. */
4408 int sqlite3MemTraceActivate(FILE *out){
4409   int rc = SQLITE_OK;
4410   if( memtraceBase.xMalloc==0 ){
4411     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4412     if( rc==SQLITE_OK ){
4413       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4414     }
4415   }
4416   memtraceOut = out;
4417   return rc;
4418 }
4419 
4420 /* Deactivate memory tracing */
4421 int sqlite3MemTraceDeactivate(void){
4422   int rc = SQLITE_OK;
4423   if( memtraceBase.xMalloc!=0 ){
4424     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4425     if( rc==SQLITE_OK ){
4426       memset(&memtraceBase, 0, sizeof(memtraceBase));
4427     }
4428   }
4429   memtraceOut = 0;
4430   return rc;
4431 }
4432 
4433 /************************* End ../ext/misc/memtrace.c ********************/
4434 /************************* Begin ../ext/misc/uint.c ******************/
4435 /*
4436 ** 2020-04-14
4437 **
4438 ** The author disclaims copyright to this source code.  In place of
4439 ** a legal notice, here is a blessing:
4440 **
4441 **    May you do good and not evil.
4442 **    May you find forgiveness for yourself and forgive others.
4443 **    May you share freely, never taking more than you give.
4444 **
4445 ******************************************************************************
4446 **
4447 ** This SQLite extension implements the UINT collating sequence.
4448 **
4449 ** UINT works like BINARY for text, except that embedded strings
4450 ** of digits compare in numeric order.
4451 **
4452 **     *   Leading zeros are handled properly, in the sense that
4453 **         they do not mess of the maginitude comparison of embedded
4454 **         strings of digits.  "x00123y" is equal to "x123y".
4455 **
4456 **     *   Only unsigned integers are recognized.  Plus and minus
4457 **         signs are ignored.  Decimal points and exponential notation
4458 **         are ignored.
4459 **
4460 **     *   Embedded integers can be of arbitrary length.  Comparison
4461 **         is *not* limited integers that can be expressed as a
4462 **         64-bit machine integer.
4463 */
4464 /* #include "sqlite3ext.h" */
4465 SQLITE_EXTENSION_INIT1
4466 #include <assert.h>
4467 #include <string.h>
4468 #include <ctype.h>
4469 
4470 /*
4471 ** Compare text in lexicographic order, except strings of digits
4472 ** compare in numeric order.
4473 */
4474 static int uintCollFunc(
4475   void *notUsed,
4476   int nKey1, const void *pKey1,
4477   int nKey2, const void *pKey2
4478 ){
4479   const unsigned char *zA = (const unsigned char*)pKey1;
4480   const unsigned char *zB = (const unsigned char*)pKey2;
4481   int i=0, j=0, x;
4482   (void)notUsed;
4483   while( i<nKey1 && j<nKey2 ){
4484     x = zA[i] - zB[j];
4485     if( isdigit(zA[i]) ){
4486       int k;
4487       if( !isdigit(zB[j]) ) return x;
4488       while( i<nKey1 && zA[i]=='0' ){ i++; }
4489       while( j<nKey2 && zB[j]=='0' ){ j++; }
4490       k = 0;
4491       while( i+k<nKey1 && isdigit(zA[i+k])
4492              && j+k<nKey2 && isdigit(zB[j+k]) ){
4493         k++;
4494       }
4495       if( i+k<nKey1 && isdigit(zA[i+k]) ){
4496         return +1;
4497       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4498         return -1;
4499       }else{
4500         x = memcmp(zA+i, zB+j, k);
4501         if( x ) return x;
4502         i += k;
4503         j += k;
4504       }
4505     }else if( x ){
4506       return x;
4507     }else{
4508       i++;
4509       j++;
4510     }
4511   }
4512   return (nKey1 - i) - (nKey2 - j);
4513 }
4514 
4515 #ifdef _WIN32
4516 
4517 #endif
4518 int sqlite3_uint_init(
4519   sqlite3 *db,
4520   char **pzErrMsg,
4521   const sqlite3_api_routines *pApi
4522 ){
4523   SQLITE_EXTENSION_INIT2(pApi);
4524   (void)pzErrMsg;  /* Unused parameter */
4525   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4526 }
4527 
4528 /************************* End ../ext/misc/uint.c ********************/
4529 /************************* Begin ../ext/misc/decimal.c ******************/
4530 /*
4531 ** 2020-06-22
4532 **
4533 ** The author disclaims copyright to this source code.  In place of
4534 ** a legal notice, here is a blessing:
4535 **
4536 **    May you do good and not evil.
4537 **    May you find forgiveness for yourself and forgive others.
4538 **    May you share freely, never taking more than you give.
4539 **
4540 ******************************************************************************
4541 **
4542 ** Routines to implement arbitrary-precision decimal math.
4543 **
4544 ** The focus here is on simplicity and correctness, not performance.
4545 */
4546 /* #include "sqlite3ext.h" */
4547 SQLITE_EXTENSION_INIT1
4548 #include <assert.h>
4549 #include <string.h>
4550 #include <ctype.h>
4551 #include <stdlib.h>
4552 
4553 /* Mark a function parameter as unused, to suppress nuisance compiler
4554 ** warnings. */
4555 #ifndef UNUSED_PARAMETER
4556 # define UNUSED_PARAMETER(X)  (void)(X)
4557 #endif
4558 
4559 
4560 /* A decimal object */
4561 typedef struct Decimal Decimal;
4562 struct Decimal {
4563   char sign;        /* 0 for positive, 1 for negative */
4564   char oom;         /* True if an OOM is encountered */
4565   char isNull;      /* True if holds a NULL rather than a number */
4566   char isInit;      /* True upon initialization */
4567   int nDigit;       /* Total number of digits */
4568   int nFrac;        /* Number of digits to the right of the decimal point */
4569   signed char *a;   /* Array of digits.  Most significant first. */
4570 };
4571 
4572 /*
4573 ** Release memory held by a Decimal, but do not free the object itself.
4574 */
4575 static void decimal_clear(Decimal *p){
4576   sqlite3_free(p->a);
4577 }
4578 
4579 /*
4580 ** Destroy a Decimal object
4581 */
4582 static void decimal_free(Decimal *p){
4583   if( p ){
4584     decimal_clear(p);
4585     sqlite3_free(p);
4586   }
4587 }
4588 
4589 /*
4590 ** Allocate a new Decimal object.  Initialize it to the number given
4591 ** by the input string.
4592 */
4593 static Decimal *decimal_new(
4594   sqlite3_context *pCtx,
4595   sqlite3_value *pIn,
4596   int nAlt,
4597   const unsigned char *zAlt
4598 ){
4599   Decimal *p;
4600   int n, i;
4601   const unsigned char *zIn;
4602   int iExp = 0;
4603   p = sqlite3_malloc( sizeof(*p) );
4604   if( p==0 ) goto new_no_mem;
4605   p->sign = 0;
4606   p->oom = 0;
4607   p->isInit = 1;
4608   p->isNull = 0;
4609   p->nDigit = 0;
4610   p->nFrac = 0;
4611   if( zAlt ){
4612     n = nAlt,
4613     zIn = zAlt;
4614   }else{
4615     if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4616       p->a = 0;
4617       p->isNull = 1;
4618       return p;
4619     }
4620     n = sqlite3_value_bytes(pIn);
4621     zIn = sqlite3_value_text(pIn);
4622   }
4623   p->a = sqlite3_malloc64( n+1 );
4624   if( p->a==0 ) goto new_no_mem;
4625   for(i=0; isspace(zIn[i]); i++){}
4626   if( zIn[i]=='-' ){
4627     p->sign = 1;
4628     i++;
4629   }else if( zIn[i]=='+' ){
4630     i++;
4631   }
4632   while( i<n && zIn[i]=='0' ) i++;
4633   while( i<n ){
4634     char c = zIn[i];
4635     if( c>='0' && c<='9' ){
4636       p->a[p->nDigit++] = c - '0';
4637     }else if( c=='.' ){
4638       p->nFrac = p->nDigit + 1;
4639     }else if( c=='e' || c=='E' ){
4640       int j = i+1;
4641       int neg = 0;
4642       if( j>=n ) break;
4643       if( zIn[j]=='-' ){
4644         neg = 1;
4645         j++;
4646       }else if( zIn[j]=='+' ){
4647         j++;
4648       }
4649       while( j<n && iExp<1000000 ){
4650         if( zIn[j]>='0' && zIn[j]<='9' ){
4651           iExp = iExp*10 + zIn[j] - '0';
4652         }
4653         j++;
4654       }
4655       if( neg ) iExp = -iExp;
4656       break;
4657     }
4658     i++;
4659   }
4660   if( p->nFrac ){
4661     p->nFrac = p->nDigit - (p->nFrac - 1);
4662   }
4663   if( iExp>0 ){
4664     if( p->nFrac>0 ){
4665       if( iExp<=p->nFrac ){
4666         p->nFrac -= iExp;
4667         iExp = 0;
4668       }else{
4669         iExp -= p->nFrac;
4670         p->nFrac = 0;
4671       }
4672     }
4673     if( iExp>0 ){
4674       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4675       if( p->a==0 ) goto new_no_mem;
4676       memset(p->a+p->nDigit, 0, iExp);
4677       p->nDigit += iExp;
4678     }
4679   }else if( iExp<0 ){
4680     int nExtra;
4681     iExp = -iExp;
4682     nExtra = p->nDigit - p->nFrac - 1;
4683     if( nExtra ){
4684       if( nExtra>=iExp ){
4685         p->nFrac += iExp;
4686         iExp  = 0;
4687       }else{
4688         iExp -= nExtra;
4689         p->nFrac = p->nDigit - 1;
4690       }
4691     }
4692     if( iExp>0 ){
4693       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4694       if( p->a==0 ) goto new_no_mem;
4695       memmove(p->a+iExp, p->a, p->nDigit);
4696       memset(p->a, 0, iExp);
4697       p->nDigit += iExp;
4698       p->nFrac += iExp;
4699     }
4700   }
4701   return p;
4702 
4703 new_no_mem:
4704   if( pCtx ) sqlite3_result_error_nomem(pCtx);
4705   sqlite3_free(p);
4706   return 0;
4707 }
4708 
4709 /*
4710 ** Make the given Decimal the result.
4711 */
4712 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4713   char *z;
4714   int i, j;
4715   int n;
4716   if( p==0 || p->oom ){
4717     sqlite3_result_error_nomem(pCtx);
4718     return;
4719   }
4720   if( p->isNull ){
4721     sqlite3_result_null(pCtx);
4722     return;
4723   }
4724   z = sqlite3_malloc( p->nDigit+4 );
4725   if( z==0 ){
4726     sqlite3_result_error_nomem(pCtx);
4727     return;
4728   }
4729   i = 0;
4730   if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4731     p->sign = 0;
4732   }
4733   if( p->sign ){
4734     z[0] = '-';
4735     i = 1;
4736   }
4737   n = p->nDigit - p->nFrac;
4738   if( n<=0 ){
4739     z[i++] = '0';
4740   }
4741   j = 0;
4742   while( n>1 && p->a[j]==0 ){
4743     j++;
4744     n--;
4745   }
4746   while( n>0  ){
4747     z[i++] = p->a[j] + '0';
4748     j++;
4749     n--;
4750   }
4751   if( p->nFrac ){
4752     z[i++] = '.';
4753     do{
4754       z[i++] = p->a[j] + '0';
4755       j++;
4756     }while( j<p->nDigit );
4757   }
4758   z[i] = 0;
4759   sqlite3_result_text(pCtx, z, i, sqlite3_free);
4760 }
4761 
4762 /*
4763 ** SQL Function:   decimal(X)
4764 **
4765 ** Convert input X into decimal and then back into text
4766 */
4767 static void decimalFunc(
4768   sqlite3_context *context,
4769   int argc,
4770   sqlite3_value **argv
4771 ){
4772   Decimal *p = decimal_new(context, argv[0], 0, 0);
4773   UNUSED_PARAMETER(argc);
4774   decimal_result(context, p);
4775   decimal_free(p);
4776 }
4777 
4778 /*
4779 ** Compare to Decimal objects.  Return negative, 0, or positive if the
4780 ** first object is less than, equal to, or greater than the second.
4781 **
4782 ** Preconditions for this routine:
4783 **
4784 **    pA!=0
4785 **    pA->isNull==0
4786 **    pB!=0
4787 **    pB->isNull==0
4788 */
4789 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4790   int nASig, nBSig, rc, n;
4791   if( pA->sign!=pB->sign ){
4792     return pA->sign ? -1 : +1;
4793   }
4794   if( pA->sign ){
4795     const Decimal *pTemp = pA;
4796     pA = pB;
4797     pB = pTemp;
4798   }
4799   nASig = pA->nDigit - pA->nFrac;
4800   nBSig = pB->nDigit - pB->nFrac;
4801   if( nASig!=nBSig ){
4802     return nASig - nBSig;
4803   }
4804   n = pA->nDigit;
4805   if( n>pB->nDigit ) n = pB->nDigit;
4806   rc = memcmp(pA->a, pB->a, n);
4807   if( rc==0 ){
4808     rc = pA->nDigit - pB->nDigit;
4809   }
4810   return rc;
4811 }
4812 
4813 /*
4814 ** SQL Function:   decimal_cmp(X, Y)
4815 **
4816 ** Return negative, zero, or positive if X is less then, equal to, or
4817 ** greater than Y.
4818 */
4819 static void decimalCmpFunc(
4820   sqlite3_context *context,
4821   int argc,
4822   sqlite3_value **argv
4823 ){
4824   Decimal *pA = 0, *pB = 0;
4825   int rc;
4826 
4827   UNUSED_PARAMETER(argc);
4828   pA = decimal_new(context, argv[0], 0, 0);
4829   if( pA==0 || pA->isNull ) goto cmp_done;
4830   pB = decimal_new(context, argv[1], 0, 0);
4831   if( pB==0 || pB->isNull ) goto cmp_done;
4832   rc = decimal_cmp(pA, pB);
4833   if( rc<0 ) rc = -1;
4834   else if( rc>0 ) rc = +1;
4835   sqlite3_result_int(context, rc);
4836 cmp_done:
4837   decimal_free(pA);
4838   decimal_free(pB);
4839 }
4840 
4841 /*
4842 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4843 ** digits to the right of the decimal point.
4844 */
4845 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4846   int nAddSig;
4847   int nAddFrac;
4848   if( p==0 ) return;
4849   nAddFrac = nFrac - p->nFrac;
4850   nAddSig = (nDigit - p->nDigit) - nAddFrac;
4851   if( nAddFrac==0 && nAddSig==0 ) return;
4852   p->a = sqlite3_realloc64(p->a, nDigit+1);
4853   if( p->a==0 ){
4854     p->oom = 1;
4855     return;
4856   }
4857   if( nAddSig ){
4858     memmove(p->a+nAddSig, p->a, p->nDigit);
4859     memset(p->a, 0, nAddSig);
4860     p->nDigit += nAddSig;
4861   }
4862   if( nAddFrac ){
4863     memset(p->a+p->nDigit, 0, nAddFrac);
4864     p->nDigit += nAddFrac;
4865     p->nFrac += nAddFrac;
4866   }
4867 }
4868 
4869 /*
4870 ** Add the value pB into pA.
4871 **
4872 ** Both pA and pB might become denormalized by this routine.
4873 */
4874 static void decimal_add(Decimal *pA, Decimal *pB){
4875   int nSig, nFrac, nDigit;
4876   int i, rc;
4877   if( pA==0 ){
4878     return;
4879   }
4880   if( pA->oom || pB==0 || pB->oom ){
4881     pA->oom = 1;
4882     return;
4883   }
4884   if( pA->isNull || pB->isNull ){
4885     pA->isNull = 1;
4886     return;
4887   }
4888   nSig = pA->nDigit - pA->nFrac;
4889   if( nSig && pA->a[0]==0 ) nSig--;
4890   if( nSig<pB->nDigit-pB->nFrac ){
4891     nSig = pB->nDigit - pB->nFrac;
4892   }
4893   nFrac = pA->nFrac;
4894   if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4895   nDigit = nSig + nFrac + 1;
4896   decimal_expand(pA, nDigit, nFrac);
4897   decimal_expand(pB, nDigit, nFrac);
4898   if( pA->oom || pB->oom ){
4899     pA->oom = 1;
4900   }else{
4901     if( pA->sign==pB->sign ){
4902       int carry = 0;
4903       for(i=nDigit-1; i>=0; i--){
4904         int x = pA->a[i] + pB->a[i] + carry;
4905         if( x>=10 ){
4906           carry = 1;
4907           pA->a[i] = x - 10;
4908         }else{
4909           carry = 0;
4910           pA->a[i] = x;
4911         }
4912       }
4913     }else{
4914       signed char *aA, *aB;
4915       int borrow = 0;
4916       rc = memcmp(pA->a, pB->a, nDigit);
4917       if( rc<0 ){
4918         aA = pB->a;
4919         aB = pA->a;
4920         pA->sign = !pA->sign;
4921       }else{
4922         aA = pA->a;
4923         aB = pB->a;
4924       }
4925       for(i=nDigit-1; i>=0; i--){
4926         int x = aA[i] - aB[i] - borrow;
4927         if( x<0 ){
4928           pA->a[i] = x+10;
4929           borrow = 1;
4930         }else{
4931           pA->a[i] = x;
4932           borrow = 0;
4933         }
4934       }
4935     }
4936   }
4937 }
4938 
4939 /*
4940 ** Compare text in decimal order.
4941 */
4942 static int decimalCollFunc(
4943   void *notUsed,
4944   int nKey1, const void *pKey1,
4945   int nKey2, const void *pKey2
4946 ){
4947   const unsigned char *zA = (const unsigned char*)pKey1;
4948   const unsigned char *zB = (const unsigned char*)pKey2;
4949   Decimal *pA = decimal_new(0, 0, nKey1, zA);
4950   Decimal *pB = decimal_new(0, 0, nKey2, zB);
4951   int rc;
4952   UNUSED_PARAMETER(notUsed);
4953   if( pA==0 || pB==0 ){
4954     rc = 0;
4955   }else{
4956     rc = decimal_cmp(pA, pB);
4957   }
4958   decimal_free(pA);
4959   decimal_free(pB);
4960   return rc;
4961 }
4962 
4963 
4964 /*
4965 ** SQL Function:   decimal_add(X, Y)
4966 **                 decimal_sub(X, Y)
4967 **
4968 ** Return the sum or difference of X and Y.
4969 */
4970 static void decimalAddFunc(
4971   sqlite3_context *context,
4972   int argc,
4973   sqlite3_value **argv
4974 ){
4975   Decimal *pA = decimal_new(context, argv[0], 0, 0);
4976   Decimal *pB = decimal_new(context, argv[1], 0, 0);
4977   UNUSED_PARAMETER(argc);
4978   decimal_add(pA, pB);
4979   decimal_result(context, pA);
4980   decimal_free(pA);
4981   decimal_free(pB);
4982 }
4983 static void decimalSubFunc(
4984   sqlite3_context *context,
4985   int argc,
4986   sqlite3_value **argv
4987 ){
4988   Decimal *pA = decimal_new(context, argv[0], 0, 0);
4989   Decimal *pB = decimal_new(context, argv[1], 0, 0);
4990   UNUSED_PARAMETER(argc);
4991   if( pB ){
4992     pB->sign = !pB->sign;
4993     decimal_add(pA, pB);
4994     decimal_result(context, pA);
4995   }
4996   decimal_free(pA);
4997   decimal_free(pB);
4998 }
4999 
5000 /* Aggregate funcion:   decimal_sum(X)
5001 **
5002 ** Works like sum() except that it uses decimal arithmetic for unlimited
5003 ** precision.
5004 */
5005 static void decimalSumStep(
5006   sqlite3_context *context,
5007   int argc,
5008   sqlite3_value **argv
5009 ){
5010   Decimal *p;
5011   Decimal *pArg;
5012   UNUSED_PARAMETER(argc);
5013   p = sqlite3_aggregate_context(context, sizeof(*p));
5014   if( p==0 ) return;
5015   if( !p->isInit ){
5016     p->isInit = 1;
5017     p->a = sqlite3_malloc(2);
5018     if( p->a==0 ){
5019       p->oom = 1;
5020     }else{
5021       p->a[0] = 0;
5022     }
5023     p->nDigit = 1;
5024     p->nFrac = 0;
5025   }
5026   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5027   pArg = decimal_new(context, argv[0], 0, 0);
5028   decimal_add(p, pArg);
5029   decimal_free(pArg);
5030 }
5031 static void decimalSumInverse(
5032   sqlite3_context *context,
5033   int argc,
5034   sqlite3_value **argv
5035 ){
5036   Decimal *p;
5037   Decimal *pArg;
5038   UNUSED_PARAMETER(argc);
5039   p = sqlite3_aggregate_context(context, sizeof(*p));
5040   if( p==0 ) return;
5041   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5042   pArg = decimal_new(context, argv[0], 0, 0);
5043   if( pArg ) pArg->sign = !pArg->sign;
5044   decimal_add(p, pArg);
5045   decimal_free(pArg);
5046 }
5047 static void decimalSumValue(sqlite3_context *context){
5048   Decimal *p = sqlite3_aggregate_context(context, 0);
5049   if( p==0 ) return;
5050   decimal_result(context, p);
5051 }
5052 static void decimalSumFinalize(sqlite3_context *context){
5053   Decimal *p = sqlite3_aggregate_context(context, 0);
5054   if( p==0 ) return;
5055   decimal_result(context, p);
5056   decimal_clear(p);
5057 }
5058 
5059 /*
5060 ** SQL Function:   decimal_mul(X, Y)
5061 **
5062 ** Return the product of X and Y.
5063 **
5064 ** All significant digits after the decimal point are retained.
5065 ** Trailing zeros after the decimal point are omitted as long as
5066 ** the number of digits after the decimal point is no less than
5067 ** either the number of digits in either input.
5068 */
5069 static void decimalMulFunc(
5070   sqlite3_context *context,
5071   int argc,
5072   sqlite3_value **argv
5073 ){
5074   Decimal *pA = decimal_new(context, argv[0], 0, 0);
5075   Decimal *pB = decimal_new(context, argv[1], 0, 0);
5076   signed char *acc = 0;
5077   int i, j, k;
5078   int minFrac;
5079   UNUSED_PARAMETER(argc);
5080   if( pA==0 || pA->oom || pA->isNull
5081    || pB==0 || pB->oom || pB->isNull
5082   ){
5083     goto mul_end;
5084   }
5085   acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5086   if( acc==0 ){
5087     sqlite3_result_error_nomem(context);
5088     goto mul_end;
5089   }
5090   memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5091   minFrac = pA->nFrac;
5092   if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5093   for(i=pA->nDigit-1; i>=0; i--){
5094     signed char f = pA->a[i];
5095     int carry = 0, x;
5096     for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5097       x = acc[k] + f*pB->a[j] + carry;
5098       acc[k] = x%10;
5099       carry = x/10;
5100     }
5101     x = acc[k] + carry;
5102     acc[k] = x%10;
5103     acc[k-1] += x/10;
5104   }
5105   sqlite3_free(pA->a);
5106   pA->a = acc;
5107   acc = 0;
5108   pA->nDigit += pB->nDigit + 2;
5109   pA->nFrac += pB->nFrac;
5110   pA->sign ^= pB->sign;
5111   while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5112     pA->nFrac--;
5113     pA->nDigit--;
5114   }
5115   decimal_result(context, pA);
5116 
5117 mul_end:
5118   sqlite3_free(acc);
5119   decimal_free(pA);
5120   decimal_free(pB);
5121 }
5122 
5123 #ifdef _WIN32
5124 
5125 #endif
5126 int sqlite3_decimal_init(
5127   sqlite3 *db,
5128   char **pzErrMsg,
5129   const sqlite3_api_routines *pApi
5130 ){
5131   int rc = SQLITE_OK;
5132   static const struct {
5133     const char *zFuncName;
5134     int nArg;
5135     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5136   } aFunc[] = {
5137     { "decimal",       1,   decimalFunc        },
5138     { "decimal_cmp",   2,   decimalCmpFunc     },
5139     { "decimal_add",   2,   decimalAddFunc     },
5140     { "decimal_sub",   2,   decimalSubFunc     },
5141     { "decimal_mul",   2,   decimalMulFunc     },
5142   };
5143   unsigned int i;
5144   (void)pzErrMsg;  /* Unused parameter */
5145 
5146   SQLITE_EXTENSION_INIT2(pApi);
5147 
5148   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5149     rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5150                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5151                    0, aFunc[i].xFunc, 0, 0);
5152   }
5153   if( rc==SQLITE_OK ){
5154     rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5155                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5156                    decimalSumStep, decimalSumFinalize,
5157                    decimalSumValue, decimalSumInverse, 0);
5158   }
5159   if( rc==SQLITE_OK ){
5160     rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5161                                   0, decimalCollFunc);
5162   }
5163   return rc;
5164 }
5165 
5166 /************************* End ../ext/misc/decimal.c ********************/
5167 /************************* Begin ../ext/misc/ieee754.c ******************/
5168 /*
5169 ** 2013-04-17
5170 **
5171 ** The author disclaims copyright to this source code.  In place of
5172 ** a legal notice, here is a blessing:
5173 **
5174 **    May you do good and not evil.
5175 **    May you find forgiveness for yourself and forgive others.
5176 **    May you share freely, never taking more than you give.
5177 **
5178 ******************************************************************************
5179 **
5180 ** This SQLite extension implements functions for the exact display
5181 ** and input of IEEE754 Binary64 floating-point numbers.
5182 **
5183 **   ieee754(X)
5184 **   ieee754(Y,Z)
5185 **
5186 ** In the first form, the value X should be a floating-point number.
5187 ** The function will return a string of the form 'ieee754(Y,Z)' where
5188 ** Y and Z are integers such that X==Y*pow(2,Z).
5189 **
5190 ** In the second form, Y and Z are integers which are the mantissa and
5191 ** base-2 exponent of a new floating point number.  The function returns
5192 ** a floating-point value equal to Y*pow(2,Z).
5193 **
5194 ** Examples:
5195 **
5196 **     ieee754(2.0)             ->     'ieee754(2,0)'
5197 **     ieee754(45.25)           ->     'ieee754(181,-2)'
5198 **     ieee754(2, 0)            ->     2.0
5199 **     ieee754(181, -2)         ->     45.25
5200 **
5201 ** Two additional functions break apart the one-argument ieee754()
5202 ** result into separate integer values:
5203 **
5204 **     ieee754_mantissa(45.25)  ->     181
5205 **     ieee754_exponent(45.25)  ->     -2
5206 **
5207 ** These functions convert binary64 numbers into blobs and back again.
5208 **
5209 **     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5210 **     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5211 **
5212 ** In all single-argument functions, if the argument is an 8-byte blob
5213 ** then that blob is interpreted as a big-endian binary64 value.
5214 **
5215 **
5216 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5217 ** -----------------------------------------------
5218 **
5219 ** This extension in combination with the separate 'decimal' extension
5220 ** can be used to compute the exact decimal representation of binary64
5221 ** values.  To begin, first compute a table of exponent values:
5222 **
5223 **    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5224 **    WITH RECURSIVE c(x,v) AS (
5225 **      VALUES(0,'1')
5226 **      UNION ALL
5227 **      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5228 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5229 **    WITH RECURSIVE c(x,v) AS (
5230 **      VALUES(-1,'0.5')
5231 **      UNION ALL
5232 **      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5233 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5234 **
5235 ** Then, to compute the exact decimal representation of a floating
5236 ** point value (the value 47.49 is used in the example) do:
5237 **
5238 **    WITH c(n) AS (VALUES(47.49))
5239 **          ---------------^^^^^---- Replace with whatever you want
5240 **    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5241 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5242 **
5243 ** Here is a query to show various boundry values for the binary64
5244 ** number format:
5245 **
5246 **    WITH c(name,bin) AS (VALUES
5247 **       ('minimum positive value',        x'0000000000000001'),
5248 **       ('maximum subnormal value',       x'000fffffffffffff'),
5249 **       ('mininum positive nornal value', x'0010000000000000'),
5250 **       ('maximum value',                 x'7fefffffffffffff'))
5251 **    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5252 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5253 **
5254 */
5255 /* #include "sqlite3ext.h" */
5256 SQLITE_EXTENSION_INIT1
5257 #include <assert.h>
5258 #include <string.h>
5259 
5260 /* Mark a function parameter as unused, to suppress nuisance compiler
5261 ** warnings. */
5262 #ifndef UNUSED_PARAMETER
5263 # define UNUSED_PARAMETER(X)  (void)(X)
5264 #endif
5265 
5266 /*
5267 ** Implementation of the ieee754() function
5268 */
5269 static void ieee754func(
5270   sqlite3_context *context,
5271   int argc,
5272   sqlite3_value **argv
5273 ){
5274   if( argc==1 ){
5275     sqlite3_int64 m, a;
5276     double r;
5277     int e;
5278     int isNeg;
5279     char zResult[100];
5280     assert( sizeof(m)==sizeof(r) );
5281     if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5282      && sqlite3_value_bytes(argv[0])==sizeof(r)
5283     ){
5284       const unsigned char *x = sqlite3_value_blob(argv[0]);
5285       unsigned int i;
5286       sqlite3_uint64 v = 0;
5287       for(i=0; i<sizeof(r); i++){
5288         v = (v<<8) | x[i];
5289       }
5290       memcpy(&r, &v, sizeof(r));
5291     }else{
5292       r = sqlite3_value_double(argv[0]);
5293     }
5294     if( r<0.0 ){
5295       isNeg = 1;
5296       r = -r;
5297     }else{
5298       isNeg = 0;
5299     }
5300     memcpy(&a,&r,sizeof(a));
5301     if( a==0 ){
5302       e = 0;
5303       m = 0;
5304     }else{
5305       e = a>>52;
5306       m = a & ((((sqlite3_int64)1)<<52)-1);
5307       if( e==0 ){
5308         m <<= 1;
5309       }else{
5310         m |= ((sqlite3_int64)1)<<52;
5311       }
5312       while( e<1075 && m>0 && (m&1)==0 ){
5313         m >>= 1;
5314         e++;
5315       }
5316       if( isNeg ) m = -m;
5317     }
5318     switch( *(int*)sqlite3_user_data(context) ){
5319       case 0:
5320         sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5321                          m, e-1075);
5322         sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5323         break;
5324       case 1:
5325         sqlite3_result_int64(context, m);
5326         break;
5327       case 2:
5328         sqlite3_result_int(context, e-1075);
5329         break;
5330     }
5331   }else{
5332     sqlite3_int64 m, e, a;
5333     double r;
5334     int isNeg = 0;
5335     m = sqlite3_value_int64(argv[0]);
5336     e = sqlite3_value_int64(argv[1]);
5337 
5338     /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
5339     if( e>10000 ){
5340       e = 10000;
5341     }else if( e<-10000 ){
5342       e = -10000;
5343     }
5344 
5345     if( m<0 ){
5346       isNeg = 1;
5347       m = -m;
5348       if( m<0 ) return;
5349     }else if( m==0 && e>-1000 && e<1000 ){
5350       sqlite3_result_double(context, 0.0);
5351       return;
5352     }
5353     while( (m>>32)&0xffe00000 ){
5354       m >>= 1;
5355       e++;
5356     }
5357     while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5358       m <<= 1;
5359       e--;
5360     }
5361     e += 1075;
5362     if( e<=0 ){
5363       /* Subnormal */
5364       if( 1-e >= 64 ){
5365         m = 0;
5366       }else{
5367         m >>= 1-e;
5368       }
5369       e = 0;
5370     }else if( e>0x7ff ){
5371       e = 0x7ff;
5372     }
5373     a = m & ((((sqlite3_int64)1)<<52)-1);
5374     a |= e<<52;
5375     if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5376     memcpy(&r, &a, sizeof(r));
5377     sqlite3_result_double(context, r);
5378   }
5379 }
5380 
5381 /*
5382 ** Functions to convert between blobs and floats.
5383 */
5384 static void ieee754func_from_blob(
5385   sqlite3_context *context,
5386   int argc,
5387   sqlite3_value **argv
5388 ){
5389   UNUSED_PARAMETER(argc);
5390   if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5391    && sqlite3_value_bytes(argv[0])==sizeof(double)
5392   ){
5393     double r;
5394     const unsigned char *x = sqlite3_value_blob(argv[0]);
5395     unsigned int i;
5396     sqlite3_uint64 v = 0;
5397     for(i=0; i<sizeof(r); i++){
5398       v = (v<<8) | x[i];
5399     }
5400     memcpy(&r, &v, sizeof(r));
5401     sqlite3_result_double(context, r);
5402   }
5403 }
5404 static void ieee754func_to_blob(
5405   sqlite3_context *context,
5406   int argc,
5407   sqlite3_value **argv
5408 ){
5409   UNUSED_PARAMETER(argc);
5410   if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5411    || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5412   ){
5413     double r = sqlite3_value_double(argv[0]);
5414     sqlite3_uint64 v;
5415     unsigned char a[sizeof(r)];
5416     unsigned int i;
5417     memcpy(&v, &r, sizeof(r));
5418     for(i=1; i<=sizeof(r); i++){
5419       a[sizeof(r)-i] = v&0xff;
5420       v >>= 8;
5421     }
5422     sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5423   }
5424 }
5425 
5426 
5427 #ifdef _WIN32
5428 
5429 #endif
5430 int sqlite3_ieee_init(
5431   sqlite3 *db,
5432   char **pzErrMsg,
5433   const sqlite3_api_routines *pApi
5434 ){
5435   static const struct {
5436     char *zFName;
5437     int nArg;
5438     int iAux;
5439     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5440   } aFunc[] = {
5441     { "ieee754",           1,   0, ieee754func },
5442     { "ieee754",           2,   0, ieee754func },
5443     { "ieee754_mantissa",  1,   1, ieee754func },
5444     { "ieee754_exponent",  1,   2, ieee754func },
5445     { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5446     { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5447 
5448   };
5449   unsigned int i;
5450   int rc = SQLITE_OK;
5451   SQLITE_EXTENSION_INIT2(pApi);
5452   (void)pzErrMsg;  /* Unused parameter */
5453   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5454     rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5455                                SQLITE_UTF8|SQLITE_INNOCUOUS,
5456                                (void*)&aFunc[i].iAux,
5457                                aFunc[i].xFunc, 0, 0);
5458   }
5459   return rc;
5460 }
5461 
5462 /************************* End ../ext/misc/ieee754.c ********************/
5463 /************************* Begin ../ext/misc/series.c ******************/
5464 /*
5465 ** 2015-08-18
5466 **
5467 ** The author disclaims copyright to this source code.  In place of
5468 ** a legal notice, here is a blessing:
5469 **
5470 **    May you do good and not evil.
5471 **    May you find forgiveness for yourself and forgive others.
5472 **    May you share freely, never taking more than you give.
5473 **
5474 *************************************************************************
5475 **
5476 ** This file demonstrates how to create a table-valued-function using
5477 ** a virtual table.  This demo implements the generate_series() function
5478 ** which gives similar results to the eponymous function in PostgreSQL.
5479 ** Examples:
5480 **
5481 **      SELECT * FROM generate_series(0,100,5);
5482 **
5483 ** The query above returns integers from 0 through 100 counting by steps
5484 ** of 5.
5485 **
5486 **      SELECT * FROM generate_series(0,100);
5487 **
5488 ** Integers from 0 through 100 with a step size of 1.
5489 **
5490 **      SELECT * FROM generate_series(20) LIMIT 10;
5491 **
5492 ** Integers 20 through 29.
5493 **
5494 ** HOW IT WORKS
5495 **
5496 ** The generate_series "function" is really a virtual table with the
5497 ** following schema:
5498 **
5499 **     CREATE TABLE generate_series(
5500 **       value,
5501 **       start HIDDEN,
5502 **       stop HIDDEN,
5503 **       step HIDDEN
5504 **     );
5505 **
5506 ** Function arguments in queries against this virtual table are translated
5507 ** into equality constraints against successive hidden columns.  In other
5508 ** words, the following pairs of queries are equivalent to each other:
5509 **
5510 **    SELECT * FROM generate_series(0,100,5);
5511 **    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5512 **
5513 **    SELECT * FROM generate_series(0,100);
5514 **    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5515 **
5516 **    SELECT * FROM generate_series(20) LIMIT 10;
5517 **    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5518 **
5519 ** The generate_series virtual table implementation leaves the xCreate method
5520 ** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5521 ** TABLE command with "generate_series" as the USING argument.  Instead, there
5522 ** is a single generate_series virtual table that is always available without
5523 ** having to be created first.
5524 **
5525 ** The xBestIndex method looks for equality constraints against the hidden
5526 ** start, stop, and step columns, and if present, it uses those constraints
5527 ** to bound the sequence of generated values.  If the equality constraints
5528 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5529 ** xBestIndex returns a small cost when both start and stop are available,
5530 ** and a very large cost if either start or stop are unavailable.  This
5531 ** encourages the query planner to order joins such that the bounds of the
5532 ** series are well-defined.
5533 */
5534 /* #include "sqlite3ext.h" */
5535 SQLITE_EXTENSION_INIT1
5536 #include <assert.h>
5537 #include <string.h>
5538 
5539 #ifndef SQLITE_OMIT_VIRTUALTABLE
5540 
5541 
5542 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5543 ** serve as the underlying representation of a cursor that scans
5544 ** over rows of the result
5545 */
5546 typedef struct series_cursor series_cursor;
5547 struct series_cursor {
5548   sqlite3_vtab_cursor base;  /* Base class - must be first */
5549   int isDesc;                /* True to count down rather than up */
5550   sqlite3_int64 iRowid;      /* The rowid */
5551   sqlite3_int64 iValue;      /* Current value ("value") */
5552   sqlite3_int64 mnValue;     /* Mimimum value ("start") */
5553   sqlite3_int64 mxValue;     /* Maximum value ("stop") */
5554   sqlite3_int64 iStep;       /* Increment ("step") */
5555 };
5556 
5557 /*
5558 ** The seriesConnect() method is invoked to create a new
5559 ** series_vtab that describes the generate_series virtual table.
5560 **
5561 ** Think of this routine as the constructor for series_vtab objects.
5562 **
5563 ** All this routine needs to do is:
5564 **
5565 **    (1) Allocate the series_vtab object and initialize all fields.
5566 **
5567 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5568 **        result set of queries against generate_series will look like.
5569 */
5570 static int seriesConnect(
5571   sqlite3 *db,
5572   void *pUnused,
5573   int argcUnused, const char *const*argvUnused,
5574   sqlite3_vtab **ppVtab,
5575   char **pzErrUnused
5576 ){
5577   sqlite3_vtab *pNew;
5578   int rc;
5579 
5580 /* Column numbers */
5581 #define SERIES_COLUMN_VALUE 0
5582 #define SERIES_COLUMN_START 1
5583 #define SERIES_COLUMN_STOP  2
5584 #define SERIES_COLUMN_STEP  3
5585 
5586   (void)pUnused;
5587   (void)argcUnused;
5588   (void)argvUnused;
5589   (void)pzErrUnused;
5590   rc = sqlite3_declare_vtab(db,
5591      "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5592   if( rc==SQLITE_OK ){
5593     pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5594     if( pNew==0 ) return SQLITE_NOMEM;
5595     memset(pNew, 0, sizeof(*pNew));
5596     sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5597   }
5598   return rc;
5599 }
5600 
5601 /*
5602 ** This method is the destructor for series_cursor objects.
5603 */
5604 static int seriesDisconnect(sqlite3_vtab *pVtab){
5605   sqlite3_free(pVtab);
5606   return SQLITE_OK;
5607 }
5608 
5609 /*
5610 ** Constructor for a new series_cursor object.
5611 */
5612 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5613   series_cursor *pCur;
5614   (void)pUnused;
5615   pCur = sqlite3_malloc( sizeof(*pCur) );
5616   if( pCur==0 ) return SQLITE_NOMEM;
5617   memset(pCur, 0, sizeof(*pCur));
5618   *ppCursor = &pCur->base;
5619   return SQLITE_OK;
5620 }
5621 
5622 /*
5623 ** Destructor for a series_cursor.
5624 */
5625 static int seriesClose(sqlite3_vtab_cursor *cur){
5626   sqlite3_free(cur);
5627   return SQLITE_OK;
5628 }
5629 
5630 
5631 /*
5632 ** Advance a series_cursor to its next row of output.
5633 */
5634 static int seriesNext(sqlite3_vtab_cursor *cur){
5635   series_cursor *pCur = (series_cursor*)cur;
5636   if( pCur->isDesc ){
5637     pCur->iValue -= pCur->iStep;
5638   }else{
5639     pCur->iValue += pCur->iStep;
5640   }
5641   pCur->iRowid++;
5642   return SQLITE_OK;
5643 }
5644 
5645 /*
5646 ** Return values of columns for the row at which the series_cursor
5647 ** is currently pointing.
5648 */
5649 static int seriesColumn(
5650   sqlite3_vtab_cursor *cur,   /* The cursor */
5651   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5652   int i                       /* Which column to return */
5653 ){
5654   series_cursor *pCur = (series_cursor*)cur;
5655   sqlite3_int64 x = 0;
5656   switch( i ){
5657     case SERIES_COLUMN_START:  x = pCur->mnValue; break;
5658     case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;
5659     case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;
5660     default:                   x = pCur->iValue;  break;
5661   }
5662   sqlite3_result_int64(ctx, x);
5663   return SQLITE_OK;
5664 }
5665 
5666 /*
5667 ** Return the rowid for the current row. In this implementation, the
5668 ** first row returned is assigned rowid value 1, and each subsequent
5669 ** row a value 1 more than that of the previous.
5670 */
5671 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5672   series_cursor *pCur = (series_cursor*)cur;
5673   *pRowid = pCur->iRowid;
5674   return SQLITE_OK;
5675 }
5676 
5677 /*
5678 ** Return TRUE if the cursor has been moved off of the last
5679 ** row of output.
5680 */
5681 static int seriesEof(sqlite3_vtab_cursor *cur){
5682   series_cursor *pCur = (series_cursor*)cur;
5683   if( pCur->isDesc ){
5684     return pCur->iValue < pCur->mnValue;
5685   }else{
5686     return pCur->iValue > pCur->mxValue;
5687   }
5688 }
5689 
5690 /* True to cause run-time checking of the start=, stop=, and/or step=
5691 ** parameters.  The only reason to do this is for testing the
5692 ** constraint checking logic for virtual tables in the SQLite core.
5693 */
5694 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5695 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5696 #endif
5697 
5698 /*
5699 ** This method is called to "rewind" the series_cursor object back
5700 ** to the first row of output.  This method is always called at least
5701 ** once prior to any call to seriesColumn() or seriesRowid() or
5702 ** seriesEof().
5703 **
5704 ** The query plan selected by seriesBestIndex is passed in the idxNum
5705 ** parameter.  (idxStr is not used in this implementation.)  idxNum
5706 ** is a bitmask showing which constraints are available:
5707 **
5708 **    1:    start=VALUE
5709 **    2:    stop=VALUE
5710 **    4:    step=VALUE
5711 **
5712 ** Also, if bit 8 is set, that means that the series should be output
5713 ** in descending order rather than in ascending order.  If bit 16 is
5714 ** set, then output must appear in ascending order.
5715 **
5716 ** This routine should initialize the cursor and position it so that it
5717 ** is pointing at the first row, or pointing off the end of the table
5718 ** (so that seriesEof() will return true) if the table is empty.
5719 */
5720 static int seriesFilter(
5721   sqlite3_vtab_cursor *pVtabCursor,
5722   int idxNum, const char *idxStrUnused,
5723   int argc, sqlite3_value **argv
5724 ){
5725   series_cursor *pCur = (series_cursor *)pVtabCursor;
5726   int i = 0;
5727   (void)idxStrUnused;
5728   if( idxNum & 1 ){
5729     pCur->mnValue = sqlite3_value_int64(argv[i++]);
5730   }else{
5731     pCur->mnValue = 0;
5732   }
5733   if( idxNum & 2 ){
5734     pCur->mxValue = sqlite3_value_int64(argv[i++]);
5735   }else{
5736     pCur->mxValue = 0xffffffff;
5737   }
5738   if( idxNum & 4 ){
5739     pCur->iStep = sqlite3_value_int64(argv[i++]);
5740     if( pCur->iStep==0 ){
5741       pCur->iStep = 1;
5742     }else if( pCur->iStep<0 ){
5743       pCur->iStep = -pCur->iStep;
5744       if( (idxNum & 16)==0 ) idxNum |= 8;
5745     }
5746   }else{
5747     pCur->iStep = 1;
5748   }
5749   for(i=0; i<argc; i++){
5750     if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5751       /* If any of the constraints have a NULL value, then return no rows.
5752       ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5753       pCur->mnValue = 1;
5754       pCur->mxValue = 0;
5755       break;
5756     }
5757   }
5758   if( idxNum & 8 ){
5759     pCur->isDesc = 1;
5760     pCur->iValue = pCur->mxValue;
5761     if( pCur->iStep>0 ){
5762       pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5763     }
5764   }else{
5765     pCur->isDesc = 0;
5766     pCur->iValue = pCur->mnValue;
5767   }
5768   pCur->iRowid = 1;
5769   return SQLITE_OK;
5770 }
5771 
5772 /*
5773 ** SQLite will invoke this method one or more times while planning a query
5774 ** that uses the generate_series virtual table.  This routine needs to create
5775 ** a query plan for each invocation and compute an estimated cost for that
5776 ** plan.
5777 **
5778 ** In this implementation idxNum is used to represent the
5779 ** query plan.  idxStr is unused.
5780 **
5781 ** The query plan is represented by bits in idxNum:
5782 **
5783 **  (1)  start = $value  -- constraint exists
5784 **  (2)  stop = $value   -- constraint exists
5785 **  (4)  step = $value   -- constraint exists
5786 **  (8)  output in descending order
5787 */
5788 static int seriesBestIndex(
5789   sqlite3_vtab *pVTab,
5790   sqlite3_index_info *pIdxInfo
5791 ){
5792   int i, j;              /* Loop over constraints */
5793   int idxNum = 0;        /* The query plan bitmask */
5794   int bStartSeen = 0;    /* EQ constraint seen on the START column */
5795   int unusableMask = 0;  /* Mask of unusable constraints */
5796   int nArg = 0;          /* Number of arguments that seriesFilter() expects */
5797   int aIdx[3];           /* Constraints on start, stop, and step */
5798   const struct sqlite3_index_constraint *pConstraint;
5799 
5800   /* This implementation assumes that the start, stop, and step columns
5801   ** are the last three columns in the virtual table. */
5802   assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5803   assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5804 
5805   aIdx[0] = aIdx[1] = aIdx[2] = -1;
5806   pConstraint = pIdxInfo->aConstraint;
5807   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5808     int iCol;    /* 0 for start, 1 for stop, 2 for step */
5809     int iMask;   /* bitmask for those column */
5810     if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5811     iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5812     assert( iCol>=0 && iCol<=2 );
5813     iMask = 1 << iCol;
5814     if( iCol==0 ) bStartSeen = 1;
5815     if( pConstraint->usable==0 ){
5816       unusableMask |=  iMask;
5817       continue;
5818     }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5819       idxNum |= iMask;
5820       aIdx[iCol] = i;
5821     }
5822   }
5823   for(i=0; i<3; i++){
5824     if( (j = aIdx[i])>=0 ){
5825       pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5826       pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5827     }
5828   }
5829   /* The current generate_column() implementation requires at least one
5830   ** argument (the START value).  Legacy versions assumed START=0 if the
5831   ** first argument was omitted.  Compile with -DZERO_ARGUMENT_GENERATE_SERIES
5832   ** to obtain the legacy behavior */
5833 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
5834   if( !bStartSeen ){
5835     sqlite3_free(pVTab->zErrMsg);
5836     pVTab->zErrMsg = sqlite3_mprintf(
5837         "first argument to \"generate_series()\" missing or unusable");
5838     return SQLITE_ERROR;
5839   }
5840 #endif
5841   if( (unusableMask & ~idxNum)!=0 ){
5842     /* The start, stop, and step columns are inputs.  Therefore if there
5843     ** are unusable constraints on any of start, stop, or step then
5844     ** this plan is unusable */
5845     return SQLITE_CONSTRAINT;
5846   }
5847   if( (idxNum & 3)==3 ){
5848     /* Both start= and stop= boundaries are available.  This is the
5849     ** the preferred case */
5850     pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5851     pIdxInfo->estimatedRows = 1000;
5852     if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
5853       if( pIdxInfo->aOrderBy[0].desc ){
5854         idxNum |= 8;
5855       }else{
5856         idxNum |= 16;
5857       }
5858       pIdxInfo->orderByConsumed = 1;
5859     }
5860   }else{
5861     /* If either boundary is missing, we have to generate a huge span
5862     ** of numbers.  Make this case very expensive so that the query
5863     ** planner will work hard to avoid it. */
5864     pIdxInfo->estimatedRows = 2147483647;
5865   }
5866   pIdxInfo->idxNum = idxNum;
5867   return SQLITE_OK;
5868 }
5869 
5870 /*
5871 ** This following structure defines all the methods for the
5872 ** generate_series virtual table.
5873 */
5874 static sqlite3_module seriesModule = {
5875   0,                         /* iVersion */
5876   0,                         /* xCreate */
5877   seriesConnect,             /* xConnect */
5878   seriesBestIndex,           /* xBestIndex */
5879   seriesDisconnect,          /* xDisconnect */
5880   0,                         /* xDestroy */
5881   seriesOpen,                /* xOpen - open a cursor */
5882   seriesClose,               /* xClose - close a cursor */
5883   seriesFilter,              /* xFilter - configure scan constraints */
5884   seriesNext,                /* xNext - advance a cursor */
5885   seriesEof,                 /* xEof - check for end of scan */
5886   seriesColumn,              /* xColumn - read data */
5887   seriesRowid,               /* xRowid - read data */
5888   0,                         /* xUpdate */
5889   0,                         /* xBegin */
5890   0,                         /* xSync */
5891   0,                         /* xCommit */
5892   0,                         /* xRollback */
5893   0,                         /* xFindMethod */
5894   0,                         /* xRename */
5895   0,                         /* xSavepoint */
5896   0,                         /* xRelease */
5897   0,                         /* xRollbackTo */
5898   0                          /* xShadowName */
5899 };
5900 
5901 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5902 
5903 #ifdef _WIN32
5904 
5905 #endif
5906 int sqlite3_series_init(
5907   sqlite3 *db,
5908   char **pzErrMsg,
5909   const sqlite3_api_routines *pApi
5910 ){
5911   int rc = SQLITE_OK;
5912   SQLITE_EXTENSION_INIT2(pApi);
5913 #ifndef SQLITE_OMIT_VIRTUALTABLE
5914   if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
5915     *pzErrMsg = sqlite3_mprintf(
5916         "generate_series() requires SQLite 3.8.12 or later");
5917     return SQLITE_ERROR;
5918   }
5919   rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5920 #endif
5921   return rc;
5922 }
5923 
5924 /************************* End ../ext/misc/series.c ********************/
5925 /************************* Begin ../ext/misc/regexp.c ******************/
5926 /*
5927 ** 2012-11-13
5928 **
5929 ** The author disclaims copyright to this source code.  In place of
5930 ** a legal notice, here is a blessing:
5931 **
5932 **    May you do good and not evil.
5933 **    May you find forgiveness for yourself and forgive others.
5934 **    May you share freely, never taking more than you give.
5935 **
5936 ******************************************************************************
5937 **
5938 ** The code in this file implements a compact but reasonably
5939 ** efficient regular-expression matcher for posix extended regular
5940 ** expressions against UTF8 text.
5941 **
5942 ** This file is an SQLite extension.  It registers a single function
5943 ** named "regexp(A,B)" where A is the regular expression and B is the
5944 ** string to be matched.  By registering this function, SQLite will also
5945 ** then implement the "B regexp A" operator.  Note that with the function
5946 ** the regular expression comes first, but with the operator it comes
5947 ** second.
5948 **
5949 **  The following regular expression syntax is supported:
5950 **
5951 **     X*      zero or more occurrences of X
5952 **     X+      one or more occurrences of X
5953 **     X?      zero or one occurrences of X
5954 **     X{p,q}  between p and q occurrences of X
5955 **     (X)     match X
5956 **     X|Y     X or Y
5957 **     ^X      X occurring at the beginning of the string
5958 **     X$      X occurring at the end of the string
5959 **     .       Match any single character
5960 **     \c      Character c where c is one of \{}()[]|*+?.
5961 **     \c      C-language escapes for c in afnrtv.  ex: \t or \n
5962 **     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
5963 **     \xXX    Where XX is exactly 2 hex digits, unicode value XX
5964 **     [abc]   Any single character from the set abc
5965 **     [^abc]  Any single character not in the set abc
5966 **     [a-z]   Any single character in the range a-z
5967 **     [^a-z]  Any single character not in the range a-z
5968 **     \b      Word boundary
5969 **     \w      Word character.  [A-Za-z0-9_]
5970 **     \W      Non-word character
5971 **     \d      Digit
5972 **     \D      Non-digit
5973 **     \s      Whitespace character
5974 **     \S      Non-whitespace character
5975 **
5976 ** A nondeterministic finite automaton (NFA) is used for matching, so the
5977 ** performance is bounded by O(N*M) where N is the size of the regular
5978 ** expression and M is the size of the input string.  The matcher never
5979 ** exhibits exponential behavior.  Note that the X{p,q} operator expands
5980 ** to p copies of X following by q-p copies of X? and that the size of the
5981 ** regular expression in the O(N*M) performance bound is computed after
5982 ** this expansion.
5983 */
5984 #include <string.h>
5985 #include <stdlib.h>
5986 /* #include "sqlite3ext.h" */
5987 SQLITE_EXTENSION_INIT1
5988 
5989 /*
5990 ** The following #defines change the names of some functions implemented in
5991 ** this file to prevent name collisions with C-library functions of the
5992 ** same name.
5993 */
5994 #define re_match   sqlite3re_match
5995 #define re_compile sqlite3re_compile
5996 #define re_free    sqlite3re_free
5997 
5998 /* The end-of-input character */
5999 #define RE_EOF            0    /* End of input */
6000 
6001 /* The NFA is implemented as sequence of opcodes taken from the following
6002 ** set.  Each opcode has a single integer argument.
6003 */
6004 #define RE_OP_MATCH       1    /* Match the one character in the argument */
6005 #define RE_OP_ANY         2    /* Match any one character.  (Implements ".") */
6006 #define RE_OP_ANYSTAR     3    /* Special optimized version of .* */
6007 #define RE_OP_FORK        4    /* Continue to both next and opcode at iArg */
6008 #define RE_OP_GOTO        5    /* Jump to opcode at iArg */
6009 #define RE_OP_ACCEPT      6    /* Halt and indicate a successful match */
6010 #define RE_OP_CC_INC      7    /* Beginning of a [...] character class */
6011 #define RE_OP_CC_EXC      8    /* Beginning of a [^...] character class */
6012 #define RE_OP_CC_VALUE    9    /* Single value in a character class */
6013 #define RE_OP_CC_RANGE   10    /* Range of values in a character class */
6014 #define RE_OP_WORD       11    /* Perl word character [A-Za-z0-9_] */
6015 #define RE_OP_NOTWORD    12    /* Not a perl word character */
6016 #define RE_OP_DIGIT      13    /* digit:  [0-9] */
6017 #define RE_OP_NOTDIGIT   14    /* Not a digit */
6018 #define RE_OP_SPACE      15    /* space:  [ \t\n\r\v\f] */
6019 #define RE_OP_NOTSPACE   16    /* Not a digit */
6020 #define RE_OP_BOUNDARY   17    /* Boundary between word and non-word */
6021 
6022 /* Each opcode is a "state" in the NFA */
6023 typedef unsigned short ReStateNumber;
6024 
6025 /* Because this is an NFA and not a DFA, multiple states can be active at
6026 ** once.  An instance of the following object records all active states in
6027 ** the NFA.  The implementation is optimized for the common case where the
6028 ** number of actives states is small.
6029 */
6030 typedef struct ReStateSet {
6031   unsigned nState;            /* Number of current states */
6032   ReStateNumber *aState;      /* Current states */
6033 } ReStateSet;
6034 
6035 /* An input string read one character at a time.
6036 */
6037 typedef struct ReInput ReInput;
6038 struct ReInput {
6039   const unsigned char *z;  /* All text */
6040   int i;                   /* Next byte to read */
6041   int mx;                  /* EOF when i>=mx */
6042 };
6043 
6044 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6045 ** an instance of the following object.
6046 */
6047 typedef struct ReCompiled ReCompiled;
6048 struct ReCompiled {
6049   ReInput sIn;                /* Regular expression text */
6050   const char *zErr;           /* Error message to return */
6051   char *aOp;                  /* Operators for the virtual machine */
6052   int *aArg;                  /* Arguments to each operator */
6053   unsigned (*xNextChar)(ReInput*);  /* Next character function */
6054   unsigned char zInit[12];    /* Initial text to match */
6055   int nInit;                  /* Number of characters in zInit */
6056   unsigned nState;            /* Number of entries in aOp[] and aArg[] */
6057   unsigned nAlloc;            /* Slots allocated for aOp[] and aArg[] */
6058 };
6059 
6060 /* Add a state to the given state set if it is not already there */
6061 static void re_add_state(ReStateSet *pSet, int newState){
6062   unsigned i;
6063   for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6064   pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6065 }
6066 
6067 /* Extract the next unicode character from *pzIn and return it.  Advance
6068 ** *pzIn to the first byte past the end of the character returned.  To
6069 ** be clear:  this routine converts utf8 to unicode.  This routine is
6070 ** optimized for the common case where the next character is a single byte.
6071 */
6072 static unsigned re_next_char(ReInput *p){
6073   unsigned c;
6074   if( p->i>=p->mx ) return 0;
6075   c = p->z[p->i++];
6076   if( c>=0x80 ){
6077     if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6078       c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6079       if( c<0x80 ) c = 0xfffd;
6080     }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6081            && (p->z[p->i+1]&0xc0)==0x80 ){
6082       c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6083       p->i += 2;
6084       if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6085     }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
6086            && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6087       c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6088                        | (p->z[p->i+2]&0x3f);
6089       p->i += 3;
6090       if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6091     }else{
6092       c = 0xfffd;
6093     }
6094   }
6095   return c;
6096 }
6097 static unsigned re_next_char_nocase(ReInput *p){
6098   unsigned c = re_next_char(p);
6099   if( c>='A' && c<='Z' ) c += 'a' - 'A';
6100   return c;
6101 }
6102 
6103 /* Return true if c is a perl "word" character:  [A-Za-z0-9_] */
6104 static int re_word_char(int c){
6105   return (c>='0' && c<='9') || (c>='a' && c<='z')
6106       || (c>='A' && c<='Z') || c=='_';
6107 }
6108 
6109 /* Return true if c is a "digit" character:  [0-9] */
6110 static int re_digit_char(int c){
6111   return (c>='0' && c<='9');
6112 }
6113 
6114 /* Return true if c is a perl "space" character:  [ \t\r\n\v\f] */
6115 static int re_space_char(int c){
6116   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6117 }
6118 
6119 /* Run a compiled regular expression on the zero-terminated input
6120 ** string zIn[].  Return true on a match and false if there is no match.
6121 */
6122 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6123   ReStateSet aStateSet[2], *pThis, *pNext;
6124   ReStateNumber aSpace[100];
6125   ReStateNumber *pToFree;
6126   unsigned int i = 0;
6127   unsigned int iSwap = 0;
6128   int c = RE_EOF+1;
6129   int cPrev = 0;
6130   int rc = 0;
6131   ReInput in;
6132 
6133   in.z = zIn;
6134   in.i = 0;
6135   in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6136 
6137   /* Look for the initial prefix match, if there is one. */
6138   if( pRe->nInit ){
6139     unsigned char x = pRe->zInit[0];
6140     while( in.i+pRe->nInit<=in.mx
6141      && (zIn[in.i]!=x ||
6142          strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6143     ){
6144       in.i++;
6145     }
6146     if( in.i+pRe->nInit>in.mx ) return 0;
6147   }
6148 
6149   if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6150     pToFree = 0;
6151     aStateSet[0].aState = aSpace;
6152   }else{
6153     pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6154     if( pToFree==0 ) return -1;
6155     aStateSet[0].aState = pToFree;
6156   }
6157   aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6158   pNext = &aStateSet[1];
6159   pNext->nState = 0;
6160   re_add_state(pNext, 0);
6161   while( c!=RE_EOF && pNext->nState>0 ){
6162     cPrev = c;
6163     c = pRe->xNextChar(&in);
6164     pThis = pNext;
6165     pNext = &aStateSet[iSwap];
6166     iSwap = 1 - iSwap;
6167     pNext->nState = 0;
6168     for(i=0; i<pThis->nState; i++){
6169       int x = pThis->aState[i];
6170       switch( pRe->aOp[x] ){
6171         case RE_OP_MATCH: {
6172           if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6173           break;
6174         }
6175         case RE_OP_ANY: {
6176           if( c!=0 ) re_add_state(pNext, x+1);
6177           break;
6178         }
6179         case RE_OP_WORD: {
6180           if( re_word_char(c) ) re_add_state(pNext, x+1);
6181           break;
6182         }
6183         case RE_OP_NOTWORD: {
6184           if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6185           break;
6186         }
6187         case RE_OP_DIGIT: {
6188           if( re_digit_char(c) ) re_add_state(pNext, x+1);
6189           break;
6190         }
6191         case RE_OP_NOTDIGIT: {
6192           if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6193           break;
6194         }
6195         case RE_OP_SPACE: {
6196           if( re_space_char(c) ) re_add_state(pNext, x+1);
6197           break;
6198         }
6199         case RE_OP_NOTSPACE: {
6200           if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6201           break;
6202         }
6203         case RE_OP_BOUNDARY: {
6204           if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6205           break;
6206         }
6207         case RE_OP_ANYSTAR: {
6208           re_add_state(pNext, x);
6209           re_add_state(pThis, x+1);
6210           break;
6211         }
6212         case RE_OP_FORK: {
6213           re_add_state(pThis, x+pRe->aArg[x]);
6214           re_add_state(pThis, x+1);
6215           break;
6216         }
6217         case RE_OP_GOTO: {
6218           re_add_state(pThis, x+pRe->aArg[x]);
6219           break;
6220         }
6221         case RE_OP_ACCEPT: {
6222           rc = 1;
6223           goto re_match_end;
6224         }
6225         case RE_OP_CC_EXC: {
6226           if( c==0 ) break;
6227           /* fall-through */ goto re_op_cc_inc;
6228         }
6229         case RE_OP_CC_INC: re_op_cc_inc: {
6230           int j = 1;
6231           int n = pRe->aArg[x];
6232           int hit = 0;
6233           for(j=1; j>0 && j<n; j++){
6234             if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6235               if( pRe->aArg[x+j]==c ){
6236                 hit = 1;
6237                 j = -1;
6238               }
6239             }else{
6240               if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6241                 hit = 1;
6242                 j = -1;
6243               }else{
6244                 j++;
6245               }
6246             }
6247           }
6248           if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6249           if( hit ) re_add_state(pNext, x+n);
6250           break;
6251         }
6252       }
6253     }
6254   }
6255   for(i=0; i<pNext->nState; i++){
6256     if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
6257   }
6258 re_match_end:
6259   sqlite3_free(pToFree);
6260   return rc;
6261 }
6262 
6263 /* Resize the opcode and argument arrays for an RE under construction.
6264 */
6265 static int re_resize(ReCompiled *p, int N){
6266   char *aOp;
6267   int *aArg;
6268   aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6269   if( aOp==0 ) return 1;
6270   p->aOp = aOp;
6271   aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6272   if( aArg==0 ) return 1;
6273   p->aArg = aArg;
6274   p->nAlloc = N;
6275   return 0;
6276 }
6277 
6278 /* Insert a new opcode and argument into an RE under construction.  The
6279 ** insertion point is just prior to existing opcode iBefore.
6280 */
6281 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6282   int i;
6283   if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6284   for(i=p->nState; i>iBefore; i--){
6285     p->aOp[i] = p->aOp[i-1];
6286     p->aArg[i] = p->aArg[i-1];
6287   }
6288   p->nState++;
6289   p->aOp[iBefore] = (char)op;
6290   p->aArg[iBefore] = arg;
6291   return iBefore;
6292 }
6293 
6294 /* Append a new opcode and argument to the end of the RE under construction.
6295 */
6296 static int re_append(ReCompiled *p, int op, int arg){
6297   return re_insert(p, p->nState, op, arg);
6298 }
6299 
6300 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6301 ** under construction.
6302 */
6303 static void re_copy(ReCompiled *p, int iStart, int N){
6304   if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6305   memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6306   memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6307   p->nState += N;
6308 }
6309 
6310 /* Return true if c is a hexadecimal digit character:  [0-9a-fA-F]
6311 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c).  If
6312 ** c is not a hex digit *pV is unchanged.
6313 */
6314 static int re_hex(int c, int *pV){
6315   if( c>='0' && c<='9' ){
6316     c -= '0';
6317   }else if( c>='a' && c<='f' ){
6318     c -= 'a' - 10;
6319   }else if( c>='A' && c<='F' ){
6320     c -= 'A' - 10;
6321   }else{
6322     return 0;
6323   }
6324   *pV = (*pV)*16 + (c & 0xff);
6325   return 1;
6326 }
6327 
6328 /* A backslash character has been seen, read the next character and
6329 ** return its interpretation.
6330 */
6331 static unsigned re_esc_char(ReCompiled *p){
6332   static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6333   static const char zTrans[] = "\a\f\n\r\t\v";
6334   int i, v = 0;
6335   char c;
6336   if( p->sIn.i>=p->sIn.mx ) return 0;
6337   c = p->sIn.z[p->sIn.i];
6338   if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6339     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6340     if( re_hex(zIn[1],&v)
6341      && re_hex(zIn[2],&v)
6342      && re_hex(zIn[3],&v)
6343      && re_hex(zIn[4],&v)
6344     ){
6345       p->sIn.i += 5;
6346       return v;
6347     }
6348   }
6349   if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6350     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6351     if( re_hex(zIn[1],&v)
6352      && re_hex(zIn[2],&v)
6353     ){
6354       p->sIn.i += 3;
6355       return v;
6356     }
6357   }
6358   for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6359   if( zEsc[i] ){
6360     if( i<6 ) c = zTrans[i];
6361     p->sIn.i++;
6362   }else{
6363     p->zErr = "unknown \\ escape";
6364   }
6365   return c;
6366 }
6367 
6368 /* Forward declaration */
6369 static const char *re_subcompile_string(ReCompiled*);
6370 
6371 /* Peek at the next byte of input */
6372 static unsigned char rePeek(ReCompiled *p){
6373   return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6374 }
6375 
6376 /* Compile RE text into a sequence of opcodes.  Continue up to the
6377 ** first unmatched ")" character, then return.  If an error is found,
6378 ** return a pointer to the error message string.
6379 */
6380 static const char *re_subcompile_re(ReCompiled *p){
6381   const char *zErr;
6382   int iStart, iEnd, iGoto;
6383   iStart = p->nState;
6384   zErr = re_subcompile_string(p);
6385   if( zErr ) return zErr;
6386   while( rePeek(p)=='|' ){
6387     iEnd = p->nState;
6388     re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6389     iGoto = re_append(p, RE_OP_GOTO, 0);
6390     p->sIn.i++;
6391     zErr = re_subcompile_string(p);
6392     if( zErr ) return zErr;
6393     p->aArg[iGoto] = p->nState - iGoto;
6394   }
6395   return 0;
6396 }
6397 
6398 /* Compile an element of regular expression text (anything that can be
6399 ** an operand to the "|" operator).  Return NULL on success or a pointer
6400 ** to the error message if there is a problem.
6401 */
6402 static const char *re_subcompile_string(ReCompiled *p){
6403   int iPrev = -1;
6404   int iStart;
6405   unsigned c;
6406   const char *zErr;
6407   while( (c = p->xNextChar(&p->sIn))!=0 ){
6408     iStart = p->nState;
6409     switch( c ){
6410       case '|':
6411       case '$':
6412       case ')': {
6413         p->sIn.i--;
6414         return 0;
6415       }
6416       case '(': {
6417         zErr = re_subcompile_re(p);
6418         if( zErr ) return zErr;
6419         if( rePeek(p)!=')' ) return "unmatched '('";
6420         p->sIn.i++;
6421         break;
6422       }
6423       case '.': {
6424         if( rePeek(p)=='*' ){
6425           re_append(p, RE_OP_ANYSTAR, 0);
6426           p->sIn.i++;
6427         }else{
6428           re_append(p, RE_OP_ANY, 0);
6429         }
6430         break;
6431       }
6432       case '*': {
6433         if( iPrev<0 ) return "'*' without operand";
6434         re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6435         re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6436         break;
6437       }
6438       case '+': {
6439         if( iPrev<0 ) return "'+' without operand";
6440         re_append(p, RE_OP_FORK, iPrev - p->nState);
6441         break;
6442       }
6443       case '?': {
6444         if( iPrev<0 ) return "'?' without operand";
6445         re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6446         break;
6447       }
6448       case '{': {
6449         int m = 0, n = 0;
6450         int sz, j;
6451         if( iPrev<0 ) return "'{m,n}' without operand";
6452         while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6453         n = m;
6454         if( c==',' ){
6455           p->sIn.i++;
6456           n = 0;
6457           while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6458         }
6459         if( c!='}' ) return "unmatched '{'";
6460         if( n>0 && n<m ) return "n less than m in '{m,n}'";
6461         p->sIn.i++;
6462         sz = p->nState - iPrev;
6463         if( m==0 ){
6464           if( n==0 ) return "both m and n are zero in '{m,n}'";
6465           re_insert(p, iPrev, RE_OP_FORK, sz+1);
6466           n--;
6467         }else{
6468           for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6469         }
6470         for(j=m; j<n; j++){
6471           re_append(p, RE_OP_FORK, sz+1);
6472           re_copy(p, iPrev, sz);
6473         }
6474         if( n==0 && m>0 ){
6475           re_append(p, RE_OP_FORK, -sz);
6476         }
6477         break;
6478       }
6479       case '[': {
6480         int iFirst = p->nState;
6481         if( rePeek(p)=='^' ){
6482           re_append(p, RE_OP_CC_EXC, 0);
6483           p->sIn.i++;
6484         }else{
6485           re_append(p, RE_OP_CC_INC, 0);
6486         }
6487         while( (c = p->xNextChar(&p->sIn))!=0 ){
6488           if( c=='[' && rePeek(p)==':' ){
6489             return "POSIX character classes not supported";
6490           }
6491           if( c=='\\' ) c = re_esc_char(p);
6492           if( rePeek(p)=='-' ){
6493             re_append(p, RE_OP_CC_RANGE, c);
6494             p->sIn.i++;
6495             c = p->xNextChar(&p->sIn);
6496             if( c=='\\' ) c = re_esc_char(p);
6497             re_append(p, RE_OP_CC_RANGE, c);
6498           }else{
6499             re_append(p, RE_OP_CC_VALUE, c);
6500           }
6501           if( rePeek(p)==']' ){ p->sIn.i++; break; }
6502         }
6503         if( c==0 ) return "unclosed '['";
6504         p->aArg[iFirst] = p->nState - iFirst;
6505         break;
6506       }
6507       case '\\': {
6508         int specialOp = 0;
6509         switch( rePeek(p) ){
6510           case 'b': specialOp = RE_OP_BOUNDARY;   break;
6511           case 'd': specialOp = RE_OP_DIGIT;      break;
6512           case 'D': specialOp = RE_OP_NOTDIGIT;   break;
6513           case 's': specialOp = RE_OP_SPACE;      break;
6514           case 'S': specialOp = RE_OP_NOTSPACE;   break;
6515           case 'w': specialOp = RE_OP_WORD;       break;
6516           case 'W': specialOp = RE_OP_NOTWORD;    break;
6517         }
6518         if( specialOp ){
6519           p->sIn.i++;
6520           re_append(p, specialOp, 0);
6521         }else{
6522           c = re_esc_char(p);
6523           re_append(p, RE_OP_MATCH, c);
6524         }
6525         break;
6526       }
6527       default: {
6528         re_append(p, RE_OP_MATCH, c);
6529         break;
6530       }
6531     }
6532     iPrev = iStart;
6533   }
6534   return 0;
6535 }
6536 
6537 /* Free and reclaim all the memory used by a previously compiled
6538 ** regular expression.  Applications should invoke this routine once
6539 ** for every call to re_compile() to avoid memory leaks.
6540 */
6541 static void re_free(ReCompiled *pRe){
6542   if( pRe ){
6543     sqlite3_free(pRe->aOp);
6544     sqlite3_free(pRe->aArg);
6545     sqlite3_free(pRe);
6546   }
6547 }
6548 
6549 /*
6550 ** Compile a textual regular expression in zIn[] into a compiled regular
6551 ** expression suitable for us by re_match() and return a pointer to the
6552 ** compiled regular expression in *ppRe.  Return NULL on success or an
6553 ** error message if something goes wrong.
6554 */
6555 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6556   ReCompiled *pRe;
6557   const char *zErr;
6558   int i, j;
6559 
6560   *ppRe = 0;
6561   pRe = sqlite3_malloc( sizeof(*pRe) );
6562   if( pRe==0 ){
6563     return "out of memory";
6564   }
6565   memset(pRe, 0, sizeof(*pRe));
6566   pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6567   if( re_resize(pRe, 30) ){
6568     re_free(pRe);
6569     return "out of memory";
6570   }
6571   if( zIn[0]=='^' ){
6572     zIn++;
6573   }else{
6574     re_append(pRe, RE_OP_ANYSTAR, 0);
6575   }
6576   pRe->sIn.z = (unsigned char*)zIn;
6577   pRe->sIn.i = 0;
6578   pRe->sIn.mx = (int)strlen(zIn);
6579   zErr = re_subcompile_re(pRe);
6580   if( zErr ){
6581     re_free(pRe);
6582     return zErr;
6583   }
6584   if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
6585     re_append(pRe, RE_OP_MATCH, RE_EOF);
6586     re_append(pRe, RE_OP_ACCEPT, 0);
6587     *ppRe = pRe;
6588   }else if( pRe->sIn.i>=pRe->sIn.mx ){
6589     re_append(pRe, RE_OP_ACCEPT, 0);
6590     *ppRe = pRe;
6591   }else{
6592     re_free(pRe);
6593     return "unrecognized character";
6594   }
6595 
6596   /* The following is a performance optimization.  If the regex begins with
6597   ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6598   ** one or more matching characters, enter those matching characters into
6599   ** zInit[].  The re_match() routine can then search ahead in the input
6600   ** string looking for the initial match without having to run the whole
6601   ** regex engine over the string.  Do not worry able trying to match
6602   ** unicode characters beyond plane 0 - those are very rare and this is
6603   ** just an optimization. */
6604   if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6605     for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6606       unsigned x = pRe->aArg[i];
6607       if( x<=127 ){
6608         pRe->zInit[j++] = (unsigned char)x;
6609       }else if( x<=0xfff ){
6610         pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6611         pRe->zInit[j++] = 0x80 | (x&0x3f);
6612       }else if( x<=0xffff ){
6613         pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));
6614         pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6615         pRe->zInit[j++] = 0x80 | (x&0x3f);
6616       }else{
6617         break;
6618       }
6619     }
6620     if( j>0 && pRe->zInit[j-1]==0 ) j--;
6621     pRe->nInit = j;
6622   }
6623   return pRe->zErr;
6624 }
6625 
6626 /*
6627 ** Implementation of the regexp() SQL function.  This function implements
6628 ** the build-in REGEXP operator.  The first argument to the function is the
6629 ** pattern and the second argument is the string.  So, the SQL statements:
6630 **
6631 **       A REGEXP B
6632 **
6633 ** is implemented as regexp(B,A).
6634 */
6635 static void re_sql_func(
6636   sqlite3_context *context,
6637   int argc,
6638   sqlite3_value **argv
6639 ){
6640   ReCompiled *pRe;          /* Compiled regular expression */
6641   const char *zPattern;     /* The regular expression */
6642   const unsigned char *zStr;/* String being searched */
6643   const char *zErr;         /* Compile error message */
6644   int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
6645 
6646   (void)argc;  /* Unused */
6647   pRe = sqlite3_get_auxdata(context, 0);
6648   if( pRe==0 ){
6649     zPattern = (const char*)sqlite3_value_text(argv[0]);
6650     if( zPattern==0 ) return;
6651     zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6652     if( zErr ){
6653       re_free(pRe);
6654       sqlite3_result_error(context, zErr, -1);
6655       return;
6656     }
6657     if( pRe==0 ){
6658       sqlite3_result_error_nomem(context);
6659       return;
6660     }
6661     setAux = 1;
6662   }
6663   zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6664   if( zStr!=0 ){
6665     sqlite3_result_int(context, re_match(pRe, zStr, -1));
6666   }
6667   if( setAux ){
6668     sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6669   }
6670 }
6671 
6672 /*
6673 ** Invoke this routine to register the regexp() function with the
6674 ** SQLite database connection.
6675 */
6676 #ifdef _WIN32
6677 
6678 #endif
6679 int sqlite3_regexp_init(
6680   sqlite3 *db,
6681   char **pzErrMsg,
6682   const sqlite3_api_routines *pApi
6683 ){
6684   int rc = SQLITE_OK;
6685   SQLITE_EXTENSION_INIT2(pApi);
6686   (void)pzErrMsg;  /* Unused */
6687   rc = sqlite3_create_function(db, "regexp", 2,
6688                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6689                             0, re_sql_func, 0, 0);
6690   if( rc==SQLITE_OK ){
6691     /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6692     ** of regexp(PATTERN,STRING). */
6693     rc = sqlite3_create_function(db, "regexpi", 2,
6694                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6695                             (void*)db, re_sql_func, 0, 0);
6696   }
6697   return rc;
6698 }
6699 
6700 /************************* End ../ext/misc/regexp.c ********************/
6701 #ifdef SQLITE_HAVE_ZLIB
6702 /************************* Begin ../ext/misc/zipfile.c ******************/
6703 /*
6704 ** 2017-12-26
6705 **
6706 ** The author disclaims copyright to this source code.  In place of
6707 ** a legal notice, here is a blessing:
6708 **
6709 **    May you do good and not evil.
6710 **    May you find forgiveness for yourself and forgive others.
6711 **    May you share freely, never taking more than you give.
6712 **
6713 ******************************************************************************
6714 **
6715 ** This file implements a virtual table for reading and writing ZIP archive
6716 ** files.
6717 **
6718 ** Usage example:
6719 **
6720 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
6721 **
6722 ** Current limitations:
6723 **
6724 **    *  No support for encryption
6725 **    *  No support for ZIP archives spanning multiple files
6726 **    *  No support for zip64 extensions
6727 **    *  Only the "inflate/deflate" (zlib) compression method is supported
6728 */
6729 /* #include "sqlite3ext.h" */
6730 SQLITE_EXTENSION_INIT1
6731 #include <stdio.h>
6732 #include <string.h>
6733 #include <assert.h>
6734 
6735 #include <zlib.h>
6736 
6737 #ifndef SQLITE_OMIT_VIRTUALTABLE
6738 
6739 #ifndef SQLITE_AMALGAMATION
6740 
6741 #ifndef UINT32_TYPE
6742 # ifdef HAVE_UINT32_T
6743 #  define UINT32_TYPE uint32_t
6744 # else
6745 #  define UINT32_TYPE unsigned int
6746 # endif
6747 #endif
6748 #ifndef UINT16_TYPE
6749 # ifdef HAVE_UINT16_T
6750 #  define UINT16_TYPE uint16_t
6751 # else
6752 #  define UINT16_TYPE unsigned short int
6753 # endif
6754 #endif
6755 /* typedef sqlite3_int64 i64; */
6756 /* typedef unsigned char u8; */
6757 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
6758 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
6759 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6760 
6761 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6762 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
6763 #endif
6764 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
6765 # define ALWAYS(X)      (1)
6766 # define NEVER(X)       (0)
6767 #elif !defined(NDEBUG)
6768 # define ALWAYS(X)      ((X)?1:(assert(0),0))
6769 # define NEVER(X)       ((X)?(assert(0),1):0)
6770 #else
6771 # define ALWAYS(X)      (X)
6772 # define NEVER(X)       (X)
6773 #endif
6774 
6775 #endif   /* SQLITE_AMALGAMATION */
6776 
6777 /*
6778 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
6779 **
6780 ** In some ways it would be better to obtain these values from system
6781 ** header files. But, the dependency is undesirable and (a) these
6782 ** have been stable for decades, (b) the values are part of POSIX and
6783 ** are also made explicit in [man stat], and (c) are part of the
6784 ** file format for zip archives.
6785 */
6786 #ifndef S_IFDIR
6787 # define S_IFDIR 0040000
6788 #endif
6789 #ifndef S_IFREG
6790 # define S_IFREG 0100000
6791 #endif
6792 #ifndef S_IFLNK
6793 # define S_IFLNK 0120000
6794 #endif
6795 
6796 static const char ZIPFILE_SCHEMA[] =
6797   "CREATE TABLE y("
6798     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
6799     "mode,"              /* 1: POSIX mode for file */
6800     "mtime,"             /* 2: Last modification time (secs since 1970)*/
6801     "sz,"                /* 3: Size of object */
6802     "rawdata,"           /* 4: Raw data */
6803     "data,"              /* 5: Uncompressed data */
6804     "method,"            /* 6: Compression method (integer) */
6805     "z HIDDEN"           /* 7: Name of zip file */
6806   ") WITHOUT ROWID;";
6807 
6808 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
6809 #define ZIPFILE_BUFFER_SIZE (64*1024)
6810 
6811 
6812 /*
6813 ** Magic numbers used to read and write zip files.
6814 **
6815 ** ZIPFILE_NEWENTRY_MADEBY:
6816 **   Use this value for the "version-made-by" field in new zip file
6817 **   entries. The upper byte indicates "unix", and the lower byte
6818 **   indicates that the zip file matches pkzip specification 3.0.
6819 **   This is what info-zip seems to do.
6820 **
6821 ** ZIPFILE_NEWENTRY_REQUIRED:
6822 **   Value for "version-required-to-extract" field of new entries.
6823 **   Version 2.0 is required to support folders and deflate compression.
6824 **
6825 ** ZIPFILE_NEWENTRY_FLAGS:
6826 **   Value for "general-purpose-bit-flags" field of new entries. Bit
6827 **   11 means "utf-8 filename and comment".
6828 **
6829 ** ZIPFILE_SIGNATURE_CDS:
6830 **   First 4 bytes of a valid CDS record.
6831 **
6832 ** ZIPFILE_SIGNATURE_LFH:
6833 **   First 4 bytes of a valid LFH record.
6834 **
6835 ** ZIPFILE_SIGNATURE_EOCD
6836 **   First 4 bytes of a valid EOCD record.
6837 */
6838 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
6839 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
6840 #define ZIPFILE_NEWENTRY_REQUIRED 20
6841 #define ZIPFILE_NEWENTRY_FLAGS    0x800
6842 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
6843 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
6844 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
6845 
6846 /*
6847 ** The sizes of the fixed-size part of each of the three main data
6848 ** structures in a zip archive.
6849 */
6850 #define ZIPFILE_LFH_FIXED_SZ      30
6851 #define ZIPFILE_EOCD_FIXED_SZ     22
6852 #define ZIPFILE_CDS_FIXED_SZ      46
6853 
6854 /*
6855 *** 4.3.16  End of central directory record:
6856 ***
6857 ***   end of central dir signature    4 bytes  (0x06054b50)
6858 ***   number of this disk             2 bytes
6859 ***   number of the disk with the
6860 ***   start of the central directory  2 bytes
6861 ***   total number of entries in the
6862 ***   central directory on this disk  2 bytes
6863 ***   total number of entries in
6864 ***   the central directory           2 bytes
6865 ***   size of the central directory   4 bytes
6866 ***   offset of start of central
6867 ***   directory with respect to
6868 ***   the starting disk number        4 bytes
6869 ***   .ZIP file comment length        2 bytes
6870 ***   .ZIP file comment       (variable size)
6871 */
6872 typedef struct ZipfileEOCD ZipfileEOCD;
6873 struct ZipfileEOCD {
6874   u16 iDisk;
6875   u16 iFirstDisk;
6876   u16 nEntry;
6877   u16 nEntryTotal;
6878   u32 nSize;
6879   u32 iOffset;
6880 };
6881 
6882 /*
6883 *** 4.3.12  Central directory structure:
6884 ***
6885 *** ...
6886 ***
6887 ***   central file header signature   4 bytes  (0x02014b50)
6888 ***   version made by                 2 bytes
6889 ***   version needed to extract       2 bytes
6890 ***   general purpose bit flag        2 bytes
6891 ***   compression method              2 bytes
6892 ***   last mod file time              2 bytes
6893 ***   last mod file date              2 bytes
6894 ***   crc-32                          4 bytes
6895 ***   compressed size                 4 bytes
6896 ***   uncompressed size               4 bytes
6897 ***   file name length                2 bytes
6898 ***   extra field length              2 bytes
6899 ***   file comment length             2 bytes
6900 ***   disk number start               2 bytes
6901 ***   internal file attributes        2 bytes
6902 ***   external file attributes        4 bytes
6903 ***   relative offset of local header 4 bytes
6904 */
6905 typedef struct ZipfileCDS ZipfileCDS;
6906 struct ZipfileCDS {
6907   u16 iVersionMadeBy;
6908   u16 iVersionExtract;
6909   u16 flags;
6910   u16 iCompression;
6911   u16 mTime;
6912   u16 mDate;
6913   u32 crc32;
6914   u32 szCompressed;
6915   u32 szUncompressed;
6916   u16 nFile;
6917   u16 nExtra;
6918   u16 nComment;
6919   u16 iDiskStart;
6920   u16 iInternalAttr;
6921   u32 iExternalAttr;
6922   u32 iOffset;
6923   char *zFile;                    /* Filename (sqlite3_malloc()) */
6924 };
6925 
6926 /*
6927 *** 4.3.7  Local file header:
6928 ***
6929 ***   local file header signature     4 bytes  (0x04034b50)
6930 ***   version needed to extract       2 bytes
6931 ***   general purpose bit flag        2 bytes
6932 ***   compression method              2 bytes
6933 ***   last mod file time              2 bytes
6934 ***   last mod file date              2 bytes
6935 ***   crc-32                          4 bytes
6936 ***   compressed size                 4 bytes
6937 ***   uncompressed size               4 bytes
6938 ***   file name length                2 bytes
6939 ***   extra field length              2 bytes
6940 ***
6941 */
6942 typedef struct ZipfileLFH ZipfileLFH;
6943 struct ZipfileLFH {
6944   u16 iVersionExtract;
6945   u16 flags;
6946   u16 iCompression;
6947   u16 mTime;
6948   u16 mDate;
6949   u32 crc32;
6950   u32 szCompressed;
6951   u32 szUncompressed;
6952   u16 nFile;
6953   u16 nExtra;
6954 };
6955 
6956 typedef struct ZipfileEntry ZipfileEntry;
6957 struct ZipfileEntry {
6958   ZipfileCDS cds;            /* Parsed CDS record */
6959   u32 mUnixTime;             /* Modification time, in UNIX format */
6960   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
6961   i64 iDataOff;              /* Offset to data in file (if aData==0) */
6962   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
6963   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
6964 };
6965 
6966 /*
6967 ** Cursor type for zipfile tables.
6968 */
6969 typedef struct ZipfileCsr ZipfileCsr;
6970 struct ZipfileCsr {
6971   sqlite3_vtab_cursor base;  /* Base class - must be first */
6972   i64 iId;                   /* Cursor ID */
6973   u8 bEof;                   /* True when at EOF */
6974   u8 bNoop;                  /* If next xNext() call is no-op */
6975 
6976   /* Used outside of write transactions */
6977   FILE *pFile;               /* Zip file */
6978   i64 iNextOff;              /* Offset of next record in central directory */
6979   ZipfileEOCD eocd;          /* Parse of central directory record */
6980 
6981   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
6982   ZipfileEntry *pCurrent;    /* Current entry */
6983   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
6984 };
6985 
6986 typedef struct ZipfileTab ZipfileTab;
6987 struct ZipfileTab {
6988   sqlite3_vtab base;         /* Base class - must be first */
6989   char *zFile;               /* Zip file this table accesses (may be NULL) */
6990   sqlite3 *db;               /* Host database connection */
6991   u8 *aBuffer;               /* Temporary buffer used for various tasks */
6992 
6993   ZipfileCsr *pCsrList;      /* List of cursors */
6994   i64 iNextCsrid;
6995 
6996   /* The following are used by write transactions only */
6997   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6998   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
6999   FILE *pWriteFd;            /* File handle open on zip archive */
7000   i64 szCurrent;             /* Current size of zip archive */
7001   i64 szOrig;                /* Size of archive at start of transaction */
7002 };
7003 
7004 /*
7005 ** Set the error message contained in context ctx to the results of
7006 ** vprintf(zFmt, ...).
7007 */
7008 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
7009   char *zMsg = 0;
7010   va_list ap;
7011   va_start(ap, zFmt);
7012   zMsg = sqlite3_vmprintf(zFmt, ap);
7013   sqlite3_result_error(ctx, zMsg, -1);
7014   sqlite3_free(zMsg);
7015   va_end(ap);
7016 }
7017 
7018 /*
7019 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
7020 ** is not quoted, do nothing.
7021 */
7022 static void zipfileDequote(char *zIn){
7023   char q = zIn[0];
7024   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
7025     int iIn = 1;
7026     int iOut = 0;
7027     if( q=='[' ) q = ']';
7028     while( ALWAYS(zIn[iIn]) ){
7029       char c = zIn[iIn++];
7030       if( c==q && zIn[iIn++]!=q ) break;
7031       zIn[iOut++] = c;
7032     }
7033     zIn[iOut] = '\0';
7034   }
7035 }
7036 
7037 /*
7038 ** Construct a new ZipfileTab virtual table object.
7039 **
7040 **   argv[0]   -> module name  ("zipfile")
7041 **   argv[1]   -> database name
7042 **   argv[2]   -> table name
7043 **   argv[...] -> "column name" and other module argument fields.
7044 */
7045 static int zipfileConnect(
7046   sqlite3 *db,
7047   void *pAux,
7048   int argc, const char *const*argv,
7049   sqlite3_vtab **ppVtab,
7050   char **pzErr
7051 ){
7052   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
7053   int nFile = 0;
7054   const char *zFile = 0;
7055   ZipfileTab *pNew = 0;
7056   int rc;
7057 
7058   /* If the table name is not "zipfile", require that the argument be
7059   ** specified. This stops zipfile tables from being created as:
7060   **
7061   **   CREATE VIRTUAL TABLE zzz USING zipfile();
7062   **
7063   ** It does not prevent:
7064   **
7065   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
7066   */
7067   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
7068   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
7069     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
7070     return SQLITE_ERROR;
7071   }
7072 
7073   if( argc>3 ){
7074     zFile = argv[3];
7075     nFile = (int)strlen(zFile)+1;
7076   }
7077 
7078   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
7079   if( rc==SQLITE_OK ){
7080     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
7081     if( pNew==0 ) return SQLITE_NOMEM;
7082     memset(pNew, 0, nByte+nFile);
7083     pNew->db = db;
7084     pNew->aBuffer = (u8*)&pNew[1];
7085     if( zFile ){
7086       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
7087       memcpy(pNew->zFile, zFile, nFile);
7088       zipfileDequote(pNew->zFile);
7089     }
7090   }
7091   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7092   *ppVtab = (sqlite3_vtab*)pNew;
7093   return rc;
7094 }
7095 
7096 /*
7097 ** Free the ZipfileEntry structure indicated by the only argument.
7098 */
7099 static void zipfileEntryFree(ZipfileEntry *p){
7100   if( p ){
7101     sqlite3_free(p->cds.zFile);
7102     sqlite3_free(p);
7103   }
7104 }
7105 
7106 /*
7107 ** Release resources that should be freed at the end of a write
7108 ** transaction.
7109 */
7110 static void zipfileCleanupTransaction(ZipfileTab *pTab){
7111   ZipfileEntry *pEntry;
7112   ZipfileEntry *pNext;
7113 
7114   if( pTab->pWriteFd ){
7115     fclose(pTab->pWriteFd);
7116     pTab->pWriteFd = 0;
7117   }
7118   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
7119     pNext = pEntry->pNext;
7120     zipfileEntryFree(pEntry);
7121   }
7122   pTab->pFirstEntry = 0;
7123   pTab->pLastEntry = 0;
7124   pTab->szCurrent = 0;
7125   pTab->szOrig = 0;
7126 }
7127 
7128 /*
7129 ** This method is the destructor for zipfile vtab objects.
7130 */
7131 static int zipfileDisconnect(sqlite3_vtab *pVtab){
7132   zipfileCleanupTransaction((ZipfileTab*)pVtab);
7133   sqlite3_free(pVtab);
7134   return SQLITE_OK;
7135 }
7136 
7137 /*
7138 ** Constructor for a new ZipfileCsr object.
7139 */
7140 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
7141   ZipfileTab *pTab = (ZipfileTab*)p;
7142   ZipfileCsr *pCsr;
7143   pCsr = sqlite3_malloc(sizeof(*pCsr));
7144   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
7145   if( pCsr==0 ){
7146     return SQLITE_NOMEM;
7147   }
7148   memset(pCsr, 0, sizeof(*pCsr));
7149   pCsr->iId = ++pTab->iNextCsrid;
7150   pCsr->pCsrNext = pTab->pCsrList;
7151   pTab->pCsrList = pCsr;
7152   return SQLITE_OK;
7153 }
7154 
7155 /*
7156 ** Reset a cursor back to the state it was in when first returned
7157 ** by zipfileOpen().
7158 */
7159 static void zipfileResetCursor(ZipfileCsr *pCsr){
7160   ZipfileEntry *p;
7161   ZipfileEntry *pNext;
7162 
7163   pCsr->bEof = 0;
7164   if( pCsr->pFile ){
7165     fclose(pCsr->pFile);
7166     pCsr->pFile = 0;
7167     zipfileEntryFree(pCsr->pCurrent);
7168     pCsr->pCurrent = 0;
7169   }
7170 
7171   for(p=pCsr->pFreeEntry; p; p=pNext){
7172     pNext = p->pNext;
7173     zipfileEntryFree(p);
7174   }
7175 }
7176 
7177 /*
7178 ** Destructor for an ZipfileCsr.
7179 */
7180 static int zipfileClose(sqlite3_vtab_cursor *cur){
7181   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7182   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
7183   ZipfileCsr **pp;
7184   zipfileResetCursor(pCsr);
7185 
7186   /* Remove this cursor from the ZipfileTab.pCsrList list. */
7187   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
7188   *pp = pCsr->pCsrNext;
7189 
7190   sqlite3_free(pCsr);
7191   return SQLITE_OK;
7192 }
7193 
7194 /*
7195 ** Set the error message for the virtual table associated with cursor
7196 ** pCsr to the results of vprintf(zFmt, ...).
7197 */
7198 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
7199   va_list ap;
7200   va_start(ap, zFmt);
7201   sqlite3_free(pTab->base.zErrMsg);
7202   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
7203   va_end(ap);
7204 }
7205 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
7206   va_list ap;
7207   va_start(ap, zFmt);
7208   sqlite3_free(pCsr->base.pVtab->zErrMsg);
7209   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7210   va_end(ap);
7211 }
7212 
7213 /*
7214 ** Read nRead bytes of data from offset iOff of file pFile into buffer
7215 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
7216 ** otherwise.
7217 **
7218 ** If an error does occur, output variable (*pzErrmsg) may be set to point
7219 ** to an English language error message. It is the responsibility of the
7220 ** caller to eventually free this buffer using
7221 ** sqlite3_free().
7222 */
7223 static int zipfileReadData(
7224   FILE *pFile,                    /* Read from this file */
7225   u8 *aRead,                      /* Read into this buffer */
7226   int nRead,                      /* Number of bytes to read */
7227   i64 iOff,                       /* Offset to read from */
7228   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
7229 ){
7230   size_t n;
7231   fseek(pFile, (long)iOff, SEEK_SET);
7232   n = fread(aRead, 1, nRead, pFile);
7233   if( (int)n!=nRead ){
7234     *pzErrmsg = sqlite3_mprintf("error in fread()");
7235     return SQLITE_ERROR;
7236   }
7237   return SQLITE_OK;
7238 }
7239 
7240 static int zipfileAppendData(
7241   ZipfileTab *pTab,
7242   const u8 *aWrite,
7243   int nWrite
7244 ){
7245   if( nWrite>0 ){
7246     size_t n = nWrite;
7247     fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
7248     n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
7249     if( (int)n!=nWrite ){
7250       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
7251       return SQLITE_ERROR;
7252     }
7253     pTab->szCurrent += nWrite;
7254   }
7255   return SQLITE_OK;
7256 }
7257 
7258 /*
7259 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7260 */
7261 static u16 zipfileGetU16(const u8 *aBuf){
7262   return (aBuf[1] << 8) + aBuf[0];
7263 }
7264 
7265 /*
7266 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7267 */
7268 static u32 zipfileGetU32(const u8 *aBuf){
7269   if( aBuf==0 ) return 0;
7270   return ((u32)(aBuf[3]) << 24)
7271        + ((u32)(aBuf[2]) << 16)
7272        + ((u32)(aBuf[1]) <<  8)
7273        + ((u32)(aBuf[0]) <<  0);
7274 }
7275 
7276 /*
7277 ** Write a 16-bit little endiate integer into buffer aBuf.
7278 */
7279 static void zipfilePutU16(u8 *aBuf, u16 val){
7280   aBuf[0] = val & 0xFF;
7281   aBuf[1] = (val>>8) & 0xFF;
7282 }
7283 
7284 /*
7285 ** Write a 32-bit little endiate integer into buffer aBuf.
7286 */
7287 static void zipfilePutU32(u8 *aBuf, u32 val){
7288   aBuf[0] = val & 0xFF;
7289   aBuf[1] = (val>>8) & 0xFF;
7290   aBuf[2] = (val>>16) & 0xFF;
7291   aBuf[3] = (val>>24) & 0xFF;
7292 }
7293 
7294 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7295 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7296 
7297 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
7298 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
7299 
7300 /*
7301 ** Magic numbers used to read CDS records.
7302 */
7303 #define ZIPFILE_CDS_NFILE_OFF        28
7304 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
7305 
7306 /*
7307 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
7308 ** if the record is not well-formed, or SQLITE_OK otherwise.
7309 */
7310 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
7311   u8 *aRead = aBuf;
7312   u32 sig = zipfileRead32(aRead);
7313   int rc = SQLITE_OK;
7314   if( sig!=ZIPFILE_SIGNATURE_CDS ){
7315     rc = SQLITE_ERROR;
7316   }else{
7317     pCDS->iVersionMadeBy = zipfileRead16(aRead);
7318     pCDS->iVersionExtract = zipfileRead16(aRead);
7319     pCDS->flags = zipfileRead16(aRead);
7320     pCDS->iCompression = zipfileRead16(aRead);
7321     pCDS->mTime = zipfileRead16(aRead);
7322     pCDS->mDate = zipfileRead16(aRead);
7323     pCDS->crc32 = zipfileRead32(aRead);
7324     pCDS->szCompressed = zipfileRead32(aRead);
7325     pCDS->szUncompressed = zipfileRead32(aRead);
7326     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7327     pCDS->nFile = zipfileRead16(aRead);
7328     pCDS->nExtra = zipfileRead16(aRead);
7329     pCDS->nComment = zipfileRead16(aRead);
7330     pCDS->iDiskStart = zipfileRead16(aRead);
7331     pCDS->iInternalAttr = zipfileRead16(aRead);
7332     pCDS->iExternalAttr = zipfileRead32(aRead);
7333     pCDS->iOffset = zipfileRead32(aRead);
7334     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
7335   }
7336 
7337   return rc;
7338 }
7339 
7340 /*
7341 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
7342 ** if the record is not well-formed, or SQLITE_OK otherwise.
7343 */
7344 static int zipfileReadLFH(
7345   u8 *aBuffer,
7346   ZipfileLFH *pLFH
7347 ){
7348   u8 *aRead = aBuffer;
7349   int rc = SQLITE_OK;
7350 
7351   u32 sig = zipfileRead32(aRead);
7352   if( sig!=ZIPFILE_SIGNATURE_LFH ){
7353     rc = SQLITE_ERROR;
7354   }else{
7355     pLFH->iVersionExtract = zipfileRead16(aRead);
7356     pLFH->flags = zipfileRead16(aRead);
7357     pLFH->iCompression = zipfileRead16(aRead);
7358     pLFH->mTime = zipfileRead16(aRead);
7359     pLFH->mDate = zipfileRead16(aRead);
7360     pLFH->crc32 = zipfileRead32(aRead);
7361     pLFH->szCompressed = zipfileRead32(aRead);
7362     pLFH->szUncompressed = zipfileRead32(aRead);
7363     pLFH->nFile = zipfileRead16(aRead);
7364     pLFH->nExtra = zipfileRead16(aRead);
7365   }
7366   return rc;
7367 }
7368 
7369 
7370 /*
7371 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
7372 ** Scan through this buffer to find an "extra-timestamp" field. If one
7373 ** exists, extract the 32-bit modification-timestamp from it and store
7374 ** the value in output parameter *pmTime.
7375 **
7376 ** Zero is returned if no extra-timestamp record could be found (and so
7377 ** *pmTime is left unchanged), or non-zero otherwise.
7378 **
7379 ** The general format of an extra field is:
7380 **
7381 **   Header ID    2 bytes
7382 **   Data Size    2 bytes
7383 **   Data         N bytes
7384 */
7385 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
7386   int ret = 0;
7387   u8 *p = aExtra;
7388   u8 *pEnd = &aExtra[nExtra];
7389 
7390   while( p<pEnd ){
7391     u16 id = zipfileRead16(p);
7392     u16 nByte = zipfileRead16(p);
7393 
7394     switch( id ){
7395       case ZIPFILE_EXTRA_TIMESTAMP: {
7396         u8 b = p[0];
7397         if( b & 0x01 ){     /* 0x01 -> modtime is present */
7398           *pmTime = zipfileGetU32(&p[1]);
7399           ret = 1;
7400         }
7401         break;
7402       }
7403     }
7404 
7405     p += nByte;
7406   }
7407   return ret;
7408 }
7409 
7410 /*
7411 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7412 ** fields of the CDS structure passed as the only argument to a 32-bit
7413 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7414 **
7415 ** "Standard" MS-DOS time format:
7416 **
7417 **   File modification time:
7418 **     Bits 00-04: seconds divided by 2
7419 **     Bits 05-10: minute
7420 **     Bits 11-15: hour
7421 **   File modification date:
7422 **     Bits 00-04: day
7423 **     Bits 05-08: month (1-12)
7424 **     Bits 09-15: years from 1980
7425 **
7426 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7427 */
7428 static u32 zipfileMtime(ZipfileCDS *pCDS){
7429   int Y,M,D,X1,X2,A,B,sec,min,hr;
7430   i64 JDsec;
7431   Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
7432   M = ((pCDS->mDate >> 5) & 0x0F);
7433   D = (pCDS->mDate & 0x1F);
7434   sec = (pCDS->mTime & 0x1F)*2;
7435   min = (pCDS->mTime >> 5) & 0x3F;
7436   hr = (pCDS->mTime >> 11) & 0x1F;
7437   if( M<=2 ){
7438     Y--;
7439     M += 12;
7440   }
7441   X1 = 36525*(Y+4716)/100;
7442   X2 = 306001*(M+1)/10000;
7443   A = Y/100;
7444   B = 2 - A + (A/4);
7445   JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
7446   return (u32)(JDsec - (i64)24405875*(i64)8640);
7447 }
7448 
7449 /*
7450 ** The opposite of zipfileMtime(). This function populates the mTime and
7451 ** mDate fields of the CDS structure passed as the first argument according
7452 ** to the UNIX timestamp value passed as the second.
7453 */
7454 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
7455   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
7456   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
7457 
7458   int A, B, C, D, E;
7459   int yr, mon, day;
7460   int hr, min, sec;
7461 
7462   A = (int)((JD - 1867216.25)/36524.25);
7463   A = (int)(JD + 1 + A - (A/4));
7464   B = A + 1524;
7465   C = (int)((B - 122.1)/365.25);
7466   D = (36525*(C&32767))/100;
7467   E = (int)((B-D)/30.6001);
7468 
7469   day = B - D - (int)(30.6001*E);
7470   mon = (E<14 ? E-1 : E-13);
7471   yr = mon>2 ? C-4716 : C-4715;
7472 
7473   hr = (mUnixTime % (24*60*60)) / (60*60);
7474   min = (mUnixTime % (60*60)) / 60;
7475   sec = (mUnixTime % 60);
7476 
7477   if( yr>=1980 ){
7478     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
7479     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
7480   }else{
7481     pCds->mDate = pCds->mTime = 0;
7482   }
7483 
7484   assert( mUnixTime<315507600
7485        || mUnixTime==zipfileMtime(pCds)
7486        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
7487        /* || (mUnixTime % 2) */
7488   );
7489 }
7490 
7491 /*
7492 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7493 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
7494 ** then pFile is a file-handle open on a zip file. In either case, this
7495 ** function creates a ZipfileEntry object based on the zip archive entry
7496 ** for which the CDS record is at offset iOff.
7497 **
7498 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
7499 ** the new object. Otherwise, an SQLite error code is returned and the
7500 ** final value of (*ppEntry) undefined.
7501 */
7502 static int zipfileGetEntry(
7503   ZipfileTab *pTab,               /* Store any error message here */
7504   const u8 *aBlob,                /* Pointer to in-memory file image */
7505   int nBlob,                      /* Size of aBlob[] in bytes */
7506   FILE *pFile,                    /* If aBlob==0, read from this file */
7507   i64 iOff,                       /* Offset of CDS record */
7508   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
7509 ){
7510   u8 *aRead;
7511   char **pzErr = &pTab->base.zErrMsg;
7512   int rc = SQLITE_OK;
7513 
7514   if( aBlob==0 ){
7515     aRead = pTab->aBuffer;
7516     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
7517   }else{
7518     aRead = (u8*)&aBlob[iOff];
7519   }
7520 
7521   if( rc==SQLITE_OK ){
7522     sqlite3_int64 nAlloc;
7523     ZipfileEntry *pNew;
7524 
7525     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
7526     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
7527     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
7528 
7529     nAlloc = sizeof(ZipfileEntry) + nExtra;
7530     if( aBlob ){
7531       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
7532     }
7533 
7534     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
7535     if( pNew==0 ){
7536       rc = SQLITE_NOMEM;
7537     }else{
7538       memset(pNew, 0, sizeof(ZipfileEntry));
7539       rc = zipfileReadCDS(aRead, &pNew->cds);
7540       if( rc!=SQLITE_OK ){
7541         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
7542       }else if( aBlob==0 ){
7543         rc = zipfileReadData(
7544             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
7545         );
7546       }else{
7547         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
7548       }
7549     }
7550 
7551     if( rc==SQLITE_OK ){
7552       u32 *pt = &pNew->mUnixTime;
7553       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
7554       pNew->aExtra = (u8*)&pNew[1];
7555       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
7556       if( pNew->cds.zFile==0 ){
7557         rc = SQLITE_NOMEM;
7558       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
7559         pNew->mUnixTime = zipfileMtime(&pNew->cds);
7560       }
7561     }
7562 
7563     if( rc==SQLITE_OK ){
7564       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
7565       ZipfileLFH lfh;
7566       if( pFile ){
7567         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
7568       }else{
7569         aRead = (u8*)&aBlob[pNew->cds.iOffset];
7570       }
7571 
7572       if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
7573       if( rc==SQLITE_OK ){
7574         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
7575         pNew->iDataOff += lfh.nFile + lfh.nExtra;
7576         if( aBlob && pNew->cds.szCompressed ){
7577           pNew->aData = &pNew->aExtra[nExtra];
7578           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
7579         }
7580       }else{
7581         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
7582             (int)pNew->cds.iOffset
7583         );
7584       }
7585     }
7586 
7587     if( rc!=SQLITE_OK ){
7588       zipfileEntryFree(pNew);
7589     }else{
7590       *ppEntry = pNew;
7591     }
7592   }
7593 
7594   return rc;
7595 }
7596 
7597 /*
7598 ** Advance an ZipfileCsr to its next row of output.
7599 */
7600 static int zipfileNext(sqlite3_vtab_cursor *cur){
7601   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7602   int rc = SQLITE_OK;
7603 
7604   if( pCsr->pFile ){
7605     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
7606     zipfileEntryFree(pCsr->pCurrent);
7607     pCsr->pCurrent = 0;
7608     if( pCsr->iNextOff>=iEof ){
7609       pCsr->bEof = 1;
7610     }else{
7611       ZipfileEntry *p = 0;
7612       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
7613       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
7614       if( rc==SQLITE_OK ){
7615         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
7616         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
7617       }
7618       pCsr->pCurrent = p;
7619     }
7620   }else{
7621     if( !pCsr->bNoop ){
7622       pCsr->pCurrent = pCsr->pCurrent->pNext;
7623     }
7624     if( pCsr->pCurrent==0 ){
7625       pCsr->bEof = 1;
7626     }
7627   }
7628 
7629   pCsr->bNoop = 0;
7630   return rc;
7631 }
7632 
7633 static void zipfileFree(void *p) {
7634   sqlite3_free(p);
7635 }
7636 
7637 /*
7638 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
7639 ** size is nOut bytes. This function uncompresses the data and sets the
7640 ** return value in context pCtx to the result (a blob).
7641 **
7642 ** If an error occurs, an error code is left in pCtx instead.
7643 */
7644 static void zipfileInflate(
7645   sqlite3_context *pCtx,          /* Store result here */
7646   const u8 *aIn,                  /* Compressed data */
7647   int nIn,                        /* Size of buffer aIn[] in bytes */
7648   int nOut                        /* Expected output size */
7649 ){
7650   u8 *aRes = sqlite3_malloc(nOut);
7651   if( aRes==0 ){
7652     sqlite3_result_error_nomem(pCtx);
7653   }else{
7654     int err;
7655     z_stream str;
7656     memset(&str, 0, sizeof(str));
7657 
7658     str.next_in = (Byte*)aIn;
7659     str.avail_in = nIn;
7660     str.next_out = (Byte*)aRes;
7661     str.avail_out = nOut;
7662 
7663     err = inflateInit2(&str, -15);
7664     if( err!=Z_OK ){
7665       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
7666     }else{
7667       err = inflate(&str, Z_NO_FLUSH);
7668       if( err!=Z_STREAM_END ){
7669         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
7670       }else{
7671         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
7672         aRes = 0;
7673       }
7674     }
7675     sqlite3_free(aRes);
7676     inflateEnd(&str);
7677   }
7678 }
7679 
7680 /*
7681 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
7682 ** compresses it and sets (*ppOut) to point to a buffer containing the
7683 ** compressed data. The caller is responsible for eventually calling
7684 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
7685 ** is set to the size of buffer (*ppOut) in bytes.
7686 **
7687 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
7688 ** code is returned and an error message left in virtual-table handle
7689 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
7690 ** case.
7691 */
7692 static int zipfileDeflate(
7693   const u8 *aIn, int nIn,         /* Input */
7694   u8 **ppOut, int *pnOut,         /* Output */
7695   char **pzErr                    /* OUT: Error message */
7696 ){
7697   int rc = SQLITE_OK;
7698   sqlite3_int64 nAlloc;
7699   z_stream str;
7700   u8 *aOut;
7701 
7702   memset(&str, 0, sizeof(str));
7703   str.next_in = (Bytef*)aIn;
7704   str.avail_in = nIn;
7705   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
7706 
7707   nAlloc = deflateBound(&str, nIn);
7708   aOut = (u8*)sqlite3_malloc64(nAlloc);
7709   if( aOut==0 ){
7710     rc = SQLITE_NOMEM;
7711   }else{
7712     int res;
7713     str.next_out = aOut;
7714     str.avail_out = nAlloc;
7715     res = deflate(&str, Z_FINISH);
7716     if( res==Z_STREAM_END ){
7717       *ppOut = aOut;
7718       *pnOut = (int)str.total_out;
7719     }else{
7720       sqlite3_free(aOut);
7721       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
7722       rc = SQLITE_ERROR;
7723     }
7724     deflateEnd(&str);
7725   }
7726 
7727   return rc;
7728 }
7729 
7730 
7731 /*
7732 ** Return values of columns for the row at which the series_cursor
7733 ** is currently pointing.
7734 */
7735 static int zipfileColumn(
7736   sqlite3_vtab_cursor *cur,   /* The cursor */
7737   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
7738   int i                       /* Which column to return */
7739 ){
7740   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7741   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
7742   int rc = SQLITE_OK;
7743   switch( i ){
7744     case 0:   /* name */
7745       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
7746       break;
7747     case 1:   /* mode */
7748       /* TODO: Whether or not the following is correct surely depends on
7749       ** the platform on which the archive was created.  */
7750       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
7751       break;
7752     case 2: { /* mtime */
7753       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
7754       break;
7755     }
7756     case 3: { /* sz */
7757       if( sqlite3_vtab_nochange(ctx)==0 ){
7758         sqlite3_result_int64(ctx, pCDS->szUncompressed);
7759       }
7760       break;
7761     }
7762     case 4:   /* rawdata */
7763       if( sqlite3_vtab_nochange(ctx) ) break;
7764     case 5: { /* data */
7765       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
7766         int sz = pCDS->szCompressed;
7767         int szFinal = pCDS->szUncompressed;
7768         if( szFinal>0 ){
7769           u8 *aBuf;
7770           u8 *aFree = 0;
7771           if( pCsr->pCurrent->aData ){
7772             aBuf = pCsr->pCurrent->aData;
7773           }else{
7774             aBuf = aFree = sqlite3_malloc64(sz);
7775             if( aBuf==0 ){
7776               rc = SQLITE_NOMEM;
7777             }else{
7778               FILE *pFile = pCsr->pFile;
7779               if( pFile==0 ){
7780                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
7781               }
7782               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
7783                   &pCsr->base.pVtab->zErrMsg
7784               );
7785             }
7786           }
7787           if( rc==SQLITE_OK ){
7788             if( i==5 && pCDS->iCompression ){
7789               zipfileInflate(ctx, aBuf, sz, szFinal);
7790             }else{
7791               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
7792             }
7793           }
7794           sqlite3_free(aFree);
7795         }else{
7796           /* Figure out if this is a directory or a zero-sized file. Consider
7797           ** it to be a directory either if the mode suggests so, or if
7798           ** the final character in the name is '/'.  */
7799           u32 mode = pCDS->iExternalAttr >> 16;
7800           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
7801             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
7802           }
7803         }
7804       }
7805       break;
7806     }
7807     case 6:   /* method */
7808       sqlite3_result_int(ctx, pCDS->iCompression);
7809       break;
7810     default:  /* z */
7811       assert( i==7 );
7812       sqlite3_result_int64(ctx, pCsr->iId);
7813       break;
7814   }
7815 
7816   return rc;
7817 }
7818 
7819 /*
7820 ** Return TRUE if the cursor is at EOF.
7821 */
7822 static int zipfileEof(sqlite3_vtab_cursor *cur){
7823   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7824   return pCsr->bEof;
7825 }
7826 
7827 /*
7828 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7829 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7830 ** is guaranteed to be a file-handle open on a zip file.
7831 **
7832 ** This function attempts to locate the EOCD record within the zip archive
7833 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7834 ** returned if successful. Otherwise, an SQLite error code is returned and
7835 ** an English language error message may be left in virtual-table pTab.
7836 */
7837 static int zipfileReadEOCD(
7838   ZipfileTab *pTab,               /* Return errors here */
7839   const u8 *aBlob,                /* Pointer to in-memory file image */
7840   int nBlob,                      /* Size of aBlob[] in bytes */
7841   FILE *pFile,                    /* Read from this file if aBlob==0 */
7842   ZipfileEOCD *pEOCD              /* Object to populate */
7843 ){
7844   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
7845   int nRead;                      /* Bytes to read from file */
7846   int rc = SQLITE_OK;
7847 
7848   memset(pEOCD, 0, sizeof(ZipfileEOCD));
7849   if( aBlob==0 ){
7850     i64 iOff;                     /* Offset to read from */
7851     i64 szFile;                   /* Total size of file in bytes */
7852     fseek(pFile, 0, SEEK_END);
7853     szFile = (i64)ftell(pFile);
7854     if( szFile==0 ){
7855       return SQLITE_OK;
7856     }
7857     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7858     iOff = szFile - nRead;
7859     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7860   }else{
7861     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7862     aRead = (u8*)&aBlob[nBlob-nRead];
7863   }
7864 
7865   if( rc==SQLITE_OK ){
7866     int i;
7867 
7868     /* Scan backwards looking for the signature bytes */
7869     for(i=nRead-20; i>=0; i--){
7870       if( aRead[i]==0x50 && aRead[i+1]==0x4b
7871        && aRead[i+2]==0x05 && aRead[i+3]==0x06
7872       ){
7873         break;
7874       }
7875     }
7876     if( i<0 ){
7877       pTab->base.zErrMsg = sqlite3_mprintf(
7878           "cannot find end of central directory record"
7879       );
7880       return SQLITE_ERROR;
7881     }
7882 
7883     aRead += i+4;
7884     pEOCD->iDisk = zipfileRead16(aRead);
7885     pEOCD->iFirstDisk = zipfileRead16(aRead);
7886     pEOCD->nEntry = zipfileRead16(aRead);
7887     pEOCD->nEntryTotal = zipfileRead16(aRead);
7888     pEOCD->nSize = zipfileRead32(aRead);
7889     pEOCD->iOffset = zipfileRead32(aRead);
7890   }
7891 
7892   return rc;
7893 }
7894 
7895 /*
7896 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
7897 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7898 ** to the end of the list. Otherwise, it is added to the list immediately
7899 ** before pBefore (which is guaranteed to be a part of said list).
7900 */
7901 static void zipfileAddEntry(
7902   ZipfileTab *pTab,
7903   ZipfileEntry *pBefore,
7904   ZipfileEntry *pNew
7905 ){
7906   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7907   assert( pNew->pNext==0 );
7908   if( pBefore==0 ){
7909     if( pTab->pFirstEntry==0 ){
7910       pTab->pFirstEntry = pTab->pLastEntry = pNew;
7911     }else{
7912       assert( pTab->pLastEntry->pNext==0 );
7913       pTab->pLastEntry->pNext = pNew;
7914       pTab->pLastEntry = pNew;
7915     }
7916   }else{
7917     ZipfileEntry **pp;
7918     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7919     pNew->pNext = pBefore;
7920     *pp = pNew;
7921   }
7922 }
7923 
7924 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7925   ZipfileEOCD eocd;
7926   int rc;
7927   int i;
7928   i64 iOff;
7929 
7930   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7931   iOff = eocd.iOffset;
7932   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7933     ZipfileEntry *pNew = 0;
7934     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7935 
7936     if( rc==SQLITE_OK ){
7937       zipfileAddEntry(pTab, 0, pNew);
7938       iOff += ZIPFILE_CDS_FIXED_SZ;
7939       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7940     }
7941   }
7942   return rc;
7943 }
7944 
7945 /*
7946 ** xFilter callback.
7947 */
7948 static int zipfileFilter(
7949   sqlite3_vtab_cursor *cur,
7950   int idxNum, const char *idxStr,
7951   int argc, sqlite3_value **argv
7952 ){
7953   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7954   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7955   const char *zFile = 0;          /* Zip file to scan */
7956   int rc = SQLITE_OK;             /* Return Code */
7957   int bInMemory = 0;              /* True for an in-memory zipfile */
7958 
7959   zipfileResetCursor(pCsr);
7960 
7961   if( pTab->zFile ){
7962     zFile = pTab->zFile;
7963   }else if( idxNum==0 ){
7964     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7965     return SQLITE_ERROR;
7966   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7967     static const u8 aEmptyBlob = 0;
7968     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7969     int nBlob = sqlite3_value_bytes(argv[0]);
7970     assert( pTab->pFirstEntry==0 );
7971     if( aBlob==0 ){
7972       aBlob = &aEmptyBlob;
7973       nBlob = 0;
7974     }
7975     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7976     pCsr->pFreeEntry = pTab->pFirstEntry;
7977     pTab->pFirstEntry = pTab->pLastEntry = 0;
7978     if( rc!=SQLITE_OK ) return rc;
7979     bInMemory = 1;
7980   }else{
7981     zFile = (const char*)sqlite3_value_text(argv[0]);
7982   }
7983 
7984   if( 0==pTab->pWriteFd && 0==bInMemory ){
7985     pCsr->pFile = fopen(zFile, "rb");
7986     if( pCsr->pFile==0 ){
7987       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7988       rc = SQLITE_ERROR;
7989     }else{
7990       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7991       if( rc==SQLITE_OK ){
7992         if( pCsr->eocd.nEntry==0 ){
7993           pCsr->bEof = 1;
7994         }else{
7995           pCsr->iNextOff = pCsr->eocd.iOffset;
7996           rc = zipfileNext(cur);
7997         }
7998       }
7999     }
8000   }else{
8001     pCsr->bNoop = 1;
8002     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
8003     rc = zipfileNext(cur);
8004   }
8005 
8006   return rc;
8007 }
8008 
8009 /*
8010 ** xBestIndex callback.
8011 */
8012 static int zipfileBestIndex(
8013   sqlite3_vtab *tab,
8014   sqlite3_index_info *pIdxInfo
8015 ){
8016   int i;
8017   int idx = -1;
8018   int unusable = 0;
8019 
8020   for(i=0; i<pIdxInfo->nConstraint; i++){
8021     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8022     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
8023     if( pCons->usable==0 ){
8024       unusable = 1;
8025     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8026       idx = i;
8027     }
8028   }
8029   pIdxInfo->estimatedCost = 1000.0;
8030   if( idx>=0 ){
8031     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
8032     pIdxInfo->aConstraintUsage[idx].omit = 1;
8033     pIdxInfo->idxNum = 1;
8034   }else if( unusable ){
8035     return SQLITE_CONSTRAINT;
8036   }
8037   return SQLITE_OK;
8038 }
8039 
8040 static ZipfileEntry *zipfileNewEntry(const char *zPath){
8041   ZipfileEntry *pNew;
8042   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
8043   if( pNew ){
8044     memset(pNew, 0, sizeof(ZipfileEntry));
8045     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
8046     if( pNew->cds.zFile==0 ){
8047       sqlite3_free(pNew);
8048       pNew = 0;
8049     }
8050   }
8051   return pNew;
8052 }
8053 
8054 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
8055   ZipfileCDS *pCds = &pEntry->cds;
8056   u8 *a = aBuf;
8057 
8058   pCds->nExtra = 9;
8059 
8060   /* Write the LFH itself */
8061   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
8062   zipfileWrite16(a, pCds->iVersionExtract);
8063   zipfileWrite16(a, pCds->flags);
8064   zipfileWrite16(a, pCds->iCompression);
8065   zipfileWrite16(a, pCds->mTime);
8066   zipfileWrite16(a, pCds->mDate);
8067   zipfileWrite32(a, pCds->crc32);
8068   zipfileWrite32(a, pCds->szCompressed);
8069   zipfileWrite32(a, pCds->szUncompressed);
8070   zipfileWrite16(a, (u16)pCds->nFile);
8071   zipfileWrite16(a, pCds->nExtra);
8072   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
8073 
8074   /* Add the file name */
8075   memcpy(a, pCds->zFile, (int)pCds->nFile);
8076   a += (int)pCds->nFile;
8077 
8078   /* The "extra" data */
8079   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8080   zipfileWrite16(a, 5);
8081   *a++ = 0x01;
8082   zipfileWrite32(a, pEntry->mUnixTime);
8083 
8084   return a-aBuf;
8085 }
8086 
8087 static int zipfileAppendEntry(
8088   ZipfileTab *pTab,
8089   ZipfileEntry *pEntry,
8090   const u8 *pData,
8091   int nData
8092 ){
8093   u8 *aBuf = pTab->aBuffer;
8094   int nBuf;
8095   int rc;
8096 
8097   nBuf = zipfileSerializeLFH(pEntry, aBuf);
8098   rc = zipfileAppendData(pTab, aBuf, nBuf);
8099   if( rc==SQLITE_OK ){
8100     pEntry->iDataOff = pTab->szCurrent;
8101     rc = zipfileAppendData(pTab, pData, nData);
8102   }
8103 
8104   return rc;
8105 }
8106 
8107 static int zipfileGetMode(
8108   sqlite3_value *pVal,
8109   int bIsDir,                     /* If true, default to directory */
8110   u32 *pMode,                     /* OUT: Mode value */
8111   char **pzErr                    /* OUT: Error message */
8112 ){
8113   const char *z = (const char*)sqlite3_value_text(pVal);
8114   u32 mode = 0;
8115   if( z==0 ){
8116     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
8117   }else if( z[0]>='0' && z[0]<='9' ){
8118     mode = (unsigned int)sqlite3_value_int(pVal);
8119   }else{
8120     const char zTemplate[11] = "-rwxrwxrwx";
8121     int i;
8122     if( strlen(z)!=10 ) goto parse_error;
8123     switch( z[0] ){
8124       case '-': mode |= S_IFREG; break;
8125       case 'd': mode |= S_IFDIR; break;
8126       case 'l': mode |= S_IFLNK; break;
8127       default: goto parse_error;
8128     }
8129     for(i=1; i<10; i++){
8130       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
8131       else if( z[i]!='-' ) goto parse_error;
8132     }
8133   }
8134   if( ((mode & S_IFDIR)==0)==bIsDir ){
8135     /* The "mode" attribute is a directory, but data has been specified.
8136     ** Or vice-versa - no data but "mode" is a file or symlink.  */
8137     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
8138     return SQLITE_CONSTRAINT;
8139   }
8140   *pMode = mode;
8141   return SQLITE_OK;
8142 
8143  parse_error:
8144   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
8145   return SQLITE_ERROR;
8146 }
8147 
8148 /*
8149 ** Both (const char*) arguments point to nul-terminated strings. Argument
8150 ** nB is the value of strlen(zB). This function returns 0 if the strings are
8151 ** identical, ignoring any trailing '/' character in either path.  */
8152 static int zipfileComparePath(const char *zA, const char *zB, int nB){
8153   int nA = (int)strlen(zA);
8154   if( nA>0 && zA[nA-1]=='/' ) nA--;
8155   if( nB>0 && zB[nB-1]=='/' ) nB--;
8156   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
8157   return 1;
8158 }
8159 
8160 static int zipfileBegin(sqlite3_vtab *pVtab){
8161   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8162   int rc = SQLITE_OK;
8163 
8164   assert( pTab->pWriteFd==0 );
8165   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
8166     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
8167     return SQLITE_ERROR;
8168   }
8169 
8170   /* Open a write fd on the file. Also load the entire central directory
8171   ** structure into memory. During the transaction any new file data is
8172   ** appended to the archive file, but the central directory is accumulated
8173   ** in main-memory until the transaction is committed.  */
8174   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
8175   if( pTab->pWriteFd==0 ){
8176     pTab->base.zErrMsg = sqlite3_mprintf(
8177         "zipfile: failed to open file %s for writing", pTab->zFile
8178         );
8179     rc = SQLITE_ERROR;
8180   }else{
8181     fseek(pTab->pWriteFd, 0, SEEK_END);
8182     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
8183     rc = zipfileLoadDirectory(pTab, 0, 0);
8184   }
8185 
8186   if( rc!=SQLITE_OK ){
8187     zipfileCleanupTransaction(pTab);
8188   }
8189 
8190   return rc;
8191 }
8192 
8193 /*
8194 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8195 ** time(2)).
8196 */
8197 static u32 zipfileTime(void){
8198   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
8199   u32 ret;
8200   if( pVfs==0 ) return 0;
8201   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
8202     i64 ms;
8203     pVfs->xCurrentTimeInt64(pVfs, &ms);
8204     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
8205   }else{
8206     double day;
8207     pVfs->xCurrentTime(pVfs, &day);
8208     ret = (u32)((day - 2440587.5) * 86400);
8209   }
8210   return ret;
8211 }
8212 
8213 /*
8214 ** Return a 32-bit timestamp in UNIX epoch format.
8215 **
8216 ** If the value passed as the only argument is either NULL or an SQL NULL,
8217 ** return the current time. Otherwise, return the value stored in (*pVal)
8218 ** cast to a 32-bit unsigned integer.
8219 */
8220 static u32 zipfileGetTime(sqlite3_value *pVal){
8221   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
8222     return zipfileTime();
8223   }
8224   return (u32)sqlite3_value_int64(pVal);
8225 }
8226 
8227 /*
8228 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8229 ** linked list.  Remove it from the list and free the object.
8230 */
8231 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
8232   if( pOld ){
8233     ZipfileEntry **pp;
8234     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
8235     *pp = (*pp)->pNext;
8236     zipfileEntryFree(pOld);
8237   }
8238 }
8239 
8240 /*
8241 ** xUpdate method.
8242 */
8243 static int zipfileUpdate(
8244   sqlite3_vtab *pVtab,
8245   int nVal,
8246   sqlite3_value **apVal,
8247   sqlite_int64 *pRowid
8248 ){
8249   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8250   int rc = SQLITE_OK;             /* Return Code */
8251   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
8252 
8253   u32 mode = 0;                   /* Mode for new entry */
8254   u32 mTime = 0;                  /* Modification time for new entry */
8255   i64 sz = 0;                     /* Uncompressed size */
8256   const char *zPath = 0;          /* Path for new entry */
8257   int nPath = 0;                  /* strlen(zPath) */
8258   const u8 *pData = 0;            /* Pointer to buffer containing content */
8259   int nData = 0;                  /* Size of pData buffer in bytes */
8260   int iMethod = 0;                /* Compression method for new entry */
8261   u8 *pFree = 0;                  /* Free this */
8262   char *zFree = 0;                /* Also free this */
8263   ZipfileEntry *pOld = 0;
8264   ZipfileEntry *pOld2 = 0;
8265   int bUpdate = 0;                /* True for an update that modifies "name" */
8266   int bIsDir = 0;
8267   u32 iCrc32 = 0;
8268 
8269   if( pTab->pWriteFd==0 ){
8270     rc = zipfileBegin(pVtab);
8271     if( rc!=SQLITE_OK ) return rc;
8272   }
8273 
8274   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
8275   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
8276     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
8277     int nDelete = (int)strlen(zDelete);
8278     if( nVal>1 ){
8279       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
8280       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
8281         bUpdate = 1;
8282       }
8283     }
8284     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
8285       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
8286         break;
8287       }
8288       assert( pOld->pNext );
8289     }
8290   }
8291 
8292   if( nVal>1 ){
8293     /* Check that "sz" and "rawdata" are both NULL: */
8294     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
8295       zipfileTableErr(pTab, "sz must be NULL");
8296       rc = SQLITE_CONSTRAINT;
8297     }
8298     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
8299       zipfileTableErr(pTab, "rawdata must be NULL");
8300       rc = SQLITE_CONSTRAINT;
8301     }
8302 
8303     if( rc==SQLITE_OK ){
8304       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
8305         /* data=NULL. A directory */
8306         bIsDir = 1;
8307       }else{
8308         /* Value specified for "data", and possibly "method". This must be
8309         ** a regular file or a symlink. */
8310         const u8 *aIn = sqlite3_value_blob(apVal[7]);
8311         int nIn = sqlite3_value_bytes(apVal[7]);
8312         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
8313 
8314         iMethod = sqlite3_value_int(apVal[8]);
8315         sz = nIn;
8316         pData = aIn;
8317         nData = nIn;
8318         if( iMethod!=0 && iMethod!=8 ){
8319           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
8320           rc = SQLITE_CONSTRAINT;
8321         }else{
8322           if( bAuto || iMethod ){
8323             int nCmp;
8324             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
8325             if( rc==SQLITE_OK ){
8326               if( iMethod || nCmp<nIn ){
8327                 iMethod = 8;
8328                 pData = pFree;
8329                 nData = nCmp;
8330               }
8331             }
8332           }
8333           iCrc32 = crc32(0, aIn, nIn);
8334         }
8335       }
8336     }
8337 
8338     if( rc==SQLITE_OK ){
8339       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
8340     }
8341 
8342     if( rc==SQLITE_OK ){
8343       zPath = (const char*)sqlite3_value_text(apVal[2]);
8344       if( zPath==0 ) zPath = "";
8345       nPath = (int)strlen(zPath);
8346       mTime = zipfileGetTime(apVal[4]);
8347     }
8348 
8349     if( rc==SQLITE_OK && bIsDir ){
8350       /* For a directory, check that the last character in the path is a
8351       ** '/'. This appears to be required for compatibility with info-zip
8352       ** (the unzip command on unix). It does not create directories
8353       ** otherwise.  */
8354       if( nPath<=0 || zPath[nPath-1]!='/' ){
8355         zFree = sqlite3_mprintf("%s/", zPath);
8356         zPath = (const char*)zFree;
8357         if( zFree==0 ){
8358           rc = SQLITE_NOMEM;
8359           nPath = 0;
8360         }else{
8361           nPath = (int)strlen(zPath);
8362         }
8363       }
8364     }
8365 
8366     /* Check that we're not inserting a duplicate entry -OR- updating an
8367     ** entry with a path, thereby making it into a duplicate. */
8368     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
8369       ZipfileEntry *p;
8370       for(p=pTab->pFirstEntry; p; p=p->pNext){
8371         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
8372           switch( sqlite3_vtab_on_conflict(pTab->db) ){
8373             case SQLITE_IGNORE: {
8374               goto zipfile_update_done;
8375             }
8376             case SQLITE_REPLACE: {
8377               pOld2 = p;
8378               break;
8379             }
8380             default: {
8381               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
8382               rc = SQLITE_CONSTRAINT;
8383               break;
8384             }
8385           }
8386           break;
8387         }
8388       }
8389     }
8390 
8391     if( rc==SQLITE_OK ){
8392       /* Create the new CDS record. */
8393       pNew = zipfileNewEntry(zPath);
8394       if( pNew==0 ){
8395         rc = SQLITE_NOMEM;
8396       }else{
8397         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8398         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8399         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8400         pNew->cds.iCompression = (u16)iMethod;
8401         zipfileMtimeToDos(&pNew->cds, mTime);
8402         pNew->cds.crc32 = iCrc32;
8403         pNew->cds.szCompressed = nData;
8404         pNew->cds.szUncompressed = (u32)sz;
8405         pNew->cds.iExternalAttr = (mode<<16);
8406         pNew->cds.iOffset = (u32)pTab->szCurrent;
8407         pNew->cds.nFile = (u16)nPath;
8408         pNew->mUnixTime = (u32)mTime;
8409         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
8410         zipfileAddEntry(pTab, pOld, pNew);
8411       }
8412     }
8413   }
8414 
8415   if( rc==SQLITE_OK && (pOld || pOld2) ){
8416     ZipfileCsr *pCsr;
8417     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8418       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
8419         pCsr->pCurrent = pCsr->pCurrent->pNext;
8420         pCsr->bNoop = 1;
8421       }
8422     }
8423 
8424     zipfileRemoveEntryFromList(pTab, pOld);
8425     zipfileRemoveEntryFromList(pTab, pOld2);
8426   }
8427 
8428 zipfile_update_done:
8429   sqlite3_free(pFree);
8430   sqlite3_free(zFree);
8431   return rc;
8432 }
8433 
8434 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
8435   u8 *a = aBuf;
8436   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
8437   zipfileWrite16(a, p->iDisk);
8438   zipfileWrite16(a, p->iFirstDisk);
8439   zipfileWrite16(a, p->nEntry);
8440   zipfileWrite16(a, p->nEntryTotal);
8441   zipfileWrite32(a, p->nSize);
8442   zipfileWrite32(a, p->iOffset);
8443   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
8444 
8445   return a-aBuf;
8446 }
8447 
8448 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
8449   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
8450   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
8451   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
8452 }
8453 
8454 /*
8455 ** Serialize the CDS structure into buffer aBuf[]. Return the number
8456 ** of bytes written.
8457 */
8458 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
8459   u8 *a = aBuf;
8460   ZipfileCDS *pCDS = &pEntry->cds;
8461 
8462   if( pEntry->aExtra==0 ){
8463     pCDS->nExtra = 9;
8464   }
8465 
8466   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
8467   zipfileWrite16(a, pCDS->iVersionMadeBy);
8468   zipfileWrite16(a, pCDS->iVersionExtract);
8469   zipfileWrite16(a, pCDS->flags);
8470   zipfileWrite16(a, pCDS->iCompression);
8471   zipfileWrite16(a, pCDS->mTime);
8472   zipfileWrite16(a, pCDS->mDate);
8473   zipfileWrite32(a, pCDS->crc32);
8474   zipfileWrite32(a, pCDS->szCompressed);
8475   zipfileWrite32(a, pCDS->szUncompressed);
8476   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
8477   zipfileWrite16(a, pCDS->nFile);
8478   zipfileWrite16(a, pCDS->nExtra);
8479   zipfileWrite16(a, pCDS->nComment);
8480   zipfileWrite16(a, pCDS->iDiskStart);
8481   zipfileWrite16(a, pCDS->iInternalAttr);
8482   zipfileWrite32(a, pCDS->iExternalAttr);
8483   zipfileWrite32(a, pCDS->iOffset);
8484 
8485   memcpy(a, pCDS->zFile, pCDS->nFile);
8486   a += pCDS->nFile;
8487 
8488   if( pEntry->aExtra ){
8489     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
8490     memcpy(a, pEntry->aExtra, n);
8491     a += n;
8492   }else{
8493     assert( pCDS->nExtra==9 );
8494     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8495     zipfileWrite16(a, 5);
8496     *a++ = 0x01;
8497     zipfileWrite32(a, pEntry->mUnixTime);
8498   }
8499 
8500   return a-aBuf;
8501 }
8502 
8503 static int zipfileCommit(sqlite3_vtab *pVtab){
8504   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8505   int rc = SQLITE_OK;
8506   if( pTab->pWriteFd ){
8507     i64 iOffset = pTab->szCurrent;
8508     ZipfileEntry *p;
8509     ZipfileEOCD eocd;
8510     int nEntry = 0;
8511 
8512     /* Write out all entries */
8513     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
8514       int n = zipfileSerializeCDS(p, pTab->aBuffer);
8515       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
8516       nEntry++;
8517     }
8518 
8519     /* Write out the EOCD record */
8520     eocd.iDisk = 0;
8521     eocd.iFirstDisk = 0;
8522     eocd.nEntry = (u16)nEntry;
8523     eocd.nEntryTotal = (u16)nEntry;
8524     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
8525     eocd.iOffset = (u32)iOffset;
8526     rc = zipfileAppendEOCD(pTab, &eocd);
8527 
8528     zipfileCleanupTransaction(pTab);
8529   }
8530   return rc;
8531 }
8532 
8533 static int zipfileRollback(sqlite3_vtab *pVtab){
8534   return zipfileCommit(pVtab);
8535 }
8536 
8537 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
8538   ZipfileCsr *pCsr;
8539   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8540     if( iId==pCsr->iId ) break;
8541   }
8542   return pCsr;
8543 }
8544 
8545 static void zipfileFunctionCds(
8546   sqlite3_context *context,
8547   int argc,
8548   sqlite3_value **argv
8549 ){
8550   ZipfileCsr *pCsr;
8551   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
8552   assert( argc>0 );
8553 
8554   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
8555   if( pCsr ){
8556     ZipfileCDS *p = &pCsr->pCurrent->cds;
8557     char *zRes = sqlite3_mprintf("{"
8558         "\"version-made-by\" : %u, "
8559         "\"version-to-extract\" : %u, "
8560         "\"flags\" : %u, "
8561         "\"compression\" : %u, "
8562         "\"time\" : %u, "
8563         "\"date\" : %u, "
8564         "\"crc32\" : %u, "
8565         "\"compressed-size\" : %u, "
8566         "\"uncompressed-size\" : %u, "
8567         "\"file-name-length\" : %u, "
8568         "\"extra-field-length\" : %u, "
8569         "\"file-comment-length\" : %u, "
8570         "\"disk-number-start\" : %u, "
8571         "\"internal-attr\" : %u, "
8572         "\"external-attr\" : %u, "
8573         "\"offset\" : %u }",
8574         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
8575         (u32)p->flags, (u32)p->iCompression,
8576         (u32)p->mTime, (u32)p->mDate,
8577         (u32)p->crc32, (u32)p->szCompressed,
8578         (u32)p->szUncompressed, (u32)p->nFile,
8579         (u32)p->nExtra, (u32)p->nComment,
8580         (u32)p->iDiskStart, (u32)p->iInternalAttr,
8581         (u32)p->iExternalAttr, (u32)p->iOffset
8582     );
8583 
8584     if( zRes==0 ){
8585       sqlite3_result_error_nomem(context);
8586     }else{
8587       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
8588       sqlite3_free(zRes);
8589     }
8590   }
8591 }
8592 
8593 /*
8594 ** xFindFunction method.
8595 */
8596 static int zipfileFindFunction(
8597   sqlite3_vtab *pVtab,            /* Virtual table handle */
8598   int nArg,                       /* Number of SQL function arguments */
8599   const char *zName,              /* Name of SQL function */
8600   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
8601   void **ppArg                    /* OUT: User data for *pxFunc */
8602 ){
8603   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
8604     *pxFunc = zipfileFunctionCds;
8605     *ppArg = (void*)pVtab;
8606     return 1;
8607   }
8608   return 0;
8609 }
8610 
8611 typedef struct ZipfileBuffer ZipfileBuffer;
8612 struct ZipfileBuffer {
8613   u8 *a;                          /* Pointer to buffer */
8614   int n;                          /* Size of buffer in bytes */
8615   int nAlloc;                     /* Byte allocated at a[] */
8616 };
8617 
8618 typedef struct ZipfileCtx ZipfileCtx;
8619 struct ZipfileCtx {
8620   int nEntry;
8621   ZipfileBuffer body;
8622   ZipfileBuffer cds;
8623 };
8624 
8625 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
8626   if( pBuf->n+nByte>pBuf->nAlloc ){
8627     u8 *aNew;
8628     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
8629     int nReq = pBuf->n + nByte;
8630 
8631     while( nNew<nReq ) nNew = nNew*2;
8632     aNew = sqlite3_realloc64(pBuf->a, nNew);
8633     if( aNew==0 ) return SQLITE_NOMEM;
8634     pBuf->a = aNew;
8635     pBuf->nAlloc = (int)nNew;
8636   }
8637   return SQLITE_OK;
8638 }
8639 
8640 /*
8641 ** xStep() callback for the zipfile() aggregate. This can be called in
8642 ** any of the following ways:
8643 **
8644 **   SELECT zipfile(name,data) ...
8645 **   SELECT zipfile(name,mode,mtime,data) ...
8646 **   SELECT zipfile(name,mode,mtime,data,method) ...
8647 */
8648 static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
8649   ZipfileCtx *p;                  /* Aggregate function context */
8650   ZipfileEntry e;                 /* New entry to add to zip archive */
8651 
8652   sqlite3_value *pName = 0;
8653   sqlite3_value *pMode = 0;
8654   sqlite3_value *pMtime = 0;
8655   sqlite3_value *pData = 0;
8656   sqlite3_value *pMethod = 0;
8657 
8658   int bIsDir = 0;
8659   u32 mode;
8660   int rc = SQLITE_OK;
8661   char *zErr = 0;
8662 
8663   int iMethod = -1;               /* Compression method to use (0 or 8) */
8664 
8665   const u8 *aData = 0;            /* Possibly compressed data for new entry */
8666   int nData = 0;                  /* Size of aData[] in bytes */
8667   int szUncompressed = 0;         /* Size of data before compression */
8668   u8 *aFree = 0;                  /* Free this before returning */
8669   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
8670 
8671   char *zName = 0;                /* Path (name) of new entry */
8672   int nName = 0;                  /* Size of zName in bytes */
8673   char *zFree = 0;                /* Free this before returning */
8674   int nByte;
8675 
8676   memset(&e, 0, sizeof(e));
8677   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8678   if( p==0 ) return;
8679 
8680   /* Martial the arguments into stack variables */
8681   if( nVal!=2 && nVal!=4 && nVal!=5 ){
8682     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
8683     rc = SQLITE_ERROR;
8684     goto zipfile_step_out;
8685   }
8686   pName = apVal[0];
8687   if( nVal==2 ){
8688     pData = apVal[1];
8689   }else{
8690     pMode = apVal[1];
8691     pMtime = apVal[2];
8692     pData = apVal[3];
8693     if( nVal==5 ){
8694       pMethod = apVal[4];
8695     }
8696   }
8697 
8698   /* Check that the 'name' parameter looks ok. */
8699   zName = (char*)sqlite3_value_text(pName);
8700   nName = sqlite3_value_bytes(pName);
8701   if( zName==0 ){
8702     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
8703     rc = SQLITE_ERROR;
8704     goto zipfile_step_out;
8705   }
8706 
8707   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
8708   ** deflate compression) or NULL (choose automatically).  */
8709   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
8710     iMethod = (int)sqlite3_value_int64(pMethod);
8711     if( iMethod!=0 && iMethod!=8 ){
8712       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
8713       rc = SQLITE_ERROR;
8714       goto zipfile_step_out;
8715     }
8716   }
8717 
8718   /* Now inspect the data. If this is NULL, then the new entry must be a
8719   ** directory.  Otherwise, figure out whether or not the data should
8720   ** be deflated or simply stored in the zip archive. */
8721   if( sqlite3_value_type(pData)==SQLITE_NULL ){
8722     bIsDir = 1;
8723     iMethod = 0;
8724   }else{
8725     aData = sqlite3_value_blob(pData);
8726     szUncompressed = nData = sqlite3_value_bytes(pData);
8727     iCrc32 = crc32(0, aData, nData);
8728     if( iMethod<0 || iMethod==8 ){
8729       int nOut = 0;
8730       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
8731       if( rc!=SQLITE_OK ){
8732         goto zipfile_step_out;
8733       }
8734       if( iMethod==8 || nOut<nData ){
8735         aData = aFree;
8736         nData = nOut;
8737         iMethod = 8;
8738       }else{
8739         iMethod = 0;
8740       }
8741     }
8742   }
8743 
8744   /* Decode the "mode" argument. */
8745   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
8746   if( rc ) goto zipfile_step_out;
8747 
8748   /* Decode the "mtime" argument. */
8749   e.mUnixTime = zipfileGetTime(pMtime);
8750 
8751   /* If this is a directory entry, ensure that there is exactly one '/'
8752   ** at the end of the path. Or, if this is not a directory and the path
8753   ** ends in '/' it is an error. */
8754   if( bIsDir==0 ){
8755     if( nName>0 && zName[nName-1]=='/' ){
8756       zErr = sqlite3_mprintf("non-directory name must not end with /");
8757       rc = SQLITE_ERROR;
8758       goto zipfile_step_out;
8759     }
8760   }else{
8761     if( nName==0 || zName[nName-1]!='/' ){
8762       zName = zFree = sqlite3_mprintf("%s/", zName);
8763       if( zName==0 ){
8764         rc = SQLITE_NOMEM;
8765         goto zipfile_step_out;
8766       }
8767       nName = (int)strlen(zName);
8768     }else{
8769       while( nName>1 && zName[nName-2]=='/' ) nName--;
8770     }
8771   }
8772 
8773   /* Assemble the ZipfileEntry object for the new zip archive entry */
8774   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8775   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8776   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8777   e.cds.iCompression = (u16)iMethod;
8778   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
8779   e.cds.crc32 = iCrc32;
8780   e.cds.szCompressed = nData;
8781   e.cds.szUncompressed = szUncompressed;
8782   e.cds.iExternalAttr = (mode<<16);
8783   e.cds.iOffset = p->body.n;
8784   e.cds.nFile = (u16)nName;
8785   e.cds.zFile = zName;
8786 
8787   /* Append the LFH to the body of the new archive */
8788   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
8789   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
8790   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
8791 
8792   /* Append the data to the body of the new archive */
8793   if( nData>0 ){
8794     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
8795     memcpy(&p->body.a[p->body.n], aData, nData);
8796     p->body.n += nData;
8797   }
8798 
8799   /* Append the CDS record to the directory of the new archive */
8800   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
8801   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
8802   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
8803 
8804   /* Increment the count of entries in the archive */
8805   p->nEntry++;
8806 
8807  zipfile_step_out:
8808   sqlite3_free(aFree);
8809   sqlite3_free(zFree);
8810   if( rc ){
8811     if( zErr ){
8812       sqlite3_result_error(pCtx, zErr, -1);
8813     }else{
8814       sqlite3_result_error_code(pCtx, rc);
8815     }
8816   }
8817   sqlite3_free(zErr);
8818 }
8819 
8820 /*
8821 ** xFinalize() callback for zipfile aggregate function.
8822 */
8823 static void zipfileFinal(sqlite3_context *pCtx){
8824   ZipfileCtx *p;
8825   ZipfileEOCD eocd;
8826   sqlite3_int64 nZip;
8827   u8 *aZip;
8828 
8829   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8830   if( p==0 ) return;
8831   if( p->nEntry>0 ){
8832     memset(&eocd, 0, sizeof(eocd));
8833     eocd.nEntry = (u16)p->nEntry;
8834     eocd.nEntryTotal = (u16)p->nEntry;
8835     eocd.nSize = p->cds.n;
8836     eocd.iOffset = p->body.n;
8837 
8838     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8839     aZip = (u8*)sqlite3_malloc64(nZip);
8840     if( aZip==0 ){
8841       sqlite3_result_error_nomem(pCtx);
8842     }else{
8843       memcpy(aZip, p->body.a, p->body.n);
8844       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8845       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8846       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8847     }
8848   }
8849 
8850   sqlite3_free(p->body.a);
8851   sqlite3_free(p->cds.a);
8852 }
8853 
8854 
8855 /*
8856 ** Register the "zipfile" virtual table.
8857 */
8858 static int zipfileRegister(sqlite3 *db){
8859   static sqlite3_module zipfileModule = {
8860     1,                         /* iVersion */
8861     zipfileConnect,            /* xCreate */
8862     zipfileConnect,            /* xConnect */
8863     zipfileBestIndex,          /* xBestIndex */
8864     zipfileDisconnect,         /* xDisconnect */
8865     zipfileDisconnect,         /* xDestroy */
8866     zipfileOpen,               /* xOpen - open a cursor */
8867     zipfileClose,              /* xClose - close a cursor */
8868     zipfileFilter,             /* xFilter - configure scan constraints */
8869     zipfileNext,               /* xNext - advance a cursor */
8870     zipfileEof,                /* xEof - check for end of scan */
8871     zipfileColumn,             /* xColumn - read data */
8872     0,                         /* xRowid - read data */
8873     zipfileUpdate,             /* xUpdate */
8874     zipfileBegin,              /* xBegin */
8875     0,                         /* xSync */
8876     zipfileCommit,             /* xCommit */
8877     zipfileRollback,           /* xRollback */
8878     zipfileFindFunction,       /* xFindMethod */
8879     0,                         /* xRename */
8880   };
8881 
8882   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
8883   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8884   if( rc==SQLITE_OK ){
8885     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
8886         zipfileStep, zipfileFinal
8887     );
8888   }
8889   assert( sizeof(i64)==8 );
8890   assert( sizeof(u32)==4 );
8891   assert( sizeof(u16)==2 );
8892   assert( sizeof(u8)==1 );
8893   return rc;
8894 }
8895 #else         /* SQLITE_OMIT_VIRTUALTABLE */
8896 # define zipfileRegister(x) SQLITE_OK
8897 #endif
8898 
8899 #ifdef _WIN32
8900 
8901 #endif
8902 int sqlite3_zipfile_init(
8903   sqlite3 *db,
8904   char **pzErrMsg,
8905   const sqlite3_api_routines *pApi
8906 ){
8907   SQLITE_EXTENSION_INIT2(pApi);
8908   (void)pzErrMsg;  /* Unused parameter */
8909   return zipfileRegister(db);
8910 }
8911 
8912 /************************* End ../ext/misc/zipfile.c ********************/
8913 /************************* Begin ../ext/misc/sqlar.c ******************/
8914 /*
8915 ** 2017-12-17
8916 **
8917 ** The author disclaims copyright to this source code.  In place of
8918 ** a legal notice, here is a blessing:
8919 **
8920 **    May you do good and not evil.
8921 **    May you find forgiveness for yourself and forgive others.
8922 **    May you share freely, never taking more than you give.
8923 **
8924 ******************************************************************************
8925 **
8926 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8927 ** for working with sqlar archives and used by the shell tool's built-in
8928 ** sqlar support.
8929 */
8930 /* #include "sqlite3ext.h" */
8931 SQLITE_EXTENSION_INIT1
8932 #include <zlib.h>
8933 #include <assert.h>
8934 
8935 /*
8936 ** Implementation of the "sqlar_compress(X)" SQL function.
8937 **
8938 ** If the type of X is SQLITE_BLOB, and compressing that blob using
8939 ** zlib utility function compress() yields a smaller blob, return the
8940 ** compressed blob. Otherwise, return a copy of X.
8941 **
8942 ** SQLar uses the "zlib format" for compressed content.  The zlib format
8943 ** contains a two-byte identification header and a four-byte checksum at
8944 ** the end.  This is different from ZIP which uses the raw deflate format.
8945 **
8946 ** Future enhancements to SQLar might add support for new compression formats.
8947 ** If so, those new formats will be identified by alternative headers in the
8948 ** compressed data.
8949 */
8950 static void sqlarCompressFunc(
8951   sqlite3_context *context,
8952   int argc,
8953   sqlite3_value **argv
8954 ){
8955   assert( argc==1 );
8956   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8957     const Bytef *pData = sqlite3_value_blob(argv[0]);
8958     uLong nData = sqlite3_value_bytes(argv[0]);
8959     uLongf nOut = compressBound(nData);
8960     Bytef *pOut;
8961 
8962     pOut = (Bytef*)sqlite3_malloc(nOut);
8963     if( pOut==0 ){
8964       sqlite3_result_error_nomem(context);
8965       return;
8966     }else{
8967       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8968         sqlite3_result_error(context, "error in compress()", -1);
8969       }else if( nOut<nData ){
8970         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8971       }else{
8972         sqlite3_result_value(context, argv[0]);
8973       }
8974       sqlite3_free(pOut);
8975     }
8976   }else{
8977     sqlite3_result_value(context, argv[0]);
8978   }
8979 }
8980 
8981 /*
8982 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8983 **
8984 ** Parameter SZ is interpreted as an integer. If it is less than or
8985 ** equal to zero, then this function returns a copy of X. Or, if
8986 ** SZ is equal to the size of X when interpreted as a blob, also
8987 ** return a copy of X. Otherwise, decompress blob X using zlib
8988 ** utility function uncompress() and return the results (another
8989 ** blob).
8990 */
8991 static void sqlarUncompressFunc(
8992   sqlite3_context *context,
8993   int argc,
8994   sqlite3_value **argv
8995 ){
8996   uLong nData;
8997   uLongf sz;
8998 
8999   assert( argc==2 );
9000   sz = sqlite3_value_int(argv[1]);
9001 
9002   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
9003     sqlite3_result_value(context, argv[0]);
9004   }else{
9005     const Bytef *pData= sqlite3_value_blob(argv[0]);
9006     Bytef *pOut = sqlite3_malloc(sz);
9007     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
9008       sqlite3_result_error(context, "error in uncompress()", -1);
9009     }else{
9010       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
9011     }
9012     sqlite3_free(pOut);
9013   }
9014 }
9015 
9016 
9017 #ifdef _WIN32
9018 
9019 #endif
9020 int sqlite3_sqlar_init(
9021   sqlite3 *db,
9022   char **pzErrMsg,
9023   const sqlite3_api_routines *pApi
9024 ){
9025   int rc = SQLITE_OK;
9026   SQLITE_EXTENSION_INIT2(pApi);
9027   (void)pzErrMsg;  /* Unused parameter */
9028   rc = sqlite3_create_function(db, "sqlar_compress", 1,
9029                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9030                                sqlarCompressFunc, 0, 0);
9031   if( rc==SQLITE_OK ){
9032     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
9033                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9034                                  sqlarUncompressFunc, 0, 0);
9035   }
9036   return rc;
9037 }
9038 
9039 /************************* End ../ext/misc/sqlar.c ********************/
9040 #endif
9041 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
9042 /*
9043 ** 2017 April 07
9044 **
9045 ** The author disclaims copyright to this source code.  In place of
9046 ** a legal notice, here is a blessing:
9047 **
9048 **    May you do good and not evil.
9049 **    May you find forgiveness for yourself and forgive others.
9050 **    May you share freely, never taking more than you give.
9051 **
9052 *************************************************************************
9053 */
9054 #if !defined(SQLITEEXPERT_H)
9055 #define SQLITEEXPERT_H 1
9056 /* #include "sqlite3.h" */
9057 
9058 typedef struct sqlite3expert sqlite3expert;
9059 
9060 /*
9061 ** Create a new sqlite3expert object.
9062 **
9063 ** If successful, a pointer to the new object is returned and (*pzErr) set
9064 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
9065 ** an English-language error message. In this case it is the responsibility
9066 ** of the caller to eventually free the error message buffer using
9067 ** sqlite3_free().
9068 */
9069 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9070 
9071 /*
9072 ** Configure an sqlite3expert object.
9073 **
9074 ** EXPERT_CONFIG_SAMPLE:
9075 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
9076 **   each candidate index. This involves scanning and sorting the entire
9077 **   contents of each user database table once for each candidate index
9078 **   associated with the table. For large databases, this can be
9079 **   prohibitively slow. This option allows the sqlite3expert object to
9080 **   be configured so that sqlite_stat1 data is instead generated based on a
9081 **   subset of each table, or so that no sqlite_stat1 data is used at all.
9082 **
9083 **   A single integer argument is passed to this option. If the value is less
9084 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
9085 **   the analysis - indexes are recommended based on the database schema only.
9086 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
9087 **   generated for each candidate index (this is the default). Finally, if the
9088 **   value falls between 0 and 100, then it represents the percentage of user
9089 **   table rows that should be considered when generating sqlite_stat1 data.
9090 **
9091 **   Examples:
9092 **
9093 **     // Do not generate any sqlite_stat1 data
9094 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
9095 **
9096 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
9097 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
9098 */
9099 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
9100 
9101 #define EXPERT_CONFIG_SAMPLE 1    /* int */
9102 
9103 /*
9104 ** Specify zero or more SQL statements to be included in the analysis.
9105 **
9106 ** Buffer zSql must contain zero or more complete SQL statements. This
9107 ** function parses all statements contained in the buffer and adds them
9108 ** to the internal list of statements to analyze. If successful, SQLITE_OK
9109 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9110 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9111 ** may be set to point to an English language error message. In this case
9112 ** the caller is responsible for eventually freeing the error message buffer
9113 ** using sqlite3_free().
9114 **
9115 ** If an error does occur while processing one of the statements in the
9116 ** buffer passed as the second argument, none of the statements in the
9117 ** buffer are added to the analysis.
9118 **
9119 ** This function must be called before sqlite3_expert_analyze(). If a call
9120 ** to this function is made on an sqlite3expert object that has already
9121 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
9122 ** immediately and no statements are added to the analysis.
9123 */
9124 int sqlite3_expert_sql(
9125   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
9126   const char *zSql,               /* SQL statement(s) to add */
9127   char **pzErr                    /* OUT: Error message (if any) */
9128 );
9129 
9130 
9131 /*
9132 ** This function is called after the sqlite3expert object has been configured
9133 ** with all SQL statements using sqlite3_expert_sql() to actually perform
9134 ** the analysis. Once this function has been called, it is not possible to
9135 ** add further SQL statements to the analysis.
9136 **
9137 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
9138 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
9139 ** point to a buffer containing an English language error message. In this
9140 ** case it is the responsibility of the caller to eventually free the buffer
9141 ** using sqlite3_free().
9142 **
9143 ** If an error does occur within this function, the sqlite3expert object
9144 ** is no longer useful for any purpose. At that point it is no longer
9145 ** possible to add further SQL statements to the object or to re-attempt
9146 ** the analysis. The sqlite3expert object must still be freed using a call
9147 ** sqlite3_expert_destroy().
9148 */
9149 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
9150 
9151 /*
9152 ** Return the total number of statements loaded using sqlite3_expert_sql().
9153 ** The total number of SQL statements may be different from the total number
9154 ** to calls to sqlite3_expert_sql().
9155 */
9156 int sqlite3_expert_count(sqlite3expert*);
9157 
9158 /*
9159 ** Return a component of the report.
9160 **
9161 ** This function is called after sqlite3_expert_analyze() to extract the
9162 ** results of the analysis. Each call to this function returns either a
9163 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9164 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
9165 ** #define constants defined below.
9166 **
9167 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
9168 ** information relating to a specific SQL statement. In these cases that
9169 ** SQL statement is identified by the value passed as the second argument.
9170 ** SQL statements are numbered from 0 in the order in which they are parsed.
9171 ** If an out-of-range value (less than zero or equal to or greater than the
9172 ** value returned by sqlite3_expert_count()) is passed as the second argument
9173 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
9174 **
9175 ** EXPERT_REPORT_SQL:
9176 **   Return the text of SQL statement iStmt.
9177 **
9178 ** EXPERT_REPORT_INDEXES:
9179 **   Return a buffer containing the CREATE INDEX statements for all recommended
9180 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL
9181 **   is returned.
9182 **
9183 ** EXPERT_REPORT_PLAN:
9184 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
9185 **   iStmt after the proposed indexes have been added to the database schema.
9186 **
9187 ** EXPERT_REPORT_CANDIDATES:
9188 **   Return a pointer to a buffer containing the CREATE INDEX statements
9189 **   for all indexes that were tested (for all SQL statements). The iStmt
9190 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
9191 */
9192 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
9193 
9194 /*
9195 ** Values for the third argument passed to sqlite3_expert_report().
9196 */
9197 #define EXPERT_REPORT_SQL        1
9198 #define EXPERT_REPORT_INDEXES    2
9199 #define EXPERT_REPORT_PLAN       3
9200 #define EXPERT_REPORT_CANDIDATES 4
9201 
9202 /*
9203 ** Free an (sqlite3expert*) handle and all associated resources. There
9204 ** should be one call to this function for each successful call to
9205 ** sqlite3-expert_new().
9206 */
9207 void sqlite3_expert_destroy(sqlite3expert*);
9208 
9209 #endif  /* !defined(SQLITEEXPERT_H) */
9210 
9211 /************************* End ../ext/expert/sqlite3expert.h ********************/
9212 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
9213 /*
9214 ** 2017 April 09
9215 **
9216 ** The author disclaims copyright to this source code.  In place of
9217 ** a legal notice, here is a blessing:
9218 **
9219 **    May you do good and not evil.
9220 **    May you find forgiveness for yourself and forgive others.
9221 **    May you share freely, never taking more than you give.
9222 **
9223 *************************************************************************
9224 */
9225 /* #include "sqlite3expert.h" */
9226 #include <assert.h>
9227 #include <string.h>
9228 #include <stdio.h>
9229 
9230 #if !defined(SQLITE_AMALGAMATION)
9231 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
9232 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
9233 #endif
9234 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
9235 # define ALWAYS(X)      (1)
9236 # define NEVER(X)       (0)
9237 #elif !defined(NDEBUG)
9238 # define ALWAYS(X)      ((X)?1:(assert(0),0))
9239 # define NEVER(X)       ((X)?(assert(0),1):0)
9240 #else
9241 # define ALWAYS(X)      (X)
9242 # define NEVER(X)       (X)
9243 #endif
9244 #endif /* !defined(SQLITE_AMALGAMATION) */
9245 
9246 
9247 #ifndef SQLITE_OMIT_VIRTUALTABLE
9248 
9249 /* typedef sqlite3_int64 i64; */
9250 /* typedef sqlite3_uint64 u64; */
9251 
9252 typedef struct IdxColumn IdxColumn;
9253 typedef struct IdxConstraint IdxConstraint;
9254 typedef struct IdxScan IdxScan;
9255 typedef struct IdxStatement IdxStatement;
9256 typedef struct IdxTable IdxTable;
9257 typedef struct IdxWrite IdxWrite;
9258 
9259 #define STRLEN  (int)strlen
9260 
9261 /*
9262 ** A temp table name that we assume no user database will actually use.
9263 ** If this assumption proves incorrect triggers on the table with the
9264 ** conflicting name will be ignored.
9265 */
9266 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
9267 
9268 /*
9269 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
9270 ** any other type of single-ended range constraint on a column).
9271 **
9272 ** pLink:
9273 **   Used to temporarily link IdxConstraint objects into lists while
9274 **   creating candidate indexes.
9275 */
9276 struct IdxConstraint {
9277   char *zColl;                    /* Collation sequence */
9278   int bRange;                     /* True for range, false for eq */
9279   int iCol;                       /* Constrained table column */
9280   int bFlag;                      /* Used by idxFindCompatible() */
9281   int bDesc;                      /* True if ORDER BY <expr> DESC */
9282   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
9283   IdxConstraint *pLink;           /* See above */
9284 };
9285 
9286 /*
9287 ** A single scan of a single table.
9288 */
9289 struct IdxScan {
9290   IdxTable *pTab;                 /* Associated table object */
9291   int iDb;                        /* Database containing table zTable */
9292   i64 covering;                   /* Mask of columns required for cov. index */
9293   IdxConstraint *pOrder;          /* ORDER BY columns */
9294   IdxConstraint *pEq;             /* List of == constraints */
9295   IdxConstraint *pRange;          /* List of < constraints */
9296   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
9297 };
9298 
9299 /*
9300 ** Information regarding a single database table. Extracted from
9301 ** "PRAGMA table_info" by function idxGetTableInfo().
9302 */
9303 struct IdxColumn {
9304   char *zName;
9305   char *zColl;
9306   int iPk;
9307 };
9308 struct IdxTable {
9309   int nCol;
9310   char *zName;                    /* Table name */
9311   IdxColumn *aCol;
9312   IdxTable *pNext;                /* Next table in linked list of all tables */
9313 };
9314 
9315 /*
9316 ** An object of the following type is created for each unique table/write-op
9317 ** seen. The objects are stored in a singly-linked list beginning at
9318 ** sqlite3expert.pWrite.
9319 */
9320 struct IdxWrite {
9321   IdxTable *pTab;
9322   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
9323   IdxWrite *pNext;
9324 };
9325 
9326 /*
9327 ** Each statement being analyzed is represented by an instance of this
9328 ** structure.
9329 */
9330 struct IdxStatement {
9331   int iId;                        /* Statement number */
9332   char *zSql;                     /* SQL statement */
9333   char *zIdx;                     /* Indexes */
9334   char *zEQP;                     /* Plan */
9335   IdxStatement *pNext;
9336 };
9337 
9338 
9339 /*
9340 ** A hash table for storing strings. With space for a payload string
9341 ** with each entry. Methods are:
9342 **
9343 **   idxHashInit()
9344 **   idxHashClear()
9345 **   idxHashAdd()
9346 **   idxHashSearch()
9347 */
9348 #define IDX_HASH_SIZE 1023
9349 typedef struct IdxHashEntry IdxHashEntry;
9350 typedef struct IdxHash IdxHash;
9351 struct IdxHashEntry {
9352   char *zKey;                     /* nul-terminated key */
9353   char *zVal;                     /* nul-terminated value string */
9354   char *zVal2;                    /* nul-terminated value string 2 */
9355   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
9356   IdxHashEntry *pNext;            /* Next entry in hash */
9357 };
9358 struct IdxHash {
9359   IdxHashEntry *pFirst;
9360   IdxHashEntry *aHash[IDX_HASH_SIZE];
9361 };
9362 
9363 /*
9364 ** sqlite3expert object.
9365 */
9366 struct sqlite3expert {
9367   int iSample;                    /* Percentage of tables to sample for stat1 */
9368   sqlite3 *db;                    /* User database */
9369   sqlite3 *dbm;                   /* In-memory db for this analysis */
9370   sqlite3 *dbv;                   /* Vtab schema for this analysis */
9371   IdxTable *pTable;               /* List of all IdxTable objects */
9372   IdxScan *pScan;                 /* List of scan objects */
9373   IdxWrite *pWrite;               /* List of write objects */
9374   IdxStatement *pStatement;       /* List of IdxStatement objects */
9375   int bRun;                       /* True once analysis has run */
9376   char **pzErrmsg;
9377   int rc;                         /* Error code from whereinfo hook */
9378   IdxHash hIdx;                   /* Hash containing all candidate indexes */
9379   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
9380 };
9381 
9382 
9383 /*
9384 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
9385 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
9386 */
9387 static void *idxMalloc(int *pRc, int nByte){
9388   void *pRet;
9389   assert( *pRc==SQLITE_OK );
9390   assert( nByte>0 );
9391   pRet = sqlite3_malloc(nByte);
9392   if( pRet ){
9393     memset(pRet, 0, nByte);
9394   }else{
9395     *pRc = SQLITE_NOMEM;
9396   }
9397   return pRet;
9398 }
9399 
9400 /*
9401 ** Initialize an IdxHash hash table.
9402 */
9403 static void idxHashInit(IdxHash *pHash){
9404   memset(pHash, 0, sizeof(IdxHash));
9405 }
9406 
9407 /*
9408 ** Reset an IdxHash hash table.
9409 */
9410 static void idxHashClear(IdxHash *pHash){
9411   int i;
9412   for(i=0; i<IDX_HASH_SIZE; i++){
9413     IdxHashEntry *pEntry;
9414     IdxHashEntry *pNext;
9415     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
9416       pNext = pEntry->pHashNext;
9417       sqlite3_free(pEntry->zVal2);
9418       sqlite3_free(pEntry);
9419     }
9420   }
9421   memset(pHash, 0, sizeof(IdxHash));
9422 }
9423 
9424 /*
9425 ** Return the index of the hash bucket that the string specified by the
9426 ** arguments to this function belongs.
9427 */
9428 static int idxHashString(const char *z, int n){
9429   unsigned int ret = 0;
9430   int i;
9431   for(i=0; i<n; i++){
9432     ret += (ret<<3) + (unsigned char)(z[i]);
9433   }
9434   return (int)(ret % IDX_HASH_SIZE);
9435 }
9436 
9437 /*
9438 ** If zKey is already present in the hash table, return non-zero and do
9439 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
9440 ** the hash table passed as the second argument.
9441 */
9442 static int idxHashAdd(
9443   int *pRc,
9444   IdxHash *pHash,
9445   const char *zKey,
9446   const char *zVal
9447 ){
9448   int nKey = STRLEN(zKey);
9449   int iHash = idxHashString(zKey, nKey);
9450   int nVal = (zVal ? STRLEN(zVal) : 0);
9451   IdxHashEntry *pEntry;
9452   assert( iHash>=0 );
9453   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9454     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9455       return 1;
9456     }
9457   }
9458   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
9459   if( pEntry ){
9460     pEntry->zKey = (char*)&pEntry[1];
9461     memcpy(pEntry->zKey, zKey, nKey);
9462     if( zVal ){
9463       pEntry->zVal = &pEntry->zKey[nKey+1];
9464       memcpy(pEntry->zVal, zVal, nVal);
9465     }
9466     pEntry->pHashNext = pHash->aHash[iHash];
9467     pHash->aHash[iHash] = pEntry;
9468 
9469     pEntry->pNext = pHash->pFirst;
9470     pHash->pFirst = pEntry;
9471   }
9472   return 0;
9473 }
9474 
9475 /*
9476 ** If zKey/nKey is present in the hash table, return a pointer to the
9477 ** hash-entry object.
9478 */
9479 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
9480   int iHash;
9481   IdxHashEntry *pEntry;
9482   if( nKey<0 ) nKey = STRLEN(zKey);
9483   iHash = idxHashString(zKey, nKey);
9484   assert( iHash>=0 );
9485   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9486     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9487       return pEntry;
9488     }
9489   }
9490   return 0;
9491 }
9492 
9493 /*
9494 ** If the hash table contains an entry with a key equal to the string
9495 ** passed as the final two arguments to this function, return a pointer
9496 ** to the payload string. Otherwise, if zKey/nKey is not present in the
9497 ** hash table, return NULL.
9498 */
9499 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
9500   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
9501   if( pEntry ) return pEntry->zVal;
9502   return 0;
9503 }
9504 
9505 /*
9506 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
9507 ** variable to point to a copy of nul-terminated string zColl.
9508 */
9509 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
9510   IdxConstraint *pNew;
9511   int nColl = STRLEN(zColl);
9512 
9513   assert( *pRc==SQLITE_OK );
9514   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
9515   if( pNew ){
9516     pNew->zColl = (char*)&pNew[1];
9517     memcpy(pNew->zColl, zColl, nColl+1);
9518   }
9519   return pNew;
9520 }
9521 
9522 /*
9523 ** An error associated with database handle db has just occurred. Pass
9524 ** the error message to callback function xOut.
9525 */
9526 static void idxDatabaseError(
9527   sqlite3 *db,                    /* Database handle */
9528   char **pzErrmsg                 /* Write error here */
9529 ){
9530   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
9531 }
9532 
9533 /*
9534 ** Prepare an SQL statement.
9535 */
9536 static int idxPrepareStmt(
9537   sqlite3 *db,                    /* Database handle to compile against */
9538   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9539   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9540   const char *zSql                /* SQL statement to compile */
9541 ){
9542   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
9543   if( rc!=SQLITE_OK ){
9544     *ppStmt = 0;
9545     idxDatabaseError(db, pzErrmsg);
9546   }
9547   return rc;
9548 }
9549 
9550 /*
9551 ** Prepare an SQL statement using the results of a printf() formatting.
9552 */
9553 static int idxPrintfPrepareStmt(
9554   sqlite3 *db,                    /* Database handle to compile against */
9555   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9556   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9557   const char *zFmt,               /* printf() format of SQL statement */
9558   ...                             /* Trailing printf() arguments */
9559 ){
9560   va_list ap;
9561   int rc;
9562   char *zSql;
9563   va_start(ap, zFmt);
9564   zSql = sqlite3_vmprintf(zFmt, ap);
9565   if( zSql==0 ){
9566     rc = SQLITE_NOMEM;
9567   }else{
9568     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
9569     sqlite3_free(zSql);
9570   }
9571   va_end(ap);
9572   return rc;
9573 }
9574 
9575 
9576 /*************************************************************************
9577 ** Beginning of virtual table implementation.
9578 */
9579 typedef struct ExpertVtab ExpertVtab;
9580 struct ExpertVtab {
9581   sqlite3_vtab base;
9582   IdxTable *pTab;
9583   sqlite3expert *pExpert;
9584 };
9585 
9586 typedef struct ExpertCsr ExpertCsr;
9587 struct ExpertCsr {
9588   sqlite3_vtab_cursor base;
9589   sqlite3_stmt *pData;
9590 };
9591 
9592 static char *expertDequote(const char *zIn){
9593   int n = STRLEN(zIn);
9594   char *zRet = sqlite3_malloc(n);
9595 
9596   assert( zIn[0]=='\'' );
9597   assert( zIn[n-1]=='\'' );
9598 
9599   if( zRet ){
9600     int iOut = 0;
9601     int iIn = 0;
9602     for(iIn=1; iIn<(n-1); iIn++){
9603       if( zIn[iIn]=='\'' ){
9604         assert( zIn[iIn+1]=='\'' );
9605         iIn++;
9606       }
9607       zRet[iOut++] = zIn[iIn];
9608     }
9609     zRet[iOut] = '\0';
9610   }
9611 
9612   return zRet;
9613 }
9614 
9615 /*
9616 ** This function is the implementation of both the xConnect and xCreate
9617 ** methods of the r-tree virtual table.
9618 **
9619 **   argv[0]   -> module name
9620 **   argv[1]   -> database name
9621 **   argv[2]   -> table name
9622 **   argv[...] -> column names...
9623 */
9624 static int expertConnect(
9625   sqlite3 *db,
9626   void *pAux,
9627   int argc, const char *const*argv,
9628   sqlite3_vtab **ppVtab,
9629   char **pzErr
9630 ){
9631   sqlite3expert *pExpert = (sqlite3expert*)pAux;
9632   ExpertVtab *p = 0;
9633   int rc;
9634 
9635   if( argc!=4 ){
9636     *pzErr = sqlite3_mprintf("internal error!");
9637     rc = SQLITE_ERROR;
9638   }else{
9639     char *zCreateTable = expertDequote(argv[3]);
9640     if( zCreateTable ){
9641       rc = sqlite3_declare_vtab(db, zCreateTable);
9642       if( rc==SQLITE_OK ){
9643         p = idxMalloc(&rc, sizeof(ExpertVtab));
9644       }
9645       if( rc==SQLITE_OK ){
9646         p->pExpert = pExpert;
9647         p->pTab = pExpert->pTable;
9648         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
9649       }
9650       sqlite3_free(zCreateTable);
9651     }else{
9652       rc = SQLITE_NOMEM;
9653     }
9654   }
9655 
9656   *ppVtab = (sqlite3_vtab*)p;
9657   return rc;
9658 }
9659 
9660 static int expertDisconnect(sqlite3_vtab *pVtab){
9661   ExpertVtab *p = (ExpertVtab*)pVtab;
9662   sqlite3_free(p);
9663   return SQLITE_OK;
9664 }
9665 
9666 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
9667   ExpertVtab *p = (ExpertVtab*)pVtab;
9668   int rc = SQLITE_OK;
9669   int n = 0;
9670   IdxScan *pScan;
9671   const int opmask =
9672     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
9673     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
9674     SQLITE_INDEX_CONSTRAINT_LE;
9675 
9676   pScan = idxMalloc(&rc, sizeof(IdxScan));
9677   if( pScan ){
9678     int i;
9679 
9680     /* Link the new scan object into the list */
9681     pScan->pTab = p->pTab;
9682     pScan->pNextScan = p->pExpert->pScan;
9683     p->pExpert->pScan = pScan;
9684 
9685     /* Add the constraints to the IdxScan object */
9686     for(i=0; i<pIdxInfo->nConstraint; i++){
9687       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
9688       if( pCons->usable
9689        && pCons->iColumn>=0
9690        && p->pTab->aCol[pCons->iColumn].iPk==0
9691        && (pCons->op & opmask)
9692       ){
9693         IdxConstraint *pNew;
9694         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
9695         pNew = idxNewConstraint(&rc, zColl);
9696         if( pNew ){
9697           pNew->iCol = pCons->iColumn;
9698           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9699             pNew->pNext = pScan->pEq;
9700             pScan->pEq = pNew;
9701           }else{
9702             pNew->bRange = 1;
9703             pNew->pNext = pScan->pRange;
9704             pScan->pRange = pNew;
9705           }
9706         }
9707         n++;
9708         pIdxInfo->aConstraintUsage[i].argvIndex = n;
9709       }
9710     }
9711 
9712     /* Add the ORDER BY to the IdxScan object */
9713     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
9714       int iCol = pIdxInfo->aOrderBy[i].iColumn;
9715       if( iCol>=0 ){
9716         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
9717         if( pNew ){
9718           pNew->iCol = iCol;
9719           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
9720           pNew->pNext = pScan->pOrder;
9721           pNew->pLink = pScan->pOrder;
9722           pScan->pOrder = pNew;
9723           n++;
9724         }
9725       }
9726     }
9727   }
9728 
9729   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
9730   return rc;
9731 }
9732 
9733 static int expertUpdate(
9734   sqlite3_vtab *pVtab,
9735   int nData,
9736   sqlite3_value **azData,
9737   sqlite_int64 *pRowid
9738 ){
9739   (void)pVtab;
9740   (void)nData;
9741   (void)azData;
9742   (void)pRowid;
9743   return SQLITE_OK;
9744 }
9745 
9746 /*
9747 ** Virtual table module xOpen method.
9748 */
9749 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9750   int rc = SQLITE_OK;
9751   ExpertCsr *pCsr;
9752   (void)pVTab;
9753   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
9754   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
9755   return rc;
9756 }
9757 
9758 /*
9759 ** Virtual table module xClose method.
9760 */
9761 static int expertClose(sqlite3_vtab_cursor *cur){
9762   ExpertCsr *pCsr = (ExpertCsr*)cur;
9763   sqlite3_finalize(pCsr->pData);
9764   sqlite3_free(pCsr);
9765   return SQLITE_OK;
9766 }
9767 
9768 /*
9769 ** Virtual table module xEof method.
9770 **
9771 ** Return non-zero if the cursor does not currently point to a valid
9772 ** record (i.e if the scan has finished), or zero otherwise.
9773 */
9774 static int expertEof(sqlite3_vtab_cursor *cur){
9775   ExpertCsr *pCsr = (ExpertCsr*)cur;
9776   return pCsr->pData==0;
9777 }
9778 
9779 /*
9780 ** Virtual table module xNext method.
9781 */
9782 static int expertNext(sqlite3_vtab_cursor *cur){
9783   ExpertCsr *pCsr = (ExpertCsr*)cur;
9784   int rc = SQLITE_OK;
9785 
9786   assert( pCsr->pData );
9787   rc = sqlite3_step(pCsr->pData);
9788   if( rc!=SQLITE_ROW ){
9789     rc = sqlite3_finalize(pCsr->pData);
9790     pCsr->pData = 0;
9791   }else{
9792     rc = SQLITE_OK;
9793   }
9794 
9795   return rc;
9796 }
9797 
9798 /*
9799 ** Virtual table module xRowid method.
9800 */
9801 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
9802   (void)cur;
9803   *pRowid = 0;
9804   return SQLITE_OK;
9805 }
9806 
9807 /*
9808 ** Virtual table module xColumn method.
9809 */
9810 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
9811   ExpertCsr *pCsr = (ExpertCsr*)cur;
9812   sqlite3_value *pVal;
9813   pVal = sqlite3_column_value(pCsr->pData, i);
9814   if( pVal ){
9815     sqlite3_result_value(ctx, pVal);
9816   }
9817   return SQLITE_OK;
9818 }
9819 
9820 /*
9821 ** Virtual table module xFilter method.
9822 */
9823 static int expertFilter(
9824   sqlite3_vtab_cursor *cur,
9825   int idxNum, const char *idxStr,
9826   int argc, sqlite3_value **argv
9827 ){
9828   ExpertCsr *pCsr = (ExpertCsr*)cur;
9829   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
9830   sqlite3expert *pExpert = pVtab->pExpert;
9831   int rc;
9832 
9833   (void)idxNum;
9834   (void)idxStr;
9835   (void)argc;
9836   (void)argv;
9837   rc = sqlite3_finalize(pCsr->pData);
9838   pCsr->pData = 0;
9839   if( rc==SQLITE_OK ){
9840     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
9841         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
9842     );
9843   }
9844 
9845   if( rc==SQLITE_OK ){
9846     rc = expertNext(cur);
9847   }
9848   return rc;
9849 }
9850 
9851 static int idxRegisterVtab(sqlite3expert *p){
9852   static sqlite3_module expertModule = {
9853     2,                            /* iVersion */
9854     expertConnect,                /* xCreate - create a table */
9855     expertConnect,                /* xConnect - connect to an existing table */
9856     expertBestIndex,              /* xBestIndex - Determine search strategy */
9857     expertDisconnect,             /* xDisconnect - Disconnect from a table */
9858     expertDisconnect,             /* xDestroy - Drop a table */
9859     expertOpen,                   /* xOpen - open a cursor */
9860     expertClose,                  /* xClose - close a cursor */
9861     expertFilter,                 /* xFilter - configure scan constraints */
9862     expertNext,                   /* xNext - advance a cursor */
9863     expertEof,                    /* xEof */
9864     expertColumn,                 /* xColumn - read data */
9865     expertRowid,                  /* xRowid - read data */
9866     expertUpdate,                 /* xUpdate - write data */
9867     0,                            /* xBegin - begin transaction */
9868     0,                            /* xSync - sync transaction */
9869     0,                            /* xCommit - commit transaction */
9870     0,                            /* xRollback - rollback transaction */
9871     0,                            /* xFindFunction - function overloading */
9872     0,                            /* xRename - rename the table */
9873     0,                            /* xSavepoint */
9874     0,                            /* xRelease */
9875     0,                            /* xRollbackTo */
9876     0,                            /* xShadowName */
9877   };
9878 
9879   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9880 }
9881 /*
9882 ** End of virtual table implementation.
9883 *************************************************************************/
9884 /*
9885 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9886 ** is called, set it to the return value of sqlite3_finalize() before
9887 ** returning. Otherwise, discard the sqlite3_finalize() return value.
9888 */
9889 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9890   int rc = sqlite3_finalize(pStmt);
9891   if( *pRc==SQLITE_OK ) *pRc = rc;
9892 }
9893 
9894 /*
9895 ** Attempt to allocate an IdxTable structure corresponding to table zTab
9896 ** in the main database of connection db. If successful, set (*ppOut) to
9897 ** point to the new object and return SQLITE_OK. Otherwise, return an
9898 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9899 ** set to point to an error string.
9900 **
9901 ** It is the responsibility of the caller to eventually free either the
9902 ** IdxTable object or error message using sqlite3_free().
9903 */
9904 static int idxGetTableInfo(
9905   sqlite3 *db,                    /* Database connection to read details from */
9906   const char *zTab,               /* Table name */
9907   IdxTable **ppOut,               /* OUT: New object (if successful) */
9908   char **pzErrmsg                 /* OUT: Error message (if not) */
9909 ){
9910   sqlite3_stmt *p1 = 0;
9911   int nCol = 0;
9912   int nTab;
9913   int nByte;
9914   IdxTable *pNew = 0;
9915   int rc, rc2;
9916   char *pCsr = 0;
9917   int nPk = 0;
9918 
9919   *ppOut = 0;
9920   if( zTab==0 ) return SQLITE_ERROR;
9921   nTab = STRLEN(zTab);
9922   nByte = sizeof(IdxTable) + nTab + 1;
9923   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9924   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9925     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9926     const char *zColSeq = 0;
9927     if( zCol==0 ){
9928       rc = SQLITE_ERROR;
9929       break;
9930     }
9931     nByte += 1 + STRLEN(zCol);
9932     rc = sqlite3_table_column_metadata(
9933         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9934     );
9935     if( zColSeq==0 ) zColSeq = "binary";
9936     nByte += 1 + STRLEN(zColSeq);
9937     nCol++;
9938     nPk += (sqlite3_column_int(p1, 5)>0);
9939   }
9940   rc2 = sqlite3_reset(p1);
9941   if( rc==SQLITE_OK ) rc = rc2;
9942 
9943   nByte += sizeof(IdxColumn) * nCol;
9944   if( rc==SQLITE_OK ){
9945     pNew = idxMalloc(&rc, nByte);
9946   }
9947   if( rc==SQLITE_OK ){
9948     pNew->aCol = (IdxColumn*)&pNew[1];
9949     pNew->nCol = nCol;
9950     pCsr = (char*)&pNew->aCol[nCol];
9951   }
9952 
9953   nCol = 0;
9954   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9955     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9956     const char *zColSeq = 0;
9957     int nCopy;
9958     if( zCol==0 ) continue;
9959     nCopy = STRLEN(zCol) + 1;
9960     pNew->aCol[nCol].zName = pCsr;
9961     pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9962     memcpy(pCsr, zCol, nCopy);
9963     pCsr += nCopy;
9964 
9965     rc = sqlite3_table_column_metadata(
9966         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9967     );
9968     if( rc==SQLITE_OK ){
9969       if( zColSeq==0 ) zColSeq = "binary";
9970       nCopy = STRLEN(zColSeq) + 1;
9971       pNew->aCol[nCol].zColl = pCsr;
9972       memcpy(pCsr, zColSeq, nCopy);
9973       pCsr += nCopy;
9974     }
9975 
9976     nCol++;
9977   }
9978   idxFinalize(&rc, p1);
9979 
9980   if( rc!=SQLITE_OK ){
9981     sqlite3_free(pNew);
9982     pNew = 0;
9983   }else if( ALWAYS(pNew!=0) ){
9984     pNew->zName = pCsr;
9985     if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
9986   }
9987 
9988   *ppOut = pNew;
9989   return rc;
9990 }
9991 
9992 /*
9993 ** This function is a no-op if *pRc is set to anything other than
9994 ** SQLITE_OK when it is called.
9995 **
9996 ** If *pRc is initially set to SQLITE_OK, then the text specified by
9997 ** the printf() style arguments is appended to zIn and the result returned
9998 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9999 ** zIn before returning.
10000 */
10001 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
10002   va_list ap;
10003   char *zAppend = 0;
10004   char *zRet = 0;
10005   int nIn = zIn ? STRLEN(zIn) : 0;
10006   int nAppend = 0;
10007   va_start(ap, zFmt);
10008   if( *pRc==SQLITE_OK ){
10009     zAppend = sqlite3_vmprintf(zFmt, ap);
10010     if( zAppend ){
10011       nAppend = STRLEN(zAppend);
10012       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
10013     }
10014     if( zAppend && zRet ){
10015       if( nIn ) memcpy(zRet, zIn, nIn);
10016       memcpy(&zRet[nIn], zAppend, nAppend+1);
10017     }else{
10018       sqlite3_free(zRet);
10019       zRet = 0;
10020       *pRc = SQLITE_NOMEM;
10021     }
10022     sqlite3_free(zAppend);
10023     sqlite3_free(zIn);
10024   }
10025   va_end(ap);
10026   return zRet;
10027 }
10028 
10029 /*
10030 ** Return true if zId must be quoted in order to use it as an SQL
10031 ** identifier, or false otherwise.
10032 */
10033 static int idxIdentifierRequiresQuotes(const char *zId){
10034   int i;
10035   for(i=0; zId[i]; i++){
10036     if( !(zId[i]=='_')
10037      && !(zId[i]>='0' && zId[i]<='9')
10038      && !(zId[i]>='a' && zId[i]<='z')
10039      && !(zId[i]>='A' && zId[i]<='Z')
10040     ){
10041       return 1;
10042     }
10043   }
10044   return 0;
10045 }
10046 
10047 /*
10048 ** This function appends an index column definition suitable for constraint
10049 ** pCons to the string passed as zIn and returns the result.
10050 */
10051 static char *idxAppendColDefn(
10052   int *pRc,                       /* IN/OUT: Error code */
10053   char *zIn,                      /* Column defn accumulated so far */
10054   IdxTable *pTab,                 /* Table index will be created on */
10055   IdxConstraint *pCons
10056 ){
10057   char *zRet = zIn;
10058   IdxColumn *p = &pTab->aCol[pCons->iCol];
10059   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
10060 
10061   if( idxIdentifierRequiresQuotes(p->zName) ){
10062     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
10063   }else{
10064     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
10065   }
10066 
10067   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
10068     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
10069       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
10070     }else{
10071       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
10072     }
10073   }
10074 
10075   if( pCons->bDesc ){
10076     zRet = idxAppendText(pRc, zRet, " DESC");
10077   }
10078   return zRet;
10079 }
10080 
10081 /*
10082 ** Search database dbm for an index compatible with the one idxCreateFromCons()
10083 ** would create from arguments pScan, pEq and pTail. If no error occurs and
10084 ** such an index is found, return non-zero. Or, if no such index is found,
10085 ** return zero.
10086 **
10087 ** If an error occurs, set *pRc to an SQLite error code and return zero.
10088 */
10089 static int idxFindCompatible(
10090   int *pRc,                       /* OUT: Error code */
10091   sqlite3* dbm,                   /* Database to search */
10092   IdxScan *pScan,                 /* Scan for table to search for index on */
10093   IdxConstraint *pEq,             /* List of == constraints */
10094   IdxConstraint *pTail            /* List of range constraints */
10095 ){
10096   const char *zTbl = pScan->pTab->zName;
10097   sqlite3_stmt *pIdxList = 0;
10098   IdxConstraint *pIter;
10099   int nEq = 0;                    /* Number of elements in pEq */
10100   int rc;
10101 
10102   /* Count the elements in list pEq */
10103   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
10104 
10105   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
10106   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10107     int bMatch = 1;
10108     IdxConstraint *pT = pTail;
10109     sqlite3_stmt *pInfo = 0;
10110     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10111     if( zIdx==0 ) continue;
10112 
10113     /* Zero the IdxConstraint.bFlag values in the pEq list */
10114     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10115 
10116     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
10117     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
10118       int iIdx = sqlite3_column_int(pInfo, 0);
10119       int iCol = sqlite3_column_int(pInfo, 1);
10120       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
10121 
10122       if( iIdx<nEq ){
10123         for(pIter=pEq; pIter; pIter=pIter->pLink){
10124           if( pIter->bFlag ) continue;
10125           if( pIter->iCol!=iCol ) continue;
10126           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
10127           pIter->bFlag = 1;
10128           break;
10129         }
10130         if( pIter==0 ){
10131           bMatch = 0;
10132           break;
10133         }
10134       }else{
10135         if( pT ){
10136           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
10137             bMatch = 0;
10138             break;
10139           }
10140           pT = pT->pLink;
10141         }
10142       }
10143     }
10144     idxFinalize(&rc, pInfo);
10145 
10146     if( rc==SQLITE_OK && bMatch ){
10147       sqlite3_finalize(pIdxList);
10148       return 1;
10149     }
10150   }
10151   idxFinalize(&rc, pIdxList);
10152 
10153   *pRc = rc;
10154   return 0;
10155 }
10156 
10157 /* Callback for sqlite3_exec() with query with leading count(*) column.
10158  * The first argument is expected to be an int*, referent to be incremented
10159  * if that leading column is not exactly '0'.
10160  */
10161 static int countNonzeros(void* pCount, int nc,
10162                          char* azResults[], char* azColumns[]){
10163   (void)azColumns;  /* Suppress unused parameter warning */
10164   if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
10165     *((int *)pCount) += 1;
10166   }
10167   return 0;
10168 }
10169 
10170 static int idxCreateFromCons(
10171   sqlite3expert *p,
10172   IdxScan *pScan,
10173   IdxConstraint *pEq,
10174   IdxConstraint *pTail
10175 ){
10176   sqlite3 *dbm = p->dbm;
10177   int rc = SQLITE_OK;
10178   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
10179     IdxTable *pTab = pScan->pTab;
10180     char *zCols = 0;
10181     char *zIdx = 0;
10182     IdxConstraint *pCons;
10183     unsigned int h = 0;
10184     const char *zFmt;
10185 
10186     for(pCons=pEq; pCons; pCons=pCons->pLink){
10187       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10188     }
10189     for(pCons=pTail; pCons; pCons=pCons->pLink){
10190       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10191     }
10192 
10193     if( rc==SQLITE_OK ){
10194       /* Hash the list of columns to come up with a name for the index */
10195       const char *zTable = pScan->pTab->zName;
10196       int quoteTable = idxIdentifierRequiresQuotes(zTable);
10197       char *zName = 0;          /* Index name */
10198       int collisions = 0;
10199       do{
10200         int i;
10201         char *zFind;
10202         for(i=0; zCols[i]; i++){
10203           h += ((h<<3) + zCols[i]);
10204         }
10205         sqlite3_free(zName);
10206         zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
10207         if( zName==0 ) break;
10208         /* Is is unique among table, view and index names? */
10209         zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
10210           " AND type in ('index','table','view')";
10211         zFind = sqlite3_mprintf(zFmt, zName);
10212         i = 0;
10213         rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
10214         assert(rc==SQLITE_OK);
10215         sqlite3_free(zFind);
10216         if( i==0 ){
10217           collisions = 0;
10218           break;
10219         }
10220         ++collisions;
10221       }while( collisions<50 && zName!=0 );
10222       if( collisions ){
10223         /* This return means "Gave up trying to find a unique index name." */
10224         rc = SQLITE_BUSY_TIMEOUT;
10225       }else if( zName==0 ){
10226         rc = SQLITE_NOMEM;
10227       }else{
10228         if( quoteTable ){
10229           zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
10230         }else{
10231           zFmt = "CREATE INDEX %s ON %s(%s)";
10232         }
10233         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
10234         if( !zIdx ){
10235           rc = SQLITE_NOMEM;
10236         }else{
10237           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
10238           if( rc!=SQLITE_OK ){
10239             rc = SQLITE_BUSY_TIMEOUT;
10240           }else{
10241             idxHashAdd(&rc, &p->hIdx, zName, zIdx);
10242           }
10243         }
10244         sqlite3_free(zName);
10245         sqlite3_free(zIdx);
10246       }
10247     }
10248 
10249     sqlite3_free(zCols);
10250   }
10251   return rc;
10252 }
10253 
10254 /*
10255 ** Return true if list pList (linked by IdxConstraint.pLink) contains
10256 ** a constraint compatible with *p. Otherwise return false.
10257 */
10258 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
10259   IdxConstraint *pCmp;
10260   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
10261     if( p->iCol==pCmp->iCol ) return 1;
10262   }
10263   return 0;
10264 }
10265 
10266 static int idxCreateFromWhere(
10267   sqlite3expert *p,
10268   IdxScan *pScan,                 /* Create indexes for this scan */
10269   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
10270 ){
10271   IdxConstraint *p1 = 0;
10272   IdxConstraint *pCon;
10273   int rc;
10274 
10275   /* Gather up all the == constraints. */
10276   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
10277     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10278       pCon->pLink = p1;
10279       p1 = pCon;
10280     }
10281   }
10282 
10283   /* Create an index using the == constraints collected above. And the
10284   ** range constraint/ORDER BY terms passed in by the caller, if any. */
10285   rc = idxCreateFromCons(p, pScan, p1, pTail);
10286 
10287   /* If no range/ORDER BY passed by the caller, create a version of the
10288   ** index for each range constraint.  */
10289   if( pTail==0 ){
10290     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
10291       assert( pCon->pLink==0 );
10292       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10293         rc = idxCreateFromCons(p, pScan, p1, pCon);
10294       }
10295     }
10296   }
10297 
10298   return rc;
10299 }
10300 
10301 /*
10302 ** Create candidate indexes in database [dbm] based on the data in
10303 ** linked-list pScan.
10304 */
10305 static int idxCreateCandidates(sqlite3expert *p){
10306   int rc = SQLITE_OK;
10307   IdxScan *pIter;
10308 
10309   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
10310     rc = idxCreateFromWhere(p, pIter, 0);
10311     if( rc==SQLITE_OK && pIter->pOrder ){
10312       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
10313     }
10314   }
10315 
10316   return rc;
10317 }
10318 
10319 /*
10320 ** Free all elements of the linked list starting at pConstraint.
10321 */
10322 static void idxConstraintFree(IdxConstraint *pConstraint){
10323   IdxConstraint *pNext;
10324   IdxConstraint *p;
10325 
10326   for(p=pConstraint; p; p=pNext){
10327     pNext = p->pNext;
10328     sqlite3_free(p);
10329   }
10330 }
10331 
10332 /*
10333 ** Free all elements of the linked list starting from pScan up until pLast
10334 ** (pLast is not freed).
10335 */
10336 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
10337   IdxScan *p;
10338   IdxScan *pNext;
10339   for(p=pScan; p!=pLast; p=pNext){
10340     pNext = p->pNextScan;
10341     idxConstraintFree(p->pOrder);
10342     idxConstraintFree(p->pEq);
10343     idxConstraintFree(p->pRange);
10344     sqlite3_free(p);
10345   }
10346 }
10347 
10348 /*
10349 ** Free all elements of the linked list starting from pStatement up
10350 ** until pLast (pLast is not freed).
10351 */
10352 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
10353   IdxStatement *p;
10354   IdxStatement *pNext;
10355   for(p=pStatement; p!=pLast; p=pNext){
10356     pNext = p->pNext;
10357     sqlite3_free(p->zEQP);
10358     sqlite3_free(p->zIdx);
10359     sqlite3_free(p);
10360   }
10361 }
10362 
10363 /*
10364 ** Free the linked list of IdxTable objects starting at pTab.
10365 */
10366 static void idxTableFree(IdxTable *pTab){
10367   IdxTable *pIter;
10368   IdxTable *pNext;
10369   for(pIter=pTab; pIter; pIter=pNext){
10370     pNext = pIter->pNext;
10371     sqlite3_free(pIter);
10372   }
10373 }
10374 
10375 /*
10376 ** Free the linked list of IdxWrite objects starting at pTab.
10377 */
10378 static void idxWriteFree(IdxWrite *pTab){
10379   IdxWrite *pIter;
10380   IdxWrite *pNext;
10381   for(pIter=pTab; pIter; pIter=pNext){
10382     pNext = pIter->pNext;
10383     sqlite3_free(pIter);
10384   }
10385 }
10386 
10387 
10388 
10389 /*
10390 ** This function is called after candidate indexes have been created. It
10391 ** runs all the queries to see which indexes they prefer, and populates
10392 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
10393 */
10394 static int idxFindIndexes(
10395   sqlite3expert *p,
10396   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
10397 ){
10398   IdxStatement *pStmt;
10399   sqlite3 *dbm = p->dbm;
10400   int rc = SQLITE_OK;
10401 
10402   IdxHash hIdx;
10403   idxHashInit(&hIdx);
10404 
10405   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
10406     IdxHashEntry *pEntry;
10407     sqlite3_stmt *pExplain = 0;
10408     idxHashClear(&hIdx);
10409     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
10410         "EXPLAIN QUERY PLAN %s", pStmt->zSql
10411     );
10412     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
10413       /* int iId = sqlite3_column_int(pExplain, 0); */
10414       /* int iParent = sqlite3_column_int(pExplain, 1); */
10415       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
10416       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
10417       int nDetail;
10418       int i;
10419 
10420       if( !zDetail ) continue;
10421       nDetail = STRLEN(zDetail);
10422 
10423       for(i=0; i<nDetail; i++){
10424         const char *zIdx = 0;
10425         if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
10426           zIdx = &zDetail[i+13];
10427         }else if( i+22<nDetail
10428             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
10429         ){
10430           zIdx = &zDetail[i+22];
10431         }
10432         if( zIdx ){
10433           const char *zSql;
10434           int nIdx = 0;
10435           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
10436             nIdx++;
10437           }
10438           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
10439           if( zSql ){
10440             idxHashAdd(&rc, &hIdx, zSql, 0);
10441             if( rc ) goto find_indexes_out;
10442           }
10443           break;
10444         }
10445       }
10446 
10447       if( zDetail[0]!='-' ){
10448         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
10449       }
10450     }
10451 
10452     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10453       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
10454     }
10455 
10456     idxFinalize(&rc, pExplain);
10457   }
10458 
10459  find_indexes_out:
10460   idxHashClear(&hIdx);
10461   return rc;
10462 }
10463 
10464 static int idxAuthCallback(
10465   void *pCtx,
10466   int eOp,
10467   const char *z3,
10468   const char *z4,
10469   const char *zDb,
10470   const char *zTrigger
10471 ){
10472   int rc = SQLITE_OK;
10473   (void)z4;
10474   (void)zTrigger;
10475   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
10476     if( sqlite3_stricmp(zDb, "main")==0 ){
10477       sqlite3expert *p = (sqlite3expert*)pCtx;
10478       IdxTable *pTab;
10479       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
10480         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
10481       }
10482       if( pTab ){
10483         IdxWrite *pWrite;
10484         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
10485           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
10486         }
10487         if( pWrite==0 ){
10488           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
10489           if( rc==SQLITE_OK ){
10490             pWrite->pTab = pTab;
10491             pWrite->eOp = eOp;
10492             pWrite->pNext = p->pWrite;
10493             p->pWrite = pWrite;
10494           }
10495         }
10496       }
10497     }
10498   }
10499   return rc;
10500 }
10501 
10502 static int idxProcessOneTrigger(
10503   sqlite3expert *p,
10504   IdxWrite *pWrite,
10505   char **pzErr
10506 ){
10507   static const char *zInt = UNIQUE_TABLE_NAME;
10508   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
10509   IdxTable *pTab = pWrite->pTab;
10510   const char *zTab = pTab->zName;
10511   const char *zSql =
10512     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
10513     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
10514     "ORDER BY type;";
10515   sqlite3_stmt *pSelect = 0;
10516   int rc = SQLITE_OK;
10517   char *zWrite = 0;
10518 
10519   /* Create the table and its triggers in the temp schema */
10520   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10521   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10522     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10523     if( zCreate==0 ) continue;
10524     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10525   }
10526   idxFinalize(&rc, pSelect);
10527 
10528   /* Rename the table in the temp schema to zInt */
10529   if( rc==SQLITE_OK ){
10530     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
10531     if( z==0 ){
10532       rc = SQLITE_NOMEM;
10533     }else{
10534       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
10535       sqlite3_free(z);
10536     }
10537   }
10538 
10539   switch( pWrite->eOp ){
10540     case SQLITE_INSERT: {
10541       int i;
10542       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
10543       for(i=0; i<pTab->nCol; i++){
10544         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
10545       }
10546       zWrite = idxAppendText(&rc, zWrite, ")");
10547       break;
10548     }
10549     case SQLITE_UPDATE: {
10550       int i;
10551       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
10552       for(i=0; i<pTab->nCol; i++){
10553         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
10554             pTab->aCol[i].zName
10555         );
10556       }
10557       break;
10558     }
10559     default: {
10560       assert( pWrite->eOp==SQLITE_DELETE );
10561       if( rc==SQLITE_OK ){
10562         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
10563         if( zWrite==0 ) rc = SQLITE_NOMEM;
10564       }
10565     }
10566   }
10567 
10568   if( rc==SQLITE_OK ){
10569     sqlite3_stmt *pX = 0;
10570     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
10571     idxFinalize(&rc, pX);
10572     if( rc!=SQLITE_OK ){
10573       idxDatabaseError(p->dbv, pzErr);
10574     }
10575   }
10576   sqlite3_free(zWrite);
10577 
10578   if( rc==SQLITE_OK ){
10579     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
10580   }
10581 
10582   return rc;
10583 }
10584 
10585 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
10586   int rc = SQLITE_OK;
10587   IdxWrite *pEnd = 0;
10588   IdxWrite *pFirst = p->pWrite;
10589 
10590   while( rc==SQLITE_OK && pFirst!=pEnd ){
10591     IdxWrite *pIter;
10592     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
10593       rc = idxProcessOneTrigger(p, pIter, pzErr);
10594     }
10595     pEnd = pFirst;
10596     pFirst = p->pWrite;
10597   }
10598 
10599   return rc;
10600 }
10601 
10602 
10603 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
10604   int rc = idxRegisterVtab(p);
10605   sqlite3_stmt *pSchema = 0;
10606 
10607   /* For each table in the main db schema:
10608   **
10609   **   1) Add an entry to the p->pTable list, and
10610   **   2) Create the equivalent virtual table in dbv.
10611   */
10612   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
10613       "SELECT type, name, sql, 1 FROM sqlite_schema "
10614       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
10615       " UNION ALL "
10616       "SELECT type, name, sql, 2 FROM sqlite_schema "
10617       "WHERE type = 'trigger'"
10618       "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
10619       "ORDER BY 4, 1"
10620   );
10621   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10622     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10623     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10624     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10625 
10626     if( zType==0 || zName==0 ) continue;
10627     if( zType[0]=='v' || zType[1]=='r' ){
10628       if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10629     }else{
10630       IdxTable *pTab;
10631       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10632       if( rc==SQLITE_OK ){
10633         int i;
10634         char *zInner = 0;
10635         char *zOuter = 0;
10636         pTab->pNext = p->pTable;
10637         p->pTable = pTab;
10638 
10639         /* The statement the vtab will pass to sqlite3_declare_vtab() */
10640         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
10641         for(i=0; i<pTab->nCol; i++){
10642           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
10643               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
10644           );
10645         }
10646         zInner = idxAppendText(&rc, zInner, ")");
10647 
10648         /* The CVT statement to create the vtab */
10649         zOuter = idxAppendText(&rc, 0,
10650             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
10651         );
10652         if( rc==SQLITE_OK ){
10653           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
10654         }
10655         sqlite3_free(zInner);
10656         sqlite3_free(zOuter);
10657       }
10658     }
10659   }
10660   idxFinalize(&rc, pSchema);
10661   return rc;
10662 }
10663 
10664 struct IdxSampleCtx {
10665   int iTarget;
10666   double target;                  /* Target nRet/nRow value */
10667   double nRow;                    /* Number of rows seen */
10668   double nRet;                    /* Number of rows returned */
10669 };
10670 
10671 static void idxSampleFunc(
10672   sqlite3_context *pCtx,
10673   int argc,
10674   sqlite3_value **argv
10675 ){
10676   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
10677   int bRet;
10678 
10679   (void)argv;
10680   assert( argc==0 );
10681   if( p->nRow==0.0 ){
10682     bRet = 1;
10683   }else{
10684     bRet = (p->nRet / p->nRow) <= p->target;
10685     if( bRet==0 ){
10686       unsigned short rnd;
10687       sqlite3_randomness(2, (void*)&rnd);
10688       bRet = ((int)rnd % 100) <= p->iTarget;
10689     }
10690   }
10691 
10692   sqlite3_result_int(pCtx, bRet);
10693   p->nRow += 1.0;
10694   p->nRet += (double)bRet;
10695 }
10696 
10697 struct IdxRemCtx {
10698   int nSlot;
10699   struct IdxRemSlot {
10700     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
10701     i64 iVal;                     /* SQLITE_INTEGER value */
10702     double rVal;                  /* SQLITE_FLOAT value */
10703     int nByte;                    /* Bytes of space allocated at z */
10704     int n;                        /* Size of buffer z */
10705     char *z;                      /* SQLITE_TEXT/BLOB value */
10706   } aSlot[1];
10707 };
10708 
10709 /*
10710 ** Implementation of scalar function rem().
10711 */
10712 static void idxRemFunc(
10713   sqlite3_context *pCtx,
10714   int argc,
10715   sqlite3_value **argv
10716 ){
10717   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
10718   struct IdxRemSlot *pSlot;
10719   int iSlot;
10720   assert( argc==2 );
10721 
10722   iSlot = sqlite3_value_int(argv[0]);
10723   assert( iSlot<=p->nSlot );
10724   pSlot = &p->aSlot[iSlot];
10725 
10726   switch( pSlot->eType ){
10727     case SQLITE_NULL:
10728       /* no-op */
10729       break;
10730 
10731     case SQLITE_INTEGER:
10732       sqlite3_result_int64(pCtx, pSlot->iVal);
10733       break;
10734 
10735     case SQLITE_FLOAT:
10736       sqlite3_result_double(pCtx, pSlot->rVal);
10737       break;
10738 
10739     case SQLITE_BLOB:
10740       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10741       break;
10742 
10743     case SQLITE_TEXT:
10744       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10745       break;
10746   }
10747 
10748   pSlot->eType = sqlite3_value_type(argv[1]);
10749   switch( pSlot->eType ){
10750     case SQLITE_NULL:
10751       /* no-op */
10752       break;
10753 
10754     case SQLITE_INTEGER:
10755       pSlot->iVal = sqlite3_value_int64(argv[1]);
10756       break;
10757 
10758     case SQLITE_FLOAT:
10759       pSlot->rVal = sqlite3_value_double(argv[1]);
10760       break;
10761 
10762     case SQLITE_BLOB:
10763     case SQLITE_TEXT: {
10764       int nByte = sqlite3_value_bytes(argv[1]);
10765       const void *pData = 0;
10766       if( nByte>pSlot->nByte ){
10767         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10768         if( zNew==0 ){
10769           sqlite3_result_error_nomem(pCtx);
10770           return;
10771         }
10772         pSlot->nByte = nByte*2;
10773         pSlot->z = zNew;
10774       }
10775       pSlot->n = nByte;
10776       if( pSlot->eType==SQLITE_BLOB ){
10777         pData = sqlite3_value_blob(argv[1]);
10778         if( pData ) memcpy(pSlot->z, pData, nByte);
10779       }else{
10780         pData = sqlite3_value_text(argv[1]);
10781         memcpy(pSlot->z, pData, nByte);
10782       }
10783       break;
10784     }
10785   }
10786 }
10787 
10788 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
10789   int rc = SQLITE_OK;
10790   const char *zMax =
10791     "SELECT max(i.seqno) FROM "
10792     "  sqlite_schema AS s, "
10793     "  pragma_index_list(s.name) AS l, "
10794     "  pragma_index_info(l.name) AS i "
10795     "WHERE s.type = 'table'";
10796   sqlite3_stmt *pMax = 0;
10797 
10798   *pnMax = 0;
10799   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
10800   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
10801     *pnMax = sqlite3_column_int(pMax, 0) + 1;
10802   }
10803   idxFinalize(&rc, pMax);
10804 
10805   return rc;
10806 }
10807 
10808 static int idxPopulateOneStat1(
10809   sqlite3expert *p,
10810   sqlite3_stmt *pIndexXInfo,
10811   sqlite3_stmt *pWriteStat,
10812   const char *zTab,
10813   const char *zIdx,
10814   char **pzErr
10815 ){
10816   char *zCols = 0;
10817   char *zOrder = 0;
10818   char *zQuery = 0;
10819   int nCol = 0;
10820   int i;
10821   sqlite3_stmt *pQuery = 0;
10822   int *aStat = 0;
10823   int rc = SQLITE_OK;
10824 
10825   assert( p->iSample>0 );
10826 
10827   /* Formulate the query text */
10828   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
10829   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
10830     const char *zComma = zCols==0 ? "" : ", ";
10831     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
10832     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
10833     zCols = idxAppendText(&rc, zCols,
10834         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
10835     );
10836     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
10837   }
10838   sqlite3_reset(pIndexXInfo);
10839   if( rc==SQLITE_OK ){
10840     if( p->iSample==100 ){
10841       zQuery = sqlite3_mprintf(
10842           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
10843       );
10844     }else{
10845       zQuery = sqlite3_mprintf(
10846           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
10847       );
10848     }
10849   }
10850   sqlite3_free(zCols);
10851   sqlite3_free(zOrder);
10852 
10853   /* Formulate the query text */
10854   if( rc==SQLITE_OK ){
10855     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10856     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
10857   }
10858   sqlite3_free(zQuery);
10859 
10860   if( rc==SQLITE_OK ){
10861     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
10862   }
10863   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10864     IdxHashEntry *pEntry;
10865     char *zStat = 0;
10866     for(i=0; i<=nCol; i++) aStat[i] = 1;
10867     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10868       aStat[0]++;
10869       for(i=0; i<nCol; i++){
10870         if( sqlite3_column_int(pQuery, i)==0 ) break;
10871       }
10872       for(/*no-op*/; i<nCol; i++){
10873         aStat[i+1]++;
10874       }
10875     }
10876 
10877     if( rc==SQLITE_OK ){
10878       int s0 = aStat[0];
10879       zStat = sqlite3_mprintf("%d", s0);
10880       if( zStat==0 ) rc = SQLITE_NOMEM;
10881       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
10882         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
10883       }
10884     }
10885 
10886     if( rc==SQLITE_OK ){
10887       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
10888       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
10889       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
10890       sqlite3_step(pWriteStat);
10891       rc = sqlite3_reset(pWriteStat);
10892     }
10893 
10894     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
10895     if( pEntry ){
10896       assert( pEntry->zVal2==0 );
10897       pEntry->zVal2 = zStat;
10898     }else{
10899       sqlite3_free(zStat);
10900     }
10901   }
10902   sqlite3_free(aStat);
10903   idxFinalize(&rc, pQuery);
10904 
10905   return rc;
10906 }
10907 
10908 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10909   int rc;
10910   char *zSql;
10911 
10912   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10913   if( rc!=SQLITE_OK ) return rc;
10914 
10915   zSql = sqlite3_mprintf(
10916       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10917   );
10918   if( zSql==0 ) return SQLITE_NOMEM;
10919   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10920   sqlite3_free(zSql);
10921 
10922   return rc;
10923 }
10924 
10925 /*
10926 ** This function is called as part of sqlite3_expert_analyze(). Candidate
10927 ** indexes have already been created in database sqlite3expert.dbm, this
10928 ** function populates sqlite_stat1 table in the same database.
10929 **
10930 ** The stat1 data is generated by querying the
10931 */
10932 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10933   int rc = SQLITE_OK;
10934   int nMax =0;
10935   struct IdxRemCtx *pCtx = 0;
10936   struct IdxSampleCtx samplectx;
10937   int i;
10938   i64 iPrev = -100000;
10939   sqlite3_stmt *pAllIndex = 0;
10940   sqlite3_stmt *pIndexXInfo = 0;
10941   sqlite3_stmt *pWrite = 0;
10942 
10943   const char *zAllIndex =
10944     "SELECT s.rowid, s.name, l.name FROM "
10945     "  sqlite_schema AS s, "
10946     "  pragma_index_list(s.name) AS l "
10947     "WHERE s.type = 'table'";
10948   const char *zIndexXInfo =
10949     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10950   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10951 
10952   /* If iSample==0, no sqlite_stat1 data is required. */
10953   if( p->iSample==0 ) return SQLITE_OK;
10954 
10955   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10956   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10957 
10958   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10959 
10960   if( rc==SQLITE_OK ){
10961     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10962     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10963   }
10964 
10965   if( rc==SQLITE_OK ){
10966     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10967     rc = sqlite3_create_function(
10968         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10969     );
10970   }
10971   if( rc==SQLITE_OK ){
10972     rc = sqlite3_create_function(
10973         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10974     );
10975   }
10976 
10977   if( rc==SQLITE_OK ){
10978     pCtx->nSlot = nMax+1;
10979     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10980   }
10981   if( rc==SQLITE_OK ){
10982     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10983   }
10984   if( rc==SQLITE_OK ){
10985     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
10986   }
10987 
10988   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10989     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10990     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10991     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10992     if( zTab==0 || zIdx==0 ) continue;
10993     if( p->iSample<100 && iPrev!=iRowid ){
10994       samplectx.target = (double)p->iSample / 100.0;
10995       samplectx.iTarget = p->iSample;
10996       samplectx.nRow = 0.0;
10997       samplectx.nRet = 0.0;
10998       rc = idxBuildSampleTable(p, zTab);
10999       if( rc!=SQLITE_OK ) break;
11000     }
11001     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
11002     iPrev = iRowid;
11003   }
11004   if( rc==SQLITE_OK && p->iSample<100 ){
11005     rc = sqlite3_exec(p->dbv,
11006         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
11007     );
11008   }
11009 
11010   idxFinalize(&rc, pAllIndex);
11011   idxFinalize(&rc, pIndexXInfo);
11012   idxFinalize(&rc, pWrite);
11013 
11014   if( pCtx ){
11015     for(i=0; i<pCtx->nSlot; i++){
11016       sqlite3_free(pCtx->aSlot[i].z);
11017     }
11018     sqlite3_free(pCtx);
11019   }
11020 
11021   if( rc==SQLITE_OK ){
11022     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
11023   }
11024 
11025   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
11026   return rc;
11027 }
11028 
11029 /*
11030 ** Allocate a new sqlite3expert object.
11031 */
11032 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
11033   int rc = SQLITE_OK;
11034   sqlite3expert *pNew;
11035 
11036   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
11037 
11038   /* Open two in-memory databases to work with. The "vtab database" (dbv)
11039   ** will contain a virtual table corresponding to each real table in
11040   ** the user database schema, and a copy of each view. It is used to
11041   ** collect information regarding the WHERE, ORDER BY and other clauses
11042   ** of the user's query.
11043   */
11044   if( rc==SQLITE_OK ){
11045     pNew->db = db;
11046     pNew->iSample = 100;
11047     rc = sqlite3_open(":memory:", &pNew->dbv);
11048   }
11049   if( rc==SQLITE_OK ){
11050     rc = sqlite3_open(":memory:", &pNew->dbm);
11051     if( rc==SQLITE_OK ){
11052       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
11053     }
11054   }
11055 
11056 
11057   /* Copy the entire schema of database [db] into [dbm]. */
11058   if( rc==SQLITE_OK ){
11059     sqlite3_stmt *pSql = 0;
11060     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
11061         "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
11062         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
11063     );
11064     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11065       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11066       if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11067     }
11068     idxFinalize(&rc, pSql);
11069   }
11070 
11071   /* Create the vtab schema */
11072   if( rc==SQLITE_OK ){
11073     rc = idxCreateVtabSchema(pNew, pzErrmsg);
11074   }
11075 
11076   /* Register the auth callback with dbv */
11077   if( rc==SQLITE_OK ){
11078     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
11079   }
11080 
11081   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
11082   ** return the new sqlite3expert handle.  */
11083   if( rc!=SQLITE_OK ){
11084     sqlite3_expert_destroy(pNew);
11085     pNew = 0;
11086   }
11087   return pNew;
11088 }
11089 
11090 /*
11091 ** Configure an sqlite3expert object.
11092 */
11093 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
11094   int rc = SQLITE_OK;
11095   va_list ap;
11096   va_start(ap, op);
11097   switch( op ){
11098     case EXPERT_CONFIG_SAMPLE: {
11099       int iVal = va_arg(ap, int);
11100       if( iVal<0 ) iVal = 0;
11101       if( iVal>100 ) iVal = 100;
11102       p->iSample = iVal;
11103       break;
11104     }
11105     default:
11106       rc = SQLITE_NOTFOUND;
11107       break;
11108   }
11109 
11110   va_end(ap);
11111   return rc;
11112 }
11113 
11114 /*
11115 ** Add an SQL statement to the analysis.
11116 */
11117 int sqlite3_expert_sql(
11118   sqlite3expert *p,               /* From sqlite3_expert_new() */
11119   const char *zSql,               /* SQL statement to add */
11120   char **pzErr                    /* OUT: Error message (if any) */
11121 ){
11122   IdxScan *pScanOrig = p->pScan;
11123   IdxStatement *pStmtOrig = p->pStatement;
11124   int rc = SQLITE_OK;
11125   const char *zStmt = zSql;
11126 
11127   if( p->bRun ) return SQLITE_MISUSE;
11128 
11129   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
11130     sqlite3_stmt *pStmt = 0;
11131     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
11132     if( rc==SQLITE_OK ){
11133       if( pStmt ){
11134         IdxStatement *pNew;
11135         const char *z = sqlite3_sql(pStmt);
11136         int n = STRLEN(z);
11137         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
11138         if( rc==SQLITE_OK ){
11139           pNew->zSql = (char*)&pNew[1];
11140           memcpy(pNew->zSql, z, n+1);
11141           pNew->pNext = p->pStatement;
11142           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
11143           p->pStatement = pNew;
11144         }
11145         sqlite3_finalize(pStmt);
11146       }
11147     }else{
11148       idxDatabaseError(p->dbv, pzErr);
11149     }
11150   }
11151 
11152   if( rc!=SQLITE_OK ){
11153     idxScanFree(p->pScan, pScanOrig);
11154     idxStatementFree(p->pStatement, pStmtOrig);
11155     p->pScan = pScanOrig;
11156     p->pStatement = pStmtOrig;
11157   }
11158 
11159   return rc;
11160 }
11161 
11162 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
11163   int rc;
11164   IdxHashEntry *pEntry;
11165 
11166   /* Do trigger processing to collect any extra IdxScan structures */
11167   rc = idxProcessTriggers(p, pzErr);
11168 
11169   /* Create candidate indexes within the in-memory database file */
11170   if( rc==SQLITE_OK ){
11171     rc = idxCreateCandidates(p);
11172   }else if ( rc==SQLITE_BUSY_TIMEOUT ){
11173     if( pzErr )
11174       *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
11175     return rc;
11176   }
11177 
11178   /* Generate the stat1 data */
11179   if( rc==SQLITE_OK ){
11180     rc = idxPopulateStat1(p, pzErr);
11181   }
11182 
11183   /* Formulate the EXPERT_REPORT_CANDIDATES text */
11184   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
11185     p->zCandidates = idxAppendText(&rc, p->zCandidates,
11186         "%s;%s%s\n", pEntry->zVal,
11187         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
11188     );
11189   }
11190 
11191   /* Figure out which of the candidate indexes are preferred by the query
11192   ** planner and report the results to the user.  */
11193   if( rc==SQLITE_OK ){
11194     rc = idxFindIndexes(p, pzErr);
11195   }
11196 
11197   if( rc==SQLITE_OK ){
11198     p->bRun = 1;
11199   }
11200   return rc;
11201 }
11202 
11203 /*
11204 ** Return the total number of statements that have been added to this
11205 ** sqlite3expert using sqlite3_expert_sql().
11206 */
11207 int sqlite3_expert_count(sqlite3expert *p){
11208   int nRet = 0;
11209   if( p->pStatement ) nRet = p->pStatement->iId+1;
11210   return nRet;
11211 }
11212 
11213 /*
11214 ** Return a component of the report.
11215 */
11216 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
11217   const char *zRet = 0;
11218   IdxStatement *pStmt;
11219 
11220   if( p->bRun==0 ) return 0;
11221   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
11222   switch( eReport ){
11223     case EXPERT_REPORT_SQL:
11224       if( pStmt ) zRet = pStmt->zSql;
11225       break;
11226     case EXPERT_REPORT_INDEXES:
11227       if( pStmt ) zRet = pStmt->zIdx;
11228       break;
11229     case EXPERT_REPORT_PLAN:
11230       if( pStmt ) zRet = pStmt->zEQP;
11231       break;
11232     case EXPERT_REPORT_CANDIDATES:
11233       zRet = p->zCandidates;
11234       break;
11235   }
11236   return zRet;
11237 }
11238 
11239 /*
11240 ** Free an sqlite3expert object.
11241 */
11242 void sqlite3_expert_destroy(sqlite3expert *p){
11243   if( p ){
11244     sqlite3_close(p->dbm);
11245     sqlite3_close(p->dbv);
11246     idxScanFree(p->pScan, 0);
11247     idxStatementFree(p->pStatement, 0);
11248     idxTableFree(p->pTable);
11249     idxWriteFree(p->pWrite);
11250     idxHashClear(&p->hIdx);
11251     sqlite3_free(p->zCandidates);
11252     sqlite3_free(p);
11253   }
11254 }
11255 
11256 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11257 
11258 /************************* End ../ext/expert/sqlite3expert.c ********************/
11259 
11260 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11261 /************************* Begin ../ext/misc/dbdata.c ******************/
11262 /*
11263 ** 2019-04-17
11264 **
11265 ** The author disclaims copyright to this source code.  In place of
11266 ** a legal notice, here is a blessing:
11267 **
11268 **    May you do good and not evil.
11269 **    May you find forgiveness for yourself and forgive others.
11270 **    May you share freely, never taking more than you give.
11271 **
11272 ******************************************************************************
11273 **
11274 ** This file contains an implementation of two eponymous virtual tables,
11275 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
11276 ** "sqlite_dbpage" eponymous virtual table be available.
11277 **
11278 ** SQLITE_DBDATA:
11279 **   sqlite_dbdata is used to extract data directly from a database b-tree
11280 **   page and its associated overflow pages, bypassing the b-tree layer.
11281 **   The table schema is equivalent to:
11282 **
11283 **     CREATE TABLE sqlite_dbdata(
11284 **       pgno INTEGER,
11285 **       cell INTEGER,
11286 **       field INTEGER,
11287 **       value ANY,
11288 **       schema TEXT HIDDEN
11289 **     );
11290 **
11291 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
11292 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11293 **   "schema".
11294 **
11295 **   Each page of the database is inspected. If it cannot be interpreted as
11296 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
11297 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
11298 **   table contains one row for each field in the record associated with
11299 **   each cell on the page. For intkey b-trees, the key value is stored in
11300 **   field -1.
11301 **
11302 **   For example, for the database:
11303 **
11304 **     CREATE TABLE t1(a, b);     -- root page is page 2
11305 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
11306 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
11307 **
11308 **   the sqlite_dbdata table contains, as well as from entries related to
11309 **   page 1, content equivalent to:
11310 **
11311 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
11312 **         (2, 0, -1, 5     ),
11313 **         (2, 0,  0, 'v'   ),
11314 **         (2, 0,  1, 'five'),
11315 **         (2, 1, -1, 10    ),
11316 **         (2, 1,  0, 'x'   ),
11317 **         (2, 1,  1, 'ten' );
11318 **
11319 **   If database corruption is encountered, this module does not report an
11320 **   error. Instead, it attempts to extract as much data as possible and
11321 **   ignores the corruption.
11322 **
11323 ** SQLITE_DBPTR:
11324 **   The sqlite_dbptr table has the following schema:
11325 **
11326 **     CREATE TABLE sqlite_dbptr(
11327 **       pgno INTEGER,
11328 **       child INTEGER,
11329 **       schema TEXT HIDDEN
11330 **     );
11331 **
11332 **   It contains one entry for each b-tree pointer between a parent and
11333 **   child page in the database.
11334 */
11335 #if !defined(SQLITEINT_H)
11336 /* #include "sqlite3ext.h" */
11337 
11338 /* typedef unsigned char u8; */
11339 
11340 #endif
11341 SQLITE_EXTENSION_INIT1
11342 #include <string.h>
11343 #include <assert.h>
11344 
11345 #define DBDATA_PADDING_BYTES 100
11346 
11347 typedef struct DbdataTable DbdataTable;
11348 typedef struct DbdataCursor DbdataCursor;
11349 
11350 /* Cursor object */
11351 struct DbdataCursor {
11352   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
11353   sqlite3_stmt *pStmt;            /* For fetching database pages */
11354 
11355   int iPgno;                      /* Current page number */
11356   u8 *aPage;                      /* Buffer containing page */
11357   int nPage;                      /* Size of aPage[] in bytes */
11358   int nCell;                      /* Number of cells on aPage[] */
11359   int iCell;                      /* Current cell number */
11360   int bOnePage;                   /* True to stop after one page */
11361   int szDb;
11362   sqlite3_int64 iRowid;
11363 
11364   /* Only for the sqlite_dbdata table */
11365   u8 *pRec;                       /* Buffer containing current record */
11366   int nRec;                       /* Size of pRec[] in bytes */
11367   int nHdr;                       /* Size of header in bytes */
11368   int iField;                     /* Current field number */
11369   u8 *pHdrPtr;
11370   u8 *pPtr;
11371 
11372   sqlite3_int64 iIntkey;          /* Integer key value */
11373 };
11374 
11375 /* Table object */
11376 struct DbdataTable {
11377   sqlite3_vtab base;              /* Base class.  Must be first */
11378   sqlite3 *db;                    /* The database connection */
11379   sqlite3_stmt *pStmt;            /* For fetching database pages */
11380   int bPtr;                       /* True for sqlite3_dbptr table */
11381 };
11382 
11383 /* Column and schema definitions for sqlite_dbdata */
11384 #define DBDATA_COLUMN_PGNO        0
11385 #define DBDATA_COLUMN_CELL        1
11386 #define DBDATA_COLUMN_FIELD       2
11387 #define DBDATA_COLUMN_VALUE       3
11388 #define DBDATA_COLUMN_SCHEMA      4
11389 #define DBDATA_SCHEMA             \
11390       "CREATE TABLE x("           \
11391       "  pgno INTEGER,"           \
11392       "  cell INTEGER,"           \
11393       "  field INTEGER,"          \
11394       "  value ANY,"              \
11395       "  schema TEXT HIDDEN"      \
11396       ")"
11397 
11398 /* Column and schema definitions for sqlite_dbptr */
11399 #define DBPTR_COLUMN_PGNO         0
11400 #define DBPTR_COLUMN_CHILD        1
11401 #define DBPTR_COLUMN_SCHEMA       2
11402 #define DBPTR_SCHEMA              \
11403       "CREATE TABLE x("           \
11404       "  pgno INTEGER,"           \
11405       "  child INTEGER,"          \
11406       "  schema TEXT HIDDEN"      \
11407       ")"
11408 
11409 /*
11410 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
11411 ** table.
11412 */
11413 static int dbdataConnect(
11414   sqlite3 *db,
11415   void *pAux,
11416   int argc, const char *const*argv,
11417   sqlite3_vtab **ppVtab,
11418   char **pzErr
11419 ){
11420   DbdataTable *pTab = 0;
11421   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
11422 
11423   if( rc==SQLITE_OK ){
11424     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
11425     if( pTab==0 ){
11426       rc = SQLITE_NOMEM;
11427     }else{
11428       memset(pTab, 0, sizeof(DbdataTable));
11429       pTab->db = db;
11430       pTab->bPtr = (pAux!=0);
11431     }
11432   }
11433 
11434   *ppVtab = (sqlite3_vtab*)pTab;
11435   return rc;
11436 }
11437 
11438 /*
11439 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
11440 */
11441 static int dbdataDisconnect(sqlite3_vtab *pVtab){
11442   DbdataTable *pTab = (DbdataTable*)pVtab;
11443   if( pTab ){
11444     sqlite3_finalize(pTab->pStmt);
11445     sqlite3_free(pVtab);
11446   }
11447   return SQLITE_OK;
11448 }
11449 
11450 /*
11451 ** This function interprets two types of constraints:
11452 **
11453 **       schema=?
11454 **       pgno=?
11455 **
11456 ** If neither are present, idxNum is set to 0. If schema=? is present,
11457 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
11458 ** in idxNum is set.
11459 **
11460 ** If both parameters are present, schema is in position 0 and pgno in
11461 ** position 1.
11462 */
11463 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
11464   DbdataTable *pTab = (DbdataTable*)tab;
11465   int i;
11466   int iSchema = -1;
11467   int iPgno = -1;
11468   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
11469 
11470   for(i=0; i<pIdx->nConstraint; i++){
11471     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
11472     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
11473       if( p->iColumn==colSchema ){
11474         if( p->usable==0 ) return SQLITE_CONSTRAINT;
11475         iSchema = i;
11476       }
11477       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
11478         iPgno = i;
11479       }
11480     }
11481   }
11482 
11483   if( iSchema>=0 ){
11484     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
11485     pIdx->aConstraintUsage[iSchema].omit = 1;
11486   }
11487   if( iPgno>=0 ){
11488     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
11489     pIdx->aConstraintUsage[iPgno].omit = 1;
11490     pIdx->estimatedCost = 100;
11491     pIdx->estimatedRows =  50;
11492 
11493     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
11494       int iCol = pIdx->aOrderBy[0].iColumn;
11495       if( pIdx->nOrderBy==1 ){
11496         pIdx->orderByConsumed = (iCol==0 || iCol==1);
11497       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
11498         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
11499       }
11500     }
11501 
11502   }else{
11503     pIdx->estimatedCost = 100000000;
11504     pIdx->estimatedRows = 1000000000;
11505   }
11506   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
11507   return SQLITE_OK;
11508 }
11509 
11510 /*
11511 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
11512 */
11513 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
11514   DbdataCursor *pCsr;
11515 
11516   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
11517   if( pCsr==0 ){
11518     return SQLITE_NOMEM;
11519   }else{
11520     memset(pCsr, 0, sizeof(DbdataCursor));
11521     pCsr->base.pVtab = pVTab;
11522   }
11523 
11524   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
11525   return SQLITE_OK;
11526 }
11527 
11528 /*
11529 ** Restore a cursor object to the state it was in when first allocated
11530 ** by dbdataOpen().
11531 */
11532 static void dbdataResetCursor(DbdataCursor *pCsr){
11533   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
11534   if( pTab->pStmt==0 ){
11535     pTab->pStmt = pCsr->pStmt;
11536   }else{
11537     sqlite3_finalize(pCsr->pStmt);
11538   }
11539   pCsr->pStmt = 0;
11540   pCsr->iPgno = 1;
11541   pCsr->iCell = 0;
11542   pCsr->iField = 0;
11543   pCsr->bOnePage = 0;
11544   sqlite3_free(pCsr->aPage);
11545   sqlite3_free(pCsr->pRec);
11546   pCsr->pRec = 0;
11547   pCsr->aPage = 0;
11548 }
11549 
11550 /*
11551 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
11552 */
11553 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
11554   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11555   dbdataResetCursor(pCsr);
11556   sqlite3_free(pCsr);
11557   return SQLITE_OK;
11558 }
11559 
11560 /*
11561 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11562 */
11563 static unsigned int get_uint16(unsigned char *a){
11564   return (a[0]<<8)|a[1];
11565 }
11566 static unsigned int get_uint32(unsigned char *a){
11567   return ((unsigned int)a[0]<<24)
11568        | ((unsigned int)a[1]<<16)
11569        | ((unsigned int)a[2]<<8)
11570        | ((unsigned int)a[3]);
11571 }
11572 
11573 /*
11574 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11575 ** If successful, set (*ppPage) to point to a buffer containing the page
11576 ** data, (*pnPage) to the size of that buffer in bytes and return
11577 ** SQLITE_OK. In this case it is the responsibility of the caller to
11578 ** eventually free the buffer using sqlite3_free().
11579 **
11580 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11581 ** return an SQLite error code.
11582 */
11583 static int dbdataLoadPage(
11584   DbdataCursor *pCsr,             /* Cursor object */
11585   unsigned int pgno,              /* Page number of page to load */
11586   u8 **ppPage,                    /* OUT: pointer to page buffer */
11587   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
11588 ){
11589   int rc2;
11590   int rc = SQLITE_OK;
11591   sqlite3_stmt *pStmt = pCsr->pStmt;
11592 
11593   *ppPage = 0;
11594   *pnPage = 0;
11595   sqlite3_bind_int64(pStmt, 2, pgno);
11596   if( SQLITE_ROW==sqlite3_step(pStmt) ){
11597     int nCopy = sqlite3_column_bytes(pStmt, 0);
11598     if( nCopy>0 ){
11599       u8 *pPage;
11600       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11601       if( pPage==0 ){
11602         rc = SQLITE_NOMEM;
11603       }else{
11604         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11605         memcpy(pPage, pCopy, nCopy);
11606         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11607       }
11608       *ppPage = pPage;
11609       *pnPage = nCopy;
11610     }
11611   }
11612   rc2 = sqlite3_reset(pStmt);
11613   if( rc==SQLITE_OK ) rc = rc2;
11614 
11615   return rc;
11616 }
11617 
11618 /*
11619 ** Read a varint.  Put the value in *pVal and return the number of bytes.
11620 */
11621 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11622   sqlite3_int64 v = 0;
11623   int i;
11624   for(i=0; i<8; i++){
11625     v = (v<<7) + (z[i]&0x7f);
11626     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11627   }
11628   v = (v<<8) + (z[i]&0xff);
11629   *pVal = v;
11630   return 9;
11631 }
11632 
11633 /*
11634 ** Return the number of bytes of space used by an SQLite value of type
11635 ** eType.
11636 */
11637 static int dbdataValueBytes(int eType){
11638   switch( eType ){
11639     case 0: case 8: case 9:
11640     case 10: case 11:
11641       return 0;
11642     case 1:
11643       return 1;
11644     case 2:
11645       return 2;
11646     case 3:
11647       return 3;
11648     case 4:
11649       return 4;
11650     case 5:
11651       return 6;
11652     case 6:
11653     case 7:
11654       return 8;
11655     default:
11656       if( eType>0 ){
11657         return ((eType-12) / 2);
11658       }
11659       return 0;
11660   }
11661 }
11662 
11663 /*
11664 ** Load a value of type eType from buffer pData and use it to set the
11665 ** result of context object pCtx.
11666 */
11667 static void dbdataValue(
11668   sqlite3_context *pCtx,
11669   int eType,
11670   u8 *pData,
11671   int nData
11672 ){
11673   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
11674     switch( eType ){
11675       case 0:
11676       case 10:
11677       case 11:
11678         sqlite3_result_null(pCtx);
11679         break;
11680 
11681       case 8:
11682         sqlite3_result_int(pCtx, 0);
11683         break;
11684       case 9:
11685         sqlite3_result_int(pCtx, 1);
11686         break;
11687 
11688       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
11689         sqlite3_uint64 v = (signed char)pData[0];
11690         pData++;
11691         switch( eType ){
11692           case 7:
11693           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11694           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11695           case 4:  v = (v<<8) + pData[0];  pData++;
11696           case 3:  v = (v<<8) + pData[0];  pData++;
11697           case 2:  v = (v<<8) + pData[0];  pData++;
11698         }
11699 
11700         if( eType==7 ){
11701           double r;
11702           memcpy(&r, &v, sizeof(r));
11703           sqlite3_result_double(pCtx, r);
11704         }else{
11705           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
11706         }
11707         break;
11708       }
11709 
11710       default: {
11711         int n = ((eType-12) / 2);
11712         if( eType % 2 ){
11713           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11714         }else{
11715           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11716         }
11717       }
11718     }
11719   }
11720 }
11721 
11722 /*
11723 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
11724 */
11725 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
11726   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11727   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11728 
11729   pCsr->iRowid++;
11730   while( 1 ){
11731     int rc;
11732     int iOff = (pCsr->iPgno==1 ? 100 : 0);
11733     int bNextPage = 0;
11734 
11735     if( pCsr->aPage==0 ){
11736       while( 1 ){
11737         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11738         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11739         if( rc!=SQLITE_OK ) return rc;
11740         if( pCsr->aPage ) break;
11741         pCsr->iPgno++;
11742       }
11743       pCsr->iCell = pTab->bPtr ? -2 : 0;
11744       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11745     }
11746 
11747     if( pTab->bPtr ){
11748       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
11749         pCsr->iCell = pCsr->nCell;
11750       }
11751       pCsr->iCell++;
11752       if( pCsr->iCell>=pCsr->nCell ){
11753         sqlite3_free(pCsr->aPage);
11754         pCsr->aPage = 0;
11755         if( pCsr->bOnePage ) return SQLITE_OK;
11756         pCsr->iPgno++;
11757       }else{
11758         return SQLITE_OK;
11759       }
11760     }else{
11761       /* If there is no record loaded, load it now. */
11762       if( pCsr->pRec==0 ){
11763         int bHasRowid = 0;
11764         int nPointer = 0;
11765         sqlite3_int64 nPayload = 0;
11766         sqlite3_int64 nHdr = 0;
11767         int iHdr;
11768         int U, X;
11769         int nLocal;
11770 
11771         switch( pCsr->aPage[iOff] ){
11772           case 0x02:
11773             nPointer = 4;
11774             break;
11775           case 0x0a:
11776             break;
11777           case 0x0d:
11778             bHasRowid = 1;
11779             break;
11780           default:
11781             /* This is not a b-tree page with records on it. Continue. */
11782             pCsr->iCell = pCsr->nCell;
11783             break;
11784         }
11785 
11786         if( pCsr->iCell>=pCsr->nCell ){
11787           bNextPage = 1;
11788         }else{
11789 
11790           iOff += 8 + nPointer + pCsr->iCell*2;
11791           if( iOff>pCsr->nPage ){
11792             bNextPage = 1;
11793           }else{
11794             iOff = get_uint16(&pCsr->aPage[iOff]);
11795           }
11796 
11797           /* For an interior node cell, skip past the child-page number */
11798           iOff += nPointer;
11799 
11800           /* Load the "byte of payload including overflow" field */
11801           if( bNextPage || iOff>pCsr->nPage ){
11802             bNextPage = 1;
11803           }else{
11804             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11805           }
11806 
11807           /* If this is a leaf intkey cell, load the rowid */
11808           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11809             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
11810           }
11811 
11812           /* Figure out how much data to read from the local page */
11813           U = pCsr->nPage;
11814           if( bHasRowid ){
11815             X = U-35;
11816           }else{
11817             X = ((U-12)*64/255)-23;
11818           }
11819           if( nPayload<=X ){
11820             nLocal = nPayload;
11821           }else{
11822             int M, K;
11823             M = ((U-12)*32/255)-23;
11824             K = M+((nPayload-M)%(U-4));
11825             if( K<=X ){
11826               nLocal = K;
11827             }else{
11828               nLocal = M;
11829             }
11830           }
11831 
11832           if( bNextPage || nLocal+iOff>pCsr->nPage ){
11833             bNextPage = 1;
11834           }else{
11835 
11836             /* Allocate space for payload. And a bit more to catch small buffer
11837             ** overruns caused by attempting to read a varint or similar from
11838             ** near the end of a corrupt record.  */
11839             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
11840             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
11841             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
11842             pCsr->nRec = nPayload;
11843 
11844             /* Load the nLocal bytes of payload */
11845             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
11846             iOff += nLocal;
11847 
11848             /* Load content from overflow pages */
11849             if( nPayload>nLocal ){
11850               sqlite3_int64 nRem = nPayload - nLocal;
11851               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11852               while( nRem>0 ){
11853                 u8 *aOvfl = 0;
11854                 int nOvfl = 0;
11855                 int nCopy;
11856                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
11857                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
11858                 if( rc!=SQLITE_OK ) return rc;
11859                 if( aOvfl==0 ) break;
11860 
11861                 nCopy = U-4;
11862                 if( nCopy>nRem ) nCopy = nRem;
11863                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
11864                 nRem -= nCopy;
11865 
11866                 pgnoOvfl = get_uint32(aOvfl);
11867                 sqlite3_free(aOvfl);
11868               }
11869             }
11870 
11871             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
11872             pCsr->nHdr = nHdr;
11873             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
11874             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
11875             pCsr->iField = (bHasRowid ? -1 : 0);
11876           }
11877         }
11878       }else{
11879         pCsr->iField++;
11880         if( pCsr->iField>0 ){
11881           sqlite3_int64 iType;
11882           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
11883             bNextPage = 1;
11884           }else{
11885             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
11886             pCsr->pPtr += dbdataValueBytes(iType);
11887           }
11888         }
11889       }
11890 
11891       if( bNextPage ){
11892         sqlite3_free(pCsr->aPage);
11893         sqlite3_free(pCsr->pRec);
11894         pCsr->aPage = 0;
11895         pCsr->pRec = 0;
11896         if( pCsr->bOnePage ) return SQLITE_OK;
11897         pCsr->iPgno++;
11898       }else{
11899         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
11900           return SQLITE_OK;
11901         }
11902 
11903         /* Advance to the next cell. The next iteration of the loop will load
11904         ** the record and so on. */
11905         sqlite3_free(pCsr->pRec);
11906         pCsr->pRec = 0;
11907         pCsr->iCell++;
11908       }
11909     }
11910   }
11911 
11912   assert( !"can't get here" );
11913   return SQLITE_OK;
11914 }
11915 
11916 /*
11917 ** Return true if the cursor is at EOF.
11918 */
11919 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11920   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11921   return pCsr->aPage==0;
11922 }
11923 
11924 /*
11925 ** Determine the size in pages of database zSchema (where zSchema is
11926 ** "main", "temp" or the name of an attached database) and set
11927 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11928 ** an SQLite error code.
11929 */
11930 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11931   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11932   char *zSql = 0;
11933   int rc, rc2;
11934   sqlite3_stmt *pStmt = 0;
11935 
11936   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11937   if( zSql==0 ) return SQLITE_NOMEM;
11938   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11939   sqlite3_free(zSql);
11940   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11941     pCsr->szDb = sqlite3_column_int(pStmt, 0);
11942   }
11943   rc2 = sqlite3_finalize(pStmt);
11944   if( rc==SQLITE_OK ) rc = rc2;
11945   return rc;
11946 }
11947 
11948 /*
11949 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11950 */
11951 static int dbdataFilter(
11952   sqlite3_vtab_cursor *pCursor,
11953   int idxNum, const char *idxStr,
11954   int argc, sqlite3_value **argv
11955 ){
11956   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11957   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11958   int rc = SQLITE_OK;
11959   const char *zSchema = "main";
11960 
11961   dbdataResetCursor(pCsr);
11962   assert( pCsr->iPgno==1 );
11963   if( idxNum & 0x01 ){
11964     zSchema = (const char*)sqlite3_value_text(argv[0]);
11965   }
11966   if( idxNum & 0x02 ){
11967     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11968     pCsr->bOnePage = 1;
11969   }else{
11970     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11971     rc = dbdataDbsize(pCsr, zSchema);
11972   }
11973 
11974   if( rc==SQLITE_OK ){
11975     if( pTab->pStmt ){
11976       pCsr->pStmt = pTab->pStmt;
11977       pTab->pStmt = 0;
11978     }else{
11979       rc = sqlite3_prepare_v2(pTab->db,
11980           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11981           &pCsr->pStmt, 0
11982       );
11983     }
11984   }
11985   if( rc==SQLITE_OK ){
11986     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
11987   }else{
11988     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11989   }
11990   if( rc==SQLITE_OK ){
11991     rc = dbdataNext(pCursor);
11992   }
11993   return rc;
11994 }
11995 
11996 /*
11997 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
11998 */
11999 static int dbdataColumn(
12000   sqlite3_vtab_cursor *pCursor,
12001   sqlite3_context *ctx,
12002   int i
12003 ){
12004   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12005   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
12006   if( pTab->bPtr ){
12007     switch( i ){
12008       case DBPTR_COLUMN_PGNO:
12009         sqlite3_result_int64(ctx, pCsr->iPgno);
12010         break;
12011       case DBPTR_COLUMN_CHILD: {
12012         int iOff = pCsr->iPgno==1 ? 100 : 0;
12013         if( pCsr->iCell<0 ){
12014           iOff += 8;
12015         }else{
12016           iOff += 12 + pCsr->iCell*2;
12017           if( iOff>pCsr->nPage ) return SQLITE_OK;
12018           iOff = get_uint16(&pCsr->aPage[iOff]);
12019         }
12020         if( iOff<=pCsr->nPage ){
12021           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
12022         }
12023         break;
12024       }
12025     }
12026   }else{
12027     switch( i ){
12028       case DBDATA_COLUMN_PGNO:
12029         sqlite3_result_int64(ctx, pCsr->iPgno);
12030         break;
12031       case DBDATA_COLUMN_CELL:
12032         sqlite3_result_int(ctx, pCsr->iCell);
12033         break;
12034       case DBDATA_COLUMN_FIELD:
12035         sqlite3_result_int(ctx, pCsr->iField);
12036         break;
12037       case DBDATA_COLUMN_VALUE: {
12038         if( pCsr->iField<0 ){
12039           sqlite3_result_int64(ctx, pCsr->iIntkey);
12040         }else{
12041           sqlite3_int64 iType;
12042           dbdataGetVarint(pCsr->pHdrPtr, &iType);
12043           dbdataValue(
12044               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
12045           );
12046         }
12047         break;
12048       }
12049     }
12050   }
12051   return SQLITE_OK;
12052 }
12053 
12054 /*
12055 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
12056 */
12057 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
12058   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12059   *pRowid = pCsr->iRowid;
12060   return SQLITE_OK;
12061 }
12062 
12063 
12064 /*
12065 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
12066 */
12067 static int sqlite3DbdataRegister(sqlite3 *db){
12068   static sqlite3_module dbdata_module = {
12069     0,                            /* iVersion */
12070     0,                            /* xCreate */
12071     dbdataConnect,                /* xConnect */
12072     dbdataBestIndex,              /* xBestIndex */
12073     dbdataDisconnect,             /* xDisconnect */
12074     0,                            /* xDestroy */
12075     dbdataOpen,                   /* xOpen - open a cursor */
12076     dbdataClose,                  /* xClose - close a cursor */
12077     dbdataFilter,                 /* xFilter - configure scan constraints */
12078     dbdataNext,                   /* xNext - advance a cursor */
12079     dbdataEof,                    /* xEof - check for end of scan */
12080     dbdataColumn,                 /* xColumn - read data */
12081     dbdataRowid,                  /* xRowid - read data */
12082     0,                            /* xUpdate */
12083     0,                            /* xBegin */
12084     0,                            /* xSync */
12085     0,                            /* xCommit */
12086     0,                            /* xRollback */
12087     0,                            /* xFindMethod */
12088     0,                            /* xRename */
12089     0,                            /* xSavepoint */
12090     0,                            /* xRelease */
12091     0,                            /* xRollbackTo */
12092     0                             /* xShadowName */
12093   };
12094 
12095   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
12096   if( rc==SQLITE_OK ){
12097     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
12098   }
12099   return rc;
12100 }
12101 
12102 #ifdef _WIN32
12103 
12104 #endif
12105 int sqlite3_dbdata_init(
12106   sqlite3 *db,
12107   char **pzErrMsg,
12108   const sqlite3_api_routines *pApi
12109 ){
12110   SQLITE_EXTENSION_INIT2(pApi);
12111   return sqlite3DbdataRegister(db);
12112 }
12113 
12114 /************************* End ../ext/misc/dbdata.c ********************/
12115 #endif
12116 
12117 #if defined(SQLITE_ENABLE_SESSION)
12118 /*
12119 ** State information for a single open session
12120 */
12121 typedef struct OpenSession OpenSession;
12122 struct OpenSession {
12123   char *zName;             /* Symbolic name for this session */
12124   int nFilter;             /* Number of xFilter rejection GLOB patterns */
12125   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
12126   sqlite3_session *p;      /* The open session */
12127 };
12128 #endif
12129 
12130 typedef struct ExpertInfo ExpertInfo;
12131 struct ExpertInfo {
12132   sqlite3expert *pExpert;
12133   int bVerbose;
12134 };
12135 
12136 /* A single line in the EQP output */
12137 typedef struct EQPGraphRow EQPGraphRow;
12138 struct EQPGraphRow {
12139   int iEqpId;           /* ID for this row */
12140   int iParentId;        /* ID of the parent row */
12141   EQPGraphRow *pNext;   /* Next row in sequence */
12142   char zText[1];        /* Text to display for this row */
12143 };
12144 
12145 /* All EQP output is collected into an instance of the following */
12146 typedef struct EQPGraph EQPGraph;
12147 struct EQPGraph {
12148   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
12149   EQPGraphRow *pLast;   /* Last element of the pRow list */
12150   char zPrefix[100];    /* Graph prefix */
12151 };
12152 
12153 /* Parameters affecting columnar mode result display (defaulting together) */
12154 typedef struct ColModeOpts {
12155   int iWrap;            /* In columnar modes, wrap lines reaching this limit */
12156   u8 bQuote;            /* Quote results for .mode box and table */
12157   u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
12158 } ColModeOpts;
12159 #define ColModeOpts_default { 60, 0, 0 }
12160 #define ColModeOpts_default_qbox { 60, 1, 0 }
12161 
12162 /*
12163 ** State information about the database connection is contained in an
12164 ** instance of the following structure.
12165 */
12166 typedef struct ShellState ShellState;
12167 struct ShellState {
12168   sqlite3 *db;           /* The database */
12169   u8 autoExplain;        /* Automatically turn on .explain mode */
12170   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
12171   u8 autoEQPtest;        /* autoEQP is in test mode */
12172   u8 autoEQPtrace;       /* autoEQP is in trace mode */
12173   u8 scanstatsOn;        /* True to display scan stats before each finalize */
12174   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
12175   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
12176   u8 nEqpLevel;          /* Depth of the EQP output graph */
12177   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
12178   u8 bSafeMode;          /* True to prohibit unsafe operations */
12179   u8 bSafeModePersist;   /* The long-term value of bSafeMode */
12180   ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
12181   unsigned statsOn;      /* True to display memory stats before each finalize */
12182   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
12183   int inputNesting;      /* Track nesting level of .read and other redirects */
12184   int outCount;          /* Revert to stdout when reaching zero */
12185   int cnt;               /* Number of records displayed so far */
12186   int lineno;            /* Line number of last line read from in */
12187   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
12188   FILE *in;              /* Read commands from this stream */
12189   FILE *out;             /* Write results here */
12190   FILE *traceOut;        /* Output for sqlite3_trace() */
12191   int nErr;              /* Number of errors seen */
12192   int mode;              /* An output mode setting */
12193   int modePrior;         /* Saved mode */
12194   int cMode;             /* temporary output mode for the current query */
12195   int normalMode;        /* Output mode before ".explain on" */
12196   int writableSchema;    /* True if PRAGMA writable_schema=ON */
12197   int showHeader;        /* True to show column names in List or Column mode */
12198   int nCheck;            /* Number of ".check" commands run */
12199   unsigned nProgress;    /* Number of progress callbacks encountered */
12200   unsigned mxProgress;   /* Maximum progress callbacks before failing */
12201   unsigned flgProgress;  /* Flags for the progress callback */
12202   unsigned shellFlgs;    /* Various flags */
12203   unsigned priorShFlgs;  /* Saved copy of flags */
12204   sqlite3_int64 szMax;   /* --maxsize argument to .open */
12205   char *zDestTable;      /* Name of destination table when MODE_Insert */
12206   char *zTempFile;       /* Temporary file that might need deleting */
12207   char zTestcase[30];    /* Name of current test case */
12208   char colSeparator[20]; /* Column separator character for several modes */
12209   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
12210   char colSepPrior[20];  /* Saved column separator */
12211   char rowSepPrior[20];  /* Saved row separator */
12212   int *colWidth;         /* Requested width of each column in columnar modes */
12213   int *actualWidth;      /* Actual width of each column */
12214   int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
12215   char nullValue[20];    /* The text to print when a NULL comes back from
12216                          ** the database */
12217   char outfile[FILENAME_MAX]; /* Filename for *out */
12218   sqlite3_stmt *pStmt;   /* Current statement if any. */
12219   FILE *pLog;            /* Write log output here */
12220   struct AuxDb {         /* Storage space for auxiliary database connections */
12221     sqlite3 *db;               /* Connection pointer */
12222     const char *zDbFilename;   /* Filename used to open the connection */
12223     char *zFreeOnClose;        /* Free this memory allocation on close */
12224 #if defined(SQLITE_ENABLE_SESSION)
12225     int nSession;              /* Number of active sessions */
12226     OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
12227 #endif
12228   } aAuxDb[5],           /* Array of all database connections */
12229     *pAuxDb;             /* Currently active database connection */
12230   int *aiIndent;         /* Array of indents used in MODE_Explain */
12231   int nIndent;           /* Size of array aiIndent[] */
12232   int iIndent;           /* Index of current op in aiIndent[] */
12233   char *zNonce;          /* Nonce for temporary safe-mode excapes */
12234   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
12235   ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
12236 };
12237 
12238 
12239 /* Allowed values for ShellState.autoEQP
12240 */
12241 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
12242 #define AUTOEQP_on       1           /* Automatic EQP is on */
12243 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
12244 #define AUTOEQP_full     3           /* Show full EXPLAIN */
12245 
12246 /* Allowed values for ShellState.openMode
12247 */
12248 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
12249 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
12250 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
12251 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
12252 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
12253 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
12254 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
12255 
12256 /* Allowed values for ShellState.eTraceType
12257 */
12258 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
12259 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
12260 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
12261 
12262 /* Bits in the ShellState.flgProgress variable */
12263 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
12264 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
12265                                    ** callback limit is reached, and for each
12266                                    ** top-level SQL statement */
12267 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
12268 
12269 /*
12270 ** These are the allowed shellFlgs values
12271 */
12272 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
12273 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
12274 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
12275 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
12276 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
12277 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
12278 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
12279 #define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
12280 #define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
12281 #define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
12282 
12283 /*
12284 ** Macros for testing and setting shellFlgs
12285 */
12286 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
12287 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
12288 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
12289 
12290 /*
12291 ** These are the allowed modes.
12292 */
12293 #define MODE_Line     0  /* One column per line.  Blank line between records */
12294 #define MODE_Column   1  /* One record per line in neat columns */
12295 #define MODE_List     2  /* One record per line with a separator */
12296 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
12297 #define MODE_Html     4  /* Generate an XHTML table */
12298 #define MODE_Insert   5  /* Generate SQL "insert" statements */
12299 #define MODE_Quote    6  /* Quote values as for SQL */
12300 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
12301 #define MODE_Csv      8  /* Quote strings, numbers are plain */
12302 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
12303 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
12304 #define MODE_Pretty  11  /* Pretty-print schemas */
12305 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
12306 #define MODE_Json    13  /* Output JSON */
12307 #define MODE_Markdown 14 /* Markdown formatting */
12308 #define MODE_Table   15  /* MySQL-style table formatting */
12309 #define MODE_Box     16  /* Unicode box-drawing characters */
12310 #define MODE_Count   17  /* Output only a count of the rows of output */
12311 #define MODE_Off     18  /* No query output shown */
12312 
12313 static const char *modeDescr[] = {
12314   "line",
12315   "column",
12316   "list",
12317   "semi",
12318   "html",
12319   "insert",
12320   "quote",
12321   "tcl",
12322   "csv",
12323   "explain",
12324   "ascii",
12325   "prettyprint",
12326   "eqp",
12327   "json",
12328   "markdown",
12329   "table",
12330   "box",
12331   "count",
12332   "off"
12333 };
12334 
12335 /*
12336 ** These are the column/row/line separators used by the various
12337 ** import/export modes.
12338 */
12339 #define SEP_Column    "|"
12340 #define SEP_Row       "\n"
12341 #define SEP_Tab       "\t"
12342 #define SEP_Space     " "
12343 #define SEP_Comma     ","
12344 #define SEP_CrLf      "\r\n"
12345 #define SEP_Unit      "\x1F"
12346 #define SEP_Record    "\x1E"
12347 
12348 /*
12349 ** Limit input nesting via .read or any other input redirect.
12350 ** It's not too expensive, so a generous allowance can be made.
12351 */
12352 #define MAX_INPUT_NESTING 25
12353 
12354 /*
12355 ** A callback for the sqlite3_log() interface.
12356 */
12357 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
12358   ShellState *p = (ShellState*)pArg;
12359   if( p->pLog==0 ) return;
12360   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
12361   fflush(p->pLog);
12362 }
12363 
12364 /*
12365 ** SQL function:  shell_putsnl(X)
12366 **
12367 ** Write the text X to the screen (or whatever output is being directed)
12368 ** adding a newline at the end, and then return X.
12369 */
12370 static void shellPutsFunc(
12371   sqlite3_context *pCtx,
12372   int nVal,
12373   sqlite3_value **apVal
12374 ){
12375   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
12376   (void)nVal;
12377   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
12378   sqlite3_result_value(pCtx, apVal[0]);
12379 }
12380 
12381 /*
12382 ** If in safe mode, print an error message described by the arguments
12383 ** and exit immediately.
12384 */
12385 static void failIfSafeMode(
12386   ShellState *p,
12387   const char *zErrMsg,
12388   ...
12389 ){
12390   if( p->bSafeMode ){
12391     va_list ap;
12392     char *zMsg;
12393     va_start(ap, zErrMsg);
12394     zMsg = sqlite3_vmprintf(zErrMsg, ap);
12395     va_end(ap);
12396     raw_printf(stderr, "line %d: ", p->lineno);
12397     utf8_printf(stderr, "%s\n", zMsg);
12398     exit(1);
12399   }
12400 }
12401 
12402 /*
12403 ** SQL function:   edit(VALUE)
12404 **                 edit(VALUE,EDITOR)
12405 **
12406 ** These steps:
12407 **
12408 **     (1) Write VALUE into a temporary file.
12409 **     (2) Run program EDITOR on that temporary file.
12410 **     (3) Read the temporary file back and return its content as the result.
12411 **     (4) Delete the temporary file
12412 **
12413 ** If the EDITOR argument is omitted, use the value in the VISUAL
12414 ** environment variable.  If still there is no EDITOR, through an error.
12415 **
12416 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12417 */
12418 #ifndef SQLITE_NOHAVE_SYSTEM
12419 static void editFunc(
12420   sqlite3_context *context,
12421   int argc,
12422   sqlite3_value **argv
12423 ){
12424   const char *zEditor;
12425   char *zTempFile = 0;
12426   sqlite3 *db;
12427   char *zCmd = 0;
12428   int bBin;
12429   int rc;
12430   int hasCRNL = 0;
12431   FILE *f = 0;
12432   sqlite3_int64 sz;
12433   sqlite3_int64 x;
12434   unsigned char *p = 0;
12435 
12436   if( argc==2 ){
12437     zEditor = (const char*)sqlite3_value_text(argv[1]);
12438   }else{
12439     zEditor = getenv("VISUAL");
12440   }
12441   if( zEditor==0 ){
12442     sqlite3_result_error(context, "no editor for edit()", -1);
12443     return;
12444   }
12445   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
12446     sqlite3_result_error(context, "NULL input to edit()", -1);
12447     return;
12448   }
12449   db = sqlite3_context_db_handle(context);
12450   zTempFile = 0;
12451   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
12452   if( zTempFile==0 ){
12453     sqlite3_uint64 r = 0;
12454     sqlite3_randomness(sizeof(r), &r);
12455     zTempFile = sqlite3_mprintf("temp%llx", r);
12456     if( zTempFile==0 ){
12457       sqlite3_result_error_nomem(context);
12458       return;
12459     }
12460   }
12461   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
12462   /* When writing the file to be edited, do \n to \r\n conversions on systems
12463   ** that want \r\n line endings */
12464   f = fopen(zTempFile, bBin ? "wb" : "w");
12465   if( f==0 ){
12466     sqlite3_result_error(context, "edit() cannot open temp file", -1);
12467     goto edit_func_end;
12468   }
12469   sz = sqlite3_value_bytes(argv[0]);
12470   if( bBin ){
12471     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
12472   }else{
12473     const char *z = (const char*)sqlite3_value_text(argv[0]);
12474     /* Remember whether or not the value originally contained \r\n */
12475     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
12476     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
12477   }
12478   fclose(f);
12479   f = 0;
12480   if( x!=sz ){
12481     sqlite3_result_error(context, "edit() could not write the whole file", -1);
12482     goto edit_func_end;
12483   }
12484   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
12485   if( zCmd==0 ){
12486     sqlite3_result_error_nomem(context);
12487     goto edit_func_end;
12488   }
12489   rc = system(zCmd);
12490   sqlite3_free(zCmd);
12491   if( rc ){
12492     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
12493     goto edit_func_end;
12494   }
12495   f = fopen(zTempFile, "rb");
12496   if( f==0 ){
12497     sqlite3_result_error(context,
12498       "edit() cannot reopen temp file after edit", -1);
12499     goto edit_func_end;
12500   }
12501   fseek(f, 0, SEEK_END);
12502   sz = ftell(f);
12503   rewind(f);
12504   p = sqlite3_malloc64( sz+1 );
12505   if( p==0 ){
12506     sqlite3_result_error_nomem(context);
12507     goto edit_func_end;
12508   }
12509   x = fread(p, 1, (size_t)sz, f);
12510   fclose(f);
12511   f = 0;
12512   if( x!=sz ){
12513     sqlite3_result_error(context, "could not read back the whole file", -1);
12514     goto edit_func_end;
12515   }
12516   if( bBin ){
12517     sqlite3_result_blob64(context, p, sz, sqlite3_free);
12518   }else{
12519     sqlite3_int64 i, j;
12520     if( hasCRNL ){
12521       /* If the original contains \r\n then do no conversions back to \n */
12522     }else{
12523       /* If the file did not originally contain \r\n then convert any new
12524       ** \r\n back into \n */
12525       for(i=j=0; i<sz; i++){
12526         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
12527         p[j++] = p[i];
12528       }
12529       sz = j;
12530       p[sz] = 0;
12531     }
12532     sqlite3_result_text64(context, (const char*)p, sz,
12533                           sqlite3_free, SQLITE_UTF8);
12534   }
12535   p = 0;
12536 
12537 edit_func_end:
12538   if( f ) fclose(f);
12539   unlink(zTempFile);
12540   sqlite3_free(zTempFile);
12541   sqlite3_free(p);
12542 }
12543 #endif /* SQLITE_NOHAVE_SYSTEM */
12544 
12545 /*
12546 ** Save or restore the current output mode
12547 */
12548 static void outputModePush(ShellState *p){
12549   p->modePrior = p->mode;
12550   p->priorShFlgs = p->shellFlgs;
12551   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
12552   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
12553 }
12554 static void outputModePop(ShellState *p){
12555   p->mode = p->modePrior;
12556   p->shellFlgs = p->priorShFlgs;
12557   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
12558   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
12559 }
12560 
12561 /*
12562 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12563 */
12564 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12565   int i;
12566   char *zBlob = (char *)pBlob;
12567   raw_printf(out,"X'");
12568   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12569   raw_printf(out,"'");
12570 }
12571 
12572 /*
12573 ** Find a string that is not found anywhere in z[].  Return a pointer
12574 ** to that string.
12575 **
12576 ** Try to use zA and zB first.  If both of those are already found in z[]
12577 ** then make up some string and store it in the buffer zBuf.
12578 */
12579 static const char *unused_string(
12580   const char *z,                    /* Result must not appear anywhere in z */
12581   const char *zA, const char *zB,   /* Try these first */
12582   char *zBuf                        /* Space to store a generated string */
12583 ){
12584   unsigned i = 0;
12585   if( strstr(z, zA)==0 ) return zA;
12586   if( strstr(z, zB)==0 ) return zB;
12587   do{
12588     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
12589   }while( strstr(z,zBuf)!=0 );
12590   return zBuf;
12591 }
12592 
12593 /*
12594 ** Output the given string as a quoted string using SQL quoting conventions.
12595 **
12596 ** See also: output_quoted_escaped_string()
12597 */
12598 static void output_quoted_string(FILE *out, const char *z){
12599   int i;
12600   char c;
12601   setBinaryMode(out, 1);
12602   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12603   if( c==0 ){
12604     utf8_printf(out,"'%s'",z);
12605   }else{
12606     raw_printf(out, "'");
12607     while( *z ){
12608       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12609       if( c=='\'' ) i++;
12610       if( i ){
12611         utf8_printf(out, "%.*s", i, z);
12612         z += i;
12613       }
12614       if( c=='\'' ){
12615         raw_printf(out, "'");
12616         continue;
12617       }
12618       if( c==0 ){
12619         break;
12620       }
12621       z++;
12622     }
12623     raw_printf(out, "'");
12624   }
12625   setTextMode(out, 1);
12626 }
12627 
12628 /*
12629 ** Output the given string as a quoted string using SQL quoting conventions.
12630 ** Additionallly , escape the "\n" and "\r" characters so that they do not
12631 ** get corrupted by end-of-line translation facilities in some operating
12632 ** systems.
12633 **
12634 ** This is like output_quoted_string() but with the addition of the \r\n
12635 ** escape mechanism.
12636 */
12637 static void output_quoted_escaped_string(FILE *out, const char *z){
12638   int i;
12639   char c;
12640   setBinaryMode(out, 1);
12641   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
12642   if( c==0 ){
12643     utf8_printf(out,"'%s'",z);
12644   }else{
12645     const char *zNL = 0;
12646     const char *zCR = 0;
12647     int nNL = 0;
12648     int nCR = 0;
12649     char zBuf1[20], zBuf2[20];
12650     for(i=0; z[i]; i++){
12651       if( z[i]=='\n' ) nNL++;
12652       if( z[i]=='\r' ) nCR++;
12653     }
12654     if( nNL ){
12655       raw_printf(out, "replace(");
12656       zNL = unused_string(z, "\\n", "\\012", zBuf1);
12657     }
12658     if( nCR ){
12659       raw_printf(out, "replace(");
12660       zCR = unused_string(z, "\\r", "\\015", zBuf2);
12661     }
12662     raw_printf(out, "'");
12663     while( *z ){
12664       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
12665       if( c=='\'' ) i++;
12666       if( i ){
12667         utf8_printf(out, "%.*s", i, z);
12668         z += i;
12669       }
12670       if( c=='\'' ){
12671         raw_printf(out, "'");
12672         continue;
12673       }
12674       if( c==0 ){
12675         break;
12676       }
12677       z++;
12678       if( c=='\n' ){
12679         raw_printf(out, "%s", zNL);
12680         continue;
12681       }
12682       raw_printf(out, "%s", zCR);
12683     }
12684     raw_printf(out, "'");
12685     if( nCR ){
12686       raw_printf(out, ",'%s',char(13))", zCR);
12687     }
12688     if( nNL ){
12689       raw_printf(out, ",'%s',char(10))", zNL);
12690     }
12691   }
12692   setTextMode(out, 1);
12693 }
12694 
12695 /*
12696 ** Output the given string as a quoted according to C or TCL quoting rules.
12697 */
12698 static void output_c_string(FILE *out, const char *z){
12699   unsigned int c;
12700   fputc('"', out);
12701   while( (c = *(z++))!=0 ){
12702     if( c=='\\' ){
12703       fputc(c, out);
12704       fputc(c, out);
12705     }else if( c=='"' ){
12706       fputc('\\', out);
12707       fputc('"', out);
12708     }else if( c=='\t' ){
12709       fputc('\\', out);
12710       fputc('t', out);
12711     }else if( c=='\n' ){
12712       fputc('\\', out);
12713       fputc('n', out);
12714     }else if( c=='\r' ){
12715       fputc('\\', out);
12716       fputc('r', out);
12717     }else if( !isprint(c&0xff) ){
12718       raw_printf(out, "\\%03o", c&0xff);
12719     }else{
12720       fputc(c, out);
12721     }
12722   }
12723   fputc('"', out);
12724 }
12725 
12726 /*
12727 ** Output the given string as a quoted according to JSON quoting rules.
12728 */
12729 static void output_json_string(FILE *out, const char *z, int n){
12730   unsigned int c;
12731   if( n<0 ) n = (int)strlen(z);
12732   fputc('"', out);
12733   while( n-- ){
12734     c = *(z++);
12735     if( c=='\\' || c=='"' ){
12736       fputc('\\', out);
12737       fputc(c, out);
12738     }else if( c<=0x1f ){
12739       fputc('\\', out);
12740       if( c=='\b' ){
12741         fputc('b', out);
12742       }else if( c=='\f' ){
12743         fputc('f', out);
12744       }else if( c=='\n' ){
12745         fputc('n', out);
12746       }else if( c=='\r' ){
12747         fputc('r', out);
12748       }else if( c=='\t' ){
12749         fputc('t', out);
12750       }else{
12751          raw_printf(out, "u%04x",c);
12752       }
12753     }else{
12754       fputc(c, out);
12755     }
12756   }
12757   fputc('"', out);
12758 }
12759 
12760 /*
12761 ** Output the given string with characters that are special to
12762 ** HTML escaped.
12763 */
12764 static void output_html_string(FILE *out, const char *z){
12765   int i;
12766   if( z==0 ) z = "";
12767   while( *z ){
12768     for(i=0;   z[i]
12769             && z[i]!='<'
12770             && z[i]!='&'
12771             && z[i]!='>'
12772             && z[i]!='\"'
12773             && z[i]!='\'';
12774         i++){}
12775     if( i>0 ){
12776       utf8_printf(out,"%.*s",i,z);
12777     }
12778     if( z[i]=='<' ){
12779       raw_printf(out,"&lt;");
12780     }else if( z[i]=='&' ){
12781       raw_printf(out,"&amp;");
12782     }else if( z[i]=='>' ){
12783       raw_printf(out,"&gt;");
12784     }else if( z[i]=='\"' ){
12785       raw_printf(out,"&quot;");
12786     }else if( z[i]=='\'' ){
12787       raw_printf(out,"&#39;");
12788     }else{
12789       break;
12790     }
12791     z += i + 1;
12792   }
12793 }
12794 
12795 /*
12796 ** If a field contains any character identified by a 1 in the following
12797 ** array, then the string must be quoted for CSV.
12798 */
12799 static const char needCsvQuote[] = {
12800   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12801   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12802   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
12803   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12804   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12805   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12806   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12807   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
12808   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12809   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12810   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12811   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12812   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12813   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12814   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12815   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12816 };
12817 
12818 /*
12819 ** Output a single term of CSV.  Actually, p->colSeparator is used for
12820 ** the separator, which may or may not be a comma.  p->nullValue is
12821 ** the null value.  Strings are quoted if necessary.  The separator
12822 ** is only issued if bSep is true.
12823 */
12824 static void output_csv(ShellState *p, const char *z, int bSep){
12825   FILE *out = p->out;
12826   if( z==0 ){
12827     utf8_printf(out,"%s",p->nullValue);
12828   }else{
12829     unsigned i;
12830     for(i=0; z[i]; i++){
12831       if( needCsvQuote[((unsigned char*)z)[i]] ){
12832         i = 0;
12833         break;
12834       }
12835     }
12836     if( i==0 || strstr(z, p->colSeparator)!=0 ){
12837       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12838       shell_check_oom(zQuoted);
12839       utf8_printf(out, "%s", zQuoted);
12840       sqlite3_free(zQuoted);
12841     }else{
12842       utf8_printf(out, "%s", z);
12843     }
12844   }
12845   if( bSep ){
12846     utf8_printf(p->out, "%s", p->colSeparator);
12847   }
12848 }
12849 
12850 /*
12851 ** This routine runs when the user presses Ctrl-C
12852 */
12853 static void interrupt_handler(int NotUsed){
12854   UNUSED_PARAMETER(NotUsed);
12855   seenInterrupt++;
12856   if( seenInterrupt>2 ) exit(1);
12857   if( globalDb ) sqlite3_interrupt(globalDb);
12858 }
12859 
12860 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12861 /*
12862 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12863 */
12864 static BOOL WINAPI ConsoleCtrlHandler(
12865   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
12866 ){
12867   if( dwCtrlType==CTRL_C_EVENT ){
12868     interrupt_handler(0);
12869     return TRUE;
12870   }
12871   return FALSE;
12872 }
12873 #endif
12874 
12875 #ifndef SQLITE_OMIT_AUTHORIZATION
12876 /*
12877 ** This authorizer runs in safe mode.
12878 */
12879 static int safeModeAuth(
12880   void *pClientData,
12881   int op,
12882   const char *zA1,
12883   const char *zA2,
12884   const char *zA3,
12885   const char *zA4
12886 ){
12887   ShellState *p = (ShellState*)pClientData;
12888   static const char *azProhibitedFunctions[] = {
12889     "edit",
12890     "fts3_tokenizer",
12891     "load_extension",
12892     "readfile",
12893     "writefile",
12894     "zipfile",
12895     "zipfile_cds",
12896   };
12897   UNUSED_PARAMETER(zA2);
12898   UNUSED_PARAMETER(zA3);
12899   UNUSED_PARAMETER(zA4);
12900   switch( op ){
12901     case SQLITE_ATTACH: {
12902       failIfSafeMode(p, "cannot run ATTACH in safe mode");
12903       break;
12904     }
12905     case SQLITE_FUNCTION: {
12906       int i;
12907       for(i=0; i<ArraySize(azProhibitedFunctions); i++){
12908         if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
12909           failIfSafeMode(p, "cannot use the %s() function in safe mode",
12910                          azProhibitedFunctions[i]);
12911         }
12912       }
12913       break;
12914     }
12915   }
12916   return SQLITE_OK;
12917 }
12918 
12919 /*
12920 ** When the ".auth ON" is set, the following authorizer callback is
12921 ** invoked.  It always returns SQLITE_OK.
12922 */
12923 static int shellAuth(
12924   void *pClientData,
12925   int op,
12926   const char *zA1,
12927   const char *zA2,
12928   const char *zA3,
12929   const char *zA4
12930 ){
12931   ShellState *p = (ShellState*)pClientData;
12932   static const char *azAction[] = { 0,
12933      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
12934      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
12935      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
12936      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
12937      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
12938      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
12939      "PRAGMA",               "READ",                 "SELECT",
12940      "TRANSACTION",          "UPDATE",               "ATTACH",
12941      "DETACH",               "ALTER_TABLE",          "REINDEX",
12942      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
12943      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
12944   };
12945   int i;
12946   const char *az[4];
12947   az[0] = zA1;
12948   az[1] = zA2;
12949   az[2] = zA3;
12950   az[3] = zA4;
12951   utf8_printf(p->out, "authorizer: %s", azAction[op]);
12952   for(i=0; i<4; i++){
12953     raw_printf(p->out, " ");
12954     if( az[i] ){
12955       output_c_string(p->out, az[i]);
12956     }else{
12957       raw_printf(p->out, "NULL");
12958     }
12959   }
12960   raw_printf(p->out, "\n");
12961   if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
12962   return SQLITE_OK;
12963 }
12964 #endif
12965 
12966 /*
12967 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
12968 **
12969 ** This routine converts some CREATE TABLE statements for shadow tables
12970 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
12971 */
12972 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
12973   if( z==0 ) return;
12974   if( zTail==0 ) return;
12975   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
12976     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
12977   }else{
12978     utf8_printf(out, "%s%s", z, zTail);
12979   }
12980 }
12981 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
12982   char c = z[n];
12983   z[n] = 0;
12984   printSchemaLine(out, z, zTail);
12985   z[n] = c;
12986 }
12987 
12988 /*
12989 ** Return true if string z[] has nothing but whitespace and comments to the
12990 ** end of the first line.
12991 */
12992 static int wsToEol(const char *z){
12993   int i;
12994   for(i=0; z[i]; i++){
12995     if( z[i]=='\n' ) return 1;
12996     if( IsSpace(z[i]) ) continue;
12997     if( z[i]=='-' && z[i+1]=='-' ) return 1;
12998     return 0;
12999   }
13000   return 1;
13001 }
13002 
13003 /*
13004 ** Add a new entry to the EXPLAIN QUERY PLAN data
13005 */
13006 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
13007   EQPGraphRow *pNew;
13008   int nText = strlen30(zText);
13009   if( p->autoEQPtest ){
13010     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13011   }
13012   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13013   shell_check_oom(pNew);
13014   pNew->iEqpId = iEqpId;
13015   pNew->iParentId = p2;
13016   memcpy(pNew->zText, zText, nText+1);
13017   pNew->pNext = 0;
13018   if( p->sGraph.pLast ){
13019     p->sGraph.pLast->pNext = pNew;
13020   }else{
13021     p->sGraph.pRow = pNew;
13022   }
13023   p->sGraph.pLast = pNew;
13024 }
13025 
13026 /*
13027 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
13028 ** in p->sGraph.
13029 */
13030 static void eqp_reset(ShellState *p){
13031   EQPGraphRow *pRow, *pNext;
13032   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
13033     pNext = pRow->pNext;
13034     sqlite3_free(pRow);
13035   }
13036   memset(&p->sGraph, 0, sizeof(p->sGraph));
13037 }
13038 
13039 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
13040 ** pOld, or return the first such line if pOld is NULL
13041 */
13042 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
13043   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
13044   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
13045   return pRow;
13046 }
13047 
13048 /* Render a single level of the graph that has iEqpId as its parent.  Called
13049 ** recursively to render sublevels.
13050 */
13051 static void eqp_render_level(ShellState *p, int iEqpId){
13052   EQPGraphRow *pRow, *pNext;
13053   int n = strlen30(p->sGraph.zPrefix);
13054   char *z;
13055   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
13056     pNext = eqp_next_row(p, iEqpId, pRow);
13057     z = pRow->zText;
13058     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
13059                 pNext ? "|--" : "`--", z);
13060     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
13061       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
13062       eqp_render_level(p, pRow->iEqpId);
13063       p->sGraph.zPrefix[n] = 0;
13064     }
13065   }
13066 }
13067 
13068 /*
13069 ** Display and reset the EXPLAIN QUERY PLAN data
13070 */
13071 static void eqp_render(ShellState *p){
13072   EQPGraphRow *pRow = p->sGraph.pRow;
13073   if( pRow ){
13074     if( pRow->zText[0]=='-' ){
13075       if( pRow->pNext==0 ){
13076         eqp_reset(p);
13077         return;
13078       }
13079       utf8_printf(p->out, "%s\n", pRow->zText+3);
13080       p->sGraph.pRow = pRow->pNext;
13081       sqlite3_free(pRow);
13082     }else{
13083       utf8_printf(p->out, "QUERY PLAN\n");
13084     }
13085     p->sGraph.zPrefix[0] = 0;
13086     eqp_render_level(p, 0);
13087     eqp_reset(p);
13088   }
13089 }
13090 
13091 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
13092 /*
13093 ** Progress handler callback.
13094 */
13095 static int progress_handler(void *pClientData) {
13096   ShellState *p = (ShellState*)pClientData;
13097   p->nProgress++;
13098   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
13099     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
13100     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
13101     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
13102     return 1;
13103   }
13104   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
13105     raw_printf(p->out, "Progress %u\n", p->nProgress);
13106   }
13107   return 0;
13108 }
13109 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
13110 
13111 /*
13112 ** Print N dashes
13113 */
13114 static void print_dashes(FILE *out, int N){
13115   const char zDash[] = "--------------------------------------------------";
13116   const int nDash = sizeof(zDash) - 1;
13117   while( N>nDash ){
13118     fputs(zDash, out);
13119     N -= nDash;
13120   }
13121   raw_printf(out, "%.*s", N, zDash);
13122 }
13123 
13124 /*
13125 ** Print a markdown or table-style row separator using ascii-art
13126 */
13127 static void print_row_separator(
13128   ShellState *p,
13129   int nArg,
13130   const char *zSep
13131 ){
13132   int i;
13133   if( nArg>0 ){
13134     fputs(zSep, p->out);
13135     print_dashes(p->out, p->actualWidth[0]+2);
13136     for(i=1; i<nArg; i++){
13137       fputs(zSep, p->out);
13138       print_dashes(p->out, p->actualWidth[i]+2);
13139     }
13140     fputs(zSep, p->out);
13141   }
13142   fputs("\n", p->out);
13143 }
13144 
13145 /*
13146 ** This is the callback routine that the shell
13147 ** invokes for each row of a query result.
13148 */
13149 static int shell_callback(
13150   void *pArg,
13151   int nArg,        /* Number of result columns */
13152   char **azArg,    /* Text of each result column */
13153   char **azCol,    /* Column names */
13154   int *aiType      /* Column types.  Might be NULL */
13155 ){
13156   int i;
13157   ShellState *p = (ShellState*)pArg;
13158 
13159   if( azArg==0 ) return 0;
13160   switch( p->cMode ){
13161     case MODE_Count:
13162     case MODE_Off: {
13163       break;
13164     }
13165     case MODE_Line: {
13166       int w = 5;
13167       if( azArg==0 ) break;
13168       for(i=0; i<nArg; i++){
13169         int len = strlen30(azCol[i] ? azCol[i] : "");
13170         if( len>w ) w = len;
13171       }
13172       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
13173       for(i=0; i<nArg; i++){
13174         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
13175                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
13176       }
13177       break;
13178     }
13179     case MODE_Explain: {
13180       static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
13181       if( nArg>ArraySize(aExplainWidth) ){
13182         nArg = ArraySize(aExplainWidth);
13183       }
13184       if( p->cnt++==0 ){
13185         for(i=0; i<nArg; i++){
13186           int w = aExplainWidth[i];
13187           utf8_width_print(p->out, w, azCol[i]);
13188           fputs(i==nArg-1 ? "\n" : "  ", p->out);
13189         }
13190         for(i=0; i<nArg; i++){
13191           int w = aExplainWidth[i];
13192           print_dashes(p->out, w);
13193           fputs(i==nArg-1 ? "\n" : "  ", p->out);
13194         }
13195       }
13196       if( azArg==0 ) break;
13197       for(i=0; i<nArg; i++){
13198         int w = aExplainWidth[i];
13199         if( i==nArg-1 ) w = 0;
13200         if( azArg[i] && strlenChar(azArg[i])>w ){
13201           w = strlenChar(azArg[i]);
13202         }
13203         if( i==1 && p->aiIndent && p->pStmt ){
13204           if( p->iIndent<p->nIndent ){
13205             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
13206           }
13207           p->iIndent++;
13208         }
13209         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
13210         fputs(i==nArg-1 ? "\n" : "  ", p->out);
13211       }
13212       break;
13213     }
13214     case MODE_Semi: {   /* .schema and .fullschema output */
13215       printSchemaLine(p->out, azArg[0], ";\n");
13216       break;
13217     }
13218     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
13219       char *z;
13220       int j;
13221       int nParen = 0;
13222       char cEnd = 0;
13223       char c;
13224       int nLine = 0;
13225       assert( nArg==1 );
13226       if( azArg[0]==0 ) break;
13227       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
13228        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
13229       ){
13230         utf8_printf(p->out, "%s;\n", azArg[0]);
13231         break;
13232       }
13233       z = sqlite3_mprintf("%s", azArg[0]);
13234       shell_check_oom(z);
13235       j = 0;
13236       for(i=0; IsSpace(z[i]); i++){}
13237       for(; (c = z[i])!=0; i++){
13238         if( IsSpace(c) ){
13239           if( z[j-1]=='\r' ) z[j-1] = '\n';
13240           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
13241         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
13242           j--;
13243         }
13244         z[j++] = c;
13245       }
13246       while( j>0 && IsSpace(z[j-1]) ){ j--; }
13247       z[j] = 0;
13248       if( strlen30(z)>=79 ){
13249         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
13250           if( c==cEnd ){
13251             cEnd = 0;
13252           }else if( c=='"' || c=='\'' || c=='`' ){
13253             cEnd = c;
13254           }else if( c=='[' ){
13255             cEnd = ']';
13256           }else if( c=='-' && z[i+1]=='-' ){
13257             cEnd = '\n';
13258           }else if( c=='(' ){
13259             nParen++;
13260           }else if( c==')' ){
13261             nParen--;
13262             if( nLine>0 && nParen==0 && j>0 ){
13263               printSchemaLineN(p->out, z, j, "\n");
13264               j = 0;
13265             }
13266           }
13267           z[j++] = c;
13268           if( nParen==1 && cEnd==0
13269            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
13270           ){
13271             if( c=='\n' ) j--;
13272             printSchemaLineN(p->out, z, j, "\n  ");
13273             j = 0;
13274             nLine++;
13275             while( IsSpace(z[i+1]) ){ i++; }
13276           }
13277         }
13278         z[j] = 0;
13279       }
13280       printSchemaLine(p->out, z, ";\n");
13281       sqlite3_free(z);
13282       break;
13283     }
13284     case MODE_List: {
13285       if( p->cnt++==0 && p->showHeader ){
13286         for(i=0; i<nArg; i++){
13287           utf8_printf(p->out,"%s%s",azCol[i],
13288                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
13289         }
13290       }
13291       if( azArg==0 ) break;
13292       for(i=0; i<nArg; i++){
13293         char *z = azArg[i];
13294         if( z==0 ) z = p->nullValue;
13295         utf8_printf(p->out, "%s", z);
13296         if( i<nArg-1 ){
13297           utf8_printf(p->out, "%s", p->colSeparator);
13298         }else{
13299           utf8_printf(p->out, "%s", p->rowSeparator);
13300         }
13301       }
13302       break;
13303     }
13304     case MODE_Html: {
13305       if( p->cnt++==0 && p->showHeader ){
13306         raw_printf(p->out,"<TR>");
13307         for(i=0; i<nArg; i++){
13308           raw_printf(p->out,"<TH>");
13309           output_html_string(p->out, azCol[i]);
13310           raw_printf(p->out,"</TH>\n");
13311         }
13312         raw_printf(p->out,"</TR>\n");
13313       }
13314       if( azArg==0 ) break;
13315       raw_printf(p->out,"<TR>");
13316       for(i=0; i<nArg; i++){
13317         raw_printf(p->out,"<TD>");
13318         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13319         raw_printf(p->out,"</TD>\n");
13320       }
13321       raw_printf(p->out,"</TR>\n");
13322       break;
13323     }
13324     case MODE_Tcl: {
13325       if( p->cnt++==0 && p->showHeader ){
13326         for(i=0; i<nArg; i++){
13327           output_c_string(p->out,azCol[i] ? azCol[i] : "");
13328           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13329         }
13330         utf8_printf(p->out, "%s", p->rowSeparator);
13331       }
13332       if( azArg==0 ) break;
13333       for(i=0; i<nArg; i++){
13334         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13335         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13336       }
13337       utf8_printf(p->out, "%s", p->rowSeparator);
13338       break;
13339     }
13340     case MODE_Csv: {
13341       setBinaryMode(p->out, 1);
13342       if( p->cnt++==0 && p->showHeader ){
13343         for(i=0; i<nArg; i++){
13344           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
13345         }
13346         utf8_printf(p->out, "%s", p->rowSeparator);
13347       }
13348       if( nArg>0 ){
13349         for(i=0; i<nArg; i++){
13350           output_csv(p, azArg[i], i<nArg-1);
13351         }
13352         utf8_printf(p->out, "%s", p->rowSeparator);
13353       }
13354       setTextMode(p->out, 1);
13355       break;
13356     }
13357     case MODE_Insert: {
13358       if( azArg==0 ) break;
13359       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
13360       if( p->showHeader ){
13361         raw_printf(p->out,"(");
13362         for(i=0; i<nArg; i++){
13363           if( i>0 ) raw_printf(p->out, ",");
13364           if( quoteChar(azCol[i]) ){
13365             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13366             shell_check_oom(z);
13367             utf8_printf(p->out, "%s", z);
13368             sqlite3_free(z);
13369           }else{
13370             raw_printf(p->out, "%s", azCol[i]);
13371           }
13372         }
13373         raw_printf(p->out,")");
13374       }
13375       p->cnt++;
13376       for(i=0; i<nArg; i++){
13377         raw_printf(p->out, i>0 ? "," : " VALUES(");
13378         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13379           utf8_printf(p->out,"NULL");
13380         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13381           if( ShellHasFlag(p, SHFLG_Newlines) ){
13382             output_quoted_string(p->out, azArg[i]);
13383           }else{
13384             output_quoted_escaped_string(p->out, azArg[i]);
13385           }
13386         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13387           utf8_printf(p->out,"%s", azArg[i]);
13388         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13389           char z[50];
13390           double r = sqlite3_column_double(p->pStmt, i);
13391           sqlite3_uint64 ur;
13392           memcpy(&ur,&r,sizeof(r));
13393           if( ur==0x7ff0000000000000LL ){
13394             raw_printf(p->out, "1e999");
13395           }else if( ur==0xfff0000000000000LL ){
13396             raw_printf(p->out, "-1e999");
13397           }else{
13398             sqlite3_int64 ir = (sqlite3_int64)r;
13399             if( r==(double)ir ){
13400               sqlite3_snprintf(50,z,"%lld.0", ir);
13401             }else{
13402               sqlite3_snprintf(50,z,"%!.20g", r);
13403             }
13404             raw_printf(p->out, "%s", z);
13405           }
13406         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13407           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13408           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13409           output_hex_blob(p->out, pBlob, nBlob);
13410         }else if( isNumber(azArg[i], 0) ){
13411           utf8_printf(p->out,"%s", azArg[i]);
13412         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
13413           output_quoted_string(p->out, azArg[i]);
13414         }else{
13415           output_quoted_escaped_string(p->out, azArg[i]);
13416         }
13417       }
13418       raw_printf(p->out,");\n");
13419       break;
13420     }
13421     case MODE_Json: {
13422       if( azArg==0 ) break;
13423       if( p->cnt==0 ){
13424         fputs("[{", p->out);
13425       }else{
13426         fputs(",\n{", p->out);
13427       }
13428       p->cnt++;
13429       for(i=0; i<nArg; i++){
13430         output_json_string(p->out, azCol[i], -1);
13431         putc(':', p->out);
13432         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13433           fputs("null",p->out);
13434         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13435           char z[50];
13436           double r = sqlite3_column_double(p->pStmt, i);
13437           sqlite3_uint64 ur;
13438           memcpy(&ur,&r,sizeof(r));
13439           if( ur==0x7ff0000000000000LL ){
13440             raw_printf(p->out, "1e999");
13441           }else if( ur==0xfff0000000000000LL ){
13442             raw_printf(p->out, "-1e999");
13443           }else{
13444             sqlite3_snprintf(50,z,"%!.20g", r);
13445             raw_printf(p->out, "%s", z);
13446           }
13447         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13448           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13449           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13450           output_json_string(p->out, pBlob, nBlob);
13451         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13452           output_json_string(p->out, azArg[i], -1);
13453         }else{
13454           utf8_printf(p->out,"%s", azArg[i]);
13455         }
13456         if( i<nArg-1 ){
13457           putc(',', p->out);
13458         }
13459       }
13460       putc('}', p->out);
13461       break;
13462     }
13463     case MODE_Quote: {
13464       if( azArg==0 ) break;
13465       if( p->cnt==0 && p->showHeader ){
13466         for(i=0; i<nArg; i++){
13467           if( i>0 ) fputs(p->colSeparator, p->out);
13468           output_quoted_string(p->out, azCol[i]);
13469         }
13470         fputs(p->rowSeparator, p->out);
13471       }
13472       p->cnt++;
13473       for(i=0; i<nArg; i++){
13474         if( i>0 ) fputs(p->colSeparator, p->out);
13475         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13476           utf8_printf(p->out,"NULL");
13477         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13478           output_quoted_string(p->out, azArg[i]);
13479         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13480           utf8_printf(p->out,"%s", azArg[i]);
13481         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13482           char z[50];
13483           double r = sqlite3_column_double(p->pStmt, i);
13484           sqlite3_snprintf(50,z,"%!.20g", r);
13485           raw_printf(p->out, "%s", z);
13486         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13487           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13488           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13489           output_hex_blob(p->out, pBlob, nBlob);
13490         }else if( isNumber(azArg[i], 0) ){
13491           utf8_printf(p->out,"%s", azArg[i]);
13492         }else{
13493           output_quoted_string(p->out, azArg[i]);
13494         }
13495       }
13496       fputs(p->rowSeparator, p->out);
13497       break;
13498     }
13499     case MODE_Ascii: {
13500       if( p->cnt++==0 && p->showHeader ){
13501         for(i=0; i<nArg; i++){
13502           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13503           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
13504         }
13505         utf8_printf(p->out, "%s", p->rowSeparator);
13506       }
13507       if( azArg==0 ) break;
13508       for(i=0; i<nArg; i++){
13509         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13510         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
13511       }
13512       utf8_printf(p->out, "%s", p->rowSeparator);
13513       break;
13514     }
13515     case MODE_EQP: {
13516       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
13517       break;
13518     }
13519   }
13520   return 0;
13521 }
13522 
13523 /*
13524 ** This is the callback routine that the SQLite library
13525 ** invokes for each row of a query result.
13526 */
13527 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
13528   /* since we don't have type info, call the shell_callback with a NULL value */
13529   return shell_callback(pArg, nArg, azArg, azCol, NULL);
13530 }
13531 
13532 /*
13533 ** This is the callback routine from sqlite3_exec() that appends all
13534 ** output onto the end of a ShellText object.
13535 */
13536 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
13537   ShellText *p = (ShellText*)pArg;
13538   int i;
13539   UNUSED_PARAMETER(az);
13540   if( azArg==0 ) return 0;
13541   if( p->n ) appendText(p, "|", 0);
13542   for(i=0; i<nArg; i++){
13543     if( i ) appendText(p, ",", 0);
13544     if( azArg[i] ) appendText(p, azArg[i], 0);
13545   }
13546   return 0;
13547 }
13548 
13549 /*
13550 ** Generate an appropriate SELFTEST table in the main database.
13551 */
13552 static void createSelftestTable(ShellState *p){
13553   char *zErrMsg = 0;
13554   sqlite3_exec(p->db,
13555     "SAVEPOINT selftest_init;\n"
13556     "CREATE TABLE IF NOT EXISTS selftest(\n"
13557     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
13558     "  op TEXT,\n"                   /* Operator:  memo run */
13559     "  cmd TEXT,\n"                  /* Command text */
13560     "  ans TEXT\n"                   /* Desired answer */
13561     ");"
13562     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
13563     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
13564     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
13565     "         'memo','Tests generated by --init');\n"
13566     "INSERT INTO [_shell$self]\n"
13567     "  SELECT 'run',\n"
13568     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
13569                                  "FROM sqlite_schema ORDER BY 2'',224))',\n"
13570     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
13571                           "FROM sqlite_schema ORDER BY 2',224));\n"
13572     "INSERT INTO [_shell$self]\n"
13573     "  SELECT 'run',"
13574     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
13575     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
13576     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
13577     "  FROM (\n"
13578     "    SELECT name FROM sqlite_schema\n"
13579     "     WHERE type='table'\n"
13580     "       AND name<>'selftest'\n"
13581     "       AND coalesce(rootpage,0)>0\n"
13582     "  )\n"
13583     " ORDER BY name;\n"
13584     "INSERT INTO [_shell$self]\n"
13585     "  VALUES('run','PRAGMA integrity_check','ok');\n"
13586     "INSERT INTO selftest(tno,op,cmd,ans)"
13587     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
13588     "DROP TABLE [_shell$self];"
13589     ,0,0,&zErrMsg);
13590   if( zErrMsg ){
13591     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
13592     sqlite3_free(zErrMsg);
13593   }
13594   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
13595 }
13596 
13597 
13598 /*
13599 ** Set the destination table field of the ShellState structure to
13600 ** the name of the table given.  Escape any quote characters in the
13601 ** table name.
13602 */
13603 static void set_table_name(ShellState *p, const char *zName){
13604   int i, n;
13605   char cQuote;
13606   char *z;
13607 
13608   if( p->zDestTable ){
13609     free(p->zDestTable);
13610     p->zDestTable = 0;
13611   }
13612   if( zName==0 ) return;
13613   cQuote = quoteChar(zName);
13614   n = strlen30(zName);
13615   if( cQuote ) n += n+2;
13616   z = p->zDestTable = malloc( n+1 );
13617   shell_check_oom(z);
13618   n = 0;
13619   if( cQuote ) z[n++] = cQuote;
13620   for(i=0; zName[i]; i++){
13621     z[n++] = zName[i];
13622     if( zName[i]==cQuote ) z[n++] = cQuote;
13623   }
13624   if( cQuote ) z[n++] = cQuote;
13625   z[n] = 0;
13626 }
13627 
13628 /*
13629 ** Maybe construct two lines of text that point out the position of a
13630 ** syntax error.  Return a pointer to the text, in memory obtained from
13631 ** sqlite3_malloc().  Or, if the most recent error does not involve a
13632 ** specific token that we can point to, return an empty string.
13633 **
13634 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
13635 ** and should be released by the caller invoking sqlite3_free().
13636 */
13637 static char *shell_error_context(const char *zSql, sqlite3 *db){
13638   int iOffset;
13639   size_t len;
13640   char *zCode;
13641   char *zMsg;
13642   int i;
13643   if( db==0
13644    || zSql==0
13645    || (iOffset = sqlite3_error_offset(db))<0
13646   ){
13647     return sqlite3_mprintf("");
13648   }
13649   while( iOffset>50 ){
13650     iOffset--;
13651     zSql++;
13652     while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
13653   }
13654   len = strlen(zSql);
13655   if( len>78 ){
13656     len = 78;
13657     while( (zSql[len]&0xc0)==0x80 ) len--;
13658   }
13659   zCode = sqlite3_mprintf("%.*s", len, zSql);
13660   for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
13661   if( iOffset<25 ){
13662     zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
13663   }else{
13664     zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
13665   }
13666   return zMsg;
13667 }
13668 
13669 
13670 /*
13671 ** Execute a query statement that will generate SQL output.  Print
13672 ** the result columns, comma-separated, on a line and then add a
13673 ** semicolon terminator to the end of that line.
13674 **
13675 ** If the number of columns is 1 and that column contains text "--"
13676 ** then write the semicolon on a separate line.  That way, if a
13677 ** "--" comment occurs at the end of the statement, the comment
13678 ** won't consume the semicolon terminator.
13679 */
13680 static int run_table_dump_query(
13681   ShellState *p,           /* Query context */
13682   const char *zSelect      /* SELECT statement to extract content */
13683 ){
13684   sqlite3_stmt *pSelect;
13685   int rc;
13686   int nResult;
13687   int i;
13688   const char *z;
13689   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13690   if( rc!=SQLITE_OK || !pSelect ){
13691     char *zContext = shell_error_context(zSelect, p->db);
13692     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
13693                 sqlite3_errmsg(p->db), zContext);
13694     sqlite3_free(zContext);
13695     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13696     return rc;
13697   }
13698   rc = sqlite3_step(pSelect);
13699   nResult = sqlite3_column_count(pSelect);
13700   while( rc==SQLITE_ROW ){
13701     z = (const char*)sqlite3_column_text(pSelect, 0);
13702     utf8_printf(p->out, "%s", z);
13703     for(i=1; i<nResult; i++){
13704       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
13705     }
13706     if( z==0 ) z = "";
13707     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
13708     if( z[0] ){
13709       raw_printf(p->out, "\n;\n");
13710     }else{
13711       raw_printf(p->out, ";\n");
13712     }
13713     rc = sqlite3_step(pSelect);
13714   }
13715   rc = sqlite3_finalize(pSelect);
13716   if( rc!=SQLITE_OK ){
13717     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13718                 sqlite3_errmsg(p->db));
13719     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13720   }
13721   return rc;
13722 }
13723 
13724 /*
13725 ** Allocate space and save off string indicating current error.
13726 */
13727 static char *save_err_msg(
13728   sqlite3 *db,           /* Database to query */
13729   const char *zPhase,    /* When the error occcurs */
13730   int rc,                /* Error code returned from API */
13731   const char *zSql       /* SQL string, or NULL */
13732 ){
13733   char *zErr;
13734   char *zContext;
13735   sqlite3_str *pStr = sqlite3_str_new(0);
13736   sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
13737   if( rc>1 ){
13738     sqlite3_str_appendf(pStr, " (%d)", rc);
13739   }
13740   zContext = shell_error_context(zSql, db);
13741   if( zContext ){
13742     sqlite3_str_appendall(pStr, zContext);
13743     sqlite3_free(zContext);
13744   }
13745   zErr = sqlite3_str_finish(pStr);
13746   shell_check_oom(zErr);
13747   return zErr;
13748 }
13749 
13750 #ifdef __linux__
13751 /*
13752 ** Attempt to display I/O stats on Linux using /proc/PID/io
13753 */
13754 static void displayLinuxIoStats(FILE *out){
13755   FILE *in;
13756   char z[200];
13757   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
13758   in = fopen(z, "rb");
13759   if( in==0 ) return;
13760   while( fgets(z, sizeof(z), in)!=0 ){
13761     static const struct {
13762       const char *zPattern;
13763       const char *zDesc;
13764     } aTrans[] = {
13765       { "rchar: ",                  "Bytes received by read():" },
13766       { "wchar: ",                  "Bytes sent to write():"    },
13767       { "syscr: ",                  "Read() system calls:"      },
13768       { "syscw: ",                  "Write() system calls:"     },
13769       { "read_bytes: ",             "Bytes read from storage:"  },
13770       { "write_bytes: ",            "Bytes written to storage:" },
13771       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
13772     };
13773     int i;
13774     for(i=0; i<ArraySize(aTrans); i++){
13775       int n = strlen30(aTrans[i].zPattern);
13776       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13777         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13778         break;
13779       }
13780     }
13781   }
13782   fclose(in);
13783 }
13784 #endif
13785 
13786 /*
13787 ** Display a single line of status using 64-bit values.
13788 */
13789 static void displayStatLine(
13790   ShellState *p,            /* The shell context */
13791   char *zLabel,             /* Label for this one line */
13792   char *zFormat,            /* Format for the result */
13793   int iStatusCtrl,          /* Which status to display */
13794   int bReset                /* True to reset the stats */
13795 ){
13796   sqlite3_int64 iCur = -1;
13797   sqlite3_int64 iHiwtr = -1;
13798   int i, nPercent;
13799   char zLine[200];
13800   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
13801   for(i=0, nPercent=0; zFormat[i]; i++){
13802     if( zFormat[i]=='%' ) nPercent++;
13803   }
13804   if( nPercent>1 ){
13805     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
13806   }else{
13807     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
13808   }
13809   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
13810 }
13811 
13812 /*
13813 ** Display memory stats.
13814 */
13815 static int display_stats(
13816   sqlite3 *db,                /* Database to query */
13817   ShellState *pArg,           /* Pointer to ShellState */
13818   int bReset                  /* True to reset the stats */
13819 ){
13820   int iCur;
13821   int iHiwtr;
13822   FILE *out;
13823   if( pArg==0 || pArg->out==0 ) return 0;
13824   out = pArg->out;
13825 
13826   if( pArg->pStmt && pArg->statsOn==2 ){
13827     int nCol, i, x;
13828     sqlite3_stmt *pStmt = pArg->pStmt;
13829     char z[100];
13830     nCol = sqlite3_column_count(pStmt);
13831     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
13832     for(i=0; i<nCol; i++){
13833       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
13834       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
13835 #ifndef SQLITE_OMIT_DECLTYPE
13836       sqlite3_snprintf(30, z+x, "declared type:");
13837       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
13838 #endif
13839 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13840       sqlite3_snprintf(30, z+x, "database name:");
13841       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
13842       sqlite3_snprintf(30, z+x, "table name:");
13843       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
13844       sqlite3_snprintf(30, z+x, "origin name:");
13845       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
13846 #endif
13847     }
13848   }
13849 
13850   if( pArg->statsOn==3 ){
13851     if( pArg->pStmt ){
13852       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13853       raw_printf(pArg->out, "VM-steps: %d\n", iCur);
13854     }
13855     return 0;
13856   }
13857 
13858   displayStatLine(pArg, "Memory Used:",
13859      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
13860   displayStatLine(pArg, "Number of Outstanding Allocations:",
13861      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
13862   if( pArg->shellFlgs & SHFLG_Pagecache ){
13863     displayStatLine(pArg, "Number of Pcache Pages Used:",
13864        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
13865   }
13866   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
13867      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
13868   displayStatLine(pArg, "Largest Allocation:",
13869      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
13870   displayStatLine(pArg, "Largest Pcache Allocation:",
13871      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
13872 #ifdef YYTRACKMAXSTACKDEPTH
13873   displayStatLine(pArg, "Deepest Parser Stack:",
13874      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
13875 #endif
13876 
13877   if( db ){
13878     if( pArg->shellFlgs & SHFLG_Lookaside ){
13879       iHiwtr = iCur = -1;
13880       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
13881                         &iCur, &iHiwtr, bReset);
13882       raw_printf(pArg->out,
13883               "Lookaside Slots Used:                %d (max %d)\n",
13884               iCur, iHiwtr);
13885       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
13886                         &iCur, &iHiwtr, bReset);
13887       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
13888               iHiwtr);
13889       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
13890                         &iCur, &iHiwtr, bReset);
13891       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
13892               iHiwtr);
13893       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
13894                         &iCur, &iHiwtr, bReset);
13895       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
13896               iHiwtr);
13897     }
13898     iHiwtr = iCur = -1;
13899     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
13900     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
13901             iCur);
13902     iHiwtr = iCur = -1;
13903     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
13904     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
13905     iHiwtr = iCur = -1;
13906     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
13907     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
13908     iHiwtr = iCur = -1;
13909     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
13910     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
13911     iHiwtr = iCur = -1;
13912     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
13913     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
13914     iHiwtr = iCur = -1;
13915     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
13916     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
13917             iCur);
13918     iHiwtr = iCur = -1;
13919     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
13920     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
13921             iCur);
13922   }
13923 
13924   if( pArg->pStmt ){
13925     int iHit, iMiss;
13926     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
13927                                bReset);
13928     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
13929     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
13930     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
13931     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
13932     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
13933     iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
13934     iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
13935     if( iHit || iMiss ){
13936       raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
13937             iHit, iHit+iMiss);
13938     }
13939     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13940     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
13941     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
13942     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
13943     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
13944     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
13945     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
13946     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
13947   }
13948 
13949 #ifdef __linux__
13950   displayLinuxIoStats(pArg->out);
13951 #endif
13952 
13953   /* Do not remove this machine readable comment: extra-stats-output-here */
13954 
13955   return 0;
13956 }
13957 
13958 /*
13959 ** Display scan stats.
13960 */
13961 static void display_scanstats(
13962   sqlite3 *db,                    /* Database to query */
13963   ShellState *pArg                /* Pointer to ShellState */
13964 ){
13965 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13966   UNUSED_PARAMETER(db);
13967   UNUSED_PARAMETER(pArg);
13968 #else
13969   int i, k, n, mx;
13970   raw_printf(pArg->out, "-------- scanstats --------\n");
13971   mx = 0;
13972   for(k=0; k<=mx; k++){
13973     double rEstLoop = 1.0;
13974     for(i=n=0; 1; i++){
13975       sqlite3_stmt *p = pArg->pStmt;
13976       sqlite3_int64 nLoop, nVisit;
13977       double rEst;
13978       int iSid;
13979       const char *zExplain;
13980       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
13981         break;
13982       }
13983       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
13984       if( iSid>mx ) mx = iSid;
13985       if( iSid!=k ) continue;
13986       if( n==0 ){
13987         rEstLoop = (double)nLoop;
13988         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
13989       }
13990       n++;
13991       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
13992       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
13993       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
13994       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
13995       rEstLoop *= rEst;
13996       raw_printf(pArg->out,
13997           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
13998           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
13999       );
14000     }
14001   }
14002   raw_printf(pArg->out, "---------------------------\n");
14003 #endif
14004 }
14005 
14006 /*
14007 ** Parameter azArray points to a zero-terminated array of strings. zStr
14008 ** points to a single nul-terminated string. Return non-zero if zStr
14009 ** is equal, according to strcmp(), to any of the strings in the array.
14010 ** Otherwise, return zero.
14011 */
14012 static int str_in_array(const char *zStr, const char **azArray){
14013   int i;
14014   for(i=0; azArray[i]; i++){
14015     if( 0==strcmp(zStr, azArray[i]) ) return 1;
14016   }
14017   return 0;
14018 }
14019 
14020 /*
14021 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
14022 ** and populate the ShellState.aiIndent[] array with the number of
14023 ** spaces each opcode should be indented before it is output.
14024 **
14025 ** The indenting rules are:
14026 **
14027 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
14028 **       all opcodes that occur between the p2 jump destination and the opcode
14029 **       itself by 2 spaces.
14030 **
14031 **     * For each "Goto", if the jump destination is earlier in the program
14032 **       and ends on one of:
14033 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
14034 **       or if the P1 parameter is one instead of zero,
14035 **       then indent all opcodes between the earlier instruction
14036 **       and "Goto" by 2 spaces.
14037 */
14038 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
14039   const char *zSql;               /* The text of the SQL statement */
14040   const char *z;                  /* Used to check if this is an EXPLAIN */
14041   int *abYield = 0;               /* True if op is an OP_Yield */
14042   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
14043   int iOp;                        /* Index of operation in p->aiIndent[] */
14044 
14045   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
14046   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
14047                             "Rewind", 0 };
14048   const char *azGoto[] = { "Goto", 0 };
14049 
14050   /* Try to figure out if this is really an EXPLAIN statement. If this
14051   ** cannot be verified, return early.  */
14052   if( sqlite3_column_count(pSql)!=8 ){
14053     p->cMode = p->mode;
14054     return;
14055   }
14056   zSql = sqlite3_sql(pSql);
14057   if( zSql==0 ) return;
14058   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
14059   if( sqlite3_strnicmp(z, "explain", 7) ){
14060     p->cMode = p->mode;
14061     return;
14062   }
14063 
14064   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
14065     int i;
14066     int iAddr = sqlite3_column_int(pSql, 0);
14067     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
14068 
14069     /* Set p2 to the P2 field of the current opcode. Then, assuming that
14070     ** p2 is an instruction address, set variable p2op to the index of that
14071     ** instruction in the aiIndent[] array. p2 and p2op may be different if
14072     ** the current instruction is part of a sub-program generated by an
14073     ** SQL trigger or foreign key.  */
14074     int p2 = sqlite3_column_int(pSql, 3);
14075     int p2op = (p2 + (iOp-iAddr));
14076 
14077     /* Grow the p->aiIndent array as required */
14078     if( iOp>=nAlloc ){
14079       if( iOp==0 ){
14080         /* Do further verfication that this is explain output.  Abort if
14081         ** it is not */
14082         static const char *explainCols[] = {
14083            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
14084         int jj;
14085         for(jj=0; jj<ArraySize(explainCols); jj++){
14086           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14087             p->cMode = p->mode;
14088             sqlite3_reset(pSql);
14089             return;
14090           }
14091         }
14092       }
14093       nAlloc += 100;
14094       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14095       shell_check_oom(p->aiIndent);
14096       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14097       shell_check_oom(abYield);
14098     }
14099     abYield[iOp] = str_in_array(zOp, azYield);
14100     p->aiIndent[iOp] = 0;
14101     p->nIndent = iOp+1;
14102 
14103     if( str_in_array(zOp, azNext) ){
14104       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14105     }
14106     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
14107      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
14108     ){
14109       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14110     }
14111   }
14112 
14113   p->iIndent = 0;
14114   sqlite3_free(abYield);
14115   sqlite3_reset(pSql);
14116 }
14117 
14118 /*
14119 ** Free the array allocated by explain_data_prepare().
14120 */
14121 static void explain_data_delete(ShellState *p){
14122   sqlite3_free(p->aiIndent);
14123   p->aiIndent = 0;
14124   p->nIndent = 0;
14125   p->iIndent = 0;
14126 }
14127 
14128 /*
14129 ** Disable and restore .wheretrace and .selecttrace settings.
14130 */
14131 static unsigned int savedSelectTrace;
14132 static unsigned int savedWhereTrace;
14133 static void disable_debug_trace_modes(void){
14134   unsigned int zero = 0;
14135   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
14136   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
14137   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
14138   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
14139 }
14140 static void restore_debug_trace_modes(void){
14141   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
14142   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
14143 }
14144 
14145 /* Create the TEMP table used to store parameter bindings */
14146 static void bind_table_init(ShellState *p){
14147   int wrSchema = 0;
14148   int defensiveMode = 0;
14149   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
14150   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
14151   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14152   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14153   sqlite3_exec(p->db,
14154     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
14155     "  key TEXT PRIMARY KEY,\n"
14156     "  value\n"
14157     ") WITHOUT ROWID;",
14158     0, 0, 0);
14159   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14160   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
14161 }
14162 
14163 /*
14164 ** Bind parameters on a prepared statement.
14165 **
14166 ** Parameter bindings are taken from a TEMP table of the form:
14167 **
14168 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
14169 **    WITHOUT ROWID;
14170 **
14171 ** No bindings occur if this table does not exist.  The name of the table
14172 ** begins with "sqlite_" so that it will not collide with ordinary application
14173 ** tables.  The table must be in the TEMP schema.
14174 */
14175 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
14176   int nVar;
14177   int i;
14178   int rc;
14179   sqlite3_stmt *pQ = 0;
14180 
14181   nVar = sqlite3_bind_parameter_count(pStmt);
14182   if( nVar==0 ) return;  /* Nothing to do */
14183   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
14184                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
14185     return; /* Parameter table does not exist */
14186   }
14187   rc = sqlite3_prepare_v2(pArg->db,
14188           "SELECT value FROM temp.sqlite_parameters"
14189           " WHERE key=?1", -1, &pQ, 0);
14190   if( rc || pQ==0 ) return;
14191   for(i=1; i<=nVar; i++){
14192     char zNum[30];
14193     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
14194     if( zVar==0 ){
14195       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
14196       zVar = zNum;
14197     }
14198     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
14199     if( sqlite3_step(pQ)==SQLITE_ROW ){
14200       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
14201     }else{
14202       sqlite3_bind_null(pStmt, i);
14203     }
14204     sqlite3_reset(pQ);
14205   }
14206   sqlite3_finalize(pQ);
14207 }
14208 
14209 /*
14210 ** UTF8 box-drawing characters.  Imagine box lines like this:
14211 **
14212 **           1
14213 **           |
14214 **       4 --+-- 2
14215 **           |
14216 **           3
14217 **
14218 ** Each box characters has between 2 and 4 of the lines leading from
14219 ** the center.  The characters are here identified by the numbers of
14220 ** their corresponding lines.
14221 */
14222 #define BOX_24   "\342\224\200"  /* U+2500 --- */
14223 #define BOX_13   "\342\224\202"  /* U+2502  |  */
14224 #define BOX_23   "\342\224\214"  /* U+250c  ,- */
14225 #define BOX_34   "\342\224\220"  /* U+2510 -,  */
14226 #define BOX_12   "\342\224\224"  /* U+2514  '- */
14227 #define BOX_14   "\342\224\230"  /* U+2518 -'  */
14228 #define BOX_123  "\342\224\234"  /* U+251c  |- */
14229 #define BOX_134  "\342\224\244"  /* U+2524 -|  */
14230 #define BOX_234  "\342\224\254"  /* U+252c -,- */
14231 #define BOX_124  "\342\224\264"  /* U+2534 -'- */
14232 #define BOX_1234 "\342\224\274"  /* U+253c -|- */
14233 
14234 /* Draw horizontal line N characters long using unicode box
14235 ** characters
14236 */
14237 static void print_box_line(FILE *out, int N){
14238   const char zDash[] =
14239       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
14240       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
14241   const int nDash = sizeof(zDash) - 1;
14242   N *= 3;
14243   while( N>nDash ){
14244     utf8_printf(out, zDash);
14245     N -= nDash;
14246   }
14247   utf8_printf(out, "%.*s", N, zDash);
14248 }
14249 
14250 /*
14251 ** Draw a horizontal separator for a MODE_Box table.
14252 */
14253 static void print_box_row_separator(
14254   ShellState *p,
14255   int nArg,
14256   const char *zSep1,
14257   const char *zSep2,
14258   const char *zSep3
14259 ){
14260   int i;
14261   if( nArg>0 ){
14262     utf8_printf(p->out, "%s", zSep1);
14263     print_box_line(p->out, p->actualWidth[0]+2);
14264     for(i=1; i<nArg; i++){
14265       utf8_printf(p->out, "%s", zSep2);
14266       print_box_line(p->out, p->actualWidth[i]+2);
14267     }
14268     utf8_printf(p->out, "%s", zSep3);
14269   }
14270   fputs("\n", p->out);
14271 }
14272 
14273 /*
14274 ** z[] is a line of text that is to be displayed the .mode box or table or
14275 ** similar tabular formats.  z[] might contain control characters such
14276 ** as \n, \t, \f, or \r.
14277 **
14278 ** Compute characters to display on the first line of z[].  Stop at the
14279 ** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
14280 ** from malloc()) of that first line, which caller should free sometime.
14281 ** Write anything to display on the next line into *pzTail.  If this is
14282 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
14283 */
14284 static char *translateForDisplayAndDup(
14285   const unsigned char *z,            /* Input text to be transformed */
14286   const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
14287   int mxWidth,                       /* Max width.  0 means no limit */
14288   u8 bWordWrap                       /* If true, avoid breaking mid-word */
14289 ){
14290   int i;                 /* Input bytes consumed */
14291   int j;                 /* Output bytes generated */
14292   int k;                 /* Input bytes to be displayed */
14293   int n;                 /* Output column number */
14294   unsigned char *zOut;   /* Output text */
14295 
14296   if( z==0 ){
14297     *pzTail = 0;
14298     return 0;
14299   }
14300   if( mxWidth<0 ) mxWidth = -mxWidth;
14301   if( mxWidth==0 ) mxWidth = 1000000;
14302   i = j = n = 0;
14303   while( n<mxWidth ){
14304     if( z[i]>=' ' ){
14305       n++;
14306       do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
14307       continue;
14308     }
14309     if( z[i]=='\t' ){
14310       do{
14311         n++;
14312         j++;
14313       }while( (n&7)!=0 && n<mxWidth );
14314       i++;
14315       continue;
14316     }
14317     break;
14318   }
14319   if( n>=mxWidth && bWordWrap  ){
14320     /* Perhaps try to back up to a better place to break the line */
14321     for(k=i; k>i/2; k--){
14322       if( isspace(z[k-1]) ) break;
14323     }
14324     if( k<=i/2 ){
14325       for(k=i; k>i/2; k--){
14326         if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
14327       }
14328     }
14329     if( k<=i/2 ){
14330       k = i;
14331     }else{
14332       i = k;
14333       while( z[i]==' ' ) i++;
14334     }
14335   }else{
14336     k = i;
14337   }
14338   if( n>=mxWidth && z[i]>=' ' ){
14339    *pzTail = &z[i];
14340   }else if( z[i]=='\r' && z[i+1]=='\n' ){
14341     *pzTail = z[i+2] ? &z[i+2] : 0;
14342   }else if( z[i]==0 || z[i+1]==0 ){
14343     *pzTail = 0;
14344   }else{
14345     *pzTail = &z[i+1];
14346   }
14347   zOut = malloc( j+1 );
14348   shell_check_oom(zOut);
14349   i = j = n = 0;
14350   while( i<k ){
14351     if( z[i]>=' ' ){
14352       n++;
14353       do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
14354       continue;
14355     }
14356     if( z[i]=='\t' ){
14357       do{
14358         n++;
14359         zOut[j++] = ' ';
14360       }while( (n&7)!=0 && n<mxWidth );
14361       i++;
14362       continue;
14363     }
14364     break;
14365   }
14366   zOut[j] = 0;
14367   return (char*)zOut;
14368 }
14369 
14370 /* Extract the value of the i-th current column for pStmt as an SQL literal
14371 ** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
14372 ** the caller.
14373 */
14374 static char *quoted_column(sqlite3_stmt *pStmt, int i){
14375   switch( sqlite3_column_type(pStmt, i) ){
14376     case SQLITE_NULL: {
14377       return sqlite3_mprintf("NULL");
14378     }
14379     case SQLITE_INTEGER:
14380     case SQLITE_FLOAT: {
14381       return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
14382     }
14383     case SQLITE_TEXT: {
14384       return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
14385     }
14386     case SQLITE_BLOB: {
14387       int j;
14388       sqlite3_str *pStr = sqlite3_str_new(0);
14389       const unsigned char *a = sqlite3_column_blob(pStmt,i);
14390       int n = sqlite3_column_bytes(pStmt,i);
14391       sqlite3_str_append(pStr, "x'", 2);
14392       for(j=0; j<n; j++){
14393         sqlite3_str_appendf(pStr, "%02x", a[j]);
14394       }
14395       sqlite3_str_append(pStr, "'", 1);
14396       return sqlite3_str_finish(pStr);
14397     }
14398   }
14399   return 0; /* Not reached */
14400 }
14401 
14402 /*
14403 ** Run a prepared statement and output the result in one of the
14404 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14405 ** or MODE_Box.
14406 **
14407 ** This is different from ordinary exec_prepared_stmt() in that
14408 ** it has to run the entire query and gather the results into memory
14409 ** first, in order to determine column widths, before providing
14410 ** any output.
14411 */
14412 static void exec_prepared_stmt_columnar(
14413   ShellState *p,                        /* Pointer to ShellState */
14414   sqlite3_stmt *pStmt                   /* Statment to run */
14415 ){
14416   sqlite3_int64 nRow = 0;
14417   int nColumn = 0;
14418   char **azData = 0;
14419   sqlite3_int64 nAlloc = 0;
14420   char *abRowDiv = 0;
14421   const unsigned char *uz;
14422   const char *z;
14423   char **azQuoted = 0;
14424   int rc;
14425   sqlite3_int64 i, nData;
14426   int j, nTotal, w, n;
14427   const char *colSep = 0;
14428   const char *rowSep = 0;
14429   const unsigned char **azNextLine = 0;
14430   int bNextLine = 0;
14431   int bMultiLineRowExists = 0;
14432   int bw = p->cmOpts.bWordWrap;
14433   const char *zEmpty = "";
14434   const char *zShowNull = p->nullValue;
14435 
14436   rc = sqlite3_step(pStmt);
14437   if( rc!=SQLITE_ROW ) return;
14438   nColumn = sqlite3_column_count(pStmt);
14439   nAlloc = nColumn*4;
14440   if( nAlloc<=0 ) nAlloc = 1;
14441   azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14442   shell_check_oom(azData);
14443   azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
14444   shell_check_oom((void*)azNextLine);
14445   memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
14446   if( p->cmOpts.bQuote ){
14447     azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
14448     shell_check_oom(azQuoted);
14449     memset(azQuoted, 0, nColumn*sizeof(char*) );
14450   }
14451   abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
14452   shell_check_oom(abRowDiv);
14453   if( nColumn>p->nWidth ){
14454     p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14455     shell_check_oom(p->colWidth);
14456     for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14457     p->nWidth = nColumn;
14458     p->actualWidth = &p->colWidth[nColumn];
14459   }
14460   memset(p->actualWidth, 0, nColumn*sizeof(int));
14461   for(i=0; i<nColumn; i++){
14462     w = p->colWidth[i];
14463     if( w<0 ) w = -w;
14464     p->actualWidth[i] = w;
14465   }
14466   for(i=0; i<nColumn; i++){
14467     const unsigned char *zNotUsed;
14468     int wx = p->colWidth[i];
14469     if( wx==0 ){
14470       wx = p->cmOpts.iWrap;
14471     }
14472     if( wx<0 ) wx = -wx;
14473     uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
14474     azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
14475   }
14476   do{
14477     int useNextLine = bNextLine;
14478     bNextLine = 0;
14479     if( (nRow+2)*nColumn >= nAlloc ){
14480       nAlloc *= 2;
14481       azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14482       shell_check_oom(azData);
14483       abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
14484       shell_check_oom(abRowDiv);
14485     }
14486     abRowDiv[nRow] = 1;
14487     nRow++;
14488     for(i=0; i<nColumn; i++){
14489       int wx = p->colWidth[i];
14490       if( wx==0 ){
14491         wx = p->cmOpts.iWrap;
14492       }
14493       if( wx<0 ) wx = -wx;
14494       if( useNextLine ){
14495         uz = azNextLine[i];
14496         if( uz==0 ) uz = (u8*)zEmpty;
14497       }else if( p->cmOpts.bQuote ){
14498         sqlite3_free(azQuoted[i]);
14499         azQuoted[i] = quoted_column(pStmt,i);
14500         uz = (const unsigned char*)azQuoted[i];
14501       }else{
14502         uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
14503         if( uz==0 ) uz = (u8*)zShowNull;
14504       }
14505       azData[nRow*nColumn + i]
14506         = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
14507       if( azNextLine[i] ){
14508         bNextLine = 1;
14509         abRowDiv[nRow-1] = 0;
14510         bMultiLineRowExists = 1;
14511       }
14512     }
14513   }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
14514   nTotal = nColumn*(nRow+1);
14515   for(i=0; i<nTotal; i++){
14516     z = azData[i];
14517     if( z==0 ) z = (char*)zEmpty;
14518     n = strlenChar(z);
14519     j = i%nColumn;
14520     if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
14521   }
14522   if( seenInterrupt ) goto columnar_end;
14523   if( nColumn==0 ) goto columnar_end;
14524   switch( p->cMode ){
14525     case MODE_Column: {
14526       colSep = "  ";
14527       rowSep = "\n";
14528       if( p->showHeader ){
14529         for(i=0; i<nColumn; i++){
14530           w = p->actualWidth[i];
14531           if( p->colWidth[i]<0 ) w = -w;
14532           utf8_width_print(p->out, w, azData[i]);
14533           fputs(i==nColumn-1?"\n":"  ", p->out);
14534         }
14535         for(i=0; i<nColumn; i++){
14536           print_dashes(p->out, p->actualWidth[i]);
14537           fputs(i==nColumn-1?"\n":"  ", p->out);
14538         }
14539       }
14540       break;
14541     }
14542     case MODE_Table: {
14543       colSep = " | ";
14544       rowSep = " |\n";
14545       print_row_separator(p, nColumn, "+");
14546       fputs("| ", p->out);
14547       for(i=0; i<nColumn; i++){
14548         w = p->actualWidth[i];
14549         n = strlenChar(azData[i]);
14550         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14551         fputs(i==nColumn-1?" |\n":" | ", p->out);
14552       }
14553       print_row_separator(p, nColumn, "+");
14554       break;
14555     }
14556     case MODE_Markdown: {
14557       colSep = " | ";
14558       rowSep = " |\n";
14559       fputs("| ", p->out);
14560       for(i=0; i<nColumn; i++){
14561         w = p->actualWidth[i];
14562         n = strlenChar(azData[i]);
14563         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14564         fputs(i==nColumn-1?" |\n":" | ", p->out);
14565       }
14566       print_row_separator(p, nColumn, "|");
14567       break;
14568     }
14569     case MODE_Box: {
14570       colSep = " " BOX_13 " ";
14571       rowSep = " " BOX_13 "\n";
14572       print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
14573       utf8_printf(p->out, BOX_13 " ");
14574       for(i=0; i<nColumn; i++){
14575         w = p->actualWidth[i];
14576         n = strlenChar(azData[i]);
14577         utf8_printf(p->out, "%*s%s%*s%s",
14578             (w-n)/2, "", azData[i], (w-n+1)/2, "",
14579             i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
14580       }
14581       print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14582       break;
14583     }
14584   }
14585   for(i=nColumn, j=0; i<nTotal; i++, j++){
14586     if( j==0 && p->cMode!=MODE_Column ){
14587       utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
14588     }
14589     z = azData[i];
14590     if( z==0 ) z = p->nullValue;
14591     w = p->actualWidth[j];
14592     if( p->colWidth[j]<0 ) w = -w;
14593     utf8_width_print(p->out, w, z);
14594     if( j==nColumn-1 ){
14595       utf8_printf(p->out, "%s", rowSep);
14596       if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
14597         if( p->cMode==MODE_Table ){
14598           print_row_separator(p, nColumn, "+");
14599         }else if( p->cMode==MODE_Box ){
14600           print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14601         }else if( p->cMode==MODE_Column ){
14602           raw_printf(p->out, "\n");
14603         }
14604       }
14605       j = -1;
14606       if( seenInterrupt ) goto columnar_end;
14607     }else{
14608       utf8_printf(p->out, "%s", colSep);
14609     }
14610   }
14611   if( p->cMode==MODE_Table ){
14612     print_row_separator(p, nColumn, "+");
14613   }else if( p->cMode==MODE_Box ){
14614     print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
14615   }
14616 columnar_end:
14617   if( seenInterrupt ){
14618     utf8_printf(p->out, "Interrupt\n");
14619   }
14620   nData = (nRow+1)*nColumn;
14621   for(i=0; i<nData; i++){
14622     z = azData[i];
14623     if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
14624   }
14625   sqlite3_free(azData);
14626   sqlite3_free((void*)azNextLine);
14627   sqlite3_free(abRowDiv);
14628   if( azQuoted ){
14629     for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
14630     sqlite3_free(azQuoted);
14631   }
14632 }
14633 
14634 /*
14635 ** Run a prepared statement
14636 */
14637 static void exec_prepared_stmt(
14638   ShellState *pArg,                                /* Pointer to ShellState */
14639   sqlite3_stmt *pStmt                              /* Statment to run */
14640 ){
14641   int rc;
14642   sqlite3_uint64 nRow = 0;
14643 
14644   if( pArg->cMode==MODE_Column
14645    || pArg->cMode==MODE_Table
14646    || pArg->cMode==MODE_Box
14647    || pArg->cMode==MODE_Markdown
14648   ){
14649     exec_prepared_stmt_columnar(pArg, pStmt);
14650     return;
14651   }
14652 
14653   /* perform the first step.  this will tell us if we
14654   ** have a result set or not and how wide it is.
14655   */
14656   rc = sqlite3_step(pStmt);
14657   /* if we have a result set... */
14658   if( SQLITE_ROW == rc ){
14659     /* allocate space for col name ptr, value ptr, and type */
14660     int nCol = sqlite3_column_count(pStmt);
14661     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
14662     if( !pData ){
14663       shell_out_of_memory();
14664     }else{
14665       char **azCols = (char **)pData;      /* Names of result columns */
14666       char **azVals = &azCols[nCol];       /* Results */
14667       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
14668       int i, x;
14669       assert(sizeof(int) <= sizeof(char *));
14670       /* save off ptrs to column names */
14671       for(i=0; i<nCol; i++){
14672         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
14673       }
14674       do{
14675         nRow++;
14676         /* extract the data and data types */
14677         for(i=0; i<nCol; i++){
14678           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
14679           if( x==SQLITE_BLOB
14680            && pArg
14681            && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
14682           ){
14683             azVals[i] = "";
14684           }else{
14685             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
14686           }
14687           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
14688             rc = SQLITE_NOMEM;
14689             break; /* from for */
14690           }
14691         } /* end for */
14692 
14693         /* if data and types extracted successfully... */
14694         if( SQLITE_ROW == rc ){
14695           /* call the supplied callback with the result row data */
14696           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
14697             rc = SQLITE_ABORT;
14698           }else{
14699             rc = sqlite3_step(pStmt);
14700           }
14701         }
14702       } while( SQLITE_ROW == rc );
14703       sqlite3_free(pData);
14704       if( pArg->cMode==MODE_Json ){
14705         fputs("]\n", pArg->out);
14706       }else if( pArg->cMode==MODE_Count ){
14707         char zBuf[200];
14708         sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
14709                          nRow, nRow!=1 ? "s" : "");
14710         printf("%s", zBuf);
14711       }
14712     }
14713   }
14714 }
14715 
14716 #ifndef SQLITE_OMIT_VIRTUALTABLE
14717 /*
14718 ** This function is called to process SQL if the previous shell command
14719 ** was ".expert". It passes the SQL in the second argument directly to
14720 ** the sqlite3expert object.
14721 **
14722 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14723 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14724 ** an English language error message. It is the responsibility of the
14725 ** caller to eventually free this buffer using sqlite3_free().
14726 */
14727 static int expertHandleSQL(
14728   ShellState *pState,
14729   const char *zSql,
14730   char **pzErr
14731 ){
14732   assert( pState->expert.pExpert );
14733   assert( pzErr==0 || *pzErr==0 );
14734   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
14735 }
14736 
14737 /*
14738 ** This function is called either to silently clean up the object
14739 ** created by the ".expert" command (if bCancel==1), or to generate a
14740 ** report from it and then clean it up (if bCancel==0).
14741 **
14742 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14743 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14744 ** an English language error message. It is the responsibility of the
14745 ** caller to eventually free this buffer using sqlite3_free().
14746 */
14747 static int expertFinish(
14748   ShellState *pState,
14749   int bCancel,
14750   char **pzErr
14751 ){
14752   int rc = SQLITE_OK;
14753   sqlite3expert *p = pState->expert.pExpert;
14754   assert( p );
14755   assert( bCancel || pzErr==0 || *pzErr==0 );
14756   if( bCancel==0 ){
14757     FILE *out = pState->out;
14758     int bVerbose = pState->expert.bVerbose;
14759 
14760     rc = sqlite3_expert_analyze(p, pzErr);
14761     if( rc==SQLITE_OK ){
14762       int nQuery = sqlite3_expert_count(p);
14763       int i;
14764 
14765       if( bVerbose ){
14766         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
14767         raw_printf(out, "-- Candidates -----------------------------\n");
14768         raw_printf(out, "%s\n", zCand);
14769       }
14770       for(i=0; i<nQuery; i++){
14771         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
14772         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
14773         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
14774         if( zIdx==0 ) zIdx = "(no new indexes)\n";
14775         if( bVerbose ){
14776           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
14777           raw_printf(out, "%s\n\n", zSql);
14778         }
14779         raw_printf(out, "%s\n", zIdx);
14780         raw_printf(out, "%s\n", zEQP);
14781       }
14782     }
14783   }
14784   sqlite3_expert_destroy(p);
14785   pState->expert.pExpert = 0;
14786   return rc;
14787 }
14788 
14789 /*
14790 ** Implementation of ".expert" dot command.
14791 */
14792 static int expertDotCommand(
14793   ShellState *pState,             /* Current shell tool state */
14794   char **azArg,                   /* Array of arguments passed to dot command */
14795   int nArg                        /* Number of entries in azArg[] */
14796 ){
14797   int rc = SQLITE_OK;
14798   char *zErr = 0;
14799   int i;
14800   int iSample = 0;
14801 
14802   assert( pState->expert.pExpert==0 );
14803   memset(&pState->expert, 0, sizeof(ExpertInfo));
14804 
14805   for(i=1; rc==SQLITE_OK && i<nArg; i++){
14806     char *z = azArg[i];
14807     int n;
14808     if( z[0]=='-' && z[1]=='-' ) z++;
14809     n = strlen30(z);
14810     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14811       pState->expert.bVerbose = 1;
14812     }
14813     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14814       if( i==(nArg-1) ){
14815         raw_printf(stderr, "option requires an argument: %s\n", z);
14816         rc = SQLITE_ERROR;
14817       }else{
14818         iSample = (int)integerValue(azArg[++i]);
14819         if( iSample<0 || iSample>100 ){
14820           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
14821           rc = SQLITE_ERROR;
14822         }
14823       }
14824     }
14825     else{
14826       raw_printf(stderr, "unknown option: %s\n", z);
14827       rc = SQLITE_ERROR;
14828     }
14829   }
14830 
14831   if( rc==SQLITE_OK ){
14832     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14833     if( pState->expert.pExpert==0 ){
14834       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
14835       rc = SQLITE_ERROR;
14836     }else{
14837       sqlite3_expert_config(
14838           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14839       );
14840     }
14841   }
14842   sqlite3_free(zErr);
14843 
14844   return rc;
14845 }
14846 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14847 
14848 /*
14849 ** Execute a statement or set of statements.  Print
14850 ** any result rows/columns depending on the current mode
14851 ** set via the supplied callback.
14852 **
14853 ** This is very similar to SQLite's built-in sqlite3_exec()
14854 ** function except it takes a slightly different callback
14855 ** and callback data argument.
14856 */
14857 static int shell_exec(
14858   ShellState *pArg,                         /* Pointer to ShellState */
14859   const char *zSql,                         /* SQL to be evaluated */
14860   char **pzErrMsg                           /* Error msg written here */
14861 ){
14862   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
14863   int rc = SQLITE_OK;             /* Return Code */
14864   int rc2;
14865   const char *zLeftover;          /* Tail of unprocessed SQL */
14866   sqlite3 *db = pArg->db;
14867 
14868   if( pzErrMsg ){
14869     *pzErrMsg = NULL;
14870   }
14871 
14872 #ifndef SQLITE_OMIT_VIRTUALTABLE
14873   if( pArg->expert.pExpert ){
14874     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
14875     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
14876   }
14877 #endif
14878 
14879   while( zSql[0] && (SQLITE_OK == rc) ){
14880     static const char *zStmtSql;
14881     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14882     if( SQLITE_OK != rc ){
14883       if( pzErrMsg ){
14884         *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
14885       }
14886     }else{
14887       if( !pStmt ){
14888         /* this happens for a comment or white-space */
14889         zSql = zLeftover;
14890         while( IsSpace(zSql[0]) ) zSql++;
14891         continue;
14892       }
14893       zStmtSql = sqlite3_sql(pStmt);
14894       if( zStmtSql==0 ) zStmtSql = "";
14895       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
14896 
14897       /* save off the prepared statment handle and reset row count */
14898       if( pArg ){
14899         pArg->pStmt = pStmt;
14900         pArg->cnt = 0;
14901       }
14902 
14903       /* echo the sql statement if echo on */
14904       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
14905         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
14906       }
14907 
14908       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
14909       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
14910         sqlite3_stmt *pExplain;
14911         char *zEQP;
14912         int triggerEQP = 0;
14913         disable_debug_trace_modes();
14914         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14915         if( pArg->autoEQP>=AUTOEQP_trigger ){
14916           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14917         }
14918         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14919         shell_check_oom(zEQP);
14920         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14921         if( rc==SQLITE_OK ){
14922           while( sqlite3_step(pExplain)==SQLITE_ROW ){
14923             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14924             int iEqpId = sqlite3_column_int(pExplain, 0);
14925             int iParentId = sqlite3_column_int(pExplain, 1);
14926             if( zEQPLine==0 ) zEQPLine = "";
14927             if( zEQPLine[0]=='-' ) eqp_render(pArg);
14928             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
14929           }
14930           eqp_render(pArg);
14931         }
14932         sqlite3_finalize(pExplain);
14933         sqlite3_free(zEQP);
14934         if( pArg->autoEQP>=AUTOEQP_full ){
14935           /* Also do an EXPLAIN for ".eqp full" mode */
14936           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14937           shell_check_oom(zEQP);
14938           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14939           if( rc==SQLITE_OK ){
14940             pArg->cMode = MODE_Explain;
14941             explain_data_prepare(pArg, pExplain);
14942             exec_prepared_stmt(pArg, pExplain);
14943             explain_data_delete(pArg);
14944           }
14945           sqlite3_finalize(pExplain);
14946           sqlite3_free(zEQP);
14947         }
14948         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
14949           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
14950           /* Reprepare pStmt before reactiving trace modes */
14951           sqlite3_finalize(pStmt);
14952           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
14953           if( pArg ) pArg->pStmt = pStmt;
14954         }
14955         restore_debug_trace_modes();
14956       }
14957 
14958       if( pArg ){
14959         pArg->cMode = pArg->mode;
14960         if( pArg->autoExplain ){
14961           if( sqlite3_stmt_isexplain(pStmt)==1 ){
14962             pArg->cMode = MODE_Explain;
14963           }
14964           if( sqlite3_stmt_isexplain(pStmt)==2 ){
14965             pArg->cMode = MODE_EQP;
14966           }
14967         }
14968 
14969         /* If the shell is currently in ".explain" mode, gather the extra
14970         ** data required to add indents to the output.*/
14971         if( pArg->cMode==MODE_Explain ){
14972           explain_data_prepare(pArg, pStmt);
14973         }
14974       }
14975 
14976       bind_prepared_stmt(pArg, pStmt);
14977       exec_prepared_stmt(pArg, pStmt);
14978       explain_data_delete(pArg);
14979       eqp_render(pArg);
14980 
14981       /* print usage stats if stats on */
14982       if( pArg && pArg->statsOn ){
14983         display_stats(db, pArg, 0);
14984       }
14985 
14986       /* print loop-counters if required */
14987       if( pArg && pArg->scanstatsOn ){
14988         display_scanstats(db, pArg);
14989       }
14990 
14991       /* Finalize the statement just executed. If this fails, save a
14992       ** copy of the error message. Otherwise, set zSql to point to the
14993       ** next statement to execute. */
14994       rc2 = sqlite3_finalize(pStmt);
14995       if( rc!=SQLITE_NOMEM ) rc = rc2;
14996       if( rc==SQLITE_OK ){
14997         zSql = zLeftover;
14998         while( IsSpace(zSql[0]) ) zSql++;
14999       }else if( pzErrMsg ){
15000         *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
15001       }
15002 
15003       /* clear saved stmt handle */
15004       if( pArg ){
15005         pArg->pStmt = NULL;
15006       }
15007     }
15008   } /* end while */
15009 
15010   return rc;
15011 }
15012 
15013 /*
15014 ** Release memory previously allocated by tableColumnList().
15015 */
15016 static void freeColumnList(char **azCol){
15017   int i;
15018   for(i=1; azCol[i]; i++){
15019     sqlite3_free(azCol[i]);
15020   }
15021   /* azCol[0] is a static string */
15022   sqlite3_free(azCol);
15023 }
15024 
15025 /*
15026 ** Return a list of pointers to strings which are the names of all
15027 ** columns in table zTab.   The memory to hold the names is dynamically
15028 ** allocated and must be released by the caller using a subsequent call
15029 ** to freeColumnList().
15030 **
15031 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
15032 ** value that needs to be preserved, then azCol[0] is filled in with the
15033 ** name of the rowid column.
15034 **
15035 ** The first regular column in the table is azCol[1].  The list is terminated
15036 ** by an entry with azCol[i]==0.
15037 */
15038 static char **tableColumnList(ShellState *p, const char *zTab){
15039   char **azCol = 0;
15040   sqlite3_stmt *pStmt;
15041   char *zSql;
15042   int nCol = 0;
15043   int nAlloc = 0;
15044   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
15045   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
15046   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
15047   int rc;
15048 
15049   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
15050   shell_check_oom(zSql);
15051   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15052   sqlite3_free(zSql);
15053   if( rc ) return 0;
15054   while( sqlite3_step(pStmt)==SQLITE_ROW ){
15055     if( nCol>=nAlloc-2 ){
15056       nAlloc = nAlloc*2 + nCol + 10;
15057       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
15058       shell_check_oom(azCol);
15059     }
15060     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
15061     shell_check_oom(azCol[nCol]);
15062     if( sqlite3_column_int(pStmt, 5) ){
15063       nPK++;
15064       if( nPK==1
15065        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
15066                           "INTEGER")==0
15067       ){
15068         isIPK = 1;
15069       }else{
15070         isIPK = 0;
15071       }
15072     }
15073   }
15074   sqlite3_finalize(pStmt);
15075   if( azCol==0 ) return 0;
15076   azCol[0] = 0;
15077   azCol[nCol+1] = 0;
15078 
15079   /* The decision of whether or not a rowid really needs to be preserved
15080   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
15081   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
15082   ** rowids on tables where the rowid is inaccessible because there are other
15083   ** columns in the table named "rowid", "_rowid_", and "oid".
15084   */
15085   if( preserveRowid && isIPK ){
15086     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
15087     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
15088     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
15089     ** ROWID aliases.  To distinguish these cases, check to see if
15090     ** there is a "pk" entry in "PRAGMA index_list".  There will be
15091     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
15092     */
15093     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
15094                            " WHERE origin='pk'", zTab);
15095     shell_check_oom(zSql);
15096     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15097     sqlite3_free(zSql);
15098     if( rc ){
15099       freeColumnList(azCol);
15100       return 0;
15101     }
15102     rc = sqlite3_step(pStmt);
15103     sqlite3_finalize(pStmt);
15104     preserveRowid = rc==SQLITE_ROW;
15105   }
15106   if( preserveRowid ){
15107     /* Only preserve the rowid if we can find a name to use for the
15108     ** rowid */
15109     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
15110     int i, j;
15111     for(j=0; j<3; j++){
15112       for(i=1; i<=nCol; i++){
15113         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
15114       }
15115       if( i>nCol ){
15116         /* At this point, we know that azRowid[j] is not the name of any
15117         ** ordinary column in the table.  Verify that azRowid[j] is a valid
15118         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
15119         ** tables will fail this last check */
15120         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
15121         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
15122         break;
15123       }
15124     }
15125   }
15126   return azCol;
15127 }
15128 
15129 /*
15130 ** Toggle the reverse_unordered_selects setting.
15131 */
15132 static void toggleSelectOrder(sqlite3 *db){
15133   sqlite3_stmt *pStmt = 0;
15134   int iSetting = 0;
15135   char zStmt[100];
15136   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
15137   if( sqlite3_step(pStmt)==SQLITE_ROW ){
15138     iSetting = sqlite3_column_int(pStmt, 0);
15139   }
15140   sqlite3_finalize(pStmt);
15141   sqlite3_snprintf(sizeof(zStmt), zStmt,
15142        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
15143   sqlite3_exec(db, zStmt, 0, 0, 0);
15144 }
15145 
15146 /*
15147 ** This is a different callback routine used for dumping the database.
15148 ** Each row received by this callback consists of a table name,
15149 ** the table type ("index" or "table") and SQL to create the table.
15150 ** This routine should print text sufficient to recreate the table.
15151 */
15152 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
15153   int rc;
15154   const char *zTable;
15155   const char *zType;
15156   const char *zSql;
15157   ShellState *p = (ShellState *)pArg;
15158   int dataOnly;
15159   int noSys;
15160 
15161   UNUSED_PARAMETER(azNotUsed);
15162   if( nArg!=3 || azArg==0 ) return 0;
15163   zTable = azArg[0];
15164   zType = azArg[1];
15165   zSql = azArg[2];
15166   dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
15167   noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
15168 
15169   if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
15170     if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
15171   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
15172     if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
15173   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
15174     return 0;
15175   }else if( dataOnly ){
15176     /* no-op */
15177   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
15178     char *zIns;
15179     if( !p->writableSchema ){
15180       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
15181       p->writableSchema = 1;
15182     }
15183     zIns = sqlite3_mprintf(
15184        "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
15185        "VALUES('table','%q','%q',0,'%q');",
15186        zTable, zTable, zSql);
15187     shell_check_oom(zIns);
15188     utf8_printf(p->out, "%s\n", zIns);
15189     sqlite3_free(zIns);
15190     return 0;
15191   }else{
15192     printSchemaLine(p->out, zSql, ";\n");
15193   }
15194 
15195   if( strcmp(zType, "table")==0 ){
15196     ShellText sSelect;
15197     ShellText sTable;
15198     char **azCol;
15199     int i;
15200     char *savedDestTable;
15201     int savedMode;
15202 
15203     azCol = tableColumnList(p, zTable);
15204     if( azCol==0 ){
15205       p->nErr++;
15206       return 0;
15207     }
15208 
15209     /* Always quote the table name, even if it appears to be pure ascii,
15210     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
15211     initText(&sTable);
15212     appendText(&sTable, zTable, quoteChar(zTable));
15213     /* If preserving the rowid, add a column list after the table name.
15214     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
15215     ** instead of the usual "INSERT INTO tab VALUES(...)".
15216     */
15217     if( azCol[0] ){
15218       appendText(&sTable, "(", 0);
15219       appendText(&sTable, azCol[0], 0);
15220       for(i=1; azCol[i]; i++){
15221         appendText(&sTable, ",", 0);
15222         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
15223       }
15224       appendText(&sTable, ")", 0);
15225     }
15226 
15227     /* Build an appropriate SELECT statement */
15228     initText(&sSelect);
15229     appendText(&sSelect, "SELECT ", 0);
15230     if( azCol[0] ){
15231       appendText(&sSelect, azCol[0], 0);
15232       appendText(&sSelect, ",", 0);
15233     }
15234     for(i=1; azCol[i]; i++){
15235       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
15236       if( azCol[i+1] ){
15237         appendText(&sSelect, ",", 0);
15238       }
15239     }
15240     freeColumnList(azCol);
15241     appendText(&sSelect, " FROM ", 0);
15242     appendText(&sSelect, zTable, quoteChar(zTable));
15243 
15244     savedDestTable = p->zDestTable;
15245     savedMode = p->mode;
15246     p->zDestTable = sTable.z;
15247     p->mode = p->cMode = MODE_Insert;
15248     rc = shell_exec(p, sSelect.z, 0);
15249     if( (rc&0xff)==SQLITE_CORRUPT ){
15250       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
15251       toggleSelectOrder(p->db);
15252       shell_exec(p, sSelect.z, 0);
15253       toggleSelectOrder(p->db);
15254     }
15255     p->zDestTable = savedDestTable;
15256     p->mode = savedMode;
15257     freeText(&sTable);
15258     freeText(&sSelect);
15259     if( rc ) p->nErr++;
15260   }
15261   return 0;
15262 }
15263 
15264 /*
15265 ** Run zQuery.  Use dump_callback() as the callback routine so that
15266 ** the contents of the query are output as SQL statements.
15267 **
15268 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
15269 ** "ORDER BY rowid DESC" to the end.
15270 */
15271 static int run_schema_dump_query(
15272   ShellState *p,
15273   const char *zQuery
15274 ){
15275   int rc;
15276   char *zErr = 0;
15277   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
15278   if( rc==SQLITE_CORRUPT ){
15279     char *zQ2;
15280     int len = strlen30(zQuery);
15281     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
15282     if( zErr ){
15283       utf8_printf(p->out, "/****** %s ******/\n", zErr);
15284       sqlite3_free(zErr);
15285       zErr = 0;
15286     }
15287     zQ2 = malloc( len+100 );
15288     if( zQ2==0 ) return rc;
15289     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
15290     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
15291     if( rc ){
15292       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
15293     }else{
15294       rc = SQLITE_CORRUPT;
15295     }
15296     sqlite3_free(zErr);
15297     free(zQ2);
15298   }
15299   return rc;
15300 }
15301 
15302 /*
15303 ** Text of help messages.
15304 **
15305 ** The help text for each individual command begins with a line that starts
15306 ** with ".".  Subsequent lines are supplimental information.
15307 **
15308 ** There must be two or more spaces between the end of the command and the
15309 ** start of the description of what that command does.
15310 */
15311 static const char *(azHelp[]) = {
15312 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15313   ".archive ...             Manage SQL archives",
15314   "   Each command must have exactly one of the following options:",
15315   "     -c, --create               Create a new archive",
15316   "     -u, --update               Add or update files with changed mtime",
15317   "     -i, --insert               Like -u but always add even if unchanged",
15318   "     -r, --remove               Remove files from archive",
15319   "     -t, --list                 List contents of archive",
15320   "     -x, --extract              Extract files from archive",
15321   "   Optional arguments:",
15322   "     -v, --verbose              Print each filename as it is processed",
15323   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
15324   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
15325   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
15326   "     -g, --glob                 Use glob matching for names in archive",
15327   "     -n, --dryrun               Show the SQL that would have occurred",
15328   "   Examples:",
15329   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
15330   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
15331   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
15332   "   See also:",
15333   "      http://sqlite.org/cli.html#sqlite_archive_support",
15334 #endif
15335 #ifndef SQLITE_OMIT_AUTHORIZATION
15336   ".auth ON|OFF             Show authorizer callbacks",
15337 #endif
15338   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
15339   "   Options:",
15340   "       --append            Use the appendvfs",
15341   "       --async             Write to FILE without journal and fsync()",
15342   ".bail on|off             Stop after hitting an error.  Default OFF",
15343   ".binary on|off           Turn binary output on or off.  Default OFF",
15344   ".cd DIRECTORY            Change the working directory to DIRECTORY",
15345   ".changes on|off          Show number of rows changed by SQL",
15346   ".check GLOB              Fail if output since .testcase does not match",
15347   ".clone NEWDB             Clone data into NEWDB from the existing database",
15348   ".connection [close] [#]  Open or close an auxiliary database connection",
15349   ".databases               List names and files of attached databases",
15350   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
15351   ".dbinfo ?DB?             Show status information about the database",
15352   ".dump ?OBJECTS?          Render database content as SQL",
15353   "   Options:",
15354   "     --data-only            Output only INSERT statements",
15355   "     --newlines             Allow unescaped newline characters in output",
15356   "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
15357   "     --preserve-rowids      Include ROWID values in the output",
15358   "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
15359   "   Additional LIKE patterns can be given in subsequent arguments",
15360   ".echo on|off             Turn command echo on or off",
15361   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
15362   "   Other Modes:",
15363 #ifdef SQLITE_DEBUG
15364   "      test                  Show raw EXPLAIN QUERY PLAN output",
15365   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15366 #endif
15367   "      trigger               Like \"full\" but also show trigger bytecode",
15368   ".excel                   Display the output of next command in spreadsheet",
15369   "   --bom                   Put a UTF8 byte-order mark on intermediate file",
15370   ".exit ?CODE?             Exit this program with return-code CODE",
15371   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
15372   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
15373   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
15374   "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
15375   "   --help                  Show CMD details",
15376   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
15377   ".headers on|off          Turn display of headers on or off",
15378   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
15379   ".import FILE TABLE       Import data from FILE into TABLE",
15380   "   Options:",
15381   "     --ascii               Use \\037 and \\036 as column and row separators",
15382   "     --csv                 Use , and \\n as column and row separators",
15383   "     --skip N              Skip the first N rows of input",
15384   "     --schema S            Target table to be S.TABLE",
15385   "     -v                    \"Verbose\" - increase auxiliary output",
15386   "   Notes:",
15387   "     *  If TABLE does not exist, it is created.  The first row of input",
15388   "        determines the column names.",
15389   "     *  If neither --csv or --ascii are used, the input mode is derived",
15390   "        from the \".mode\" output mode",
15391   "     *  If FILE begins with \"|\" then it is a command that generates the",
15392   "        input text.",
15393 #ifndef SQLITE_OMIT_TEST_CONTROL
15394   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
15395 #endif
15396   ".indexes ?TABLE?         Show names of indexes",
15397   "                           If TABLE is specified, only show indexes for",
15398   "                           tables matching TABLE using the LIKE operator.",
15399 #ifdef SQLITE_ENABLE_IOTRACE
15400   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
15401 #endif
15402   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
15403   ".lint OPTIONS            Report potential schema issues.",
15404   "     Options:",
15405   "        fkey-indexes     Find missing foreign key indexes",
15406 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15407   ".load FILE ?ENTRY?       Load an extension library",
15408 #endif
15409   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
15410   ".mode MODE ?OPTIONS?     Set output mode",
15411   "   MODE is one of:",
15412   "     ascii       Columns/rows delimited by 0x1F and 0x1E",
15413   "     box         Tables using unicode box-drawing characters",
15414   "     csv         Comma-separated values",
15415   "     column      Output in columns.  (See .width)",
15416   "     html        HTML <table> code",
15417   "     insert      SQL insert statements for TABLE",
15418   "     json        Results in a JSON array",
15419   "     line        One value per line",
15420   "     list        Values delimited by \"|\"",
15421   "     markdown    Markdown table format",
15422   "     qbox        Shorthand for \"box --width 60 --quote\"",
15423   "     quote       Escape answers as for SQL",
15424   "     table       ASCII-art table",
15425   "     tabs        Tab-separated values",
15426   "     tcl         TCL list elements",
15427   "   OPTIONS: (for columnar modes or insert mode):",
15428   "     --wrap N       Wrap output lines to no longer than N characters",
15429   "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
15430   "     --ww           Shorthand for \"--wordwrap 1\"",
15431   "     --quote        Quote output text as SQL literals",
15432   "     --noquote      Do not quote output text",
15433   "     TABLE          The name of SQL table used for \"insert\" mode",
15434   ".nonce STRING            Suspend safe mode for one command if nonce matches",
15435   ".nullvalue STRING        Use STRING in place of NULL values",
15436   ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
15437   "     If FILE begins with '|' then open as a pipe",
15438   "       --bom  Put a UTF8 byte-order mark at the beginning",
15439   "       -e     Send output to the system text editor",
15440   "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
15441   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
15442   "     Options:",
15443   "        --append        Use appendvfs to append database to the end of FILE",
15444 #ifndef SQLITE_OMIT_DESERIALIZE
15445   "        --deserialize   Load into memory using sqlite3_deserialize()",
15446   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
15447   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
15448 #endif
15449   "        --new           Initialize FILE to an empty database",
15450   "        --nofollow      Do not follow symbolic links",
15451   "        --readonly      Open FILE readonly",
15452   "        --zip           FILE is a ZIP archive",
15453   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
15454   "   If FILE begins with '|' then open it as a pipe.",
15455   "   Options:",
15456   "     --bom                 Prefix output with a UTF8 byte-order mark",
15457   "     -e                    Send output to the system text editor",
15458   "     -x                    Send output as CSV to a spreadsheet",
15459   ".parameter CMD ...       Manage SQL parameter bindings",
15460   "   clear                   Erase all bindings",
15461   "   init                    Initialize the TEMP table that holds bindings",
15462   "   list                    List the current parameter bindings",
15463   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
15464   "                           PARAMETER should start with one of: $ : @ ?",
15465   "   unset PARAMETER         Remove PARAMETER from the binding table",
15466   ".print STRING...         Print literal STRING",
15467 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
15468   ".progress N              Invoke progress handler after every N opcodes",
15469   "   --limit N                 Interrupt after N progress callbacks",
15470   "   --once                    Do no more than one progress interrupt",
15471   "   --quiet|-q                No output except at interrupts",
15472   "   --reset                   Reset the count for each input and interrupt",
15473 #endif
15474   ".prompt MAIN CONTINUE    Replace the standard prompts",
15475   ".quit                    Exit this program",
15476   ".read FILE               Read input from FILE or command output",
15477   "    If FILE begins with \"|\", it is a command that generates the input.",
15478 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15479   ".recover                 Recover as much data as possible from corrupt db.",
15480   "   --freelist-corrupt       Assume the freelist is corrupt",
15481   "   --recovery-db NAME       Store recovery metadata in database file NAME",
15482   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
15483   "   --no-rowids              Do not attempt to recover rowid values",
15484   "                            that are not also INTEGER PRIMARY KEYs",
15485 #endif
15486   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
15487   ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
15488   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
15489   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
15490   "   Options:",
15491   "      --indent             Try to pretty-print the schema",
15492   "      --nosys              Omit objects whose names start with \"sqlite_\"",
15493   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
15494   "    Options:",
15495   "       --init               Create a new SELFTEST table",
15496   "       -v                   Verbose output",
15497   ".separator COL ?ROW?     Change the column and row separators",
15498 #if defined(SQLITE_ENABLE_SESSION)
15499   ".session ?NAME? CMD ...  Create or control sessions",
15500   "   Subcommands:",
15501   "     attach TABLE             Attach TABLE",
15502   "     changeset FILE           Write a changeset into FILE",
15503   "     close                    Close one session",
15504   "     enable ?BOOLEAN?         Set or query the enable bit",
15505   "     filter GLOB...           Reject tables matching GLOBs",
15506   "     indirect ?BOOLEAN?       Mark or query the indirect status",
15507   "     isempty                  Query whether the session is empty",
15508   "     list                     List currently open session names",
15509   "     open DB NAME             Open a new session on DB",
15510   "     patchset FILE            Write a patchset into FILE",
15511   "   If ?NAME? is omitted, the first defined session is used.",
15512 #endif
15513   ".sha3sum ...             Compute a SHA3 hash of database content",
15514   "    Options:",
15515   "      --schema              Also hash the sqlite_schema table",
15516   "      --sha3-224            Use the sha3-224 algorithm",
15517   "      --sha3-256            Use the sha3-256 algorithm (default)",
15518   "      --sha3-384            Use the sha3-384 algorithm",
15519   "      --sha3-512            Use the sha3-512 algorithm",
15520   "    Any other argument is a LIKE pattern for tables to hash",
15521 #ifndef SQLITE_NOHAVE_SYSTEM
15522   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
15523 #endif
15524   ".show                    Show the current values for various settings",
15525   ".stats ?ARG?             Show stats or turn stats on or off",
15526   "   off                      Turn off automatic stat display",
15527   "   on                       Turn on automatic stat display",
15528   "   stmt                     Show statement stats",
15529   "   vmstep                   Show the virtual machine step count only",
15530 #ifndef SQLITE_NOHAVE_SYSTEM
15531   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
15532 #endif
15533   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
15534   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
15535   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
15536   "                           Run \".testctrl\" with no arguments for details",
15537   ".timeout MS              Try opening locked tables for MS milliseconds",
15538   ".timer on|off            Turn SQL timer on or off",
15539 #ifndef SQLITE_OMIT_TRACE
15540   ".trace ?OPTIONS?         Output each SQL statement as it is run",
15541   "    FILE                    Send output to FILE",
15542   "    stdout                  Send output to stdout",
15543   "    stderr                  Send output to stderr",
15544   "    off                     Disable tracing",
15545   "    --expanded              Expand query parameters",
15546 #ifdef SQLITE_ENABLE_NORMALIZE
15547   "    --normalized            Normal the SQL statements",
15548 #endif
15549   "    --plain                 Show SQL as it is input",
15550   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
15551   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
15552   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
15553   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
15554 #endif /* SQLITE_OMIT_TRACE */
15555 #ifdef SQLITE_DEBUG
15556   ".unmodule NAME ...       Unregister virtual table modules",
15557   "    --allexcept             Unregister everything except those named",
15558 #endif
15559   ".vfsinfo ?AUX?           Information about the top-level VFS",
15560   ".vfslist                 List all available VFSes",
15561   ".vfsname ?AUX?           Print the name of the VFS stack",
15562   ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
15563   "     Negative values right-justify",
15564 };
15565 
15566 /*
15567 ** Output help text.
15568 **
15569 ** zPattern describes the set of commands for which help text is provided.
15570 ** If zPattern is NULL, then show all commands, but only give a one-line
15571 ** description of each.
15572 **
15573 ** Return the number of matches.
15574 */
15575 static int showHelp(FILE *out, const char *zPattern){
15576   int i = 0;
15577   int j = 0;
15578   int n = 0;
15579   char *zPat;
15580   if( zPattern==0
15581    || zPattern[0]=='0'
15582    || strcmp(zPattern,"-a")==0
15583    || strcmp(zPattern,"-all")==0
15584    || strcmp(zPattern,"--all")==0
15585   ){
15586     /* Show all commands, but only one line per command */
15587     if( zPattern==0 ) zPattern = "";
15588     for(i=0; i<ArraySize(azHelp); i++){
15589       if( azHelp[i][0]=='.' || zPattern[0] ){
15590         utf8_printf(out, "%s\n", azHelp[i]);
15591         n++;
15592       }
15593     }
15594   }else{
15595     /* Look for commands that for which zPattern is an exact prefix */
15596     zPat = sqlite3_mprintf(".%s*", zPattern);
15597     shell_check_oom(zPat);
15598     for(i=0; i<ArraySize(azHelp); i++){
15599       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15600         utf8_printf(out, "%s\n", azHelp[i]);
15601         j = i+1;
15602         n++;
15603       }
15604     }
15605     sqlite3_free(zPat);
15606     if( n ){
15607       if( n==1 ){
15608         /* when zPattern is a prefix of exactly one command, then include the
15609         ** details of that command, which should begin at offset j */
15610         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
15611           utf8_printf(out, "%s\n", azHelp[j]);
15612           j++;
15613         }
15614       }
15615       return n;
15616     }
15617     /* Look for commands that contain zPattern anywhere.  Show the complete
15618     ** text of all commands that match. */
15619     zPat = sqlite3_mprintf("%%%s%%", zPattern);
15620     shell_check_oom(zPat);
15621     for(i=0; i<ArraySize(azHelp); i++){
15622       if( azHelp[i][0]=='.' ) j = i;
15623       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15624         utf8_printf(out, "%s\n", azHelp[j]);
15625         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
15626           j++;
15627           utf8_printf(out, "%s\n", azHelp[j]);
15628         }
15629         i = j;
15630         n++;
15631       }
15632     }
15633     sqlite3_free(zPat);
15634   }
15635   return n;
15636 }
15637 
15638 /* Forward reference */
15639 static int process_input(ShellState *p);
15640 
15641 /*
15642 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
15643 ** and return a pointer to the buffer. The caller is responsible for freeing
15644 ** the memory.
15645 **
15646 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
15647 ** read.
15648 **
15649 ** For convenience, a nul-terminator byte is always appended to the data read
15650 ** from the file before the buffer is returned. This byte is not included in
15651 ** the final value of (*pnByte), if applicable.
15652 **
15653 ** NULL is returned if any error is encountered. The final value of *pnByte
15654 ** is undefined in this case.
15655 */
15656 static char *readFile(const char *zName, int *pnByte){
15657   FILE *in = fopen(zName, "rb");
15658   long nIn;
15659   size_t nRead;
15660   char *pBuf;
15661   if( in==0 ) return 0;
15662   fseek(in, 0, SEEK_END);
15663   nIn = ftell(in);
15664   rewind(in);
15665   pBuf = sqlite3_malloc64( nIn+1 );
15666   if( pBuf==0 ){ fclose(in); return 0; }
15667   nRead = fread(pBuf, nIn, 1, in);
15668   fclose(in);
15669   if( nRead!=1 ){
15670     sqlite3_free(pBuf);
15671     return 0;
15672   }
15673   pBuf[nIn] = 0;
15674   if( pnByte ) *pnByte = nIn;
15675   return pBuf;
15676 }
15677 
15678 #if defined(SQLITE_ENABLE_SESSION)
15679 /*
15680 ** Close a single OpenSession object and release all of its associated
15681 ** resources.
15682 */
15683 static void session_close(OpenSession *pSession){
15684   int i;
15685   sqlite3session_delete(pSession->p);
15686   sqlite3_free(pSession->zName);
15687   for(i=0; i<pSession->nFilter; i++){
15688     sqlite3_free(pSession->azFilter[i]);
15689   }
15690   sqlite3_free(pSession->azFilter);
15691   memset(pSession, 0, sizeof(OpenSession));
15692 }
15693 #endif
15694 
15695 /*
15696 ** Close all OpenSession objects and release all associated resources.
15697 */
15698 #if defined(SQLITE_ENABLE_SESSION)
15699 static void session_close_all(ShellState *p, int i){
15700   int j;
15701   struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
15702   for(j=0; j<pAuxDb->nSession; j++){
15703     session_close(&pAuxDb->aSession[j]);
15704   }
15705   pAuxDb->nSession = 0;
15706 }
15707 #else
15708 # define session_close_all(X,Y)
15709 #endif
15710 
15711 /*
15712 ** Implementation of the xFilter function for an open session.  Omit
15713 ** any tables named by ".session filter" but let all other table through.
15714 */
15715 #if defined(SQLITE_ENABLE_SESSION)
15716 static int session_filter(void *pCtx, const char *zTab){
15717   OpenSession *pSession = (OpenSession*)pCtx;
15718   int i;
15719   for(i=0; i<pSession->nFilter; i++){
15720     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
15721   }
15722   return 1;
15723 }
15724 #endif
15725 
15726 /*
15727 ** Try to deduce the type of file for zName based on its content.  Return
15728 ** one of the SHELL_OPEN_* constants.
15729 **
15730 ** If the file does not exist or is empty but its name looks like a ZIP
15731 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
15732 ** Otherwise, assume an ordinary database regardless of the filename if
15733 ** the type cannot be determined from content.
15734 */
15735 int deduceDatabaseType(const char *zName, int dfltZip){
15736   FILE *f = fopen(zName, "rb");
15737   size_t n;
15738   int rc = SHELL_OPEN_UNSPEC;
15739   char zBuf[100];
15740   if( f==0 ){
15741     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15742        return SHELL_OPEN_ZIPFILE;
15743     }else{
15744        return SHELL_OPEN_NORMAL;
15745     }
15746   }
15747   n = fread(zBuf, 16, 1, f);
15748   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
15749     fclose(f);
15750     return SHELL_OPEN_NORMAL;
15751   }
15752   fseek(f, -25, SEEK_END);
15753   n = fread(zBuf, 25, 1, f);
15754   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
15755     rc = SHELL_OPEN_APPENDVFS;
15756   }else{
15757     fseek(f, -22, SEEK_END);
15758     n = fread(zBuf, 22, 1, f);
15759     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
15760        && zBuf[3]==0x06 ){
15761       rc = SHELL_OPEN_ZIPFILE;
15762     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15763       rc = SHELL_OPEN_ZIPFILE;
15764     }
15765   }
15766   fclose(f);
15767   return rc;
15768 }
15769 
15770 #ifndef SQLITE_OMIT_DESERIALIZE
15771 /*
15772 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15773 ** program.  Read content from the file in p->aAuxDb[].zDbFilename.
15774 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
15775 */
15776 static unsigned char *readHexDb(ShellState *p, int *pnData){
15777   unsigned char *a = 0;
15778   int nLine;
15779   int n = 0;
15780   int pgsz = 0;
15781   int iOffset = 0;
15782   int j, k;
15783   int rc;
15784   FILE *in;
15785   const char *zDbFilename = p->pAuxDb->zDbFilename;
15786   unsigned int x[16];
15787   char zLine[1000];
15788   if( zDbFilename ){
15789     in = fopen(zDbFilename, "r");
15790     if( in==0 ){
15791       utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
15792       return 0;
15793     }
15794     nLine = 0;
15795   }else{
15796     in = p->in;
15797     nLine = p->lineno;
15798     if( in==0 ) in = stdin;
15799   }
15800   *pnData = 0;
15801   nLine++;
15802   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
15803   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
15804   if( rc!=2 ) goto readHexDb_error;
15805   if( n<0 ) goto readHexDb_error;
15806   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15807   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
15808   a = sqlite3_malloc( n ? n : 1 );
15809   shell_check_oom(a);
15810   memset(a, 0, n);
15811   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15812     utf8_printf(stderr, "invalid pagesize\n");
15813     goto readHexDb_error;
15814   }
15815   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
15816     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
15817     if( rc==2 ){
15818       iOffset = k;
15819       continue;
15820     }
15821     if( strncmp(zLine, "| end ", 6)==0 ){
15822       break;
15823     }
15824     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
15825                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
15826                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
15827     if( rc==17 ){
15828       k = iOffset+j;
15829       if( k+16<=n && k>=0 ){
15830         int ii;
15831         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
15832       }
15833     }
15834   }
15835   *pnData = n;
15836   if( in!=p->in ){
15837     fclose(in);
15838   }else{
15839     p->lineno = nLine;
15840   }
15841   return a;
15842 
15843 readHexDb_error:
15844   if( in!=p->in ){
15845     fclose(in);
15846   }else{
15847     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
15848       nLine++;
15849       if(strncmp(zLine, "| end ", 6)==0 ) break;
15850     }
15851     p->lineno = nLine;
15852   }
15853   sqlite3_free(a);
15854   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
15855   return 0;
15856 }
15857 #endif /* SQLITE_OMIT_DESERIALIZE */
15858 
15859 /*
15860 ** Scalar function "shell_int32". The first argument to this function
15861 ** must be a blob. The second a non-negative integer. This function
15862 ** reads and returns a 32-bit big-endian integer from byte
15863 ** offset (4*<arg2>) of the blob.
15864 */
15865 static void shellInt32(
15866   sqlite3_context *context,
15867   int argc,
15868   sqlite3_value **argv
15869 ){
15870   const unsigned char *pBlob;
15871   int nBlob;
15872   int iInt;
15873 
15874   UNUSED_PARAMETER(argc);
15875   nBlob = sqlite3_value_bytes(argv[0]);
15876   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15877   iInt = sqlite3_value_int(argv[1]);
15878 
15879   if( iInt>=0 && (iInt+1)*4<=nBlob ){
15880     const unsigned char *a = &pBlob[iInt*4];
15881     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
15882                        + ((sqlite3_int64)a[1]<<16)
15883                        + ((sqlite3_int64)a[2]<< 8)
15884                        + ((sqlite3_int64)a[3]<< 0);
15885     sqlite3_result_int64(context, iVal);
15886   }
15887 }
15888 
15889 /*
15890 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
15891 ** using "..." with internal double-quote characters doubled.
15892 */
15893 static void shellIdQuote(
15894   sqlite3_context *context,
15895   int argc,
15896   sqlite3_value **argv
15897 ){
15898   const char *zName = (const char*)sqlite3_value_text(argv[0]);
15899   UNUSED_PARAMETER(argc);
15900   if( zName ){
15901     char *z = sqlite3_mprintf("\"%w\"", zName);
15902     sqlite3_result_text(context, z, -1, sqlite3_free);
15903   }
15904 }
15905 
15906 /*
15907 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
15908 */
15909 static void shellUSleepFunc(
15910   sqlite3_context *context,
15911   int argcUnused,
15912   sqlite3_value **argv
15913 ){
15914   int sleep = sqlite3_value_int(argv[0]);
15915   (void)argcUnused;
15916   sqlite3_sleep(sleep/1000);
15917   sqlite3_result_int(context, sleep);
15918 }
15919 
15920 /*
15921 ** Scalar function "shell_escape_crnl" used by the .recover command.
15922 ** The argument passed to this function is the output of built-in
15923 ** function quote(). If the first character of the input is "'",
15924 ** indicating that the value passed to quote() was a text value,
15925 ** then this function searches the input for "\n" and "\r" characters
15926 ** and adds a wrapper similar to the following:
15927 **
15928 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
15929 **
15930 ** Or, if the first character of the input is not "'", then a copy
15931 ** of the input is returned.
15932 */
15933 static void shellEscapeCrnl(
15934   sqlite3_context *context,
15935   int argc,
15936   sqlite3_value **argv
15937 ){
15938   const char *zText = (const char*)sqlite3_value_text(argv[0]);
15939   UNUSED_PARAMETER(argc);
15940   if( zText && zText[0]=='\'' ){
15941     int nText = sqlite3_value_bytes(argv[0]);
15942     int i;
15943     char zBuf1[20];
15944     char zBuf2[20];
15945     const char *zNL = 0;
15946     const char *zCR = 0;
15947     int nCR = 0;
15948     int nNL = 0;
15949 
15950     for(i=0; zText[i]; i++){
15951       if( zNL==0 && zText[i]=='\n' ){
15952         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
15953         nNL = (int)strlen(zNL);
15954       }
15955       if( zCR==0 && zText[i]=='\r' ){
15956         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
15957         nCR = (int)strlen(zCR);
15958       }
15959     }
15960 
15961     if( zNL || zCR ){
15962       int iOut = 0;
15963       i64 nMax = (nNL > nCR) ? nNL : nCR;
15964       i64 nAlloc = nMax * nText + (nMax+64)*2;
15965       char *zOut = (char*)sqlite3_malloc64(nAlloc);
15966       if( zOut==0 ){
15967         sqlite3_result_error_nomem(context);
15968         return;
15969       }
15970 
15971       if( zNL && zCR ){
15972         memcpy(&zOut[iOut], "replace(replace(", 16);
15973         iOut += 16;
15974       }else{
15975         memcpy(&zOut[iOut], "replace(", 8);
15976         iOut += 8;
15977       }
15978       for(i=0; zText[i]; i++){
15979         if( zText[i]=='\n' ){
15980           memcpy(&zOut[iOut], zNL, nNL);
15981           iOut += nNL;
15982         }else if( zText[i]=='\r' ){
15983           memcpy(&zOut[iOut], zCR, nCR);
15984           iOut += nCR;
15985         }else{
15986           zOut[iOut] = zText[i];
15987           iOut++;
15988         }
15989       }
15990 
15991       if( zNL ){
15992         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15993         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
15994         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
15995       }
15996       if( zCR ){
15997         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15998         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
15999         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
16000       }
16001 
16002       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
16003       sqlite3_free(zOut);
16004       return;
16005     }
16006   }
16007 
16008   sqlite3_result_value(context, argv[0]);
16009 }
16010 
16011 /* Flags for open_db().
16012 **
16013 ** The default behavior of open_db() is to exit(1) if the database fails to
16014 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
16015 ** but still returns without calling exit.
16016 **
16017 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
16018 ** ZIP archive if the file does not exist or is empty and its name matches
16019 ** the *.zip pattern.
16020 */
16021 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
16022 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
16023 
16024 /*
16025 ** Make sure the database is open.  If it is not, then open it.  If
16026 ** the database fails to open, print an error message and exit.
16027 */
16028 static void open_db(ShellState *p, int openFlags){
16029   if( p->db==0 ){
16030     const char *zDbFilename = p->pAuxDb->zDbFilename;
16031     if( p->openMode==SHELL_OPEN_UNSPEC ){
16032       if( zDbFilename==0 || zDbFilename[0]==0 ){
16033         p->openMode = SHELL_OPEN_NORMAL;
16034       }else{
16035         p->openMode = (u8)deduceDatabaseType(zDbFilename,
16036                              (openFlags & OPEN_DB_ZIPFILE)!=0);
16037       }
16038     }
16039     switch( p->openMode ){
16040       case SHELL_OPEN_APPENDVFS: {
16041         sqlite3_open_v2(zDbFilename, &p->db,
16042            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
16043         break;
16044       }
16045       case SHELL_OPEN_HEXDB:
16046       case SHELL_OPEN_DESERIALIZE: {
16047         sqlite3_open(0, &p->db);
16048         break;
16049       }
16050       case SHELL_OPEN_ZIPFILE: {
16051         sqlite3_open(":memory:", &p->db);
16052         break;
16053       }
16054       case SHELL_OPEN_READONLY: {
16055         sqlite3_open_v2(zDbFilename, &p->db,
16056             SQLITE_OPEN_READONLY|p->openFlags, 0);
16057         break;
16058       }
16059       case SHELL_OPEN_UNSPEC:
16060       case SHELL_OPEN_NORMAL: {
16061         sqlite3_open_v2(zDbFilename, &p->db,
16062            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
16063         break;
16064       }
16065     }
16066     globalDb = p->db;
16067     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
16068       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
16069           zDbFilename, sqlite3_errmsg(p->db));
16070       if( openFlags & OPEN_DB_KEEPALIVE ){
16071         sqlite3_open(":memory:", &p->db);
16072         return;
16073       }
16074       exit(1);
16075     }
16076 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16077     sqlite3_enable_load_extension(p->db, 1);
16078 #endif
16079     sqlite3_fileio_init(p->db, 0, 0);
16080     sqlite3_shathree_init(p->db, 0, 0);
16081     sqlite3_completion_init(p->db, 0, 0);
16082     sqlite3_uint_init(p->db, 0, 0);
16083     sqlite3_decimal_init(p->db, 0, 0);
16084     sqlite3_regexp_init(p->db, 0, 0);
16085     sqlite3_ieee_init(p->db, 0, 0);
16086     sqlite3_series_init(p->db, 0, 0);
16087 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16088     sqlite3_dbdata_init(p->db, 0, 0);
16089 #endif
16090 #ifdef SQLITE_HAVE_ZLIB
16091     sqlite3_zipfile_init(p->db, 0, 0);
16092     sqlite3_sqlar_init(p->db, 0, 0);
16093 #endif
16094     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
16095                             shellAddSchemaName, 0, 0);
16096     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
16097                             shellModuleSchema, 0, 0);
16098     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
16099                             shellPutsFunc, 0, 0);
16100     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
16101                             shellEscapeCrnl, 0, 0);
16102     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
16103                             shellInt32, 0, 0);
16104     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
16105                             shellIdQuote, 0, 0);
16106     sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
16107                             shellUSleepFunc, 0, 0);
16108 #ifndef SQLITE_NOHAVE_SYSTEM
16109     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
16110                             editFunc, 0, 0);
16111     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
16112                             editFunc, 0, 0);
16113 #endif
16114     if( p->openMode==SHELL_OPEN_ZIPFILE ){
16115       char *zSql = sqlite3_mprintf(
16116          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
16117       shell_check_oom(zSql);
16118       sqlite3_exec(p->db, zSql, 0, 0, 0);
16119       sqlite3_free(zSql);
16120     }
16121 #ifndef SQLITE_OMIT_DESERIALIZE
16122     else
16123     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
16124       int rc;
16125       int nData = 0;
16126       unsigned char *aData;
16127       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
16128         aData = (unsigned char*)readFile(zDbFilename, &nData);
16129       }else{
16130         aData = readHexDb(p, &nData);
16131         if( aData==0 ){
16132           return;
16133         }
16134       }
16135       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
16136                    SQLITE_DESERIALIZE_RESIZEABLE |
16137                    SQLITE_DESERIALIZE_FREEONCLOSE);
16138       if( rc ){
16139         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
16140       }
16141       if( p->szMax>0 ){
16142         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
16143       }
16144     }
16145 #endif
16146   }
16147   if( p->bSafeModePersist && p->db!=0 ){
16148     sqlite3_set_authorizer(p->db, safeModeAuth, p);
16149   }
16150 }
16151 
16152 /*
16153 ** Attempt to close the databaes connection.  Report errors.
16154 */
16155 void close_db(sqlite3 *db){
16156   int rc = sqlite3_close(db);
16157   if( rc ){
16158     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
16159         rc, sqlite3_errmsg(db));
16160   }
16161 }
16162 
16163 #if HAVE_READLINE || HAVE_EDITLINE
16164 /*
16165 ** Readline completion callbacks
16166 */
16167 static char *readline_completion_generator(const char *text, int state){
16168   static sqlite3_stmt *pStmt = 0;
16169   char *zRet;
16170   if( state==0 ){
16171     char *zSql;
16172     sqlite3_finalize(pStmt);
16173     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
16174                            "  FROM completion(%Q) ORDER BY 1", text);
16175     shell_check_oom(zSql);
16176     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
16177     sqlite3_free(zSql);
16178   }
16179   if( sqlite3_step(pStmt)==SQLITE_ROW ){
16180     const char *z = (const char*)sqlite3_column_text(pStmt,0);
16181     zRet = z ? strdup(z) : 0;
16182   }else{
16183     sqlite3_finalize(pStmt);
16184     pStmt = 0;
16185     zRet = 0;
16186   }
16187   return zRet;
16188 }
16189 static char **readline_completion(const char *zText, int iStart, int iEnd){
16190   rl_attempted_completion_over = 1;
16191   return rl_completion_matches(zText, readline_completion_generator);
16192 }
16193 
16194 #elif HAVE_LINENOISE
16195 /*
16196 ** Linenoise completion callback
16197 */
16198 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
16199   int nLine = strlen30(zLine);
16200   int i, iStart;
16201   sqlite3_stmt *pStmt = 0;
16202   char *zSql;
16203   char zBuf[1000];
16204 
16205   if( nLine>sizeof(zBuf)-30 ) return;
16206   if( zLine[0]=='.' || zLine[0]=='#') return;
16207   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
16208   if( i==nLine-1 ) return;
16209   iStart = i+1;
16210   memcpy(zBuf, zLine, iStart);
16211   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
16212                          "  FROM completion(%Q,%Q) ORDER BY 1",
16213                          &zLine[iStart], zLine);
16214   shell_check_oom(zSql);
16215   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
16216   sqlite3_free(zSql);
16217   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
16218   while( sqlite3_step(pStmt)==SQLITE_ROW ){
16219     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
16220     int nCompletion = sqlite3_column_bytes(pStmt, 0);
16221     if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
16222       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
16223       linenoiseAddCompletion(lc, zBuf);
16224     }
16225   }
16226   sqlite3_finalize(pStmt);
16227 }
16228 #endif
16229 
16230 /*
16231 ** Do C-language style dequoting.
16232 **
16233 **    \a    -> alarm
16234 **    \b    -> backspace
16235 **    \t    -> tab
16236 **    \n    -> newline
16237 **    \v    -> vertical tab
16238 **    \f    -> form feed
16239 **    \r    -> carriage return
16240 **    \s    -> space
16241 **    \"    -> "
16242 **    \'    -> '
16243 **    \\    -> backslash
16244 **    \NNN  -> ascii character NNN in octal
16245 */
16246 static void resolve_backslashes(char *z){
16247   int i, j;
16248   char c;
16249   while( *z && *z!='\\' ) z++;
16250   for(i=j=0; (c = z[i])!=0; i++, j++){
16251     if( c=='\\' && z[i+1]!=0 ){
16252       c = z[++i];
16253       if( c=='a' ){
16254         c = '\a';
16255       }else if( c=='b' ){
16256         c = '\b';
16257       }else if( c=='t' ){
16258         c = '\t';
16259       }else if( c=='n' ){
16260         c = '\n';
16261       }else if( c=='v' ){
16262         c = '\v';
16263       }else if( c=='f' ){
16264         c = '\f';
16265       }else if( c=='r' ){
16266         c = '\r';
16267       }else if( c=='"' ){
16268         c = '"';
16269       }else if( c=='\'' ){
16270         c = '\'';
16271       }else if( c=='\\' ){
16272         c = '\\';
16273       }else if( c>='0' && c<='7' ){
16274         c -= '0';
16275         if( z[i+1]>='0' && z[i+1]<='7' ){
16276           i++;
16277           c = (c<<3) + z[i] - '0';
16278           if( z[i+1]>='0' && z[i+1]<='7' ){
16279             i++;
16280             c = (c<<3) + z[i] - '0';
16281           }
16282         }
16283       }
16284     }
16285     z[j] = c;
16286   }
16287   if( j<i ) z[j] = 0;
16288 }
16289 
16290 /*
16291 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
16292 ** for TRUE and FALSE.  Return the integer value if appropriate.
16293 */
16294 static int booleanValue(const char *zArg){
16295   int i;
16296   if( zArg[0]=='0' && zArg[1]=='x' ){
16297     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
16298   }else{
16299     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
16300   }
16301   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
16302   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
16303     return 1;
16304   }
16305   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
16306     return 0;
16307   }
16308   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
16309           zArg);
16310   return 0;
16311 }
16312 
16313 /*
16314 ** Set or clear a shell flag according to a boolean value.
16315 */
16316 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
16317   if( booleanValue(zArg) ){
16318     ShellSetFlag(p, mFlag);
16319   }else{
16320     ShellClearFlag(p, mFlag);
16321   }
16322 }
16323 
16324 /*
16325 ** Close an output file, assuming it is not stderr or stdout
16326 */
16327 static void output_file_close(FILE *f){
16328   if( f && f!=stdout && f!=stderr ) fclose(f);
16329 }
16330 
16331 /*
16332 ** Try to open an output file.   The names "stdout" and "stderr" are
16333 ** recognized and do the right thing.  NULL is returned if the output
16334 ** filename is "off".
16335 */
16336 static FILE *output_file_open(const char *zFile, int bTextMode){
16337   FILE *f;
16338   if( strcmp(zFile,"stdout")==0 ){
16339     f = stdout;
16340   }else if( strcmp(zFile, "stderr")==0 ){
16341     f = stderr;
16342   }else if( strcmp(zFile, "off")==0 ){
16343     f = 0;
16344   }else{
16345     f = fopen(zFile, bTextMode ? "w" : "wb");
16346     if( f==0 ){
16347       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16348     }
16349   }
16350   return f;
16351 }
16352 
16353 #ifndef SQLITE_OMIT_TRACE
16354 /*
16355 ** A routine for handling output from sqlite3_trace().
16356 */
16357 static int sql_trace_callback(
16358   unsigned mType,         /* The trace type */
16359   void *pArg,             /* The ShellState pointer */
16360   void *pP,               /* Usually a pointer to sqlite_stmt */
16361   void *pX                /* Auxiliary output */
16362 ){
16363   ShellState *p = (ShellState*)pArg;
16364   sqlite3_stmt *pStmt;
16365   const char *zSql;
16366   int nSql;
16367   if( p->traceOut==0 ) return 0;
16368   if( mType==SQLITE_TRACE_CLOSE ){
16369     utf8_printf(p->traceOut, "-- closing database connection\n");
16370     return 0;
16371   }
16372   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
16373     zSql = (const char*)pX;
16374   }else{
16375     pStmt = (sqlite3_stmt*)pP;
16376     switch( p->eTraceType ){
16377       case SHELL_TRACE_EXPANDED: {
16378         zSql = sqlite3_expanded_sql(pStmt);
16379         break;
16380       }
16381 #ifdef SQLITE_ENABLE_NORMALIZE
16382       case SHELL_TRACE_NORMALIZED: {
16383         zSql = sqlite3_normalized_sql(pStmt);
16384         break;
16385       }
16386 #endif
16387       default: {
16388         zSql = sqlite3_sql(pStmt);
16389         break;
16390       }
16391     }
16392   }
16393   if( zSql==0 ) return 0;
16394   nSql = strlen30(zSql);
16395   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
16396   switch( mType ){
16397     case SQLITE_TRACE_ROW:
16398     case SQLITE_TRACE_STMT: {
16399       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
16400       break;
16401     }
16402     case SQLITE_TRACE_PROFILE: {
16403       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16404       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
16405       break;
16406     }
16407   }
16408   return 0;
16409 }
16410 #endif
16411 
16412 /*
16413 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
16414 ** a useful spot to set a debugger breakpoint.
16415 */
16416 static void test_breakpoint(void){
16417   static int nCall = 0;
16418   nCall++;
16419 }
16420 
16421 /*
16422 ** An object used to read a CSV and other files for import.
16423 */
16424 typedef struct ImportCtx ImportCtx;
16425 struct ImportCtx {
16426   const char *zFile;  /* Name of the input file */
16427   FILE *in;           /* Read the CSV text from this input stream */
16428   int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
16429   char *z;            /* Accumulated text for a field */
16430   int n;              /* Number of bytes in z */
16431   int nAlloc;         /* Space allocated for z[] */
16432   int nLine;          /* Current line number */
16433   int nRow;           /* Number of rows imported */
16434   int nErr;           /* Number of errors encountered */
16435   int bNotFirst;      /* True if one or more bytes already read */
16436   int cTerm;          /* Character that terminated the most recent field */
16437   int cColSep;        /* The column separator character.  (Usually ",") */
16438   int cRowSep;        /* The row separator character.  (Usually "\n") */
16439 };
16440 
16441 /* Clean up resourced used by an ImportCtx */
16442 static void import_cleanup(ImportCtx *p){
16443   if( p->in!=0 && p->xCloser!=0 ){
16444     p->xCloser(p->in);
16445     p->in = 0;
16446   }
16447   sqlite3_free(p->z);
16448   p->z = 0;
16449 }
16450 
16451 /* Append a single byte to z[] */
16452 static void import_append_char(ImportCtx *p, int c){
16453   if( p->n+1>=p->nAlloc ){
16454     p->nAlloc += p->nAlloc + 100;
16455     p->z = sqlite3_realloc64(p->z, p->nAlloc);
16456     shell_check_oom(p->z);
16457   }
16458   p->z[p->n++] = (char)c;
16459 }
16460 
16461 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
16462 ** with the option of having a separator other than ",".
16463 **
16464 **   +  Input comes from p->in.
16465 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
16466 **      from sqlite3_malloc64().
16467 **   +  Use p->cSep as the column separator.  The default is ",".
16468 **   +  Use p->rSep as the row separator.  The default is "\n".
16469 **   +  Keep track of the line number in p->nLine.
16470 **   +  Store the character that terminates the field in p->cTerm.  Store
16471 **      EOF on end-of-file.
16472 **   +  Report syntax errors on stderr
16473 */
16474 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
16475   int c;
16476   int cSep = p->cColSep;
16477   int rSep = p->cRowSep;
16478   p->n = 0;
16479   c = fgetc(p->in);
16480   if( c==EOF || seenInterrupt ){
16481     p->cTerm = EOF;
16482     return 0;
16483   }
16484   if( c=='"' ){
16485     int pc, ppc;
16486     int startLine = p->nLine;
16487     int cQuote = c;
16488     pc = ppc = 0;
16489     while( 1 ){
16490       c = fgetc(p->in);
16491       if( c==rSep ) p->nLine++;
16492       if( c==cQuote ){
16493         if( pc==cQuote ){
16494           pc = 0;
16495           continue;
16496         }
16497       }
16498       if( (c==cSep && pc==cQuote)
16499        || (c==rSep && pc==cQuote)
16500        || (c==rSep && pc=='\r' && ppc==cQuote)
16501        || (c==EOF && pc==cQuote)
16502       ){
16503         do{ p->n--; }while( p->z[p->n]!=cQuote );
16504         p->cTerm = c;
16505         break;
16506       }
16507       if( pc==cQuote && c!='\r' ){
16508         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
16509                 p->zFile, p->nLine, cQuote);
16510       }
16511       if( c==EOF ){
16512         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
16513                 p->zFile, startLine, cQuote);
16514         p->cTerm = c;
16515         break;
16516       }
16517       import_append_char(p, c);
16518       ppc = pc;
16519       pc = c;
16520     }
16521   }else{
16522     /* If this is the first field being parsed and it begins with the
16523     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
16524     if( (c&0xff)==0xef && p->bNotFirst==0 ){
16525       import_append_char(p, c);
16526       c = fgetc(p->in);
16527       if( (c&0xff)==0xbb ){
16528         import_append_char(p, c);
16529         c = fgetc(p->in);
16530         if( (c&0xff)==0xbf ){
16531           p->bNotFirst = 1;
16532           p->n = 0;
16533           return csv_read_one_field(p);
16534         }
16535       }
16536     }
16537     while( c!=EOF && c!=cSep && c!=rSep ){
16538       import_append_char(p, c);
16539       c = fgetc(p->in);
16540     }
16541     if( c==rSep ){
16542       p->nLine++;
16543       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
16544     }
16545     p->cTerm = c;
16546   }
16547   if( p->z ) p->z[p->n] = 0;
16548   p->bNotFirst = 1;
16549   return p->z;
16550 }
16551 
16552 /* Read a single field of ASCII delimited text.
16553 **
16554 **   +  Input comes from p->in.
16555 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
16556 **      from sqlite3_malloc64().
16557 **   +  Use p->cSep as the column separator.  The default is "\x1F".
16558 **   +  Use p->rSep as the row separator.  The default is "\x1E".
16559 **   +  Keep track of the row number in p->nLine.
16560 **   +  Store the character that terminates the field in p->cTerm.  Store
16561 **      EOF on end-of-file.
16562 **   +  Report syntax errors on stderr
16563 */
16564 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
16565   int c;
16566   int cSep = p->cColSep;
16567   int rSep = p->cRowSep;
16568   p->n = 0;
16569   c = fgetc(p->in);
16570   if( c==EOF || seenInterrupt ){
16571     p->cTerm = EOF;
16572     return 0;
16573   }
16574   while( c!=EOF && c!=cSep && c!=rSep ){
16575     import_append_char(p, c);
16576     c = fgetc(p->in);
16577   }
16578   if( c==rSep ){
16579     p->nLine++;
16580   }
16581   p->cTerm = c;
16582   if( p->z ) p->z[p->n] = 0;
16583   return p->z;
16584 }
16585 
16586 /*
16587 ** Try to transfer data for table zTable.  If an error is seen while
16588 ** moving forward, try to go backwards.  The backwards movement won't
16589 ** work for WITHOUT ROWID tables.
16590 */
16591 static void tryToCloneData(
16592   ShellState *p,
16593   sqlite3 *newDb,
16594   const char *zTable
16595 ){
16596   sqlite3_stmt *pQuery = 0;
16597   sqlite3_stmt *pInsert = 0;
16598   char *zQuery = 0;
16599   char *zInsert = 0;
16600   int rc;
16601   int i, j, n;
16602   int nTable = strlen30(zTable);
16603   int k = 0;
16604   int cnt = 0;
16605   const int spinRate = 10000;
16606 
16607   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16608   shell_check_oom(zQuery);
16609   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16610   if( rc ){
16611     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16612             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16613             zQuery);
16614     goto end_data_xfer;
16615   }
16616   n = sqlite3_column_count(pQuery);
16617   zInsert = sqlite3_malloc64(200 + nTable + n*3);
16618   shell_check_oom(zInsert);
16619   sqlite3_snprintf(200+nTable,zInsert,
16620                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16621   i = strlen30(zInsert);
16622   for(j=1; j<n; j++){
16623     memcpy(zInsert+i, ",?", 2);
16624     i += 2;
16625   }
16626   memcpy(zInsert+i, ");", 3);
16627   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
16628   if( rc ){
16629     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16630             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
16631             zQuery);
16632     goto end_data_xfer;
16633   }
16634   for(k=0; k<2; k++){
16635     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16636       for(i=0; i<n; i++){
16637         switch( sqlite3_column_type(pQuery, i) ){
16638           case SQLITE_NULL: {
16639             sqlite3_bind_null(pInsert, i+1);
16640             break;
16641           }
16642           case SQLITE_INTEGER: {
16643             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
16644             break;
16645           }
16646           case SQLITE_FLOAT: {
16647             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
16648             break;
16649           }
16650           case SQLITE_TEXT: {
16651             sqlite3_bind_text(pInsert, i+1,
16652                              (const char*)sqlite3_column_text(pQuery,i),
16653                              -1, SQLITE_STATIC);
16654             break;
16655           }
16656           case SQLITE_BLOB: {
16657             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
16658                                             sqlite3_column_bytes(pQuery,i),
16659                                             SQLITE_STATIC);
16660             break;
16661           }
16662         }
16663       } /* End for */
16664       rc = sqlite3_step(pInsert);
16665       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
16666         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
16667                         sqlite3_errmsg(newDb));
16668       }
16669       sqlite3_reset(pInsert);
16670       cnt++;
16671       if( (cnt%spinRate)==0 ){
16672         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
16673         fflush(stdout);
16674       }
16675     } /* End while */
16676     if( rc==SQLITE_DONE ) break;
16677     sqlite3_finalize(pQuery);
16678     sqlite3_free(zQuery);
16679     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16680                              zTable);
16681     shell_check_oom(zQuery);
16682     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16683     if( rc ){
16684       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16685       break;
16686     }
16687   } /* End for(k=0...) */
16688 
16689 end_data_xfer:
16690   sqlite3_finalize(pQuery);
16691   sqlite3_finalize(pInsert);
16692   sqlite3_free(zQuery);
16693   sqlite3_free(zInsert);
16694 }
16695 
16696 
16697 /*
16698 ** Try to transfer all rows of the schema that match zWhere.  For
16699 ** each row, invoke xForEach() on the object defined by that row.
16700 ** If an error is encountered while moving forward through the
16701 ** sqlite_schema table, try again moving backwards.
16702 */
16703 static void tryToCloneSchema(
16704   ShellState *p,
16705   sqlite3 *newDb,
16706   const char *zWhere,
16707   void (*xForEach)(ShellState*,sqlite3*,const char*)
16708 ){
16709   sqlite3_stmt *pQuery = 0;
16710   char *zQuery = 0;
16711   int rc;
16712   const unsigned char *zName;
16713   const unsigned char *zSql;
16714   char *zErrMsg = 0;
16715 
16716   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16717                            " WHERE %s", zWhere);
16718   shell_check_oom(zQuery);
16719   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16720   if( rc ){
16721     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16722                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16723                     zQuery);
16724     goto end_schema_xfer;
16725   }
16726   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16727     zName = sqlite3_column_text(pQuery, 0);
16728     zSql = sqlite3_column_text(pQuery, 1);
16729     if( zName==0 || zSql==0 ) continue;
16730     printf("%s... ", zName); fflush(stdout);
16731     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16732     if( zErrMsg ){
16733       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16734       sqlite3_free(zErrMsg);
16735       zErrMsg = 0;
16736     }
16737     if( xForEach ){
16738       xForEach(p, newDb, (const char*)zName);
16739     }
16740     printf("done\n");
16741   }
16742   if( rc!=SQLITE_DONE ){
16743     sqlite3_finalize(pQuery);
16744     sqlite3_free(zQuery);
16745     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16746                              " WHERE %s ORDER BY rowid DESC", zWhere);
16747     shell_check_oom(zQuery);
16748     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16749     if( rc ){
16750       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16751                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16752                       zQuery);
16753       goto end_schema_xfer;
16754     }
16755     while( sqlite3_step(pQuery)==SQLITE_ROW ){
16756       zName = sqlite3_column_text(pQuery, 0);
16757       zSql = sqlite3_column_text(pQuery, 1);
16758       if( zName==0 || zSql==0 ) continue;
16759       printf("%s... ", zName); fflush(stdout);
16760       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16761       if( zErrMsg ){
16762         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16763         sqlite3_free(zErrMsg);
16764         zErrMsg = 0;
16765       }
16766       if( xForEach ){
16767         xForEach(p, newDb, (const char*)zName);
16768       }
16769       printf("done\n");
16770     }
16771   }
16772 end_schema_xfer:
16773   sqlite3_finalize(pQuery);
16774   sqlite3_free(zQuery);
16775 }
16776 
16777 /*
16778 ** Open a new database file named "zNewDb".  Try to recover as much information
16779 ** as possible out of the main database (which might be corrupt) and write it
16780 ** into zNewDb.
16781 */
16782 static void tryToClone(ShellState *p, const char *zNewDb){
16783   int rc;
16784   sqlite3 *newDb = 0;
16785   if( access(zNewDb,0)==0 ){
16786     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
16787     return;
16788   }
16789   rc = sqlite3_open(zNewDb, &newDb);
16790   if( rc ){
16791     utf8_printf(stderr, "Cannot create output database: %s\n",
16792             sqlite3_errmsg(newDb));
16793   }else{
16794     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
16795     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
16796     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
16797     tryToCloneSchema(p, newDb, "type!='table'", 0);
16798     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
16799     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16800   }
16801   close_db(newDb);
16802 }
16803 
16804 /*
16805 ** Change the output file back to stdout.
16806 **
16807 ** If the p->doXdgOpen flag is set, that means the output was being
16808 ** redirected to a temporary file named by p->zTempFile.  In that case,
16809 ** launch start/open/xdg-open on that temporary file.
16810 */
16811 static void output_reset(ShellState *p){
16812   if( p->outfile[0]=='|' ){
16813 #ifndef SQLITE_OMIT_POPEN
16814     pclose(p->out);
16815 #endif
16816   }else{
16817     output_file_close(p->out);
16818 #ifndef SQLITE_NOHAVE_SYSTEM
16819     if( p->doXdgOpen ){
16820       const char *zXdgOpenCmd =
16821 #if defined(_WIN32)
16822       "start";
16823 #elif defined(__APPLE__)
16824       "open";
16825 #else
16826       "xdg-open";
16827 #endif
16828       char *zCmd;
16829       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
16830       if( system(zCmd) ){
16831         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
16832       }else{
16833         /* Give the start/open/xdg-open command some time to get
16834         ** going before we continue, and potential delete the
16835         ** p->zTempFile data file out from under it */
16836         sqlite3_sleep(2000);
16837       }
16838       sqlite3_free(zCmd);
16839       outputModePop(p);
16840       p->doXdgOpen = 0;
16841     }
16842 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
16843   }
16844   p->outfile[0] = 0;
16845   p->out = stdout;
16846 }
16847 
16848 /*
16849 ** Run an SQL command and return the single integer result.
16850 */
16851 static int db_int(sqlite3 *db, const char *zSql){
16852   sqlite3_stmt *pStmt;
16853   int res = 0;
16854   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
16855   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16856     res = sqlite3_column_int(pStmt,0);
16857   }
16858   sqlite3_finalize(pStmt);
16859   return res;
16860 }
16861 
16862 /*
16863 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16864 */
16865 static unsigned int get2byteInt(unsigned char *a){
16866   return (a[0]<<8) + a[1];
16867 }
16868 static unsigned int get4byteInt(unsigned char *a){
16869   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
16870 }
16871 
16872 /*
16873 ** Implementation of the ".dbinfo" command.
16874 **
16875 ** Return 1 on error, 2 to exit, and 0 otherwise.
16876 */
16877 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
16878   static const struct { const char *zName; int ofst; } aField[] = {
16879      { "file change counter:",  24  },
16880      { "database page count:",  28  },
16881      { "freelist page count:",  36  },
16882      { "schema cookie:",        40  },
16883      { "schema format:",        44  },
16884      { "default cache size:",   48  },
16885      { "autovacuum top root:",  52  },
16886      { "incremental vacuum:",   64  },
16887      { "text encoding:",        56  },
16888      { "user version:",         60  },
16889      { "application id:",       68  },
16890      { "software version:",     96  },
16891   };
16892   static const struct { const char *zName; const char *zSql; } aQuery[] = {
16893      { "number of tables:",
16894        "SELECT count(*) FROM %s WHERE type='table'" },
16895      { "number of indexes:",
16896        "SELECT count(*) FROM %s WHERE type='index'" },
16897      { "number of triggers:",
16898        "SELECT count(*) FROM %s WHERE type='trigger'" },
16899      { "number of views:",
16900        "SELECT count(*) FROM %s WHERE type='view'" },
16901      { "schema size:",
16902        "SELECT total(length(sql)) FROM %s" },
16903   };
16904   int i, rc;
16905   unsigned iDataVersion;
16906   char *zSchemaTab;
16907   char *zDb = nArg>=2 ? azArg[1] : "main";
16908   sqlite3_stmt *pStmt = 0;
16909   unsigned char aHdr[100];
16910   open_db(p, 0);
16911   if( p->db==0 ) return 1;
16912   rc = sqlite3_prepare_v2(p->db,
16913              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
16914              -1, &pStmt, 0);
16915   if( rc ){
16916     utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
16917     sqlite3_finalize(pStmt);
16918     return 1;
16919   }
16920   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
16921   if( sqlite3_step(pStmt)==SQLITE_ROW
16922    && sqlite3_column_bytes(pStmt,0)>100
16923   ){
16924     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
16925     sqlite3_finalize(pStmt);
16926   }else{
16927     raw_printf(stderr, "unable to read database header\n");
16928     sqlite3_finalize(pStmt);
16929     return 1;
16930   }
16931   i = get2byteInt(aHdr+16);
16932   if( i==1 ) i = 65536;
16933   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
16934   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
16935   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
16936   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
16937   for(i=0; i<ArraySize(aField); i++){
16938     int ofst = aField[i].ofst;
16939     unsigned int val = get4byteInt(aHdr + ofst);
16940     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
16941     switch( ofst ){
16942       case 56: {
16943         if( val==1 ) raw_printf(p->out, " (utf8)");
16944         if( val==2 ) raw_printf(p->out, " (utf16le)");
16945         if( val==3 ) raw_printf(p->out, " (utf16be)");
16946       }
16947     }
16948     raw_printf(p->out, "\n");
16949   }
16950   if( zDb==0 ){
16951     zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
16952   }else if( strcmp(zDb,"temp")==0 ){
16953     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
16954   }else{
16955     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
16956   }
16957   for(i=0; i<ArraySize(aQuery); i++){
16958     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
16959     int val = db_int(p->db, zSql);
16960     sqlite3_free(zSql);
16961     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
16962   }
16963   sqlite3_free(zSchemaTab);
16964   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
16965   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
16966   return 0;
16967 }
16968 
16969 /*
16970 ** Print the current sqlite3_errmsg() value to stderr and return 1.
16971 */
16972 static int shellDatabaseError(sqlite3 *db){
16973   const char *zErr = sqlite3_errmsg(db);
16974   utf8_printf(stderr, "Error: %s\n", zErr);
16975   return 1;
16976 }
16977 
16978 /*
16979 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
16980 ** if they match and FALSE (0) if they do not match.
16981 **
16982 ** Globbing rules:
16983 **
16984 **      '*'       Matches any sequence of zero or more characters.
16985 **
16986 **      '?'       Matches exactly one character.
16987 **
16988 **     [...]      Matches one character from the enclosed list of
16989 **                characters.
16990 **
16991 **     [^...]     Matches one character not in the enclosed list.
16992 **
16993 **      '#'       Matches any sequence of one or more digits with an
16994 **                optional + or - sign in front
16995 **
16996 **      ' '       Any span of whitespace matches any other span of
16997 **                whitespace.
16998 **
16999 ** Extra whitespace at the end of z[] is ignored.
17000 */
17001 static int testcase_glob(const char *zGlob, const char *z){
17002   int c, c2;
17003   int invert;
17004   int seen;
17005 
17006   while( (c = (*(zGlob++)))!=0 ){
17007     if( IsSpace(c) ){
17008       if( !IsSpace(*z) ) return 0;
17009       while( IsSpace(*zGlob) ) zGlob++;
17010       while( IsSpace(*z) ) z++;
17011     }else if( c=='*' ){
17012       while( (c=(*(zGlob++))) == '*' || c=='?' ){
17013         if( c=='?' && (*(z++))==0 ) return 0;
17014       }
17015       if( c==0 ){
17016         return 1;
17017       }else if( c=='[' ){
17018         while( *z && testcase_glob(zGlob-1,z)==0 ){
17019           z++;
17020         }
17021         return (*z)!=0;
17022       }
17023       while( (c2 = (*(z++)))!=0 ){
17024         while( c2!=c ){
17025           c2 = *(z++);
17026           if( c2==0 ) return 0;
17027         }
17028         if( testcase_glob(zGlob,z) ) return 1;
17029       }
17030       return 0;
17031     }else if( c=='?' ){
17032       if( (*(z++))==0 ) return 0;
17033     }else if( c=='[' ){
17034       int prior_c = 0;
17035       seen = 0;
17036       invert = 0;
17037       c = *(z++);
17038       if( c==0 ) return 0;
17039       c2 = *(zGlob++);
17040       if( c2=='^' ){
17041         invert = 1;
17042         c2 = *(zGlob++);
17043       }
17044       if( c2==']' ){
17045         if( c==']' ) seen = 1;
17046         c2 = *(zGlob++);
17047       }
17048       while( c2 && c2!=']' ){
17049         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
17050           c2 = *(zGlob++);
17051           if( c>=prior_c && c<=c2 ) seen = 1;
17052           prior_c = 0;
17053         }else{
17054           if( c==c2 ){
17055             seen = 1;
17056           }
17057           prior_c = c2;
17058         }
17059         c2 = *(zGlob++);
17060       }
17061       if( c2==0 || (seen ^ invert)==0 ) return 0;
17062     }else if( c=='#' ){
17063       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
17064       if( !IsDigit(z[0]) ) return 0;
17065       z++;
17066       while( IsDigit(z[0]) ){ z++; }
17067     }else{
17068       if( c!=(*(z++)) ) return 0;
17069     }
17070   }
17071   while( IsSpace(*z) ){ z++; }
17072   return *z==0;
17073 }
17074 
17075 
17076 /*
17077 ** Compare the string as a command-line option with either one or two
17078 ** initial "-" characters.
17079 */
17080 static int optionMatch(const char *zStr, const char *zOpt){
17081   if( zStr[0]!='-' ) return 0;
17082   zStr++;
17083   if( zStr[0]=='-' ) zStr++;
17084   return strcmp(zStr, zOpt)==0;
17085 }
17086 
17087 /*
17088 ** Delete a file.
17089 */
17090 int shellDeleteFile(const char *zFilename){
17091   int rc;
17092 #ifdef _WIN32
17093   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
17094   rc = _wunlink(z);
17095   sqlite3_free(z);
17096 #else
17097   rc = unlink(zFilename);
17098 #endif
17099   return rc;
17100 }
17101 
17102 /*
17103 ** Try to delete the temporary file (if there is one) and free the
17104 ** memory used to hold the name of the temp file.
17105 */
17106 static void clearTempFile(ShellState *p){
17107   if( p->zTempFile==0 ) return;
17108   if( p->doXdgOpen ) return;
17109   if( shellDeleteFile(p->zTempFile) ) return;
17110   sqlite3_free(p->zTempFile);
17111   p->zTempFile = 0;
17112 }
17113 
17114 /*
17115 ** Create a new temp file name with the given suffix.
17116 */
17117 static void newTempFile(ShellState *p, const char *zSuffix){
17118   clearTempFile(p);
17119   sqlite3_free(p->zTempFile);
17120   p->zTempFile = 0;
17121   if( p->db ){
17122     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
17123   }
17124   if( p->zTempFile==0 ){
17125     /* If p->db is an in-memory database then the TEMPFILENAME file-control
17126     ** will not work and we will need to fallback to guessing */
17127     char *zTemp;
17128     sqlite3_uint64 r;
17129     sqlite3_randomness(sizeof(r), &r);
17130     zTemp = getenv("TEMP");
17131     if( zTemp==0 ) zTemp = getenv("TMP");
17132     if( zTemp==0 ){
17133 #ifdef _WIN32
17134       zTemp = "\\tmp";
17135 #else
17136       zTemp = "/tmp";
17137 #endif
17138     }
17139     p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
17140   }else{
17141     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
17142   }
17143   shell_check_oom(p->zTempFile);
17144 }
17145 
17146 
17147 /*
17148 ** The implementation of SQL scalar function fkey_collate_clause(), used
17149 ** by the ".lint fkey-indexes" command. This scalar function is always
17150 ** called with four arguments - the parent table name, the parent column name,
17151 ** the child table name and the child column name.
17152 **
17153 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
17154 **
17155 ** If either of the named tables or columns do not exist, this function
17156 ** returns an empty string. An empty string is also returned if both tables
17157 ** and columns exist but have the same default collation sequence. Or,
17158 ** if both exist but the default collation sequences are different, this
17159 ** function returns the string " COLLATE <parent-collation>", where
17160 ** <parent-collation> is the default collation sequence of the parent column.
17161 */
17162 static void shellFkeyCollateClause(
17163   sqlite3_context *pCtx,
17164   int nVal,
17165   sqlite3_value **apVal
17166 ){
17167   sqlite3 *db = sqlite3_context_db_handle(pCtx);
17168   const char *zParent;
17169   const char *zParentCol;
17170   const char *zParentSeq;
17171   const char *zChild;
17172   const char *zChildCol;
17173   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
17174   int rc;
17175 
17176   assert( nVal==4 );
17177   zParent = (const char*)sqlite3_value_text(apVal[0]);
17178   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
17179   zChild = (const char*)sqlite3_value_text(apVal[2]);
17180   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
17181 
17182   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
17183   rc = sqlite3_table_column_metadata(
17184       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
17185   );
17186   if( rc==SQLITE_OK ){
17187     rc = sqlite3_table_column_metadata(
17188         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
17189     );
17190   }
17191 
17192   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
17193     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
17194     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
17195     sqlite3_free(z);
17196   }
17197 }
17198 
17199 
17200 /*
17201 ** The implementation of dot-command ".lint fkey-indexes".
17202 */
17203 static int lintFkeyIndexes(
17204   ShellState *pState,             /* Current shell tool state */
17205   char **azArg,                   /* Array of arguments passed to dot command */
17206   int nArg                        /* Number of entries in azArg[] */
17207 ){
17208   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
17209   FILE *out = pState->out;        /* Stream to write non-error output to */
17210   int bVerbose = 0;               /* If -verbose is present */
17211   int bGroupByParent = 0;         /* If -groupbyparent is present */
17212   int i;                          /* To iterate through azArg[] */
17213   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
17214   int rc;                         /* Return code */
17215   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
17216 
17217   /*
17218   ** This SELECT statement returns one row for each foreign key constraint
17219   ** in the schema of the main database. The column values are:
17220   **
17221   ** 0. The text of an SQL statement similar to:
17222   **
17223   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
17224   **
17225   **    This SELECT is similar to the one that the foreign keys implementation
17226   **    needs to run internally on child tables. If there is an index that can
17227   **    be used to optimize this query, then it can also be used by the FK
17228   **    implementation to optimize DELETE or UPDATE statements on the parent
17229   **    table.
17230   **
17231   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
17232   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
17233   **    contains an index that can be used to optimize the query.
17234   **
17235   ** 2. Human readable text that describes the child table and columns. e.g.
17236   **
17237   **       "child_table(child_key1, child_key2)"
17238   **
17239   ** 3. Human readable text that describes the parent table and columns. e.g.
17240   **
17241   **       "parent_table(parent_key1, parent_key2)"
17242   **
17243   ** 4. A full CREATE INDEX statement for an index that could be used to
17244   **    optimize DELETE or UPDATE statements on the parent table. e.g.
17245   **
17246   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
17247   **
17248   ** 5. The name of the parent table.
17249   **
17250   ** These six values are used by the C logic below to generate the report.
17251   */
17252   const char *zSql =
17253   "SELECT "
17254     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
17255     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
17256     "  || fkey_collate_clause("
17257     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
17258     ", "
17259     "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
17260     "  || group_concat('*=?', ' AND ') || ')'"
17261     ", "
17262     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
17263     ", "
17264     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
17265     ", "
17266     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
17267     "  || ' ON ' || quote(s.name) || '('"
17268     "  || group_concat(quote(f.[from]) ||"
17269     "        fkey_collate_clause("
17270     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
17271     "  || ');'"
17272     ", "
17273     "     f.[table] "
17274     "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
17275     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
17276     "GROUP BY s.name, f.id "
17277     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
17278   ;
17279   const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
17280 
17281   for(i=2; i<nArg; i++){
17282     int n = strlen30(azArg[i]);
17283     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
17284       bVerbose = 1;
17285     }
17286     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
17287       bGroupByParent = 1;
17288       zIndent = "    ";
17289     }
17290     else{
17291       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
17292           azArg[0], azArg[1]
17293       );
17294       return SQLITE_ERROR;
17295     }
17296   }
17297 
17298   /* Register the fkey_collate_clause() SQL function */
17299   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
17300       0, shellFkeyCollateClause, 0, 0
17301   );
17302 
17303 
17304   if( rc==SQLITE_OK ){
17305     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
17306   }
17307   if( rc==SQLITE_OK ){
17308     sqlite3_bind_int(pSql, 1, bGroupByParent);
17309   }
17310 
17311   if( rc==SQLITE_OK ){
17312     int rc2;
17313     char *zPrev = 0;
17314     while( SQLITE_ROW==sqlite3_step(pSql) ){
17315       int res = -1;
17316       sqlite3_stmt *pExplain = 0;
17317       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
17318       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
17319       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
17320       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
17321       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
17322       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
17323 
17324       if( zEQP==0 ) continue;
17325       if( zGlob==0 ) continue;
17326       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
17327       if( rc!=SQLITE_OK ) break;
17328       if( SQLITE_ROW==sqlite3_step(pExplain) ){
17329         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17330         res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
17331                           || 0==sqlite3_strglob(zGlobIPK, zPlan));
17332       }
17333       rc = sqlite3_finalize(pExplain);
17334       if( rc!=SQLITE_OK ) break;
17335 
17336       if( res<0 ){
17337         raw_printf(stderr, "Error: internal error");
17338         break;
17339       }else{
17340         if( bGroupByParent
17341         && (bVerbose || res==0)
17342         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
17343         ){
17344           raw_printf(out, "-- Parent table %s\n", zParent);
17345           sqlite3_free(zPrev);
17346           zPrev = sqlite3_mprintf("%s", zParent);
17347         }
17348 
17349         if( res==0 ){
17350           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
17351         }else if( bVerbose ){
17352           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
17353               zIndent, zFrom, zTarget
17354           );
17355         }
17356       }
17357     }
17358     sqlite3_free(zPrev);
17359 
17360     if( rc!=SQLITE_OK ){
17361       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17362     }
17363 
17364     rc2 = sqlite3_finalize(pSql);
17365     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
17366       rc = rc2;
17367       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17368     }
17369   }else{
17370     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17371   }
17372 
17373   return rc;
17374 }
17375 
17376 /*
17377 ** Implementation of ".lint" dot command.
17378 */
17379 static int lintDotCommand(
17380   ShellState *pState,             /* Current shell tool state */
17381   char **azArg,                   /* Array of arguments passed to dot command */
17382   int nArg                        /* Number of entries in azArg[] */
17383 ){
17384   int n;
17385   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
17386   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
17387   return lintFkeyIndexes(pState, azArg, nArg);
17388 
17389  usage:
17390   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
17391   raw_printf(stderr, "Where sub-commands are:\n");
17392   raw_printf(stderr, "    fkey-indexes\n");
17393   return SQLITE_ERROR;
17394 }
17395 
17396 #if !defined SQLITE_OMIT_VIRTUALTABLE
17397 static void shellPrepare(
17398   sqlite3 *db,
17399   int *pRc,
17400   const char *zSql,
17401   sqlite3_stmt **ppStmt
17402 ){
17403   *ppStmt = 0;
17404   if( *pRc==SQLITE_OK ){
17405     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
17406     if( rc!=SQLITE_OK ){
17407       raw_printf(stderr, "sql error: %s (%d)\n",
17408           sqlite3_errmsg(db), sqlite3_errcode(db)
17409       );
17410       *pRc = rc;
17411     }
17412   }
17413 }
17414 
17415 /*
17416 ** Create a prepared statement using printf-style arguments for the SQL.
17417 **
17418 ** This routine is could be marked "static".  But it is not always used,
17419 ** depending on compile-time options.  By omitting the "static", we avoid
17420 ** nuisance compiler warnings about "defined but not used".
17421 */
17422 void shellPreparePrintf(
17423   sqlite3 *db,
17424   int *pRc,
17425   sqlite3_stmt **ppStmt,
17426   const char *zFmt,
17427   ...
17428 ){
17429   *ppStmt = 0;
17430   if( *pRc==SQLITE_OK ){
17431     va_list ap;
17432     char *z;
17433     va_start(ap, zFmt);
17434     z = sqlite3_vmprintf(zFmt, ap);
17435     va_end(ap);
17436     if( z==0 ){
17437       *pRc = SQLITE_NOMEM;
17438     }else{
17439       shellPrepare(db, pRc, z, ppStmt);
17440       sqlite3_free(z);
17441     }
17442   }
17443 }
17444 
17445 /* Finalize the prepared statement created using shellPreparePrintf().
17446 **
17447 ** This routine is could be marked "static".  But it is not always used,
17448 ** depending on compile-time options.  By omitting the "static", we avoid
17449 ** nuisance compiler warnings about "defined but not used".
17450 */
17451 void shellFinalize(
17452   int *pRc,
17453   sqlite3_stmt *pStmt
17454 ){
17455   if( pStmt ){
17456     sqlite3 *db = sqlite3_db_handle(pStmt);
17457     int rc = sqlite3_finalize(pStmt);
17458     if( *pRc==SQLITE_OK ){
17459       if( rc!=SQLITE_OK ){
17460         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17461       }
17462       *pRc = rc;
17463     }
17464   }
17465 }
17466 
17467 /* Reset the prepared statement created using shellPreparePrintf().
17468 **
17469 ** This routine is could be marked "static".  But it is not always used,
17470 ** depending on compile-time options.  By omitting the "static", we avoid
17471 ** nuisance compiler warnings about "defined but not used".
17472 */
17473 void shellReset(
17474   int *pRc,
17475   sqlite3_stmt *pStmt
17476 ){
17477   int rc = sqlite3_reset(pStmt);
17478   if( *pRc==SQLITE_OK ){
17479     if( rc!=SQLITE_OK ){
17480       sqlite3 *db = sqlite3_db_handle(pStmt);
17481       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17482     }
17483     *pRc = rc;
17484   }
17485 }
17486 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
17487 
17488 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17489 /******************************************************************************
17490 ** The ".archive" or ".ar" command.
17491 */
17492 /*
17493 ** Structure representing a single ".ar" command.
17494 */
17495 typedef struct ArCommand ArCommand;
17496 struct ArCommand {
17497   u8 eCmd;                        /* An AR_CMD_* value */
17498   u8 bVerbose;                    /* True if --verbose */
17499   u8 bZip;                        /* True if the archive is a ZIP */
17500   u8 bDryRun;                     /* True if --dry-run */
17501   u8 bAppend;                     /* True if --append */
17502   u8 bGlob;                       /* True if --glob */
17503   u8 fromCmdLine;                 /* Run from -A instead of .archive */
17504   int nArg;                       /* Number of command arguments */
17505   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
17506   const char *zFile;              /* --file argument, or NULL */
17507   const char *zDir;               /* --directory argument, or NULL */
17508   char **azArg;                   /* Array of command arguments */
17509   ShellState *p;                  /* Shell state */
17510   sqlite3 *db;                    /* Database containing the archive */
17511 };
17512 
17513 /*
17514 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
17515 */
17516 static int arUsage(FILE *f){
17517   showHelp(f,"archive");
17518   return SQLITE_ERROR;
17519 }
17520 
17521 /*
17522 ** Print an error message for the .ar command to stderr and return
17523 ** SQLITE_ERROR.
17524 */
17525 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
17526   va_list ap;
17527   char *z;
17528   va_start(ap, zFmt);
17529   z = sqlite3_vmprintf(zFmt, ap);
17530   va_end(ap);
17531   utf8_printf(stderr, "Error: %s\n", z);
17532   if( pAr->fromCmdLine ){
17533     utf8_printf(stderr, "Use \"-A\" for more help\n");
17534   }else{
17535     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
17536   }
17537   sqlite3_free(z);
17538   return SQLITE_ERROR;
17539 }
17540 
17541 /*
17542 ** Values for ArCommand.eCmd.
17543 */
17544 #define AR_CMD_CREATE       1
17545 #define AR_CMD_UPDATE       2
17546 #define AR_CMD_INSERT       3
17547 #define AR_CMD_EXTRACT      4
17548 #define AR_CMD_LIST         5
17549 #define AR_CMD_HELP         6
17550 #define AR_CMD_REMOVE       7
17551 
17552 /*
17553 ** Other (non-command) switches.
17554 */
17555 #define AR_SWITCH_VERBOSE     8
17556 #define AR_SWITCH_FILE        9
17557 #define AR_SWITCH_DIRECTORY  10
17558 #define AR_SWITCH_APPEND     11
17559 #define AR_SWITCH_DRYRUN     12
17560 #define AR_SWITCH_GLOB       13
17561 
17562 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
17563   switch( eSwitch ){
17564     case AR_CMD_CREATE:
17565     case AR_CMD_EXTRACT:
17566     case AR_CMD_LIST:
17567     case AR_CMD_REMOVE:
17568     case AR_CMD_UPDATE:
17569     case AR_CMD_INSERT:
17570     case AR_CMD_HELP:
17571       if( pAr->eCmd ){
17572         return arErrorMsg(pAr, "multiple command options");
17573       }
17574       pAr->eCmd = eSwitch;
17575       break;
17576 
17577     case AR_SWITCH_DRYRUN:
17578       pAr->bDryRun = 1;
17579       break;
17580     case AR_SWITCH_GLOB:
17581       pAr->bGlob = 1;
17582       break;
17583     case AR_SWITCH_VERBOSE:
17584       pAr->bVerbose = 1;
17585       break;
17586     case AR_SWITCH_APPEND:
17587       pAr->bAppend = 1;
17588       /* Fall thru into --file */
17589     case AR_SWITCH_FILE:
17590       pAr->zFile = zArg;
17591       break;
17592     case AR_SWITCH_DIRECTORY:
17593       pAr->zDir = zArg;
17594       break;
17595   }
17596 
17597   return SQLITE_OK;
17598 }
17599 
17600 /*
17601 ** Parse the command line for an ".ar" command. The results are written into
17602 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
17603 ** successfully, otherwise an error message is written to stderr and
17604 ** SQLITE_ERROR returned.
17605 */
17606 static int arParseCommand(
17607   char **azArg,                   /* Array of arguments passed to dot command */
17608   int nArg,                       /* Number of entries in azArg[] */
17609   ArCommand *pAr                  /* Populate this object */
17610 ){
17611   struct ArSwitch {
17612     const char *zLong;
17613     char cShort;
17614     u8 eSwitch;
17615     u8 bArg;
17616   } aSwitch[] = {
17617     { "create",    'c', AR_CMD_CREATE,       0 },
17618     { "extract",   'x', AR_CMD_EXTRACT,      0 },
17619     { "insert",    'i', AR_CMD_INSERT,       0 },
17620     { "list",      't', AR_CMD_LIST,         0 },
17621     { "remove",    'r', AR_CMD_REMOVE,       0 },
17622     { "update",    'u', AR_CMD_UPDATE,       0 },
17623     { "help",      'h', AR_CMD_HELP,         0 },
17624     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
17625     { "file",      'f', AR_SWITCH_FILE,      1 },
17626     { "append",    'a', AR_SWITCH_APPEND,    1 },
17627     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
17628     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
17629     { "glob",      'g', AR_SWITCH_GLOB,      0 },
17630   };
17631   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
17632   struct ArSwitch *pEnd = &aSwitch[nSwitch];
17633 
17634   if( nArg<=1 ){
17635     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
17636     return arUsage(stderr);
17637   }else{
17638     char *z = azArg[1];
17639     if( z[0]!='-' ){
17640       /* Traditional style [tar] invocation */
17641       int i;
17642       int iArg = 2;
17643       for(i=0; z[i]; i++){
17644         const char *zArg = 0;
17645         struct ArSwitch *pOpt;
17646         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17647           if( z[i]==pOpt->cShort ) break;
17648         }
17649         if( pOpt==pEnd ){
17650           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17651         }
17652         if( pOpt->bArg ){
17653           if( iArg>=nArg ){
17654             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
17655           }
17656           zArg = azArg[iArg++];
17657         }
17658         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17659       }
17660       pAr->nArg = nArg-iArg;
17661       if( pAr->nArg>0 ){
17662         pAr->azArg = &azArg[iArg];
17663       }
17664     }else{
17665       /* Non-traditional invocation */
17666       int iArg;
17667       for(iArg=1; iArg<nArg; iArg++){
17668         int n;
17669         z = azArg[iArg];
17670         if( z[0]!='-' ){
17671           /* All remaining command line words are command arguments. */
17672           pAr->azArg = &azArg[iArg];
17673           pAr->nArg = nArg-iArg;
17674           break;
17675         }
17676         n = strlen30(z);
17677 
17678         if( z[1]!='-' ){
17679           int i;
17680           /* One or more short options */
17681           for(i=1; i<n; i++){
17682             const char *zArg = 0;
17683             struct ArSwitch *pOpt;
17684             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17685               if( z[i]==pOpt->cShort ) break;
17686             }
17687             if( pOpt==pEnd ){
17688               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17689             }
17690             if( pOpt->bArg ){
17691               if( i<(n-1) ){
17692                 zArg = &z[i+1];
17693                 i = n;
17694               }else{
17695                 if( iArg>=(nArg-1) ){
17696                   return arErrorMsg(pAr, "option requires an argument: %c",
17697                                     z[i]);
17698                 }
17699                 zArg = azArg[++iArg];
17700               }
17701             }
17702             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17703           }
17704         }else if( z[2]=='\0' ){
17705           /* A -- option, indicating that all remaining command line words
17706           ** are command arguments.  */
17707           pAr->azArg = &azArg[iArg+1];
17708           pAr->nArg = nArg-iArg-1;
17709           break;
17710         }else{
17711           /* A long option */
17712           const char *zArg = 0;             /* Argument for option, if any */
17713           struct ArSwitch *pMatch = 0;      /* Matching option */
17714           struct ArSwitch *pOpt;            /* Iterator */
17715           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17716             const char *zLong = pOpt->zLong;
17717             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
17718               if( pMatch ){
17719                 return arErrorMsg(pAr, "ambiguous option: %s",z);
17720               }else{
17721                 pMatch = pOpt;
17722               }
17723             }
17724           }
17725 
17726           if( pMatch==0 ){
17727             return arErrorMsg(pAr, "unrecognized option: %s", z);
17728           }
17729           if( pMatch->bArg ){
17730             if( iArg>=(nArg-1) ){
17731               return arErrorMsg(pAr, "option requires an argument: %s", z);
17732             }
17733             zArg = azArg[++iArg];
17734           }
17735           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
17736         }
17737       }
17738     }
17739   }
17740 
17741   return SQLITE_OK;
17742 }
17743 
17744 /*
17745 ** This function assumes that all arguments within the ArCommand.azArg[]
17746 ** array refer to archive members, as for the --extract, --list or --remove
17747 ** commands. It checks that each of them are "present". If any specified
17748 ** file is not present in the archive, an error is printed to stderr and an
17749 ** error code returned. Otherwise, if all specified arguments are present
17750 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
17751 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
17752 ** when pAr->bGlob is true.
17753 **
17754 ** This function strips any trailing '/' characters from each argument.
17755 ** This is consistent with the way the [tar] command seems to work on
17756 ** Linux.
17757 */
17758 static int arCheckEntries(ArCommand *pAr){
17759   int rc = SQLITE_OK;
17760   if( pAr->nArg ){
17761     int i, j;
17762     sqlite3_stmt *pTest = 0;
17763     const char *zSel = (pAr->bGlob)
17764       ? "SELECT name FROM %s WHERE glob($name,name)"
17765       : "SELECT name FROM %s WHERE name=$name";
17766 
17767     shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
17768     j = sqlite3_bind_parameter_index(pTest, "$name");
17769     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17770       char *z = pAr->azArg[i];
17771       int n = strlen30(z);
17772       int bOk = 0;
17773       while( n>0 && z[n-1]=='/' ) n--;
17774       z[n] = '\0';
17775       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
17776       if( SQLITE_ROW==sqlite3_step(pTest) ){
17777         bOk = 1;
17778       }
17779       shellReset(&rc, pTest);
17780       if( rc==SQLITE_OK && bOk==0 ){
17781         utf8_printf(stderr, "not found in archive: %s\n", z);
17782         rc = SQLITE_ERROR;
17783       }
17784     }
17785     shellFinalize(&rc, pTest);
17786   }
17787   return rc;
17788 }
17789 
17790 /*
17791 ** Format a WHERE clause that can be used against the "sqlar" table to
17792 ** identify all archive members that match the command arguments held
17793 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
17794 ** The caller is responsible for eventually calling sqlite3_free() on
17795 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
17796 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
17797 */
17798 static void arWhereClause(
17799   int *pRc,
17800   ArCommand *pAr,
17801   char **pzWhere                  /* OUT: New WHERE clause */
17802 ){
17803   char *zWhere = 0;
17804   const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
17805   if( *pRc==SQLITE_OK ){
17806     if( pAr->nArg==0 ){
17807       zWhere = sqlite3_mprintf("1");
17808     }else{
17809       int i;
17810       const char *zSep = "";
17811       for(i=0; i<pAr->nArg; i++){
17812         const char *z = pAr->azArg[i];
17813         zWhere = sqlite3_mprintf(
17814           "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
17815           zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
17816         );
17817         if( zWhere==0 ){
17818           *pRc = SQLITE_NOMEM;
17819           break;
17820         }
17821         zSep = " OR ";
17822       }
17823     }
17824   }
17825   *pzWhere = zWhere;
17826 }
17827 
17828 /*
17829 ** Implementation of .ar "lisT" command.
17830 */
17831 static int arListCommand(ArCommand *pAr){
17832   const char *zSql = "SELECT %s FROM %s WHERE %s";
17833   const char *azCols[] = {
17834     "name",
17835     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
17836   };
17837 
17838   char *zWhere = 0;
17839   sqlite3_stmt *pSql = 0;
17840   int rc;
17841 
17842   rc = arCheckEntries(pAr);
17843   arWhereClause(&rc, pAr, &zWhere);
17844 
17845   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
17846                      pAr->zSrcTable, zWhere);
17847   if( pAr->bDryRun ){
17848     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17849   }else{
17850     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17851       if( pAr->bVerbose ){
17852         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
17853             sqlite3_column_text(pSql, 0),
17854             sqlite3_column_int(pSql, 1),
17855             sqlite3_column_text(pSql, 2),
17856             sqlite3_column_text(pSql, 3)
17857         );
17858       }else{
17859         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17860       }
17861     }
17862   }
17863   shellFinalize(&rc, pSql);
17864   sqlite3_free(zWhere);
17865   return rc;
17866 }
17867 
17868 
17869 /*
17870 ** Implementation of .ar "Remove" command.
17871 */
17872 static int arRemoveCommand(ArCommand *pAr){
17873   int rc = 0;
17874   char *zSql = 0;
17875   char *zWhere = 0;
17876 
17877   if( pAr->nArg ){
17878     /* Verify that args actually exist within the archive before proceeding.
17879     ** And formulate a WHERE clause to match them.  */
17880     rc = arCheckEntries(pAr);
17881     arWhereClause(&rc, pAr, &zWhere);
17882   }
17883   if( rc==SQLITE_OK ){
17884     zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
17885                            pAr->zSrcTable, zWhere);
17886     if( pAr->bDryRun ){
17887       utf8_printf(pAr->p->out, "%s\n", zSql);
17888     }else{
17889       char *zErr = 0;
17890       rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
17891       if( rc==SQLITE_OK ){
17892         rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17893         if( rc!=SQLITE_OK ){
17894           sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17895         }else{
17896           rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
17897         }
17898       }
17899       if( zErr ){
17900         utf8_printf(stdout, "ERROR: %s\n", zErr);
17901         sqlite3_free(zErr);
17902       }
17903     }
17904   }
17905   sqlite3_free(zWhere);
17906   sqlite3_free(zSql);
17907   return rc;
17908 }
17909 
17910 /*
17911 ** Implementation of .ar "eXtract" command.
17912 */
17913 static int arExtractCommand(ArCommand *pAr){
17914   const char *zSql1 =
17915     "SELECT "
17916     " ($dir || name),"
17917     " writefile(($dir || name), %s, mode, mtime) "
17918     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
17919     " AND name NOT GLOB '*..[/\\]*'";
17920 
17921   const char *azExtraArg[] = {
17922     "sqlar_uncompress(data, sz)",
17923     "data"
17924   };
17925 
17926   sqlite3_stmt *pSql = 0;
17927   int rc = SQLITE_OK;
17928   char *zDir = 0;
17929   char *zWhere = 0;
17930   int i, j;
17931 
17932   /* If arguments are specified, check that they actually exist within
17933   ** the archive before proceeding. And formulate a WHERE clause to
17934   ** match them.  */
17935   rc = arCheckEntries(pAr);
17936   arWhereClause(&rc, pAr, &zWhere);
17937 
17938   if( rc==SQLITE_OK ){
17939     if( pAr->zDir ){
17940       zDir = sqlite3_mprintf("%s/", pAr->zDir);
17941     }else{
17942       zDir = sqlite3_mprintf("");
17943     }
17944     if( zDir==0 ) rc = SQLITE_NOMEM;
17945   }
17946 
17947   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
17948       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
17949   );
17950 
17951   if( rc==SQLITE_OK ){
17952     j = sqlite3_bind_parameter_index(pSql, "$dir");
17953     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
17954 
17955     /* Run the SELECT statement twice. The first time, writefile() is called
17956     ** for all archive members that should be extracted. The second time,
17957     ** only for the directories. This is because the timestamps for
17958     ** extracted directories must be reset after they are populated (as
17959     ** populating them changes the timestamp).  */
17960     for(i=0; i<2; i++){
17961       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
17962       sqlite3_bind_int(pSql, j, i);
17963       if( pAr->bDryRun ){
17964         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17965       }else{
17966         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17967           if( i==0 && pAr->bVerbose ){
17968             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17969           }
17970         }
17971       }
17972       shellReset(&rc, pSql);
17973     }
17974     shellFinalize(&rc, pSql);
17975   }
17976 
17977   sqlite3_free(zDir);
17978   sqlite3_free(zWhere);
17979   return rc;
17980 }
17981 
17982 /*
17983 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
17984 */
17985 static int arExecSql(ArCommand *pAr, const char *zSql){
17986   int rc;
17987   if( pAr->bDryRun ){
17988     utf8_printf(pAr->p->out, "%s\n", zSql);
17989     rc = SQLITE_OK;
17990   }else{
17991     char *zErr = 0;
17992     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17993     if( zErr ){
17994       utf8_printf(stdout, "ERROR: %s\n", zErr);
17995       sqlite3_free(zErr);
17996     }
17997   }
17998   return rc;
17999 }
18000 
18001 
18002 /*
18003 ** Implementation of .ar "create", "insert", and "update" commands.
18004 **
18005 **     create    ->     Create a new SQL archive
18006 **     insert    ->     Insert or reinsert all files listed
18007 **     update    ->     Insert files that have changed or that were not
18008 **                      previously in the archive
18009 **
18010 ** Create the "sqlar" table in the database if it does not already exist.
18011 ** Then add each file in the azFile[] array to the archive. Directories
18012 ** are added recursively. If argument bVerbose is non-zero, a message is
18013 ** printed on stdout for each file archived.
18014 **
18015 ** The create command is the same as update, except that it drops
18016 ** any existing "sqlar" table before beginning.  The "insert" command
18017 ** always overwrites every file named on the command-line, where as
18018 ** "update" only overwrites if the size or mtime or mode has changed.
18019 */
18020 static int arCreateOrUpdateCommand(
18021   ArCommand *pAr,                 /* Command arguments and options */
18022   int bUpdate,                    /* true for a --create. */
18023   int bOnlyIfChanged              /* Only update if file has changed */
18024 ){
18025   const char *zCreate =
18026       "CREATE TABLE IF NOT EXISTS sqlar(\n"
18027       "  name TEXT PRIMARY KEY,  -- name of the file\n"
18028       "  mode INT,               -- access permissions\n"
18029       "  mtime INT,              -- last modification time\n"
18030       "  sz INT,                 -- original file size\n"
18031       "  data BLOB               -- compressed content\n"
18032       ")";
18033   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
18034   const char *zInsertFmt[2] = {
18035      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
18036      "  SELECT\n"
18037      "    %s,\n"
18038      "    mode,\n"
18039      "    mtime,\n"
18040      "    CASE substr(lsmode(mode),1,1)\n"
18041      "      WHEN '-' THEN length(data)\n"
18042      "      WHEN 'd' THEN 0\n"
18043      "      ELSE -1 END,\n"
18044      "    sqlar_compress(data)\n"
18045      "  FROM fsdir(%Q,%Q) AS disk\n"
18046      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
18047      ,
18048      "REPLACE INTO %s(name,mode,mtime,data)\n"
18049      "  SELECT\n"
18050      "    %s,\n"
18051      "    mode,\n"
18052      "    mtime,\n"
18053      "    data\n"
18054      "  FROM fsdir(%Q,%Q) AS disk\n"
18055      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
18056   };
18057   int i;                          /* For iterating through azFile[] */
18058   int rc;                         /* Return code */
18059   const char *zTab = 0;           /* SQL table into which to insert */
18060   char *zSql;
18061   char zTemp[50];
18062   char *zExists = 0;
18063 
18064   arExecSql(pAr, "PRAGMA page_size=512");
18065   rc = arExecSql(pAr, "SAVEPOINT ar;");
18066   if( rc!=SQLITE_OK ) return rc;
18067   zTemp[0] = 0;
18068   if( pAr->bZip ){
18069     /* Initialize the zipfile virtual table, if necessary */
18070     if( pAr->zFile ){
18071       sqlite3_uint64 r;
18072       sqlite3_randomness(sizeof(r),&r);
18073       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
18074       zTab = zTemp;
18075       zSql = sqlite3_mprintf(
18076          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
18077          zTab, pAr->zFile
18078       );
18079       rc = arExecSql(pAr, zSql);
18080       sqlite3_free(zSql);
18081     }else{
18082       zTab = "zip";
18083     }
18084   }else{
18085     /* Initialize the table for an SQLAR */
18086     zTab = "sqlar";
18087     if( bUpdate==0 ){
18088       rc = arExecSql(pAr, zDrop);
18089       if( rc!=SQLITE_OK ) goto end_ar_transaction;
18090     }
18091     rc = arExecSql(pAr, zCreate);
18092   }
18093   if( bOnlyIfChanged ){
18094     zExists = sqlite3_mprintf(
18095       " AND NOT EXISTS("
18096           "SELECT 1 FROM %s AS mem"
18097           " WHERE mem.name=disk.name"
18098           " AND mem.mtime=disk.mtime"
18099           " AND mem.mode=disk.mode)", zTab);
18100   }else{
18101     zExists = sqlite3_mprintf("");
18102   }
18103   if( zExists==0 ) rc = SQLITE_NOMEM;
18104   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
18105     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
18106         pAr->bVerbose ? "shell_putsnl(name)" : "name",
18107         pAr->azArg[i], pAr->zDir, zExists);
18108     rc = arExecSql(pAr, zSql2);
18109     sqlite3_free(zSql2);
18110   }
18111 end_ar_transaction:
18112   if( rc!=SQLITE_OK ){
18113     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
18114   }else{
18115     rc = arExecSql(pAr, "RELEASE ar;");
18116     if( pAr->bZip && pAr->zFile ){
18117       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
18118       arExecSql(pAr, zSql);
18119       sqlite3_free(zSql);
18120     }
18121   }
18122   sqlite3_free(zExists);
18123   return rc;
18124 }
18125 
18126 /*
18127 ** Implementation of ".ar" dot command.
18128 */
18129 static int arDotCommand(
18130   ShellState *pState,          /* Current shell tool state */
18131   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
18132   char **azArg,                /* Array of arguments passed to dot command */
18133   int nArg                     /* Number of entries in azArg[] */
18134 ){
18135   ArCommand cmd;
18136   int rc;
18137   memset(&cmd, 0, sizeof(cmd));
18138   cmd.fromCmdLine = fromCmdLine;
18139   rc = arParseCommand(azArg, nArg, &cmd);
18140   if( rc==SQLITE_OK ){
18141     int eDbType = SHELL_OPEN_UNSPEC;
18142     cmd.p = pState;
18143     cmd.db = pState->db;
18144     if( cmd.zFile ){
18145       eDbType = deduceDatabaseType(cmd.zFile, 1);
18146     }else{
18147       eDbType = pState->openMode;
18148     }
18149     if( eDbType==SHELL_OPEN_ZIPFILE ){
18150       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
18151         if( cmd.zFile==0 ){
18152           cmd.zSrcTable = sqlite3_mprintf("zip");
18153         }else{
18154           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
18155         }
18156       }
18157       cmd.bZip = 1;
18158     }else if( cmd.zFile ){
18159       int flags;
18160       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
18161       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
18162            || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
18163         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
18164       }else{
18165         flags = SQLITE_OPEN_READONLY;
18166       }
18167       cmd.db = 0;
18168       if( cmd.bDryRun ){
18169         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
18170              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
18171       }
18172       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
18173              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
18174       if( rc!=SQLITE_OK ){
18175         utf8_printf(stderr, "cannot open file: %s (%s)\n",
18176             cmd.zFile, sqlite3_errmsg(cmd.db)
18177         );
18178         goto end_ar_command;
18179       }
18180       sqlite3_fileio_init(cmd.db, 0, 0);
18181       sqlite3_sqlar_init(cmd.db, 0, 0);
18182       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
18183                               shellPutsFunc, 0, 0);
18184 
18185     }
18186     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
18187       if( cmd.eCmd!=AR_CMD_CREATE
18188        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
18189       ){
18190         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
18191         rc = SQLITE_ERROR;
18192         goto end_ar_command;
18193       }
18194       cmd.zSrcTable = sqlite3_mprintf("sqlar");
18195     }
18196 
18197     switch( cmd.eCmd ){
18198       case AR_CMD_CREATE:
18199         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
18200         break;
18201 
18202       case AR_CMD_EXTRACT:
18203         rc = arExtractCommand(&cmd);
18204         break;
18205 
18206       case AR_CMD_LIST:
18207         rc = arListCommand(&cmd);
18208         break;
18209 
18210       case AR_CMD_HELP:
18211         arUsage(pState->out);
18212         break;
18213 
18214       case AR_CMD_INSERT:
18215         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
18216         break;
18217 
18218       case AR_CMD_REMOVE:
18219         rc = arRemoveCommand(&cmd);
18220         break;
18221 
18222       default:
18223         assert( cmd.eCmd==AR_CMD_UPDATE );
18224         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
18225         break;
18226     }
18227   }
18228 end_ar_command:
18229   if( cmd.db!=pState->db ){
18230     close_db(cmd.db);
18231   }
18232   sqlite3_free(cmd.zSrcTable);
18233 
18234   return rc;
18235 }
18236 /* End of the ".archive" or ".ar" command logic
18237 *******************************************************************************/
18238 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
18239 
18240 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18241 /*
18242 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
18243 ** Otherwise, the SQL statement or statements in zSql are executed using
18244 ** database connection db and the error code written to *pRc before
18245 ** this function returns.
18246 */
18247 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
18248   int rc = *pRc;
18249   if( rc==SQLITE_OK ){
18250     char *zErr = 0;
18251     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
18252     if( rc!=SQLITE_OK ){
18253       raw_printf(stderr, "SQL error: %s\n", zErr);
18254     }
18255     sqlite3_free(zErr);
18256     *pRc = rc;
18257   }
18258 }
18259 
18260 /*
18261 ** Like shellExec(), except that zFmt is a printf() style format string.
18262 */
18263 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
18264   char *z = 0;
18265   if( *pRc==SQLITE_OK ){
18266     va_list ap;
18267     va_start(ap, zFmt);
18268     z = sqlite3_vmprintf(zFmt, ap);
18269     va_end(ap);
18270     if( z==0 ){
18271       *pRc = SQLITE_NOMEM;
18272     }else{
18273       shellExec(db, pRc, z);
18274     }
18275     sqlite3_free(z);
18276   }
18277 }
18278 
18279 /*
18280 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18281 ** Otherwise, an attempt is made to allocate, zero and return a pointer
18282 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
18283 ** to SQLITE_NOMEM and NULL returned.
18284 */
18285 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
18286   void *pRet = 0;
18287   if( *pRc==SQLITE_OK ){
18288     pRet = sqlite3_malloc64(nByte);
18289     if( pRet==0 ){
18290       *pRc = SQLITE_NOMEM;
18291     }else{
18292       memset(pRet, 0, nByte);
18293     }
18294   }
18295   return pRet;
18296 }
18297 
18298 /*
18299 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18300 ** Otherwise, zFmt is treated as a printf() style string. The result of
18301 ** formatting it along with any trailing arguments is written into a
18302 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
18303 ** It is the responsibility of the caller to eventually free this buffer
18304 ** using a call to sqlite3_free().
18305 **
18306 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
18307 ** pointer returned.
18308 */
18309 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
18310   char *z = 0;
18311   if( *pRc==SQLITE_OK ){
18312     va_list ap;
18313     va_start(ap, zFmt);
18314     z = sqlite3_vmprintf(zFmt, ap);
18315     va_end(ap);
18316     if( z==0 ){
18317       *pRc = SQLITE_NOMEM;
18318     }
18319   }
18320   return z;
18321 }
18322 
18323 
18324 /*
18325 ** When running the ".recover" command, each output table, and the special
18326 ** orphaned row table if it is required, is represented by an instance
18327 ** of the following struct.
18328 */
18329 typedef struct RecoverTable RecoverTable;
18330 struct RecoverTable {
18331   char *zQuoted;                  /* Quoted version of table name */
18332   int nCol;                       /* Number of columns in table */
18333   char **azlCol;                  /* Array of column lists */
18334   int iPk;                        /* Index of IPK column */
18335 };
18336 
18337 /*
18338 ** Free a RecoverTable object allocated by recoverFindTable() or
18339 ** recoverOrphanTable().
18340 */
18341 static void recoverFreeTable(RecoverTable *pTab){
18342   if( pTab ){
18343     sqlite3_free(pTab->zQuoted);
18344     if( pTab->azlCol ){
18345       int i;
18346       for(i=0; i<=pTab->nCol; i++){
18347         sqlite3_free(pTab->azlCol[i]);
18348       }
18349       sqlite3_free(pTab->azlCol);
18350     }
18351     sqlite3_free(pTab);
18352   }
18353 }
18354 
18355 /*
18356 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18357 ** Otherwise, it allocates and returns a RecoverTable object based on the
18358 ** final four arguments passed to this function. It is the responsibility
18359 ** of the caller to eventually free the returned object using
18360 ** recoverFreeTable().
18361 */
18362 static RecoverTable *recoverNewTable(
18363   int *pRc,                       /* IN/OUT: Error code */
18364   const char *zName,              /* Name of table */
18365   const char *zSql,               /* CREATE TABLE statement */
18366   int bIntkey,
18367   int nCol
18368 ){
18369   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
18370   int rc = *pRc;
18371   RecoverTable *pTab = 0;
18372 
18373   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
18374   if( rc==SQLITE_OK ){
18375     int nSqlCol = 0;
18376     int bSqlIntkey = 0;
18377     sqlite3_stmt *pStmt = 0;
18378 
18379     rc = sqlite3_open("", &dbtmp);
18380     if( rc==SQLITE_OK ){
18381       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
18382                               shellIdQuote, 0, 0);
18383     }
18384     if( rc==SQLITE_OK ){
18385       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
18386     }
18387     if( rc==SQLITE_OK ){
18388       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
18389       if( rc==SQLITE_ERROR ){
18390         rc = SQLITE_OK;
18391         goto finished;
18392       }
18393     }
18394     shellPreparePrintf(dbtmp, &rc, &pStmt,
18395         "SELECT count(*) FROM pragma_table_info(%Q)", zName
18396     );
18397     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18398       nSqlCol = sqlite3_column_int(pStmt, 0);
18399     }
18400     shellFinalize(&rc, pStmt);
18401 
18402     if( rc!=SQLITE_OK || nSqlCol<nCol ){
18403       goto finished;
18404     }
18405 
18406     shellPreparePrintf(dbtmp, &rc, &pStmt,
18407       "SELECT ("
18408       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
18409       ") FROM sqlite_schema WHERE name = %Q", zName
18410     );
18411     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18412       bSqlIntkey = sqlite3_column_int(pStmt, 0);
18413     }
18414     shellFinalize(&rc, pStmt);
18415 
18416     if( bIntkey==bSqlIntkey ){
18417       int i;
18418       const char *zPk = "_rowid_";
18419       sqlite3_stmt *pPkFinder = 0;
18420 
18421       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
18422       ** set zPk to the name of the PK column, and pTab->iPk to the index
18423       ** of the column, where columns are 0-numbered from left to right.
18424       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
18425       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
18426       pTab->iPk = -2;
18427       if( bIntkey ){
18428         shellPreparePrintf(dbtmp, &rc, &pPkFinder,
18429           "SELECT cid, name FROM pragma_table_info(%Q) "
18430           "  WHERE pk=1 AND type='integer' COLLATE nocase"
18431           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
18432           , zName, zName
18433         );
18434         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18435           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18436           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18437           if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
18438         }
18439       }
18440 
18441       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18442       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
18443       pTab->nCol = nSqlCol;
18444 
18445       if( bIntkey ){
18446         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
18447       }else{
18448         pTab->azlCol[0] = shellMPrintf(&rc, "");
18449       }
18450       i = 1;
18451       shellPreparePrintf(dbtmp, &rc, &pStmt,
18452           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
18453           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
18454           "FROM pragma_table_info(%Q)",
18455           bIntkey ? ", " : "", pTab->iPk,
18456           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
18457           zName
18458       );
18459       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18460         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
18461         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
18462         i++;
18463       }
18464       shellFinalize(&rc, pStmt);
18465 
18466       shellFinalize(&rc, pPkFinder);
18467     }
18468   }
18469 
18470  finished:
18471   sqlite3_close(dbtmp);
18472   *pRc = rc;
18473   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
18474     recoverFreeTable(pTab);
18475     pTab = 0;
18476   }
18477   return pTab;
18478 }
18479 
18480 /*
18481 ** This function is called to search the schema recovered from the
18482 ** sqlite_schema table of the (possibly) corrupt database as part
18483 ** of a ".recover" command. Specifically, for a table with root page
18484 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
18485 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
18486 ** those.
18487 **
18488 ** If a table is found, a (RecoverTable*) object is returned. Or, if
18489 ** no such table is found, but bIntkey is false and iRoot is the
18490 ** root page of an index in the recovered schema, then (*pbNoop) is
18491 ** set to true and NULL returned. Or, if there is no such table or
18492 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
18493 ** the caller should write data to the orphans table.
18494 */
18495 static RecoverTable *recoverFindTable(
18496   ShellState *pState,             /* Shell state object */
18497   int *pRc,                       /* IN/OUT: Error code */
18498   int iRoot,                      /* Root page of table */
18499   int bIntkey,                    /* True for an intkey table */
18500   int nCol,                       /* Number of columns in table */
18501   int *pbNoop                     /* OUT: True if iRoot is root of index */
18502 ){
18503   sqlite3_stmt *pStmt = 0;
18504   RecoverTable *pRet = 0;
18505   int bNoop = 0;
18506   const char *zSql = 0;
18507   const char *zName = 0;
18508 
18509   /* Search the recovered schema for an object with root page iRoot. */
18510   shellPreparePrintf(pState->db, pRc, &pStmt,
18511       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
18512   );
18513   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18514     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
18515     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
18516       bNoop = 1;
18517       break;
18518     }
18519     if( sqlite3_stricmp(zType, "table")==0 ){
18520       zName = (const char*)sqlite3_column_text(pStmt, 1);
18521       zSql = (const char*)sqlite3_column_text(pStmt, 2);
18522       if( zName!=0 && zSql!=0 ){
18523         pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18524         break;
18525       }
18526     }
18527   }
18528 
18529   shellFinalize(pRc, pStmt);
18530   *pbNoop = bNoop;
18531   return pRet;
18532 }
18533 
18534 /*
18535 ** Return a RecoverTable object representing the orphans table.
18536 */
18537 static RecoverTable *recoverOrphanTable(
18538   ShellState *pState,             /* Shell state object */
18539   int *pRc,                       /* IN/OUT: Error code */
18540   const char *zLostAndFound,      /* Base name for orphans table */
18541   int nCol                        /* Number of user data columns */
18542 ){
18543   RecoverTable *pTab = 0;
18544   if( nCol>=0 && *pRc==SQLITE_OK ){
18545     int i;
18546 
18547     /* This block determines the name of the orphan table. The prefered
18548     ** name is zLostAndFound. But if that clashes with another name
18549     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
18550     ** and so on until a non-clashing name is found.  */
18551     int iTab = 0;
18552     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
18553     sqlite3_stmt *pTest = 0;
18554     shellPrepare(pState->db, pRc,
18555         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
18556     );
18557     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18558     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
18559       shellReset(pRc, pTest);
18560       sqlite3_free(zTab);
18561       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
18562       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18563     }
18564     shellFinalize(pRc, pTest);
18565 
18566     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
18567     if( pTab ){
18568       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
18569       pTab->nCol = nCol;
18570       pTab->iPk = -2;
18571       if( nCol>0 ){
18572         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
18573         if( pTab->azlCol ){
18574           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
18575           for(i=nCol-1; i>=0; i--){
18576             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
18577           }
18578         }
18579       }
18580 
18581       if( *pRc!=SQLITE_OK ){
18582         recoverFreeTable(pTab);
18583         pTab = 0;
18584       }else{
18585         raw_printf(pState->out,
18586             "CREATE TABLE %s(rootpgno INTEGER, "
18587             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
18588         );
18589         for(i=0; i<nCol; i++){
18590           raw_printf(pState->out, ", c%d", i);
18591         }
18592         raw_printf(pState->out, ");\n");
18593       }
18594     }
18595     sqlite3_free(zTab);
18596   }
18597   return pTab;
18598 }
18599 
18600 /*
18601 ** This function is called to recover data from the database. A script
18602 ** to construct a new database containing all recovered data is output
18603 ** on stream pState->out.
18604 */
18605 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18606   int rc = SQLITE_OK;
18607   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
18608   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
18609   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
18610   const char *zRecoveryDb = "";   /* Name of "recovery" database */
18611   const char *zLostAndFound = "lost_and_found";
18612   int i;
18613   int nOrphan = -1;
18614   RecoverTable *pOrphan = 0;
18615 
18616   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
18617   int bRowids = 1;                /* 0 if --no-rowids */
18618   for(i=1; i<nArg; i++){
18619     char *z = azArg[i];
18620     int n;
18621     if( z[0]=='-' && z[1]=='-' ) z++;
18622     n = strlen30(z);
18623     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18624       bFreelist = 0;
18625     }else
18626     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
18627       i++;
18628       zRecoveryDb = azArg[i];
18629     }else
18630     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18631       i++;
18632       zLostAndFound = azArg[i];
18633     }else
18634     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18635       bRowids = 0;
18636     }
18637     else{
18638       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
18639       showHelp(pState->out, azArg[0]);
18640       return 1;
18641     }
18642   }
18643 
18644   shellExecPrintf(pState->db, &rc,
18645     /* Attach an in-memory database named 'recovery'. Create an indexed
18646     ** cache of the sqlite_dbptr virtual table. */
18647     "PRAGMA writable_schema = on;"
18648     "ATTACH %Q AS recovery;"
18649     "DROP TABLE IF EXISTS recovery.dbptr;"
18650     "DROP TABLE IF EXISTS recovery.freelist;"
18651     "DROP TABLE IF EXISTS recovery.map;"
18652     "DROP TABLE IF EXISTS recovery.schema;"
18653     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18654   );
18655 
18656   if( bFreelist ){
18657     shellExec(pState->db, &rc,
18658       "WITH trunk(pgno) AS ("
18659       "  SELECT shell_int32("
18660       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18661       "      WHERE x>0"
18662       "    UNION"
18663       "  SELECT shell_int32("
18664       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18665       "      FROM trunk WHERE x>0"
18666       "),"
18667       "freelist(data, n, freepgno) AS ("
18668       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18669       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18670       "    UNION ALL"
18671       "  SELECT data, n-1, shell_int32(data, 2+n) "
18672       "      FROM freelist WHERE n>=0"
18673       ")"
18674       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18675     );
18676   }
18677 
18678   /* If this is an auto-vacuum database, add all pointer-map pages to
18679   ** the freelist table. Do this regardless of whether or not
18680   ** --freelist-corrupt was specified.  */
18681   shellExec(pState->db, &rc,
18682     "WITH ptrmap(pgno) AS ("
18683     "  SELECT 2 WHERE shell_int32("
18684     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18685     "  )"
18686     "    UNION ALL "
18687     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18688     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18689     ")"
18690     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18691   );
18692 
18693   shellExec(pState->db, &rc,
18694     "CREATE TABLE recovery.dbptr("
18695     "      pgno, child, PRIMARY KEY(child, pgno)"
18696     ") WITHOUT ROWID;"
18697     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18698     "    SELECT * FROM sqlite_dbptr"
18699     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18700 
18701     /* Delete any pointer to page 1. This ensures that page 1 is considered
18702     ** a root page, regardless of how corrupt the db is. */
18703     "DELETE FROM recovery.dbptr WHERE child = 1;"
18704 
18705     /* Delete all pointers to any pages that have more than one pointer
18706     ** to them. Such pages will be treated as root pages when recovering
18707     ** data.  */
18708     "DELETE FROM recovery.dbptr WHERE child IN ("
18709     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18710     ");"
18711 
18712     /* Create the "map" table that will (eventually) contain instructions
18713     ** for dealing with each page in the db that contains one or more
18714     ** records. */
18715     "CREATE TABLE recovery.map("
18716       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18717     ");"
18718 
18719     /* Populate table [map]. If there are circular loops of pages in the
18720     ** database, the following adds all pages in such a loop to the map
18721     ** as individual root pages. This could be handled better.  */
18722     "WITH pages(i, maxlen) AS ("
18723     "  SELECT page_count, ("
18724     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18725     "  ) FROM pragma_page_count WHERE page_count>0"
18726     "    UNION ALL"
18727     "  SELECT i-1, ("
18728     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18729     "  ) FROM pages WHERE i>=2"
18730     ")"
18731     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18732     "  SELECT i, maxlen, NULL, ("
18733     "    WITH p(orig, pgno, parent) AS ("
18734     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18735     "        UNION "
18736     "      SELECT i, p.parent, "
18737     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18738     "    )"
18739     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18740     ") "
18741     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18742     "UPDATE recovery.map AS o SET intkey = ("
18743     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18744     ");"
18745 
18746     /* Extract data from page 1 and any linked pages into table
18747     ** recovery.schema. With the same schema as an sqlite_schema table.  */
18748     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18749     "INSERT INTO recovery.schema SELECT "
18750     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
18751     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
18752     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
18753     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
18754     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
18755     "FROM sqlite_dbdata WHERE pgno IN ("
18756     "  SELECT pgno FROM recovery.map WHERE root=1"
18757     ")"
18758     "GROUP BY pgno, cell;"
18759     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18760   );
18761 
18762   /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
18763   ** CREATE TABLE statements that extracted from the existing schema.  */
18764   if( rc==SQLITE_OK ){
18765     sqlite3_stmt *pStmt = 0;
18766     /* ".recover" might output content in an order which causes immediate
18767     ** foreign key constraints to be violated. So disable foreign-key
18768     ** constraint enforcement to prevent problems when running the output
18769     ** script. */
18770     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
18771     raw_printf(pState->out, "BEGIN;\n");
18772     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
18773     shellPrepare(pState->db, &rc,
18774         "SELECT sql FROM recovery.schema "
18775         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
18776     );
18777     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18778       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
18779       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
18780           &zCreateTable[12]
18781       );
18782     }
18783     shellFinalize(&rc, pStmt);
18784   }
18785 
18786   /* Figure out if an orphan table will be required. And if so, how many
18787   ** user columns it should contain */
18788   shellPrepare(pState->db, &rc,
18789       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
18790       , &pLoop
18791   );
18792   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18793     nOrphan = sqlite3_column_int(pLoop, 0);
18794   }
18795   shellFinalize(&rc, pLoop);
18796   pLoop = 0;
18797 
18798   shellPrepare(pState->db, &rc,
18799       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
18800   );
18801 
18802   shellPrepare(pState->db, &rc,
18803       "SELECT max(field), group_concat(shell_escape_crnl(quote"
18804       "(case when (? AND field<0) then NULL else value end)"
18805       "), ', ')"
18806       ", min(field) "
18807       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
18808       "GROUP BY cell", &pCells
18809   );
18810 
18811   /* Loop through each root page. */
18812   shellPrepare(pState->db, &rc,
18813       "SELECT root, intkey, max(maxlen) FROM recovery.map"
18814       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
18815       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
18816       ")", &pLoop
18817   );
18818   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18819     int iRoot = sqlite3_column_int(pLoop, 0);
18820     int bIntkey = sqlite3_column_int(pLoop, 1);
18821     int nCol = sqlite3_column_int(pLoop, 2);
18822     int bNoop = 0;
18823     RecoverTable *pTab;
18824 
18825     assert( bIntkey==0 || bIntkey==1 );
18826     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
18827     if( bNoop || rc ) continue;
18828     if( pTab==0 ){
18829       if( pOrphan==0 ){
18830         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18831       }
18832       pTab = pOrphan;
18833       if( pTab==0 ) break;
18834     }
18835 
18836     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
18837       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
18838     }
18839     sqlite3_bind_int(pPages, 1, iRoot);
18840     if( bRowids==0 && pTab->iPk<0 ){
18841       sqlite3_bind_int(pCells, 1, 1);
18842     }else{
18843       sqlite3_bind_int(pCells, 1, 0);
18844     }
18845     sqlite3_bind_int(pCells, 3, pTab->iPk);
18846 
18847     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
18848       int iPgno = sqlite3_column_int(pPages, 0);
18849       sqlite3_bind_int(pCells, 2, iPgno);
18850       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
18851         int nField = sqlite3_column_int(pCells, 0);
18852         int iMin = sqlite3_column_int(pCells, 2);
18853         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
18854 
18855         RecoverTable *pTab2 = pTab;
18856         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
18857           if( pOrphan==0 ){
18858             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18859           }
18860           pTab2 = pOrphan;
18861           if( pTab2==0 ) break;
18862         }
18863 
18864         nField = nField+1;
18865         if( pTab2==pOrphan ){
18866           raw_printf(pState->out,
18867               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
18868               pTab2->zQuoted, iRoot, iPgno, nField,
18869               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
18870           );
18871         }else{
18872           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
18873               pTab2->zQuoted, pTab2->azlCol[nField], zVal
18874           );
18875         }
18876       }
18877       shellReset(&rc, pCells);
18878     }
18879     shellReset(&rc, pPages);
18880     if( pTab!=pOrphan ) recoverFreeTable(pTab);
18881   }
18882   shellFinalize(&rc, pLoop);
18883   shellFinalize(&rc, pPages);
18884   shellFinalize(&rc, pCells);
18885   recoverFreeTable(pOrphan);
18886 
18887   /* The rest of the schema */
18888   if( rc==SQLITE_OK ){
18889     sqlite3_stmt *pStmt = 0;
18890     shellPrepare(pState->db, &rc,
18891         "SELECT sql, name FROM recovery.schema "
18892         "WHERE sql NOT LIKE 'create table%'", &pStmt
18893     );
18894     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18895       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
18896       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
18897         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
18898         char *zPrint = shellMPrintf(&rc,
18899           "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
18900           zName, zName, zSql
18901         );
18902         raw_printf(pState->out, "%s;\n", zPrint);
18903         sqlite3_free(zPrint);
18904       }else{
18905         raw_printf(pState->out, "%s;\n", zSql);
18906       }
18907     }
18908     shellFinalize(&rc, pStmt);
18909   }
18910 
18911   if( rc==SQLITE_OK ){
18912     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
18913     raw_printf(pState->out, "COMMIT;\n");
18914   }
18915   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
18916   return rc;
18917 }
18918 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18919 
18920 
18921 /*
18922  * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
18923  * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
18924  *   close db and set it to 0, and return the columns spec, to later
18925  *   be sqlite3_free()'ed by the caller.
18926  * The return is 0 when either:
18927  *   (a) The db was not initialized and zCol==0 (There are no columns.)
18928  *   (b) zCol!=0  (Column was added, db initialized as needed.)
18929  * The 3rd argument, pRenamed, references an out parameter. If the
18930  * pointer is non-zero, its referent will be set to a summary of renames
18931  * done if renaming was necessary, or set to 0 if none was done. The out
18932  * string (if any) must be sqlite3_free()'ed by the caller.
18933  */
18934 #ifdef SHELL_DEBUG
18935 #define rc_err_oom_die(rc) \
18936   if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
18937   else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
18938     fprintf(stderr,"E:%d\n",rc), assert(0)
18939 #else
18940 static void rc_err_oom_die(int rc){
18941   if( rc==SQLITE_NOMEM ) shell_check_oom(0);
18942   assert(rc==SQLITE_OK||rc==SQLITE_DONE);
18943 }
18944 #endif
18945 
18946 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
18947 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
18948 #else  /* Otherwise, memory is faster/better for the transient DB. */
18949 static const char *zCOL_DB = ":memory:";
18950 #endif
18951 
18952 /* Define character (as C string) to separate generated column ordinal
18953  * from protected part of incoming column names. This defaults to "_"
18954  * so that incoming column identifiers that did not need not be quoted
18955  * remain usable without being quoted. It must be one character.
18956  */
18957 #ifndef SHELL_AUTOCOLUMN_SEP
18958 # define AUTOCOLUMN_SEP "_"
18959 #else
18960 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
18961 #endif
18962 
18963 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
18964   /* Queries and D{D,M}L used here */
18965   static const char * const zTabMake = "\
18966 CREATE TABLE ColNames(\
18967  cpos INTEGER PRIMARY KEY,\
18968  name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
18969 CREATE VIEW RepeatedNames AS \
18970 SELECT DISTINCT t.name FROM ColNames t \
18971 WHERE t.name COLLATE NOCASE IN (\
18972  SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
18973 );\
18974 ";
18975   static const char * const zTabFill = "\
18976 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
18977  VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
18978 ";
18979   static const char * const zHasDupes = "\
18980 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
18981  <count(name) FROM ColNames\
18982 ";
18983 #ifdef SHELL_COLUMN_RENAME_CLEAN
18984   static const char * const zDedoctor = "\
18985 UPDATE ColNames SET chop=iif(\
18986   (substring(name,nlen,1) BETWEEN '0' AND '9')\
18987   AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
18988  nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
18989  0\
18990 )\
18991 ";
18992 #endif
18993   static const char * const zSetReps = "\
18994 UPDATE ColNames AS t SET reps=\
18995 (SELECT count(*) FROM ColNames d \
18996  WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
18997  COLLATE NOCASE\
18998 )\
18999 ";
19000 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
19001   static const char * const zColDigits = "\
19002 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
19003 ";
19004 #endif
19005   static const char * const zRenameRank =
19006 #ifdef SHELL_COLUMN_RENAME_CLEAN
19007     "UPDATE ColNames AS t SET suff="
19008     "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
19009 #else /* ...RENAME_MINIMAL_ONE_PASS */
19010 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
19011 "  SELECT 0 AS nlz"
19012 "  UNION"
19013 "  SELECT nlz+1 AS nlz FROM Lzn"
19014 "  WHERE EXISTS("
19015 "   SELECT 1"
19016 "   FROM ColNames t, ColNames o"
19017 "   WHERE"
19018 "    iif(t.name IN (SELECT * FROM RepeatedNames),"
19019 "     printf('%s"AUTOCOLUMN_SEP"%s',"
19020 "      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
19021 "     t.name"
19022 "    )"
19023 "    ="
19024 "    iif(o.name IN (SELECT * FROM RepeatedNames),"
19025 "     printf('%s"AUTOCOLUMN_SEP"%s',"
19026 "      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
19027 "     o.name"
19028 "    )"
19029 "    COLLATE NOCASE"
19030 "    AND o.cpos<>t.cpos"
19031 "   GROUP BY t.cpos"
19032 "  )"
19033 ") UPDATE Colnames AS t SET"
19034 " chop = 0," /* No chopping, never touch incoming names. */
19035 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
19036 "  printf('"AUTOCOLUMN_SEP"%s', substring("
19037 "   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
19038 "  ''"
19039 " )"
19040 #endif
19041     ;
19042   static const char * const zCollectVar = "\
19043 SELECT\
19044  '('||x'0a'\
19045  || group_concat(\
19046   cname||' TEXT',\
19047   ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
19048  ||')' AS ColsSpec \
19049 FROM (\
19050  SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
19051  FROM ColNames ORDER BY cpos\
19052 )";
19053   static const char * const zRenamesDone =
19054     "SELECT group_concat("
19055     " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
19056     " ','||x'0a')"
19057     "FROM ColNames WHERE suff<>'' OR chop!=0"
19058     ;
19059   int rc;
19060   sqlite3_stmt *pStmt = 0;
19061   assert(pDb!=0);
19062   if( zColNew ){
19063     /* Add initial or additional column. Init db if necessary. */
19064     if( *pDb==0 ){
19065       if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
19066 #ifdef SHELL_COLFIX_DB
19067       if(*zCOL_DB!=':')
19068         sqlite3_exec(*pDb,"drop table if exists ColNames;"
19069                      "drop view if exists RepeatedNames;",0,0,0);
19070 #endif
19071       rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
19072       rc_err_oom_die(rc);
19073     }
19074     assert(*pDb!=0);
19075     rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
19076     rc_err_oom_die(rc);
19077     rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
19078     rc_err_oom_die(rc);
19079     rc = sqlite3_step(pStmt);
19080     rc_err_oom_die(rc);
19081     sqlite3_finalize(pStmt);
19082     return 0;
19083   }else if( *pDb==0 ){
19084     return 0;
19085   }else{
19086     /* Formulate the columns spec, close the DB, zero *pDb. */
19087     char *zColsSpec = 0;
19088     int hasDupes = db_int(*pDb, zHasDupes);
19089 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
19090     int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
19091 #else
19092 # define nDigits 2
19093 #endif
19094     if( hasDupes ){
19095 #ifdef SHELL_COLUMN_RENAME_CLEAN
19096       rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
19097       rc_err_oom_die(rc);
19098 #endif
19099       rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
19100       rc_err_oom_die(rc);
19101       rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
19102       rc_err_oom_die(rc);
19103       sqlite3_bind_int(pStmt, 1, nDigits);
19104       rc = sqlite3_step(pStmt);
19105       sqlite3_finalize(pStmt);
19106       assert(rc==SQLITE_DONE);
19107     }
19108     assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
19109     rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
19110     rc_err_oom_die(rc);
19111     rc = sqlite3_step(pStmt);
19112     if( rc==SQLITE_ROW ){
19113       zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
19114     }else{
19115       zColsSpec = 0;
19116     }
19117     if( pzRenamed!=0 ){
19118       if( !hasDupes ) *pzRenamed = 0;
19119       else{
19120         sqlite3_finalize(pStmt);
19121         if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
19122             && SQLITE_ROW==sqlite3_step(pStmt) ){
19123           *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
19124         }else
19125           *pzRenamed = 0;
19126       }
19127     }
19128     sqlite3_finalize(pStmt);
19129     sqlite3_close(*pDb);
19130     *pDb = 0;
19131     return zColsSpec;
19132   }
19133 }
19134 
19135 /*
19136 ** If an input line begins with "." then invoke this routine to
19137 ** process that line.
19138 **
19139 ** Return 1 on error, 2 to exit, and 0 otherwise.
19140 */
19141 static int do_meta_command(char *zLine, ShellState *p){
19142   int h = 1;
19143   int nArg = 0;
19144   int n, c;
19145   int rc = 0;
19146   char *azArg[52];
19147 
19148 #ifndef SQLITE_OMIT_VIRTUALTABLE
19149   if( p->expert.pExpert ){
19150     expertFinish(p, 1, 0);
19151   }
19152 #endif
19153 
19154   /* Parse the input line into tokens.
19155   */
19156   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
19157     while( IsSpace(zLine[h]) ){ h++; }
19158     if( zLine[h]==0 ) break;
19159     if( zLine[h]=='\'' || zLine[h]=='"' ){
19160       int delim = zLine[h++];
19161       azArg[nArg++] = &zLine[h];
19162       while( zLine[h] && zLine[h]!=delim ){
19163         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
19164         h++;
19165       }
19166       if( zLine[h]==delim ){
19167         zLine[h++] = 0;
19168       }
19169       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
19170     }else{
19171       azArg[nArg++] = &zLine[h];
19172       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
19173       if( zLine[h] ) zLine[h++] = 0;
19174       resolve_backslashes(azArg[nArg-1]);
19175     }
19176   }
19177   azArg[nArg] = 0;
19178 
19179   /* Process the input line.
19180   */
19181   if( nArg==0 ) return 0; /* no tokens, no error */
19182   n = strlen30(azArg[0]);
19183   c = azArg[0][0];
19184   clearTempFile(p);
19185 
19186 #ifndef SQLITE_OMIT_AUTHORIZATION
19187   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
19188     if( nArg!=2 ){
19189       raw_printf(stderr, "Usage: .auth ON|OFF\n");
19190       rc = 1;
19191       goto meta_command_exit;
19192     }
19193     open_db(p, 0);
19194     if( booleanValue(azArg[1]) ){
19195       sqlite3_set_authorizer(p->db, shellAuth, p);
19196     }else if( p->bSafeModePersist ){
19197       sqlite3_set_authorizer(p->db, safeModeAuth, p);
19198     }else{
19199       sqlite3_set_authorizer(p->db, 0, 0);
19200     }
19201   }else
19202 #endif
19203 
19204 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19205   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19206     open_db(p, 0);
19207     failIfSafeMode(p, "cannot run .archive in safe mode");
19208     rc = arDotCommand(p, 0, azArg, nArg);
19209   }else
19210 #endif
19211 
19212   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
19213    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
19214   ){
19215     const char *zDestFile = 0;
19216     const char *zDb = 0;
19217     sqlite3 *pDest;
19218     sqlite3_backup *pBackup;
19219     int j;
19220     int bAsync = 0;
19221     const char *zVfs = 0;
19222     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
19223     for(j=1; j<nArg; j++){
19224       const char *z = azArg[j];
19225       if( z[0]=='-' ){
19226         if( z[1]=='-' ) z++;
19227         if( strcmp(z, "-append")==0 ){
19228           zVfs = "apndvfs";
19229         }else
19230         if( strcmp(z, "-async")==0 ){
19231           bAsync = 1;
19232         }else
19233         {
19234           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
19235           return 1;
19236         }
19237       }else if( zDestFile==0 ){
19238         zDestFile = azArg[j];
19239       }else if( zDb==0 ){
19240         zDb = zDestFile;
19241         zDestFile = azArg[j];
19242       }else{
19243         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
19244         return 1;
19245       }
19246     }
19247     if( zDestFile==0 ){
19248       raw_printf(stderr, "missing FILENAME argument on .backup\n");
19249       return 1;
19250     }
19251     if( zDb==0 ) zDb = "main";
19252     rc = sqlite3_open_v2(zDestFile, &pDest,
19253                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
19254     if( rc!=SQLITE_OK ){
19255       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
19256       close_db(pDest);
19257       return 1;
19258     }
19259     if( bAsync ){
19260       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
19261                    0, 0, 0);
19262     }
19263     open_db(p, 0);
19264     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
19265     if( pBackup==0 ){
19266       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19267       close_db(pDest);
19268       return 1;
19269     }
19270     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
19271     sqlite3_backup_finish(pBackup);
19272     if( rc==SQLITE_DONE ){
19273       rc = 0;
19274     }else{
19275       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19276       rc = 1;
19277     }
19278     close_db(pDest);
19279   }else
19280 
19281   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19282     if( nArg==2 ){
19283       bail_on_error = booleanValue(azArg[1]);
19284     }else{
19285       raw_printf(stderr, "Usage: .bail on|off\n");
19286       rc = 1;
19287     }
19288   }else
19289 
19290   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
19291     if( nArg==2 ){
19292       if( booleanValue(azArg[1]) ){
19293         setBinaryMode(p->out, 1);
19294       }else{
19295         setTextMode(p->out, 1);
19296       }
19297     }else{
19298       raw_printf(stderr, "Usage: .binary on|off\n");
19299       rc = 1;
19300     }
19301   }else
19302 
19303   /* The undocumented ".breakpoint" command causes a call to the no-op
19304   ** routine named test_breakpoint().
19305   */
19306   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19307     test_breakpoint();
19308   }else
19309 
19310   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19311     failIfSafeMode(p, "cannot run .cd in safe mode");
19312     if( nArg==2 ){
19313 #if defined(_WIN32) || defined(WIN32)
19314       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
19315       rc = !SetCurrentDirectoryW(z);
19316       sqlite3_free(z);
19317 #else
19318       rc = chdir(azArg[1]);
19319 #endif
19320       if( rc ){
19321         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
19322         rc = 1;
19323       }
19324     }else{
19325       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
19326       rc = 1;
19327     }
19328   }else
19329 
19330   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19331     if( nArg==2 ){
19332       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19333     }else{
19334       raw_printf(stderr, "Usage: .changes on|off\n");
19335       rc = 1;
19336     }
19337   }else
19338 
19339   /* Cancel output redirection, if it is currently set (by .testcase)
19340   ** Then read the content of the testcase-out.txt file and compare against
19341   ** azArg[1].  If there are differences, report an error and exit.
19342   */
19343   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
19344     char *zRes = 0;
19345     output_reset(p);
19346     if( nArg!=2 ){
19347       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
19348       rc = 2;
19349     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
19350       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
19351       rc = 2;
19352     }else if( testcase_glob(azArg[1],zRes)==0 ){
19353       utf8_printf(stderr,
19354                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
19355                  p->zTestcase, azArg[1], zRes);
19356       rc = 1;
19357     }else{
19358       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
19359       p->nCheck++;
19360     }
19361     sqlite3_free(zRes);
19362   }else
19363 
19364   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19365     failIfSafeMode(p, "cannot run .clone in safe mode");
19366     if( nArg==2 ){
19367       tryToClone(p, azArg[1]);
19368     }else{
19369       raw_printf(stderr, "Usage: .clone FILENAME\n");
19370       rc = 1;
19371     }
19372   }else
19373 
19374   if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19375     if( nArg==1 ){
19376       /* List available connections */
19377       int i;
19378       for(i=0; i<ArraySize(p->aAuxDb); i++){
19379         const char *zFile = p->aAuxDb[i].zDbFilename;
19380         if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
19381           zFile = "(not open)";
19382         }else if( zFile==0 ){
19383           zFile = "(memory)";
19384         }else if( zFile[0]==0 ){
19385           zFile = "(temporary-file)";
19386         }
19387         if( p->pAuxDb == &p->aAuxDb[i] ){
19388           utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
19389         }else if( p->aAuxDb[i].db!=0 ){
19390           utf8_printf(stdout, "       %d: %s\n", i, zFile);
19391         }
19392       }
19393     }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
19394       int i = azArg[1][0] - '0';
19395       if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
19396         p->pAuxDb->db = p->db;
19397         p->pAuxDb = &p->aAuxDb[i];
19398         globalDb = p->db = p->pAuxDb->db;
19399         p->pAuxDb->db = 0;
19400       }
19401     }else if( nArg==3 && strcmp(azArg[1], "close")==0
19402            && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
19403       int i = azArg[2][0] - '0';
19404       if( i<0 || i>=ArraySize(p->aAuxDb) ){
19405         /* No-op */
19406       }else if( p->pAuxDb == &p->aAuxDb[i] ){
19407         raw_printf(stderr, "cannot close the active database connection\n");
19408         rc = 1;
19409       }else if( p->aAuxDb[i].db ){
19410         session_close_all(p, i);
19411         close_db(p->aAuxDb[i].db);
19412         p->aAuxDb[i].db = 0;
19413       }
19414     }else{
19415       raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
19416       rc = 1;
19417     }
19418   }else
19419 
19420   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
19421     char **azName = 0;
19422     int nName = 0;
19423     sqlite3_stmt *pStmt;
19424     int i;
19425     open_db(p, 0);
19426     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19427     if( rc ){
19428       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19429       rc = 1;
19430     }else{
19431       while( sqlite3_step(pStmt)==SQLITE_ROW ){
19432         const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
19433         const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
19434         if( zSchema==0 || zFile==0 ) continue;
19435         azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
19436         shell_check_oom(azName);
19437         azName[nName*2] = strdup(zSchema);
19438         azName[nName*2+1] = strdup(zFile);
19439         nName++;
19440       }
19441     }
19442     sqlite3_finalize(pStmt);
19443     for(i=0; i<nName; i++){
19444       int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
19445       int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
19446       const char *z = azName[i*2+1];
19447       utf8_printf(p->out, "%s: %s %s%s\n",
19448          azName[i*2],
19449          z && z[0] ? z : "\"\"",
19450          bRdonly ? "r/o" : "r/w",
19451          eTxn==SQLITE_TXN_NONE ? "" :
19452             eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
19453       free(azName[i*2]);
19454       free(azName[i*2+1]);
19455     }
19456     sqlite3_free(azName);
19457   }else
19458 
19459   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
19460     static const struct DbConfigChoices {
19461       const char *zName;
19462       int op;
19463     } aDbConfig[] = {
19464         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
19465         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
19466         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
19467         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
19468         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
19469         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
19470         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
19471         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
19472         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
19473         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
19474         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
19475         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
19476         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
19477         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
19478         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
19479         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
19480     };
19481     int ii, v;
19482     open_db(p, 0);
19483     for(ii=0; ii<ArraySize(aDbConfig); ii++){
19484       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
19485       if( nArg>=3 ){
19486         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
19487       }
19488       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
19489       utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
19490       if( nArg>1 ) break;
19491     }
19492     if( nArg>1 && ii==ArraySize(aDbConfig) ){
19493       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
19494       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
19495     }
19496   }else
19497 
19498   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
19499     rc = shell_dbinfo_command(p, nArg, azArg);
19500   }else
19501 
19502 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19503   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
19504     open_db(p, 0);
19505     rc = recoverDatabaseCmd(p, nArg, azArg);
19506   }else
19507 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
19508 
19509   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
19510     char *zLike = 0;
19511     char *zSql;
19512     int i;
19513     int savedShowHeader = p->showHeader;
19514     int savedShellFlags = p->shellFlgs;
19515     ShellClearFlag(p,
19516        SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
19517        |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
19518     for(i=1; i<nArg; i++){
19519       if( azArg[i][0]=='-' ){
19520         const char *z = azArg[i]+1;
19521         if( z[0]=='-' ) z++;
19522         if( strcmp(z,"preserve-rowids")==0 ){
19523 #ifdef SQLITE_OMIT_VIRTUALTABLE
19524           raw_printf(stderr, "The --preserve-rowids option is not compatible"
19525                              " with SQLITE_OMIT_VIRTUALTABLE\n");
19526           rc = 1;
19527           sqlite3_free(zLike);
19528           goto meta_command_exit;
19529 #else
19530           ShellSetFlag(p, SHFLG_PreserveRowid);
19531 #endif
19532         }else
19533         if( strcmp(z,"newlines")==0 ){
19534           ShellSetFlag(p, SHFLG_Newlines);
19535         }else
19536         if( strcmp(z,"data-only")==0 ){
19537           ShellSetFlag(p, SHFLG_DumpDataOnly);
19538         }else
19539         if( strcmp(z,"nosys")==0 ){
19540           ShellSetFlag(p, SHFLG_DumpNoSys);
19541         }else
19542         {
19543           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
19544           rc = 1;
19545           sqlite3_free(zLike);
19546           goto meta_command_exit;
19547         }
19548       }else{
19549         /* azArg[i] contains a LIKE pattern. This ".dump" request should
19550         ** only dump data for tables for which either the table name matches
19551         ** the LIKE pattern, or the table appears to be a shadow table of
19552         ** a virtual table for which the name matches the LIKE pattern.
19553         */
19554         char *zExpr = sqlite3_mprintf(
19555             "name LIKE %Q ESCAPE '\\' OR EXISTS ("
19556             "  SELECT 1 FROM sqlite_schema WHERE "
19557             "    name LIKE %Q ESCAPE '\\' AND"
19558             "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
19559             "    substr(o.name, 1, length(name)+1) == (name||'_')"
19560             ")", azArg[i], azArg[i]
19561         );
19562 
19563         if( zLike ){
19564           zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
19565         }else{
19566           zLike = zExpr;
19567         }
19568       }
19569     }
19570 
19571     open_db(p, 0);
19572 
19573     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19574       /* When playing back a "dump", the content might appear in an order
19575       ** which causes immediate foreign key constraints to be violated.
19576       ** So disable foreign-key constraint enforcement to prevent problems. */
19577       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
19578       raw_printf(p->out, "BEGIN TRANSACTION;\n");
19579     }
19580     p->writableSchema = 0;
19581     p->showHeader = 0;
19582     /* Set writable_schema=ON since doing so forces SQLite to initialize
19583     ** as much of the schema as it can even if the sqlite_schema table is
19584     ** corrupt. */
19585     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
19586     p->nErr = 0;
19587     if( zLike==0 ) zLike = sqlite3_mprintf("true");
19588     zSql = sqlite3_mprintf(
19589       "SELECT name, type, sql FROM sqlite_schema AS o "
19590       "WHERE (%s) AND type=='table'"
19591       "  AND sql NOT NULL"
19592       " ORDER BY tbl_name='sqlite_sequence', rowid",
19593       zLike
19594     );
19595     run_schema_dump_query(p,zSql);
19596     sqlite3_free(zSql);
19597     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19598       zSql = sqlite3_mprintf(
19599         "SELECT sql FROM sqlite_schema AS o "
19600         "WHERE (%s) AND sql NOT NULL"
19601         "  AND type IN ('index','trigger','view')",
19602         zLike
19603       );
19604       run_table_dump_query(p, zSql);
19605       sqlite3_free(zSql);
19606     }
19607     sqlite3_free(zLike);
19608     if( p->writableSchema ){
19609       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
19610       p->writableSchema = 0;
19611     }
19612     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
19613     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
19614     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19615       raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
19616     }
19617     p->showHeader = savedShowHeader;
19618     p->shellFlgs = savedShellFlags;
19619   }else
19620 
19621   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
19622     if( nArg==2 ){
19623       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
19624     }else{
19625       raw_printf(stderr, "Usage: .echo on|off\n");
19626       rc = 1;
19627     }
19628   }else
19629 
19630   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
19631     if( nArg==2 ){
19632       p->autoEQPtest = 0;
19633       if( p->autoEQPtrace ){
19634         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
19635         p->autoEQPtrace = 0;
19636       }
19637       if( strcmp(azArg[1],"full")==0 ){
19638         p->autoEQP = AUTOEQP_full;
19639       }else if( strcmp(azArg[1],"trigger")==0 ){
19640         p->autoEQP = AUTOEQP_trigger;
19641 #ifdef SQLITE_DEBUG
19642       }else if( strcmp(azArg[1],"test")==0 ){
19643         p->autoEQP = AUTOEQP_on;
19644         p->autoEQPtest = 1;
19645       }else if( strcmp(azArg[1],"trace")==0 ){
19646         p->autoEQP = AUTOEQP_full;
19647         p->autoEQPtrace = 1;
19648         open_db(p, 0);
19649         sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
19650         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
19651 #endif
19652       }else{
19653         p->autoEQP = (u8)booleanValue(azArg[1]);
19654       }
19655     }else{
19656       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19657       rc = 1;
19658     }
19659   }else
19660 
19661   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19662     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19663     rc = 2;
19664   }else
19665 
19666   /* The ".explain" command is automatic now.  It is largely pointless.  It
19667   ** retained purely for backwards compatibility */
19668   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19669     int val = 1;
19670     if( nArg>=2 ){
19671       if( strcmp(azArg[1],"auto")==0 ){
19672         val = 99;
19673       }else{
19674         val =  booleanValue(azArg[1]);
19675       }
19676     }
19677     if( val==1 && p->mode!=MODE_Explain ){
19678       p->normalMode = p->mode;
19679       p->mode = MODE_Explain;
19680       p->autoExplain = 0;
19681     }else if( val==0 ){
19682       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19683       p->autoExplain = 0;
19684     }else if( val==99 ){
19685       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19686       p->autoExplain = 1;
19687     }
19688   }else
19689 
19690 #ifndef SQLITE_OMIT_VIRTUALTABLE
19691   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19692     if( p->bSafeMode ){
19693       raw_printf(stderr,
19694         "Cannot run experimental commands such as \"%s\" in safe mode\n",
19695         azArg[0]);
19696       rc = 1;
19697     }else{
19698       open_db(p, 0);
19699       expertDotCommand(p, azArg, nArg);
19700     }
19701   }else
19702 #endif
19703 
19704   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19705     static const struct {
19706        const char *zCtrlName;   /* Name of a test-control option */
19707        int ctrlCode;            /* Integer code for that option */
19708        const char *zUsage;      /* Usage notes */
19709     } aCtrl[] = {
19710       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
19711       { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
19712       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
19713       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
19714       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
19715    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
19716       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
19717       { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
19718       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
19719       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
19720    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
19721     };
19722     int filectrl = -1;
19723     int iCtrl = -1;
19724     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
19725     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
19726     int n2, i;
19727     const char *zCmd = 0;
19728     const char *zSchema = 0;
19729 
19730     open_db(p, 0);
19731     zCmd = nArg>=2 ? azArg[1] : "help";
19732 
19733     if( zCmd[0]=='-'
19734      && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
19735      && nArg>=4
19736     ){
19737       zSchema = azArg[2];
19738       for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
19739       nArg -= 2;
19740       zCmd = azArg[1];
19741     }
19742 
19743     /* The argument can optionally begin with "-" or "--" */
19744     if( zCmd[0]=='-' && zCmd[1] ){
19745       zCmd++;
19746       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19747     }
19748 
19749     /* --help lists all file-controls */
19750     if( strcmp(zCmd,"help")==0 ){
19751       utf8_printf(p->out, "Available file-controls:\n");
19752       for(i=0; i<ArraySize(aCtrl); i++){
19753         utf8_printf(p->out, "  .filectrl %s %s\n",
19754                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19755       }
19756       rc = 1;
19757       goto meta_command_exit;
19758     }
19759 
19760     /* convert filectrl text option to value. allow any unique prefix
19761     ** of the option name, or a numerical value. */
19762     n2 = strlen30(zCmd);
19763     for(i=0; i<ArraySize(aCtrl); i++){
19764       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19765         if( filectrl<0 ){
19766           filectrl = aCtrl[i].ctrlCode;
19767           iCtrl = i;
19768         }else{
19769           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
19770                               "Use \".filectrl --help\" for help\n", zCmd);
19771           rc = 1;
19772           goto meta_command_exit;
19773         }
19774       }
19775     }
19776     if( filectrl<0 ){
19777       utf8_printf(stderr,"Error: unknown file-control: %s\n"
19778                          "Use \".filectrl --help\" for help\n", zCmd);
19779     }else{
19780       switch(filectrl){
19781         case SQLITE_FCNTL_SIZE_LIMIT: {
19782           if( nArg!=2 && nArg!=3 ) break;
19783           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
19784           sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
19785           isOk = 1;
19786           break;
19787         }
19788         case SQLITE_FCNTL_LOCK_TIMEOUT:
19789         case SQLITE_FCNTL_CHUNK_SIZE: {
19790           int x;
19791           if( nArg!=3 ) break;
19792           x = (int)integerValue(azArg[2]);
19793           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19794           isOk = 2;
19795           break;
19796         }
19797         case SQLITE_FCNTL_PERSIST_WAL:
19798         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
19799           int x;
19800           if( nArg!=2 && nArg!=3 ) break;
19801           x = nArg==3 ? booleanValue(azArg[2]) : -1;
19802           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19803           iRes = x;
19804           isOk = 1;
19805           break;
19806         }
19807         case SQLITE_FCNTL_DATA_VERSION:
19808         case SQLITE_FCNTL_HAS_MOVED: {
19809           int x;
19810           if( nArg!=2 ) break;
19811           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19812           iRes = x;
19813           isOk = 1;
19814           break;
19815         }
19816         case SQLITE_FCNTL_TEMPFILENAME: {
19817           char *z = 0;
19818           if( nArg!=2 ) break;
19819           sqlite3_file_control(p->db, zSchema, filectrl, &z);
19820           if( z ){
19821             utf8_printf(p->out, "%s\n", z);
19822             sqlite3_free(z);
19823           }
19824           isOk = 2;
19825           break;
19826         }
19827         case SQLITE_FCNTL_RESERVE_BYTES: {
19828           int x;
19829           if( nArg>=3 ){
19830             x = atoi(azArg[2]);
19831             sqlite3_file_control(p->db, zSchema, filectrl, &x);
19832           }
19833           x = -1;
19834           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19835           utf8_printf(p->out,"%d\n", x);
19836           isOk = 2;
19837           break;
19838         }
19839       }
19840     }
19841     if( isOk==0 && iCtrl>=0 ){
19842       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
19843       rc = 1;
19844     }else if( isOk==1 ){
19845       char zBuf[100];
19846       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
19847       raw_printf(p->out, "%s\n", zBuf);
19848     }
19849   }else
19850 
19851   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
19852     ShellState data;
19853     int doStats = 0;
19854     memcpy(&data, p, sizeof(data));
19855     data.showHeader = 0;
19856     data.cMode = data.mode = MODE_Semi;
19857     if( nArg==2 && optionMatch(azArg[1], "indent") ){
19858       data.cMode = data.mode = MODE_Pretty;
19859       nArg = 1;
19860     }
19861     if( nArg!=1 ){
19862       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
19863       rc = 1;
19864       goto meta_command_exit;
19865     }
19866     open_db(p, 0);
19867     rc = sqlite3_exec(p->db,
19868        "SELECT sql FROM"
19869        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
19870        "     FROM sqlite_schema UNION ALL"
19871        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
19872        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
19873        "ORDER BY x",
19874        callback, &data, 0
19875     );
19876     if( rc==SQLITE_OK ){
19877       sqlite3_stmt *pStmt;
19878       rc = sqlite3_prepare_v2(p->db,
19879                "SELECT rowid FROM sqlite_schema"
19880                " WHERE name GLOB 'sqlite_stat[134]'",
19881                -1, &pStmt, 0);
19882       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
19883       sqlite3_finalize(pStmt);
19884     }
19885     if( doStats==0 ){
19886       raw_printf(p->out, "/* No STAT tables available */\n");
19887     }else{
19888       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19889       data.cMode = data.mode = MODE_Insert;
19890       data.zDestTable = "sqlite_stat1";
19891       shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
19892       data.zDestTable = "sqlite_stat4";
19893       shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
19894       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19895     }
19896   }else
19897 
19898   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
19899     if( nArg==2 ){
19900       p->showHeader = booleanValue(azArg[1]);
19901       p->shellFlgs |= SHFLG_HeaderSet;
19902     }else{
19903       raw_printf(stderr, "Usage: .headers on|off\n");
19904       rc = 1;
19905     }
19906   }else
19907 
19908   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
19909     if( nArg>=2 ){
19910       n = showHelp(p->out, azArg[1]);
19911       if( n==0 ){
19912         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
19913       }
19914     }else{
19915       showHelp(p->out, 0);
19916     }
19917   }else
19918 
19919   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19920     char *zTable = 0;           /* Insert data into this table */
19921     char *zSchema = 0;          /* within this schema (may default to "main") */
19922     char *zFile = 0;            /* Name of file to extra content from */
19923     sqlite3_stmt *pStmt = NULL; /* A statement */
19924     int nCol;                   /* Number of columns in the table */
19925     int nByte;                  /* Number of bytes in an SQL string */
19926     int i, j;                   /* Loop counters */
19927     int needCommit;             /* True to COMMIT or ROLLBACK at end */
19928     int nSep;                   /* Number of bytes in p->colSeparator[] */
19929     char *zSql;                 /* An SQL statement */
19930     char *zFullTabName;         /* Table name with schema if applicable */
19931     ImportCtx sCtx;             /* Reader context */
19932     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
19933     int eVerbose = 0;           /* Larger for more console output */
19934     int nSkip = 0;              /* Initial lines to skip */
19935     int useOutputMode = 1;      /* Use output mode to determine separators */
19936     char *zCreate = 0;          /* CREATE TABLE statement text */
19937 
19938     failIfSafeMode(p, "cannot run .import in safe mode");
19939     memset(&sCtx, 0, sizeof(sCtx));
19940     sCtx.z = sqlite3_malloc64(120);
19941     if( sCtx.z==0 ){
19942       import_cleanup(&sCtx);
19943       shell_out_of_memory();
19944     }
19945     if( p->mode==MODE_Ascii ){
19946       xRead = ascii_read_one_field;
19947     }else{
19948       xRead = csv_read_one_field;
19949     }
19950     for(i=1; i<nArg; i++){
19951       char *z = azArg[i];
19952       if( z[0]=='-' && z[1]=='-' ) z++;
19953       if( z[0]!='-' ){
19954         if( zFile==0 ){
19955           zFile = z;
19956         }else if( zTable==0 ){
19957           zTable = z;
19958         }else{
19959           utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
19960           showHelp(p->out, "import");
19961           rc = 1;
19962           goto meta_command_exit;
19963         }
19964       }else if( strcmp(z,"-v")==0 ){
19965         eVerbose++;
19966       }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
19967         zSchema = azArg[++i];
19968       }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
19969         nSkip = integerValue(azArg[++i]);
19970       }else if( strcmp(z,"-ascii")==0 ){
19971         sCtx.cColSep = SEP_Unit[0];
19972         sCtx.cRowSep = SEP_Record[0];
19973         xRead = ascii_read_one_field;
19974         useOutputMode = 0;
19975       }else if( strcmp(z,"-csv")==0 ){
19976         sCtx.cColSep = ',';
19977         sCtx.cRowSep = '\n';
19978         xRead = csv_read_one_field;
19979         useOutputMode = 0;
19980       }else{
19981         utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
19982         showHelp(p->out, "import");
19983         rc = 1;
19984         goto meta_command_exit;
19985       }
19986     }
19987     if( zTable==0 ){
19988       utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
19989                   zFile==0 ? "FILE" : "TABLE");
19990       showHelp(p->out, "import");
19991       rc = 1;
19992       goto meta_command_exit;
19993     }
19994     seenInterrupt = 0;
19995     open_db(p, 0);
19996     if( useOutputMode ){
19997       /* If neither the --csv or --ascii options are specified, then set
19998       ** the column and row separator characters from the output mode. */
19999       nSep = strlen30(p->colSeparator);
20000       if( nSep==0 ){
20001         raw_printf(stderr,
20002                    "Error: non-null column separator required for import\n");
20003         rc = 1;
20004         goto meta_command_exit;
20005       }
20006       if( nSep>1 ){
20007         raw_printf(stderr,
20008               "Error: multi-character column separators not allowed"
20009               " for import\n");
20010         rc = 1;
20011         goto meta_command_exit;
20012       }
20013       nSep = strlen30(p->rowSeparator);
20014       if( nSep==0 ){
20015         raw_printf(stderr,
20016             "Error: non-null row separator required for import\n");
20017         rc = 1;
20018         goto meta_command_exit;
20019       }
20020       if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
20021         /* When importing CSV (only), if the row separator is set to the
20022         ** default output row separator, change it to the default input
20023         ** row separator.  This avoids having to maintain different input
20024         ** and output row separators. */
20025         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20026         nSep = strlen30(p->rowSeparator);
20027       }
20028       if( nSep>1 ){
20029         raw_printf(stderr, "Error: multi-character row separators not allowed"
20030                            " for import\n");
20031         rc = 1;
20032         goto meta_command_exit;
20033       }
20034       sCtx.cColSep = p->colSeparator[0];
20035       sCtx.cRowSep = p->rowSeparator[0];
20036     }
20037     sCtx.zFile = zFile;
20038     sCtx.nLine = 1;
20039     if( sCtx.zFile[0]=='|' ){
20040 #ifdef SQLITE_OMIT_POPEN
20041       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20042       rc = 1;
20043       goto meta_command_exit;
20044 #else
20045       sCtx.in = popen(sCtx.zFile+1, "r");
20046       sCtx.zFile = "<pipe>";
20047       sCtx.xCloser = pclose;
20048 #endif
20049     }else{
20050       sCtx.in = fopen(sCtx.zFile, "rb");
20051       sCtx.xCloser = fclose;
20052     }
20053     if( sCtx.in==0 ){
20054       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
20055       rc = 1;
20056       import_cleanup(&sCtx);
20057       goto meta_command_exit;
20058     }
20059     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
20060       char zSep[2];
20061       zSep[1] = 0;
20062       zSep[0] = sCtx.cColSep;
20063       utf8_printf(p->out, "Column separator ");
20064       output_c_string(p->out, zSep);
20065       utf8_printf(p->out, ", row separator ");
20066       zSep[0] = sCtx.cRowSep;
20067       output_c_string(p->out, zSep);
20068       utf8_printf(p->out, "\n");
20069     }
20070     /* Below, resources must be freed before exit. */
20071     while( (nSkip--)>0 ){
20072       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
20073     }
20074     if( zSchema!=0 ){
20075       zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
20076     }else{
20077       zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
20078     }
20079     zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
20080     if( zSql==0 || zFullTabName==0 ){
20081       import_cleanup(&sCtx);
20082       shell_out_of_memory();
20083     }
20084     nByte = strlen30(zSql);
20085     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20086     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
20087     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
20088       sqlite3 *dbCols = 0;
20089       char *zRenames = 0;
20090       char *zColDefs;
20091       zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
20092       while( xRead(&sCtx) ){
20093         zAutoColumn(sCtx.z, &dbCols, 0);
20094         if( sCtx.cTerm!=sCtx.cColSep ) break;
20095       }
20096       zColDefs = zAutoColumn(0, &dbCols, &zRenames);
20097       if( zRenames!=0 ){
20098         utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
20099                     "Columns renamed during .import %s due to duplicates:\n"
20100                     "%s\n", sCtx.zFile, zRenames);
20101         sqlite3_free(zRenames);
20102       }
20103       assert(dbCols==0);
20104       if( zColDefs==0 ){
20105         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
20106       import_fail:
20107         sqlite3_free(zCreate);
20108         sqlite3_free(zSql);
20109         sqlite3_free(zFullTabName);
20110         import_cleanup(&sCtx);
20111         rc = 1;
20112         goto meta_command_exit;
20113       }
20114       zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
20115       if( eVerbose>=1 ){
20116         utf8_printf(p->out, "%s\n", zCreate);
20117       }
20118       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
20119       if( rc ){
20120         utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
20121         goto import_fail;
20122       }
20123       sqlite3_free(zCreate);
20124       zCreate = 0;
20125       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20126     }
20127     if( rc ){
20128       if (pStmt) sqlite3_finalize(pStmt);
20129       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
20130       goto import_fail;
20131     }
20132     sqlite3_free(zSql);
20133     nCol = sqlite3_column_count(pStmt);
20134     sqlite3_finalize(pStmt);
20135     pStmt = 0;
20136     if( nCol==0 ) return 0; /* no columns, no error */
20137     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
20138     if( zSql==0 ){
20139       import_cleanup(&sCtx);
20140       shell_out_of_memory();
20141     }
20142     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
20143     j = strlen30(zSql);
20144     for(i=1; i<nCol; i++){
20145       zSql[j++] = ',';
20146       zSql[j++] = '?';
20147     }
20148     zSql[j++] = ')';
20149     zSql[j] = 0;
20150     if( eVerbose>=2 ){
20151       utf8_printf(p->out, "Insert using: %s\n", zSql);
20152     }
20153     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20154     if( rc ){
20155       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20156       if (pStmt) sqlite3_finalize(pStmt);
20157       goto import_fail;
20158     }
20159     sqlite3_free(zSql);
20160     sqlite3_free(zFullTabName);
20161     needCommit = sqlite3_get_autocommit(p->db);
20162     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
20163     do{
20164       int startLine = sCtx.nLine;
20165       for(i=0; i<nCol; i++){
20166         char *z = xRead(&sCtx);
20167         /*
20168         ** Did we reach end-of-file before finding any columns?
20169         ** If so, stop instead of NULL filling the remaining columns.
20170         */
20171         if( z==0 && i==0 ) break;
20172         /*
20173         ** Did we reach end-of-file OR end-of-line before finding any
20174         ** columns in ASCII mode?  If so, stop instead of NULL filling
20175         ** the remaining columns.
20176         */
20177         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
20178         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
20179         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
20180           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
20181                           "filling the rest with NULL\n",
20182                           sCtx.zFile, startLine, nCol, i+1);
20183           i += 2;
20184           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
20185         }
20186       }
20187       if( sCtx.cTerm==sCtx.cColSep ){
20188         do{
20189           xRead(&sCtx);
20190           i++;
20191         }while( sCtx.cTerm==sCtx.cColSep );
20192         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
20193                         "extras ignored\n",
20194                         sCtx.zFile, startLine, nCol, i);
20195       }
20196       if( i>=nCol ){
20197         sqlite3_step(pStmt);
20198         rc = sqlite3_reset(pStmt);
20199         if( rc!=SQLITE_OK ){
20200           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
20201                       startLine, sqlite3_errmsg(p->db));
20202           sCtx.nErr++;
20203         }else{
20204           sCtx.nRow++;
20205         }
20206       }
20207     }while( sCtx.cTerm!=EOF );
20208 
20209     import_cleanup(&sCtx);
20210     sqlite3_finalize(pStmt);
20211     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
20212     if( eVerbose>0 ){
20213       utf8_printf(p->out,
20214           "Added %d rows with %d errors using %d lines of input\n",
20215           sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
20216     }
20217   }else
20218 
20219 #ifndef SQLITE_UNTESTABLE
20220   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20221     char *zSql;
20222     char *zCollist = 0;
20223     sqlite3_stmt *pStmt;
20224     int tnum = 0;
20225     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
20226     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
20227     int i;
20228     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
20229       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
20230                           "       .imposter off\n");
20231       /* Also allowed, but not documented:
20232       **
20233       **    .imposter TABLE IMPOSTER
20234       **
20235       ** where TABLE is a WITHOUT ROWID table.  In that case, the
20236       ** imposter is another WITHOUT ROWID table with the columns in
20237       ** storage order. */
20238       rc = 1;
20239       goto meta_command_exit;
20240     }
20241     open_db(p, 0);
20242     if( nArg==2 ){
20243       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
20244       goto meta_command_exit;
20245     }
20246     zSql = sqlite3_mprintf(
20247       "SELECT rootpage, 0 FROM sqlite_schema"
20248       " WHERE name='%q' AND type='index'"
20249       "UNION ALL "
20250       "SELECT rootpage, 1 FROM sqlite_schema"
20251       " WHERE name='%q' AND type='table'"
20252       "   AND sql LIKE '%%without%%rowid%%'",
20253       azArg[1], azArg[1]
20254     );
20255     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20256     sqlite3_free(zSql);
20257     if( sqlite3_step(pStmt)==SQLITE_ROW ){
20258       tnum = sqlite3_column_int(pStmt, 0);
20259       isWO = sqlite3_column_int(pStmt, 1);
20260     }
20261     sqlite3_finalize(pStmt);
20262     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
20263     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20264     sqlite3_free(zSql);
20265     i = 0;
20266     while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20267       char zLabel[20];
20268       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
20269       i++;
20270       if( zCol==0 ){
20271         if( sqlite3_column_int(pStmt,1)==-1 ){
20272           zCol = "_ROWID_";
20273         }else{
20274           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
20275           zCol = zLabel;
20276         }
20277       }
20278       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
20279         lenPK = (int)strlen(zCollist);
20280       }
20281       if( zCollist==0 ){
20282         zCollist = sqlite3_mprintf("\"%w\"", zCol);
20283       }else{
20284         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
20285       }
20286     }
20287     sqlite3_finalize(pStmt);
20288     if( i==0 || tnum==0 ){
20289       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
20290       rc = 1;
20291       sqlite3_free(zCollist);
20292       goto meta_command_exit;
20293     }
20294     if( lenPK==0 ) lenPK = 100000;
20295     zSql = sqlite3_mprintf(
20296           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
20297           azArg[2], zCollist, lenPK, zCollist);
20298     sqlite3_free(zCollist);
20299     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
20300     if( rc==SQLITE_OK ){
20301       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
20302       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
20303       if( rc ){
20304         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
20305       }else{
20306         utf8_printf(stdout, "%s;\n", zSql);
20307         raw_printf(stdout,
20308           "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
20309           azArg[1], isWO ? "table" : "index"
20310         );
20311       }
20312     }else{
20313       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
20314       rc = 1;
20315     }
20316     sqlite3_free(zSql);
20317   }else
20318 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
20319 
20320 #ifdef SQLITE_ENABLE_IOTRACE
20321   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
20322     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
20323     if( iotrace && iotrace!=stdout ) fclose(iotrace);
20324     iotrace = 0;
20325     if( nArg<2 ){
20326       sqlite3IoTrace = 0;
20327     }else if( strcmp(azArg[1], "-")==0 ){
20328       sqlite3IoTrace = iotracePrintf;
20329       iotrace = stdout;
20330     }else{
20331       iotrace = fopen(azArg[1], "w");
20332       if( iotrace==0 ){
20333         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20334         sqlite3IoTrace = 0;
20335         rc = 1;
20336       }else{
20337         sqlite3IoTrace = iotracePrintf;
20338       }
20339     }
20340   }else
20341 #endif
20342 
20343   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
20344     static const struct {
20345        const char *zLimitName;   /* Name of a limit */
20346        int limitCode;            /* Integer code for that limit */
20347     } aLimit[] = {
20348       { "length",                SQLITE_LIMIT_LENGTH                    },
20349       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
20350       { "column",                SQLITE_LIMIT_COLUMN                    },
20351       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
20352       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
20353       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
20354       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
20355       { "attached",              SQLITE_LIMIT_ATTACHED                  },
20356       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
20357       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
20358       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
20359       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
20360     };
20361     int i, n2;
20362     open_db(p, 0);
20363     if( nArg==1 ){
20364       for(i=0; i<ArraySize(aLimit); i++){
20365         printf("%20s %d\n", aLimit[i].zLimitName,
20366                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
20367       }
20368     }else if( nArg>3 ){
20369       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
20370       rc = 1;
20371       goto meta_command_exit;
20372     }else{
20373       int iLimit = -1;
20374       n2 = strlen30(azArg[1]);
20375       for(i=0; i<ArraySize(aLimit); i++){
20376         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
20377           if( iLimit<0 ){
20378             iLimit = i;
20379           }else{
20380             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
20381             rc = 1;
20382             goto meta_command_exit;
20383           }
20384         }
20385       }
20386       if( iLimit<0 ){
20387         utf8_printf(stderr, "unknown limit: \"%s\"\n"
20388                         "enter \".limits\" with no arguments for a list.\n",
20389                          azArg[1]);
20390         rc = 1;
20391         goto meta_command_exit;
20392       }
20393       if( nArg==3 ){
20394         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
20395                       (int)integerValue(azArg[2]));
20396       }
20397       printf("%20s %d\n", aLimit[iLimit].zLimitName,
20398              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
20399     }
20400   }else
20401 
20402   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20403     open_db(p, 0);
20404     lintDotCommand(p, azArg, nArg);
20405   }else
20406 
20407 #ifndef SQLITE_OMIT_LOAD_EXTENSION
20408   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20409     const char *zFile, *zProc;
20410     char *zErrMsg = 0;
20411     failIfSafeMode(p, "cannot run .load in safe mode");
20412     if( nArg<2 ){
20413       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
20414       rc = 1;
20415       goto meta_command_exit;
20416     }
20417     zFile = azArg[1];
20418     zProc = nArg>=3 ? azArg[2] : 0;
20419     open_db(p, 0);
20420     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
20421     if( rc!=SQLITE_OK ){
20422       utf8_printf(stderr, "Error: %s\n", zErrMsg);
20423       sqlite3_free(zErrMsg);
20424       rc = 1;
20425     }
20426   }else
20427 #endif
20428 
20429   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20430     failIfSafeMode(p, "cannot run .log in safe mode");
20431     if( nArg!=2 ){
20432       raw_printf(stderr, "Usage: .log FILENAME\n");
20433       rc = 1;
20434     }else{
20435       const char *zFile = azArg[1];
20436       output_file_close(p->pLog);
20437       p->pLog = output_file_open(zFile, 0);
20438     }
20439   }else
20440 
20441   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20442     const char *zMode = 0;
20443     const char *zTabname = 0;
20444     int i, n2;
20445     ColModeOpts cmOpts = ColModeOpts_default;
20446     for(i=1; i<nArg; i++){
20447       const char *z = azArg[i];
20448       if( optionMatch(z,"wrap") && i+1<nArg ){
20449         cmOpts.iWrap = integerValue(azArg[++i]);
20450       }else if( optionMatch(z,"ww") ){
20451         cmOpts.bWordWrap = 1;
20452       }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
20453         cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
20454       }else if( optionMatch(z,"quote") ){
20455         cmOpts.bQuote = 1;
20456       }else if( optionMatch(z,"noquote") ){
20457         cmOpts.bQuote = 0;
20458       }else if( zMode==0 ){
20459         zMode = z;
20460         /* Apply defaults for qbox pseudo-mods. If that
20461          * overwrites already-set values, user was informed of this.
20462          */
20463         if( strcmp(z, "qbox")==0 ){
20464           ColModeOpts cmo = ColModeOpts_default_qbox;
20465           zMode = "box";
20466           cmOpts = cmo;
20467         }
20468       }else if( zTabname==0 ){
20469         zTabname = z;
20470       }else if( z[0]=='-' ){
20471         utf8_printf(stderr, "unknown option: %s\n", z);
20472         utf8_printf(stderr, "options:\n"
20473                             "  --noquote\n"
20474                             "  --quote\n"
20475                             "  --wordwrap on/off\n"
20476                             "  --wrap N\n"
20477                             "  --ww\n");
20478         rc = 1;
20479         goto meta_command_exit;
20480       }else{
20481         utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20482         rc = 1;
20483         goto meta_command_exit;
20484       }
20485     }
20486     if( zMode==0 ){
20487       if( p->mode==MODE_Column
20488        || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
20489       ){
20490         raw_printf
20491           (p->out,
20492            "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
20493            modeDescr[p->mode], p->cmOpts.iWrap,
20494            p->cmOpts.bWordWrap ? "on" : "off",
20495            p->cmOpts.bQuote ? "" : "no");
20496       }else{
20497         raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20498       }
20499       zMode = modeDescr[p->mode];
20500     }
20501     n2 = strlen30(zMode);
20502     if( strncmp(zMode,"lines",n2)==0 ){
20503       p->mode = MODE_Line;
20504       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20505     }else if( strncmp(zMode,"columns",n2)==0 ){
20506       p->mode = MODE_Column;
20507       if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
20508         p->showHeader = 1;
20509       }
20510       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20511       p->cmOpts = cmOpts;
20512     }else if( strncmp(zMode,"list",n2)==0 ){
20513       p->mode = MODE_List;
20514       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
20515       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20516     }else if( strncmp(zMode,"html",n2)==0 ){
20517       p->mode = MODE_Html;
20518     }else if( strncmp(zMode,"tcl",n2)==0 ){
20519       p->mode = MODE_Tcl;
20520       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
20521       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20522     }else if( strncmp(zMode,"csv",n2)==0 ){
20523       p->mode = MODE_Csv;
20524       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20525       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20526     }else if( strncmp(zMode,"tabs",n2)==0 ){
20527       p->mode = MODE_List;
20528       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20529     }else if( strncmp(zMode,"insert",n2)==0 ){
20530       p->mode = MODE_Insert;
20531       set_table_name(p, zTabname ? zTabname : "table");
20532     }else if( strncmp(zMode,"quote",n2)==0 ){
20533       p->mode = MODE_Quote;
20534       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20535       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20536     }else if( strncmp(zMode,"ascii",n2)==0 ){
20537       p->mode = MODE_Ascii;
20538       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
20539       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20540     }else if( strncmp(zMode,"markdown",n2)==0 ){
20541       p->mode = MODE_Markdown;
20542       p->cmOpts = cmOpts;
20543     }else if( strncmp(zMode,"table",n2)==0 ){
20544       p->mode = MODE_Table;
20545       p->cmOpts = cmOpts;
20546     }else if( strncmp(zMode,"box",n2)==0 ){
20547       p->mode = MODE_Box;
20548       p->cmOpts = cmOpts;
20549     }else if( strncmp(zMode,"count",n2)==0 ){
20550       p->mode = MODE_Count;
20551     }else if( strncmp(zMode,"off",n2)==0 ){
20552       p->mode = MODE_Off;
20553     }else if( strncmp(zMode,"json",n2)==0 ){
20554       p->mode = MODE_Json;
20555     }else{
20556       raw_printf(stderr, "Error: mode should be one of: "
20557          "ascii box column csv html insert json line list markdown "
20558          "qbox quote table tabs tcl\n");
20559       rc = 1;
20560     }
20561     p->cMode = p->mode;
20562   }else
20563 
20564   if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20565     if( nArg!=2 ){
20566       raw_printf(stderr, "Usage: .nonce NONCE\n");
20567       rc = 1;
20568     }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
20569       raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
20570                  p->lineno, azArg[1]);
20571       exit(1);
20572     }else{
20573       p->bSafeMode = 0;
20574       return 0;  /* Return immediately to bypass the safe mode reset
20575                  ** at the end of this procedure */
20576     }
20577   }else
20578 
20579   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20580     if( nArg==2 ){
20581       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20582                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
20583     }else{
20584       raw_printf(stderr, "Usage: .nullvalue STRING\n");
20585       rc = 1;
20586     }
20587   }else
20588 
20589   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
20590     const char *zFN = 0;     /* Pointer to constant filename */
20591     char *zNewFilename = 0;  /* Name of the database file to open */
20592     int iName = 1;           /* Index in azArg[] of the filename */
20593     int newFlag = 0;         /* True to delete file before opening */
20594     int openMode = SHELL_OPEN_UNSPEC;
20595 
20596     /* Check for command-line arguments */
20597     for(iName=1; iName<nArg; iName++){
20598       const char *z = azArg[iName];
20599       if( optionMatch(z,"new") ){
20600         newFlag = 1;
20601 #ifdef SQLITE_HAVE_ZLIB
20602       }else if( optionMatch(z, "zip") ){
20603         openMode = SHELL_OPEN_ZIPFILE;
20604 #endif
20605       }else if( optionMatch(z, "append") ){
20606         openMode = SHELL_OPEN_APPENDVFS;
20607       }else if( optionMatch(z, "readonly") ){
20608         openMode = SHELL_OPEN_READONLY;
20609       }else if( optionMatch(z, "nofollow") ){
20610         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
20611 #ifndef SQLITE_OMIT_DESERIALIZE
20612       }else if( optionMatch(z, "deserialize") ){
20613         openMode = SHELL_OPEN_DESERIALIZE;
20614       }else if( optionMatch(z, "hexdb") ){
20615         openMode = SHELL_OPEN_HEXDB;
20616       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20617         p->szMax = integerValue(azArg[++iName]);
20618 #endif /* SQLITE_OMIT_DESERIALIZE */
20619       }else if( z[0]=='-' ){
20620         utf8_printf(stderr, "unknown option: %s\n", z);
20621         rc = 1;
20622         goto meta_command_exit;
20623       }else if( zFN ){
20624         utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20625         rc = 1;
20626         goto meta_command_exit;
20627       }else{
20628         zFN = z;
20629       }
20630     }
20631 
20632     /* Close the existing database */
20633     session_close_all(p, -1);
20634     close_db(p->db);
20635     p->db = 0;
20636     p->pAuxDb->zDbFilename = 0;
20637     sqlite3_free(p->pAuxDb->zFreeOnClose);
20638     p->pAuxDb->zFreeOnClose = 0;
20639     p->openMode = openMode;
20640     p->openFlags = 0;
20641     p->szMax = 0;
20642 
20643     /* If a filename is specified, try to open it first */
20644     if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20645       if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20646       if( p->bSafeMode
20647        && p->openMode!=SHELL_OPEN_HEXDB
20648        && zFN
20649        && strcmp(zFN,":memory:")!=0
20650       ){
20651         failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20652       }
20653       if( zFN ){
20654         zNewFilename = sqlite3_mprintf("%s", zFN);
20655         shell_check_oom(zNewFilename);
20656       }else{
20657         zNewFilename = 0;
20658       }
20659       p->pAuxDb->zDbFilename = zNewFilename;
20660       open_db(p, OPEN_DB_KEEPALIVE);
20661       if( p->db==0 ){
20662         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
20663         sqlite3_free(zNewFilename);
20664       }else{
20665         p->pAuxDb->zFreeOnClose = zNewFilename;
20666       }
20667     }
20668     if( p->db==0 ){
20669       /* As a fall-back open a TEMP database */
20670       p->pAuxDb->zDbFilename = 0;
20671       open_db(p, 0);
20672     }
20673   }else
20674 
20675   if( (c=='o'
20676         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20677    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20678   ){
20679     char *zFile = 0;
20680     int bTxtMode = 0;
20681     int i;
20682     int eMode = 0;
20683     int bBOM = 0;
20684     int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
20685 
20686     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
20687     if( c=='e' ){
20688       eMode = 'x';
20689       bOnce = 2;
20690     }else if( strncmp(azArg[0],"once",n)==0 ){
20691       bOnce = 1;
20692     }
20693     for(i=1; i<nArg; i++){
20694       char *z = azArg[i];
20695       if( z[0]=='-' ){
20696         if( z[1]=='-' ) z++;
20697         if( strcmp(z,"-bom")==0 ){
20698           bBOM = 1;
20699         }else if( c!='e' && strcmp(z,"-x")==0 ){
20700           eMode = 'x';  /* spreadsheet */
20701         }else if( c!='e' && strcmp(z,"-e")==0 ){
20702           eMode = 'e';  /* text editor */
20703         }else{
20704           utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
20705                       azArg[i]);
20706           showHelp(p->out, azArg[0]);
20707           rc = 1;
20708           goto meta_command_exit;
20709         }
20710       }else if( zFile==0 && eMode!='e' && eMode!='x' ){
20711         zFile = sqlite3_mprintf("%s", z);
20712         if( zFile && zFile[0]=='|' ){
20713           while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
20714           break;
20715         }
20716       }else{
20717         utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
20718                     azArg[i]);
20719         showHelp(p->out, azArg[0]);
20720         rc = 1;
20721         sqlite3_free(zFile);
20722         goto meta_command_exit;
20723       }
20724     }
20725     if( zFile==0 ){
20726       zFile = sqlite3_mprintf("stdout");
20727     }
20728     if( bOnce ){
20729       p->outCount = 2;
20730     }else{
20731       p->outCount = 0;
20732     }
20733     output_reset(p);
20734 #ifndef SQLITE_NOHAVE_SYSTEM
20735     if( eMode=='e' || eMode=='x' ){
20736       p->doXdgOpen = 1;
20737       outputModePush(p);
20738       if( eMode=='x' ){
20739         /* spreadsheet mode.  Output as CSV. */
20740         newTempFile(p, "csv");
20741         ShellClearFlag(p, SHFLG_Echo);
20742         p->mode = MODE_Csv;
20743         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20744         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20745       }else{
20746         /* text editor mode */
20747         newTempFile(p, "txt");
20748         bTxtMode = 1;
20749       }
20750       sqlite3_free(zFile);
20751       zFile = sqlite3_mprintf("%s", p->zTempFile);
20752     }
20753 #endif /* SQLITE_NOHAVE_SYSTEM */
20754     shell_check_oom(zFile);
20755     if( zFile[0]=='|' ){
20756 #ifdef SQLITE_OMIT_POPEN
20757       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20758       rc = 1;
20759       p->out = stdout;
20760 #else
20761       p->out = popen(zFile + 1, "w");
20762       if( p->out==0 ){
20763         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
20764         p->out = stdout;
20765         rc = 1;
20766       }else{
20767         if( bBOM ) fprintf(p->out,"\357\273\277");
20768         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20769       }
20770 #endif
20771     }else{
20772       p->out = output_file_open(zFile, bTxtMode);
20773       if( p->out==0 ){
20774         if( strcmp(zFile,"off")!=0 ){
20775           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
20776         }
20777         p->out = stdout;
20778         rc = 1;
20779       } else {
20780         if( bBOM ) fprintf(p->out,"\357\273\277");
20781         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20782       }
20783     }
20784     sqlite3_free(zFile);
20785   }else
20786 
20787   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20788     open_db(p,0);
20789     if( nArg<=1 ) goto parameter_syntax_error;
20790 
20791     /* .parameter clear
20792     ** Clear all bind parameters by dropping the TEMP table that holds them.
20793     */
20794     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
20795       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
20796                    0, 0, 0);
20797     }else
20798 
20799     /* .parameter list
20800     ** List all bind parameters.
20801     */
20802     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
20803       sqlite3_stmt *pStmt = 0;
20804       int rx;
20805       int len = 0;
20806       rx = sqlite3_prepare_v2(p->db,
20807              "SELECT max(length(key)) "
20808              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20809       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20810         len = sqlite3_column_int(pStmt, 0);
20811         if( len>40 ) len = 40;
20812       }
20813       sqlite3_finalize(pStmt);
20814       pStmt = 0;
20815       if( len ){
20816         rx = sqlite3_prepare_v2(p->db,
20817              "SELECT key, quote(value) "
20818              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20819         while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20820           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
20821                       sqlite3_column_text(pStmt,1));
20822         }
20823         sqlite3_finalize(pStmt);
20824       }
20825     }else
20826 
20827     /* .parameter init
20828     ** Make sure the TEMP table used to hold bind parameters exists.
20829     ** Create it if necessary.
20830     */
20831     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
20832       bind_table_init(p);
20833     }else
20834 
20835     /* .parameter set NAME VALUE
20836     ** Set or reset a bind parameter.  NAME should be the full parameter
20837     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
20838     ** VALUE can be in either SQL literal notation, or if not it will be
20839     ** understood to be a text string.
20840     */
20841     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
20842       int rx;
20843       char *zSql;
20844       sqlite3_stmt *pStmt;
20845       const char *zKey = azArg[2];
20846       const char *zValue = azArg[3];
20847       bind_table_init(p);
20848       zSql = sqlite3_mprintf(
20849                   "REPLACE INTO temp.sqlite_parameters(key,value)"
20850                   "VALUES(%Q,%s);", zKey, zValue);
20851       shell_check_oom(zSql);
20852       pStmt = 0;
20853       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20854       sqlite3_free(zSql);
20855       if( rx!=SQLITE_OK ){
20856         sqlite3_finalize(pStmt);
20857         pStmt = 0;
20858         zSql = sqlite3_mprintf(
20859                    "REPLACE INTO temp.sqlite_parameters(key,value)"
20860                    "VALUES(%Q,%Q);", zKey, zValue);
20861         shell_check_oom(zSql);
20862         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20863         sqlite3_free(zSql);
20864         if( rx!=SQLITE_OK ){
20865           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
20866           sqlite3_finalize(pStmt);
20867           pStmt = 0;
20868           rc = 1;
20869         }
20870       }
20871       sqlite3_step(pStmt);
20872       sqlite3_finalize(pStmt);
20873     }else
20874 
20875     /* .parameter unset NAME
20876     ** Remove the NAME binding from the parameter binding table, if it
20877     ** exists.
20878     */
20879     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
20880       char *zSql = sqlite3_mprintf(
20881           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20882       shell_check_oom(zSql);
20883       sqlite3_exec(p->db, zSql, 0, 0, 0);
20884       sqlite3_free(zSql);
20885     }else
20886     /* If no command name matches, show a syntax error */
20887     parameter_syntax_error:
20888     showHelp(p->out, "parameter");
20889   }else
20890 
20891   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
20892     int i;
20893     for(i=1; i<nArg; i++){
20894       if( i>1 ) raw_printf(p->out, " ");
20895       utf8_printf(p->out, "%s", azArg[i]);
20896     }
20897     raw_printf(p->out, "\n");
20898   }else
20899 
20900 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
20901   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
20902     int i;
20903     int nn = 0;
20904     p->flgProgress = 0;
20905     p->mxProgress = 0;
20906     p->nProgress = 0;
20907     for(i=1; i<nArg; i++){
20908       const char *z = azArg[i];
20909       if( z[0]=='-' ){
20910         z++;
20911         if( z[0]=='-' ) z++;
20912         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
20913           p->flgProgress |= SHELL_PROGRESS_QUIET;
20914           continue;
20915         }
20916         if( strcmp(z,"reset")==0 ){
20917           p->flgProgress |= SHELL_PROGRESS_RESET;
20918           continue;
20919         }
20920         if( strcmp(z,"once")==0 ){
20921           p->flgProgress |= SHELL_PROGRESS_ONCE;
20922           continue;
20923         }
20924         if( strcmp(z,"limit")==0 ){
20925           if( i+1>=nArg ){
20926             utf8_printf(stderr, "Error: missing argument on --limit\n");
20927             rc = 1;
20928             goto meta_command_exit;
20929           }else{
20930             p->mxProgress = (int)integerValue(azArg[++i]);
20931           }
20932           continue;
20933         }
20934         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
20935         rc = 1;
20936         goto meta_command_exit;
20937       }else{
20938         nn = (int)integerValue(z);
20939       }
20940     }
20941     open_db(p, 0);
20942     sqlite3_progress_handler(p->db, nn, progress_handler, p);
20943   }else
20944 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
20945 
20946   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
20947     if( nArg >= 2) {
20948       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
20949     }
20950     if( nArg >= 3) {
20951       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
20952     }
20953   }else
20954 
20955   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
20956     rc = 2;
20957   }else
20958 
20959   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
20960     FILE *inSaved = p->in;
20961     int savedLineno = p->lineno;
20962     failIfSafeMode(p, "cannot run .read in safe mode");
20963     if( nArg!=2 ){
20964       raw_printf(stderr, "Usage: .read FILE\n");
20965       rc = 1;
20966       goto meta_command_exit;
20967     }
20968     if( azArg[1][0]=='|' ){
20969 #ifdef SQLITE_OMIT_POPEN
20970       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20971       rc = 1;
20972       p->out = stdout;
20973 #else
20974       p->in = popen(azArg[1]+1, "r");
20975       if( p->in==0 ){
20976         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20977         rc = 1;
20978       }else{
20979         rc = process_input(p);
20980         pclose(p->in);
20981       }
20982 #endif
20983     }else if( (p->in = openChrSource(azArg[1]))==0 ){
20984       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
20985       rc = 1;
20986     }else{
20987       rc = process_input(p);
20988       fclose(p->in);
20989     }
20990     p->in = inSaved;
20991     p->lineno = savedLineno;
20992   }else
20993 
20994   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20995     const char *zSrcFile;
20996     const char *zDb;
20997     sqlite3 *pSrc;
20998     sqlite3_backup *pBackup;
20999     int nTimeout = 0;
21000 
21001     failIfSafeMode(p, "cannot run .restore in safe mode");
21002     if( nArg==2 ){
21003       zSrcFile = azArg[1];
21004       zDb = "main";
21005     }else if( nArg==3 ){
21006       zSrcFile = azArg[2];
21007       zDb = azArg[1];
21008     }else{
21009       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
21010       rc = 1;
21011       goto meta_command_exit;
21012     }
21013     rc = sqlite3_open(zSrcFile, &pSrc);
21014     if( rc!=SQLITE_OK ){
21015       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
21016       close_db(pSrc);
21017       return 1;
21018     }
21019     open_db(p, 0);
21020     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
21021     if( pBackup==0 ){
21022       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21023       close_db(pSrc);
21024       return 1;
21025     }
21026     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
21027           || rc==SQLITE_BUSY  ){
21028       if( rc==SQLITE_BUSY ){
21029         if( nTimeout++ >= 3 ) break;
21030         sqlite3_sleep(100);
21031       }
21032     }
21033     sqlite3_backup_finish(pBackup);
21034     if( rc==SQLITE_DONE ){
21035       rc = 0;
21036     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
21037       raw_printf(stderr, "Error: source database is busy\n");
21038       rc = 1;
21039     }else{
21040       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21041       rc = 1;
21042     }
21043     close_db(pSrc);
21044   }else
21045 
21046   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21047     if( nArg==2 ){
21048       p->scanstatsOn = (u8)booleanValue(azArg[1]);
21049 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
21050       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
21051 #endif
21052     }else{
21053       raw_printf(stderr, "Usage: .scanstats on|off\n");
21054       rc = 1;
21055     }
21056   }else
21057 
21058   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
21059     ShellText sSelect;
21060     ShellState data;
21061     char *zErrMsg = 0;
21062     const char *zDiv = "(";
21063     const char *zName = 0;
21064     int iSchema = 0;
21065     int bDebug = 0;
21066     int bNoSystemTabs = 0;
21067     int ii;
21068 
21069     open_db(p, 0);
21070     memcpy(&data, p, sizeof(data));
21071     data.showHeader = 0;
21072     data.cMode = data.mode = MODE_Semi;
21073     initText(&sSelect);
21074     for(ii=1; ii<nArg; ii++){
21075       if( optionMatch(azArg[ii],"indent") ){
21076         data.cMode = data.mode = MODE_Pretty;
21077       }else if( optionMatch(azArg[ii],"debug") ){
21078         bDebug = 1;
21079       }else if( optionMatch(azArg[ii],"nosys") ){
21080         bNoSystemTabs = 1;
21081       }else if( azArg[ii][0]=='-' ){
21082         utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
21083         rc = 1;
21084         goto meta_command_exit;
21085       }else if( zName==0 ){
21086         zName = azArg[ii];
21087       }else{
21088         raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
21089         rc = 1;
21090         goto meta_command_exit;
21091       }
21092     }
21093     if( zName!=0 ){
21094       int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
21095                   || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
21096                   || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
21097                   || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
21098       if( isSchema ){
21099         char *new_argv[2], *new_colv[2];
21100         new_argv[0] = sqlite3_mprintf(
21101                       "CREATE TABLE %s (\n"
21102                       "  type text,\n"
21103                       "  name text,\n"
21104                       "  tbl_name text,\n"
21105                       "  rootpage integer,\n"
21106                       "  sql text\n"
21107                       ")", zName);
21108         shell_check_oom(new_argv[0]);
21109         new_argv[1] = 0;
21110         new_colv[0] = "sql";
21111         new_colv[1] = 0;
21112         callback(&data, 1, new_argv, new_colv);
21113         sqlite3_free(new_argv[0]);
21114       }
21115     }
21116     if( zDiv ){
21117       sqlite3_stmt *pStmt = 0;
21118       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
21119                               -1, &pStmt, 0);
21120       if( rc ){
21121         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21122         sqlite3_finalize(pStmt);
21123         rc = 1;
21124         goto meta_command_exit;
21125       }
21126       appendText(&sSelect, "SELECT sql FROM", 0);
21127       iSchema = 0;
21128       while( sqlite3_step(pStmt)==SQLITE_ROW ){
21129         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
21130         char zScNum[30];
21131         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
21132         appendText(&sSelect, zDiv, 0);
21133         zDiv = " UNION ALL ";
21134         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
21135         if( sqlite3_stricmp(zDb, "main")!=0 ){
21136           appendText(&sSelect, zDb, '\'');
21137         }else{
21138           appendText(&sSelect, "NULL", 0);
21139         }
21140         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
21141         appendText(&sSelect, zScNum, 0);
21142         appendText(&sSelect, " AS snum, ", 0);
21143         appendText(&sSelect, zDb, '\'');
21144         appendText(&sSelect, " AS sname FROM ", 0);
21145         appendText(&sSelect, zDb, quoteChar(zDb));
21146         appendText(&sSelect, ".sqlite_schema", 0);
21147       }
21148       sqlite3_finalize(pStmt);
21149 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
21150       if( zName ){
21151         appendText(&sSelect,
21152            " UNION ALL SELECT shell_module_schema(name),"
21153            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
21154         0);
21155       }
21156 #endif
21157       appendText(&sSelect, ") WHERE ", 0);
21158       if( zName ){
21159         char *zQarg = sqlite3_mprintf("%Q", zName);
21160         int bGlob;
21161         shell_check_oom(zQarg);
21162         bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
21163                 strchr(zName, '[') != 0;
21164         if( strchr(zName, '.') ){
21165           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
21166         }else{
21167           appendText(&sSelect, "lower(tbl_name)", 0);
21168         }
21169         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
21170         appendText(&sSelect, zQarg, 0);
21171         if( !bGlob ){
21172           appendText(&sSelect, " ESCAPE '\\' ", 0);
21173         }
21174         appendText(&sSelect, " AND ", 0);
21175         sqlite3_free(zQarg);
21176       }
21177       if( bNoSystemTabs ){
21178         appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
21179       }
21180       appendText(&sSelect, "sql IS NOT NULL"
21181                            " ORDER BY snum, rowid", 0);
21182       if( bDebug ){
21183         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
21184       }else{
21185         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
21186       }
21187       freeText(&sSelect);
21188     }
21189     if( zErrMsg ){
21190       utf8_printf(stderr,"Error: %s\n", zErrMsg);
21191       sqlite3_free(zErrMsg);
21192       rc = 1;
21193     }else if( rc != SQLITE_OK ){
21194       raw_printf(stderr,"Error: querying schema information\n");
21195       rc = 1;
21196     }else{
21197       rc = 0;
21198     }
21199   }else
21200 
21201   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
21202     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21203     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
21204   }else
21205 
21206 #if defined(SQLITE_ENABLE_SESSION)
21207   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
21208     struct AuxDb *pAuxDb = p->pAuxDb;
21209     OpenSession *pSession = &pAuxDb->aSession[0];
21210     char **azCmd = &azArg[1];
21211     int iSes = 0;
21212     int nCmd = nArg - 1;
21213     int i;
21214     if( nArg<=1 ) goto session_syntax_error;
21215     open_db(p, 0);
21216     if( nArg>=3 ){
21217       for(iSes=0; iSes<pAuxDb->nSession; iSes++){
21218         if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
21219       }
21220       if( iSes<pAuxDb->nSession ){
21221         pSession = &pAuxDb->aSession[iSes];
21222         azCmd++;
21223         nCmd--;
21224       }else{
21225         pSession = &pAuxDb->aSession[0];
21226         iSes = 0;
21227       }
21228     }
21229 
21230     /* .session attach TABLE
21231     ** Invoke the sqlite3session_attach() interface to attach a particular
21232     ** table so that it is never filtered.
21233     */
21234     if( strcmp(azCmd[0],"attach")==0 ){
21235       if( nCmd!=2 ) goto session_syntax_error;
21236       if( pSession->p==0 ){
21237         session_not_open:
21238         raw_printf(stderr, "ERROR: No sessions are open\n");
21239       }else{
21240         rc = sqlite3session_attach(pSession->p, azCmd[1]);
21241         if( rc ){
21242           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
21243           rc = 0;
21244         }
21245       }
21246     }else
21247 
21248     /* .session changeset FILE
21249     ** .session patchset FILE
21250     ** Write a changeset or patchset into a file.  The file is overwritten.
21251     */
21252     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
21253       FILE *out = 0;
21254       failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
21255       if( nCmd!=2 ) goto session_syntax_error;
21256       if( pSession->p==0 ) goto session_not_open;
21257       out = fopen(azCmd[1], "wb");
21258       if( out==0 ){
21259         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
21260                     azCmd[1]);
21261       }else{
21262         int szChng;
21263         void *pChng;
21264         if( azCmd[0][0]=='c' ){
21265           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
21266         }else{
21267           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
21268         }
21269         if( rc ){
21270           printf("Error: error code %d\n", rc);
21271           rc = 0;
21272         }
21273         if( pChng
21274           && fwrite(pChng, szChng, 1, out)!=1 ){
21275           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
21276                   szChng);
21277         }
21278         sqlite3_free(pChng);
21279         fclose(out);
21280       }
21281     }else
21282 
21283     /* .session close
21284     ** Close the identified session
21285     */
21286     if( strcmp(azCmd[0], "close")==0 ){
21287       if( nCmd!=1 ) goto session_syntax_error;
21288       if( pAuxDb->nSession ){
21289         session_close(pSession);
21290         pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
21291       }
21292     }else
21293 
21294     /* .session enable ?BOOLEAN?
21295     ** Query or set the enable flag
21296     */
21297     if( strcmp(azCmd[0], "enable")==0 ){
21298       int ii;
21299       if( nCmd>2 ) goto session_syntax_error;
21300       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21301       if( pAuxDb->nSession ){
21302         ii = sqlite3session_enable(pSession->p, ii);
21303         utf8_printf(p->out, "session %s enable flag = %d\n",
21304                     pSession->zName, ii);
21305       }
21306     }else
21307 
21308     /* .session filter GLOB ....
21309     ** Set a list of GLOB patterns of table names to be excluded.
21310     */
21311     if( strcmp(azCmd[0], "filter")==0 ){
21312       int ii, nByte;
21313       if( nCmd<2 ) goto session_syntax_error;
21314       if( pAuxDb->nSession ){
21315         for(ii=0; ii<pSession->nFilter; ii++){
21316           sqlite3_free(pSession->azFilter[ii]);
21317         }
21318         sqlite3_free(pSession->azFilter);
21319         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
21320         pSession->azFilter = sqlite3_malloc( nByte );
21321         if( pSession->azFilter==0 ){
21322           raw_printf(stderr, "Error: out or memory\n");
21323           exit(1);
21324         }
21325         for(ii=1; ii<nCmd; ii++){
21326           char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
21327           shell_check_oom(x);
21328         }
21329         pSession->nFilter = ii-1;
21330       }
21331     }else
21332 
21333     /* .session indirect ?BOOLEAN?
21334     ** Query or set the indirect flag
21335     */
21336     if( strcmp(azCmd[0], "indirect")==0 ){
21337       int ii;
21338       if( nCmd>2 ) goto session_syntax_error;
21339       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21340       if( pAuxDb->nSession ){
21341         ii = sqlite3session_indirect(pSession->p, ii);
21342         utf8_printf(p->out, "session %s indirect flag = %d\n",
21343                     pSession->zName, ii);
21344       }
21345     }else
21346 
21347     /* .session isempty
21348     ** Determine if the session is empty
21349     */
21350     if( strcmp(azCmd[0], "isempty")==0 ){
21351       int ii;
21352       if( nCmd!=1 ) goto session_syntax_error;
21353       if( pAuxDb->nSession ){
21354         ii = sqlite3session_isempty(pSession->p);
21355         utf8_printf(p->out, "session %s isempty flag = %d\n",
21356                     pSession->zName, ii);
21357       }
21358     }else
21359 
21360     /* .session list
21361     ** List all currently open sessions
21362     */
21363     if( strcmp(azCmd[0],"list")==0 ){
21364       for(i=0; i<pAuxDb->nSession; i++){
21365         utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
21366       }
21367     }else
21368 
21369     /* .session open DB NAME
21370     ** Open a new session called NAME on the attached database DB.
21371     ** DB is normally "main".
21372     */
21373     if( strcmp(azCmd[0],"open")==0 ){
21374       char *zName;
21375       if( nCmd!=3 ) goto session_syntax_error;
21376       zName = azCmd[2];
21377       if( zName[0]==0 ) goto session_syntax_error;
21378       for(i=0; i<pAuxDb->nSession; i++){
21379         if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
21380           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
21381           goto meta_command_exit;
21382         }
21383       }
21384       if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
21385         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
21386         goto meta_command_exit;
21387       }
21388       pSession = &pAuxDb->aSession[pAuxDb->nSession];
21389       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
21390       if( rc ){
21391         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
21392         rc = 0;
21393         goto meta_command_exit;
21394       }
21395       pSession->nFilter = 0;
21396       sqlite3session_table_filter(pSession->p, session_filter, pSession);
21397       pAuxDb->nSession++;
21398       pSession->zName = sqlite3_mprintf("%s", zName);
21399       shell_check_oom(pSession->zName);
21400     }else
21401     /* If no command name matches, show a syntax error */
21402     session_syntax_error:
21403     showHelp(p->out, "session");
21404   }else
21405 #endif
21406 
21407 #ifdef SQLITE_DEBUG
21408   /* Undocumented commands for internal testing.  Subject to change
21409   ** without notice. */
21410   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
21411     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
21412       int i, v;
21413       for(i=1; i<nArg; i++){
21414         v = booleanValue(azArg[i]);
21415         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
21416       }
21417     }
21418     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
21419       int i; sqlite3_int64 v;
21420       for(i=1; i<nArg; i++){
21421         char zBuf[200];
21422         v = integerValue(azArg[i]);
21423         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
21424         utf8_printf(p->out, "%s", zBuf);
21425       }
21426     }
21427   }else
21428 #endif
21429 
21430   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
21431     int bIsInit = 0;         /* True to initialize the SELFTEST table */
21432     int bVerbose = 0;        /* Verbose output */
21433     int bSelftestExists;     /* True if SELFTEST already exists */
21434     int i, k;                /* Loop counters */
21435     int nTest = 0;           /* Number of tests runs */
21436     int nErr = 0;            /* Number of errors seen */
21437     ShellText str;           /* Answer for a query */
21438     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
21439 
21440     open_db(p,0);
21441     for(i=1; i<nArg; i++){
21442       const char *z = azArg[i];
21443       if( z[0]=='-' && z[1]=='-' ) z++;
21444       if( strcmp(z,"-init")==0 ){
21445         bIsInit = 1;
21446       }else
21447       if( strcmp(z,"-v")==0 ){
21448         bVerbose++;
21449       }else
21450       {
21451         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21452                     azArg[i], azArg[0]);
21453         raw_printf(stderr, "Should be one of: --init -v\n");
21454         rc = 1;
21455         goto meta_command_exit;
21456       }
21457     }
21458     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
21459            != SQLITE_OK ){
21460       bSelftestExists = 0;
21461     }else{
21462       bSelftestExists = 1;
21463     }
21464     if( bIsInit ){
21465       createSelftestTable(p);
21466       bSelftestExists = 1;
21467     }
21468     initText(&str);
21469     appendText(&str, "x", 0);
21470     for(k=bSelftestExists; k>=0; k--){
21471       if( k==1 ){
21472         rc = sqlite3_prepare_v2(p->db,
21473             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
21474             -1, &pStmt, 0);
21475       }else{
21476         rc = sqlite3_prepare_v2(p->db,
21477           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
21478           "      (1,'run','PRAGMA integrity_check','ok')",
21479           -1, &pStmt, 0);
21480       }
21481       if( rc ){
21482         raw_printf(stderr, "Error querying the selftest table\n");
21483         rc = 1;
21484         sqlite3_finalize(pStmt);
21485         goto meta_command_exit;
21486       }
21487       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
21488         int tno = sqlite3_column_int(pStmt, 0);
21489         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
21490         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
21491         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
21492 
21493         if( zOp==0 ) continue;
21494         if( zSql==0 ) continue;
21495         if( zAns==0 ) continue;
21496         k = 0;
21497         if( bVerbose>0 ){
21498           printf("%d: %s %s\n", tno, zOp, zSql);
21499         }
21500         if( strcmp(zOp,"memo")==0 ){
21501           utf8_printf(p->out, "%s\n", zSql);
21502         }else
21503         if( strcmp(zOp,"run")==0 ){
21504           char *zErrMsg = 0;
21505           str.n = 0;
21506           str.z[0] = 0;
21507           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
21508           nTest++;
21509           if( bVerbose ){
21510             utf8_printf(p->out, "Result: %s\n", str.z);
21511           }
21512           if( rc || zErrMsg ){
21513             nErr++;
21514             rc = 1;
21515             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
21516             sqlite3_free(zErrMsg);
21517           }else if( strcmp(zAns,str.z)!=0 ){
21518             nErr++;
21519             rc = 1;
21520             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
21521             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
21522           }
21523         }else
21524         {
21525           utf8_printf(stderr,
21526             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
21527           rc = 1;
21528           break;
21529         }
21530       } /* End loop over rows of content from SELFTEST */
21531       sqlite3_finalize(pStmt);
21532     } /* End loop over k */
21533     freeText(&str);
21534     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
21535   }else
21536 
21537   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
21538     if( nArg<2 || nArg>3 ){
21539       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
21540       rc = 1;
21541     }
21542     if( nArg>=2 ){
21543       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
21544                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
21545     }
21546     if( nArg>=3 ){
21547       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
21548                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
21549     }
21550   }else
21551 
21552   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
21553     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
21554     int i;                   /* Loop counter */
21555     int bSchema = 0;         /* Also hash the schema */
21556     int bSeparate = 0;       /* Hash each table separately */
21557     int iSize = 224;         /* Hash algorithm to use */
21558     int bDebug = 0;          /* Only show the query that would have run */
21559     sqlite3_stmt *pStmt;     /* For querying tables names */
21560     char *zSql;              /* SQL to be run */
21561     char *zSep;              /* Separator */
21562     ShellText sSql;          /* Complete SQL for the query to run the hash */
21563     ShellText sQuery;        /* Set of queries used to read all content */
21564     open_db(p, 0);
21565     for(i=1; i<nArg; i++){
21566       const char *z = azArg[i];
21567       if( z[0]=='-' ){
21568         z++;
21569         if( z[0]=='-' ) z++;
21570         if( strcmp(z,"schema")==0 ){
21571           bSchema = 1;
21572         }else
21573         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
21574          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
21575         ){
21576           iSize = atoi(&z[5]);
21577         }else
21578         if( strcmp(z,"debug")==0 ){
21579           bDebug = 1;
21580         }else
21581         {
21582           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21583                       azArg[i], azArg[0]);
21584           showHelp(p->out, azArg[0]);
21585           rc = 1;
21586           goto meta_command_exit;
21587         }
21588       }else if( zLike ){
21589         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
21590         rc = 1;
21591         goto meta_command_exit;
21592       }else{
21593         zLike = z;
21594         bSeparate = 1;
21595         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
21596       }
21597     }
21598     if( bSchema ){
21599       zSql = "SELECT lower(name) FROM sqlite_schema"
21600              " WHERE type='table' AND coalesce(rootpage,0)>1"
21601              " UNION ALL SELECT 'sqlite_schema'"
21602              " ORDER BY 1 collate nocase";
21603     }else{
21604       zSql = "SELECT lower(name) FROM sqlite_schema"
21605              " WHERE type='table' AND coalesce(rootpage,0)>1"
21606              " AND name NOT LIKE 'sqlite_%'"
21607              " ORDER BY 1 collate nocase";
21608     }
21609     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21610     initText(&sQuery);
21611     initText(&sSql);
21612     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
21613     zSep = "VALUES(";
21614     while( SQLITE_ROW==sqlite3_step(pStmt) ){
21615       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21616       if( zTab==0 ) continue;
21617       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21618       if( strncmp(zTab, "sqlite_",7)!=0 ){
21619         appendText(&sQuery,"SELECT * FROM ", 0);
21620         appendText(&sQuery,zTab,'"');
21621         appendText(&sQuery," NOT INDEXED;", 0);
21622       }else if( strcmp(zTab, "sqlite_schema")==0 ){
21623         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
21624                            " ORDER BY name;", 0);
21625       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
21626         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
21627                            " ORDER BY name;", 0);
21628       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
21629         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
21630                            " ORDER BY tbl,idx;", 0);
21631       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
21632         appendText(&sQuery, "SELECT * FROM ", 0);
21633         appendText(&sQuery, zTab, 0);
21634         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
21635       }
21636       appendText(&sSql, zSep, 0);
21637       appendText(&sSql, sQuery.z, '\'');
21638       sQuery.n = 0;
21639       appendText(&sSql, ",", 0);
21640       appendText(&sSql, zTab, '\'');
21641       zSep = "),(";
21642     }
21643     sqlite3_finalize(pStmt);
21644     if( bSeparate ){
21645       zSql = sqlite3_mprintf(
21646           "%s))"
21647           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
21648           "   FROM [sha3sum$query]",
21649           sSql.z, iSize);
21650     }else{
21651       zSql = sqlite3_mprintf(
21652           "%s))"
21653           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
21654           "   FROM [sha3sum$query]",
21655           sSql.z, iSize);
21656     }
21657     shell_check_oom(zSql);
21658     freeText(&sQuery);
21659     freeText(&sSql);
21660     if( bDebug ){
21661       utf8_printf(p->out, "%s\n", zSql);
21662     }else{
21663       shell_exec(p, zSql, 0);
21664     }
21665     sqlite3_free(zSql);
21666   }else
21667 
21668 #ifndef SQLITE_NOHAVE_SYSTEM
21669   if( c=='s'
21670    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21671   ){
21672     char *zCmd;
21673     int i, x;
21674     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
21675     if( nArg<2 ){
21676       raw_printf(stderr, "Usage: .system COMMAND\n");
21677       rc = 1;
21678       goto meta_command_exit;
21679     }
21680     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21681     for(i=2; i<nArg && zCmd!=0; i++){
21682       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
21683                              zCmd, azArg[i]);
21684     }
21685     x = zCmd!=0 ? system(zCmd) : 1;
21686     sqlite3_free(zCmd);
21687     if( x ) raw_printf(stderr, "System command returns %d\n", x);
21688   }else
21689 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21690 
21691   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21692     static const char *azBool[] = { "off", "on", "trigger", "full"};
21693     const char *zOut;
21694     int i;
21695     if( nArg!=1 ){
21696       raw_printf(stderr, "Usage: .show\n");
21697       rc = 1;
21698       goto meta_command_exit;
21699     }
21700     utf8_printf(p->out, "%12.12s: %s\n","echo",
21701                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
21702     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
21703     utf8_printf(p->out, "%12.12s: %s\n","explain",
21704          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
21705     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21706     if( p->mode==MODE_Column
21707      || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
21708     ){
21709       utf8_printf
21710         (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
21711          modeDescr[p->mode], p->cmOpts.iWrap,
21712          p->cmOpts.bWordWrap ? "on" : "off",
21713          p->cmOpts.bQuote ? "" : "no");
21714     }else{
21715       utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21716     }
21717     utf8_printf(p->out, "%12.12s: ", "nullvalue");
21718       output_c_string(p->out, p->nullValue);
21719       raw_printf(p->out, "\n");
21720     utf8_printf(p->out,"%12.12s: %s\n","output",
21721             strlen30(p->outfile) ? p->outfile : "stdout");
21722     utf8_printf(p->out,"%12.12s: ", "colseparator");
21723       output_c_string(p->out, p->colSeparator);
21724       raw_printf(p->out, "\n");
21725     utf8_printf(p->out,"%12.12s: ", "rowseparator");
21726       output_c_string(p->out, p->rowSeparator);
21727       raw_printf(p->out, "\n");
21728     switch( p->statsOn ){
21729       case 0:  zOut = "off";     break;
21730       default: zOut = "on";      break;
21731       case 2:  zOut = "stmt";    break;
21732       case 3:  zOut = "vmstep";  break;
21733     }
21734     utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
21735     utf8_printf(p->out, "%12.12s: ", "width");
21736     for (i=0;i<p->nWidth;i++) {
21737       raw_printf(p->out, "%d ", p->colWidth[i]);
21738     }
21739     raw_printf(p->out, "\n");
21740     utf8_printf(p->out, "%12.12s: %s\n", "filename",
21741                 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
21742   }else
21743 
21744   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
21745     if( nArg==2 ){
21746       if( strcmp(azArg[1],"stmt")==0 ){
21747         p->statsOn = 2;
21748       }else if( strcmp(azArg[1],"vmstep")==0 ){
21749         p->statsOn = 3;
21750       }else{
21751         p->statsOn = (u8)booleanValue(azArg[1]);
21752       }
21753     }else if( nArg==1 ){
21754       display_stats(p->db, p, 0);
21755     }else{
21756       raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
21757       rc = 1;
21758     }
21759   }else
21760 
21761   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
21762    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
21763                  || strncmp(azArg[0], "indexes", n)==0) )
21764   ){
21765     sqlite3_stmt *pStmt;
21766     char **azResult;
21767     int nRow, nAlloc;
21768     int ii;
21769     ShellText s;
21770     initText(&s);
21771     open_db(p, 0);
21772     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
21773     if( rc ){
21774       sqlite3_finalize(pStmt);
21775       return shellDatabaseError(p->db);
21776     }
21777 
21778     if( nArg>2 && c=='i' ){
21779       /* It is an historical accident that the .indexes command shows an error
21780       ** when called with the wrong number of arguments whereas the .tables
21781       ** command does not. */
21782       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
21783       rc = 1;
21784       sqlite3_finalize(pStmt);
21785       goto meta_command_exit;
21786     }
21787     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
21788       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
21789       if( zDbName==0 ) continue;
21790       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
21791       if( sqlite3_stricmp(zDbName, "main")==0 ){
21792         appendText(&s, "SELECT name FROM ", 0);
21793       }else{
21794         appendText(&s, "SELECT ", 0);
21795         appendText(&s, zDbName, '\'');
21796         appendText(&s, "||'.'||name FROM ", 0);
21797       }
21798       appendText(&s, zDbName, '"');
21799       appendText(&s, ".sqlite_schema ", 0);
21800       if( c=='t' ){
21801         appendText(&s," WHERE type IN ('table','view')"
21802                       "   AND name NOT LIKE 'sqlite_%'"
21803                       "   AND name LIKE ?1", 0);
21804       }else{
21805         appendText(&s," WHERE type='index'"
21806                       "   AND tbl_name LIKE ?1", 0);
21807       }
21808     }
21809     rc = sqlite3_finalize(pStmt);
21810     if( rc==SQLITE_OK ){
21811       appendText(&s, " ORDER BY 1", 0);
21812       rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
21813     }
21814     freeText(&s);
21815     if( rc ) return shellDatabaseError(p->db);
21816 
21817     /* Run the SQL statement prepared by the above block. Store the results
21818     ** as an array of nul-terminated strings in azResult[].  */
21819     nRow = nAlloc = 0;
21820     azResult = 0;
21821     if( nArg>1 ){
21822       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
21823     }else{
21824       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
21825     }
21826     while( sqlite3_step(pStmt)==SQLITE_ROW ){
21827       if( nRow>=nAlloc ){
21828         char **azNew;
21829         int n2 = nAlloc*2 + 10;
21830         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21831         shell_check_oom(azNew);
21832         nAlloc = n2;
21833         azResult = azNew;
21834       }
21835       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21836       shell_check_oom(azResult[nRow]);
21837       nRow++;
21838     }
21839     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
21840       rc = shellDatabaseError(p->db);
21841     }
21842 
21843     /* Pretty-print the contents of array azResult[] to the output */
21844     if( rc==0 && nRow>0 ){
21845       int len, maxlen = 0;
21846       int i, j;
21847       int nPrintCol, nPrintRow;
21848       for(i=0; i<nRow; i++){
21849         len = strlen30(azResult[i]);
21850         if( len>maxlen ) maxlen = len;
21851       }
21852       nPrintCol = 80/(maxlen+2);
21853       if( nPrintCol<1 ) nPrintCol = 1;
21854       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
21855       for(i=0; i<nPrintRow; i++){
21856         for(j=i; j<nRow; j+=nPrintRow){
21857           char *zSp = j<nPrintRow ? "" : "  ";
21858           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
21859                       azResult[j] ? azResult[j]:"");
21860         }
21861         raw_printf(p->out, "\n");
21862       }
21863     }
21864 
21865     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21866     sqlite3_free(azResult);
21867   }else
21868 
21869   /* Begin redirecting output to the file "testcase-out.txt" */
21870   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21871     output_reset(p);
21872     p->out = output_file_open("testcase-out.txt", 0);
21873     if( p->out==0 ){
21874       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
21875     }
21876     if( nArg>=2 ){
21877       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21878     }else{
21879       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21880     }
21881   }else
21882 
21883 #ifndef SQLITE_UNTESTABLE
21884   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21885     static const struct {
21886        const char *zCtrlName;   /* Name of a test-control option */
21887        int ctrlCode;            /* Integer code for that option */
21888        int unSafe;              /* Not valid for --safe mode */
21889        const char *zUsage;      /* Usage notes */
21890     } aCtrl[] = {
21891       { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
21892       { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
21893     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
21894     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
21895       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
21896       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
21897     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
21898       { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
21899       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
21900       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
21901       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
21902       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
21903 #ifdef YYCOVERAGE
21904       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
21905 #endif
21906       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
21907       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
21908       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
21909       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
21910       { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
21911       { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
21912       { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
21913     };
21914     int testctrl = -1;
21915     int iCtrl = -1;
21916     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
21917     int isOk = 0;
21918     int i, n2;
21919     const char *zCmd = 0;
21920 
21921     open_db(p, 0);
21922     zCmd = nArg>=2 ? azArg[1] : "help";
21923 
21924     /* The argument can optionally begin with "-" or "--" */
21925     if( zCmd[0]=='-' && zCmd[1] ){
21926       zCmd++;
21927       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
21928     }
21929 
21930     /* --help lists all test-controls */
21931     if( strcmp(zCmd,"help")==0 ){
21932       utf8_printf(p->out, "Available test-controls:\n");
21933       for(i=0; i<ArraySize(aCtrl); i++){
21934         utf8_printf(p->out, "  .testctrl %s %s\n",
21935                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
21936       }
21937       rc = 1;
21938       goto meta_command_exit;
21939     }
21940 
21941     /* convert testctrl text option to value. allow any unique prefix
21942     ** of the option name, or a numerical value. */
21943     n2 = strlen30(zCmd);
21944     for(i=0; i<ArraySize(aCtrl); i++){
21945       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
21946         if( testctrl<0 ){
21947           testctrl = aCtrl[i].ctrlCode;
21948           iCtrl = i;
21949         }else{
21950           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
21951                               "Use \".testctrl --help\" for help\n", zCmd);
21952           rc = 1;
21953           goto meta_command_exit;
21954         }
21955       }
21956     }
21957     if( testctrl<0 ){
21958       utf8_printf(stderr,"Error: unknown test-control: %s\n"
21959                          "Use \".testctrl --help\" for help\n", zCmd);
21960     }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
21961       utf8_printf(stderr,
21962          "line %d: \".testctrl %s\" may not be used in safe mode\n",
21963          p->lineno, aCtrl[iCtrl].zCtrlName);
21964       exit(1);
21965     }else{
21966       switch(testctrl){
21967 
21968         /* sqlite3_test_control(int, db, int) */
21969         case SQLITE_TESTCTRL_OPTIMIZATIONS:
21970           if( nArg==3 ){
21971             unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
21972             rc2 = sqlite3_test_control(testctrl, p->db, opt);
21973             isOk = 3;
21974           }
21975           break;
21976 
21977         /* sqlite3_test_control(int) */
21978         case SQLITE_TESTCTRL_PRNG_SAVE:
21979         case SQLITE_TESTCTRL_PRNG_RESTORE:
21980         case SQLITE_TESTCTRL_BYTEORDER:
21981           if( nArg==2 ){
21982             rc2 = sqlite3_test_control(testctrl);
21983             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
21984           }
21985           break;
21986 
21987         /* sqlite3_test_control(int, uint) */
21988         case SQLITE_TESTCTRL_PENDING_BYTE:
21989           if( nArg==3 ){
21990             unsigned int opt = (unsigned int)integerValue(azArg[2]);
21991             rc2 = sqlite3_test_control(testctrl, opt);
21992             isOk = 3;
21993           }
21994           break;
21995 
21996         /* sqlite3_test_control(int, int, sqlite3*) */
21997         case SQLITE_TESTCTRL_PRNG_SEED:
21998           if( nArg==3 || nArg==4 ){
21999             int ii = (int)integerValue(azArg[2]);
22000             sqlite3 *db;
22001             if( ii==0 && strcmp(azArg[2],"random")==0 ){
22002               sqlite3_randomness(sizeof(ii),&ii);
22003               printf("-- random seed: %d\n", ii);
22004             }
22005             if( nArg==3 ){
22006               db = 0;
22007             }else{
22008               db = p->db;
22009               /* Make sure the schema has been loaded */
22010               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
22011             }
22012             rc2 = sqlite3_test_control(testctrl, ii, db);
22013             isOk = 3;
22014           }
22015           break;
22016 
22017         /* sqlite3_test_control(int, int) */
22018         case SQLITE_TESTCTRL_ASSERT:
22019         case SQLITE_TESTCTRL_ALWAYS:
22020           if( nArg==3 ){
22021             int opt = booleanValue(azArg[2]);
22022             rc2 = sqlite3_test_control(testctrl, opt);
22023             isOk = 1;
22024           }
22025           break;
22026 
22027         /* sqlite3_test_control(int, int) */
22028         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
22029         case SQLITE_TESTCTRL_NEVER_CORRUPT:
22030           if( nArg==3 ){
22031             int opt = booleanValue(azArg[2]);
22032             rc2 = sqlite3_test_control(testctrl, opt);
22033             isOk = 3;
22034           }
22035           break;
22036 
22037         /* sqlite3_test_control(sqlite3*) */
22038         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
22039           rc2 = sqlite3_test_control(testctrl, p->db);
22040           isOk = 3;
22041           break;
22042 
22043         case SQLITE_TESTCTRL_IMPOSTER:
22044           if( nArg==5 ){
22045             rc2 = sqlite3_test_control(testctrl, p->db,
22046                           azArg[2],
22047                           integerValue(azArg[3]),
22048                           integerValue(azArg[4]));
22049             isOk = 3;
22050           }
22051           break;
22052 
22053         case SQLITE_TESTCTRL_SEEK_COUNT: {
22054           u64 x = 0;
22055           rc2 = sqlite3_test_control(testctrl, p->db, &x);
22056           utf8_printf(p->out, "%llu\n", x);
22057           isOk = 3;
22058           break;
22059         }
22060 
22061 #ifdef YYCOVERAGE
22062         case SQLITE_TESTCTRL_PARSER_COVERAGE: {
22063           if( nArg==2 ){
22064             sqlite3_test_control(testctrl, p->out);
22065             isOk = 3;
22066           }
22067           break;
22068         }
22069 #endif
22070 #ifdef SQLITE_DEBUG
22071         case SQLITE_TESTCTRL_TUNE: {
22072           if( nArg==4 ){
22073             int id = (int)integerValue(azArg[2]);
22074             int val = (int)integerValue(azArg[3]);
22075             sqlite3_test_control(testctrl, id, &val);
22076             isOk = 3;
22077           }else if( nArg==3 ){
22078             int id = (int)integerValue(azArg[2]);
22079             sqlite3_test_control(testctrl, -id, &rc2);
22080             isOk = 1;
22081           }else if( nArg==2 ){
22082             int id = 1;
22083             while(1){
22084               int val = 0;
22085               rc2 = sqlite3_test_control(testctrl, -id, &val);
22086               if( rc2!=SQLITE_OK ) break;
22087               if( id>1 ) utf8_printf(p->out, "  ");
22088               utf8_printf(p->out, "%d: %d", id, val);
22089               id++;
22090             }
22091             if( id>1 ) utf8_printf(p->out, "\n");
22092             isOk = 3;
22093           }
22094           break;
22095         }
22096 #endif
22097         case SQLITE_TESTCTRL_SORTER_MMAP:
22098           if( nArg==3 ){
22099             int opt = (unsigned int)integerValue(azArg[2]);
22100             rc2 = sqlite3_test_control(testctrl, p->db, opt);
22101             isOk = 3;
22102           }
22103           break;
22104       }
22105     }
22106     if( isOk==0 && iCtrl>=0 ){
22107       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
22108       rc = 1;
22109     }else if( isOk==1 ){
22110       raw_printf(p->out, "%d\n", rc2);
22111     }else if( isOk==2 ){
22112       raw_printf(p->out, "0x%08x\n", rc2);
22113     }
22114   }else
22115 #endif /* !defined(SQLITE_UNTESTABLE) */
22116 
22117   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
22118     open_db(p, 0);
22119     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
22120   }else
22121 
22122   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
22123     if( nArg==2 ){
22124       enableTimer = booleanValue(azArg[1]);
22125       if( enableTimer && !HAS_TIMER ){
22126         raw_printf(stderr, "Error: timer not available on this system.\n");
22127         enableTimer = 0;
22128       }
22129     }else{
22130       raw_printf(stderr, "Usage: .timer on|off\n");
22131       rc = 1;
22132     }
22133   }else
22134 
22135 #ifndef SQLITE_OMIT_TRACE
22136   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
22137     int mType = 0;
22138     int jj;
22139     open_db(p, 0);
22140     for(jj=1; jj<nArg; jj++){
22141       const char *z = azArg[jj];
22142       if( z[0]=='-' ){
22143         if( optionMatch(z, "expanded") ){
22144           p->eTraceType = SHELL_TRACE_EXPANDED;
22145         }
22146 #ifdef SQLITE_ENABLE_NORMALIZE
22147         else if( optionMatch(z, "normalized") ){
22148           p->eTraceType = SHELL_TRACE_NORMALIZED;
22149         }
22150 #endif
22151         else if( optionMatch(z, "plain") ){
22152           p->eTraceType = SHELL_TRACE_PLAIN;
22153         }
22154         else if( optionMatch(z, "profile") ){
22155           mType |= SQLITE_TRACE_PROFILE;
22156         }
22157         else if( optionMatch(z, "row") ){
22158           mType |= SQLITE_TRACE_ROW;
22159         }
22160         else if( optionMatch(z, "stmt") ){
22161           mType |= SQLITE_TRACE_STMT;
22162         }
22163         else if( optionMatch(z, "close") ){
22164           mType |= SQLITE_TRACE_CLOSE;
22165         }
22166         else {
22167           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
22168           rc = 1;
22169           goto meta_command_exit;
22170         }
22171       }else{
22172         output_file_close(p->traceOut);
22173         p->traceOut = output_file_open(azArg[1], 0);
22174       }
22175     }
22176     if( p->traceOut==0 ){
22177       sqlite3_trace_v2(p->db, 0, 0, 0);
22178     }else{
22179       if( mType==0 ) mType = SQLITE_TRACE_STMT;
22180       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
22181     }
22182   }else
22183 #endif /* !defined(SQLITE_OMIT_TRACE) */
22184 
22185 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22186   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
22187     int ii;
22188     int lenOpt;
22189     char *zOpt;
22190     if( nArg<2 ){
22191       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
22192       rc = 1;
22193       goto meta_command_exit;
22194     }
22195     open_db(p, 0);
22196     zOpt = azArg[1];
22197     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
22198     lenOpt = (int)strlen(zOpt);
22199     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
22200       assert( azArg[nArg]==0 );
22201       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
22202     }else{
22203       for(ii=1; ii<nArg; ii++){
22204         sqlite3_create_module(p->db, azArg[ii], 0, 0);
22205       }
22206     }
22207   }else
22208 #endif
22209 
22210 #if SQLITE_USER_AUTHENTICATION
22211   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
22212     if( nArg<2 ){
22213       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
22214       rc = 1;
22215       goto meta_command_exit;
22216     }
22217     open_db(p, 0);
22218     if( strcmp(azArg[1],"login")==0 ){
22219       if( nArg!=4 ){
22220         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
22221         rc = 1;
22222         goto meta_command_exit;
22223       }
22224       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
22225                                      strlen30(azArg[3]));
22226       if( rc ){
22227         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
22228         rc = 1;
22229       }
22230     }else if( strcmp(azArg[1],"add")==0 ){
22231       if( nArg!=5 ){
22232         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
22233         rc = 1;
22234         goto meta_command_exit;
22235       }
22236       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
22237                             booleanValue(azArg[4]));
22238       if( rc ){
22239         raw_printf(stderr, "User-Add failed: %d\n", rc);
22240         rc = 1;
22241       }
22242     }else if( strcmp(azArg[1],"edit")==0 ){
22243       if( nArg!=5 ){
22244         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
22245         rc = 1;
22246         goto meta_command_exit;
22247       }
22248       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
22249                               booleanValue(azArg[4]));
22250       if( rc ){
22251         raw_printf(stderr, "User-Edit failed: %d\n", rc);
22252         rc = 1;
22253       }
22254     }else if( strcmp(azArg[1],"delete")==0 ){
22255       if( nArg!=3 ){
22256         raw_printf(stderr, "Usage: .user delete USER\n");
22257         rc = 1;
22258         goto meta_command_exit;
22259       }
22260       rc = sqlite3_user_delete(p->db, azArg[2]);
22261       if( rc ){
22262         raw_printf(stderr, "User-Delete failed: %d\n", rc);
22263         rc = 1;
22264       }
22265     }else{
22266       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
22267       rc = 1;
22268       goto meta_command_exit;
22269     }
22270   }else
22271 #endif /* SQLITE_USER_AUTHENTICATION */
22272 
22273   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
22274     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
22275         sqlite3_libversion(), sqlite3_sourceid());
22276 #if SQLITE_HAVE_ZLIB
22277     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
22278 #endif
22279 #define CTIMEOPT_VAL_(opt) #opt
22280 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
22281 #if defined(__clang__) && defined(__clang_major__)
22282     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
22283                     CTIMEOPT_VAL(__clang_minor__) "."
22284                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
22285 #elif defined(_MSC_VER)
22286     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
22287 #elif defined(__GNUC__) && defined(__VERSION__)
22288     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
22289 #endif
22290   }else
22291 
22292   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
22293     const char *zDbName = nArg==2 ? azArg[1] : "main";
22294     sqlite3_vfs *pVfs = 0;
22295     if( p->db ){
22296       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
22297       if( pVfs ){
22298         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
22299         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
22300         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
22301         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
22302       }
22303     }
22304   }else
22305 
22306   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
22307     sqlite3_vfs *pVfs;
22308     sqlite3_vfs *pCurrent = 0;
22309     if( p->db ){
22310       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
22311     }
22312     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
22313       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
22314            pVfs==pCurrent ? "  <--- CURRENT" : "");
22315       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
22316       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
22317       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
22318       if( pVfs->pNext ){
22319         raw_printf(p->out, "-----------------------------------\n");
22320       }
22321     }
22322   }else
22323 
22324   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
22325     const char *zDbName = nArg==2 ? azArg[1] : "main";
22326     char *zVfsName = 0;
22327     if( p->db ){
22328       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
22329       if( zVfsName ){
22330         utf8_printf(p->out, "%s\n", zVfsName);
22331         sqlite3_free(zVfsName);
22332       }
22333     }
22334   }else
22335 
22336   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
22337     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
22338     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
22339   }else
22340 
22341   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
22342     int j;
22343     assert( nArg<=ArraySize(azArg) );
22344     p->nWidth = nArg-1;
22345     p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
22346     if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
22347     if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
22348     for(j=1; j<nArg; j++){
22349       p->colWidth[j-1] = (int)integerValue(azArg[j]);
22350     }
22351   }else
22352 
22353   {
22354     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
22355       " \"%s\". Enter \".help\" for help\n", azArg[0]);
22356     rc = 1;
22357   }
22358 
22359 meta_command_exit:
22360   if( p->outCount ){
22361     p->outCount--;
22362     if( p->outCount==0 ) output_reset(p);
22363   }
22364   p->bSafeMode = p->bSafeModePersist;
22365   return rc;
22366 }
22367 
22368 /* Line scan result and intermediate states (supporting scan resumption)
22369 */
22370 #ifndef CHAR_BIT
22371 # define CHAR_BIT 8
22372 #endif
22373 typedef enum {
22374   QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
22375   QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
22376   QSS_Start = 0
22377 } QuickScanState;
22378 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
22379 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
22380 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
22381 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
22382 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
22383 
22384 /*
22385 ** Scan line for classification to guide shell's handling.
22386 ** The scan is resumable for subsequent lines when prior
22387 ** return values are passed as the 2nd argument.
22388 */
22389 static QuickScanState quickscan(char *zLine, QuickScanState qss){
22390   char cin;
22391   char cWait = (char)qss; /* intentional narrowing loss */
22392   if( cWait==0 ){
22393   PlainScan:
22394     assert( cWait==0 );
22395     while( (cin = *zLine++)!=0 ){
22396       if( IsSpace(cin) )
22397         continue;
22398       switch (cin){
22399       case '-':
22400         if( *zLine!='-' )
22401           break;
22402         while((cin = *++zLine)!=0 )
22403           if( cin=='\n')
22404             goto PlainScan;
22405         return qss;
22406       case ';':
22407         qss |= QSS_EndingSemi;
22408         continue;
22409       case '/':
22410         if( *zLine=='*' ){
22411           ++zLine;
22412           cWait = '*';
22413           qss = QSS_SETV(qss, cWait);
22414           goto TermScan;
22415         }
22416         break;
22417       case '[':
22418         cin = ']';
22419         /* fall thru */
22420       case '`': case '\'': case '"':
22421         cWait = cin;
22422         qss = QSS_HasDark | cWait;
22423         goto TermScan;
22424       default:
22425         break;
22426       }
22427       qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
22428     }
22429   }else{
22430   TermScan:
22431     while( (cin = *zLine++)!=0 ){
22432       if( cin==cWait ){
22433         switch( cWait ){
22434         case '*':
22435           if( *zLine != '/' )
22436             continue;
22437           ++zLine;
22438           cWait = 0;
22439           qss = QSS_SETV(qss, 0);
22440           goto PlainScan;
22441         case '`': case '\'': case '"':
22442           if(*zLine==cWait){
22443             ++zLine;
22444             continue;
22445           }
22446           /* fall thru */
22447         case ']':
22448           cWait = 0;
22449           qss = QSS_SETV(qss, 0);
22450           goto PlainScan;
22451         default: assert(0);
22452         }
22453       }
22454     }
22455   }
22456   return qss;
22457 }
22458 
22459 /*
22460 ** Return TRUE if the line typed in is an SQL command terminator other
22461 ** than a semi-colon.  The SQL Server style "go" command is understood
22462 ** as is the Oracle "/".
22463 */
22464 static int line_is_command_terminator(char *zLine){
22465   while( IsSpace(zLine[0]) ){ zLine++; };
22466   if( zLine[0]=='/' )
22467     zLine += 1; /* Oracle */
22468   else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
22469     zLine += 2; /* SQL Server */
22470   else
22471     return 0;
22472   return quickscan(zLine,QSS_Start)==QSS_Start;
22473 }
22474 
22475 /*
22476 ** We need a default sqlite3_complete() implementation to use in case
22477 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
22478 ** any arbitrary text is a complete SQL statement.  This is not very
22479 ** user-friendly, but it does seem to work.
22480 */
22481 #ifdef SQLITE_OMIT_COMPLETE
22482 #define sqlite3_complete(x) 1
22483 #endif
22484 
22485 /*
22486 ** Return true if zSql is a complete SQL statement.  Return false if it
22487 ** ends in the middle of a string literal or C-style comment.
22488 */
22489 static int line_is_complete(char *zSql, int nSql){
22490   int rc;
22491   if( zSql==0 ) return 1;
22492   zSql[nSql] = ';';
22493   zSql[nSql+1] = 0;
22494   rc = sqlite3_complete(zSql);
22495   zSql[nSql] = 0;
22496   return rc;
22497 }
22498 
22499 /*
22500 ** Run a single line of SQL.  Return the number of errors.
22501 */
22502 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
22503   int rc;
22504   char *zErrMsg = 0;
22505 
22506   open_db(p, 0);
22507   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
22508   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
22509   BEGIN_TIMER;
22510   rc = shell_exec(p, zSql, &zErrMsg);
22511   END_TIMER;
22512   if( rc || zErrMsg ){
22513     char zPrefix[100];
22514     const char *zErrorTail;
22515     const char *zErrorType;
22516     if( zErrMsg==0 ){
22517       zErrorType = "Error";
22518       zErrorTail = sqlite3_errmsg(p->db);
22519     }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
22520       zErrorType = "Parse error";
22521       zErrorTail = &zErrMsg[12];
22522     }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
22523       zErrorType = "Runtime error";
22524       zErrorTail = &zErrMsg[10];
22525     }else{
22526       zErrorType = "Error";
22527       zErrorTail = zErrMsg;
22528     }
22529     if( in!=0 || !stdin_is_interactive ){
22530       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
22531                        "%s near line %d:", zErrorType, startline);
22532     }else{
22533       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
22534     }
22535     utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
22536     sqlite3_free(zErrMsg);
22537     zErrMsg = 0;
22538     return 1;
22539   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
22540     char zLineBuf[2000];
22541     sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
22542             "changes: %lld   total_changes: %lld",
22543             sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
22544     raw_printf(p->out, "%s\n", zLineBuf);
22545   }
22546   return 0;
22547 }
22548 
22549 
22550 /*
22551 ** Read input from *in and process it.  If *in==0 then input
22552 ** is interactive - the user is typing it it.  Otherwise, input
22553 ** is coming from a file or device.  A prompt is issued and history
22554 ** is saved only if input is interactive.  An interrupt signal will
22555 ** cause this routine to exit immediately, unless input is interactive.
22556 **
22557 ** Return the number of errors.
22558 */
22559 static int process_input(ShellState *p){
22560   char *zLine = 0;          /* A single input line */
22561   char *zSql = 0;           /* Accumulated SQL text */
22562   int nLine;                /* Length of current line */
22563   int nSql = 0;             /* Bytes of zSql[] used */
22564   int nAlloc = 0;           /* Allocated zSql[] space */
22565   int rc;                   /* Error code */
22566   int errCnt = 0;           /* Number of errors seen */
22567   int startline = 0;        /* Line number for start of current input */
22568   QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
22569 
22570   if( p->inputNesting==MAX_INPUT_NESTING ){
22571     /* This will be more informative in a later version. */
22572     utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
22573                 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
22574     return 1;
22575   }
22576   ++p->inputNesting;
22577   p->lineno = 0;
22578   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
22579     fflush(p->out);
22580     zLine = one_input_line(p->in, zLine, nSql>0);
22581     if( zLine==0 ){
22582       /* End of input */
22583       if( p->in==0 && stdin_is_interactive ) printf("\n");
22584       break;
22585     }
22586     if( seenInterrupt ){
22587       if( p->in!=0 ) break;
22588       seenInterrupt = 0;
22589     }
22590     p->lineno++;
22591     if( QSS_INPLAIN(qss)
22592         && line_is_command_terminator(zLine)
22593         && line_is_complete(zSql, nSql) ){
22594       memcpy(zLine,";",2);
22595     }
22596     qss = quickscan(zLine, qss);
22597     if( QSS_PLAINWHITE(qss) && nSql==0 ){
22598       if( ShellHasFlag(p, SHFLG_Echo) )
22599         printf("%s\n", zLine);
22600       /* Just swallow single-line whitespace */
22601       qss = QSS_Start;
22602       continue;
22603     }
22604     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
22605       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
22606       if( zLine[0]=='.' ){
22607         rc = do_meta_command(zLine, p);
22608         if( rc==2 ){ /* exit requested */
22609           break;
22610         }else if( rc ){
22611           errCnt++;
22612         }
22613       }
22614       qss = QSS_Start;
22615       continue;
22616     }
22617     /* No single-line dispositions remain; accumulate line(s). */
22618     nLine = strlen30(zLine);
22619     if( nSql+nLine+2>=nAlloc ){
22620       /* Grow buffer by half-again increments when big. */
22621       nAlloc = nSql+(nSql>>1)+nLine+100;
22622       zSql = realloc(zSql, nAlloc);
22623       shell_check_oom(zSql);
22624     }
22625     if( nSql==0 ){
22626       int i;
22627       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
22628       assert( nAlloc>0 && zSql!=0 );
22629       memcpy(zSql, zLine+i, nLine+1-i);
22630       startline = p->lineno;
22631       nSql = nLine-i;
22632     }else{
22633       zSql[nSql++] = '\n';
22634       memcpy(zSql+nSql, zLine, nLine+1);
22635       nSql += nLine;
22636     }
22637     if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
22638       errCnt += runOneSqlLine(p, zSql, p->in, startline);
22639       nSql = 0;
22640       if( p->outCount ){
22641         output_reset(p);
22642         p->outCount = 0;
22643       }else{
22644         clearTempFile(p);
22645       }
22646       p->bSafeMode = p->bSafeModePersist;
22647       qss = QSS_Start;
22648     }else if( nSql && QSS_PLAINWHITE(qss) ){
22649       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
22650       nSql = 0;
22651       qss = QSS_Start;
22652     }
22653   }
22654   if( nSql ){
22655     /* This may be incomplete. Let the SQL parser deal with that. */
22656     errCnt += runOneSqlLine(p, zSql, p->in, startline);
22657   }
22658   free(zSql);
22659   free(zLine);
22660   --p->inputNesting;
22661   return errCnt>0;
22662 }
22663 
22664 /*
22665 ** Return a pathname which is the user's home directory.  A
22666 ** 0 return indicates an error of some kind.
22667 */
22668 static char *find_home_dir(int clearFlag){
22669   static char *home_dir = NULL;
22670   if( clearFlag ){
22671     free(home_dir);
22672     home_dir = 0;
22673     return 0;
22674   }
22675   if( home_dir ) return home_dir;
22676 
22677 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
22678      && !defined(__RTP__) && !defined(_WRS_KERNEL)
22679   {
22680     struct passwd *pwent;
22681     uid_t uid = getuid();
22682     if( (pwent=getpwuid(uid)) != NULL) {
22683       home_dir = pwent->pw_dir;
22684     }
22685   }
22686 #endif
22687 
22688 #if defined(_WIN32_WCE)
22689   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
22690    */
22691   home_dir = "/";
22692 #else
22693 
22694 #if defined(_WIN32) || defined(WIN32)
22695   if (!home_dir) {
22696     home_dir = getenv("USERPROFILE");
22697   }
22698 #endif
22699 
22700   if (!home_dir) {
22701     home_dir = getenv("HOME");
22702   }
22703 
22704 #if defined(_WIN32) || defined(WIN32)
22705   if (!home_dir) {
22706     char *zDrive, *zPath;
22707     int n;
22708     zDrive = getenv("HOMEDRIVE");
22709     zPath = getenv("HOMEPATH");
22710     if( zDrive && zPath ){
22711       n = strlen30(zDrive) + strlen30(zPath) + 1;
22712       home_dir = malloc( n );
22713       if( home_dir==0 ) return 0;
22714       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
22715       return home_dir;
22716     }
22717     home_dir = "c:\\";
22718   }
22719 #endif
22720 
22721 #endif /* !_WIN32_WCE */
22722 
22723   if( home_dir ){
22724     int n = strlen30(home_dir) + 1;
22725     char *z = malloc( n );
22726     if( z ) memcpy(z, home_dir, n);
22727     home_dir = z;
22728   }
22729 
22730   return home_dir;
22731 }
22732 
22733 /*
22734 ** Read input from the file given by sqliterc_override.  Or if that
22735 ** parameter is NULL, take input from ~/.sqliterc
22736 **
22737 ** Returns the number of errors.
22738 */
22739 static void process_sqliterc(
22740   ShellState *p,                  /* Configuration data */
22741   const char *sqliterc_override   /* Name of config file. NULL to use default */
22742 ){
22743   char *home_dir = NULL;
22744   const char *sqliterc = sqliterc_override;
22745   char *zBuf = 0;
22746   FILE *inSaved = p->in;
22747   int savedLineno = p->lineno;
22748 
22749   if (sqliterc == NULL) {
22750     home_dir = find_home_dir(0);
22751     if( home_dir==0 ){
22752       raw_printf(stderr, "-- warning: cannot find home directory;"
22753                       " cannot read ~/.sqliterc\n");
22754       return;
22755     }
22756     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
22757     shell_check_oom(zBuf);
22758     sqliterc = zBuf;
22759   }
22760   p->in = fopen(sqliterc,"rb");
22761   if( p->in ){
22762     if( stdin_is_interactive ){
22763       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
22764     }
22765     if( process_input(p) && bail_on_error ) exit(1);
22766     fclose(p->in);
22767   }else if( sqliterc_override!=0 ){
22768     utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
22769     if( bail_on_error ) exit(1);
22770   }
22771   p->in = inSaved;
22772   p->lineno = savedLineno;
22773   sqlite3_free(zBuf);
22774 }
22775 
22776 /*
22777 ** Show available command line options
22778 */
22779 static const char zOptions[] =
22780 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22781   "   -A ARGS...           run \".archive ARGS\" and exit\n"
22782 #endif
22783   "   -append              append the database to the end of the file\n"
22784   "   -ascii               set output mode to 'ascii'\n"
22785   "   -bail                stop after hitting an error\n"
22786   "   -batch               force batch I/O\n"
22787   "   -box                 set output mode to 'box'\n"
22788   "   -column              set output mode to 'column'\n"
22789   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
22790   "   -csv                 set output mode to 'csv'\n"
22791 #if !defined(SQLITE_OMIT_DESERIALIZE)
22792   "   -deserialize         open the database using sqlite3_deserialize()\n"
22793 #endif
22794   "   -echo                print commands before execution\n"
22795   "   -init FILENAME       read/process named file\n"
22796   "   -[no]header          turn headers on or off\n"
22797 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
22798   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
22799 #endif
22800   "   -help                show this message\n"
22801   "   -html                set output mode to HTML\n"
22802   "   -interactive         force interactive I/O\n"
22803   "   -json                set output mode to 'json'\n"
22804   "   -line                set output mode to 'line'\n"
22805   "   -list                set output mode to 'list'\n"
22806   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
22807   "   -markdown            set output mode to 'markdown'\n"
22808 #if !defined(SQLITE_OMIT_DESERIALIZE)
22809   "   -maxsize N           maximum size for a --deserialize database\n"
22810 #endif
22811   "   -memtrace            trace all memory allocations and deallocations\n"
22812   "   -mmap N              default mmap size set to N\n"
22813 #ifdef SQLITE_ENABLE_MULTIPLEX
22814   "   -multiplex           enable the multiplexor VFS\n"
22815 #endif
22816   "   -newline SEP         set output row separator. Default: '\\n'\n"
22817   "   -nofollow            refuse to open symbolic links to database files\n"
22818   "   -nonce STRING        set the safe-mode escape nonce\n"
22819   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
22820   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
22821   "   -quote               set output mode to 'quote'\n"
22822   "   -readonly            open the database read-only\n"
22823   "   -safe                enable safe-mode\n"
22824   "   -separator SEP       set output column separator. Default: '|'\n"
22825 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22826   "   -sorterref SIZE      sorter references threshold size\n"
22827 #endif
22828   "   -stats               print memory stats before each finalize\n"
22829   "   -table               set output mode to 'table'\n"
22830   "   -tabs                set output mode to 'tabs'\n"
22831   "   -version             show SQLite version\n"
22832   "   -vfs NAME            use NAME as the default VFS\n"
22833 #ifdef SQLITE_ENABLE_VFSTRACE
22834   "   -vfstrace            enable tracing of all VFS calls\n"
22835 #endif
22836 #ifdef SQLITE_HAVE_ZLIB
22837   "   -zip                 open the file as a ZIP Archive\n"
22838 #endif
22839 ;
22840 static void usage(int showDetail){
22841   utf8_printf(stderr,
22842       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
22843       "FILENAME is the name of an SQLite database. A new database is created\n"
22844       "if the file does not previously exist.\n", Argv0);
22845   if( showDetail ){
22846     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
22847   }else{
22848     raw_printf(stderr, "Use the -help option for additional information\n");
22849   }
22850   exit(1);
22851 }
22852 
22853 /*
22854 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
22855 ** error message if it is initialized.
22856 */
22857 static void verify_uninitialized(void){
22858   if( sqlite3_config(-1)==SQLITE_MISUSE ){
22859     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
22860                         " initialization.\n");
22861   }
22862 }
22863 
22864 /*
22865 ** Initialize the state information in data
22866 */
22867 static void main_init(ShellState *data) {
22868   memset(data, 0, sizeof(*data));
22869   data->normalMode = data->cMode = data->mode = MODE_List;
22870   data->autoExplain = 1;
22871   data->pAuxDb = &data->aAuxDb[0];
22872   memcpy(data->colSeparator,SEP_Column, 2);
22873   memcpy(data->rowSeparator,SEP_Row, 2);
22874   data->showHeader = 0;
22875   data->shellFlgs = SHFLG_Lookaside;
22876   verify_uninitialized();
22877   sqlite3_config(SQLITE_CONFIG_URI, 1);
22878   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
22879   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
22880   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
22881   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
22882 }
22883 
22884 /*
22885 ** Output text to the console in a font that attracts extra attention.
22886 */
22887 #ifdef _WIN32
22888 static void printBold(const char *zText){
22889 #if !SQLITE_OS_WINRT
22890   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
22891   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
22892   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
22893   SetConsoleTextAttribute(out,
22894          FOREGROUND_RED|FOREGROUND_INTENSITY
22895   );
22896 #endif
22897   printf("%s", zText);
22898 #if !SQLITE_OS_WINRT
22899   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
22900 #endif
22901 }
22902 #else
22903 static void printBold(const char *zText){
22904   printf("\033[1m%s\033[0m", zText);
22905 }
22906 #endif
22907 
22908 /*
22909 ** Get the argument to an --option.  Throw an error and die if no argument
22910 ** is available.
22911 */
22912 static char *cmdline_option_value(int argc, char **argv, int i){
22913   if( i==argc ){
22914     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
22915             argv[0], argv[argc-1]);
22916     exit(1);
22917   }
22918   return argv[i];
22919 }
22920 
22921 #ifndef SQLITE_SHELL_IS_UTF8
22922 #  if (defined(_WIN32) || defined(WIN32)) \
22923    && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
22924 #    define SQLITE_SHELL_IS_UTF8          (0)
22925 #  else
22926 #    define SQLITE_SHELL_IS_UTF8          (1)
22927 #  endif
22928 #endif
22929 
22930 #if SQLITE_SHELL_IS_UTF8
22931 int SQLITE_CDECL main(int argc, char **argv){
22932 #else
22933 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
22934   char **argv;
22935 #endif
22936   char *zErrMsg = 0;
22937   ShellState data;
22938   const char *zInitFile = 0;
22939   int i;
22940   int rc = 0;
22941   int warnInmemoryDb = 0;
22942   int readStdin = 1;
22943   int nCmd = 0;
22944   char **azCmd = 0;
22945   const char *zVfs = 0;           /* Value of -vfs command-line option */
22946 #if !SQLITE_SHELL_IS_UTF8
22947   char **argvToFree = 0;
22948   int argcToFree = 0;
22949 #endif
22950 
22951   setBinaryMode(stdin, 0);
22952   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
22953   stdin_is_interactive = isatty(0);
22954   stdout_is_console = isatty(1);
22955 
22956 #if !defined(_WIN32_WCE)
22957   if( getenv("SQLITE_DEBUG_BREAK") ){
22958     if( isatty(0) && isatty(2) ){
22959       fprintf(stderr,
22960           "attach debugger to process %d and press any key to continue.\n",
22961           GETPID());
22962       fgetc(stdin);
22963     }else{
22964 #if defined(_WIN32) || defined(WIN32)
22965 #if SQLITE_OS_WINRT
22966       __debugbreak();
22967 #else
22968       DebugBreak();
22969 #endif
22970 #elif defined(SIGTRAP)
22971       raise(SIGTRAP);
22972 #endif
22973     }
22974   }
22975 #endif
22976 
22977 #if USE_SYSTEM_SQLITE+0!=1
22978   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
22979     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
22980             sqlite3_sourceid(), SQLITE_SOURCE_ID);
22981     exit(1);
22982   }
22983 #endif
22984   main_init(&data);
22985 
22986   /* On Windows, we must translate command-line arguments into UTF-8.
22987   ** The SQLite memory allocator subsystem has to be enabled in order to
22988   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
22989   ** subsequent sqlite3_config() calls will work.  So copy all results into
22990   ** memory that does not come from the SQLite memory allocator.
22991   */
22992 #if !SQLITE_SHELL_IS_UTF8
22993   sqlite3_initialize();
22994   argvToFree = malloc(sizeof(argv[0])*argc*2);
22995   shell_check_oom(argvToFree);
22996   argcToFree = argc;
22997   argv = argvToFree + argc;
22998   for(i=0; i<argc; i++){
22999     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
23000     int n;
23001     shell_check_oom(z);
23002     n = (int)strlen(z);
23003     argv[i] = malloc( n+1 );
23004     shell_check_oom(argv[i]);
23005     memcpy(argv[i], z, n+1);
23006     argvToFree[i] = argv[i];
23007     sqlite3_free(z);
23008   }
23009   sqlite3_shutdown();
23010 #endif
23011 
23012   assert( argc>=1 && argv && argv[0] );
23013   Argv0 = argv[0];
23014 
23015   /* Make sure we have a valid signal handler early, before anything
23016   ** else is done.
23017   */
23018 #ifdef SIGINT
23019   signal(SIGINT, interrupt_handler);
23020 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
23021   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
23022 #endif
23023 
23024 #ifdef SQLITE_SHELL_DBNAME_PROC
23025   {
23026     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
23027     ** of a C-function that will provide the name of the database file.  Use
23028     ** this compile-time option to embed this shell program in larger
23029     ** applications. */
23030     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
23031     SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
23032     warnInmemoryDb = 0;
23033   }
23034 #endif
23035 
23036   /* Do an initial pass through the command-line argument to locate
23037   ** the name of the database file, the name of the initialization file,
23038   ** the size of the alternative malloc heap,
23039   ** and the first command to execute.
23040   */
23041   verify_uninitialized();
23042   for(i=1; i<argc; i++){
23043     char *z;
23044     z = argv[i];
23045     if( z[0]!='-' ){
23046       if( data.aAuxDb->zDbFilename==0 ){
23047         data.aAuxDb->zDbFilename = z;
23048       }else{
23049         /* Excesss arguments are interpreted as SQL (or dot-commands) and
23050         ** mean that nothing is read from stdin */
23051         readStdin = 0;
23052         nCmd++;
23053         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
23054         shell_check_oom(azCmd);
23055         azCmd[nCmd-1] = z;
23056       }
23057     }
23058     if( z[1]=='-' ) z++;
23059     if( strcmp(z,"-separator")==0
23060      || strcmp(z,"-nullvalue")==0
23061      || strcmp(z,"-newline")==0
23062      || strcmp(z,"-cmd")==0
23063     ){
23064       (void)cmdline_option_value(argc, argv, ++i);
23065     }else if( strcmp(z,"-init")==0 ){
23066       zInitFile = cmdline_option_value(argc, argv, ++i);
23067     }else if( strcmp(z,"-batch")==0 ){
23068       /* Need to check for batch mode here to so we can avoid printing
23069       ** informational messages (like from process_sqliterc) before
23070       ** we do the actual processing of arguments later in a second pass.
23071       */
23072       stdin_is_interactive = 0;
23073     }else if( strcmp(z,"-heap")==0 ){
23074 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
23075       const char *zSize;
23076       sqlite3_int64 szHeap;
23077 
23078       zSize = cmdline_option_value(argc, argv, ++i);
23079       szHeap = integerValue(zSize);
23080       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
23081       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
23082 #else
23083       (void)cmdline_option_value(argc, argv, ++i);
23084 #endif
23085     }else if( strcmp(z,"-pagecache")==0 ){
23086       sqlite3_int64 n, sz;
23087       sz = integerValue(cmdline_option_value(argc,argv,++i));
23088       if( sz>70000 ) sz = 70000;
23089       if( sz<0 ) sz = 0;
23090       n = integerValue(cmdline_option_value(argc,argv,++i));
23091       if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
23092         n = 0xffffffffffffLL/sz;
23093       }
23094       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
23095                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
23096       data.shellFlgs |= SHFLG_Pagecache;
23097     }else if( strcmp(z,"-lookaside")==0 ){
23098       int n, sz;
23099       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
23100       if( sz<0 ) sz = 0;
23101       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23102       if( n<0 ) n = 0;
23103       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
23104       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
23105     }else if( strcmp(z,"-threadsafe")==0 ){
23106       int n;
23107       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23108       switch( n ){
23109          case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
23110          case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
23111          default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
23112       }
23113 #ifdef SQLITE_ENABLE_VFSTRACE
23114     }else if( strcmp(z,"-vfstrace")==0 ){
23115       extern int vfstrace_register(
23116          const char *zTraceName,
23117          const char *zOldVfsName,
23118          int (*xOut)(const char*,void*),
23119          void *pOutArg,
23120          int makeDefault
23121       );
23122       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
23123 #endif
23124 #ifdef SQLITE_ENABLE_MULTIPLEX
23125     }else if( strcmp(z,"-multiplex")==0 ){
23126       extern int sqlite3_multiple_initialize(const char*,int);
23127       sqlite3_multiplex_initialize(0, 1);
23128 #endif
23129     }else if( strcmp(z,"-mmap")==0 ){
23130       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23131       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
23132 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23133     }else if( strcmp(z,"-sorterref")==0 ){
23134       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23135       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
23136 #endif
23137     }else if( strcmp(z,"-vfs")==0 ){
23138       zVfs = cmdline_option_value(argc, argv, ++i);
23139 #ifdef SQLITE_HAVE_ZLIB
23140     }else if( strcmp(z,"-zip")==0 ){
23141       data.openMode = SHELL_OPEN_ZIPFILE;
23142 #endif
23143     }else if( strcmp(z,"-append")==0 ){
23144       data.openMode = SHELL_OPEN_APPENDVFS;
23145 #ifndef SQLITE_OMIT_DESERIALIZE
23146     }else if( strcmp(z,"-deserialize")==0 ){
23147       data.openMode = SHELL_OPEN_DESERIALIZE;
23148     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23149       data.szMax = integerValue(argv[++i]);
23150 #endif
23151     }else if( strcmp(z,"-readonly")==0 ){
23152       data.openMode = SHELL_OPEN_READONLY;
23153     }else if( strcmp(z,"-nofollow")==0 ){
23154       data.openFlags = SQLITE_OPEN_NOFOLLOW;
23155 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23156     }else if( strncmp(z, "-A",2)==0 ){
23157       /* All remaining command-line arguments are passed to the ".archive"
23158       ** command, so ignore them */
23159       break;
23160 #endif
23161     }else if( strcmp(z, "-memtrace")==0 ){
23162       sqlite3MemTraceActivate(stderr);
23163     }else if( strcmp(z,"-bail")==0 ){
23164       bail_on_error = 1;
23165     }else if( strcmp(z,"-nonce")==0 ){
23166       free(data.zNonce);
23167       data.zNonce = strdup(argv[++i]);
23168     }else if( strcmp(z,"-safe")==0 ){
23169       /* no-op - catch this on the second pass */
23170     }
23171   }
23172   verify_uninitialized();
23173 
23174 
23175 #ifdef SQLITE_SHELL_INIT_PROC
23176   {
23177     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
23178     ** of a C-function that will perform initialization actions on SQLite that
23179     ** occur just before or after sqlite3_initialize(). Use this compile-time
23180     ** option to embed this shell program in larger applications. */
23181     extern void SQLITE_SHELL_INIT_PROC(void);
23182     SQLITE_SHELL_INIT_PROC();
23183   }
23184 #else
23185   /* All the sqlite3_config() calls have now been made. So it is safe
23186   ** to call sqlite3_initialize() and process any command line -vfs option. */
23187   sqlite3_initialize();
23188 #endif
23189 
23190   if( zVfs ){
23191     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
23192     if( pVfs ){
23193       sqlite3_vfs_register(pVfs, 1);
23194     }else{
23195       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
23196       exit(1);
23197     }
23198   }
23199 
23200   if( data.pAuxDb->zDbFilename==0 ){
23201 #ifndef SQLITE_OMIT_MEMORYDB
23202     data.pAuxDb->zDbFilename = ":memory:";
23203     warnInmemoryDb = argc==1;
23204 #else
23205     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
23206     return 1;
23207 #endif
23208   }
23209   data.out = stdout;
23210   sqlite3_appendvfs_init(0,0,0);
23211 
23212   /* Go ahead and open the database file if it already exists.  If the
23213   ** file does not exist, delay opening it.  This prevents empty database
23214   ** files from being created if a user mistypes the database name argument
23215   ** to the sqlite command-line tool.
23216   */
23217   if( access(data.pAuxDb->zDbFilename, 0)==0 ){
23218     open_db(&data, 0);
23219   }
23220 
23221   /* Process the initialization file if there is one.  If no -init option
23222   ** is given on the command line, look for a file named ~/.sqliterc and
23223   ** try to process it.
23224   */
23225   process_sqliterc(&data,zInitFile);
23226 
23227   /* Make a second pass through the command-line argument and set
23228   ** options.  This second pass is delayed until after the initialization
23229   ** file is processed so that the command-line arguments will override
23230   ** settings in the initialization file.
23231   */
23232   for(i=1; i<argc; i++){
23233     char *z = argv[i];
23234     if( z[0]!='-' ) continue;
23235     if( z[1]=='-' ){ z++; }
23236     if( strcmp(z,"-init")==0 ){
23237       i++;
23238     }else if( strcmp(z,"-html")==0 ){
23239       data.mode = MODE_Html;
23240     }else if( strcmp(z,"-list")==0 ){
23241       data.mode = MODE_List;
23242     }else if( strcmp(z,"-quote")==0 ){
23243       data.mode = MODE_Quote;
23244       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
23245       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23246     }else if( strcmp(z,"-line")==0 ){
23247       data.mode = MODE_Line;
23248     }else if( strcmp(z,"-column")==0 ){
23249       data.mode = MODE_Column;
23250     }else if( strcmp(z,"-json")==0 ){
23251       data.mode = MODE_Json;
23252     }else if( strcmp(z,"-markdown")==0 ){
23253       data.mode = MODE_Markdown;
23254     }else if( strcmp(z,"-table")==0 ){
23255       data.mode = MODE_Table;
23256     }else if( strcmp(z,"-box")==0 ){
23257       data.mode = MODE_Box;
23258     }else if( strcmp(z,"-csv")==0 ){
23259       data.mode = MODE_Csv;
23260       memcpy(data.colSeparator,",",2);
23261 #ifdef SQLITE_HAVE_ZLIB
23262     }else if( strcmp(z,"-zip")==0 ){
23263       data.openMode = SHELL_OPEN_ZIPFILE;
23264 #endif
23265     }else if( strcmp(z,"-append")==0 ){
23266       data.openMode = SHELL_OPEN_APPENDVFS;
23267 #ifndef SQLITE_OMIT_DESERIALIZE
23268     }else if( strcmp(z,"-deserialize")==0 ){
23269       data.openMode = SHELL_OPEN_DESERIALIZE;
23270     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23271       data.szMax = integerValue(argv[++i]);
23272 #endif
23273     }else if( strcmp(z,"-readonly")==0 ){
23274       data.openMode = SHELL_OPEN_READONLY;
23275     }else if( strcmp(z,"-nofollow")==0 ){
23276       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
23277     }else if( strcmp(z,"-ascii")==0 ){
23278       data.mode = MODE_Ascii;
23279       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
23280       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
23281     }else if( strcmp(z,"-tabs")==0 ){
23282       data.mode = MODE_List;
23283       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
23284       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23285     }else if( strcmp(z,"-separator")==0 ){
23286       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
23287                        "%s",cmdline_option_value(argc,argv,++i));
23288     }else if( strcmp(z,"-newline")==0 ){
23289       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
23290                        "%s",cmdline_option_value(argc,argv,++i));
23291     }else if( strcmp(z,"-nullvalue")==0 ){
23292       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
23293                        "%s",cmdline_option_value(argc,argv,++i));
23294     }else if( strcmp(z,"-header")==0 ){
23295       data.showHeader = 1;
23296       ShellSetFlag(&data, SHFLG_HeaderSet);
23297      }else if( strcmp(z,"-noheader")==0 ){
23298       data.showHeader = 0;
23299       ShellSetFlag(&data, SHFLG_HeaderSet);
23300     }else if( strcmp(z,"-echo")==0 ){
23301       ShellSetFlag(&data, SHFLG_Echo);
23302     }else if( strcmp(z,"-eqp")==0 ){
23303       data.autoEQP = AUTOEQP_on;
23304     }else if( strcmp(z,"-eqpfull")==0 ){
23305       data.autoEQP = AUTOEQP_full;
23306     }else if( strcmp(z,"-stats")==0 ){
23307       data.statsOn = 1;
23308     }else if( strcmp(z,"-scanstats")==0 ){
23309       data.scanstatsOn = 1;
23310     }else if( strcmp(z,"-backslash")==0 ){
23311       /* Undocumented command-line option: -backslash
23312       ** Causes C-style backslash escapes to be evaluated in SQL statements
23313       ** prior to sending the SQL into SQLite.  Useful for injecting
23314       ** crazy bytes in the middle of SQL statements for testing and debugging.
23315       */
23316       ShellSetFlag(&data, SHFLG_Backslash);
23317     }else if( strcmp(z,"-bail")==0 ){
23318       /* No-op.  The bail_on_error flag should already be set. */
23319     }else if( strcmp(z,"-version")==0 ){
23320       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
23321       return 0;
23322     }else if( strcmp(z,"-interactive")==0 ){
23323       stdin_is_interactive = 1;
23324     }else if( strcmp(z,"-batch")==0 ){
23325       stdin_is_interactive = 0;
23326     }else if( strcmp(z,"-heap")==0 ){
23327       i++;
23328     }else if( strcmp(z,"-pagecache")==0 ){
23329       i+=2;
23330     }else if( strcmp(z,"-lookaside")==0 ){
23331       i+=2;
23332     }else if( strcmp(z,"-threadsafe")==0 ){
23333       i+=2;
23334     }else if( strcmp(z,"-nonce")==0 ){
23335       i += 2;
23336     }else if( strcmp(z,"-mmap")==0 ){
23337       i++;
23338     }else if( strcmp(z,"-memtrace")==0 ){
23339       i++;
23340 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23341     }else if( strcmp(z,"-sorterref")==0 ){
23342       i++;
23343 #endif
23344     }else if( strcmp(z,"-vfs")==0 ){
23345       i++;
23346 #ifdef SQLITE_ENABLE_VFSTRACE
23347     }else if( strcmp(z,"-vfstrace")==0 ){
23348       i++;
23349 #endif
23350 #ifdef SQLITE_ENABLE_MULTIPLEX
23351     }else if( strcmp(z,"-multiplex")==0 ){
23352       i++;
23353 #endif
23354     }else if( strcmp(z,"-help")==0 ){
23355       usage(1);
23356     }else if( strcmp(z,"-cmd")==0 ){
23357       /* Run commands that follow -cmd first and separately from commands
23358       ** that simply appear on the command-line.  This seems goofy.  It would
23359       ** be better if all commands ran in the order that they appear.  But
23360       ** we retain the goofy behavior for historical compatibility. */
23361       if( i==argc-1 ) break;
23362       z = cmdline_option_value(argc,argv,++i);
23363       if( z[0]=='.' ){
23364         rc = do_meta_command(z, &data);
23365         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
23366       }else{
23367         open_db(&data, 0);
23368         rc = shell_exec(&data, z, &zErrMsg);
23369         if( zErrMsg!=0 ){
23370           utf8_printf(stderr,"Error: %s\n", zErrMsg);
23371           if( bail_on_error ) return rc!=0 ? rc : 1;
23372         }else if( rc!=0 ){
23373           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
23374           if( bail_on_error ) return rc;
23375         }
23376       }
23377 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23378     }else if( strncmp(z, "-A", 2)==0 ){
23379       if( nCmd>0 ){
23380         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
23381                             " with \"%s\"\n", z);
23382         return 1;
23383       }
23384       open_db(&data, OPEN_DB_ZIPFILE);
23385       if( z[2] ){
23386         argv[i] = &z[2];
23387         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
23388       }else{
23389         arDotCommand(&data, 1, argv+i, argc-i);
23390       }
23391       readStdin = 0;
23392       break;
23393 #endif
23394     }else if( strcmp(z,"-safe")==0 ){
23395       data.bSafeMode = data.bSafeModePersist = 1;
23396     }else{
23397       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
23398       raw_printf(stderr,"Use -help for a list of options.\n");
23399       return 1;
23400     }
23401     data.cMode = data.mode;
23402   }
23403 
23404   if( !readStdin ){
23405     /* Run all arguments that do not begin with '-' as if they were separate
23406     ** command-line inputs, except for the argToSkip argument which contains
23407     ** the database filename.
23408     */
23409     for(i=0; i<nCmd; i++){
23410       if( azCmd[i][0]=='.' ){
23411         rc = do_meta_command(azCmd[i], &data);
23412         if( rc ){
23413           free(azCmd);
23414           return rc==2 ? 0 : rc;
23415         }
23416       }else{
23417         open_db(&data, 0);
23418         rc = shell_exec(&data, azCmd[i], &zErrMsg);
23419         if( zErrMsg || rc ){
23420           if( zErrMsg!=0 ){
23421             utf8_printf(stderr,"Error: %s\n", zErrMsg);
23422           }else{
23423             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
23424           }
23425           sqlite3_free(zErrMsg);
23426           free(azCmd);
23427           return rc!=0 ? rc : 1;
23428         }
23429       }
23430     }
23431   }else{
23432     /* Run commands received from standard input
23433     */
23434     if( stdin_is_interactive ){
23435       char *zHome;
23436       char *zHistory;
23437       int nHistory;
23438       printf(
23439         "SQLite version %s %.19s\n" /*extra-version-info*/
23440         "Enter \".help\" for usage hints.\n",
23441         sqlite3_libversion(), sqlite3_sourceid()
23442       );
23443       if( warnInmemoryDb ){
23444         printf("Connected to a ");
23445         printBold("transient in-memory database");
23446         printf(".\nUse \".open FILENAME\" to reopen on a "
23447                "persistent database.\n");
23448       }
23449       zHistory = getenv("SQLITE_HISTORY");
23450       if( zHistory ){
23451         zHistory = strdup(zHistory);
23452       }else if( (zHome = find_home_dir(0))!=0 ){
23453         nHistory = strlen30(zHome) + 20;
23454         if( (zHistory = malloc(nHistory))!=0 ){
23455           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
23456         }
23457       }
23458       if( zHistory ){ shell_read_history(zHistory); }
23459 #if HAVE_READLINE || HAVE_EDITLINE
23460       rl_attempted_completion_function = readline_completion;
23461 #elif HAVE_LINENOISE
23462       linenoiseSetCompletionCallback(linenoise_completion);
23463 #endif
23464       data.in = 0;
23465       rc = process_input(&data);
23466       if( zHistory ){
23467         shell_stifle_history(2000);
23468         shell_write_history(zHistory);
23469         free(zHistory);
23470       }
23471     }else{
23472       data.in = stdin;
23473       rc = process_input(&data);
23474     }
23475   }
23476   free(azCmd);
23477   set_table_name(&data, 0);
23478   if( data.db ){
23479     session_close_all(&data, -1);
23480     close_db(data.db);
23481   }
23482   for(i=0; i<ArraySize(data.aAuxDb); i++){
23483     sqlite3_free(data.aAuxDb[i].zFreeOnClose);
23484     if( data.aAuxDb[i].db ){
23485       session_close_all(&data, i);
23486       close_db(data.aAuxDb[i].db);
23487     }
23488   }
23489   find_home_dir(1);
23490   output_reset(&data);
23491   data.doXdgOpen = 0;
23492   clearTempFile(&data);
23493 #if !SQLITE_SHELL_IS_UTF8
23494   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
23495   free(argvToFree);
23496 #endif
23497   free(data.colWidth);
23498   free(data.zNonce);
23499   /* Clear the global data structure so that valgrind will detect memory
23500   ** leaks */
23501   memset(&data, 0, sizeof(data));
23502   return rc;
23503 }
23504