xref: /freebsd/contrib/sqlite3/shell.c (revision c19fb1f9)
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 typedef unsigned int u32;
38 typedef unsigned short int u16;
39 
40 /*
41 ** Optionally #include a user-defined header, whereby compilation options
42 ** may be set prior to where they take effect, but after platform setup.
43 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
44 ** file. Note that this macro has a like effect on sqlite3.c compilation.
45 */
46 # define SHELL_STRINGIFY_(f) #f
47 # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
48 #ifdef SQLITE_CUSTOM_INCLUDE
49 # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
50 #endif
51 
52 /*
53 ** Determine if we are dealing with WinRT, which provides only a subset of
54 ** the full Win32 API.
55 */
56 #if !defined(SQLITE_OS_WINRT)
57 # define SQLITE_OS_WINRT 0
58 #endif
59 
60 /*
61 ** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
62 ** somewhat for use as a WASM module in a web browser. This flag
63 ** should only be used when building the "fiddle" web application, as
64 ** the browser-mode build has much different user input requirements
65 ** and this build mode rewires the user input subsystem to account for
66 ** that.
67 */
68 
69 /*
70 ** Warning pragmas copied from msvc.h in the core.
71 */
72 #if defined(_MSC_VER)
73 #pragma warning(disable : 4054)
74 #pragma warning(disable : 4055)
75 #pragma warning(disable : 4100)
76 #pragma warning(disable : 4127)
77 #pragma warning(disable : 4130)
78 #pragma warning(disable : 4152)
79 #pragma warning(disable : 4189)
80 #pragma warning(disable : 4206)
81 #pragma warning(disable : 4210)
82 #pragma warning(disable : 4232)
83 #pragma warning(disable : 4244)
84 #pragma warning(disable : 4305)
85 #pragma warning(disable : 4306)
86 #pragma warning(disable : 4702)
87 #pragma warning(disable : 4706)
88 #endif /* defined(_MSC_VER) */
89 
90 /*
91 ** No support for loadable extensions in VxWorks.
92 */
93 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
94 # define SQLITE_OMIT_LOAD_EXTENSION 1
95 #endif
96 
97 /*
98 ** Enable large-file support for fopen() and friends on unix.
99 */
100 #ifndef SQLITE_DISABLE_LFS
101 # define _LARGE_FILE       1
102 # ifndef _FILE_OFFSET_BITS
103 #   define _FILE_OFFSET_BITS 64
104 # endif
105 # define _LARGEFILE_SOURCE 1
106 #endif
107 
108 #if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
109 /*
110 ** emcc requires _POSIX_SOURCE (or one of several similar defines)
111 ** to expose strdup().
112 */
113 # define _POSIX_SOURCE
114 #endif
115 
116 #include <stdlib.h>
117 #include <string.h>
118 #include <stdio.h>
119 #include <assert.h>
120 #include <math.h>
121 #include "sqlite3.h"
122 typedef sqlite3_int64 i64;
123 typedef sqlite3_uint64 u64;
124 typedef unsigned char u8;
125 #if SQLITE_USER_AUTHENTICATION
126 # include "sqlite3userauth.h"
127 #endif
128 #include <ctype.h>
129 #include <stdarg.h>
130 
131 #if !defined(_WIN32) && !defined(WIN32)
132 # include <signal.h>
133 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
134 #  include <pwd.h>
135 # endif
136 #endif
137 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
138 # include <unistd.h>
139 # include <dirent.h>
140 # define GETPID getpid
141 # if defined(__MINGW32__)
142 #  define DIRENT dirent
143 #  ifndef S_ISLNK
144 #   define S_ISLNK(mode) (0)
145 #  endif
146 # endif
147 #else
148 # define GETPID (int)GetCurrentProcessId
149 #endif
150 #include <sys/types.h>
151 #include <sys/stat.h>
152 
153 #if HAVE_READLINE
154 # include <readline/readline.h>
155 # include <readline/history.h>
156 #endif
157 
158 #if HAVE_EDITLINE
159 # include <editline/readline.h>
160 #endif
161 
162 #if HAVE_EDITLINE || HAVE_READLINE
163 
164 # define shell_add_history(X) add_history(X)
165 # define shell_read_history(X) read_history(X)
166 # define shell_write_history(X) write_history(X)
167 # define shell_stifle_history(X) stifle_history(X)
168 # define shell_readline(X) readline(X)
169 
170 #elif HAVE_LINENOISE
171 
172 # include "linenoise.h"
173 # define shell_add_history(X) linenoiseHistoryAdd(X)
174 # define shell_read_history(X) linenoiseHistoryLoad(X)
175 # define shell_write_history(X) linenoiseHistorySave(X)
176 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
177 # define shell_readline(X) linenoise(X)
178 
179 #else
180 
181 # define shell_read_history(X)
182 # define shell_write_history(X)
183 # define shell_stifle_history(X)
184 
185 # define SHELL_USE_LOCAL_GETLINE 1
186 #endif
187 
188 #ifndef deliberate_fall_through
189 /* Quiet some compilers about some of our intentional code. */
190 # if defined(GCC_VERSION) && GCC_VERSION>=7000000
191 #  define deliberate_fall_through __attribute__((fallthrough));
192 # else
193 #  define deliberate_fall_through
194 # endif
195 #endif
196 
197 #if defined(_WIN32) || defined(WIN32)
198 # if SQLITE_OS_WINRT
199 #  define SQLITE_OMIT_POPEN 1
200 # else
201 #  include <io.h>
202 #  include <fcntl.h>
203 #  define isatty(h) _isatty(h)
204 #  ifndef access
205 #   define access(f,m) _access((f),(m))
206 #  endif
207 #  ifndef unlink
208 #   define unlink _unlink
209 #  endif
210 #  ifndef strdup
211 #   define strdup _strdup
212 #  endif
213 #  undef popen
214 #  define popen _popen
215 #  undef pclose
216 #  define pclose _pclose
217 # endif
218 #else
219  /* Make sure isatty() has a prototype. */
220  extern int isatty(int);
221 
222 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
223   /* popen and pclose are not C89 functions and so are
224   ** sometimes omitted from the <stdio.h> header */
225    extern FILE *popen(const char*,const char*);
226    extern int pclose(FILE*);
227 # else
228 #  define SQLITE_OMIT_POPEN 1
229 # endif
230 #endif
231 
232 #if defined(_WIN32_WCE)
233 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
234  * thus we always assume that we have a console. That can be
235  * overridden with the -batch command line option.
236  */
237 #define isatty(x) 1
238 #endif
239 
240 /* ctype macros that work with signed characters */
241 #define IsSpace(X)  isspace((unsigned char)X)
242 #define IsDigit(X)  isdigit((unsigned char)X)
243 #define ToLower(X)  (char)tolower((unsigned char)X)
244 
245 #if defined(_WIN32) || defined(WIN32)
246 #if SQLITE_OS_WINRT
247 #include <intrin.h>
248 #endif
249 #undef WIN32_LEAN_AND_MEAN
250 #define WIN32_LEAN_AND_MEAN
251 #include <windows.h>
252 
253 /* string conversion routines only needed on Win32 */
254 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
255 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
256 #endif
257 
258 /* Use console I/O package as a direct INCLUDE. */
259 #define SQLITE_INTERNAL_LINKAGE static
260 
261 #ifdef SQLITE_SHELL_FIDDLE
262 /* Deselect most features from the console I/O package for Fiddle. */
263 # define SQLITE_CIO_NO_REDIRECT
264 # define SQLITE_CIO_NO_CLASSIFY
265 # define SQLITE_CIO_NO_TRANSLATE
266 # define SQLITE_CIO_NO_SETMODE
267 #endif
268 /************************* Begin ../ext/consio/console_io.h ******************/
269 /*
270 ** 2023 November 1
271 **
272 ** The author disclaims copyright to this source code.  In place of
273 ** a legal notice, here is a blessing:
274 **
275 **    May you do good and not evil.
276 **    May you find forgiveness for yourself and forgive others.
277 **    May you share freely, never taking more than you give.
278 **
279 ********************************************************************************
280 ** This file exposes various interfaces used for console and other I/O
281 ** by the SQLite project command-line tools. These interfaces are used
282 ** at either source conglomeration time, compilation time, or run time.
283 ** This source provides for either inclusion into conglomerated,
284 ** "single-source" forms or separate compilation then linking.
285 **
286 ** Platform dependencies are "hidden" here by various stratagems so
287 ** that, provided certain conditions are met, the programs using this
288 ** source or object code compiled from it need no explicit conditional
289 ** compilation in their source for their console and stream I/O.
290 **
291 ** The symbols and functionality exposed here are not a public API.
292 ** This code may change in tandem with other project code as needed.
293 **
294 ** When this .h file and its companion .c are directly incorporated into
295 ** a source conglomeration (such as shell.c), the preprocessor symbol
296 ** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O
297 ** translation for Windows is effected for the build.
298 */
299 #define HAVE_CONSOLE_IO_H 1
300 #ifndef SQLITE_INTERNAL_LINKAGE
301 # define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */
302 # include <stdio.h>
303 #else
304 # define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */
305 #endif
306 
307 #ifndef SQLITE3_H
308 /* # include "sqlite3.h" */
309 #endif
310 
311 #ifndef SQLITE_CIO_NO_CLASSIFY
312 
313 /* Define enum for use with following function. */
314 typedef enum StreamsAreConsole {
315   SAC_NoConsole = 0,
316   SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4,
317   SAC_AnyConsole = 0x7
318 } StreamsAreConsole;
319 
320 /*
321 ** Classify the three standard I/O streams according to whether
322 ** they are connected to a console attached to the process.
323 **
324 ** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
325 ** or SAC_NoConsole if none of the streams reaches a console.
326 **
327 ** This function should be called before any I/O is done with
328 ** the given streams. As a side-effect, the given inputs are
329 ** recorded so that later I/O operations on them may be done
330 ** differently than the C library FILE* I/O would be done,
331 ** iff the stream is used for the I/O functions that follow,
332 ** and to support the ones that use an implicit stream.
333 **
334 ** On some platforms, stream or console mode alteration (aka
335 ** "Setup") may be made which is undone by consoleRestore().
336 */
337 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
338 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr );
339 /* A usual call for convenience: */
340 #define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr)
341 
342 /*
343 ** After an initial call to consoleClassifySetup(...), renew
344 ** the same setup it effected. (A call not after is an error.)
345 ** This will restore state altered by consoleRestore();
346 **
347 ** Applications which run an inferior (child) process which
348 ** inherits the same I/O streams may call this function after
349 ** such a process exits to guard against console mode changes.
350 */
351 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void);
352 
353 /*
354 ** Undo any side-effects left by consoleClassifySetup(...).
355 **
356 ** This should be called after consoleClassifySetup() and
357 ** before the process terminates normally. It is suitable
358 ** for use with the atexit() C library procedure. After
359 ** this call, no console I/O should be done until one of
360 ** console{Classify or Renew}Setup(...) is called again.
361 **
362 ** Applications which run an inferior (child) process that
363 ** inherits the same I/O streams might call this procedure
364 ** before so that said process will have a console setup
365 ** however users have configured it or come to expect.
366 */
367 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void );
368 
369 #else /* defined(SQLITE_CIO_NO_CLASSIFY) */
370 # define consoleClassifySetup(i,o,e)
371 # define consoleRenewSetup()
372 # define consoleRestore()
373 #endif /* defined(SQLITE_CIO_NO_CLASSIFY) */
374 
375 #ifndef SQLITE_CIO_NO_REDIRECT
376 /*
377 ** Set stream to be used for the functions below which write
378 ** to "the designated X stream", where X is Output or Error.
379 ** Returns the previous value.
380 **
381 ** Alternatively, pass the special value, invalidFileStream,
382 ** to get the designated stream value without setting it.
383 **
384 ** Before the designated streams are set, they default to
385 ** those passed to consoleClassifySetup(...), and before
386 ** that is called they default to stdout and stderr.
387 **
388 ** It is error to close a stream so designated, then, without
389 ** designating another, use the corresponding {o,e}Emit(...).
390 */
391 SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream;
392 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf);
393 # ifdef CONSIO_SET_ERROR_STREAM
394 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf);
395 # endif
396 #else
397 # define setOutputStream(pf)
398 # define setErrorStream(pf)
399 #endif /* !defined(SQLITE_CIO_NO_REDIRECT) */
400 
401 #ifndef SQLITE_CIO_NO_TRANSLATE
402 /*
403 ** Emit output like fprintf(). If the output is going to the
404 ** console and translation from UTF-8 is necessary, perform
405 ** the needed translation. Otherwise, write formatted output
406 ** to the provided stream almost as-is, possibly with newline
407 ** translation as specified by set{Binary,Text}Mode().
408 */
409 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...);
410 /* Like fPrintfUtf8 except stream is always the designated output. */
411 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...);
412 /* Like fPrintfUtf8 except stream is always the designated error. */
413 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...);
414 
415 /*
416 ** Emit output like fputs(). If the output is going to the
417 ** console and translation from UTF-8 is necessary, perform
418 ** the needed translation. Otherwise, write given text to the
419 ** provided stream almost as-is, possibly with newline
420 ** translation as specified by set{Binary,Text}Mode().
421 */
422 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO);
423 /* Like fPutsUtf8 except stream is always the designated output. */
424 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z);
425 /* Like fPutsUtf8 except stream is always the designated error. */
426 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z);
427 
428 /*
429 ** Emit output like fPutsUtf8(), except that the length of the
430 ** accepted char or character sequence is limited by nAccept.
431 **
432 ** Returns the number of accepted char values.
433 */
434 #ifdef CONSIO_SPUTB
435 SQLITE_INTERNAL_LINKAGE int
436 fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept);
437 /* Like fPutbUtf8 except stream is always the designated output. */
438 #endif
439 SQLITE_INTERNAL_LINKAGE int
440 oPutbUtf8(const char *cBuf, int nAccept);
441 /* Like fPutbUtf8 except stream is always the designated error. */
442 #ifdef CONSIO_EPUTB
443 SQLITE_INTERNAL_LINKAGE int
444 ePutbUtf8(const char *cBuf, int nAccept);
445 #endif
446 
447 /*
448 ** Collect input like fgets(...) with special provisions for input
449 ** from the console on platforms that require same. Defers to the
450 ** C library fgets() when input is not from the console. Newline
451 ** translation may be done as set by set{Binary,Text}Mode(). As a
452 ** convenience, pfIn==NULL is treated as stdin.
453 */
454 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn);
455 /* Like fGetsUtf8 except stream is always the designated input. */
456 /* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */
457 
458 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
459 
460 #ifndef SQLITE_CIO_NO_SETMODE
461 /*
462 ** Set given stream for binary mode, where newline translation is
463 ** not done, or for text mode where, for some platforms, newlines
464 ** are translated to the platform's conventional char sequence.
465 ** If bFlush true, flush the stream.
466 **
467 ** An additional side-effect is that if the stream is one passed
468 ** to consoleClassifySetup() as an output, it is flushed first.
469 **
470 ** Note that binary/text mode has no effect on console I/O
471 ** translation. On all platforms, newline to the console starts
472 ** a new line and CR,LF chars from the console become a newline.
473 */
474 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush);
475 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush);
476 #endif
477 
478 #ifdef SQLITE_CIO_PROMPTED_IN
479 typedef struct Prompts {
480   int numPrompts;
481   const char **azPrompts;
482 } Prompts;
483 
484 /*
485 ** Macros for use of a line editor.
486 **
487 ** The following macros define operations involving use of a
488 ** line-editing library or simple console interaction.
489 ** A "T" argument is a text (char *) buffer or filename.
490 ** A "N" argument is an integer.
491 **
492 ** SHELL_ADD_HISTORY(T) // Record text as line(s) of history.
493 ** SHELL_READ_HISTORY(T) // Read history from file named by T.
494 ** SHELL_WRITE_HISTORY(T) // Write history to file named by T.
495 ** SHELL_STIFLE_HISTORY(N) // Limit history to N entries.
496 **
497 ** A console program which does interactive console input is
498 ** expected to call:
499 ** SHELL_READ_HISTORY(T) before collecting such input;
500 ** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
501 ** SHELL_STIFLE_HISTORY(N) after console input ceases; then
502 ** SHELL_WRITE_HISTORY(T) before the program exits.
503 */
504 
505 /*
506 ** Retrieve a single line of input text from an input stream.
507 **
508 ** If pfIn is the input stream passed to consoleClassifySetup(),
509 ** and azPrompt is not NULL, then a prompt is issued before the
510 ** line is collected, as selected by the isContinuation flag.
511 ** Array azPrompt[{0,1}] holds the {main,continuation} prompt.
512 **
513 ** If zBufPrior is not NULL then it is a buffer from a prior
514 ** call to this routine that can be reused, or will be freed.
515 **
516 ** The result is stored in space obtained from malloc() and
517 ** must either be freed by the caller or else passed back to
518 ** this function as zBufPrior for reuse.
519 **
520 ** This function may call upon services of a line-editing
521 ** library to interactively collect line edited input.
522 */
523 SQLITE_INTERNAL_LINKAGE char *
524 shellGetLine(FILE *pfIn, char *zBufPrior, int nLen,
525              short isContinuation, Prompts azPrompt);
526 #endif /* defined(SQLITE_CIO_PROMPTED_IN) */
527 /*
528 ** TBD: Define an interface for application(s) to generate
529 ** completion candidates for use by the line-editor.
530 **
531 ** This may be premature; the CLI is the only application
532 ** that does this. Yet, getting line-editing melded into
533 ** console I/O is desirable because a line-editing library
534 ** may have to establish console operating mode, possibly
535 ** in a way that interferes with the above functionality.
536 */
537 
538 #if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))
539 /* Skip over as much z[] input char sequence as is valid UTF-8,
540 ** limited per nAccept char's or whole characters and containing
541 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
542 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
543 ** Limit: nAccept>=0 => char count, nAccept<0 => character
544  */
545 SQLITE_INTERNAL_LINKAGE const char*
546 zSkipValidUtf8(const char *z, int nAccept, long ccm);
547 
548 #endif
549 
550 /************************* End ../ext/consio/console_io.h ********************/
551 /************************* Begin ../ext/consio/console_io.c ******************/
552 /*
553 ** 2023 November 4
554 **
555 ** The author disclaims copyright to this source code.  In place of
556 ** a legal notice, here is a blessing:
557 **
558 **    May you do good and not evil.
559 **    May you find forgiveness for yourself and forgive others.
560 **    May you share freely, never taking more than you give.
561 **
562 ********************************************************************************
563 ** This file implements various interfaces used for console and stream I/O
564 ** by the SQLite project command-line tools, as explained in console_io.h .
565 ** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there.
566 */
567 
568 #ifndef SQLITE_CDECL
569 # define SQLITE_CDECL
570 #endif
571 
572 #ifndef SHELL_NO_SYSINC
573 # include <stdarg.h>
574 # include <string.h>
575 # include <stdlib.h>
576 # include <limits.h>
577 # include <assert.h>
578 /* # include "sqlite3.h" */
579 #endif
580 #ifndef HAVE_CONSOLE_IO_H
581 # include "console_io.h"
582 #endif
583 
584 #ifndef SQLITE_CIO_NO_TRANSLATE
585 # if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
586 #  ifndef SHELL_NO_SYSINC
587 #   include <io.h>
588 #   include <fcntl.h>
589 #   undef WIN32_LEAN_AND_MEAN
590 #   define WIN32_LEAN_AND_MEAN
591 #   include <windows.h>
592 #  endif
593 #  define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */
594 # else
595 #  ifndef SHELL_NO_SYSINC
596 #   include <unistd.h>
597 #  endif
598 #  define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */
599 # endif
600 #else
601 # define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */
602 #endif
603 
604 #if CIO_WIN_WC_XLATE
605 /* Character used to represent a known-incomplete UTF-8 char group (�) */
606 static WCHAR cBadGroup = 0xfffd;
607 #endif
608 
609 #if CIO_WIN_WC_XLATE
handleOfFile(FILE * pf)610 static HANDLE handleOfFile(FILE *pf){
611   int fileDesc = _fileno(pf);
612   union { intptr_t osfh; HANDLE fh; } fid = {
613     (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE
614   };
615   return fid.fh;
616 }
617 #endif
618 
619 #ifndef SQLITE_CIO_NO_TRANSLATE
620 typedef struct PerStreamTags {
621 # if CIO_WIN_WC_XLATE
622   HANDLE hx;
623   DWORD consMode;
624   char acIncomplete[4];
625 # else
626   short reachesConsole;
627 # endif
628   FILE *pf;
629 } PerStreamTags;
630 
631 /* Define NULL-like value for things which can validly be 0. */
632 # define SHELL_INVALID_FILE_PTR ((FILE *)~0)
633 # if CIO_WIN_WC_XLATE
634 #  define SHELL_INVALID_CONS_MODE 0xFFFF0000
635 # endif
636 
637 # if CIO_WIN_WC_XLATE
638 #  define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \
639       {0,0,0,0}, SHELL_INVALID_FILE_PTR }
640 # else
641 #  define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR }
642 # endif
643 
644 /* Quickly say whether a known output is going to the console. */
645 # if CIO_WIN_WC_XLATE
pstReachesConsole(PerStreamTags * ppst)646 static short pstReachesConsole(PerStreamTags *ppst){
647   return (ppst->hx != INVALID_HANDLE_VALUE);
648 }
649 # else
650 #  define pstReachesConsole(ppst) 0
651 # endif
652 
653 # if CIO_WIN_WC_XLATE
restoreConsoleArb(PerStreamTags * ppst)654 static void restoreConsoleArb(PerStreamTags *ppst){
655   if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode);
656 }
657 # else
658 #  define restoreConsoleArb(ppst)
659 # endif
660 
661 /* Say whether FILE* appears to be a console, collect associated info. */
streamOfConsole(FILE * pf,PerStreamTags * ppst)662 static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
663 # if CIO_WIN_WC_XLATE
664   short rv = 0;
665   DWORD dwCM = SHELL_INVALID_CONS_MODE;
666   HANDLE fh = handleOfFile(pf);
667   ppst->pf = pf;
668   if( INVALID_HANDLE_VALUE != fh ){
669     rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM));
670   }
671   ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE;
672   ppst->consMode = dwCM;
673   return rv;
674 # else
675   ppst->pf = pf;
676   ppst->reachesConsole = ( (short)isatty(fileno(pf)) );
677   return ppst->reachesConsole;
678 # endif
679 }
680 
681 # if CIO_WIN_WC_XLATE
682 /* Define console modes for use with the Windows Console API. */
683 #  define SHELL_CONI_MODE \
684   (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \
685   | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT)
686 #  define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \
687   | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
688 # endif
689 
690 typedef struct ConsoleInfo {
691   PerStreamTags pstSetup[3];
692   PerStreamTags pstDesignated[3];
693   StreamsAreConsole sacSetup;
694 } ConsoleInfo;
695 
isValidStreamInfo(PerStreamTags * ppst)696 static short isValidStreamInfo(PerStreamTags *ppst){
697   return (ppst->pf != SHELL_INVALID_FILE_PTR);
698 }
699 
700 static ConsoleInfo consoleInfo = {
701   { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
702   { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER },
703   SAC_NoConsole /* sacSetup */
704 };
705 
706 SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0;
707 
708 # if CIO_WIN_WC_XLATE
maybeSetupAsConsole(PerStreamTags * ppst,short odir)709 static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){
710   if( pstReachesConsole(ppst) ){
711     DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE;
712     SetConsoleMode(ppst->hx, cm);
713   }
714 }
715 # else
716 #  define maybeSetupAsConsole(ppst,odir)
717 # endif
718 
consoleRenewSetup(void)719 SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){
720 # if CIO_WIN_WC_XLATE
721   int ix = 0;
722   while( ix < 6 ){
723     PerStreamTags *ppst = (ix<3)?
724       &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3];
725     maybeSetupAsConsole(ppst, (ix % 3)>0);
726     ++ix;
727   }
728 # endif
729 }
730 
731 SQLITE_INTERNAL_LINKAGE StreamsAreConsole
consoleClassifySetup(FILE * pfIn,FILE * pfOut,FILE * pfErr)732 consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){
733   StreamsAreConsole rv = SAC_NoConsole;
734   FILE* apf[3] = { pfIn, pfOut, pfErr };
735   int ix;
736   for( ix = 2; ix >= 0; --ix ){
737     PerStreamTags *ppst = &consoleInfo.pstSetup[ix];
738     if( streamOfConsole(apf[ix], ppst) ){
739       rv |= (SAC_InConsole<<ix);
740     }
741     consoleInfo.pstDesignated[ix] = *ppst;
742     if( ix > 0 ) fflush(apf[ix]);
743   }
744   consoleInfo.sacSetup = rv;
745   consoleRenewSetup();
746   return rv;
747 }
748 
consoleRestore(void)749 SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){
750 # if CIO_WIN_WC_XLATE
751   static ConsoleInfo *pci = &consoleInfo;
752   if( pci->sacSetup ){
753     int ix;
754     for( ix=0; ix<3; ++ix ){
755       if( pci->sacSetup & (SAC_InConsole<<ix) ){
756         PerStreamTags *ppst = &pci->pstSetup[ix];
757         SetConsoleMode(ppst->hx, ppst->consMode);
758       }
759     }
760   }
761 # endif
762 }
763 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
764 
765 #ifdef SQLITE_CIO_INPUT_REDIR
766 /* Say whether given FILE* is among those known, via either
767 ** consoleClassifySetup() or set{Output,Error}Stream, as
768 ** readable, and return an associated PerStreamTags pointer
769 ** if so. Otherwise, return 0.
770 */
isKnownReadable(FILE * pf)771 static PerStreamTags * isKnownReadable(FILE *pf){
772   static PerStreamTags *apst[] = {
773     &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0
774   };
775   int ix = 0;
776   do {
777     if( apst[ix]->pf == pf ) break;
778   } while( apst[++ix] != 0 );
779   return apst[ix];
780 }
781 #endif
782 
783 #ifndef SQLITE_CIO_NO_TRANSLATE
784 /* Say whether given FILE* is among those known, via either
785 ** consoleClassifySetup() or set{Output,Error}Stream, as
786 ** writable, and return an associated PerStreamTags pointer
787 ** if so. Otherwise, return 0.
788 */
isKnownWritable(FILE * pf)789 static PerStreamTags * isKnownWritable(FILE *pf){
790   static PerStreamTags *apst[] = {
791     &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2],
792     &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0
793   };
794   int ix = 0;
795   do {
796     if( apst[ix]->pf == pf ) break;
797   } while( apst[++ix] != 0 );
798   return apst[ix];
799 }
800 
designateEmitStream(FILE * pf,unsigned chix)801 static FILE *designateEmitStream(FILE *pf, unsigned chix){
802   FILE *rv = consoleInfo.pstDesignated[chix].pf;
803   if( pf == invalidFileStream ) return rv;
804   else{
805     /* Setting a possibly new output stream. */
806     PerStreamTags *ppst = isKnownWritable(pf);
807     if( ppst != 0 ){
808       PerStreamTags pst = *ppst;
809       consoleInfo.pstDesignated[chix] = pst;
810     }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]);
811   }
812   return rv;
813 }
814 
setOutputStream(FILE * pf)815 SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){
816   return designateEmitStream(pf, 1);
817 }
818 # ifdef CONSIO_SET_ERROR_STREAM
setErrorStream(FILE * pf)819 SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){
820   return designateEmitStream(pf, 2);
821 }
822 # endif
823 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
824 
825 #ifndef SQLITE_CIO_NO_SETMODE
826 # if CIO_WIN_WC_XLATE
setModeFlushQ(FILE * pf,short bFlush,int mode)827 static void setModeFlushQ(FILE *pf, short bFlush, int mode){
828   if( bFlush ) fflush(pf);
829   _setmode(_fileno(pf), mode);
830 }
831 # else
832 #  define setModeFlushQ(f, b, m) if(b) fflush(f)
833 # endif
834 
setBinaryMode(FILE * pf,short bFlush)835 SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){
836   setModeFlushQ(pf, bFlush, _O_BINARY);
837 }
setTextMode(FILE * pf,short bFlush)838 SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){
839   setModeFlushQ(pf, bFlush, _O_TEXT);
840 }
841 # undef setModeFlushQ
842 
843 #else /* defined(SQLITE_CIO_NO_SETMODE) */
844 # define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
845 # define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0)
846 #endif /* defined(SQLITE_CIO_NO_SETMODE) */
847 
848 #ifndef SQLITE_CIO_NO_TRANSLATE
849 # if CIO_WIN_WC_XLATE
850 /* Write buffer cBuf as output to stream known to reach console,
851 ** limited to ncTake char's. Return ncTake on success, else 0. */
conZstrEmit(PerStreamTags * ppst,const char * z,int ncTake)852 static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){
853   int rv = 0;
854   if( z!=NULL ){
855     int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0);
856     if( nwc > 0 ){
857       WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR));
858       if( zw!=NULL ){
859         nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc);
860         if( nwc > 0 ){
861           /* Translation from UTF-8 to UTF-16, then WCHARs out. */
862           if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){
863             rv = ncTake;
864           }
865         }
866         sqlite3_free(zw);
867       }
868     }
869   }
870   return rv;
871 }
872 
873 /* For {f,o,e}PrintfUtf8() when stream is known to reach console. */
conioVmPrintf(PerStreamTags * ppst,const char * zFormat,va_list ap)874 static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){
875   char *z = sqlite3_vmprintf(zFormat, ap);
876   if( z ){
877     int rv = conZstrEmit(ppst, z, (int)strlen(z));
878     sqlite3_free(z);
879     return rv;
880   }else return 0;
881 }
882 # endif /* CIO_WIN_WC_XLATE */
883 
884 # ifdef CONSIO_GET_EMIT_STREAM
getDesignatedEmitStream(FILE * pf,unsigned chix,PerStreamTags * ppst)885 static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix,
886                                                PerStreamTags *ppst){
887   PerStreamTags *rv = isKnownWritable(pf);
888   short isValid = (rv!=0)? isValidStreamInfo(rv) : 0;
889   if( rv != 0 && isValid ) return rv;
890   streamOfConsole(pf, ppst);
891   return ppst;
892 }
893 # endif
894 
895 /* Get stream info, either for designated output or error stream when
896 ** chix equals 1 or 2, or for an arbitrary stream when chix == 0.
897 ** In either case, ppst references a caller-owned PerStreamTags
898 ** struct which may be filled in if none of the known writable
899 ** streams is being held by consoleInfo. The ppf parameter is a
900 ** byref output when chix!=0 and a byref input when chix==0.
901  */
902 static PerStreamTags *
getEmitStreamInfo(unsigned chix,PerStreamTags * ppst,FILE ** ppf)903 getEmitStreamInfo(unsigned chix, PerStreamTags *ppst,
904                   /* in/out */ FILE **ppf){
905   PerStreamTags *ppstTry;
906   FILE *pfEmit;
907   if( chix > 0 ){
908     ppstTry = &consoleInfo.pstDesignated[chix];
909     if( !isValidStreamInfo(ppstTry) ){
910       ppstTry = &consoleInfo.pstSetup[chix];
911       pfEmit = ppst->pf;
912     }else pfEmit = ppstTry->pf;
913     if( !isValidStreamInfo(ppstTry) ){
914       pfEmit = (chix > 1)? stderr : stdout;
915       ppstTry = ppst;
916       streamOfConsole(pfEmit, ppstTry);
917     }
918     *ppf = pfEmit;
919   }else{
920     ppstTry = isKnownWritable(*ppf);
921     if( ppstTry != 0 ) return ppstTry;
922     streamOfConsole(*ppf, ppst);
923     return ppst;
924   }
925   return ppstTry;
926 }
927 
oPrintfUtf8(const char * zFormat,...)928 SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){
929   va_list ap;
930   int rv;
931   FILE *pfOut;
932   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
933 # if CIO_WIN_WC_XLATE
934   PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
935 # else
936   getEmitStreamInfo(1, &pst, &pfOut);
937 # endif
938   assert(zFormat!=0);
939   va_start(ap, zFormat);
940 # if CIO_WIN_WC_XLATE
941   if( pstReachesConsole(ppst) ){
942     rv = conioVmPrintf(ppst, zFormat, ap);
943   }else{
944 # endif
945     rv = vfprintf(pfOut, zFormat, ap);
946 # if CIO_WIN_WC_XLATE
947   }
948 # endif
949   va_end(ap);
950   return rv;
951 }
952 
ePrintfUtf8(const char * zFormat,...)953 SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){
954   va_list ap;
955   int rv;
956   FILE *pfErr;
957   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
958 # if CIO_WIN_WC_XLATE
959   PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
960 # else
961   getEmitStreamInfo(2, &pst, &pfErr);
962 # endif
963   assert(zFormat!=0);
964   va_start(ap, zFormat);
965 # if CIO_WIN_WC_XLATE
966   if( pstReachesConsole(ppst) ){
967     rv = conioVmPrintf(ppst, zFormat, ap);
968   }else{
969 # endif
970     rv = vfprintf(pfErr, zFormat, ap);
971 # if CIO_WIN_WC_XLATE
972   }
973 # endif
974   va_end(ap);
975   return rv;
976 }
977 
fPrintfUtf8(FILE * pfO,const char * zFormat,...)978 SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){
979   va_list ap;
980   int rv;
981   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
982 # if CIO_WIN_WC_XLATE
983   PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
984 # else
985   getEmitStreamInfo(0, &pst, &pfO);
986 # endif
987   assert(zFormat!=0);
988   va_start(ap, zFormat);
989 # if CIO_WIN_WC_XLATE
990   if( pstReachesConsole(ppst) ){
991     maybeSetupAsConsole(ppst, 1);
992     rv = conioVmPrintf(ppst, zFormat, ap);
993     if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
994   }else{
995 # endif
996     rv = vfprintf(pfO, zFormat, ap);
997 # if CIO_WIN_WC_XLATE
998   }
999 # endif
1000   va_end(ap);
1001   return rv;
1002 }
1003 
fPutsUtf8(const char * z,FILE * pfO)1004 SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){
1005   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1006 # if CIO_WIN_WC_XLATE
1007   PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1008 # else
1009   getEmitStreamInfo(0, &pst, &pfO);
1010 # endif
1011   assert(z!=0);
1012 # if CIO_WIN_WC_XLATE
1013   if( pstReachesConsole(ppst) ){
1014     int rv;
1015     maybeSetupAsConsole(ppst, 1);
1016     rv = conZstrEmit(ppst, z, (int)strlen(z));
1017     if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1018     return rv;
1019   }else {
1020 # endif
1021     return (fputs(z, pfO)<0)? 0 : (int)strlen(z);
1022 # if CIO_WIN_WC_XLATE
1023   }
1024 # endif
1025 }
1026 
ePutsUtf8(const char * z)1027 SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){
1028   FILE *pfErr;
1029   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1030 # if CIO_WIN_WC_XLATE
1031   PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1032 # else
1033   getEmitStreamInfo(2, &pst, &pfErr);
1034 # endif
1035   assert(z!=0);
1036 # if CIO_WIN_WC_XLATE
1037   if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1038   else {
1039 # endif
1040     return (fputs(z, pfErr)<0)? 0 : (int)strlen(z);
1041 # if CIO_WIN_WC_XLATE
1042   }
1043 # endif
1044 }
1045 
oPutsUtf8(const char * z)1046 SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){
1047   FILE *pfOut;
1048   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1049 # if CIO_WIN_WC_XLATE
1050   PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1051 # else
1052   getEmitStreamInfo(1, &pst, &pfOut);
1053 # endif
1054   assert(z!=0);
1055 # if CIO_WIN_WC_XLATE
1056   if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z));
1057   else {
1058 # endif
1059     return (fputs(z, pfOut)<0)? 0 : (int)strlen(z);
1060 # if CIO_WIN_WC_XLATE
1061   }
1062 # endif
1063 }
1064 
1065 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1066 
1067 #if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE))
1068 /* Skip over as much z[] input char sequence as is valid UTF-8,
1069 ** limited per nAccept char's or whole characters and containing
1070 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
1071 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
1072 ** Limit: nAccept>=0 => char count, nAccept<0 => character
1073  */
1074 SQLITE_INTERNAL_LINKAGE const char*
zSkipValidUtf8(const char * z,int nAccept,long ccm)1075 zSkipValidUtf8(const char *z, int nAccept, long ccm){
1076   int ng = (nAccept<0)? -nAccept : 0;
1077   const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
1078   assert(z!=0);
1079   while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
1080     char c = *z;
1081     if( (c & 0x80) == 0 ){
1082       if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
1083       ++z; /* ASCII */
1084     }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
1085     else{
1086       const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
1087       do{
1088         if( pcLimit && zt >= pcLimit ) return z;
1089         else{
1090           char ct = *zt++;
1091           if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
1092             /* Trailing bytes are too few, too many, or invalid. */
1093             return z;
1094           }
1095         }
1096       } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
1097       z = zt;
1098     }
1099   }
1100   return z;
1101 }
1102 #endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/
1103 
1104 #ifndef SQLITE_CIO_NO_TRANSLATE
1105 # ifdef CONSIO_SPUTB
1106 SQLITE_INTERNAL_LINKAGE int
fPutbUtf8(FILE * pfO,const char * cBuf,int nAccept)1107 fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){
1108   assert(pfO!=0);
1109 #  if CIO_WIN_WC_XLATE
1110   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1111   PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO);
1112   if( pstReachesConsole(ppst) ){
1113     int rv;
1114     maybeSetupAsConsole(ppst, 1);
1115     rv = conZstrEmit(ppst, cBuf, nAccept);
1116     if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst);
1117     return rv;
1118   }else {
1119 #  endif
1120     return (int)fwrite(cBuf, 1, nAccept, pfO);
1121 #  if CIO_WIN_WC_XLATE
1122   }
1123 #  endif
1124 }
1125 # endif
1126 
1127 SQLITE_INTERNAL_LINKAGE int
oPutbUtf8(const char * cBuf,int nAccept)1128 oPutbUtf8(const char *cBuf, int nAccept){
1129   FILE *pfOut;
1130   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1131 # if CIO_WIN_WC_XLATE
1132   PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut);
1133 # else
1134   getEmitStreamInfo(1, &pst, &pfOut);
1135 # endif
1136 # if CIO_WIN_WC_XLATE
1137   if( pstReachesConsole(ppst) ){
1138     return conZstrEmit(ppst, cBuf, nAccept);
1139   }else {
1140 # endif
1141     return (int)fwrite(cBuf, 1, nAccept, pfOut);
1142 # if CIO_WIN_WC_XLATE
1143   }
1144 # endif
1145 }
1146 
1147 # ifdef CONSIO_EPUTB
1148 SQLITE_INTERNAL_LINKAGE int
ePutbUtf8(const char * cBuf,int nAccept)1149 ePutbUtf8(const char *cBuf, int nAccept){
1150   FILE *pfErr;
1151   PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */
1152   PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr);
1153 #  if CIO_WIN_WC_XLATE
1154   if( pstReachesConsole(ppst) ){
1155     return conZstrEmit(ppst, cBuf, nAccept);
1156   }else {
1157 #  endif
1158     return (int)fwrite(cBuf, 1, nAccept, pfErr);
1159 #  if CIO_WIN_WC_XLATE
1160   }
1161 #  endif
1162 }
1163 # endif /* defined(CONSIO_EPUTB) */
1164 
fGetsUtf8(char * cBuf,int ncMax,FILE * pfIn)1165 SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1166   if( pfIn==0 ) pfIn = stdin;
1167 # if CIO_WIN_WC_XLATE
1168   if( pfIn == consoleInfo.pstSetup[0].pf
1169       && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){
1170 #  if CIO_WIN_WC_XLATE==1
1171 #   define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */
1172     WCHAR wcBuf[SHELL_GULP+1];
1173     int lend = 0, noc = 0;
1174     if( ncMax > 0 ) cBuf[0] = 0;
1175     while( noc < ncMax-8-1 && !lend ){
1176       /* There is room for at least 2 more characters and a 0-terminator. */
1177       int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4;
1178 #   undef SHELL_GULP
1179       DWORD nbr = 0;
1180       BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0);
1181       if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){
1182         /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */
1183         DWORD nbrx;
1184         bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0);
1185         if( bRC ) nbr += nbrx;
1186       }
1187       if( !bRC || (noc==0 && nbr==0) ) return 0;
1188       if( nbr > 0 ){
1189         int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0);
1190         if( nmb != 0 && noc+nmb <= ncMax ){
1191           int iseg = noc;
1192           nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0);
1193           noc += nmb;
1194           /* Fixup line-ends as coded by Windows for CR (or "Enter".)
1195           ** This is done without regard for any setMode{Text,Binary}()
1196           ** call that might have been done on the interactive input.
1197           */
1198           if( noc > 0 ){
1199             if( cBuf[noc-1]=='\n' ){
1200               lend = 1;
1201               if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n';
1202             }
1203           }
1204           /* Check for ^Z (anywhere in line) too, to act as EOF. */
1205           while( iseg < noc ){
1206             if( cBuf[iseg]=='\x1a' ){
1207               noc = iseg; /* Chop ^Z and anything following. */
1208               lend = 1; /* Counts as end of line too. */
1209               break;
1210             }
1211             ++iseg;
1212           }
1213         }else break; /* Drop apparent garbage in. (Could assert.) */
1214       }else break;
1215     }
1216     /* If got nothing, (after ^Z chop), must be at end-of-file. */
1217     if( noc > 0 ){
1218       cBuf[noc] = 0;
1219       return cBuf;
1220     }else return 0;
1221 #  endif
1222   }else{
1223 # endif
1224     return fgets(cBuf, ncMax, pfIn);
1225 # if CIO_WIN_WC_XLATE
1226   }
1227 # endif
1228 }
1229 #endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1230 
1231 #undef SHELL_INVALID_FILE_PTR
1232 
1233 /************************* End ../ext/consio/console_io.c ********************/
1234 
1235 #ifndef SQLITE_SHELL_FIDDLE
1236 
1237 /* From here onward, fgets() is redirected to the console_io library. */
1238 # define fgets(b,n,f) fGetsUtf8(b,n,f)
1239 /*
1240  * Define macros for emitting output text in various ways:
1241  *  sputz(s, z)      => emit 0-terminated string z to given stream s
1242  *  sputf(s, f, ...) => emit varargs per format f to given stream s
1243  *  oputz(z)         => emit 0-terminated string z to default stream
1244  *  oputf(f, ...)    => emit varargs per format f to default stream
1245  *  eputz(z)         => emit 0-terminated string z to error stream
1246  *  eputf(f, ...)    => emit varargs per format f to error stream
1247  *  oputb(b, n)      => emit char buffer b[0..n-1] to default stream
1248  *
1249  * Note that the default stream is whatever has been last set via:
1250  *   setOutputStream(FILE *pf)
1251  * This is normally the stream that CLI normal output goes to.
1252  * For the stand-alone CLI, it is stdout with no .output redirect.
1253  */
1254 # define sputz(s,z) fPutsUtf8(z,s)
1255 # define sputf fPrintfUtf8
1256 # define oputz(z) oPutsUtf8(z)
1257 # define oputf oPrintfUtf8
1258 # define eputz(z) ePutsUtf8(z)
1259 # define eputf ePrintfUtf8
1260 # define oputb(buf,na) oPutbUtf8(buf,na)
1261 
1262 #else
1263 /* For Fiddle, all console handling and emit redirection is omitted. */
1264 # define sputz(fp,z) fputs(z,fp)
1265 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
1266 # define oputz(z) fputs(z,stdout)
1267 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
1268 # define eputz(z) fputs(z,stderr)
1269 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
1270 # define oputb(buf,na) fwrite(buf,1,na,stdout)
1271 #endif
1272 
1273 /* True if the timer is enabled */
1274 static int enableTimer = 0;
1275 
1276 /* A version of strcmp() that works with NULL values */
cli_strcmp(const char * a,const char * b)1277 static int cli_strcmp(const char *a, const char *b){
1278   if( a==0 ) a = "";
1279   if( b==0 ) b = "";
1280   return strcmp(a,b);
1281 }
cli_strncmp(const char * a,const char * b,size_t n)1282 static int cli_strncmp(const char *a, const char *b, size_t n){
1283   if( a==0 ) a = "";
1284   if( b==0 ) b = "";
1285   return strncmp(a,b,n);
1286 }
1287 
1288 /* Return the current wall-clock time */
timeOfDay(void)1289 static sqlite3_int64 timeOfDay(void){
1290   static sqlite3_vfs *clockVfs = 0;
1291   sqlite3_int64 t;
1292   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
1293   if( clockVfs==0 ) return 0;  /* Never actually happens */
1294   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
1295     clockVfs->xCurrentTimeInt64(clockVfs, &t);
1296   }else{
1297     double r;
1298     clockVfs->xCurrentTime(clockVfs, &r);
1299     t = (sqlite3_int64)(r*86400000.0);
1300   }
1301   return t;
1302 }
1303 
1304 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
1305 #include <sys/time.h>
1306 #include <sys/resource.h>
1307 
1308 /* VxWorks does not support getrusage() as far as we can determine */
1309 #if defined(_WRS_KERNEL) || defined(__RTP__)
1310 struct rusage {
1311   struct timeval ru_utime; /* user CPU time used */
1312   struct timeval ru_stime; /* system CPU time used */
1313 };
1314 #define getrusage(A,B) memset(B,0,sizeof(*B))
1315 #endif
1316 
1317 /* Saved resource information for the beginning of an operation */
1318 static struct rusage sBegin;  /* CPU time at start */
1319 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
1320 
1321 /*
1322 ** Begin timing an operation
1323 */
beginTimer(void)1324 static void beginTimer(void){
1325   if( enableTimer ){
1326     getrusage(RUSAGE_SELF, &sBegin);
1327     iBegin = timeOfDay();
1328   }
1329 }
1330 
1331 /* Return the difference of two time_structs in seconds */
timeDiff(struct timeval * pStart,struct timeval * pEnd)1332 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
1333   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
1334          (double)(pEnd->tv_sec - pStart->tv_sec);
1335 }
1336 
1337 /*
1338 ** Print the timing results.
1339 */
endTimer(void)1340 static void endTimer(void){
1341   if( enableTimer ){
1342     sqlite3_int64 iEnd = timeOfDay();
1343     struct rusage sEnd;
1344     getrusage(RUSAGE_SELF, &sEnd);
1345     sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1346           (iEnd - iBegin)*0.001,
1347           timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
1348           timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
1349   }
1350 }
1351 
1352 #define BEGIN_TIMER beginTimer()
1353 #define END_TIMER endTimer()
1354 #define HAS_TIMER 1
1355 
1356 #elif (defined(_WIN32) || defined(WIN32))
1357 
1358 /* Saved resource information for the beginning of an operation */
1359 static HANDLE hProcess;
1360 static FILETIME ftKernelBegin;
1361 static FILETIME ftUserBegin;
1362 static sqlite3_int64 ftWallBegin;
1363 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
1364                                     LPFILETIME, LPFILETIME);
1365 static GETPROCTIMES getProcessTimesAddr = NULL;
1366 
1367 /*
1368 ** Check to see if we have timer support.  Return 1 if necessary
1369 ** support found (or found previously).
1370 */
hasTimer(void)1371 static int hasTimer(void){
1372   if( getProcessTimesAddr ){
1373     return 1;
1374   } else {
1375 #if !SQLITE_OS_WINRT
1376     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
1377     ** versions. See if the version we are running on has it, and if it
1378     ** does, save off a pointer to it and the current process handle.
1379     */
1380     hProcess = GetCurrentProcess();
1381     if( hProcess ){
1382       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
1383       if( NULL != hinstLib ){
1384         getProcessTimesAddr =
1385             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
1386         if( NULL != getProcessTimesAddr ){
1387           return 1;
1388         }
1389         FreeLibrary(hinstLib);
1390       }
1391     }
1392 #endif
1393   }
1394   return 0;
1395 }
1396 
1397 /*
1398 ** Begin timing an operation
1399 */
beginTimer(void)1400 static void beginTimer(void){
1401   if( enableTimer && getProcessTimesAddr ){
1402     FILETIME ftCreation, ftExit;
1403     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
1404                         &ftKernelBegin,&ftUserBegin);
1405     ftWallBegin = timeOfDay();
1406   }
1407 }
1408 
1409 /* Return the difference of two FILETIME structs in seconds */
timeDiff(FILETIME * pStart,FILETIME * pEnd)1410 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
1411   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
1412   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
1413   return (double) ((i64End - i64Start) / 10000000.0);
1414 }
1415 
1416 /*
1417 ** Print the timing results.
1418 */
endTimer(void)1419 static void endTimer(void){
1420   if( enableTimer && getProcessTimesAddr){
1421     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
1422     sqlite3_int64 ftWallEnd = timeOfDay();
1423     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
1424     sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
1425           (ftWallEnd - ftWallBegin)*0.001,
1426           timeDiff(&ftUserBegin, &ftUserEnd),
1427           timeDiff(&ftKernelBegin, &ftKernelEnd));
1428   }
1429 }
1430 
1431 #define BEGIN_TIMER beginTimer()
1432 #define END_TIMER endTimer()
1433 #define HAS_TIMER hasTimer()
1434 
1435 #else
1436 #define BEGIN_TIMER
1437 #define END_TIMER
1438 #define HAS_TIMER 0
1439 #endif
1440 
1441 /*
1442 ** Used to prevent warnings about unused parameters
1443 */
1444 #define UNUSED_PARAMETER(x) (void)(x)
1445 
1446 /*
1447 ** Number of elements in an array
1448 */
1449 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
1450 
1451 /*
1452 ** If the following flag is set, then command execution stops
1453 ** at an error if we are not interactive.
1454 */
1455 static int bail_on_error = 0;
1456 
1457 /*
1458 ** Treat stdin as an interactive input if the following variable
1459 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
1460 */
1461 static int stdin_is_interactive = 1;
1462 
1463 /*
1464 ** On Windows systems we need to know if standard output is a console
1465 ** in order to show that UTF-16 translation is done in the sign-on
1466 ** banner. The following variable is true if it is the console.
1467 */
1468 static int stdout_is_console = 1;
1469 
1470 /*
1471 ** The following is the open SQLite database.  We make a pointer
1472 ** to this database a static variable so that it can be accessed
1473 ** by the SIGINT handler to interrupt database processing.
1474 */
1475 static sqlite3 *globalDb = 0;
1476 
1477 /*
1478 ** True if an interrupt (Control-C) has been received.
1479 */
1480 static volatile int seenInterrupt = 0;
1481 
1482 /*
1483 ** This is the name of our program. It is set in main(), used
1484 ** in a number of other places, mostly for error messages.
1485 */
1486 static char *Argv0;
1487 
1488 /*
1489 ** Prompt strings. Initialized in main. Settable with
1490 **   .prompt main continue
1491 */
1492 #define PROMPT_LEN_MAX 20
1493 /* First line prompt.   default: "sqlite> " */
1494 static char mainPrompt[PROMPT_LEN_MAX];
1495 /* Continuation prompt. default: "   ...> " */
1496 static char continuePrompt[PROMPT_LEN_MAX];
1497 
1498 /* This is variant of the standard-library strncpy() routine with the
1499 ** one change that the destination string is always zero-terminated, even
1500 ** if there is no zero-terminator in the first n-1 characters of the source
1501 ** string.
1502 */
shell_strncpy(char * dest,const char * src,size_t n)1503 static char *shell_strncpy(char *dest, const char *src, size_t n){
1504   size_t i;
1505   for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
1506   dest[i] = 0;
1507   return dest;
1508 }
1509 
1510 /*
1511 ** Optionally disable dynamic continuation prompt.
1512 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
1513 ** or open parentheses level if non-zero, or continuation prompt as set.
1514 ** This facility interacts with the scanner and process_input() where the
1515 ** below 5 macros are used.
1516 */
1517 #ifdef SQLITE_OMIT_DYNAPROMPT
1518 # define CONTINUATION_PROMPT continuePrompt
1519 # define CONTINUE_PROMPT_RESET
1520 # define CONTINUE_PROMPT_AWAITS(p,s)
1521 # define CONTINUE_PROMPT_AWAITC(p,c)
1522 # define CONTINUE_PAREN_INCR(p,n)
1523 # define CONTINUE_PROMPT_PSTATE 0
1524 typedef void *t_NoDynaPrompt;
1525 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
1526 #else
1527 # define CONTINUATION_PROMPT dynamicContinuePrompt()
1528 # define CONTINUE_PROMPT_RESET \
1529   do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
1530 # define CONTINUE_PROMPT_AWAITS(p,s) \
1531   if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
1532 # define CONTINUE_PROMPT_AWAITC(p,c) \
1533   if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
1534 # define CONTINUE_PAREN_INCR(p,n) \
1535   if(p && stdin_is_interactive) (trackParenLevel(p,n))
1536 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
1537 typedef struct DynaPrompt *t_DynaPromptRef;
1538 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
1539 
1540 static struct DynaPrompt {
1541   char dynamicPrompt[PROMPT_LEN_MAX];
1542   char acAwait[2];
1543   int inParenLevel;
1544   char *zScannerAwaits;
1545 } dynPrompt = { {0}, {0}, 0, 0 };
1546 
1547 /* Record parenthesis nesting level change, or force level to 0. */
trackParenLevel(struct DynaPrompt * p,int ni)1548 static void trackParenLevel(struct DynaPrompt *p, int ni){
1549   p->inParenLevel += ni;
1550   if( ni==0 ) p->inParenLevel = 0;
1551   p->zScannerAwaits = 0;
1552 }
1553 
1554 /* Record that a lexeme is opened, or closed with args==0. */
setLexemeOpen(struct DynaPrompt * p,char * s,char c)1555 static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
1556   if( s!=0 || c==0 ){
1557     p->zScannerAwaits = s;
1558     p->acAwait[0] = 0;
1559   }else{
1560     p->acAwait[0] = c;
1561     p->zScannerAwaits = p->acAwait;
1562   }
1563 }
1564 
1565 /* Upon demand, derive the continuation prompt to display. */
dynamicContinuePrompt(void)1566 static char *dynamicContinuePrompt(void){
1567   if( continuePrompt[0]==0
1568       || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
1569     return continuePrompt;
1570   }else{
1571     if( dynPrompt.zScannerAwaits ){
1572       size_t ncp = strlen(continuePrompt);
1573       size_t ndp = strlen(dynPrompt.zScannerAwaits);
1574       if( ndp > ncp-3 ) return continuePrompt;
1575       strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
1576       while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
1577       shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1578               PROMPT_LEN_MAX-4);
1579     }else{
1580       if( dynPrompt.inParenLevel>9 ){
1581         shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
1582       }else if( dynPrompt.inParenLevel<0 ){
1583         shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
1584       }else{
1585         shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
1586         dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
1587       }
1588       shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
1589                     PROMPT_LEN_MAX-4);
1590     }
1591   }
1592   return dynPrompt.dynamicPrompt;
1593 }
1594 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
1595 
1596 /* Indicate out-of-memory and exit. */
shell_out_of_memory(void)1597 static void shell_out_of_memory(void){
1598   eputz("Error: out of memory\n");
1599   exit(1);
1600 }
1601 
1602 /* Check a pointer to see if it is NULL.  If it is NULL, exit with an
1603 ** out-of-memory error.
1604 */
shell_check_oom(const void * p)1605 static void shell_check_oom(const void *p){
1606   if( p==0 ) shell_out_of_memory();
1607 }
1608 
1609 /*
1610 ** Write I/O traces to the following stream.
1611 */
1612 #ifdef SQLITE_ENABLE_IOTRACE
1613 static FILE *iotrace = 0;
1614 #endif
1615 
1616 /*
1617 ** This routine works like printf in that its first argument is a
1618 ** format string and subsequent arguments are values to be substituted
1619 ** in place of % fields.  The result of formatting this string
1620 ** is written to iotrace.
1621 */
1622 #ifdef SQLITE_ENABLE_IOTRACE
iotracePrintf(const char * zFormat,...)1623 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
1624   va_list ap;
1625   char *z;
1626   if( iotrace==0 ) return;
1627   va_start(ap, zFormat);
1628   z = sqlite3_vmprintf(zFormat, ap);
1629   va_end(ap);
1630   sputf(iotrace, "%s", z);
1631   sqlite3_free(z);
1632 }
1633 #endif
1634 
1635 /*
1636 ** Output string zUtf to Out stream as w characters.  If w is negative,
1637 ** then right-justify the text.  W is the width in UTF-8 characters, not
1638 ** in bytes.  This is different from the %*.*s specification in printf
1639 ** since with %*.*s the width is measured in bytes, not characters.
1640 */
utf8_width_print(int w,const char * zUtf)1641 static void utf8_width_print(int w, const char *zUtf){
1642   int i;
1643   int n;
1644   int aw = w<0 ? -w : w;
1645   if( zUtf==0 ) zUtf = "";
1646   for(i=n=0; zUtf[i]; i++){
1647     if( (zUtf[i]&0xc0)!=0x80 ){
1648       n++;
1649       if( n==aw ){
1650         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
1651         break;
1652       }
1653     }
1654   }
1655   if( n>=aw ){
1656     oputf("%.*s", i, zUtf);
1657   }else if( w<0 ){
1658     oputf("%*s%s", aw-n, "", zUtf);
1659   }else{
1660     oputf("%s%*s", zUtf, aw-n, "");
1661   }
1662 }
1663 
1664 
1665 /*
1666 ** Determines if a string is a number of not.
1667 */
isNumber(const char * z,int * realnum)1668 static int isNumber(const char *z, int *realnum){
1669   if( *z=='-' || *z=='+' ) z++;
1670   if( !IsDigit(*z) ){
1671     return 0;
1672   }
1673   z++;
1674   if( realnum ) *realnum = 0;
1675   while( IsDigit(*z) ){ z++; }
1676   if( *z=='.' ){
1677     z++;
1678     if( !IsDigit(*z) ) return 0;
1679     while( IsDigit(*z) ){ z++; }
1680     if( realnum ) *realnum = 1;
1681   }
1682   if( *z=='e' || *z=='E' ){
1683     z++;
1684     if( *z=='+' || *z=='-' ) z++;
1685     if( !IsDigit(*z) ) return 0;
1686     while( IsDigit(*z) ){ z++; }
1687     if( realnum ) *realnum = 1;
1688   }
1689   return *z==0;
1690 }
1691 
1692 /*
1693 ** Compute a string length that is limited to what can be stored in
1694 ** lower 30 bits of a 32-bit signed integer.
1695 */
strlen30(const char * z)1696 static int strlen30(const char *z){
1697   const char *z2 = z;
1698   while( *z2 ){ z2++; }
1699   return 0x3fffffff & (int)(z2 - z);
1700 }
1701 
1702 /*
1703 ** Return the length of a string in characters.  Multibyte UTF8 characters
1704 ** count as a single character.
1705 */
strlenChar(const char * z)1706 static int strlenChar(const char *z){
1707   int n = 0;
1708   while( *z ){
1709     if( (0xc0&*(z++))!=0x80 ) n++;
1710   }
1711   return n;
1712 }
1713 
1714 /*
1715 ** Return open FILE * if zFile exists, can be opened for read
1716 ** and is an ordinary file or a character stream source.
1717 ** Otherwise return 0.
1718 */
openChrSource(const char * zFile)1719 static FILE * openChrSource(const char *zFile){
1720 #if defined(_WIN32) || defined(WIN32)
1721   struct __stat64 x = {0};
1722 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
1723   /* On Windows, open first, then check the stream nature. This order
1724   ** is necessary because _stat() and sibs, when checking a named pipe,
1725   ** effectively break the pipe as its supplier sees it. */
1726   FILE *rv = fopen(zFile, "rb");
1727   if( rv==0 ) return 0;
1728   if( _fstat64(_fileno(rv), &x) != 0
1729       || !STAT_CHR_SRC(x.st_mode)){
1730     fclose(rv);
1731     rv = 0;
1732   }
1733   return rv;
1734 #else
1735   struct stat x = {0};
1736   int rc = stat(zFile, &x);
1737 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
1738   if( rc!=0 ) return 0;
1739   if( STAT_CHR_SRC(x.st_mode) ){
1740     return fopen(zFile, "rb");
1741   }else{
1742     return 0;
1743   }
1744 #endif
1745 #undef STAT_CHR_SRC
1746 }
1747 
1748 /*
1749 ** This routine reads a line of text from FILE in, stores
1750 ** the text in memory obtained from malloc() and returns a pointer
1751 ** to the text.  NULL is returned at end of file, or if malloc()
1752 ** fails.
1753 **
1754 ** If zLine is not NULL then it is a malloced buffer returned from
1755 ** a previous call to this routine that may be reused.
1756 */
local_getline(char * zLine,FILE * in)1757 static char *local_getline(char *zLine, FILE *in){
1758   int nLine = zLine==0 ? 0 : 100;
1759   int n = 0;
1760 
1761   while( 1 ){
1762     if( n+100>nLine ){
1763       nLine = nLine*2 + 100;
1764       zLine = realloc(zLine, nLine);
1765       shell_check_oom(zLine);
1766     }
1767     if( fgets(&zLine[n], nLine - n, in)==0 ){
1768       if( n==0 ){
1769         free(zLine);
1770         return 0;
1771       }
1772       zLine[n] = 0;
1773       break;
1774     }
1775     while( zLine[n] ) n++;
1776     if( n>0 && zLine[n-1]=='\n' ){
1777       n--;
1778       if( n>0 && zLine[n-1]=='\r' ) n--;
1779       zLine[n] = 0;
1780       break;
1781     }
1782   }
1783   return zLine;
1784 }
1785 
1786 /*
1787 ** Retrieve a single line of input text.
1788 **
1789 ** If in==0 then read from standard input and prompt before each line.
1790 ** If isContinuation is true, then a continuation prompt is appropriate.
1791 ** If isContinuation is zero, then the main prompt should be used.
1792 **
1793 ** If zPrior is not NULL then it is a buffer from a prior call to this
1794 ** routine that can be reused.
1795 **
1796 ** The result is stored in space obtained from malloc() and must either
1797 ** be freed by the caller or else passed back into this routine via the
1798 ** zPrior argument for reuse.
1799 */
1800 #ifndef SQLITE_SHELL_FIDDLE
one_input_line(FILE * in,char * zPrior,int isContinuation)1801 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
1802   char *zPrompt;
1803   char *zResult;
1804   if( in!=0 ){
1805     zResult = local_getline(zPrior, in);
1806   }else{
1807     zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
1808 #if SHELL_USE_LOCAL_GETLINE
1809     sputz(stdout, zPrompt);
1810     fflush(stdout);
1811     do{
1812       zResult = local_getline(zPrior, stdin);
1813       zPrior = 0;
1814       /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1815       if( zResult==0 ) sqlite3_sleep(50);
1816     }while( zResult==0 && seenInterrupt>0 );
1817 #else
1818     free(zPrior);
1819     zResult = shell_readline(zPrompt);
1820     while( zResult==0 ){
1821       /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1822       sqlite3_sleep(50);
1823       if( seenInterrupt==0 ) break;
1824       zResult = shell_readline("");
1825     }
1826     if( zResult && *zResult ) shell_add_history(zResult);
1827 #endif
1828   }
1829   return zResult;
1830 }
1831 #endif /* !SQLITE_SHELL_FIDDLE */
1832 
1833 /*
1834 ** Return the value of a hexadecimal digit.  Return -1 if the input
1835 ** is not a hex digit.
1836 */
hexDigitValue(char c)1837 static int hexDigitValue(char c){
1838   if( c>='0' && c<='9' ) return c - '0';
1839   if( c>='a' && c<='f' ) return c - 'a' + 10;
1840   if( c>='A' && c<='F' ) return c - 'A' + 10;
1841   return -1;
1842 }
1843 
1844 /*
1845 ** Interpret zArg as an integer value, possibly with suffixes.
1846 */
integerValue(const char * zArg)1847 static sqlite3_int64 integerValue(const char *zArg){
1848   sqlite3_int64 v = 0;
1849   static const struct { char *zSuffix; int iMult; } aMult[] = {
1850     { "KiB", 1024 },
1851     { "MiB", 1024*1024 },
1852     { "GiB", 1024*1024*1024 },
1853     { "KB",  1000 },
1854     { "MB",  1000000 },
1855     { "GB",  1000000000 },
1856     { "K",   1000 },
1857     { "M",   1000000 },
1858     { "G",   1000000000 },
1859   };
1860   int i;
1861   int isNeg = 0;
1862   if( zArg[0]=='-' ){
1863     isNeg = 1;
1864     zArg++;
1865   }else if( zArg[0]=='+' ){
1866     zArg++;
1867   }
1868   if( zArg[0]=='0' && zArg[1]=='x' ){
1869     int x;
1870     zArg += 2;
1871     while( (x = hexDigitValue(zArg[0]))>=0 ){
1872       v = (v<<4) + x;
1873       zArg++;
1874     }
1875   }else{
1876     while( IsDigit(zArg[0]) ){
1877       v = v*10 + zArg[0] - '0';
1878       zArg++;
1879     }
1880   }
1881   for(i=0; i<ArraySize(aMult); i++){
1882     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1883       v *= aMult[i].iMult;
1884       break;
1885     }
1886   }
1887   return isNeg? -v : v;
1888 }
1889 
1890 /*
1891 ** A variable length string to which one can append text.
1892 */
1893 typedef struct ShellText ShellText;
1894 struct ShellText {
1895   char *z;
1896   int n;
1897   int nAlloc;
1898 };
1899 
1900 /*
1901 ** Initialize and destroy a ShellText object
1902 */
initText(ShellText * p)1903 static void initText(ShellText *p){
1904   memset(p, 0, sizeof(*p));
1905 }
freeText(ShellText * p)1906 static void freeText(ShellText *p){
1907   free(p->z);
1908   initText(p);
1909 }
1910 
1911 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1912 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1913 ** added to zIn, and the result returned in memory obtained from malloc().
1914 ** zIn, if it was not NULL, is freed.
1915 **
1916 ** If the third argument, quote, is not '\0', then it is used as a
1917 ** quote character for zAppend.
1918 */
appendText(ShellText * p,const char * zAppend,char quote)1919 static void appendText(ShellText *p, const char *zAppend, char quote){
1920   i64 len;
1921   i64 i;
1922   i64 nAppend = strlen30(zAppend);
1923 
1924   len = nAppend+p->n+1;
1925   if( quote ){
1926     len += 2;
1927     for(i=0; i<nAppend; i++){
1928       if( zAppend[i]==quote ) len++;
1929     }
1930   }
1931 
1932   if( p->z==0 || p->n+len>=p->nAlloc ){
1933     p->nAlloc = p->nAlloc*2 + len + 20;
1934     p->z = realloc(p->z, p->nAlloc);
1935     shell_check_oom(p->z);
1936   }
1937 
1938   if( quote ){
1939     char *zCsr = p->z+p->n;
1940     *zCsr++ = quote;
1941     for(i=0; i<nAppend; i++){
1942       *zCsr++ = zAppend[i];
1943       if( zAppend[i]==quote ) *zCsr++ = quote;
1944     }
1945     *zCsr++ = quote;
1946     p->n = (int)(zCsr - p->z);
1947     *zCsr = '\0';
1948   }else{
1949     memcpy(p->z+p->n, zAppend, nAppend);
1950     p->n += nAppend;
1951     p->z[p->n] = '\0';
1952   }
1953 }
1954 
1955 /*
1956 ** Attempt to determine if identifier zName needs to be quoted, either
1957 ** because it contains non-alphanumeric characters, or because it is an
1958 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
1959 ** that quoting is required.
1960 **
1961 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
1962 */
quoteChar(const char * zName)1963 static char quoteChar(const char *zName){
1964   int i;
1965   if( zName==0 ) return '"';
1966   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
1967   for(i=0; zName[i]; i++){
1968     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
1969   }
1970   return sqlite3_keyword_check(zName, i) ? '"' : 0;
1971 }
1972 
1973 /*
1974 ** Construct a fake object name and column list to describe the structure
1975 ** of the view, virtual table, or table valued function zSchema.zName.
1976 */
shellFakeSchema(sqlite3 * db,const char * zSchema,const char * zName)1977 static char *shellFakeSchema(
1978   sqlite3 *db,            /* The database connection containing the vtab */
1979   const char *zSchema,    /* Schema of the database holding the vtab */
1980   const char *zName       /* The name of the virtual table */
1981 ){
1982   sqlite3_stmt *pStmt = 0;
1983   char *zSql;
1984   ShellText s;
1985   char cQuote;
1986   char *zDiv = "(";
1987   int nRow = 0;
1988 
1989   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1990                          zSchema ? zSchema : "main", zName);
1991   shell_check_oom(zSql);
1992   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1993   sqlite3_free(zSql);
1994   initText(&s);
1995   if( zSchema ){
1996     cQuote = quoteChar(zSchema);
1997     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
1998     appendText(&s, zSchema, cQuote);
1999     appendText(&s, ".", 0);
2000   }
2001   cQuote = quoteChar(zName);
2002   appendText(&s, zName, cQuote);
2003   while( sqlite3_step(pStmt)==SQLITE_ROW ){
2004     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
2005     nRow++;
2006     appendText(&s, zDiv, 0);
2007     zDiv = ",";
2008     if( zCol==0 ) zCol = "";
2009     cQuote = quoteChar(zCol);
2010     appendText(&s, zCol, cQuote);
2011   }
2012   appendText(&s, ")", 0);
2013   sqlite3_finalize(pStmt);
2014   if( nRow==0 ){
2015     freeText(&s);
2016     s.z = 0;
2017   }
2018   return s.z;
2019 }
2020 
2021 /*
2022 ** SQL function:  strtod(X)
2023 **
2024 ** Use the C-library strtod() function to convert string X into a double.
2025 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
2026 ** routines against the C-library.
2027 */
shellStrtod(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)2028 static void shellStrtod(
2029   sqlite3_context *pCtx,
2030   int nVal,
2031   sqlite3_value **apVal
2032 ){
2033   char *z = (char*)sqlite3_value_text(apVal[0]);
2034   UNUSED_PARAMETER(nVal);
2035   if( z==0 ) return;
2036   sqlite3_result_double(pCtx, strtod(z,0));
2037 }
2038 
2039 /*
2040 ** SQL function:  dtostr(X)
2041 **
2042 ** Use the C-library printf() function to convert real value X into a string.
2043 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
2044 ** routines against the C-library.
2045 */
shellDtostr(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)2046 static void shellDtostr(
2047   sqlite3_context *pCtx,
2048   int nVal,
2049   sqlite3_value **apVal
2050 ){
2051   double r = sqlite3_value_double(apVal[0]);
2052   int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
2053   char z[400];
2054   if( n<1 ) n = 1;
2055   if( n>350 ) n = 350;
2056   sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
2057   sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
2058 }
2059 
2060 
2061 /*
2062 ** SQL function:  shell_module_schema(X)
2063 **
2064 ** Return a fake schema for the table-valued function or eponymous virtual
2065 ** table X.
2066 */
shellModuleSchema(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)2067 static void shellModuleSchema(
2068   sqlite3_context *pCtx,
2069   int nVal,
2070   sqlite3_value **apVal
2071 ){
2072   const char *zName;
2073   char *zFake;
2074   UNUSED_PARAMETER(nVal);
2075   zName = (const char*)sqlite3_value_text(apVal[0]);
2076   zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
2077   if( zFake ){
2078     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
2079                         -1, sqlite3_free);
2080     free(zFake);
2081   }
2082 }
2083 
2084 /*
2085 ** SQL function:  shell_add_schema(S,X)
2086 **
2087 ** Add the schema name X to the CREATE statement in S and return the result.
2088 ** Examples:
2089 **
2090 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
2091 **
2092 ** Also works on
2093 **
2094 **    CREATE INDEX
2095 **    CREATE UNIQUE INDEX
2096 **    CREATE VIEW
2097 **    CREATE TRIGGER
2098 **    CREATE VIRTUAL TABLE
2099 **
2100 ** This UDF is used by the .schema command to insert the schema name of
2101 ** attached databases into the middle of the sqlite_schema.sql field.
2102 */
shellAddSchemaName(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)2103 static void shellAddSchemaName(
2104   sqlite3_context *pCtx,
2105   int nVal,
2106   sqlite3_value **apVal
2107 ){
2108   static const char *aPrefix[] = {
2109      "TABLE",
2110      "INDEX",
2111      "UNIQUE INDEX",
2112      "VIEW",
2113      "TRIGGER",
2114      "VIRTUAL TABLE"
2115   };
2116   int i = 0;
2117   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
2118   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
2119   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
2120   sqlite3 *db = sqlite3_context_db_handle(pCtx);
2121   UNUSED_PARAMETER(nVal);
2122   if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
2123     for(i=0; i<ArraySize(aPrefix); i++){
2124       int n = strlen30(aPrefix[i]);
2125       if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
2126         char *z = 0;
2127         char *zFake = 0;
2128         if( zSchema ){
2129           char cQuote = quoteChar(zSchema);
2130           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
2131             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
2132           }else{
2133             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
2134           }
2135         }
2136         if( zName
2137          && aPrefix[i][0]=='V'
2138          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
2139         ){
2140           if( z==0 ){
2141             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
2142           }else{
2143             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
2144           }
2145           free(zFake);
2146         }
2147         if( z ){
2148           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
2149           return;
2150         }
2151       }
2152     }
2153   }
2154   sqlite3_result_value(pCtx, apVal[0]);
2155 }
2156 
2157 /*
2158 ** The source code for several run-time loadable extensions is inserted
2159 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
2160 ** code, we need to override some macros to make the included program code
2161 ** work here in the middle of this regular program.
2162 */
2163 #define SQLITE_EXTENSION_INIT1
2164 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
2165 
2166 #if defined(_WIN32) && defined(_MSC_VER)
2167 /************************* Begin test_windirent.h ******************/
2168 /*
2169 ** 2015 November 30
2170 **
2171 ** The author disclaims copyright to this source code.  In place of
2172 ** a legal notice, here is a blessing:
2173 **
2174 **    May you do good and not evil.
2175 **    May you find forgiveness for yourself and forgive others.
2176 **    May you share freely, never taking more than you give.
2177 **
2178 *************************************************************************
2179 ** This file contains declarations for most of the opendir() family of
2180 ** POSIX functions on Win32 using the MSVCRT.
2181 */
2182 
2183 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
2184 #define SQLITE_WINDIRENT_H
2185 
2186 /*
2187 ** We need several data types from the Windows SDK header.
2188 */
2189 
2190 #ifndef WIN32_LEAN_AND_MEAN
2191 #define WIN32_LEAN_AND_MEAN
2192 #endif
2193 
2194 #include "windows.h"
2195 
2196 /*
2197 ** We need several support functions from the SQLite core.
2198 */
2199 
2200 /* #include "sqlite3.h" */
2201 
2202 /*
2203 ** We need several things from the ANSI and MSVCRT headers.
2204 */
2205 
2206 #include <stdio.h>
2207 #include <stdlib.h>
2208 #include <errno.h>
2209 #include <io.h>
2210 #include <limits.h>
2211 #include <sys/types.h>
2212 #include <sys/stat.h>
2213 
2214 /*
2215 ** We may need several defines that should have been in "sys/stat.h".
2216 */
2217 
2218 #ifndef S_ISREG
2219 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
2220 #endif
2221 
2222 #ifndef S_ISDIR
2223 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
2224 #endif
2225 
2226 #ifndef S_ISLNK
2227 #define S_ISLNK(mode) (0)
2228 #endif
2229 
2230 /*
2231 ** We may need to provide the "mode_t" type.
2232 */
2233 
2234 #ifndef MODE_T_DEFINED
2235   #define MODE_T_DEFINED
2236   typedef unsigned short mode_t;
2237 #endif
2238 
2239 /*
2240 ** We may need to provide the "ino_t" type.
2241 */
2242 
2243 #ifndef INO_T_DEFINED
2244   #define INO_T_DEFINED
2245   typedef unsigned short ino_t;
2246 #endif
2247 
2248 /*
2249 ** We need to define "NAME_MAX" if it was not present in "limits.h".
2250 */
2251 
2252 #ifndef NAME_MAX
2253 #  ifdef FILENAME_MAX
2254 #    define NAME_MAX (FILENAME_MAX)
2255 #  else
2256 #    define NAME_MAX (260)
2257 #  endif
2258 #endif
2259 
2260 /*
2261 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
2262 */
2263 
2264 #ifndef NULL_INTPTR_T
2265 #  define NULL_INTPTR_T ((intptr_t)(0))
2266 #endif
2267 
2268 #ifndef BAD_INTPTR_T
2269 #  define BAD_INTPTR_T ((intptr_t)(-1))
2270 #endif
2271 
2272 /*
2273 ** We need to provide the necessary structures and related types.
2274 */
2275 
2276 #ifndef DIRENT_DEFINED
2277 #define DIRENT_DEFINED
2278 typedef struct DIRENT DIRENT;
2279 typedef DIRENT *LPDIRENT;
2280 struct DIRENT {
2281   ino_t d_ino;               /* Sequence number, do not use. */
2282   unsigned d_attributes;     /* Win32 file attributes. */
2283   char d_name[NAME_MAX + 1]; /* Name within the directory. */
2284 };
2285 #endif
2286 
2287 #ifndef DIR_DEFINED
2288 #define DIR_DEFINED
2289 typedef struct DIR DIR;
2290 typedef DIR *LPDIR;
2291 struct DIR {
2292   intptr_t d_handle; /* Value returned by "_findfirst". */
2293   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
2294   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
2295 };
2296 #endif
2297 
2298 /*
2299 ** Provide a macro, for use by the implementation, to determine if a
2300 ** particular directory entry should be skipped over when searching for
2301 ** the next directory entry that should be returned by the readdir() or
2302 ** readdir_r() functions.
2303 */
2304 
2305 #ifndef is_filtered
2306 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
2307 #endif
2308 
2309 /*
2310 ** Provide the function prototype for the POSIX compatible getenv()
2311 ** function.  This function is not thread-safe.
2312 */
2313 
2314 extern const char *windirent_getenv(const char *name);
2315 
2316 /*
2317 ** Finally, we can provide the function prototypes for the opendir(),
2318 ** readdir(), readdir_r(), and closedir() POSIX functions.
2319 */
2320 
2321 extern LPDIR opendir(const char *dirname);
2322 extern LPDIRENT readdir(LPDIR dirp);
2323 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
2324 extern INT closedir(LPDIR dirp);
2325 
2326 #endif /* defined(WIN32) && defined(_MSC_VER) */
2327 
2328 /************************* End test_windirent.h ********************/
2329 /************************* Begin test_windirent.c ******************/
2330 /*
2331 ** 2015 November 30
2332 **
2333 ** The author disclaims copyright to this source code.  In place of
2334 ** a legal notice, here is a blessing:
2335 **
2336 **    May you do good and not evil.
2337 **    May you find forgiveness for yourself and forgive others.
2338 **    May you share freely, never taking more than you give.
2339 **
2340 *************************************************************************
2341 ** This file contains code to implement most of the opendir() family of
2342 ** POSIX functions on Win32 using the MSVCRT.
2343 */
2344 
2345 #if defined(_WIN32) && defined(_MSC_VER)
2346 /* #include "test_windirent.h" */
2347 
2348 /*
2349 ** Implementation of the POSIX getenv() function using the Win32 API.
2350 ** This function is not thread-safe.
2351 */
windirent_getenv(const char * name)2352 const char *windirent_getenv(
2353   const char *name
2354 ){
2355   static char value[32768]; /* Maximum length, per MSDN */
2356   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
2357   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
2358 
2359   memset(value, 0, sizeof(value));
2360   dwRet = GetEnvironmentVariableA(name, value, dwSize);
2361   if( dwRet==0 || dwRet>dwSize ){
2362     /*
2363     ** The function call to GetEnvironmentVariableA() failed -OR-
2364     ** the buffer is not large enough.  Either way, return NULL.
2365     */
2366     return 0;
2367   }else{
2368     /*
2369     ** The function call to GetEnvironmentVariableA() succeeded
2370     ** -AND- the buffer contains the entire value.
2371     */
2372     return value;
2373   }
2374 }
2375 
2376 /*
2377 ** Implementation of the POSIX opendir() function using the MSVCRT.
2378 */
opendir(const char * dirname)2379 LPDIR opendir(
2380   const char *dirname
2381 ){
2382   struct _finddata_t data;
2383   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
2384   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
2385 
2386   if( dirp==NULL ) return NULL;
2387   memset(dirp, 0, sizeof(DIR));
2388 
2389   /* TODO: Remove this if Unix-style root paths are not used. */
2390   if( sqlite3_stricmp(dirname, "/")==0 ){
2391     dirname = windirent_getenv("SystemDrive");
2392   }
2393 
2394   memset(&data, 0, sizeof(struct _finddata_t));
2395   _snprintf(data.name, namesize, "%s\\*", dirname);
2396   dirp->d_handle = _findfirst(data.name, &data);
2397 
2398   if( dirp->d_handle==BAD_INTPTR_T ){
2399     closedir(dirp);
2400     return NULL;
2401   }
2402 
2403   /* TODO: Remove this block to allow hidden and/or system files. */
2404   if( is_filtered(data) ){
2405 next:
2406 
2407     memset(&data, 0, sizeof(struct _finddata_t));
2408     if( _findnext(dirp->d_handle, &data)==-1 ){
2409       closedir(dirp);
2410       return NULL;
2411     }
2412 
2413     /* TODO: Remove this block to allow hidden and/or system files. */
2414     if( is_filtered(data) ) goto next;
2415   }
2416 
2417   dirp->d_first.d_attributes = data.attrib;
2418   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
2419   dirp->d_first.d_name[NAME_MAX] = '\0';
2420 
2421   return dirp;
2422 }
2423 
2424 /*
2425 ** Implementation of the POSIX readdir() function using the MSVCRT.
2426 */
readdir(LPDIR dirp)2427 LPDIRENT readdir(
2428   LPDIR dirp
2429 ){
2430   struct _finddata_t data;
2431 
2432   if( dirp==NULL ) return NULL;
2433 
2434   if( dirp->d_first.d_ino==0 ){
2435     dirp->d_first.d_ino++;
2436     dirp->d_next.d_ino++;
2437 
2438     return &dirp->d_first;
2439   }
2440 
2441 next:
2442 
2443   memset(&data, 0, sizeof(struct _finddata_t));
2444   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
2445 
2446   /* TODO: Remove this block to allow hidden and/or system files. */
2447   if( is_filtered(data) ) goto next;
2448 
2449   dirp->d_next.d_ino++;
2450   dirp->d_next.d_attributes = data.attrib;
2451   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
2452   dirp->d_next.d_name[NAME_MAX] = '\0';
2453 
2454   return &dirp->d_next;
2455 }
2456 
2457 /*
2458 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
2459 */
readdir_r(LPDIR dirp,LPDIRENT entry,LPDIRENT * result)2460 INT readdir_r(
2461   LPDIR dirp,
2462   LPDIRENT entry,
2463   LPDIRENT *result
2464 ){
2465   struct _finddata_t data;
2466 
2467   if( dirp==NULL ) return EBADF;
2468 
2469   if( dirp->d_first.d_ino==0 ){
2470     dirp->d_first.d_ino++;
2471     dirp->d_next.d_ino++;
2472 
2473     entry->d_ino = dirp->d_first.d_ino;
2474     entry->d_attributes = dirp->d_first.d_attributes;
2475     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
2476     entry->d_name[NAME_MAX] = '\0';
2477 
2478     *result = entry;
2479     return 0;
2480   }
2481 
2482 next:
2483 
2484   memset(&data, 0, sizeof(struct _finddata_t));
2485   if( _findnext(dirp->d_handle, &data)==-1 ){
2486     *result = NULL;
2487     return ENOENT;
2488   }
2489 
2490   /* TODO: Remove this block to allow hidden and/or system files. */
2491   if( is_filtered(data) ) goto next;
2492 
2493   entry->d_ino = (ino_t)-1; /* not available */
2494   entry->d_attributes = data.attrib;
2495   strncpy(entry->d_name, data.name, NAME_MAX);
2496   entry->d_name[NAME_MAX] = '\0';
2497 
2498   *result = entry;
2499   return 0;
2500 }
2501 
2502 /*
2503 ** Implementation of the POSIX closedir() function using the MSVCRT.
2504 */
closedir(LPDIR dirp)2505 INT closedir(
2506   LPDIR dirp
2507 ){
2508   INT result = 0;
2509 
2510   if( dirp==NULL ) return EINVAL;
2511 
2512   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
2513     result = _findclose(dirp->d_handle);
2514   }
2515 
2516   sqlite3_free(dirp);
2517   return result;
2518 }
2519 
2520 #endif /* defined(WIN32) && defined(_MSC_VER) */
2521 
2522 /************************* End test_windirent.c ********************/
2523 #define dirent DIRENT
2524 #endif
2525 /************************* Begin ../ext/misc/memtrace.c ******************/
2526 /*
2527 ** 2019-01-21
2528 **
2529 ** The author disclaims copyright to this source code.  In place of
2530 ** a legal notice, here is a blessing:
2531 **
2532 **    May you do good and not evil.
2533 **    May you find forgiveness for yourself and forgive others.
2534 **    May you share freely, never taking more than you give.
2535 **
2536 *************************************************************************
2537 **
2538 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
2539 ** mechanism to add a tracing layer on top of SQLite.  If this extension
2540 ** is registered prior to sqlite3_initialize(), it will cause all memory
2541 ** allocation activities to be logged on standard output, or to some other
2542 ** FILE specified by the initializer.
2543 **
2544 ** This file needs to be compiled into the application that uses it.
2545 **
2546 ** This extension is used to implement the --memtrace option of the
2547 ** command-line shell.
2548 */
2549 #include <assert.h>
2550 #include <string.h>
2551 #include <stdio.h>
2552 
2553 /* The original memory allocation routines */
2554 static sqlite3_mem_methods memtraceBase;
2555 static FILE *memtraceOut;
2556 
2557 /* Methods that trace memory allocations */
memtraceMalloc(int n)2558 static void *memtraceMalloc(int n){
2559   if( memtraceOut ){
2560     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
2561             memtraceBase.xRoundup(n));
2562   }
2563   return memtraceBase.xMalloc(n);
2564 }
memtraceFree(void * p)2565 static void memtraceFree(void *p){
2566   if( p==0 ) return;
2567   if( memtraceOut ){
2568     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
2569   }
2570   memtraceBase.xFree(p);
2571 }
memtraceRealloc(void * p,int n)2572 static void *memtraceRealloc(void *p, int n){
2573   if( p==0 ) return memtraceMalloc(n);
2574   if( n==0 ){
2575     memtraceFree(p);
2576     return 0;
2577   }
2578   if( memtraceOut ){
2579     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
2580             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
2581   }
2582   return memtraceBase.xRealloc(p, n);
2583 }
memtraceSize(void * p)2584 static int memtraceSize(void *p){
2585   return memtraceBase.xSize(p);
2586 }
memtraceRoundup(int n)2587 static int memtraceRoundup(int n){
2588   return memtraceBase.xRoundup(n);
2589 }
memtraceInit(void * p)2590 static int memtraceInit(void *p){
2591   return memtraceBase.xInit(p);
2592 }
memtraceShutdown(void * p)2593 static void memtraceShutdown(void *p){
2594   memtraceBase.xShutdown(p);
2595 }
2596 
2597 /* The substitute memory allocator */
2598 static sqlite3_mem_methods ersaztMethods = {
2599   memtraceMalloc,
2600   memtraceFree,
2601   memtraceRealloc,
2602   memtraceSize,
2603   memtraceRoundup,
2604   memtraceInit,
2605   memtraceShutdown,
2606   0
2607 };
2608 
2609 /* Begin tracing memory allocations to out. */
sqlite3MemTraceActivate(FILE * out)2610 int sqlite3MemTraceActivate(FILE *out){
2611   int rc = SQLITE_OK;
2612   if( memtraceBase.xMalloc==0 ){
2613     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
2614     if( rc==SQLITE_OK ){
2615       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
2616     }
2617   }
2618   memtraceOut = out;
2619   return rc;
2620 }
2621 
2622 /* Deactivate memory tracing */
sqlite3MemTraceDeactivate(void)2623 int sqlite3MemTraceDeactivate(void){
2624   int rc = SQLITE_OK;
2625   if( memtraceBase.xMalloc!=0 ){
2626     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
2627     if( rc==SQLITE_OK ){
2628       memset(&memtraceBase, 0, sizeof(memtraceBase));
2629     }
2630   }
2631   memtraceOut = 0;
2632   return rc;
2633 }
2634 
2635 /************************* End ../ext/misc/memtrace.c ********************/
2636 /************************* Begin ../ext/misc/pcachetrace.c ******************/
2637 /*
2638 ** 2023-06-21
2639 **
2640 ** The author disclaims copyright to this source code.  In place of
2641 ** a legal notice, here is a blessing:
2642 **
2643 **    May you do good and not evil.
2644 **    May you find forgiveness for yourself and forgive others.
2645 **    May you share freely, never taking more than you give.
2646 **
2647 *************************************************************************
2648 **
2649 ** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2
2650 ** mechanism to add a tracing layer on top of pluggable page cache of
2651 ** SQLite.  If this extension is registered prior to sqlite3_initialize(),
2652 ** it will cause all page cache activities to be logged on standard output,
2653 ** or to some other FILE specified by the initializer.
2654 **
2655 ** This file needs to be compiled into the application that uses it.
2656 **
2657 ** This extension is used to implement the --pcachetrace option of the
2658 ** command-line shell.
2659 */
2660 #include <assert.h>
2661 #include <string.h>
2662 #include <stdio.h>
2663 
2664 /* The original page cache routines */
2665 static sqlite3_pcache_methods2 pcacheBase;
2666 static FILE *pcachetraceOut;
2667 
2668 /* Methods that trace pcache activity */
pcachetraceInit(void * pArg)2669 static int pcachetraceInit(void *pArg){
2670   int nRes;
2671   if( pcachetraceOut ){
2672     fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg);
2673   }
2674   nRes = pcacheBase.xInit(pArg);
2675   if( pcachetraceOut ){
2676     fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes);
2677   }
2678   return nRes;
2679 }
pcachetraceShutdown(void * pArg)2680 static void pcachetraceShutdown(void *pArg){
2681   if( pcachetraceOut ){
2682     fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg);
2683   }
2684   pcacheBase.xShutdown(pArg);
2685 }
pcachetraceCreate(int szPage,int szExtra,int bPurge)2686 static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){
2687   sqlite3_pcache *pRes;
2688   if( pcachetraceOut ){
2689     fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n",
2690             szPage, szExtra, bPurge);
2691   }
2692   pRes = pcacheBase.xCreate(szPage, szExtra, bPurge);
2693   if( pcachetraceOut ){
2694     fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n",
2695             szPage, szExtra, bPurge, pRes);
2696   }
2697   return pRes;
2698 }
pcachetraceCachesize(sqlite3_pcache * p,int nCachesize)2699 static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){
2700   if( pcachetraceOut ){
2701     fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize);
2702   }
2703   pcacheBase.xCachesize(p, nCachesize);
2704 }
pcachetracePagecount(sqlite3_pcache * p)2705 static int pcachetracePagecount(sqlite3_pcache *p){
2706   int nRes;
2707   if( pcachetraceOut ){
2708     fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p);
2709   }
2710   nRes = pcacheBase.xPagecount(p);
2711   if( pcachetraceOut ){
2712     fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes);
2713   }
2714   return nRes;
2715 }
pcachetraceFetch(sqlite3_pcache * p,unsigned key,int crFg)2716 static sqlite3_pcache_page *pcachetraceFetch(
2717   sqlite3_pcache *p,
2718   unsigned key,
2719   int crFg
2720 ){
2721   sqlite3_pcache_page *pRes;
2722   if( pcachetraceOut ){
2723     fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg);
2724   }
2725   pRes = pcacheBase.xFetch(p, key, crFg);
2726   if( pcachetraceOut ){
2727     fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n",
2728             p, key, crFg, pRes);
2729   }
2730   return pRes;
2731 }
pcachetraceUnpin(sqlite3_pcache * p,sqlite3_pcache_page * pPg,int bDiscard)2732 static void pcachetraceUnpin(
2733   sqlite3_pcache *p,
2734   sqlite3_pcache_page *pPg,
2735   int bDiscard
2736 ){
2737   if( pcachetraceOut ){
2738     fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n",
2739             p, pPg, bDiscard);
2740   }
2741   pcacheBase.xUnpin(p, pPg, bDiscard);
2742 }
pcachetraceRekey(sqlite3_pcache * p,sqlite3_pcache_page * pPg,unsigned oldKey,unsigned newKey)2743 static void pcachetraceRekey(
2744   sqlite3_pcache *p,
2745   sqlite3_pcache_page *pPg,
2746   unsigned oldKey,
2747   unsigned newKey
2748 ){
2749   if( pcachetraceOut ){
2750     fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n",
2751         p, pPg, oldKey, newKey);
2752   }
2753   pcacheBase.xRekey(p, pPg, oldKey, newKey);
2754 }
pcachetraceTruncate(sqlite3_pcache * p,unsigned n)2755 static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){
2756   if( pcachetraceOut ){
2757     fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n);
2758   }
2759   pcacheBase.xTruncate(p, n);
2760 }
pcachetraceDestroy(sqlite3_pcache * p)2761 static void pcachetraceDestroy(sqlite3_pcache *p){
2762   if( pcachetraceOut ){
2763     fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p);
2764   }
2765   pcacheBase.xDestroy(p);
2766 }
pcachetraceShrink(sqlite3_pcache * p)2767 static void pcachetraceShrink(sqlite3_pcache *p){
2768   if( pcachetraceOut ){
2769     fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p);
2770   }
2771   pcacheBase.xShrink(p);
2772 }
2773 
2774 /* The substitute pcache methods */
2775 static sqlite3_pcache_methods2 ersaztPcacheMethods = {
2776   0,
2777   0,
2778   pcachetraceInit,
2779   pcachetraceShutdown,
2780   pcachetraceCreate,
2781   pcachetraceCachesize,
2782   pcachetracePagecount,
2783   pcachetraceFetch,
2784   pcachetraceUnpin,
2785   pcachetraceRekey,
2786   pcachetraceTruncate,
2787   pcachetraceDestroy,
2788   pcachetraceShrink
2789 };
2790 
2791 /* Begin tracing memory allocations to out. */
sqlite3PcacheTraceActivate(FILE * out)2792 int sqlite3PcacheTraceActivate(FILE *out){
2793   int rc = SQLITE_OK;
2794   if( pcacheBase.xFetch==0 ){
2795     rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase);
2796     if( rc==SQLITE_OK ){
2797       rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods);
2798     }
2799   }
2800   pcachetraceOut = out;
2801   return rc;
2802 }
2803 
2804 /* Deactivate memory tracing */
sqlite3PcacheTraceDeactivate(void)2805 int sqlite3PcacheTraceDeactivate(void){
2806   int rc = SQLITE_OK;
2807   if( pcacheBase.xFetch!=0 ){
2808     rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase);
2809     if( rc==SQLITE_OK ){
2810       memset(&pcacheBase, 0, sizeof(pcacheBase));
2811     }
2812   }
2813   pcachetraceOut = 0;
2814   return rc;
2815 }
2816 
2817 /************************* End ../ext/misc/pcachetrace.c ********************/
2818 /************************* Begin ../ext/misc/shathree.c ******************/
2819 /*
2820 ** 2017-03-08
2821 **
2822 ** The author disclaims copyright to this source code.  In place of
2823 ** a legal notice, here is a blessing:
2824 **
2825 **    May you do good and not evil.
2826 **    May you find forgiveness for yourself and forgive others.
2827 **    May you share freely, never taking more than you give.
2828 **
2829 ******************************************************************************
2830 **
2831 ** This SQLite extension implements functions that compute SHA3 hashes
2832 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2833 ** Two SQL functions are implemented:
2834 **
2835 **     sha3(X,SIZE)
2836 **     sha3_query(Y,SIZE)
2837 **
2838 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
2839 ** X is NULL.
2840 **
2841 ** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
2842 ** and returns a hash of their results.
2843 **
2844 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
2845 ** is used.  If SIZE is included it must be one of the integers 224, 256,
2846 ** 384, or 512, to determine SHA3 hash variant that is computed.
2847 */
2848 /* #include "sqlite3ext.h" */
2849 SQLITE_EXTENSION_INIT1
2850 #include <assert.h>
2851 #include <string.h>
2852 #include <stdarg.h>
2853 
2854 #ifndef SQLITE_AMALGAMATION
2855 /* typedef sqlite3_uint64 u64; */
2856 #endif /* SQLITE_AMALGAMATION */
2857 
2858 /******************************************************************************
2859 ** The Hash Engine
2860 */
2861 /*
2862 ** Macros to determine whether the machine is big or little endian,
2863 ** and whether or not that determination is run-time or compile-time.
2864 **
2865 ** For best performance, an attempt is made to guess at the byte-order
2866 ** using C-preprocessor macros.  If that is unsuccessful, or if
2867 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
2868 ** at run-time.
2869 */
2870 #ifndef SHA3_BYTEORDER
2871 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
2872      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
2873      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
2874      defined(__arm__)
2875 #   define SHA3_BYTEORDER    1234
2876 # elif defined(sparc)    || defined(__ppc__)
2877 #   define SHA3_BYTEORDER    4321
2878 # else
2879 #   define SHA3_BYTEORDER 0
2880 # endif
2881 #endif
2882 
2883 
2884 /*
2885 ** State structure for a SHA3 hash in progress
2886 */
2887 typedef struct SHA3Context SHA3Context;
2888 struct SHA3Context {
2889   union {
2890     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
2891     unsigned char x[1600];    /* ... or 1600 bytes */
2892   } u;
2893   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
2894   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
2895   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
2896 };
2897 
2898 /*
2899 ** A single step of the Keccak mixing function for a 1600-bit state
2900 */
KeccakF1600Step(SHA3Context * p)2901 static void KeccakF1600Step(SHA3Context *p){
2902   int i;
2903   u64 b0, b1, b2, b3, b4;
2904   u64 c0, c1, c2, c3, c4;
2905   u64 d0, d1, d2, d3, d4;
2906   static const u64 RC[] = {
2907     0x0000000000000001ULL,  0x0000000000008082ULL,
2908     0x800000000000808aULL,  0x8000000080008000ULL,
2909     0x000000000000808bULL,  0x0000000080000001ULL,
2910     0x8000000080008081ULL,  0x8000000000008009ULL,
2911     0x000000000000008aULL,  0x0000000000000088ULL,
2912     0x0000000080008009ULL,  0x000000008000000aULL,
2913     0x000000008000808bULL,  0x800000000000008bULL,
2914     0x8000000000008089ULL,  0x8000000000008003ULL,
2915     0x8000000000008002ULL,  0x8000000000000080ULL,
2916     0x000000000000800aULL,  0x800000008000000aULL,
2917     0x8000000080008081ULL,  0x8000000000008080ULL,
2918     0x0000000080000001ULL,  0x8000000080008008ULL
2919   };
2920 # define a00 (p->u.s[0])
2921 # define a01 (p->u.s[1])
2922 # define a02 (p->u.s[2])
2923 # define a03 (p->u.s[3])
2924 # define a04 (p->u.s[4])
2925 # define a10 (p->u.s[5])
2926 # define a11 (p->u.s[6])
2927 # define a12 (p->u.s[7])
2928 # define a13 (p->u.s[8])
2929 # define a14 (p->u.s[9])
2930 # define a20 (p->u.s[10])
2931 # define a21 (p->u.s[11])
2932 # define a22 (p->u.s[12])
2933 # define a23 (p->u.s[13])
2934 # define a24 (p->u.s[14])
2935 # define a30 (p->u.s[15])
2936 # define a31 (p->u.s[16])
2937 # define a32 (p->u.s[17])
2938 # define a33 (p->u.s[18])
2939 # define a34 (p->u.s[19])
2940 # define a40 (p->u.s[20])
2941 # define a41 (p->u.s[21])
2942 # define a42 (p->u.s[22])
2943 # define a43 (p->u.s[23])
2944 # define a44 (p->u.s[24])
2945 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
2946 
2947   for(i=0; i<24; i+=4){
2948     c0 = a00^a10^a20^a30^a40;
2949     c1 = a01^a11^a21^a31^a41;
2950     c2 = a02^a12^a22^a32^a42;
2951     c3 = a03^a13^a23^a33^a43;
2952     c4 = a04^a14^a24^a34^a44;
2953     d0 = c4^ROL64(c1, 1);
2954     d1 = c0^ROL64(c2, 1);
2955     d2 = c1^ROL64(c3, 1);
2956     d3 = c2^ROL64(c4, 1);
2957     d4 = c3^ROL64(c0, 1);
2958 
2959     b0 = (a00^d0);
2960     b1 = ROL64((a11^d1), 44);
2961     b2 = ROL64((a22^d2), 43);
2962     b3 = ROL64((a33^d3), 21);
2963     b4 = ROL64((a44^d4), 14);
2964     a00 =   b0 ^((~b1)&  b2 );
2965     a00 ^= RC[i];
2966     a11 =   b1 ^((~b2)&  b3 );
2967     a22 =   b2 ^((~b3)&  b4 );
2968     a33 =   b3 ^((~b4)&  b0 );
2969     a44 =   b4 ^((~b0)&  b1 );
2970 
2971     b2 = ROL64((a20^d0), 3);
2972     b3 = ROL64((a31^d1), 45);
2973     b4 = ROL64((a42^d2), 61);
2974     b0 = ROL64((a03^d3), 28);
2975     b1 = ROL64((a14^d4), 20);
2976     a20 =   b0 ^((~b1)&  b2 );
2977     a31 =   b1 ^((~b2)&  b3 );
2978     a42 =   b2 ^((~b3)&  b4 );
2979     a03 =   b3 ^((~b4)&  b0 );
2980     a14 =   b4 ^((~b0)&  b1 );
2981 
2982     b4 = ROL64((a40^d0), 18);
2983     b0 = ROL64((a01^d1), 1);
2984     b1 = ROL64((a12^d2), 6);
2985     b2 = ROL64((a23^d3), 25);
2986     b3 = ROL64((a34^d4), 8);
2987     a40 =   b0 ^((~b1)&  b2 );
2988     a01 =   b1 ^((~b2)&  b3 );
2989     a12 =   b2 ^((~b3)&  b4 );
2990     a23 =   b3 ^((~b4)&  b0 );
2991     a34 =   b4 ^((~b0)&  b1 );
2992 
2993     b1 = ROL64((a10^d0), 36);
2994     b2 = ROL64((a21^d1), 10);
2995     b3 = ROL64((a32^d2), 15);
2996     b4 = ROL64((a43^d3), 56);
2997     b0 = ROL64((a04^d4), 27);
2998     a10 =   b0 ^((~b1)&  b2 );
2999     a21 =   b1 ^((~b2)&  b3 );
3000     a32 =   b2 ^((~b3)&  b4 );
3001     a43 =   b3 ^((~b4)&  b0 );
3002     a04 =   b4 ^((~b0)&  b1 );
3003 
3004     b3 = ROL64((a30^d0), 41);
3005     b4 = ROL64((a41^d1), 2);
3006     b0 = ROL64((a02^d2), 62);
3007     b1 = ROL64((a13^d3), 55);
3008     b2 = ROL64((a24^d4), 39);
3009     a30 =   b0 ^((~b1)&  b2 );
3010     a41 =   b1 ^((~b2)&  b3 );
3011     a02 =   b2 ^((~b3)&  b4 );
3012     a13 =   b3 ^((~b4)&  b0 );
3013     a24 =   b4 ^((~b0)&  b1 );
3014 
3015     c0 = a00^a20^a40^a10^a30;
3016     c1 = a11^a31^a01^a21^a41;
3017     c2 = a22^a42^a12^a32^a02;
3018     c3 = a33^a03^a23^a43^a13;
3019     c4 = a44^a14^a34^a04^a24;
3020     d0 = c4^ROL64(c1, 1);
3021     d1 = c0^ROL64(c2, 1);
3022     d2 = c1^ROL64(c3, 1);
3023     d3 = c2^ROL64(c4, 1);
3024     d4 = c3^ROL64(c0, 1);
3025 
3026     b0 = (a00^d0);
3027     b1 = ROL64((a31^d1), 44);
3028     b2 = ROL64((a12^d2), 43);
3029     b3 = ROL64((a43^d3), 21);
3030     b4 = ROL64((a24^d4), 14);
3031     a00 =   b0 ^((~b1)&  b2 );
3032     a00 ^= RC[i+1];
3033     a31 =   b1 ^((~b2)&  b3 );
3034     a12 =   b2 ^((~b3)&  b4 );
3035     a43 =   b3 ^((~b4)&  b0 );
3036     a24 =   b4 ^((~b0)&  b1 );
3037 
3038     b2 = ROL64((a40^d0), 3);
3039     b3 = ROL64((a21^d1), 45);
3040     b4 = ROL64((a02^d2), 61);
3041     b0 = ROL64((a33^d3), 28);
3042     b1 = ROL64((a14^d4), 20);
3043     a40 =   b0 ^((~b1)&  b2 );
3044     a21 =   b1 ^((~b2)&  b3 );
3045     a02 =   b2 ^((~b3)&  b4 );
3046     a33 =   b3 ^((~b4)&  b0 );
3047     a14 =   b4 ^((~b0)&  b1 );
3048 
3049     b4 = ROL64((a30^d0), 18);
3050     b0 = ROL64((a11^d1), 1);
3051     b1 = ROL64((a42^d2), 6);
3052     b2 = ROL64((a23^d3), 25);
3053     b3 = ROL64((a04^d4), 8);
3054     a30 =   b0 ^((~b1)&  b2 );
3055     a11 =   b1 ^((~b2)&  b3 );
3056     a42 =   b2 ^((~b3)&  b4 );
3057     a23 =   b3 ^((~b4)&  b0 );
3058     a04 =   b4 ^((~b0)&  b1 );
3059 
3060     b1 = ROL64((a20^d0), 36);
3061     b2 = ROL64((a01^d1), 10);
3062     b3 = ROL64((a32^d2), 15);
3063     b4 = ROL64((a13^d3), 56);
3064     b0 = ROL64((a44^d4), 27);
3065     a20 =   b0 ^((~b1)&  b2 );
3066     a01 =   b1 ^((~b2)&  b3 );
3067     a32 =   b2 ^((~b3)&  b4 );
3068     a13 =   b3 ^((~b4)&  b0 );
3069     a44 =   b4 ^((~b0)&  b1 );
3070 
3071     b3 = ROL64((a10^d0), 41);
3072     b4 = ROL64((a41^d1), 2);
3073     b0 = ROL64((a22^d2), 62);
3074     b1 = ROL64((a03^d3), 55);
3075     b2 = ROL64((a34^d4), 39);
3076     a10 =   b0 ^((~b1)&  b2 );
3077     a41 =   b1 ^((~b2)&  b3 );
3078     a22 =   b2 ^((~b3)&  b4 );
3079     a03 =   b3 ^((~b4)&  b0 );
3080     a34 =   b4 ^((~b0)&  b1 );
3081 
3082     c0 = a00^a40^a30^a20^a10;
3083     c1 = a31^a21^a11^a01^a41;
3084     c2 = a12^a02^a42^a32^a22;
3085     c3 = a43^a33^a23^a13^a03;
3086     c4 = a24^a14^a04^a44^a34;
3087     d0 = c4^ROL64(c1, 1);
3088     d1 = c0^ROL64(c2, 1);
3089     d2 = c1^ROL64(c3, 1);
3090     d3 = c2^ROL64(c4, 1);
3091     d4 = c3^ROL64(c0, 1);
3092 
3093     b0 = (a00^d0);
3094     b1 = ROL64((a21^d1), 44);
3095     b2 = ROL64((a42^d2), 43);
3096     b3 = ROL64((a13^d3), 21);
3097     b4 = ROL64((a34^d4), 14);
3098     a00 =   b0 ^((~b1)&  b2 );
3099     a00 ^= RC[i+2];
3100     a21 =   b1 ^((~b2)&  b3 );
3101     a42 =   b2 ^((~b3)&  b4 );
3102     a13 =   b3 ^((~b4)&  b0 );
3103     a34 =   b4 ^((~b0)&  b1 );
3104 
3105     b2 = ROL64((a30^d0), 3);
3106     b3 = ROL64((a01^d1), 45);
3107     b4 = ROL64((a22^d2), 61);
3108     b0 = ROL64((a43^d3), 28);
3109     b1 = ROL64((a14^d4), 20);
3110     a30 =   b0 ^((~b1)&  b2 );
3111     a01 =   b1 ^((~b2)&  b3 );
3112     a22 =   b2 ^((~b3)&  b4 );
3113     a43 =   b3 ^((~b4)&  b0 );
3114     a14 =   b4 ^((~b0)&  b1 );
3115 
3116     b4 = ROL64((a10^d0), 18);
3117     b0 = ROL64((a31^d1), 1);
3118     b1 = ROL64((a02^d2), 6);
3119     b2 = ROL64((a23^d3), 25);
3120     b3 = ROL64((a44^d4), 8);
3121     a10 =   b0 ^((~b1)&  b2 );
3122     a31 =   b1 ^((~b2)&  b3 );
3123     a02 =   b2 ^((~b3)&  b4 );
3124     a23 =   b3 ^((~b4)&  b0 );
3125     a44 =   b4 ^((~b0)&  b1 );
3126 
3127     b1 = ROL64((a40^d0), 36);
3128     b2 = ROL64((a11^d1), 10);
3129     b3 = ROL64((a32^d2), 15);
3130     b4 = ROL64((a03^d3), 56);
3131     b0 = ROL64((a24^d4), 27);
3132     a40 =   b0 ^((~b1)&  b2 );
3133     a11 =   b1 ^((~b2)&  b3 );
3134     a32 =   b2 ^((~b3)&  b4 );
3135     a03 =   b3 ^((~b4)&  b0 );
3136     a24 =   b4 ^((~b0)&  b1 );
3137 
3138     b3 = ROL64((a20^d0), 41);
3139     b4 = ROL64((a41^d1), 2);
3140     b0 = ROL64((a12^d2), 62);
3141     b1 = ROL64((a33^d3), 55);
3142     b2 = ROL64((a04^d4), 39);
3143     a20 =   b0 ^((~b1)&  b2 );
3144     a41 =   b1 ^((~b2)&  b3 );
3145     a12 =   b2 ^((~b3)&  b4 );
3146     a33 =   b3 ^((~b4)&  b0 );
3147     a04 =   b4 ^((~b0)&  b1 );
3148 
3149     c0 = a00^a30^a10^a40^a20;
3150     c1 = a21^a01^a31^a11^a41;
3151     c2 = a42^a22^a02^a32^a12;
3152     c3 = a13^a43^a23^a03^a33;
3153     c4 = a34^a14^a44^a24^a04;
3154     d0 = c4^ROL64(c1, 1);
3155     d1 = c0^ROL64(c2, 1);
3156     d2 = c1^ROL64(c3, 1);
3157     d3 = c2^ROL64(c4, 1);
3158     d4 = c3^ROL64(c0, 1);
3159 
3160     b0 = (a00^d0);
3161     b1 = ROL64((a01^d1), 44);
3162     b2 = ROL64((a02^d2), 43);
3163     b3 = ROL64((a03^d3), 21);
3164     b4 = ROL64((a04^d4), 14);
3165     a00 =   b0 ^((~b1)&  b2 );
3166     a00 ^= RC[i+3];
3167     a01 =   b1 ^((~b2)&  b3 );
3168     a02 =   b2 ^((~b3)&  b4 );
3169     a03 =   b3 ^((~b4)&  b0 );
3170     a04 =   b4 ^((~b0)&  b1 );
3171 
3172     b2 = ROL64((a10^d0), 3);
3173     b3 = ROL64((a11^d1), 45);
3174     b4 = ROL64((a12^d2), 61);
3175     b0 = ROL64((a13^d3), 28);
3176     b1 = ROL64((a14^d4), 20);
3177     a10 =   b0 ^((~b1)&  b2 );
3178     a11 =   b1 ^((~b2)&  b3 );
3179     a12 =   b2 ^((~b3)&  b4 );
3180     a13 =   b3 ^((~b4)&  b0 );
3181     a14 =   b4 ^((~b0)&  b1 );
3182 
3183     b4 = ROL64((a20^d0), 18);
3184     b0 = ROL64((a21^d1), 1);
3185     b1 = ROL64((a22^d2), 6);
3186     b2 = ROL64((a23^d3), 25);
3187     b3 = ROL64((a24^d4), 8);
3188     a20 =   b0 ^((~b1)&  b2 );
3189     a21 =   b1 ^((~b2)&  b3 );
3190     a22 =   b2 ^((~b3)&  b4 );
3191     a23 =   b3 ^((~b4)&  b0 );
3192     a24 =   b4 ^((~b0)&  b1 );
3193 
3194     b1 = ROL64((a30^d0), 36);
3195     b2 = ROL64((a31^d1), 10);
3196     b3 = ROL64((a32^d2), 15);
3197     b4 = ROL64((a33^d3), 56);
3198     b0 = ROL64((a34^d4), 27);
3199     a30 =   b0 ^((~b1)&  b2 );
3200     a31 =   b1 ^((~b2)&  b3 );
3201     a32 =   b2 ^((~b3)&  b4 );
3202     a33 =   b3 ^((~b4)&  b0 );
3203     a34 =   b4 ^((~b0)&  b1 );
3204 
3205     b3 = ROL64((a40^d0), 41);
3206     b4 = ROL64((a41^d1), 2);
3207     b0 = ROL64((a42^d2), 62);
3208     b1 = ROL64((a43^d3), 55);
3209     b2 = ROL64((a44^d4), 39);
3210     a40 =   b0 ^((~b1)&  b2 );
3211     a41 =   b1 ^((~b2)&  b3 );
3212     a42 =   b2 ^((~b3)&  b4 );
3213     a43 =   b3 ^((~b4)&  b0 );
3214     a44 =   b4 ^((~b0)&  b1 );
3215   }
3216 }
3217 
3218 /*
3219 ** Initialize a new hash.  iSize determines the size of the hash
3220 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
3221 ** can be zero to use the default hash size of 256 bits.
3222 */
SHA3Init(SHA3Context * p,int iSize)3223 static void SHA3Init(SHA3Context *p, int iSize){
3224   memset(p, 0, sizeof(*p));
3225   if( iSize>=128 && iSize<=512 ){
3226     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
3227   }else{
3228     p->nRate = (1600 - 2*256)/8;
3229   }
3230 #if SHA3_BYTEORDER==1234
3231   /* Known to be little-endian at compile-time. No-op */
3232 #elif SHA3_BYTEORDER==4321
3233   p->ixMask = 7;  /* Big-endian */
3234 #else
3235   {
3236     static unsigned int one = 1;
3237     if( 1==*(unsigned char*)&one ){
3238       /* Little endian.  No byte swapping. */
3239       p->ixMask = 0;
3240     }else{
3241       /* Big endian.  Byte swap. */
3242       p->ixMask = 7;
3243     }
3244   }
3245 #endif
3246 }
3247 
3248 /*
3249 ** Make consecutive calls to the SHA3Update function to add new content
3250 ** to the hash
3251 */
SHA3Update(SHA3Context * p,const unsigned char * aData,unsigned int nData)3252 static void SHA3Update(
3253   SHA3Context *p,
3254   const unsigned char *aData,
3255   unsigned int nData
3256 ){
3257   unsigned int i = 0;
3258   if( aData==0 ) return;
3259 #if SHA3_BYTEORDER==1234
3260   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
3261     for(; i+7<nData; i+=8){
3262       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
3263       p->nLoaded += 8;
3264       if( p->nLoaded>=p->nRate ){
3265         KeccakF1600Step(p);
3266         p->nLoaded = 0;
3267       }
3268     }
3269   }
3270 #endif
3271   for(; i<nData; i++){
3272 #if SHA3_BYTEORDER==1234
3273     p->u.x[p->nLoaded] ^= aData[i];
3274 #elif SHA3_BYTEORDER==4321
3275     p->u.x[p->nLoaded^0x07] ^= aData[i];
3276 #else
3277     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
3278 #endif
3279     p->nLoaded++;
3280     if( p->nLoaded==p->nRate ){
3281       KeccakF1600Step(p);
3282       p->nLoaded = 0;
3283     }
3284   }
3285 }
3286 
3287 /*
3288 ** After all content has been added, invoke SHA3Final() to compute
3289 ** the final hash.  The function returns a pointer to the binary
3290 ** hash value.
3291 */
SHA3Final(SHA3Context * p)3292 static unsigned char *SHA3Final(SHA3Context *p){
3293   unsigned int i;
3294   if( p->nLoaded==p->nRate-1 ){
3295     const unsigned char c1 = 0x86;
3296     SHA3Update(p, &c1, 1);
3297   }else{
3298     const unsigned char c2 = 0x06;
3299     const unsigned char c3 = 0x80;
3300     SHA3Update(p, &c2, 1);
3301     p->nLoaded = p->nRate - 1;
3302     SHA3Update(p, &c3, 1);
3303   }
3304   for(i=0; i<p->nRate; i++){
3305     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
3306   }
3307   return &p->u.x[p->nRate];
3308 }
3309 /* End of the hashing logic
3310 *****************************************************************************/
3311 
3312 /*
3313 ** Implementation of the sha3(X,SIZE) function.
3314 **
3315 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
3316 ** size is 256.  If X is a BLOB, it is hashed as is.
3317 ** For all other non-NULL types of input, X is converted into a UTF-8 string
3318 ** and the string is hashed without the trailing 0x00 terminator.  The hash
3319 ** of a NULL value is NULL.
3320 */
sha3Func(sqlite3_context * context,int argc,sqlite3_value ** argv)3321 static void sha3Func(
3322   sqlite3_context *context,
3323   int argc,
3324   sqlite3_value **argv
3325 ){
3326   SHA3Context cx;
3327   int eType = sqlite3_value_type(argv[0]);
3328   int nByte = sqlite3_value_bytes(argv[0]);
3329   int iSize;
3330   if( argc==1 ){
3331     iSize = 256;
3332   }else{
3333     iSize = sqlite3_value_int(argv[1]);
3334     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
3335       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
3336                                     "384 512", -1);
3337       return;
3338     }
3339   }
3340   if( eType==SQLITE_NULL ) return;
3341   SHA3Init(&cx, iSize);
3342   if( eType==SQLITE_BLOB ){
3343     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
3344   }else{
3345     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
3346   }
3347   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
3348 }
3349 
3350 /* Compute a string using sqlite3_vsnprintf() with a maximum length
3351 ** of 50 bytes and add it to the hash.
3352 */
sha3_step_vformat(SHA3Context * p,const char * zFormat,...)3353 static void sha3_step_vformat(
3354   SHA3Context *p,                 /* Add content to this context */
3355   const char *zFormat,
3356   ...
3357 ){
3358   va_list ap;
3359   int n;
3360   char zBuf[50];
3361   va_start(ap, zFormat);
3362   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
3363   va_end(ap);
3364   n = (int)strlen(zBuf);
3365   SHA3Update(p, (unsigned char*)zBuf, n);
3366 }
3367 
3368 /*
3369 ** Implementation of the sha3_query(SQL,SIZE) function.
3370 **
3371 ** This function compiles and runs the SQL statement(s) given in the
3372 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
3373 ** size is 256.
3374 **
3375 ** The format of the byte stream that is hashed is summarized as follows:
3376 **
3377 **       S<n>:<sql>
3378 **       R
3379 **       N
3380 **       I<int>
3381 **       F<ieee-float>
3382 **       B<size>:<bytes>
3383 **       T<size>:<text>
3384 **
3385 ** <sql> is the original SQL text for each statement run and <n> is
3386 ** the size of that text.  The SQL text is UTF-8.  A single R character
3387 ** occurs before the start of each row.  N means a NULL value.
3388 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
3389 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
3390 ** B means blobs of <size> bytes.  T means text rendered as <size>
3391 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
3392 ** text integers.
3393 **
3394 ** For each SQL statement in the X input, there is one S segment.  Each
3395 ** S segment is followed by zero or more R segments, one for each row in the
3396 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
3397 ** one for each column in the result set.  Segments are concatentated directly
3398 ** with no delimiters of any kind.
3399 */
sha3QueryFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)3400 static void sha3QueryFunc(
3401   sqlite3_context *context,
3402   int argc,
3403   sqlite3_value **argv
3404 ){
3405   sqlite3 *db = sqlite3_context_db_handle(context);
3406   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
3407   sqlite3_stmt *pStmt = 0;
3408   int nCol;                   /* Number of columns in the result set */
3409   int i;                      /* Loop counter */
3410   int rc;
3411   int n;
3412   const char *z;
3413   SHA3Context cx;
3414   int iSize;
3415 
3416   if( argc==1 ){
3417     iSize = 256;
3418   }else{
3419     iSize = sqlite3_value_int(argv[1]);
3420     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
3421       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
3422                                     "384 512", -1);
3423       return;
3424     }
3425   }
3426   if( zSql==0 ) return;
3427   SHA3Init(&cx, iSize);
3428   while( zSql[0] ){
3429     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
3430     if( rc ){
3431       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
3432                                    zSql, sqlite3_errmsg(db));
3433       sqlite3_finalize(pStmt);
3434       sqlite3_result_error(context, zMsg, -1);
3435       sqlite3_free(zMsg);
3436       return;
3437     }
3438     if( !sqlite3_stmt_readonly(pStmt) ){
3439       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
3440       sqlite3_finalize(pStmt);
3441       sqlite3_result_error(context, zMsg, -1);
3442       sqlite3_free(zMsg);
3443       return;
3444     }
3445     nCol = sqlite3_column_count(pStmt);
3446     z = sqlite3_sql(pStmt);
3447     if( z ){
3448       n = (int)strlen(z);
3449       sha3_step_vformat(&cx,"S%d:",n);
3450       SHA3Update(&cx,(unsigned char*)z,n);
3451     }
3452 
3453     /* Compute a hash over the result of the query */
3454     while( SQLITE_ROW==sqlite3_step(pStmt) ){
3455       SHA3Update(&cx,(const unsigned char*)"R",1);
3456       for(i=0; i<nCol; i++){
3457         switch( sqlite3_column_type(pStmt,i) ){
3458           case SQLITE_NULL: {
3459             SHA3Update(&cx, (const unsigned char*)"N",1);
3460             break;
3461           }
3462           case SQLITE_INTEGER: {
3463             sqlite3_uint64 u;
3464             int j;
3465             unsigned char x[9];
3466             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
3467             memcpy(&u, &v, 8);
3468             for(j=8; j>=1; j--){
3469               x[j] = u & 0xff;
3470               u >>= 8;
3471             }
3472             x[0] = 'I';
3473             SHA3Update(&cx, x, 9);
3474             break;
3475           }
3476           case SQLITE_FLOAT: {
3477             sqlite3_uint64 u;
3478             int j;
3479             unsigned char x[9];
3480             double r = sqlite3_column_double(pStmt,i);
3481             memcpy(&u, &r, 8);
3482             for(j=8; j>=1; j--){
3483               x[j] = u & 0xff;
3484               u >>= 8;
3485             }
3486             x[0] = 'F';
3487             SHA3Update(&cx,x,9);
3488             break;
3489           }
3490           case SQLITE_TEXT: {
3491             int n2 = sqlite3_column_bytes(pStmt, i);
3492             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
3493             sha3_step_vformat(&cx,"T%d:",n2);
3494             SHA3Update(&cx, z2, n2);
3495             break;
3496           }
3497           case SQLITE_BLOB: {
3498             int n2 = sqlite3_column_bytes(pStmt, i);
3499             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
3500             sha3_step_vformat(&cx,"B%d:",n2);
3501             SHA3Update(&cx, z2, n2);
3502             break;
3503           }
3504         }
3505       }
3506     }
3507     sqlite3_finalize(pStmt);
3508   }
3509   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
3510 }
3511 
3512 
3513 #ifdef _WIN32
3514 
3515 #endif
sqlite3_shathree_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)3516 int sqlite3_shathree_init(
3517   sqlite3 *db,
3518   char **pzErrMsg,
3519   const sqlite3_api_routines *pApi
3520 ){
3521   int rc = SQLITE_OK;
3522   SQLITE_EXTENSION_INIT2(pApi);
3523   (void)pzErrMsg;  /* Unused parameter */
3524   rc = sqlite3_create_function(db, "sha3", 1,
3525                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
3526                       0, sha3Func, 0, 0);
3527   if( rc==SQLITE_OK ){
3528     rc = sqlite3_create_function(db, "sha3", 2,
3529                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
3530                       0, sha3Func, 0, 0);
3531   }
3532   if( rc==SQLITE_OK ){
3533     rc = sqlite3_create_function(db, "sha3_query", 1,
3534                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
3535                       0, sha3QueryFunc, 0, 0);
3536   }
3537   if( rc==SQLITE_OK ){
3538     rc = sqlite3_create_function(db, "sha3_query", 2,
3539                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
3540                       0, sha3QueryFunc, 0, 0);
3541   }
3542   return rc;
3543 }
3544 
3545 /************************* End ../ext/misc/shathree.c ********************/
3546 /************************* Begin ../ext/misc/uint.c ******************/
3547 /*
3548 ** 2020-04-14
3549 **
3550 ** The author disclaims copyright to this source code.  In place of
3551 ** a legal notice, here is a blessing:
3552 **
3553 **    May you do good and not evil.
3554 **    May you find forgiveness for yourself and forgive others.
3555 **    May you share freely, never taking more than you give.
3556 **
3557 ******************************************************************************
3558 **
3559 ** This SQLite extension implements the UINT collating sequence.
3560 **
3561 ** UINT works like BINARY for text, except that embedded strings
3562 ** of digits compare in numeric order.
3563 **
3564 **     *   Leading zeros are handled properly, in the sense that
3565 **         they do not mess of the maginitude comparison of embedded
3566 **         strings of digits.  "x00123y" is equal to "x123y".
3567 **
3568 **     *   Only unsigned integers are recognized.  Plus and minus
3569 **         signs are ignored.  Decimal points and exponential notation
3570 **         are ignored.
3571 **
3572 **     *   Embedded integers can be of arbitrary length.  Comparison
3573 **         is *not* limited integers that can be expressed as a
3574 **         64-bit machine integer.
3575 */
3576 /* #include "sqlite3ext.h" */
3577 SQLITE_EXTENSION_INIT1
3578 #include <assert.h>
3579 #include <string.h>
3580 #include <ctype.h>
3581 
3582 /*
3583 ** Compare text in lexicographic order, except strings of digits
3584 ** compare in numeric order.
3585 */
uintCollFunc(void * notUsed,int nKey1,const void * pKey1,int nKey2,const void * pKey2)3586 static int uintCollFunc(
3587   void *notUsed,
3588   int nKey1, const void *pKey1,
3589   int nKey2, const void *pKey2
3590 ){
3591   const unsigned char *zA = (const unsigned char*)pKey1;
3592   const unsigned char *zB = (const unsigned char*)pKey2;
3593   int i=0, j=0, x;
3594   (void)notUsed;
3595   while( i<nKey1 && j<nKey2 ){
3596     x = zA[i] - zB[j];
3597     if( isdigit(zA[i]) ){
3598       int k;
3599       if( !isdigit(zB[j]) ) return x;
3600       while( i<nKey1 && zA[i]=='0' ){ i++; }
3601       while( j<nKey2 && zB[j]=='0' ){ j++; }
3602       k = 0;
3603       while( i+k<nKey1 && isdigit(zA[i+k])
3604              && j+k<nKey2 && isdigit(zB[j+k]) ){
3605         k++;
3606       }
3607       if( i+k<nKey1 && isdigit(zA[i+k]) ){
3608         return +1;
3609       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
3610         return -1;
3611       }else{
3612         x = memcmp(zA+i, zB+j, k);
3613         if( x ) return x;
3614         i += k;
3615         j += k;
3616       }
3617     }else if( x ){
3618       return x;
3619     }else{
3620       i++;
3621       j++;
3622     }
3623   }
3624   return (nKey1 - i) - (nKey2 - j);
3625 }
3626 
3627 #ifdef _WIN32
3628 
3629 #endif
sqlite3_uint_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)3630 int sqlite3_uint_init(
3631   sqlite3 *db,
3632   char **pzErrMsg,
3633   const sqlite3_api_routines *pApi
3634 ){
3635   SQLITE_EXTENSION_INIT2(pApi);
3636   (void)pzErrMsg;  /* Unused parameter */
3637   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
3638 }
3639 
3640 /************************* End ../ext/misc/uint.c ********************/
3641 /************************* Begin ../ext/misc/decimal.c ******************/
3642 /*
3643 ** 2020-06-22
3644 **
3645 ** The author disclaims copyright to this source code.  In place of
3646 ** a legal notice, here is a blessing:
3647 **
3648 **    May you do good and not evil.
3649 **    May you find forgiveness for yourself and forgive others.
3650 **    May you share freely, never taking more than you give.
3651 **
3652 ******************************************************************************
3653 **
3654 ** Routines to implement arbitrary-precision decimal math.
3655 **
3656 ** The focus here is on simplicity and correctness, not performance.
3657 */
3658 /* #include "sqlite3ext.h" */
3659 SQLITE_EXTENSION_INIT1
3660 #include <assert.h>
3661 #include <string.h>
3662 #include <ctype.h>
3663 #include <stdlib.h>
3664 
3665 /* Mark a function parameter as unused, to suppress nuisance compiler
3666 ** warnings. */
3667 #ifndef UNUSED_PARAMETER
3668 # define UNUSED_PARAMETER(X)  (void)(X)
3669 #endif
3670 
3671 
3672 /* A decimal object */
3673 typedef struct Decimal Decimal;
3674 struct Decimal {
3675   char sign;        /* 0 for positive, 1 for negative */
3676   char oom;         /* True if an OOM is encountered */
3677   char isNull;      /* True if holds a NULL rather than a number */
3678   char isInit;      /* True upon initialization */
3679   int nDigit;       /* Total number of digits */
3680   int nFrac;        /* Number of digits to the right of the decimal point */
3681   signed char *a;   /* Array of digits.  Most significant first. */
3682 };
3683 
3684 /*
3685 ** Release memory held by a Decimal, but do not free the object itself.
3686 */
decimal_clear(Decimal * p)3687 static void decimal_clear(Decimal *p){
3688   sqlite3_free(p->a);
3689 }
3690 
3691 /*
3692 ** Destroy a Decimal object
3693 */
decimal_free(Decimal * p)3694 static void decimal_free(Decimal *p){
3695   if( p ){
3696     decimal_clear(p);
3697     sqlite3_free(p);
3698   }
3699 }
3700 
3701 /*
3702 ** Allocate a new Decimal object initialized to the text in zIn[].
3703 ** Return NULL if any kind of error occurs.
3704 */
decimalNewFromText(const char * zIn,int n)3705 static Decimal *decimalNewFromText(const char *zIn, int n){
3706   Decimal *p = 0;
3707   int i;
3708   int iExp = 0;
3709 
3710   p = sqlite3_malloc( sizeof(*p) );
3711   if( p==0 ) goto new_from_text_failed;
3712   p->sign = 0;
3713   p->oom = 0;
3714   p->isInit = 1;
3715   p->isNull = 0;
3716   p->nDigit = 0;
3717   p->nFrac = 0;
3718   p->a = sqlite3_malloc64( n+1 );
3719   if( p->a==0 ) goto new_from_text_failed;
3720   for(i=0; isspace(zIn[i]); i++){}
3721   if( zIn[i]=='-' ){
3722     p->sign = 1;
3723     i++;
3724   }else if( zIn[i]=='+' ){
3725     i++;
3726   }
3727   while( i<n && zIn[i]=='0' ) i++;
3728   while( i<n ){
3729     char c = zIn[i];
3730     if( c>='0' && c<='9' ){
3731       p->a[p->nDigit++] = c - '0';
3732     }else if( c=='.' ){
3733       p->nFrac = p->nDigit + 1;
3734     }else if( c=='e' || c=='E' ){
3735       int j = i+1;
3736       int neg = 0;
3737       if( j>=n ) break;
3738       if( zIn[j]=='-' ){
3739         neg = 1;
3740         j++;
3741       }else if( zIn[j]=='+' ){
3742         j++;
3743       }
3744       while( j<n && iExp<1000000 ){
3745         if( zIn[j]>='0' && zIn[j]<='9' ){
3746           iExp = iExp*10 + zIn[j] - '0';
3747         }
3748         j++;
3749       }
3750       if( neg ) iExp = -iExp;
3751       break;
3752     }
3753     i++;
3754   }
3755   if( p->nFrac ){
3756     p->nFrac = p->nDigit - (p->nFrac - 1);
3757   }
3758   if( iExp>0 ){
3759     if( p->nFrac>0 ){
3760       if( iExp<=p->nFrac ){
3761         p->nFrac -= iExp;
3762         iExp = 0;
3763       }else{
3764         iExp -= p->nFrac;
3765         p->nFrac = 0;
3766       }
3767     }
3768     if( iExp>0 ){
3769       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3770       if( p->a==0 ) goto new_from_text_failed;
3771       memset(p->a+p->nDigit, 0, iExp);
3772       p->nDigit += iExp;
3773     }
3774   }else if( iExp<0 ){
3775     int nExtra;
3776     iExp = -iExp;
3777     nExtra = p->nDigit - p->nFrac - 1;
3778     if( nExtra ){
3779       if( nExtra>=iExp ){
3780         p->nFrac += iExp;
3781         iExp  = 0;
3782       }else{
3783         iExp -= nExtra;
3784         p->nFrac = p->nDigit - 1;
3785       }
3786     }
3787     if( iExp>0 ){
3788       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3789       if( p->a==0 ) goto new_from_text_failed;
3790       memmove(p->a+iExp, p->a, p->nDigit);
3791       memset(p->a, 0, iExp);
3792       p->nDigit += iExp;
3793       p->nFrac += iExp;
3794     }
3795   }
3796   return p;
3797 
3798 new_from_text_failed:
3799   if( p ){
3800     if( p->a ) sqlite3_free(p->a);
3801     sqlite3_free(p);
3802   }
3803   return 0;
3804 }
3805 
3806 /* Forward reference */
3807 static Decimal *decimalFromDouble(double);
3808 
3809 /*
3810 ** Allocate a new Decimal object from an sqlite3_value.  Return a pointer
3811 ** to the new object, or NULL if there is an error.  If the pCtx argument
3812 ** is not NULL, then errors are reported on it as well.
3813 **
3814 ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
3815 ** directly into a Decimal.  For SQLITE_FLOAT or for SQLITE_BLOB of length
3816 ** 8 bytes, the resulting double value is expanded into its decimal equivalent.
3817 ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
3818 ** then NULL is returned.
3819 */
decimal_new(sqlite3_context * pCtx,sqlite3_value * pIn,int bTextOnly)3820 static Decimal *decimal_new(
3821   sqlite3_context *pCtx,       /* Report error here, if not null */
3822   sqlite3_value *pIn,          /* Construct the decimal object from this */
3823   int bTextOnly                /* Always interpret pIn as text if true */
3824 ){
3825   Decimal *p = 0;
3826   int eType = sqlite3_value_type(pIn);
3827   if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
3828     eType = SQLITE_TEXT;
3829   }
3830   switch( eType ){
3831     case SQLITE_TEXT:
3832     case SQLITE_INTEGER: {
3833       const char *zIn = (const char*)sqlite3_value_text(pIn);
3834       int n = sqlite3_value_bytes(pIn);
3835       p = decimalNewFromText(zIn, n);
3836       if( p==0 ) goto new_failed;
3837       break;
3838     }
3839 
3840     case SQLITE_FLOAT: {
3841       p = decimalFromDouble(sqlite3_value_double(pIn));
3842       break;
3843     }
3844 
3845     case SQLITE_BLOB: {
3846       const unsigned char *x;
3847       unsigned int i;
3848       sqlite3_uint64 v = 0;
3849       double r;
3850 
3851       if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
3852       x = sqlite3_value_blob(pIn);
3853       for(i=0; i<sizeof(r); i++){
3854         v = (v<<8) | x[i];
3855       }
3856       memcpy(&r, &v, sizeof(r));
3857       p = decimalFromDouble(r);
3858       break;
3859     }
3860 
3861     case SQLITE_NULL: {
3862       break;
3863     }
3864   }
3865   return p;
3866 
3867 new_failed:
3868   if( pCtx ) sqlite3_result_error_nomem(pCtx);
3869   sqlite3_free(p);
3870   return 0;
3871 }
3872 
3873 /*
3874 ** Make the given Decimal the result.
3875 */
decimal_result(sqlite3_context * pCtx,Decimal * p)3876 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
3877   char *z;
3878   int i, j;
3879   int n;
3880   if( p==0 || p->oom ){
3881     sqlite3_result_error_nomem(pCtx);
3882     return;
3883   }
3884   if( p->isNull ){
3885     sqlite3_result_null(pCtx);
3886     return;
3887   }
3888   z = sqlite3_malloc( p->nDigit+4 );
3889   if( z==0 ){
3890     sqlite3_result_error_nomem(pCtx);
3891     return;
3892   }
3893   i = 0;
3894   if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
3895     p->sign = 0;
3896   }
3897   if( p->sign ){
3898     z[0] = '-';
3899     i = 1;
3900   }
3901   n = p->nDigit - p->nFrac;
3902   if( n<=0 ){
3903     z[i++] = '0';
3904   }
3905   j = 0;
3906   while( n>1 && p->a[j]==0 ){
3907     j++;
3908     n--;
3909   }
3910   while( n>0  ){
3911     z[i++] = p->a[j] + '0';
3912     j++;
3913     n--;
3914   }
3915   if( p->nFrac ){
3916     z[i++] = '.';
3917     do{
3918       z[i++] = p->a[j] + '0';
3919       j++;
3920     }while( j<p->nDigit );
3921   }
3922   z[i] = 0;
3923   sqlite3_result_text(pCtx, z, i, sqlite3_free);
3924 }
3925 
3926 /*
3927 ** Make the given Decimal the result in an format similar to  '%+#e'.
3928 ** In other words, show exponential notation with leading and trailing
3929 ** zeros omitted.
3930 */
decimal_result_sci(sqlite3_context * pCtx,Decimal * p)3931 static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
3932   char *z;       /* The output buffer */
3933   int i;         /* Loop counter */
3934   int nZero;     /* Number of leading zeros */
3935   int nDigit;    /* Number of digits not counting trailing zeros */
3936   int nFrac;     /* Digits to the right of the decimal point */
3937   int exp;       /* Exponent value */
3938   signed char zero;     /* Zero value */
3939   signed char *a;       /* Array of digits */
3940 
3941   if( p==0 || p->oom ){
3942     sqlite3_result_error_nomem(pCtx);
3943     return;
3944   }
3945   if( p->isNull ){
3946     sqlite3_result_null(pCtx);
3947     return;
3948   }
3949   for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3950   for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3951   nFrac = p->nFrac + (nDigit - p->nDigit);
3952   nDigit -= nZero;
3953   z = sqlite3_malloc( nDigit+20 );
3954   if( z==0 ){
3955     sqlite3_result_error_nomem(pCtx);
3956     return;
3957   }
3958   if( nDigit==0 ){
3959     zero = 0;
3960     a = &zero;
3961     nDigit = 1;
3962     nFrac = 0;
3963   }else{
3964     a = &p->a[nZero];
3965   }
3966   if( p->sign && nDigit>0 ){
3967     z[0] = '-';
3968   }else{
3969     z[0] = '+';
3970   }
3971   z[1] = a[0]+'0';
3972   z[2] = '.';
3973   if( nDigit==1 ){
3974     z[3] = '0';
3975     i = 4;
3976   }else{
3977     for(i=1; i<nDigit; i++){
3978       z[2+i] = a[i]+'0';
3979     }
3980     i = nDigit+2;
3981   }
3982   exp = nDigit - nFrac - 1;
3983   sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
3984   sqlite3_result_text(pCtx, z, -1, sqlite3_free);
3985 }
3986 
3987 /*
3988 ** Compare to Decimal objects.  Return negative, 0, or positive if the
3989 ** first object is less than, equal to, or greater than the second.
3990 **
3991 ** Preconditions for this routine:
3992 **
3993 **    pA!=0
3994 **    pA->isNull==0
3995 **    pB!=0
3996 **    pB->isNull==0
3997 */
decimal_cmp(const Decimal * pA,const Decimal * pB)3998 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
3999   int nASig, nBSig, rc, n;
4000   if( pA->sign!=pB->sign ){
4001     return pA->sign ? -1 : +1;
4002   }
4003   if( pA->sign ){
4004     const Decimal *pTemp = pA;
4005     pA = pB;
4006     pB = pTemp;
4007   }
4008   nASig = pA->nDigit - pA->nFrac;
4009   nBSig = pB->nDigit - pB->nFrac;
4010   if( nASig!=nBSig ){
4011     return nASig - nBSig;
4012   }
4013   n = pA->nDigit;
4014   if( n>pB->nDigit ) n = pB->nDigit;
4015   rc = memcmp(pA->a, pB->a, n);
4016   if( rc==0 ){
4017     rc = pA->nDigit - pB->nDigit;
4018   }
4019   return rc;
4020 }
4021 
4022 /*
4023 ** SQL Function:   decimal_cmp(X, Y)
4024 **
4025 ** Return negative, zero, or positive if X is less then, equal to, or
4026 ** greater than Y.
4027 */
decimalCmpFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)4028 static void decimalCmpFunc(
4029   sqlite3_context *context,
4030   int argc,
4031   sqlite3_value **argv
4032 ){
4033   Decimal *pA = 0, *pB = 0;
4034   int rc;
4035 
4036   UNUSED_PARAMETER(argc);
4037   pA = decimal_new(context, argv[0], 1);
4038   if( pA==0 || pA->isNull ) goto cmp_done;
4039   pB = decimal_new(context, argv[1], 1);
4040   if( pB==0 || pB->isNull ) goto cmp_done;
4041   rc = decimal_cmp(pA, pB);
4042   if( rc<0 ) rc = -1;
4043   else if( rc>0 ) rc = +1;
4044   sqlite3_result_int(context, rc);
4045 cmp_done:
4046   decimal_free(pA);
4047   decimal_free(pB);
4048 }
4049 
4050 /*
4051 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4052 ** digits to the right of the decimal point.
4053 */
decimal_expand(Decimal * p,int nDigit,int nFrac)4054 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4055   int nAddSig;
4056   int nAddFrac;
4057   if( p==0 ) return;
4058   nAddFrac = nFrac - p->nFrac;
4059   nAddSig = (nDigit - p->nDigit) - nAddFrac;
4060   if( nAddFrac==0 && nAddSig==0 ) return;
4061   p->a = sqlite3_realloc64(p->a, nDigit+1);
4062   if( p->a==0 ){
4063     p->oom = 1;
4064     return;
4065   }
4066   if( nAddSig ){
4067     memmove(p->a+nAddSig, p->a, p->nDigit);
4068     memset(p->a, 0, nAddSig);
4069     p->nDigit += nAddSig;
4070   }
4071   if( nAddFrac ){
4072     memset(p->a+p->nDigit, 0, nAddFrac);
4073     p->nDigit += nAddFrac;
4074     p->nFrac += nAddFrac;
4075   }
4076 }
4077 
4078 /*
4079 ** Add the value pB into pA.   A := A + B.
4080 **
4081 ** Both pA and pB might become denormalized by this routine.
4082 */
decimal_add(Decimal * pA,Decimal * pB)4083 static void decimal_add(Decimal *pA, Decimal *pB){
4084   int nSig, nFrac, nDigit;
4085   int i, rc;
4086   if( pA==0 ){
4087     return;
4088   }
4089   if( pA->oom || pB==0 || pB->oom ){
4090     pA->oom = 1;
4091     return;
4092   }
4093   if( pA->isNull || pB->isNull ){
4094     pA->isNull = 1;
4095     return;
4096   }
4097   nSig = pA->nDigit - pA->nFrac;
4098   if( nSig && pA->a[0]==0 ) nSig--;
4099   if( nSig<pB->nDigit-pB->nFrac ){
4100     nSig = pB->nDigit - pB->nFrac;
4101   }
4102   nFrac = pA->nFrac;
4103   if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4104   nDigit = nSig + nFrac + 1;
4105   decimal_expand(pA, nDigit, nFrac);
4106   decimal_expand(pB, nDigit, nFrac);
4107   if( pA->oom || pB->oom ){
4108     pA->oom = 1;
4109   }else{
4110     if( pA->sign==pB->sign ){
4111       int carry = 0;
4112       for(i=nDigit-1; i>=0; i--){
4113         int x = pA->a[i] + pB->a[i] + carry;
4114         if( x>=10 ){
4115           carry = 1;
4116           pA->a[i] = x - 10;
4117         }else{
4118           carry = 0;
4119           pA->a[i] = x;
4120         }
4121       }
4122     }else{
4123       signed char *aA, *aB;
4124       int borrow = 0;
4125       rc = memcmp(pA->a, pB->a, nDigit);
4126       if( rc<0 ){
4127         aA = pB->a;
4128         aB = pA->a;
4129         pA->sign = !pA->sign;
4130       }else{
4131         aA = pA->a;
4132         aB = pB->a;
4133       }
4134       for(i=nDigit-1; i>=0; i--){
4135         int x = aA[i] - aB[i] - borrow;
4136         if( x<0 ){
4137           pA->a[i] = x+10;
4138           borrow = 1;
4139         }else{
4140           pA->a[i] = x;
4141           borrow = 0;
4142         }
4143       }
4144     }
4145   }
4146 }
4147 
4148 /*
4149 ** Multiply A by B.   A := A * B
4150 **
4151 ** All significant digits after the decimal point are retained.
4152 ** Trailing zeros after the decimal point are omitted as long as
4153 ** the number of digits after the decimal point is no less than
4154 ** either the number of digits in either input.
4155 */
decimalMul(Decimal * pA,Decimal * pB)4156 static void decimalMul(Decimal *pA, Decimal *pB){
4157   signed char *acc = 0;
4158   int i, j, k;
4159   int minFrac;
4160 
4161   if( pA==0 || pA->oom || pA->isNull
4162    || pB==0 || pB->oom || pB->isNull
4163   ){
4164     goto mul_end;
4165   }
4166   acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4167   if( acc==0 ){
4168     pA->oom = 1;
4169     goto mul_end;
4170   }
4171   memset(acc, 0, pA->nDigit + pB->nDigit + 2);
4172   minFrac = pA->nFrac;
4173   if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
4174   for(i=pA->nDigit-1; i>=0; i--){
4175     signed char f = pA->a[i];
4176     int carry = 0, x;
4177     for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
4178       x = acc[k] + f*pB->a[j] + carry;
4179       acc[k] = x%10;
4180       carry = x/10;
4181     }
4182     x = acc[k] + carry;
4183     acc[k] = x%10;
4184     acc[k-1] += x/10;
4185   }
4186   sqlite3_free(pA->a);
4187   pA->a = acc;
4188   acc = 0;
4189   pA->nDigit += pB->nDigit + 2;
4190   pA->nFrac += pB->nFrac;
4191   pA->sign ^= pB->sign;
4192   while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
4193     pA->nFrac--;
4194     pA->nDigit--;
4195   }
4196 
4197 mul_end:
4198   sqlite3_free(acc);
4199 }
4200 
4201 /*
4202 ** Create a new Decimal object that contains an integer power of 2.
4203 */
decimalPow2(int N)4204 static Decimal *decimalPow2(int N){
4205   Decimal *pA = 0;      /* The result to be returned */
4206   Decimal *pX = 0;      /* Multiplier */
4207   if( N<-20000 || N>20000 ) goto pow2_fault;
4208   pA = decimalNewFromText("1.0", 3);
4209   if( pA==0 || pA->oom ) goto pow2_fault;
4210   if( N==0 ) return pA;
4211   if( N>0 ){
4212     pX = decimalNewFromText("2.0", 3);
4213   }else{
4214     N = -N;
4215     pX = decimalNewFromText("0.5", 3);
4216   }
4217   if( pX==0 || pX->oom ) goto pow2_fault;
4218   while( 1 /* Exit by break */ ){
4219     if( N & 1 ){
4220       decimalMul(pA, pX);
4221       if( pA->oom ) goto pow2_fault;
4222     }
4223     N >>= 1;
4224     if( N==0 ) break;
4225     decimalMul(pX, pX);
4226   }
4227   decimal_free(pX);
4228   return pA;
4229 
4230 pow2_fault:
4231   decimal_free(pA);
4232   decimal_free(pX);
4233   return 0;
4234 }
4235 
4236 /*
4237 ** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
4238 */
decimalFromDouble(double r)4239 static Decimal *decimalFromDouble(double r){
4240   sqlite3_int64 m, a;
4241   int e;
4242   int isNeg;
4243   Decimal *pA;
4244   Decimal *pX;
4245   char zNum[100];
4246   if( r<0.0 ){
4247     isNeg = 1;
4248     r = -r;
4249   }else{
4250     isNeg = 0;
4251   }
4252   memcpy(&a,&r,sizeof(a));
4253   if( a==0 ){
4254     e = 0;
4255     m = 0;
4256   }else{
4257     e = a>>52;
4258     m = a & ((((sqlite3_int64)1)<<52)-1);
4259     if( e==0 ){
4260       m <<= 1;
4261     }else{
4262       m |= ((sqlite3_int64)1)<<52;
4263     }
4264     while( e<1075 && m>0 && (m&1)==0 ){
4265       m >>= 1;
4266       e++;
4267     }
4268     if( isNeg ) m = -m;
4269     e = e - 1075;
4270     if( e>971 ){
4271       return 0;  /* A NaN or an Infinity */
4272     }
4273   }
4274 
4275   /* At this point m is the integer significand and e is the exponent */
4276   sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
4277   pA = decimalNewFromText(zNum, (int)strlen(zNum));
4278   pX = decimalPow2(e);
4279   decimalMul(pA, pX);
4280   decimal_free(pX);
4281   return pA;
4282 }
4283 
4284 /*
4285 ** SQL Function:   decimal(X)
4286 ** OR:             decimal_exp(X)
4287 **
4288 ** Convert input X into decimal and then back into text.
4289 **
4290 ** If X is originally a float, then a full decimal expansion of that floating
4291 ** point value is done.  Or if X is an 8-byte blob, it is interpreted
4292 ** as a float and similarly expanded.
4293 **
4294 ** The decimal_exp(X) function returns the result in exponential notation.
4295 ** decimal(X) returns a complete decimal, without the e+NNN at the end.
4296 */
decimalFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)4297 static void decimalFunc(
4298   sqlite3_context *context,
4299   int argc,
4300   sqlite3_value **argv
4301 ){
4302   Decimal *p =  decimal_new(context, argv[0], 0);
4303   UNUSED_PARAMETER(argc);
4304   if( p ){
4305     if( sqlite3_user_data(context)!=0 ){
4306       decimal_result_sci(context, p);
4307     }else{
4308       decimal_result(context, p);
4309     }
4310     decimal_free(p);
4311   }
4312 }
4313 
4314 /*
4315 ** Compare text in decimal order.
4316 */
decimalCollFunc(void * notUsed,int nKey1,const void * pKey1,int nKey2,const void * pKey2)4317 static int decimalCollFunc(
4318   void *notUsed,
4319   int nKey1, const void *pKey1,
4320   int nKey2, const void *pKey2
4321 ){
4322   const unsigned char *zA = (const unsigned char*)pKey1;
4323   const unsigned char *zB = (const unsigned char*)pKey2;
4324   Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
4325   Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
4326   int rc;
4327   UNUSED_PARAMETER(notUsed);
4328   if( pA==0 || pB==0 ){
4329     rc = 0;
4330   }else{
4331     rc = decimal_cmp(pA, pB);
4332   }
4333   decimal_free(pA);
4334   decimal_free(pB);
4335   return rc;
4336 }
4337 
4338 
4339 /*
4340 ** SQL Function:   decimal_add(X, Y)
4341 **                 decimal_sub(X, Y)
4342 **
4343 ** Return the sum or difference of X and Y.
4344 */
decimalAddFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)4345 static void decimalAddFunc(
4346   sqlite3_context *context,
4347   int argc,
4348   sqlite3_value **argv
4349 ){
4350   Decimal *pA = decimal_new(context, argv[0], 1);
4351   Decimal *pB = decimal_new(context, argv[1], 1);
4352   UNUSED_PARAMETER(argc);
4353   decimal_add(pA, pB);
4354   decimal_result(context, pA);
4355   decimal_free(pA);
4356   decimal_free(pB);
4357 }
decimalSubFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)4358 static void decimalSubFunc(
4359   sqlite3_context *context,
4360   int argc,
4361   sqlite3_value **argv
4362 ){
4363   Decimal *pA = decimal_new(context, argv[0], 1);
4364   Decimal *pB = decimal_new(context, argv[1], 1);
4365   UNUSED_PARAMETER(argc);
4366   if( pB ){
4367     pB->sign = !pB->sign;
4368     decimal_add(pA, pB);
4369     decimal_result(context, pA);
4370   }
4371   decimal_free(pA);
4372   decimal_free(pB);
4373 }
4374 
4375 /* Aggregate funcion:   decimal_sum(X)
4376 **
4377 ** Works like sum() except that it uses decimal arithmetic for unlimited
4378 ** precision.
4379 */
decimalSumStep(sqlite3_context * context,int argc,sqlite3_value ** argv)4380 static void decimalSumStep(
4381   sqlite3_context *context,
4382   int argc,
4383   sqlite3_value **argv
4384 ){
4385   Decimal *p;
4386   Decimal *pArg;
4387   UNUSED_PARAMETER(argc);
4388   p = sqlite3_aggregate_context(context, sizeof(*p));
4389   if( p==0 ) return;
4390   if( !p->isInit ){
4391     p->isInit = 1;
4392     p->a = sqlite3_malloc(2);
4393     if( p->a==0 ){
4394       p->oom = 1;
4395     }else{
4396       p->a[0] = 0;
4397     }
4398     p->nDigit = 1;
4399     p->nFrac = 0;
4400   }
4401   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4402   pArg = decimal_new(context, argv[0], 1);
4403   decimal_add(p, pArg);
4404   decimal_free(pArg);
4405 }
decimalSumInverse(sqlite3_context * context,int argc,sqlite3_value ** argv)4406 static void decimalSumInverse(
4407   sqlite3_context *context,
4408   int argc,
4409   sqlite3_value **argv
4410 ){
4411   Decimal *p;
4412   Decimal *pArg;
4413   UNUSED_PARAMETER(argc);
4414   p = sqlite3_aggregate_context(context, sizeof(*p));
4415   if( p==0 ) return;
4416   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4417   pArg = decimal_new(context, argv[0], 1);
4418   if( pArg ) pArg->sign = !pArg->sign;
4419   decimal_add(p, pArg);
4420   decimal_free(pArg);
4421 }
decimalSumValue(sqlite3_context * context)4422 static void decimalSumValue(sqlite3_context *context){
4423   Decimal *p = sqlite3_aggregate_context(context, 0);
4424   if( p==0 ) return;
4425   decimal_result(context, p);
4426 }
decimalSumFinalize(sqlite3_context * context)4427 static void decimalSumFinalize(sqlite3_context *context){
4428   Decimal *p = sqlite3_aggregate_context(context, 0);
4429   if( p==0 ) return;
4430   decimal_result(context, p);
4431   decimal_clear(p);
4432 }
4433 
4434 /*
4435 ** SQL Function:   decimal_mul(X, Y)
4436 **
4437 ** Return the product of X and Y.
4438 */
decimalMulFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)4439 static void decimalMulFunc(
4440   sqlite3_context *context,
4441   int argc,
4442   sqlite3_value **argv
4443 ){
4444   Decimal *pA = decimal_new(context, argv[0], 1);
4445   Decimal *pB = decimal_new(context, argv[1], 1);
4446   UNUSED_PARAMETER(argc);
4447   if( pA==0 || pA->oom || pA->isNull
4448    || pB==0 || pB->oom || pB->isNull
4449   ){
4450     goto mul_end;
4451   }
4452   decimalMul(pA, pB);
4453   if( pA->oom ){
4454     goto mul_end;
4455   }
4456   decimal_result(context, pA);
4457 
4458 mul_end:
4459   decimal_free(pA);
4460   decimal_free(pB);
4461 }
4462 
4463 /*
4464 ** SQL Function:   decimal_pow2(N)
4465 **
4466 ** Return the N-th power of 2.  N must be an integer.
4467 */
decimalPow2Func(sqlite3_context * context,int argc,sqlite3_value ** argv)4468 static void decimalPow2Func(
4469   sqlite3_context *context,
4470   int argc,
4471   sqlite3_value **argv
4472 ){
4473   UNUSED_PARAMETER(argc);
4474   if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
4475     Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
4476     decimal_result_sci(context, pA);
4477     decimal_free(pA);
4478   }
4479 }
4480 
4481 #ifdef _WIN32
4482 
4483 #endif
sqlite3_decimal_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)4484 int sqlite3_decimal_init(
4485   sqlite3 *db,
4486   char **pzErrMsg,
4487   const sqlite3_api_routines *pApi
4488 ){
4489   int rc = SQLITE_OK;
4490   static const struct {
4491     const char *zFuncName;
4492     int nArg;
4493     int iArg;
4494     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
4495   } aFunc[] = {
4496     { "decimal",       1, 0,  decimalFunc        },
4497     { "decimal_exp",   1, 1,  decimalFunc        },
4498     { "decimal_cmp",   2, 0,  decimalCmpFunc     },
4499     { "decimal_add",   2, 0,  decimalAddFunc     },
4500     { "decimal_sub",   2, 0,  decimalSubFunc     },
4501     { "decimal_mul",   2, 0,  decimalMulFunc     },
4502     { "decimal_pow2",  1, 0,  decimalPow2Func    },
4503   };
4504   unsigned int i;
4505   (void)pzErrMsg;  /* Unused parameter */
4506 
4507   SQLITE_EXTENSION_INIT2(pApi);
4508 
4509   for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
4510     rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
4511                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
4512                    aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
4513   }
4514   if( rc==SQLITE_OK ){
4515     rc = sqlite3_create_window_function(db, "decimal_sum", 1,
4516                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
4517                    decimalSumStep, decimalSumFinalize,
4518                    decimalSumValue, decimalSumInverse, 0);
4519   }
4520   if( rc==SQLITE_OK ){
4521     rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
4522                                   0, decimalCollFunc);
4523   }
4524   return rc;
4525 }
4526 
4527 /************************* End ../ext/misc/decimal.c ********************/
4528 #undef sqlite3_base_init
4529 #define sqlite3_base_init sqlite3_base64_init
4530 /************************* Begin ../ext/misc/base64.c ******************/
4531 /*
4532 ** 2022-11-18
4533 **
4534 ** The author disclaims copyright to this source code.  In place of
4535 ** a legal notice, here is a blessing:
4536 **
4537 **    May you do good and not evil.
4538 **    May you find forgiveness for yourself and forgive others.
4539 **    May you share freely, never taking more than you give.
4540 **
4541 *************************************************************************
4542 **
4543 ** This is a SQLite extension for converting in either direction
4544 ** between a (binary) blob and base64 text. Base64 can transit a
4545 ** sane USASCII channel unmolested. It also plays nicely in CSV or
4546 ** written as TCL brace-enclosed literals or SQL string literals,
4547 ** and can be used unmodified in XML-like documents.
4548 **
4549 ** This is an independent implementation of conversions specified in
4550 ** RFC 4648, done on the above date by the author (Larry Brasfield)
4551 ** who thereby has the right to put this into the public domain.
4552 **
4553 ** The conversions meet RFC 4648 requirements, provided that this
4554 ** C source specifies that line-feeds are included in the encoded
4555 ** data to limit visible line lengths to 72 characters and to
4556 ** terminate any encoded blob having non-zero length.
4557 **
4558 ** Length limitations are not imposed except that the runtime
4559 ** SQLite string or blob length limits are respected. Otherwise,
4560 ** any length binary sequence can be represented and recovered.
4561 ** Generated base64 sequences, with their line-feeds included,
4562 ** can be concatenated; the result converted back to binary will
4563 ** be the concatenation of the represented binary sequences.
4564 **
4565 ** This SQLite3 extension creates a function, base64(x), which
4566 ** either: converts text x containing base64 to a returned blob;
4567 ** or converts a blob x to returned text containing base64. An
4568 ** error will be thrown for other input argument types.
4569 **
4570 ** This code relies on UTF-8 encoding only with respect to the
4571 ** meaning of the first 128 (7-bit) codes matching that of USASCII.
4572 ** It will fail miserably if somehow made to try to convert EBCDIC.
4573 ** Because it is table-driven, it could be enhanced to handle that,
4574 ** but the world and SQLite have moved on from that anachronism.
4575 **
4576 ** To build the extension:
4577 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
4578 ** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c
4579 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c
4580 ** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c
4581 ** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll
4582 */
4583 
4584 #include <assert.h>
4585 
4586 /* #include "sqlite3ext.h" */
4587 
4588 #ifndef deliberate_fall_through
4589 /* Quiet some compilers about some of our intentional code. */
4590 # if GCC_VERSION>=7000000
4591 #  define deliberate_fall_through __attribute__((fallthrough));
4592 # else
4593 #  define deliberate_fall_through
4594 # endif
4595 #endif
4596 
4597 SQLITE_EXTENSION_INIT1;
4598 
4599 #define PC 0x80 /* pad character */
4600 #define WS 0x81 /* whitespace */
4601 #define ND 0x82 /* Not above or digit-value */
4602 #define PAD_CHAR '='
4603 
4604 #ifndef U8_TYPEDEF
4605 /* typedef unsigned char u8; */
4606 #define U8_TYPEDEF
4607 #endif
4608 
4609 /* Decoding table, ASCII (7-bit) value to base 64 digit value or other */
4610 static const u8 b64DigitValues[128] = {
4611   /*                             HT LF VT  FF CR       */
4612     ND,ND,ND,ND, ND,ND,ND,ND, ND,WS,WS,WS, WS,WS,ND,ND,
4613   /*                                                US */
4614     ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND,
4615   /*sp                                  +            / */
4616     WS,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,62, ND,ND,ND,63,
4617   /* 0  1            5            9            =       */
4618     52,53,54,55, 56,57,58,59, 60,61,ND,ND, ND,PC,ND,ND,
4619   /*    A                                            O */
4620     ND, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
4621   /* P                               Z                 */
4622     15,16,17,18, 19,20,21,22, 23,24,25,ND, ND,ND,ND,ND,
4623   /*    a                                            o */
4624     ND,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
4625   /* p                               z                 */
4626     41,42,43,44, 45,46,47,48, 49,50,51,ND, ND,ND,ND,ND
4627 };
4628 
4629 static const char b64Numerals[64+1]
4630 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4631 
4632 #define BX_DV_PROTO(c) \
4633   ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80)
4634 #define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80)
4635 #define IS_BX_WS(bdp) ((bdp)==WS)
4636 #define IS_BX_PAD(bdp) ((bdp)==PC)
4637 #define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)])
4638 /* Width of base64 lines. Should be an integer multiple of 4. */
4639 #define B64_DARK_MAX 72
4640 
4641 /* Encode a byte buffer into base64 text with linefeeds appended to limit
4642 ** encoded group lengths to B64_DARK_MAX or to terminate the last group.
4643 */
toBase64(u8 * pIn,int nbIn,char * pOut)4644 static char* toBase64( u8 *pIn, int nbIn, char *pOut ){
4645   int nCol = 0;
4646   while( nbIn >= 3 ){
4647     /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */
4648     pOut[0] = BX_NUMERAL(pIn[0]>>2);
4649     pOut[1] = BX_NUMERAL(((pIn[0]<<4)|(pIn[1]>>4))&0x3f);
4650     pOut[2] = BX_NUMERAL(((pIn[1]&0xf)<<2)|(pIn[2]>>6));
4651     pOut[3] = BX_NUMERAL(pIn[2]&0x3f);
4652     pOut += 4;
4653     nbIn -= 3;
4654     pIn += 3;
4655     if( (nCol += 4)>=B64_DARK_MAX || nbIn<=0 ){
4656       *pOut++ = '\n';
4657       nCol = 0;
4658     }
4659   }
4660   if( nbIn > 0 ){
4661     signed char nco = nbIn+1;
4662     int nbe;
4663     unsigned long qv = *pIn++;
4664     for( nbe=1; nbe<3; ++nbe ){
4665       qv <<= 8;
4666       if( nbe<nbIn ) qv |= *pIn++;
4667     }
4668     for( nbe=3; nbe>=0; --nbe ){
4669       char ce = (nbe<nco)? BX_NUMERAL((u8)(qv & 0x3f)) : PAD_CHAR;
4670       qv >>= 6;
4671       pOut[nbe] = ce;
4672     }
4673     pOut += 4;
4674     *pOut++ = '\n';
4675   }
4676   *pOut = 0;
4677   return pOut;
4678 }
4679 
4680 /* Skip over text which is not base64 numeral(s). */
skipNonB64(char * s,int nc)4681 static char * skipNonB64( char *s, int nc ){
4682   char c;
4683   while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s;
4684   return s;
4685 }
4686 
4687 /* Decode base64 text into a byte buffer. */
fromBase64(char * pIn,int ncIn,u8 * pOut)4688 static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){
4689   if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
4690   while( ncIn>0 && *pIn!=PAD_CHAR ){
4691     static signed char nboi[] = { 0, 0, 1, 2, 3 };
4692     char *pUse = skipNonB64(pIn, ncIn);
4693     unsigned long qv = 0L;
4694     int nti, nbo, nac;
4695     ncIn -= (pUse - pIn);
4696     pIn = pUse;
4697     nti = (ncIn>4)? 4 : ncIn;
4698     ncIn -= nti;
4699     nbo = nboi[nti];
4700     if( nbo==0 ) break;
4701     for( nac=0; nac<4; ++nac ){
4702       char c = (nac<nti)? *pIn++ : b64Numerals[0];
4703       u8 bdp = BX_DV_PROTO(c);
4704       switch( bdp ){
4705       case ND:
4706         /*  Treat dark non-digits as pad, but they terminate decode too. */
4707         ncIn = 0;
4708         deliberate_fall_through;
4709       case WS:
4710         /* Treat whitespace as pad and terminate this group.*/
4711         nti = nac;
4712         deliberate_fall_through;
4713       case PC:
4714         bdp = 0;
4715         --nbo;
4716         deliberate_fall_through;
4717       default: /* bdp is the digit value. */
4718         qv = qv<<6 | bdp;
4719         break;
4720       }
4721     }
4722     switch( nbo ){
4723     case 3:
4724       pOut[2] = (qv) & 0xff;
4725     case 2:
4726       pOut[1] = (qv>>8) & 0xff;
4727     case 1:
4728       pOut[0] = (qv>>16) & 0xff;
4729     }
4730     pOut += nbo;
4731   }
4732   return pOut;
4733 }
4734 
4735 /* This function does the work for the SQLite base64(x) UDF. */
base64(sqlite3_context * context,int na,sqlite3_value * av[])4736 static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
4737   int nb, nc, nv = sqlite3_value_bytes(av[0]);
4738   int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
4739                             SQLITE_LIMIT_LENGTH, -1);
4740   char *cBuf;
4741   u8 *bBuf;
4742   assert(na==1);
4743   switch( sqlite3_value_type(av[0]) ){
4744   case SQLITE_BLOB:
4745     nb = nv;
4746     nc = 4*(nv+2/3); /* quads needed */
4747     nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
4748     if( nvMax < nc ){
4749       sqlite3_result_error(context, "blob expanded to base64 too big", -1);
4750       return;
4751     }
4752     bBuf = (u8*)sqlite3_value_blob(av[0]);
4753     if( !bBuf ){
4754       if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
4755         goto memFail;
4756       }
4757       sqlite3_result_text(context,"",-1,SQLITE_STATIC);
4758       break;
4759     }
4760     cBuf = sqlite3_malloc(nc);
4761     if( !cBuf ) goto memFail;
4762     nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf);
4763     sqlite3_result_text(context, cBuf, nc, sqlite3_free);
4764     break;
4765   case SQLITE_TEXT:
4766     nc = nv;
4767     nb = 3*((nv+3)/4); /* may overestimate due to LF and padding */
4768     if( nvMax < nb ){
4769       sqlite3_result_error(context, "blob from base64 may be too big", -1);
4770       return;
4771     }else if( nb<1 ){
4772       nb = 1;
4773     }
4774     cBuf = (char *)sqlite3_value_text(av[0]);
4775     if( !cBuf ){
4776       if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
4777         goto memFail;
4778       }
4779       sqlite3_result_zeroblob(context, 0);
4780       break;
4781     }
4782     bBuf = sqlite3_malloc(nb);
4783     if( !bBuf ) goto memFail;
4784     nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf);
4785     sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
4786     break;
4787   default:
4788     sqlite3_result_error(context, "base64 accepts only blob or text", -1);
4789     return;
4790   }
4791   return;
4792  memFail:
4793   sqlite3_result_error(context, "base64 OOM", -1);
4794 }
4795 
4796 /*
4797 ** Establish linkage to running SQLite library.
4798 */
4799 #ifndef SQLITE_SHELL_EXTFUNCS
4800 #ifdef _WIN32
4801 
4802 #endif
sqlite3_base_init(sqlite3 * db,char ** pzErr,const sqlite3_api_routines * pApi)4803 int sqlite3_base_init
4804 #else
4805 static int sqlite3_base64_init
4806 #endif
4807 (sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
4808   SQLITE_EXTENSION_INIT2(pApi);
4809   (void)pzErr;
4810   return sqlite3_create_function
4811     (db, "base64", 1,
4812      SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
4813      0, base64, 0, 0);
4814 }
4815 
4816 /*
4817 ** Define some macros to allow this extension to be built into the shell
4818 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
4819 ** allows shell.c, as distributed, to have this extension built in.
4820 */
4821 #define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0)
4822 #define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
4823 
4824 /************************* End ../ext/misc/base64.c ********************/
4825 #undef sqlite3_base_init
4826 #define sqlite3_base_init sqlite3_base85_init
4827 #define OMIT_BASE85_CHECKER
4828 /************************* Begin ../ext/misc/base85.c ******************/
4829 /*
4830 ** 2022-11-16
4831 **
4832 ** The author disclaims copyright to this source code.  In place of
4833 ** a legal notice, here is a blessing:
4834 **
4835 **    May you do good and not evil.
4836 **    May you find forgiveness for yourself and forgive others.
4837 **    May you share freely, never taking more than you give.
4838 **
4839 *************************************************************************
4840 **
4841 ** This is a utility for converting binary to base85 or vice-versa.
4842 ** It can be built as a standalone program or an SQLite3 extension.
4843 **
4844 ** Much like base64 representations, base85 can be sent through a
4845 ** sane USASCII channel unmolested. It also plays nicely in CSV or
4846 ** written as TCL brace-enclosed literals or SQL string literals.
4847 ** It is not suited for unmodified use in XML-like documents.
4848 **
4849 ** The encoding used resembles Ascii85, but was devised by the author
4850 ** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85
4851 ** variant sources existed, in the 1984 timeframe on a VAX mainframe.
4852 ** Further, this is an independent implementation of a base85 system.
4853 ** Hence, the author has rightfully put this into the public domain.
4854 **
4855 ** Base85 numerals are taken from the set of 7-bit USASCII codes,
4856 ** excluding control characters and Space ! " ' ( ) { | } ~ Del
4857 ** in code order representing digit values 0 to 84 (base 10.)
4858 **
4859 ** Groups of 4 bytes, interpreted as big-endian 32-bit values,
4860 ** are represented as 5-digit base85 numbers with MS to LS digit
4861 ** order. Groups of 1-3 bytes are represented with 2-4 digits,
4862 ** still big-endian but 8-24 bit values. (Using big-endian yields
4863 ** the simplest transition to byte groups smaller than 4 bytes.
4864 ** These byte groups can also be considered base-256 numbers.)
4865 ** Groups of 0 bytes are represented with 0 digits and vice-versa.
4866 ** No pad characters are used; Encoded base85 numeral sequence
4867 ** (aka "group") length maps 1-to-1 to the decoded binary length.
4868 **
4869 ** Any character not in the base85 numeral set delimits groups.
4870 ** When base85 is streamed or stored in containers of indefinite
4871 ** size, newline is used to separate it into sub-sequences of no
4872 ** more than 80 digits so that fgets() can be used to read it.
4873 **
4874 ** Length limitations are not imposed except that the runtime
4875 ** SQLite string or blob length limits are respected. Otherwise,
4876 ** any length binary sequence can be represented and recovered.
4877 ** Base85 sequences can be concatenated by separating them with
4878 ** a non-base85 character; the conversion to binary will then
4879 ** be the concatenation of the represented binary sequences.
4880 
4881 ** The standalone program either converts base85 on stdin to create
4882 ** a binary file or converts a binary file to base85 on stdout.
4883 ** Read or make it blurt its help for invocation details.
4884 **
4885 ** The SQLite3 extension creates a function, base85(x), which will
4886 ** either convert text base85 to a blob or a blob to text base85
4887 ** and return the result (or throw an error for other types.)
4888 ** Unless built with OMIT_BASE85_CHECKER defined, it also creates a
4889 ** function, is_base85(t), which returns 1 iff the text t contains
4890 ** nothing other than base85 numerals and whitespace, or 0 otherwise.
4891 **
4892 ** To build the extension:
4893 ** Set shell variable SQDIR=<your favorite SQLite checkout directory>
4894 ** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted.
4895 ** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c
4896 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c
4897 ** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c
4898 ** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll
4899 **
4900 ** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg.
4901 ** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85
4902 ** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c
4903 ** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c
4904 */
4905 
4906 #include <stdio.h>
4907 #include <memory.h>
4908 #include <string.h>
4909 #include <assert.h>
4910 #ifndef OMIT_BASE85_CHECKER
4911 # include <ctype.h>
4912 #endif
4913 
4914 #ifndef BASE85_STANDALONE
4915 
4916 /* # include "sqlite3ext.h" */
4917 
4918 SQLITE_EXTENSION_INIT1;
4919 
4920 #else
4921 
4922 # ifdef _WIN32
4923 #  include <io.h>
4924 #  include <fcntl.h>
4925 # else
4926 #  define setmode(fd,m)
4927 # endif
4928 
4929 static char *zHelp =
4930   "Usage: base85 <dirFlag> <binFile>\n"
4931   " <dirFlag> is either -r to read or -w to write <binFile>,\n"
4932   "   content to be converted to/from base85 on stdout/stdin.\n"
4933   " <binFile> names a binary file to be rendered or created.\n"
4934   "   Or, the name '-' refers to the stdin or stdout stream.\n"
4935   ;
4936 
sayHelp()4937 static void sayHelp(){
4938   printf("%s", zHelp);
4939 }
4940 #endif
4941 
4942 #ifndef U8_TYPEDEF
4943 /* typedef unsigned char u8; */
4944 #define U8_TYPEDEF
4945 #endif
4946 
4947 /* Classify c according to interval within USASCII set w.r.t. base85
4948  * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not.
4949  */
4950 #define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z'))
4951 
4952 /* Provide digitValue to b85Numeral offset as a function of above class. */
4953 static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 };
4954 #define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)]
4955 
4956 /* Say whether c is a base85 numeral. */
4957 #define IS_B85( c ) (B85_CLASS(c) & 1)
4958 
4959 #if 0 /* Not used, */
4960 static u8 base85DigitValue( char c ){
4961   u8 dv = (u8)(c - '#');
4962   if( dv>87 ) return 0xff;
4963   return (dv > 3)? dv-3 : dv;
4964 }
4965 #endif
4966 
4967 /* Width of base64 lines. Should be an integer multiple of 5. */
4968 #define B85_DARK_MAX 80
4969 
4970 
skipNonB85(char * s,int nc)4971 static char * skipNonB85( char *s, int nc ){
4972   char c;
4973   while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s;
4974   return s;
4975 }
4976 
4977 /* Convert small integer, known to be in 0..84 inclusive, to base85 numeral.
4978  * Do not use the macro form with argument expression having a side-effect.*/
4979 #if 0
4980 static char base85Numeral( u8 b ){
4981   return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*');
4982 }
4983 #else
4984 # define base85Numeral( dn )\
4985   ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*')))
4986 #endif
4987 
putcs(char * pc,char * s)4988 static char *putcs(char *pc, char *s){
4989   char c;
4990   while( (c = *s++)!=0 ) *pc++ = c;
4991   return pc;
4992 }
4993 
4994 /* Encode a byte buffer into base85 text. If pSep!=0, it's a C string
4995 ** to be appended to encoded groups to limit their length to B85_DARK_MAX
4996 ** or to terminate the last group (to aid concatenation.)
4997 */
toBase85(u8 * pIn,int nbIn,char * pOut,char * pSep)4998 static char* toBase85( u8 *pIn, int nbIn, char *pOut, char *pSep ){
4999   int nCol = 0;
5000   while( nbIn >= 4 ){
5001     int nco = 5;
5002     unsigned long qbv = (((unsigned long)pIn[0])<<24) |
5003                         (pIn[1]<<16) | (pIn[2]<<8) | pIn[3];
5004     while( nco > 0 ){
5005       unsigned nqv = (unsigned)(qbv/85UL);
5006       unsigned char dv = qbv - 85UL*nqv;
5007       qbv = nqv;
5008       pOut[--nco] = base85Numeral(dv);
5009     }
5010     nbIn -= 4;
5011     pIn += 4;
5012     pOut += 5;
5013     if( pSep && (nCol += 5)>=B85_DARK_MAX ){
5014       pOut = putcs(pOut, pSep);
5015       nCol = 0;
5016     }
5017   }
5018   if( nbIn > 0 ){
5019     int nco = nbIn + 1;
5020     unsigned long qv = *pIn++;
5021     int nbe = 1;
5022     while( nbe++ < nbIn ){
5023       qv = (qv<<8) | *pIn++;
5024     }
5025     nCol += nco;
5026     while( nco > 0 ){
5027       u8 dv = (u8)(qv % 85);
5028       qv /= 85;
5029       pOut[--nco] = base85Numeral(dv);
5030     }
5031     pOut += (nbIn+1);
5032   }
5033   if( pSep && nCol>0 ) pOut = putcs(pOut, pSep);
5034   *pOut = 0;
5035   return pOut;
5036 }
5037 
5038 /* Decode base85 text into a byte buffer. */
fromBase85(char * pIn,int ncIn,u8 * pOut)5039 static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){
5040   if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
5041   while( ncIn>0 ){
5042     static signed char nboi[] = { 0, 0, 1, 2, 3, 4 };
5043     char *pUse = skipNonB85(pIn, ncIn);
5044     unsigned long qv = 0L;
5045     int nti, nbo;
5046     ncIn -= (pUse - pIn);
5047     pIn = pUse;
5048     nti = (ncIn>5)? 5 : ncIn;
5049     nbo = nboi[nti];
5050     if( nbo==0 ) break;
5051     while( nti>0 ){
5052       char c = *pIn++;
5053       u8 cdo = B85_DNOS(c);
5054       --ncIn;
5055       if( cdo==0 ) break;
5056       qv = 85 * qv + (c - cdo);
5057       --nti;
5058     }
5059     nbo -= nti; /* Adjust for early (non-digit) end of group. */
5060     switch( nbo ){
5061     case 4:
5062       *pOut++ = (qv >> 24)&0xff;
5063     case 3:
5064       *pOut++ = (qv >> 16)&0xff;
5065     case 2:
5066       *pOut++ = (qv >> 8)&0xff;
5067     case 1:
5068       *pOut++ = qv&0xff;
5069     case 0:
5070       break;
5071     }
5072   }
5073   return pOut;
5074 }
5075 
5076 #ifndef OMIT_BASE85_CHECKER
5077 /* Say whether input char sequence is all (base85 and/or whitespace).*/
allBase85(char * p,int len)5078 static int allBase85( char *p, int len ){
5079   char c;
5080   while( len-- > 0 && (c = *p++) != 0 ){
5081     if( !IS_B85(c) && !isspace(c) ) return 0;
5082   }
5083   return 1;
5084 }
5085 #endif
5086 
5087 #ifndef BASE85_STANDALONE
5088 
5089 # ifndef OMIT_BASE85_CHECKER
5090 /* This function does the work for the SQLite is_base85(t) UDF. */
is_base85(sqlite3_context * context,int na,sqlite3_value * av[])5091 static void is_base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5092   assert(na==1);
5093   switch( sqlite3_value_type(av[0]) ){
5094   case SQLITE_TEXT:
5095     {
5096       int rv = allBase85( (char *)sqlite3_value_text(av[0]),
5097                           sqlite3_value_bytes(av[0]) );
5098       sqlite3_result_int(context, rv);
5099     }
5100     break;
5101   case SQLITE_NULL:
5102     sqlite3_result_null(context);
5103     break;
5104   default:
5105     sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1);
5106     return;
5107   }
5108 }
5109 # endif
5110 
5111 /* This function does the work for the SQLite base85(x) UDF. */
base85(sqlite3_context * context,int na,sqlite3_value * av[])5112 static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5113   int nb, nc, nv = sqlite3_value_bytes(av[0]);
5114   int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
5115                             SQLITE_LIMIT_LENGTH, -1);
5116   char *cBuf;
5117   u8 *bBuf;
5118   assert(na==1);
5119   switch( sqlite3_value_type(av[0]) ){
5120   case SQLITE_BLOB:
5121     nb = nv;
5122     /*    ulongs    tail   newlines  tailenc+nul*/
5123     nc = 5*(nv/4) + nv%4 + nv/64+1 + 2;
5124     if( nvMax < nc ){
5125       sqlite3_result_error(context, "blob expanded to base85 too big", -1);
5126       return;
5127     }
5128     bBuf = (u8*)sqlite3_value_blob(av[0]);
5129     if( !bBuf ){
5130       if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
5131         goto memFail;
5132       }
5133       sqlite3_result_text(context,"",-1,SQLITE_STATIC);
5134       break;
5135     }
5136     cBuf = sqlite3_malloc(nc);
5137     if( !cBuf ) goto memFail;
5138     nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf);
5139     sqlite3_result_text(context, cBuf, nc, sqlite3_free);
5140     break;
5141   case SQLITE_TEXT:
5142     nc = nv;
5143     nb = 4*(nv/5) + nv%5; /* may overestimate */
5144     if( nvMax < nb ){
5145       sqlite3_result_error(context, "blob from base85 may be too big", -1);
5146       return;
5147     }else if( nb<1 ){
5148       nb = 1;
5149     }
5150     cBuf = (char *)sqlite3_value_text(av[0]);
5151     if( !cBuf ){
5152       if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
5153         goto memFail;
5154       }
5155       sqlite3_result_zeroblob(context, 0);
5156       break;
5157     }
5158     bBuf = sqlite3_malloc(nb);
5159     if( !bBuf ) goto memFail;
5160     nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf);
5161     sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
5162     break;
5163   default:
5164     sqlite3_result_error(context, "base85 accepts only blob or text.", -1);
5165     return;
5166   }
5167   return;
5168  memFail:
5169   sqlite3_result_error(context, "base85 OOM", -1);
5170 }
5171 
5172 /*
5173 ** Establish linkage to running SQLite library.
5174 */
5175 #ifndef SQLITE_SHELL_EXTFUNCS
5176 #ifdef _WIN32
5177 
5178 #endif
sqlite3_base_init(sqlite3 * db,char ** pzErr,const sqlite3_api_routines * pApi)5179 int sqlite3_base_init
5180 #else
5181 static int sqlite3_base85_init
5182 #endif
5183 (sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
5184   SQLITE_EXTENSION_INIT2(pApi);
5185   (void)pzErr;
5186 # ifndef OMIT_BASE85_CHECKER
5187   {
5188     int rc = sqlite3_create_function
5189       (db, "is_base85", 1,
5190        SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_UTF8,
5191        0, is_base85, 0, 0);
5192     if( rc!=SQLITE_OK ) return rc;
5193   }
5194 # endif
5195   return sqlite3_create_function
5196     (db, "base85", 1,
5197      SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
5198      0, base85, 0, 0);
5199 }
5200 
5201 /*
5202 ** Define some macros to allow this extension to be built into the shell
5203 ** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
5204 ** allows shell.c, as distributed, to have this extension built in.
5205 */
5206 # define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0)
5207 # define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
5208 
5209 #else /* standalone program */
5210 
main(int na,char * av[])5211 int main(int na, char *av[]){
5212   int cin;
5213   int rc = 0;
5214   u8 bBuf[4*(B85_DARK_MAX/5)];
5215   char cBuf[5*(sizeof(bBuf)/4)+2];
5216   size_t nio;
5217 # ifndef OMIT_BASE85_CHECKER
5218   int b85Clean = 1;
5219 # endif
5220   char rw;
5221   FILE *fb = 0, *foc = 0;
5222   char fmode[3] = "xb";
5223   if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){
5224     sayHelp();
5225     return 0;
5226   }
5227   fmode[0] = rw;
5228   if( av[2][0]=='-' && av[2][1]==0 ){
5229     switch( rw ){
5230     case 'r':
5231       fb = stdin;
5232       setmode(fileno(stdin), O_BINARY);
5233       break;
5234     case 'w':
5235       fb = stdout;
5236       setmode(fileno(stdout), O_BINARY);
5237       break;
5238     }
5239   }else{
5240     fb = fopen(av[2], fmode);
5241     foc = fb;
5242   }
5243   if( !fb ){
5244     fprintf(stderr, "Cannot open %s for %c\n", av[2], rw);
5245     rc = 1;
5246   }else{
5247     switch( rw ){
5248     case 'r':
5249       while( (nio = fread( bBuf, 1, sizeof(bBuf), fb))>0 ){
5250         toBase85( bBuf, (int)nio, cBuf, 0 );
5251         fprintf(stdout, "%s\n", cBuf);
5252       }
5253       break;
5254     case 'w':
5255       while( 0 != fgets(cBuf, sizeof(cBuf), stdin) ){
5256         int nc = strlen(cBuf);
5257         size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf;
5258         if( 1 != fwrite(bBuf, nbo, 1, fb) ) rc = 1;
5259 # ifndef OMIT_BASE85_CHECKER
5260         b85Clean &= allBase85( cBuf, nc );
5261 # endif
5262       }
5263       break;
5264     default:
5265       sayHelp();
5266       rc = 1;
5267     }
5268     if( foc ) fclose(foc);
5269   }
5270 # ifndef OMIT_BASE85_CHECKER
5271   if( !b85Clean ){
5272     fprintf(stderr, "Base85 input had non-base85 dark or control content.\n");
5273   }
5274 # endif
5275   return rc;
5276 }
5277 
5278 #endif
5279 
5280 /************************* End ../ext/misc/base85.c ********************/
5281 /************************* Begin ../ext/misc/ieee754.c ******************/
5282 /*
5283 ** 2013-04-17
5284 **
5285 ** The author disclaims copyright to this source code.  In place of
5286 ** a legal notice, here is a blessing:
5287 **
5288 **    May you do good and not evil.
5289 **    May you find forgiveness for yourself and forgive others.
5290 **    May you share freely, never taking more than you give.
5291 **
5292 ******************************************************************************
5293 **
5294 ** This SQLite extension implements functions for the exact display
5295 ** and input of IEEE754 Binary64 floating-point numbers.
5296 **
5297 **   ieee754(X)
5298 **   ieee754(Y,Z)
5299 **
5300 ** In the first form, the value X should be a floating-point number.
5301 ** The function will return a string of the form 'ieee754(Y,Z)' where
5302 ** Y and Z are integers such that X==Y*pow(2,Z).
5303 **
5304 ** In the second form, Y and Z are integers which are the mantissa and
5305 ** base-2 exponent of a new floating point number.  The function returns
5306 ** a floating-point value equal to Y*pow(2,Z).
5307 **
5308 ** Examples:
5309 **
5310 **     ieee754(2.0)             ->     'ieee754(2,0)'
5311 **     ieee754(45.25)           ->     'ieee754(181,-2)'
5312 **     ieee754(2, 0)            ->     2.0
5313 **     ieee754(181, -2)         ->     45.25
5314 **
5315 ** Two additional functions break apart the one-argument ieee754()
5316 ** result into separate integer values:
5317 **
5318 **     ieee754_mantissa(45.25)  ->     181
5319 **     ieee754_exponent(45.25)  ->     -2
5320 **
5321 ** These functions convert binary64 numbers into blobs and back again.
5322 **
5323 **     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5324 **     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5325 **
5326 ** In all single-argument functions, if the argument is an 8-byte blob
5327 ** then that blob is interpreted as a big-endian binary64 value.
5328 **
5329 **
5330 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5331 ** -----------------------------------------------
5332 **
5333 ** This extension in combination with the separate 'decimal' extension
5334 ** can be used to compute the exact decimal representation of binary64
5335 ** values.  To begin, first compute a table of exponent values:
5336 **
5337 **    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5338 **    WITH RECURSIVE c(x,v) AS (
5339 **      VALUES(0,'1')
5340 **      UNION ALL
5341 **      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5342 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5343 **    WITH RECURSIVE c(x,v) AS (
5344 **      VALUES(-1,'0.5')
5345 **      UNION ALL
5346 **      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5347 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5348 **
5349 ** Then, to compute the exact decimal representation of a floating
5350 ** point value (the value 47.49 is used in the example) do:
5351 **
5352 **    WITH c(n) AS (VALUES(47.49))
5353 **          ---------------^^^^^---- Replace with whatever you want
5354 **    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5355 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5356 **
5357 ** Here is a query to show various boundry values for the binary64
5358 ** number format:
5359 **
5360 **    WITH c(name,bin) AS (VALUES
5361 **       ('minimum positive value',        x'0000000000000001'),
5362 **       ('maximum subnormal value',       x'000fffffffffffff'),
5363 **       ('mininum positive nornal value', x'0010000000000000'),
5364 **       ('maximum value',                 x'7fefffffffffffff'))
5365 **    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5366 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5367 **
5368 */
5369 /* #include "sqlite3ext.h" */
5370 SQLITE_EXTENSION_INIT1
5371 #include <assert.h>
5372 #include <string.h>
5373 
5374 /* Mark a function parameter as unused, to suppress nuisance compiler
5375 ** warnings. */
5376 #ifndef UNUSED_PARAMETER
5377 # define UNUSED_PARAMETER(X)  (void)(X)
5378 #endif
5379 
5380 /*
5381 ** Implementation of the ieee754() function
5382 */
ieee754func(sqlite3_context * context,int argc,sqlite3_value ** argv)5383 static void ieee754func(
5384   sqlite3_context *context,
5385   int argc,
5386   sqlite3_value **argv
5387 ){
5388   if( argc==1 ){
5389     sqlite3_int64 m, a;
5390     double r;
5391     int e;
5392     int isNeg;
5393     char zResult[100];
5394     assert( sizeof(m)==sizeof(r) );
5395     if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5396      && sqlite3_value_bytes(argv[0])==sizeof(r)
5397     ){
5398       const unsigned char *x = sqlite3_value_blob(argv[0]);
5399       unsigned int i;
5400       sqlite3_uint64 v = 0;
5401       for(i=0; i<sizeof(r); i++){
5402         v = (v<<8) | x[i];
5403       }
5404       memcpy(&r, &v, sizeof(r));
5405     }else{
5406       r = sqlite3_value_double(argv[0]);
5407     }
5408     if( r<0.0 ){
5409       isNeg = 1;
5410       r = -r;
5411     }else{
5412       isNeg = 0;
5413     }
5414     memcpy(&a,&r,sizeof(a));
5415     if( a==0 ){
5416       e = 0;
5417       m = 0;
5418     }else{
5419       e = a>>52;
5420       m = a & ((((sqlite3_int64)1)<<52)-1);
5421       if( e==0 ){
5422         m <<= 1;
5423       }else{
5424         m |= ((sqlite3_int64)1)<<52;
5425       }
5426       while( e<1075 && m>0 && (m&1)==0 ){
5427         m >>= 1;
5428         e++;
5429       }
5430       if( isNeg ) m = -m;
5431     }
5432     switch( *(int*)sqlite3_user_data(context) ){
5433       case 0:
5434         sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5435                          m, e-1075);
5436         sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5437         break;
5438       case 1:
5439         sqlite3_result_int64(context, m);
5440         break;
5441       case 2:
5442         sqlite3_result_int(context, e-1075);
5443         break;
5444     }
5445   }else{
5446     sqlite3_int64 m, e, a;
5447     double r;
5448     int isNeg = 0;
5449     m = sqlite3_value_int64(argv[0]);
5450     e = sqlite3_value_int64(argv[1]);
5451 
5452     /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
5453     if( e>10000 ){
5454       e = 10000;
5455     }else if( e<-10000 ){
5456       e = -10000;
5457     }
5458 
5459     if( m<0 ){
5460       isNeg = 1;
5461       m = -m;
5462       if( m<0 ) return;
5463     }else if( m==0 && e>-1000 && e<1000 ){
5464       sqlite3_result_double(context, 0.0);
5465       return;
5466     }
5467     while( (m>>32)&0xffe00000 ){
5468       m >>= 1;
5469       e++;
5470     }
5471     while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5472       m <<= 1;
5473       e--;
5474     }
5475     e += 1075;
5476     if( e<=0 ){
5477       /* Subnormal */
5478       if( 1-e >= 64 ){
5479         m = 0;
5480       }else{
5481         m >>= 1-e;
5482       }
5483       e = 0;
5484     }else if( e>0x7ff ){
5485       e = 0x7ff;
5486     }
5487     a = m & ((((sqlite3_int64)1)<<52)-1);
5488     a |= e<<52;
5489     if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5490     memcpy(&r, &a, sizeof(r));
5491     sqlite3_result_double(context, r);
5492   }
5493 }
5494 
5495 /*
5496 ** Functions to convert between blobs and floats.
5497 */
ieee754func_from_blob(sqlite3_context * context,int argc,sqlite3_value ** argv)5498 static void ieee754func_from_blob(
5499   sqlite3_context *context,
5500   int argc,
5501   sqlite3_value **argv
5502 ){
5503   UNUSED_PARAMETER(argc);
5504   if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5505    && sqlite3_value_bytes(argv[0])==sizeof(double)
5506   ){
5507     double r;
5508     const unsigned char *x = sqlite3_value_blob(argv[0]);
5509     unsigned int i;
5510     sqlite3_uint64 v = 0;
5511     for(i=0; i<sizeof(r); i++){
5512       v = (v<<8) | x[i];
5513     }
5514     memcpy(&r, &v, sizeof(r));
5515     sqlite3_result_double(context, r);
5516   }
5517 }
ieee754func_to_blob(sqlite3_context * context,int argc,sqlite3_value ** argv)5518 static void ieee754func_to_blob(
5519   sqlite3_context *context,
5520   int argc,
5521   sqlite3_value **argv
5522 ){
5523   UNUSED_PARAMETER(argc);
5524   if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5525    || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5526   ){
5527     double r = sqlite3_value_double(argv[0]);
5528     sqlite3_uint64 v;
5529     unsigned char a[sizeof(r)];
5530     unsigned int i;
5531     memcpy(&v, &r, sizeof(r));
5532     for(i=1; i<=sizeof(r); i++){
5533       a[sizeof(r)-i] = v&0xff;
5534       v >>= 8;
5535     }
5536     sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5537   }
5538 }
5539 
5540 /*
5541 ** SQL Function:   ieee754_inc(r,N)
5542 **
5543 ** Move the floating point value r by N quantums and return the new
5544 ** values.
5545 **
5546 ** Behind the scenes: this routine merely casts r into a 64-bit unsigned
5547 ** integer, adds N, then casts the value back into float.
5548 **
5549 ** Example:  To find the smallest positive number:
5550 **
5551 **     SELECT ieee754_inc(0.0,+1);
5552 */
ieee754inc(sqlite3_context * context,int argc,sqlite3_value ** argv)5553 static void ieee754inc(
5554   sqlite3_context *context,
5555   int argc,
5556   sqlite3_value **argv
5557 ){
5558   double r;
5559   sqlite3_int64 N;
5560   sqlite3_uint64 m1, m2;
5561   double r2;
5562   UNUSED_PARAMETER(argc);
5563   r = sqlite3_value_double(argv[0]);
5564   N = sqlite3_value_int64(argv[1]);
5565   memcpy(&m1, &r, 8);
5566   m2 = m1 + N;
5567   memcpy(&r2, &m2, 8);
5568   sqlite3_result_double(context, r2);
5569 }
5570 
5571 
5572 #ifdef _WIN32
5573 
5574 #endif
sqlite3_ieee_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)5575 int sqlite3_ieee_init(
5576   sqlite3 *db,
5577   char **pzErrMsg,
5578   const sqlite3_api_routines *pApi
5579 ){
5580   static const struct {
5581     char *zFName;
5582     int nArg;
5583     int iAux;
5584     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5585   } aFunc[] = {
5586     { "ieee754",           1,   0, ieee754func },
5587     { "ieee754",           2,   0, ieee754func },
5588     { "ieee754_mantissa",  1,   1, ieee754func },
5589     { "ieee754_exponent",  1,   2, ieee754func },
5590     { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5591     { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5592     { "ieee754_inc",       2,   0, ieee754inc  },
5593   };
5594   unsigned int i;
5595   int rc = SQLITE_OK;
5596   SQLITE_EXTENSION_INIT2(pApi);
5597   (void)pzErrMsg;  /* Unused parameter */
5598   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5599     rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5600                                SQLITE_UTF8|SQLITE_INNOCUOUS,
5601                                (void*)&aFunc[i].iAux,
5602                                aFunc[i].xFunc, 0, 0);
5603   }
5604   return rc;
5605 }
5606 
5607 /************************* End ../ext/misc/ieee754.c ********************/
5608 /************************* Begin ../ext/misc/series.c ******************/
5609 /*
5610 ** 2015-08-18, 2023-04-28
5611 **
5612 ** The author disclaims copyright to this source code.  In place of
5613 ** a legal notice, here is a blessing:
5614 **
5615 **    May you do good and not evil.
5616 **    May you find forgiveness for yourself and forgive others.
5617 **    May you share freely, never taking more than you give.
5618 **
5619 *************************************************************************
5620 **
5621 ** This file demonstrates how to create a table-valued-function using
5622 ** a virtual table.  This demo implements the generate_series() function
5623 ** which gives the same results as the eponymous function in PostgreSQL,
5624 ** within the limitation that its arguments are signed 64-bit integers.
5625 **
5626 ** Considering its equivalents to generate_series(start,stop,step): A
5627 ** value V[n] sequence is produced for integer n ascending from 0 where
5628 **  ( V[n] == start + n * step  &&  sgn(V[n] - stop) * sgn(step) >= 0 )
5629 ** for each produced value (independent of production time ordering.)
5630 **
5631 ** All parameters must be either integer or convertable to integer.
5632 ** The start parameter is required.
5633 ** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
5634 ** The step parameter defaults to 1 and 0 is treated as 1.
5635 **
5636 ** Examples:
5637 **
5638 **      SELECT * FROM generate_series(0,100,5);
5639 **
5640 ** The query above returns integers from 0 through 100 counting by steps
5641 ** of 5.
5642 **
5643 **      SELECT * FROM generate_series(0,100);
5644 **
5645 ** Integers from 0 through 100 with a step size of 1.
5646 **
5647 **      SELECT * FROM generate_series(20) LIMIT 10;
5648 **
5649 ** Integers 20 through 29.
5650 **
5651 **      SELECT * FROM generate_series(0,-100,-5);
5652 **
5653 ** Integers 0 -5 -10 ... -100.
5654 **
5655 **      SELECT * FROM generate_series(0,-1);
5656 **
5657 ** Empty sequence.
5658 **
5659 ** HOW IT WORKS
5660 **
5661 ** The generate_series "function" is really a virtual table with the
5662 ** following schema:
5663 **
5664 **     CREATE TABLE generate_series(
5665 **       value,
5666 **       start HIDDEN,
5667 **       stop HIDDEN,
5668 **       step HIDDEN
5669 **     );
5670 **
5671 ** The virtual table also has a rowid, logically equivalent to n+1 where
5672 ** "n" is the ascending integer in the aforesaid production definition.
5673 **
5674 ** Function arguments in queries against this virtual table are translated
5675 ** into equality constraints against successive hidden columns.  In other
5676 ** words, the following pairs of queries are equivalent to each other:
5677 **
5678 **    SELECT * FROM generate_series(0,100,5);
5679 **    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5680 **
5681 **    SELECT * FROM generate_series(0,100);
5682 **    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5683 **
5684 **    SELECT * FROM generate_series(20) LIMIT 10;
5685 **    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5686 **
5687 ** The generate_series virtual table implementation leaves the xCreate method
5688 ** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5689 ** TABLE command with "generate_series" as the USING argument.  Instead, there
5690 ** is a single generate_series virtual table that is always available without
5691 ** having to be created first.
5692 **
5693 ** The xBestIndex method looks for equality constraints against the hidden
5694 ** start, stop, and step columns, and if present, it uses those constraints
5695 ** to bound the sequence of generated values.  If the equality constraints
5696 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5697 ** xBestIndex returns a small cost when both start and stop are available,
5698 ** and a very large cost if either start or stop are unavailable.  This
5699 ** encourages the query planner to order joins such that the bounds of the
5700 ** series are well-defined.
5701 */
5702 /* #include "sqlite3ext.h" */
5703 SQLITE_EXTENSION_INIT1
5704 #include <assert.h>
5705 #include <string.h>
5706 #include <limits.h>
5707 
5708 #ifndef SQLITE_OMIT_VIRTUALTABLE
5709 /*
5710 ** Return that member of a generate_series(...) sequence whose 0-based
5711 ** index is ix. The 0th member is given by smBase. The sequence members
5712 ** progress per ix increment by smStep.
5713 */
genSeqMember(sqlite3_int64 smBase,sqlite3_int64 smStep,sqlite3_uint64 ix)5714 static sqlite3_int64 genSeqMember(sqlite3_int64 smBase,
5715                                   sqlite3_int64 smStep,
5716                                   sqlite3_uint64 ix){
5717   if( ix>=(sqlite3_uint64)LLONG_MAX ){
5718     /* Get ix into signed i64 range. */
5719     ix -= (sqlite3_uint64)LLONG_MAX;
5720     /* With 2's complement ALU, this next can be 1 step, but is split into
5721      * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
5722     smBase += (LLONG_MAX/2) * smStep;
5723     smBase += (LLONG_MAX - LLONG_MAX/2) * smStep;
5724   }
5725   /* Under UBSAN (or on 1's complement machines), must do this last term
5726    * in steps to avoid the dreaded (and harmless) signed multiply overlow. */
5727   if( ix>=2 ){
5728     sqlite3_int64 ix2 = (sqlite3_int64)ix/2;
5729     smBase += ix2*smStep;
5730     ix -= ix2;
5731   }
5732   return smBase + ((sqlite3_int64)ix)*smStep;
5733 }
5734 
5735 /* typedef unsigned char u8; */
5736 
5737 typedef struct SequenceSpec {
5738   sqlite3_int64 iBase;         /* Starting value ("start") */
5739   sqlite3_int64 iTerm;         /* Given terminal value ("stop") */
5740   sqlite3_int64 iStep;         /* Increment ("step") */
5741   sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
5742   sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
5743   sqlite3_int64 iValueNow;     /* Current value during generation */
5744   u8 isNotEOF;                 /* Sequence generation not exhausted */
5745   u8 isReversing;              /* Sequence is being reverse generated */
5746 } SequenceSpec;
5747 
5748 /*
5749 ** Prepare a SequenceSpec for use in generating an integer series
5750 ** given initialized iBase, iTerm and iStep values. Sequence is
5751 ** initialized per given isReversing. Other members are computed.
5752 */
setupSequence(SequenceSpec * pss)5753 static void setupSequence( SequenceSpec *pss ){
5754   int bSameSigns;
5755   pss->uSeqIndexMax = 0;
5756   pss->isNotEOF = 0;
5757   bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0);
5758   if( pss->iTerm < pss->iBase ){
5759     sqlite3_uint64 nuspan = 0;
5760     if( bSameSigns ){
5761       nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm);
5762     }else{
5763       /* Under UBSAN (or on 1's complement machines), must do this in steps.
5764        * In this clause, iBase>=0 and iTerm<0 . */
5765       nuspan = 1;
5766       nuspan += pss->iBase;
5767       nuspan += -(pss->iTerm+1);
5768     }
5769     if( pss->iStep<0 ){
5770       pss->isNotEOF = 1;
5771       if( nuspan==ULONG_MAX ){
5772         pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1;
5773       }else if( pss->iStep>LLONG_MIN ){
5774         pss->uSeqIndexMax = nuspan/-pss->iStep;
5775       }
5776     }
5777   }else if( pss->iTerm > pss->iBase ){
5778     sqlite3_uint64 puspan = 0;
5779     if( bSameSigns ){
5780       puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase);
5781     }else{
5782       /* Under UBSAN (or on 1's complement machines), must do this in steps.
5783        * In this clause, iTerm>=0 and iBase<0 . */
5784       puspan = 1;
5785       puspan += pss->iTerm;
5786       puspan += -(pss->iBase+1);
5787     }
5788     if( pss->iStep>0 ){
5789       pss->isNotEOF = 1;
5790       pss->uSeqIndexMax = puspan/pss->iStep;
5791     }
5792   }else if( pss->iTerm == pss->iBase ){
5793       pss->isNotEOF = 1;
5794       pss->uSeqIndexMax = 0;
5795   }
5796   pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0;
5797   pss->iValueNow = (pss->isReversing)
5798     ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax)
5799     : pss->iBase;
5800 }
5801 
5802 /*
5803 ** Progress sequence generator to yield next value, if any.
5804 ** Leave its state to either yield next value or be at EOF.
5805 ** Return whether there is a next value, or 0 at EOF.
5806 */
progressSequence(SequenceSpec * pss)5807 static int progressSequence( SequenceSpec *pss ){
5808   if( !pss->isNotEOF ) return 0;
5809   if( pss->isReversing ){
5810     if( pss->uSeqIndexNow > 0 ){
5811       pss->uSeqIndexNow--;
5812       pss->iValueNow -= pss->iStep;
5813     }else{
5814       pss->isNotEOF = 0;
5815     }
5816   }else{
5817     if( pss->uSeqIndexNow < pss->uSeqIndexMax ){
5818       pss->uSeqIndexNow++;
5819       pss->iValueNow += pss->iStep;
5820     }else{
5821       pss->isNotEOF = 0;
5822     }
5823   }
5824   return pss->isNotEOF;
5825 }
5826 
5827 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5828 ** serve as the underlying representation of a cursor that scans
5829 ** over rows of the result
5830 */
5831 typedef struct series_cursor series_cursor;
5832 struct series_cursor {
5833   sqlite3_vtab_cursor base;  /* Base class - must be first */
5834   SequenceSpec ss;           /* (this) Derived class data */
5835 };
5836 
5837 /*
5838 ** The seriesConnect() method is invoked to create a new
5839 ** series_vtab that describes the generate_series virtual table.
5840 **
5841 ** Think of this routine as the constructor for series_vtab objects.
5842 **
5843 ** All this routine needs to do is:
5844 **
5845 **    (1) Allocate the series_vtab object and initialize all fields.
5846 **
5847 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5848 **        result set of queries against generate_series will look like.
5849 */
seriesConnect(sqlite3 * db,void * pUnused,int argcUnused,const char * const * argvUnused,sqlite3_vtab ** ppVtab,char ** pzErrUnused)5850 static int seriesConnect(
5851   sqlite3 *db,
5852   void *pUnused,
5853   int argcUnused, const char *const*argvUnused,
5854   sqlite3_vtab **ppVtab,
5855   char **pzErrUnused
5856 ){
5857   sqlite3_vtab *pNew;
5858   int rc;
5859 
5860 /* Column numbers */
5861 #define SERIES_COLUMN_VALUE 0
5862 #define SERIES_COLUMN_START 1
5863 #define SERIES_COLUMN_STOP  2
5864 #define SERIES_COLUMN_STEP  3
5865 
5866   (void)pUnused;
5867   (void)argcUnused;
5868   (void)argvUnused;
5869   (void)pzErrUnused;
5870   rc = sqlite3_declare_vtab(db,
5871      "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5872   if( rc==SQLITE_OK ){
5873     pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5874     if( pNew==0 ) return SQLITE_NOMEM;
5875     memset(pNew, 0, sizeof(*pNew));
5876     sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5877   }
5878   return rc;
5879 }
5880 
5881 /*
5882 ** This method is the destructor for series_cursor objects.
5883 */
seriesDisconnect(sqlite3_vtab * pVtab)5884 static int seriesDisconnect(sqlite3_vtab *pVtab){
5885   sqlite3_free(pVtab);
5886   return SQLITE_OK;
5887 }
5888 
5889 /*
5890 ** Constructor for a new series_cursor object.
5891 */
seriesOpen(sqlite3_vtab * pUnused,sqlite3_vtab_cursor ** ppCursor)5892 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5893   series_cursor *pCur;
5894   (void)pUnused;
5895   pCur = sqlite3_malloc( sizeof(*pCur) );
5896   if( pCur==0 ) return SQLITE_NOMEM;
5897   memset(pCur, 0, sizeof(*pCur));
5898   *ppCursor = &pCur->base;
5899   return SQLITE_OK;
5900 }
5901 
5902 /*
5903 ** Destructor for a series_cursor.
5904 */
seriesClose(sqlite3_vtab_cursor * cur)5905 static int seriesClose(sqlite3_vtab_cursor *cur){
5906   sqlite3_free(cur);
5907   return SQLITE_OK;
5908 }
5909 
5910 
5911 /*
5912 ** Advance a series_cursor to its next row of output.
5913 */
seriesNext(sqlite3_vtab_cursor * cur)5914 static int seriesNext(sqlite3_vtab_cursor *cur){
5915   series_cursor *pCur = (series_cursor*)cur;
5916   progressSequence( & pCur->ss );
5917   return SQLITE_OK;
5918 }
5919 
5920 /*
5921 ** Return values of columns for the row at which the series_cursor
5922 ** is currently pointing.
5923 */
seriesColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)5924 static int seriesColumn(
5925   sqlite3_vtab_cursor *cur,   /* The cursor */
5926   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5927   int i                       /* Which column to return */
5928 ){
5929   series_cursor *pCur = (series_cursor*)cur;
5930   sqlite3_int64 x = 0;
5931   switch( i ){
5932     case SERIES_COLUMN_START:  x = pCur->ss.iBase; break;
5933     case SERIES_COLUMN_STOP:   x = pCur->ss.iTerm; break;
5934     case SERIES_COLUMN_STEP:   x = pCur->ss.iStep;   break;
5935     default:                   x = pCur->ss.iValueNow;  break;
5936   }
5937   sqlite3_result_int64(ctx, x);
5938   return SQLITE_OK;
5939 }
5940 
5941 #ifndef LARGEST_UINT64
5942 #define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
5943 #endif
5944 
5945 /*
5946 ** Return the rowid for the current row, logically equivalent to n+1 where
5947 ** "n" is the ascending integer in the aforesaid production definition.
5948 */
seriesRowid(sqlite3_vtab_cursor * cur,sqlite_int64 * pRowid)5949 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5950   series_cursor *pCur = (series_cursor*)cur;
5951   sqlite3_uint64 n = pCur->ss.uSeqIndexNow;
5952   *pRowid = (sqlite3_int64)((n<LARGEST_UINT64)? n+1 : 0);
5953   return SQLITE_OK;
5954 }
5955 
5956 /*
5957 ** Return TRUE if the cursor has been moved off of the last
5958 ** row of output.
5959 */
seriesEof(sqlite3_vtab_cursor * cur)5960 static int seriesEof(sqlite3_vtab_cursor *cur){
5961   series_cursor *pCur = (series_cursor*)cur;
5962   return !pCur->ss.isNotEOF;
5963 }
5964 
5965 /* True to cause run-time checking of the start=, stop=, and/or step=
5966 ** parameters.  The only reason to do this is for testing the
5967 ** constraint checking logic for virtual tables in the SQLite core.
5968 */
5969 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5970 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5971 #endif
5972 
5973 /*
5974 ** This method is called to "rewind" the series_cursor object back
5975 ** to the first row of output.  This method is always called at least
5976 ** once prior to any call to seriesColumn() or seriesRowid() or
5977 ** seriesEof().
5978 **
5979 ** The query plan selected by seriesBestIndex is passed in the idxNum
5980 ** parameter.  (idxStr is not used in this implementation.)  idxNum
5981 ** is a bitmask showing which constraints are available:
5982 **
5983 **    1:    start=VALUE
5984 **    2:    stop=VALUE
5985 **    4:    step=VALUE
5986 **
5987 ** Also, if bit 8 is set, that means that the series should be output
5988 ** in descending order rather than in ascending order.  If bit 16 is
5989 ** set, then output must appear in ascending order.
5990 **
5991 ** This routine should initialize the cursor and position it so that it
5992 ** is pointing at the first row, or pointing off the end of the table
5993 ** (so that seriesEof() will return true) if the table is empty.
5994 */
seriesFilter(sqlite3_vtab_cursor * pVtabCursor,int idxNum,const char * idxStrUnused,int argc,sqlite3_value ** argv)5995 static int seriesFilter(
5996   sqlite3_vtab_cursor *pVtabCursor,
5997   int idxNum, const char *idxStrUnused,
5998   int argc, sqlite3_value **argv
5999 ){
6000   series_cursor *pCur = (series_cursor *)pVtabCursor;
6001   int i = 0;
6002   (void)idxStrUnused;
6003   if( idxNum & 1 ){
6004     pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6005   }else{
6006     pCur->ss.iBase = 0;
6007   }
6008   if( idxNum & 2 ){
6009     pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6010   }else{
6011     pCur->ss.iTerm = 0xffffffff;
6012   }
6013   if( idxNum & 4 ){
6014     pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6015     if( pCur->ss.iStep==0 ){
6016       pCur->ss.iStep = 1;
6017     }else if( pCur->ss.iStep<0 ){
6018       if( (idxNum & 16)==0 ) idxNum |= 8;
6019     }
6020   }else{
6021     pCur->ss.iStep = 1;
6022   }
6023   for(i=0; i<argc; i++){
6024     if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6025       /* If any of the constraints have a NULL value, then return no rows.
6026       ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
6027       pCur->ss.iBase = 1;
6028       pCur->ss.iTerm = 0;
6029       pCur->ss.iStep = 1;
6030       break;
6031     }
6032   }
6033   if( idxNum & 8 ){
6034     pCur->ss.isReversing = pCur->ss.iStep > 0;
6035   }else{
6036     pCur->ss.isReversing = pCur->ss.iStep < 0;
6037   }
6038   setupSequence( &pCur->ss );
6039   return SQLITE_OK;
6040 }
6041 
6042 /*
6043 ** SQLite will invoke this method one or more times while planning a query
6044 ** that uses the generate_series virtual table.  This routine needs to create
6045 ** a query plan for each invocation and compute an estimated cost for that
6046 ** plan.
6047 **
6048 ** In this implementation idxNum is used to represent the
6049 ** query plan.  idxStr is unused.
6050 **
6051 ** The query plan is represented by bits in idxNum:
6052 **
6053 **  (1)  start = $value  -- constraint exists
6054 **  (2)  stop = $value   -- constraint exists
6055 **  (4)  step = $value   -- constraint exists
6056 **  (8)  output in descending order
6057 */
seriesBestIndex(sqlite3_vtab * pVTab,sqlite3_index_info * pIdxInfo)6058 static int seriesBestIndex(
6059   sqlite3_vtab *pVTab,
6060   sqlite3_index_info *pIdxInfo
6061 ){
6062   int i, j;              /* Loop over constraints */
6063   int idxNum = 0;        /* The query plan bitmask */
6064   int bStartSeen = 0;    /* EQ constraint seen on the START column */
6065   int unusableMask = 0;  /* Mask of unusable constraints */
6066   int nArg = 0;          /* Number of arguments that seriesFilter() expects */
6067   int aIdx[3];           /* Constraints on start, stop, and step */
6068   const struct sqlite3_index_constraint *pConstraint;
6069 
6070   /* This implementation assumes that the start, stop, and step columns
6071   ** are the last three columns in the virtual table. */
6072   assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
6073   assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
6074 
6075   aIdx[0] = aIdx[1] = aIdx[2] = -1;
6076   pConstraint = pIdxInfo->aConstraint;
6077   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
6078     int iCol;    /* 0 for start, 1 for stop, 2 for step */
6079     int iMask;   /* bitmask for those column */
6080     if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
6081     iCol = pConstraint->iColumn - SERIES_COLUMN_START;
6082     assert( iCol>=0 && iCol<=2 );
6083     iMask = 1 << iCol;
6084     if( iCol==0 ) bStartSeen = 1;
6085     if( pConstraint->usable==0 ){
6086       unusableMask |=  iMask;
6087       continue;
6088     }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6089       idxNum |= iMask;
6090       aIdx[iCol] = i;
6091     }
6092   }
6093   for(i=0; i<3; i++){
6094     if( (j = aIdx[i])>=0 ){
6095       pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
6096       pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
6097     }
6098   }
6099   /* The current generate_column() implementation requires at least one
6100   ** argument (the START value).  Legacy versions assumed START=0 if the
6101   ** first argument was omitted.  Compile with -DZERO_ARGUMENT_GENERATE_SERIES
6102   ** to obtain the legacy behavior */
6103 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6104   if( !bStartSeen ){
6105     sqlite3_free(pVTab->zErrMsg);
6106     pVTab->zErrMsg = sqlite3_mprintf(
6107         "first argument to \"generate_series()\" missing or unusable");
6108     return SQLITE_ERROR;
6109   }
6110 #endif
6111   if( (unusableMask & ~idxNum)!=0 ){
6112     /* The start, stop, and step columns are inputs.  Therefore if there
6113     ** are unusable constraints on any of start, stop, or step then
6114     ** this plan is unusable */
6115     return SQLITE_CONSTRAINT;
6116   }
6117   if( (idxNum & 3)==3 ){
6118     /* Both start= and stop= boundaries are available.  This is the
6119     ** the preferred case */
6120     pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
6121     pIdxInfo->estimatedRows = 1000;
6122     if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
6123       if( pIdxInfo->aOrderBy[0].desc ){
6124         idxNum |= 8;
6125       }else{
6126         idxNum |= 16;
6127       }
6128       pIdxInfo->orderByConsumed = 1;
6129     }
6130   }else{
6131     /* If either boundary is missing, we have to generate a huge span
6132     ** of numbers.  Make this case very expensive so that the query
6133     ** planner will work hard to avoid it. */
6134     pIdxInfo->estimatedRows = 2147483647;
6135   }
6136   pIdxInfo->idxNum = idxNum;
6137   return SQLITE_OK;
6138 }
6139 
6140 /*
6141 ** This following structure defines all the methods for the
6142 ** generate_series virtual table.
6143 */
6144 static sqlite3_module seriesModule = {
6145   0,                         /* iVersion */
6146   0,                         /* xCreate */
6147   seriesConnect,             /* xConnect */
6148   seriesBestIndex,           /* xBestIndex */
6149   seriesDisconnect,          /* xDisconnect */
6150   0,                         /* xDestroy */
6151   seriesOpen,                /* xOpen - open a cursor */
6152   seriesClose,               /* xClose - close a cursor */
6153   seriesFilter,              /* xFilter - configure scan constraints */
6154   seriesNext,                /* xNext - advance a cursor */
6155   seriesEof,                 /* xEof - check for end of scan */
6156   seriesColumn,              /* xColumn - read data */
6157   seriesRowid,               /* xRowid - read data */
6158   0,                         /* xUpdate */
6159   0,                         /* xBegin */
6160   0,                         /* xSync */
6161   0,                         /* xCommit */
6162   0,                         /* xRollback */
6163   0,                         /* xFindMethod */
6164   0,                         /* xRename */
6165   0,                         /* xSavepoint */
6166   0,                         /* xRelease */
6167   0,                         /* xRollbackTo */
6168   0,                         /* xShadowName */
6169   0                          /* xIntegrity */
6170 };
6171 
6172 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6173 
6174 #ifdef _WIN32
6175 
6176 #endif
sqlite3_series_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)6177 int sqlite3_series_init(
6178   sqlite3 *db,
6179   char **pzErrMsg,
6180   const sqlite3_api_routines *pApi
6181 ){
6182   int rc = SQLITE_OK;
6183   SQLITE_EXTENSION_INIT2(pApi);
6184 #ifndef SQLITE_OMIT_VIRTUALTABLE
6185   if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
6186     *pzErrMsg = sqlite3_mprintf(
6187         "generate_series() requires SQLite 3.8.12 or later");
6188     return SQLITE_ERROR;
6189   }
6190   rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
6191 #endif
6192   return rc;
6193 }
6194 
6195 /************************* End ../ext/misc/series.c ********************/
6196 /************************* Begin ../ext/misc/regexp.c ******************/
6197 /*
6198 ** 2012-11-13
6199 **
6200 ** The author disclaims copyright to this source code.  In place of
6201 ** a legal notice, here is a blessing:
6202 **
6203 **    May you do good and not evil.
6204 **    May you find forgiveness for yourself and forgive others.
6205 **    May you share freely, never taking more than you give.
6206 **
6207 ******************************************************************************
6208 **
6209 ** The code in this file implements a compact but reasonably
6210 ** efficient regular-expression matcher for posix extended regular
6211 ** expressions against UTF8 text.
6212 **
6213 ** This file is an SQLite extension.  It registers a single function
6214 ** named "regexp(A,B)" where A is the regular expression and B is the
6215 ** string to be matched.  By registering this function, SQLite will also
6216 ** then implement the "B regexp A" operator.  Note that with the function
6217 ** the regular expression comes first, but with the operator it comes
6218 ** second.
6219 **
6220 **  The following regular expression syntax is supported:
6221 **
6222 **     X*      zero or more occurrences of X
6223 **     X+      one or more occurrences of X
6224 **     X?      zero or one occurrences of X
6225 **     X{p,q}  between p and q occurrences of X
6226 **     (X)     match X
6227 **     X|Y     X or Y
6228 **     ^X      X occurring at the beginning of the string
6229 **     X$      X occurring at the end of the string
6230 **     .       Match any single character
6231 **     \c      Character c where c is one of \{}()[]|*+?.
6232 **     \c      C-language escapes for c in afnrtv.  ex: \t or \n
6233 **     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
6234 **     \xXX    Where XX is exactly 2 hex digits, unicode value XX
6235 **     [abc]   Any single character from the set abc
6236 **     [^abc]  Any single character not in the set abc
6237 **     [a-z]   Any single character in the range a-z
6238 **     [^a-z]  Any single character not in the range a-z
6239 **     \b      Word boundary
6240 **     \w      Word character.  [A-Za-z0-9_]
6241 **     \W      Non-word character
6242 **     \d      Digit
6243 **     \D      Non-digit
6244 **     \s      Whitespace character
6245 **     \S      Non-whitespace character
6246 **
6247 ** A nondeterministic finite automaton (NFA) is used for matching, so the
6248 ** performance is bounded by O(N*M) where N is the size of the regular
6249 ** expression and M is the size of the input string.  The matcher never
6250 ** exhibits exponential behavior.  Note that the X{p,q} operator expands
6251 ** to p copies of X following by q-p copies of X? and that the size of the
6252 ** regular expression in the O(N*M) performance bound is computed after
6253 ** this expansion.
6254 */
6255 #include <string.h>
6256 #include <stdlib.h>
6257 /* #include "sqlite3ext.h" */
6258 SQLITE_EXTENSION_INIT1
6259 
6260 /*
6261 ** The following #defines change the names of some functions implemented in
6262 ** this file to prevent name collisions with C-library functions of the
6263 ** same name.
6264 */
6265 #define re_match   sqlite3re_match
6266 #define re_compile sqlite3re_compile
6267 #define re_free    sqlite3re_free
6268 
6269 /* The end-of-input character */
6270 #define RE_EOF            0    /* End of input */
6271 #define RE_START  0xfffffff    /* Start of input - larger than an UTF-8 */
6272 
6273 /* The NFA is implemented as sequence of opcodes taken from the following
6274 ** set.  Each opcode has a single integer argument.
6275 */
6276 #define RE_OP_MATCH       1    /* Match the one character in the argument */
6277 #define RE_OP_ANY         2    /* Match any one character.  (Implements ".") */
6278 #define RE_OP_ANYSTAR     3    /* Special optimized version of .* */
6279 #define RE_OP_FORK        4    /* Continue to both next and opcode at iArg */
6280 #define RE_OP_GOTO        5    /* Jump to opcode at iArg */
6281 #define RE_OP_ACCEPT      6    /* Halt and indicate a successful match */
6282 #define RE_OP_CC_INC      7    /* Beginning of a [...] character class */
6283 #define RE_OP_CC_EXC      8    /* Beginning of a [^...] character class */
6284 #define RE_OP_CC_VALUE    9    /* Single value in a character class */
6285 #define RE_OP_CC_RANGE   10    /* Range of values in a character class */
6286 #define RE_OP_WORD       11    /* Perl word character [A-Za-z0-9_] */
6287 #define RE_OP_NOTWORD    12    /* Not a perl word character */
6288 #define RE_OP_DIGIT      13    /* digit:  [0-9] */
6289 #define RE_OP_NOTDIGIT   14    /* Not a digit */
6290 #define RE_OP_SPACE      15    /* space:  [ \t\n\r\v\f] */
6291 #define RE_OP_NOTSPACE   16    /* Not a digit */
6292 #define RE_OP_BOUNDARY   17    /* Boundary between word and non-word */
6293 #define RE_OP_ATSTART    18    /* Currently at the start of the string */
6294 
6295 #if defined(SQLITE_DEBUG)
6296 /* Opcode names used for symbolic debugging */
6297 static const char *ReOpName[] = {
6298   "EOF",
6299   "MATCH",
6300   "ANY",
6301   "ANYSTAR",
6302   "FORK",
6303   "GOTO",
6304   "ACCEPT",
6305   "CC_INC",
6306   "CC_EXC",
6307   "CC_VALUE",
6308   "CC_RANGE",
6309   "WORD",
6310   "NOTWORD",
6311   "DIGIT",
6312   "NOTDIGIT",
6313   "SPACE",
6314   "NOTSPACE",
6315   "BOUNDARY",
6316   "ATSTART",
6317 };
6318 #endif /* SQLITE_DEBUG */
6319 
6320 
6321 /* Each opcode is a "state" in the NFA */
6322 typedef unsigned short ReStateNumber;
6323 
6324 /* Because this is an NFA and not a DFA, multiple states can be active at
6325 ** once.  An instance of the following object records all active states in
6326 ** the NFA.  The implementation is optimized for the common case where the
6327 ** number of actives states is small.
6328 */
6329 typedef struct ReStateSet {
6330   unsigned nState;            /* Number of current states */
6331   ReStateNumber *aState;      /* Current states */
6332 } ReStateSet;
6333 
6334 /* An input string read one character at a time.
6335 */
6336 typedef struct ReInput ReInput;
6337 struct ReInput {
6338   const unsigned char *z;  /* All text */
6339   int i;                   /* Next byte to read */
6340   int mx;                  /* EOF when i>=mx */
6341 };
6342 
6343 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6344 ** an instance of the following object.
6345 */
6346 typedef struct ReCompiled ReCompiled;
6347 struct ReCompiled {
6348   ReInput sIn;                /* Regular expression text */
6349   const char *zErr;           /* Error message to return */
6350   char *aOp;                  /* Operators for the virtual machine */
6351   int *aArg;                  /* Arguments to each operator */
6352   unsigned (*xNextChar)(ReInput*);  /* Next character function */
6353   unsigned char zInit[12];    /* Initial text to match */
6354   int nInit;                  /* Number of bytes in zInit */
6355   unsigned nState;            /* Number of entries in aOp[] and aArg[] */
6356   unsigned nAlloc;            /* Slots allocated for aOp[] and aArg[] */
6357 };
6358 
6359 /* Add a state to the given state set if it is not already there */
re_add_state(ReStateSet * pSet,int newState)6360 static void re_add_state(ReStateSet *pSet, int newState){
6361   unsigned i;
6362   for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6363   pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6364 }
6365 
6366 /* Extract the next unicode character from *pzIn and return it.  Advance
6367 ** *pzIn to the first byte past the end of the character returned.  To
6368 ** be clear:  this routine converts utf8 to unicode.  This routine is
6369 ** optimized for the common case where the next character is a single byte.
6370 */
re_next_char(ReInput * p)6371 static unsigned re_next_char(ReInput *p){
6372   unsigned c;
6373   if( p->i>=p->mx ) return 0;
6374   c = p->z[p->i++];
6375   if( c>=0x80 ){
6376     if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6377       c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6378       if( c<0x80 ) c = 0xfffd;
6379     }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6380            && (p->z[p->i+1]&0xc0)==0x80 ){
6381       c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6382       p->i += 2;
6383       if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6384     }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80
6385            && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6386       c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6387                        | (p->z[p->i+2]&0x3f);
6388       p->i += 3;
6389       if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6390     }else{
6391       c = 0xfffd;
6392     }
6393   }
6394   return c;
6395 }
re_next_char_nocase(ReInput * p)6396 static unsigned re_next_char_nocase(ReInput *p){
6397   unsigned c = re_next_char(p);
6398   if( c>='A' && c<='Z' ) c += 'a' - 'A';
6399   return c;
6400 }
6401 
6402 /* Return true if c is a perl "word" character:  [A-Za-z0-9_] */
re_word_char(int c)6403 static int re_word_char(int c){
6404   return (c>='0' && c<='9') || (c>='a' && c<='z')
6405       || (c>='A' && c<='Z') || c=='_';
6406 }
6407 
6408 /* Return true if c is a "digit" character:  [0-9] */
re_digit_char(int c)6409 static int re_digit_char(int c){
6410   return (c>='0' && c<='9');
6411 }
6412 
6413 /* Return true if c is a perl "space" character:  [ \t\r\n\v\f] */
re_space_char(int c)6414 static int re_space_char(int c){
6415   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6416 }
6417 
6418 /* Run a compiled regular expression on the zero-terminated input
6419 ** string zIn[].  Return true on a match and false if there is no match.
6420 */
re_match(ReCompiled * pRe,const unsigned char * zIn,int nIn)6421 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6422   ReStateSet aStateSet[2], *pThis, *pNext;
6423   ReStateNumber aSpace[100];
6424   ReStateNumber *pToFree;
6425   unsigned int i = 0;
6426   unsigned int iSwap = 0;
6427   int c = RE_START;
6428   int cPrev = 0;
6429   int rc = 0;
6430   ReInput in;
6431 
6432   in.z = zIn;
6433   in.i = 0;
6434   in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6435 
6436   /* Look for the initial prefix match, if there is one. */
6437   if( pRe->nInit ){
6438     unsigned char x = pRe->zInit[0];
6439     while( in.i+pRe->nInit<=in.mx
6440      && (zIn[in.i]!=x ||
6441          strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6442     ){
6443       in.i++;
6444     }
6445     if( in.i+pRe->nInit>in.mx ) return 0;
6446     c = RE_START-1;
6447   }
6448 
6449   if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6450     pToFree = 0;
6451     aStateSet[0].aState = aSpace;
6452   }else{
6453     pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6454     if( pToFree==0 ) return -1;
6455     aStateSet[0].aState = pToFree;
6456   }
6457   aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6458   pNext = &aStateSet[1];
6459   pNext->nState = 0;
6460   re_add_state(pNext, 0);
6461   while( c!=RE_EOF && pNext->nState>0 ){
6462     cPrev = c;
6463     c = pRe->xNextChar(&in);
6464     pThis = pNext;
6465     pNext = &aStateSet[iSwap];
6466     iSwap = 1 - iSwap;
6467     pNext->nState = 0;
6468     for(i=0; i<pThis->nState; i++){
6469       int x = pThis->aState[i];
6470       switch( pRe->aOp[x] ){
6471         case RE_OP_MATCH: {
6472           if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6473           break;
6474         }
6475         case RE_OP_ATSTART: {
6476           if( cPrev==RE_START ) re_add_state(pThis, x+1);
6477           break;
6478         }
6479         case RE_OP_ANY: {
6480           if( c!=0 ) re_add_state(pNext, x+1);
6481           break;
6482         }
6483         case RE_OP_WORD: {
6484           if( re_word_char(c) ) re_add_state(pNext, x+1);
6485           break;
6486         }
6487         case RE_OP_NOTWORD: {
6488           if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6489           break;
6490         }
6491         case RE_OP_DIGIT: {
6492           if( re_digit_char(c) ) re_add_state(pNext, x+1);
6493           break;
6494         }
6495         case RE_OP_NOTDIGIT: {
6496           if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6497           break;
6498         }
6499         case RE_OP_SPACE: {
6500           if( re_space_char(c) ) re_add_state(pNext, x+1);
6501           break;
6502         }
6503         case RE_OP_NOTSPACE: {
6504           if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6505           break;
6506         }
6507         case RE_OP_BOUNDARY: {
6508           if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6509           break;
6510         }
6511         case RE_OP_ANYSTAR: {
6512           re_add_state(pNext, x);
6513           re_add_state(pThis, x+1);
6514           break;
6515         }
6516         case RE_OP_FORK: {
6517           re_add_state(pThis, x+pRe->aArg[x]);
6518           re_add_state(pThis, x+1);
6519           break;
6520         }
6521         case RE_OP_GOTO: {
6522           re_add_state(pThis, x+pRe->aArg[x]);
6523           break;
6524         }
6525         case RE_OP_ACCEPT: {
6526           rc = 1;
6527           goto re_match_end;
6528         }
6529         case RE_OP_CC_EXC: {
6530           if( c==0 ) break;
6531           /* fall-through */ goto re_op_cc_inc;
6532         }
6533         case RE_OP_CC_INC: re_op_cc_inc: {
6534           int j = 1;
6535           int n = pRe->aArg[x];
6536           int hit = 0;
6537           for(j=1; j>0 && j<n; j++){
6538             if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6539               if( pRe->aArg[x+j]==c ){
6540                 hit = 1;
6541                 j = -1;
6542               }
6543             }else{
6544               if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6545                 hit = 1;
6546                 j = -1;
6547               }else{
6548                 j++;
6549               }
6550             }
6551           }
6552           if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6553           if( hit ) re_add_state(pNext, x+n);
6554           break;
6555         }
6556       }
6557     }
6558   }
6559   for(i=0; i<pNext->nState; i++){
6560     int x = pNext->aState[i];
6561     while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x];
6562     if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; }
6563   }
6564 re_match_end:
6565   sqlite3_free(pToFree);
6566   return rc;
6567 }
6568 
6569 /* Resize the opcode and argument arrays for an RE under construction.
6570 */
re_resize(ReCompiled * p,int N)6571 static int re_resize(ReCompiled *p, int N){
6572   char *aOp;
6573   int *aArg;
6574   aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6575   if( aOp==0 ) return 1;
6576   p->aOp = aOp;
6577   aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6578   if( aArg==0 ) return 1;
6579   p->aArg = aArg;
6580   p->nAlloc = N;
6581   return 0;
6582 }
6583 
6584 /* Insert a new opcode and argument into an RE under construction.  The
6585 ** insertion point is just prior to existing opcode iBefore.
6586 */
re_insert(ReCompiled * p,int iBefore,int op,int arg)6587 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6588   int i;
6589   if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6590   for(i=p->nState; i>iBefore; i--){
6591     p->aOp[i] = p->aOp[i-1];
6592     p->aArg[i] = p->aArg[i-1];
6593   }
6594   p->nState++;
6595   p->aOp[iBefore] = (char)op;
6596   p->aArg[iBefore] = arg;
6597   return iBefore;
6598 }
6599 
6600 /* Append a new opcode and argument to the end of the RE under construction.
6601 */
re_append(ReCompiled * p,int op,int arg)6602 static int re_append(ReCompiled *p, int op, int arg){
6603   return re_insert(p, p->nState, op, arg);
6604 }
6605 
6606 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6607 ** under construction.
6608 */
re_copy(ReCompiled * p,int iStart,int N)6609 static void re_copy(ReCompiled *p, int iStart, int N){
6610   if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6611   memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6612   memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6613   p->nState += N;
6614 }
6615 
6616 /* Return true if c is a hexadecimal digit character:  [0-9a-fA-F]
6617 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c).  If
6618 ** c is not a hex digit *pV is unchanged.
6619 */
re_hex(int c,int * pV)6620 static int re_hex(int c, int *pV){
6621   if( c>='0' && c<='9' ){
6622     c -= '0';
6623   }else if( c>='a' && c<='f' ){
6624     c -= 'a' - 10;
6625   }else if( c>='A' && c<='F' ){
6626     c -= 'A' - 10;
6627   }else{
6628     return 0;
6629   }
6630   *pV = (*pV)*16 + (c & 0xff);
6631   return 1;
6632 }
6633 
6634 /* A backslash character has been seen, read the next character and
6635 ** return its interpretation.
6636 */
re_esc_char(ReCompiled * p)6637 static unsigned re_esc_char(ReCompiled *p){
6638   static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6639   static const char zTrans[] = "\a\f\n\r\t\v";
6640   int i, v = 0;
6641   char c;
6642   if( p->sIn.i>=p->sIn.mx ) return 0;
6643   c = p->sIn.z[p->sIn.i];
6644   if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6645     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6646     if( re_hex(zIn[1],&v)
6647      && re_hex(zIn[2],&v)
6648      && re_hex(zIn[3],&v)
6649      && re_hex(zIn[4],&v)
6650     ){
6651       p->sIn.i += 5;
6652       return v;
6653     }
6654   }
6655   if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6656     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6657     if( re_hex(zIn[1],&v)
6658      && re_hex(zIn[2],&v)
6659     ){
6660       p->sIn.i += 3;
6661       return v;
6662     }
6663   }
6664   for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6665   if( zEsc[i] ){
6666     if( i<6 ) c = zTrans[i];
6667     p->sIn.i++;
6668   }else{
6669     p->zErr = "unknown \\ escape";
6670   }
6671   return c;
6672 }
6673 
6674 /* Forward declaration */
6675 static const char *re_subcompile_string(ReCompiled*);
6676 
6677 /* Peek at the next byte of input */
rePeek(ReCompiled * p)6678 static unsigned char rePeek(ReCompiled *p){
6679   return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6680 }
6681 
6682 /* Compile RE text into a sequence of opcodes.  Continue up to the
6683 ** first unmatched ")" character, then return.  If an error is found,
6684 ** return a pointer to the error message string.
6685 */
re_subcompile_re(ReCompiled * p)6686 static const char *re_subcompile_re(ReCompiled *p){
6687   const char *zErr;
6688   int iStart, iEnd, iGoto;
6689   iStart = p->nState;
6690   zErr = re_subcompile_string(p);
6691   if( zErr ) return zErr;
6692   while( rePeek(p)=='|' ){
6693     iEnd = p->nState;
6694     re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6695     iGoto = re_append(p, RE_OP_GOTO, 0);
6696     p->sIn.i++;
6697     zErr = re_subcompile_string(p);
6698     if( zErr ) return zErr;
6699     p->aArg[iGoto] = p->nState - iGoto;
6700   }
6701   return 0;
6702 }
6703 
6704 /* Compile an element of regular expression text (anything that can be
6705 ** an operand to the "|" operator).  Return NULL on success or a pointer
6706 ** to the error message if there is a problem.
6707 */
re_subcompile_string(ReCompiled * p)6708 static const char *re_subcompile_string(ReCompiled *p){
6709   int iPrev = -1;
6710   int iStart;
6711   unsigned c;
6712   const char *zErr;
6713   while( (c = p->xNextChar(&p->sIn))!=0 ){
6714     iStart = p->nState;
6715     switch( c ){
6716       case '|':
6717       case ')': {
6718         p->sIn.i--;
6719         return 0;
6720       }
6721       case '(': {
6722         zErr = re_subcompile_re(p);
6723         if( zErr ) return zErr;
6724         if( rePeek(p)!=')' ) return "unmatched '('";
6725         p->sIn.i++;
6726         break;
6727       }
6728       case '.': {
6729         if( rePeek(p)=='*' ){
6730           re_append(p, RE_OP_ANYSTAR, 0);
6731           p->sIn.i++;
6732         }else{
6733           re_append(p, RE_OP_ANY, 0);
6734         }
6735         break;
6736       }
6737       case '*': {
6738         if( iPrev<0 ) return "'*' without operand";
6739         re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6740         re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6741         break;
6742       }
6743       case '+': {
6744         if( iPrev<0 ) return "'+' without operand";
6745         re_append(p, RE_OP_FORK, iPrev - p->nState);
6746         break;
6747       }
6748       case '?': {
6749         if( iPrev<0 ) return "'?' without operand";
6750         re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6751         break;
6752       }
6753       case '$': {
6754         re_append(p, RE_OP_MATCH, RE_EOF);
6755         break;
6756       }
6757       case '^': {
6758         re_append(p, RE_OP_ATSTART, 0);
6759         break;
6760       }
6761       case '{': {
6762         int m = 0, n = 0;
6763         int sz, j;
6764         if( iPrev<0 ) return "'{m,n}' without operand";
6765         while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6766         n = m;
6767         if( c==',' ){
6768           p->sIn.i++;
6769           n = 0;
6770           while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6771         }
6772         if( c!='}' ) return "unmatched '{'";
6773         if( n>0 && n<m ) return "n less than m in '{m,n}'";
6774         p->sIn.i++;
6775         sz = p->nState - iPrev;
6776         if( m==0 ){
6777           if( n==0 ) return "both m and n are zero in '{m,n}'";
6778           re_insert(p, iPrev, RE_OP_FORK, sz+1);
6779           iPrev++;
6780           n--;
6781         }else{
6782           for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6783         }
6784         for(j=m; j<n; j++){
6785           re_append(p, RE_OP_FORK, sz+1);
6786           re_copy(p, iPrev, sz);
6787         }
6788         if( n==0 && m>0 ){
6789           re_append(p, RE_OP_FORK, -sz);
6790         }
6791         break;
6792       }
6793       case '[': {
6794         unsigned int iFirst = p->nState;
6795         if( rePeek(p)=='^' ){
6796           re_append(p, RE_OP_CC_EXC, 0);
6797           p->sIn.i++;
6798         }else{
6799           re_append(p, RE_OP_CC_INC, 0);
6800         }
6801         while( (c = p->xNextChar(&p->sIn))!=0 ){
6802           if( c=='[' && rePeek(p)==':' ){
6803             return "POSIX character classes not supported";
6804           }
6805           if( c=='\\' ) c = re_esc_char(p);
6806           if( rePeek(p)=='-' ){
6807             re_append(p, RE_OP_CC_RANGE, c);
6808             p->sIn.i++;
6809             c = p->xNextChar(&p->sIn);
6810             if( c=='\\' ) c = re_esc_char(p);
6811             re_append(p, RE_OP_CC_RANGE, c);
6812           }else{
6813             re_append(p, RE_OP_CC_VALUE, c);
6814           }
6815           if( rePeek(p)==']' ){ p->sIn.i++; break; }
6816         }
6817         if( c==0 ) return "unclosed '['";
6818         if( p->nState>iFirst ) p->aArg[iFirst] = p->nState - iFirst;
6819         break;
6820       }
6821       case '\\': {
6822         int specialOp = 0;
6823         switch( rePeek(p) ){
6824           case 'b': specialOp = RE_OP_BOUNDARY;   break;
6825           case 'd': specialOp = RE_OP_DIGIT;      break;
6826           case 'D': specialOp = RE_OP_NOTDIGIT;   break;
6827           case 's': specialOp = RE_OP_SPACE;      break;
6828           case 'S': specialOp = RE_OP_NOTSPACE;   break;
6829           case 'w': specialOp = RE_OP_WORD;       break;
6830           case 'W': specialOp = RE_OP_NOTWORD;    break;
6831         }
6832         if( specialOp ){
6833           p->sIn.i++;
6834           re_append(p, specialOp, 0);
6835         }else{
6836           c = re_esc_char(p);
6837           re_append(p, RE_OP_MATCH, c);
6838         }
6839         break;
6840       }
6841       default: {
6842         re_append(p, RE_OP_MATCH, c);
6843         break;
6844       }
6845     }
6846     iPrev = iStart;
6847   }
6848   return 0;
6849 }
6850 
6851 /* Free and reclaim all the memory used by a previously compiled
6852 ** regular expression.  Applications should invoke this routine once
6853 ** for every call to re_compile() to avoid memory leaks.
6854 */
re_free(ReCompiled * pRe)6855 static void re_free(ReCompiled *pRe){
6856   if( pRe ){
6857     sqlite3_free(pRe->aOp);
6858     sqlite3_free(pRe->aArg);
6859     sqlite3_free(pRe);
6860   }
6861 }
6862 
6863 /*
6864 ** Compile a textual regular expression in zIn[] into a compiled regular
6865 ** expression suitable for us by re_match() and return a pointer to the
6866 ** compiled regular expression in *ppRe.  Return NULL on success or an
6867 ** error message if something goes wrong.
6868 */
re_compile(ReCompiled ** ppRe,const char * zIn,int noCase)6869 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6870   ReCompiled *pRe;
6871   const char *zErr;
6872   int i, j;
6873 
6874   *ppRe = 0;
6875   pRe = sqlite3_malloc( sizeof(*pRe) );
6876   if( pRe==0 ){
6877     return "out of memory";
6878   }
6879   memset(pRe, 0, sizeof(*pRe));
6880   pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6881   if( re_resize(pRe, 30) ){
6882     re_free(pRe);
6883     return "out of memory";
6884   }
6885   if( zIn[0]=='^' ){
6886     zIn++;
6887   }else{
6888     re_append(pRe, RE_OP_ANYSTAR, 0);
6889   }
6890   pRe->sIn.z = (unsigned char*)zIn;
6891   pRe->sIn.i = 0;
6892   pRe->sIn.mx = (int)strlen(zIn);
6893   zErr = re_subcompile_re(pRe);
6894   if( zErr ){
6895     re_free(pRe);
6896     return zErr;
6897   }
6898   if( pRe->sIn.i>=pRe->sIn.mx ){
6899     re_append(pRe, RE_OP_ACCEPT, 0);
6900     *ppRe = pRe;
6901   }else{
6902     re_free(pRe);
6903     return "unrecognized character";
6904   }
6905 
6906   /* The following is a performance optimization.  If the regex begins with
6907   ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6908   ** one or more matching characters, enter those matching characters into
6909   ** zInit[].  The re_match() routine can then search ahead in the input
6910   ** string looking for the initial match without having to run the whole
6911   ** regex engine over the string.  Do not worry about trying to match
6912   ** unicode characters beyond plane 0 - those are very rare and this is
6913   ** just an optimization. */
6914   if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6915     for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6916       unsigned x = pRe->aArg[i];
6917       if( x<=0x7f ){
6918         pRe->zInit[j++] = (unsigned char)x;
6919       }else if( x<=0x7ff ){
6920         pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6921         pRe->zInit[j++] = 0x80 | (x&0x3f);
6922       }else if( x<=0xffff ){
6923         pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
6924         pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6925         pRe->zInit[j++] = 0x80 | (x&0x3f);
6926       }else{
6927         break;
6928       }
6929     }
6930     if( j>0 && pRe->zInit[j-1]==0 ) j--;
6931     pRe->nInit = j;
6932   }
6933   return pRe->zErr;
6934 }
6935 
6936 /*
6937 ** Implementation of the regexp() SQL function.  This function implements
6938 ** the build-in REGEXP operator.  The first argument to the function is the
6939 ** pattern and the second argument is the string.  So, the SQL statements:
6940 **
6941 **       A REGEXP B
6942 **
6943 ** is implemented as regexp(B,A).
6944 */
re_sql_func(sqlite3_context * context,int argc,sqlite3_value ** argv)6945 static void re_sql_func(
6946   sqlite3_context *context,
6947   int argc,
6948   sqlite3_value **argv
6949 ){
6950   ReCompiled *pRe;          /* Compiled regular expression */
6951   const char *zPattern;     /* The regular expression */
6952   const unsigned char *zStr;/* String being searched */
6953   const char *zErr;         /* Compile error message */
6954   int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
6955 
6956   (void)argc;  /* Unused */
6957   pRe = sqlite3_get_auxdata(context, 0);
6958   if( pRe==0 ){
6959     zPattern = (const char*)sqlite3_value_text(argv[0]);
6960     if( zPattern==0 ) return;
6961     zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6962     if( zErr ){
6963       re_free(pRe);
6964       sqlite3_result_error(context, zErr, -1);
6965       return;
6966     }
6967     if( pRe==0 ){
6968       sqlite3_result_error_nomem(context);
6969       return;
6970     }
6971     setAux = 1;
6972   }
6973   zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6974   if( zStr!=0 ){
6975     sqlite3_result_int(context, re_match(pRe, zStr, -1));
6976   }
6977   if( setAux ){
6978     sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6979   }
6980 }
6981 
6982 #if defined(SQLITE_DEBUG)
6983 /*
6984 ** This function is used for testing and debugging only.  It is only available
6985 ** if the SQLITE_DEBUG compile-time option is used.
6986 **
6987 ** Compile a regular expression and then convert the compiled expression into
6988 ** text and return that text.
6989 */
re_bytecode_func(sqlite3_context * context,int argc,sqlite3_value ** argv)6990 static void re_bytecode_func(
6991   sqlite3_context *context,
6992   int argc,
6993   sqlite3_value **argv
6994 ){
6995   const char *zPattern;
6996   const char *zErr;
6997   ReCompiled *pRe;
6998   sqlite3_str *pStr;
6999   int i;
7000   int n;
7001   char *z;
7002   (void)argc;
7003 
7004   zPattern = (const char*)sqlite3_value_text(argv[0]);
7005   if( zPattern==0 ) return;
7006   zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
7007   if( zErr ){
7008     re_free(pRe);
7009     sqlite3_result_error(context, zErr, -1);
7010     return;
7011   }
7012   if( pRe==0 ){
7013     sqlite3_result_error_nomem(context);
7014     return;
7015   }
7016   pStr = sqlite3_str_new(0);
7017   if( pStr==0 ) goto re_bytecode_func_err;
7018   if( pRe->nInit>0 ){
7019     sqlite3_str_appendf(pStr, "INIT     ");
7020     for(i=0; i<pRe->nInit; i++){
7021       sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]);
7022     }
7023     sqlite3_str_appendf(pStr, "\n");
7024   }
7025   for(i=0; (unsigned)i<pRe->nState; i++){
7026     sqlite3_str_appendf(pStr, "%-8s %4d\n",
7027          ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]);
7028   }
7029   n = sqlite3_str_length(pStr);
7030   z = sqlite3_str_finish(pStr);
7031   if( n==0 ){
7032     sqlite3_free(z);
7033   }else{
7034     sqlite3_result_text(context, z, n-1, sqlite3_free);
7035   }
7036 
7037 re_bytecode_func_err:
7038   re_free(pRe);
7039 }
7040 
7041 #endif /* SQLITE_DEBUG */
7042 
7043 
7044 /*
7045 ** Invoke this routine to register the regexp() function with the
7046 ** SQLite database connection.
7047 */
7048 #ifdef _WIN32
7049 
7050 #endif
sqlite3_regexp_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)7051 int sqlite3_regexp_init(
7052   sqlite3 *db,
7053   char **pzErrMsg,
7054   const sqlite3_api_routines *pApi
7055 ){
7056   int rc = SQLITE_OK;
7057   SQLITE_EXTENSION_INIT2(pApi);
7058   (void)pzErrMsg;  /* Unused */
7059   rc = sqlite3_create_function(db, "regexp", 2,
7060                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7061                             0, re_sql_func, 0, 0);
7062   if( rc==SQLITE_OK ){
7063     /* The regexpi(PATTERN,STRING) function is a case-insensitive version
7064     ** of regexp(PATTERN,STRING). */
7065     rc = sqlite3_create_function(db, "regexpi", 2,
7066                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7067                             (void*)db, re_sql_func, 0, 0);
7068 #if defined(SQLITE_DEBUG)
7069     if( rc==SQLITE_OK ){
7070       rc = sqlite3_create_function(db, "regexp_bytecode", 1,
7071                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
7072                             0, re_bytecode_func, 0, 0);
7073     }
7074 #endif /* SQLITE_DEBUG */
7075   }
7076   return rc;
7077 }
7078 
7079 /************************* End ../ext/misc/regexp.c ********************/
7080 #ifndef SQLITE_SHELL_FIDDLE
7081 /************************* Begin ../ext/misc/fileio.c ******************/
7082 /*
7083 ** 2014-06-13
7084 **
7085 ** The author disclaims copyright to this source code.  In place of
7086 ** a legal notice, here is a blessing:
7087 **
7088 **    May you do good and not evil.
7089 **    May you find forgiveness for yourself and forgive others.
7090 **    May you share freely, never taking more than you give.
7091 **
7092 ******************************************************************************
7093 **
7094 ** This SQLite extension implements SQL functions readfile() and
7095 ** writefile(), and eponymous virtual type "fsdir".
7096 **
7097 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
7098 **
7099 **   If neither of the optional arguments is present, then this UDF
7100 **   function writes blob DATA to file FILE. If successful, the number
7101 **   of bytes written is returned. If an error occurs, NULL is returned.
7102 **
7103 **   If the first option argument - MODE - is present, then it must
7104 **   be passed an integer value that corresponds to a POSIX mode
7105 **   value (file type + permissions, as returned in the stat.st_mode
7106 **   field by the stat() system call). Three types of files may
7107 **   be written/created:
7108 **
7109 **     regular files:  (mode & 0170000)==0100000
7110 **     symbolic links: (mode & 0170000)==0120000
7111 **     directories:    (mode & 0170000)==0040000
7112 **
7113 **   For a directory, the DATA is ignored. For a symbolic link, it is
7114 **   interpreted as text and used as the target of the link. For a
7115 **   regular file, it is interpreted as a blob and written into the
7116 **   named file. Regardless of the type of file, its permissions are
7117 **   set to (mode & 0777) before returning.
7118 **
7119 **   If the optional MTIME argument is present, then it is interpreted
7120 **   as an integer - the number of seconds since the unix epoch. The
7121 **   modification-time of the target file is set to this value before
7122 **   returning.
7123 **
7124 **   If three or more arguments are passed to this function and an
7125 **   error is encountered, an exception is raised.
7126 **
7127 ** READFILE(FILE):
7128 **
7129 **   Read and return the contents of file FILE (type blob) from disk.
7130 **
7131 ** FSDIR:
7132 **
7133 **   Used as follows:
7134 **
7135 **     SELECT * FROM fsdir($path [, $dir]);
7136 **
7137 **   Parameter $path is an absolute or relative pathname. If the file that it
7138 **   refers to does not exist, it is an error. If the path refers to a regular
7139 **   file or symbolic link, it returns a single row. Or, if the path refers
7140 **   to a directory, it returns one row for the directory, and one row for each
7141 **   file within the hierarchy rooted at $path.
7142 **
7143 **   Each row has the following columns:
7144 **
7145 **     name:  Path to file or directory (text value).
7146 **     mode:  Value of stat.st_mode for directory entry (an integer).
7147 **     mtime: Value of stat.st_mtime for directory entry (an integer).
7148 **     data:  For a regular file, a blob containing the file data. For a
7149 **            symlink, a text value containing the text of the link. For a
7150 **            directory, NULL.
7151 **
7152 **   If a non-NULL value is specified for the optional $dir parameter and
7153 **   $path is a relative path, then $path is interpreted relative to $dir.
7154 **   And the paths returned in the "name" column of the table are also
7155 **   relative to directory $dir.
7156 **
7157 ** Notes on building this extension for Windows:
7158 **   Unless linked statically with the SQLite library, a preprocessor
7159 **   symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
7160 **   DLL form of this extension for WIN32. See its use below for details.
7161 */
7162 /* #include "sqlite3ext.h" */
7163 SQLITE_EXTENSION_INIT1
7164 #include <stdio.h>
7165 #include <string.h>
7166 #include <assert.h>
7167 
7168 #include <sys/types.h>
7169 #include <sys/stat.h>
7170 #include <fcntl.h>
7171 #if !defined(_WIN32) && !defined(WIN32)
7172 #  include <unistd.h>
7173 #  include <dirent.h>
7174 #  include <utime.h>
7175 #  include <sys/time.h>
7176 #else
7177 #  include "windows.h"
7178 #  include <io.h>
7179 #  include <direct.h>
7180 /* #  include "test_windirent.h" */
7181 #  define dirent DIRENT
7182 #  ifndef chmod
7183 #    define chmod _chmod
7184 #  endif
7185 #  ifndef stat
7186 #    define stat _stat
7187 #  endif
7188 #  define mkdir(path,mode) _mkdir(path)
7189 #  define lstat(path,buf) stat(path,buf)
7190 #endif
7191 #include <time.h>
7192 #include <errno.h>
7193 
7194 
7195 /*
7196 ** Structure of the fsdir() table-valued function
7197 */
7198                  /*    0    1    2     3    4           5             */
7199 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
7200 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
7201 #define FSDIR_COLUMN_MODE     1     /* Access mode */
7202 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
7203 #define FSDIR_COLUMN_DATA     3     /* File content */
7204 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
7205 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
7206 
7207 
7208 /*
7209 ** Set the result stored by context ctx to a blob containing the
7210 ** contents of file zName.  Or, leave the result unchanged (NULL)
7211 ** if the file does not exist or is unreadable.
7212 **
7213 ** If the file exceeds the SQLite blob size limit, through an
7214 ** SQLITE_TOOBIG error.
7215 **
7216 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
7217 ** off of disk.
7218 */
readFileContents(sqlite3_context * ctx,const char * zName)7219 static void readFileContents(sqlite3_context *ctx, const char *zName){
7220   FILE *in;
7221   sqlite3_int64 nIn;
7222   void *pBuf;
7223   sqlite3 *db;
7224   int mxBlob;
7225 
7226   in = fopen(zName, "rb");
7227   if( in==0 ){
7228     /* File does not exist or is unreadable. Leave the result set to NULL. */
7229     return;
7230   }
7231   fseek(in, 0, SEEK_END);
7232   nIn = ftell(in);
7233   rewind(in);
7234   db = sqlite3_context_db_handle(ctx);
7235   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
7236   if( nIn>mxBlob ){
7237     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
7238     fclose(in);
7239     return;
7240   }
7241   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
7242   if( pBuf==0 ){
7243     sqlite3_result_error_nomem(ctx);
7244     fclose(in);
7245     return;
7246   }
7247   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
7248     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
7249   }else{
7250     sqlite3_result_error_code(ctx, SQLITE_IOERR);
7251     sqlite3_free(pBuf);
7252   }
7253   fclose(in);
7254 }
7255 
7256 /*
7257 ** Implementation of the "readfile(X)" SQL function.  The entire content
7258 ** of the file named X is read and returned as a BLOB.  NULL is returned
7259 ** if the file does not exist or is unreadable.
7260 */
readfileFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)7261 static void readfileFunc(
7262   sqlite3_context *context,
7263   int argc,
7264   sqlite3_value **argv
7265 ){
7266   const char *zName;
7267   (void)(argc);  /* Unused parameter */
7268   zName = (const char*)sqlite3_value_text(argv[0]);
7269   if( zName==0 ) return;
7270   readFileContents(context, zName);
7271 }
7272 
7273 /*
7274 ** Set the error message contained in context ctx to the results of
7275 ** vprintf(zFmt, ...).
7276 */
ctxErrorMsg(sqlite3_context * ctx,const char * zFmt,...)7277 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
7278   char *zMsg = 0;
7279   va_list ap;
7280   va_start(ap, zFmt);
7281   zMsg = sqlite3_vmprintf(zFmt, ap);
7282   sqlite3_result_error(ctx, zMsg, -1);
7283   sqlite3_free(zMsg);
7284   va_end(ap);
7285 }
7286 
7287 #if defined(_WIN32)
7288 /*
7289 ** This function is designed to convert a Win32 FILETIME structure into the
7290 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
7291 */
fileTimeToUnixTime(LPFILETIME pFileTime)7292 static sqlite3_uint64 fileTimeToUnixTime(
7293   LPFILETIME pFileTime
7294 ){
7295   SYSTEMTIME epochSystemTime;
7296   ULARGE_INTEGER epochIntervals;
7297   FILETIME epochFileTime;
7298   ULARGE_INTEGER fileIntervals;
7299 
7300   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
7301   epochSystemTime.wYear = 1970;
7302   epochSystemTime.wMonth = 1;
7303   epochSystemTime.wDay = 1;
7304   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
7305   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
7306   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
7307 
7308   fileIntervals.LowPart = pFileTime->dwLowDateTime;
7309   fileIntervals.HighPart = pFileTime->dwHighDateTime;
7310 
7311   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
7312 }
7313 
7314 
7315 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
7316 #  /* To allow a standalone DLL, use this next replacement function: */
7317 #  undef sqlite3_win32_utf8_to_unicode
7318 #  define sqlite3_win32_utf8_to_unicode utf8_to_utf16
7319 #
utf8_to_utf16(const char * z)7320 LPWSTR utf8_to_utf16(const char *z){
7321   int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
7322   LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
7323   if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
7324     return rv;
7325   sqlite3_free(rv);
7326   return 0;
7327 }
7328 #endif
7329 
7330 /*
7331 ** This function attempts to normalize the time values found in the stat()
7332 ** buffer to UTC.  This is necessary on Win32, where the runtime library
7333 ** appears to return these values as local times.
7334 */
statTimesToUtc(const char * zPath,struct stat * pStatBuf)7335 static void statTimesToUtc(
7336   const char *zPath,
7337   struct stat *pStatBuf
7338 ){
7339   HANDLE hFindFile;
7340   WIN32_FIND_DATAW fd;
7341   LPWSTR zUnicodeName;
7342   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
7343   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
7344   if( zUnicodeName ){
7345     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
7346     hFindFile = FindFirstFileW(zUnicodeName, &fd);
7347     if( hFindFile!=NULL ){
7348       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
7349       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
7350       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
7351       FindClose(hFindFile);
7352     }
7353     sqlite3_free(zUnicodeName);
7354   }
7355 }
7356 #endif
7357 
7358 /*
7359 ** This function is used in place of stat().  On Windows, special handling
7360 ** is required in order for the included time to be returned as UTC.  On all
7361 ** other systems, this function simply calls stat().
7362 */
fileStat(const char * zPath,struct stat * pStatBuf)7363 static int fileStat(
7364   const char *zPath,
7365   struct stat *pStatBuf
7366 ){
7367 #if defined(_WIN32)
7368   int rc = stat(zPath, pStatBuf);
7369   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
7370   return rc;
7371 #else
7372   return stat(zPath, pStatBuf);
7373 #endif
7374 }
7375 
7376 /*
7377 ** This function is used in place of lstat().  On Windows, special handling
7378 ** is required in order for the included time to be returned as UTC.  On all
7379 ** other systems, this function simply calls lstat().
7380 */
fileLinkStat(const char * zPath,struct stat * pStatBuf)7381 static int fileLinkStat(
7382   const char *zPath,
7383   struct stat *pStatBuf
7384 ){
7385 #if defined(_WIN32)
7386   int rc = lstat(zPath, pStatBuf);
7387   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
7388   return rc;
7389 #else
7390   return lstat(zPath, pStatBuf);
7391 #endif
7392 }
7393 
7394 /*
7395 ** Argument zFile is the name of a file that will be created and/or written
7396 ** by SQL function writefile(). This function ensures that the directory
7397 ** zFile will be written to exists, creating it if required. The permissions
7398 ** for any path components created by this function are set in accordance
7399 ** with the current umask.
7400 **
7401 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
7402 ** SQLITE_OK is returned if the directory is successfully created, or
7403 ** SQLITE_ERROR otherwise.
7404 */
makeDirectory(const char * zFile)7405 static int makeDirectory(
7406   const char *zFile
7407 ){
7408   char *zCopy = sqlite3_mprintf("%s", zFile);
7409   int rc = SQLITE_OK;
7410 
7411   if( zCopy==0 ){
7412     rc = SQLITE_NOMEM;
7413   }else{
7414     int nCopy = (int)strlen(zCopy);
7415     int i = 1;
7416 
7417     while( rc==SQLITE_OK ){
7418       struct stat sStat;
7419       int rc2;
7420 
7421       for(; zCopy[i]!='/' && i<nCopy; i++);
7422       if( i==nCopy ) break;
7423       zCopy[i] = '\0';
7424 
7425       rc2 = fileStat(zCopy, &sStat);
7426       if( rc2!=0 ){
7427         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
7428       }else{
7429         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
7430       }
7431       zCopy[i] = '/';
7432       i++;
7433     }
7434 
7435     sqlite3_free(zCopy);
7436   }
7437 
7438   return rc;
7439 }
7440 
7441 /*
7442 ** This function does the work for the writefile() UDF. Refer to
7443 ** header comments at the top of this file for details.
7444 */
writeFile(sqlite3_context * pCtx,const char * zFile,sqlite3_value * pData,mode_t mode,sqlite3_int64 mtime)7445 static int writeFile(
7446   sqlite3_context *pCtx,          /* Context to return bytes written in */
7447   const char *zFile,              /* File to write */
7448   sqlite3_value *pData,           /* Data to write */
7449   mode_t mode,                    /* MODE parameter passed to writefile() */
7450   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
7451 ){
7452   if( zFile==0 ) return 1;
7453 #if !defined(_WIN32) && !defined(WIN32)
7454   if( S_ISLNK(mode) ){
7455     const char *zTo = (const char*)sqlite3_value_text(pData);
7456     if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
7457   }else
7458 #endif
7459   {
7460     if( S_ISDIR(mode) ){
7461       if( mkdir(zFile, mode) ){
7462         /* The mkdir() call to create the directory failed. This might not
7463         ** be an error though - if there is already a directory at the same
7464         ** path and either the permissions already match or can be changed
7465         ** to do so using chmod(), it is not an error.  */
7466         struct stat sStat;
7467         if( errno!=EEXIST
7468          || 0!=fileStat(zFile, &sStat)
7469          || !S_ISDIR(sStat.st_mode)
7470          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
7471         ){
7472           return 1;
7473         }
7474       }
7475     }else{
7476       sqlite3_int64 nWrite = 0;
7477       const char *z;
7478       int rc = 0;
7479       FILE *out = fopen(zFile, "wb");
7480       if( out==0 ) return 1;
7481       z = (const char*)sqlite3_value_blob(pData);
7482       if( z ){
7483         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
7484         nWrite = sqlite3_value_bytes(pData);
7485         if( nWrite!=n ){
7486           rc = 1;
7487         }
7488       }
7489       fclose(out);
7490       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
7491         rc = 1;
7492       }
7493       if( rc ) return 2;
7494       sqlite3_result_int64(pCtx, nWrite);
7495     }
7496   }
7497 
7498   if( mtime>=0 ){
7499 #if defined(_WIN32)
7500 #if !SQLITE_OS_WINRT
7501     /* Windows */
7502     FILETIME lastAccess;
7503     FILETIME lastWrite;
7504     SYSTEMTIME currentTime;
7505     LONGLONG intervals;
7506     HANDLE hFile;
7507     LPWSTR zUnicodeName;
7508     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
7509 
7510     GetSystemTime(&currentTime);
7511     SystemTimeToFileTime(&currentTime, &lastAccess);
7512     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
7513     lastWrite.dwLowDateTime = (DWORD)intervals;
7514     lastWrite.dwHighDateTime = intervals >> 32;
7515     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
7516     if( zUnicodeName==0 ){
7517       return 1;
7518     }
7519     hFile = CreateFileW(
7520       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
7521       FILE_FLAG_BACKUP_SEMANTICS, NULL
7522     );
7523     sqlite3_free(zUnicodeName);
7524     if( hFile!=INVALID_HANDLE_VALUE ){
7525       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
7526       CloseHandle(hFile);
7527       return !bResult;
7528     }else{
7529       return 1;
7530     }
7531 #endif
7532 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
7533     /* Recent unix */
7534     struct timespec times[2];
7535     times[0].tv_nsec = times[1].tv_nsec = 0;
7536     times[0].tv_sec = time(0);
7537     times[1].tv_sec = mtime;
7538     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
7539       return 1;
7540     }
7541 #else
7542     /* Legacy unix */
7543     struct timeval times[2];
7544     times[0].tv_usec = times[1].tv_usec = 0;
7545     times[0].tv_sec = time(0);
7546     times[1].tv_sec = mtime;
7547     if( utimes(zFile, times) ){
7548       return 1;
7549     }
7550 #endif
7551   }
7552 
7553   return 0;
7554 }
7555 
7556 /*
7557 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
7558 ** Refer to header comments at the top of this file for details.
7559 */
writefileFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)7560 static void writefileFunc(
7561   sqlite3_context *context,
7562   int argc,
7563   sqlite3_value **argv
7564 ){
7565   const char *zFile;
7566   mode_t mode = 0;
7567   int res;
7568   sqlite3_int64 mtime = -1;
7569 
7570   if( argc<2 || argc>4 ){
7571     sqlite3_result_error(context,
7572         "wrong number of arguments to function writefile()", -1
7573     );
7574     return;
7575   }
7576 
7577   zFile = (const char*)sqlite3_value_text(argv[0]);
7578   if( zFile==0 ) return;
7579   if( argc>=3 ){
7580     mode = (mode_t)sqlite3_value_int(argv[2]);
7581   }
7582   if( argc==4 ){
7583     mtime = sqlite3_value_int64(argv[3]);
7584   }
7585 
7586   res = writeFile(context, zFile, argv[1], mode, mtime);
7587   if( res==1 && errno==ENOENT ){
7588     if( makeDirectory(zFile)==SQLITE_OK ){
7589       res = writeFile(context, zFile, argv[1], mode, mtime);
7590     }
7591   }
7592 
7593   if( argc>2 && res!=0 ){
7594     if( S_ISLNK(mode) ){
7595       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
7596     }else if( S_ISDIR(mode) ){
7597       ctxErrorMsg(context, "failed to create directory: %s", zFile);
7598     }else{
7599       ctxErrorMsg(context, "failed to write file: %s", zFile);
7600     }
7601   }
7602 }
7603 
7604 /*
7605 ** SQL function:   lsmode(MODE)
7606 **
7607 ** Given a numberic st_mode from stat(), convert it into a human-readable
7608 ** text string in the style of "ls -l".
7609 */
lsModeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)7610 static void lsModeFunc(
7611   sqlite3_context *context,
7612   int argc,
7613   sqlite3_value **argv
7614 ){
7615   int i;
7616   int iMode = sqlite3_value_int(argv[0]);
7617   char z[16];
7618   (void)argc;
7619   if( S_ISLNK(iMode) ){
7620     z[0] = 'l';
7621   }else if( S_ISREG(iMode) ){
7622     z[0] = '-';
7623   }else if( S_ISDIR(iMode) ){
7624     z[0] = 'd';
7625   }else{
7626     z[0] = '?';
7627   }
7628   for(i=0; i<3; i++){
7629     int m = (iMode >> ((2-i)*3));
7630     char *a = &z[1 + i*3];
7631     a[0] = (m & 0x4) ? 'r' : '-';
7632     a[1] = (m & 0x2) ? 'w' : '-';
7633     a[2] = (m & 0x1) ? 'x' : '-';
7634   }
7635   z[10] = '\0';
7636   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
7637 }
7638 
7639 #ifndef SQLITE_OMIT_VIRTUALTABLE
7640 
7641 /*
7642 ** Cursor type for recursively iterating through a directory structure.
7643 */
7644 typedef struct fsdir_cursor fsdir_cursor;
7645 typedef struct FsdirLevel FsdirLevel;
7646 
7647 struct FsdirLevel {
7648   DIR *pDir;                 /* From opendir() */
7649   char *zDir;                /* Name of directory (nul-terminated) */
7650 };
7651 
7652 struct fsdir_cursor {
7653   sqlite3_vtab_cursor base;  /* Base class - must be first */
7654 
7655   int nLvl;                  /* Number of entries in aLvl[] array */
7656   int iLvl;                  /* Index of current entry */
7657   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
7658 
7659   const char *zBase;
7660   int nBase;
7661 
7662   struct stat sStat;         /* Current lstat() results */
7663   char *zPath;               /* Path to current entry */
7664   sqlite3_int64 iRowid;      /* Current rowid */
7665 };
7666 
7667 typedef struct fsdir_tab fsdir_tab;
7668 struct fsdir_tab {
7669   sqlite3_vtab base;         /* Base class - must be first */
7670 };
7671 
7672 /*
7673 ** Construct a new fsdir virtual table object.
7674 */
fsdirConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)7675 static int fsdirConnect(
7676   sqlite3 *db,
7677   void *pAux,
7678   int argc, const char *const*argv,
7679   sqlite3_vtab **ppVtab,
7680   char **pzErr
7681 ){
7682   fsdir_tab *pNew = 0;
7683   int rc;
7684   (void)pAux;
7685   (void)argc;
7686   (void)argv;
7687   (void)pzErr;
7688   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
7689   if( rc==SQLITE_OK ){
7690     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
7691     if( pNew==0 ) return SQLITE_NOMEM;
7692     memset(pNew, 0, sizeof(*pNew));
7693     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7694   }
7695   *ppVtab = (sqlite3_vtab*)pNew;
7696   return rc;
7697 }
7698 
7699 /*
7700 ** This method is the destructor for fsdir vtab objects.
7701 */
fsdirDisconnect(sqlite3_vtab * pVtab)7702 static int fsdirDisconnect(sqlite3_vtab *pVtab){
7703   sqlite3_free(pVtab);
7704   return SQLITE_OK;
7705 }
7706 
7707 /*
7708 ** Constructor for a new fsdir_cursor object.
7709 */
fsdirOpen(sqlite3_vtab * p,sqlite3_vtab_cursor ** ppCursor)7710 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
7711   fsdir_cursor *pCur;
7712   (void)p;
7713   pCur = sqlite3_malloc( sizeof(*pCur) );
7714   if( pCur==0 ) return SQLITE_NOMEM;
7715   memset(pCur, 0, sizeof(*pCur));
7716   pCur->iLvl = -1;
7717   *ppCursor = &pCur->base;
7718   return SQLITE_OK;
7719 }
7720 
7721 /*
7722 ** Reset a cursor back to the state it was in when first returned
7723 ** by fsdirOpen().
7724 */
fsdirResetCursor(fsdir_cursor * pCur)7725 static void fsdirResetCursor(fsdir_cursor *pCur){
7726   int i;
7727   for(i=0; i<=pCur->iLvl; i++){
7728     FsdirLevel *pLvl = &pCur->aLvl[i];
7729     if( pLvl->pDir ) closedir(pLvl->pDir);
7730     sqlite3_free(pLvl->zDir);
7731   }
7732   sqlite3_free(pCur->zPath);
7733   sqlite3_free(pCur->aLvl);
7734   pCur->aLvl = 0;
7735   pCur->zPath = 0;
7736   pCur->zBase = 0;
7737   pCur->nBase = 0;
7738   pCur->nLvl = 0;
7739   pCur->iLvl = -1;
7740   pCur->iRowid = 1;
7741 }
7742 
7743 /*
7744 ** Destructor for an fsdir_cursor.
7745 */
fsdirClose(sqlite3_vtab_cursor * cur)7746 static int fsdirClose(sqlite3_vtab_cursor *cur){
7747   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7748 
7749   fsdirResetCursor(pCur);
7750   sqlite3_free(pCur);
7751   return SQLITE_OK;
7752 }
7753 
7754 /*
7755 ** Set the error message for the virtual table associated with cursor
7756 ** pCur to the results of vprintf(zFmt, ...).
7757 */
fsdirSetErrmsg(fsdir_cursor * pCur,const char * zFmt,...)7758 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
7759   va_list ap;
7760   va_start(ap, zFmt);
7761   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7762   va_end(ap);
7763 }
7764 
7765 
7766 /*
7767 ** Advance an fsdir_cursor to its next row of output.
7768 */
fsdirNext(sqlite3_vtab_cursor * cur)7769 static int fsdirNext(sqlite3_vtab_cursor *cur){
7770   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7771   mode_t m = pCur->sStat.st_mode;
7772 
7773   pCur->iRowid++;
7774   if( S_ISDIR(m) ){
7775     /* Descend into this directory */
7776     int iNew = pCur->iLvl + 1;
7777     FsdirLevel *pLvl;
7778     if( iNew>=pCur->nLvl ){
7779       int nNew = iNew+1;
7780       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
7781       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
7782       if( aNew==0 ) return SQLITE_NOMEM;
7783       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
7784       pCur->aLvl = aNew;
7785       pCur->nLvl = nNew;
7786     }
7787     pCur->iLvl = iNew;
7788     pLvl = &pCur->aLvl[iNew];
7789 
7790     pLvl->zDir = pCur->zPath;
7791     pCur->zPath = 0;
7792     pLvl->pDir = opendir(pLvl->zDir);
7793     if( pLvl->pDir==0 ){
7794       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
7795       return SQLITE_ERROR;
7796     }
7797   }
7798 
7799   while( pCur->iLvl>=0 ){
7800     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
7801     struct dirent *pEntry = readdir(pLvl->pDir);
7802     if( pEntry ){
7803       if( pEntry->d_name[0]=='.' ){
7804        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
7805        if( pEntry->d_name[1]=='\0' ) continue;
7806       }
7807       sqlite3_free(pCur->zPath);
7808       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
7809       if( pCur->zPath==0 ) return SQLITE_NOMEM;
7810       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
7811         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
7812         return SQLITE_ERROR;
7813       }
7814       return SQLITE_OK;
7815     }
7816     closedir(pLvl->pDir);
7817     sqlite3_free(pLvl->zDir);
7818     pLvl->pDir = 0;
7819     pLvl->zDir = 0;
7820     pCur->iLvl--;
7821   }
7822 
7823   /* EOF */
7824   sqlite3_free(pCur->zPath);
7825   pCur->zPath = 0;
7826   return SQLITE_OK;
7827 }
7828 
7829 /*
7830 ** Return values of columns for the row at which the series_cursor
7831 ** is currently pointing.
7832 */
fsdirColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)7833 static int fsdirColumn(
7834   sqlite3_vtab_cursor *cur,   /* The cursor */
7835   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
7836   int i                       /* Which column to return */
7837 ){
7838   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7839   switch( i ){
7840     case FSDIR_COLUMN_NAME: {
7841       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
7842       break;
7843     }
7844 
7845     case FSDIR_COLUMN_MODE:
7846       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
7847       break;
7848 
7849     case FSDIR_COLUMN_MTIME:
7850       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
7851       break;
7852 
7853     case FSDIR_COLUMN_DATA: {
7854       mode_t m = pCur->sStat.st_mode;
7855       if( S_ISDIR(m) ){
7856         sqlite3_result_null(ctx);
7857 #if !defined(_WIN32) && !defined(WIN32)
7858       }else if( S_ISLNK(m) ){
7859         char aStatic[64];
7860         char *aBuf = aStatic;
7861         sqlite3_int64 nBuf = 64;
7862         int n;
7863 
7864         while( 1 ){
7865           n = readlink(pCur->zPath, aBuf, nBuf);
7866           if( n<nBuf ) break;
7867           if( aBuf!=aStatic ) sqlite3_free(aBuf);
7868           nBuf = nBuf*2;
7869           aBuf = sqlite3_malloc64(nBuf);
7870           if( aBuf==0 ){
7871             sqlite3_result_error_nomem(ctx);
7872             return SQLITE_NOMEM;
7873           }
7874         }
7875 
7876         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
7877         if( aBuf!=aStatic ) sqlite3_free(aBuf);
7878 #endif
7879       }else{
7880         readFileContents(ctx, pCur->zPath);
7881       }
7882     }
7883     case FSDIR_COLUMN_PATH:
7884     default: {
7885       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
7886       ** always return their values as NULL */
7887       break;
7888     }
7889   }
7890   return SQLITE_OK;
7891 }
7892 
7893 /*
7894 ** Return the rowid for the current row. In this implementation, the
7895 ** first row returned is assigned rowid value 1, and each subsequent
7896 ** row a value 1 more than that of the previous.
7897 */
fsdirRowid(sqlite3_vtab_cursor * cur,sqlite_int64 * pRowid)7898 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7899   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7900   *pRowid = pCur->iRowid;
7901   return SQLITE_OK;
7902 }
7903 
7904 /*
7905 ** Return TRUE if the cursor has been moved off of the last
7906 ** row of output.
7907 */
fsdirEof(sqlite3_vtab_cursor * cur)7908 static int fsdirEof(sqlite3_vtab_cursor *cur){
7909   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7910   return (pCur->zPath==0);
7911 }
7912 
7913 /*
7914 ** xFilter callback.
7915 **
7916 ** idxNum==1   PATH parameter only
7917 ** idxNum==2   Both PATH and DIR supplied
7918 */
fsdirFilter(sqlite3_vtab_cursor * cur,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)7919 static int fsdirFilter(
7920   sqlite3_vtab_cursor *cur,
7921   int idxNum, const char *idxStr,
7922   int argc, sqlite3_value **argv
7923 ){
7924   const char *zDir = 0;
7925   fsdir_cursor *pCur = (fsdir_cursor*)cur;
7926   (void)idxStr;
7927   fsdirResetCursor(pCur);
7928 
7929   if( idxNum==0 ){
7930     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
7931     return SQLITE_ERROR;
7932   }
7933 
7934   assert( argc==idxNum && (argc==1 || argc==2) );
7935   zDir = (const char*)sqlite3_value_text(argv[0]);
7936   if( zDir==0 ){
7937     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
7938     return SQLITE_ERROR;
7939   }
7940   if( argc==2 ){
7941     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
7942   }
7943   if( pCur->zBase ){
7944     pCur->nBase = (int)strlen(pCur->zBase)+1;
7945     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
7946   }else{
7947     pCur->zPath = sqlite3_mprintf("%s", zDir);
7948   }
7949 
7950   if( pCur->zPath==0 ){
7951     return SQLITE_NOMEM;
7952   }
7953   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
7954     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
7955     return SQLITE_ERROR;
7956   }
7957 
7958   return SQLITE_OK;
7959 }
7960 
7961 /*
7962 ** SQLite will invoke this method one or more times while planning a query
7963 ** that uses the generate_series virtual table.  This routine needs to create
7964 ** a query plan for each invocation and compute an estimated cost for that
7965 ** plan.
7966 **
7967 ** In this implementation idxNum is used to represent the
7968 ** query plan.  idxStr is unused.
7969 **
7970 ** The query plan is represented by values of idxNum:
7971 **
7972 **  (1)  The path value is supplied by argv[0]
7973 **  (2)  Path is in argv[0] and dir is in argv[1]
7974 */
fsdirBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdxInfo)7975 static int fsdirBestIndex(
7976   sqlite3_vtab *tab,
7977   sqlite3_index_info *pIdxInfo
7978 ){
7979   int i;                 /* Loop over constraints */
7980   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
7981   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
7982   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
7983   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
7984   const struct sqlite3_index_constraint *pConstraint;
7985 
7986   (void)tab;
7987   pConstraint = pIdxInfo->aConstraint;
7988   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
7989     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
7990     switch( pConstraint->iColumn ){
7991       case FSDIR_COLUMN_PATH: {
7992         if( pConstraint->usable ){
7993           idxPath = i;
7994           seenPath = 0;
7995         }else if( idxPath<0 ){
7996           seenPath = 1;
7997         }
7998         break;
7999       }
8000       case FSDIR_COLUMN_DIR: {
8001         if( pConstraint->usable ){
8002           idxDir = i;
8003           seenDir = 0;
8004         }else if( idxDir<0 ){
8005           seenDir = 1;
8006         }
8007         break;
8008       }
8009     }
8010   }
8011   if( seenPath || seenDir ){
8012     /* If input parameters are unusable, disallow this plan */
8013     return SQLITE_CONSTRAINT;
8014   }
8015 
8016   if( idxPath<0 ){
8017     pIdxInfo->idxNum = 0;
8018     /* The pIdxInfo->estimatedCost should have been initialized to a huge
8019     ** number.  Leave it unchanged. */
8020     pIdxInfo->estimatedRows = 0x7fffffff;
8021   }else{
8022     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
8023     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
8024     if( idxDir>=0 ){
8025       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
8026       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
8027       pIdxInfo->idxNum = 2;
8028       pIdxInfo->estimatedCost = 10.0;
8029     }else{
8030       pIdxInfo->idxNum = 1;
8031       pIdxInfo->estimatedCost = 100.0;
8032     }
8033   }
8034 
8035   return SQLITE_OK;
8036 }
8037 
8038 /*
8039 ** Register the "fsdir" virtual table.
8040 */
fsdirRegister(sqlite3 * db)8041 static int fsdirRegister(sqlite3 *db){
8042   static sqlite3_module fsdirModule = {
8043     0,                         /* iVersion */
8044     0,                         /* xCreate */
8045     fsdirConnect,              /* xConnect */
8046     fsdirBestIndex,            /* xBestIndex */
8047     fsdirDisconnect,           /* xDisconnect */
8048     0,                         /* xDestroy */
8049     fsdirOpen,                 /* xOpen - open a cursor */
8050     fsdirClose,                /* xClose - close a cursor */
8051     fsdirFilter,               /* xFilter - configure scan constraints */
8052     fsdirNext,                 /* xNext - advance a cursor */
8053     fsdirEof,                  /* xEof - check for end of scan */
8054     fsdirColumn,               /* xColumn - read data */
8055     fsdirRowid,                /* xRowid - read data */
8056     0,                         /* xUpdate */
8057     0,                         /* xBegin */
8058     0,                         /* xSync */
8059     0,                         /* xCommit */
8060     0,                         /* xRollback */
8061     0,                         /* xFindMethod */
8062     0,                         /* xRename */
8063     0,                         /* xSavepoint */
8064     0,                         /* xRelease */
8065     0,                         /* xRollbackTo */
8066     0,                         /* xShadowName */
8067     0                          /* xIntegrity */
8068   };
8069 
8070   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
8071   return rc;
8072 }
8073 #else         /* SQLITE_OMIT_VIRTUALTABLE */
8074 # define fsdirRegister(x) SQLITE_OK
8075 #endif
8076 
8077 #ifdef _WIN32
8078 
8079 #endif
sqlite3_fileio_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)8080 int sqlite3_fileio_init(
8081   sqlite3 *db,
8082   char **pzErrMsg,
8083   const sqlite3_api_routines *pApi
8084 ){
8085   int rc = SQLITE_OK;
8086   SQLITE_EXTENSION_INIT2(pApi);
8087   (void)pzErrMsg;  /* Unused parameter */
8088   rc = sqlite3_create_function(db, "readfile", 1,
8089                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
8090                                readfileFunc, 0, 0);
8091   if( rc==SQLITE_OK ){
8092     rc = sqlite3_create_function(db, "writefile", -1,
8093                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
8094                                  writefileFunc, 0, 0);
8095   }
8096   if( rc==SQLITE_OK ){
8097     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
8098                                  lsModeFunc, 0, 0);
8099   }
8100   if( rc==SQLITE_OK ){
8101     rc = fsdirRegister(db);
8102   }
8103   return rc;
8104 }
8105 
8106 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
8107 /* To allow a standalone DLL, make test_windirent.c use the same
8108  * redefined SQLite API calls as the above extension code does.
8109  * Just pull in this .c to accomplish this. As a beneficial side
8110  * effect, this extension becomes a single translation unit. */
8111 #  include "test_windirent.c"
8112 #endif
8113 
8114 /************************* End ../ext/misc/fileio.c ********************/
8115 /************************* Begin ../ext/misc/completion.c ******************/
8116 /*
8117 ** 2017-07-10
8118 **
8119 ** The author disclaims copyright to this source code.  In place of
8120 ** a legal notice, here is a blessing:
8121 **
8122 **    May you do good and not evil.
8123 **    May you find forgiveness for yourself and forgive others.
8124 **    May you share freely, never taking more than you give.
8125 **
8126 *************************************************************************
8127 **
8128 ** This file implements an eponymous virtual table that returns suggested
8129 ** completions for a partial SQL input.
8130 **
8131 ** Suggested usage:
8132 **
8133 **     SELECT DISTINCT candidate COLLATE nocase
8134 **       FROM completion($prefix,$wholeline)
8135 **      ORDER BY 1;
8136 **
8137 ** The two query parameters are optional.  $prefix is the text of the
8138 ** current word being typed and that is to be completed.  $wholeline is
8139 ** the complete input line, used for context.
8140 **
8141 ** The raw completion() table might return the same candidate multiple
8142 ** times, for example if the same column name is used to two or more
8143 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
8144 ** the DISTINCT and ORDER BY are recommended.
8145 **
8146 ** This virtual table operates at the speed of human typing, and so there
8147 ** is no attempt to make it fast.  Even a slow implementation will be much
8148 ** faster than any human can type.
8149 **
8150 */
8151 /* #include "sqlite3ext.h" */
8152 SQLITE_EXTENSION_INIT1
8153 #include <assert.h>
8154 #include <string.h>
8155 #include <ctype.h>
8156 
8157 #ifndef SQLITE_OMIT_VIRTUALTABLE
8158 
8159 /* completion_vtab is a subclass of sqlite3_vtab which will
8160 ** serve as the underlying representation of a completion virtual table
8161 */
8162 typedef struct completion_vtab completion_vtab;
8163 struct completion_vtab {
8164   sqlite3_vtab base;  /* Base class - must be first */
8165   sqlite3 *db;        /* Database connection for this completion vtab */
8166 };
8167 
8168 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
8169 ** serve as the underlying representation of a cursor that scans
8170 ** over rows of the result
8171 */
8172 typedef struct completion_cursor completion_cursor;
8173 struct completion_cursor {
8174   sqlite3_vtab_cursor base;  /* Base class - must be first */
8175   sqlite3 *db;               /* Database connection for this cursor */
8176   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
8177   char *zPrefix;             /* The prefix for the word we want to complete */
8178   char *zLine;               /* The whole that we want to complete */
8179   const char *zCurrentRow;   /* Current output row */
8180   int szRow;                 /* Length of the zCurrentRow string */
8181   sqlite3_stmt *pStmt;       /* Current statement */
8182   sqlite3_int64 iRowid;      /* The rowid */
8183   int ePhase;                /* Current phase */
8184   int j;                     /* inter-phase counter */
8185 };
8186 
8187 /* Values for ePhase:
8188 */
8189 #define COMPLETION_FIRST_PHASE   1
8190 #define COMPLETION_KEYWORDS      1
8191 #define COMPLETION_PRAGMAS       2
8192 #define COMPLETION_FUNCTIONS     3
8193 #define COMPLETION_COLLATIONS    4
8194 #define COMPLETION_INDEXES       5
8195 #define COMPLETION_TRIGGERS      6
8196 #define COMPLETION_DATABASES     7
8197 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
8198 #define COMPLETION_COLUMNS       9
8199 #define COMPLETION_MODULES       10
8200 #define COMPLETION_EOF           11
8201 
8202 /*
8203 ** The completionConnect() method is invoked to create a new
8204 ** completion_vtab that describes the completion virtual table.
8205 **
8206 ** Think of this routine as the constructor for completion_vtab objects.
8207 **
8208 ** All this routine needs to do is:
8209 **
8210 **    (1) Allocate the completion_vtab object and initialize all fields.
8211 **
8212 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
8213 **        result set of queries against completion will look like.
8214 */
completionConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)8215 static int completionConnect(
8216   sqlite3 *db,
8217   void *pAux,
8218   int argc, const char *const*argv,
8219   sqlite3_vtab **ppVtab,
8220   char **pzErr
8221 ){
8222   completion_vtab *pNew;
8223   int rc;
8224 
8225   (void)(pAux);    /* Unused parameter */
8226   (void)(argc);    /* Unused parameter */
8227   (void)(argv);    /* Unused parameter */
8228   (void)(pzErr);   /* Unused parameter */
8229 
8230 /* Column numbers */
8231 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
8232 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
8233 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
8234 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
8235 
8236   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
8237   rc = sqlite3_declare_vtab(db,
8238       "CREATE TABLE x("
8239       "  candidate TEXT,"
8240       "  prefix TEXT HIDDEN,"
8241       "  wholeline TEXT HIDDEN,"
8242       "  phase INT HIDDEN"        /* Used for debugging only */
8243       ")");
8244   if( rc==SQLITE_OK ){
8245     pNew = sqlite3_malloc( sizeof(*pNew) );
8246     *ppVtab = (sqlite3_vtab*)pNew;
8247     if( pNew==0 ) return SQLITE_NOMEM;
8248     memset(pNew, 0, sizeof(*pNew));
8249     pNew->db = db;
8250   }
8251   return rc;
8252 }
8253 
8254 /*
8255 ** This method is the destructor for completion_cursor objects.
8256 */
completionDisconnect(sqlite3_vtab * pVtab)8257 static int completionDisconnect(sqlite3_vtab *pVtab){
8258   sqlite3_free(pVtab);
8259   return SQLITE_OK;
8260 }
8261 
8262 /*
8263 ** Constructor for a new completion_cursor object.
8264 */
completionOpen(sqlite3_vtab * p,sqlite3_vtab_cursor ** ppCursor)8265 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
8266   completion_cursor *pCur;
8267   pCur = sqlite3_malloc( sizeof(*pCur) );
8268   if( pCur==0 ) return SQLITE_NOMEM;
8269   memset(pCur, 0, sizeof(*pCur));
8270   pCur->db = ((completion_vtab*)p)->db;
8271   *ppCursor = &pCur->base;
8272   return SQLITE_OK;
8273 }
8274 
8275 /*
8276 ** Reset the completion_cursor.
8277 */
completionCursorReset(completion_cursor * pCur)8278 static void completionCursorReset(completion_cursor *pCur){
8279   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
8280   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
8281   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
8282   pCur->j = 0;
8283 }
8284 
8285 /*
8286 ** Destructor for a completion_cursor.
8287 */
completionClose(sqlite3_vtab_cursor * cur)8288 static int completionClose(sqlite3_vtab_cursor *cur){
8289   completionCursorReset((completion_cursor*)cur);
8290   sqlite3_free(cur);
8291   return SQLITE_OK;
8292 }
8293 
8294 /*
8295 ** Advance a completion_cursor to its next row of output.
8296 **
8297 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
8298 ** record the current state of the scan.  This routine sets ->zCurrentRow
8299 ** to the current row of output and then returns.  If no more rows remain,
8300 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
8301 ** table that has reached the end of its scan.
8302 **
8303 ** The current implementation just lists potential identifiers and
8304 ** keywords and filters them by zPrefix.  Future enhancements should
8305 ** take zLine into account to try to restrict the set of identifiers and
8306 ** keywords based on what would be legal at the current point of input.
8307 */
completionNext(sqlite3_vtab_cursor * cur)8308 static int completionNext(sqlite3_vtab_cursor *cur){
8309   completion_cursor *pCur = (completion_cursor*)cur;
8310   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
8311   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
8312   pCur->iRowid++;
8313   while( pCur->ePhase!=COMPLETION_EOF ){
8314     switch( pCur->ePhase ){
8315       case COMPLETION_KEYWORDS: {
8316         if( pCur->j >= sqlite3_keyword_count() ){
8317           pCur->zCurrentRow = 0;
8318           pCur->ePhase = COMPLETION_DATABASES;
8319         }else{
8320           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
8321         }
8322         iCol = -1;
8323         break;
8324       }
8325       case COMPLETION_DATABASES: {
8326         if( pCur->pStmt==0 ){
8327           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
8328                              &pCur->pStmt, 0);
8329         }
8330         iCol = 1;
8331         eNextPhase = COMPLETION_TABLES;
8332         break;
8333       }
8334       case COMPLETION_TABLES: {
8335         if( pCur->pStmt==0 ){
8336           sqlite3_stmt *pS2;
8337           char *zSql = 0;
8338           const char *zSep = "";
8339           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
8340           while( sqlite3_step(pS2)==SQLITE_ROW ){
8341             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8342             zSql = sqlite3_mprintf(
8343                "%z%s"
8344                "SELECT name FROM \"%w\".sqlite_schema",
8345                zSql, zSep, zDb
8346             );
8347             if( zSql==0 ) return SQLITE_NOMEM;
8348             zSep = " UNION ";
8349           }
8350           sqlite3_finalize(pS2);
8351           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
8352           sqlite3_free(zSql);
8353         }
8354         iCol = 0;
8355         eNextPhase = COMPLETION_COLUMNS;
8356         break;
8357       }
8358       case COMPLETION_COLUMNS: {
8359         if( pCur->pStmt==0 ){
8360           sqlite3_stmt *pS2;
8361           char *zSql = 0;
8362           const char *zSep = "";
8363           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
8364           while( sqlite3_step(pS2)==SQLITE_ROW ){
8365             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8366             zSql = sqlite3_mprintf(
8367                "%z%s"
8368                "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
8369                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
8370                " WHERE sm.type='table'",
8371                zSql, zSep, zDb, zDb
8372             );
8373             if( zSql==0 ) return SQLITE_NOMEM;
8374             zSep = " UNION ";
8375           }
8376           sqlite3_finalize(pS2);
8377           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
8378           sqlite3_free(zSql);
8379         }
8380         iCol = 0;
8381         eNextPhase = COMPLETION_EOF;
8382         break;
8383       }
8384     }
8385     if( iCol<0 ){
8386       /* This case is when the phase presets zCurrentRow */
8387       if( pCur->zCurrentRow==0 ) continue;
8388     }else{
8389       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
8390         /* Extract the next row of content */
8391         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
8392         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
8393       }else{
8394         /* When all rows are finished, advance to the next phase */
8395         sqlite3_finalize(pCur->pStmt);
8396         pCur->pStmt = 0;
8397         pCur->ePhase = eNextPhase;
8398         continue;
8399       }
8400     }
8401     if( pCur->nPrefix==0 ) break;
8402     if( pCur->nPrefix<=pCur->szRow
8403      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
8404     ){
8405       break;
8406     }
8407   }
8408 
8409   return SQLITE_OK;
8410 }
8411 
8412 /*
8413 ** Return values of columns for the row at which the completion_cursor
8414 ** is currently pointing.
8415 */
completionColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)8416 static int completionColumn(
8417   sqlite3_vtab_cursor *cur,   /* The cursor */
8418   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
8419   int i                       /* Which column to return */
8420 ){
8421   completion_cursor *pCur = (completion_cursor*)cur;
8422   switch( i ){
8423     case COMPLETION_COLUMN_CANDIDATE: {
8424       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
8425       break;
8426     }
8427     case COMPLETION_COLUMN_PREFIX: {
8428       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
8429       break;
8430     }
8431     case COMPLETION_COLUMN_WHOLELINE: {
8432       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
8433       break;
8434     }
8435     case COMPLETION_COLUMN_PHASE: {
8436       sqlite3_result_int(ctx, pCur->ePhase);
8437       break;
8438     }
8439   }
8440   return SQLITE_OK;
8441 }
8442 
8443 /*
8444 ** Return the rowid for the current row.  In this implementation, the
8445 ** rowid is the same as the output value.
8446 */
completionRowid(sqlite3_vtab_cursor * cur,sqlite_int64 * pRowid)8447 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8448   completion_cursor *pCur = (completion_cursor*)cur;
8449   *pRowid = pCur->iRowid;
8450   return SQLITE_OK;
8451 }
8452 
8453 /*
8454 ** Return TRUE if the cursor has been moved off of the last
8455 ** row of output.
8456 */
completionEof(sqlite3_vtab_cursor * cur)8457 static int completionEof(sqlite3_vtab_cursor *cur){
8458   completion_cursor *pCur = (completion_cursor*)cur;
8459   return pCur->ePhase >= COMPLETION_EOF;
8460 }
8461 
8462 /*
8463 ** This method is called to "rewind" the completion_cursor object back
8464 ** to the first row of output.  This method is always called at least
8465 ** once prior to any call to completionColumn() or completionRowid() or
8466 ** completionEof().
8467 */
completionFilter(sqlite3_vtab_cursor * pVtabCursor,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)8468 static int completionFilter(
8469   sqlite3_vtab_cursor *pVtabCursor,
8470   int idxNum, const char *idxStr,
8471   int argc, sqlite3_value **argv
8472 ){
8473   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
8474   int iArg = 0;
8475   (void)(idxStr);   /* Unused parameter */
8476   (void)(argc);     /* Unused parameter */
8477   completionCursorReset(pCur);
8478   if( idxNum & 1 ){
8479     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
8480     if( pCur->nPrefix>0 ){
8481       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8482       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8483     }
8484     iArg = 1;
8485   }
8486   if( idxNum & 2 ){
8487     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
8488     if( pCur->nLine>0 ){
8489       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8490       if( pCur->zLine==0 ) return SQLITE_NOMEM;
8491     }
8492   }
8493   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
8494     int i = pCur->nLine;
8495     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
8496       i--;
8497     }
8498     pCur->nPrefix = pCur->nLine - i;
8499     if( pCur->nPrefix>0 ){
8500       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
8501       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8502     }
8503   }
8504   pCur->iRowid = 0;
8505   pCur->ePhase = COMPLETION_FIRST_PHASE;
8506   return completionNext(pVtabCursor);
8507 }
8508 
8509 /*
8510 ** SQLite will invoke this method one or more times while planning a query
8511 ** that uses the completion virtual table.  This routine needs to create
8512 ** a query plan for each invocation and compute an estimated cost for that
8513 ** plan.
8514 **
8515 ** There are two hidden parameters that act as arguments to the table-valued
8516 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
8517 ** is available and bit 1 is set if "wholeline" is available.
8518 */
completionBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdxInfo)8519 static int completionBestIndex(
8520   sqlite3_vtab *tab,
8521   sqlite3_index_info *pIdxInfo
8522 ){
8523   int i;                 /* Loop over constraints */
8524   int idxNum = 0;        /* The query plan bitmask */
8525   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
8526   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
8527   int nArg = 0;          /* Number of arguments that completeFilter() expects */
8528   const struct sqlite3_index_constraint *pConstraint;
8529 
8530   (void)(tab);    /* Unused parameter */
8531   pConstraint = pIdxInfo->aConstraint;
8532   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
8533     if( pConstraint->usable==0 ) continue;
8534     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
8535     switch( pConstraint->iColumn ){
8536       case COMPLETION_COLUMN_PREFIX:
8537         prefixIdx = i;
8538         idxNum |= 1;
8539         break;
8540       case COMPLETION_COLUMN_WHOLELINE:
8541         wholelineIdx = i;
8542         idxNum |= 2;
8543         break;
8544     }
8545   }
8546   if( prefixIdx>=0 ){
8547     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
8548     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
8549   }
8550   if( wholelineIdx>=0 ){
8551     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
8552     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
8553   }
8554   pIdxInfo->idxNum = idxNum;
8555   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
8556   pIdxInfo->estimatedRows = 500 - 100*nArg;
8557   return SQLITE_OK;
8558 }
8559 
8560 /*
8561 ** This following structure defines all the methods for the
8562 ** completion virtual table.
8563 */
8564 static sqlite3_module completionModule = {
8565   0,                         /* iVersion */
8566   0,                         /* xCreate */
8567   completionConnect,         /* xConnect */
8568   completionBestIndex,       /* xBestIndex */
8569   completionDisconnect,      /* xDisconnect */
8570   0,                         /* xDestroy */
8571   completionOpen,            /* xOpen - open a cursor */
8572   completionClose,           /* xClose - close a cursor */
8573   completionFilter,          /* xFilter - configure scan constraints */
8574   completionNext,            /* xNext - advance a cursor */
8575   completionEof,             /* xEof - check for end of scan */
8576   completionColumn,          /* xColumn - read data */
8577   completionRowid,           /* xRowid - read data */
8578   0,                         /* xUpdate */
8579   0,                         /* xBegin */
8580   0,                         /* xSync */
8581   0,                         /* xCommit */
8582   0,                         /* xRollback */
8583   0,                         /* xFindMethod */
8584   0,                         /* xRename */
8585   0,                         /* xSavepoint */
8586   0,                         /* xRelease */
8587   0,                         /* xRollbackTo */
8588   0,                         /* xShadowName */
8589   0                          /* xIntegrity */
8590 };
8591 
8592 #endif /* SQLITE_OMIT_VIRTUALTABLE */
8593 
sqlite3CompletionVtabInit(sqlite3 * db)8594 int sqlite3CompletionVtabInit(sqlite3 *db){
8595   int rc = SQLITE_OK;
8596 #ifndef SQLITE_OMIT_VIRTUALTABLE
8597   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
8598 #endif
8599   return rc;
8600 }
8601 
8602 #ifdef _WIN32
8603 
8604 #endif
sqlite3_completion_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)8605 int sqlite3_completion_init(
8606   sqlite3 *db,
8607   char **pzErrMsg,
8608   const sqlite3_api_routines *pApi
8609 ){
8610   int rc = SQLITE_OK;
8611   SQLITE_EXTENSION_INIT2(pApi);
8612   (void)(pzErrMsg);  /* Unused parameter */
8613 #ifndef SQLITE_OMIT_VIRTUALTABLE
8614   rc = sqlite3CompletionVtabInit(db);
8615 #endif
8616   return rc;
8617 }
8618 
8619 /************************* End ../ext/misc/completion.c ********************/
8620 /************************* Begin ../ext/misc/appendvfs.c ******************/
8621 /*
8622 ** 2017-10-20
8623 **
8624 ** The author disclaims copyright to this source code.  In place of
8625 ** a legal notice, here is a blessing:
8626 **
8627 **    May you do good and not evil.
8628 **    May you find forgiveness for yourself and forgive others.
8629 **    May you share freely, never taking more than you give.
8630 **
8631 ******************************************************************************
8632 **
8633 ** This file implements a VFS shim that allows an SQLite database to be
8634 ** appended onto the end of some other file, such as an executable.
8635 **
8636 ** A special record must appear at the end of the file that identifies the
8637 ** file as an appended database and provides the offset to the first page
8638 ** of the exposed content. (Or, it is the length of the content prefix.)
8639 ** For best performance page 1 should be located at a disk page boundary,
8640 ** though that is not required.
8641 **
8642 ** When opening a database using this VFS, the connection might treat
8643 ** the file as an ordinary SQLite database, or it might treat it as a
8644 ** database appended onto some other file.  The decision is made by
8645 ** applying the following rules in order:
8646 **
8647 **  (1)  An empty file is an ordinary database.
8648 **
8649 **  (2)  If the file ends with the appendvfs trailer string
8650 **       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
8651 **
8652 **  (3)  If the file begins with the standard SQLite prefix string
8653 **       "SQLite format 3", that file is an ordinary database.
8654 **
8655 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
8656 **       set, then a new database is appended to the already existing file.
8657 **
8658 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
8659 **
8660 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
8661 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
8662 ** This VFS will not read or write past the 1GiB mark.  This restriction
8663 ** might be lifted in future versions.  For now, if you need a larger
8664 ** database, then keep it in a separate file.
8665 **
8666 ** If the file being opened is a plain database (not an appended one), then
8667 ** this shim is a pass-through into the default underlying VFS. (rule 3)
8668 **/
8669 /* #include "sqlite3ext.h" */
8670 SQLITE_EXTENSION_INIT1
8671 #include <string.h>
8672 #include <assert.h>
8673 
8674 /* The append mark at the end of the database is:
8675 **
8676 **     Start-Of-SQLite3-NNNNNNNN
8677 **     123456789 123456789 12345
8678 **
8679 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
8680 ** the offset to page 1, and also the length of the prefix content.
8681 */
8682 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
8683 #define APND_MARK_PREFIX_SZ  17
8684 #define APND_MARK_FOS_SZ      8
8685 #define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
8686 
8687 /*
8688 ** Maximum size of the combined prefix + database + append-mark.  This
8689 ** must be less than 0x40000000 to avoid locking issues on Windows.
8690 */
8691 #define APND_MAX_SIZE  (0x40000000)
8692 
8693 /*
8694 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
8695 */
8696 #ifndef APND_ROUNDUP
8697 #define APND_ROUNDUP 4096
8698 #endif
8699 #define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
8700 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
8701 
8702 /*
8703 ** Forward declaration of objects used by this utility
8704 */
8705 typedef struct sqlite3_vfs ApndVfs;
8706 typedef struct ApndFile ApndFile;
8707 
8708 /* Access to a lower-level VFS that (might) implement dynamic loading,
8709 ** access to randomness, etc.
8710 */
8711 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
8712 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
8713 
8714 /* An open appendvfs file
8715 **
8716 ** An instance of this structure describes the appended database file.
8717 ** A separate sqlite3_file object is always appended. The appended
8718 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
8719 ** the entire file, including the prefix, the database, and the
8720 ** append-mark.
8721 **
8722 ** The structure of an AppendVFS database is like this:
8723 **
8724 **   +-------------+---------+----------+-------------+
8725 **   | prefix-file | padding | database | append-mark |
8726 **   +-------------+---------+----------+-------------+
8727 **                           ^          ^
8728 **                           |          |
8729 **                         iPgOne      iMark
8730 **
8731 **
8732 ** "prefix file" -  file onto which the database has been appended.
8733 ** "padding"     -  zero or more bytes inserted so that "database"
8734 **                  starts on an APND_ROUNDUP boundary
8735 ** "database"    -  The SQLite database file
8736 ** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
8737 **                  the offset from the start of prefix-file to the start
8738 **                  of "database".
8739 **
8740 ** The size of the database is iMark - iPgOne.
8741 **
8742 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
8743 ** of iPgOne stored as a big-ending 64-bit integer.
8744 **
8745 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
8746 ** Or, iMark is -1 to indicate that it has not yet been written.
8747 */
8748 struct ApndFile {
8749   sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
8750   sqlite3_int64 iPgOne;     /* Offset to the start of the database */
8751   sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
8752   /* Always followed by another sqlite3_file that describes the whole file */
8753 };
8754 
8755 /*
8756 ** Methods for ApndFile
8757 */
8758 static int apndClose(sqlite3_file*);
8759 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
8760 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
8761 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
8762 static int apndSync(sqlite3_file*, int flags);
8763 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
8764 static int apndLock(sqlite3_file*, int);
8765 static int apndUnlock(sqlite3_file*, int);
8766 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
8767 static int apndFileControl(sqlite3_file*, int op, void *pArg);
8768 static int apndSectorSize(sqlite3_file*);
8769 static int apndDeviceCharacteristics(sqlite3_file*);
8770 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
8771 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
8772 static void apndShmBarrier(sqlite3_file*);
8773 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
8774 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
8775 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
8776 
8777 /*
8778 ** Methods for ApndVfs
8779 */
8780 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
8781 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
8782 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
8783 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
8784 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
8785 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
8786 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
8787 static void apndDlClose(sqlite3_vfs*, void*);
8788 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
8789 static int apndSleep(sqlite3_vfs*, int microseconds);
8790 static int apndCurrentTime(sqlite3_vfs*, double*);
8791 static int apndGetLastError(sqlite3_vfs*, int, char *);
8792 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
8793 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
8794 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
8795 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
8796 
8797 static sqlite3_vfs apnd_vfs = {
8798   3,                            /* iVersion (set when registered) */
8799   0,                            /* szOsFile (set when registered) */
8800   1024,                         /* mxPathname */
8801   0,                            /* pNext */
8802   "apndvfs",                    /* zName */
8803   0,                            /* pAppData (set when registered) */
8804   apndOpen,                     /* xOpen */
8805   apndDelete,                   /* xDelete */
8806   apndAccess,                   /* xAccess */
8807   apndFullPathname,             /* xFullPathname */
8808   apndDlOpen,                   /* xDlOpen */
8809   apndDlError,                  /* xDlError */
8810   apndDlSym,                    /* xDlSym */
8811   apndDlClose,                  /* xDlClose */
8812   apndRandomness,               /* xRandomness */
8813   apndSleep,                    /* xSleep */
8814   apndCurrentTime,              /* xCurrentTime */
8815   apndGetLastError,             /* xGetLastError */
8816   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
8817   apndSetSystemCall,            /* xSetSystemCall */
8818   apndGetSystemCall,            /* xGetSystemCall */
8819   apndNextSystemCall            /* xNextSystemCall */
8820 };
8821 
8822 static const sqlite3_io_methods apnd_io_methods = {
8823   3,                              /* iVersion */
8824   apndClose,                      /* xClose */
8825   apndRead,                       /* xRead */
8826   apndWrite,                      /* xWrite */
8827   apndTruncate,                   /* xTruncate */
8828   apndSync,                       /* xSync */
8829   apndFileSize,                   /* xFileSize */
8830   apndLock,                       /* xLock */
8831   apndUnlock,                     /* xUnlock */
8832   apndCheckReservedLock,          /* xCheckReservedLock */
8833   apndFileControl,                /* xFileControl */
8834   apndSectorSize,                 /* xSectorSize */
8835   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
8836   apndShmMap,                     /* xShmMap */
8837   apndShmLock,                    /* xShmLock */
8838   apndShmBarrier,                 /* xShmBarrier */
8839   apndShmUnmap,                   /* xShmUnmap */
8840   apndFetch,                      /* xFetch */
8841   apndUnfetch                     /* xUnfetch */
8842 };
8843 
8844 /*
8845 ** Close an apnd-file.
8846 */
apndClose(sqlite3_file * pFile)8847 static int apndClose(sqlite3_file *pFile){
8848   pFile = ORIGFILE(pFile);
8849   return pFile->pMethods->xClose(pFile);
8850 }
8851 
8852 /*
8853 ** Read data from an apnd-file.
8854 */
apndRead(sqlite3_file * pFile,void * zBuf,int iAmt,sqlite_int64 iOfst)8855 static int apndRead(
8856   sqlite3_file *pFile,
8857   void *zBuf,
8858   int iAmt,
8859   sqlite_int64 iOfst
8860 ){
8861   ApndFile *paf = (ApndFile *)pFile;
8862   pFile = ORIGFILE(pFile);
8863   return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
8864 }
8865 
8866 /*
8867 ** Add the append-mark onto what should become the end of the file.
8868 *  If and only if this succeeds, internal ApndFile.iMark is updated.
8869 *  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
8870 */
apndWriteMark(ApndFile * paf,sqlite3_file * pFile,sqlite_int64 iWriteEnd)8871 static int apndWriteMark(
8872   ApndFile *paf,
8873   sqlite3_file *pFile,
8874   sqlite_int64 iWriteEnd
8875 ){
8876   sqlite_int64 iPgOne = paf->iPgOne;
8877   unsigned char a[APND_MARK_SIZE];
8878   int i = APND_MARK_FOS_SZ;
8879   int rc;
8880   assert(pFile == ORIGFILE(paf));
8881   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
8882   while( --i >= 0 ){
8883     a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
8884     iPgOne >>= 8;
8885   }
8886   iWriteEnd += paf->iPgOne;
8887   if( SQLITE_OK==(rc = pFile->pMethods->xWrite
8888                   (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
8889     paf->iMark = iWriteEnd;
8890   }
8891   return rc;
8892 }
8893 
8894 /*
8895 ** Write data to an apnd-file.
8896 */
apndWrite(sqlite3_file * pFile,const void * zBuf,int iAmt,sqlite_int64 iOfst)8897 static int apndWrite(
8898   sqlite3_file *pFile,
8899   const void *zBuf,
8900   int iAmt,
8901   sqlite_int64 iOfst
8902 ){
8903   ApndFile *paf = (ApndFile *)pFile;
8904   sqlite_int64 iWriteEnd = iOfst + iAmt;
8905   if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
8906   pFile = ORIGFILE(pFile);
8907   /* If append-mark is absent or will be overwritten, write it. */
8908   if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
8909     int rc = apndWriteMark(paf, pFile, iWriteEnd);
8910     if( SQLITE_OK!=rc ) return rc;
8911   }
8912   return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
8913 }
8914 
8915 /*
8916 ** Truncate an apnd-file.
8917 */
apndTruncate(sqlite3_file * pFile,sqlite_int64 size)8918 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
8919   ApndFile *paf = (ApndFile *)pFile;
8920   pFile = ORIGFILE(pFile);
8921   /* The append mark goes out first so truncate failure does not lose it. */
8922   if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
8923   /* Truncate underlying file just past append mark */
8924   return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
8925 }
8926 
8927 /*
8928 ** Sync an apnd-file.
8929 */
apndSync(sqlite3_file * pFile,int flags)8930 static int apndSync(sqlite3_file *pFile, int flags){
8931   pFile = ORIGFILE(pFile);
8932   return pFile->pMethods->xSync(pFile, flags);
8933 }
8934 
8935 /*
8936 ** Return the current file-size of an apnd-file.
8937 ** If the append mark is not yet there, the file-size is 0.
8938 */
apndFileSize(sqlite3_file * pFile,sqlite_int64 * pSize)8939 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
8940   ApndFile *paf = (ApndFile *)pFile;
8941   *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
8942   return SQLITE_OK;
8943 }
8944 
8945 /*
8946 ** Lock an apnd-file.
8947 */
apndLock(sqlite3_file * pFile,int eLock)8948 static int apndLock(sqlite3_file *pFile, int eLock){
8949   pFile = ORIGFILE(pFile);
8950   return pFile->pMethods->xLock(pFile, eLock);
8951 }
8952 
8953 /*
8954 ** Unlock an apnd-file.
8955 */
apndUnlock(sqlite3_file * pFile,int eLock)8956 static int apndUnlock(sqlite3_file *pFile, int eLock){
8957   pFile = ORIGFILE(pFile);
8958   return pFile->pMethods->xUnlock(pFile, eLock);
8959 }
8960 
8961 /*
8962 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
8963 */
apndCheckReservedLock(sqlite3_file * pFile,int * pResOut)8964 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
8965   pFile = ORIGFILE(pFile);
8966   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
8967 }
8968 
8969 /*
8970 ** File control method. For custom operations on an apnd-file.
8971 */
apndFileControl(sqlite3_file * pFile,int op,void * pArg)8972 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
8973   ApndFile *paf = (ApndFile *)pFile;
8974   int rc;
8975   pFile = ORIGFILE(pFile);
8976   if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
8977   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
8978   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
8979     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
8980   }
8981   return rc;
8982 }
8983 
8984 /*
8985 ** Return the sector-size in bytes for an apnd-file.
8986 */
apndSectorSize(sqlite3_file * pFile)8987 static int apndSectorSize(sqlite3_file *pFile){
8988   pFile = ORIGFILE(pFile);
8989   return pFile->pMethods->xSectorSize(pFile);
8990 }
8991 
8992 /*
8993 ** Return the device characteristic flags supported by an apnd-file.
8994 */
apndDeviceCharacteristics(sqlite3_file * pFile)8995 static int apndDeviceCharacteristics(sqlite3_file *pFile){
8996   pFile = ORIGFILE(pFile);
8997   return pFile->pMethods->xDeviceCharacteristics(pFile);
8998 }
8999 
9000 /* Create a shared memory file mapping */
apndShmMap(sqlite3_file * pFile,int iPg,int pgsz,int bExtend,void volatile ** pp)9001 static int apndShmMap(
9002   sqlite3_file *pFile,
9003   int iPg,
9004   int pgsz,
9005   int bExtend,
9006   void volatile **pp
9007 ){
9008   pFile = ORIGFILE(pFile);
9009   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
9010 }
9011 
9012 /* Perform locking on a shared-memory segment */
apndShmLock(sqlite3_file * pFile,int offset,int n,int flags)9013 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
9014   pFile = ORIGFILE(pFile);
9015   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
9016 }
9017 
9018 /* Memory barrier operation on shared memory */
apndShmBarrier(sqlite3_file * pFile)9019 static void apndShmBarrier(sqlite3_file *pFile){
9020   pFile = ORIGFILE(pFile);
9021   pFile->pMethods->xShmBarrier(pFile);
9022 }
9023 
9024 /* Unmap a shared memory segment */
apndShmUnmap(sqlite3_file * pFile,int deleteFlag)9025 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
9026   pFile = ORIGFILE(pFile);
9027   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
9028 }
9029 
9030 /* Fetch a page of a memory-mapped file */
apndFetch(sqlite3_file * pFile,sqlite3_int64 iOfst,int iAmt,void ** pp)9031 static int apndFetch(
9032   sqlite3_file *pFile,
9033   sqlite3_int64 iOfst,
9034   int iAmt,
9035   void **pp
9036 ){
9037   ApndFile *p = (ApndFile *)pFile;
9038   if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
9039     return SQLITE_IOERR; /* Cannot read what is not yet there. */
9040   }
9041   pFile = ORIGFILE(pFile);
9042   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
9043 }
9044 
9045 /* Release a memory-mapped page */
apndUnfetch(sqlite3_file * pFile,sqlite3_int64 iOfst,void * pPage)9046 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
9047   ApndFile *p = (ApndFile *)pFile;
9048   pFile = ORIGFILE(pFile);
9049   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
9050 }
9051 
9052 /*
9053 ** Try to read the append-mark off the end of a file.  Return the
9054 ** start of the appended database if the append-mark is present.
9055 ** If there is no valid append-mark, return -1;
9056 **
9057 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
9058 ** indicates that the appended database contains at least one page.  The
9059 ** start-of-database value must be a multiple of 512.
9060 */
apndReadMark(sqlite3_int64 sz,sqlite3_file * pFile)9061 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
9062   int rc, i;
9063   sqlite3_int64 iMark;
9064   int msbs = 8 * (APND_MARK_FOS_SZ-1);
9065   unsigned char a[APND_MARK_SIZE];
9066 
9067   if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
9068   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
9069   if( rc ) return -1;
9070   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
9071   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
9072   for(i=1; i<8; i++){
9073     msbs -= 8;
9074     iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
9075   }
9076   if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
9077   if( iMark & 0x1ff ) return -1;
9078   return iMark;
9079 }
9080 
9081 static const char apvfsSqliteHdr[] = "SQLite format 3";
9082 /*
9083 ** Check to see if the file is an appendvfs SQLite database file.
9084 ** Return true iff it is such. Parameter sz is the file's size.
9085 */
apndIsAppendvfsDatabase(sqlite3_int64 sz,sqlite3_file * pFile)9086 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
9087   int rc;
9088   char zHdr[16];
9089   sqlite3_int64 iMark = apndReadMark(sz, pFile);
9090   if( iMark>=0 ){
9091     /* If file has the correct end-marker, the expected odd size, and the
9092     ** SQLite DB type marker where the end-marker puts it, then it
9093     ** is an appendvfs database.
9094     */
9095     rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
9096     if( SQLITE_OK==rc
9097      && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
9098      && (sz & 0x1ff) == APND_MARK_SIZE
9099      && sz>=512+APND_MARK_SIZE
9100     ){
9101       return 1; /* It's an appendvfs database */
9102     }
9103   }
9104   return 0;
9105 }
9106 
9107 /*
9108 ** Check to see if the file is an ordinary SQLite database file.
9109 ** Return true iff so. Parameter sz is the file's size.
9110 */
apndIsOrdinaryDatabaseFile(sqlite3_int64 sz,sqlite3_file * pFile)9111 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
9112   char zHdr[16];
9113   if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
9114    || (sz & 0x1ff) != 0
9115    || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
9116    || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
9117   ){
9118     return 0;
9119   }else{
9120     return 1;
9121   }
9122 }
9123 
9124 /*
9125 ** Open an apnd file handle.
9126 */
apndOpen(sqlite3_vfs * pApndVfs,const char * zName,sqlite3_file * pFile,int flags,int * pOutFlags)9127 static int apndOpen(
9128   sqlite3_vfs *pApndVfs,
9129   const char *zName,
9130   sqlite3_file *pFile,
9131   int flags,
9132   int *pOutFlags
9133 ){
9134   ApndFile *pApndFile = (ApndFile*)pFile;
9135   sqlite3_file *pBaseFile = ORIGFILE(pFile);
9136   sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
9137   int rc;
9138   sqlite3_int64 sz = 0;
9139   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
9140     /* The appendvfs is not to be used for transient or temporary databases.
9141     ** Just use the base VFS open to initialize the given file object and
9142     ** open the underlying file. (Appendvfs is then unused for this file.)
9143     */
9144     return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
9145   }
9146   memset(pApndFile, 0, sizeof(ApndFile));
9147   pFile->pMethods = &apnd_io_methods;
9148   pApndFile->iMark = -1;    /* Append mark not yet written */
9149 
9150   rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
9151   if( rc==SQLITE_OK ){
9152     rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
9153     if( rc ){
9154       pBaseFile->pMethods->xClose(pBaseFile);
9155     }
9156   }
9157   if( rc ){
9158     pFile->pMethods = 0;
9159     return rc;
9160   }
9161   if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
9162     /* The file being opened appears to be just an ordinary DB. Copy
9163     ** the base dispatch-table so this instance mimics the base VFS.
9164     */
9165     memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
9166     return SQLITE_OK;
9167   }
9168   pApndFile->iPgOne = apndReadMark(sz, pFile);
9169   if( pApndFile->iPgOne>=0 ){
9170     pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
9171     return SQLITE_OK;
9172   }
9173   if( (flags & SQLITE_OPEN_CREATE)==0 ){
9174     pBaseFile->pMethods->xClose(pBaseFile);
9175     rc = SQLITE_CANTOPEN;
9176     pFile->pMethods = 0;
9177   }else{
9178     /* Round newly added appendvfs location to #define'd page boundary.
9179     ** Note that nothing has yet been written to the underlying file.
9180     ** The append mark will be written along with first content write.
9181     ** Until then, paf->iMark value indicates it is not yet written.
9182     */
9183     pApndFile->iPgOne = APND_START_ROUNDUP(sz);
9184   }
9185   return rc;
9186 }
9187 
9188 /*
9189 ** Delete an apnd file.
9190 ** For an appendvfs, this could mean delete the appendvfs portion,
9191 ** leaving the appendee as it was before it gained an appendvfs.
9192 ** For now, this code deletes the underlying file too.
9193 */
apndDelete(sqlite3_vfs * pVfs,const char * zPath,int dirSync)9194 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
9195   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
9196 }
9197 
9198 /*
9199 ** All other VFS methods are pass-thrus.
9200 */
apndAccess(sqlite3_vfs * pVfs,const char * zPath,int flags,int * pResOut)9201 static int apndAccess(
9202   sqlite3_vfs *pVfs,
9203   const char *zPath,
9204   int flags,
9205   int *pResOut
9206 ){
9207   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
9208 }
apndFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nOut,char * zOut)9209 static int apndFullPathname(
9210   sqlite3_vfs *pVfs,
9211   const char *zPath,
9212   int nOut,
9213   char *zOut
9214 ){
9215   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
9216 }
apndDlOpen(sqlite3_vfs * pVfs,const char * zPath)9217 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
9218   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
9219 }
apndDlError(sqlite3_vfs * pVfs,int nByte,char * zErrMsg)9220 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
9221   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
9222 }
apndDlSym(sqlite3_vfs * pVfs,void * p,const char * zSym)9223 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
9224   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
9225 }
apndDlClose(sqlite3_vfs * pVfs,void * pHandle)9226 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
9227   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
9228 }
apndRandomness(sqlite3_vfs * pVfs,int nByte,char * zBufOut)9229 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
9230   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
9231 }
apndSleep(sqlite3_vfs * pVfs,int nMicro)9232 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
9233   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
9234 }
apndCurrentTime(sqlite3_vfs * pVfs,double * pTimeOut)9235 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
9236   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
9237 }
apndGetLastError(sqlite3_vfs * pVfs,int a,char * b)9238 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
9239   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
9240 }
apndCurrentTimeInt64(sqlite3_vfs * pVfs,sqlite3_int64 * p)9241 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
9242   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
9243 }
apndSetSystemCall(sqlite3_vfs * pVfs,const char * zName,sqlite3_syscall_ptr pCall)9244 static int apndSetSystemCall(
9245   sqlite3_vfs *pVfs,
9246   const char *zName,
9247   sqlite3_syscall_ptr pCall
9248 ){
9249   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
9250 }
apndGetSystemCall(sqlite3_vfs * pVfs,const char * zName)9251 static sqlite3_syscall_ptr apndGetSystemCall(
9252   sqlite3_vfs *pVfs,
9253   const char *zName
9254 ){
9255   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
9256 }
apndNextSystemCall(sqlite3_vfs * pVfs,const char * zName)9257 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
9258   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
9259 }
9260 
9261 
9262 #ifdef _WIN32
9263 
9264 #endif
9265 /*
9266 ** This routine is called when the extension is loaded.
9267 ** Register the new VFS.
9268 */
sqlite3_appendvfs_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)9269 int sqlite3_appendvfs_init(
9270   sqlite3 *db,
9271   char **pzErrMsg,
9272   const sqlite3_api_routines *pApi
9273 ){
9274   int rc = SQLITE_OK;
9275   sqlite3_vfs *pOrig;
9276   SQLITE_EXTENSION_INIT2(pApi);
9277   (void)pzErrMsg;
9278   (void)db;
9279   pOrig = sqlite3_vfs_find(0);
9280   if( pOrig==0 ) return SQLITE_ERROR;
9281   apnd_vfs.iVersion = pOrig->iVersion;
9282   apnd_vfs.pAppData = pOrig;
9283   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
9284   rc = sqlite3_vfs_register(&apnd_vfs, 0);
9285 #ifdef APPENDVFS_TEST
9286   if( rc==SQLITE_OK ){
9287     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
9288   }
9289 #endif
9290   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
9291   return rc;
9292 }
9293 
9294 /************************* End ../ext/misc/appendvfs.c ********************/
9295 #endif
9296 #ifdef SQLITE_HAVE_ZLIB
9297 /************************* Begin ../ext/misc/zipfile.c ******************/
9298 /*
9299 ** 2017-12-26
9300 **
9301 ** The author disclaims copyright to this source code.  In place of
9302 ** a legal notice, here is a blessing:
9303 **
9304 **    May you do good and not evil.
9305 **    May you find forgiveness for yourself and forgive others.
9306 **    May you share freely, never taking more than you give.
9307 **
9308 ******************************************************************************
9309 **
9310 ** This file implements a virtual table for reading and writing ZIP archive
9311 ** files.
9312 **
9313 ** Usage example:
9314 **
9315 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
9316 **
9317 ** Current limitations:
9318 **
9319 **    *  No support for encryption
9320 **    *  No support for ZIP archives spanning multiple files
9321 **    *  No support for zip64 extensions
9322 **    *  Only the "inflate/deflate" (zlib) compression method is supported
9323 */
9324 /* #include "sqlite3ext.h" */
9325 SQLITE_EXTENSION_INIT1
9326 #include <stdio.h>
9327 #include <string.h>
9328 #include <assert.h>
9329 #include <stdint.h>
9330 
9331 #include <zlib.h>
9332 
9333 #ifndef SQLITE_OMIT_VIRTUALTABLE
9334 
9335 #ifndef SQLITE_AMALGAMATION
9336 
9337 #ifndef UINT32_TYPE
9338 # ifdef HAVE_UINT32_T
9339 #  define UINT32_TYPE uint32_t
9340 # else
9341 #  define UINT32_TYPE unsigned int
9342 # endif
9343 #endif
9344 #ifndef UINT16_TYPE
9345 # ifdef HAVE_UINT16_T
9346 #  define UINT16_TYPE uint16_t
9347 # else
9348 #  define UINT16_TYPE unsigned short int
9349 # endif
9350 #endif
9351 /* typedef sqlite3_int64 i64; */
9352 /* typedef unsigned char u8; */
9353 /* typedef UINT32_TYPE u32;           // 4-byte unsigned integer // */
9354 /* typedef UINT16_TYPE u16;           // 2-byte unsigned integer // */
9355 #define MIN(a,b) ((a)<(b) ? (a) : (b))
9356 
9357 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
9358 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
9359 #endif
9360 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
9361 # define ALWAYS(X)      (1)
9362 # define NEVER(X)       (0)
9363 #elif !defined(NDEBUG)
9364 # define ALWAYS(X)      ((X)?1:(assert(0),0))
9365 # define NEVER(X)       ((X)?(assert(0),1):0)
9366 #else
9367 # define ALWAYS(X)      (X)
9368 # define NEVER(X)       (X)
9369 #endif
9370 
9371 #endif   /* SQLITE_AMALGAMATION */
9372 
9373 /*
9374 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
9375 **
9376 ** In some ways it would be better to obtain these values from system
9377 ** header files. But, the dependency is undesirable and (a) these
9378 ** have been stable for decades, (b) the values are part of POSIX and
9379 ** are also made explicit in [man stat], and (c) are part of the
9380 ** file format for zip archives.
9381 */
9382 #ifndef S_IFDIR
9383 # define S_IFDIR 0040000
9384 #endif
9385 #ifndef S_IFREG
9386 # define S_IFREG 0100000
9387 #endif
9388 #ifndef S_IFLNK
9389 # define S_IFLNK 0120000
9390 #endif
9391 
9392 static const char ZIPFILE_SCHEMA[] =
9393   "CREATE TABLE y("
9394     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
9395     "mode,"              /* 1: POSIX mode for file */
9396     "mtime,"             /* 2: Last modification time (secs since 1970)*/
9397     "sz,"                /* 3: Size of object */
9398     "rawdata,"           /* 4: Raw data */
9399     "data,"              /* 5: Uncompressed data */
9400     "method,"            /* 6: Compression method (integer) */
9401     "z HIDDEN"           /* 7: Name of zip file */
9402   ") WITHOUT ROWID;";
9403 
9404 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
9405 #define ZIPFILE_BUFFER_SIZE (64*1024)
9406 
9407 
9408 /*
9409 ** Magic numbers used to read and write zip files.
9410 **
9411 ** ZIPFILE_NEWENTRY_MADEBY:
9412 **   Use this value for the "version-made-by" field in new zip file
9413 **   entries. The upper byte indicates "unix", and the lower byte
9414 **   indicates that the zip file matches pkzip specification 3.0.
9415 **   This is what info-zip seems to do.
9416 **
9417 ** ZIPFILE_NEWENTRY_REQUIRED:
9418 **   Value for "version-required-to-extract" field of new entries.
9419 **   Version 2.0 is required to support folders and deflate compression.
9420 **
9421 ** ZIPFILE_NEWENTRY_FLAGS:
9422 **   Value for "general-purpose-bit-flags" field of new entries. Bit
9423 **   11 means "utf-8 filename and comment".
9424 **
9425 ** ZIPFILE_SIGNATURE_CDS:
9426 **   First 4 bytes of a valid CDS record.
9427 **
9428 ** ZIPFILE_SIGNATURE_LFH:
9429 **   First 4 bytes of a valid LFH record.
9430 **
9431 ** ZIPFILE_SIGNATURE_EOCD
9432 **   First 4 bytes of a valid EOCD record.
9433 */
9434 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
9435 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
9436 #define ZIPFILE_NEWENTRY_REQUIRED 20
9437 #define ZIPFILE_NEWENTRY_FLAGS    0x800
9438 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
9439 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
9440 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
9441 
9442 /*
9443 ** The sizes of the fixed-size part of each of the three main data
9444 ** structures in a zip archive.
9445 */
9446 #define ZIPFILE_LFH_FIXED_SZ      30
9447 #define ZIPFILE_EOCD_FIXED_SZ     22
9448 #define ZIPFILE_CDS_FIXED_SZ      46
9449 
9450 /*
9451 *** 4.3.16  End of central directory record:
9452 ***
9453 ***   end of central dir signature    4 bytes  (0x06054b50)
9454 ***   number of this disk             2 bytes
9455 ***   number of the disk with the
9456 ***   start of the central directory  2 bytes
9457 ***   total number of entries in the
9458 ***   central directory on this disk  2 bytes
9459 ***   total number of entries in
9460 ***   the central directory           2 bytes
9461 ***   size of the central directory   4 bytes
9462 ***   offset of start of central
9463 ***   directory with respect to
9464 ***   the starting disk number        4 bytes
9465 ***   .ZIP file comment length        2 bytes
9466 ***   .ZIP file comment       (variable size)
9467 */
9468 typedef struct ZipfileEOCD ZipfileEOCD;
9469 struct ZipfileEOCD {
9470   u16 iDisk;
9471   u16 iFirstDisk;
9472   u16 nEntry;
9473   u16 nEntryTotal;
9474   u32 nSize;
9475   u32 iOffset;
9476 };
9477 
9478 /*
9479 *** 4.3.12  Central directory structure:
9480 ***
9481 *** ...
9482 ***
9483 ***   central file header signature   4 bytes  (0x02014b50)
9484 ***   version made by                 2 bytes
9485 ***   version needed to extract       2 bytes
9486 ***   general purpose bit flag        2 bytes
9487 ***   compression method              2 bytes
9488 ***   last mod file time              2 bytes
9489 ***   last mod file date              2 bytes
9490 ***   crc-32                          4 bytes
9491 ***   compressed size                 4 bytes
9492 ***   uncompressed size               4 bytes
9493 ***   file name length                2 bytes
9494 ***   extra field length              2 bytes
9495 ***   file comment length             2 bytes
9496 ***   disk number start               2 bytes
9497 ***   internal file attributes        2 bytes
9498 ***   external file attributes        4 bytes
9499 ***   relative offset of local header 4 bytes
9500 */
9501 typedef struct ZipfileCDS ZipfileCDS;
9502 struct ZipfileCDS {
9503   u16 iVersionMadeBy;
9504   u16 iVersionExtract;
9505   u16 flags;
9506   u16 iCompression;
9507   u16 mTime;
9508   u16 mDate;
9509   u32 crc32;
9510   u32 szCompressed;
9511   u32 szUncompressed;
9512   u16 nFile;
9513   u16 nExtra;
9514   u16 nComment;
9515   u16 iDiskStart;
9516   u16 iInternalAttr;
9517   u32 iExternalAttr;
9518   u32 iOffset;
9519   char *zFile;                    /* Filename (sqlite3_malloc()) */
9520 };
9521 
9522 /*
9523 *** 4.3.7  Local file header:
9524 ***
9525 ***   local file header signature     4 bytes  (0x04034b50)
9526 ***   version needed to extract       2 bytes
9527 ***   general purpose bit flag        2 bytes
9528 ***   compression method              2 bytes
9529 ***   last mod file time              2 bytes
9530 ***   last mod file date              2 bytes
9531 ***   crc-32                          4 bytes
9532 ***   compressed size                 4 bytes
9533 ***   uncompressed size               4 bytes
9534 ***   file name length                2 bytes
9535 ***   extra field length              2 bytes
9536 ***
9537 */
9538 typedef struct ZipfileLFH ZipfileLFH;
9539 struct ZipfileLFH {
9540   u16 iVersionExtract;
9541   u16 flags;
9542   u16 iCompression;
9543   u16 mTime;
9544   u16 mDate;
9545   u32 crc32;
9546   u32 szCompressed;
9547   u32 szUncompressed;
9548   u16 nFile;
9549   u16 nExtra;
9550 };
9551 
9552 typedef struct ZipfileEntry ZipfileEntry;
9553 struct ZipfileEntry {
9554   ZipfileCDS cds;            /* Parsed CDS record */
9555   u32 mUnixTime;             /* Modification time, in UNIX format */
9556   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
9557   i64 iDataOff;              /* Offset to data in file (if aData==0) */
9558   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
9559   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
9560 };
9561 
9562 /*
9563 ** Cursor type for zipfile tables.
9564 */
9565 typedef struct ZipfileCsr ZipfileCsr;
9566 struct ZipfileCsr {
9567   sqlite3_vtab_cursor base;  /* Base class - must be first */
9568   i64 iId;                   /* Cursor ID */
9569   u8 bEof;                   /* True when at EOF */
9570   u8 bNoop;                  /* If next xNext() call is no-op */
9571 
9572   /* Used outside of write transactions */
9573   FILE *pFile;               /* Zip file */
9574   i64 iNextOff;              /* Offset of next record in central directory */
9575   ZipfileEOCD eocd;          /* Parse of central directory record */
9576 
9577   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
9578   ZipfileEntry *pCurrent;    /* Current entry */
9579   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
9580 };
9581 
9582 typedef struct ZipfileTab ZipfileTab;
9583 struct ZipfileTab {
9584   sqlite3_vtab base;         /* Base class - must be first */
9585   char *zFile;               /* Zip file this table accesses (may be NULL) */
9586   sqlite3 *db;               /* Host database connection */
9587   u8 *aBuffer;               /* Temporary buffer used for various tasks */
9588 
9589   ZipfileCsr *pCsrList;      /* List of cursors */
9590   i64 iNextCsrid;
9591 
9592   /* The following are used by write transactions only */
9593   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
9594   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
9595   FILE *pWriteFd;            /* File handle open on zip archive */
9596   i64 szCurrent;             /* Current size of zip archive */
9597   i64 szOrig;                /* Size of archive at start of transaction */
9598 };
9599 
9600 /*
9601 ** Set the error message contained in context ctx to the results of
9602 ** vprintf(zFmt, ...).
9603 */
zipfileCtxErrorMsg(sqlite3_context * ctx,const char * zFmt,...)9604 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
9605   char *zMsg = 0;
9606   va_list ap;
9607   va_start(ap, zFmt);
9608   zMsg = sqlite3_vmprintf(zFmt, ap);
9609   sqlite3_result_error(ctx, zMsg, -1);
9610   sqlite3_free(zMsg);
9611   va_end(ap);
9612 }
9613 
9614 /*
9615 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
9616 ** is not quoted, do nothing.
9617 */
zipfileDequote(char * zIn)9618 static void zipfileDequote(char *zIn){
9619   char q = zIn[0];
9620   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
9621     int iIn = 1;
9622     int iOut = 0;
9623     if( q=='[' ) q = ']';
9624     while( ALWAYS(zIn[iIn]) ){
9625       char c = zIn[iIn++];
9626       if( c==q && zIn[iIn++]!=q ) break;
9627       zIn[iOut++] = c;
9628     }
9629     zIn[iOut] = '\0';
9630   }
9631 }
9632 
9633 /*
9634 ** Construct a new ZipfileTab virtual table object.
9635 **
9636 **   argv[0]   -> module name  ("zipfile")
9637 **   argv[1]   -> database name
9638 **   argv[2]   -> table name
9639 **   argv[...] -> "column name" and other module argument fields.
9640 */
zipfileConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)9641 static int zipfileConnect(
9642   sqlite3 *db,
9643   void *pAux,
9644   int argc, const char *const*argv,
9645   sqlite3_vtab **ppVtab,
9646   char **pzErr
9647 ){
9648   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
9649   int nFile = 0;
9650   const char *zFile = 0;
9651   ZipfileTab *pNew = 0;
9652   int rc;
9653   (void)pAux;
9654 
9655   /* If the table name is not "zipfile", require that the argument be
9656   ** specified. This stops zipfile tables from being created as:
9657   **
9658   **   CREATE VIRTUAL TABLE zzz USING zipfile();
9659   **
9660   ** It does not prevent:
9661   **
9662   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
9663   */
9664   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
9665   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
9666     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
9667     return SQLITE_ERROR;
9668   }
9669 
9670   if( argc>3 ){
9671     zFile = argv[3];
9672     nFile = (int)strlen(zFile)+1;
9673   }
9674 
9675   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
9676   if( rc==SQLITE_OK ){
9677     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
9678     if( pNew==0 ) return SQLITE_NOMEM;
9679     memset(pNew, 0, nByte+nFile);
9680     pNew->db = db;
9681     pNew->aBuffer = (u8*)&pNew[1];
9682     if( zFile ){
9683       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
9684       memcpy(pNew->zFile, zFile, nFile);
9685       zipfileDequote(pNew->zFile);
9686     }
9687   }
9688   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
9689   *ppVtab = (sqlite3_vtab*)pNew;
9690   return rc;
9691 }
9692 
9693 /*
9694 ** Free the ZipfileEntry structure indicated by the only argument.
9695 */
zipfileEntryFree(ZipfileEntry * p)9696 static void zipfileEntryFree(ZipfileEntry *p){
9697   if( p ){
9698     sqlite3_free(p->cds.zFile);
9699     sqlite3_free(p);
9700   }
9701 }
9702 
9703 /*
9704 ** Release resources that should be freed at the end of a write
9705 ** transaction.
9706 */
zipfileCleanupTransaction(ZipfileTab * pTab)9707 static void zipfileCleanupTransaction(ZipfileTab *pTab){
9708   ZipfileEntry *pEntry;
9709   ZipfileEntry *pNext;
9710 
9711   if( pTab->pWriteFd ){
9712     fclose(pTab->pWriteFd);
9713     pTab->pWriteFd = 0;
9714   }
9715   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
9716     pNext = pEntry->pNext;
9717     zipfileEntryFree(pEntry);
9718   }
9719   pTab->pFirstEntry = 0;
9720   pTab->pLastEntry = 0;
9721   pTab->szCurrent = 0;
9722   pTab->szOrig = 0;
9723 }
9724 
9725 /*
9726 ** This method is the destructor for zipfile vtab objects.
9727 */
zipfileDisconnect(sqlite3_vtab * pVtab)9728 static int zipfileDisconnect(sqlite3_vtab *pVtab){
9729   zipfileCleanupTransaction((ZipfileTab*)pVtab);
9730   sqlite3_free(pVtab);
9731   return SQLITE_OK;
9732 }
9733 
9734 /*
9735 ** Constructor for a new ZipfileCsr object.
9736 */
zipfileOpen(sqlite3_vtab * p,sqlite3_vtab_cursor ** ppCsr)9737 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
9738   ZipfileTab *pTab = (ZipfileTab*)p;
9739   ZipfileCsr *pCsr;
9740   pCsr = sqlite3_malloc(sizeof(*pCsr));
9741   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
9742   if( pCsr==0 ){
9743     return SQLITE_NOMEM;
9744   }
9745   memset(pCsr, 0, sizeof(*pCsr));
9746   pCsr->iId = ++pTab->iNextCsrid;
9747   pCsr->pCsrNext = pTab->pCsrList;
9748   pTab->pCsrList = pCsr;
9749   return SQLITE_OK;
9750 }
9751 
9752 /*
9753 ** Reset a cursor back to the state it was in when first returned
9754 ** by zipfileOpen().
9755 */
zipfileResetCursor(ZipfileCsr * pCsr)9756 static void zipfileResetCursor(ZipfileCsr *pCsr){
9757   ZipfileEntry *p;
9758   ZipfileEntry *pNext;
9759 
9760   pCsr->bEof = 0;
9761   if( pCsr->pFile ){
9762     fclose(pCsr->pFile);
9763     pCsr->pFile = 0;
9764     zipfileEntryFree(pCsr->pCurrent);
9765     pCsr->pCurrent = 0;
9766   }
9767 
9768   for(p=pCsr->pFreeEntry; p; p=pNext){
9769     pNext = p->pNext;
9770     zipfileEntryFree(p);
9771   }
9772 }
9773 
9774 /*
9775 ** Destructor for an ZipfileCsr.
9776 */
zipfileClose(sqlite3_vtab_cursor * cur)9777 static int zipfileClose(sqlite3_vtab_cursor *cur){
9778   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
9779   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
9780   ZipfileCsr **pp;
9781   zipfileResetCursor(pCsr);
9782 
9783   /* Remove this cursor from the ZipfileTab.pCsrList list. */
9784   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
9785   *pp = pCsr->pCsrNext;
9786 
9787   sqlite3_free(pCsr);
9788   return SQLITE_OK;
9789 }
9790 
9791 /*
9792 ** Set the error message for the virtual table associated with cursor
9793 ** pCsr to the results of vprintf(zFmt, ...).
9794 */
zipfileTableErr(ZipfileTab * pTab,const char * zFmt,...)9795 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
9796   va_list ap;
9797   va_start(ap, zFmt);
9798   sqlite3_free(pTab->base.zErrMsg);
9799   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
9800   va_end(ap);
9801 }
zipfileCursorErr(ZipfileCsr * pCsr,const char * zFmt,...)9802 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
9803   va_list ap;
9804   va_start(ap, zFmt);
9805   sqlite3_free(pCsr->base.pVtab->zErrMsg);
9806   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
9807   va_end(ap);
9808 }
9809 
9810 /*
9811 ** Read nRead bytes of data from offset iOff of file pFile into buffer
9812 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
9813 ** otherwise.
9814 **
9815 ** If an error does occur, output variable (*pzErrmsg) may be set to point
9816 ** to an English language error message. It is the responsibility of the
9817 ** caller to eventually free this buffer using
9818 ** sqlite3_free().
9819 */
zipfileReadData(FILE * pFile,u8 * aRead,int nRead,i64 iOff,char ** pzErrmsg)9820 static int zipfileReadData(
9821   FILE *pFile,                    /* Read from this file */
9822   u8 *aRead,                      /* Read into this buffer */
9823   int nRead,                      /* Number of bytes to read */
9824   i64 iOff,                       /* Offset to read from */
9825   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
9826 ){
9827   size_t n;
9828   fseek(pFile, (long)iOff, SEEK_SET);
9829   n = fread(aRead, 1, nRead, pFile);
9830   if( (int)n!=nRead ){
9831     *pzErrmsg = sqlite3_mprintf("error in fread()");
9832     return SQLITE_ERROR;
9833   }
9834   return SQLITE_OK;
9835 }
9836 
zipfileAppendData(ZipfileTab * pTab,const u8 * aWrite,int nWrite)9837 static int zipfileAppendData(
9838   ZipfileTab *pTab,
9839   const u8 *aWrite,
9840   int nWrite
9841 ){
9842   if( nWrite>0 ){
9843     size_t n = nWrite;
9844     fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
9845     n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
9846     if( (int)n!=nWrite ){
9847       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
9848       return SQLITE_ERROR;
9849     }
9850     pTab->szCurrent += nWrite;
9851   }
9852   return SQLITE_OK;
9853 }
9854 
9855 /*
9856 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
9857 */
zipfileGetU16(const u8 * aBuf)9858 static u16 zipfileGetU16(const u8 *aBuf){
9859   return (aBuf[1] << 8) + aBuf[0];
9860 }
9861 
9862 /*
9863 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
9864 */
zipfileGetU32(const u8 * aBuf)9865 static u32 zipfileGetU32(const u8 *aBuf){
9866   if( aBuf==0 ) return 0;
9867   return ((u32)(aBuf[3]) << 24)
9868        + ((u32)(aBuf[2]) << 16)
9869        + ((u32)(aBuf[1]) <<  8)
9870        + ((u32)(aBuf[0]) <<  0);
9871 }
9872 
9873 /*
9874 ** Write a 16-bit little endiate integer into buffer aBuf.
9875 */
zipfilePutU16(u8 * aBuf,u16 val)9876 static void zipfilePutU16(u8 *aBuf, u16 val){
9877   aBuf[0] = val & 0xFF;
9878   aBuf[1] = (val>>8) & 0xFF;
9879 }
9880 
9881 /*
9882 ** Write a 32-bit little endiate integer into buffer aBuf.
9883 */
zipfilePutU32(u8 * aBuf,u32 val)9884 static void zipfilePutU32(u8 *aBuf, u32 val){
9885   aBuf[0] = val & 0xFF;
9886   aBuf[1] = (val>>8) & 0xFF;
9887   aBuf[2] = (val>>16) & 0xFF;
9888   aBuf[3] = (val>>24) & 0xFF;
9889 }
9890 
9891 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
9892 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
9893 
9894 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
9895 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
9896 
9897 /*
9898 ** Magic numbers used to read CDS records.
9899 */
9900 #define ZIPFILE_CDS_NFILE_OFF        28
9901 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
9902 
9903 /*
9904 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
9905 ** if the record is not well-formed, or SQLITE_OK otherwise.
9906 */
zipfileReadCDS(u8 * aBuf,ZipfileCDS * pCDS)9907 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
9908   u8 *aRead = aBuf;
9909   u32 sig = zipfileRead32(aRead);
9910   int rc = SQLITE_OK;
9911   if( sig!=ZIPFILE_SIGNATURE_CDS ){
9912     rc = SQLITE_ERROR;
9913   }else{
9914     pCDS->iVersionMadeBy = zipfileRead16(aRead);
9915     pCDS->iVersionExtract = zipfileRead16(aRead);
9916     pCDS->flags = zipfileRead16(aRead);
9917     pCDS->iCompression = zipfileRead16(aRead);
9918     pCDS->mTime = zipfileRead16(aRead);
9919     pCDS->mDate = zipfileRead16(aRead);
9920     pCDS->crc32 = zipfileRead32(aRead);
9921     pCDS->szCompressed = zipfileRead32(aRead);
9922     pCDS->szUncompressed = zipfileRead32(aRead);
9923     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
9924     pCDS->nFile = zipfileRead16(aRead);
9925     pCDS->nExtra = zipfileRead16(aRead);
9926     pCDS->nComment = zipfileRead16(aRead);
9927     pCDS->iDiskStart = zipfileRead16(aRead);
9928     pCDS->iInternalAttr = zipfileRead16(aRead);
9929     pCDS->iExternalAttr = zipfileRead32(aRead);
9930     pCDS->iOffset = zipfileRead32(aRead);
9931     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
9932   }
9933 
9934   return rc;
9935 }
9936 
9937 /*
9938 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
9939 ** if the record is not well-formed, or SQLITE_OK otherwise.
9940 */
zipfileReadLFH(u8 * aBuffer,ZipfileLFH * pLFH)9941 static int zipfileReadLFH(
9942   u8 *aBuffer,
9943   ZipfileLFH *pLFH
9944 ){
9945   u8 *aRead = aBuffer;
9946   int rc = SQLITE_OK;
9947 
9948   u32 sig = zipfileRead32(aRead);
9949   if( sig!=ZIPFILE_SIGNATURE_LFH ){
9950     rc = SQLITE_ERROR;
9951   }else{
9952     pLFH->iVersionExtract = zipfileRead16(aRead);
9953     pLFH->flags = zipfileRead16(aRead);
9954     pLFH->iCompression = zipfileRead16(aRead);
9955     pLFH->mTime = zipfileRead16(aRead);
9956     pLFH->mDate = zipfileRead16(aRead);
9957     pLFH->crc32 = zipfileRead32(aRead);
9958     pLFH->szCompressed = zipfileRead32(aRead);
9959     pLFH->szUncompressed = zipfileRead32(aRead);
9960     pLFH->nFile = zipfileRead16(aRead);
9961     pLFH->nExtra = zipfileRead16(aRead);
9962   }
9963   return rc;
9964 }
9965 
9966 
9967 /*
9968 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
9969 ** Scan through this buffer to find an "extra-timestamp" field. If one
9970 ** exists, extract the 32-bit modification-timestamp from it and store
9971 ** the value in output parameter *pmTime.
9972 **
9973 ** Zero is returned if no extra-timestamp record could be found (and so
9974 ** *pmTime is left unchanged), or non-zero otherwise.
9975 **
9976 ** The general format of an extra field is:
9977 **
9978 **   Header ID    2 bytes
9979 **   Data Size    2 bytes
9980 **   Data         N bytes
9981 */
zipfileScanExtra(u8 * aExtra,int nExtra,u32 * pmTime)9982 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
9983   int ret = 0;
9984   u8 *p = aExtra;
9985   u8 *pEnd = &aExtra[nExtra];
9986 
9987   while( p<pEnd ){
9988     u16 id = zipfileRead16(p);
9989     u16 nByte = zipfileRead16(p);
9990 
9991     switch( id ){
9992       case ZIPFILE_EXTRA_TIMESTAMP: {
9993         u8 b = p[0];
9994         if( b & 0x01 ){     /* 0x01 -> modtime is present */
9995           *pmTime = zipfileGetU32(&p[1]);
9996           ret = 1;
9997         }
9998         break;
9999       }
10000     }
10001 
10002     p += nByte;
10003   }
10004   return ret;
10005 }
10006 
10007 /*
10008 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
10009 ** fields of the CDS structure passed as the only argument to a 32-bit
10010 ** UNIX seconds-since-the-epoch timestamp. Return the result.
10011 **
10012 ** "Standard" MS-DOS time format:
10013 **
10014 **   File modification time:
10015 **     Bits 00-04: seconds divided by 2
10016 **     Bits 05-10: minute
10017 **     Bits 11-15: hour
10018 **   File modification date:
10019 **     Bits 00-04: day
10020 **     Bits 05-08: month (1-12)
10021 **     Bits 09-15: years from 1980
10022 **
10023 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
10024 */
zipfileMtime(ZipfileCDS * pCDS)10025 static u32 zipfileMtime(ZipfileCDS *pCDS){
10026   int Y,M,D,X1,X2,A,B,sec,min,hr;
10027   i64 JDsec;
10028   Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
10029   M = ((pCDS->mDate >> 5) & 0x0F);
10030   D = (pCDS->mDate & 0x1F);
10031   sec = (pCDS->mTime & 0x1F)*2;
10032   min = (pCDS->mTime >> 5) & 0x3F;
10033   hr = (pCDS->mTime >> 11) & 0x1F;
10034   if( M<=2 ){
10035     Y--;
10036     M += 12;
10037   }
10038   X1 = 36525*(Y+4716)/100;
10039   X2 = 306001*(M+1)/10000;
10040   A = Y/100;
10041   B = 2 - A + (A/4);
10042   JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
10043   return (u32)(JDsec - (i64)24405875*(i64)8640);
10044 }
10045 
10046 /*
10047 ** The opposite of zipfileMtime(). This function populates the mTime and
10048 ** mDate fields of the CDS structure passed as the first argument according
10049 ** to the UNIX timestamp value passed as the second.
10050 */
zipfileMtimeToDos(ZipfileCDS * pCds,u32 mUnixTime)10051 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
10052   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
10053   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
10054 
10055   int A, B, C, D, E;
10056   int yr, mon, day;
10057   int hr, min, sec;
10058 
10059   A = (int)((JD - 1867216.25)/36524.25);
10060   A = (int)(JD + 1 + A - (A/4));
10061   B = A + 1524;
10062   C = (int)((B - 122.1)/365.25);
10063   D = (36525*(C&32767))/100;
10064   E = (int)((B-D)/30.6001);
10065 
10066   day = B - D - (int)(30.6001*E);
10067   mon = (E<14 ? E-1 : E-13);
10068   yr = mon>2 ? C-4716 : C-4715;
10069 
10070   hr = (mUnixTime % (24*60*60)) / (60*60);
10071   min = (mUnixTime % (60*60)) / 60;
10072   sec = (mUnixTime % 60);
10073 
10074   if( yr>=1980 ){
10075     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
10076     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
10077   }else{
10078     pCds->mDate = pCds->mTime = 0;
10079   }
10080 
10081   assert( mUnixTime<315507600
10082        || mUnixTime==zipfileMtime(pCds)
10083        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
10084        /* || (mUnixTime % 2) */
10085   );
10086 }
10087 
10088 /*
10089 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
10090 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
10091 ** then pFile is a file-handle open on a zip file. In either case, this
10092 ** function creates a ZipfileEntry object based on the zip archive entry
10093 ** for which the CDS record is at offset iOff.
10094 **
10095 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
10096 ** the new object. Otherwise, an SQLite error code is returned and the
10097 ** final value of (*ppEntry) undefined.
10098 */
zipfileGetEntry(ZipfileTab * pTab,const u8 * aBlob,int nBlob,FILE * pFile,i64 iOff,ZipfileEntry ** ppEntry)10099 static int zipfileGetEntry(
10100   ZipfileTab *pTab,               /* Store any error message here */
10101   const u8 *aBlob,                /* Pointer to in-memory file image */
10102   int nBlob,                      /* Size of aBlob[] in bytes */
10103   FILE *pFile,                    /* If aBlob==0, read from this file */
10104   i64 iOff,                       /* Offset of CDS record */
10105   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
10106 ){
10107   u8 *aRead;
10108   char **pzErr = &pTab->base.zErrMsg;
10109   int rc = SQLITE_OK;
10110   (void)nBlob;
10111 
10112   if( aBlob==0 ){
10113     aRead = pTab->aBuffer;
10114     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
10115   }else{
10116     aRead = (u8*)&aBlob[iOff];
10117   }
10118 
10119   if( rc==SQLITE_OK ){
10120     sqlite3_int64 nAlloc;
10121     ZipfileEntry *pNew;
10122 
10123     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
10124     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
10125     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
10126 
10127     nAlloc = sizeof(ZipfileEntry) + nExtra;
10128     if( aBlob ){
10129       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
10130     }
10131 
10132     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
10133     if( pNew==0 ){
10134       rc = SQLITE_NOMEM;
10135     }else{
10136       memset(pNew, 0, sizeof(ZipfileEntry));
10137       rc = zipfileReadCDS(aRead, &pNew->cds);
10138       if( rc!=SQLITE_OK ){
10139         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
10140       }else if( aBlob==0 ){
10141         rc = zipfileReadData(
10142             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
10143         );
10144       }else{
10145         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
10146       }
10147     }
10148 
10149     if( rc==SQLITE_OK ){
10150       u32 *pt = &pNew->mUnixTime;
10151       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
10152       pNew->aExtra = (u8*)&pNew[1];
10153       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
10154       if( pNew->cds.zFile==0 ){
10155         rc = SQLITE_NOMEM;
10156       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
10157         pNew->mUnixTime = zipfileMtime(&pNew->cds);
10158       }
10159     }
10160 
10161     if( rc==SQLITE_OK ){
10162       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
10163       ZipfileLFH lfh;
10164       if( pFile ){
10165         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
10166       }else{
10167         aRead = (u8*)&aBlob[pNew->cds.iOffset];
10168       }
10169 
10170       if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
10171       if( rc==SQLITE_OK ){
10172         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
10173         pNew->iDataOff += lfh.nFile + lfh.nExtra;
10174         if( aBlob && pNew->cds.szCompressed ){
10175           pNew->aData = &pNew->aExtra[nExtra];
10176           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
10177         }
10178       }else{
10179         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
10180             (int)pNew->cds.iOffset
10181         );
10182       }
10183     }
10184 
10185     if( rc!=SQLITE_OK ){
10186       zipfileEntryFree(pNew);
10187     }else{
10188       *ppEntry = pNew;
10189     }
10190   }
10191 
10192   return rc;
10193 }
10194 
10195 /*
10196 ** Advance an ZipfileCsr to its next row of output.
10197 */
zipfileNext(sqlite3_vtab_cursor * cur)10198 static int zipfileNext(sqlite3_vtab_cursor *cur){
10199   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10200   int rc = SQLITE_OK;
10201 
10202   if( pCsr->pFile ){
10203     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
10204     zipfileEntryFree(pCsr->pCurrent);
10205     pCsr->pCurrent = 0;
10206     if( pCsr->iNextOff>=iEof ){
10207       pCsr->bEof = 1;
10208     }else{
10209       ZipfileEntry *p = 0;
10210       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
10211       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
10212       if( rc==SQLITE_OK ){
10213         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
10214         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
10215       }
10216       pCsr->pCurrent = p;
10217     }
10218   }else{
10219     if( !pCsr->bNoop ){
10220       pCsr->pCurrent = pCsr->pCurrent->pNext;
10221     }
10222     if( pCsr->pCurrent==0 ){
10223       pCsr->bEof = 1;
10224     }
10225   }
10226 
10227   pCsr->bNoop = 0;
10228   return rc;
10229 }
10230 
zipfileFree(void * p)10231 static void zipfileFree(void *p) {
10232   sqlite3_free(p);
10233 }
10234 
10235 /*
10236 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
10237 ** size is nOut bytes. This function uncompresses the data and sets the
10238 ** return value in context pCtx to the result (a blob).
10239 **
10240 ** If an error occurs, an error code is left in pCtx instead.
10241 */
zipfileInflate(sqlite3_context * pCtx,const u8 * aIn,int nIn,int nOut)10242 static void zipfileInflate(
10243   sqlite3_context *pCtx,          /* Store result here */
10244   const u8 *aIn,                  /* Compressed data */
10245   int nIn,                        /* Size of buffer aIn[] in bytes */
10246   int nOut                        /* Expected output size */
10247 ){
10248   u8 *aRes = sqlite3_malloc(nOut);
10249   if( aRes==0 ){
10250     sqlite3_result_error_nomem(pCtx);
10251   }else{
10252     int err;
10253     z_stream str;
10254     memset(&str, 0, sizeof(str));
10255 
10256     str.next_in = (Byte*)aIn;
10257     str.avail_in = nIn;
10258     str.next_out = (Byte*)aRes;
10259     str.avail_out = nOut;
10260 
10261     err = inflateInit2(&str, -15);
10262     if( err!=Z_OK ){
10263       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
10264     }else{
10265       err = inflate(&str, Z_NO_FLUSH);
10266       if( err!=Z_STREAM_END ){
10267         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
10268       }else{
10269         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
10270         aRes = 0;
10271       }
10272     }
10273     sqlite3_free(aRes);
10274     inflateEnd(&str);
10275   }
10276 }
10277 
10278 /*
10279 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
10280 ** compresses it and sets (*ppOut) to point to a buffer containing the
10281 ** compressed data. The caller is responsible for eventually calling
10282 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
10283 ** is set to the size of buffer (*ppOut) in bytes.
10284 **
10285 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
10286 ** code is returned and an error message left in virtual-table handle
10287 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
10288 ** case.
10289 */
zipfileDeflate(const u8 * aIn,int nIn,u8 ** ppOut,int * pnOut,char ** pzErr)10290 static int zipfileDeflate(
10291   const u8 *aIn, int nIn,         /* Input */
10292   u8 **ppOut, int *pnOut,         /* Output */
10293   char **pzErr                    /* OUT: Error message */
10294 ){
10295   int rc = SQLITE_OK;
10296   sqlite3_int64 nAlloc;
10297   z_stream str;
10298   u8 *aOut;
10299 
10300   memset(&str, 0, sizeof(str));
10301   str.next_in = (Bytef*)aIn;
10302   str.avail_in = nIn;
10303   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
10304 
10305   nAlloc = deflateBound(&str, nIn);
10306   aOut = (u8*)sqlite3_malloc64(nAlloc);
10307   if( aOut==0 ){
10308     rc = SQLITE_NOMEM;
10309   }else{
10310     int res;
10311     str.next_out = aOut;
10312     str.avail_out = nAlloc;
10313     res = deflate(&str, Z_FINISH);
10314     if( res==Z_STREAM_END ){
10315       *ppOut = aOut;
10316       *pnOut = (int)str.total_out;
10317     }else{
10318       sqlite3_free(aOut);
10319       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
10320       rc = SQLITE_ERROR;
10321     }
10322     deflateEnd(&str);
10323   }
10324 
10325   return rc;
10326 }
10327 
10328 
10329 /*
10330 ** Return values of columns for the row at which the series_cursor
10331 ** is currently pointing.
10332 */
zipfileColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)10333 static int zipfileColumn(
10334   sqlite3_vtab_cursor *cur,   /* The cursor */
10335   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
10336   int i                       /* Which column to return */
10337 ){
10338   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10339   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
10340   int rc = SQLITE_OK;
10341   switch( i ){
10342     case 0:   /* name */
10343       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
10344       break;
10345     case 1:   /* mode */
10346       /* TODO: Whether or not the following is correct surely depends on
10347       ** the platform on which the archive was created.  */
10348       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
10349       break;
10350     case 2: { /* mtime */
10351       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
10352       break;
10353     }
10354     case 3: { /* sz */
10355       if( sqlite3_vtab_nochange(ctx)==0 ){
10356         sqlite3_result_int64(ctx, pCDS->szUncompressed);
10357       }
10358       break;
10359     }
10360     case 4:   /* rawdata */
10361       if( sqlite3_vtab_nochange(ctx) ) break;
10362     case 5: { /* data */
10363       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
10364         int sz = pCDS->szCompressed;
10365         int szFinal = pCDS->szUncompressed;
10366         if( szFinal>0 ){
10367           u8 *aBuf;
10368           u8 *aFree = 0;
10369           if( pCsr->pCurrent->aData ){
10370             aBuf = pCsr->pCurrent->aData;
10371           }else{
10372             aBuf = aFree = sqlite3_malloc64(sz);
10373             if( aBuf==0 ){
10374               rc = SQLITE_NOMEM;
10375             }else{
10376               FILE *pFile = pCsr->pFile;
10377               if( pFile==0 ){
10378                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
10379               }
10380               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
10381                   &pCsr->base.pVtab->zErrMsg
10382               );
10383             }
10384           }
10385           if( rc==SQLITE_OK ){
10386             if( i==5 && pCDS->iCompression ){
10387               zipfileInflate(ctx, aBuf, sz, szFinal);
10388             }else{
10389               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
10390             }
10391           }
10392           sqlite3_free(aFree);
10393         }else{
10394           /* Figure out if this is a directory or a zero-sized file. Consider
10395           ** it to be a directory either if the mode suggests so, or if
10396           ** the final character in the name is '/'.  */
10397           u32 mode = pCDS->iExternalAttr >> 16;
10398           if( !(mode & S_IFDIR)
10399            && pCDS->nFile>=1
10400            && pCDS->zFile[pCDS->nFile-1]!='/'
10401           ){
10402             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
10403           }
10404         }
10405       }
10406       break;
10407     }
10408     case 6:   /* method */
10409       sqlite3_result_int(ctx, pCDS->iCompression);
10410       break;
10411     default:  /* z */
10412       assert( i==7 );
10413       sqlite3_result_int64(ctx, pCsr->iId);
10414       break;
10415   }
10416 
10417   return rc;
10418 }
10419 
10420 /*
10421 ** Return TRUE if the cursor is at EOF.
10422 */
zipfileEof(sqlite3_vtab_cursor * cur)10423 static int zipfileEof(sqlite3_vtab_cursor *cur){
10424   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10425   return pCsr->bEof;
10426 }
10427 
10428 /*
10429 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
10430 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
10431 ** is guaranteed to be a file-handle open on a zip file.
10432 **
10433 ** This function attempts to locate the EOCD record within the zip archive
10434 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
10435 ** returned if successful. Otherwise, an SQLite error code is returned and
10436 ** an English language error message may be left in virtual-table pTab.
10437 */
zipfileReadEOCD(ZipfileTab * pTab,const u8 * aBlob,int nBlob,FILE * pFile,ZipfileEOCD * pEOCD)10438 static int zipfileReadEOCD(
10439   ZipfileTab *pTab,               /* Return errors here */
10440   const u8 *aBlob,                /* Pointer to in-memory file image */
10441   int nBlob,                      /* Size of aBlob[] in bytes */
10442   FILE *pFile,                    /* Read from this file if aBlob==0 */
10443   ZipfileEOCD *pEOCD              /* Object to populate */
10444 ){
10445   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
10446   int nRead;                      /* Bytes to read from file */
10447   int rc = SQLITE_OK;
10448 
10449   memset(pEOCD, 0, sizeof(ZipfileEOCD));
10450   if( aBlob==0 ){
10451     i64 iOff;                     /* Offset to read from */
10452     i64 szFile;                   /* Total size of file in bytes */
10453     fseek(pFile, 0, SEEK_END);
10454     szFile = (i64)ftell(pFile);
10455     if( szFile==0 ){
10456       return SQLITE_OK;
10457     }
10458     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
10459     iOff = szFile - nRead;
10460     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
10461   }else{
10462     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
10463     aRead = (u8*)&aBlob[nBlob-nRead];
10464   }
10465 
10466   if( rc==SQLITE_OK ){
10467     int i;
10468 
10469     /* Scan backwards looking for the signature bytes */
10470     for(i=nRead-20; i>=0; i--){
10471       if( aRead[i]==0x50 && aRead[i+1]==0x4b
10472        && aRead[i+2]==0x05 && aRead[i+3]==0x06
10473       ){
10474         break;
10475       }
10476     }
10477     if( i<0 ){
10478       pTab->base.zErrMsg = sqlite3_mprintf(
10479           "cannot find end of central directory record"
10480       );
10481       return SQLITE_ERROR;
10482     }
10483 
10484     aRead += i+4;
10485     pEOCD->iDisk = zipfileRead16(aRead);
10486     pEOCD->iFirstDisk = zipfileRead16(aRead);
10487     pEOCD->nEntry = zipfileRead16(aRead);
10488     pEOCD->nEntryTotal = zipfileRead16(aRead);
10489     pEOCD->nSize = zipfileRead32(aRead);
10490     pEOCD->iOffset = zipfileRead32(aRead);
10491   }
10492 
10493   return rc;
10494 }
10495 
10496 /*
10497 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
10498 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
10499 ** to the end of the list. Otherwise, it is added to the list immediately
10500 ** before pBefore (which is guaranteed to be a part of said list).
10501 */
zipfileAddEntry(ZipfileTab * pTab,ZipfileEntry * pBefore,ZipfileEntry * pNew)10502 static void zipfileAddEntry(
10503   ZipfileTab *pTab,
10504   ZipfileEntry *pBefore,
10505   ZipfileEntry *pNew
10506 ){
10507   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
10508   assert( pNew->pNext==0 );
10509   if( pBefore==0 ){
10510     if( pTab->pFirstEntry==0 ){
10511       pTab->pFirstEntry = pTab->pLastEntry = pNew;
10512     }else{
10513       assert( pTab->pLastEntry->pNext==0 );
10514       pTab->pLastEntry->pNext = pNew;
10515       pTab->pLastEntry = pNew;
10516     }
10517   }else{
10518     ZipfileEntry **pp;
10519     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
10520     pNew->pNext = pBefore;
10521     *pp = pNew;
10522   }
10523 }
10524 
zipfileLoadDirectory(ZipfileTab * pTab,const u8 * aBlob,int nBlob)10525 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
10526   ZipfileEOCD eocd;
10527   int rc;
10528   int i;
10529   i64 iOff;
10530 
10531   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
10532   iOff = eocd.iOffset;
10533   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
10534     ZipfileEntry *pNew = 0;
10535     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
10536 
10537     if( rc==SQLITE_OK ){
10538       zipfileAddEntry(pTab, 0, pNew);
10539       iOff += ZIPFILE_CDS_FIXED_SZ;
10540       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
10541     }
10542   }
10543   return rc;
10544 }
10545 
10546 /*
10547 ** xFilter callback.
10548 */
zipfileFilter(sqlite3_vtab_cursor * cur,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)10549 static int zipfileFilter(
10550   sqlite3_vtab_cursor *cur,
10551   int idxNum, const char *idxStr,
10552   int argc, sqlite3_value **argv
10553 ){
10554   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
10555   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
10556   const char *zFile = 0;          /* Zip file to scan */
10557   int rc = SQLITE_OK;             /* Return Code */
10558   int bInMemory = 0;              /* True for an in-memory zipfile */
10559 
10560   (void)idxStr;
10561   (void)argc;
10562 
10563   zipfileResetCursor(pCsr);
10564 
10565   if( pTab->zFile ){
10566     zFile = pTab->zFile;
10567   }else if( idxNum==0 ){
10568     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
10569     return SQLITE_ERROR;
10570   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
10571     static const u8 aEmptyBlob = 0;
10572     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
10573     int nBlob = sqlite3_value_bytes(argv[0]);
10574     assert( pTab->pFirstEntry==0 );
10575     if( aBlob==0 ){
10576       aBlob = &aEmptyBlob;
10577       nBlob = 0;
10578     }
10579     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
10580     pCsr->pFreeEntry = pTab->pFirstEntry;
10581     pTab->pFirstEntry = pTab->pLastEntry = 0;
10582     if( rc!=SQLITE_OK ) return rc;
10583     bInMemory = 1;
10584   }else{
10585     zFile = (const char*)sqlite3_value_text(argv[0]);
10586   }
10587 
10588   if( 0==pTab->pWriteFd && 0==bInMemory ){
10589     pCsr->pFile = zFile ? fopen(zFile, "rb") : 0;
10590     if( pCsr->pFile==0 ){
10591       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
10592       rc = SQLITE_ERROR;
10593     }else{
10594       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
10595       if( rc==SQLITE_OK ){
10596         if( pCsr->eocd.nEntry==0 ){
10597           pCsr->bEof = 1;
10598         }else{
10599           pCsr->iNextOff = pCsr->eocd.iOffset;
10600           rc = zipfileNext(cur);
10601         }
10602       }
10603     }
10604   }else{
10605     pCsr->bNoop = 1;
10606     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
10607     rc = zipfileNext(cur);
10608   }
10609 
10610   return rc;
10611 }
10612 
10613 /*
10614 ** xBestIndex callback.
10615 */
zipfileBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdxInfo)10616 static int zipfileBestIndex(
10617   sqlite3_vtab *tab,
10618   sqlite3_index_info *pIdxInfo
10619 ){
10620   int i;
10621   int idx = -1;
10622   int unusable = 0;
10623   (void)tab;
10624 
10625   for(i=0; i<pIdxInfo->nConstraint; i++){
10626     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
10627     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
10628     if( pCons->usable==0 ){
10629       unusable = 1;
10630     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10631       idx = i;
10632     }
10633   }
10634   pIdxInfo->estimatedCost = 1000.0;
10635   if( idx>=0 ){
10636     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
10637     pIdxInfo->aConstraintUsage[idx].omit = 1;
10638     pIdxInfo->idxNum = 1;
10639   }else if( unusable ){
10640     return SQLITE_CONSTRAINT;
10641   }
10642   return SQLITE_OK;
10643 }
10644 
zipfileNewEntry(const char * zPath)10645 static ZipfileEntry *zipfileNewEntry(const char *zPath){
10646   ZipfileEntry *pNew;
10647   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
10648   if( pNew ){
10649     memset(pNew, 0, sizeof(ZipfileEntry));
10650     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
10651     if( pNew->cds.zFile==0 ){
10652       sqlite3_free(pNew);
10653       pNew = 0;
10654     }
10655   }
10656   return pNew;
10657 }
10658 
zipfileSerializeLFH(ZipfileEntry * pEntry,u8 * aBuf)10659 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
10660   ZipfileCDS *pCds = &pEntry->cds;
10661   u8 *a = aBuf;
10662 
10663   pCds->nExtra = 9;
10664 
10665   /* Write the LFH itself */
10666   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
10667   zipfileWrite16(a, pCds->iVersionExtract);
10668   zipfileWrite16(a, pCds->flags);
10669   zipfileWrite16(a, pCds->iCompression);
10670   zipfileWrite16(a, pCds->mTime);
10671   zipfileWrite16(a, pCds->mDate);
10672   zipfileWrite32(a, pCds->crc32);
10673   zipfileWrite32(a, pCds->szCompressed);
10674   zipfileWrite32(a, pCds->szUncompressed);
10675   zipfileWrite16(a, (u16)pCds->nFile);
10676   zipfileWrite16(a, pCds->nExtra);
10677   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
10678 
10679   /* Add the file name */
10680   memcpy(a, pCds->zFile, (int)pCds->nFile);
10681   a += (int)pCds->nFile;
10682 
10683   /* The "extra" data */
10684   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
10685   zipfileWrite16(a, 5);
10686   *a++ = 0x01;
10687   zipfileWrite32(a, pEntry->mUnixTime);
10688 
10689   return a-aBuf;
10690 }
10691 
zipfileAppendEntry(ZipfileTab * pTab,ZipfileEntry * pEntry,const u8 * pData,int nData)10692 static int zipfileAppendEntry(
10693   ZipfileTab *pTab,
10694   ZipfileEntry *pEntry,
10695   const u8 *pData,
10696   int nData
10697 ){
10698   u8 *aBuf = pTab->aBuffer;
10699   int nBuf;
10700   int rc;
10701 
10702   nBuf = zipfileSerializeLFH(pEntry, aBuf);
10703   rc = zipfileAppendData(pTab, aBuf, nBuf);
10704   if( rc==SQLITE_OK ){
10705     pEntry->iDataOff = pTab->szCurrent;
10706     rc = zipfileAppendData(pTab, pData, nData);
10707   }
10708 
10709   return rc;
10710 }
10711 
zipfileGetMode(sqlite3_value * pVal,int bIsDir,u32 * pMode,char ** pzErr)10712 static int zipfileGetMode(
10713   sqlite3_value *pVal,
10714   int bIsDir,                     /* If true, default to directory */
10715   u32 *pMode,                     /* OUT: Mode value */
10716   char **pzErr                    /* OUT: Error message */
10717 ){
10718   const char *z = (const char*)sqlite3_value_text(pVal);
10719   u32 mode = 0;
10720   if( z==0 ){
10721     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
10722   }else if( z[0]>='0' && z[0]<='9' ){
10723     mode = (unsigned int)sqlite3_value_int(pVal);
10724   }else{
10725     const char zTemplate[11] = "-rwxrwxrwx";
10726     int i;
10727     if( strlen(z)!=10 ) goto parse_error;
10728     switch( z[0] ){
10729       case '-': mode |= S_IFREG; break;
10730       case 'd': mode |= S_IFDIR; break;
10731       case 'l': mode |= S_IFLNK; break;
10732       default: goto parse_error;
10733     }
10734     for(i=1; i<10; i++){
10735       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
10736       else if( z[i]!='-' ) goto parse_error;
10737     }
10738   }
10739   if( ((mode & S_IFDIR)==0)==bIsDir ){
10740     /* The "mode" attribute is a directory, but data has been specified.
10741     ** Or vice-versa - no data but "mode" is a file or symlink.  */
10742     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
10743     return SQLITE_CONSTRAINT;
10744   }
10745   *pMode = mode;
10746   return SQLITE_OK;
10747 
10748  parse_error:
10749   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
10750   return SQLITE_ERROR;
10751 }
10752 
10753 /*
10754 ** Both (const char*) arguments point to nul-terminated strings. Argument
10755 ** nB is the value of strlen(zB). This function returns 0 if the strings are
10756 ** identical, ignoring any trailing '/' character in either path.  */
zipfileComparePath(const char * zA,const char * zB,int nB)10757 static int zipfileComparePath(const char *zA, const char *zB, int nB){
10758   int nA = (int)strlen(zA);
10759   if( nA>0 && zA[nA-1]=='/' ) nA--;
10760   if( nB>0 && zB[nB-1]=='/' ) nB--;
10761   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
10762   return 1;
10763 }
10764 
zipfileBegin(sqlite3_vtab * pVtab)10765 static int zipfileBegin(sqlite3_vtab *pVtab){
10766   ZipfileTab *pTab = (ZipfileTab*)pVtab;
10767   int rc = SQLITE_OK;
10768 
10769   assert( pTab->pWriteFd==0 );
10770   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
10771     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
10772     return SQLITE_ERROR;
10773   }
10774 
10775   /* Open a write fd on the file. Also load the entire central directory
10776   ** structure into memory. During the transaction any new file data is
10777   ** appended to the archive file, but the central directory is accumulated
10778   ** in main-memory until the transaction is committed.  */
10779   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
10780   if( pTab->pWriteFd==0 ){
10781     pTab->base.zErrMsg = sqlite3_mprintf(
10782         "zipfile: failed to open file %s for writing", pTab->zFile
10783         );
10784     rc = SQLITE_ERROR;
10785   }else{
10786     fseek(pTab->pWriteFd, 0, SEEK_END);
10787     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
10788     rc = zipfileLoadDirectory(pTab, 0, 0);
10789   }
10790 
10791   if( rc!=SQLITE_OK ){
10792     zipfileCleanupTransaction(pTab);
10793   }
10794 
10795   return rc;
10796 }
10797 
10798 /*
10799 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
10800 ** time(2)).
10801 */
zipfileTime(void)10802 static u32 zipfileTime(void){
10803   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
10804   u32 ret;
10805   if( pVfs==0 ) return 0;
10806   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
10807     i64 ms;
10808     pVfs->xCurrentTimeInt64(pVfs, &ms);
10809     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
10810   }else{
10811     double day;
10812     pVfs->xCurrentTime(pVfs, &day);
10813     ret = (u32)((day - 2440587.5) * 86400);
10814   }
10815   return ret;
10816 }
10817 
10818 /*
10819 ** Return a 32-bit timestamp in UNIX epoch format.
10820 **
10821 ** If the value passed as the only argument is either NULL or an SQL NULL,
10822 ** return the current time. Otherwise, return the value stored in (*pVal)
10823 ** cast to a 32-bit unsigned integer.
10824 */
zipfileGetTime(sqlite3_value * pVal)10825 static u32 zipfileGetTime(sqlite3_value *pVal){
10826   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
10827     return zipfileTime();
10828   }
10829   return (u32)sqlite3_value_int64(pVal);
10830 }
10831 
10832 /*
10833 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
10834 ** linked list.  Remove it from the list and free the object.
10835 */
zipfileRemoveEntryFromList(ZipfileTab * pTab,ZipfileEntry * pOld)10836 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
10837   if( pOld ){
10838     if( pTab->pFirstEntry==pOld ){
10839       pTab->pFirstEntry = pOld->pNext;
10840       if( pTab->pLastEntry==pOld ) pTab->pLastEntry = 0;
10841     }else{
10842       ZipfileEntry *p;
10843       for(p=pTab->pFirstEntry; p; p=p->pNext){
10844         if( p->pNext==pOld ){
10845           p->pNext = pOld->pNext;
10846           if( pTab->pLastEntry==pOld ) pTab->pLastEntry = p;
10847           break;
10848         }
10849       }
10850     }
10851     zipfileEntryFree(pOld);
10852   }
10853 }
10854 
10855 /*
10856 ** xUpdate method.
10857 */
zipfileUpdate(sqlite3_vtab * pVtab,int nVal,sqlite3_value ** apVal,sqlite_int64 * pRowid)10858 static int zipfileUpdate(
10859   sqlite3_vtab *pVtab,
10860   int nVal,
10861   sqlite3_value **apVal,
10862   sqlite_int64 *pRowid
10863 ){
10864   ZipfileTab *pTab = (ZipfileTab*)pVtab;
10865   int rc = SQLITE_OK;             /* Return Code */
10866   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
10867 
10868   u32 mode = 0;                   /* Mode for new entry */
10869   u32 mTime = 0;                  /* Modification time for new entry */
10870   i64 sz = 0;                     /* Uncompressed size */
10871   const char *zPath = 0;          /* Path for new entry */
10872   int nPath = 0;                  /* strlen(zPath) */
10873   const u8 *pData = 0;            /* Pointer to buffer containing content */
10874   int nData = 0;                  /* Size of pData buffer in bytes */
10875   int iMethod = 0;                /* Compression method for new entry */
10876   u8 *pFree = 0;                  /* Free this */
10877   char *zFree = 0;                /* Also free this */
10878   ZipfileEntry *pOld = 0;
10879   ZipfileEntry *pOld2 = 0;
10880   int bUpdate = 0;                /* True for an update that modifies "name" */
10881   int bIsDir = 0;
10882   u32 iCrc32 = 0;
10883 
10884   (void)pRowid;
10885 
10886   if( pTab->pWriteFd==0 ){
10887     rc = zipfileBegin(pVtab);
10888     if( rc!=SQLITE_OK ) return rc;
10889   }
10890 
10891   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
10892   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
10893     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
10894     int nDelete = (int)strlen(zDelete);
10895     if( nVal>1 ){
10896       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
10897       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
10898         bUpdate = 1;
10899       }
10900     }
10901     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
10902       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
10903         break;
10904       }
10905       assert( pOld->pNext );
10906     }
10907   }
10908 
10909   if( nVal>1 ){
10910     /* Check that "sz" and "rawdata" are both NULL: */
10911     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
10912       zipfileTableErr(pTab, "sz must be NULL");
10913       rc = SQLITE_CONSTRAINT;
10914     }
10915     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
10916       zipfileTableErr(pTab, "rawdata must be NULL");
10917       rc = SQLITE_CONSTRAINT;
10918     }
10919 
10920     if( rc==SQLITE_OK ){
10921       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
10922         /* data=NULL. A directory */
10923         bIsDir = 1;
10924       }else{
10925         /* Value specified for "data", and possibly "method". This must be
10926         ** a regular file or a symlink. */
10927         const u8 *aIn = sqlite3_value_blob(apVal[7]);
10928         int nIn = sqlite3_value_bytes(apVal[7]);
10929         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
10930 
10931         iMethod = sqlite3_value_int(apVal[8]);
10932         sz = nIn;
10933         pData = aIn;
10934         nData = nIn;
10935         if( iMethod!=0 && iMethod!=8 ){
10936           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
10937           rc = SQLITE_CONSTRAINT;
10938         }else{
10939           if( bAuto || iMethod ){
10940             int nCmp;
10941             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
10942             if( rc==SQLITE_OK ){
10943               if( iMethod || nCmp<nIn ){
10944                 iMethod = 8;
10945                 pData = pFree;
10946                 nData = nCmp;
10947               }
10948             }
10949           }
10950           iCrc32 = crc32(0, aIn, nIn);
10951         }
10952       }
10953     }
10954 
10955     if( rc==SQLITE_OK ){
10956       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
10957     }
10958 
10959     if( rc==SQLITE_OK ){
10960       zPath = (const char*)sqlite3_value_text(apVal[2]);
10961       if( zPath==0 ) zPath = "";
10962       nPath = (int)strlen(zPath);
10963       mTime = zipfileGetTime(apVal[4]);
10964     }
10965 
10966     if( rc==SQLITE_OK && bIsDir ){
10967       /* For a directory, check that the last character in the path is a
10968       ** '/'. This appears to be required for compatibility with info-zip
10969       ** (the unzip command on unix). It does not create directories
10970       ** otherwise.  */
10971       if( nPath<=0 || zPath[nPath-1]!='/' ){
10972         zFree = sqlite3_mprintf("%s/", zPath);
10973         zPath = (const char*)zFree;
10974         if( zFree==0 ){
10975           rc = SQLITE_NOMEM;
10976           nPath = 0;
10977         }else{
10978           nPath = (int)strlen(zPath);
10979         }
10980       }
10981     }
10982 
10983     /* Check that we're not inserting a duplicate entry -OR- updating an
10984     ** entry with a path, thereby making it into a duplicate. */
10985     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
10986       ZipfileEntry *p;
10987       for(p=pTab->pFirstEntry; p; p=p->pNext){
10988         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
10989           switch( sqlite3_vtab_on_conflict(pTab->db) ){
10990             case SQLITE_IGNORE: {
10991               goto zipfile_update_done;
10992             }
10993             case SQLITE_REPLACE: {
10994               pOld2 = p;
10995               break;
10996             }
10997             default: {
10998               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
10999               rc = SQLITE_CONSTRAINT;
11000               break;
11001             }
11002           }
11003           break;
11004         }
11005       }
11006     }
11007 
11008     if( rc==SQLITE_OK ){
11009       /* Create the new CDS record. */
11010       pNew = zipfileNewEntry(zPath);
11011       if( pNew==0 ){
11012         rc = SQLITE_NOMEM;
11013       }else{
11014         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
11015         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
11016         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
11017         pNew->cds.iCompression = (u16)iMethod;
11018         zipfileMtimeToDos(&pNew->cds, mTime);
11019         pNew->cds.crc32 = iCrc32;
11020         pNew->cds.szCompressed = nData;
11021         pNew->cds.szUncompressed = (u32)sz;
11022         pNew->cds.iExternalAttr = (mode<<16);
11023         pNew->cds.iOffset = (u32)pTab->szCurrent;
11024         pNew->cds.nFile = (u16)nPath;
11025         pNew->mUnixTime = (u32)mTime;
11026         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
11027         zipfileAddEntry(pTab, pOld, pNew);
11028       }
11029     }
11030   }
11031 
11032   if( rc==SQLITE_OK && (pOld || pOld2) ){
11033     ZipfileCsr *pCsr;
11034     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
11035       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
11036         pCsr->pCurrent = pCsr->pCurrent->pNext;
11037         pCsr->bNoop = 1;
11038       }
11039     }
11040 
11041     zipfileRemoveEntryFromList(pTab, pOld);
11042     zipfileRemoveEntryFromList(pTab, pOld2);
11043   }
11044 
11045 zipfile_update_done:
11046   sqlite3_free(pFree);
11047   sqlite3_free(zFree);
11048   return rc;
11049 }
11050 
zipfileSerializeEOCD(ZipfileEOCD * p,u8 * aBuf)11051 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
11052   u8 *a = aBuf;
11053   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
11054   zipfileWrite16(a, p->iDisk);
11055   zipfileWrite16(a, p->iFirstDisk);
11056   zipfileWrite16(a, p->nEntry);
11057   zipfileWrite16(a, p->nEntryTotal);
11058   zipfileWrite32(a, p->nSize);
11059   zipfileWrite32(a, p->iOffset);
11060   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
11061 
11062   return a-aBuf;
11063 }
11064 
zipfileAppendEOCD(ZipfileTab * pTab,ZipfileEOCD * p)11065 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
11066   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
11067   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
11068   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
11069 }
11070 
11071 /*
11072 ** Serialize the CDS structure into buffer aBuf[]. Return the number
11073 ** of bytes written.
11074 */
zipfileSerializeCDS(ZipfileEntry * pEntry,u8 * aBuf)11075 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
11076   u8 *a = aBuf;
11077   ZipfileCDS *pCDS = &pEntry->cds;
11078 
11079   if( pEntry->aExtra==0 ){
11080     pCDS->nExtra = 9;
11081   }
11082 
11083   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
11084   zipfileWrite16(a, pCDS->iVersionMadeBy);
11085   zipfileWrite16(a, pCDS->iVersionExtract);
11086   zipfileWrite16(a, pCDS->flags);
11087   zipfileWrite16(a, pCDS->iCompression);
11088   zipfileWrite16(a, pCDS->mTime);
11089   zipfileWrite16(a, pCDS->mDate);
11090   zipfileWrite32(a, pCDS->crc32);
11091   zipfileWrite32(a, pCDS->szCompressed);
11092   zipfileWrite32(a, pCDS->szUncompressed);
11093   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
11094   zipfileWrite16(a, pCDS->nFile);
11095   zipfileWrite16(a, pCDS->nExtra);
11096   zipfileWrite16(a, pCDS->nComment);
11097   zipfileWrite16(a, pCDS->iDiskStart);
11098   zipfileWrite16(a, pCDS->iInternalAttr);
11099   zipfileWrite32(a, pCDS->iExternalAttr);
11100   zipfileWrite32(a, pCDS->iOffset);
11101 
11102   memcpy(a, pCDS->zFile, pCDS->nFile);
11103   a += pCDS->nFile;
11104 
11105   if( pEntry->aExtra ){
11106     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
11107     memcpy(a, pEntry->aExtra, n);
11108     a += n;
11109   }else{
11110     assert( pCDS->nExtra==9 );
11111     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
11112     zipfileWrite16(a, 5);
11113     *a++ = 0x01;
11114     zipfileWrite32(a, pEntry->mUnixTime);
11115   }
11116 
11117   return a-aBuf;
11118 }
11119 
zipfileCommit(sqlite3_vtab * pVtab)11120 static int zipfileCommit(sqlite3_vtab *pVtab){
11121   ZipfileTab *pTab = (ZipfileTab*)pVtab;
11122   int rc = SQLITE_OK;
11123   if( pTab->pWriteFd ){
11124     i64 iOffset = pTab->szCurrent;
11125     ZipfileEntry *p;
11126     ZipfileEOCD eocd;
11127     int nEntry = 0;
11128 
11129     /* Write out all entries */
11130     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
11131       int n = zipfileSerializeCDS(p, pTab->aBuffer);
11132       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
11133       nEntry++;
11134     }
11135 
11136     /* Write out the EOCD record */
11137     eocd.iDisk = 0;
11138     eocd.iFirstDisk = 0;
11139     eocd.nEntry = (u16)nEntry;
11140     eocd.nEntryTotal = (u16)nEntry;
11141     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
11142     eocd.iOffset = (u32)iOffset;
11143     rc = zipfileAppendEOCD(pTab, &eocd);
11144 
11145     zipfileCleanupTransaction(pTab);
11146   }
11147   return rc;
11148 }
11149 
zipfileRollback(sqlite3_vtab * pVtab)11150 static int zipfileRollback(sqlite3_vtab *pVtab){
11151   return zipfileCommit(pVtab);
11152 }
11153 
zipfileFindCursor(ZipfileTab * pTab,i64 iId)11154 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
11155   ZipfileCsr *pCsr;
11156   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
11157     if( iId==pCsr->iId ) break;
11158   }
11159   return pCsr;
11160 }
11161 
zipfileFunctionCds(sqlite3_context * context,int argc,sqlite3_value ** argv)11162 static void zipfileFunctionCds(
11163   sqlite3_context *context,
11164   int argc,
11165   sqlite3_value **argv
11166 ){
11167   ZipfileCsr *pCsr;
11168   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
11169   assert( argc>0 );
11170 
11171   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
11172   if( pCsr ){
11173     ZipfileCDS *p = &pCsr->pCurrent->cds;
11174     char *zRes = sqlite3_mprintf("{"
11175         "\"version-made-by\" : %u, "
11176         "\"version-to-extract\" : %u, "
11177         "\"flags\" : %u, "
11178         "\"compression\" : %u, "
11179         "\"time\" : %u, "
11180         "\"date\" : %u, "
11181         "\"crc32\" : %u, "
11182         "\"compressed-size\" : %u, "
11183         "\"uncompressed-size\" : %u, "
11184         "\"file-name-length\" : %u, "
11185         "\"extra-field-length\" : %u, "
11186         "\"file-comment-length\" : %u, "
11187         "\"disk-number-start\" : %u, "
11188         "\"internal-attr\" : %u, "
11189         "\"external-attr\" : %u, "
11190         "\"offset\" : %u }",
11191         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
11192         (u32)p->flags, (u32)p->iCompression,
11193         (u32)p->mTime, (u32)p->mDate,
11194         (u32)p->crc32, (u32)p->szCompressed,
11195         (u32)p->szUncompressed, (u32)p->nFile,
11196         (u32)p->nExtra, (u32)p->nComment,
11197         (u32)p->iDiskStart, (u32)p->iInternalAttr,
11198         (u32)p->iExternalAttr, (u32)p->iOffset
11199     );
11200 
11201     if( zRes==0 ){
11202       sqlite3_result_error_nomem(context);
11203     }else{
11204       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
11205       sqlite3_free(zRes);
11206     }
11207   }
11208 }
11209 
11210 /*
11211 ** xFindFunction method.
11212 */
zipfileFindFunction(sqlite3_vtab * pVtab,int nArg,const char * zName,void (** pxFunc)(sqlite3_context *,int,sqlite3_value **),void ** ppArg)11213 static int zipfileFindFunction(
11214   sqlite3_vtab *pVtab,            /* Virtual table handle */
11215   int nArg,                       /* Number of SQL function arguments */
11216   const char *zName,              /* Name of SQL function */
11217   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
11218   void **ppArg                    /* OUT: User data for *pxFunc */
11219 ){
11220   (void)nArg;
11221   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
11222     *pxFunc = zipfileFunctionCds;
11223     *ppArg = (void*)pVtab;
11224     return 1;
11225   }
11226   return 0;
11227 }
11228 
11229 typedef struct ZipfileBuffer ZipfileBuffer;
11230 struct ZipfileBuffer {
11231   u8 *a;                          /* Pointer to buffer */
11232   int n;                          /* Size of buffer in bytes */
11233   int nAlloc;                     /* Byte allocated at a[] */
11234 };
11235 
11236 typedef struct ZipfileCtx ZipfileCtx;
11237 struct ZipfileCtx {
11238   int nEntry;
11239   ZipfileBuffer body;
11240   ZipfileBuffer cds;
11241 };
11242 
zipfileBufferGrow(ZipfileBuffer * pBuf,int nByte)11243 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
11244   if( pBuf->n+nByte>pBuf->nAlloc ){
11245     u8 *aNew;
11246     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
11247     int nReq = pBuf->n + nByte;
11248 
11249     while( nNew<nReq ) nNew = nNew*2;
11250     aNew = sqlite3_realloc64(pBuf->a, nNew);
11251     if( aNew==0 ) return SQLITE_NOMEM;
11252     pBuf->a = aNew;
11253     pBuf->nAlloc = (int)nNew;
11254   }
11255   return SQLITE_OK;
11256 }
11257 
11258 /*
11259 ** xStep() callback for the zipfile() aggregate. This can be called in
11260 ** any of the following ways:
11261 **
11262 **   SELECT zipfile(name,data) ...
11263 **   SELECT zipfile(name,mode,mtime,data) ...
11264 **   SELECT zipfile(name,mode,mtime,data,method) ...
11265 */
zipfileStep(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)11266 static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
11267   ZipfileCtx *p;                  /* Aggregate function context */
11268   ZipfileEntry e;                 /* New entry to add to zip archive */
11269 
11270   sqlite3_value *pName = 0;
11271   sqlite3_value *pMode = 0;
11272   sqlite3_value *pMtime = 0;
11273   sqlite3_value *pData = 0;
11274   sqlite3_value *pMethod = 0;
11275 
11276   int bIsDir = 0;
11277   u32 mode;
11278   int rc = SQLITE_OK;
11279   char *zErr = 0;
11280 
11281   int iMethod = -1;               /* Compression method to use (0 or 8) */
11282 
11283   const u8 *aData = 0;            /* Possibly compressed data for new entry */
11284   int nData = 0;                  /* Size of aData[] in bytes */
11285   int szUncompressed = 0;         /* Size of data before compression */
11286   u8 *aFree = 0;                  /* Free this before returning */
11287   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
11288 
11289   char *zName = 0;                /* Path (name) of new entry */
11290   int nName = 0;                  /* Size of zName in bytes */
11291   char *zFree = 0;                /* Free this before returning */
11292   int nByte;
11293 
11294   memset(&e, 0, sizeof(e));
11295   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
11296   if( p==0 ) return;
11297 
11298   /* Martial the arguments into stack variables */
11299   if( nVal!=2 && nVal!=4 && nVal!=5 ){
11300     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
11301     rc = SQLITE_ERROR;
11302     goto zipfile_step_out;
11303   }
11304   pName = apVal[0];
11305   if( nVal==2 ){
11306     pData = apVal[1];
11307   }else{
11308     pMode = apVal[1];
11309     pMtime = apVal[2];
11310     pData = apVal[3];
11311     if( nVal==5 ){
11312       pMethod = apVal[4];
11313     }
11314   }
11315 
11316   /* Check that the 'name' parameter looks ok. */
11317   zName = (char*)sqlite3_value_text(pName);
11318   nName = sqlite3_value_bytes(pName);
11319   if( zName==0 ){
11320     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
11321     rc = SQLITE_ERROR;
11322     goto zipfile_step_out;
11323   }
11324 
11325   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
11326   ** deflate compression) or NULL (choose automatically).  */
11327   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
11328     iMethod = (int)sqlite3_value_int64(pMethod);
11329     if( iMethod!=0 && iMethod!=8 ){
11330       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
11331       rc = SQLITE_ERROR;
11332       goto zipfile_step_out;
11333     }
11334   }
11335 
11336   /* Now inspect the data. If this is NULL, then the new entry must be a
11337   ** directory.  Otherwise, figure out whether or not the data should
11338   ** be deflated or simply stored in the zip archive. */
11339   if( sqlite3_value_type(pData)==SQLITE_NULL ){
11340     bIsDir = 1;
11341     iMethod = 0;
11342   }else{
11343     aData = sqlite3_value_blob(pData);
11344     szUncompressed = nData = sqlite3_value_bytes(pData);
11345     iCrc32 = crc32(0, aData, nData);
11346     if( iMethod<0 || iMethod==8 ){
11347       int nOut = 0;
11348       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
11349       if( rc!=SQLITE_OK ){
11350         goto zipfile_step_out;
11351       }
11352       if( iMethod==8 || nOut<nData ){
11353         aData = aFree;
11354         nData = nOut;
11355         iMethod = 8;
11356       }else{
11357         iMethod = 0;
11358       }
11359     }
11360   }
11361 
11362   /* Decode the "mode" argument. */
11363   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
11364   if( rc ) goto zipfile_step_out;
11365 
11366   /* Decode the "mtime" argument. */
11367   e.mUnixTime = zipfileGetTime(pMtime);
11368 
11369   /* If this is a directory entry, ensure that there is exactly one '/'
11370   ** at the end of the path. Or, if this is not a directory and the path
11371   ** ends in '/' it is an error. */
11372   if( bIsDir==0 ){
11373     if( nName>0 && zName[nName-1]=='/' ){
11374       zErr = sqlite3_mprintf("non-directory name must not end with /");
11375       rc = SQLITE_ERROR;
11376       goto zipfile_step_out;
11377     }
11378   }else{
11379     if( nName==0 || zName[nName-1]!='/' ){
11380       zName = zFree = sqlite3_mprintf("%s/", zName);
11381       if( zName==0 ){
11382         rc = SQLITE_NOMEM;
11383         goto zipfile_step_out;
11384       }
11385       nName = (int)strlen(zName);
11386     }else{
11387       while( nName>1 && zName[nName-2]=='/' ) nName--;
11388     }
11389   }
11390 
11391   /* Assemble the ZipfileEntry object for the new zip archive entry */
11392   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
11393   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
11394   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
11395   e.cds.iCompression = (u16)iMethod;
11396   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
11397   e.cds.crc32 = iCrc32;
11398   e.cds.szCompressed = nData;
11399   e.cds.szUncompressed = szUncompressed;
11400   e.cds.iExternalAttr = (mode<<16);
11401   e.cds.iOffset = p->body.n;
11402   e.cds.nFile = (u16)nName;
11403   e.cds.zFile = zName;
11404 
11405   /* Append the LFH to the body of the new archive */
11406   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
11407   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
11408   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
11409 
11410   /* Append the data to the body of the new archive */
11411   if( nData>0 ){
11412     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
11413     memcpy(&p->body.a[p->body.n], aData, nData);
11414     p->body.n += nData;
11415   }
11416 
11417   /* Append the CDS record to the directory of the new archive */
11418   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
11419   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
11420   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
11421 
11422   /* Increment the count of entries in the archive */
11423   p->nEntry++;
11424 
11425  zipfile_step_out:
11426   sqlite3_free(aFree);
11427   sqlite3_free(zFree);
11428   if( rc ){
11429     if( zErr ){
11430       sqlite3_result_error(pCtx, zErr, -1);
11431     }else{
11432       sqlite3_result_error_code(pCtx, rc);
11433     }
11434   }
11435   sqlite3_free(zErr);
11436 }
11437 
11438 /*
11439 ** xFinalize() callback for zipfile aggregate function.
11440 */
zipfileFinal(sqlite3_context * pCtx)11441 static void zipfileFinal(sqlite3_context *pCtx){
11442   ZipfileCtx *p;
11443   ZipfileEOCD eocd;
11444   sqlite3_int64 nZip;
11445   u8 *aZip;
11446 
11447   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
11448   if( p==0 ) return;
11449   if( p->nEntry>0 ){
11450     memset(&eocd, 0, sizeof(eocd));
11451     eocd.nEntry = (u16)p->nEntry;
11452     eocd.nEntryTotal = (u16)p->nEntry;
11453     eocd.nSize = p->cds.n;
11454     eocd.iOffset = p->body.n;
11455 
11456     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
11457     aZip = (u8*)sqlite3_malloc64(nZip);
11458     if( aZip==0 ){
11459       sqlite3_result_error_nomem(pCtx);
11460     }else{
11461       memcpy(aZip, p->body.a, p->body.n);
11462       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
11463       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
11464       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
11465     }
11466   }
11467 
11468   sqlite3_free(p->body.a);
11469   sqlite3_free(p->cds.a);
11470 }
11471 
11472 
11473 /*
11474 ** Register the "zipfile" virtual table.
11475 */
zipfileRegister(sqlite3 * db)11476 static int zipfileRegister(sqlite3 *db){
11477   static sqlite3_module zipfileModule = {
11478     1,                         /* iVersion */
11479     zipfileConnect,            /* xCreate */
11480     zipfileConnect,            /* xConnect */
11481     zipfileBestIndex,          /* xBestIndex */
11482     zipfileDisconnect,         /* xDisconnect */
11483     zipfileDisconnect,         /* xDestroy */
11484     zipfileOpen,               /* xOpen - open a cursor */
11485     zipfileClose,              /* xClose - close a cursor */
11486     zipfileFilter,             /* xFilter - configure scan constraints */
11487     zipfileNext,               /* xNext - advance a cursor */
11488     zipfileEof,                /* xEof - check for end of scan */
11489     zipfileColumn,             /* xColumn - read data */
11490     0,                         /* xRowid - read data */
11491     zipfileUpdate,             /* xUpdate */
11492     zipfileBegin,              /* xBegin */
11493     0,                         /* xSync */
11494     zipfileCommit,             /* xCommit */
11495     zipfileRollback,           /* xRollback */
11496     zipfileFindFunction,       /* xFindMethod */
11497     0,                         /* xRename */
11498     0,                         /* xSavepoint */
11499     0,                         /* xRelease */
11500     0,                         /* xRollback */
11501     0,                         /* xShadowName */
11502     0                          /* xIntegrity */
11503   };
11504 
11505   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
11506   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
11507   if( rc==SQLITE_OK ){
11508     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
11509         zipfileStep, zipfileFinal
11510     );
11511   }
11512   assert( sizeof(i64)==8 );
11513   assert( sizeof(u32)==4 );
11514   assert( sizeof(u16)==2 );
11515   assert( sizeof(u8)==1 );
11516   return rc;
11517 }
11518 #else         /* SQLITE_OMIT_VIRTUALTABLE */
11519 # define zipfileRegister(x) SQLITE_OK
11520 #endif
11521 
11522 #ifdef _WIN32
11523 
11524 #endif
sqlite3_zipfile_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)11525 int sqlite3_zipfile_init(
11526   sqlite3 *db,
11527   char **pzErrMsg,
11528   const sqlite3_api_routines *pApi
11529 ){
11530   SQLITE_EXTENSION_INIT2(pApi);
11531   (void)pzErrMsg;  /* Unused parameter */
11532   return zipfileRegister(db);
11533 }
11534 
11535 /************************* End ../ext/misc/zipfile.c ********************/
11536 /************************* Begin ../ext/misc/sqlar.c ******************/
11537 /*
11538 ** 2017-12-17
11539 **
11540 ** The author disclaims copyright to this source code.  In place of
11541 ** a legal notice, here is a blessing:
11542 **
11543 **    May you do good and not evil.
11544 **    May you find forgiveness for yourself and forgive others.
11545 **    May you share freely, never taking more than you give.
11546 **
11547 ******************************************************************************
11548 **
11549 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
11550 ** for working with sqlar archives and used by the shell tool's built-in
11551 ** sqlar support.
11552 */
11553 /* #include "sqlite3ext.h" */
11554 SQLITE_EXTENSION_INIT1
11555 #include <zlib.h>
11556 #include <assert.h>
11557 
11558 /*
11559 ** Implementation of the "sqlar_compress(X)" SQL function.
11560 **
11561 ** If the type of X is SQLITE_BLOB, and compressing that blob using
11562 ** zlib utility function compress() yields a smaller blob, return the
11563 ** compressed blob. Otherwise, return a copy of X.
11564 **
11565 ** SQLar uses the "zlib format" for compressed content.  The zlib format
11566 ** contains a two-byte identification header and a four-byte checksum at
11567 ** the end.  This is different from ZIP which uses the raw deflate format.
11568 **
11569 ** Future enhancements to SQLar might add support for new compression formats.
11570 ** If so, those new formats will be identified by alternative headers in the
11571 ** compressed data.
11572 */
sqlarCompressFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)11573 static void sqlarCompressFunc(
11574   sqlite3_context *context,
11575   int argc,
11576   sqlite3_value **argv
11577 ){
11578   assert( argc==1 );
11579   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
11580     const Bytef *pData = sqlite3_value_blob(argv[0]);
11581     uLong nData = sqlite3_value_bytes(argv[0]);
11582     uLongf nOut = compressBound(nData);
11583     Bytef *pOut;
11584 
11585     pOut = (Bytef*)sqlite3_malloc(nOut);
11586     if( pOut==0 ){
11587       sqlite3_result_error_nomem(context);
11588       return;
11589     }else{
11590       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
11591         sqlite3_result_error(context, "error in compress()", -1);
11592       }else if( nOut<nData ){
11593         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
11594       }else{
11595         sqlite3_result_value(context, argv[0]);
11596       }
11597       sqlite3_free(pOut);
11598     }
11599   }else{
11600     sqlite3_result_value(context, argv[0]);
11601   }
11602 }
11603 
11604 /*
11605 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
11606 **
11607 ** Parameter SZ is interpreted as an integer. If it is less than or
11608 ** equal to zero, then this function returns a copy of X. Or, if
11609 ** SZ is equal to the size of X when interpreted as a blob, also
11610 ** return a copy of X. Otherwise, decompress blob X using zlib
11611 ** utility function uncompress() and return the results (another
11612 ** blob).
11613 */
sqlarUncompressFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)11614 static void sqlarUncompressFunc(
11615   sqlite3_context *context,
11616   int argc,
11617   sqlite3_value **argv
11618 ){
11619   uLong nData;
11620   uLongf sz;
11621 
11622   assert( argc==2 );
11623   sz = sqlite3_value_int(argv[1]);
11624 
11625   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
11626     sqlite3_result_value(context, argv[0]);
11627   }else{
11628     const Bytef *pData= sqlite3_value_blob(argv[0]);
11629     Bytef *pOut = sqlite3_malloc(sz);
11630     if( pOut==0 ){
11631       sqlite3_result_error_nomem(context);
11632     }else if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
11633       sqlite3_result_error(context, "error in uncompress()", -1);
11634     }else{
11635       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
11636     }
11637     sqlite3_free(pOut);
11638   }
11639 }
11640 
11641 #ifdef _WIN32
11642 
11643 #endif
sqlite3_sqlar_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)11644 int sqlite3_sqlar_init(
11645   sqlite3 *db,
11646   char **pzErrMsg,
11647   const sqlite3_api_routines *pApi
11648 ){
11649   int rc = SQLITE_OK;
11650   SQLITE_EXTENSION_INIT2(pApi);
11651   (void)pzErrMsg;  /* Unused parameter */
11652   rc = sqlite3_create_function(db, "sqlar_compress", 1,
11653                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
11654                                sqlarCompressFunc, 0, 0);
11655   if( rc==SQLITE_OK ){
11656     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
11657                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
11658                                  sqlarUncompressFunc, 0, 0);
11659   }
11660   return rc;
11661 }
11662 
11663 /************************* End ../ext/misc/sqlar.c ********************/
11664 #endif
11665 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
11666 /*
11667 ** 2017 April 07
11668 **
11669 ** The author disclaims copyright to this source code.  In place of
11670 ** a legal notice, here is a blessing:
11671 **
11672 **    May you do good and not evil.
11673 **    May you find forgiveness for yourself and forgive others.
11674 **    May you share freely, never taking more than you give.
11675 **
11676 *************************************************************************
11677 */
11678 #if !defined(SQLITEEXPERT_H)
11679 #define SQLITEEXPERT_H 1
11680 /* #include "sqlite3.h" */
11681 
11682 typedef struct sqlite3expert sqlite3expert;
11683 
11684 /*
11685 ** Create a new sqlite3expert object.
11686 **
11687 ** If successful, a pointer to the new object is returned and (*pzErr) set
11688 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
11689 ** an English-language error message. In this case it is the responsibility
11690 ** of the caller to eventually free the error message buffer using
11691 ** sqlite3_free().
11692 */
11693 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
11694 
11695 /*
11696 ** Configure an sqlite3expert object.
11697 **
11698 ** EXPERT_CONFIG_SAMPLE:
11699 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
11700 **   each candidate index. This involves scanning and sorting the entire
11701 **   contents of each user database table once for each candidate index
11702 **   associated with the table. For large databases, this can be
11703 **   prohibitively slow. This option allows the sqlite3expert object to
11704 **   be configured so that sqlite_stat1 data is instead generated based on a
11705 **   subset of each table, or so that no sqlite_stat1 data is used at all.
11706 **
11707 **   A single integer argument is passed to this option. If the value is less
11708 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
11709 **   the analysis - indexes are recommended based on the database schema only.
11710 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
11711 **   generated for each candidate index (this is the default). Finally, if the
11712 **   value falls between 0 and 100, then it represents the percentage of user
11713 **   table rows that should be considered when generating sqlite_stat1 data.
11714 **
11715 **   Examples:
11716 **
11717 **     // Do not generate any sqlite_stat1 data
11718 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
11719 **
11720 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
11721 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
11722 */
11723 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
11724 
11725 #define EXPERT_CONFIG_SAMPLE 1    /* int */
11726 
11727 /*
11728 ** Specify zero or more SQL statements to be included in the analysis.
11729 **
11730 ** Buffer zSql must contain zero or more complete SQL statements. This
11731 ** function parses all statements contained in the buffer and adds them
11732 ** to the internal list of statements to analyze. If successful, SQLITE_OK
11733 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
11734 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
11735 ** may be set to point to an English language error message. In this case
11736 ** the caller is responsible for eventually freeing the error message buffer
11737 ** using sqlite3_free().
11738 **
11739 ** If an error does occur while processing one of the statements in the
11740 ** buffer passed as the second argument, none of the statements in the
11741 ** buffer are added to the analysis.
11742 **
11743 ** This function must be called before sqlite3_expert_analyze(). If a call
11744 ** to this function is made on an sqlite3expert object that has already
11745 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
11746 ** immediately and no statements are added to the analysis.
11747 */
11748 int sqlite3_expert_sql(
11749   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
11750   const char *zSql,               /* SQL statement(s) to add */
11751   char **pzErr                    /* OUT: Error message (if any) */
11752 );
11753 
11754 
11755 /*
11756 ** This function is called after the sqlite3expert object has been configured
11757 ** with all SQL statements using sqlite3_expert_sql() to actually perform
11758 ** the analysis. Once this function has been called, it is not possible to
11759 ** add further SQL statements to the analysis.
11760 **
11761 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
11762 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
11763 ** point to a buffer containing an English language error message. In this
11764 ** case it is the responsibility of the caller to eventually free the buffer
11765 ** using sqlite3_free().
11766 **
11767 ** If an error does occur within this function, the sqlite3expert object
11768 ** is no longer useful for any purpose. At that point it is no longer
11769 ** possible to add further SQL statements to the object or to re-attempt
11770 ** the analysis. The sqlite3expert object must still be freed using a call
11771 ** sqlite3_expert_destroy().
11772 */
11773 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
11774 
11775 /*
11776 ** Return the total number of statements loaded using sqlite3_expert_sql().
11777 ** The total number of SQL statements may be different from the total number
11778 ** to calls to sqlite3_expert_sql().
11779 */
11780 int sqlite3_expert_count(sqlite3expert*);
11781 
11782 /*
11783 ** Return a component of the report.
11784 **
11785 ** This function is called after sqlite3_expert_analyze() to extract the
11786 ** results of the analysis. Each call to this function returns either a
11787 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
11788 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
11789 ** #define constants defined below.
11790 **
11791 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
11792 ** information relating to a specific SQL statement. In these cases that
11793 ** SQL statement is identified by the value passed as the second argument.
11794 ** SQL statements are numbered from 0 in the order in which they are parsed.
11795 ** If an out-of-range value (less than zero or equal to or greater than the
11796 ** value returned by sqlite3_expert_count()) is passed as the second argument
11797 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
11798 **
11799 ** EXPERT_REPORT_SQL:
11800 **   Return the text of SQL statement iStmt.
11801 **
11802 ** EXPERT_REPORT_INDEXES:
11803 **   Return a buffer containing the CREATE INDEX statements for all recommended
11804 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL
11805 **   is returned.
11806 **
11807 ** EXPERT_REPORT_PLAN:
11808 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
11809 **   iStmt after the proposed indexes have been added to the database schema.
11810 **
11811 ** EXPERT_REPORT_CANDIDATES:
11812 **   Return a pointer to a buffer containing the CREATE INDEX statements
11813 **   for all indexes that were tested (for all SQL statements). The iStmt
11814 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
11815 */
11816 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
11817 
11818 /*
11819 ** Values for the third argument passed to sqlite3_expert_report().
11820 */
11821 #define EXPERT_REPORT_SQL        1
11822 #define EXPERT_REPORT_INDEXES    2
11823 #define EXPERT_REPORT_PLAN       3
11824 #define EXPERT_REPORT_CANDIDATES 4
11825 
11826 /*
11827 ** Free an (sqlite3expert*) handle and all associated resources. There
11828 ** should be one call to this function for each successful call to
11829 ** sqlite3-expert_new().
11830 */
11831 void sqlite3_expert_destroy(sqlite3expert*);
11832 
11833 #endif  /* !defined(SQLITEEXPERT_H) */
11834 
11835 /************************* End ../ext/expert/sqlite3expert.h ********************/
11836 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
11837 /*
11838 ** 2017 April 09
11839 **
11840 ** The author disclaims copyright to this source code.  In place of
11841 ** a legal notice, here is a blessing:
11842 **
11843 **    May you do good and not evil.
11844 **    May you find forgiveness for yourself and forgive others.
11845 **    May you share freely, never taking more than you give.
11846 **
11847 *************************************************************************
11848 */
11849 /* #include "sqlite3expert.h" */
11850 #include <assert.h>
11851 #include <string.h>
11852 #include <stdio.h>
11853 
11854 #if !defined(SQLITE_AMALGAMATION)
11855 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
11856 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
11857 #endif
11858 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
11859 # define ALWAYS(X)      (1)
11860 # define NEVER(X)       (0)
11861 #elif !defined(NDEBUG)
11862 # define ALWAYS(X)      ((X)?1:(assert(0),0))
11863 # define NEVER(X)       ((X)?(assert(0),1):0)
11864 #else
11865 # define ALWAYS(X)      (X)
11866 # define NEVER(X)       (X)
11867 #endif
11868 #endif /* !defined(SQLITE_AMALGAMATION) */
11869 
11870 
11871 #ifndef SQLITE_OMIT_VIRTUALTABLE
11872 
11873 /* typedef sqlite3_int64 i64; */
11874 /* typedef sqlite3_uint64 u64; */
11875 
11876 typedef struct IdxColumn IdxColumn;
11877 typedef struct IdxConstraint IdxConstraint;
11878 typedef struct IdxScan IdxScan;
11879 typedef struct IdxStatement IdxStatement;
11880 typedef struct IdxTable IdxTable;
11881 typedef struct IdxWrite IdxWrite;
11882 
11883 #define STRLEN  (int)strlen
11884 
11885 /*
11886 ** A temp table name that we assume no user database will actually use.
11887 ** If this assumption proves incorrect triggers on the table with the
11888 ** conflicting name will be ignored.
11889 */
11890 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
11891 
11892 /*
11893 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
11894 ** any other type of single-ended range constraint on a column).
11895 **
11896 ** pLink:
11897 **   Used to temporarily link IdxConstraint objects into lists while
11898 **   creating candidate indexes.
11899 */
11900 struct IdxConstraint {
11901   char *zColl;                    /* Collation sequence */
11902   int bRange;                     /* True for range, false for eq */
11903   int iCol;                       /* Constrained table column */
11904   int bFlag;                      /* Used by idxFindCompatible() */
11905   int bDesc;                      /* True if ORDER BY <expr> DESC */
11906   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
11907   IdxConstraint *pLink;           /* See above */
11908 };
11909 
11910 /*
11911 ** A single scan of a single table.
11912 */
11913 struct IdxScan {
11914   IdxTable *pTab;                 /* Associated table object */
11915   int iDb;                        /* Database containing table zTable */
11916   i64 covering;                   /* Mask of columns required for cov. index */
11917   IdxConstraint *pOrder;          /* ORDER BY columns */
11918   IdxConstraint *pEq;             /* List of == constraints */
11919   IdxConstraint *pRange;          /* List of < constraints */
11920   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
11921 };
11922 
11923 /*
11924 ** Information regarding a single database table. Extracted from
11925 ** "PRAGMA table_info" by function idxGetTableInfo().
11926 */
11927 struct IdxColumn {
11928   char *zName;
11929   char *zColl;
11930   int iPk;
11931 };
11932 struct IdxTable {
11933   int nCol;
11934   char *zName;                    /* Table name */
11935   IdxColumn *aCol;
11936   IdxTable *pNext;                /* Next table in linked list of all tables */
11937 };
11938 
11939 /*
11940 ** An object of the following type is created for each unique table/write-op
11941 ** seen. The objects are stored in a singly-linked list beginning at
11942 ** sqlite3expert.pWrite.
11943 */
11944 struct IdxWrite {
11945   IdxTable *pTab;
11946   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
11947   IdxWrite *pNext;
11948 };
11949 
11950 /*
11951 ** Each statement being analyzed is represented by an instance of this
11952 ** structure.
11953 */
11954 struct IdxStatement {
11955   int iId;                        /* Statement number */
11956   char *zSql;                     /* SQL statement */
11957   char *zIdx;                     /* Indexes */
11958   char *zEQP;                     /* Plan */
11959   IdxStatement *pNext;
11960 };
11961 
11962 
11963 /*
11964 ** A hash table for storing strings. With space for a payload string
11965 ** with each entry. Methods are:
11966 **
11967 **   idxHashInit()
11968 **   idxHashClear()
11969 **   idxHashAdd()
11970 **   idxHashSearch()
11971 */
11972 #define IDX_HASH_SIZE 1023
11973 typedef struct IdxHashEntry IdxHashEntry;
11974 typedef struct IdxHash IdxHash;
11975 struct IdxHashEntry {
11976   char *zKey;                     /* nul-terminated key */
11977   char *zVal;                     /* nul-terminated value string */
11978   char *zVal2;                    /* nul-terminated value string 2 */
11979   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
11980   IdxHashEntry *pNext;            /* Next entry in hash */
11981 };
11982 struct IdxHash {
11983   IdxHashEntry *pFirst;
11984   IdxHashEntry *aHash[IDX_HASH_SIZE];
11985 };
11986 
11987 /*
11988 ** sqlite3expert object.
11989 */
11990 struct sqlite3expert {
11991   int iSample;                    /* Percentage of tables to sample for stat1 */
11992   sqlite3 *db;                    /* User database */
11993   sqlite3 *dbm;                   /* In-memory db for this analysis */
11994   sqlite3 *dbv;                   /* Vtab schema for this analysis */
11995   IdxTable *pTable;               /* List of all IdxTable objects */
11996   IdxScan *pScan;                 /* List of scan objects */
11997   IdxWrite *pWrite;               /* List of write objects */
11998   IdxStatement *pStatement;       /* List of IdxStatement objects */
11999   int bRun;                       /* True once analysis has run */
12000   char **pzErrmsg;
12001   int rc;                         /* Error code from whereinfo hook */
12002   IdxHash hIdx;                   /* Hash containing all candidate indexes */
12003   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
12004 };
12005 
12006 
12007 /*
12008 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
12009 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
12010 */
idxMalloc(int * pRc,int nByte)12011 static void *idxMalloc(int *pRc, int nByte){
12012   void *pRet;
12013   assert( *pRc==SQLITE_OK );
12014   assert( nByte>0 );
12015   pRet = sqlite3_malloc(nByte);
12016   if( pRet ){
12017     memset(pRet, 0, nByte);
12018   }else{
12019     *pRc = SQLITE_NOMEM;
12020   }
12021   return pRet;
12022 }
12023 
12024 /*
12025 ** Initialize an IdxHash hash table.
12026 */
idxHashInit(IdxHash * pHash)12027 static void idxHashInit(IdxHash *pHash){
12028   memset(pHash, 0, sizeof(IdxHash));
12029 }
12030 
12031 /*
12032 ** Reset an IdxHash hash table.
12033 */
idxHashClear(IdxHash * pHash)12034 static void idxHashClear(IdxHash *pHash){
12035   int i;
12036   for(i=0; i<IDX_HASH_SIZE; i++){
12037     IdxHashEntry *pEntry;
12038     IdxHashEntry *pNext;
12039     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
12040       pNext = pEntry->pHashNext;
12041       sqlite3_free(pEntry->zVal2);
12042       sqlite3_free(pEntry);
12043     }
12044   }
12045   memset(pHash, 0, sizeof(IdxHash));
12046 }
12047 
12048 /*
12049 ** Return the index of the hash bucket that the string specified by the
12050 ** arguments to this function belongs.
12051 */
idxHashString(const char * z,int n)12052 static int idxHashString(const char *z, int n){
12053   unsigned int ret = 0;
12054   int i;
12055   for(i=0; i<n; i++){
12056     ret += (ret<<3) + (unsigned char)(z[i]);
12057   }
12058   return (int)(ret % IDX_HASH_SIZE);
12059 }
12060 
12061 /*
12062 ** If zKey is already present in the hash table, return non-zero and do
12063 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
12064 ** the hash table passed as the second argument.
12065 */
idxHashAdd(int * pRc,IdxHash * pHash,const char * zKey,const char * zVal)12066 static int idxHashAdd(
12067   int *pRc,
12068   IdxHash *pHash,
12069   const char *zKey,
12070   const char *zVal
12071 ){
12072   int nKey = STRLEN(zKey);
12073   int iHash = idxHashString(zKey, nKey);
12074   int nVal = (zVal ? STRLEN(zVal) : 0);
12075   IdxHashEntry *pEntry;
12076   assert( iHash>=0 );
12077   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
12078     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
12079       return 1;
12080     }
12081   }
12082   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
12083   if( pEntry ){
12084     pEntry->zKey = (char*)&pEntry[1];
12085     memcpy(pEntry->zKey, zKey, nKey);
12086     if( zVal ){
12087       pEntry->zVal = &pEntry->zKey[nKey+1];
12088       memcpy(pEntry->zVal, zVal, nVal);
12089     }
12090     pEntry->pHashNext = pHash->aHash[iHash];
12091     pHash->aHash[iHash] = pEntry;
12092 
12093     pEntry->pNext = pHash->pFirst;
12094     pHash->pFirst = pEntry;
12095   }
12096   return 0;
12097 }
12098 
12099 /*
12100 ** If zKey/nKey is present in the hash table, return a pointer to the
12101 ** hash-entry object.
12102 */
idxHashFind(IdxHash * pHash,const char * zKey,int nKey)12103 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
12104   int iHash;
12105   IdxHashEntry *pEntry;
12106   if( nKey<0 ) nKey = STRLEN(zKey);
12107   iHash = idxHashString(zKey, nKey);
12108   assert( iHash>=0 );
12109   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
12110     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
12111       return pEntry;
12112     }
12113   }
12114   return 0;
12115 }
12116 
12117 /*
12118 ** If the hash table contains an entry with a key equal to the string
12119 ** passed as the final two arguments to this function, return a pointer
12120 ** to the payload string. Otherwise, if zKey/nKey is not present in the
12121 ** hash table, return NULL.
12122 */
idxHashSearch(IdxHash * pHash,const char * zKey,int nKey)12123 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
12124   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
12125   if( pEntry ) return pEntry->zVal;
12126   return 0;
12127 }
12128 
12129 /*
12130 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
12131 ** variable to point to a copy of nul-terminated string zColl.
12132 */
idxNewConstraint(int * pRc,const char * zColl)12133 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
12134   IdxConstraint *pNew;
12135   int nColl = STRLEN(zColl);
12136 
12137   assert( *pRc==SQLITE_OK );
12138   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
12139   if( pNew ){
12140     pNew->zColl = (char*)&pNew[1];
12141     memcpy(pNew->zColl, zColl, nColl+1);
12142   }
12143   return pNew;
12144 }
12145 
12146 /*
12147 ** An error associated with database handle db has just occurred. Pass
12148 ** the error message to callback function xOut.
12149 */
idxDatabaseError(sqlite3 * db,char ** pzErrmsg)12150 static void idxDatabaseError(
12151   sqlite3 *db,                    /* Database handle */
12152   char **pzErrmsg                 /* Write error here */
12153 ){
12154   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
12155 }
12156 
12157 /*
12158 ** Prepare an SQL statement.
12159 */
idxPrepareStmt(sqlite3 * db,sqlite3_stmt ** ppStmt,char ** pzErrmsg,const char * zSql)12160 static int idxPrepareStmt(
12161   sqlite3 *db,                    /* Database handle to compile against */
12162   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
12163   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
12164   const char *zSql                /* SQL statement to compile */
12165 ){
12166   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12167   if( rc!=SQLITE_OK ){
12168     *ppStmt = 0;
12169     idxDatabaseError(db, pzErrmsg);
12170   }
12171   return rc;
12172 }
12173 
12174 /*
12175 ** Prepare an SQL statement using the results of a printf() formatting.
12176 */
idxPrintfPrepareStmt(sqlite3 * db,sqlite3_stmt ** ppStmt,char ** pzErrmsg,const char * zFmt,...)12177 static int idxPrintfPrepareStmt(
12178   sqlite3 *db,                    /* Database handle to compile against */
12179   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
12180   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
12181   const char *zFmt,               /* printf() format of SQL statement */
12182   ...                             /* Trailing printf() arguments */
12183 ){
12184   va_list ap;
12185   int rc;
12186   char *zSql;
12187   va_start(ap, zFmt);
12188   zSql = sqlite3_vmprintf(zFmt, ap);
12189   if( zSql==0 ){
12190     rc = SQLITE_NOMEM;
12191   }else{
12192     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
12193     sqlite3_free(zSql);
12194   }
12195   va_end(ap);
12196   return rc;
12197 }
12198 
12199 
12200 /*************************************************************************
12201 ** Beginning of virtual table implementation.
12202 */
12203 typedef struct ExpertVtab ExpertVtab;
12204 struct ExpertVtab {
12205   sqlite3_vtab base;
12206   IdxTable *pTab;
12207   sqlite3expert *pExpert;
12208 };
12209 
12210 typedef struct ExpertCsr ExpertCsr;
12211 struct ExpertCsr {
12212   sqlite3_vtab_cursor base;
12213   sqlite3_stmt *pData;
12214 };
12215 
expertDequote(const char * zIn)12216 static char *expertDequote(const char *zIn){
12217   int n = STRLEN(zIn);
12218   char *zRet = sqlite3_malloc(n);
12219 
12220   assert( zIn[0]=='\'' );
12221   assert( zIn[n-1]=='\'' );
12222 
12223   if( zRet ){
12224     int iOut = 0;
12225     int iIn = 0;
12226     for(iIn=1; iIn<(n-1); iIn++){
12227       if( zIn[iIn]=='\'' ){
12228         assert( zIn[iIn+1]=='\'' );
12229         iIn++;
12230       }
12231       zRet[iOut++] = zIn[iIn];
12232     }
12233     zRet[iOut] = '\0';
12234   }
12235 
12236   return zRet;
12237 }
12238 
12239 /*
12240 ** This function is the implementation of both the xConnect and xCreate
12241 ** methods of the r-tree virtual table.
12242 **
12243 **   argv[0]   -> module name
12244 **   argv[1]   -> database name
12245 **   argv[2]   -> table name
12246 **   argv[...] -> column names...
12247 */
expertConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)12248 static int expertConnect(
12249   sqlite3 *db,
12250   void *pAux,
12251   int argc, const char *const*argv,
12252   sqlite3_vtab **ppVtab,
12253   char **pzErr
12254 ){
12255   sqlite3expert *pExpert = (sqlite3expert*)pAux;
12256   ExpertVtab *p = 0;
12257   int rc;
12258 
12259   if( argc!=4 ){
12260     *pzErr = sqlite3_mprintf("internal error!");
12261     rc = SQLITE_ERROR;
12262   }else{
12263     char *zCreateTable = expertDequote(argv[3]);
12264     if( zCreateTable ){
12265       rc = sqlite3_declare_vtab(db, zCreateTable);
12266       if( rc==SQLITE_OK ){
12267         p = idxMalloc(&rc, sizeof(ExpertVtab));
12268       }
12269       if( rc==SQLITE_OK ){
12270         p->pExpert = pExpert;
12271         p->pTab = pExpert->pTable;
12272         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
12273       }
12274       sqlite3_free(zCreateTable);
12275     }else{
12276       rc = SQLITE_NOMEM;
12277     }
12278   }
12279 
12280   *ppVtab = (sqlite3_vtab*)p;
12281   return rc;
12282 }
12283 
expertDisconnect(sqlite3_vtab * pVtab)12284 static int expertDisconnect(sqlite3_vtab *pVtab){
12285   ExpertVtab *p = (ExpertVtab*)pVtab;
12286   sqlite3_free(p);
12287   return SQLITE_OK;
12288 }
12289 
expertBestIndex(sqlite3_vtab * pVtab,sqlite3_index_info * pIdxInfo)12290 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
12291   ExpertVtab *p = (ExpertVtab*)pVtab;
12292   int rc = SQLITE_OK;
12293   int n = 0;
12294   IdxScan *pScan;
12295   const int opmask =
12296     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
12297     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
12298     SQLITE_INDEX_CONSTRAINT_LE;
12299 
12300   pScan = idxMalloc(&rc, sizeof(IdxScan));
12301   if( pScan ){
12302     int i;
12303 
12304     /* Link the new scan object into the list */
12305     pScan->pTab = p->pTab;
12306     pScan->pNextScan = p->pExpert->pScan;
12307     p->pExpert->pScan = pScan;
12308 
12309     /* Add the constraints to the IdxScan object */
12310     for(i=0; i<pIdxInfo->nConstraint; i++){
12311       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
12312       if( pCons->usable
12313        && pCons->iColumn>=0
12314        && p->pTab->aCol[pCons->iColumn].iPk==0
12315        && (pCons->op & opmask)
12316       ){
12317         IdxConstraint *pNew;
12318         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
12319         pNew = idxNewConstraint(&rc, zColl);
12320         if( pNew ){
12321           pNew->iCol = pCons->iColumn;
12322           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
12323             pNew->pNext = pScan->pEq;
12324             pScan->pEq = pNew;
12325           }else{
12326             pNew->bRange = 1;
12327             pNew->pNext = pScan->pRange;
12328             pScan->pRange = pNew;
12329           }
12330         }
12331         n++;
12332         pIdxInfo->aConstraintUsage[i].argvIndex = n;
12333       }
12334     }
12335 
12336     /* Add the ORDER BY to the IdxScan object */
12337     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
12338       int iCol = pIdxInfo->aOrderBy[i].iColumn;
12339       if( iCol>=0 ){
12340         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
12341         if( pNew ){
12342           pNew->iCol = iCol;
12343           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
12344           pNew->pNext = pScan->pOrder;
12345           pNew->pLink = pScan->pOrder;
12346           pScan->pOrder = pNew;
12347           n++;
12348         }
12349       }
12350     }
12351   }
12352 
12353   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
12354   return rc;
12355 }
12356 
expertUpdate(sqlite3_vtab * pVtab,int nData,sqlite3_value ** azData,sqlite_int64 * pRowid)12357 static int expertUpdate(
12358   sqlite3_vtab *pVtab,
12359   int nData,
12360   sqlite3_value **azData,
12361   sqlite_int64 *pRowid
12362 ){
12363   (void)pVtab;
12364   (void)nData;
12365   (void)azData;
12366   (void)pRowid;
12367   return SQLITE_OK;
12368 }
12369 
12370 /*
12371 ** Virtual table module xOpen method.
12372 */
expertOpen(sqlite3_vtab * pVTab,sqlite3_vtab_cursor ** ppCursor)12373 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
12374   int rc = SQLITE_OK;
12375   ExpertCsr *pCsr;
12376   (void)pVTab;
12377   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
12378   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
12379   return rc;
12380 }
12381 
12382 /*
12383 ** Virtual table module xClose method.
12384 */
expertClose(sqlite3_vtab_cursor * cur)12385 static int expertClose(sqlite3_vtab_cursor *cur){
12386   ExpertCsr *pCsr = (ExpertCsr*)cur;
12387   sqlite3_finalize(pCsr->pData);
12388   sqlite3_free(pCsr);
12389   return SQLITE_OK;
12390 }
12391 
12392 /*
12393 ** Virtual table module xEof method.
12394 **
12395 ** Return non-zero if the cursor does not currently point to a valid
12396 ** record (i.e if the scan has finished), or zero otherwise.
12397 */
expertEof(sqlite3_vtab_cursor * cur)12398 static int expertEof(sqlite3_vtab_cursor *cur){
12399   ExpertCsr *pCsr = (ExpertCsr*)cur;
12400   return pCsr->pData==0;
12401 }
12402 
12403 /*
12404 ** Virtual table module xNext method.
12405 */
expertNext(sqlite3_vtab_cursor * cur)12406 static int expertNext(sqlite3_vtab_cursor *cur){
12407   ExpertCsr *pCsr = (ExpertCsr*)cur;
12408   int rc = SQLITE_OK;
12409 
12410   assert( pCsr->pData );
12411   rc = sqlite3_step(pCsr->pData);
12412   if( rc!=SQLITE_ROW ){
12413     rc = sqlite3_finalize(pCsr->pData);
12414     pCsr->pData = 0;
12415   }else{
12416     rc = SQLITE_OK;
12417   }
12418 
12419   return rc;
12420 }
12421 
12422 /*
12423 ** Virtual table module xRowid method.
12424 */
expertRowid(sqlite3_vtab_cursor * cur,sqlite_int64 * pRowid)12425 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
12426   (void)cur;
12427   *pRowid = 0;
12428   return SQLITE_OK;
12429 }
12430 
12431 /*
12432 ** Virtual table module xColumn method.
12433 */
expertColumn(sqlite3_vtab_cursor * cur,sqlite3_context * ctx,int i)12434 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
12435   ExpertCsr *pCsr = (ExpertCsr*)cur;
12436   sqlite3_value *pVal;
12437   pVal = sqlite3_column_value(pCsr->pData, i);
12438   if( pVal ){
12439     sqlite3_result_value(ctx, pVal);
12440   }
12441   return SQLITE_OK;
12442 }
12443 
12444 /*
12445 ** Virtual table module xFilter method.
12446 */
expertFilter(sqlite3_vtab_cursor * cur,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)12447 static int expertFilter(
12448   sqlite3_vtab_cursor *cur,
12449   int idxNum, const char *idxStr,
12450   int argc, sqlite3_value **argv
12451 ){
12452   ExpertCsr *pCsr = (ExpertCsr*)cur;
12453   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
12454   sqlite3expert *pExpert = pVtab->pExpert;
12455   int rc;
12456 
12457   (void)idxNum;
12458   (void)idxStr;
12459   (void)argc;
12460   (void)argv;
12461   rc = sqlite3_finalize(pCsr->pData);
12462   pCsr->pData = 0;
12463   if( rc==SQLITE_OK ){
12464     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
12465         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
12466     );
12467   }
12468 
12469   if( rc==SQLITE_OK ){
12470     rc = expertNext(cur);
12471   }
12472   return rc;
12473 }
12474 
idxRegisterVtab(sqlite3expert * p)12475 static int idxRegisterVtab(sqlite3expert *p){
12476   static sqlite3_module expertModule = {
12477     2,                            /* iVersion */
12478     expertConnect,                /* xCreate - create a table */
12479     expertConnect,                /* xConnect - connect to an existing table */
12480     expertBestIndex,              /* xBestIndex - Determine search strategy */
12481     expertDisconnect,             /* xDisconnect - Disconnect from a table */
12482     expertDisconnect,             /* xDestroy - Drop a table */
12483     expertOpen,                   /* xOpen - open a cursor */
12484     expertClose,                  /* xClose - close a cursor */
12485     expertFilter,                 /* xFilter - configure scan constraints */
12486     expertNext,                   /* xNext - advance a cursor */
12487     expertEof,                    /* xEof */
12488     expertColumn,                 /* xColumn - read data */
12489     expertRowid,                  /* xRowid - read data */
12490     expertUpdate,                 /* xUpdate - write data */
12491     0,                            /* xBegin - begin transaction */
12492     0,                            /* xSync - sync transaction */
12493     0,                            /* xCommit - commit transaction */
12494     0,                            /* xRollback - rollback transaction */
12495     0,                            /* xFindFunction - function overloading */
12496     0,                            /* xRename - rename the table */
12497     0,                            /* xSavepoint */
12498     0,                            /* xRelease */
12499     0,                            /* xRollbackTo */
12500     0,                            /* xShadowName */
12501     0,                            /* xIntegrity */
12502   };
12503 
12504   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
12505 }
12506 /*
12507 ** End of virtual table implementation.
12508 *************************************************************************/
12509 /*
12510 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
12511 ** is called, set it to the return value of sqlite3_finalize() before
12512 ** returning. Otherwise, discard the sqlite3_finalize() return value.
12513 */
idxFinalize(int * pRc,sqlite3_stmt * pStmt)12514 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
12515   int rc = sqlite3_finalize(pStmt);
12516   if( *pRc==SQLITE_OK ) *pRc = rc;
12517 }
12518 
12519 /*
12520 ** Attempt to allocate an IdxTable structure corresponding to table zTab
12521 ** in the main database of connection db. If successful, set (*ppOut) to
12522 ** point to the new object and return SQLITE_OK. Otherwise, return an
12523 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
12524 ** set to point to an error string.
12525 **
12526 ** It is the responsibility of the caller to eventually free either the
12527 ** IdxTable object or error message using sqlite3_free().
12528 */
idxGetTableInfo(sqlite3 * db,const char * zTab,IdxTable ** ppOut,char ** pzErrmsg)12529 static int idxGetTableInfo(
12530   sqlite3 *db,                    /* Database connection to read details from */
12531   const char *zTab,               /* Table name */
12532   IdxTable **ppOut,               /* OUT: New object (if successful) */
12533   char **pzErrmsg                 /* OUT: Error message (if not) */
12534 ){
12535   sqlite3_stmt *p1 = 0;
12536   int nCol = 0;
12537   int nTab;
12538   int nByte;
12539   IdxTable *pNew = 0;
12540   int rc, rc2;
12541   char *pCsr = 0;
12542   int nPk = 0;
12543 
12544   *ppOut = 0;
12545   if( zTab==0 ) return SQLITE_ERROR;
12546   nTab = STRLEN(zTab);
12547   nByte = sizeof(IdxTable) + nTab + 1;
12548   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
12549   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
12550     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
12551     const char *zColSeq = 0;
12552     if( zCol==0 ){
12553       rc = SQLITE_ERROR;
12554       break;
12555     }
12556     nByte += 1 + STRLEN(zCol);
12557     rc = sqlite3_table_column_metadata(
12558         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
12559     );
12560     if( zColSeq==0 ) zColSeq = "binary";
12561     nByte += 1 + STRLEN(zColSeq);
12562     nCol++;
12563     nPk += (sqlite3_column_int(p1, 5)>0);
12564   }
12565   rc2 = sqlite3_reset(p1);
12566   if( rc==SQLITE_OK ) rc = rc2;
12567 
12568   nByte += sizeof(IdxColumn) * nCol;
12569   if( rc==SQLITE_OK ){
12570     pNew = idxMalloc(&rc, nByte);
12571   }
12572   if( rc==SQLITE_OK ){
12573     pNew->aCol = (IdxColumn*)&pNew[1];
12574     pNew->nCol = nCol;
12575     pCsr = (char*)&pNew->aCol[nCol];
12576   }
12577 
12578   nCol = 0;
12579   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
12580     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
12581     const char *zColSeq = 0;
12582     int nCopy;
12583     if( zCol==0 ) continue;
12584     nCopy = STRLEN(zCol) + 1;
12585     pNew->aCol[nCol].zName = pCsr;
12586     pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
12587     memcpy(pCsr, zCol, nCopy);
12588     pCsr += nCopy;
12589 
12590     rc = sqlite3_table_column_metadata(
12591         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
12592     );
12593     if( rc==SQLITE_OK ){
12594       if( zColSeq==0 ) zColSeq = "binary";
12595       nCopy = STRLEN(zColSeq) + 1;
12596       pNew->aCol[nCol].zColl = pCsr;
12597       memcpy(pCsr, zColSeq, nCopy);
12598       pCsr += nCopy;
12599     }
12600 
12601     nCol++;
12602   }
12603   idxFinalize(&rc, p1);
12604 
12605   if( rc!=SQLITE_OK ){
12606     sqlite3_free(pNew);
12607     pNew = 0;
12608   }else if( ALWAYS(pNew!=0) ){
12609     pNew->zName = pCsr;
12610     if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
12611   }
12612 
12613   *ppOut = pNew;
12614   return rc;
12615 }
12616 
12617 /*
12618 ** This function is a no-op if *pRc is set to anything other than
12619 ** SQLITE_OK when it is called.
12620 **
12621 ** If *pRc is initially set to SQLITE_OK, then the text specified by
12622 ** the printf() style arguments is appended to zIn and the result returned
12623 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
12624 ** zIn before returning.
12625 */
idxAppendText(int * pRc,char * zIn,const char * zFmt,...)12626 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
12627   va_list ap;
12628   char *zAppend = 0;
12629   char *zRet = 0;
12630   int nIn = zIn ? STRLEN(zIn) : 0;
12631   int nAppend = 0;
12632   va_start(ap, zFmt);
12633   if( *pRc==SQLITE_OK ){
12634     zAppend = sqlite3_vmprintf(zFmt, ap);
12635     if( zAppend ){
12636       nAppend = STRLEN(zAppend);
12637       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
12638     }
12639     if( zAppend && zRet ){
12640       if( nIn ) memcpy(zRet, zIn, nIn);
12641       memcpy(&zRet[nIn], zAppend, nAppend+1);
12642     }else{
12643       sqlite3_free(zRet);
12644       zRet = 0;
12645       *pRc = SQLITE_NOMEM;
12646     }
12647     sqlite3_free(zAppend);
12648     sqlite3_free(zIn);
12649   }
12650   va_end(ap);
12651   return zRet;
12652 }
12653 
12654 /*
12655 ** Return true if zId must be quoted in order to use it as an SQL
12656 ** identifier, or false otherwise.
12657 */
idxIdentifierRequiresQuotes(const char * zId)12658 static int idxIdentifierRequiresQuotes(const char *zId){
12659   int i;
12660   int nId = STRLEN(zId);
12661 
12662   if( sqlite3_keyword_check(zId, nId) ) return 1;
12663 
12664   for(i=0; zId[i]; i++){
12665     if( !(zId[i]=='_')
12666      && !(zId[i]>='0' && zId[i]<='9')
12667      && !(zId[i]>='a' && zId[i]<='z')
12668      && !(zId[i]>='A' && zId[i]<='Z')
12669     ){
12670       return 1;
12671     }
12672   }
12673   return 0;
12674 }
12675 
12676 /*
12677 ** This function appends an index column definition suitable for constraint
12678 ** pCons to the string passed as zIn and returns the result.
12679 */
idxAppendColDefn(int * pRc,char * zIn,IdxTable * pTab,IdxConstraint * pCons)12680 static char *idxAppendColDefn(
12681   int *pRc,                       /* IN/OUT: Error code */
12682   char *zIn,                      /* Column defn accumulated so far */
12683   IdxTable *pTab,                 /* Table index will be created on */
12684   IdxConstraint *pCons
12685 ){
12686   char *zRet = zIn;
12687   IdxColumn *p = &pTab->aCol[pCons->iCol];
12688   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
12689 
12690   if( idxIdentifierRequiresQuotes(p->zName) ){
12691     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
12692   }else{
12693     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
12694   }
12695 
12696   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
12697     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
12698       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
12699     }else{
12700       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
12701     }
12702   }
12703 
12704   if( pCons->bDesc ){
12705     zRet = idxAppendText(pRc, zRet, " DESC");
12706   }
12707   return zRet;
12708 }
12709 
12710 /*
12711 ** Search database dbm for an index compatible with the one idxCreateFromCons()
12712 ** would create from arguments pScan, pEq and pTail. If no error occurs and
12713 ** such an index is found, return non-zero. Or, if no such index is found,
12714 ** return zero.
12715 **
12716 ** If an error occurs, set *pRc to an SQLite error code and return zero.
12717 */
idxFindCompatible(int * pRc,sqlite3 * dbm,IdxScan * pScan,IdxConstraint * pEq,IdxConstraint * pTail)12718 static int idxFindCompatible(
12719   int *pRc,                       /* OUT: Error code */
12720   sqlite3* dbm,                   /* Database to search */
12721   IdxScan *pScan,                 /* Scan for table to search for index on */
12722   IdxConstraint *pEq,             /* List of == constraints */
12723   IdxConstraint *pTail            /* List of range constraints */
12724 ){
12725   const char *zTbl = pScan->pTab->zName;
12726   sqlite3_stmt *pIdxList = 0;
12727   IdxConstraint *pIter;
12728   int nEq = 0;                    /* Number of elements in pEq */
12729   int rc;
12730 
12731   /* Count the elements in list pEq */
12732   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
12733 
12734   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
12735   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
12736     int bMatch = 1;
12737     IdxConstraint *pT = pTail;
12738     sqlite3_stmt *pInfo = 0;
12739     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
12740     if( zIdx==0 ) continue;
12741 
12742     /* Zero the IdxConstraint.bFlag values in the pEq list */
12743     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
12744 
12745     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
12746     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
12747       int iIdx = sqlite3_column_int(pInfo, 0);
12748       int iCol = sqlite3_column_int(pInfo, 1);
12749       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
12750 
12751       if( iIdx<nEq ){
12752         for(pIter=pEq; pIter; pIter=pIter->pLink){
12753           if( pIter->bFlag ) continue;
12754           if( pIter->iCol!=iCol ) continue;
12755           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
12756           pIter->bFlag = 1;
12757           break;
12758         }
12759         if( pIter==0 ){
12760           bMatch = 0;
12761           break;
12762         }
12763       }else{
12764         if( pT ){
12765           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
12766             bMatch = 0;
12767             break;
12768           }
12769           pT = pT->pLink;
12770         }
12771       }
12772     }
12773     idxFinalize(&rc, pInfo);
12774 
12775     if( rc==SQLITE_OK && bMatch ){
12776       sqlite3_finalize(pIdxList);
12777       return 1;
12778     }
12779   }
12780   idxFinalize(&rc, pIdxList);
12781 
12782   *pRc = rc;
12783   return 0;
12784 }
12785 
12786 /* Callback for sqlite3_exec() with query with leading count(*) column.
12787  * The first argument is expected to be an int*, referent to be incremented
12788  * if that leading column is not exactly '0'.
12789  */
countNonzeros(void * pCount,int nc,char * azResults[],char * azColumns[])12790 static int countNonzeros(void* pCount, int nc,
12791                          char* azResults[], char* azColumns[]){
12792   (void)azColumns;  /* Suppress unused parameter warning */
12793   if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
12794     *((int *)pCount) += 1;
12795   }
12796   return 0;
12797 }
12798 
idxCreateFromCons(sqlite3expert * p,IdxScan * pScan,IdxConstraint * pEq,IdxConstraint * pTail)12799 static int idxCreateFromCons(
12800   sqlite3expert *p,
12801   IdxScan *pScan,
12802   IdxConstraint *pEq,
12803   IdxConstraint *pTail
12804 ){
12805   sqlite3 *dbm = p->dbm;
12806   int rc = SQLITE_OK;
12807   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
12808     IdxTable *pTab = pScan->pTab;
12809     char *zCols = 0;
12810     char *zIdx = 0;
12811     IdxConstraint *pCons;
12812     unsigned int h = 0;
12813     const char *zFmt;
12814 
12815     for(pCons=pEq; pCons; pCons=pCons->pLink){
12816       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
12817     }
12818     for(pCons=pTail; pCons; pCons=pCons->pLink){
12819       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
12820     }
12821 
12822     if( rc==SQLITE_OK ){
12823       /* Hash the list of columns to come up with a name for the index */
12824       const char *zTable = pScan->pTab->zName;
12825       int quoteTable = idxIdentifierRequiresQuotes(zTable);
12826       char *zName = 0;          /* Index name */
12827       int collisions = 0;
12828       do{
12829         int i;
12830         char *zFind;
12831         for(i=0; zCols[i]; i++){
12832           h += ((h<<3) + zCols[i]);
12833         }
12834         sqlite3_free(zName);
12835         zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
12836         if( zName==0 ) break;
12837         /* Is is unique among table, view and index names? */
12838         zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
12839           " AND type in ('index','table','view')";
12840         zFind = sqlite3_mprintf(zFmt, zName);
12841         i = 0;
12842         rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
12843         assert(rc==SQLITE_OK);
12844         sqlite3_free(zFind);
12845         if( i==0 ){
12846           collisions = 0;
12847           break;
12848         }
12849         ++collisions;
12850       }while( collisions<50 && zName!=0 );
12851       if( collisions ){
12852         /* This return means "Gave up trying to find a unique index name." */
12853         rc = SQLITE_BUSY_TIMEOUT;
12854       }else if( zName==0 ){
12855         rc = SQLITE_NOMEM;
12856       }else{
12857         if( quoteTable ){
12858           zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
12859         }else{
12860           zFmt = "CREATE INDEX %s ON %s(%s)";
12861         }
12862         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
12863         if( !zIdx ){
12864           rc = SQLITE_NOMEM;
12865         }else{
12866           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
12867           if( rc!=SQLITE_OK ){
12868             rc = SQLITE_BUSY_TIMEOUT;
12869           }else{
12870             idxHashAdd(&rc, &p->hIdx, zName, zIdx);
12871           }
12872         }
12873         sqlite3_free(zName);
12874         sqlite3_free(zIdx);
12875       }
12876     }
12877 
12878     sqlite3_free(zCols);
12879   }
12880   return rc;
12881 }
12882 
12883 /*
12884 ** Return true if list pList (linked by IdxConstraint.pLink) contains
12885 ** a constraint compatible with *p. Otherwise return false.
12886 */
idxFindConstraint(IdxConstraint * pList,IdxConstraint * p)12887 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
12888   IdxConstraint *pCmp;
12889   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
12890     if( p->iCol==pCmp->iCol ) return 1;
12891   }
12892   return 0;
12893 }
12894 
idxCreateFromWhere(sqlite3expert * p,IdxScan * pScan,IdxConstraint * pTail)12895 static int idxCreateFromWhere(
12896   sqlite3expert *p,
12897   IdxScan *pScan,                 /* Create indexes for this scan */
12898   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
12899 ){
12900   IdxConstraint *p1 = 0;
12901   IdxConstraint *pCon;
12902   int rc;
12903 
12904   /* Gather up all the == constraints. */
12905   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
12906     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
12907       pCon->pLink = p1;
12908       p1 = pCon;
12909     }
12910   }
12911 
12912   /* Create an index using the == constraints collected above. And the
12913   ** range constraint/ORDER BY terms passed in by the caller, if any. */
12914   rc = idxCreateFromCons(p, pScan, p1, pTail);
12915 
12916   /* If no range/ORDER BY passed by the caller, create a version of the
12917   ** index for each range constraint.  */
12918   if( pTail==0 ){
12919     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
12920       assert( pCon->pLink==0 );
12921       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
12922         rc = idxCreateFromCons(p, pScan, p1, pCon);
12923       }
12924     }
12925   }
12926 
12927   return rc;
12928 }
12929 
12930 /*
12931 ** Create candidate indexes in database [dbm] based on the data in
12932 ** linked-list pScan.
12933 */
idxCreateCandidates(sqlite3expert * p)12934 static int idxCreateCandidates(sqlite3expert *p){
12935   int rc = SQLITE_OK;
12936   IdxScan *pIter;
12937 
12938   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
12939     rc = idxCreateFromWhere(p, pIter, 0);
12940     if( rc==SQLITE_OK && pIter->pOrder ){
12941       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
12942     }
12943   }
12944 
12945   return rc;
12946 }
12947 
12948 /*
12949 ** Free all elements of the linked list starting at pConstraint.
12950 */
idxConstraintFree(IdxConstraint * pConstraint)12951 static void idxConstraintFree(IdxConstraint *pConstraint){
12952   IdxConstraint *pNext;
12953   IdxConstraint *p;
12954 
12955   for(p=pConstraint; p; p=pNext){
12956     pNext = p->pNext;
12957     sqlite3_free(p);
12958   }
12959 }
12960 
12961 /*
12962 ** Free all elements of the linked list starting from pScan up until pLast
12963 ** (pLast is not freed).
12964 */
idxScanFree(IdxScan * pScan,IdxScan * pLast)12965 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
12966   IdxScan *p;
12967   IdxScan *pNext;
12968   for(p=pScan; p!=pLast; p=pNext){
12969     pNext = p->pNextScan;
12970     idxConstraintFree(p->pOrder);
12971     idxConstraintFree(p->pEq);
12972     idxConstraintFree(p->pRange);
12973     sqlite3_free(p);
12974   }
12975 }
12976 
12977 /*
12978 ** Free all elements of the linked list starting from pStatement up
12979 ** until pLast (pLast is not freed).
12980 */
idxStatementFree(IdxStatement * pStatement,IdxStatement * pLast)12981 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
12982   IdxStatement *p;
12983   IdxStatement *pNext;
12984   for(p=pStatement; p!=pLast; p=pNext){
12985     pNext = p->pNext;
12986     sqlite3_free(p->zEQP);
12987     sqlite3_free(p->zIdx);
12988     sqlite3_free(p);
12989   }
12990 }
12991 
12992 /*
12993 ** Free the linked list of IdxTable objects starting at pTab.
12994 */
idxTableFree(IdxTable * pTab)12995 static void idxTableFree(IdxTable *pTab){
12996   IdxTable *pIter;
12997   IdxTable *pNext;
12998   for(pIter=pTab; pIter; pIter=pNext){
12999     pNext = pIter->pNext;
13000     sqlite3_free(pIter);
13001   }
13002 }
13003 
13004 /*
13005 ** Free the linked list of IdxWrite objects starting at pTab.
13006 */
idxWriteFree(IdxWrite * pTab)13007 static void idxWriteFree(IdxWrite *pTab){
13008   IdxWrite *pIter;
13009   IdxWrite *pNext;
13010   for(pIter=pTab; pIter; pIter=pNext){
13011     pNext = pIter->pNext;
13012     sqlite3_free(pIter);
13013   }
13014 }
13015 
13016 
13017 
13018 /*
13019 ** This function is called after candidate indexes have been created. It
13020 ** runs all the queries to see which indexes they prefer, and populates
13021 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
13022 */
idxFindIndexes(sqlite3expert * p,char ** pzErr)13023 static int idxFindIndexes(
13024   sqlite3expert *p,
13025   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
13026 ){
13027   IdxStatement *pStmt;
13028   sqlite3 *dbm = p->dbm;
13029   int rc = SQLITE_OK;
13030 
13031   IdxHash hIdx;
13032   idxHashInit(&hIdx);
13033 
13034   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
13035     IdxHashEntry *pEntry;
13036     sqlite3_stmt *pExplain = 0;
13037     idxHashClear(&hIdx);
13038     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
13039         "EXPLAIN QUERY PLAN %s", pStmt->zSql
13040     );
13041     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
13042       /* int iId = sqlite3_column_int(pExplain, 0); */
13043       /* int iParent = sqlite3_column_int(pExplain, 1); */
13044       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
13045       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
13046       int nDetail;
13047       int i;
13048 
13049       if( !zDetail ) continue;
13050       nDetail = STRLEN(zDetail);
13051 
13052       for(i=0; i<nDetail; i++){
13053         const char *zIdx = 0;
13054         if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
13055           zIdx = &zDetail[i+13];
13056         }else if( i+22<nDetail
13057             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
13058         ){
13059           zIdx = &zDetail[i+22];
13060         }
13061         if( zIdx ){
13062           const char *zSql;
13063           int nIdx = 0;
13064           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
13065             nIdx++;
13066           }
13067           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
13068           if( zSql ){
13069             idxHashAdd(&rc, &hIdx, zSql, 0);
13070             if( rc ) goto find_indexes_out;
13071           }
13072           break;
13073         }
13074       }
13075 
13076       if( zDetail[0]!='-' ){
13077         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
13078       }
13079     }
13080 
13081     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
13082       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
13083     }
13084 
13085     idxFinalize(&rc, pExplain);
13086   }
13087 
13088  find_indexes_out:
13089   idxHashClear(&hIdx);
13090   return rc;
13091 }
13092 
idxAuthCallback(void * pCtx,int eOp,const char * z3,const char * z4,const char * zDb,const char * zTrigger)13093 static int idxAuthCallback(
13094   void *pCtx,
13095   int eOp,
13096   const char *z3,
13097   const char *z4,
13098   const char *zDb,
13099   const char *zTrigger
13100 ){
13101   int rc = SQLITE_OK;
13102   (void)z4;
13103   (void)zTrigger;
13104   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
13105     if( sqlite3_stricmp(zDb, "main")==0 ){
13106       sqlite3expert *p = (sqlite3expert*)pCtx;
13107       IdxTable *pTab;
13108       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
13109         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
13110       }
13111       if( pTab ){
13112         IdxWrite *pWrite;
13113         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
13114           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
13115         }
13116         if( pWrite==0 ){
13117           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
13118           if( rc==SQLITE_OK ){
13119             pWrite->pTab = pTab;
13120             pWrite->eOp = eOp;
13121             pWrite->pNext = p->pWrite;
13122             p->pWrite = pWrite;
13123           }
13124         }
13125       }
13126     }
13127   }
13128   return rc;
13129 }
13130 
idxProcessOneTrigger(sqlite3expert * p,IdxWrite * pWrite,char ** pzErr)13131 static int idxProcessOneTrigger(
13132   sqlite3expert *p,
13133   IdxWrite *pWrite,
13134   char **pzErr
13135 ){
13136   static const char *zInt = UNIQUE_TABLE_NAME;
13137   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
13138   IdxTable *pTab = pWrite->pTab;
13139   const char *zTab = pTab->zName;
13140   const char *zSql =
13141     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
13142     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
13143     "ORDER BY type;";
13144   sqlite3_stmt *pSelect = 0;
13145   int rc = SQLITE_OK;
13146   char *zWrite = 0;
13147 
13148   /* Create the table and its triggers in the temp schema */
13149   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
13150   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
13151     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
13152     if( zCreate==0 ) continue;
13153     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
13154   }
13155   idxFinalize(&rc, pSelect);
13156 
13157   /* Rename the table in the temp schema to zInt */
13158   if( rc==SQLITE_OK ){
13159     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
13160     if( z==0 ){
13161       rc = SQLITE_NOMEM;
13162     }else{
13163       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
13164       sqlite3_free(z);
13165     }
13166   }
13167 
13168   switch( pWrite->eOp ){
13169     case SQLITE_INSERT: {
13170       int i;
13171       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
13172       for(i=0; i<pTab->nCol; i++){
13173         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
13174       }
13175       zWrite = idxAppendText(&rc, zWrite, ")");
13176       break;
13177     }
13178     case SQLITE_UPDATE: {
13179       int i;
13180       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
13181       for(i=0; i<pTab->nCol; i++){
13182         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
13183             pTab->aCol[i].zName
13184         );
13185       }
13186       break;
13187     }
13188     default: {
13189       assert( pWrite->eOp==SQLITE_DELETE );
13190       if( rc==SQLITE_OK ){
13191         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
13192         if( zWrite==0 ) rc = SQLITE_NOMEM;
13193       }
13194     }
13195   }
13196 
13197   if( rc==SQLITE_OK ){
13198     sqlite3_stmt *pX = 0;
13199     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
13200     idxFinalize(&rc, pX);
13201     if( rc!=SQLITE_OK ){
13202       idxDatabaseError(p->dbv, pzErr);
13203     }
13204   }
13205   sqlite3_free(zWrite);
13206 
13207   if( rc==SQLITE_OK ){
13208     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
13209   }
13210 
13211   return rc;
13212 }
13213 
idxProcessTriggers(sqlite3expert * p,char ** pzErr)13214 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
13215   int rc = SQLITE_OK;
13216   IdxWrite *pEnd = 0;
13217   IdxWrite *pFirst = p->pWrite;
13218 
13219   while( rc==SQLITE_OK && pFirst!=pEnd ){
13220     IdxWrite *pIter;
13221     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
13222       rc = idxProcessOneTrigger(p, pIter, pzErr);
13223     }
13224     pEnd = pFirst;
13225     pFirst = p->pWrite;
13226   }
13227 
13228   return rc;
13229 }
13230 
13231 
idxCreateVtabSchema(sqlite3expert * p,char ** pzErrmsg)13232 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
13233   int rc = idxRegisterVtab(p);
13234   sqlite3_stmt *pSchema = 0;
13235 
13236   /* For each table in the main db schema:
13237   **
13238   **   1) Add an entry to the p->pTable list, and
13239   **   2) Create the equivalent virtual table in dbv.
13240   */
13241   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
13242       "SELECT type, name, sql, 1 FROM sqlite_schema "
13243       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
13244       " UNION ALL "
13245       "SELECT type, name, sql, 2 FROM sqlite_schema "
13246       "WHERE type = 'trigger'"
13247       "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
13248       "ORDER BY 4, 1"
13249   );
13250   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
13251     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
13252     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
13253     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
13254 
13255     if( zType==0 || zName==0 ) continue;
13256     if( zType[0]=='v' || zType[1]=='r' ){
13257       if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
13258     }else{
13259       IdxTable *pTab;
13260       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
13261       if( rc==SQLITE_OK ){
13262         int i;
13263         char *zInner = 0;
13264         char *zOuter = 0;
13265         pTab->pNext = p->pTable;
13266         p->pTable = pTab;
13267 
13268         /* The statement the vtab will pass to sqlite3_declare_vtab() */
13269         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
13270         for(i=0; i<pTab->nCol; i++){
13271           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
13272               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
13273           );
13274         }
13275         zInner = idxAppendText(&rc, zInner, ")");
13276 
13277         /* The CVT statement to create the vtab */
13278         zOuter = idxAppendText(&rc, 0,
13279             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
13280         );
13281         if( rc==SQLITE_OK ){
13282           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
13283         }
13284         sqlite3_free(zInner);
13285         sqlite3_free(zOuter);
13286       }
13287     }
13288   }
13289   idxFinalize(&rc, pSchema);
13290   return rc;
13291 }
13292 
13293 struct IdxSampleCtx {
13294   int iTarget;
13295   double target;                  /* Target nRet/nRow value */
13296   double nRow;                    /* Number of rows seen */
13297   double nRet;                    /* Number of rows returned */
13298 };
13299 
idxSampleFunc(sqlite3_context * pCtx,int argc,sqlite3_value ** argv)13300 static void idxSampleFunc(
13301   sqlite3_context *pCtx,
13302   int argc,
13303   sqlite3_value **argv
13304 ){
13305   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
13306   int bRet;
13307 
13308   (void)argv;
13309   assert( argc==0 );
13310   if( p->nRow==0.0 ){
13311     bRet = 1;
13312   }else{
13313     bRet = (p->nRet / p->nRow) <= p->target;
13314     if( bRet==0 ){
13315       unsigned short rnd;
13316       sqlite3_randomness(2, (void*)&rnd);
13317       bRet = ((int)rnd % 100) <= p->iTarget;
13318     }
13319   }
13320 
13321   sqlite3_result_int(pCtx, bRet);
13322   p->nRow += 1.0;
13323   p->nRet += (double)bRet;
13324 }
13325 
13326 struct IdxRemCtx {
13327   int nSlot;
13328   struct IdxRemSlot {
13329     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
13330     i64 iVal;                     /* SQLITE_INTEGER value */
13331     double rVal;                  /* SQLITE_FLOAT value */
13332     int nByte;                    /* Bytes of space allocated at z */
13333     int n;                        /* Size of buffer z */
13334     char *z;                      /* SQLITE_TEXT/BLOB value */
13335   } aSlot[1];
13336 };
13337 
13338 /*
13339 ** Implementation of scalar function rem().
13340 */
idxRemFunc(sqlite3_context * pCtx,int argc,sqlite3_value ** argv)13341 static void idxRemFunc(
13342   sqlite3_context *pCtx,
13343   int argc,
13344   sqlite3_value **argv
13345 ){
13346   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
13347   struct IdxRemSlot *pSlot;
13348   int iSlot;
13349   assert( argc==2 );
13350 
13351   iSlot = sqlite3_value_int(argv[0]);
13352   assert( iSlot<=p->nSlot );
13353   pSlot = &p->aSlot[iSlot];
13354 
13355   switch( pSlot->eType ){
13356     case SQLITE_NULL:
13357       /* no-op */
13358       break;
13359 
13360     case SQLITE_INTEGER:
13361       sqlite3_result_int64(pCtx, pSlot->iVal);
13362       break;
13363 
13364     case SQLITE_FLOAT:
13365       sqlite3_result_double(pCtx, pSlot->rVal);
13366       break;
13367 
13368     case SQLITE_BLOB:
13369       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
13370       break;
13371 
13372     case SQLITE_TEXT:
13373       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
13374       break;
13375   }
13376 
13377   pSlot->eType = sqlite3_value_type(argv[1]);
13378   switch( pSlot->eType ){
13379     case SQLITE_NULL:
13380       /* no-op */
13381       break;
13382 
13383     case SQLITE_INTEGER:
13384       pSlot->iVal = sqlite3_value_int64(argv[1]);
13385       break;
13386 
13387     case SQLITE_FLOAT:
13388       pSlot->rVal = sqlite3_value_double(argv[1]);
13389       break;
13390 
13391     case SQLITE_BLOB:
13392     case SQLITE_TEXT: {
13393       int nByte = sqlite3_value_bytes(argv[1]);
13394       const void *pData = 0;
13395       if( nByte>pSlot->nByte ){
13396         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
13397         if( zNew==0 ){
13398           sqlite3_result_error_nomem(pCtx);
13399           return;
13400         }
13401         pSlot->nByte = nByte*2;
13402         pSlot->z = zNew;
13403       }
13404       pSlot->n = nByte;
13405       if( pSlot->eType==SQLITE_BLOB ){
13406         pData = sqlite3_value_blob(argv[1]);
13407         if( pData ) memcpy(pSlot->z, pData, nByte);
13408       }else{
13409         pData = sqlite3_value_text(argv[1]);
13410         memcpy(pSlot->z, pData, nByte);
13411       }
13412       break;
13413     }
13414   }
13415 }
13416 
idxLargestIndex(sqlite3 * db,int * pnMax,char ** pzErr)13417 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
13418   int rc = SQLITE_OK;
13419   const char *zMax =
13420     "SELECT max(i.seqno) FROM "
13421     "  sqlite_schema AS s, "
13422     "  pragma_index_list(s.name) AS l, "
13423     "  pragma_index_info(l.name) AS i "
13424     "WHERE s.type = 'table'";
13425   sqlite3_stmt *pMax = 0;
13426 
13427   *pnMax = 0;
13428   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
13429   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
13430     *pnMax = sqlite3_column_int(pMax, 0) + 1;
13431   }
13432   idxFinalize(&rc, pMax);
13433 
13434   return rc;
13435 }
13436 
idxPopulateOneStat1(sqlite3expert * p,sqlite3_stmt * pIndexXInfo,sqlite3_stmt * pWriteStat,const char * zTab,const char * zIdx,char ** pzErr)13437 static int idxPopulateOneStat1(
13438   sqlite3expert *p,
13439   sqlite3_stmt *pIndexXInfo,
13440   sqlite3_stmt *pWriteStat,
13441   const char *zTab,
13442   const char *zIdx,
13443   char **pzErr
13444 ){
13445   char *zCols = 0;
13446   char *zOrder = 0;
13447   char *zQuery = 0;
13448   int nCol = 0;
13449   int i;
13450   sqlite3_stmt *pQuery = 0;
13451   int *aStat = 0;
13452   int rc = SQLITE_OK;
13453 
13454   assert( p->iSample>0 );
13455 
13456   /* Formulate the query text */
13457   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
13458   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
13459     const char *zComma = zCols==0 ? "" : ", ";
13460     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
13461     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
13462     zCols = idxAppendText(&rc, zCols,
13463         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
13464     );
13465     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
13466   }
13467   sqlite3_reset(pIndexXInfo);
13468   if( rc==SQLITE_OK ){
13469     if( p->iSample==100 ){
13470       zQuery = sqlite3_mprintf(
13471           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
13472       );
13473     }else{
13474       zQuery = sqlite3_mprintf(
13475           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
13476       );
13477     }
13478   }
13479   sqlite3_free(zCols);
13480   sqlite3_free(zOrder);
13481 
13482   /* Formulate the query text */
13483   if( rc==SQLITE_OK ){
13484     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
13485     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
13486   }
13487   sqlite3_free(zQuery);
13488 
13489   if( rc==SQLITE_OK ){
13490     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
13491   }
13492   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
13493     IdxHashEntry *pEntry;
13494     char *zStat = 0;
13495     for(i=0; i<=nCol; i++) aStat[i] = 1;
13496     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
13497       aStat[0]++;
13498       for(i=0; i<nCol; i++){
13499         if( sqlite3_column_int(pQuery, i)==0 ) break;
13500       }
13501       for(/*no-op*/; i<nCol; i++){
13502         aStat[i+1]++;
13503       }
13504     }
13505 
13506     if( rc==SQLITE_OK ){
13507       int s0 = aStat[0];
13508       zStat = sqlite3_mprintf("%d", s0);
13509       if( zStat==0 ) rc = SQLITE_NOMEM;
13510       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
13511         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
13512       }
13513     }
13514 
13515     if( rc==SQLITE_OK ){
13516       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
13517       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
13518       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
13519       sqlite3_step(pWriteStat);
13520       rc = sqlite3_reset(pWriteStat);
13521     }
13522 
13523     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
13524     if( pEntry ){
13525       assert( pEntry->zVal2==0 );
13526       pEntry->zVal2 = zStat;
13527     }else{
13528       sqlite3_free(zStat);
13529     }
13530   }
13531   sqlite3_free(aStat);
13532   idxFinalize(&rc, pQuery);
13533 
13534   return rc;
13535 }
13536 
idxBuildSampleTable(sqlite3expert * p,const char * zTab)13537 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
13538   int rc;
13539   char *zSql;
13540 
13541   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
13542   if( rc!=SQLITE_OK ) return rc;
13543 
13544   zSql = sqlite3_mprintf(
13545       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
13546   );
13547   if( zSql==0 ) return SQLITE_NOMEM;
13548   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
13549   sqlite3_free(zSql);
13550 
13551   return rc;
13552 }
13553 
13554 /*
13555 ** This function is called as part of sqlite3_expert_analyze(). Candidate
13556 ** indexes have already been created in database sqlite3expert.dbm, this
13557 ** function populates sqlite_stat1 table in the same database.
13558 **
13559 ** The stat1 data is generated by querying the
13560 */
idxPopulateStat1(sqlite3expert * p,char ** pzErr)13561 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
13562   int rc = SQLITE_OK;
13563   int nMax =0;
13564   struct IdxRemCtx *pCtx = 0;
13565   struct IdxSampleCtx samplectx;
13566   int i;
13567   i64 iPrev = -100000;
13568   sqlite3_stmt *pAllIndex = 0;
13569   sqlite3_stmt *pIndexXInfo = 0;
13570   sqlite3_stmt *pWrite = 0;
13571 
13572   const char *zAllIndex =
13573     "SELECT s.rowid, s.name, l.name FROM "
13574     "  sqlite_schema AS s, "
13575     "  pragma_index_list(s.name) AS l "
13576     "WHERE s.type = 'table'";
13577   const char *zIndexXInfo =
13578     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
13579   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
13580 
13581   /* If iSample==0, no sqlite_stat1 data is required. */
13582   if( p->iSample==0 ) return SQLITE_OK;
13583 
13584   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
13585   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
13586 
13587   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
13588 
13589   if( rc==SQLITE_OK ){
13590     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
13591     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
13592   }
13593 
13594   if( rc==SQLITE_OK ){
13595     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
13596     rc = sqlite3_create_function(
13597         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
13598     );
13599   }
13600   if( rc==SQLITE_OK ){
13601     rc = sqlite3_create_function(
13602         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
13603     );
13604   }
13605 
13606   if( rc==SQLITE_OK ){
13607     pCtx->nSlot = nMax+1;
13608     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
13609   }
13610   if( rc==SQLITE_OK ){
13611     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
13612   }
13613   if( rc==SQLITE_OK ){
13614     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
13615   }
13616 
13617   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
13618     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
13619     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
13620     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
13621     if( zTab==0 || zIdx==0 ) continue;
13622     if( p->iSample<100 && iPrev!=iRowid ){
13623       samplectx.target = (double)p->iSample / 100.0;
13624       samplectx.iTarget = p->iSample;
13625       samplectx.nRow = 0.0;
13626       samplectx.nRet = 0.0;
13627       rc = idxBuildSampleTable(p, zTab);
13628       if( rc!=SQLITE_OK ) break;
13629     }
13630     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
13631     iPrev = iRowid;
13632   }
13633   if( rc==SQLITE_OK && p->iSample<100 ){
13634     rc = sqlite3_exec(p->dbv,
13635         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
13636     );
13637   }
13638 
13639   idxFinalize(&rc, pAllIndex);
13640   idxFinalize(&rc, pIndexXInfo);
13641   idxFinalize(&rc, pWrite);
13642 
13643   if( pCtx ){
13644     for(i=0; i<pCtx->nSlot; i++){
13645       sqlite3_free(pCtx->aSlot[i].z);
13646     }
13647     sqlite3_free(pCtx);
13648   }
13649 
13650   if( rc==SQLITE_OK ){
13651     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
13652   }
13653 
13654   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
13655   return rc;
13656 }
13657 
13658 /*
13659 ** Define and possibly pretend to use a useless collation sequence.
13660 ** This pretense allows expert to accept SQL using custom collations.
13661 */
dummyCompare(void * up1,int up2,const void * up3,int up4,const void * up5)13662 int dummyCompare(void *up1, int up2, const void *up3, int up4, const void *up5){
13663   (void)up1;
13664   (void)up2;
13665   (void)up3;
13666   (void)up4;
13667   (void)up5;
13668   assert(0); /* VDBE should never be run. */
13669   return 0;
13670 }
13671 /* And a callback to register above upon actual need */
useDummyCS(void * up1,sqlite3 * db,int etr,const char * zName)13672 void useDummyCS(void *up1, sqlite3 *db, int etr, const char *zName){
13673   (void)up1;
13674   sqlite3_create_collation_v2(db, zName, etr, 0, dummyCompare, 0);
13675 }
13676 
13677 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
13678   && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
13679 /*
13680 ** dummy functions for no-op implementation of UDFs during expert's work
13681 */
dummyUDF(sqlite3_context * up1,int up2,sqlite3_value ** up3)13682 void dummyUDF(sqlite3_context *up1, int up2, sqlite3_value **up3){
13683   (void)up1;
13684   (void)up2;
13685   (void)up3;
13686   assert(0); /* VDBE should never be run. */
13687 }
dummyUDFvalue(sqlite3_context * up1)13688 void dummyUDFvalue(sqlite3_context *up1){
13689   (void)up1;
13690   assert(0); /* VDBE should never be run. */
13691 }
13692 
13693 /*
13694 ** Register UDFs from user database with another.
13695 */
registerUDFs(sqlite3 * dbSrc,sqlite3 * dbDst)13696 int registerUDFs(sqlite3 *dbSrc, sqlite3 *dbDst){
13697   sqlite3_stmt *pStmt;
13698   int rc = sqlite3_prepare_v2(dbSrc,
13699             "SELECT name,type,enc,narg,flags "
13700             "FROM pragma_function_list() "
13701             "WHERE builtin==0", -1, &pStmt, 0);
13702   if( rc==SQLITE_OK ){
13703     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
13704       int nargs = sqlite3_column_int(pStmt,3);
13705       int flags = sqlite3_column_int(pStmt,4);
13706       const char *name = (char*)sqlite3_column_text(pStmt,0);
13707       const char *type = (char*)sqlite3_column_text(pStmt,1);
13708       const char *enc = (char*)sqlite3_column_text(pStmt,2);
13709       if( name==0 || type==0 || enc==0 ){
13710         /* no-op.  Only happens on OOM */
13711       }else{
13712         int ienc = SQLITE_UTF8;
13713         int rcf = SQLITE_ERROR;
13714         if( strcmp(enc,"utf16le")==0 ) ienc = SQLITE_UTF16LE;
13715         else if( strcmp(enc,"utf16be")==0 ) ienc = SQLITE_UTF16BE;
13716         ienc |= (flags & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY));
13717         if( strcmp(type,"w")==0 ){
13718           rcf = sqlite3_create_window_function(dbDst,name,nargs,ienc,0,
13719                                                dummyUDF,dummyUDFvalue,0,0,0);
13720         }else if( strcmp(type,"a")==0 ){
13721           rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
13722                                         0,dummyUDF,dummyUDFvalue);
13723         }else if( strcmp(type,"s")==0 ){
13724           rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
13725                                         dummyUDF,0,0);
13726         }
13727         if( rcf!=SQLITE_OK ){
13728           rc = rcf;
13729           break;
13730         }
13731       }
13732     }
13733     sqlite3_finalize(pStmt);
13734     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
13735   }
13736   return rc;
13737 }
13738 #endif
13739 
13740 /*
13741 ** Allocate a new sqlite3expert object.
13742 */
sqlite3_expert_new(sqlite3 * db,char ** pzErrmsg)13743 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
13744   int rc = SQLITE_OK;
13745   sqlite3expert *pNew;
13746 
13747   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
13748 
13749   /* Open two in-memory databases to work with. The "vtab database" (dbv)
13750   ** will contain a virtual table corresponding to each real table in
13751   ** the user database schema, and a copy of each view. It is used to
13752   ** collect information regarding the WHERE, ORDER BY and other clauses
13753   ** of the user's query.
13754   */
13755   if( rc==SQLITE_OK ){
13756     pNew->db = db;
13757     pNew->iSample = 100;
13758     rc = sqlite3_open(":memory:", &pNew->dbv);
13759   }
13760   if( rc==SQLITE_OK ){
13761     rc = sqlite3_open(":memory:", &pNew->dbm);
13762     if( rc==SQLITE_OK ){
13763       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
13764     }
13765   }
13766 
13767   /* Allow custom collations to be dealt with through prepare. */
13768   if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS);
13769   if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS);
13770 
13771 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
13772   && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
13773   /* Register UDFs from database [db] with [dbm] and [dbv]. */
13774   if( rc==SQLITE_OK ){
13775     rc = registerUDFs(pNew->db, pNew->dbm);
13776   }
13777   if( rc==SQLITE_OK ){
13778     rc = registerUDFs(pNew->db, pNew->dbv);
13779   }
13780 #endif
13781 
13782   /* Copy the entire schema of database [db] into [dbm]. */
13783   if( rc==SQLITE_OK ){
13784     sqlite3_stmt *pSql = 0;
13785     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
13786         "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
13787         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
13788     );
13789     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13790       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
13791       if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
13792     }
13793     idxFinalize(&rc, pSql);
13794   }
13795 
13796   /* Create the vtab schema */
13797   if( rc==SQLITE_OK ){
13798     rc = idxCreateVtabSchema(pNew, pzErrmsg);
13799   }
13800 
13801   /* Register the auth callback with dbv */
13802   if( rc==SQLITE_OK ){
13803     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
13804   }
13805 
13806   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
13807   ** return the new sqlite3expert handle.  */
13808   if( rc!=SQLITE_OK ){
13809     sqlite3_expert_destroy(pNew);
13810     pNew = 0;
13811   }
13812   return pNew;
13813 }
13814 
13815 /*
13816 ** Configure an sqlite3expert object.
13817 */
sqlite3_expert_config(sqlite3expert * p,int op,...)13818 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
13819   int rc = SQLITE_OK;
13820   va_list ap;
13821   va_start(ap, op);
13822   switch( op ){
13823     case EXPERT_CONFIG_SAMPLE: {
13824       int iVal = va_arg(ap, int);
13825       if( iVal<0 ) iVal = 0;
13826       if( iVal>100 ) iVal = 100;
13827       p->iSample = iVal;
13828       break;
13829     }
13830     default:
13831       rc = SQLITE_NOTFOUND;
13832       break;
13833   }
13834 
13835   va_end(ap);
13836   return rc;
13837 }
13838 
13839 /*
13840 ** Add an SQL statement to the analysis.
13841 */
sqlite3_expert_sql(sqlite3expert * p,const char * zSql,char ** pzErr)13842 int sqlite3_expert_sql(
13843   sqlite3expert *p,               /* From sqlite3_expert_new() */
13844   const char *zSql,               /* SQL statement to add */
13845   char **pzErr                    /* OUT: Error message (if any) */
13846 ){
13847   IdxScan *pScanOrig = p->pScan;
13848   IdxStatement *pStmtOrig = p->pStatement;
13849   int rc = SQLITE_OK;
13850   const char *zStmt = zSql;
13851 
13852   if( p->bRun ) return SQLITE_MISUSE;
13853 
13854   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
13855     sqlite3_stmt *pStmt = 0;
13856     /* Ensure that the provided statement compiles against user's DB. */
13857     rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt);
13858     if( rc!=SQLITE_OK ) break;
13859     sqlite3_finalize(pStmt);
13860     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
13861     if( rc==SQLITE_OK ){
13862       if( pStmt ){
13863         IdxStatement *pNew;
13864         const char *z = sqlite3_sql(pStmt);
13865         int n = STRLEN(z);
13866         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
13867         if( rc==SQLITE_OK ){
13868           pNew->zSql = (char*)&pNew[1];
13869           memcpy(pNew->zSql, z, n+1);
13870           pNew->pNext = p->pStatement;
13871           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
13872           p->pStatement = pNew;
13873         }
13874         sqlite3_finalize(pStmt);
13875       }
13876     }else{
13877       idxDatabaseError(p->dbv, pzErr);
13878     }
13879   }
13880 
13881   if( rc!=SQLITE_OK ){
13882     idxScanFree(p->pScan, pScanOrig);
13883     idxStatementFree(p->pStatement, pStmtOrig);
13884     p->pScan = pScanOrig;
13885     p->pStatement = pStmtOrig;
13886   }
13887 
13888   return rc;
13889 }
13890 
sqlite3_expert_analyze(sqlite3expert * p,char ** pzErr)13891 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
13892   int rc;
13893   IdxHashEntry *pEntry;
13894 
13895   /* Do trigger processing to collect any extra IdxScan structures */
13896   rc = idxProcessTriggers(p, pzErr);
13897 
13898   /* Create candidate indexes within the in-memory database file */
13899   if( rc==SQLITE_OK ){
13900     rc = idxCreateCandidates(p);
13901   }else if ( rc==SQLITE_BUSY_TIMEOUT ){
13902     if( pzErr )
13903       *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
13904     return rc;
13905   }
13906 
13907   /* Generate the stat1 data */
13908   if( rc==SQLITE_OK ){
13909     rc = idxPopulateStat1(p, pzErr);
13910   }
13911 
13912   /* Formulate the EXPERT_REPORT_CANDIDATES text */
13913   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
13914     p->zCandidates = idxAppendText(&rc, p->zCandidates,
13915         "%s;%s%s\n", pEntry->zVal,
13916         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
13917     );
13918   }
13919 
13920   /* Figure out which of the candidate indexes are preferred by the query
13921   ** planner and report the results to the user.  */
13922   if( rc==SQLITE_OK ){
13923     rc = idxFindIndexes(p, pzErr);
13924   }
13925 
13926   if( rc==SQLITE_OK ){
13927     p->bRun = 1;
13928   }
13929   return rc;
13930 }
13931 
13932 /*
13933 ** Return the total number of statements that have been added to this
13934 ** sqlite3expert using sqlite3_expert_sql().
13935 */
sqlite3_expert_count(sqlite3expert * p)13936 int sqlite3_expert_count(sqlite3expert *p){
13937   int nRet = 0;
13938   if( p->pStatement ) nRet = p->pStatement->iId+1;
13939   return nRet;
13940 }
13941 
13942 /*
13943 ** Return a component of the report.
13944 */
sqlite3_expert_report(sqlite3expert * p,int iStmt,int eReport)13945 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
13946   const char *zRet = 0;
13947   IdxStatement *pStmt;
13948 
13949   if( p->bRun==0 ) return 0;
13950   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
13951   switch( eReport ){
13952     case EXPERT_REPORT_SQL:
13953       if( pStmt ) zRet = pStmt->zSql;
13954       break;
13955     case EXPERT_REPORT_INDEXES:
13956       if( pStmt ) zRet = pStmt->zIdx;
13957       break;
13958     case EXPERT_REPORT_PLAN:
13959       if( pStmt ) zRet = pStmt->zEQP;
13960       break;
13961     case EXPERT_REPORT_CANDIDATES:
13962       zRet = p->zCandidates;
13963       break;
13964   }
13965   return zRet;
13966 }
13967 
13968 /*
13969 ** Free an sqlite3expert object.
13970 */
sqlite3_expert_destroy(sqlite3expert * p)13971 void sqlite3_expert_destroy(sqlite3expert *p){
13972   if( p ){
13973     sqlite3_close(p->dbm);
13974     sqlite3_close(p->dbv);
13975     idxScanFree(p->pScan, 0);
13976     idxStatementFree(p->pStatement, 0);
13977     idxTableFree(p->pTable);
13978     idxWriteFree(p->pWrite);
13979     idxHashClear(&p->hIdx);
13980     sqlite3_free(p->zCandidates);
13981     sqlite3_free(p);
13982   }
13983 }
13984 
13985 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13986 
13987 /************************* End ../ext/expert/sqlite3expert.c ********************/
13988 
13989 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
13990 #define SQLITE_SHELL_HAVE_RECOVER 1
13991 #else
13992 #define SQLITE_SHELL_HAVE_RECOVER 0
13993 #endif
13994 #if SQLITE_SHELL_HAVE_RECOVER
13995 /************************* Begin ../ext/recover/sqlite3recover.h ******************/
13996 /*
13997 ** 2022-08-27
13998 **
13999 ** The author disclaims copyright to this source code.  In place of
14000 ** a legal notice, here is a blessing:
14001 **
14002 **    May you do good and not evil.
14003 **    May you find forgiveness for yourself and forgive others.
14004 **    May you share freely, never taking more than you give.
14005 **
14006 *************************************************************************
14007 **
14008 ** This file contains the public interface to the "recover" extension -
14009 ** an SQLite extension designed to recover data from corrupted database
14010 ** files.
14011 */
14012 
14013 /*
14014 ** OVERVIEW:
14015 **
14016 ** To use the API to recover data from a corrupted database, an
14017 ** application:
14018 **
14019 **   1) Creates an sqlite3_recover handle by calling either
14020 **      sqlite3_recover_init() or sqlite3_recover_init_sql().
14021 **
14022 **   2) Configures the new handle using one or more calls to
14023 **      sqlite3_recover_config().
14024 **
14025 **   3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
14026 **      the handle until it returns something other than SQLITE_OK. If it
14027 **      returns SQLITE_DONE, then the recovery operation completed without
14028 **      error. If it returns some other non-SQLITE_OK value, then an error
14029 **      has occurred.
14030 **
14031 **   4) Retrieves any error code and English language error message using the
14032 **      sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
14033 **      respectively.
14034 **
14035 **   5) Destroys the sqlite3_recover handle and frees all resources
14036 **      using sqlite3_recover_finish().
14037 **
14038 ** The application may abandon the recovery operation at any point
14039 ** before it is finished by passing the sqlite3_recover handle to
14040 ** sqlite3_recover_finish(). This is not an error, but the final state
14041 ** of the output database, or the results of running the partial script
14042 ** delivered to the SQL callback, are undefined.
14043 */
14044 
14045 #ifndef _SQLITE_RECOVER_H
14046 #define _SQLITE_RECOVER_H
14047 
14048 /* #include "sqlite3.h" */
14049 
14050 #ifdef __cplusplus
14051 extern "C" {
14052 #endif
14053 
14054 /*
14055 ** An instance of the sqlite3_recover object represents a recovery
14056 ** operation in progress.
14057 **
14058 ** Constructors:
14059 **
14060 **    sqlite3_recover_init()
14061 **    sqlite3_recover_init_sql()
14062 **
14063 ** Destructor:
14064 **
14065 **    sqlite3_recover_finish()
14066 **
14067 ** Methods:
14068 **
14069 **    sqlite3_recover_config()
14070 **    sqlite3_recover_errcode()
14071 **    sqlite3_recover_errmsg()
14072 **    sqlite3_recover_run()
14073 **    sqlite3_recover_step()
14074 */
14075 typedef struct sqlite3_recover sqlite3_recover;
14076 
14077 /*
14078 ** These two APIs attempt to create and return a new sqlite3_recover object.
14079 ** In both cases the first two arguments identify the (possibly
14080 ** corrupt) database to recover data from. The first argument is an open
14081 ** database handle and the second the name of a database attached to that
14082 ** handle (i.e. "main", "temp" or the name of an attached database).
14083 **
14084 ** If sqlite3_recover_init() is used to create the new sqlite3_recover
14085 ** handle, then data is recovered into a new database, identified by
14086 ** string parameter zUri. zUri may be an absolute or relative file path,
14087 ** or may be an SQLite URI. If the identified database file already exists,
14088 ** it is overwritten.
14089 **
14090 ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
14091 ** be returned to the user as a series of SQL statements. Executing these
14092 ** SQL statements results in the same database as would have been created
14093 ** had sqlite3_recover_init() been used. For each SQL statement in the
14094 ** output, the callback function passed as the third argument (xSql) is
14095 ** invoked once. The first parameter is a passed a copy of the fourth argument
14096 ** to this function (pCtx) as its first parameter, and a pointer to a
14097 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
14098 ** the second. If the xSql callback returns any value other than SQLITE_OK,
14099 ** then processing is immediately abandoned and the value returned used as
14100 ** the recover handle error code (see below).
14101 **
14102 ** If an out-of-memory error occurs, NULL may be returned instead of
14103 ** a valid handle. In all other cases, it is the responsibility of the
14104 ** application to avoid resource leaks by ensuring that
14105 ** sqlite3_recover_finish() is called on all allocated handles.
14106 */
14107 sqlite3_recover *sqlite3_recover_init(
14108   sqlite3* db,
14109   const char *zDb,
14110   const char *zUri
14111 );
14112 sqlite3_recover *sqlite3_recover_init_sql(
14113   sqlite3* db,
14114   const char *zDb,
14115   int (*xSql)(void*, const char*),
14116   void *pCtx
14117 );
14118 
14119 /*
14120 ** Configure an sqlite3_recover object that has just been created using
14121 ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
14122 ** may only be called before the first call to sqlite3_recover_step()
14123 ** or sqlite3_recover_run() on the object.
14124 **
14125 ** The second argument passed to this function must be one of the
14126 ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
14127 ** depend on the specific SQLITE_RECOVER_* symbol in use.
14128 **
14129 ** SQLITE_OK is returned if the configuration operation was successful,
14130 ** or an SQLite error code otherwise.
14131 */
14132 int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
14133 
14134 /*
14135 ** SQLITE_RECOVER_LOST_AND_FOUND:
14136 **   The pArg argument points to a string buffer containing the name
14137 **   of a "lost-and-found" table in the output database, or NULL. If
14138 **   the argument is non-NULL and the database contains seemingly
14139 **   valid pages that cannot be associated with any table in the
14140 **   recovered part of the schema, data is extracted from these
14141 **   pages to add to the lost-and-found table.
14142 **
14143 ** SQLITE_RECOVER_FREELIST_CORRUPT:
14144 **   The pArg value must actually be a pointer to a value of type
14145 **   int containing value 0 or 1 cast as a (void*). If this option is set
14146 **   (argument is 1) and a lost-and-found table has been configured using
14147 **   SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
14148 **   corrupt and an attempt is made to recover records from pages that
14149 **   appear to be linked into the freelist. Otherwise, pages on the freelist
14150 **   are ignored. Setting this option can recover more data from the
14151 **   database, but often ends up "recovering" deleted records. The default
14152 **   value is 0 (clear).
14153 **
14154 ** SQLITE_RECOVER_ROWIDS:
14155 **   The pArg value must actually be a pointer to a value of type
14156 **   int containing value 0 or 1 cast as a (void*). If this option is set
14157 **   (argument is 1), then an attempt is made to recover rowid values
14158 **   that are not also INTEGER PRIMARY KEY values. If this option is
14159 **   clear, then new rowids are assigned to all recovered rows. The
14160 **   default value is 1 (set).
14161 **
14162 ** SQLITE_RECOVER_SLOWINDEXES:
14163 **   The pArg value must actually be a pointer to a value of type
14164 **   int containing value 0 or 1 cast as a (void*). If this option is clear
14165 **   (argument is 0), then when creating an output database, the recover
14166 **   module creates and populates non-UNIQUE indexes right at the end of the
14167 **   recovery operation - after all recoverable data has been inserted
14168 **   into the new database. This is faster overall, but means that the
14169 **   final call to sqlite3_recover_step() for a recovery operation may
14170 **   be need to create a large number of indexes, which may be very slow.
14171 **
14172 **   Or, if this option is set (argument is 1), then non-UNIQUE indexes
14173 **   are created in the output database before it is populated with
14174 **   recovered data. This is slower overall, but avoids the slow call
14175 **   to sqlite3_recover_step() at the end of the recovery operation.
14176 **
14177 **   The default option value is 0.
14178 */
14179 #define SQLITE_RECOVER_LOST_AND_FOUND   1
14180 #define SQLITE_RECOVER_FREELIST_CORRUPT 2
14181 #define SQLITE_RECOVER_ROWIDS           3
14182 #define SQLITE_RECOVER_SLOWINDEXES      4
14183 
14184 /*
14185 ** Perform a unit of work towards the recovery operation. This function
14186 ** must normally be called multiple times to complete database recovery.
14187 **
14188 ** If no error occurs but the recovery operation is not completed, this
14189 ** function returns SQLITE_OK. If recovery has been completed successfully
14190 ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
14191 ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
14192 ** considered an error if some or all of the data cannot be recovered
14193 ** due to database corruption.
14194 **
14195 ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
14196 ** all further such calls on the same recover handle are no-ops that return
14197 ** the same non-SQLITE_OK value.
14198 */
14199 int sqlite3_recover_step(sqlite3_recover*);
14200 
14201 /*
14202 ** Run the recovery operation to completion. Return SQLITE_OK if successful,
14203 ** or an SQLite error code otherwise. Calling this function is the same
14204 ** as executing:
14205 **
14206 **     while( SQLITE_OK==sqlite3_recover_step(p) );
14207 **     return sqlite3_recover_errcode(p);
14208 */
14209 int sqlite3_recover_run(sqlite3_recover*);
14210 
14211 /*
14212 ** If an error has been encountered during a prior call to
14213 ** sqlite3_recover_step(), then this function attempts to return a
14214 ** pointer to a buffer containing an English language explanation of
14215 ** the error. If no error message is available, or if an out-of memory
14216 ** error occurs while attempting to allocate a buffer in which to format
14217 ** the error message, NULL is returned.
14218 **
14219 ** The returned buffer remains valid until the sqlite3_recover handle is
14220 ** destroyed using sqlite3_recover_finish().
14221 */
14222 const char *sqlite3_recover_errmsg(sqlite3_recover*);
14223 
14224 /*
14225 ** If this function is called on an sqlite3_recover handle after
14226 ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
14227 */
14228 int sqlite3_recover_errcode(sqlite3_recover*);
14229 
14230 /*
14231 ** Clean up a recovery object created by a call to sqlite3_recover_init().
14232 ** The results of using a recovery object with any API after it has been
14233 ** passed to this function are undefined.
14234 **
14235 ** This function returns the same value as sqlite3_recover_errcode().
14236 */
14237 int sqlite3_recover_finish(sqlite3_recover*);
14238 
14239 
14240 #ifdef __cplusplus
14241 }  /* end of the 'extern "C"' block */
14242 #endif
14243 
14244 #endif /* ifndef _SQLITE_RECOVER_H */
14245 
14246 /************************* End ../ext/recover/sqlite3recover.h ********************/
14247 # ifndef SQLITE_HAVE_SQLITE3R
14248 /************************* Begin ../ext/recover/dbdata.c ******************/
14249 /*
14250 ** 2019-04-17
14251 **
14252 ** The author disclaims copyright to this source code.  In place of
14253 ** a legal notice, here is a blessing:
14254 **
14255 **    May you do good and not evil.
14256 **    May you find forgiveness for yourself and forgive others.
14257 **    May you share freely, never taking more than you give.
14258 **
14259 ******************************************************************************
14260 **
14261 ** This file contains an implementation of two eponymous virtual tables,
14262 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
14263 ** "sqlite_dbpage" eponymous virtual table be available.
14264 **
14265 ** SQLITE_DBDATA:
14266 **   sqlite_dbdata is used to extract data directly from a database b-tree
14267 **   page and its associated overflow pages, bypassing the b-tree layer.
14268 **   The table schema is equivalent to:
14269 **
14270 **     CREATE TABLE sqlite_dbdata(
14271 **       pgno INTEGER,
14272 **       cell INTEGER,
14273 **       field INTEGER,
14274 **       value ANY,
14275 **       schema TEXT HIDDEN
14276 **     );
14277 **
14278 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
14279 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
14280 **   "schema".
14281 **
14282 **   Each page of the database is inspected. If it cannot be interpreted as
14283 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
14284 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
14285 **   table contains one row for each field in the record associated with
14286 **   each cell on the page. For intkey b-trees, the key value is stored in
14287 **   field -1.
14288 **
14289 **   For example, for the database:
14290 **
14291 **     CREATE TABLE t1(a, b);     -- root page is page 2
14292 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
14293 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
14294 **
14295 **   the sqlite_dbdata table contains, as well as from entries related to
14296 **   page 1, content equivalent to:
14297 **
14298 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
14299 **         (2, 0, -1, 5     ),
14300 **         (2, 0,  0, 'v'   ),
14301 **         (2, 0,  1, 'five'),
14302 **         (2, 1, -1, 10    ),
14303 **         (2, 1,  0, 'x'   ),
14304 **         (2, 1,  1, 'ten' );
14305 **
14306 **   If database corruption is encountered, this module does not report an
14307 **   error. Instead, it attempts to extract as much data as possible and
14308 **   ignores the corruption.
14309 **
14310 ** SQLITE_DBPTR:
14311 **   The sqlite_dbptr table has the following schema:
14312 **
14313 **     CREATE TABLE sqlite_dbptr(
14314 **       pgno INTEGER,
14315 **       child INTEGER,
14316 **       schema TEXT HIDDEN
14317 **     );
14318 **
14319 **   It contains one entry for each b-tree pointer between a parent and
14320 **   child page in the database.
14321 */
14322 
14323 #if !defined(SQLITEINT_H)
14324 /* #include "sqlite3.h" */
14325 
14326 /* typedef unsigned char u8; */
14327 /* typedef unsigned int u32; */
14328 
14329 #endif
14330 #include <string.h>
14331 #include <assert.h>
14332 
14333 #ifndef SQLITE_OMIT_VIRTUALTABLE
14334 
14335 #define DBDATA_PADDING_BYTES 100
14336 
14337 typedef struct DbdataTable DbdataTable;
14338 typedef struct DbdataCursor DbdataCursor;
14339 
14340 /* Cursor object */
14341 struct DbdataCursor {
14342   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
14343   sqlite3_stmt *pStmt;            /* For fetching database pages */
14344 
14345   int iPgno;                      /* Current page number */
14346   u8 *aPage;                      /* Buffer containing page */
14347   int nPage;                      /* Size of aPage[] in bytes */
14348   int nCell;                      /* Number of cells on aPage[] */
14349   int iCell;                      /* Current cell number */
14350   int bOnePage;                   /* True to stop after one page */
14351   int szDb;
14352   sqlite3_int64 iRowid;
14353 
14354   /* Only for the sqlite_dbdata table */
14355   u8 *pRec;                       /* Buffer containing current record */
14356   sqlite3_int64 nRec;             /* Size of pRec[] in bytes */
14357   sqlite3_int64 nHdr;             /* Size of header in bytes */
14358   int iField;                     /* Current field number */
14359   u8 *pHdrPtr;
14360   u8 *pPtr;
14361   u32 enc;                        /* Text encoding */
14362 
14363   sqlite3_int64 iIntkey;          /* Integer key value */
14364 };
14365 
14366 /* Table object */
14367 struct DbdataTable {
14368   sqlite3_vtab base;              /* Base class.  Must be first */
14369   sqlite3 *db;                    /* The database connection */
14370   sqlite3_stmt *pStmt;            /* For fetching database pages */
14371   int bPtr;                       /* True for sqlite3_dbptr table */
14372 };
14373 
14374 /* Column and schema definitions for sqlite_dbdata */
14375 #define DBDATA_COLUMN_PGNO        0
14376 #define DBDATA_COLUMN_CELL        1
14377 #define DBDATA_COLUMN_FIELD       2
14378 #define DBDATA_COLUMN_VALUE       3
14379 #define DBDATA_COLUMN_SCHEMA      4
14380 #define DBDATA_SCHEMA             \
14381       "CREATE TABLE x("           \
14382       "  pgno INTEGER,"           \
14383       "  cell INTEGER,"           \
14384       "  field INTEGER,"          \
14385       "  value ANY,"              \
14386       "  schema TEXT HIDDEN"      \
14387       ")"
14388 
14389 /* Column and schema definitions for sqlite_dbptr */
14390 #define DBPTR_COLUMN_PGNO         0
14391 #define DBPTR_COLUMN_CHILD        1
14392 #define DBPTR_COLUMN_SCHEMA       2
14393 #define DBPTR_SCHEMA              \
14394       "CREATE TABLE x("           \
14395       "  pgno INTEGER,"           \
14396       "  child INTEGER,"          \
14397       "  schema TEXT HIDDEN"      \
14398       ")"
14399 
14400 /*
14401 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
14402 ** table.
14403 */
dbdataConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)14404 static int dbdataConnect(
14405   sqlite3 *db,
14406   void *pAux,
14407   int argc, const char *const*argv,
14408   sqlite3_vtab **ppVtab,
14409   char **pzErr
14410 ){
14411   DbdataTable *pTab = 0;
14412   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
14413 
14414   (void)argc;
14415   (void)argv;
14416   (void)pzErr;
14417   sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS);
14418   if( rc==SQLITE_OK ){
14419     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
14420     if( pTab==0 ){
14421       rc = SQLITE_NOMEM;
14422     }else{
14423       memset(pTab, 0, sizeof(DbdataTable));
14424       pTab->db = db;
14425       pTab->bPtr = (pAux!=0);
14426     }
14427   }
14428 
14429   *ppVtab = (sqlite3_vtab*)pTab;
14430   return rc;
14431 }
14432 
14433 /*
14434 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
14435 */
dbdataDisconnect(sqlite3_vtab * pVtab)14436 static int dbdataDisconnect(sqlite3_vtab *pVtab){
14437   DbdataTable *pTab = (DbdataTable*)pVtab;
14438   if( pTab ){
14439     sqlite3_finalize(pTab->pStmt);
14440     sqlite3_free(pVtab);
14441   }
14442   return SQLITE_OK;
14443 }
14444 
14445 /*
14446 ** This function interprets two types of constraints:
14447 **
14448 **       schema=?
14449 **       pgno=?
14450 **
14451 ** If neither are present, idxNum is set to 0. If schema=? is present,
14452 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
14453 ** in idxNum is set.
14454 **
14455 ** If both parameters are present, schema is in position 0 and pgno in
14456 ** position 1.
14457 */
dbdataBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdx)14458 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
14459   DbdataTable *pTab = (DbdataTable*)tab;
14460   int i;
14461   int iSchema = -1;
14462   int iPgno = -1;
14463   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
14464 
14465   for(i=0; i<pIdx->nConstraint; i++){
14466     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
14467     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
14468       if( p->iColumn==colSchema ){
14469         if( p->usable==0 ) return SQLITE_CONSTRAINT;
14470         iSchema = i;
14471       }
14472       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
14473         iPgno = i;
14474       }
14475     }
14476   }
14477 
14478   if( iSchema>=0 ){
14479     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
14480     pIdx->aConstraintUsage[iSchema].omit = 1;
14481   }
14482   if( iPgno>=0 ){
14483     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
14484     pIdx->aConstraintUsage[iPgno].omit = 1;
14485     pIdx->estimatedCost = 100;
14486     pIdx->estimatedRows =  50;
14487 
14488     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
14489       int iCol = pIdx->aOrderBy[0].iColumn;
14490       if( pIdx->nOrderBy==1 ){
14491         pIdx->orderByConsumed = (iCol==0 || iCol==1);
14492       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
14493         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
14494       }
14495     }
14496 
14497   }else{
14498     pIdx->estimatedCost = 100000000;
14499     pIdx->estimatedRows = 1000000000;
14500   }
14501   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
14502   return SQLITE_OK;
14503 }
14504 
14505 /*
14506 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
14507 */
dbdataOpen(sqlite3_vtab * pVTab,sqlite3_vtab_cursor ** ppCursor)14508 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
14509   DbdataCursor *pCsr;
14510 
14511   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
14512   if( pCsr==0 ){
14513     return SQLITE_NOMEM;
14514   }else{
14515     memset(pCsr, 0, sizeof(DbdataCursor));
14516     pCsr->base.pVtab = pVTab;
14517   }
14518 
14519   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
14520   return SQLITE_OK;
14521 }
14522 
14523 /*
14524 ** Restore a cursor object to the state it was in when first allocated
14525 ** by dbdataOpen().
14526 */
dbdataResetCursor(DbdataCursor * pCsr)14527 static void dbdataResetCursor(DbdataCursor *pCsr){
14528   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
14529   if( pTab->pStmt==0 ){
14530     pTab->pStmt = pCsr->pStmt;
14531   }else{
14532     sqlite3_finalize(pCsr->pStmt);
14533   }
14534   pCsr->pStmt = 0;
14535   pCsr->iPgno = 1;
14536   pCsr->iCell = 0;
14537   pCsr->iField = 0;
14538   pCsr->bOnePage = 0;
14539   sqlite3_free(pCsr->aPage);
14540   sqlite3_free(pCsr->pRec);
14541   pCsr->pRec = 0;
14542   pCsr->aPage = 0;
14543 }
14544 
14545 /*
14546 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
14547 */
dbdataClose(sqlite3_vtab_cursor * pCursor)14548 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
14549   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14550   dbdataResetCursor(pCsr);
14551   sqlite3_free(pCsr);
14552   return SQLITE_OK;
14553 }
14554 
14555 /*
14556 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
14557 */
get_uint16(unsigned char * a)14558 static u32 get_uint16(unsigned char *a){
14559   return (a[0]<<8)|a[1];
14560 }
get_uint32(unsigned char * a)14561 static u32 get_uint32(unsigned char *a){
14562   return ((u32)a[0]<<24)
14563        | ((u32)a[1]<<16)
14564        | ((u32)a[2]<<8)
14565        | ((u32)a[3]);
14566 }
14567 
14568 /*
14569 ** Load page pgno from the database via the sqlite_dbpage virtual table.
14570 ** If successful, set (*ppPage) to point to a buffer containing the page
14571 ** data, (*pnPage) to the size of that buffer in bytes and return
14572 ** SQLITE_OK. In this case it is the responsibility of the caller to
14573 ** eventually free the buffer using sqlite3_free().
14574 **
14575 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
14576 ** return an SQLite error code.
14577 */
dbdataLoadPage(DbdataCursor * pCsr,u32 pgno,u8 ** ppPage,int * pnPage)14578 static int dbdataLoadPage(
14579   DbdataCursor *pCsr,             /* Cursor object */
14580   u32 pgno,                       /* Page number of page to load */
14581   u8 **ppPage,                    /* OUT: pointer to page buffer */
14582   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
14583 ){
14584   int rc2;
14585   int rc = SQLITE_OK;
14586   sqlite3_stmt *pStmt = pCsr->pStmt;
14587 
14588   *ppPage = 0;
14589   *pnPage = 0;
14590   if( pgno>0 ){
14591     sqlite3_bind_int64(pStmt, 2, pgno);
14592     if( SQLITE_ROW==sqlite3_step(pStmt) ){
14593       int nCopy = sqlite3_column_bytes(pStmt, 0);
14594       if( nCopy>0 ){
14595         u8 *pPage;
14596         pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
14597         if( pPage==0 ){
14598           rc = SQLITE_NOMEM;
14599         }else{
14600           const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
14601           memcpy(pPage, pCopy, nCopy);
14602           memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
14603         }
14604         *ppPage = pPage;
14605         *pnPage = nCopy;
14606       }
14607     }
14608     rc2 = sqlite3_reset(pStmt);
14609     if( rc==SQLITE_OK ) rc = rc2;
14610   }
14611 
14612   return rc;
14613 }
14614 
14615 /*
14616 ** Read a varint.  Put the value in *pVal and return the number of bytes.
14617 */
dbdataGetVarint(const u8 * z,sqlite3_int64 * pVal)14618 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
14619   sqlite3_uint64 u = 0;
14620   int i;
14621   for(i=0; i<8; i++){
14622     u = (u<<7) + (z[i]&0x7f);
14623     if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
14624   }
14625   u = (u<<8) + (z[i]&0xff);
14626   *pVal = (sqlite3_int64)u;
14627   return 9;
14628 }
14629 
14630 /*
14631 ** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
14632 ** or greater than 0xFFFFFFFF. This can be used for all varints in an
14633 ** SQLite database except for key values in intkey tables.
14634 */
dbdataGetVarintU32(const u8 * z,sqlite3_int64 * pVal)14635 static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
14636   sqlite3_int64 val;
14637   int nRet = dbdataGetVarint(z, &val);
14638   if( val<0 || val>0xFFFFFFFF ) val = 0;
14639   *pVal = val;
14640   return nRet;
14641 }
14642 
14643 /*
14644 ** Return the number of bytes of space used by an SQLite value of type
14645 ** eType.
14646 */
dbdataValueBytes(int eType)14647 static int dbdataValueBytes(int eType){
14648   switch( eType ){
14649     case 0: case 8: case 9:
14650     case 10: case 11:
14651       return 0;
14652     case 1:
14653       return 1;
14654     case 2:
14655       return 2;
14656     case 3:
14657       return 3;
14658     case 4:
14659       return 4;
14660     case 5:
14661       return 6;
14662     case 6:
14663     case 7:
14664       return 8;
14665     default:
14666       if( eType>0 ){
14667         return ((eType-12) / 2);
14668       }
14669       return 0;
14670   }
14671 }
14672 
14673 /*
14674 ** Load a value of type eType from buffer pData and use it to set the
14675 ** result of context object pCtx.
14676 */
dbdataValue(sqlite3_context * pCtx,u32 enc,int eType,u8 * pData,sqlite3_int64 nData)14677 static void dbdataValue(
14678   sqlite3_context *pCtx,
14679   u32 enc,
14680   int eType,
14681   u8 *pData,
14682   sqlite3_int64 nData
14683 ){
14684   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
14685     switch( eType ){
14686       case 0:
14687       case 10:
14688       case 11:
14689         sqlite3_result_null(pCtx);
14690         break;
14691 
14692       case 8:
14693         sqlite3_result_int(pCtx, 0);
14694         break;
14695       case 9:
14696         sqlite3_result_int(pCtx, 1);
14697         break;
14698 
14699       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
14700         sqlite3_uint64 v = (signed char)pData[0];
14701         pData++;
14702         switch( eType ){
14703           case 7:
14704           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
14705           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
14706           case 4:  v = (v<<8) + pData[0];  pData++;
14707           case 3:  v = (v<<8) + pData[0];  pData++;
14708           case 2:  v = (v<<8) + pData[0];  pData++;
14709         }
14710 
14711         if( eType==7 ){
14712           double r;
14713           memcpy(&r, &v, sizeof(r));
14714           sqlite3_result_double(pCtx, r);
14715         }else{
14716           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
14717         }
14718         break;
14719       }
14720 
14721       default: {
14722         int n = ((eType-12) / 2);
14723         if( eType % 2 ){
14724           switch( enc ){
14725 #ifndef SQLITE_OMIT_UTF16
14726             case SQLITE_UTF16BE:
14727               sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
14728               break;
14729             case SQLITE_UTF16LE:
14730               sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
14731               break;
14732 #endif
14733             default:
14734               sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
14735               break;
14736           }
14737         }else{
14738           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
14739         }
14740       }
14741     }
14742   }
14743 }
14744 
14745 /*
14746 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
14747 */
dbdataNext(sqlite3_vtab_cursor * pCursor)14748 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
14749   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14750   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
14751 
14752   pCsr->iRowid++;
14753   while( 1 ){
14754     int rc;
14755     int iOff = (pCsr->iPgno==1 ? 100 : 0);
14756     int bNextPage = 0;
14757 
14758     if( pCsr->aPage==0 ){
14759       while( 1 ){
14760         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
14761         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
14762         if( rc!=SQLITE_OK ) return rc;
14763         if( pCsr->aPage && pCsr->nPage>=256 ) break;
14764         sqlite3_free(pCsr->aPage);
14765         pCsr->aPage = 0;
14766         if( pCsr->bOnePage ) return SQLITE_OK;
14767         pCsr->iPgno++;
14768       }
14769 
14770       assert( iOff+3+2<=pCsr->nPage );
14771       pCsr->iCell = pTab->bPtr ? -2 : 0;
14772       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
14773     }
14774 
14775     if( pTab->bPtr ){
14776       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
14777         pCsr->iCell = pCsr->nCell;
14778       }
14779       pCsr->iCell++;
14780       if( pCsr->iCell>=pCsr->nCell ){
14781         sqlite3_free(pCsr->aPage);
14782         pCsr->aPage = 0;
14783         if( pCsr->bOnePage ) return SQLITE_OK;
14784         pCsr->iPgno++;
14785       }else{
14786         return SQLITE_OK;
14787       }
14788     }else{
14789       /* If there is no record loaded, load it now. */
14790       if( pCsr->pRec==0 ){
14791         int bHasRowid = 0;
14792         int nPointer = 0;
14793         sqlite3_int64 nPayload = 0;
14794         sqlite3_int64 nHdr = 0;
14795         int iHdr;
14796         int U, X;
14797         int nLocal;
14798 
14799         switch( pCsr->aPage[iOff] ){
14800           case 0x02:
14801             nPointer = 4;
14802             break;
14803           case 0x0a:
14804             break;
14805           case 0x0d:
14806             bHasRowid = 1;
14807             break;
14808           default:
14809             /* This is not a b-tree page with records on it. Continue. */
14810             pCsr->iCell = pCsr->nCell;
14811             break;
14812         }
14813 
14814         if( pCsr->iCell>=pCsr->nCell ){
14815           bNextPage = 1;
14816         }else{
14817 
14818           iOff += 8 + nPointer + pCsr->iCell*2;
14819           if( iOff>pCsr->nPage ){
14820             bNextPage = 1;
14821           }else{
14822             iOff = get_uint16(&pCsr->aPage[iOff]);
14823           }
14824 
14825           /* For an interior node cell, skip past the child-page number */
14826           iOff += nPointer;
14827 
14828           /* Load the "byte of payload including overflow" field */
14829           if( bNextPage || iOff>pCsr->nPage ){
14830             bNextPage = 1;
14831           }else{
14832             iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
14833             if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
14834           }
14835 
14836           /* If this is a leaf intkey cell, load the rowid */
14837           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
14838             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
14839           }
14840 
14841           /* Figure out how much data to read from the local page */
14842           U = pCsr->nPage;
14843           if( bHasRowid ){
14844             X = U-35;
14845           }else{
14846             X = ((U-12)*64/255)-23;
14847           }
14848           if( nPayload<=X ){
14849             nLocal = nPayload;
14850           }else{
14851             int M, K;
14852             M = ((U-12)*32/255)-23;
14853             K = M+((nPayload-M)%(U-4));
14854             if( K<=X ){
14855               nLocal = K;
14856             }else{
14857               nLocal = M;
14858             }
14859           }
14860 
14861           if( bNextPage || nLocal+iOff>pCsr->nPage ){
14862             bNextPage = 1;
14863           }else{
14864 
14865             /* Allocate space for payload. And a bit more to catch small buffer
14866             ** overruns caused by attempting to read a varint or similar from
14867             ** near the end of a corrupt record.  */
14868             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
14869             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
14870             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
14871             pCsr->nRec = nPayload;
14872 
14873             /* Load the nLocal bytes of payload */
14874             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
14875             iOff += nLocal;
14876 
14877             /* Load content from overflow pages */
14878             if( nPayload>nLocal ){
14879               sqlite3_int64 nRem = nPayload - nLocal;
14880               u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
14881               while( nRem>0 ){
14882                 u8 *aOvfl = 0;
14883                 int nOvfl = 0;
14884                 int nCopy;
14885                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
14886                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
14887                 if( rc!=SQLITE_OK ) return rc;
14888                 if( aOvfl==0 ) break;
14889 
14890                 nCopy = U-4;
14891                 if( nCopy>nRem ) nCopy = nRem;
14892                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
14893                 nRem -= nCopy;
14894 
14895                 pgnoOvfl = get_uint32(aOvfl);
14896                 sqlite3_free(aOvfl);
14897               }
14898             }
14899 
14900             iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
14901             if( nHdr>nPayload ) nHdr = 0;
14902             pCsr->nHdr = nHdr;
14903             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
14904             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
14905             pCsr->iField = (bHasRowid ? -1 : 0);
14906           }
14907         }
14908       }else{
14909         pCsr->iField++;
14910         if( pCsr->iField>0 ){
14911           sqlite3_int64 iType;
14912           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
14913             bNextPage = 1;
14914           }else{
14915             int szField = 0;
14916             pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
14917             szField = dbdataValueBytes(iType);
14918             if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){
14919               pCsr->pPtr = &pCsr->pRec[pCsr->nRec];
14920             }else{
14921               pCsr->pPtr += szField;
14922             }
14923           }
14924         }
14925       }
14926 
14927       if( bNextPage ){
14928         sqlite3_free(pCsr->aPage);
14929         sqlite3_free(pCsr->pRec);
14930         pCsr->aPage = 0;
14931         pCsr->pRec = 0;
14932         if( pCsr->bOnePage ) return SQLITE_OK;
14933         pCsr->iPgno++;
14934       }else{
14935         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
14936           return SQLITE_OK;
14937         }
14938 
14939         /* Advance to the next cell. The next iteration of the loop will load
14940         ** the record and so on. */
14941         sqlite3_free(pCsr->pRec);
14942         pCsr->pRec = 0;
14943         pCsr->iCell++;
14944       }
14945     }
14946   }
14947 
14948   assert( !"can't get here" );
14949   return SQLITE_OK;
14950 }
14951 
14952 /*
14953 ** Return true if the cursor is at EOF.
14954 */
dbdataEof(sqlite3_vtab_cursor * pCursor)14955 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
14956   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
14957   return pCsr->aPage==0;
14958 }
14959 
14960 /*
14961 ** Return true if nul-terminated string zSchema ends in "()". Or false
14962 ** otherwise.
14963 */
dbdataIsFunction(const char * zSchema)14964 static int dbdataIsFunction(const char *zSchema){
14965   size_t n = strlen(zSchema);
14966   if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
14967     return (int)n-2;
14968   }
14969   return 0;
14970 }
14971 
14972 /*
14973 ** Determine the size in pages of database zSchema (where zSchema is
14974 ** "main", "temp" or the name of an attached database) and set
14975 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
14976 ** an SQLite error code.
14977 */
dbdataDbsize(DbdataCursor * pCsr,const char * zSchema)14978 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
14979   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
14980   char *zSql = 0;
14981   int rc, rc2;
14982   int nFunc = 0;
14983   sqlite3_stmt *pStmt = 0;
14984 
14985   if( (nFunc = dbdataIsFunction(zSchema))>0 ){
14986     zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
14987   }else{
14988     zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
14989   }
14990   if( zSql==0 ) return SQLITE_NOMEM;
14991 
14992   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
14993   sqlite3_free(zSql);
14994   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14995     pCsr->szDb = sqlite3_column_int(pStmt, 0);
14996   }
14997   rc2 = sqlite3_finalize(pStmt);
14998   if( rc==SQLITE_OK ) rc = rc2;
14999   return rc;
15000 }
15001 
15002 /*
15003 ** Attempt to figure out the encoding of the database by retrieving page 1
15004 ** and inspecting the header field. If successful, set the pCsr->enc variable
15005 ** and return SQLITE_OK. Otherwise, return an SQLite error code.
15006 */
dbdataGetEncoding(DbdataCursor * pCsr)15007 static int dbdataGetEncoding(DbdataCursor *pCsr){
15008   int rc = SQLITE_OK;
15009   int nPg1 = 0;
15010   u8 *aPg1 = 0;
15011   rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
15012   if( rc==SQLITE_OK && nPg1>=(56+4) ){
15013     pCsr->enc = get_uint32(&aPg1[56]);
15014   }
15015   sqlite3_free(aPg1);
15016   return rc;
15017 }
15018 
15019 
15020 /*
15021 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
15022 */
dbdataFilter(sqlite3_vtab_cursor * pCursor,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)15023 static int dbdataFilter(
15024   sqlite3_vtab_cursor *pCursor,
15025   int idxNum, const char *idxStr,
15026   int argc, sqlite3_value **argv
15027 ){
15028   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15029   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
15030   int rc = SQLITE_OK;
15031   const char *zSchema = "main";
15032   (void)idxStr;
15033   (void)argc;
15034 
15035   dbdataResetCursor(pCsr);
15036   assert( pCsr->iPgno==1 );
15037   if( idxNum & 0x01 ){
15038     zSchema = (const char*)sqlite3_value_text(argv[0]);
15039     if( zSchema==0 ) zSchema = "";
15040   }
15041   if( idxNum & 0x02 ){
15042     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
15043     pCsr->bOnePage = 1;
15044   }else{
15045     rc = dbdataDbsize(pCsr, zSchema);
15046   }
15047 
15048   if( rc==SQLITE_OK ){
15049     int nFunc = 0;
15050     if( pTab->pStmt ){
15051       pCsr->pStmt = pTab->pStmt;
15052       pTab->pStmt = 0;
15053     }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
15054       char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
15055       if( zSql==0 ){
15056         rc = SQLITE_NOMEM;
15057       }else{
15058         rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
15059         sqlite3_free(zSql);
15060       }
15061     }else{
15062       rc = sqlite3_prepare_v2(pTab->db,
15063           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
15064           &pCsr->pStmt, 0
15065       );
15066     }
15067   }
15068   if( rc==SQLITE_OK ){
15069     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
15070   }
15071 
15072   /* Try to determine the encoding of the db by inspecting the header
15073   ** field on page 1. */
15074   if( rc==SQLITE_OK ){
15075     rc = dbdataGetEncoding(pCsr);
15076   }
15077 
15078   if( rc!=SQLITE_OK ){
15079     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
15080   }
15081 
15082   if( rc==SQLITE_OK ){
15083     rc = dbdataNext(pCursor);
15084   }
15085   return rc;
15086 }
15087 
15088 /*
15089 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
15090 */
dbdataColumn(sqlite3_vtab_cursor * pCursor,sqlite3_context * ctx,int i)15091 static int dbdataColumn(
15092   sqlite3_vtab_cursor *pCursor,
15093   sqlite3_context *ctx,
15094   int i
15095 ){
15096   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15097   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
15098   if( pTab->bPtr ){
15099     switch( i ){
15100       case DBPTR_COLUMN_PGNO:
15101         sqlite3_result_int64(ctx, pCsr->iPgno);
15102         break;
15103       case DBPTR_COLUMN_CHILD: {
15104         int iOff = pCsr->iPgno==1 ? 100 : 0;
15105         if( pCsr->iCell<0 ){
15106           iOff += 8;
15107         }else{
15108           iOff += 12 + pCsr->iCell*2;
15109           if( iOff>pCsr->nPage ) return SQLITE_OK;
15110           iOff = get_uint16(&pCsr->aPage[iOff]);
15111         }
15112         if( iOff<=pCsr->nPage ){
15113           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
15114         }
15115         break;
15116       }
15117     }
15118   }else{
15119     switch( i ){
15120       case DBDATA_COLUMN_PGNO:
15121         sqlite3_result_int64(ctx, pCsr->iPgno);
15122         break;
15123       case DBDATA_COLUMN_CELL:
15124         sqlite3_result_int(ctx, pCsr->iCell);
15125         break;
15126       case DBDATA_COLUMN_FIELD:
15127         sqlite3_result_int(ctx, pCsr->iField);
15128         break;
15129       case DBDATA_COLUMN_VALUE: {
15130         if( pCsr->iField<0 ){
15131           sqlite3_result_int64(ctx, pCsr->iIntkey);
15132         }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){
15133           sqlite3_int64 iType;
15134           dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
15135           dbdataValue(
15136               ctx, pCsr->enc, iType, pCsr->pPtr,
15137               &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
15138           );
15139         }
15140         break;
15141       }
15142     }
15143   }
15144   return SQLITE_OK;
15145 }
15146 
15147 /*
15148 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
15149 */
dbdataRowid(sqlite3_vtab_cursor * pCursor,sqlite_int64 * pRowid)15150 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
15151   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
15152   *pRowid = pCsr->iRowid;
15153   return SQLITE_OK;
15154 }
15155 
15156 
15157 /*
15158 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
15159 */
sqlite3DbdataRegister(sqlite3 * db)15160 static int sqlite3DbdataRegister(sqlite3 *db){
15161   static sqlite3_module dbdata_module = {
15162     0,                            /* iVersion */
15163     0,                            /* xCreate */
15164     dbdataConnect,                /* xConnect */
15165     dbdataBestIndex,              /* xBestIndex */
15166     dbdataDisconnect,             /* xDisconnect */
15167     0,                            /* xDestroy */
15168     dbdataOpen,                   /* xOpen - open a cursor */
15169     dbdataClose,                  /* xClose - close a cursor */
15170     dbdataFilter,                 /* xFilter - configure scan constraints */
15171     dbdataNext,                   /* xNext - advance a cursor */
15172     dbdataEof,                    /* xEof - check for end of scan */
15173     dbdataColumn,                 /* xColumn - read data */
15174     dbdataRowid,                  /* xRowid - read data */
15175     0,                            /* xUpdate */
15176     0,                            /* xBegin */
15177     0,                            /* xSync */
15178     0,                            /* xCommit */
15179     0,                            /* xRollback */
15180     0,                            /* xFindMethod */
15181     0,                            /* xRename */
15182     0,                            /* xSavepoint */
15183     0,                            /* xRelease */
15184     0,                            /* xRollbackTo */
15185     0,                            /* xShadowName */
15186     0                             /* xIntegrity */
15187   };
15188 
15189   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
15190   if( rc==SQLITE_OK ){
15191     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
15192   }
15193   return rc;
15194 }
15195 
sqlite3_dbdata_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)15196 int sqlite3_dbdata_init(
15197   sqlite3 *db,
15198   char **pzErrMsg,
15199   const sqlite3_api_routines *pApi
15200 ){
15201   (void)pzErrMsg;
15202   return sqlite3DbdataRegister(db);
15203 }
15204 
15205 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
15206 
15207 /************************* End ../ext/recover/dbdata.c ********************/
15208 /************************* Begin ../ext/recover/sqlite3recover.c ******************/
15209 /*
15210 ** 2022-08-27
15211 **
15212 ** The author disclaims copyright to this source code.  In place of
15213 ** a legal notice, here is a blessing:
15214 **
15215 **    May you do good and not evil.
15216 **    May you find forgiveness for yourself and forgive others.
15217 **    May you share freely, never taking more than you give.
15218 **
15219 *************************************************************************
15220 **
15221 */
15222 
15223 
15224 /* #include "sqlite3recover.h" */
15225 #include <assert.h>
15226 #include <string.h>
15227 
15228 #ifndef SQLITE_OMIT_VIRTUALTABLE
15229 
15230 /*
15231 ** Declaration for public API function in file dbdata.c. This may be called
15232 ** with NULL as the final two arguments to register the sqlite_dbptr and
15233 ** sqlite_dbdata virtual tables with a database handle.
15234 */
15235 #ifdef _WIN32
15236 
15237 #endif
15238 int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
15239 
15240 /* typedef unsigned int u32; */
15241 /* typedef unsigned char u8; */
15242 /* typedef sqlite3_int64 i64; */
15243 
15244 typedef struct RecoverTable RecoverTable;
15245 typedef struct RecoverColumn RecoverColumn;
15246 
15247 /*
15248 ** When recovering rows of data that can be associated with table
15249 ** definitions recovered from the sqlite_schema table, each table is
15250 ** represented by an instance of the following object.
15251 **
15252 ** iRoot:
15253 **   The root page in the original database. Not necessarily (and usually
15254 **   not) the same in the recovered database.
15255 **
15256 ** zTab:
15257 **   Name of the table.
15258 **
15259 ** nCol/aCol[]:
15260 **   aCol[] is an array of nCol columns. In the order in which they appear
15261 **   in the table.
15262 **
15263 ** bIntkey:
15264 **   Set to true for intkey tables, false for WITHOUT ROWID.
15265 **
15266 ** iRowidBind:
15267 **   Each column in the aCol[] array has associated with it the index of
15268 **   the bind parameter its values will be bound to in the INSERT statement
15269 **   used to construct the output database. If the table does has a rowid
15270 **   but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
15271 **   index of the bind paramater to which the rowid value should be bound.
15272 **   Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
15273 **   KEY column, then the rowid value should be bound to the index associated
15274 **   with the column.
15275 **
15276 ** pNext:
15277 **   All RecoverTable objects used by the recovery operation are allocated
15278 **   and populated as part of creating the recovered database schema in
15279 **   the output database, before any non-schema data are recovered. They
15280 **   are then stored in a singly-linked list linked by this variable beginning
15281 **   at sqlite3_recover.pTblList.
15282 */
15283 struct RecoverTable {
15284   u32 iRoot;                      /* Root page in original database */
15285   char *zTab;                     /* Name of table */
15286   int nCol;                       /* Number of columns in table */
15287   RecoverColumn *aCol;            /* Array of columns */
15288   int bIntkey;                    /* True for intkey, false for without rowid */
15289   int iRowidBind;                 /* If >0, bind rowid to INSERT here */
15290   RecoverTable *pNext;
15291 };
15292 
15293 /*
15294 ** Each database column is represented by an instance of the following object
15295 ** stored in the RecoverTable.aCol[] array of the associated table.
15296 **
15297 ** iField:
15298 **   The index of the associated field within database records. Or -1 if
15299 **   there is no associated field (e.g. for virtual generated columns).
15300 **
15301 ** iBind:
15302 **   The bind index of the INSERT statement to bind this columns values
15303 **   to. Or 0 if there is no such index (iff (iField<0)).
15304 **
15305 ** bIPK:
15306 **   True if this is the INTEGER PRIMARY KEY column.
15307 **
15308 ** zCol:
15309 **   Name of column.
15310 **
15311 ** eHidden:
15312 **   A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
15313 */
15314 struct RecoverColumn {
15315   int iField;                     /* Field in record on disk */
15316   int iBind;                      /* Binding to use in INSERT */
15317   int bIPK;                       /* True for IPK column */
15318   char *zCol;
15319   int eHidden;
15320 };
15321 
15322 #define RECOVER_EHIDDEN_NONE    0      /* Normal database column */
15323 #define RECOVER_EHIDDEN_HIDDEN  1      /* Column is __HIDDEN__ */
15324 #define RECOVER_EHIDDEN_VIRTUAL 2      /* Virtual generated column */
15325 #define RECOVER_EHIDDEN_STORED  3      /* Stored generated column */
15326 
15327 /*
15328 ** Bitmap object used to track pages in the input database. Allocated
15329 ** and manipulated only by the following functions:
15330 **
15331 **     recoverBitmapAlloc()
15332 **     recoverBitmapFree()
15333 **     recoverBitmapSet()
15334 **     recoverBitmapQuery()
15335 **
15336 ** nPg:
15337 **   Largest page number that may be stored in the bitmap. The range
15338 **   of valid keys is 1 to nPg, inclusive.
15339 **
15340 ** aElem[]:
15341 **   Array large enough to contain a bit for each key. For key value
15342 **   iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
15343 **   In other words, the following is true if bit iKey is set, or
15344 **   false if it is clear:
15345 **
15346 **       (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
15347 */
15348 typedef struct RecoverBitmap RecoverBitmap;
15349 struct RecoverBitmap {
15350   i64 nPg;                        /* Size of bitmap */
15351   u32 aElem[1];                   /* Array of 32-bit bitmasks */
15352 };
15353 
15354 /*
15355 ** State variables (part of the sqlite3_recover structure) used while
15356 ** recovering data for tables identified in the recovered schema (state
15357 ** RECOVER_STATE_WRITING).
15358 */
15359 typedef struct RecoverStateW1 RecoverStateW1;
15360 struct RecoverStateW1 {
15361   sqlite3_stmt *pTbls;
15362   sqlite3_stmt *pSel;
15363   sqlite3_stmt *pInsert;
15364   int nInsert;
15365 
15366   RecoverTable *pTab;             /* Table currently being written */
15367   int nMax;                       /* Max column count in any schema table */
15368   sqlite3_value **apVal;          /* Array of nMax values */
15369   int nVal;                       /* Number of valid entries in apVal[] */
15370   int bHaveRowid;
15371   i64 iRowid;
15372   i64 iPrevPage;
15373   int iPrevCell;
15374 };
15375 
15376 /*
15377 ** State variables (part of the sqlite3_recover structure) used while
15378 ** recovering data destined for the lost and found table (states
15379 ** RECOVER_STATE_LOSTANDFOUND[123]).
15380 */
15381 typedef struct RecoverStateLAF RecoverStateLAF;
15382 struct RecoverStateLAF {
15383   RecoverBitmap *pUsed;
15384   i64 nPg;                        /* Size of db in pages */
15385   sqlite3_stmt *pAllAndParent;
15386   sqlite3_stmt *pMapInsert;
15387   sqlite3_stmt *pMaxField;
15388   sqlite3_stmt *pUsedPages;
15389   sqlite3_stmt *pFindRoot;
15390   sqlite3_stmt *pInsert;          /* INSERT INTO lost_and_found ... */
15391   sqlite3_stmt *pAllPage;
15392   sqlite3_stmt *pPageData;
15393   sqlite3_value **apVal;
15394   int nMaxField;
15395 };
15396 
15397 /*
15398 ** Main recover handle structure.
15399 */
15400 struct sqlite3_recover {
15401   /* Copies of sqlite3_recover_init[_sql]() parameters */
15402   sqlite3 *dbIn;                  /* Input database */
15403   char *zDb;                      /* Name of input db ("main" etc.) */
15404   char *zUri;                     /* URI for output database */
15405   void *pSqlCtx;                  /* SQL callback context */
15406   int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
15407 
15408   /* Values configured by sqlite3_recover_config() */
15409   char *zStateDb;                 /* State database to use (or NULL) */
15410   char *zLostAndFound;            /* Name of lost-and-found table (or NULL) */
15411   int bFreelistCorrupt;           /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
15412   int bRecoverRowid;              /* SQLITE_RECOVER_ROWIDS setting */
15413   int bSlowIndexes;               /* SQLITE_RECOVER_SLOWINDEXES setting */
15414 
15415   int pgsz;
15416   int detected_pgsz;
15417   int nReserve;
15418   u8 *pPage1Disk;
15419   u8 *pPage1Cache;
15420 
15421   /* Error code and error message */
15422   int errCode;                    /* For sqlite3_recover_errcode() */
15423   char *zErrMsg;                  /* For sqlite3_recover_errmsg() */
15424 
15425   int eState;
15426   int bCloseTransaction;
15427 
15428   /* Variables used with eState==RECOVER_STATE_WRITING */
15429   RecoverStateW1 w1;
15430 
15431   /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
15432   RecoverStateLAF laf;
15433 
15434   /* Fields used within sqlite3_recover_run() */
15435   sqlite3 *dbOut;                 /* Output database */
15436   sqlite3_stmt *pGetPage;         /* SELECT against input db sqlite_dbdata */
15437   RecoverTable *pTblList;         /* List of tables recovered from schema */
15438 };
15439 
15440 /*
15441 ** The various states in which an sqlite3_recover object may exist:
15442 **
15443 **   RECOVER_STATE_INIT:
15444 **    The object is initially created in this state. sqlite3_recover_step()
15445 **    has yet to be called. This is the only state in which it is permitted
15446 **    to call sqlite3_recover_config().
15447 **
15448 **   RECOVER_STATE_WRITING:
15449 **
15450 **   RECOVER_STATE_LOSTANDFOUND1:
15451 **    State to populate the bitmap of pages used by other tables or the
15452 **    database freelist.
15453 **
15454 **   RECOVER_STATE_LOSTANDFOUND2:
15455 **    Populate the recovery.map table - used to figure out a "root" page
15456 **    for each lost page from in the database from which records are
15457 **    extracted.
15458 **
15459 **   RECOVER_STATE_LOSTANDFOUND3:
15460 **    Populate the lost-and-found table itself.
15461 */
15462 #define RECOVER_STATE_INIT           0
15463 #define RECOVER_STATE_WRITING        1
15464 #define RECOVER_STATE_LOSTANDFOUND1  2
15465 #define RECOVER_STATE_LOSTANDFOUND2  3
15466 #define RECOVER_STATE_LOSTANDFOUND3  4
15467 #define RECOVER_STATE_SCHEMA2        5
15468 #define RECOVER_STATE_DONE           6
15469 
15470 
15471 /*
15472 ** Global variables used by this extension.
15473 */
15474 typedef struct RecoverGlobal RecoverGlobal;
15475 struct RecoverGlobal {
15476   const sqlite3_io_methods *pMethods;
15477   sqlite3_recover *p;
15478 };
15479 static RecoverGlobal recover_g;
15480 
15481 /*
15482 ** Use this static SQLite mutex to protect the globals during the
15483 ** first call to sqlite3_recover_step().
15484 */
15485 #define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
15486 
15487 
15488 /*
15489 ** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
15490 */
15491 #define RECOVER_ROWID_DEFAULT 1
15492 
15493 /*
15494 ** Mutex handling:
15495 **
15496 **    recoverEnterMutex()       -   Enter the recovery mutex
15497 **    recoverLeaveMutex()       -   Leave the recovery mutex
15498 **    recoverAssertMutexHeld()  -   Assert that the recovery mutex is held
15499 */
15500 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
15501 # define recoverEnterMutex()
15502 # define recoverLeaveMutex()
15503 #else
recoverEnterMutex(void)15504 static void recoverEnterMutex(void){
15505   sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
15506 }
recoverLeaveMutex(void)15507 static void recoverLeaveMutex(void){
15508   sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
15509 }
15510 #endif
15511 #if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
recoverAssertMutexHeld(void)15512 static void recoverAssertMutexHeld(void){
15513   assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
15514 }
15515 #else
15516 # define recoverAssertMutexHeld()
15517 #endif
15518 
15519 
15520 /*
15521 ** Like strlen(). But handles NULL pointer arguments.
15522 */
recoverStrlen(const char * zStr)15523 static int recoverStrlen(const char *zStr){
15524   if( zStr==0 ) return 0;
15525   return (int)(strlen(zStr)&0x7fffffff);
15526 }
15527 
15528 /*
15529 ** This function is a no-op if the recover handle passed as the first
15530 ** argument already contains an error (if p->errCode!=SQLITE_OK).
15531 **
15532 ** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
15533 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
15534 ** if an OOM error occurs, NULL is returned and the handle error code
15535 ** (p->errCode) set to SQLITE_NOMEM.
15536 */
recoverMalloc(sqlite3_recover * p,i64 nByte)15537 static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
15538   void *pRet = 0;
15539   assert( nByte>0 );
15540   if( p->errCode==SQLITE_OK ){
15541     pRet = sqlite3_malloc64(nByte);
15542     if( pRet ){
15543       memset(pRet, 0, nByte);
15544     }else{
15545       p->errCode = SQLITE_NOMEM;
15546     }
15547   }
15548   return pRet;
15549 }
15550 
15551 /*
15552 ** Set the error code and error message for the recover handle passed as
15553 ** the first argument. The error code is set to the value of parameter
15554 ** errCode.
15555 **
15556 ** Parameter zFmt must be a printf() style formatting string. The handle
15557 ** error message is set to the result of using any trailing arguments for
15558 ** parameter substitutions in the formatting string.
15559 **
15560 ** For example:
15561 **
15562 **   recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
15563 */
recoverError(sqlite3_recover * p,int errCode,const char * zFmt,...)15564 static int recoverError(
15565   sqlite3_recover *p,
15566   int errCode,
15567   const char *zFmt, ...
15568 ){
15569   char *z = 0;
15570   va_list ap;
15571   va_start(ap, zFmt);
15572   if( zFmt ){
15573     z = sqlite3_vmprintf(zFmt, ap);
15574     va_end(ap);
15575   }
15576   sqlite3_free(p->zErrMsg);
15577   p->zErrMsg = z;
15578   p->errCode = errCode;
15579   return errCode;
15580 }
15581 
15582 
15583 /*
15584 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
15585 ** In this case it returns NULL.
15586 **
15587 ** Otherwise, an attempt is made to allocate and return a bitmap object
15588 ** large enough to store a bit for all page numbers between 1 and nPg,
15589 ** inclusive. The bitmap is initially zeroed.
15590 */
recoverBitmapAlloc(sqlite3_recover * p,i64 nPg)15591 static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
15592   int nElem = (nPg+1+31) / 32;
15593   int nByte = sizeof(RecoverBitmap) + nElem*sizeof(u32);
15594   RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
15595 
15596   if( pRet ){
15597     pRet->nPg = nPg;
15598   }
15599   return pRet;
15600 }
15601 
15602 /*
15603 ** Free a bitmap object allocated by recoverBitmapAlloc().
15604 */
recoverBitmapFree(RecoverBitmap * pMap)15605 static void recoverBitmapFree(RecoverBitmap *pMap){
15606   sqlite3_free(pMap);
15607 }
15608 
15609 /*
15610 ** Set the bit associated with page iPg in bitvec pMap.
15611 */
recoverBitmapSet(RecoverBitmap * pMap,i64 iPg)15612 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
15613   if( iPg<=pMap->nPg ){
15614     int iElem = (iPg / 32);
15615     int iBit = (iPg % 32);
15616     pMap->aElem[iElem] |= (((u32)1) << iBit);
15617   }
15618 }
15619 
15620 /*
15621 ** Query bitmap object pMap for the state of the bit associated with page
15622 ** iPg. Return 1 if it is set, or 0 otherwise.
15623 */
recoverBitmapQuery(RecoverBitmap * pMap,i64 iPg)15624 static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
15625   int ret = 1;
15626   if( iPg<=pMap->nPg && iPg>0 ){
15627     int iElem = (iPg / 32);
15628     int iBit = (iPg % 32);
15629     ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
15630   }
15631   return ret;
15632 }
15633 
15634 /*
15635 ** Set the recover handle error to the error code and message returned by
15636 ** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
15637 ** handle db.
15638 */
recoverDbError(sqlite3_recover * p,sqlite3 * db)15639 static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
15640   return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
15641 }
15642 
15643 /*
15644 ** This function is a no-op if recover handle p already contains an error
15645 ** (if p->errCode!=SQLITE_OK).
15646 **
15647 ** Otherwise, it attempts to prepare the SQL statement in zSql against
15648 ** database handle db. If successful, the statement handle is returned.
15649 ** Or, if an error occurs, NULL is returned and an error left in the
15650 ** recover handle.
15651 */
recoverPrepare(sqlite3_recover * p,sqlite3 * db,const char * zSql)15652 static sqlite3_stmt *recoverPrepare(
15653   sqlite3_recover *p,
15654   sqlite3 *db,
15655   const char *zSql
15656 ){
15657   sqlite3_stmt *pStmt = 0;
15658   if( p->errCode==SQLITE_OK ){
15659     if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
15660       recoverDbError(p, db);
15661     }
15662   }
15663   return pStmt;
15664 }
15665 
15666 /*
15667 ** This function is a no-op if recover handle p already contains an error
15668 ** (if p->errCode!=SQLITE_OK).
15669 **
15670 ** Otherwise, argument zFmt is used as a printf() style format string,
15671 ** along with any trailing arguments, to create an SQL statement. This
15672 ** SQL statement is prepared against database handle db and, if successful,
15673 ** the statment handle returned. Or, if an error occurs - either during
15674 ** the printf() formatting or when preparing the resulting SQL - an
15675 ** error code and message are left in the recover handle.
15676 */
recoverPreparePrintf(sqlite3_recover * p,sqlite3 * db,const char * zFmt,...)15677 static sqlite3_stmt *recoverPreparePrintf(
15678   sqlite3_recover *p,
15679   sqlite3 *db,
15680   const char *zFmt, ...
15681 ){
15682   sqlite3_stmt *pStmt = 0;
15683   if( p->errCode==SQLITE_OK ){
15684     va_list ap;
15685     char *z;
15686     va_start(ap, zFmt);
15687     z = sqlite3_vmprintf(zFmt, ap);
15688     va_end(ap);
15689     if( z==0 ){
15690       p->errCode = SQLITE_NOMEM;
15691     }else{
15692       pStmt = recoverPrepare(p, db, z);
15693       sqlite3_free(z);
15694     }
15695   }
15696   return pStmt;
15697 }
15698 
15699 /*
15700 ** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
15701 ** indicates that an error occurred, and there is not already an error
15702 ** in the recover handle passed as the first argument, set the error
15703 ** code and error message appropriately.
15704 **
15705 ** This function returns a copy of the statement handle pointer passed
15706 ** as the second argument.
15707 */
recoverReset(sqlite3_recover * p,sqlite3_stmt * pStmt)15708 static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
15709   int rc = sqlite3_reset(pStmt);
15710   if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
15711     recoverDbError(p, sqlite3_db_handle(pStmt));
15712   }
15713   return pStmt;
15714 }
15715 
15716 /*
15717 ** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
15718 ** indicates that an error occurred, and there is not already an error
15719 ** in the recover handle passed as the first argument, set the error
15720 ** code and error message appropriately.
15721 */
recoverFinalize(sqlite3_recover * p,sqlite3_stmt * pStmt)15722 static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
15723   sqlite3 *db = sqlite3_db_handle(pStmt);
15724   int rc = sqlite3_finalize(pStmt);
15725   if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
15726     recoverDbError(p, db);
15727   }
15728 }
15729 
15730 /*
15731 ** This function is a no-op if recover handle p already contains an error
15732 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
15733 ** case.
15734 **
15735 ** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK.
15736 ** Or, if an error occurs, leave an error code and message in the recover
15737 ** handle and return a copy of the error code.
15738 */
recoverExec(sqlite3_recover * p,sqlite3 * db,const char * zSql)15739 static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
15740   if( p->errCode==SQLITE_OK ){
15741     int rc = sqlite3_exec(db, zSql, 0, 0, 0);
15742     if( rc ){
15743       recoverDbError(p, db);
15744     }
15745   }
15746   return p->errCode;
15747 }
15748 
15749 /*
15750 ** Bind the value pVal to parameter iBind of statement pStmt. Leave an
15751 ** error in the recover handle passed as the first argument if an error
15752 ** (e.g. an OOM) occurs.
15753 */
recoverBindValue(sqlite3_recover * p,sqlite3_stmt * pStmt,int iBind,sqlite3_value * pVal)15754 static void recoverBindValue(
15755   sqlite3_recover *p,
15756   sqlite3_stmt *pStmt,
15757   int iBind,
15758   sqlite3_value *pVal
15759 ){
15760   if( p->errCode==SQLITE_OK ){
15761     int rc = sqlite3_bind_value(pStmt, iBind, pVal);
15762     if( rc ) recoverError(p, rc, 0);
15763   }
15764 }
15765 
15766 /*
15767 ** This function is a no-op if recover handle p already contains an error
15768 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
15769 **
15770 ** Otherwise, an attempt is made to interpret zFmt as a printf() style
15771 ** formatting string and the result of using the trailing arguments for
15772 ** parameter substitution with it written into a buffer obtained from
15773 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
15774 ** It is the responsibility of the caller to eventually free the buffer
15775 ** using sqlite3_free().
15776 **
15777 ** Or, if an error occurs, an error code and message is left in the recover
15778 ** handle and NULL returned.
15779 */
recoverMPrintf(sqlite3_recover * p,const char * zFmt,...)15780 static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
15781   va_list ap;
15782   char *z;
15783   va_start(ap, zFmt);
15784   z = sqlite3_vmprintf(zFmt, ap);
15785   va_end(ap);
15786   if( p->errCode==SQLITE_OK ){
15787     if( z==0 ) p->errCode = SQLITE_NOMEM;
15788   }else{
15789     sqlite3_free(z);
15790     z = 0;
15791   }
15792   return z;
15793 }
15794 
15795 /*
15796 ** This function is a no-op if recover handle p already contains an error
15797 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
15798 **
15799 ** Otherwise, execute "PRAGMA page_count" against the input database. If
15800 ** successful, return the integer result. Or, if an error occurs, leave an
15801 ** error code and error message in the sqlite3_recover handle and return
15802 ** zero.
15803 */
recoverPageCount(sqlite3_recover * p)15804 static i64 recoverPageCount(sqlite3_recover *p){
15805   i64 nPg = 0;
15806   if( p->errCode==SQLITE_OK ){
15807     sqlite3_stmt *pStmt = 0;
15808     pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
15809     if( pStmt ){
15810       sqlite3_step(pStmt);
15811       nPg = sqlite3_column_int64(pStmt, 0);
15812     }
15813     recoverFinalize(p, pStmt);
15814   }
15815   return nPg;
15816 }
15817 
15818 /*
15819 ** Implementation of SQL scalar function "read_i32". The first argument to
15820 ** this function must be a blob. The second a non-negative integer. This
15821 ** function reads and returns a 32-bit big-endian integer from byte
15822 ** offset (4*<arg2>) of the blob.
15823 **
15824 **     SELECT read_i32(<blob>, <idx>)
15825 */
recoverReadI32(sqlite3_context * context,int argc,sqlite3_value ** argv)15826 static void recoverReadI32(
15827   sqlite3_context *context,
15828   int argc,
15829   sqlite3_value **argv
15830 ){
15831   const unsigned char *pBlob;
15832   int nBlob;
15833   int iInt;
15834 
15835   assert( argc==2 );
15836   nBlob = sqlite3_value_bytes(argv[0]);
15837   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15838   iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
15839 
15840   if( (iInt+1)*4<=nBlob ){
15841     const unsigned char *a = &pBlob[iInt*4];
15842     i64 iVal = ((i64)a[0]<<24)
15843              + ((i64)a[1]<<16)
15844              + ((i64)a[2]<< 8)
15845              + ((i64)a[3]<< 0);
15846     sqlite3_result_int64(context, iVal);
15847   }
15848 }
15849 
15850 /*
15851 ** Implementation of SQL scalar function "page_is_used". This function
15852 ** is used as part of the procedure for locating orphan rows for the
15853 ** lost-and-found table, and it depends on those routines having populated
15854 ** the sqlite3_recover.laf.pUsed variable.
15855 **
15856 ** The only argument to this function is a page-number. It returns true
15857 ** if the page has already been used somehow during data recovery, or false
15858 ** otherwise.
15859 **
15860 **     SELECT page_is_used(<pgno>);
15861 */
recoverPageIsUsed(sqlite3_context * pCtx,int nArg,sqlite3_value ** apArg)15862 static void recoverPageIsUsed(
15863   sqlite3_context *pCtx,
15864   int nArg,
15865   sqlite3_value **apArg
15866 ){
15867   sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
15868   i64 pgno = sqlite3_value_int64(apArg[0]);
15869   assert( nArg==1 );
15870   sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
15871 }
15872 
15873 /*
15874 ** The implementation of a user-defined SQL function invoked by the
15875 ** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
15876 ** of the database being recovered.
15877 **
15878 ** This function always takes a single integer argument. If the argument
15879 ** is zero, then the value returned is the number of pages in the db being
15880 ** recovered. If the argument is greater than zero, it is a page number.
15881 ** The value returned in this case is an SQL blob containing the data for
15882 ** the identified page of the db being recovered. e.g.
15883 **
15884 **     SELECT getpage(0);       -- return number of pages in db
15885 **     SELECT getpage(4);       -- return page 4 of db as a blob of data
15886 */
recoverGetPage(sqlite3_context * pCtx,int nArg,sqlite3_value ** apArg)15887 static void recoverGetPage(
15888   sqlite3_context *pCtx,
15889   int nArg,
15890   sqlite3_value **apArg
15891 ){
15892   sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
15893   i64 pgno = sqlite3_value_int64(apArg[0]);
15894   sqlite3_stmt *pStmt = 0;
15895 
15896   assert( nArg==1 );
15897   if( pgno==0 ){
15898     i64 nPg = recoverPageCount(p);
15899     sqlite3_result_int64(pCtx, nPg);
15900     return;
15901   }else{
15902     if( p->pGetPage==0 ){
15903       pStmt = p->pGetPage = recoverPreparePrintf(
15904           p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
15905       );
15906     }else if( p->errCode==SQLITE_OK ){
15907       pStmt = p->pGetPage;
15908     }
15909 
15910     if( pStmt ){
15911       sqlite3_bind_int64(pStmt, 1, pgno);
15912       if( SQLITE_ROW==sqlite3_step(pStmt) ){
15913         const u8 *aPg;
15914         int nPg;
15915         assert( p->errCode==SQLITE_OK );
15916         aPg = sqlite3_column_blob(pStmt, 0);
15917         nPg = sqlite3_column_bytes(pStmt, 0);
15918         if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
15919           aPg = p->pPage1Disk;
15920         }
15921         sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
15922       }
15923       recoverReset(p, pStmt);
15924     }
15925   }
15926 
15927   if( p->errCode ){
15928     if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
15929     sqlite3_result_error_code(pCtx, p->errCode);
15930   }
15931 }
15932 
15933 /*
15934 ** Find a string that is not found anywhere in z[].  Return a pointer
15935 ** to that string.
15936 **
15937 ** Try to use zA and zB first.  If both of those are already found in z[]
15938 ** then make up some string and store it in the buffer zBuf.
15939 */
recoverUnusedString(const char * z,const char * zA,const char * zB,char * zBuf)15940 static const char *recoverUnusedString(
15941   const char *z,                    /* Result must not appear anywhere in z */
15942   const char *zA, const char *zB,   /* Try these first */
15943   char *zBuf                        /* Space to store a generated string */
15944 ){
15945   unsigned i = 0;
15946   if( strstr(z, zA)==0 ) return zA;
15947   if( strstr(z, zB)==0 ) return zB;
15948   do{
15949     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
15950   }while( strstr(z,zBuf)!=0 );
15951   return zBuf;
15952 }
15953 
15954 /*
15955 ** Implementation of scalar SQL function "escape_crnl".  The argument passed to
15956 ** this function is the output of built-in function quote(). If the first
15957 ** character of the input is "'", indicating that the value passed to quote()
15958 ** was a text value, then this function searches the input for "\n" and "\r"
15959 ** characters and adds a wrapper similar to the following:
15960 **
15961 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
15962 **
15963 ** Or, if the first character of the input is not "'", then a copy of the input
15964 ** is returned.
15965 */
recoverEscapeCrnl(sqlite3_context * context,int argc,sqlite3_value ** argv)15966 static void recoverEscapeCrnl(
15967   sqlite3_context *context,
15968   int argc,
15969   sqlite3_value **argv
15970 ){
15971   const char *zText = (const char*)sqlite3_value_text(argv[0]);
15972   (void)argc;
15973   if( zText && zText[0]=='\'' ){
15974     int nText = sqlite3_value_bytes(argv[0]);
15975     int i;
15976     char zBuf1[20];
15977     char zBuf2[20];
15978     const char *zNL = 0;
15979     const char *zCR = 0;
15980     int nCR = 0;
15981     int nNL = 0;
15982 
15983     for(i=0; zText[i]; i++){
15984       if( zNL==0 && zText[i]=='\n' ){
15985         zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
15986         nNL = (int)strlen(zNL);
15987       }
15988       if( zCR==0 && zText[i]=='\r' ){
15989         zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
15990         nCR = (int)strlen(zCR);
15991       }
15992     }
15993 
15994     if( zNL || zCR ){
15995       int iOut = 0;
15996       i64 nMax = (nNL > nCR) ? nNL : nCR;
15997       i64 nAlloc = nMax * nText + (nMax+64)*2;
15998       char *zOut = (char*)sqlite3_malloc64(nAlloc);
15999       if( zOut==0 ){
16000         sqlite3_result_error_nomem(context);
16001         return;
16002       }
16003 
16004       if( zNL && zCR ){
16005         memcpy(&zOut[iOut], "replace(replace(", 16);
16006         iOut += 16;
16007       }else{
16008         memcpy(&zOut[iOut], "replace(", 8);
16009         iOut += 8;
16010       }
16011       for(i=0; zText[i]; i++){
16012         if( zText[i]=='\n' ){
16013           memcpy(&zOut[iOut], zNL, nNL);
16014           iOut += nNL;
16015         }else if( zText[i]=='\r' ){
16016           memcpy(&zOut[iOut], zCR, nCR);
16017           iOut += nCR;
16018         }else{
16019           zOut[iOut] = zText[i];
16020           iOut++;
16021         }
16022       }
16023 
16024       if( zNL ){
16025         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
16026         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
16027         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
16028       }
16029       if( zCR ){
16030         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
16031         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
16032         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
16033       }
16034 
16035       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
16036       sqlite3_free(zOut);
16037       return;
16038     }
16039   }
16040 
16041   sqlite3_result_value(context, argv[0]);
16042 }
16043 
16044 /*
16045 ** This function is a no-op if recover handle p already contains an error
16046 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16047 ** this case.
16048 **
16049 ** Otherwise, attempt to populate temporary table "recovery.schema" with the
16050 ** parts of the database schema that can be extracted from the input database.
16051 **
16052 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
16053 ** and error message are left in the recover handle and a copy of the
16054 ** error code returned. It is not considered an error if part of all of
16055 ** the database schema cannot be recovered due to corruption.
16056 */
recoverCacheSchema(sqlite3_recover * p)16057 static int recoverCacheSchema(sqlite3_recover *p){
16058   return recoverExec(p, p->dbOut,
16059     "WITH RECURSIVE pages(p) AS ("
16060     "  SELECT 1"
16061     "    UNION"
16062     "  SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
16063     ")"
16064     "INSERT INTO recovery.schema SELECT"
16065     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
16066     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
16067     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
16068     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
16069     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
16070     "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
16071     "  SELECT p FROM pages"
16072     ") GROUP BY pgno, cell"
16073   );
16074 }
16075 
16076 /*
16077 ** If this recover handle is not in SQL callback mode (i.e. was not created
16078 ** using sqlite3_recover_init_sql()) of if an error has already occurred,
16079 ** this function is a no-op. Otherwise, issue a callback with SQL statement
16080 ** zSql as the parameter.
16081 **
16082 ** If the callback returns non-zero, set the recover handle error code to
16083 ** the value returned (so that the caller will abandon processing).
16084 */
recoverSqlCallback(sqlite3_recover * p,const char * zSql)16085 static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
16086   if( p->errCode==SQLITE_OK && p->xSql ){
16087     int res = p->xSql(p->pSqlCtx, zSql);
16088     if( res ){
16089       recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
16090     }
16091   }
16092 }
16093 
16094 /*
16095 ** Transfer the following settings from the input database to the output
16096 ** database:
16097 **
16098 **   + page-size,
16099 **   + auto-vacuum settings,
16100 **   + database encoding,
16101 **   + user-version (PRAGMA user_version), and
16102 **   + application-id (PRAGMA application_id), and
16103 */
recoverTransferSettings(sqlite3_recover * p)16104 static void recoverTransferSettings(sqlite3_recover *p){
16105   const char *aPragma[] = {
16106     "encoding",
16107     "page_size",
16108     "auto_vacuum",
16109     "user_version",
16110     "application_id"
16111   };
16112   int ii;
16113 
16114   /* Truncate the output database to 0 pages in size. This is done by
16115   ** opening a new, empty, temp db, then using the backup API to clobber
16116   ** any existing output db with a copy of it. */
16117   if( p->errCode==SQLITE_OK ){
16118     sqlite3 *db2 = 0;
16119     int rc = sqlite3_open("", &db2);
16120     if( rc!=SQLITE_OK ){
16121       recoverDbError(p, db2);
16122       return;
16123     }
16124 
16125     for(ii=0; ii<(int)(sizeof(aPragma)/sizeof(aPragma[0])); ii++){
16126       const char *zPrag = aPragma[ii];
16127       sqlite3_stmt *p1 = 0;
16128       p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
16129       if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
16130         const char *zArg = (const char*)sqlite3_column_text(p1, 0);
16131         char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
16132         recoverSqlCallback(p, z2);
16133         recoverExec(p, db2, z2);
16134         sqlite3_free(z2);
16135         if( zArg==0 ){
16136           recoverError(p, SQLITE_NOMEM, 0);
16137         }
16138       }
16139       recoverFinalize(p, p1);
16140     }
16141     recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;");
16142 
16143     if( p->errCode==SQLITE_OK ){
16144       sqlite3 *db = p->dbOut;
16145       sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
16146       if( pBackup ){
16147         sqlite3_backup_step(pBackup, -1);
16148         p->errCode = sqlite3_backup_finish(pBackup);
16149       }else{
16150         recoverDbError(p, db);
16151       }
16152     }
16153 
16154     sqlite3_close(db2);
16155   }
16156 }
16157 
16158 /*
16159 ** This function is a no-op if recover handle p already contains an error
16160 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16161 ** this case.
16162 **
16163 ** Otherwise, an attempt is made to open the output database, attach
16164 ** and create the schema of the temporary database used to store
16165 ** intermediate data, and to register all required user functions and
16166 ** virtual table modules with the output handle.
16167 **
16168 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
16169 ** and error message are left in the recover handle and a copy of the
16170 ** error code returned.
16171 */
recoverOpenOutput(sqlite3_recover * p)16172 static int recoverOpenOutput(sqlite3_recover *p){
16173   struct Func {
16174     const char *zName;
16175     int nArg;
16176     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
16177   } aFunc[] = {
16178     { "getpage", 1, recoverGetPage },
16179     { "page_is_used", 1, recoverPageIsUsed },
16180     { "read_i32", 2, recoverReadI32 },
16181     { "escape_crnl", 1, recoverEscapeCrnl },
16182   };
16183 
16184   const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
16185   sqlite3 *db = 0;                /* New database handle */
16186   int ii;                         /* For iterating through aFunc[] */
16187 
16188   assert( p->dbOut==0 );
16189 
16190   if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
16191     recoverDbError(p, db);
16192   }
16193 
16194   /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
16195   ** These two are registered with the output database handle - this
16196   ** module depends on the input handle supporting the sqlite_dbpage
16197   ** virtual table only.  */
16198   if( p->errCode==SQLITE_OK ){
16199     p->errCode = sqlite3_dbdata_init(db, 0, 0);
16200   }
16201 
16202   /* Register the custom user-functions with the output handle. */
16203   for(ii=0;
16204       p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0]));
16205       ii++){
16206     p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
16207         aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
16208     );
16209   }
16210 
16211   p->dbOut = db;
16212   return p->errCode;
16213 }
16214 
16215 /*
16216 ** Attach the auxiliary database 'recovery' to the output database handle.
16217 ** This temporary database is used during the recovery process and then
16218 ** discarded.
16219 */
recoverOpenRecovery(sqlite3_recover * p)16220 static void recoverOpenRecovery(sqlite3_recover *p){
16221   char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
16222   recoverExec(p, p->dbOut, zSql);
16223   recoverExec(p, p->dbOut,
16224       "PRAGMA writable_schema = 1;"
16225       "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);"
16226       "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
16227   );
16228   sqlite3_free(zSql);
16229 }
16230 
16231 
16232 /*
16233 ** This function is a no-op if recover handle p already contains an error
16234 ** (if p->errCode!=SQLITE_OK).
16235 **
16236 ** Otherwise, argument zName must be the name of a table that has just been
16237 ** created in the output database. This function queries the output db
16238 ** for the schema of said table, and creates a RecoverTable object to
16239 ** store the schema in memory. The new RecoverTable object is linked into
16240 ** the list at sqlite3_recover.pTblList.
16241 **
16242 ** Parameter iRoot must be the root page of table zName in the INPUT
16243 ** database.
16244 */
recoverAddTable(sqlite3_recover * p,const char * zName,i64 iRoot)16245 static void recoverAddTable(
16246   sqlite3_recover *p,
16247   const char *zName,              /* Name of table created in output db */
16248   i64 iRoot                       /* Root page of same table in INPUT db */
16249 ){
16250   sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
16251       "PRAGMA table_xinfo(%Q)", zName
16252   );
16253 
16254   if( pStmt ){
16255     int iPk = -1;
16256     int iBind = 1;
16257     RecoverTable *pNew = 0;
16258     int nCol = 0;
16259     int nName = recoverStrlen(zName);
16260     int nByte = 0;
16261     while( sqlite3_step(pStmt)==SQLITE_ROW ){
16262       nCol++;
16263       nByte += (sqlite3_column_bytes(pStmt, 1)+1);
16264     }
16265     nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
16266     recoverReset(p, pStmt);
16267 
16268     pNew = recoverMalloc(p, nByte);
16269     if( pNew ){
16270       int i = 0;
16271       int iField = 0;
16272       char *csr = 0;
16273       pNew->aCol = (RecoverColumn*)&pNew[1];
16274       pNew->zTab = csr = (char*)&pNew->aCol[nCol];
16275       pNew->nCol = nCol;
16276       pNew->iRoot = iRoot;
16277       memcpy(csr, zName, nName);
16278       csr += nName+1;
16279 
16280       for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
16281         int iPKF = sqlite3_column_int(pStmt, 5);
16282         int n = sqlite3_column_bytes(pStmt, 1);
16283         const char *z = (const char*)sqlite3_column_text(pStmt, 1);
16284         const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
16285         int eHidden = sqlite3_column_int(pStmt, 6);
16286 
16287         if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
16288         if( iPKF>1 ) iPk = -2;
16289         pNew->aCol[i].zCol = csr;
16290         pNew->aCol[i].eHidden = eHidden;
16291         if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
16292           pNew->aCol[i].iField = -1;
16293         }else{
16294           pNew->aCol[i].iField = iField++;
16295         }
16296         if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
16297          && eHidden!=RECOVER_EHIDDEN_STORED
16298         ){
16299           pNew->aCol[i].iBind = iBind++;
16300         }
16301         memcpy(csr, z, n);
16302         csr += (n+1);
16303       }
16304 
16305       pNew->pNext = p->pTblList;
16306       p->pTblList = pNew;
16307       pNew->bIntkey = 1;
16308     }
16309 
16310     recoverFinalize(p, pStmt);
16311 
16312     pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
16313     while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16314       int iField = sqlite3_column_int(pStmt, 0);
16315       int iCol = sqlite3_column_int(pStmt, 1);
16316 
16317       assert( iCol<pNew->nCol );
16318       pNew->aCol[iCol].iField = iField;
16319 
16320       pNew->bIntkey = 0;
16321       iPk = -2;
16322     }
16323     recoverFinalize(p, pStmt);
16324 
16325     if( p->errCode==SQLITE_OK ){
16326       if( iPk>=0 ){
16327         pNew->aCol[iPk].bIPK = 1;
16328       }else if( pNew->bIntkey ){
16329         pNew->iRowidBind = iBind++;
16330       }
16331     }
16332   }
16333 }
16334 
16335 /*
16336 ** This function is called after recoverCacheSchema() has cached those parts
16337 ** of the input database schema that could be recovered in temporary table
16338 ** "recovery.schema". This function creates in the output database copies
16339 ** of all parts of that schema that must be created before the tables can
16340 ** be populated. Specifically, this means:
16341 **
16342 **     * all tables that are not VIRTUAL, and
16343 **     * UNIQUE indexes.
16344 **
16345 ** If the recovery handle uses SQL callbacks, then callbacks containing
16346 ** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
16347 **
16348 ** Additionally, records are added to the sqlite_schema table of the
16349 ** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
16350 ** records are written directly to sqlite_schema, not actually executed.
16351 ** If the handle is in SQL callback mode, then callbacks are invoked
16352 ** with equivalent SQL statements.
16353 */
recoverWriteSchema1(sqlite3_recover * p)16354 static int recoverWriteSchema1(sqlite3_recover *p){
16355   sqlite3_stmt *pSelect = 0;
16356   sqlite3_stmt *pTblname = 0;
16357 
16358   pSelect = recoverPrepare(p, p->dbOut,
16359       "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
16360       "  SELECT rootpage, name, sql, "
16361       "    type='table', "
16362       "    sql LIKE 'create virtual%',"
16363       "    (type='index' AND (sql LIKE '%unique%' OR ?1))"
16364       "  FROM recovery.schema"
16365       ")"
16366       "SELECT rootpage, tbl, isVirtual, name, sql"
16367       " FROM dbschema "
16368       "  WHERE tbl OR isIndex"
16369       "  ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
16370   );
16371 
16372   pTblname = recoverPrepare(p, p->dbOut,
16373       "SELECT name FROM sqlite_schema "
16374       "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
16375   );
16376 
16377   if( pSelect ){
16378     sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
16379     while( sqlite3_step(pSelect)==SQLITE_ROW ){
16380       i64 iRoot = sqlite3_column_int64(pSelect, 0);
16381       int bTable = sqlite3_column_int(pSelect, 1);
16382       int bVirtual = sqlite3_column_int(pSelect, 2);
16383       const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
16384       const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
16385       char *zFree = 0;
16386       int rc = SQLITE_OK;
16387 
16388       if( bVirtual ){
16389         zSql = (const char*)(zFree = recoverMPrintf(p,
16390             "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
16391             zName, zName, zSql
16392         ));
16393       }
16394       rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
16395       if( rc==SQLITE_OK ){
16396         recoverSqlCallback(p, zSql);
16397         if( bTable && !bVirtual ){
16398           if( SQLITE_ROW==sqlite3_step(pTblname) ){
16399             const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
16400             recoverAddTable(p, zTbl, iRoot);
16401           }
16402           recoverReset(p, pTblname);
16403         }
16404       }else if( rc!=SQLITE_ERROR ){
16405         recoverDbError(p, p->dbOut);
16406       }
16407       sqlite3_free(zFree);
16408     }
16409   }
16410   recoverFinalize(p, pSelect);
16411   recoverFinalize(p, pTblname);
16412 
16413   return p->errCode;
16414 }
16415 
16416 /*
16417 ** This function is called after the output database has been populated. It
16418 ** adds all recovered schema elements that were not created in the output
16419 ** database by recoverWriteSchema1() - everything except for tables and
16420 ** UNIQUE indexes. Specifically:
16421 **
16422 **     * views,
16423 **     * triggers,
16424 **     * non-UNIQUE indexes.
16425 **
16426 ** If the recover handle is in SQL callback mode, then equivalent callbacks
16427 ** are issued to create the schema elements.
16428 */
recoverWriteSchema2(sqlite3_recover * p)16429 static int recoverWriteSchema2(sqlite3_recover *p){
16430   sqlite3_stmt *pSelect = 0;
16431 
16432   pSelect = recoverPrepare(p, p->dbOut,
16433       p->bSlowIndexes ?
16434       "SELECT rootpage, sql FROM recovery.schema "
16435       "  WHERE type!='table' AND type!='index'"
16436       :
16437       "SELECT rootpage, sql FROM recovery.schema "
16438       "  WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
16439   );
16440 
16441   if( pSelect ){
16442     while( sqlite3_step(pSelect)==SQLITE_ROW ){
16443       const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
16444       int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0);
16445       if( rc==SQLITE_OK ){
16446         recoverSqlCallback(p, zSql);
16447       }else if( rc!=SQLITE_ERROR ){
16448         recoverDbError(p, p->dbOut);
16449       }
16450     }
16451   }
16452   recoverFinalize(p, pSelect);
16453 
16454   return p->errCode;
16455 }
16456 
16457 /*
16458 ** This function is a no-op if recover handle p already contains an error
16459 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
16460 **
16461 ** Otherwise, if the recover handle is configured to create an output
16462 ** database (was created by sqlite3_recover_init()), then this function
16463 ** prepares and returns an SQL statement to INSERT a new record into table
16464 ** pTab, assuming the first nField fields of a record extracted from disk
16465 ** are valid.
16466 **
16467 ** For example, if table pTab is:
16468 **
16469 **     CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
16470 **
16471 ** And nField is 4, then the SQL statement prepared and returned is:
16472 **
16473 **     INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
16474 **
16475 ** In this case even though 4 values were extracted from the input db,
16476 ** only 3 are written to the output, as the generated STORED column
16477 ** cannot be written.
16478 **
16479 ** If the recover handle is in SQL callback mode, then the SQL statement
16480 ** prepared is such that evaluating it returns a single row containing
16481 ** a single text value - itself an SQL statement similar to the above,
16482 ** except with SQL literals in place of the variables. For example:
16483 **
16484 **     SELECT 'INSERT INTO (a, c, d) VALUES ('
16485 **          || quote(?1) || ', '
16486 **          || quote(?2) || ', '
16487 **          || quote(?3) || ')';
16488 **
16489 ** In either case, it is the responsibility of the caller to eventually
16490 ** free the statement handle using sqlite3_finalize().
16491 */
recoverInsertStmt(sqlite3_recover * p,RecoverTable * pTab,int nField)16492 static sqlite3_stmt *recoverInsertStmt(
16493   sqlite3_recover *p,
16494   RecoverTable *pTab,
16495   int nField
16496 ){
16497   sqlite3_stmt *pRet = 0;
16498   const char *zSep = "";
16499   const char *zSqlSep = "";
16500   char *zSql = 0;
16501   char *zFinal = 0;
16502   char *zBind = 0;
16503   int ii;
16504   int bSql = p->xSql ? 1 : 0;
16505 
16506   if( nField<=0 ) return 0;
16507 
16508   assert( nField<=pTab->nCol );
16509 
16510   zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
16511 
16512   if( pTab->iRowidBind ){
16513     assert( pTab->bIntkey );
16514     zSql = recoverMPrintf(p, "%z_rowid_", zSql);
16515     if( bSql ){
16516       zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
16517     }else{
16518       zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
16519     }
16520     zSqlSep = "||', '||";
16521     zSep = ", ";
16522   }
16523 
16524   for(ii=0; ii<nField; ii++){
16525     int eHidden = pTab->aCol[ii].eHidden;
16526     if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
16527      && eHidden!=RECOVER_EHIDDEN_STORED
16528     ){
16529       assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
16530       zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
16531 
16532       if( bSql ){
16533         zBind = recoverMPrintf(p,
16534             "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
16535         );
16536         zSqlSep = "||', '||";
16537       }else{
16538         zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
16539       }
16540       zSep = ", ";
16541     }
16542   }
16543 
16544   if( bSql ){
16545     zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
16546         zSql, zBind
16547     );
16548   }else{
16549     zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
16550   }
16551 
16552   pRet = recoverPrepare(p, p->dbOut, zFinal);
16553   sqlite3_free(zSql);
16554   sqlite3_free(zBind);
16555   sqlite3_free(zFinal);
16556 
16557   return pRet;
16558 }
16559 
16560 
16561 /*
16562 ** Search the list of RecoverTable objects at p->pTblList for one that
16563 ** has root page iRoot in the input database. If such an object is found,
16564 ** return a pointer to it. Otherwise, return NULL.
16565 */
recoverFindTable(sqlite3_recover * p,u32 iRoot)16566 static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
16567   RecoverTable *pRet = 0;
16568   for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
16569   return pRet;
16570 }
16571 
16572 /*
16573 ** This function attempts to create a lost and found table within the
16574 ** output db. If successful, it returns a pointer to a buffer containing
16575 ** the name of the new table. It is the responsibility of the caller to
16576 ** eventually free this buffer using sqlite3_free().
16577 **
16578 ** If an error occurs, NULL is returned and an error code and error
16579 ** message left in the recover handle.
16580 */
recoverLostAndFoundCreate(sqlite3_recover * p,int nField)16581 static char *recoverLostAndFoundCreate(
16582   sqlite3_recover *p,             /* Recover object */
16583   int nField                      /* Number of column fields in new table */
16584 ){
16585   char *zTbl = 0;
16586   sqlite3_stmt *pProbe = 0;
16587   int ii = 0;
16588 
16589   pProbe = recoverPrepare(p, p->dbOut,
16590     "SELECT 1 FROM sqlite_schema WHERE name=?"
16591   );
16592   for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
16593     int bFail = 0;
16594     if( ii<0 ){
16595       zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
16596     }else{
16597       zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
16598     }
16599 
16600     if( p->errCode==SQLITE_OK ){
16601       sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
16602       if( SQLITE_ROW==sqlite3_step(pProbe) ){
16603         bFail = 1;
16604       }
16605       recoverReset(p, pProbe);
16606     }
16607 
16608     if( bFail ){
16609       sqlite3_clear_bindings(pProbe);
16610       sqlite3_free(zTbl);
16611       zTbl = 0;
16612     }
16613   }
16614   recoverFinalize(p, pProbe);
16615 
16616   if( zTbl ){
16617     const char *zSep = 0;
16618     char *zField = 0;
16619     char *zSql = 0;
16620 
16621     zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
16622     for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
16623       zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
16624       zSep = ", ";
16625     }
16626 
16627     zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
16628     sqlite3_free(zField);
16629 
16630     recoverExec(p, p->dbOut, zSql);
16631     recoverSqlCallback(p, zSql);
16632     sqlite3_free(zSql);
16633   }else if( p->errCode==SQLITE_OK ){
16634     recoverError(
16635         p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
16636     );
16637   }
16638 
16639   return zTbl;
16640 }
16641 
16642 /*
16643 ** Synthesize and prepare an INSERT statement to write to the lost_and_found
16644 ** table in the output database. The name of the table is zTab, and it has
16645 ** nField c* fields.
16646 */
recoverLostAndFoundInsert(sqlite3_recover * p,const char * zTab,int nField)16647 static sqlite3_stmt *recoverLostAndFoundInsert(
16648   sqlite3_recover *p,
16649   const char *zTab,
16650   int nField
16651 ){
16652   int nTotal = nField + 4;
16653   int ii;
16654   char *zBind = 0;
16655   sqlite3_stmt *pRet = 0;
16656 
16657   if( p->xSql==0 ){
16658     for(ii=0; ii<nTotal; ii++){
16659       zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
16660     }
16661     pRet = recoverPreparePrintf(
16662         p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
16663     );
16664   }else{
16665     const char *zSep = "";
16666     for(ii=0; ii<nTotal; ii++){
16667       zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
16668       zSep = "|| ', ' ||";
16669     }
16670     pRet = recoverPreparePrintf(
16671         p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
16672     );
16673   }
16674 
16675   sqlite3_free(zBind);
16676   return pRet;
16677 }
16678 
16679 /*
16680 ** Input database page iPg contains data that will be written to the
16681 ** lost-and-found table of the output database. This function attempts
16682 ** to identify the root page of the tree that page iPg belonged to.
16683 ** If successful, it sets output variable (*piRoot) to the page number
16684 ** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
16685 ** an SQLite error code is returned and the final value of *piRoot
16686 ** undefined.
16687 */
recoverLostAndFoundFindRoot(sqlite3_recover * p,i64 iPg,i64 * piRoot)16688 static int recoverLostAndFoundFindRoot(
16689   sqlite3_recover *p,
16690   i64 iPg,
16691   i64 *piRoot
16692 ){
16693   RecoverStateLAF *pLaf = &p->laf;
16694 
16695   if( pLaf->pFindRoot==0 ){
16696     pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
16697         "WITH RECURSIVE p(pgno) AS ("
16698         "  SELECT ?"
16699         "    UNION"
16700         "  SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
16701         ") "
16702         "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
16703         "    AND m.parent IS NULL"
16704     );
16705   }
16706   if( p->errCode==SQLITE_OK ){
16707     sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
16708     if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
16709       *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
16710     }else{
16711       *piRoot = iPg;
16712     }
16713     recoverReset(p, pLaf->pFindRoot);
16714   }
16715   return p->errCode;
16716 }
16717 
16718 /*
16719 ** Recover data from page iPage of the input database and write it to
16720 ** the lost-and-found table in the output database.
16721 */
recoverLostAndFoundOnePage(sqlite3_recover * p,i64 iPage)16722 static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
16723   RecoverStateLAF *pLaf = &p->laf;
16724   sqlite3_value **apVal = pLaf->apVal;
16725   sqlite3_stmt *pPageData = pLaf->pPageData;
16726   sqlite3_stmt *pInsert = pLaf->pInsert;
16727 
16728   int nVal = -1;
16729   int iPrevCell = 0;
16730   i64 iRoot = 0;
16731   int bHaveRowid = 0;
16732   i64 iRowid = 0;
16733   int ii = 0;
16734 
16735   if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
16736   sqlite3_bind_int64(pPageData, 1, iPage);
16737   while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
16738     int iCell = sqlite3_column_int64(pPageData, 0);
16739     int iField = sqlite3_column_int64(pPageData, 1);
16740 
16741     if( iPrevCell!=iCell && nVal>=0 ){
16742       /* Insert the new row */
16743       sqlite3_bind_int64(pInsert, 1, iRoot);      /* rootpgno */
16744       sqlite3_bind_int64(pInsert, 2, iPage);      /* pgno */
16745       sqlite3_bind_int(pInsert, 3, nVal);         /* nfield */
16746       if( bHaveRowid ){
16747         sqlite3_bind_int64(pInsert, 4, iRowid);   /* id */
16748       }
16749       for(ii=0; ii<nVal; ii++){
16750         recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
16751       }
16752       if( sqlite3_step(pInsert)==SQLITE_ROW ){
16753         recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
16754       }
16755       recoverReset(p, pInsert);
16756 
16757       /* Discard the accumulated row data */
16758       for(ii=0; ii<nVal; ii++){
16759         sqlite3_value_free(apVal[ii]);
16760         apVal[ii] = 0;
16761       }
16762       sqlite3_clear_bindings(pInsert);
16763       bHaveRowid = 0;
16764       nVal = -1;
16765     }
16766 
16767     if( iCell<0 ) break;
16768 
16769     if( iField<0 ){
16770       assert( nVal==-1 );
16771       iRowid = sqlite3_column_int64(pPageData, 2);
16772       bHaveRowid = 1;
16773       nVal = 0;
16774     }else if( iField<pLaf->nMaxField ){
16775       sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
16776       apVal[iField] = sqlite3_value_dup(pVal);
16777       assert( iField==nVal || (nVal==-1 && iField==0) );
16778       nVal = iField+1;
16779       if( apVal[iField]==0 ){
16780         recoverError(p, SQLITE_NOMEM, 0);
16781       }
16782     }
16783 
16784     iPrevCell = iCell;
16785   }
16786   recoverReset(p, pPageData);
16787 
16788   for(ii=0; ii<nVal; ii++){
16789     sqlite3_value_free(apVal[ii]);
16790     apVal[ii] = 0;
16791   }
16792 }
16793 
16794 /*
16795 ** Perform one step (sqlite3_recover_step()) of work for the connection
16796 ** passed as the only argument, which is guaranteed to be in
16797 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
16798 ** table of the output database is populated with recovered data that can
16799 ** not be assigned to any recovered schema object.
16800 */
recoverLostAndFound3Step(sqlite3_recover * p)16801 static int recoverLostAndFound3Step(sqlite3_recover *p){
16802   RecoverStateLAF *pLaf = &p->laf;
16803   if( p->errCode==SQLITE_OK ){
16804     if( pLaf->pInsert==0 ){
16805       return SQLITE_DONE;
16806     }else{
16807       if( p->errCode==SQLITE_OK ){
16808         int res = sqlite3_step(pLaf->pAllPage);
16809         if( res==SQLITE_ROW ){
16810           i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
16811           if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
16812             recoverLostAndFoundOnePage(p, iPage);
16813           }
16814         }else{
16815           recoverReset(p, pLaf->pAllPage);
16816           return SQLITE_DONE;
16817         }
16818       }
16819     }
16820   }
16821   return SQLITE_OK;
16822 }
16823 
16824 /*
16825 ** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
16826 ** state - during which the lost-and-found table of the output database
16827 ** is populated with recovered data that can not be assigned to any
16828 ** recovered schema object.
16829 */
recoverLostAndFound3Init(sqlite3_recover * p)16830 static void recoverLostAndFound3Init(sqlite3_recover *p){
16831   RecoverStateLAF *pLaf = &p->laf;
16832 
16833   if( pLaf->nMaxField>0 ){
16834     char *zTab = 0;               /* Name of lost_and_found table */
16835 
16836     zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
16837     pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
16838     sqlite3_free(zTab);
16839 
16840     pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
16841         "WITH RECURSIVE seq(ii) AS ("
16842         "  SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
16843         ")"
16844         "SELECT ii FROM seq" , p->laf.nPg
16845     );
16846     pLaf->pPageData = recoverPrepare(p, p->dbOut,
16847         "SELECT cell, field, value "
16848         "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
16849         "UNION ALL "
16850         "SELECT -1, -1, -1"
16851     );
16852 
16853     pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
16854         pLaf->nMaxField*sizeof(sqlite3_value*)
16855     );
16856   }
16857 }
16858 
16859 /*
16860 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
16861 ** tables recovered from the schema of the input database are populated with
16862 ** recovered data.
16863 */
recoverWriteDataInit(sqlite3_recover * p)16864 static int recoverWriteDataInit(sqlite3_recover *p){
16865   RecoverStateW1 *p1 = &p->w1;
16866   RecoverTable *pTbl = 0;
16867   int nByte = 0;
16868 
16869   /* Figure out the maximum number of columns for any table in the schema */
16870   assert( p1->nMax==0 );
16871   for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
16872     if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
16873   }
16874 
16875   /* Allocate an array of (sqlite3_value*) in which to accumulate the values
16876   ** that will be written to the output database in a single row. */
16877   nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
16878   p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
16879   if( p1->apVal==0 ) return p->errCode;
16880 
16881   /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
16882   ** to loop through cells that appear to belong to a single table (pSel). */
16883   p1->pTbls = recoverPrepare(p, p->dbOut,
16884       "SELECT rootpage FROM recovery.schema "
16885       "  WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
16886       "  ORDER BY (tbl_name='sqlite_sequence') ASC"
16887   );
16888   p1->pSel = recoverPrepare(p, p->dbOut,
16889       "WITH RECURSIVE pages(page) AS ("
16890       "  SELECT ?1"
16891       "    UNION"
16892       "  SELECT child FROM sqlite_dbptr('getpage()'), pages "
16893       "    WHERE pgno=page"
16894       ") "
16895       "SELECT page, cell, field, value "
16896       "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
16897       "UNION ALL "
16898       "SELECT 0, 0, 0, 0"
16899   );
16900 
16901   return p->errCode;
16902 }
16903 
16904 /*
16905 ** Clean up resources allocated by recoverWriteDataInit() (stuff in
16906 ** sqlite3_recover.w1).
16907 */
recoverWriteDataCleanup(sqlite3_recover * p)16908 static void recoverWriteDataCleanup(sqlite3_recover *p){
16909   RecoverStateW1 *p1 = &p->w1;
16910   int ii;
16911   for(ii=0; ii<p1->nVal; ii++){
16912     sqlite3_value_free(p1->apVal[ii]);
16913   }
16914   sqlite3_free(p1->apVal);
16915   recoverFinalize(p, p1->pInsert);
16916   recoverFinalize(p, p1->pTbls);
16917   recoverFinalize(p, p1->pSel);
16918   memset(p1, 0, sizeof(*p1));
16919 }
16920 
16921 /*
16922 ** Perform one step (sqlite3_recover_step()) of work for the connection
16923 ** passed as the only argument, which is guaranteed to be in
16924 ** RECOVER_STATE_WRITING state - during which tables recovered from the
16925 ** schema of the input database are populated with recovered data.
16926 */
recoverWriteDataStep(sqlite3_recover * p)16927 static int recoverWriteDataStep(sqlite3_recover *p){
16928   RecoverStateW1 *p1 = &p->w1;
16929   sqlite3_stmt *pSel = p1->pSel;
16930   sqlite3_value **apVal = p1->apVal;
16931 
16932   if( p->errCode==SQLITE_OK && p1->pTab==0 ){
16933     if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
16934       i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
16935       p1->pTab = recoverFindTable(p, iRoot);
16936 
16937       recoverFinalize(p, p1->pInsert);
16938       p1->pInsert = 0;
16939 
16940       /* If this table is unknown, return early. The caller will invoke this
16941       ** function again and it will move on to the next table.  */
16942       if( p1->pTab==0 ) return p->errCode;
16943 
16944       /* If this is the sqlite_sequence table, delete any rows added by
16945       ** earlier INSERT statements on tables with AUTOINCREMENT primary
16946       ** keys before recovering its contents. The p1->pTbls SELECT statement
16947       ** is rigged to deliver "sqlite_sequence" last of all, so we don't
16948       ** worry about it being modified after it is recovered. */
16949       if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
16950         recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
16951         recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
16952       }
16953 
16954       /* Bind the root page of this table within the original database to
16955       ** SELECT statement p1->pSel. The SELECT statement will then iterate
16956       ** through cells that look like they belong to table pTab.  */
16957       sqlite3_bind_int64(pSel, 1, iRoot);
16958 
16959       p1->nVal = 0;
16960       p1->bHaveRowid = 0;
16961       p1->iPrevPage = -1;
16962       p1->iPrevCell = -1;
16963     }else{
16964       return SQLITE_DONE;
16965     }
16966   }
16967   assert( p->errCode!=SQLITE_OK || p1->pTab );
16968 
16969   if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
16970     RecoverTable *pTab = p1->pTab;
16971 
16972     i64 iPage = sqlite3_column_int64(pSel, 0);
16973     int iCell = sqlite3_column_int(pSel, 1);
16974     int iField = sqlite3_column_int(pSel, 2);
16975     sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
16976     int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
16977 
16978     assert( bNewCell==0 || (iField==-1 || iField==0) );
16979     assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
16980 
16981     if( bNewCell ){
16982       int ii = 0;
16983       if( p1->nVal>=0 ){
16984         if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
16985           recoverFinalize(p, p1->pInsert);
16986           p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
16987           p1->nInsert = p1->nVal;
16988         }
16989         if( p1->nVal>0 ){
16990           sqlite3_stmt *pInsert = p1->pInsert;
16991           for(ii=0; ii<pTab->nCol; ii++){
16992             RecoverColumn *pCol = &pTab->aCol[ii];
16993             int iBind = pCol->iBind;
16994             if( iBind>0 ){
16995               if( pCol->bIPK ){
16996                 sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
16997               }else if( pCol->iField<p1->nVal ){
16998                 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
16999               }
17000             }
17001           }
17002           if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
17003             sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
17004           }
17005           if( SQLITE_ROW==sqlite3_step(pInsert) ){
17006             const char *z = (const char*)sqlite3_column_text(pInsert, 0);
17007             recoverSqlCallback(p, z);
17008           }
17009           recoverReset(p, pInsert);
17010           assert( p->errCode || pInsert );
17011           if( pInsert ) sqlite3_clear_bindings(pInsert);
17012         }
17013       }
17014 
17015       for(ii=0; ii<p1->nVal; ii++){
17016         sqlite3_value_free(apVal[ii]);
17017         apVal[ii] = 0;
17018       }
17019       p1->nVal = -1;
17020       p1->bHaveRowid = 0;
17021     }
17022 
17023     if( iPage!=0 ){
17024       if( iField<0 ){
17025         p1->iRowid = sqlite3_column_int64(pSel, 3);
17026         assert( p1->nVal==-1 );
17027         p1->nVal = 0;
17028         p1->bHaveRowid = 1;
17029       }else if( iField<pTab->nCol ){
17030         assert( apVal[iField]==0 );
17031         apVal[iField] = sqlite3_value_dup( pVal );
17032         if( apVal[iField]==0 ){
17033           recoverError(p, SQLITE_NOMEM, 0);
17034         }
17035         p1->nVal = iField+1;
17036       }
17037       p1->iPrevCell = iCell;
17038       p1->iPrevPage = iPage;
17039     }
17040   }else{
17041     recoverReset(p, pSel);
17042     p1->pTab = 0;
17043   }
17044 
17045   return p->errCode;
17046 }
17047 
17048 /*
17049 ** Initialize resources required by sqlite3_recover_step() in
17050 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17051 ** already allocated to a recovered schema element is determined.
17052 */
recoverLostAndFound1Init(sqlite3_recover * p)17053 static void recoverLostAndFound1Init(sqlite3_recover *p){
17054   RecoverStateLAF *pLaf = &p->laf;
17055   sqlite3_stmt *pStmt = 0;
17056 
17057   assert( p->laf.pUsed==0 );
17058   pLaf->nPg = recoverPageCount(p);
17059   pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
17060 
17061   /* Prepare a statement to iterate through all pages that are part of any tree
17062   ** in the recoverable part of the input database schema to the bitmap. And,
17063   ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
17064   ** freelist.  */
17065   pStmt = recoverPrepare(
17066       p, p->dbOut,
17067       "WITH trunk(pgno) AS ("
17068       "  SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
17069       "    UNION"
17070       "  SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
17071       "),"
17072       "trunkdata(pgno, data) AS ("
17073       "  SELECT pgno, getpage(pgno) FROM trunk"
17074       "),"
17075       "freelist(data, n, freepgno) AS ("
17076       "  SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
17077       "    UNION ALL"
17078       "  SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
17079       "),"
17080       ""
17081       "roots(r) AS ("
17082       "  SELECT 1 UNION ALL"
17083       "  SELECT rootpage FROM recovery.schema WHERE rootpage>0"
17084       "),"
17085       "used(page) AS ("
17086       "  SELECT r FROM roots"
17087       "    UNION"
17088       "  SELECT child FROM sqlite_dbptr('getpage()'), used "
17089       "    WHERE pgno=page"
17090       ") "
17091       "SELECT page FROM used"
17092       " UNION ALL "
17093       "SELECT freepgno FROM freelist WHERE NOT ?"
17094   );
17095   if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
17096   pLaf->pUsedPages = pStmt;
17097 }
17098 
17099 /*
17100 ** Perform one step (sqlite3_recover_step()) of work for the connection
17101 ** passed as the only argument, which is guaranteed to be in
17102 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17103 ** already allocated to a recovered schema element is determined.
17104 */
recoverLostAndFound1Step(sqlite3_recover * p)17105 static int recoverLostAndFound1Step(sqlite3_recover *p){
17106   RecoverStateLAF *pLaf = &p->laf;
17107   int rc = p->errCode;
17108   if( rc==SQLITE_OK ){
17109     rc = sqlite3_step(pLaf->pUsedPages);
17110     if( rc==SQLITE_ROW ){
17111       i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
17112       recoverBitmapSet(pLaf->pUsed, iPg);
17113       rc = SQLITE_OK;
17114     }else{
17115       recoverFinalize(p, pLaf->pUsedPages);
17116       pLaf->pUsedPages = 0;
17117     }
17118   }
17119   return rc;
17120 }
17121 
17122 /*
17123 ** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
17124 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
17125 ** are sorted into sets that likely belonged to the same database tree.
17126 */
recoverLostAndFound2Init(sqlite3_recover * p)17127 static void recoverLostAndFound2Init(sqlite3_recover *p){
17128   RecoverStateLAF *pLaf = &p->laf;
17129 
17130   assert( p->laf.pAllAndParent==0 );
17131   assert( p->laf.pMapInsert==0 );
17132   assert( p->laf.pMaxField==0 );
17133   assert( p->laf.nMaxField==0 );
17134 
17135   pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
17136       "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
17137   );
17138   pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
17139       "WITH RECURSIVE seq(ii) AS ("
17140       "  SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
17141       ")"
17142       "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
17143       " UNION ALL "
17144       "SELECT NULL, ii FROM seq", p->laf.nPg
17145   );
17146   pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
17147       "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
17148   );
17149 }
17150 
17151 /*
17152 ** Perform one step (sqlite3_recover_step()) of work for the connection
17153 ** passed as the only argument, which is guaranteed to be in
17154 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
17155 ** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
17156 ** to the same database tree.
17157 */
recoverLostAndFound2Step(sqlite3_recover * p)17158 static int recoverLostAndFound2Step(sqlite3_recover *p){
17159   RecoverStateLAF *pLaf = &p->laf;
17160   if( p->errCode==SQLITE_OK ){
17161     int res = sqlite3_step(pLaf->pAllAndParent);
17162     if( res==SQLITE_ROW ){
17163       i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
17164       if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
17165         sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
17166         sqlite3_bind_value(pLaf->pMapInsert, 2,
17167             sqlite3_column_value(pLaf->pAllAndParent, 0)
17168         );
17169         sqlite3_step(pLaf->pMapInsert);
17170         recoverReset(p, pLaf->pMapInsert);
17171         sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
17172         if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
17173           int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
17174           if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
17175         }
17176         recoverReset(p, pLaf->pMaxField);
17177       }
17178     }else{
17179       recoverFinalize(p, pLaf->pAllAndParent);
17180       pLaf->pAllAndParent =0;
17181       return SQLITE_DONE;
17182     }
17183   }
17184   return p->errCode;
17185 }
17186 
17187 /*
17188 ** Free all resources allocated as part of sqlite3_recover_step() calls
17189 ** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
17190 */
recoverLostAndFoundCleanup(sqlite3_recover * p)17191 static void recoverLostAndFoundCleanup(sqlite3_recover *p){
17192   recoverBitmapFree(p->laf.pUsed);
17193   p->laf.pUsed = 0;
17194   sqlite3_finalize(p->laf.pUsedPages);
17195   sqlite3_finalize(p->laf.pAllAndParent);
17196   sqlite3_finalize(p->laf.pMapInsert);
17197   sqlite3_finalize(p->laf.pMaxField);
17198   sqlite3_finalize(p->laf.pFindRoot);
17199   sqlite3_finalize(p->laf.pInsert);
17200   sqlite3_finalize(p->laf.pAllPage);
17201   sqlite3_finalize(p->laf.pPageData);
17202   p->laf.pUsedPages = 0;
17203   p->laf.pAllAndParent = 0;
17204   p->laf.pMapInsert = 0;
17205   p->laf.pMaxField = 0;
17206   p->laf.pFindRoot = 0;
17207   p->laf.pInsert = 0;
17208   p->laf.pAllPage = 0;
17209   p->laf.pPageData = 0;
17210   sqlite3_free(p->laf.apVal);
17211   p->laf.apVal = 0;
17212 }
17213 
17214 /*
17215 ** Free all resources allocated as part of sqlite3_recover_step() calls.
17216 */
recoverFinalCleanup(sqlite3_recover * p)17217 static void recoverFinalCleanup(sqlite3_recover *p){
17218   RecoverTable *pTab = 0;
17219   RecoverTable *pNext = 0;
17220 
17221   recoverWriteDataCleanup(p);
17222   recoverLostAndFoundCleanup(p);
17223 
17224   for(pTab=p->pTblList; pTab; pTab=pNext){
17225     pNext = pTab->pNext;
17226     sqlite3_free(pTab);
17227   }
17228   p->pTblList = 0;
17229   sqlite3_finalize(p->pGetPage);
17230   p->pGetPage = 0;
17231   sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
17232 
17233   {
17234 #ifndef NDEBUG
17235     int res =
17236 #endif
17237        sqlite3_close(p->dbOut);
17238     assert( res==SQLITE_OK );
17239   }
17240   p->dbOut = 0;
17241 }
17242 
17243 /*
17244 ** Decode and return an unsigned 16-bit big-endian integer value from
17245 ** buffer a[].
17246 */
recoverGetU16(const u8 * a)17247 static u32 recoverGetU16(const u8 *a){
17248   return (((u32)a[0])<<8) + ((u32)a[1]);
17249 }
17250 
17251 /*
17252 ** Decode and return an unsigned 32-bit big-endian integer value from
17253 ** buffer a[].
17254 */
recoverGetU32(const u8 * a)17255 static u32 recoverGetU32(const u8 *a){
17256   return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
17257 }
17258 
17259 /*
17260 ** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
17261 ** and return the number of bytes consumed.
17262 */
recoverGetVarint(const u8 * a,i64 * pVal)17263 static int recoverGetVarint(const u8 *a, i64 *pVal){
17264   sqlite3_uint64 u = 0;
17265   int i;
17266   for(i=0; i<8; i++){
17267     u = (u<<7) + (a[i]&0x7f);
17268     if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
17269   }
17270   u = (u<<8) + (a[i]&0xff);
17271   *pVal = (sqlite3_int64)u;
17272   return 9;
17273 }
17274 
17275 /*
17276 ** The second argument points to a buffer n bytes in size. If this buffer
17277 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
17278 ** return the page-size in bytes. Otherwise, if the buffer does not
17279 ** appear to contain a well-formed b-tree page, return 0.
17280 */
recoverIsValidPage(u8 * aTmp,const u8 * a,int n)17281 static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
17282   u8 *aUsed = aTmp;
17283   int nFrag = 0;
17284   int nActual = 0;
17285   int iFree = 0;
17286   int nCell = 0;                  /* Number of cells on page */
17287   int iCellOff = 0;               /* Offset of cell array in page */
17288   int iContent = 0;
17289   int eType = 0;
17290   int ii = 0;
17291 
17292   eType = (int)a[0];
17293   if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
17294 
17295   iFree = (int)recoverGetU16(&a[1]);
17296   nCell = (int)recoverGetU16(&a[3]);
17297   iContent = (int)recoverGetU16(&a[5]);
17298   if( iContent==0 ) iContent = 65536;
17299   nFrag = (int)a[7];
17300 
17301   if( iContent>n ) return 0;
17302 
17303   memset(aUsed, 0, n);
17304   memset(aUsed, 0xFF, iContent);
17305 
17306   /* Follow the free-list. This is the same format for all b-tree pages. */
17307   if( iFree && iFree<=iContent ) return 0;
17308   while( iFree ){
17309     int iNext = 0;
17310     int nByte = 0;
17311     if( iFree>(n-4) ) return 0;
17312     iNext = recoverGetU16(&a[iFree]);
17313     nByte = recoverGetU16(&a[iFree+2]);
17314     if( iFree+nByte>n || nByte<4 ) return 0;
17315     if( iNext && iNext<iFree+nByte ) return 0;
17316     memset(&aUsed[iFree], 0xFF, nByte);
17317     iFree = iNext;
17318   }
17319 
17320   /* Run through the cells */
17321   if( eType==0x02 || eType==0x05 ){
17322     iCellOff = 12;
17323   }else{
17324     iCellOff = 8;
17325   }
17326   if( (iCellOff + 2*nCell)>iContent ) return 0;
17327   for(ii=0; ii<nCell; ii++){
17328     int iByte;
17329     i64 nPayload = 0;
17330     int nByte = 0;
17331     int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
17332     if( iOff<iContent || iOff>n ){
17333       return 0;
17334     }
17335     if( eType==0x05 || eType==0x02 ) nByte += 4;
17336     nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
17337     if( eType==0x0D ){
17338       i64 dummy = 0;
17339       nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
17340     }
17341     if( eType!=0x05 ){
17342       int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
17343       int M = ((n-12)*32/255)-23;
17344       int K = M+((nPayload-M)%(n-4));
17345 
17346       if( nPayload<X ){
17347         nByte += nPayload;
17348       }else if( K<=X ){
17349         nByte += K+4;
17350       }else{
17351         nByte += M+4;
17352       }
17353     }
17354 
17355     if( iOff+nByte>n ){
17356       return 0;
17357     }
17358     for(iByte=iOff; iByte<(iOff+nByte); iByte++){
17359       if( aUsed[iByte]!=0 ){
17360         return 0;
17361       }
17362       aUsed[iByte] = 0xFF;
17363     }
17364   }
17365 
17366   nActual = 0;
17367   for(ii=0; ii<n; ii++){
17368     if( aUsed[ii]==0 ) nActual++;
17369   }
17370   return (nActual==nFrag);
17371 }
17372 
17373 
17374 static int recoverVfsClose(sqlite3_file*);
17375 static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
17376 static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
17377 static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
17378 static int recoverVfsSync(sqlite3_file*, int flags);
17379 static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
17380 static int recoverVfsLock(sqlite3_file*, int);
17381 static int recoverVfsUnlock(sqlite3_file*, int);
17382 static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
17383 static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
17384 static int recoverVfsSectorSize(sqlite3_file*);
17385 static int recoverVfsDeviceCharacteristics(sqlite3_file*);
17386 static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
17387 static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
17388 static void recoverVfsShmBarrier(sqlite3_file*);
17389 static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
17390 static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
17391 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
17392 
17393 static sqlite3_io_methods recover_methods = {
17394   2, /* iVersion */
17395   recoverVfsClose,
17396   recoverVfsRead,
17397   recoverVfsWrite,
17398   recoverVfsTruncate,
17399   recoverVfsSync,
17400   recoverVfsFileSize,
17401   recoverVfsLock,
17402   recoverVfsUnlock,
17403   recoverVfsCheckReservedLock,
17404   recoverVfsFileControl,
17405   recoverVfsSectorSize,
17406   recoverVfsDeviceCharacteristics,
17407   recoverVfsShmMap,
17408   recoverVfsShmLock,
17409   recoverVfsShmBarrier,
17410   recoverVfsShmUnmap,
17411   recoverVfsFetch,
17412   recoverVfsUnfetch
17413 };
17414 
recoverVfsClose(sqlite3_file * pFd)17415 static int recoverVfsClose(sqlite3_file *pFd){
17416   assert( pFd->pMethods!=&recover_methods );
17417   return pFd->pMethods->xClose(pFd);
17418 }
17419 
17420 /*
17421 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
17422 */
recoverPutU16(u8 * a,u32 v)17423 static void recoverPutU16(u8 *a, u32 v){
17424   a[0] = (v>>8) & 0x00FF;
17425   a[1] = (v>>0) & 0x00FF;
17426 }
17427 
17428 /*
17429 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
17430 */
recoverPutU32(u8 * a,u32 v)17431 static void recoverPutU32(u8 *a, u32 v){
17432   a[0] = (v>>24) & 0x00FF;
17433   a[1] = (v>>16) & 0x00FF;
17434   a[2] = (v>>8) & 0x00FF;
17435   a[3] = (v>>0) & 0x00FF;
17436 }
17437 
17438 /*
17439 ** Detect the page-size of the database opened by file-handle pFd by
17440 ** searching the first part of the file for a well-formed SQLite b-tree
17441 ** page. If parameter nReserve is non-zero, then as well as searching for
17442 ** a b-tree page with zero reserved bytes, this function searches for one
17443 ** with nReserve reserved bytes at the end of it.
17444 **
17445 ** If successful, set variable p->detected_pgsz to the detected page-size
17446 ** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
17447 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
17448 ** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
17449 ** is returned. The final value of p->detected_pgsz is undefined in this
17450 ** case.
17451 */
recoverVfsDetectPagesize(sqlite3_recover * p,sqlite3_file * pFd,u32 nReserve,i64 nSz)17452 static int recoverVfsDetectPagesize(
17453   sqlite3_recover *p,             /* Recover handle */
17454   sqlite3_file *pFd,              /* File-handle open on input database */
17455   u32 nReserve,                   /* Possible nReserve value */
17456   i64 nSz                         /* Size of database file in bytes */
17457 ){
17458   int rc = SQLITE_OK;
17459   const int nMin = 512;
17460   const int nMax = 65536;
17461   const int nMaxBlk = 4;
17462   u32 pgsz = 0;
17463   int iBlk = 0;
17464   u8 *aPg = 0;
17465   u8 *aTmp = 0;
17466   int nBlk = 0;
17467 
17468   aPg = (u8*)sqlite3_malloc(2*nMax);
17469   if( aPg==0 ) return SQLITE_NOMEM;
17470   aTmp = &aPg[nMax];
17471 
17472   nBlk = (nSz+nMax-1)/nMax;
17473   if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
17474 
17475   do {
17476     for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
17477       int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
17478       memset(aPg, 0, nMax);
17479       rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
17480       if( rc==SQLITE_OK ){
17481         int pgsz2;
17482         for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
17483           int iOff;
17484           for(iOff=0; iOff<nMax; iOff+=pgsz2){
17485             if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
17486               pgsz = pgsz2;
17487               break;
17488             }
17489           }
17490         }
17491       }
17492     }
17493     if( pgsz>(u32)p->detected_pgsz ){
17494       p->detected_pgsz = pgsz;
17495       p->nReserve = nReserve;
17496     }
17497     if( nReserve==0 ) break;
17498     nReserve = 0;
17499   }while( 1 );
17500 
17501   p->detected_pgsz = pgsz;
17502   sqlite3_free(aPg);
17503   return rc;
17504 }
17505 
17506 /*
17507 ** The xRead() method of the wrapper VFS. This is used to intercept calls
17508 ** to read page 1 of the input database.
17509 */
recoverVfsRead(sqlite3_file * pFd,void * aBuf,int nByte,i64 iOff)17510 static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
17511   int rc = SQLITE_OK;
17512   if( pFd->pMethods==&recover_methods ){
17513     pFd->pMethods = recover_g.pMethods;
17514     rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
17515     if( nByte==16 ){
17516       sqlite3_randomness(16, aBuf);
17517     }else
17518     if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
17519       /* Ensure that the database has a valid header file. The only fields
17520       ** that really matter to recovery are:
17521       **
17522       **   + Database page size (16-bits at offset 16)
17523       **   + Size of db in pages (32-bits at offset 28)
17524       **   + Database encoding (32-bits at offset 56)
17525       **
17526       ** Also preserved are:
17527       **
17528       **   + first freelist page (32-bits at offset 32)
17529       **   + size of freelist (32-bits at offset 36)
17530       **   + the wal-mode flags (16-bits at offset 18)
17531       **
17532       ** We also try to preserve the auto-vacuum, incr-value, user-version
17533       ** and application-id fields - all 32 bit quantities at offsets
17534       ** 52, 60, 64 and 68. All other fields are set to known good values.
17535       **
17536       ** Byte offset 105 should also contain the page-size as a 16-bit
17537       ** integer.
17538       */
17539       const int aPreserve[] = {32, 36, 52, 60, 64, 68};
17540       u8 aHdr[108] = {
17541         0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
17542         0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
17543         0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
17544         0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
17545         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17546         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
17547         0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
17548         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17549         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17550         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17551         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17552         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17553         0x00, 0x2e, 0x5b, 0x30,
17554 
17555         0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
17556       };
17557       u8 *a = (u8*)aBuf;
17558 
17559       u32 pgsz = recoverGetU16(&a[16]);
17560       u32 nReserve = a[20];
17561       u32 enc = recoverGetU32(&a[56]);
17562       u32 dbsz = 0;
17563       i64 dbFileSize = 0;
17564       int ii;
17565       sqlite3_recover *p = recover_g.p;
17566 
17567       if( pgsz==0x01 ) pgsz = 65536;
17568       rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
17569 
17570       if( rc==SQLITE_OK && p->detected_pgsz==0 ){
17571         rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
17572       }
17573       if( p->detected_pgsz ){
17574         pgsz = p->detected_pgsz;
17575         nReserve = p->nReserve;
17576       }
17577 
17578       if( pgsz ){
17579         dbsz = dbFileSize / pgsz;
17580       }
17581       if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
17582         enc = SQLITE_UTF8;
17583       }
17584 
17585       sqlite3_free(p->pPage1Cache);
17586       p->pPage1Cache = 0;
17587       p->pPage1Disk = 0;
17588 
17589       p->pgsz = nByte;
17590       p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
17591       if( p->pPage1Cache ){
17592         p->pPage1Disk = &p->pPage1Cache[nByte];
17593         memcpy(p->pPage1Disk, aBuf, nByte);
17594         aHdr[18] = a[18];
17595         aHdr[19] = a[19];
17596         recoverPutU32(&aHdr[28], dbsz);
17597         recoverPutU32(&aHdr[56], enc);
17598         recoverPutU16(&aHdr[105], pgsz-nReserve);
17599         if( pgsz==65536 ) pgsz = 1;
17600         recoverPutU16(&aHdr[16], pgsz);
17601         aHdr[20] = nReserve;
17602         for(ii=0; ii<(int)(sizeof(aPreserve)/sizeof(aPreserve[0])); ii++){
17603           memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
17604         }
17605         memcpy(aBuf, aHdr, sizeof(aHdr));
17606         memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
17607 
17608         memcpy(p->pPage1Cache, aBuf, nByte);
17609       }else{
17610         rc = p->errCode;
17611       }
17612 
17613     }
17614     pFd->pMethods = &recover_methods;
17615   }else{
17616     rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
17617   }
17618   return rc;
17619 }
17620 
17621 /*
17622 ** Used to make sqlite3_io_methods wrapper methods less verbose.
17623 */
17624 #define RECOVER_VFS_WRAPPER(code)                         \
17625   int rc = SQLITE_OK;                                     \
17626   if( pFd->pMethods==&recover_methods ){                  \
17627     pFd->pMethods = recover_g.pMethods;                   \
17628     rc = code;                                            \
17629     pFd->pMethods = &recover_methods;                     \
17630   }else{                                                  \
17631     rc = code;                                            \
17632   }                                                       \
17633   return rc;
17634 
17635 /*
17636 ** Methods of the wrapper VFS. All methods except for xRead() and xClose()
17637 ** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
17638 ** method on the lower level VFS, then reinstall the wrapper before returning.
17639 ** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
17640 */
recoverVfsWrite(sqlite3_file * pFd,const void * aBuf,int nByte,i64 iOff)17641 static int recoverVfsWrite(
17642   sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
17643 ){
17644   RECOVER_VFS_WRAPPER (
17645       pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
17646   );
17647 }
recoverVfsTruncate(sqlite3_file * pFd,sqlite3_int64 size)17648 static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
17649   RECOVER_VFS_WRAPPER (
17650       pFd->pMethods->xTruncate(pFd, size)
17651   );
17652 }
recoverVfsSync(sqlite3_file * pFd,int flags)17653 static int recoverVfsSync(sqlite3_file *pFd, int flags){
17654   RECOVER_VFS_WRAPPER (
17655       pFd->pMethods->xSync(pFd, flags)
17656   );
17657 }
recoverVfsFileSize(sqlite3_file * pFd,sqlite3_int64 * pSize)17658 static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
17659   RECOVER_VFS_WRAPPER (
17660       pFd->pMethods->xFileSize(pFd, pSize)
17661   );
17662 }
recoverVfsLock(sqlite3_file * pFd,int eLock)17663 static int recoverVfsLock(sqlite3_file *pFd, int eLock){
17664   RECOVER_VFS_WRAPPER (
17665       pFd->pMethods->xLock(pFd, eLock)
17666   );
17667 }
recoverVfsUnlock(sqlite3_file * pFd,int eLock)17668 static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
17669   RECOVER_VFS_WRAPPER (
17670       pFd->pMethods->xUnlock(pFd, eLock)
17671   );
17672 }
recoverVfsCheckReservedLock(sqlite3_file * pFd,int * pResOut)17673 static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
17674   RECOVER_VFS_WRAPPER (
17675       pFd->pMethods->xCheckReservedLock(pFd, pResOut)
17676   );
17677 }
recoverVfsFileControl(sqlite3_file * pFd,int op,void * pArg)17678 static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
17679   RECOVER_VFS_WRAPPER (
17680     (pFd->pMethods ?  pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
17681   );
17682 }
recoverVfsSectorSize(sqlite3_file * pFd)17683 static int recoverVfsSectorSize(sqlite3_file *pFd){
17684   RECOVER_VFS_WRAPPER (
17685       pFd->pMethods->xSectorSize(pFd)
17686   );
17687 }
recoverVfsDeviceCharacteristics(sqlite3_file * pFd)17688 static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
17689   RECOVER_VFS_WRAPPER (
17690       pFd->pMethods->xDeviceCharacteristics(pFd)
17691   );
17692 }
recoverVfsShmMap(sqlite3_file * pFd,int iPg,int pgsz,int bExtend,void volatile ** pp)17693 static int recoverVfsShmMap(
17694   sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
17695 ){
17696   RECOVER_VFS_WRAPPER (
17697       pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
17698   );
17699 }
recoverVfsShmLock(sqlite3_file * pFd,int offset,int n,int flags)17700 static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
17701   RECOVER_VFS_WRAPPER (
17702       pFd->pMethods->xShmLock(pFd, offset, n, flags)
17703   );
17704 }
recoverVfsShmBarrier(sqlite3_file * pFd)17705 static void recoverVfsShmBarrier(sqlite3_file *pFd){
17706   if( pFd->pMethods==&recover_methods ){
17707     pFd->pMethods = recover_g.pMethods;
17708     pFd->pMethods->xShmBarrier(pFd);
17709     pFd->pMethods = &recover_methods;
17710   }else{
17711     pFd->pMethods->xShmBarrier(pFd);
17712   }
17713 }
recoverVfsShmUnmap(sqlite3_file * pFd,int deleteFlag)17714 static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
17715   RECOVER_VFS_WRAPPER (
17716       pFd->pMethods->xShmUnmap(pFd, deleteFlag)
17717   );
17718 }
17719 
recoverVfsFetch(sqlite3_file * pFd,sqlite3_int64 iOff,int iAmt,void ** pp)17720 static int recoverVfsFetch(
17721   sqlite3_file *pFd,
17722   sqlite3_int64 iOff,
17723   int iAmt,
17724   void **pp
17725 ){
17726   (void)pFd;
17727   (void)iOff;
17728   (void)iAmt;
17729   *pp = 0;
17730   return SQLITE_OK;
17731 }
recoverVfsUnfetch(sqlite3_file * pFd,sqlite3_int64 iOff,void * p)17732 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
17733   (void)pFd;
17734   (void)iOff;
17735   (void)p;
17736   return SQLITE_OK;
17737 }
17738 
17739 /*
17740 ** Install the VFS wrapper around the file-descriptor open on the input
17741 ** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
17742 ** when this function is called.
17743 */
recoverInstallWrapper(sqlite3_recover * p)17744 static void recoverInstallWrapper(sqlite3_recover *p){
17745   sqlite3_file *pFd = 0;
17746   assert( recover_g.pMethods==0 );
17747   recoverAssertMutexHeld();
17748   sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
17749   assert( pFd==0 || pFd->pMethods!=&recover_methods );
17750   if( pFd && pFd->pMethods ){
17751     int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
17752     recover_g.pMethods = pFd->pMethods;
17753     recover_g.p = p;
17754     recover_methods.iVersion = iVersion;
17755     pFd->pMethods = &recover_methods;
17756   }
17757 }
17758 
17759 /*
17760 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
17761 ** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
17762 ** held when this function is called.
17763 */
recoverUninstallWrapper(sqlite3_recover * p)17764 static void recoverUninstallWrapper(sqlite3_recover *p){
17765   sqlite3_file *pFd = 0;
17766   recoverAssertMutexHeld();
17767   sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
17768   if( pFd && pFd->pMethods ){
17769     pFd->pMethods = recover_g.pMethods;
17770     recover_g.pMethods = 0;
17771     recover_g.p = 0;
17772   }
17773 }
17774 
17775 /*
17776 ** This function does the work of a single sqlite3_recover_step() call. It
17777 ** is guaranteed that the handle is not in an error state when this
17778 ** function is called.
17779 */
recoverStep(sqlite3_recover * p)17780 static void recoverStep(sqlite3_recover *p){
17781   assert( p && p->errCode==SQLITE_OK );
17782   switch( p->eState ){
17783     case RECOVER_STATE_INIT:
17784       /* This is the very first call to sqlite3_recover_step() on this object.
17785       */
17786       recoverSqlCallback(p, "BEGIN");
17787       recoverSqlCallback(p, "PRAGMA writable_schema = on");
17788 
17789       recoverEnterMutex();
17790       recoverInstallWrapper(p);
17791 
17792       /* Open the output database. And register required virtual tables and
17793       ** user functions with the new handle. */
17794       recoverOpenOutput(p);
17795 
17796       /* Open transactions on both the input and output databases. */
17797       sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
17798       recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
17799       recoverExec(p, p->dbIn, "BEGIN");
17800       if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
17801       recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
17802       recoverTransferSettings(p);
17803       recoverOpenRecovery(p);
17804       recoverCacheSchema(p);
17805 
17806       recoverUninstallWrapper(p);
17807       recoverLeaveMutex();
17808 
17809       recoverExec(p, p->dbOut, "BEGIN");
17810 
17811       recoverWriteSchema1(p);
17812       p->eState = RECOVER_STATE_WRITING;
17813       break;
17814 
17815     case RECOVER_STATE_WRITING: {
17816       if( p->w1.pTbls==0 ){
17817         recoverWriteDataInit(p);
17818       }
17819       if( SQLITE_DONE==recoverWriteDataStep(p) ){
17820         recoverWriteDataCleanup(p);
17821         if( p->zLostAndFound ){
17822           p->eState = RECOVER_STATE_LOSTANDFOUND1;
17823         }else{
17824           p->eState = RECOVER_STATE_SCHEMA2;
17825         }
17826       }
17827       break;
17828     }
17829 
17830     case RECOVER_STATE_LOSTANDFOUND1: {
17831       if( p->laf.pUsed==0 ){
17832         recoverLostAndFound1Init(p);
17833       }
17834       if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
17835         p->eState = RECOVER_STATE_LOSTANDFOUND2;
17836       }
17837       break;
17838     }
17839     case RECOVER_STATE_LOSTANDFOUND2: {
17840       if( p->laf.pAllAndParent==0 ){
17841         recoverLostAndFound2Init(p);
17842       }
17843       if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
17844         p->eState = RECOVER_STATE_LOSTANDFOUND3;
17845       }
17846       break;
17847     }
17848 
17849     case RECOVER_STATE_LOSTANDFOUND3: {
17850       if( p->laf.pInsert==0 ){
17851         recoverLostAndFound3Init(p);
17852       }
17853       if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
17854         p->eState = RECOVER_STATE_SCHEMA2;
17855       }
17856       break;
17857     }
17858 
17859     case RECOVER_STATE_SCHEMA2: {
17860       int rc = SQLITE_OK;
17861 
17862       recoverWriteSchema2(p);
17863       p->eState = RECOVER_STATE_DONE;
17864 
17865       /* If no error has occurred, commit the write transaction on the output
17866       ** database. Regardless of whether or not an error has occurred, make
17867       ** an attempt to end the read transaction on the input database.  */
17868       recoverExec(p, p->dbOut, "COMMIT");
17869       rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
17870       if( p->errCode==SQLITE_OK ) p->errCode = rc;
17871 
17872       recoverSqlCallback(p, "PRAGMA writable_schema = off");
17873       recoverSqlCallback(p, "COMMIT");
17874       p->eState = RECOVER_STATE_DONE;
17875       recoverFinalCleanup(p);
17876       break;
17877     };
17878 
17879     case RECOVER_STATE_DONE: {
17880       /* no-op */
17881       break;
17882     };
17883   }
17884 }
17885 
17886 
17887 /*
17888 ** This is a worker function that does the heavy lifting for both init
17889 ** functions:
17890 **
17891 **     sqlite3_recover_init()
17892 **     sqlite3_recover_init_sql()
17893 **
17894 ** All this function does is allocate space for the recover handle and
17895 ** take copies of the input parameters. All the real work is done within
17896 ** sqlite3_recover_run().
17897 */
recoverInit(sqlite3 * db,const char * zDb,const char * zUri,int (* xSql)(void *,const char *),void * pSqlCtx)17898 sqlite3_recover *recoverInit(
17899   sqlite3* db,
17900   const char *zDb,
17901   const char *zUri,               /* Output URI for _recover_init() */
17902   int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
17903   void *pSqlCtx                   /* Context arg for _recover_init_sql() */
17904 ){
17905   sqlite3_recover *pRet = 0;
17906   int nDb = 0;
17907   int nUri = 0;
17908   int nByte = 0;
17909 
17910   if( zDb==0 ){ zDb = "main"; }
17911 
17912   nDb = recoverStrlen(zDb);
17913   nUri = recoverStrlen(zUri);
17914 
17915   nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
17916   pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
17917   if( pRet ){
17918     memset(pRet, 0, nByte);
17919     pRet->dbIn = db;
17920     pRet->zDb = (char*)&pRet[1];
17921     pRet->zUri = &pRet->zDb[nDb+1];
17922     memcpy(pRet->zDb, zDb, nDb);
17923     if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
17924     pRet->xSql = xSql;
17925     pRet->pSqlCtx = pSqlCtx;
17926     pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
17927   }
17928 
17929   return pRet;
17930 }
17931 
17932 /*
17933 ** Initialize a recovery handle that creates a new database containing
17934 ** the recovered data.
17935 */
sqlite3_recover_init(sqlite3 * db,const char * zDb,const char * zUri)17936 sqlite3_recover *sqlite3_recover_init(
17937   sqlite3* db,
17938   const char *zDb,
17939   const char *zUri
17940 ){
17941   return recoverInit(db, zDb, zUri, 0, 0);
17942 }
17943 
17944 /*
17945 ** Initialize a recovery handle that returns recovered data in the
17946 ** form of SQL statements via a callback.
17947 */
sqlite3_recover_init_sql(sqlite3 * db,const char * zDb,int (* xSql)(void *,const char *),void * pSqlCtx)17948 sqlite3_recover *sqlite3_recover_init_sql(
17949   sqlite3* db,
17950   const char *zDb,
17951   int (*xSql)(void*, const char*),
17952   void *pSqlCtx
17953 ){
17954   return recoverInit(db, zDb, 0, xSql, pSqlCtx);
17955 }
17956 
17957 /*
17958 ** Return the handle error message, if any.
17959 */
sqlite3_recover_errmsg(sqlite3_recover * p)17960 const char *sqlite3_recover_errmsg(sqlite3_recover *p){
17961   return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
17962 }
17963 
17964 /*
17965 ** Return the handle error code.
17966 */
sqlite3_recover_errcode(sqlite3_recover * p)17967 int sqlite3_recover_errcode(sqlite3_recover *p){
17968   return p ? p->errCode : SQLITE_NOMEM;
17969 }
17970 
17971 /*
17972 ** Configure the handle.
17973 */
sqlite3_recover_config(sqlite3_recover * p,int op,void * pArg)17974 int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
17975   int rc = SQLITE_OK;
17976   if( p==0 ){
17977     rc = SQLITE_NOMEM;
17978   }else if( p->eState!=RECOVER_STATE_INIT ){
17979     rc = SQLITE_MISUSE;
17980   }else{
17981     switch( op ){
17982       case 789:
17983         /* This undocumented magic configuration option is used to set the
17984         ** name of the auxiliary database that is ATTACH-ed to the database
17985         ** connection and used to hold state information during the
17986         ** recovery process.  This option is for debugging use only and
17987         ** is subject to change or removal at any time. */
17988         sqlite3_free(p->zStateDb);
17989         p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
17990         break;
17991 
17992       case SQLITE_RECOVER_LOST_AND_FOUND: {
17993         const char *zArg = (const char*)pArg;
17994         sqlite3_free(p->zLostAndFound);
17995         if( zArg ){
17996           p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
17997         }else{
17998           p->zLostAndFound = 0;
17999         }
18000         break;
18001       }
18002 
18003       case SQLITE_RECOVER_FREELIST_CORRUPT:
18004         p->bFreelistCorrupt = *(int*)pArg;
18005         break;
18006 
18007       case SQLITE_RECOVER_ROWIDS:
18008         p->bRecoverRowid = *(int*)pArg;
18009         break;
18010 
18011       case SQLITE_RECOVER_SLOWINDEXES:
18012         p->bSlowIndexes = *(int*)pArg;
18013         break;
18014 
18015       default:
18016         rc = SQLITE_NOTFOUND;
18017         break;
18018     }
18019   }
18020 
18021   return rc;
18022 }
18023 
18024 /*
18025 ** Do a unit of work towards the recovery job. Return SQLITE_OK if
18026 ** no error has occurred but database recovery is not finished, SQLITE_DONE
18027 ** if database recovery has been successfully completed, or an SQLite
18028 ** error code if an error has occurred.
18029 */
sqlite3_recover_step(sqlite3_recover * p)18030 int sqlite3_recover_step(sqlite3_recover *p){
18031   if( p==0 ) return SQLITE_NOMEM;
18032   if( p->errCode==SQLITE_OK ) recoverStep(p);
18033   if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
18034     return SQLITE_DONE;
18035   }
18036   return p->errCode;
18037 }
18038 
18039 /*
18040 ** Do the configured recovery operation. Return SQLITE_OK if successful, or
18041 ** else an SQLite error code.
18042 */
sqlite3_recover_run(sqlite3_recover * p)18043 int sqlite3_recover_run(sqlite3_recover *p){
18044   while( SQLITE_OK==sqlite3_recover_step(p) );
18045   return sqlite3_recover_errcode(p);
18046 }
18047 
18048 
18049 /*
18050 ** Free all resources associated with the recover handle passed as the only
18051 ** argument. The results of using a handle with any sqlite3_recover_**
18052 ** API function after it has been passed to this function are undefined.
18053 **
18054 ** A copy of the value returned by the first call made to sqlite3_recover_run()
18055 ** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
18056 ** not been called on this handle.
18057 */
sqlite3_recover_finish(sqlite3_recover * p)18058 int sqlite3_recover_finish(sqlite3_recover *p){
18059   int rc;
18060   if( p==0 ){
18061     rc = SQLITE_NOMEM;
18062   }else{
18063     recoverFinalCleanup(p);
18064     if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
18065       rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0);
18066       if( p->errCode==SQLITE_OK ) p->errCode = rc;
18067     }
18068     rc = p->errCode;
18069     sqlite3_free(p->zErrMsg);
18070     sqlite3_free(p->zStateDb);
18071     sqlite3_free(p->zLostAndFound);
18072     sqlite3_free(p->pPage1Cache);
18073     sqlite3_free(p);
18074   }
18075   return rc;
18076 }
18077 
18078 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
18079 
18080 /************************* End ../ext/recover/sqlite3recover.c ********************/
18081 # endif /* SQLITE_HAVE_SQLITE3R */
18082 #endif
18083 #ifdef SQLITE_SHELL_EXTSRC
18084 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
18085 #endif
18086 
18087 #if defined(SQLITE_ENABLE_SESSION)
18088 /*
18089 ** State information for a single open session
18090 */
18091 typedef struct OpenSession OpenSession;
18092 struct OpenSession {
18093   char *zName;             /* Symbolic name for this session */
18094   int nFilter;             /* Number of xFilter rejection GLOB patterns */
18095   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
18096   sqlite3_session *p;      /* The open session */
18097 };
18098 #endif
18099 
18100 typedef struct ExpertInfo ExpertInfo;
18101 struct ExpertInfo {
18102   sqlite3expert *pExpert;
18103   int bVerbose;
18104 };
18105 
18106 /* A single line in the EQP output */
18107 typedef struct EQPGraphRow EQPGraphRow;
18108 struct EQPGraphRow {
18109   int iEqpId;           /* ID for this row */
18110   int iParentId;        /* ID of the parent row */
18111   EQPGraphRow *pNext;   /* Next row in sequence */
18112   char zText[1];        /* Text to display for this row */
18113 };
18114 
18115 /* All EQP output is collected into an instance of the following */
18116 typedef struct EQPGraph EQPGraph;
18117 struct EQPGraph {
18118   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
18119   EQPGraphRow *pLast;   /* Last element of the pRow list */
18120   char zPrefix[100];    /* Graph prefix */
18121 };
18122 
18123 /* Parameters affecting columnar mode result display (defaulting together) */
18124 typedef struct ColModeOpts {
18125   int iWrap;            /* In columnar modes, wrap lines reaching this limit */
18126   u8 bQuote;            /* Quote results for .mode box and table */
18127   u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
18128 } ColModeOpts;
18129 #define ColModeOpts_default { 60, 0, 0 }
18130 #define ColModeOpts_default_qbox { 60, 1, 0 }
18131 
18132 /*
18133 ** State information about the database connection is contained in an
18134 ** instance of the following structure.
18135 */
18136 typedef struct ShellState ShellState;
18137 struct ShellState {
18138   sqlite3 *db;           /* The database */
18139   u8 autoExplain;        /* Automatically turn on .explain mode */
18140   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
18141   u8 autoEQPtest;        /* autoEQP is in test mode */
18142   u8 autoEQPtrace;       /* autoEQP is in trace mode */
18143   u8 scanstatsOn;        /* True to display scan stats before each finalize */
18144   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
18145   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
18146   u8 nEqpLevel;          /* Depth of the EQP output graph */
18147   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
18148   u8 bSafeMode;          /* True to prohibit unsafe operations */
18149   u8 bSafeModePersist;   /* The long-term value of bSafeMode */
18150   u8 eRestoreState;      /* See comments above doAutoDetectRestore() */
18151   ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
18152   unsigned statsOn;      /* True to display memory stats before each finalize */
18153   unsigned mEqpLines;    /* Mask of vertical lines in the EQP output graph */
18154   int inputNesting;      /* Track nesting level of .read and other redirects */
18155   int outCount;          /* Revert to stdout when reaching zero */
18156   int cnt;               /* Number of records displayed so far */
18157   int lineno;            /* Line number of last line read from in */
18158   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
18159   FILE *in;              /* Read commands from this stream */
18160   FILE *out;             /* Write results here */
18161   FILE *traceOut;        /* Output for sqlite3_trace() */
18162   int nErr;              /* Number of errors seen */
18163   int mode;              /* An output mode setting */
18164   int modePrior;         /* Saved mode */
18165   int cMode;             /* temporary output mode for the current query */
18166   int normalMode;        /* Output mode before ".explain on" */
18167   int writableSchema;    /* True if PRAGMA writable_schema=ON */
18168   int showHeader;        /* True to show column names in List or Column mode */
18169   int nCheck;            /* Number of ".check" commands run */
18170   unsigned nProgress;    /* Number of progress callbacks encountered */
18171   unsigned mxProgress;   /* Maximum progress callbacks before failing */
18172   unsigned flgProgress;  /* Flags for the progress callback */
18173   unsigned shellFlgs;    /* Various flags */
18174   unsigned priorShFlgs;  /* Saved copy of flags */
18175   sqlite3_int64 szMax;   /* --maxsize argument to .open */
18176   char *zDestTable;      /* Name of destination table when MODE_Insert */
18177   char *zTempFile;       /* Temporary file that might need deleting */
18178   char zTestcase[30];    /* Name of current test case */
18179   char colSeparator[20]; /* Column separator character for several modes */
18180   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
18181   char colSepPrior[20];  /* Saved column separator */
18182   char rowSepPrior[20];  /* Saved row separator */
18183   int *colWidth;         /* Requested width of each column in columnar modes */
18184   int *actualWidth;      /* Actual width of each column */
18185   int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
18186   char nullValue[20];    /* The text to print when a NULL comes back from
18187                          ** the database */
18188   char outfile[FILENAME_MAX]; /* Filename for *out */
18189   sqlite3_stmt *pStmt;   /* Current statement if any. */
18190   FILE *pLog;            /* Write log output here */
18191   struct AuxDb {         /* Storage space for auxiliary database connections */
18192     sqlite3 *db;               /* Connection pointer */
18193     const char *zDbFilename;   /* Filename used to open the connection */
18194     char *zFreeOnClose;        /* Free this memory allocation on close */
18195 #if defined(SQLITE_ENABLE_SESSION)
18196     int nSession;              /* Number of active sessions */
18197     OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
18198 #endif
18199   } aAuxDb[5],           /* Array of all database connections */
18200     *pAuxDb;             /* Currently active database connection */
18201   int *aiIndent;         /* Array of indents used in MODE_Explain */
18202   int nIndent;           /* Size of array aiIndent[] */
18203   int iIndent;           /* Index of current op in aiIndent[] */
18204   char *zNonce;          /* Nonce for temporary safe-mode escapes */
18205   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
18206   ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
18207 #ifdef SQLITE_SHELL_FIDDLE
18208   struct {
18209     const char * zInput; /* Input string from wasm/JS proxy */
18210     const char * zPos;   /* Cursor pos into zInput */
18211     const char * zDefaultDbName; /* Default name for db file */
18212   } wasm;
18213 #endif
18214 };
18215 
18216 #ifdef SQLITE_SHELL_FIDDLE
18217 static ShellState shellState;
18218 #endif
18219 
18220 
18221 /* Allowed values for ShellState.autoEQP
18222 */
18223 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
18224 #define AUTOEQP_on       1           /* Automatic EQP is on */
18225 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
18226 #define AUTOEQP_full     3           /* Show full EXPLAIN */
18227 
18228 /* Allowed values for ShellState.openMode
18229 */
18230 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
18231 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
18232 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
18233 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
18234 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
18235 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
18236 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
18237 
18238 /* Allowed values for ShellState.eTraceType
18239 */
18240 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
18241 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
18242 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
18243 
18244 /* Bits in the ShellState.flgProgress variable */
18245 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
18246 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progress
18247                                    ** callback limit is reached, and for each
18248                                    ** top-level SQL statement */
18249 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
18250 
18251 /*
18252 ** These are the allowed shellFlgs values
18253 */
18254 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
18255 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
18256 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
18257 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
18258 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
18259 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
18260 #define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
18261 #define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
18262 #define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
18263 #define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
18264 #define SHFLG_TestingMode    0x00000400 /* allow unsafe testing features */
18265 
18266 /*
18267 ** Macros for testing and setting shellFlgs
18268 */
18269 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
18270 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
18271 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
18272 
18273 /*
18274 ** These are the allowed modes.
18275 */
18276 #define MODE_Line     0  /* One column per line.  Blank line between records */
18277 #define MODE_Column   1  /* One record per line in neat columns */
18278 #define MODE_List     2  /* One record per line with a separator */
18279 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
18280 #define MODE_Html     4  /* Generate an XHTML table */
18281 #define MODE_Insert   5  /* Generate SQL "insert" statements */
18282 #define MODE_Quote    6  /* Quote values as for SQL */
18283 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
18284 #define MODE_Csv      8  /* Quote strings, numbers are plain */
18285 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
18286 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
18287 #define MODE_Pretty  11  /* Pretty-print schemas */
18288 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
18289 #define MODE_Json    13  /* Output JSON */
18290 #define MODE_Markdown 14 /* Markdown formatting */
18291 #define MODE_Table   15  /* MySQL-style table formatting */
18292 #define MODE_Box     16  /* Unicode box-drawing characters */
18293 #define MODE_Count   17  /* Output only a count of the rows of output */
18294 #define MODE_Off     18  /* No query output shown */
18295 #define MODE_ScanExp 19  /* Like MODE_Explain, but for ".scanstats vm" */
18296 
18297 static const char *modeDescr[] = {
18298   "line",
18299   "column",
18300   "list",
18301   "semi",
18302   "html",
18303   "insert",
18304   "quote",
18305   "tcl",
18306   "csv",
18307   "explain",
18308   "ascii",
18309   "prettyprint",
18310   "eqp",
18311   "json",
18312   "markdown",
18313   "table",
18314   "box",
18315   "count",
18316   "off"
18317 };
18318 
18319 /*
18320 ** These are the column/row/line separators used by the various
18321 ** import/export modes.
18322 */
18323 #define SEP_Column    "|"
18324 #define SEP_Row       "\n"
18325 #define SEP_Tab       "\t"
18326 #define SEP_Space     " "
18327 #define SEP_Comma     ","
18328 #define SEP_CrLf      "\r\n"
18329 #define SEP_Unit      "\x1F"
18330 #define SEP_Record    "\x1E"
18331 
18332 /*
18333 ** Limit input nesting via .read or any other input redirect.
18334 ** It's not too expensive, so a generous allowance can be made.
18335 */
18336 #define MAX_INPUT_NESTING 25
18337 
18338 /*
18339 ** A callback for the sqlite3_log() interface.
18340 */
shellLog(void * pArg,int iErrCode,const char * zMsg)18341 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
18342   ShellState *p = (ShellState*)pArg;
18343   if( p->pLog==0 ) return;
18344   sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
18345   fflush(p->pLog);
18346 }
18347 
18348 /*
18349 ** SQL function:  shell_putsnl(X)
18350 **
18351 ** Write the text X to the screen (or whatever output is being directed)
18352 ** adding a newline at the end, and then return X.
18353 */
shellPutsFunc(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)18354 static void shellPutsFunc(
18355   sqlite3_context *pCtx,
18356   int nVal,
18357   sqlite3_value **apVal
18358 ){
18359   /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
18360   (void)nVal;
18361   oputf("%s\n", sqlite3_value_text(apVal[0]));
18362   sqlite3_result_value(pCtx, apVal[0]);
18363 }
18364 
18365 /*
18366 ** If in safe mode, print an error message described by the arguments
18367 ** and exit immediately.
18368 */
failIfSafeMode(ShellState * p,const char * zErrMsg,...)18369 static void failIfSafeMode(
18370   ShellState *p,
18371   const char *zErrMsg,
18372   ...
18373 ){
18374   if( p->bSafeMode ){
18375     va_list ap;
18376     char *zMsg;
18377     va_start(ap, zErrMsg);
18378     zMsg = sqlite3_vmprintf(zErrMsg, ap);
18379     va_end(ap);
18380     eputf("line %d: %s\n", p->lineno, zMsg);
18381     exit(1);
18382   }
18383 }
18384 
18385 /*
18386 ** SQL function:   edit(VALUE)
18387 **                 edit(VALUE,EDITOR)
18388 **
18389 ** These steps:
18390 **
18391 **     (1) Write VALUE into a temporary file.
18392 **     (2) Run program EDITOR on that temporary file.
18393 **     (3) Read the temporary file back and return its content as the result.
18394 **     (4) Delete the temporary file
18395 **
18396 ** If the EDITOR argument is omitted, use the value in the VISUAL
18397 ** environment variable.  If still there is no EDITOR, through an error.
18398 **
18399 ** Also throw an error if the EDITOR program returns a non-zero exit code.
18400 */
18401 #ifndef SQLITE_NOHAVE_SYSTEM
editFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)18402 static void editFunc(
18403   sqlite3_context *context,
18404   int argc,
18405   sqlite3_value **argv
18406 ){
18407   const char *zEditor;
18408   char *zTempFile = 0;
18409   sqlite3 *db;
18410   char *zCmd = 0;
18411   int bBin;
18412   int rc;
18413   int hasCRNL = 0;
18414   FILE *f = 0;
18415   sqlite3_int64 sz;
18416   sqlite3_int64 x;
18417   unsigned char *p = 0;
18418 
18419   if( argc==2 ){
18420     zEditor = (const char*)sqlite3_value_text(argv[1]);
18421   }else{
18422     zEditor = getenv("VISUAL");
18423   }
18424   if( zEditor==0 ){
18425     sqlite3_result_error(context, "no editor for edit()", -1);
18426     return;
18427   }
18428   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
18429     sqlite3_result_error(context, "NULL input to edit()", -1);
18430     return;
18431   }
18432   db = sqlite3_context_db_handle(context);
18433   zTempFile = 0;
18434   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
18435   if( zTempFile==0 ){
18436     sqlite3_uint64 r = 0;
18437     sqlite3_randomness(sizeof(r), &r);
18438     zTempFile = sqlite3_mprintf("temp%llx", r);
18439     if( zTempFile==0 ){
18440       sqlite3_result_error_nomem(context);
18441       return;
18442     }
18443   }
18444   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
18445   /* When writing the file to be edited, do \n to \r\n conversions on systems
18446   ** that want \r\n line endings */
18447   f = fopen(zTempFile, bBin ? "wb" : "w");
18448   if( f==0 ){
18449     sqlite3_result_error(context, "edit() cannot open temp file", -1);
18450     goto edit_func_end;
18451   }
18452   sz = sqlite3_value_bytes(argv[0]);
18453   if( bBin ){
18454     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
18455   }else{
18456     const char *z = (const char*)sqlite3_value_text(argv[0]);
18457     /* Remember whether or not the value originally contained \r\n */
18458     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
18459     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
18460   }
18461   fclose(f);
18462   f = 0;
18463   if( x!=sz ){
18464     sqlite3_result_error(context, "edit() could not write the whole file", -1);
18465     goto edit_func_end;
18466   }
18467   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
18468   if( zCmd==0 ){
18469     sqlite3_result_error_nomem(context);
18470     goto edit_func_end;
18471   }
18472   rc = system(zCmd);
18473   sqlite3_free(zCmd);
18474   if( rc ){
18475     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
18476     goto edit_func_end;
18477   }
18478   f = fopen(zTempFile, "rb");
18479   if( f==0 ){
18480     sqlite3_result_error(context,
18481       "edit() cannot reopen temp file after edit", -1);
18482     goto edit_func_end;
18483   }
18484   fseek(f, 0, SEEK_END);
18485   sz = ftell(f);
18486   rewind(f);
18487   p = sqlite3_malloc64( sz+1 );
18488   if( p==0 ){
18489     sqlite3_result_error_nomem(context);
18490     goto edit_func_end;
18491   }
18492   x = fread(p, 1, (size_t)sz, f);
18493   fclose(f);
18494   f = 0;
18495   if( x!=sz ){
18496     sqlite3_result_error(context, "could not read back the whole file", -1);
18497     goto edit_func_end;
18498   }
18499   if( bBin ){
18500     sqlite3_result_blob64(context, p, sz, sqlite3_free);
18501   }else{
18502     sqlite3_int64 i, j;
18503     if( hasCRNL ){
18504       /* If the original contains \r\n then do no conversions back to \n */
18505     }else{
18506       /* If the file did not originally contain \r\n then convert any new
18507       ** \r\n back into \n */
18508       p[sz] = 0;
18509       for(i=j=0; i<sz; i++){
18510         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
18511         p[j++] = p[i];
18512       }
18513       sz = j;
18514       p[sz] = 0;
18515     }
18516     sqlite3_result_text64(context, (const char*)p, sz,
18517                           sqlite3_free, SQLITE_UTF8);
18518   }
18519   p = 0;
18520 
18521 edit_func_end:
18522   if( f ) fclose(f);
18523   unlink(zTempFile);
18524   sqlite3_free(zTempFile);
18525   sqlite3_free(p);
18526 }
18527 #endif /* SQLITE_NOHAVE_SYSTEM */
18528 
18529 /*
18530 ** Save or restore the current output mode
18531 */
outputModePush(ShellState * p)18532 static void outputModePush(ShellState *p){
18533   p->modePrior = p->mode;
18534   p->priorShFlgs = p->shellFlgs;
18535   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
18536   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
18537 }
outputModePop(ShellState * p)18538 static void outputModePop(ShellState *p){
18539   p->mode = p->modePrior;
18540   p->shellFlgs = p->priorShFlgs;
18541   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
18542   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
18543 }
18544 
18545 /*
18546 ** Output the given string as a hex-encoded blob (eg. X'1234' )
18547 */
output_hex_blob(const void * pBlob,int nBlob)18548 static void output_hex_blob(const void *pBlob, int nBlob){
18549   int i;
18550   unsigned char *aBlob = (unsigned char*)pBlob;
18551 
18552   char *zStr = sqlite3_malloc(nBlob*2 + 1);
18553   shell_check_oom(zStr);
18554 
18555   for(i=0; i<nBlob; i++){
18556     static const char aHex[] = {
18557         '0', '1', '2', '3', '4', '5', '6', '7',
18558         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
18559     };
18560     zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
18561     zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
18562   }
18563   zStr[i*2] = '\0';
18564 
18565   oputf("X'%s'", zStr);
18566   sqlite3_free(zStr);
18567 }
18568 
18569 /*
18570 ** Find a string that is not found anywhere in z[].  Return a pointer
18571 ** to that string.
18572 **
18573 ** Try to use zA and zB first.  If both of those are already found in z[]
18574 ** then make up some string and store it in the buffer zBuf.
18575 */
unused_string(const char * z,const char * zA,const char * zB,char * zBuf)18576 static const char *unused_string(
18577   const char *z,                    /* Result must not appear anywhere in z */
18578   const char *zA, const char *zB,   /* Try these first */
18579   char *zBuf                        /* Space to store a generated string */
18580 ){
18581   unsigned i = 0;
18582   if( strstr(z, zA)==0 ) return zA;
18583   if( strstr(z, zB)==0 ) return zB;
18584   do{
18585     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
18586   }while( strstr(z,zBuf)!=0 );
18587   return zBuf;
18588 }
18589 
18590 /*
18591 ** Output the given string as a quoted string using SQL quoting conventions.
18592 **
18593 ** See also: output_quoted_escaped_string()
18594 */
output_quoted_string(const char * z)18595 static void output_quoted_string(const char *z){
18596   int i;
18597   char c;
18598 #ifndef SQLITE_SHELL_FIDDLE
18599   FILE *pfO = setOutputStream(invalidFileStream);
18600   setBinaryMode(pfO, 1);
18601 #endif
18602   if( z==0 ) return;
18603   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18604   if( c==0 ){
18605     oputf("'%s'",z);
18606   }else{
18607     oputz("'");
18608     while( *z ){
18609       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
18610       if( c=='\'' ) i++;
18611       if( i ){
18612         oputf("%.*s", i, z);
18613         z += i;
18614       }
18615       if( c=='\'' ){
18616         oputz("'");
18617         continue;
18618       }
18619       if( c==0 ){
18620         break;
18621       }
18622       z++;
18623     }
18624     oputz("'");
18625   }
18626 #ifndef SQLITE_SHELL_FIDDLE
18627   setTextMode(pfO, 1);
18628 #else
18629   setTextMode(stdout, 1);
18630 #endif
18631 }
18632 
18633 /*
18634 ** Output the given string as a quoted string using SQL quoting conventions.
18635 ** Additionallly , escape the "\n" and "\r" characters so that they do not
18636 ** get corrupted by end-of-line translation facilities in some operating
18637 ** systems.
18638 **
18639 ** This is like output_quoted_string() but with the addition of the \r\n
18640 ** escape mechanism.
18641 */
output_quoted_escaped_string(const char * z)18642 static void output_quoted_escaped_string(const char *z){
18643   int i;
18644   char c;
18645 #ifndef SQLITE_SHELL_FIDDLE
18646   FILE *pfO = setOutputStream(invalidFileStream);
18647   setBinaryMode(pfO, 1);
18648 #endif
18649   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
18650   if( c==0 ){
18651     oputf("'%s'",z);
18652   }else{
18653     const char *zNL = 0;
18654     const char *zCR = 0;
18655     int nNL = 0;
18656     int nCR = 0;
18657     char zBuf1[20], zBuf2[20];
18658     for(i=0; z[i]; i++){
18659       if( z[i]=='\n' ) nNL++;
18660       if( z[i]=='\r' ) nCR++;
18661     }
18662     if( nNL ){
18663       oputz("replace(");
18664       zNL = unused_string(z, "\\n", "\\012", zBuf1);
18665     }
18666     if( nCR ){
18667       oputz("replace(");
18668       zCR = unused_string(z, "\\r", "\\015", zBuf2);
18669     }
18670     oputz("'");
18671     while( *z ){
18672       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
18673       if( c=='\'' ) i++;
18674       if( i ){
18675         oputf("%.*s", i, z);
18676         z += i;
18677       }
18678       if( c=='\'' ){
18679         oputz("'");
18680         continue;
18681       }
18682       if( c==0 ){
18683         break;
18684       }
18685       z++;
18686       if( c=='\n' ){
18687         oputz(zNL);
18688         continue;
18689       }
18690       oputz(zCR);
18691     }
18692     oputz("'");
18693     if( nCR ){
18694       oputf(",'%s',char(13))", zCR);
18695     }
18696     if( nNL ){
18697       oputf(",'%s',char(10))", zNL);
18698     }
18699   }
18700 #ifndef SQLITE_SHELL_FIDDLE
18701   setTextMode(pfO, 1);
18702 #else
18703   setTextMode(stdout, 1);
18704 #endif
18705 }
18706 
18707 /*
18708 ** Find earliest of chars within s specified in zAny.
18709 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
18710 */
anyOfInStr(const char * s,const char * zAny,size_t ns)18711 static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
18712   const char *pcFirst = 0;
18713   if( ns == ~(size_t)0 ) ns = strlen(s);
18714   while(*zAny){
18715     const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
18716     if( pc ){
18717       pcFirst = pc;
18718       ns = pcFirst - s;
18719     }
18720     ++zAny;
18721   }
18722   return pcFirst;
18723 }
18724 /*
18725 ** Output the given string as a quoted according to C or TCL quoting rules.
18726 */
output_c_string(const char * z)18727 static void output_c_string(const char *z){
18728   char c;
18729   static const char *zq = "\"";
18730   static long ctrlMask = ~0L;
18731   static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
18732   char ace[3] = "\\?";
18733   char cbsSay;
18734   oputz(zq);
18735   while( *z!=0 ){
18736     const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
18737     const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
18738     const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
18739     if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
18740     if( (c = *pcEnd)==0 ) break;
18741     ++pcEnd;
18742     switch( c ){
18743     case '\\': case '"':
18744       cbsSay = (char)c;
18745       break;
18746     case '\t': cbsSay = 't'; break;
18747     case '\n': cbsSay = 'n'; break;
18748     case '\r': cbsSay = 'r'; break;
18749     case '\f': cbsSay = 'f'; break;
18750     default: cbsSay = 0; break;
18751     }
18752     if( cbsSay ){
18753       ace[1] = cbsSay;
18754       oputz(ace);
18755     }else if( !isprint(c&0xff) ){
18756       oputf("\\%03o", c&0xff);
18757     }else{
18758       ace[1] = (char)c;
18759       oputz(ace+1);
18760     }
18761     z = pcEnd;
18762   }
18763   oputz(zq);
18764 }
18765 
18766 /*
18767 ** Output the given string as a quoted according to JSON quoting rules.
18768 */
output_json_string(const char * z,i64 n)18769 static void output_json_string(const char *z, i64 n){
18770   char c;
18771   static const char *zq = "\"";
18772   static long ctrlMask = ~0L;
18773   static const char *zDQBS = "\"\\";
18774   const char *pcLimit;
18775   char ace[3] = "\\?";
18776   char cbsSay;
18777 
18778   if( z==0 ) z = "";
18779   pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
18780   oputz(zq);
18781   while( z < pcLimit ){
18782     const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
18783     const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
18784     const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
18785     if( pcEnd > z ){
18786       oputb(z, (int)(pcEnd-z));
18787       z = pcEnd;
18788     }
18789     if( z >= pcLimit ) break;
18790     c = *(z++);
18791     switch( c ){
18792     case '"': case '\\':
18793       cbsSay = (char)c;
18794       break;
18795     case '\b': cbsSay = 'b'; break;
18796     case '\f': cbsSay = 'f'; break;
18797     case '\n': cbsSay = 'n'; break;
18798     case '\r': cbsSay = 'r'; break;
18799     case '\t': cbsSay = 't'; break;
18800     default: cbsSay = 0; break;
18801     }
18802     if( cbsSay ){
18803       ace[1] = cbsSay;
18804       oputz(ace);
18805     }else if( c<=0x1f ){
18806       oputf("u%04x", c);
18807     }else{
18808       ace[1] = (char)c;
18809       oputz(ace+1);
18810     }
18811   }
18812   oputz(zq);
18813 }
18814 
18815 /*
18816 ** Output the given string with characters that are special to
18817 ** HTML escaped.
18818 */
output_html_string(const char * z)18819 static void output_html_string(const char *z){
18820   int i;
18821   if( z==0 ) z = "";
18822   while( *z ){
18823     for(i=0;   z[i]
18824             && z[i]!='<'
18825             && z[i]!='&'
18826             && z[i]!='>'
18827             && z[i]!='\"'
18828             && z[i]!='\'';
18829         i++){}
18830     if( i>0 ){
18831       oputf("%.*s",i,z);
18832     }
18833     if( z[i]=='<' ){
18834       oputz("&lt;");
18835     }else if( z[i]=='&' ){
18836       oputz("&amp;");
18837     }else if( z[i]=='>' ){
18838       oputz("&gt;");
18839     }else if( z[i]=='\"' ){
18840       oputz("&quot;");
18841     }else if( z[i]=='\'' ){
18842       oputz("&#39;");
18843     }else{
18844       break;
18845     }
18846     z += i + 1;
18847   }
18848 }
18849 
18850 /*
18851 ** If a field contains any character identified by a 1 in the following
18852 ** array, then the string must be quoted for CSV.
18853 */
18854 static const char needCsvQuote[] = {
18855   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18856   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18857   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
18858   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
18859   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
18860   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
18861   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
18862   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
18863   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18864   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18865   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18866   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18867   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18868   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18869   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18870   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
18871 };
18872 
18873 /*
18874 ** Output a single term of CSV.  Actually, p->colSeparator is used for
18875 ** the separator, which may or may not be a comma.  p->nullValue is
18876 ** the null value.  Strings are quoted if necessary.  The separator
18877 ** is only issued if bSep is true.
18878 */
output_csv(ShellState * p,const char * z,int bSep)18879 static void output_csv(ShellState *p, const char *z, int bSep){
18880   if( z==0 ){
18881     oputf("%s",p->nullValue);
18882   }else{
18883     unsigned i;
18884     for(i=0; z[i]; i++){
18885       if( needCsvQuote[((unsigned char*)z)[i]] ){
18886         i = 0;
18887         break;
18888       }
18889     }
18890     if( i==0 || strstr(z, p->colSeparator)!=0 ){
18891       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
18892       shell_check_oom(zQuoted);
18893       oputz(zQuoted);
18894       sqlite3_free(zQuoted);
18895     }else{
18896       oputz(z);
18897     }
18898   }
18899   if( bSep ){
18900     oputz(p->colSeparator);
18901   }
18902 }
18903 
18904 /*
18905 ** This routine runs when the user presses Ctrl-C
18906 */
interrupt_handler(int NotUsed)18907 static void interrupt_handler(int NotUsed){
18908   UNUSED_PARAMETER(NotUsed);
18909   if( ++seenInterrupt>1 ) exit(1);
18910   if( globalDb ) sqlite3_interrupt(globalDb);
18911 }
18912 
18913 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
18914 /*
18915 ** This routine runs for console events (e.g. Ctrl-C) on Win32
18916 */
ConsoleCtrlHandler(DWORD dwCtrlType)18917 static BOOL WINAPI ConsoleCtrlHandler(
18918   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
18919 ){
18920   if( dwCtrlType==CTRL_C_EVENT ){
18921     interrupt_handler(0);
18922     return TRUE;
18923   }
18924   return FALSE;
18925 }
18926 #endif
18927 
18928 #ifndef SQLITE_OMIT_AUTHORIZATION
18929 /*
18930 ** This authorizer runs in safe mode.
18931 */
safeModeAuth(void * pClientData,int op,const char * zA1,const char * zA2,const char * zA3,const char * zA4)18932 static int safeModeAuth(
18933   void *pClientData,
18934   int op,
18935   const char *zA1,
18936   const char *zA2,
18937   const char *zA3,
18938   const char *zA4
18939 ){
18940   ShellState *p = (ShellState*)pClientData;
18941   static const char *azProhibitedFunctions[] = {
18942     "edit",
18943     "fts3_tokenizer",
18944     "load_extension",
18945     "readfile",
18946     "writefile",
18947     "zipfile",
18948     "zipfile_cds",
18949   };
18950   UNUSED_PARAMETER(zA1);
18951   UNUSED_PARAMETER(zA3);
18952   UNUSED_PARAMETER(zA4);
18953   switch( op ){
18954     case SQLITE_ATTACH: {
18955 #ifndef SQLITE_SHELL_FIDDLE
18956       /* In WASM builds the filesystem is a virtual sandbox, so
18957       ** there's no harm in using ATTACH. */
18958       failIfSafeMode(p, "cannot run ATTACH in safe mode");
18959 #endif
18960       break;
18961     }
18962     case SQLITE_FUNCTION: {
18963       int i;
18964       for(i=0; i<ArraySize(azProhibitedFunctions); i++){
18965         if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
18966           failIfSafeMode(p, "cannot use the %s() function in safe mode",
18967                          azProhibitedFunctions[i]);
18968         }
18969       }
18970       break;
18971     }
18972   }
18973   return SQLITE_OK;
18974 }
18975 
18976 /*
18977 ** When the ".auth ON" is set, the following authorizer callback is
18978 ** invoked.  It always returns SQLITE_OK.
18979 */
shellAuth(void * pClientData,int op,const char * zA1,const char * zA2,const char * zA3,const char * zA4)18980 static int shellAuth(
18981   void *pClientData,
18982   int op,
18983   const char *zA1,
18984   const char *zA2,
18985   const char *zA3,
18986   const char *zA4
18987 ){
18988   ShellState *p = (ShellState*)pClientData;
18989   static const char *azAction[] = { 0,
18990      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
18991      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
18992      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
18993      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
18994      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
18995      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
18996      "PRAGMA",               "READ",                 "SELECT",
18997      "TRANSACTION",          "UPDATE",               "ATTACH",
18998      "DETACH",               "ALTER_TABLE",          "REINDEX",
18999      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
19000      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
19001   };
19002   int i;
19003   const char *az[4];
19004   az[0] = zA1;
19005   az[1] = zA2;
19006   az[2] = zA3;
19007   az[3] = zA4;
19008   oputf("authorizer: %s", azAction[op]);
19009   for(i=0; i<4; i++){
19010     oputz(" ");
19011     if( az[i] ){
19012       output_c_string(az[i]);
19013     }else{
19014       oputz("NULL");
19015     }
19016   }
19017   oputz("\n");
19018   if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
19019   return SQLITE_OK;
19020 }
19021 #endif
19022 
19023 /*
19024 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
19025 **
19026 ** This routine converts some CREATE TABLE statements for shadow tables
19027 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
19028 **
19029 ** If the schema statement in z[] contains a start-of-comment and if
19030 ** sqlite3_complete() returns false, try to terminate the comment before
19031 ** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
19032 */
printSchemaLine(const char * z,const char * zTail)19033 static void printSchemaLine(const char *z, const char *zTail){
19034   char *zToFree = 0;
19035   if( z==0 ) return;
19036   if( zTail==0 ) return;
19037   if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
19038     const char *zOrig = z;
19039     static const char *azTerm[] = { "", "*/", "\n" };
19040     int i;
19041     for(i=0; i<ArraySize(azTerm); i++){
19042       char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
19043       shell_check_oom(zNew);
19044       if( sqlite3_complete(zNew) ){
19045         size_t n = strlen(zNew);
19046         zNew[n-1] = 0;
19047         zToFree = zNew;
19048         z = zNew;
19049         break;
19050       }
19051       sqlite3_free(zNew);
19052     }
19053   }
19054   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
19055     oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
19056   }else{
19057     oputf("%s%s", z, zTail);
19058   }
19059   sqlite3_free(zToFree);
19060 }
printSchemaLineN(char * z,int n,const char * zTail)19061 static void printSchemaLineN(char *z, int n, const char *zTail){
19062   char c = z[n];
19063   z[n] = 0;
19064   printSchemaLine(z, zTail);
19065   z[n] = c;
19066 }
19067 
19068 /*
19069 ** Return true if string z[] has nothing but whitespace and comments to the
19070 ** end of the first line.
19071 */
wsToEol(const char * z)19072 static int wsToEol(const char *z){
19073   int i;
19074   for(i=0; z[i]; i++){
19075     if( z[i]=='\n' ) return 1;
19076     if( IsSpace(z[i]) ) continue;
19077     if( z[i]=='-' && z[i+1]=='-' ) return 1;
19078     return 0;
19079   }
19080   return 1;
19081 }
19082 
19083 /*
19084 ** Add a new entry to the EXPLAIN QUERY PLAN data
19085 */
eqp_append(ShellState * p,int iEqpId,int p2,const char * zText)19086 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
19087   EQPGraphRow *pNew;
19088   i64 nText;
19089   if( zText==0 ) return;
19090   nText = strlen(zText);
19091   if( p->autoEQPtest ){
19092     oputf("%d,%d,%s\n", iEqpId, p2, zText);
19093   }
19094   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
19095   shell_check_oom(pNew);
19096   pNew->iEqpId = iEqpId;
19097   pNew->iParentId = p2;
19098   memcpy(pNew->zText, zText, nText+1);
19099   pNew->pNext = 0;
19100   if( p->sGraph.pLast ){
19101     p->sGraph.pLast->pNext = pNew;
19102   }else{
19103     p->sGraph.pRow = pNew;
19104   }
19105   p->sGraph.pLast = pNew;
19106 }
19107 
19108 /*
19109 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
19110 ** in p->sGraph.
19111 */
eqp_reset(ShellState * p)19112 static void eqp_reset(ShellState *p){
19113   EQPGraphRow *pRow, *pNext;
19114   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
19115     pNext = pRow->pNext;
19116     sqlite3_free(pRow);
19117   }
19118   memset(&p->sGraph, 0, sizeof(p->sGraph));
19119 }
19120 
19121 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
19122 ** pOld, or return the first such line if pOld is NULL
19123 */
eqp_next_row(ShellState * p,int iEqpId,EQPGraphRow * pOld)19124 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
19125   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
19126   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
19127   return pRow;
19128 }
19129 
19130 /* Render a single level of the graph that has iEqpId as its parent.  Called
19131 ** recursively to render sublevels.
19132 */
eqp_render_level(ShellState * p,int iEqpId)19133 static void eqp_render_level(ShellState *p, int iEqpId){
19134   EQPGraphRow *pRow, *pNext;
19135   i64 n = strlen(p->sGraph.zPrefix);
19136   char *z;
19137   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
19138     pNext = eqp_next_row(p, iEqpId, pRow);
19139     z = pRow->zText;
19140     oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
19141     if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
19142       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
19143       eqp_render_level(p, pRow->iEqpId);
19144       p->sGraph.zPrefix[n] = 0;
19145     }
19146   }
19147 }
19148 
19149 /*
19150 ** Display and reset the EXPLAIN QUERY PLAN data
19151 */
eqp_render(ShellState * p,i64 nCycle)19152 static void eqp_render(ShellState *p, i64 nCycle){
19153   EQPGraphRow *pRow = p->sGraph.pRow;
19154   if( pRow ){
19155     if( pRow->zText[0]=='-' ){
19156       if( pRow->pNext==0 ){
19157         eqp_reset(p);
19158         return;
19159       }
19160       oputf("%s\n", pRow->zText+3);
19161       p->sGraph.pRow = pRow->pNext;
19162       sqlite3_free(pRow);
19163     }else if( nCycle>0 ){
19164       oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
19165     }else{
19166       oputz("QUERY PLAN\n");
19167     }
19168     p->sGraph.zPrefix[0] = 0;
19169     eqp_render_level(p, 0);
19170     eqp_reset(p);
19171   }
19172 }
19173 
19174 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19175 /*
19176 ** Progress handler callback.
19177 */
progress_handler(void * pClientData)19178 static int progress_handler(void *pClientData) {
19179   ShellState *p = (ShellState*)pClientData;
19180   p->nProgress++;
19181   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
19182     oputf("Progress limit reached (%u)\n", p->nProgress);
19183     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
19184     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
19185     return 1;
19186   }
19187   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
19188     oputf("Progress %u\n", p->nProgress);
19189   }
19190   return 0;
19191 }
19192 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19193 
19194 /*
19195 ** Print N dashes
19196 */
print_dashes(int N)19197 static void print_dashes(int N){
19198   const char zDash[] = "--------------------------------------------------";
19199   const int nDash = sizeof(zDash) - 1;
19200   while( N>nDash ){
19201     oputz(zDash);
19202     N -= nDash;
19203   }
19204   oputf("%.*s", N, zDash);
19205 }
19206 
19207 /*
19208 ** Print a markdown or table-style row separator using ascii-art
19209 */
print_row_separator(ShellState * p,int nArg,const char * zSep)19210 static void print_row_separator(
19211   ShellState *p,
19212   int nArg,
19213   const char *zSep
19214 ){
19215   int i;
19216   if( nArg>0 ){
19217     oputz(zSep);
19218     print_dashes(p->actualWidth[0]+2);
19219     for(i=1; i<nArg; i++){
19220       oputz(zSep);
19221       print_dashes(p->actualWidth[i]+2);
19222     }
19223     oputz(zSep);
19224   }
19225   oputz("\n");
19226 }
19227 
19228 /*
19229 ** This is the callback routine that the shell
19230 ** invokes for each row of a query result.
19231 */
shell_callback(void * pArg,int nArg,char ** azArg,char ** azCol,int * aiType)19232 static int shell_callback(
19233   void *pArg,
19234   int nArg,        /* Number of result columns */
19235   char **azArg,    /* Text of each result column */
19236   char **azCol,    /* Column names */
19237   int *aiType      /* Column types.  Might be NULL */
19238 ){
19239   int i;
19240   ShellState *p = (ShellState*)pArg;
19241 
19242   if( azArg==0 ) return 0;
19243   switch( p->cMode ){
19244     case MODE_Count:
19245     case MODE_Off: {
19246       break;
19247     }
19248     case MODE_Line: {
19249       int w = 5;
19250       if( azArg==0 ) break;
19251       for(i=0; i<nArg; i++){
19252         int len = strlen30(azCol[i] ? azCol[i] : "");
19253         if( len>w ) w = len;
19254       }
19255       if( p->cnt++>0 ) oputz(p->rowSeparator);
19256       for(i=0; i<nArg; i++){
19257         oputf("%*s = %s%s", w, azCol[i],
19258               azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
19259       }
19260       break;
19261     }
19262     case MODE_ScanExp:
19263     case MODE_Explain: {
19264       static const int aExplainWidth[] = {4,       13, 4, 4, 4, 13, 2, 13};
19265       static const int aExplainMap[] =   {0,       1,  2, 3, 4, 5,  6, 7 };
19266       static const int aScanExpWidth[] = {4, 6, 6, 13, 4, 4, 4, 13, 2, 13};
19267       static const int aScanExpMap[] =   {0, 9, 8, 1,  2, 3, 4, 5,  6, 7 };
19268 
19269       const int *aWidth = aExplainWidth;
19270       const int *aMap = aExplainMap;
19271       int nWidth = ArraySize(aExplainWidth);
19272       int iIndent = 1;
19273 
19274       if( p->cMode==MODE_ScanExp ){
19275         aWidth = aScanExpWidth;
19276         aMap = aScanExpMap;
19277         nWidth = ArraySize(aScanExpWidth);
19278         iIndent = 3;
19279       }
19280       if( nArg>nWidth ) nArg = nWidth;
19281 
19282       /* If this is the first row seen, print out the headers */
19283       if( p->cnt++==0 ){
19284         for(i=0; i<nArg; i++){
19285           utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
19286           oputz(i==nArg-1 ? "\n" : "  ");
19287         }
19288         for(i=0; i<nArg; i++){
19289           print_dashes(aWidth[i]);
19290           oputz(i==nArg-1 ? "\n" : "  ");
19291         }
19292       }
19293 
19294       /* If there is no data, exit early. */
19295       if( azArg==0 ) break;
19296 
19297       for(i=0; i<nArg; i++){
19298         const char *zSep = "  ";
19299         int w = aWidth[i];
19300         const char *zVal = azArg[ aMap[i] ];
19301         if( i==nArg-1 ) w = 0;
19302         if( zVal && strlenChar(zVal)>w ){
19303           w = strlenChar(zVal);
19304           zSep = " ";
19305         }
19306         if( i==iIndent && p->aiIndent && p->pStmt ){
19307           if( p->iIndent<p->nIndent ){
19308             oputf("%*.s", p->aiIndent[p->iIndent], "");
19309           }
19310           p->iIndent++;
19311         }
19312         utf8_width_print(w, zVal ? zVal : p->nullValue);
19313         oputz(i==nArg-1 ? "\n" : zSep);
19314       }
19315       break;
19316     }
19317     case MODE_Semi: {   /* .schema and .fullschema output */
19318       printSchemaLine(azArg[0], ";\n");
19319       break;
19320     }
19321     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
19322       char *z;
19323       int j;
19324       int nParen = 0;
19325       char cEnd = 0;
19326       char c;
19327       int nLine = 0;
19328       assert( nArg==1 );
19329       if( azArg[0]==0 ) break;
19330       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
19331        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
19332       ){
19333         oputf("%s;\n", azArg[0]);
19334         break;
19335       }
19336       z = sqlite3_mprintf("%s", azArg[0]);
19337       shell_check_oom(z);
19338       j = 0;
19339       for(i=0; IsSpace(z[i]); i++){}
19340       for(; (c = z[i])!=0; i++){
19341         if( IsSpace(c) ){
19342           if( z[j-1]=='\r' ) z[j-1] = '\n';
19343           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
19344         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
19345           j--;
19346         }
19347         z[j++] = c;
19348       }
19349       while( j>0 && IsSpace(z[j-1]) ){ j--; }
19350       z[j] = 0;
19351       if( strlen30(z)>=79 ){
19352         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
19353           if( c==cEnd ){
19354             cEnd = 0;
19355           }else if( c=='"' || c=='\'' || c=='`' ){
19356             cEnd = c;
19357           }else if( c=='[' ){
19358             cEnd = ']';
19359           }else if( c=='-' && z[i+1]=='-' ){
19360             cEnd = '\n';
19361           }else if( c=='(' ){
19362             nParen++;
19363           }else if( c==')' ){
19364             nParen--;
19365             if( nLine>0 && nParen==0 && j>0 ){
19366               printSchemaLineN(z, j, "\n");
19367               j = 0;
19368             }
19369           }
19370           z[j++] = c;
19371           if( nParen==1 && cEnd==0
19372            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
19373           ){
19374             if( c=='\n' ) j--;
19375             printSchemaLineN(z, j, "\n  ");
19376             j = 0;
19377             nLine++;
19378             while( IsSpace(z[i+1]) ){ i++; }
19379           }
19380         }
19381         z[j] = 0;
19382       }
19383       printSchemaLine(z, ";\n");
19384       sqlite3_free(z);
19385       break;
19386     }
19387     case MODE_List: {
19388       if( p->cnt++==0 && p->showHeader ){
19389         for(i=0; i<nArg; i++){
19390           oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
19391         }
19392       }
19393       if( azArg==0 ) break;
19394       for(i=0; i<nArg; i++){
19395         char *z = azArg[i];
19396         if( z==0 ) z = p->nullValue;
19397         oputz(z);
19398         oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
19399       }
19400       break;
19401     }
19402     case MODE_Html: {
19403       if( p->cnt++==0 && p->showHeader ){
19404         oputz("<TR>");
19405         for(i=0; i<nArg; i++){
19406           oputz("<TH>");
19407           output_html_string(azCol[i]);
19408           oputz("</TH>\n");
19409         }
19410         oputz("</TR>\n");
19411       }
19412       if( azArg==0 ) break;
19413       oputz("<TR>");
19414       for(i=0; i<nArg; i++){
19415         oputz("<TD>");
19416         output_html_string(azArg[i] ? azArg[i] : p->nullValue);
19417         oputz("</TD>\n");
19418       }
19419       oputz("</TR>\n");
19420       break;
19421     }
19422     case MODE_Tcl: {
19423       if( p->cnt++==0 && p->showHeader ){
19424         for(i=0; i<nArg; i++){
19425           output_c_string(azCol[i] ? azCol[i] : "");
19426           if(i<nArg-1) oputz(p->colSeparator);
19427         }
19428         oputz(p->rowSeparator);
19429       }
19430       if( azArg==0 ) break;
19431       for(i=0; i<nArg; i++){
19432         output_c_string(azArg[i] ? azArg[i] : p->nullValue);
19433         if(i<nArg-1) oputz(p->colSeparator);
19434       }
19435       oputz(p->rowSeparator);
19436       break;
19437     }
19438     case MODE_Csv: {
19439       setBinaryMode(p->out, 1);
19440       if( p->cnt++==0 && p->showHeader ){
19441         for(i=0; i<nArg; i++){
19442           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
19443         }
19444         oputz(p->rowSeparator);
19445       }
19446       if( nArg>0 ){
19447         for(i=0; i<nArg; i++){
19448           output_csv(p, azArg[i], i<nArg-1);
19449         }
19450         oputz(p->rowSeparator);
19451       }
19452       setTextMode(p->out, 1);
19453       break;
19454     }
19455     case MODE_Insert: {
19456       if( azArg==0 ) break;
19457       oputf("INSERT INTO %s",p->zDestTable);
19458       if( p->showHeader ){
19459         oputz("(");
19460         for(i=0; i<nArg; i++){
19461           if( i>0 ) oputz(",");
19462           if( quoteChar(azCol[i]) ){
19463             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
19464             shell_check_oom(z);
19465             oputz(z);
19466             sqlite3_free(z);
19467           }else{
19468             oputf("%s", azCol[i]);
19469           }
19470         }
19471         oputz(")");
19472       }
19473       p->cnt++;
19474       for(i=0; i<nArg; i++){
19475         oputz(i>0 ? "," : " VALUES(");
19476         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19477           oputz("NULL");
19478         }else if( aiType && aiType[i]==SQLITE_TEXT ){
19479           if( ShellHasFlag(p, SHFLG_Newlines) ){
19480             output_quoted_string(azArg[i]);
19481           }else{
19482             output_quoted_escaped_string(azArg[i]);
19483           }
19484         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19485           oputz(azArg[i]);
19486         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19487           char z[50];
19488           double r = sqlite3_column_double(p->pStmt, i);
19489           sqlite3_uint64 ur;
19490           memcpy(&ur,&r,sizeof(r));
19491           if( ur==0x7ff0000000000000LL ){
19492             oputz("9.0e+999");
19493           }else if( ur==0xfff0000000000000LL ){
19494             oputz("-9.0e+999");
19495           }else{
19496             sqlite3_int64 ir = (sqlite3_int64)r;
19497             if( r==(double)ir ){
19498               sqlite3_snprintf(50,z,"%lld.0", ir);
19499             }else{
19500               sqlite3_snprintf(50,z,"%!.20g", r);
19501             }
19502             oputz(z);
19503           }
19504         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19505           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19506           int nBlob = sqlite3_column_bytes(p->pStmt, i);
19507           output_hex_blob(pBlob, nBlob);
19508         }else if( isNumber(azArg[i], 0) ){
19509           oputz(azArg[i]);
19510         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
19511           output_quoted_string(azArg[i]);
19512         }else{
19513           output_quoted_escaped_string(azArg[i]);
19514         }
19515       }
19516       oputz(");\n");
19517       break;
19518     }
19519     case MODE_Json: {
19520       if( azArg==0 ) break;
19521       if( p->cnt==0 ){
19522         fputs("[{", p->out);
19523       }else{
19524         fputs(",\n{", p->out);
19525       }
19526       p->cnt++;
19527       for(i=0; i<nArg; i++){
19528         output_json_string(azCol[i], -1);
19529         oputz(":");
19530         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19531           oputz("null");
19532         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19533           char z[50];
19534           double r = sqlite3_column_double(p->pStmt, i);
19535           sqlite3_uint64 ur;
19536           memcpy(&ur,&r,sizeof(r));
19537           if( ur==0x7ff0000000000000LL ){
19538             oputz("9.0e+999");
19539           }else if( ur==0xfff0000000000000LL ){
19540             oputz("-9.0e+999");
19541           }else{
19542             sqlite3_snprintf(50,z,"%!.20g", r);
19543             oputz(z);
19544           }
19545         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19546           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19547           int nBlob = sqlite3_column_bytes(p->pStmt, i);
19548           output_json_string(pBlob, nBlob);
19549         }else if( aiType && aiType[i]==SQLITE_TEXT ){
19550           output_json_string(azArg[i], -1);
19551         }else{
19552           oputz(azArg[i]);
19553         }
19554         if( i<nArg-1 ){
19555           oputz(",");
19556         }
19557       }
19558       oputz("}");
19559       break;
19560     }
19561     case MODE_Quote: {
19562       if( azArg==0 ) break;
19563       if( p->cnt==0 && p->showHeader ){
19564         for(i=0; i<nArg; i++){
19565           if( i>0 ) fputs(p->colSeparator, p->out);
19566           output_quoted_string(azCol[i]);
19567         }
19568         fputs(p->rowSeparator, p->out);
19569       }
19570       p->cnt++;
19571       for(i=0; i<nArg; i++){
19572         if( i>0 ) fputs(p->colSeparator, p->out);
19573         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
19574           oputz("NULL");
19575         }else if( aiType && aiType[i]==SQLITE_TEXT ){
19576           output_quoted_string(azArg[i]);
19577         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
19578           oputz(azArg[i]);
19579         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
19580           char z[50];
19581           double r = sqlite3_column_double(p->pStmt, i);
19582           sqlite3_snprintf(50,z,"%!.20g", r);
19583           oputz(z);
19584         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
19585           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
19586           int nBlob = sqlite3_column_bytes(p->pStmt, i);
19587           output_hex_blob(pBlob, nBlob);
19588         }else if( isNumber(azArg[i], 0) ){
19589           oputz(azArg[i]);
19590         }else{
19591           output_quoted_string(azArg[i]);
19592         }
19593       }
19594       fputs(p->rowSeparator, p->out);
19595       break;
19596     }
19597     case MODE_Ascii: {
19598       if( p->cnt++==0 && p->showHeader ){
19599         for(i=0; i<nArg; i++){
19600           if( i>0 ) oputz(p->colSeparator);
19601           oputz(azCol[i] ? azCol[i] : "");
19602         }
19603         oputz(p->rowSeparator);
19604       }
19605       if( azArg==0 ) break;
19606       for(i=0; i<nArg; i++){
19607         if( i>0 ) oputz(p->colSeparator);
19608         oputz(azArg[i] ? azArg[i] : p->nullValue);
19609       }
19610       oputz(p->rowSeparator);
19611       break;
19612     }
19613     case MODE_EQP: {
19614       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
19615       break;
19616     }
19617   }
19618   return 0;
19619 }
19620 
19621 /*
19622 ** This is the callback routine that the SQLite library
19623 ** invokes for each row of a query result.
19624 */
callback(void * pArg,int nArg,char ** azArg,char ** azCol)19625 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
19626   /* since we don't have type info, call the shell_callback with a NULL value */
19627   return shell_callback(pArg, nArg, azArg, azCol, NULL);
19628 }
19629 
19630 /*
19631 ** This is the callback routine from sqlite3_exec() that appends all
19632 ** output onto the end of a ShellText object.
19633 */
captureOutputCallback(void * pArg,int nArg,char ** azArg,char ** az)19634 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
19635   ShellText *p = (ShellText*)pArg;
19636   int i;
19637   UNUSED_PARAMETER(az);
19638   if( azArg==0 ) return 0;
19639   if( p->n ) appendText(p, "|", 0);
19640   for(i=0; i<nArg; i++){
19641     if( i ) appendText(p, ",", 0);
19642     if( azArg[i] ) appendText(p, azArg[i], 0);
19643   }
19644   return 0;
19645 }
19646 
19647 /*
19648 ** Generate an appropriate SELFTEST table in the main database.
19649 */
createSelftestTable(ShellState * p)19650 static void createSelftestTable(ShellState *p){
19651   char *zErrMsg = 0;
19652   sqlite3_exec(p->db,
19653     "SAVEPOINT selftest_init;\n"
19654     "CREATE TABLE IF NOT EXISTS selftest(\n"
19655     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
19656     "  op TEXT,\n"                   /* Operator:  memo run */
19657     "  cmd TEXT,\n"                  /* Command text */
19658     "  ans TEXT\n"                   /* Desired answer */
19659     ");"
19660     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
19661     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
19662     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
19663     "         'memo','Tests generated by --init');\n"
19664     "INSERT INTO [_shell$self]\n"
19665     "  SELECT 'run',\n"
19666     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
19667                                  "FROM sqlite_schema ORDER BY 2'',224))',\n"
19668     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
19669                           "FROM sqlite_schema ORDER BY 2',224));\n"
19670     "INSERT INTO [_shell$self]\n"
19671     "  SELECT 'run',"
19672     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
19673     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
19674     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
19675     "  FROM (\n"
19676     "    SELECT name FROM sqlite_schema\n"
19677     "     WHERE type='table'\n"
19678     "       AND name<>'selftest'\n"
19679     "       AND coalesce(rootpage,0)>0\n"
19680     "  )\n"
19681     " ORDER BY name;\n"
19682     "INSERT INTO [_shell$self]\n"
19683     "  VALUES('run','PRAGMA integrity_check','ok');\n"
19684     "INSERT INTO selftest(tno,op,cmd,ans)"
19685     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
19686     "DROP TABLE [_shell$self];"
19687     ,0,0,&zErrMsg);
19688   if( zErrMsg ){
19689     eputf("SELFTEST initialization failure: %s\n", zErrMsg);
19690     sqlite3_free(zErrMsg);
19691   }
19692   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
19693 }
19694 
19695 
19696 /*
19697 ** Set the destination table field of the ShellState structure to
19698 ** the name of the table given.  Escape any quote characters in the
19699 ** table name.
19700 */
set_table_name(ShellState * p,const char * zName)19701 static void set_table_name(ShellState *p, const char *zName){
19702   int i, n;
19703   char cQuote;
19704   char *z;
19705 
19706   if( p->zDestTable ){
19707     free(p->zDestTable);
19708     p->zDestTable = 0;
19709   }
19710   if( zName==0 ) return;
19711   cQuote = quoteChar(zName);
19712   n = strlen30(zName);
19713   if( cQuote ) n += n+2;
19714   z = p->zDestTable = malloc( n+1 );
19715   shell_check_oom(z);
19716   n = 0;
19717   if( cQuote ) z[n++] = cQuote;
19718   for(i=0; zName[i]; i++){
19719     z[n++] = zName[i];
19720     if( zName[i]==cQuote ) z[n++] = cQuote;
19721   }
19722   if( cQuote ) z[n++] = cQuote;
19723   z[n] = 0;
19724 }
19725 
19726 /*
19727 ** Maybe construct two lines of text that point out the position of a
19728 ** syntax error.  Return a pointer to the text, in memory obtained from
19729 ** sqlite3_malloc().  Or, if the most recent error does not involve a
19730 ** specific token that we can point to, return an empty string.
19731 **
19732 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
19733 ** and should be released by the caller invoking sqlite3_free().
19734 */
shell_error_context(const char * zSql,sqlite3 * db)19735 static char *shell_error_context(const char *zSql, sqlite3 *db){
19736   int iOffset;
19737   size_t len;
19738   char *zCode;
19739   char *zMsg;
19740   int i;
19741   if( db==0
19742    || zSql==0
19743    || (iOffset = sqlite3_error_offset(db))<0
19744    || iOffset>=(int)strlen(zSql)
19745   ){
19746     return sqlite3_mprintf("");
19747   }
19748   while( iOffset>50 ){
19749     iOffset--;
19750     zSql++;
19751     while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
19752   }
19753   len = strlen(zSql);
19754   if( len>78 ){
19755     len = 78;
19756     while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
19757   }
19758   zCode = sqlite3_mprintf("%.*s", len, zSql);
19759   shell_check_oom(zCode);
19760   for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
19761   if( iOffset<25 ){
19762     zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode,iOffset,"");
19763   }else{
19764     zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode,iOffset-14,"");
19765   }
19766   return zMsg;
19767 }
19768 
19769 
19770 /*
19771 ** Execute a query statement that will generate SQL output.  Print
19772 ** the result columns, comma-separated, on a line and then add a
19773 ** semicolon terminator to the end of that line.
19774 **
19775 ** If the number of columns is 1 and that column contains text "--"
19776 ** then write the semicolon on a separate line.  That way, if a
19777 ** "--" comment occurs at the end of the statement, the comment
19778 ** won't consume the semicolon terminator.
19779 */
run_table_dump_query(ShellState * p,const char * zSelect)19780 static int run_table_dump_query(
19781   ShellState *p,           /* Query context */
19782   const char *zSelect      /* SELECT statement to extract content */
19783 ){
19784   sqlite3_stmt *pSelect;
19785   int rc;
19786   int nResult;
19787   int i;
19788   const char *z;
19789   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
19790   if( rc!=SQLITE_OK || !pSelect ){
19791     char *zContext = shell_error_context(zSelect, p->db);
19792     oputf("/**** ERROR: (%d) %s *****/\n%s",
19793           rc, sqlite3_errmsg(p->db), zContext);
19794     sqlite3_free(zContext);
19795     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19796     return rc;
19797   }
19798   rc = sqlite3_step(pSelect);
19799   nResult = sqlite3_column_count(pSelect);
19800   while( rc==SQLITE_ROW ){
19801     z = (const char*)sqlite3_column_text(pSelect, 0);
19802     oputf("%s", z);
19803     for(i=1; i<nResult; i++){
19804       oputf(",%s", sqlite3_column_text(pSelect, i));
19805     }
19806     if( z==0 ) z = "";
19807     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
19808     if( z[0] ){
19809       oputz("\n;\n");
19810     }else{
19811       oputz(";\n");
19812     }
19813     rc = sqlite3_step(pSelect);
19814   }
19815   rc = sqlite3_finalize(pSelect);
19816   if( rc!=SQLITE_OK ){
19817     oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
19818     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
19819   }
19820   return rc;
19821 }
19822 
19823 /*
19824 ** Allocate space and save off string indicating current error.
19825 */
save_err_msg(sqlite3 * db,const char * zPhase,int rc,const char * zSql)19826 static char *save_err_msg(
19827   sqlite3 *db,           /* Database to query */
19828   const char *zPhase,    /* When the error occurs */
19829   int rc,                /* Error code returned from API */
19830   const char *zSql       /* SQL string, or NULL */
19831 ){
19832   char *zErr;
19833   char *zContext;
19834   sqlite3_str *pStr = sqlite3_str_new(0);
19835   sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
19836   if( rc>1 ){
19837     sqlite3_str_appendf(pStr, " (%d)", rc);
19838   }
19839   zContext = shell_error_context(zSql, db);
19840   if( zContext ){
19841     sqlite3_str_appendall(pStr, zContext);
19842     sqlite3_free(zContext);
19843   }
19844   zErr = sqlite3_str_finish(pStr);
19845   shell_check_oom(zErr);
19846   return zErr;
19847 }
19848 
19849 #ifdef __linux__
19850 /*
19851 ** Attempt to display I/O stats on Linux using /proc/PID/io
19852 */
displayLinuxIoStats(void)19853 static void displayLinuxIoStats(void){
19854   FILE *in;
19855   char z[200];
19856   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
19857   in = fopen(z, "rb");
19858   if( in==0 ) return;
19859   while( fgets(z, sizeof(z), in)!=0 ){
19860     static const struct {
19861       const char *zPattern;
19862       const char *zDesc;
19863     } aTrans[] = {
19864       { "rchar: ",                  "Bytes received by read():" },
19865       { "wchar: ",                  "Bytes sent to write():"    },
19866       { "syscr: ",                  "Read() system calls:"      },
19867       { "syscw: ",                  "Write() system calls:"     },
19868       { "read_bytes: ",             "Bytes read from storage:"  },
19869       { "write_bytes: ",            "Bytes written to storage:" },
19870       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
19871     };
19872     int i;
19873     for(i=0; i<ArraySize(aTrans); i++){
19874       int n = strlen30(aTrans[i].zPattern);
19875       if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
19876         oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
19877         break;
19878       }
19879     }
19880   }
19881   fclose(in);
19882 }
19883 #endif
19884 
19885 /*
19886 ** Display a single line of status using 64-bit values.
19887 */
displayStatLine(char * zLabel,char * zFormat,int iStatusCtrl,int bReset)19888 static void displayStatLine(
19889   char *zLabel,             /* Label for this one line */
19890   char *zFormat,            /* Format for the result */
19891   int iStatusCtrl,          /* Which status to display */
19892   int bReset                /* True to reset the stats */
19893 ){
19894   sqlite3_int64 iCur = -1;
19895   sqlite3_int64 iHiwtr = -1;
19896   int i, nPercent;
19897   char zLine[200];
19898   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
19899   for(i=0, nPercent=0; zFormat[i]; i++){
19900     if( zFormat[i]=='%' ) nPercent++;
19901   }
19902   if( nPercent>1 ){
19903     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
19904   }else{
19905     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
19906   }
19907   oputf("%-36s %s\n", zLabel, zLine);
19908 }
19909 
19910 /*
19911 ** Display memory stats.
19912 */
display_stats(sqlite3 * db,ShellState * pArg,int bReset)19913 static int display_stats(
19914   sqlite3 *db,                /* Database to query */
19915   ShellState *pArg,           /* Pointer to ShellState */
19916   int bReset                  /* True to reset the stats */
19917 ){
19918   int iCur;
19919   int iHiwtr;
19920   if( pArg==0 || pArg->out==0 ) return 0;
19921 
19922   if( pArg->pStmt && pArg->statsOn==2 ){
19923     int nCol, i, x;
19924     sqlite3_stmt *pStmt = pArg->pStmt;
19925     char z[100];
19926     nCol = sqlite3_column_count(pStmt);
19927     oputf("%-36s %d\n", "Number of output columns:", nCol);
19928     for(i=0; i<nCol; i++){
19929       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
19930       oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
19931 #ifndef SQLITE_OMIT_DECLTYPE
19932       sqlite3_snprintf(30, z+x, "declared type:");
19933       oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
19934 #endif
19935 #ifdef SQLITE_ENABLE_COLUMN_METADATA
19936       sqlite3_snprintf(30, z+x, "database name:");
19937       oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
19938       sqlite3_snprintf(30, z+x, "table name:");
19939       oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
19940       sqlite3_snprintf(30, z+x, "origin name:");
19941       oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
19942 #endif
19943     }
19944   }
19945 
19946   if( pArg->statsOn==3 ){
19947     if( pArg->pStmt ){
19948       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
19949       oputf("VM-steps: %d\n", iCur);
19950     }
19951     return 0;
19952   }
19953 
19954   displayStatLine("Memory Used:",
19955      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
19956   displayStatLine("Number of Outstanding Allocations:",
19957      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
19958   if( pArg->shellFlgs & SHFLG_Pagecache ){
19959     displayStatLine("Number of Pcache Pages Used:",
19960        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
19961   }
19962   displayStatLine("Number of Pcache Overflow Bytes:",
19963      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
19964   displayStatLine("Largest Allocation:",
19965      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
19966   displayStatLine("Largest Pcache Allocation:",
19967      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
19968 #ifdef YYTRACKMAXSTACKDEPTH
19969   displayStatLine("Deepest Parser Stack:",
19970      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
19971 #endif
19972 
19973   if( db ){
19974     if( pArg->shellFlgs & SHFLG_Lookaside ){
19975       iHiwtr = iCur = -1;
19976       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
19977                         &iCur, &iHiwtr, bReset);
19978       oputf("Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
19979       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
19980                         &iCur, &iHiwtr, bReset);
19981       oputf("Successful lookaside attempts:       %d\n", iHiwtr);
19982       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
19983                         &iCur, &iHiwtr, bReset);
19984       oputf("Lookaside failures due to size:      %d\n", iHiwtr);
19985       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
19986                         &iCur, &iHiwtr, bReset);
19987       oputf("Lookaside failures due to OOM:       %d\n", iHiwtr);
19988     }
19989     iHiwtr = iCur = -1;
19990     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
19991     oputf("Pager Heap Usage:                    %d bytes\n", iCur);
19992     iHiwtr = iCur = -1;
19993     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
19994     oputf("Page cache hits:                     %d\n", iCur);
19995     iHiwtr = iCur = -1;
19996     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
19997     oputf("Page cache misses:                   %d\n", iCur);
19998     iHiwtr = iCur = -1;
19999     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
20000     oputf("Page cache writes:                   %d\n", iCur);
20001     iHiwtr = iCur = -1;
20002     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
20003     oputf("Page cache spills:                   %d\n", iCur);
20004     iHiwtr = iCur = -1;
20005     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
20006     oputf("Schema Heap Usage:                   %d bytes\n", iCur);
20007     iHiwtr = iCur = -1;
20008     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
20009     oputf("Statement Heap/Lookaside Usage:      %d bytes\n", iCur);
20010   }
20011 
20012   if( pArg->pStmt ){
20013     int iHit, iMiss;
20014     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
20015                                bReset);
20016     oputf("Fullscan Steps:                      %d\n", iCur);
20017     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
20018     oputf("Sort Operations:                     %d\n", iCur);
20019     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
20020     oputf("Autoindex Inserts:                   %d\n", iCur);
20021     iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
20022                                bReset);
20023     iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
20024                                 bReset);
20025     if( iHit || iMiss ){
20026       oputf("Bloom filter bypass taken:           %d/%d\n", iHit, iHit+iMiss);
20027     }
20028     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
20029     oputf("Virtual Machine Steps:               %d\n", iCur);
20030     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
20031     oputf("Reprepare operations:                %d\n", iCur);
20032     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
20033     oputf("Number of times run:                 %d\n", iCur);
20034     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
20035     oputf("Memory used by prepared stmt:        %d\n", iCur);
20036   }
20037 
20038 #ifdef __linux__
20039   displayLinuxIoStats();
20040 #endif
20041 
20042   /* Do not remove this machine readable comment: extra-stats-output-here */
20043 
20044   return 0;
20045 }
20046 
20047 
20048 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
scanStatsHeight(sqlite3_stmt * p,int iEntry)20049 static int scanStatsHeight(sqlite3_stmt *p, int iEntry){
20050   int iPid = 0;
20051   int ret = 1;
20052   sqlite3_stmt_scanstatus_v2(p, iEntry,
20053       SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
20054   );
20055   while( iPid!=0 ){
20056     int ii;
20057     for(ii=0; 1; ii++){
20058       int iId;
20059       int res;
20060       res = sqlite3_stmt_scanstatus_v2(p, ii,
20061           SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
20062       );
20063       if( res ) break;
20064       if( iId==iPid ){
20065         sqlite3_stmt_scanstatus_v2(p, ii,
20066             SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
20067         );
20068       }
20069     }
20070     ret++;
20071   }
20072   return ret;
20073 }
20074 #endif
20075 
20076 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
display_explain_scanstats(sqlite3 * db,ShellState * pArg)20077 static void display_explain_scanstats(
20078   sqlite3 *db,                    /* Database to query */
20079   ShellState *pArg                /* Pointer to ShellState */
20080 ){
20081   static const int f = SQLITE_SCANSTAT_COMPLEX;
20082   sqlite3_stmt *p = pArg->pStmt;
20083   int ii = 0;
20084   i64 nTotal = 0;
20085   int nWidth = 0;
20086   eqp_reset(pArg);
20087 
20088   for(ii=0; 1; ii++){
20089     const char *z = 0;
20090     int n = 0;
20091     if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
20092       break;
20093     }
20094     n = (int)strlen(z) + scanStatsHeight(p, ii)*3;
20095     if( n>nWidth ) nWidth = n;
20096   }
20097   nWidth += 4;
20098 
20099   sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
20100   for(ii=0; 1; ii++){
20101     i64 nLoop = 0;
20102     i64 nRow = 0;
20103     i64 nCycle = 0;
20104     int iId = 0;
20105     int iPid = 0;
20106     const char *zo = 0;
20107     const char *zName = 0;
20108     char *zText = 0;
20109     double rEst = 0.0;
20110 
20111     if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
20112       break;
20113     }
20114     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
20115     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
20116     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
20117     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
20118     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
20119     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
20120     sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
20121 
20122     zText = sqlite3_mprintf("%s", zo);
20123     if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
20124       char *z = 0;
20125       if( nCycle>=0 && nTotal>0 ){
20126         z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
20127             nCycle, ((nCycle*100)+nTotal/2) / nTotal
20128         );
20129       }
20130       if( nLoop>=0 ){
20131         z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop);
20132       }
20133       if( nRow>=0 ){
20134         z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow);
20135       }
20136 
20137       if( zName && pArg->scanstatsOn>1 ){
20138         double rpl = (double)nRow / (double)nLoop;
20139         z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst);
20140       }
20141 
20142       zText = sqlite3_mprintf(
20143           "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z
20144       );
20145     }
20146 
20147     eqp_append(pArg, iId, iPid, zText);
20148     sqlite3_free(zText);
20149   }
20150 
20151   eqp_render(pArg, nTotal);
20152 }
20153 #endif
20154 
20155 
20156 /*
20157 ** Parameter azArray points to a zero-terminated array of strings. zStr
20158 ** points to a single nul-terminated string. Return non-zero if zStr
20159 ** is equal, according to strcmp(), to any of the strings in the array.
20160 ** Otherwise, return zero.
20161 */
str_in_array(const char * zStr,const char ** azArray)20162 static int str_in_array(const char *zStr, const char **azArray){
20163   int i;
20164   for(i=0; azArray[i]; i++){
20165     if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
20166   }
20167   return 0;
20168 }
20169 
20170 /*
20171 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
20172 ** and populate the ShellState.aiIndent[] array with the number of
20173 ** spaces each opcode should be indented before it is output.
20174 **
20175 ** The indenting rules are:
20176 **
20177 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
20178 **       all opcodes that occur between the p2 jump destination and the opcode
20179 **       itself by 2 spaces.
20180 **
20181 **     * Do the previous for "Return" instructions for when P2 is positive.
20182 **       See tag-20220407a in wherecode.c and vdbe.c.
20183 **
20184 **     * For each "Goto", if the jump destination is earlier in the program
20185 **       and ends on one of:
20186 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
20187 **       or if the P1 parameter is one instead of zero,
20188 **       then indent all opcodes between the earlier instruction
20189 **       and "Goto" by 2 spaces.
20190 */
explain_data_prepare(ShellState * p,sqlite3_stmt * pSql)20191 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
20192   int *abYield = 0;               /* True if op is an OP_Yield */
20193   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
20194   int iOp;                        /* Index of operation in p->aiIndent[] */
20195 
20196   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
20197                            "Return", 0 };
20198   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
20199                             "Rewind", 0 };
20200   const char *azGoto[] = { "Goto", 0 };
20201 
20202   /* The caller guarantees that the leftmost 4 columns of the statement
20203   ** passed to this function are equivalent to the leftmost 4 columns
20204   ** of EXPLAIN statement output. In practice the statement may be
20205   ** an EXPLAIN, or it may be a query on the bytecode() virtual table.  */
20206   assert( sqlite3_column_count(pSql)>=4 );
20207   assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) );
20208   assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) );
20209   assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) );
20210   assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) );
20211 
20212   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
20213     int i;
20214     int iAddr = sqlite3_column_int(pSql, 0);
20215     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
20216     int p1 = sqlite3_column_int(pSql, 2);
20217     int p2 = sqlite3_column_int(pSql, 3);
20218 
20219     /* Assuming that p2 is an instruction address, set variable p2op to the
20220     ** index of that instruction in the aiIndent[] array. p2 and p2op may be
20221     ** different if the current instruction is part of a sub-program generated
20222     ** by an SQL trigger or foreign key.  */
20223     int p2op = (p2 + (iOp-iAddr));
20224 
20225     /* Grow the p->aiIndent array as required */
20226     if( iOp>=nAlloc ){
20227       nAlloc += 100;
20228       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
20229       shell_check_oom(p->aiIndent);
20230       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
20231       shell_check_oom(abYield);
20232     }
20233 
20234     abYield[iOp] = str_in_array(zOp, azYield);
20235     p->aiIndent[iOp] = 0;
20236     p->nIndent = iOp+1;
20237     if( str_in_array(zOp, azNext) && p2op>0 ){
20238       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
20239     }
20240     if( str_in_array(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
20241       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
20242     }
20243   }
20244 
20245   p->iIndent = 0;
20246   sqlite3_free(abYield);
20247   sqlite3_reset(pSql);
20248 }
20249 
20250 /*
20251 ** Free the array allocated by explain_data_prepare().
20252 */
explain_data_delete(ShellState * p)20253 static void explain_data_delete(ShellState *p){
20254   sqlite3_free(p->aiIndent);
20255   p->aiIndent = 0;
20256   p->nIndent = 0;
20257   p->iIndent = 0;
20258 }
20259 
20260 static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
20261 
20262 /*
20263 ** Display scan stats.
20264 */
display_scanstats(sqlite3 * db,ShellState * pArg)20265 static void display_scanstats(
20266   sqlite3 *db,                    /* Database to query */
20267   ShellState *pArg                /* Pointer to ShellState */
20268 ){
20269 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20270   UNUSED_PARAMETER(db);
20271   UNUSED_PARAMETER(pArg);
20272 #else
20273   if( pArg->scanstatsOn==3 ){
20274     const char *zSql =
20275       "  SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
20276       "   round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
20277       "   FROM bytecode(?)";
20278 
20279     int rc = SQLITE_OK;
20280     sqlite3_stmt *pStmt = 0;
20281     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
20282     if( rc==SQLITE_OK ){
20283       sqlite3_stmt *pSave = pArg->pStmt;
20284       pArg->pStmt = pStmt;
20285       sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
20286 
20287       pArg->cnt = 0;
20288       pArg->cMode = MODE_ScanExp;
20289       explain_data_prepare(pArg, pStmt);
20290       exec_prepared_stmt(pArg, pStmt);
20291       explain_data_delete(pArg);
20292 
20293       sqlite3_finalize(pStmt);
20294       pArg->pStmt = pSave;
20295     }
20296   }else{
20297     display_explain_scanstats(db, pArg);
20298   }
20299 #endif
20300 }
20301 
20302 /*
20303 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
20304 */
20305 static unsigned int savedSelectTrace;
20306 static unsigned int savedWhereTrace;
disable_debug_trace_modes(void)20307 static void disable_debug_trace_modes(void){
20308   unsigned int zero = 0;
20309   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
20310   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
20311   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
20312   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
20313 }
restore_debug_trace_modes(void)20314 static void restore_debug_trace_modes(void){
20315   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
20316   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
20317 }
20318 
20319 /* Create the TEMP table used to store parameter bindings */
bind_table_init(ShellState * p)20320 static void bind_table_init(ShellState *p){
20321   int wrSchema = 0;
20322   int defensiveMode = 0;
20323   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
20324   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
20325   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
20326   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
20327   sqlite3_exec(p->db,
20328     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
20329     "  key TEXT PRIMARY KEY,\n"
20330     "  value\n"
20331     ") WITHOUT ROWID;",
20332     0, 0, 0);
20333   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
20334   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
20335 }
20336 
20337 /*
20338 ** Bind parameters on a prepared statement.
20339 **
20340 ** Parameter bindings are taken from a TEMP table of the form:
20341 **
20342 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
20343 **    WITHOUT ROWID;
20344 **
20345 ** No bindings occur if this table does not exist.  The name of the table
20346 ** begins with "sqlite_" so that it will not collide with ordinary application
20347 ** tables.  The table must be in the TEMP schema.
20348 */
bind_prepared_stmt(ShellState * pArg,sqlite3_stmt * pStmt)20349 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
20350   int nVar;
20351   int i;
20352   int rc;
20353   sqlite3_stmt *pQ = 0;
20354 
20355   nVar = sqlite3_bind_parameter_count(pStmt);
20356   if( nVar==0 ) return;  /* Nothing to do */
20357   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
20358                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
20359     rc = SQLITE_NOTFOUND;
20360     pQ = 0;
20361   }else{
20362     rc = sqlite3_prepare_v2(pArg->db,
20363             "SELECT value FROM temp.sqlite_parameters"
20364             " WHERE key=?1", -1, &pQ, 0);
20365   }
20366   for(i=1; i<=nVar; i++){
20367     char zNum[30];
20368     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
20369     if( zVar==0 ){
20370       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
20371       zVar = zNum;
20372     }
20373     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
20374     if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
20375       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
20376 #ifdef NAN
20377     }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
20378       sqlite3_bind_double(pStmt, i, NAN);
20379 #endif
20380 #ifdef INFINITY
20381     }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
20382       sqlite3_bind_double(pStmt, i, INFINITY);
20383 #endif
20384     }else{
20385       sqlite3_bind_null(pStmt, i);
20386     }
20387     sqlite3_reset(pQ);
20388   }
20389   sqlite3_finalize(pQ);
20390 }
20391 
20392 /*
20393 ** UTF8 box-drawing characters.  Imagine box lines like this:
20394 **
20395 **           1
20396 **           |
20397 **       4 --+-- 2
20398 **           |
20399 **           3
20400 **
20401 ** Each box characters has between 2 and 4 of the lines leading from
20402 ** the center.  The characters are here identified by the numbers of
20403 ** their corresponding lines.
20404 */
20405 #define BOX_24   "\342\224\200"  /* U+2500 --- */
20406 #define BOX_13   "\342\224\202"  /* U+2502  |  */
20407 #define BOX_23   "\342\224\214"  /* U+250c  ,- */
20408 #define BOX_34   "\342\224\220"  /* U+2510 -,  */
20409 #define BOX_12   "\342\224\224"  /* U+2514  '- */
20410 #define BOX_14   "\342\224\230"  /* U+2518 -'  */
20411 #define BOX_123  "\342\224\234"  /* U+251c  |- */
20412 #define BOX_134  "\342\224\244"  /* U+2524 -|  */
20413 #define BOX_234  "\342\224\254"  /* U+252c -,- */
20414 #define BOX_124  "\342\224\264"  /* U+2534 -'- */
20415 #define BOX_1234 "\342\224\274"  /* U+253c -|- */
20416 
20417 /* Draw horizontal line N characters long using unicode box
20418 ** characters
20419 */
print_box_line(int N)20420 static void print_box_line(int N){
20421   const char zDash[] =
20422       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
20423       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
20424   const int nDash = sizeof(zDash) - 1;
20425   N *= 3;
20426   while( N>nDash ){
20427     oputz(zDash);
20428     N -= nDash;
20429   }
20430   oputf("%.*s", N, zDash);
20431 }
20432 
20433 /*
20434 ** Draw a horizontal separator for a MODE_Box table.
20435 */
print_box_row_separator(ShellState * p,int nArg,const char * zSep1,const char * zSep2,const char * zSep3)20436 static void print_box_row_separator(
20437   ShellState *p,
20438   int nArg,
20439   const char *zSep1,
20440   const char *zSep2,
20441   const char *zSep3
20442 ){
20443   int i;
20444   if( nArg>0 ){
20445     oputz(zSep1);
20446     print_box_line(p->actualWidth[0]+2);
20447     for(i=1; i<nArg; i++){
20448       oputz(zSep2);
20449       print_box_line(p->actualWidth[i]+2);
20450     }
20451     oputz(zSep3);
20452   }
20453   oputz("\n");
20454 }
20455 
20456 /*
20457 ** z[] is a line of text that is to be displayed the .mode box or table or
20458 ** similar tabular formats.  z[] might contain control characters such
20459 ** as \n, \t, \f, or \r.
20460 **
20461 ** Compute characters to display on the first line of z[].  Stop at the
20462 ** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
20463 ** from malloc()) of that first line, which caller should free sometime.
20464 ** Write anything to display on the next line into *pzTail.  If this is
20465 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
20466 */
translateForDisplayAndDup(const unsigned char * z,const unsigned char ** pzTail,int mxWidth,u8 bWordWrap)20467 static char *translateForDisplayAndDup(
20468   const unsigned char *z,            /* Input text to be transformed */
20469   const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
20470   int mxWidth,                       /* Max width.  0 means no limit */
20471   u8 bWordWrap                       /* If true, avoid breaking mid-word */
20472 ){
20473   int i;                 /* Input bytes consumed */
20474   int j;                 /* Output bytes generated */
20475   int k;                 /* Input bytes to be displayed */
20476   int n;                 /* Output column number */
20477   unsigned char *zOut;   /* Output text */
20478 
20479   if( z==0 ){
20480     *pzTail = 0;
20481     return 0;
20482   }
20483   if( mxWidth<0 ) mxWidth = -mxWidth;
20484   if( mxWidth==0 ) mxWidth = 1000000;
20485   i = j = n = 0;
20486   while( n<mxWidth ){
20487     if( z[i]>=' ' ){
20488       n++;
20489       do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
20490       continue;
20491     }
20492     if( z[i]=='\t' ){
20493       do{
20494         n++;
20495         j++;
20496       }while( (n&7)!=0 && n<mxWidth );
20497       i++;
20498       continue;
20499     }
20500     break;
20501   }
20502   if( n>=mxWidth && bWordWrap  ){
20503     /* Perhaps try to back up to a better place to break the line */
20504     for(k=i; k>i/2; k--){
20505       if( isspace(z[k-1]) ) break;
20506     }
20507     if( k<=i/2 ){
20508       for(k=i; k>i/2; k--){
20509         if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
20510       }
20511     }
20512     if( k<=i/2 ){
20513       k = i;
20514     }else{
20515       i = k;
20516       while( z[i]==' ' ) i++;
20517     }
20518   }else{
20519     k = i;
20520   }
20521   if( n>=mxWidth && z[i]>=' ' ){
20522    *pzTail = &z[i];
20523   }else if( z[i]=='\r' && z[i+1]=='\n' ){
20524     *pzTail = z[i+2] ? &z[i+2] : 0;
20525   }else if( z[i]==0 || z[i+1]==0 ){
20526     *pzTail = 0;
20527   }else{
20528     *pzTail = &z[i+1];
20529   }
20530   zOut = malloc( j+1 );
20531   shell_check_oom(zOut);
20532   i = j = n = 0;
20533   while( i<k ){
20534     if( z[i]>=' ' ){
20535       n++;
20536       do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
20537       continue;
20538     }
20539     if( z[i]=='\t' ){
20540       do{
20541         n++;
20542         zOut[j++] = ' ';
20543       }while( (n&7)!=0 && n<mxWidth );
20544       i++;
20545       continue;
20546     }
20547     break;
20548   }
20549   zOut[j] = 0;
20550   return (char*)zOut;
20551 }
20552 
20553 /* Extract the value of the i-th current column for pStmt as an SQL literal
20554 ** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
20555 ** the caller.
20556 */
quoted_column(sqlite3_stmt * pStmt,int i)20557 static char *quoted_column(sqlite3_stmt *pStmt, int i){
20558   switch( sqlite3_column_type(pStmt, i) ){
20559     case SQLITE_NULL: {
20560       return sqlite3_mprintf("NULL");
20561     }
20562     case SQLITE_INTEGER:
20563     case SQLITE_FLOAT: {
20564       return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
20565     }
20566     case SQLITE_TEXT: {
20567       return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
20568     }
20569     case SQLITE_BLOB: {
20570       int j;
20571       sqlite3_str *pStr = sqlite3_str_new(0);
20572       const unsigned char *a = sqlite3_column_blob(pStmt,i);
20573       int n = sqlite3_column_bytes(pStmt,i);
20574       sqlite3_str_append(pStr, "x'", 2);
20575       for(j=0; j<n; j++){
20576         sqlite3_str_appendf(pStr, "%02x", a[j]);
20577       }
20578       sqlite3_str_append(pStr, "'", 1);
20579       return sqlite3_str_finish(pStr);
20580     }
20581   }
20582   return 0; /* Not reached */
20583 }
20584 
20585 /*
20586 ** Run a prepared statement and output the result in one of the
20587 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
20588 ** or MODE_Box.
20589 **
20590 ** This is different from ordinary exec_prepared_stmt() in that
20591 ** it has to run the entire query and gather the results into memory
20592 ** first, in order to determine column widths, before providing
20593 ** any output.
20594 */
exec_prepared_stmt_columnar(ShellState * p,sqlite3_stmt * pStmt)20595 static void exec_prepared_stmt_columnar(
20596   ShellState *p,                        /* Pointer to ShellState */
20597   sqlite3_stmt *pStmt                   /* Statement to run */
20598 ){
20599   sqlite3_int64 nRow = 0;
20600   int nColumn = 0;
20601   char **azData = 0;
20602   sqlite3_int64 nAlloc = 0;
20603   char *abRowDiv = 0;
20604   const unsigned char *uz;
20605   const char *z;
20606   char **azQuoted = 0;
20607   int rc;
20608   sqlite3_int64 i, nData;
20609   int j, nTotal, w, n;
20610   const char *colSep = 0;
20611   const char *rowSep = 0;
20612   const unsigned char **azNextLine = 0;
20613   int bNextLine = 0;
20614   int bMultiLineRowExists = 0;
20615   int bw = p->cmOpts.bWordWrap;
20616   const char *zEmpty = "";
20617   const char *zShowNull = p->nullValue;
20618 
20619   rc = sqlite3_step(pStmt);
20620   if( rc!=SQLITE_ROW ) return;
20621   nColumn = sqlite3_column_count(pStmt);
20622   nAlloc = nColumn*4;
20623   if( nAlloc<=0 ) nAlloc = 1;
20624   azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
20625   shell_check_oom(azData);
20626   azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
20627   shell_check_oom(azNextLine);
20628   memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
20629   if( p->cmOpts.bQuote ){
20630     azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
20631     shell_check_oom(azQuoted);
20632     memset(azQuoted, 0, nColumn*sizeof(char*) );
20633   }
20634   abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
20635   shell_check_oom(abRowDiv);
20636   if( nColumn>p->nWidth ){
20637     p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
20638     shell_check_oom(p->colWidth);
20639     for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
20640     p->nWidth = nColumn;
20641     p->actualWidth = &p->colWidth[nColumn];
20642   }
20643   memset(p->actualWidth, 0, nColumn*sizeof(int));
20644   for(i=0; i<nColumn; i++){
20645     w = p->colWidth[i];
20646     if( w<0 ) w = -w;
20647     p->actualWidth[i] = w;
20648   }
20649   for(i=0; i<nColumn; i++){
20650     const unsigned char *zNotUsed;
20651     int wx = p->colWidth[i];
20652     if( wx==0 ){
20653       wx = p->cmOpts.iWrap;
20654     }
20655     if( wx<0 ) wx = -wx;
20656     uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
20657     if( uz==0 ) uz = (u8*)"";
20658     azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
20659   }
20660   do{
20661     int useNextLine = bNextLine;
20662     bNextLine = 0;
20663     if( (nRow+2)*nColumn >= nAlloc ){
20664       nAlloc *= 2;
20665       azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
20666       shell_check_oom(azData);
20667       abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
20668       shell_check_oom(abRowDiv);
20669     }
20670     abRowDiv[nRow] = 1;
20671     nRow++;
20672     for(i=0; i<nColumn; i++){
20673       int wx = p->colWidth[i];
20674       if( wx==0 ){
20675         wx = p->cmOpts.iWrap;
20676       }
20677       if( wx<0 ) wx = -wx;
20678       if( useNextLine ){
20679         uz = azNextLine[i];
20680         if( uz==0 ) uz = (u8*)zEmpty;
20681       }else if( p->cmOpts.bQuote ){
20682         sqlite3_free(azQuoted[i]);
20683         azQuoted[i] = quoted_column(pStmt,i);
20684         uz = (const unsigned char*)azQuoted[i];
20685       }else{
20686         uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
20687         if( uz==0 ) uz = (u8*)zShowNull;
20688       }
20689       azData[nRow*nColumn + i]
20690         = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
20691       if( azNextLine[i] ){
20692         bNextLine = 1;
20693         abRowDiv[nRow-1] = 0;
20694         bMultiLineRowExists = 1;
20695       }
20696     }
20697   }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
20698   nTotal = nColumn*(nRow+1);
20699   for(i=0; i<nTotal; i++){
20700     z = azData[i];
20701     if( z==0 ) z = (char*)zEmpty;
20702     n = strlenChar(z);
20703     j = i%nColumn;
20704     if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
20705   }
20706   if( seenInterrupt ) goto columnar_end;
20707   if( nColumn==0 ) goto columnar_end;
20708   switch( p->cMode ){
20709     case MODE_Column: {
20710       colSep = "  ";
20711       rowSep = "\n";
20712       if( p->showHeader ){
20713         for(i=0; i<nColumn; i++){
20714           w = p->actualWidth[i];
20715           if( p->colWidth[i]<0 ) w = -w;
20716           utf8_width_print(w, azData[i]);
20717           fputs(i==nColumn-1?"\n":"  ", p->out);
20718         }
20719         for(i=0; i<nColumn; i++){
20720           print_dashes(p->actualWidth[i]);
20721           fputs(i==nColumn-1?"\n":"  ", p->out);
20722         }
20723       }
20724       break;
20725     }
20726     case MODE_Table: {
20727       colSep = " | ";
20728       rowSep = " |\n";
20729       print_row_separator(p, nColumn, "+");
20730       fputs("| ", p->out);
20731       for(i=0; i<nColumn; i++){
20732         w = p->actualWidth[i];
20733         n = strlenChar(azData[i]);
20734         oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20735         oputz(i==nColumn-1?" |\n":" | ");
20736       }
20737       print_row_separator(p, nColumn, "+");
20738       break;
20739     }
20740     case MODE_Markdown: {
20741       colSep = " | ";
20742       rowSep = " |\n";
20743       fputs("| ", p->out);
20744       for(i=0; i<nColumn; i++){
20745         w = p->actualWidth[i];
20746         n = strlenChar(azData[i]);
20747         oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
20748         oputz(i==nColumn-1?" |\n":" | ");
20749       }
20750       print_row_separator(p, nColumn, "|");
20751       break;
20752     }
20753     case MODE_Box: {
20754       colSep = " " BOX_13 " ";
20755       rowSep = " " BOX_13 "\n";
20756       print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
20757       oputz(BOX_13 " ");
20758       for(i=0; i<nColumn; i++){
20759         w = p->actualWidth[i];
20760         n = strlenChar(azData[i]);
20761         oputf("%*s%s%*s%s",
20762               (w-n)/2, "", azData[i], (w-n+1)/2, "",
20763               i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
20764       }
20765       print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20766       break;
20767     }
20768   }
20769   for(i=nColumn, j=0; i<nTotal; i++, j++){
20770     if( j==0 && p->cMode!=MODE_Column ){
20771       oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
20772     }
20773     z = azData[i];
20774     if( z==0 ) z = p->nullValue;
20775     w = p->actualWidth[j];
20776     if( p->colWidth[j]<0 ) w = -w;
20777     utf8_width_print(w, z);
20778     if( j==nColumn-1 ){
20779       oputz(rowSep);
20780       if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
20781         if( p->cMode==MODE_Table ){
20782           print_row_separator(p, nColumn, "+");
20783         }else if( p->cMode==MODE_Box ){
20784           print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
20785         }else if( p->cMode==MODE_Column ){
20786           oputz("\n");
20787         }
20788       }
20789       j = -1;
20790       if( seenInterrupt ) goto columnar_end;
20791     }else{
20792       oputz(colSep);
20793     }
20794   }
20795   if( p->cMode==MODE_Table ){
20796     print_row_separator(p, nColumn, "+");
20797   }else if( p->cMode==MODE_Box ){
20798     print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
20799   }
20800 columnar_end:
20801   if( seenInterrupt ){
20802     oputz("Interrupt\n");
20803   }
20804   nData = (nRow+1)*nColumn;
20805   for(i=0; i<nData; i++){
20806     z = azData[i];
20807     if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
20808   }
20809   sqlite3_free(azData);
20810   sqlite3_free((void*)azNextLine);
20811   sqlite3_free(abRowDiv);
20812   if( azQuoted ){
20813     for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
20814     sqlite3_free(azQuoted);
20815   }
20816 }
20817 
20818 /*
20819 ** Run a prepared statement
20820 */
exec_prepared_stmt(ShellState * pArg,sqlite3_stmt * pStmt)20821 static void exec_prepared_stmt(
20822   ShellState *pArg,                                /* Pointer to ShellState */
20823   sqlite3_stmt *pStmt                              /* Statement to run */
20824 ){
20825   int rc;
20826   sqlite3_uint64 nRow = 0;
20827 
20828   if( pArg->cMode==MODE_Column
20829    || pArg->cMode==MODE_Table
20830    || pArg->cMode==MODE_Box
20831    || pArg->cMode==MODE_Markdown
20832   ){
20833     exec_prepared_stmt_columnar(pArg, pStmt);
20834     return;
20835   }
20836 
20837   /* perform the first step.  this will tell us if we
20838   ** have a result set or not and how wide it is.
20839   */
20840   rc = sqlite3_step(pStmt);
20841   /* if we have a result set... */
20842   if( SQLITE_ROW == rc ){
20843     /* allocate space for col name ptr, value ptr, and type */
20844     int nCol = sqlite3_column_count(pStmt);
20845     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
20846     if( !pData ){
20847       shell_out_of_memory();
20848     }else{
20849       char **azCols = (char **)pData;      /* Names of result columns */
20850       char **azVals = &azCols[nCol];       /* Results */
20851       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
20852       int i, x;
20853       assert(sizeof(int) <= sizeof(char *));
20854       /* save off ptrs to column names */
20855       for(i=0; i<nCol; i++){
20856         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
20857       }
20858       do{
20859         nRow++;
20860         /* extract the data and data types */
20861         for(i=0; i<nCol; i++){
20862           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
20863           if( x==SQLITE_BLOB
20864            && pArg
20865            && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
20866           ){
20867             azVals[i] = "";
20868           }else{
20869             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
20870           }
20871           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
20872             rc = SQLITE_NOMEM;
20873             break; /* from for */
20874           }
20875         } /* end for */
20876 
20877         /* if data and types extracted successfully... */
20878         if( SQLITE_ROW == rc ){
20879           /* call the supplied callback with the result row data */
20880           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
20881             rc = SQLITE_ABORT;
20882           }else{
20883             rc = sqlite3_step(pStmt);
20884           }
20885         }
20886       } while( SQLITE_ROW == rc );
20887       sqlite3_free(pData);
20888       if( pArg->cMode==MODE_Json ){
20889         fputs("]\n", pArg->out);
20890       }else if( pArg->cMode==MODE_Count ){
20891         char zBuf[200];
20892         sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
20893                          nRow, nRow!=1 ? "s" : "");
20894         printf("%s", zBuf);
20895       }
20896     }
20897   }
20898 }
20899 
20900 #ifndef SQLITE_OMIT_VIRTUALTABLE
20901 /*
20902 ** This function is called to process SQL if the previous shell command
20903 ** was ".expert". It passes the SQL in the second argument directly to
20904 ** the sqlite3expert object.
20905 **
20906 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
20907 ** code. In this case, (*pzErr) may be set to point to a buffer containing
20908 ** an English language error message. It is the responsibility of the
20909 ** caller to eventually free this buffer using sqlite3_free().
20910 */
expertHandleSQL(ShellState * pState,const char * zSql,char ** pzErr)20911 static int expertHandleSQL(
20912   ShellState *pState,
20913   const char *zSql,
20914   char **pzErr
20915 ){
20916   assert( pState->expert.pExpert );
20917   assert( pzErr==0 || *pzErr==0 );
20918   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
20919 }
20920 
20921 /*
20922 ** This function is called either to silently clean up the object
20923 ** created by the ".expert" command (if bCancel==1), or to generate a
20924 ** report from it and then clean it up (if bCancel==0).
20925 **
20926 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
20927 ** code. In this case, (*pzErr) may be set to point to a buffer containing
20928 ** an English language error message. It is the responsibility of the
20929 ** caller to eventually free this buffer using sqlite3_free().
20930 */
expertFinish(ShellState * pState,int bCancel,char ** pzErr)20931 static int expertFinish(
20932   ShellState *pState,
20933   int bCancel,
20934   char **pzErr
20935 ){
20936   int rc = SQLITE_OK;
20937   sqlite3expert *p = pState->expert.pExpert;
20938   assert( p );
20939   assert( bCancel || pzErr==0 || *pzErr==0 );
20940   if( bCancel==0 ){
20941     int bVerbose = pState->expert.bVerbose;
20942 
20943     rc = sqlite3_expert_analyze(p, pzErr);
20944     if( rc==SQLITE_OK ){
20945       int nQuery = sqlite3_expert_count(p);
20946       int i;
20947 
20948       if( bVerbose ){
20949         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
20950         oputz("-- Candidates -----------------------------\n");
20951         oputf("%s\n", zCand);
20952       }
20953       for(i=0; i<nQuery; i++){
20954         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
20955         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
20956         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
20957         if( zIdx==0 ) zIdx = "(no new indexes)\n";
20958         if( bVerbose ){
20959           oputf("-- Query %d --------------------------------\n",i+1);
20960           oputf("%s\n\n", zSql);
20961         }
20962         oputf("%s\n", zIdx);
20963         oputf("%s\n", zEQP);
20964       }
20965     }
20966   }
20967   sqlite3_expert_destroy(p);
20968   pState->expert.pExpert = 0;
20969   return rc;
20970 }
20971 
20972 /*
20973 ** Implementation of ".expert" dot command.
20974 */
expertDotCommand(ShellState * pState,char ** azArg,int nArg)20975 static int expertDotCommand(
20976   ShellState *pState,             /* Current shell tool state */
20977   char **azArg,                   /* Array of arguments passed to dot command */
20978   int nArg                        /* Number of entries in azArg[] */
20979 ){
20980   int rc = SQLITE_OK;
20981   char *zErr = 0;
20982   int i;
20983   int iSample = 0;
20984 
20985   assert( pState->expert.pExpert==0 );
20986   memset(&pState->expert, 0, sizeof(ExpertInfo));
20987 
20988   for(i=1; rc==SQLITE_OK && i<nArg; i++){
20989     char *z = azArg[i];
20990     int n;
20991     if( z[0]=='-' && z[1]=='-' ) z++;
20992     n = strlen30(z);
20993     if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
20994       pState->expert.bVerbose = 1;
20995     }
20996     else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
20997       if( i==(nArg-1) ){
20998         eputf("option requires an argument: %s\n", z);
20999         rc = SQLITE_ERROR;
21000       }else{
21001         iSample = (int)integerValue(azArg[++i]);
21002         if( iSample<0 || iSample>100 ){
21003           eputf("value out of range: %s\n", azArg[i]);
21004           rc = SQLITE_ERROR;
21005         }
21006       }
21007     }
21008     else{
21009       eputf("unknown option: %s\n", z);
21010       rc = SQLITE_ERROR;
21011     }
21012   }
21013 
21014   if( rc==SQLITE_OK ){
21015     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
21016     if( pState->expert.pExpert==0 ){
21017       eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
21018       rc = SQLITE_ERROR;
21019     }else{
21020       sqlite3_expert_config(
21021           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
21022       );
21023     }
21024   }
21025   sqlite3_free(zErr);
21026 
21027   return rc;
21028 }
21029 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
21030 
21031 /*
21032 ** Execute a statement or set of statements.  Print
21033 ** any result rows/columns depending on the current mode
21034 ** set via the supplied callback.
21035 **
21036 ** This is very similar to SQLite's built-in sqlite3_exec()
21037 ** function except it takes a slightly different callback
21038 ** and callback data argument.
21039 */
shell_exec(ShellState * pArg,const char * zSql,char ** pzErrMsg)21040 static int shell_exec(
21041   ShellState *pArg,                         /* Pointer to ShellState */
21042   const char *zSql,                         /* SQL to be evaluated */
21043   char **pzErrMsg                           /* Error msg written here */
21044 ){
21045   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
21046   int rc = SQLITE_OK;             /* Return Code */
21047   int rc2;
21048   const char *zLeftover;          /* Tail of unprocessed SQL */
21049   sqlite3 *db = pArg->db;
21050 
21051   if( pzErrMsg ){
21052     *pzErrMsg = NULL;
21053   }
21054 
21055 #ifndef SQLITE_OMIT_VIRTUALTABLE
21056   if( pArg->expert.pExpert ){
21057     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
21058     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
21059   }
21060 #endif
21061 
21062   while( zSql[0] && (SQLITE_OK == rc) ){
21063     static const char *zStmtSql;
21064     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
21065     if( SQLITE_OK != rc ){
21066       if( pzErrMsg ){
21067         *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
21068       }
21069     }else{
21070       if( !pStmt ){
21071         /* this happens for a comment or white-space */
21072         zSql = zLeftover;
21073         while( IsSpace(zSql[0]) ) zSql++;
21074         continue;
21075       }
21076       zStmtSql = sqlite3_sql(pStmt);
21077       if( zStmtSql==0 ) zStmtSql = "";
21078       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
21079 
21080       /* save off the prepared statement handle and reset row count */
21081       if( pArg ){
21082         pArg->pStmt = pStmt;
21083         pArg->cnt = 0;
21084       }
21085 
21086       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
21087       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
21088         sqlite3_stmt *pExplain;
21089         int triggerEQP = 0;
21090         disable_debug_trace_modes();
21091         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
21092         if( pArg->autoEQP>=AUTOEQP_trigger ){
21093           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
21094         }
21095         pExplain = pStmt;
21096         sqlite3_reset(pExplain);
21097         rc = sqlite3_stmt_explain(pExplain, 2);
21098         if( rc==SQLITE_OK ){
21099           while( sqlite3_step(pExplain)==SQLITE_ROW ){
21100             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
21101             int iEqpId = sqlite3_column_int(pExplain, 0);
21102             int iParentId = sqlite3_column_int(pExplain, 1);
21103             if( zEQPLine==0 ) zEQPLine = "";
21104             if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
21105             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
21106           }
21107           eqp_render(pArg, 0);
21108         }
21109         if( pArg->autoEQP>=AUTOEQP_full ){
21110           /* Also do an EXPLAIN for ".eqp full" mode */
21111           sqlite3_reset(pExplain);
21112           rc = sqlite3_stmt_explain(pExplain, 1);
21113           if( rc==SQLITE_OK ){
21114             pArg->cMode = MODE_Explain;
21115             assert( sqlite3_stmt_isexplain(pExplain)==1 );
21116             explain_data_prepare(pArg, pExplain);
21117             exec_prepared_stmt(pArg, pExplain);
21118             explain_data_delete(pArg);
21119           }
21120         }
21121         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
21122           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
21123         }
21124         sqlite3_reset(pStmt);
21125         sqlite3_stmt_explain(pStmt, 0);
21126         restore_debug_trace_modes();
21127       }
21128 
21129       if( pArg ){
21130         int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
21131         pArg->cMode = pArg->mode;
21132         if( pArg->autoExplain ){
21133           if( bIsExplain ){
21134             pArg->cMode = MODE_Explain;
21135           }
21136           if( sqlite3_stmt_isexplain(pStmt)==2 ){
21137             pArg->cMode = MODE_EQP;
21138           }
21139         }
21140 
21141         /* If the shell is currently in ".explain" mode, gather the extra
21142         ** data required to add indents to the output.*/
21143         if( pArg->cMode==MODE_Explain && bIsExplain ){
21144           explain_data_prepare(pArg, pStmt);
21145         }
21146       }
21147 
21148       bind_prepared_stmt(pArg, pStmt);
21149       exec_prepared_stmt(pArg, pStmt);
21150       explain_data_delete(pArg);
21151       eqp_render(pArg, 0);
21152 
21153       /* print usage stats if stats on */
21154       if( pArg && pArg->statsOn ){
21155         display_stats(db, pArg, 0);
21156       }
21157 
21158       /* print loop-counters if required */
21159       if( pArg && pArg->scanstatsOn ){
21160         display_scanstats(db, pArg);
21161       }
21162 
21163       /* Finalize the statement just executed. If this fails, save a
21164       ** copy of the error message. Otherwise, set zSql to point to the
21165       ** next statement to execute. */
21166       rc2 = sqlite3_finalize(pStmt);
21167       if( rc!=SQLITE_NOMEM ) rc = rc2;
21168       if( rc==SQLITE_OK ){
21169         zSql = zLeftover;
21170         while( IsSpace(zSql[0]) ) zSql++;
21171       }else if( pzErrMsg ){
21172         *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
21173       }
21174 
21175       /* clear saved stmt handle */
21176       if( pArg ){
21177         pArg->pStmt = NULL;
21178       }
21179     }
21180   } /* end while */
21181 
21182   return rc;
21183 }
21184 
21185 /*
21186 ** Release memory previously allocated by tableColumnList().
21187 */
freeColumnList(char ** azCol)21188 static void freeColumnList(char **azCol){
21189   int i;
21190   for(i=1; azCol[i]; i++){
21191     sqlite3_free(azCol[i]);
21192   }
21193   /* azCol[0] is a static string */
21194   sqlite3_free(azCol);
21195 }
21196 
21197 /*
21198 ** Return a list of pointers to strings which are the names of all
21199 ** columns in table zTab.   The memory to hold the names is dynamically
21200 ** allocated and must be released by the caller using a subsequent call
21201 ** to freeColumnList().
21202 **
21203 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
21204 ** value that needs to be preserved, then azCol[0] is filled in with the
21205 ** name of the rowid column.
21206 **
21207 ** The first regular column in the table is azCol[1].  The list is terminated
21208 ** by an entry with azCol[i]==0.
21209 */
tableColumnList(ShellState * p,const char * zTab)21210 static char **tableColumnList(ShellState *p, const char *zTab){
21211   char **azCol = 0;
21212   sqlite3_stmt *pStmt;
21213   char *zSql;
21214   int nCol = 0;
21215   int nAlloc = 0;
21216   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
21217   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
21218   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
21219   int rc;
21220 
21221   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
21222   shell_check_oom(zSql);
21223   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21224   sqlite3_free(zSql);
21225   if( rc ) return 0;
21226   while( sqlite3_step(pStmt)==SQLITE_ROW ){
21227     if( nCol>=nAlloc-2 ){
21228       nAlloc = nAlloc*2 + nCol + 10;
21229       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
21230       shell_check_oom(azCol);
21231     }
21232     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
21233     shell_check_oom(azCol[nCol]);
21234     if( sqlite3_column_int(pStmt, 5) ){
21235       nPK++;
21236       if( nPK==1
21237        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
21238                           "INTEGER")==0
21239       ){
21240         isIPK = 1;
21241       }else{
21242         isIPK = 0;
21243       }
21244     }
21245   }
21246   sqlite3_finalize(pStmt);
21247   if( azCol==0 ) return 0;
21248   azCol[0] = 0;
21249   azCol[nCol+1] = 0;
21250 
21251   /* The decision of whether or not a rowid really needs to be preserved
21252   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
21253   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
21254   ** rowids on tables where the rowid is inaccessible because there are other
21255   ** columns in the table named "rowid", "_rowid_", and "oid".
21256   */
21257   if( preserveRowid && isIPK ){
21258     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
21259     ** might be an alias for the ROWID.  But it might also be a WITHOUT ROWID
21260     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
21261     ** ROWID aliases.  To distinguish these cases, check to see if
21262     ** there is a "pk" entry in "PRAGMA index_list".  There will be
21263     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
21264     */
21265     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
21266                            " WHERE origin='pk'", zTab);
21267     shell_check_oom(zSql);
21268     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21269     sqlite3_free(zSql);
21270     if( rc ){
21271       freeColumnList(azCol);
21272       return 0;
21273     }
21274     rc = sqlite3_step(pStmt);
21275     sqlite3_finalize(pStmt);
21276     preserveRowid = rc==SQLITE_ROW;
21277   }
21278   if( preserveRowid ){
21279     /* Only preserve the rowid if we can find a name to use for the
21280     ** rowid */
21281     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
21282     int i, j;
21283     for(j=0; j<3; j++){
21284       for(i=1; i<=nCol; i++){
21285         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
21286       }
21287       if( i>nCol ){
21288         /* At this point, we know that azRowid[j] is not the name of any
21289         ** ordinary column in the table.  Verify that azRowid[j] is a valid
21290         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
21291         ** tables will fail this last check */
21292         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
21293         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
21294         break;
21295       }
21296     }
21297   }
21298   return azCol;
21299 }
21300 
21301 /*
21302 ** Toggle the reverse_unordered_selects setting.
21303 */
toggleSelectOrder(sqlite3 * db)21304 static void toggleSelectOrder(sqlite3 *db){
21305   sqlite3_stmt *pStmt = 0;
21306   int iSetting = 0;
21307   char zStmt[100];
21308   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
21309   if( sqlite3_step(pStmt)==SQLITE_ROW ){
21310     iSetting = sqlite3_column_int(pStmt, 0);
21311   }
21312   sqlite3_finalize(pStmt);
21313   sqlite3_snprintf(sizeof(zStmt), zStmt,
21314        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
21315   sqlite3_exec(db, zStmt, 0, 0, 0);
21316 }
21317 
21318 /*
21319 ** This is a different callback routine used for dumping the database.
21320 ** Each row received by this callback consists of a table name,
21321 ** the table type ("index" or "table") and SQL to create the table.
21322 ** This routine should print text sufficient to recreate the table.
21323 */
dump_callback(void * pArg,int nArg,char ** azArg,char ** azNotUsed)21324 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
21325   int rc;
21326   const char *zTable;
21327   const char *zType;
21328   const char *zSql;
21329   ShellState *p = (ShellState *)pArg;
21330   int dataOnly;
21331   int noSys;
21332 
21333   UNUSED_PARAMETER(azNotUsed);
21334   if( nArg!=3 || azArg==0 ) return 0;
21335   zTable = azArg[0];
21336   zType = azArg[1];
21337   zSql = azArg[2];
21338   if( zTable==0 ) return 0;
21339   if( zType==0 ) return 0;
21340   dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
21341   noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
21342 
21343   if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
21344     if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
21345   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
21346     if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
21347   }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
21348     return 0;
21349   }else if( dataOnly ){
21350     /* no-op */
21351   }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
21352     char *zIns;
21353     if( !p->writableSchema ){
21354       oputz("PRAGMA writable_schema=ON;\n");
21355       p->writableSchema = 1;
21356     }
21357     zIns = sqlite3_mprintf(
21358        "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
21359        "VALUES('table','%q','%q',0,'%q');",
21360        zTable, zTable, zSql);
21361     shell_check_oom(zIns);
21362     oputf("%s\n", zIns);
21363     sqlite3_free(zIns);
21364     return 0;
21365   }else{
21366     printSchemaLine(zSql, ";\n");
21367   }
21368 
21369   if( cli_strcmp(zType, "table")==0 ){
21370     ShellText sSelect;
21371     ShellText sTable;
21372     char **azCol;
21373     int i;
21374     char *savedDestTable;
21375     int savedMode;
21376 
21377     azCol = tableColumnList(p, zTable);
21378     if( azCol==0 ){
21379       p->nErr++;
21380       return 0;
21381     }
21382 
21383     /* Always quote the table name, even if it appears to be pure ascii,
21384     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
21385     initText(&sTable);
21386     appendText(&sTable, zTable, quoteChar(zTable));
21387     /* If preserving the rowid, add a column list after the table name.
21388     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
21389     ** instead of the usual "INSERT INTO tab VALUES(...)".
21390     */
21391     if( azCol[0] ){
21392       appendText(&sTable, "(", 0);
21393       appendText(&sTable, azCol[0], 0);
21394       for(i=1; azCol[i]; i++){
21395         appendText(&sTable, ",", 0);
21396         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
21397       }
21398       appendText(&sTable, ")", 0);
21399     }
21400 
21401     /* Build an appropriate SELECT statement */
21402     initText(&sSelect);
21403     appendText(&sSelect, "SELECT ", 0);
21404     if( azCol[0] ){
21405       appendText(&sSelect, azCol[0], 0);
21406       appendText(&sSelect, ",", 0);
21407     }
21408     for(i=1; azCol[i]; i++){
21409       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
21410       if( azCol[i+1] ){
21411         appendText(&sSelect, ",", 0);
21412       }
21413     }
21414     freeColumnList(azCol);
21415     appendText(&sSelect, " FROM ", 0);
21416     appendText(&sSelect, zTable, quoteChar(zTable));
21417 
21418     savedDestTable = p->zDestTable;
21419     savedMode = p->mode;
21420     p->zDestTable = sTable.z;
21421     p->mode = p->cMode = MODE_Insert;
21422     rc = shell_exec(p, sSelect.z, 0);
21423     if( (rc&0xff)==SQLITE_CORRUPT ){
21424       oputz("/****** CORRUPTION ERROR *******/\n");
21425       toggleSelectOrder(p->db);
21426       shell_exec(p, sSelect.z, 0);
21427       toggleSelectOrder(p->db);
21428     }
21429     p->zDestTable = savedDestTable;
21430     p->mode = savedMode;
21431     freeText(&sTable);
21432     freeText(&sSelect);
21433     if( rc ) p->nErr++;
21434   }
21435   return 0;
21436 }
21437 
21438 /*
21439 ** Run zQuery.  Use dump_callback() as the callback routine so that
21440 ** the contents of the query are output as SQL statements.
21441 **
21442 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
21443 ** "ORDER BY rowid DESC" to the end.
21444 */
run_schema_dump_query(ShellState * p,const char * zQuery)21445 static int run_schema_dump_query(
21446   ShellState *p,
21447   const char *zQuery
21448 ){
21449   int rc;
21450   char *zErr = 0;
21451   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
21452   if( rc==SQLITE_CORRUPT ){
21453     char *zQ2;
21454     int len = strlen30(zQuery);
21455     oputz("/****** CORRUPTION ERROR *******/\n");
21456     if( zErr ){
21457       oputf("/****** %s ******/\n", zErr);
21458       sqlite3_free(zErr);
21459       zErr = 0;
21460     }
21461     zQ2 = malloc( len+100 );
21462     if( zQ2==0 ) return rc;
21463     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
21464     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
21465     if( rc ){
21466       oputf("/****** ERROR: %s ******/\n", zErr);
21467     }else{
21468       rc = SQLITE_CORRUPT;
21469     }
21470     sqlite3_free(zErr);
21471     free(zQ2);
21472   }
21473   return rc;
21474 }
21475 
21476 /*
21477 ** Text of help messages.
21478 **
21479 ** The help text for each individual command begins with a line that starts
21480 ** with ".".  Subsequent lines are supplemental information.
21481 **
21482 ** There must be two or more spaces between the end of the command and the
21483 ** start of the description of what that command does.
21484 */
21485 static const char *(azHelp[]) = {
21486 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
21487   && !defined(SQLITE_SHELL_FIDDLE)
21488   ".archive ...             Manage SQL archives",
21489   "   Each command must have exactly one of the following options:",
21490   "     -c, --create               Create a new archive",
21491   "     -u, --update               Add or update files with changed mtime",
21492   "     -i, --insert               Like -u but always add even if unchanged",
21493   "     -r, --remove               Remove files from archive",
21494   "     -t, --list                 List contents of archive",
21495   "     -x, --extract              Extract files from archive",
21496   "   Optional arguments:",
21497   "     -v, --verbose              Print each filename as it is processed",
21498   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
21499   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
21500   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
21501   "     -g, --glob                 Use glob matching for names in archive",
21502   "     -n, --dryrun               Show the SQL that would have occurred",
21503   "   Examples:",
21504   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
21505   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
21506   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
21507   "   See also:",
21508   "      http://sqlite.org/cli.html#sqlite_archive_support",
21509 #endif
21510 #ifndef SQLITE_OMIT_AUTHORIZATION
21511   ".auth ON|OFF             Show authorizer callbacks",
21512 #endif
21513 #ifndef SQLITE_SHELL_FIDDLE
21514   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
21515   "   Options:",
21516   "       --append            Use the appendvfs",
21517   "       --async             Write to FILE without journal and fsync()",
21518 #endif
21519   ".bail on|off             Stop after hitting an error.  Default OFF",
21520 #ifndef SQLITE_SHELL_FIDDLE
21521   ".cd DIRECTORY            Change the working directory to DIRECTORY",
21522 #endif
21523   ".changes on|off          Show number of rows changed by SQL",
21524 #ifndef SQLITE_SHELL_FIDDLE
21525   ".check GLOB              Fail if output since .testcase does not match",
21526   ".clone NEWDB             Clone data into NEWDB from the existing database",
21527 #endif
21528   ".connection [close] [#]  Open or close an auxiliary database connection",
21529 #if defined(_WIN32) || defined(WIN32)
21530   ".crnl on|off             Translate \\n to \\r\\n.  Default ON",
21531 #endif
21532   ".databases               List names and files of attached databases",
21533   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
21534 #if SQLITE_SHELL_HAVE_RECOVER
21535   ".dbinfo ?DB?             Show status information about the database",
21536 #endif
21537   ".dump ?OBJECTS?          Render database content as SQL",
21538   "   Options:",
21539   "     --data-only            Output only INSERT statements",
21540   "     --newlines             Allow unescaped newline characters in output",
21541   "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
21542   "     --preserve-rowids      Include ROWID values in the output",
21543   "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
21544   "   Additional LIKE patterns can be given in subsequent arguments",
21545   ".echo on|off             Turn command echo on or off",
21546   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
21547   "   Other Modes:",
21548 #ifdef SQLITE_DEBUG
21549   "      test                  Show raw EXPLAIN QUERY PLAN output",
21550   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
21551 #endif
21552   "      trigger               Like \"full\" but also show trigger bytecode",
21553 #ifndef SQLITE_SHELL_FIDDLE
21554   ".excel                   Display the output of next command in spreadsheet",
21555   "   --bom                   Put a UTF8 byte-order mark on intermediate file",
21556 #endif
21557 #ifndef SQLITE_SHELL_FIDDLE
21558   ".exit ?CODE?             Exit this program with return-code CODE",
21559 #endif
21560   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
21561   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
21562   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
21563   "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
21564   "   --help                  Show CMD details",
21565   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
21566   ".headers on|off          Turn display of headers on or off",
21567   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
21568 #ifndef SQLITE_SHELL_FIDDLE
21569   ".import FILE TABLE       Import data from FILE into TABLE",
21570   "   Options:",
21571   "     --ascii               Use \\037 and \\036 as column and row separators",
21572   "     --csv                 Use , and \\n as column and row separators",
21573   "     --skip N              Skip the first N rows of input",
21574   "     --schema S            Target table to be S.TABLE",
21575   "     -v                    \"Verbose\" - increase auxiliary output",
21576   "   Notes:",
21577   "     *  If TABLE does not exist, it is created.  The first row of input",
21578   "        determines the column names.",
21579   "     *  If neither --csv or --ascii are used, the input mode is derived",
21580   "        from the \".mode\" output mode",
21581   "     *  If FILE begins with \"|\" then it is a command that generates the",
21582   "        input text.",
21583 #endif
21584 #ifndef SQLITE_OMIT_TEST_CONTROL
21585   ",imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
21586 #endif
21587   ".indexes ?TABLE?         Show names of indexes",
21588   "                           If TABLE is specified, only show indexes for",
21589   "                           tables matching TABLE using the LIKE operator.",
21590 #ifdef SQLITE_ENABLE_IOTRACE
21591   ",iotrace FILE            Enable I/O diagnostic logging to FILE",
21592 #endif
21593   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
21594   ".lint OPTIONS            Report potential schema issues.",
21595   "     Options:",
21596   "        fkey-indexes     Find missing foreign key indexes",
21597 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
21598   ".load FILE ?ENTRY?       Load an extension library",
21599 #endif
21600 #if !defined(SQLITE_SHELL_FIDDLE)
21601   ".log FILE|on|off         Turn logging on or off.  FILE can be stderr/stdout",
21602 #else
21603   ".log on|off              Turn logging on or off.",
21604 #endif
21605   ".mode MODE ?OPTIONS?     Set output mode",
21606   "   MODE is one of:",
21607   "     ascii       Columns/rows delimited by 0x1F and 0x1E",
21608   "     box         Tables using unicode box-drawing characters",
21609   "     csv         Comma-separated values",
21610   "     column      Output in columns.  (See .width)",
21611   "     html        HTML <table> code",
21612   "     insert      SQL insert statements for TABLE",
21613   "     json        Results in a JSON array",
21614   "     line        One value per line",
21615   "     list        Values delimited by \"|\"",
21616   "     markdown    Markdown table format",
21617   "     qbox        Shorthand for \"box --wrap 60 --quote\"",
21618   "     quote       Escape answers as for SQL",
21619   "     table       ASCII-art table",
21620   "     tabs        Tab-separated values",
21621   "     tcl         TCL list elements",
21622   "   OPTIONS: (for columnar modes or insert mode):",
21623   "     --wrap N       Wrap output lines to no longer than N characters",
21624   "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
21625   "     --ww           Shorthand for \"--wordwrap 1\"",
21626   "     --quote        Quote output text as SQL literals",
21627   "     --noquote      Do not quote output text",
21628   "     TABLE          The name of SQL table used for \"insert\" mode",
21629 #ifndef SQLITE_SHELL_FIDDLE
21630   ".nonce STRING            Suspend safe mode for one command if nonce matches",
21631 #endif
21632   ".nullvalue STRING        Use STRING in place of NULL values",
21633 #ifndef SQLITE_SHELL_FIDDLE
21634   ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
21635   "     If FILE begins with '|' then open as a pipe",
21636   "       --bom  Put a UTF8 byte-order mark at the beginning",
21637   "       -e     Send output to the system text editor",
21638   "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
21639   /* Note that .open is (partially) available in WASM builds but is
21640   ** currently only intended to be used by the fiddle tool, not
21641   ** end users, so is "undocumented." */
21642   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
21643   "     Options:",
21644   "        --append        Use appendvfs to append database to the end of FILE",
21645 #endif
21646 #ifndef SQLITE_OMIT_DESERIALIZE
21647   "        --deserialize   Load into memory using sqlite3_deserialize()",
21648   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
21649   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
21650 #endif
21651   "        --new           Initialize FILE to an empty database",
21652   "        --nofollow      Do not follow symbolic links",
21653   "        --readonly      Open FILE readonly",
21654   "        --zip           FILE is a ZIP archive",
21655 #ifndef SQLITE_SHELL_FIDDLE
21656   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
21657   "   If FILE begins with '|' then open it as a pipe.",
21658   "   Options:",
21659   "     --bom                 Prefix output with a UTF8 byte-order mark",
21660   "     -e                    Send output to the system text editor",
21661   "     -x                    Send output as CSV to a spreadsheet",
21662 #endif
21663   ".parameter CMD ...       Manage SQL parameter bindings",
21664   "   clear                   Erase all bindings",
21665   "   init                    Initialize the TEMP table that holds bindings",
21666   "   list                    List the current parameter bindings",
21667   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
21668   "                           PARAMETER should start with one of: $ : @ ?",
21669   "   unset PARAMETER         Remove PARAMETER from the binding table",
21670   ".print STRING...         Print literal STRING",
21671 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
21672   ".progress N              Invoke progress handler after every N opcodes",
21673   "   --limit N                 Interrupt after N progress callbacks",
21674   "   --once                    Do no more than one progress interrupt",
21675   "   --quiet|-q                No output except at interrupts",
21676   "   --reset                   Reset the count for each input and interrupt",
21677 #endif
21678   ".prompt MAIN CONTINUE    Replace the standard prompts",
21679 #ifndef SQLITE_SHELL_FIDDLE
21680   ".quit                    Stop interpreting input stream, exit if primary.",
21681   ".read FILE               Read input from FILE or command output",
21682   "    If FILE begins with \"|\", it is a command that generates the input.",
21683 #endif
21684 #if SQLITE_SHELL_HAVE_RECOVER
21685   ".recover                 Recover as much data as possible from corrupt db.",
21686   "   --ignore-freelist        Ignore pages that appear to be on db freelist",
21687   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
21688   "   --no-rowids              Do not attempt to recover rowid values",
21689   "                            that are not also INTEGER PRIMARY KEYs",
21690 #endif
21691 #ifndef SQLITE_SHELL_FIDDLE
21692   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
21693   ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
21694 #endif
21695   ".scanstats on|off|est    Turn sqlite3_stmt_scanstatus() metrics on or off",
21696   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
21697   "   Options:",
21698   "      --indent             Try to pretty-print the schema",
21699   "      --nosys              Omit objects whose names start with \"sqlite_\"",
21700   ",selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
21701   "    Options:",
21702   "       --init               Create a new SELFTEST table",
21703   "       -v                   Verbose output",
21704   ".separator COL ?ROW?     Change the column and row separators",
21705 #if defined(SQLITE_ENABLE_SESSION)
21706   ".session ?NAME? CMD ...  Create or control sessions",
21707   "   Subcommands:",
21708   "     attach TABLE             Attach TABLE",
21709   "     changeset FILE           Write a changeset into FILE",
21710   "     close                    Close one session",
21711   "     enable ?BOOLEAN?         Set or query the enable bit",
21712   "     filter GLOB...           Reject tables matching GLOBs",
21713   "     indirect ?BOOLEAN?       Mark or query the indirect status",
21714   "     isempty                  Query whether the session is empty",
21715   "     list                     List currently open session names",
21716   "     open DB NAME             Open a new session on DB",
21717   "     patchset FILE            Write a patchset into FILE",
21718   "   If ?NAME? is omitted, the first defined session is used.",
21719 #endif
21720   ".sha3sum ...             Compute a SHA3 hash of database content",
21721   "    Options:",
21722   "      --schema              Also hash the sqlite_schema table",
21723   "      --sha3-224            Use the sha3-224 algorithm",
21724   "      --sha3-256            Use the sha3-256 algorithm (default)",
21725   "      --sha3-384            Use the sha3-384 algorithm",
21726   "      --sha3-512            Use the sha3-512 algorithm",
21727   "    Any other argument is a LIKE pattern for tables to hash",
21728 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21729   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
21730 #endif
21731   ".show                    Show the current values for various settings",
21732   ".stats ?ARG?             Show stats or turn stats on or off",
21733   "   off                      Turn off automatic stat display",
21734   "   on                       Turn on automatic stat display",
21735   "   stmt                     Show statement stats",
21736   "   vmstep                   Show the virtual machine step count only",
21737 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21738   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
21739 #endif
21740   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
21741 #ifndef SQLITE_SHELL_FIDDLE
21742   ",testcase NAME           Begin redirecting output to 'testcase-out.txt'",
21743 #endif
21744   ",testctrl CMD ...        Run various sqlite3_test_control() operations",
21745   "                           Run \".testctrl\" with no arguments for details",
21746   ".timeout MS              Try opening locked tables for MS milliseconds",
21747   ".timer on|off            Turn SQL timer on or off",
21748 #ifndef SQLITE_OMIT_TRACE
21749   ".trace ?OPTIONS?         Output each SQL statement as it is run",
21750   "    FILE                    Send output to FILE",
21751   "    stdout                  Send output to stdout",
21752   "    stderr                  Send output to stderr",
21753   "    off                     Disable tracing",
21754   "    --expanded              Expand query parameters",
21755 #ifdef SQLITE_ENABLE_NORMALIZE
21756   "    --normalized            Normal the SQL statements",
21757 #endif
21758   "    --plain                 Show SQL as it is input",
21759   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
21760   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
21761   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
21762   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
21763 #endif /* SQLITE_OMIT_TRACE */
21764 #ifdef SQLITE_DEBUG
21765   ".unmodule NAME ...       Unregister virtual table modules",
21766   "    --allexcept             Unregister everything except those named",
21767 #endif
21768   ".version                 Show source, library and compiler versions",
21769   ".vfsinfo ?AUX?           Information about the top-level VFS",
21770   ".vfslist                 List all available VFSes",
21771   ".vfsname ?AUX?           Print the name of the VFS stack",
21772   ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
21773   "     Negative values right-justify",
21774 };
21775 
21776 /*
21777 ** Output help text.
21778 **
21779 ** zPattern describes the set of commands for which help text is provided.
21780 ** If zPattern is NULL, then show all commands, but only give a one-line
21781 ** description of each.
21782 **
21783 ** Return the number of matches.
21784 */
showHelp(FILE * out,const char * zPattern)21785 static int showHelp(FILE *out, const char *zPattern){
21786   int i = 0;
21787   int j = 0;
21788   int n = 0;
21789   char *zPat;
21790   if( zPattern==0
21791    || zPattern[0]=='0'
21792    || cli_strcmp(zPattern,"-a")==0
21793    || cli_strcmp(zPattern,"-all")==0
21794    || cli_strcmp(zPattern,"--all")==0
21795   ){
21796     enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
21797     enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
21798     /* Show all or most commands
21799     ** *zPattern==0   => summary of documented commands only
21800     ** *zPattern=='0' => whole help for undocumented commands
21801     ** Otherwise      => whole help for documented commands
21802     */
21803     enum HelpWanted hw = HW_SummaryOnly;
21804     enum HelpHave hh = HH_More;
21805     if( zPattern!=0 ){
21806       hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
21807     }
21808     for(i=0; i<ArraySize(azHelp); i++){
21809       switch( azHelp[i][0] ){
21810       case ',':
21811         hh = HH_Summary|HH_Undoc;
21812         break;
21813       case '.':
21814         hh = HH_Summary;
21815         break;
21816       default:
21817         hh &= ~HH_Summary;
21818         break;
21819       }
21820       if( ((hw^hh)&HH_Undoc)==0 ){
21821         if( (hh&HH_Summary)!=0 ){
21822           sputf(out, ".%s\n", azHelp[i]+1);
21823           ++n;
21824         }else if( (hw&HW_SummaryOnly)==0 ){
21825           sputf(out, "%s\n", azHelp[i]);
21826         }
21827       }
21828     }
21829   }else{
21830     /* Seek documented commands for which zPattern is an exact prefix */
21831     zPat = sqlite3_mprintf(".%s*", zPattern);
21832     shell_check_oom(zPat);
21833     for(i=0; i<ArraySize(azHelp); i++){
21834       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
21835         sputf(out, "%s\n", azHelp[i]);
21836         j = i+1;
21837         n++;
21838       }
21839     }
21840     sqlite3_free(zPat);
21841     if( n ){
21842       if( n==1 ){
21843         /* when zPattern is a prefix of exactly one command, then include
21844         ** the details of that command, which should begin at offset j */
21845         while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
21846           sputf(out, "%s\n", azHelp[j]);
21847           j++;
21848         }
21849       }
21850       return n;
21851     }
21852     /* Look for documented commands that contain zPattern anywhere.
21853     ** Show complete text of all documented commands that match. */
21854     zPat = sqlite3_mprintf("%%%s%%", zPattern);
21855     shell_check_oom(zPat);
21856     for(i=0; i<ArraySize(azHelp); i++){
21857       if( azHelp[i][0]==',' ){
21858         while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
21859         continue;
21860       }
21861       if( azHelp[i][0]=='.' ) j = i;
21862       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
21863         sputf(out, "%s\n", azHelp[j]);
21864         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
21865           j++;
21866           sputf(out, "%s\n", azHelp[j]);
21867         }
21868         i = j;
21869         n++;
21870       }
21871     }
21872     sqlite3_free(zPat);
21873   }
21874   return n;
21875 }
21876 
21877 /* Forward reference */
21878 static int process_input(ShellState *p);
21879 
21880 /*
21881 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
21882 ** and return a pointer to the buffer. The caller is responsible for freeing
21883 ** the memory.
21884 **
21885 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
21886 ** read.
21887 **
21888 ** For convenience, a nul-terminator byte is always appended to the data read
21889 ** from the file before the buffer is returned. This byte is not included in
21890 ** the final value of (*pnByte), if applicable.
21891 **
21892 ** NULL is returned if any error is encountered. The final value of *pnByte
21893 ** is undefined in this case.
21894 */
readFile(const char * zName,int * pnByte)21895 static char *readFile(const char *zName, int *pnByte){
21896   FILE *in = fopen(zName, "rb");
21897   long nIn;
21898   size_t nRead;
21899   char *pBuf;
21900   int rc;
21901   if( in==0 ) return 0;
21902   rc = fseek(in, 0, SEEK_END);
21903   if( rc!=0 ){
21904     eputf("Error: '%s' not seekable\n", zName);
21905     fclose(in);
21906     return 0;
21907   }
21908   nIn = ftell(in);
21909   rewind(in);
21910   pBuf = sqlite3_malloc64( nIn+1 );
21911   if( pBuf==0 ){
21912     eputz("Error: out of memory\n");
21913     fclose(in);
21914     return 0;
21915   }
21916   nRead = fread(pBuf, nIn, 1, in);
21917   fclose(in);
21918   if( nRead!=1 ){
21919     sqlite3_free(pBuf);
21920     eputf("Error: cannot read '%s'\n", zName);
21921     return 0;
21922   }
21923   pBuf[nIn] = 0;
21924   if( pnByte ) *pnByte = nIn;
21925   return pBuf;
21926 }
21927 
21928 #if defined(SQLITE_ENABLE_SESSION)
21929 /*
21930 ** Close a single OpenSession object and release all of its associated
21931 ** resources.
21932 */
session_close(OpenSession * pSession)21933 static void session_close(OpenSession *pSession){
21934   int i;
21935   sqlite3session_delete(pSession->p);
21936   sqlite3_free(pSession->zName);
21937   for(i=0; i<pSession->nFilter; i++){
21938     sqlite3_free(pSession->azFilter[i]);
21939   }
21940   sqlite3_free(pSession->azFilter);
21941   memset(pSession, 0, sizeof(OpenSession));
21942 }
21943 #endif
21944 
21945 /*
21946 ** Close all OpenSession objects and release all associated resources.
21947 */
21948 #if defined(SQLITE_ENABLE_SESSION)
session_close_all(ShellState * p,int i)21949 static void session_close_all(ShellState *p, int i){
21950   int j;
21951   struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
21952   for(j=0; j<pAuxDb->nSession; j++){
21953     session_close(&pAuxDb->aSession[j]);
21954   }
21955   pAuxDb->nSession = 0;
21956 }
21957 #else
21958 # define session_close_all(X,Y)
21959 #endif
21960 
21961 /*
21962 ** Implementation of the xFilter function for an open session.  Omit
21963 ** any tables named by ".session filter" but let all other table through.
21964 */
21965 #if defined(SQLITE_ENABLE_SESSION)
session_filter(void * pCtx,const char * zTab)21966 static int session_filter(void *pCtx, const char *zTab){
21967   OpenSession *pSession = (OpenSession*)pCtx;
21968   int i;
21969   for(i=0; i<pSession->nFilter; i++){
21970     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
21971   }
21972   return 1;
21973 }
21974 #endif
21975 
21976 /*
21977 ** Try to deduce the type of file for zName based on its content.  Return
21978 ** one of the SHELL_OPEN_* constants.
21979 **
21980 ** If the file does not exist or is empty but its name looks like a ZIP
21981 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
21982 ** Otherwise, assume an ordinary database regardless of the filename if
21983 ** the type cannot be determined from content.
21984 */
deduceDatabaseType(const char * zName,int dfltZip)21985 int deduceDatabaseType(const char *zName, int dfltZip){
21986   FILE *f = fopen(zName, "rb");
21987   size_t n;
21988   int rc = SHELL_OPEN_UNSPEC;
21989   char zBuf[100];
21990   if( f==0 ){
21991     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
21992        return SHELL_OPEN_ZIPFILE;
21993     }else{
21994        return SHELL_OPEN_NORMAL;
21995     }
21996   }
21997   n = fread(zBuf, 16, 1, f);
21998   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
21999     fclose(f);
22000     return SHELL_OPEN_NORMAL;
22001   }
22002   fseek(f, -25, SEEK_END);
22003   n = fread(zBuf, 25, 1, f);
22004   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
22005     rc = SHELL_OPEN_APPENDVFS;
22006   }else{
22007     fseek(f, -22, SEEK_END);
22008     n = fread(zBuf, 22, 1, f);
22009     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
22010        && zBuf[3]==0x06 ){
22011       rc = SHELL_OPEN_ZIPFILE;
22012     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
22013       rc = SHELL_OPEN_ZIPFILE;
22014     }
22015   }
22016   fclose(f);
22017   return rc;
22018 }
22019 
22020 #ifndef SQLITE_OMIT_DESERIALIZE
22021 /*
22022 ** Reconstruct an in-memory database using the output from the "dbtotxt"
22023 ** program.  Read content from the file in p->aAuxDb[].zDbFilename.
22024 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
22025 */
readHexDb(ShellState * p,int * pnData)22026 static unsigned char *readHexDb(ShellState *p, int *pnData){
22027   unsigned char *a = 0;
22028   int nLine;
22029   int n = 0;
22030   int pgsz = 0;
22031   int iOffset = 0;
22032   int j, k;
22033   int rc;
22034   FILE *in;
22035   const char *zDbFilename = p->pAuxDb->zDbFilename;
22036   unsigned int x[16];
22037   char zLine[1000];
22038   if( zDbFilename ){
22039     in = fopen(zDbFilename, "r");
22040     if( in==0 ){
22041       eputf("cannot open \"%s\" for reading\n", zDbFilename);
22042       return 0;
22043     }
22044     nLine = 0;
22045   }else{
22046     in = p->in;
22047     nLine = p->lineno;
22048     if( in==0 ) in = stdin;
22049   }
22050   *pnData = 0;
22051   nLine++;
22052   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
22053   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
22054   if( rc!=2 ) goto readHexDb_error;
22055   if( n<0 ) goto readHexDb_error;
22056   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
22057   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
22058   a = sqlite3_malloc( n ? n : 1 );
22059   shell_check_oom(a);
22060   memset(a, 0, n);
22061   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
22062     eputz("invalid pagesize\n");
22063     goto readHexDb_error;
22064   }
22065   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
22066     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
22067     if( rc==2 ){
22068       iOffset = k;
22069       continue;
22070     }
22071     if( cli_strncmp(zLine, "| end ", 6)==0 ){
22072       break;
22073     }
22074     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
22075                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
22076                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
22077     if( rc==17 ){
22078       k = iOffset+j;
22079       if( k+16<=n && k>=0 ){
22080         int ii;
22081         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
22082       }
22083     }
22084   }
22085   *pnData = n;
22086   if( in!=p->in ){
22087     fclose(in);
22088   }else{
22089     p->lineno = nLine;
22090   }
22091   return a;
22092 
22093 readHexDb_error:
22094   if( in!=p->in ){
22095     fclose(in);
22096   }else{
22097     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
22098       nLine++;
22099       if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
22100     }
22101     p->lineno = nLine;
22102   }
22103   sqlite3_free(a);
22104   eputf("Error on line %d of --hexdb input\n", nLine);
22105   return 0;
22106 }
22107 #endif /* SQLITE_OMIT_DESERIALIZE */
22108 
22109 /*
22110 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
22111 */
shellUSleepFunc(sqlite3_context * context,int argcUnused,sqlite3_value ** argv)22112 static void shellUSleepFunc(
22113   sqlite3_context *context,
22114   int argcUnused,
22115   sqlite3_value **argv
22116 ){
22117   int sleep = sqlite3_value_int(argv[0]);
22118   (void)argcUnused;
22119   sqlite3_sleep(sleep/1000);
22120   sqlite3_result_int(context, sleep);
22121 }
22122 
22123 /* Flags for open_db().
22124 **
22125 ** The default behavior of open_db() is to exit(1) if the database fails to
22126 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
22127 ** but still returns without calling exit.
22128 **
22129 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
22130 ** ZIP archive if the file does not exist or is empty and its name matches
22131 ** the *.zip pattern.
22132 */
22133 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
22134 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
22135 
22136 /*
22137 ** Make sure the database is open.  If it is not, then open it.  If
22138 ** the database fails to open, print an error message and exit.
22139 */
open_db(ShellState * p,int openFlags)22140 static void open_db(ShellState *p, int openFlags){
22141   if( p->db==0 ){
22142     const char *zDbFilename = p->pAuxDb->zDbFilename;
22143     if( p->openMode==SHELL_OPEN_UNSPEC ){
22144       if( zDbFilename==0 || zDbFilename[0]==0 ){
22145         p->openMode = SHELL_OPEN_NORMAL;
22146       }else{
22147         p->openMode = (u8)deduceDatabaseType(zDbFilename,
22148                              (openFlags & OPEN_DB_ZIPFILE)!=0);
22149       }
22150     }
22151     switch( p->openMode ){
22152       case SHELL_OPEN_APPENDVFS: {
22153         sqlite3_open_v2(zDbFilename, &p->db,
22154            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
22155         break;
22156       }
22157       case SHELL_OPEN_HEXDB:
22158       case SHELL_OPEN_DESERIALIZE: {
22159         sqlite3_open(0, &p->db);
22160         break;
22161       }
22162       case SHELL_OPEN_ZIPFILE: {
22163         sqlite3_open(":memory:", &p->db);
22164         break;
22165       }
22166       case SHELL_OPEN_READONLY: {
22167         sqlite3_open_v2(zDbFilename, &p->db,
22168             SQLITE_OPEN_READONLY|p->openFlags, 0);
22169         break;
22170       }
22171       case SHELL_OPEN_UNSPEC:
22172       case SHELL_OPEN_NORMAL: {
22173         sqlite3_open_v2(zDbFilename, &p->db,
22174            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
22175         break;
22176       }
22177     }
22178     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22179       eputf("Error: unable to open database \"%s\": %s\n",
22180             zDbFilename, sqlite3_errmsg(p->db));
22181       if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
22182         exit(1);
22183       }
22184       sqlite3_close(p->db);
22185       sqlite3_open(":memory:", &p->db);
22186       if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
22187         eputz("Also: unable to open substitute in-memory database.\n");
22188         exit(1);
22189       }else{
22190         eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
22191               zDbFilename);
22192       }
22193     }
22194     globalDb = p->db;
22195     sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
22196 
22197     /* Reflect the use or absence of --unsafe-testing invocation. */
22198     {
22199       int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
22200       sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
22201       sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
22202     }
22203 
22204 #ifndef SQLITE_OMIT_LOAD_EXTENSION
22205     sqlite3_enable_load_extension(p->db, 1);
22206 #endif
22207     sqlite3_shathree_init(p->db, 0, 0);
22208     sqlite3_uint_init(p->db, 0, 0);
22209     sqlite3_decimal_init(p->db, 0, 0);
22210     sqlite3_base64_init(p->db, 0, 0);
22211     sqlite3_base85_init(p->db, 0, 0);
22212     sqlite3_regexp_init(p->db, 0, 0);
22213     sqlite3_ieee_init(p->db, 0, 0);
22214     sqlite3_series_init(p->db, 0, 0);
22215 #ifndef SQLITE_SHELL_FIDDLE
22216     sqlite3_fileio_init(p->db, 0, 0);
22217     sqlite3_completion_init(p->db, 0, 0);
22218 #endif
22219 #ifdef SQLITE_HAVE_ZLIB
22220     if( !p->bSafeModePersist ){
22221       sqlite3_zipfile_init(p->db, 0, 0);
22222       sqlite3_sqlar_init(p->db, 0, 0);
22223     }
22224 #endif
22225 #ifdef SQLITE_SHELL_EXTFUNCS
22226     /* Create a preprocessing mechanism for extensions to make
22227      * their own provisions for being built into the shell.
22228      * This is a short-span macro. See further below for usage.
22229      */
22230 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
22231 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
22232     /* Let custom-included extensions get their ..._init() called.
22233      * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
22234      * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
22235      * initialization routine to be called.
22236      */
22237     {
22238       int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
22239     /* Let custom-included extensions expose their functionality.
22240      * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
22241      * the SQL functions, virtual tables, collating sequences or
22242      * VFS's implemented by the extension to be registered.
22243      */
22244       if( irc==SQLITE_OK
22245           || irc==SQLITE_OK_LOAD_PERMANENTLY ){
22246         SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
22247       }
22248 #undef SHELL_SUB_MACRO
22249 #undef SHELL_SUBMACRO
22250     }
22251 #endif
22252 
22253     sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
22254                             shellStrtod, 0, 0);
22255     sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
22256                             shellDtostr, 0, 0);
22257     sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
22258                             shellDtostr, 0, 0);
22259     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
22260                             shellAddSchemaName, 0, 0);
22261     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
22262                             shellModuleSchema, 0, 0);
22263     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
22264                             shellPutsFunc, 0, 0);
22265     sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
22266                             shellUSleepFunc, 0, 0);
22267 #ifndef SQLITE_NOHAVE_SYSTEM
22268     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
22269                             editFunc, 0, 0);
22270     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
22271                             editFunc, 0, 0);
22272 #endif
22273 
22274     if( p->openMode==SHELL_OPEN_ZIPFILE ){
22275       char *zSql = sqlite3_mprintf(
22276          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
22277       shell_check_oom(zSql);
22278       sqlite3_exec(p->db, zSql, 0, 0, 0);
22279       sqlite3_free(zSql);
22280     }
22281 #ifndef SQLITE_OMIT_DESERIALIZE
22282     else
22283     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
22284       int rc;
22285       int nData = 0;
22286       unsigned char *aData;
22287       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
22288         aData = (unsigned char*)readFile(zDbFilename, &nData);
22289       }else{
22290         aData = readHexDb(p, &nData);
22291       }
22292       if( aData==0 ){
22293         return;
22294       }
22295       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
22296                    SQLITE_DESERIALIZE_RESIZEABLE |
22297                    SQLITE_DESERIALIZE_FREEONCLOSE);
22298       if( rc ){
22299         eputf("Error: sqlite3_deserialize() returns %d\n", rc);
22300       }
22301       if( p->szMax>0 ){
22302         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
22303       }
22304     }
22305 #endif
22306   }
22307   if( p->db!=0 ){
22308     if( p->bSafeModePersist ){
22309       sqlite3_set_authorizer(p->db, safeModeAuth, p);
22310     }
22311     sqlite3_db_config(
22312         p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
22313     );
22314   }
22315 }
22316 
22317 /*
22318 ** Attempt to close the database connection.  Report errors.
22319 */
close_db(sqlite3 * db)22320 void close_db(sqlite3 *db){
22321   int rc = sqlite3_close(db);
22322   if( rc ){
22323     eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
22324   }
22325 }
22326 
22327 #if HAVE_READLINE || HAVE_EDITLINE
22328 /*
22329 ** Readline completion callbacks
22330 */
readline_completion_generator(const char * text,int state)22331 static char *readline_completion_generator(const char *text, int state){
22332   static sqlite3_stmt *pStmt = 0;
22333   char *zRet;
22334   if( state==0 ){
22335     char *zSql;
22336     sqlite3_finalize(pStmt);
22337     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
22338                            "  FROM completion(%Q) ORDER BY 1", text);
22339     shell_check_oom(zSql);
22340     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
22341     sqlite3_free(zSql);
22342   }
22343   if( sqlite3_step(pStmt)==SQLITE_ROW ){
22344     const char *z = (const char*)sqlite3_column_text(pStmt,0);
22345     zRet = z ? strdup(z) : 0;
22346   }else{
22347     sqlite3_finalize(pStmt);
22348     pStmt = 0;
22349     zRet = 0;
22350   }
22351   return zRet;
22352 }
readline_completion(const char * zText,int iStart,int iEnd)22353 static char **readline_completion(const char *zText, int iStart, int iEnd){
22354   (void)iStart;
22355   (void)iEnd;
22356   rl_attempted_completion_over = 1;
22357   return rl_completion_matches(zText, readline_completion_generator);
22358 }
22359 
22360 #elif HAVE_LINENOISE
22361 /*
22362 ** Linenoise completion callback
22363 */
linenoise_completion(const char * zLine,linenoiseCompletions * lc)22364 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
22365   i64 nLine = strlen(zLine);
22366   i64 i, iStart;
22367   sqlite3_stmt *pStmt = 0;
22368   char *zSql;
22369   char zBuf[1000];
22370 
22371   if( nLine>(i64)sizeof(zBuf)-30 ) return;
22372   if( zLine[0]=='.' || zLine[0]=='#') return;
22373   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
22374   if( i==nLine-1 ) return;
22375   iStart = i+1;
22376   memcpy(zBuf, zLine, iStart);
22377   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
22378                          "  FROM completion(%Q,%Q) ORDER BY 1",
22379                          &zLine[iStart], zLine);
22380   shell_check_oom(zSql);
22381   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
22382   sqlite3_free(zSql);
22383   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
22384   while( sqlite3_step(pStmt)==SQLITE_ROW ){
22385     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
22386     int nCompletion = sqlite3_column_bytes(pStmt, 0);
22387     if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
22388       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
22389       linenoiseAddCompletion(lc, zBuf);
22390     }
22391   }
22392   sqlite3_finalize(pStmt);
22393 }
22394 #endif
22395 
22396 /*
22397 ** Do C-language style dequoting.
22398 **
22399 **    \a    -> alarm
22400 **    \b    -> backspace
22401 **    \t    -> tab
22402 **    \n    -> newline
22403 **    \v    -> vertical tab
22404 **    \f    -> form feed
22405 **    \r    -> carriage return
22406 **    \s    -> space
22407 **    \"    -> "
22408 **    \'    -> '
22409 **    \\    -> backslash
22410 **    \NNN  -> ascii character NNN in octal
22411 **    \xHH  -> ascii character HH in hexadecimal
22412 */
resolve_backslashes(char * z)22413 static void resolve_backslashes(char *z){
22414   int i, j;
22415   char c;
22416   while( *z && *z!='\\' ) z++;
22417   for(i=j=0; (c = z[i])!=0; i++, j++){
22418     if( c=='\\' && z[i+1]!=0 ){
22419       c = z[++i];
22420       if( c=='a' ){
22421         c = '\a';
22422       }else if( c=='b' ){
22423         c = '\b';
22424       }else if( c=='t' ){
22425         c = '\t';
22426       }else if( c=='n' ){
22427         c = '\n';
22428       }else if( c=='v' ){
22429         c = '\v';
22430       }else if( c=='f' ){
22431         c = '\f';
22432       }else if( c=='r' ){
22433         c = '\r';
22434       }else if( c=='"' ){
22435         c = '"';
22436       }else if( c=='\'' ){
22437         c = '\'';
22438       }else if( c=='\\' ){
22439         c = '\\';
22440       }else if( c=='x' ){
22441         int nhd = 0, hdv;
22442         u8 hv = 0;
22443         while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
22444           hv = (u8)((hv<<4)|hdv);
22445           ++nhd;
22446         }
22447         i += nhd;
22448         c = (u8)hv;
22449       }else if( c>='0' && c<='7' ){
22450         c -= '0';
22451         if( z[i+1]>='0' && z[i+1]<='7' ){
22452           i++;
22453           c = (c<<3) + z[i] - '0';
22454           if( z[i+1]>='0' && z[i+1]<='7' ){
22455             i++;
22456             c = (c<<3) + z[i] - '0';
22457           }
22458         }
22459       }
22460     }
22461     z[j] = c;
22462   }
22463   if( j<i ) z[j] = 0;
22464 }
22465 
22466 /*
22467 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
22468 ** for TRUE and FALSE.  Return the integer value if appropriate.
22469 */
booleanValue(const char * zArg)22470 static int booleanValue(const char *zArg){
22471   int i;
22472   if( zArg[0]=='0' && zArg[1]=='x' ){
22473     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
22474   }else{
22475     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
22476   }
22477   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
22478   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
22479     return 1;
22480   }
22481   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
22482     return 0;
22483   }
22484   eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
22485   return 0;
22486 }
22487 
22488 /*
22489 ** Set or clear a shell flag according to a boolean value.
22490 */
setOrClearFlag(ShellState * p,unsigned mFlag,const char * zArg)22491 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
22492   if( booleanValue(zArg) ){
22493     ShellSetFlag(p, mFlag);
22494   }else{
22495     ShellClearFlag(p, mFlag);
22496   }
22497 }
22498 
22499 /*
22500 ** Close an output file, assuming it is not stderr or stdout
22501 */
output_file_close(FILE * f)22502 static void output_file_close(FILE *f){
22503   if( f && f!=stdout && f!=stderr ) fclose(f);
22504 }
22505 
22506 /*
22507 ** Try to open an output file.   The names "stdout" and "stderr" are
22508 ** recognized and do the right thing.  NULL is returned if the output
22509 ** filename is "off".
22510 */
output_file_open(const char * zFile,int bTextMode)22511 static FILE *output_file_open(const char *zFile, int bTextMode){
22512   FILE *f;
22513   if( cli_strcmp(zFile,"stdout")==0 ){
22514     f = stdout;
22515   }else if( cli_strcmp(zFile, "stderr")==0 ){
22516     f = stderr;
22517   }else if( cli_strcmp(zFile, "off")==0 ){
22518     f = 0;
22519   }else{
22520     f = fopen(zFile, bTextMode ? "w" : "wb");
22521     if( f==0 ){
22522       eputf("Error: cannot open \"%s\"\n", zFile);
22523     }
22524   }
22525   return f;
22526 }
22527 
22528 #ifndef SQLITE_OMIT_TRACE
22529 /*
22530 ** A routine for handling output from sqlite3_trace().
22531 */
sql_trace_callback(unsigned mType,void * pArg,void * pP,void * pX)22532 static int sql_trace_callback(
22533   unsigned mType,         /* The trace type */
22534   void *pArg,             /* The ShellState pointer */
22535   void *pP,               /* Usually a pointer to sqlite_stmt */
22536   void *pX                /* Auxiliary output */
22537 ){
22538   ShellState *p = (ShellState*)pArg;
22539   sqlite3_stmt *pStmt;
22540   const char *zSql;
22541   i64 nSql;
22542   if( p->traceOut==0 ) return 0;
22543   if( mType==SQLITE_TRACE_CLOSE ){
22544     sputz(p->traceOut, "-- closing database connection\n");
22545     return 0;
22546   }
22547   if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
22548     zSql = (const char*)pX;
22549   }else{
22550     pStmt = (sqlite3_stmt*)pP;
22551     switch( p->eTraceType ){
22552       case SHELL_TRACE_EXPANDED: {
22553         zSql = sqlite3_expanded_sql(pStmt);
22554         break;
22555       }
22556 #ifdef SQLITE_ENABLE_NORMALIZE
22557       case SHELL_TRACE_NORMALIZED: {
22558         zSql = sqlite3_normalized_sql(pStmt);
22559         break;
22560       }
22561 #endif
22562       default: {
22563         zSql = sqlite3_sql(pStmt);
22564         break;
22565       }
22566     }
22567   }
22568   if( zSql==0 ) return 0;
22569   nSql = strlen(zSql);
22570   if( nSql>1000000000 ) nSql = 1000000000;
22571   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
22572   switch( mType ){
22573     case SQLITE_TRACE_ROW:
22574     case SQLITE_TRACE_STMT: {
22575       sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
22576       break;
22577     }
22578     case SQLITE_TRACE_PROFILE: {
22579       sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
22580       sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
22581       break;
22582     }
22583   }
22584   return 0;
22585 }
22586 #endif
22587 
22588 /*
22589 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
22590 ** a useful spot to set a debugger breakpoint.
22591 **
22592 ** This routine does not do anything practical.  The code are there simply
22593 ** to prevent the compiler from optimizing this routine out.
22594 */
test_breakpoint(void)22595 static void test_breakpoint(void){
22596   static unsigned int nCall = 0;
22597   if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
22598 }
22599 
22600 /*
22601 ** An object used to read a CSV and other files for import.
22602 */
22603 typedef struct ImportCtx ImportCtx;
22604 struct ImportCtx {
22605   const char *zFile;  /* Name of the input file */
22606   FILE *in;           /* Read the CSV text from this input stream */
22607   int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
22608   char *z;            /* Accumulated text for a field */
22609   int n;              /* Number of bytes in z */
22610   int nAlloc;         /* Space allocated for z[] */
22611   int nLine;          /* Current line number */
22612   int nRow;           /* Number of rows imported */
22613   int nErr;           /* Number of errors encountered */
22614   int bNotFirst;      /* True if one or more bytes already read */
22615   int cTerm;          /* Character that terminated the most recent field */
22616   int cColSep;        /* The column separator character.  (Usually ",") */
22617   int cRowSep;        /* The row separator character.  (Usually "\n") */
22618 };
22619 
22620 /* Clean up resourced used by an ImportCtx */
import_cleanup(ImportCtx * p)22621 static void import_cleanup(ImportCtx *p){
22622   if( p->in!=0 && p->xCloser!=0 ){
22623     p->xCloser(p->in);
22624     p->in = 0;
22625   }
22626   sqlite3_free(p->z);
22627   p->z = 0;
22628 }
22629 
22630 /* Append a single byte to z[] */
import_append_char(ImportCtx * p,int c)22631 static void import_append_char(ImportCtx *p, int c){
22632   if( p->n+1>=p->nAlloc ){
22633     p->nAlloc += p->nAlloc + 100;
22634     p->z = sqlite3_realloc64(p->z, p->nAlloc);
22635     shell_check_oom(p->z);
22636   }
22637   p->z[p->n++] = (char)c;
22638 }
22639 
22640 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
22641 ** with the option of having a separator other than ",".
22642 **
22643 **   +  Input comes from p->in.
22644 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
22645 **      from sqlite3_malloc64().
22646 **   +  Use p->cSep as the column separator.  The default is ",".
22647 **   +  Use p->rSep as the row separator.  The default is "\n".
22648 **   +  Keep track of the line number in p->nLine.
22649 **   +  Store the character that terminates the field in p->cTerm.  Store
22650 **      EOF on end-of-file.
22651 **   +  Report syntax errors on stderr
22652 */
csv_read_one_field(ImportCtx * p)22653 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
22654   int c;
22655   int cSep = (u8)p->cColSep;
22656   int rSep = (u8)p->cRowSep;
22657   p->n = 0;
22658   c = fgetc(p->in);
22659   if( c==EOF || seenInterrupt ){
22660     p->cTerm = EOF;
22661     return 0;
22662   }
22663   if( c=='"' ){
22664     int pc, ppc;
22665     int startLine = p->nLine;
22666     int cQuote = c;
22667     pc = ppc = 0;
22668     while( 1 ){
22669       c = fgetc(p->in);
22670       if( c==rSep ) p->nLine++;
22671       if( c==cQuote ){
22672         if( pc==cQuote ){
22673           pc = 0;
22674           continue;
22675         }
22676       }
22677       if( (c==cSep && pc==cQuote)
22678        || (c==rSep && pc==cQuote)
22679        || (c==rSep && pc=='\r' && ppc==cQuote)
22680        || (c==EOF && pc==cQuote)
22681       ){
22682         do{ p->n--; }while( p->z[p->n]!=cQuote );
22683         p->cTerm = c;
22684         break;
22685       }
22686       if( pc==cQuote && c!='\r' ){
22687         eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
22688       }
22689       if( c==EOF ){
22690         eputf("%s:%d: unterminated %c-quoted field\n",
22691               p->zFile, startLine, cQuote);
22692         p->cTerm = c;
22693         break;
22694       }
22695       import_append_char(p, c);
22696       ppc = pc;
22697       pc = c;
22698     }
22699   }else{
22700     /* If this is the first field being parsed and it begins with the
22701     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
22702     if( (c&0xff)==0xef && p->bNotFirst==0 ){
22703       import_append_char(p, c);
22704       c = fgetc(p->in);
22705       if( (c&0xff)==0xbb ){
22706         import_append_char(p, c);
22707         c = fgetc(p->in);
22708         if( (c&0xff)==0xbf ){
22709           p->bNotFirst = 1;
22710           p->n = 0;
22711           return csv_read_one_field(p);
22712         }
22713       }
22714     }
22715     while( c!=EOF && c!=cSep && c!=rSep ){
22716       import_append_char(p, c);
22717       c = fgetc(p->in);
22718     }
22719     if( c==rSep ){
22720       p->nLine++;
22721       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
22722     }
22723     p->cTerm = c;
22724   }
22725   if( p->z ) p->z[p->n] = 0;
22726   p->bNotFirst = 1;
22727   return p->z;
22728 }
22729 
22730 /* Read a single field of ASCII delimited text.
22731 **
22732 **   +  Input comes from p->in.
22733 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
22734 **      from sqlite3_malloc64().
22735 **   +  Use p->cSep as the column separator.  The default is "\x1F".
22736 **   +  Use p->rSep as the row separator.  The default is "\x1E".
22737 **   +  Keep track of the row number in p->nLine.
22738 **   +  Store the character that terminates the field in p->cTerm.  Store
22739 **      EOF on end-of-file.
22740 **   +  Report syntax errors on stderr
22741 */
ascii_read_one_field(ImportCtx * p)22742 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
22743   int c;
22744   int cSep = (u8)p->cColSep;
22745   int rSep = (u8)p->cRowSep;
22746   p->n = 0;
22747   c = fgetc(p->in);
22748   if( c==EOF || seenInterrupt ){
22749     p->cTerm = EOF;
22750     return 0;
22751   }
22752   while( c!=EOF && c!=cSep && c!=rSep ){
22753     import_append_char(p, c);
22754     c = fgetc(p->in);
22755   }
22756   if( c==rSep ){
22757     p->nLine++;
22758   }
22759   p->cTerm = c;
22760   if( p->z ) p->z[p->n] = 0;
22761   return p->z;
22762 }
22763 
22764 /*
22765 ** Try to transfer data for table zTable.  If an error is seen while
22766 ** moving forward, try to go backwards.  The backwards movement won't
22767 ** work for WITHOUT ROWID tables.
22768 */
tryToCloneData(ShellState * p,sqlite3 * newDb,const char * zTable)22769 static void tryToCloneData(
22770   ShellState *p,
22771   sqlite3 *newDb,
22772   const char *zTable
22773 ){
22774   sqlite3_stmt *pQuery = 0;
22775   sqlite3_stmt *pInsert = 0;
22776   char *zQuery = 0;
22777   char *zInsert = 0;
22778   int rc;
22779   int i, j, n;
22780   int nTable = strlen30(zTable);
22781   int k = 0;
22782   int cnt = 0;
22783   const int spinRate = 10000;
22784 
22785   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
22786   shell_check_oom(zQuery);
22787   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22788   if( rc ){
22789     eputf("Error %d: %s on [%s]\n",
22790           sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
22791     goto end_data_xfer;
22792   }
22793   n = sqlite3_column_count(pQuery);
22794   zInsert = sqlite3_malloc64(200 + nTable + n*3);
22795   shell_check_oom(zInsert);
22796   sqlite3_snprintf(200+nTable,zInsert,
22797                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
22798   i = strlen30(zInsert);
22799   for(j=1; j<n; j++){
22800     memcpy(zInsert+i, ",?", 2);
22801     i += 2;
22802   }
22803   memcpy(zInsert+i, ");", 3);
22804   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
22805   if( rc ){
22806     eputf("Error %d: %s on [%s]\n",
22807           sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
22808     goto end_data_xfer;
22809   }
22810   for(k=0; k<2; k++){
22811     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22812       for(i=0; i<n; i++){
22813         switch( sqlite3_column_type(pQuery, i) ){
22814           case SQLITE_NULL: {
22815             sqlite3_bind_null(pInsert, i+1);
22816             break;
22817           }
22818           case SQLITE_INTEGER: {
22819             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
22820             break;
22821           }
22822           case SQLITE_FLOAT: {
22823             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
22824             break;
22825           }
22826           case SQLITE_TEXT: {
22827             sqlite3_bind_text(pInsert, i+1,
22828                              (const char*)sqlite3_column_text(pQuery,i),
22829                              -1, SQLITE_STATIC);
22830             break;
22831           }
22832           case SQLITE_BLOB: {
22833             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
22834                                             sqlite3_column_bytes(pQuery,i),
22835                                             SQLITE_STATIC);
22836             break;
22837           }
22838         }
22839       } /* End for */
22840       rc = sqlite3_step(pInsert);
22841       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
22842         eputf("Error %d: %s\n",
22843               sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
22844       }
22845       sqlite3_reset(pInsert);
22846       cnt++;
22847       if( (cnt%spinRate)==0 ){
22848         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
22849         fflush(stdout);
22850       }
22851     } /* End while */
22852     if( rc==SQLITE_DONE ) break;
22853     sqlite3_finalize(pQuery);
22854     sqlite3_free(zQuery);
22855     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
22856                              zTable);
22857     shell_check_oom(zQuery);
22858     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22859     if( rc ){
22860       eputf("Warning: cannot step \"%s\" backwards", zTable);
22861       break;
22862     }
22863   } /* End for(k=0...) */
22864 
22865 end_data_xfer:
22866   sqlite3_finalize(pQuery);
22867   sqlite3_finalize(pInsert);
22868   sqlite3_free(zQuery);
22869   sqlite3_free(zInsert);
22870 }
22871 
22872 
22873 /*
22874 ** Try to transfer all rows of the schema that match zWhere.  For
22875 ** each row, invoke xForEach() on the object defined by that row.
22876 ** If an error is encountered while moving forward through the
22877 ** sqlite_schema table, try again moving backwards.
22878 */
tryToCloneSchema(ShellState * p,sqlite3 * newDb,const char * zWhere,void (* xForEach)(ShellState *,sqlite3 *,const char *))22879 static void tryToCloneSchema(
22880   ShellState *p,
22881   sqlite3 *newDb,
22882   const char *zWhere,
22883   void (*xForEach)(ShellState*,sqlite3*,const char*)
22884 ){
22885   sqlite3_stmt *pQuery = 0;
22886   char *zQuery = 0;
22887   int rc;
22888   const unsigned char *zName;
22889   const unsigned char *zSql;
22890   char *zErrMsg = 0;
22891 
22892   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22893                            " WHERE %s ORDER BY rowid ASC", zWhere);
22894   shell_check_oom(zQuery);
22895   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22896   if( rc ){
22897     eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
22898           sqlite3_errmsg(p->db), zQuery);
22899     goto end_schema_xfer;
22900   }
22901   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
22902     zName = sqlite3_column_text(pQuery, 0);
22903     zSql = sqlite3_column_text(pQuery, 1);
22904     if( zName==0 || zSql==0 ) continue;
22905     if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
22906       sputf(stdout, "%s... ", zName); fflush(stdout);
22907       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22908       if( zErrMsg ){
22909         eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22910         sqlite3_free(zErrMsg);
22911         zErrMsg = 0;
22912       }
22913     }
22914     if( xForEach ){
22915       xForEach(p, newDb, (const char*)zName);
22916     }
22917     sputz(stdout, "done\n");
22918   }
22919   if( rc!=SQLITE_DONE ){
22920     sqlite3_finalize(pQuery);
22921     sqlite3_free(zQuery);
22922     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
22923                              " WHERE %s ORDER BY rowid DESC", zWhere);
22924     shell_check_oom(zQuery);
22925     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
22926     if( rc ){
22927       eputf("Error: (%d) %s on [%s]\n",
22928             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
22929       goto end_schema_xfer;
22930     }
22931     while( sqlite3_step(pQuery)==SQLITE_ROW ){
22932       zName = sqlite3_column_text(pQuery, 0);
22933       zSql = sqlite3_column_text(pQuery, 1);
22934       if( zName==0 || zSql==0 ) continue;
22935       if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
22936       sputf(stdout, "%s... ", zName); fflush(stdout);
22937       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
22938       if( zErrMsg ){
22939         eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
22940         sqlite3_free(zErrMsg);
22941         zErrMsg = 0;
22942       }
22943       if( xForEach ){
22944         xForEach(p, newDb, (const char*)zName);
22945       }
22946       sputz(stdout, "done\n");
22947     }
22948   }
22949 end_schema_xfer:
22950   sqlite3_finalize(pQuery);
22951   sqlite3_free(zQuery);
22952 }
22953 
22954 /*
22955 ** Open a new database file named "zNewDb".  Try to recover as much information
22956 ** as possible out of the main database (which might be corrupt) and write it
22957 ** into zNewDb.
22958 */
tryToClone(ShellState * p,const char * zNewDb)22959 static void tryToClone(ShellState *p, const char *zNewDb){
22960   int rc;
22961   sqlite3 *newDb = 0;
22962   if( access(zNewDb,0)==0 ){
22963     eputf("File \"%s\" already exists.\n", zNewDb);
22964     return;
22965   }
22966   rc = sqlite3_open(zNewDb, &newDb);
22967   if( rc ){
22968     eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
22969   }else{
22970     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
22971     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
22972     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
22973     tryToCloneSchema(p, newDb, "type!='table'", 0);
22974     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
22975     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
22976   }
22977   close_db(newDb);
22978 }
22979 
22980 #ifndef SQLITE_SHELL_FIDDLE
22981 /*
22982 ** Change the output stream (file or pipe or console) to something else.
22983 */
output_redir(ShellState * p,FILE * pfNew)22984 static void output_redir(ShellState *p, FILE *pfNew){
22985   if( p->out != stdout ) eputz("Output already redirected.\n");
22986   else{
22987     p->out = pfNew;
22988     setOutputStream(pfNew);
22989   }
22990 }
22991 
22992 /*
22993 ** Change the output file back to stdout.
22994 **
22995 ** If the p->doXdgOpen flag is set, that means the output was being
22996 ** redirected to a temporary file named by p->zTempFile.  In that case,
22997 ** launch start/open/xdg-open on that temporary file.
22998 */
output_reset(ShellState * p)22999 static void output_reset(ShellState *p){
23000   if( p->outfile[0]=='|' ){
23001 #ifndef SQLITE_OMIT_POPEN
23002     pclose(p->out);
23003 #endif
23004   }else{
23005     output_file_close(p->out);
23006 #ifndef SQLITE_NOHAVE_SYSTEM
23007     if( p->doXdgOpen ){
23008       const char *zXdgOpenCmd =
23009 #if defined(_WIN32)
23010       "start";
23011 #elif defined(__APPLE__)
23012       "open";
23013 #else
23014       "xdg-open";
23015 #endif
23016       char *zCmd;
23017       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
23018       if( system(zCmd) ){
23019         eputf("Failed: [%s]\n", zCmd);
23020       }else{
23021         /* Give the start/open/xdg-open command some time to get
23022         ** going before we continue, and potential delete the
23023         ** p->zTempFile data file out from under it */
23024         sqlite3_sleep(2000);
23025       }
23026       sqlite3_free(zCmd);
23027       outputModePop(p);
23028       p->doXdgOpen = 0;
23029     }
23030 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
23031   }
23032   p->outfile[0] = 0;
23033   p->out = stdout;
23034   setOutputStream(stdout);
23035 }
23036 #else
23037 # define output_redir(SS,pfO)
23038 # define output_reset(SS)
23039 #endif
23040 
23041 /*
23042 ** Run an SQL command and return the single integer result.
23043 */
db_int(sqlite3 * db,const char * zSql)23044 static int db_int(sqlite3 *db, const char *zSql){
23045   sqlite3_stmt *pStmt;
23046   int res = 0;
23047   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
23048   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
23049     res = sqlite3_column_int(pStmt,0);
23050   }
23051   sqlite3_finalize(pStmt);
23052   return res;
23053 }
23054 
23055 #if SQLITE_SHELL_HAVE_RECOVER
23056 /*
23057 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
23058 */
get2byteInt(unsigned char * a)23059 static unsigned int get2byteInt(unsigned char *a){
23060   return (a[0]<<8) + a[1];
23061 }
get4byteInt(unsigned char * a)23062 static unsigned int get4byteInt(unsigned char *a){
23063   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
23064 }
23065 
23066 /*
23067 ** Implementation of the ".dbinfo" command.
23068 **
23069 ** Return 1 on error, 2 to exit, and 0 otherwise.
23070 */
shell_dbinfo_command(ShellState * p,int nArg,char ** azArg)23071 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
23072   static const struct { const char *zName; int ofst; } aField[] = {
23073      { "file change counter:",  24  },
23074      { "database page count:",  28  },
23075      { "freelist page count:",  36  },
23076      { "schema cookie:",        40  },
23077      { "schema format:",        44  },
23078      { "default cache size:",   48  },
23079      { "autovacuum top root:",  52  },
23080      { "incremental vacuum:",   64  },
23081      { "text encoding:",        56  },
23082      { "user version:",         60  },
23083      { "application id:",       68  },
23084      { "software version:",     96  },
23085   };
23086   static const struct { const char *zName; const char *zSql; } aQuery[] = {
23087      { "number of tables:",
23088        "SELECT count(*) FROM %s WHERE type='table'" },
23089      { "number of indexes:",
23090        "SELECT count(*) FROM %s WHERE type='index'" },
23091      { "number of triggers:",
23092        "SELECT count(*) FROM %s WHERE type='trigger'" },
23093      { "number of views:",
23094        "SELECT count(*) FROM %s WHERE type='view'" },
23095      { "schema size:",
23096        "SELECT total(length(sql)) FROM %s" },
23097   };
23098   int i, rc;
23099   unsigned iDataVersion;
23100   char *zSchemaTab;
23101   char *zDb = nArg>=2 ? azArg[1] : "main";
23102   sqlite3_stmt *pStmt = 0;
23103   unsigned char aHdr[100];
23104   open_db(p, 0);
23105   if( p->db==0 ) return 1;
23106   rc = sqlite3_prepare_v2(p->db,
23107              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
23108              -1, &pStmt, 0);
23109   if( rc ){
23110     eputf("error: %s\n", sqlite3_errmsg(p->db));
23111     sqlite3_finalize(pStmt);
23112     return 1;
23113   }
23114   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
23115   if( sqlite3_step(pStmt)==SQLITE_ROW
23116    && sqlite3_column_bytes(pStmt,0)>100
23117   ){
23118     const u8 *pb = sqlite3_column_blob(pStmt,0);
23119     shell_check_oom(pb);
23120     memcpy(aHdr, pb, 100);
23121     sqlite3_finalize(pStmt);
23122   }else{
23123     eputz("unable to read database header\n");
23124     sqlite3_finalize(pStmt);
23125     return 1;
23126   }
23127   i = get2byteInt(aHdr+16);
23128   if( i==1 ) i = 65536;
23129   oputf("%-20s %d\n", "database page size:", i);
23130   oputf("%-20s %d\n", "write format:", aHdr[18]);
23131   oputf("%-20s %d\n", "read format:", aHdr[19]);
23132   oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
23133   for(i=0; i<ArraySize(aField); i++){
23134     int ofst = aField[i].ofst;
23135     unsigned int val = get4byteInt(aHdr + ofst);
23136     oputf("%-20s %u", aField[i].zName, val);
23137     switch( ofst ){
23138       case 56: {
23139         if( val==1 ) oputz(" (utf8)");
23140         if( val==2 ) oputz(" (utf16le)");
23141         if( val==3 ) oputz(" (utf16be)");
23142       }
23143     }
23144     oputz("\n");
23145   }
23146   if( zDb==0 ){
23147     zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
23148   }else if( cli_strcmp(zDb,"temp")==0 ){
23149     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
23150   }else{
23151     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
23152   }
23153   for(i=0; i<ArraySize(aQuery); i++){
23154     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
23155     int val = db_int(p->db, zSql);
23156     sqlite3_free(zSql);
23157     oputf("%-20s %d\n", aQuery[i].zName, val);
23158   }
23159   sqlite3_free(zSchemaTab);
23160   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
23161   oputf("%-20s %u\n", "data version", iDataVersion);
23162   return 0;
23163 }
23164 #endif /* SQLITE_SHELL_HAVE_RECOVER */
23165 
23166 /*
23167 ** Print the current sqlite3_errmsg() value to stderr and return 1.
23168 */
shellDatabaseError(sqlite3 * db)23169 static int shellDatabaseError(sqlite3 *db){
23170   const char *zErr = sqlite3_errmsg(db);
23171   eputf("Error: %s\n", zErr);
23172   return 1;
23173 }
23174 
23175 /*
23176 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
23177 ** if they match and FALSE (0) if they do not match.
23178 **
23179 ** Globbing rules:
23180 **
23181 **      '*'       Matches any sequence of zero or more characters.
23182 **
23183 **      '?'       Matches exactly one character.
23184 **
23185 **     [...]      Matches one character from the enclosed list of
23186 **                characters.
23187 **
23188 **     [^...]     Matches one character not in the enclosed list.
23189 **
23190 **      '#'       Matches any sequence of one or more digits with an
23191 **                optional + or - sign in front
23192 **
23193 **      ' '       Any span of whitespace matches any other span of
23194 **                whitespace.
23195 **
23196 ** Extra whitespace at the end of z[] is ignored.
23197 */
testcase_glob(const char * zGlob,const char * z)23198 static int testcase_glob(const char *zGlob, const char *z){
23199   int c, c2;
23200   int invert;
23201   int seen;
23202 
23203   while( (c = (*(zGlob++)))!=0 ){
23204     if( IsSpace(c) ){
23205       if( !IsSpace(*z) ) return 0;
23206       while( IsSpace(*zGlob) ) zGlob++;
23207       while( IsSpace(*z) ) z++;
23208     }else if( c=='*' ){
23209       while( (c=(*(zGlob++))) == '*' || c=='?' ){
23210         if( c=='?' && (*(z++))==0 ) return 0;
23211       }
23212       if( c==0 ){
23213         return 1;
23214       }else if( c=='[' ){
23215         while( *z && testcase_glob(zGlob-1,z)==0 ){
23216           z++;
23217         }
23218         return (*z)!=0;
23219       }
23220       while( (c2 = (*(z++)))!=0 ){
23221         while( c2!=c ){
23222           c2 = *(z++);
23223           if( c2==0 ) return 0;
23224         }
23225         if( testcase_glob(zGlob,z) ) return 1;
23226       }
23227       return 0;
23228     }else if( c=='?' ){
23229       if( (*(z++))==0 ) return 0;
23230     }else if( c=='[' ){
23231       int prior_c = 0;
23232       seen = 0;
23233       invert = 0;
23234       c = *(z++);
23235       if( c==0 ) return 0;
23236       c2 = *(zGlob++);
23237       if( c2=='^' ){
23238         invert = 1;
23239         c2 = *(zGlob++);
23240       }
23241       if( c2==']' ){
23242         if( c==']' ) seen = 1;
23243         c2 = *(zGlob++);
23244       }
23245       while( c2 && c2!=']' ){
23246         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
23247           c2 = *(zGlob++);
23248           if( c>=prior_c && c<=c2 ) seen = 1;
23249           prior_c = 0;
23250         }else{
23251           if( c==c2 ){
23252             seen = 1;
23253           }
23254           prior_c = c2;
23255         }
23256         c2 = *(zGlob++);
23257       }
23258       if( c2==0 || (seen ^ invert)==0 ) return 0;
23259     }else if( c=='#' ){
23260       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
23261       if( !IsDigit(z[0]) ) return 0;
23262       z++;
23263       while( IsDigit(z[0]) ){ z++; }
23264     }else{
23265       if( c!=(*(z++)) ) return 0;
23266     }
23267   }
23268   while( IsSpace(*z) ){ z++; }
23269   return *z==0;
23270 }
23271 
23272 
23273 /*
23274 ** Compare the string as a command-line option with either one or two
23275 ** initial "-" characters.
23276 */
optionMatch(const char * zStr,const char * zOpt)23277 static int optionMatch(const char *zStr, const char *zOpt){
23278   if( zStr[0]!='-' ) return 0;
23279   zStr++;
23280   if( zStr[0]=='-' ) zStr++;
23281   return cli_strcmp(zStr, zOpt)==0;
23282 }
23283 
23284 /*
23285 ** Delete a file.
23286 */
shellDeleteFile(const char * zFilename)23287 int shellDeleteFile(const char *zFilename){
23288   int rc;
23289 #ifdef _WIN32
23290   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
23291   rc = _wunlink(z);
23292   sqlite3_free(z);
23293 #else
23294   rc = unlink(zFilename);
23295 #endif
23296   return rc;
23297 }
23298 
23299 /*
23300 ** Try to delete the temporary file (if there is one) and free the
23301 ** memory used to hold the name of the temp file.
23302 */
clearTempFile(ShellState * p)23303 static void clearTempFile(ShellState *p){
23304   if( p->zTempFile==0 ) return;
23305   if( p->doXdgOpen ) return;
23306   if( shellDeleteFile(p->zTempFile) ) return;
23307   sqlite3_free(p->zTempFile);
23308   p->zTempFile = 0;
23309 }
23310 
23311 /*
23312 ** Create a new temp file name with the given suffix.
23313 */
newTempFile(ShellState * p,const char * zSuffix)23314 static void newTempFile(ShellState *p, const char *zSuffix){
23315   clearTempFile(p);
23316   sqlite3_free(p->zTempFile);
23317   p->zTempFile = 0;
23318   if( p->db ){
23319     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
23320   }
23321   if( p->zTempFile==0 ){
23322     /* If p->db is an in-memory database then the TEMPFILENAME file-control
23323     ** will not work and we will need to fallback to guessing */
23324     char *zTemp;
23325     sqlite3_uint64 r;
23326     sqlite3_randomness(sizeof(r), &r);
23327     zTemp = getenv("TEMP");
23328     if( zTemp==0 ) zTemp = getenv("TMP");
23329     if( zTemp==0 ){
23330 #ifdef _WIN32
23331       zTemp = "\\tmp";
23332 #else
23333       zTemp = "/tmp";
23334 #endif
23335     }
23336     p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
23337   }else{
23338     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
23339   }
23340   shell_check_oom(p->zTempFile);
23341 }
23342 
23343 
23344 /*
23345 ** The implementation of SQL scalar function fkey_collate_clause(), used
23346 ** by the ".lint fkey-indexes" command. This scalar function is always
23347 ** called with four arguments - the parent table name, the parent column name,
23348 ** the child table name and the child column name.
23349 **
23350 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
23351 **
23352 ** If either of the named tables or columns do not exist, this function
23353 ** returns an empty string. An empty string is also returned if both tables
23354 ** and columns exist but have the same default collation sequence. Or,
23355 ** if both exist but the default collation sequences are different, this
23356 ** function returns the string " COLLATE <parent-collation>", where
23357 ** <parent-collation> is the default collation sequence of the parent column.
23358 */
shellFkeyCollateClause(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)23359 static void shellFkeyCollateClause(
23360   sqlite3_context *pCtx,
23361   int nVal,
23362   sqlite3_value **apVal
23363 ){
23364   sqlite3 *db = sqlite3_context_db_handle(pCtx);
23365   const char *zParent;
23366   const char *zParentCol;
23367   const char *zParentSeq;
23368   const char *zChild;
23369   const char *zChildCol;
23370   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
23371   int rc;
23372 
23373   assert( nVal==4 );
23374   zParent = (const char*)sqlite3_value_text(apVal[0]);
23375   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
23376   zChild = (const char*)sqlite3_value_text(apVal[2]);
23377   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
23378 
23379   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
23380   rc = sqlite3_table_column_metadata(
23381       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
23382   );
23383   if( rc==SQLITE_OK ){
23384     rc = sqlite3_table_column_metadata(
23385         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
23386     );
23387   }
23388 
23389   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
23390     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
23391     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
23392     sqlite3_free(z);
23393   }
23394 }
23395 
23396 
23397 /*
23398 ** The implementation of dot-command ".lint fkey-indexes".
23399 */
lintFkeyIndexes(ShellState * pState,char ** azArg,int nArg)23400 static int lintFkeyIndexes(
23401   ShellState *pState,             /* Current shell tool state */
23402   char **azArg,                   /* Array of arguments passed to dot command */
23403   int nArg                        /* Number of entries in azArg[] */
23404 ){
23405   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
23406   int bVerbose = 0;               /* If -verbose is present */
23407   int bGroupByParent = 0;         /* If -groupbyparent is present */
23408   int i;                          /* To iterate through azArg[] */
23409   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
23410   int rc;                         /* Return code */
23411   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
23412 
23413   /*
23414   ** This SELECT statement returns one row for each foreign key constraint
23415   ** in the schema of the main database. The column values are:
23416   **
23417   ** 0. The text of an SQL statement similar to:
23418   **
23419   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
23420   **
23421   **    This SELECT is similar to the one that the foreign keys implementation
23422   **    needs to run internally on child tables. If there is an index that can
23423   **    be used to optimize this query, then it can also be used by the FK
23424   **    implementation to optimize DELETE or UPDATE statements on the parent
23425   **    table.
23426   **
23427   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
23428   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
23429   **    contains an index that can be used to optimize the query.
23430   **
23431   ** 2. Human readable text that describes the child table and columns. e.g.
23432   **
23433   **       "child_table(child_key1, child_key2)"
23434   **
23435   ** 3. Human readable text that describes the parent table and columns. e.g.
23436   **
23437   **       "parent_table(parent_key1, parent_key2)"
23438   **
23439   ** 4. A full CREATE INDEX statement for an index that could be used to
23440   **    optimize DELETE or UPDATE statements on the parent table. e.g.
23441   **
23442   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
23443   **
23444   ** 5. The name of the parent table.
23445   **
23446   ** These six values are used by the C logic below to generate the report.
23447   */
23448   const char *zSql =
23449   "SELECT "
23450     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
23451     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
23452     "  || fkey_collate_clause("
23453     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
23454     ", "
23455     "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
23456     "  || group_concat('*=?', ' AND ') || ')'"
23457     ", "
23458     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
23459     ", "
23460     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
23461     ", "
23462     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
23463     "  || ' ON ' || quote(s.name) || '('"
23464     "  || group_concat(quote(f.[from]) ||"
23465     "        fkey_collate_clause("
23466     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
23467     "  || ');'"
23468     ", "
23469     "     f.[table] "
23470     "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
23471     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
23472     "GROUP BY s.name, f.id "
23473     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
23474   ;
23475   const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
23476 
23477   for(i=2; i<nArg; i++){
23478     int n = strlen30(azArg[i]);
23479     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
23480       bVerbose = 1;
23481     }
23482     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
23483       bGroupByParent = 1;
23484       zIndent = "    ";
23485     }
23486     else{
23487       eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
23488       return SQLITE_ERROR;
23489     }
23490   }
23491 
23492   /* Register the fkey_collate_clause() SQL function */
23493   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
23494       0, shellFkeyCollateClause, 0, 0
23495   );
23496 
23497 
23498   if( rc==SQLITE_OK ){
23499     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
23500   }
23501   if( rc==SQLITE_OK ){
23502     sqlite3_bind_int(pSql, 1, bGroupByParent);
23503   }
23504 
23505   if( rc==SQLITE_OK ){
23506     int rc2;
23507     char *zPrev = 0;
23508     while( SQLITE_ROW==sqlite3_step(pSql) ){
23509       int res = -1;
23510       sqlite3_stmt *pExplain = 0;
23511       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
23512       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
23513       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
23514       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
23515       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
23516       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
23517 
23518       if( zEQP==0 ) continue;
23519       if( zGlob==0 ) continue;
23520       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
23521       if( rc!=SQLITE_OK ) break;
23522       if( SQLITE_ROW==sqlite3_step(pExplain) ){
23523         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
23524         res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
23525                           || 0==sqlite3_strglob(zGlobIPK, zPlan));
23526       }
23527       rc = sqlite3_finalize(pExplain);
23528       if( rc!=SQLITE_OK ) break;
23529 
23530       if( res<0 ){
23531         eputz("Error: internal error");
23532         break;
23533       }else{
23534         if( bGroupByParent
23535         && (bVerbose || res==0)
23536         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
23537         ){
23538           oputf("-- Parent table %s\n", zParent);
23539           sqlite3_free(zPrev);
23540           zPrev = sqlite3_mprintf("%s", zParent);
23541         }
23542 
23543         if( res==0 ){
23544           oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
23545         }else if( bVerbose ){
23546           oputf("%s/* no extra indexes required for %s -> %s */\n",
23547                 zIndent, zFrom, zTarget
23548           );
23549         }
23550       }
23551     }
23552     sqlite3_free(zPrev);
23553 
23554     if( rc!=SQLITE_OK ){
23555       eputf("%s\n", sqlite3_errmsg(db));
23556     }
23557 
23558     rc2 = sqlite3_finalize(pSql);
23559     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
23560       rc = rc2;
23561       eputf("%s\n", sqlite3_errmsg(db));
23562     }
23563   }else{
23564     eputf("%s\n", sqlite3_errmsg(db));
23565   }
23566 
23567   return rc;
23568 }
23569 
23570 /*
23571 ** Implementation of ".lint" dot command.
23572 */
lintDotCommand(ShellState * pState,char ** azArg,int nArg)23573 static int lintDotCommand(
23574   ShellState *pState,             /* Current shell tool state */
23575   char **azArg,                   /* Array of arguments passed to dot command */
23576   int nArg                        /* Number of entries in azArg[] */
23577 ){
23578   int n;
23579   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
23580   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
23581   return lintFkeyIndexes(pState, azArg, nArg);
23582 
23583  usage:
23584   eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
23585   eputz("Where sub-commands are:\n");
23586   eputz("    fkey-indexes\n");
23587   return SQLITE_ERROR;
23588 }
23589 
shellPrepare(sqlite3 * db,int * pRc,const char * zSql,sqlite3_stmt ** ppStmt)23590 static void shellPrepare(
23591   sqlite3 *db,
23592   int *pRc,
23593   const char *zSql,
23594   sqlite3_stmt **ppStmt
23595 ){
23596   *ppStmt = 0;
23597   if( *pRc==SQLITE_OK ){
23598     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
23599     if( rc!=SQLITE_OK ){
23600       eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
23601       *pRc = rc;
23602     }
23603   }
23604 }
23605 
23606 /*
23607 ** Create a prepared statement using printf-style arguments for the SQL.
23608 */
shellPreparePrintf(sqlite3 * db,int * pRc,sqlite3_stmt ** ppStmt,const char * zFmt,...)23609 static void shellPreparePrintf(
23610   sqlite3 *db,
23611   int *pRc,
23612   sqlite3_stmt **ppStmt,
23613   const char *zFmt,
23614   ...
23615 ){
23616   *ppStmt = 0;
23617   if( *pRc==SQLITE_OK ){
23618     va_list ap;
23619     char *z;
23620     va_start(ap, zFmt);
23621     z = sqlite3_vmprintf(zFmt, ap);
23622     va_end(ap);
23623     if( z==0 ){
23624       *pRc = SQLITE_NOMEM;
23625     }else{
23626       shellPrepare(db, pRc, z, ppStmt);
23627       sqlite3_free(z);
23628     }
23629   }
23630 }
23631 
23632 /*
23633 ** Finalize the prepared statement created using shellPreparePrintf().
23634 */
shellFinalize(int * pRc,sqlite3_stmt * pStmt)23635 static void shellFinalize(
23636   int *pRc,
23637   sqlite3_stmt *pStmt
23638 ){
23639   if( pStmt ){
23640     sqlite3 *db = sqlite3_db_handle(pStmt);
23641     int rc = sqlite3_finalize(pStmt);
23642     if( *pRc==SQLITE_OK ){
23643       if( rc!=SQLITE_OK ){
23644         eputf("SQL error: %s\n", sqlite3_errmsg(db));
23645       }
23646       *pRc = rc;
23647     }
23648   }
23649 }
23650 
23651 #if !defined SQLITE_OMIT_VIRTUALTABLE
23652 /* Reset the prepared statement created using shellPreparePrintf().
23653 **
23654 ** This routine is could be marked "static".  But it is not always used,
23655 ** depending on compile-time options.  By omitting the "static", we avoid
23656 ** nuisance compiler warnings about "defined but not used".
23657 */
shellReset(int * pRc,sqlite3_stmt * pStmt)23658 void shellReset(
23659   int *pRc,
23660   sqlite3_stmt *pStmt
23661 ){
23662   int rc = sqlite3_reset(pStmt);
23663   if( *pRc==SQLITE_OK ){
23664     if( rc!=SQLITE_OK ){
23665       sqlite3 *db = sqlite3_db_handle(pStmt);
23666       eputf("SQL error: %s\n", sqlite3_errmsg(db));
23667     }
23668     *pRc = rc;
23669   }
23670 }
23671 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
23672 
23673 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23674 /******************************************************************************
23675 ** The ".archive" or ".ar" command.
23676 */
23677 /*
23678 ** Structure representing a single ".ar" command.
23679 */
23680 typedef struct ArCommand ArCommand;
23681 struct ArCommand {
23682   u8 eCmd;                        /* An AR_CMD_* value */
23683   u8 bVerbose;                    /* True if --verbose */
23684   u8 bZip;                        /* True if the archive is a ZIP */
23685   u8 bDryRun;                     /* True if --dry-run */
23686   u8 bAppend;                     /* True if --append */
23687   u8 bGlob;                       /* True if --glob */
23688   u8 fromCmdLine;                 /* Run from -A instead of .archive */
23689   int nArg;                       /* Number of command arguments */
23690   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
23691   const char *zFile;              /* --file argument, or NULL */
23692   const char *zDir;               /* --directory argument, or NULL */
23693   char **azArg;                   /* Array of command arguments */
23694   ShellState *p;                  /* Shell state */
23695   sqlite3 *db;                    /* Database containing the archive */
23696 };
23697 
23698 /*
23699 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
23700 */
arUsage(FILE * f)23701 static int arUsage(FILE *f){
23702   showHelp(f,"archive");
23703   return SQLITE_ERROR;
23704 }
23705 
23706 /*
23707 ** Print an error message for the .ar command to stderr and return
23708 ** SQLITE_ERROR.
23709 */
arErrorMsg(ArCommand * pAr,const char * zFmt,...)23710 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
23711   va_list ap;
23712   char *z;
23713   va_start(ap, zFmt);
23714   z = sqlite3_vmprintf(zFmt, ap);
23715   va_end(ap);
23716   eputf("Error: %s\n", z);
23717   if( pAr->fromCmdLine ){
23718     eputz("Use \"-A\" for more help\n");
23719   }else{
23720     eputz("Use \".archive --help\" for more help\n");
23721   }
23722   sqlite3_free(z);
23723   return SQLITE_ERROR;
23724 }
23725 
23726 /*
23727 ** Values for ArCommand.eCmd.
23728 */
23729 #define AR_CMD_CREATE       1
23730 #define AR_CMD_UPDATE       2
23731 #define AR_CMD_INSERT       3
23732 #define AR_CMD_EXTRACT      4
23733 #define AR_CMD_LIST         5
23734 #define AR_CMD_HELP         6
23735 #define AR_CMD_REMOVE       7
23736 
23737 /*
23738 ** Other (non-command) switches.
23739 */
23740 #define AR_SWITCH_VERBOSE     8
23741 #define AR_SWITCH_FILE        9
23742 #define AR_SWITCH_DIRECTORY  10
23743 #define AR_SWITCH_APPEND     11
23744 #define AR_SWITCH_DRYRUN     12
23745 #define AR_SWITCH_GLOB       13
23746 
arProcessSwitch(ArCommand * pAr,int eSwitch,const char * zArg)23747 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
23748   switch( eSwitch ){
23749     case AR_CMD_CREATE:
23750     case AR_CMD_EXTRACT:
23751     case AR_CMD_LIST:
23752     case AR_CMD_REMOVE:
23753     case AR_CMD_UPDATE:
23754     case AR_CMD_INSERT:
23755     case AR_CMD_HELP:
23756       if( pAr->eCmd ){
23757         return arErrorMsg(pAr, "multiple command options");
23758       }
23759       pAr->eCmd = eSwitch;
23760       break;
23761 
23762     case AR_SWITCH_DRYRUN:
23763       pAr->bDryRun = 1;
23764       break;
23765     case AR_SWITCH_GLOB:
23766       pAr->bGlob = 1;
23767       break;
23768     case AR_SWITCH_VERBOSE:
23769       pAr->bVerbose = 1;
23770       break;
23771     case AR_SWITCH_APPEND:
23772       pAr->bAppend = 1;
23773       deliberate_fall_through;
23774     case AR_SWITCH_FILE:
23775       pAr->zFile = zArg;
23776       break;
23777     case AR_SWITCH_DIRECTORY:
23778       pAr->zDir = zArg;
23779       break;
23780   }
23781 
23782   return SQLITE_OK;
23783 }
23784 
23785 /*
23786 ** Parse the command line for an ".ar" command. The results are written into
23787 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
23788 ** successfully, otherwise an error message is written to stderr and
23789 ** SQLITE_ERROR returned.
23790 */
arParseCommand(char ** azArg,int nArg,ArCommand * pAr)23791 static int arParseCommand(
23792   char **azArg,                   /* Array of arguments passed to dot command */
23793   int nArg,                       /* Number of entries in azArg[] */
23794   ArCommand *pAr                  /* Populate this object */
23795 ){
23796   struct ArSwitch {
23797     const char *zLong;
23798     char cShort;
23799     u8 eSwitch;
23800     u8 bArg;
23801   } aSwitch[] = {
23802     { "create",    'c', AR_CMD_CREATE,       0 },
23803     { "extract",   'x', AR_CMD_EXTRACT,      0 },
23804     { "insert",    'i', AR_CMD_INSERT,       0 },
23805     { "list",      't', AR_CMD_LIST,         0 },
23806     { "remove",    'r', AR_CMD_REMOVE,       0 },
23807     { "update",    'u', AR_CMD_UPDATE,       0 },
23808     { "help",      'h', AR_CMD_HELP,         0 },
23809     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
23810     { "file",      'f', AR_SWITCH_FILE,      1 },
23811     { "append",    'a', AR_SWITCH_APPEND,    1 },
23812     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
23813     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
23814     { "glob",      'g', AR_SWITCH_GLOB,      0 },
23815   };
23816   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
23817   struct ArSwitch *pEnd = &aSwitch[nSwitch];
23818 
23819   if( nArg<=1 ){
23820     eputz("Wrong number of arguments.  Usage:\n");
23821     return arUsage(stderr);
23822   }else{
23823     char *z = azArg[1];
23824     if( z[0]!='-' ){
23825       /* Traditional style [tar] invocation */
23826       int i;
23827       int iArg = 2;
23828       for(i=0; z[i]; i++){
23829         const char *zArg = 0;
23830         struct ArSwitch *pOpt;
23831         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23832           if( z[i]==pOpt->cShort ) break;
23833         }
23834         if( pOpt==pEnd ){
23835           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
23836         }
23837         if( pOpt->bArg ){
23838           if( iArg>=nArg ){
23839             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
23840           }
23841           zArg = azArg[iArg++];
23842         }
23843         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
23844       }
23845       pAr->nArg = nArg-iArg;
23846       if( pAr->nArg>0 ){
23847         pAr->azArg = &azArg[iArg];
23848       }
23849     }else{
23850       /* Non-traditional invocation */
23851       int iArg;
23852       for(iArg=1; iArg<nArg; iArg++){
23853         int n;
23854         z = azArg[iArg];
23855         if( z[0]!='-' ){
23856           /* All remaining command line words are command arguments. */
23857           pAr->azArg = &azArg[iArg];
23858           pAr->nArg = nArg-iArg;
23859           break;
23860         }
23861         n = strlen30(z);
23862 
23863         if( z[1]!='-' ){
23864           int i;
23865           /* One or more short options */
23866           for(i=1; i<n; i++){
23867             const char *zArg = 0;
23868             struct ArSwitch *pOpt;
23869             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23870               if( z[i]==pOpt->cShort ) break;
23871             }
23872             if( pOpt==pEnd ){
23873               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
23874             }
23875             if( pOpt->bArg ){
23876               if( i<(n-1) ){
23877                 zArg = &z[i+1];
23878                 i = n;
23879               }else{
23880                 if( iArg>=(nArg-1) ){
23881                   return arErrorMsg(pAr, "option requires an argument: %c",
23882                                     z[i]);
23883                 }
23884                 zArg = azArg[++iArg];
23885               }
23886             }
23887             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
23888           }
23889         }else if( z[2]=='\0' ){
23890           /* A -- option, indicating that all remaining command line words
23891           ** are command arguments.  */
23892           pAr->azArg = &azArg[iArg+1];
23893           pAr->nArg = nArg-iArg-1;
23894           break;
23895         }else{
23896           /* A long option */
23897           const char *zArg = 0;             /* Argument for option, if any */
23898           struct ArSwitch *pMatch = 0;      /* Matching option */
23899           struct ArSwitch *pOpt;            /* Iterator */
23900           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
23901             const char *zLong = pOpt->zLong;
23902             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
23903               if( pMatch ){
23904                 return arErrorMsg(pAr, "ambiguous option: %s",z);
23905               }else{
23906                 pMatch = pOpt;
23907               }
23908             }
23909           }
23910 
23911           if( pMatch==0 ){
23912             return arErrorMsg(pAr, "unrecognized option: %s", z);
23913           }
23914           if( pMatch->bArg ){
23915             if( iArg>=(nArg-1) ){
23916               return arErrorMsg(pAr, "option requires an argument: %s", z);
23917             }
23918             zArg = azArg[++iArg];
23919           }
23920           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
23921         }
23922       }
23923     }
23924   }
23925   if( pAr->eCmd==0 ){
23926     eputz("Required argument missing.  Usage:\n");
23927     return arUsage(stderr);
23928   }
23929   return SQLITE_OK;
23930 }
23931 
23932 /*
23933 ** This function assumes that all arguments within the ArCommand.azArg[]
23934 ** array refer to archive members, as for the --extract, --list or --remove
23935 ** commands. It checks that each of them are "present". If any specified
23936 ** file is not present in the archive, an error is printed to stderr and an
23937 ** error code returned. Otherwise, if all specified arguments are present
23938 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
23939 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
23940 ** when pAr->bGlob is true.
23941 **
23942 ** This function strips any trailing '/' characters from each argument.
23943 ** This is consistent with the way the [tar] command seems to work on
23944 ** Linux.
23945 */
arCheckEntries(ArCommand * pAr)23946 static int arCheckEntries(ArCommand *pAr){
23947   int rc = SQLITE_OK;
23948   if( pAr->nArg ){
23949     int i, j;
23950     sqlite3_stmt *pTest = 0;
23951     const char *zSel = (pAr->bGlob)
23952       ? "SELECT name FROM %s WHERE glob($name,name)"
23953       : "SELECT name FROM %s WHERE name=$name";
23954 
23955     shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
23956     j = sqlite3_bind_parameter_index(pTest, "$name");
23957     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
23958       char *z = pAr->azArg[i];
23959       int n = strlen30(z);
23960       int bOk = 0;
23961       while( n>0 && z[n-1]=='/' ) n--;
23962       z[n] = '\0';
23963       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
23964       if( SQLITE_ROW==sqlite3_step(pTest) ){
23965         bOk = 1;
23966       }
23967       shellReset(&rc, pTest);
23968       if( rc==SQLITE_OK && bOk==0 ){
23969         eputf("not found in archive: %s\n", z);
23970         rc = SQLITE_ERROR;
23971       }
23972     }
23973     shellFinalize(&rc, pTest);
23974   }
23975   return rc;
23976 }
23977 
23978 /*
23979 ** Format a WHERE clause that can be used against the "sqlar" table to
23980 ** identify all archive members that match the command arguments held
23981 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
23982 ** The caller is responsible for eventually calling sqlite3_free() on
23983 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
23984 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
23985 */
arWhereClause(int * pRc,ArCommand * pAr,char ** pzWhere)23986 static void arWhereClause(
23987   int *pRc,
23988   ArCommand *pAr,
23989   char **pzWhere                  /* OUT: New WHERE clause */
23990 ){
23991   char *zWhere = 0;
23992   const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
23993   if( *pRc==SQLITE_OK ){
23994     if( pAr->nArg==0 ){
23995       zWhere = sqlite3_mprintf("1");
23996     }else{
23997       int i;
23998       const char *zSep = "";
23999       for(i=0; i<pAr->nArg; i++){
24000         const char *z = pAr->azArg[i];
24001         zWhere = sqlite3_mprintf(
24002           "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
24003           zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
24004         );
24005         if( zWhere==0 ){
24006           *pRc = SQLITE_NOMEM;
24007           break;
24008         }
24009         zSep = " OR ";
24010       }
24011     }
24012   }
24013   *pzWhere = zWhere;
24014 }
24015 
24016 /*
24017 ** Implementation of .ar "lisT" command.
24018 */
arListCommand(ArCommand * pAr)24019 static int arListCommand(ArCommand *pAr){
24020   const char *zSql = "SELECT %s FROM %s WHERE %s";
24021   const char *azCols[] = {
24022     "name",
24023     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
24024   };
24025 
24026   char *zWhere = 0;
24027   sqlite3_stmt *pSql = 0;
24028   int rc;
24029 
24030   rc = arCheckEntries(pAr);
24031   arWhereClause(&rc, pAr, &zWhere);
24032 
24033   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
24034                      pAr->zSrcTable, zWhere);
24035   if( pAr->bDryRun ){
24036     oputf("%s\n", sqlite3_sql(pSql));
24037   }else{
24038     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24039       if( pAr->bVerbose ){
24040         oputf("%s % 10d  %s  %s\n",
24041               sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
24042               sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
24043       }else{
24044         oputf("%s\n", sqlite3_column_text(pSql, 0));
24045       }
24046     }
24047   }
24048   shellFinalize(&rc, pSql);
24049   sqlite3_free(zWhere);
24050   return rc;
24051 }
24052 
24053 /*
24054 ** Implementation of .ar "Remove" command.
24055 */
arRemoveCommand(ArCommand * pAr)24056 static int arRemoveCommand(ArCommand *pAr){
24057   int rc = 0;
24058   char *zSql = 0;
24059   char *zWhere = 0;
24060 
24061   if( pAr->nArg ){
24062     /* Verify that args actually exist within the archive before proceeding.
24063     ** And formulate a WHERE clause to match them.  */
24064     rc = arCheckEntries(pAr);
24065     arWhereClause(&rc, pAr, &zWhere);
24066   }
24067   if( rc==SQLITE_OK ){
24068     zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
24069                            pAr->zSrcTable, zWhere);
24070     if( pAr->bDryRun ){
24071       oputf("%s\n", zSql);
24072     }else{
24073       char *zErr = 0;
24074       rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
24075       if( rc==SQLITE_OK ){
24076         rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
24077         if( rc!=SQLITE_OK ){
24078           sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
24079         }else{
24080           rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
24081         }
24082       }
24083       if( zErr ){
24084         sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
24085         sqlite3_free(zErr);
24086       }
24087     }
24088   }
24089   sqlite3_free(zWhere);
24090   sqlite3_free(zSql);
24091   return rc;
24092 }
24093 
24094 /*
24095 ** Implementation of .ar "eXtract" command.
24096 */
arExtractCommand(ArCommand * pAr)24097 static int arExtractCommand(ArCommand *pAr){
24098   const char *zSql1 =
24099     "SELECT "
24100     " ($dir || name),"
24101     " writefile(($dir || name), %s, mode, mtime) "
24102     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
24103     " AND name NOT GLOB '*..[/\\]*'";
24104 
24105   const char *azExtraArg[] = {
24106     "sqlar_uncompress(data, sz)",
24107     "data"
24108   };
24109 
24110   sqlite3_stmt *pSql = 0;
24111   int rc = SQLITE_OK;
24112   char *zDir = 0;
24113   char *zWhere = 0;
24114   int i, j;
24115 
24116   /* If arguments are specified, check that they actually exist within
24117   ** the archive before proceeding. And formulate a WHERE clause to
24118   ** match them.  */
24119   rc = arCheckEntries(pAr);
24120   arWhereClause(&rc, pAr, &zWhere);
24121 
24122   if( rc==SQLITE_OK ){
24123     if( pAr->zDir ){
24124       zDir = sqlite3_mprintf("%s/", pAr->zDir);
24125     }else{
24126       zDir = sqlite3_mprintf("");
24127     }
24128     if( zDir==0 ) rc = SQLITE_NOMEM;
24129   }
24130 
24131   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
24132       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
24133   );
24134 
24135   if( rc==SQLITE_OK ){
24136     j = sqlite3_bind_parameter_index(pSql, "$dir");
24137     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
24138 
24139     /* Run the SELECT statement twice. The first time, writefile() is called
24140     ** for all archive members that should be extracted. The second time,
24141     ** only for the directories. This is because the timestamps for
24142     ** extracted directories must be reset after they are populated (as
24143     ** populating them changes the timestamp).  */
24144     for(i=0; i<2; i++){
24145       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
24146       sqlite3_bind_int(pSql, j, i);
24147       if( pAr->bDryRun ){
24148         oputf("%s\n", sqlite3_sql(pSql));
24149       }else{
24150         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
24151           if( i==0 && pAr->bVerbose ){
24152             oputf("%s\n", sqlite3_column_text(pSql, 0));
24153           }
24154         }
24155       }
24156       shellReset(&rc, pSql);
24157     }
24158     shellFinalize(&rc, pSql);
24159   }
24160 
24161   sqlite3_free(zDir);
24162   sqlite3_free(zWhere);
24163   return rc;
24164 }
24165 
24166 /*
24167 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
24168 */
arExecSql(ArCommand * pAr,const char * zSql)24169 static int arExecSql(ArCommand *pAr, const char *zSql){
24170   int rc;
24171   if( pAr->bDryRun ){
24172     oputf("%s\n", zSql);
24173     rc = SQLITE_OK;
24174   }else{
24175     char *zErr = 0;
24176     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
24177     if( zErr ){
24178       sputf(stdout, "ERROR: %s\n", zErr);
24179       sqlite3_free(zErr);
24180     }
24181   }
24182   return rc;
24183 }
24184 
24185 
24186 /*
24187 ** Implementation of .ar "create", "insert", and "update" commands.
24188 **
24189 **     create    ->     Create a new SQL archive
24190 **     insert    ->     Insert or reinsert all files listed
24191 **     update    ->     Insert files that have changed or that were not
24192 **                      previously in the archive
24193 **
24194 ** Create the "sqlar" table in the database if it does not already exist.
24195 ** Then add each file in the azFile[] array to the archive. Directories
24196 ** are added recursively. If argument bVerbose is non-zero, a message is
24197 ** printed on stdout for each file archived.
24198 **
24199 ** The create command is the same as update, except that it drops
24200 ** any existing "sqlar" table before beginning.  The "insert" command
24201 ** always overwrites every file named on the command-line, where as
24202 ** "update" only overwrites if the size or mtime or mode has changed.
24203 */
arCreateOrUpdateCommand(ArCommand * pAr,int bUpdate,int bOnlyIfChanged)24204 static int arCreateOrUpdateCommand(
24205   ArCommand *pAr,                 /* Command arguments and options */
24206   int bUpdate,                    /* true for a --create. */
24207   int bOnlyIfChanged              /* Only update if file has changed */
24208 ){
24209   const char *zCreate =
24210       "CREATE TABLE IF NOT EXISTS sqlar(\n"
24211       "  name TEXT PRIMARY KEY,  -- name of the file\n"
24212       "  mode INT,               -- access permissions\n"
24213       "  mtime INT,              -- last modification time\n"
24214       "  sz INT,                 -- original file size\n"
24215       "  data BLOB               -- compressed content\n"
24216       ")";
24217   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
24218   const char *zInsertFmt[2] = {
24219      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
24220      "  SELECT\n"
24221      "    %s,\n"
24222      "    mode,\n"
24223      "    mtime,\n"
24224      "    CASE substr(lsmode(mode),1,1)\n"
24225      "      WHEN '-' THEN length(data)\n"
24226      "      WHEN 'd' THEN 0\n"
24227      "      ELSE -1 END,\n"
24228      "    sqlar_compress(data)\n"
24229      "  FROM fsdir(%Q,%Q) AS disk\n"
24230      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
24231      ,
24232      "REPLACE INTO %s(name,mode,mtime,data)\n"
24233      "  SELECT\n"
24234      "    %s,\n"
24235      "    mode,\n"
24236      "    mtime,\n"
24237      "    data\n"
24238      "  FROM fsdir(%Q,%Q) AS disk\n"
24239      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
24240   };
24241   int i;                          /* For iterating through azFile[] */
24242   int rc;                         /* Return code */
24243   const char *zTab = 0;           /* SQL table into which to insert */
24244   char *zSql;
24245   char zTemp[50];
24246   char *zExists = 0;
24247 
24248   arExecSql(pAr, "PRAGMA page_size=512");
24249   rc = arExecSql(pAr, "SAVEPOINT ar;");
24250   if( rc!=SQLITE_OK ) return rc;
24251   zTemp[0] = 0;
24252   if( pAr->bZip ){
24253     /* Initialize the zipfile virtual table, if necessary */
24254     if( pAr->zFile ){
24255       sqlite3_uint64 r;
24256       sqlite3_randomness(sizeof(r),&r);
24257       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
24258       zTab = zTemp;
24259       zSql = sqlite3_mprintf(
24260          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
24261          zTab, pAr->zFile
24262       );
24263       rc = arExecSql(pAr, zSql);
24264       sqlite3_free(zSql);
24265     }else{
24266       zTab = "zip";
24267     }
24268   }else{
24269     /* Initialize the table for an SQLAR */
24270     zTab = "sqlar";
24271     if( bUpdate==0 ){
24272       rc = arExecSql(pAr, zDrop);
24273       if( rc!=SQLITE_OK ) goto end_ar_transaction;
24274     }
24275     rc = arExecSql(pAr, zCreate);
24276   }
24277   if( bOnlyIfChanged ){
24278     zExists = sqlite3_mprintf(
24279       " AND NOT EXISTS("
24280           "SELECT 1 FROM %s AS mem"
24281           " WHERE mem.name=disk.name"
24282           " AND mem.mtime=disk.mtime"
24283           " AND mem.mode=disk.mode)", zTab);
24284   }else{
24285     zExists = sqlite3_mprintf("");
24286   }
24287   if( zExists==0 ) rc = SQLITE_NOMEM;
24288   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
24289     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
24290         pAr->bVerbose ? "shell_putsnl(name)" : "name",
24291         pAr->azArg[i], pAr->zDir, zExists);
24292     rc = arExecSql(pAr, zSql2);
24293     sqlite3_free(zSql2);
24294   }
24295 end_ar_transaction:
24296   if( rc!=SQLITE_OK ){
24297     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
24298   }else{
24299     rc = arExecSql(pAr, "RELEASE ar;");
24300     if( pAr->bZip && pAr->zFile ){
24301       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
24302       arExecSql(pAr, zSql);
24303       sqlite3_free(zSql);
24304     }
24305   }
24306   sqlite3_free(zExists);
24307   return rc;
24308 }
24309 
24310 /*
24311 ** Implementation of ".ar" dot command.
24312 */
arDotCommand(ShellState * pState,int fromCmdLine,char ** azArg,int nArg)24313 static int arDotCommand(
24314   ShellState *pState,          /* Current shell tool state */
24315   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
24316   char **azArg,                /* Array of arguments passed to dot command */
24317   int nArg                     /* Number of entries in azArg[] */
24318 ){
24319   ArCommand cmd;
24320   int rc;
24321   memset(&cmd, 0, sizeof(cmd));
24322   cmd.fromCmdLine = fromCmdLine;
24323   rc = arParseCommand(azArg, nArg, &cmd);
24324   if( rc==SQLITE_OK ){
24325     int eDbType = SHELL_OPEN_UNSPEC;
24326     cmd.p = pState;
24327     cmd.db = pState->db;
24328     if( cmd.zFile ){
24329       eDbType = deduceDatabaseType(cmd.zFile, 1);
24330     }else{
24331       eDbType = pState->openMode;
24332     }
24333     if( eDbType==SHELL_OPEN_ZIPFILE ){
24334       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
24335         if( cmd.zFile==0 ){
24336           cmd.zSrcTable = sqlite3_mprintf("zip");
24337         }else{
24338           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
24339         }
24340       }
24341       cmd.bZip = 1;
24342     }else if( cmd.zFile ){
24343       int flags;
24344       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
24345       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
24346            || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
24347         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
24348       }else{
24349         flags = SQLITE_OPEN_READONLY;
24350       }
24351       cmd.db = 0;
24352       if( cmd.bDryRun ){
24353         oputf("-- open database '%s'%s\n", cmd.zFile,
24354               eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
24355       }
24356       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
24357              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
24358       if( rc!=SQLITE_OK ){
24359         eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
24360         goto end_ar_command;
24361       }
24362       sqlite3_fileio_init(cmd.db, 0, 0);
24363       sqlite3_sqlar_init(cmd.db, 0, 0);
24364       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
24365                               shellPutsFunc, 0, 0);
24366 
24367     }
24368     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
24369       if( cmd.eCmd!=AR_CMD_CREATE
24370        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
24371       ){
24372         eputz("database does not contain an 'sqlar' table\n");
24373         rc = SQLITE_ERROR;
24374         goto end_ar_command;
24375       }
24376       cmd.zSrcTable = sqlite3_mprintf("sqlar");
24377     }
24378 
24379     switch( cmd.eCmd ){
24380       case AR_CMD_CREATE:
24381         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
24382         break;
24383 
24384       case AR_CMD_EXTRACT:
24385         rc = arExtractCommand(&cmd);
24386         break;
24387 
24388       case AR_CMD_LIST:
24389         rc = arListCommand(&cmd);
24390         break;
24391 
24392       case AR_CMD_HELP:
24393         arUsage(pState->out);
24394         break;
24395 
24396       case AR_CMD_INSERT:
24397         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
24398         break;
24399 
24400       case AR_CMD_REMOVE:
24401         rc = arRemoveCommand(&cmd);
24402         break;
24403 
24404       default:
24405         assert( cmd.eCmd==AR_CMD_UPDATE );
24406         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
24407         break;
24408     }
24409   }
24410 end_ar_command:
24411   if( cmd.db!=pState->db ){
24412     close_db(cmd.db);
24413   }
24414   sqlite3_free(cmd.zSrcTable);
24415 
24416   return rc;
24417 }
24418 /* End of the ".archive" or ".ar" command logic
24419 *******************************************************************************/
24420 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
24421 
24422 #if SQLITE_SHELL_HAVE_RECOVER
24423 
24424 /*
24425 ** This function is used as a callback by the recover extension. Simply
24426 ** print the supplied SQL statement to stdout.
24427 */
recoverSqlCb(void * pCtx,const char * zSql)24428 static int recoverSqlCb(void *pCtx, const char *zSql){
24429   ShellState *pState = (ShellState*)pCtx;
24430   sputf(pState->out, "%s;\n", zSql);
24431   return SQLITE_OK;
24432 }
24433 
24434 /*
24435 ** This function is called to recover data from the database. A script
24436 ** to construct a new database containing all recovered data is output
24437 ** on stream pState->out.
24438 */
recoverDatabaseCmd(ShellState * pState,int nArg,char ** azArg)24439 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
24440   int rc = SQLITE_OK;
24441   const char *zRecoveryDb = "";   /* Name of "recovery" database.  Debug only */
24442   const char *zLAF = "lost_and_found";
24443   int bFreelist = 1;              /* 0 if --ignore-freelist is specified */
24444   int bRowids = 1;                /* 0 if --no-rowids */
24445   sqlite3_recover *p = 0;
24446   int i = 0;
24447 
24448   for(i=1; i<nArg; i++){
24449     char *z = azArg[i];
24450     int n;
24451     if( z[0]=='-' && z[1]=='-' ) z++;
24452     n = strlen30(z);
24453     if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
24454       bFreelist = 0;
24455     }else
24456     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
24457       /* This option determines the name of the ATTACH-ed database used
24458       ** internally by the recovery extension.  The default is "" which
24459       ** means to use a temporary database that is automatically deleted
24460       ** when closed.  This option is undocumented and might disappear at
24461       ** any moment. */
24462       i++;
24463       zRecoveryDb = azArg[i];
24464     }else
24465     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
24466       i++;
24467       zLAF = azArg[i];
24468     }else
24469     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
24470       bRowids = 0;
24471     }
24472     else{
24473       eputf("unexpected option: %s\n", azArg[i]);
24474       showHelp(pState->out, azArg[0]);
24475       return 1;
24476     }
24477   }
24478 
24479   p = sqlite3_recover_init_sql(
24480       pState->db, "main", recoverSqlCb, (void*)pState
24481   );
24482 
24483   sqlite3_recover_config(p, 789, (void*)zRecoveryDb);  /* Debug use only */
24484   sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
24485   sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
24486   sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
24487 
24488   sqlite3_recover_run(p);
24489   if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
24490     const char *zErr = sqlite3_recover_errmsg(p);
24491     int errCode = sqlite3_recover_errcode(p);
24492     eputf("sql error: %s (%d)\n", zErr, errCode);
24493   }
24494   rc = sqlite3_recover_finish(p);
24495   return rc;
24496 }
24497 #endif /* SQLITE_SHELL_HAVE_RECOVER */
24498 
24499 
24500 /*
24501  * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
24502  * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
24503  *   close db and set it to 0, and return the columns spec, to later
24504  *   be sqlite3_free()'ed by the caller.
24505  * The return is 0 when either:
24506  *   (a) The db was not initialized and zCol==0 (There are no columns.)
24507  *   (b) zCol!=0  (Column was added, db initialized as needed.)
24508  * The 3rd argument, pRenamed, references an out parameter. If the
24509  * pointer is non-zero, its referent will be set to a summary of renames
24510  * done if renaming was necessary, or set to 0 if none was done. The out
24511  * string (if any) must be sqlite3_free()'ed by the caller.
24512  */
24513 #ifdef SHELL_DEBUG
24514 #define rc_err_oom_die(rc) \
24515   if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
24516   else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
24517     eputf("E:%d\n",rc), assert(0)
24518 #else
rc_err_oom_die(int rc)24519 static void rc_err_oom_die(int rc){
24520   if( rc==SQLITE_NOMEM ) shell_check_oom(0);
24521   assert(rc==SQLITE_OK||rc==SQLITE_DONE);
24522 }
24523 #endif
24524 
24525 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
24526 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
24527 #else  /* Otherwise, memory is faster/better for the transient DB. */
24528 static const char *zCOL_DB = ":memory:";
24529 #endif
24530 
24531 /* Define character (as C string) to separate generated column ordinal
24532  * from protected part of incoming column names. This defaults to "_"
24533  * so that incoming column identifiers that did not need not be quoted
24534  * remain usable without being quoted. It must be one character.
24535  */
24536 #ifndef SHELL_AUTOCOLUMN_SEP
24537 # define AUTOCOLUMN_SEP "_"
24538 #else
24539 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
24540 #endif
24541 
zAutoColumn(const char * zColNew,sqlite3 ** pDb,char ** pzRenamed)24542 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
24543   /* Queries and D{D,M}L used here */
24544   static const char * const zTabMake = "\
24545 CREATE TABLE ColNames(\
24546  cpos INTEGER PRIMARY KEY,\
24547  name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
24548 CREATE VIEW RepeatedNames AS \
24549 SELECT DISTINCT t.name FROM ColNames t \
24550 WHERE t.name COLLATE NOCASE IN (\
24551  SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
24552 );\
24553 ";
24554   static const char * const zTabFill = "\
24555 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
24556  VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
24557 ";
24558   static const char * const zHasDupes = "\
24559 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
24560  <count(name) FROM ColNames\
24561 ";
24562 #ifdef SHELL_COLUMN_RENAME_CLEAN
24563   static const char * const zDedoctor = "\
24564 UPDATE ColNames SET chop=iif(\
24565   (substring(name,nlen,1) BETWEEN '0' AND '9')\
24566   AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
24567  nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
24568  0\
24569 )\
24570 ";
24571 #endif
24572   static const char * const zSetReps = "\
24573 UPDATE ColNames AS t SET reps=\
24574 (SELECT count(*) FROM ColNames d \
24575  WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
24576  COLLATE NOCASE\
24577 )\
24578 ";
24579 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
24580   static const char * const zColDigits = "\
24581 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
24582 ";
24583 #else
24584   /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
24585   static const char * const zColDigits = "\
24586 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
24587  WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
24588  ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
24589 ";
24590 #endif
24591   static const char * const zRenameRank =
24592 #ifdef SHELL_COLUMN_RENAME_CLEAN
24593     "UPDATE ColNames AS t SET suff="
24594     "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
24595 #else /* ...RENAME_MINIMAL_ONE_PASS */
24596 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
24597 "  SELECT 0 AS nlz"
24598 "  UNION"
24599 "  SELECT nlz+1 AS nlz FROM Lzn"
24600 "  WHERE EXISTS("
24601 "   SELECT 1"
24602 "   FROM ColNames t, ColNames o"
24603 "   WHERE"
24604 "    iif(t.name IN (SELECT * FROM RepeatedNames),"
24605 "     printf('%s"AUTOCOLUMN_SEP"%s',"
24606 "      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
24607 "     t.name"
24608 "    )"
24609 "    ="
24610 "    iif(o.name IN (SELECT * FROM RepeatedNames),"
24611 "     printf('%s"AUTOCOLUMN_SEP"%s',"
24612 "      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
24613 "     o.name"
24614 "    )"
24615 "    COLLATE NOCASE"
24616 "    AND o.cpos<>t.cpos"
24617 "   GROUP BY t.cpos"
24618 "  )"
24619 ") UPDATE Colnames AS t SET"
24620 " chop = 0," /* No chopping, never touch incoming names. */
24621 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
24622 "  printf('"AUTOCOLUMN_SEP"%s', substring("
24623 "   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
24624 "  ''"
24625 " )"
24626 #endif
24627     ;
24628   static const char * const zCollectVar = "\
24629 SELECT\
24630  '('||x'0a'\
24631  || group_concat(\
24632   cname||' TEXT',\
24633   ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
24634  ||')' AS ColsSpec \
24635 FROM (\
24636  SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
24637  FROM ColNames ORDER BY cpos\
24638 )";
24639   static const char * const zRenamesDone =
24640     "SELECT group_concat("
24641     " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
24642     " ','||x'0a')"
24643     "FROM ColNames WHERE suff<>'' OR chop!=0"
24644     ;
24645   int rc;
24646   sqlite3_stmt *pStmt = 0;
24647   assert(pDb!=0);
24648   if( zColNew ){
24649     /* Add initial or additional column. Init db if necessary. */
24650     if( *pDb==0 ){
24651       if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
24652 #ifdef SHELL_COLFIX_DB
24653       if(*zCOL_DB!=':')
24654         sqlite3_exec(*pDb,"drop table if exists ColNames;"
24655                      "drop view if exists RepeatedNames;",0,0,0);
24656 #endif
24657 #undef SHELL_COLFIX_DB
24658       rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
24659       rc_err_oom_die(rc);
24660     }
24661     assert(*pDb!=0);
24662     rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
24663     rc_err_oom_die(rc);
24664     rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
24665     rc_err_oom_die(rc);
24666     rc = sqlite3_step(pStmt);
24667     rc_err_oom_die(rc);
24668     sqlite3_finalize(pStmt);
24669     return 0;
24670   }else if( *pDb==0 ){
24671     return 0;
24672   }else{
24673     /* Formulate the columns spec, close the DB, zero *pDb. */
24674     char *zColsSpec = 0;
24675     int hasDupes = db_int(*pDb, zHasDupes);
24676     int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
24677     if( hasDupes ){
24678 #ifdef SHELL_COLUMN_RENAME_CLEAN
24679       rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
24680       rc_err_oom_die(rc);
24681 #endif
24682       rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
24683       rc_err_oom_die(rc);
24684       rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
24685       rc_err_oom_die(rc);
24686       sqlite3_bind_int(pStmt, 1, nDigits);
24687       rc = sqlite3_step(pStmt);
24688       sqlite3_finalize(pStmt);
24689       if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
24690     }
24691     assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
24692     rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
24693     rc_err_oom_die(rc);
24694     rc = sqlite3_step(pStmt);
24695     if( rc==SQLITE_ROW ){
24696       zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
24697     }else{
24698       zColsSpec = 0;
24699     }
24700     if( pzRenamed!=0 ){
24701       if( !hasDupes ) *pzRenamed = 0;
24702       else{
24703         sqlite3_finalize(pStmt);
24704         if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
24705             && SQLITE_ROW==sqlite3_step(pStmt) ){
24706           *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
24707         }else
24708           *pzRenamed = 0;
24709       }
24710     }
24711     sqlite3_finalize(pStmt);
24712     sqlite3_close(*pDb);
24713     *pDb = 0;
24714     return zColsSpec;
24715   }
24716 }
24717 
24718 /*
24719 ** Check if the sqlite_schema table contains one or more virtual tables. If
24720 ** parameter zLike is not NULL, then it is an SQL expression that the
24721 ** sqlite_schema row must also match. If one or more such rows are found,
24722 ** print the following warning to the output:
24723 **
24724 ** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
24725 */
outputDumpWarning(ShellState * p,const char * zLike)24726 static int outputDumpWarning(ShellState *p, const char *zLike){
24727   int rc = SQLITE_OK;
24728   sqlite3_stmt *pStmt = 0;
24729   shellPreparePrintf(p->db, &rc, &pStmt,
24730     "SELECT 1 FROM sqlite_schema o WHERE "
24731     "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
24732   );
24733   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
24734     oputz("/* WARNING: "
24735           "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
24736     );
24737   }
24738   shellFinalize(&rc, pStmt);
24739   return rc;
24740 }
24741 
24742 /*
24743 ** If an input line begins with "." then invoke this routine to
24744 ** process that line.
24745 **
24746 ** Return 1 on error, 2 to exit, and 0 otherwise.
24747 */
do_meta_command(char * zLine,ShellState * p)24748 static int do_meta_command(char *zLine, ShellState *p){
24749   int h = 1;
24750   int nArg = 0;
24751   int n, c;
24752   int rc = 0;
24753   char *azArg[52];
24754 
24755 #ifndef SQLITE_OMIT_VIRTUALTABLE
24756   if( p->expert.pExpert ){
24757     expertFinish(p, 1, 0);
24758   }
24759 #endif
24760 
24761   /* Parse the input line into tokens.
24762   */
24763   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
24764     while( IsSpace(zLine[h]) ){ h++; }
24765     if( zLine[h]==0 ) break;
24766     if( zLine[h]=='\'' || zLine[h]=='"' ){
24767       int delim = zLine[h++];
24768       azArg[nArg++] = &zLine[h];
24769       while( zLine[h] && zLine[h]!=delim ){
24770         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
24771         h++;
24772       }
24773       if( zLine[h]==delim ){
24774         zLine[h++] = 0;
24775       }
24776       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
24777     }else{
24778       azArg[nArg++] = &zLine[h];
24779       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
24780       if( zLine[h] ) zLine[h++] = 0;
24781     }
24782   }
24783   azArg[nArg] = 0;
24784 
24785   /* Process the input line.
24786   */
24787   if( nArg==0 ) return 0; /* no tokens, no error */
24788   n = strlen30(azArg[0]);
24789   c = azArg[0][0];
24790   clearTempFile(p);
24791 
24792 #ifndef SQLITE_OMIT_AUTHORIZATION
24793   if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
24794     if( nArg!=2 ){
24795       eputz("Usage: .auth ON|OFF\n");
24796       rc = 1;
24797       goto meta_command_exit;
24798     }
24799     open_db(p, 0);
24800     if( booleanValue(azArg[1]) ){
24801       sqlite3_set_authorizer(p->db, shellAuth, p);
24802     }else if( p->bSafeModePersist ){
24803       sqlite3_set_authorizer(p->db, safeModeAuth, p);
24804     }else{
24805       sqlite3_set_authorizer(p->db, 0, 0);
24806     }
24807   }else
24808 #endif
24809 
24810 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
24811   && !defined(SQLITE_SHELL_FIDDLE)
24812   if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
24813     open_db(p, 0);
24814     failIfSafeMode(p, "cannot run .archive in safe mode");
24815     rc = arDotCommand(p, 0, azArg, nArg);
24816   }else
24817 #endif
24818 
24819 #ifndef SQLITE_SHELL_FIDDLE
24820   if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
24821    || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
24822   ){
24823     const char *zDestFile = 0;
24824     const char *zDb = 0;
24825     sqlite3 *pDest;
24826     sqlite3_backup *pBackup;
24827     int j;
24828     int bAsync = 0;
24829     const char *zVfs = 0;
24830     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
24831     for(j=1; j<nArg; j++){
24832       const char *z = azArg[j];
24833       if( z[0]=='-' ){
24834         if( z[1]=='-' ) z++;
24835         if( cli_strcmp(z, "-append")==0 ){
24836           zVfs = "apndvfs";
24837         }else
24838         if( cli_strcmp(z, "-async")==0 ){
24839           bAsync = 1;
24840         }else
24841         {
24842           eputf("unknown option: %s\n", azArg[j]);
24843           return 1;
24844         }
24845       }else if( zDestFile==0 ){
24846         zDestFile = azArg[j];
24847       }else if( zDb==0 ){
24848         zDb = zDestFile;
24849         zDestFile = azArg[j];
24850       }else{
24851         eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
24852         return 1;
24853       }
24854     }
24855     if( zDestFile==0 ){
24856       eputz("missing FILENAME argument on .backup\n");
24857       return 1;
24858     }
24859     if( zDb==0 ) zDb = "main";
24860     rc = sqlite3_open_v2(zDestFile, &pDest,
24861                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
24862     if( rc!=SQLITE_OK ){
24863       eputf("Error: cannot open \"%s\"\n", zDestFile);
24864       close_db(pDest);
24865       return 1;
24866     }
24867     if( bAsync ){
24868       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
24869                    0, 0, 0);
24870     }
24871     open_db(p, 0);
24872     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
24873     if( pBackup==0 ){
24874       eputf("Error: %s\n", sqlite3_errmsg(pDest));
24875       close_db(pDest);
24876       return 1;
24877     }
24878     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
24879     sqlite3_backup_finish(pBackup);
24880     if( rc==SQLITE_DONE ){
24881       rc = 0;
24882     }else{
24883       eputf("Error: %s\n", sqlite3_errmsg(pDest));
24884       rc = 1;
24885     }
24886     close_db(pDest);
24887   }else
24888 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24889 
24890   if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
24891     if( nArg==2 ){
24892       bail_on_error = booleanValue(azArg[1]);
24893     }else{
24894       eputz("Usage: .bail on|off\n");
24895       rc = 1;
24896     }
24897   }else
24898 
24899   /* Undocumented.  Legacy only.  See "crnl" below */
24900   if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
24901     if( nArg==2 ){
24902       if( booleanValue(azArg[1]) ){
24903         setBinaryMode(p->out, 1);
24904       }else{
24905         setTextMode(p->out, 1);
24906       }
24907     }else{
24908       eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
24909             "Usage: .binary on|off\n");
24910       rc = 1;
24911     }
24912   }else
24913 
24914   /* The undocumented ".breakpoint" command causes a call to the no-op
24915   ** routine named test_breakpoint().
24916   */
24917   if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
24918     test_breakpoint();
24919   }else
24920 
24921 #ifndef SQLITE_SHELL_FIDDLE
24922   if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
24923     failIfSafeMode(p, "cannot run .cd in safe mode");
24924     if( nArg==2 ){
24925 #if defined(_WIN32) || defined(WIN32)
24926       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
24927       rc = !SetCurrentDirectoryW(z);
24928       sqlite3_free(z);
24929 #else
24930       rc = chdir(azArg[1]);
24931 #endif
24932       if( rc ){
24933         eputf("Cannot change to directory \"%s\"\n", azArg[1]);
24934         rc = 1;
24935       }
24936     }else{
24937       eputz("Usage: .cd DIRECTORY\n");
24938       rc = 1;
24939     }
24940   }else
24941 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24942 
24943   if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
24944     if( nArg==2 ){
24945       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
24946     }else{
24947       eputz("Usage: .changes on|off\n");
24948       rc = 1;
24949     }
24950   }else
24951 
24952 #ifndef SQLITE_SHELL_FIDDLE
24953   /* Cancel output redirection, if it is currently set (by .testcase)
24954   ** Then read the content of the testcase-out.txt file and compare against
24955   ** azArg[1].  If there are differences, report an error and exit.
24956   */
24957   if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
24958     char *zRes = 0;
24959     output_reset(p);
24960     if( nArg!=2 ){
24961       eputz("Usage: .check GLOB-PATTERN\n");
24962       rc = 2;
24963     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
24964       rc = 2;
24965     }else if( testcase_glob(azArg[1],zRes)==0 ){
24966       eputf("testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
24967             p->zTestcase, azArg[1], zRes);
24968       rc = 1;
24969     }else{
24970       oputf("testcase-%s ok\n", p->zTestcase);
24971       p->nCheck++;
24972     }
24973     sqlite3_free(zRes);
24974   }else
24975 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24976 
24977 #ifndef SQLITE_SHELL_FIDDLE
24978   if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
24979     failIfSafeMode(p, "cannot run .clone in safe mode");
24980     if( nArg==2 ){
24981       tryToClone(p, azArg[1]);
24982     }else{
24983       eputz("Usage: .clone FILENAME\n");
24984       rc = 1;
24985     }
24986   }else
24987 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
24988 
24989   if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
24990     if( nArg==1 ){
24991       /* List available connections */
24992       int i;
24993       for(i=0; i<ArraySize(p->aAuxDb); i++){
24994         const char *zFile = p->aAuxDb[i].zDbFilename;
24995         if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
24996           zFile = "(not open)";
24997         }else if( zFile==0 ){
24998           zFile = "(memory)";
24999         }else if( zFile[0]==0 ){
25000           zFile = "(temporary-file)";
25001         }
25002         if( p->pAuxDb == &p->aAuxDb[i] ){
25003           sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
25004         }else if( p->aAuxDb[i].db!=0 ){
25005           sputf(stdout, "       %d: %s\n", i, zFile);
25006         }
25007       }
25008     }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
25009       int i = azArg[1][0] - '0';
25010       if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
25011         p->pAuxDb->db = p->db;
25012         p->pAuxDb = &p->aAuxDb[i];
25013         globalDb = p->db = p->pAuxDb->db;
25014         p->pAuxDb->db = 0;
25015       }
25016     }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
25017            && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
25018       int i = azArg[2][0] - '0';
25019       if( i<0 || i>=ArraySize(p->aAuxDb) ){
25020         /* No-op */
25021       }else if( p->pAuxDb == &p->aAuxDb[i] ){
25022         eputz("cannot close the active database connection\n");
25023         rc = 1;
25024       }else if( p->aAuxDb[i].db ){
25025         session_close_all(p, i);
25026         close_db(p->aAuxDb[i].db);
25027         p->aAuxDb[i].db = 0;
25028       }
25029     }else{
25030       eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
25031       rc = 1;
25032     }
25033   }else
25034 
25035   if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
25036     if( nArg==2 ){
25037       if( booleanValue(azArg[1]) ){
25038         setTextMode(p->out, 1);
25039       }else{
25040         setBinaryMode(p->out, 1);
25041       }
25042     }else{
25043 #if !defined(_WIN32) && !defined(WIN32)
25044       eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
25045 #endif
25046       eputz("Usage: .crnl on|off\n");
25047       rc = 1;
25048     }
25049   }else
25050 
25051   if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
25052     char **azName = 0;
25053     int nName = 0;
25054     sqlite3_stmt *pStmt;
25055     int i;
25056     open_db(p, 0);
25057     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
25058     if( rc ){
25059       eputf("Error: %s\n", sqlite3_errmsg(p->db));
25060       rc = 1;
25061     }else{
25062       while( sqlite3_step(pStmt)==SQLITE_ROW ){
25063         const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
25064         const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
25065         if( zSchema==0 || zFile==0 ) continue;
25066         azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
25067         shell_check_oom(azName);
25068         azName[nName*2] = strdup(zSchema);
25069         azName[nName*2+1] = strdup(zFile);
25070         nName++;
25071       }
25072     }
25073     sqlite3_finalize(pStmt);
25074     for(i=0; i<nName; i++){
25075       int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
25076       int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
25077       const char *z = azName[i*2+1];
25078       oputf("%s: %s %s%s\n",
25079             azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
25080             eTxn==SQLITE_TXN_NONE ? "" :
25081             eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
25082       free(azName[i*2]);
25083       free(azName[i*2+1]);
25084     }
25085     sqlite3_free(azName);
25086   }else
25087 
25088   if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
25089     static const struct DbConfigChoices {
25090       const char *zName;
25091       int op;
25092     } aDbConfig[] = {
25093         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
25094         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
25095         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
25096         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
25097         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
25098         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
25099         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
25100         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
25101         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
25102         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
25103         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
25104         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
25105         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
25106         { "reverse_scanorder",  SQLITE_DBCONFIG_REVERSE_SCANORDER     },
25107         { "stmt_scanstatus",    SQLITE_DBCONFIG_STMT_SCANSTATUS       },
25108         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
25109         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
25110         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
25111     };
25112     int ii, v;
25113     open_db(p, 0);
25114     for(ii=0; ii<ArraySize(aDbConfig); ii++){
25115       if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
25116       if( nArg>=3 ){
25117         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
25118       }
25119       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
25120       oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
25121       if( nArg>1 ) break;
25122     }
25123     if( nArg>1 && ii==ArraySize(aDbConfig) ){
25124       eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
25125       eputz("Enter \".dbconfig\" with no arguments for a list\n");
25126     }
25127   }else
25128 
25129 #if SQLITE_SHELL_HAVE_RECOVER
25130   if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
25131     rc = shell_dbinfo_command(p, nArg, azArg);
25132   }else
25133 
25134   if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
25135     open_db(p, 0);
25136     rc = recoverDatabaseCmd(p, nArg, azArg);
25137   }else
25138 #endif /* SQLITE_SHELL_HAVE_RECOVER */
25139 
25140   if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
25141     char *zLike = 0;
25142     char *zSql;
25143     int i;
25144     int savedShowHeader = p->showHeader;
25145     int savedShellFlags = p->shellFlgs;
25146     ShellClearFlag(p,
25147        SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
25148        |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
25149     for(i=1; i<nArg; i++){
25150       if( azArg[i][0]=='-' ){
25151         const char *z = azArg[i]+1;
25152         if( z[0]=='-' ) z++;
25153         if( cli_strcmp(z,"preserve-rowids")==0 ){
25154 #ifdef SQLITE_OMIT_VIRTUALTABLE
25155           eputz("The --preserve-rowids option is not compatible"
25156                 " with SQLITE_OMIT_VIRTUALTABLE\n");
25157           rc = 1;
25158           sqlite3_free(zLike);
25159           goto meta_command_exit;
25160 #else
25161           ShellSetFlag(p, SHFLG_PreserveRowid);
25162 #endif
25163         }else
25164         if( cli_strcmp(z,"newlines")==0 ){
25165           ShellSetFlag(p, SHFLG_Newlines);
25166         }else
25167         if( cli_strcmp(z,"data-only")==0 ){
25168           ShellSetFlag(p, SHFLG_DumpDataOnly);
25169         }else
25170         if( cli_strcmp(z,"nosys")==0 ){
25171           ShellSetFlag(p, SHFLG_DumpNoSys);
25172         }else
25173         {
25174           eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
25175           rc = 1;
25176           sqlite3_free(zLike);
25177           goto meta_command_exit;
25178         }
25179       }else{
25180         /* azArg[i] contains a LIKE pattern. This ".dump" request should
25181         ** only dump data for tables for which either the table name matches
25182         ** the LIKE pattern, or the table appears to be a shadow table of
25183         ** a virtual table for which the name matches the LIKE pattern.
25184         */
25185         char *zExpr = sqlite3_mprintf(
25186             "name LIKE %Q ESCAPE '\\' OR EXISTS ("
25187             "  SELECT 1 FROM sqlite_schema WHERE "
25188             "    name LIKE %Q ESCAPE '\\' AND"
25189             "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
25190             "    substr(o.name, 1, length(name)+1) == (name||'_')"
25191             ")", azArg[i], azArg[i]
25192         );
25193 
25194         if( zLike ){
25195           zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
25196         }else{
25197           zLike = zExpr;
25198         }
25199       }
25200     }
25201 
25202     open_db(p, 0);
25203 
25204     outputDumpWarning(p, zLike);
25205     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25206       /* When playing back a "dump", the content might appear in an order
25207       ** which causes immediate foreign key constraints to be violated.
25208       ** So disable foreign-key constraint enforcement to prevent problems. */
25209       oputz("PRAGMA foreign_keys=OFF;\n");
25210       oputz("BEGIN TRANSACTION;\n");
25211     }
25212     p->writableSchema = 0;
25213     p->showHeader = 0;
25214     /* Set writable_schema=ON since doing so forces SQLite to initialize
25215     ** as much of the schema as it can even if the sqlite_schema table is
25216     ** corrupt. */
25217     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
25218     p->nErr = 0;
25219     if( zLike==0 ) zLike = sqlite3_mprintf("true");
25220     zSql = sqlite3_mprintf(
25221       "SELECT name, type, sql FROM sqlite_schema AS o "
25222       "WHERE (%s) AND type=='table'"
25223       "  AND sql NOT NULL"
25224       " ORDER BY tbl_name='sqlite_sequence', rowid",
25225       zLike
25226     );
25227     run_schema_dump_query(p,zSql);
25228     sqlite3_free(zSql);
25229     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25230       zSql = sqlite3_mprintf(
25231         "SELECT sql FROM sqlite_schema AS o "
25232         "WHERE (%s) AND sql NOT NULL"
25233         "  AND type IN ('index','trigger','view')",
25234         zLike
25235       );
25236       run_table_dump_query(p, zSql);
25237       sqlite3_free(zSql);
25238     }
25239     sqlite3_free(zLike);
25240     if( p->writableSchema ){
25241       oputz("PRAGMA writable_schema=OFF;\n");
25242       p->writableSchema = 0;
25243     }
25244     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
25245     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
25246     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
25247       oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
25248     }
25249     p->showHeader = savedShowHeader;
25250     p->shellFlgs = savedShellFlags;
25251   }else
25252 
25253   if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
25254     if( nArg==2 ){
25255       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
25256     }else{
25257       eputz("Usage: .echo on|off\n");
25258       rc = 1;
25259     }
25260   }else
25261 
25262   if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
25263     if( nArg==2 ){
25264       p->autoEQPtest = 0;
25265       if( p->autoEQPtrace ){
25266         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
25267         p->autoEQPtrace = 0;
25268       }
25269       if( cli_strcmp(azArg[1],"full")==0 ){
25270         p->autoEQP = AUTOEQP_full;
25271       }else if( cli_strcmp(azArg[1],"trigger")==0 ){
25272         p->autoEQP = AUTOEQP_trigger;
25273 #ifdef SQLITE_DEBUG
25274       }else if( cli_strcmp(azArg[1],"test")==0 ){
25275         p->autoEQP = AUTOEQP_on;
25276         p->autoEQPtest = 1;
25277       }else if( cli_strcmp(azArg[1],"trace")==0 ){
25278         p->autoEQP = AUTOEQP_full;
25279         p->autoEQPtrace = 1;
25280         open_db(p, 0);
25281         sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
25282         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
25283 #endif
25284       }else{
25285         p->autoEQP = (u8)booleanValue(azArg[1]);
25286       }
25287     }else{
25288       eputz("Usage: .eqp off|on|trace|trigger|full\n");
25289       rc = 1;
25290     }
25291   }else
25292 
25293 #ifndef SQLITE_SHELL_FIDDLE
25294   if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
25295     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
25296     rc = 2;
25297   }else
25298 #endif
25299 
25300   /* The ".explain" command is automatic now.  It is largely pointless.  It
25301   ** retained purely for backwards compatibility */
25302   if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
25303     int val = 1;
25304     if( nArg>=2 ){
25305       if( cli_strcmp(azArg[1],"auto")==0 ){
25306         val = 99;
25307       }else{
25308         val =  booleanValue(azArg[1]);
25309       }
25310     }
25311     if( val==1 && p->mode!=MODE_Explain ){
25312       p->normalMode = p->mode;
25313       p->mode = MODE_Explain;
25314       p->autoExplain = 0;
25315     }else if( val==0 ){
25316       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
25317       p->autoExplain = 0;
25318     }else if( val==99 ){
25319       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
25320       p->autoExplain = 1;
25321     }
25322   }else
25323 
25324 #ifndef SQLITE_OMIT_VIRTUALTABLE
25325   if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
25326     if( p->bSafeMode ){
25327       eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
25328             azArg[0]);
25329       rc = 1;
25330     }else{
25331       open_db(p, 0);
25332       expertDotCommand(p, azArg, nArg);
25333     }
25334   }else
25335 #endif
25336 
25337   if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
25338     static const struct {
25339        const char *zCtrlName;   /* Name of a test-control option */
25340        int ctrlCode;            /* Integer code for that option */
25341        const char *zUsage;      /* Usage notes */
25342     } aCtrl[] = {
25343       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
25344       { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
25345       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
25346       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
25347       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
25348    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
25349       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
25350       { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
25351       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
25352       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
25353    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
25354     };
25355     int filectrl = -1;
25356     int iCtrl = -1;
25357     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
25358     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
25359     int n2, i;
25360     const char *zCmd = 0;
25361     const char *zSchema = 0;
25362 
25363     open_db(p, 0);
25364     zCmd = nArg>=2 ? azArg[1] : "help";
25365 
25366     if( zCmd[0]=='-'
25367      && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
25368      && nArg>=4
25369     ){
25370       zSchema = azArg[2];
25371       for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
25372       nArg -= 2;
25373       zCmd = azArg[1];
25374     }
25375 
25376     /* The argument can optionally begin with "-" or "--" */
25377     if( zCmd[0]=='-' && zCmd[1] ){
25378       zCmd++;
25379       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
25380     }
25381 
25382     /* --help lists all file-controls */
25383     if( cli_strcmp(zCmd,"help")==0 ){
25384       oputz("Available file-controls:\n");
25385       for(i=0; i<ArraySize(aCtrl); i++){
25386         oputf("  .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
25387       }
25388       rc = 1;
25389       goto meta_command_exit;
25390     }
25391 
25392     /* convert filectrl text option to value. allow any unique prefix
25393     ** of the option name, or a numerical value. */
25394     n2 = strlen30(zCmd);
25395     for(i=0; i<ArraySize(aCtrl); i++){
25396       if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
25397         if( filectrl<0 ){
25398           filectrl = aCtrl[i].ctrlCode;
25399           iCtrl = i;
25400         }else{
25401           eputf("Error: ambiguous file-control: \"%s\"\n"
25402                 "Use \".filectrl --help\" for help\n", zCmd);
25403           rc = 1;
25404           goto meta_command_exit;
25405         }
25406       }
25407     }
25408     if( filectrl<0 ){
25409       eputf("Error: unknown file-control: %s\n"
25410             "Use \".filectrl --help\" for help\n", zCmd);
25411     }else{
25412       switch(filectrl){
25413         case SQLITE_FCNTL_SIZE_LIMIT: {
25414           if( nArg!=2 && nArg!=3 ) break;
25415           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
25416           sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
25417           isOk = 1;
25418           break;
25419         }
25420         case SQLITE_FCNTL_LOCK_TIMEOUT:
25421         case SQLITE_FCNTL_CHUNK_SIZE: {
25422           int x;
25423           if( nArg!=3 ) break;
25424           x = (int)integerValue(azArg[2]);
25425           sqlite3_file_control(p->db, zSchema, filectrl, &x);
25426           isOk = 2;
25427           break;
25428         }
25429         case SQLITE_FCNTL_PERSIST_WAL:
25430         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
25431           int x;
25432           if( nArg!=2 && nArg!=3 ) break;
25433           x = nArg==3 ? booleanValue(azArg[2]) : -1;
25434           sqlite3_file_control(p->db, zSchema, filectrl, &x);
25435           iRes = x;
25436           isOk = 1;
25437           break;
25438         }
25439         case SQLITE_FCNTL_DATA_VERSION:
25440         case SQLITE_FCNTL_HAS_MOVED: {
25441           int x;
25442           if( nArg!=2 ) break;
25443           sqlite3_file_control(p->db, zSchema, filectrl, &x);
25444           iRes = x;
25445           isOk = 1;
25446           break;
25447         }
25448         case SQLITE_FCNTL_TEMPFILENAME: {
25449           char *z = 0;
25450           if( nArg!=2 ) break;
25451           sqlite3_file_control(p->db, zSchema, filectrl, &z);
25452           if( z ){
25453             oputf("%s\n", z);
25454             sqlite3_free(z);
25455           }
25456           isOk = 2;
25457           break;
25458         }
25459         case SQLITE_FCNTL_RESERVE_BYTES: {
25460           int x;
25461           if( nArg>=3 ){
25462             x = atoi(azArg[2]);
25463             sqlite3_file_control(p->db, zSchema, filectrl, &x);
25464           }
25465           x = -1;
25466           sqlite3_file_control(p->db, zSchema, filectrl, &x);
25467           oputf("%d\n", x);
25468           isOk = 2;
25469           break;
25470         }
25471       }
25472     }
25473     if( isOk==0 && iCtrl>=0 ){
25474       oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
25475       rc = 1;
25476     }else if( isOk==1 ){
25477       char zBuf[100];
25478       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
25479       oputf("%s\n", zBuf);
25480     }
25481   }else
25482 
25483   if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
25484     ShellState data;
25485     int doStats = 0;
25486     memcpy(&data, p, sizeof(data));
25487     data.showHeader = 0;
25488     data.cMode = data.mode = MODE_Semi;
25489     if( nArg==2 && optionMatch(azArg[1], "indent") ){
25490       data.cMode = data.mode = MODE_Pretty;
25491       nArg = 1;
25492     }
25493     if( nArg!=1 ){
25494       eputz("Usage: .fullschema ?--indent?\n");
25495       rc = 1;
25496       goto meta_command_exit;
25497     }
25498     open_db(p, 0);
25499     rc = sqlite3_exec(p->db,
25500        "SELECT sql FROM"
25501        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
25502        "     FROM sqlite_schema UNION ALL"
25503        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
25504        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
25505        "ORDER BY x",
25506        callback, &data, 0
25507     );
25508     if( rc==SQLITE_OK ){
25509       sqlite3_stmt *pStmt;
25510       rc = sqlite3_prepare_v2(p->db,
25511                "SELECT rowid FROM sqlite_schema"
25512                " WHERE name GLOB 'sqlite_stat[134]'",
25513                -1, &pStmt, 0);
25514       if( rc==SQLITE_OK ){
25515         doStats = sqlite3_step(pStmt)==SQLITE_ROW;
25516         sqlite3_finalize(pStmt);
25517       }
25518     }
25519     if( doStats==0 ){
25520       oputz("/* No STAT tables available */\n");
25521     }else{
25522       oputz("ANALYZE sqlite_schema;\n");
25523       data.cMode = data.mode = MODE_Insert;
25524       data.zDestTable = "sqlite_stat1";
25525       shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
25526       data.zDestTable = "sqlite_stat4";
25527       shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
25528       oputz("ANALYZE sqlite_schema;\n");
25529     }
25530   }else
25531 
25532   if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
25533     if( nArg==2 ){
25534       p->showHeader = booleanValue(azArg[1]);
25535       p->shellFlgs |= SHFLG_HeaderSet;
25536     }else{
25537       eputz("Usage: .headers on|off\n");
25538       rc = 1;
25539     }
25540   }else
25541 
25542   if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
25543     if( nArg>=2 ){
25544       n = showHelp(p->out, azArg[1]);
25545       if( n==0 ){
25546         oputf("Nothing matches '%s'\n", azArg[1]);
25547       }
25548     }else{
25549       showHelp(p->out, 0);
25550     }
25551   }else
25552 
25553 #ifndef SQLITE_SHELL_FIDDLE
25554   if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
25555     char *zTable = 0;           /* Insert data into this table */
25556     char *zSchema = 0;          /* within this schema (may default to "main") */
25557     char *zFile = 0;            /* Name of file to extra content from */
25558     sqlite3_stmt *pStmt = NULL; /* A statement */
25559     int nCol;                   /* Number of columns in the table */
25560     int nByte;                  /* Number of bytes in an SQL string */
25561     int i, j;                   /* Loop counters */
25562     int needCommit;             /* True to COMMIT or ROLLBACK at end */
25563     int nSep;                   /* Number of bytes in p->colSeparator[] */
25564     char *zSql;                 /* An SQL statement */
25565     char *zFullTabName;         /* Table name with schema if applicable */
25566     ImportCtx sCtx;             /* Reader context */
25567     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
25568     int eVerbose = 0;           /* Larger for more console output */
25569     int nSkip = 0;              /* Initial lines to skip */
25570     int useOutputMode = 1;      /* Use output mode to determine separators */
25571     char *zCreate = 0;          /* CREATE TABLE statement text */
25572 
25573     failIfSafeMode(p, "cannot run .import in safe mode");
25574     memset(&sCtx, 0, sizeof(sCtx));
25575     if( p->mode==MODE_Ascii ){
25576       xRead = ascii_read_one_field;
25577     }else{
25578       xRead = csv_read_one_field;
25579     }
25580     rc = 1;
25581     for(i=1; i<nArg; i++){
25582       char *z = azArg[i];
25583       if( z[0]=='-' && z[1]=='-' ) z++;
25584       if( z[0]!='-' ){
25585         if( zFile==0 ){
25586           zFile = z;
25587         }else if( zTable==0 ){
25588           zTable = z;
25589         }else{
25590           oputf("ERROR: extra argument: \"%s\".  Usage:\n", z);
25591           showHelp(p->out, "import");
25592           goto meta_command_exit;
25593         }
25594       }else if( cli_strcmp(z,"-v")==0 ){
25595         eVerbose++;
25596       }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
25597         zSchema = azArg[++i];
25598       }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
25599         nSkip = integerValue(azArg[++i]);
25600       }else if( cli_strcmp(z,"-ascii")==0 ){
25601         sCtx.cColSep = SEP_Unit[0];
25602         sCtx.cRowSep = SEP_Record[0];
25603         xRead = ascii_read_one_field;
25604         useOutputMode = 0;
25605       }else if( cli_strcmp(z,"-csv")==0 ){
25606         sCtx.cColSep = ',';
25607         sCtx.cRowSep = '\n';
25608         xRead = csv_read_one_field;
25609         useOutputMode = 0;
25610       }else{
25611         oputf("ERROR: unknown option: \"%s\".  Usage:\n", z);
25612         showHelp(p->out, "import");
25613         goto meta_command_exit;
25614       }
25615     }
25616     if( zTable==0 ){
25617       oputf("ERROR: missing %s argument. Usage:\n",
25618             zFile==0 ? "FILE" : "TABLE");
25619       showHelp(p->out, "import");
25620       goto meta_command_exit;
25621     }
25622     seenInterrupt = 0;
25623     open_db(p, 0);
25624     if( useOutputMode ){
25625       /* If neither the --csv or --ascii options are specified, then set
25626       ** the column and row separator characters from the output mode. */
25627       nSep = strlen30(p->colSeparator);
25628       if( nSep==0 ){
25629         eputz("Error: non-null column separator required for import\n");
25630         goto meta_command_exit;
25631       }
25632       if( nSep>1 ){
25633         eputz("Error: multi-character column separators not allowed"
25634               " for import\n");
25635         goto meta_command_exit;
25636       }
25637       nSep = strlen30(p->rowSeparator);
25638       if( nSep==0 ){
25639         eputz("Error: non-null row separator required for import\n");
25640         goto meta_command_exit;
25641       }
25642       if( nSep==2 && p->mode==MODE_Csv
25643        && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
25644       ){
25645         /* When importing CSV (only), if the row separator is set to the
25646         ** default output row separator, change it to the default input
25647         ** row separator.  This avoids having to maintain different input
25648         ** and output row separators. */
25649         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
25650         nSep = strlen30(p->rowSeparator);
25651       }
25652       if( nSep>1 ){
25653         eputz("Error: multi-character row separators not allowed"
25654               " for import\n");
25655         goto meta_command_exit;
25656       }
25657       sCtx.cColSep = (u8)p->colSeparator[0];
25658       sCtx.cRowSep = (u8)p->rowSeparator[0];
25659     }
25660     sCtx.zFile = zFile;
25661     sCtx.nLine = 1;
25662     if( sCtx.zFile[0]=='|' ){
25663 #ifdef SQLITE_OMIT_POPEN
25664       eputz("Error: pipes are not supported in this OS\n");
25665       goto meta_command_exit;
25666 #else
25667       sCtx.in = popen(sCtx.zFile+1, "r");
25668       sCtx.zFile = "<pipe>";
25669       sCtx.xCloser = pclose;
25670 #endif
25671     }else{
25672       sCtx.in = fopen(sCtx.zFile, "rb");
25673       sCtx.xCloser = fclose;
25674     }
25675     if( sCtx.in==0 ){
25676       eputf("Error: cannot open \"%s\"\n", zFile);
25677       goto meta_command_exit;
25678     }
25679     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
25680       char zSep[2];
25681       zSep[1] = 0;
25682       zSep[0] = sCtx.cColSep;
25683       oputz("Column separator ");
25684       output_c_string(zSep);
25685       oputz(", row separator ");
25686       zSep[0] = sCtx.cRowSep;
25687       output_c_string(zSep);
25688       oputz("\n");
25689     }
25690     sCtx.z = sqlite3_malloc64(120);
25691     if( sCtx.z==0 ){
25692       import_cleanup(&sCtx);
25693       shell_out_of_memory();
25694     }
25695     /* Below, resources must be freed before exit. */
25696     while( (nSkip--)>0 ){
25697       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
25698     }
25699     if( zSchema!=0 ){
25700       zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
25701     }else{
25702       zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
25703     }
25704     zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
25705     if( zSql==0 || zFullTabName==0 ){
25706       import_cleanup(&sCtx);
25707       shell_out_of_memory();
25708     }
25709     nByte = strlen30(zSql);
25710     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25711     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
25712     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
25713       sqlite3 *dbCols = 0;
25714       char *zRenames = 0;
25715       char *zColDefs;
25716       zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
25717       while( xRead(&sCtx) ){
25718         zAutoColumn(sCtx.z, &dbCols, 0);
25719         if( sCtx.cTerm!=sCtx.cColSep ) break;
25720       }
25721       zColDefs = zAutoColumn(0, &dbCols, &zRenames);
25722       if( zRenames!=0 ){
25723         sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
25724               "Columns renamed during .import %s due to duplicates:\n"
25725               "%s\n", sCtx.zFile, zRenames);
25726         sqlite3_free(zRenames);
25727       }
25728       assert(dbCols==0);
25729       if( zColDefs==0 ){
25730         eputf("%s: empty file\n", sCtx.zFile);
25731       import_fail:
25732         sqlite3_free(zCreate);
25733         sqlite3_free(zSql);
25734         sqlite3_free(zFullTabName);
25735         import_cleanup(&sCtx);
25736         rc = 1;
25737         goto meta_command_exit;
25738       }
25739       zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
25740       if( eVerbose>=1 ){
25741         oputf("%s\n", zCreate);
25742       }
25743       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
25744       if( rc ){
25745         eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
25746         goto import_fail;
25747       }
25748       sqlite3_free(zCreate);
25749       zCreate = 0;
25750       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25751     }
25752     if( rc ){
25753       if (pStmt) sqlite3_finalize(pStmt);
25754       eputf("Error: %s\n", sqlite3_errmsg(p->db));
25755       goto import_fail;
25756     }
25757     sqlite3_free(zSql);
25758     nCol = sqlite3_column_count(pStmt);
25759     sqlite3_finalize(pStmt);
25760     pStmt = 0;
25761     if( nCol==0 ) return 0; /* no columns, no error */
25762     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
25763     if( zSql==0 ){
25764       import_cleanup(&sCtx);
25765       shell_out_of_memory();
25766     }
25767     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
25768     j = strlen30(zSql);
25769     for(i=1; i<nCol; i++){
25770       zSql[j++] = ',';
25771       zSql[j++] = '?';
25772     }
25773     zSql[j++] = ')';
25774     zSql[j] = 0;
25775     if( eVerbose>=2 ){
25776       oputf("Insert using: %s\n", zSql);
25777     }
25778     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25779     if( rc ){
25780       eputf("Error: %s\n", sqlite3_errmsg(p->db));
25781       if (pStmt) sqlite3_finalize(pStmt);
25782       goto import_fail;
25783     }
25784     sqlite3_free(zSql);
25785     sqlite3_free(zFullTabName);
25786     needCommit = sqlite3_get_autocommit(p->db);
25787     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
25788     do{
25789       int startLine = sCtx.nLine;
25790       for(i=0; i<nCol; i++){
25791         char *z = xRead(&sCtx);
25792         /*
25793         ** Did we reach end-of-file before finding any columns?
25794         ** If so, stop instead of NULL filling the remaining columns.
25795         */
25796         if( z==0 && i==0 ) break;
25797         /*
25798         ** Did we reach end-of-file OR end-of-line before finding any
25799         ** columns in ASCII mode?  If so, stop instead of NULL filling
25800         ** the remaining columns.
25801         */
25802         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
25803         /*
25804         ** For CSV mode, per RFC 4180, accept EOF in lieu of final
25805         ** record terminator but only for last field of multi-field row.
25806         ** (If there are too few fields, it's not valid CSV anyway.)
25807         */
25808         if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
25809           z = "";
25810         }
25811         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
25812         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
25813           eputf("%s:%d: expected %d columns but found %d"
25814                 " - filling the rest with NULL\n",
25815                 sCtx.zFile, startLine, nCol, i+1);
25816           i += 2;
25817           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
25818         }
25819       }
25820       if( sCtx.cTerm==sCtx.cColSep ){
25821         do{
25822           xRead(&sCtx);
25823           i++;
25824         }while( sCtx.cTerm==sCtx.cColSep );
25825         eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
25826               sCtx.zFile, startLine, nCol, i);
25827       }
25828       if( i>=nCol ){
25829         sqlite3_step(pStmt);
25830         rc = sqlite3_reset(pStmt);
25831         if( rc!=SQLITE_OK ){
25832           eputf("%s:%d: INSERT failed: %s\n",
25833                 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
25834           sCtx.nErr++;
25835         }else{
25836           sCtx.nRow++;
25837         }
25838       }
25839     }while( sCtx.cTerm!=EOF );
25840 
25841     import_cleanup(&sCtx);
25842     sqlite3_finalize(pStmt);
25843     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
25844     if( eVerbose>0 ){
25845       oputf("Added %d rows with %d errors using %d lines of input\n",
25846             sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
25847     }
25848   }else
25849 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
25850 
25851 #ifndef SQLITE_UNTESTABLE
25852   if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
25853     char *zSql;
25854     char *zCollist = 0;
25855     sqlite3_stmt *pStmt;
25856     int tnum = 0;
25857     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
25858     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
25859     int i;
25860     if( !ShellHasFlag(p,SHFLG_TestingMode) ){
25861       eputf(".%s unavailable without --unsafe-testing\n",
25862             "imposter");
25863       rc = 1;
25864       goto meta_command_exit;
25865     }
25866     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
25867       eputz("Usage: .imposter INDEX IMPOSTER\n"
25868             "       .imposter off\n");
25869       /* Also allowed, but not documented:
25870       **
25871       **    .imposter TABLE IMPOSTER
25872       **
25873       ** where TABLE is a WITHOUT ROWID table.  In that case, the
25874       ** imposter is another WITHOUT ROWID table with the columns in
25875       ** storage order. */
25876       rc = 1;
25877       goto meta_command_exit;
25878     }
25879     open_db(p, 0);
25880     if( nArg==2 ){
25881       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
25882       goto meta_command_exit;
25883     }
25884     zSql = sqlite3_mprintf(
25885       "SELECT rootpage, 0 FROM sqlite_schema"
25886       " WHERE name='%q' AND type='index'"
25887       "UNION ALL "
25888       "SELECT rootpage, 1 FROM sqlite_schema"
25889       " WHERE name='%q' AND type='table'"
25890       "   AND sql LIKE '%%without%%rowid%%'",
25891       azArg[1], azArg[1]
25892     );
25893     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25894     sqlite3_free(zSql);
25895     if( sqlite3_step(pStmt)==SQLITE_ROW ){
25896       tnum = sqlite3_column_int(pStmt, 0);
25897       isWO = sqlite3_column_int(pStmt, 1);
25898     }
25899     sqlite3_finalize(pStmt);
25900     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
25901     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25902     sqlite3_free(zSql);
25903     i = 0;
25904     while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
25905       char zLabel[20];
25906       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
25907       i++;
25908       if( zCol==0 ){
25909         if( sqlite3_column_int(pStmt,1)==-1 ){
25910           zCol = "_ROWID_";
25911         }else{
25912           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
25913           zCol = zLabel;
25914         }
25915       }
25916       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
25917         lenPK = (int)strlen(zCollist);
25918       }
25919       if( zCollist==0 ){
25920         zCollist = sqlite3_mprintf("\"%w\"", zCol);
25921       }else{
25922         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
25923       }
25924     }
25925     sqlite3_finalize(pStmt);
25926     if( i==0 || tnum==0 ){
25927       eputf("no such index: \"%s\"\n", azArg[1]);
25928       rc = 1;
25929       sqlite3_free(zCollist);
25930       goto meta_command_exit;
25931     }
25932     if( lenPK==0 ) lenPK = 100000;
25933     zSql = sqlite3_mprintf(
25934           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
25935           azArg[2], zCollist, lenPK, zCollist);
25936     sqlite3_free(zCollist);
25937     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
25938     if( rc==SQLITE_OK ){
25939       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
25940       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
25941       if( rc ){
25942         eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
25943       }else{
25944         sputf(stdout, "%s;\n", zSql);
25945         sputf(stdout, "WARNING: writing to an imposter table will corrupt"
25946               " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
25947       }
25948     }else{
25949       eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
25950       rc = 1;
25951     }
25952     sqlite3_free(zSql);
25953   }else
25954 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
25955 
25956 #ifdef SQLITE_ENABLE_IOTRACE
25957   if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
25958     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
25959     if( iotrace && iotrace!=stdout ) fclose(iotrace);
25960     iotrace = 0;
25961     if( nArg<2 ){
25962       sqlite3IoTrace = 0;
25963     }else if( cli_strcmp(azArg[1], "-")==0 ){
25964       sqlite3IoTrace = iotracePrintf;
25965       iotrace = stdout;
25966     }else{
25967       iotrace = fopen(azArg[1], "w");
25968       if( iotrace==0 ){
25969         eputf("Error: cannot open \"%s\"\n", azArg[1]);
25970         sqlite3IoTrace = 0;
25971         rc = 1;
25972       }else{
25973         sqlite3IoTrace = iotracePrintf;
25974       }
25975     }
25976   }else
25977 #endif
25978 
25979   if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
25980     static const struct {
25981        const char *zLimitName;   /* Name of a limit */
25982        int limitCode;            /* Integer code for that limit */
25983     } aLimit[] = {
25984       { "length",                SQLITE_LIMIT_LENGTH                    },
25985       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
25986       { "column",                SQLITE_LIMIT_COLUMN                    },
25987       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
25988       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
25989       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
25990       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
25991       { "attached",              SQLITE_LIMIT_ATTACHED                  },
25992       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
25993       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
25994       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
25995       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
25996     };
25997     int i, n2;
25998     open_db(p, 0);
25999     if( nArg==1 ){
26000       for(i=0; i<ArraySize(aLimit); i++){
26001         sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
26002               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
26003       }
26004     }else if( nArg>3 ){
26005       eputz("Usage: .limit NAME ?NEW-VALUE?\n");
26006       rc = 1;
26007       goto meta_command_exit;
26008     }else{
26009       int iLimit = -1;
26010       n2 = strlen30(azArg[1]);
26011       for(i=0; i<ArraySize(aLimit); i++){
26012         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
26013           if( iLimit<0 ){
26014             iLimit = i;
26015           }else{
26016             eputf("ambiguous limit: \"%s\"\n", azArg[1]);
26017             rc = 1;
26018             goto meta_command_exit;
26019           }
26020         }
26021       }
26022       if( iLimit<0 ){
26023         eputf("unknown limit: \"%s\"\n"
26024               "enter \".limits\" with no arguments for a list.\n",
26025               azArg[1]);
26026         rc = 1;
26027         goto meta_command_exit;
26028       }
26029       if( nArg==3 ){
26030         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
26031                       (int)integerValue(azArg[2]));
26032       }
26033       sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
26034             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
26035     }
26036   }else
26037 
26038   if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
26039     open_db(p, 0);
26040     lintDotCommand(p, azArg, nArg);
26041   }else
26042 
26043 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
26044   if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
26045     const char *zFile, *zProc;
26046     char *zErrMsg = 0;
26047     failIfSafeMode(p, "cannot run .load in safe mode");
26048     if( nArg<2 || azArg[1][0]==0 ){
26049       /* Must have a non-empty FILE. (Will not load self.) */
26050       eputz("Usage: .load FILE ?ENTRYPOINT?\n");
26051       rc = 1;
26052       goto meta_command_exit;
26053     }
26054     zFile = azArg[1];
26055     zProc = nArg>=3 ? azArg[2] : 0;
26056     open_db(p, 0);
26057     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
26058     if( rc!=SQLITE_OK ){
26059       eputf("Error: %s\n", zErrMsg);
26060       sqlite3_free(zErrMsg);
26061       rc = 1;
26062     }
26063   }else
26064 #endif
26065 
26066   if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
26067     if( nArg!=2 ){
26068       eputz("Usage: .log FILENAME\n");
26069       rc = 1;
26070     }else{
26071       const char *zFile = azArg[1];
26072       if( p->bSafeMode
26073        && cli_strcmp(zFile,"on")!=0
26074        && cli_strcmp(zFile,"off")!=0
26075       ){
26076         sputz(stdout, "cannot set .log to anything other"
26077               " than \"on\" or \"off\"\n");
26078         zFile = "off";
26079       }
26080       output_file_close(p->pLog);
26081       if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
26082       p->pLog = output_file_open(zFile, 0);
26083     }
26084   }else
26085 
26086   if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
26087     const char *zMode = 0;
26088     const char *zTabname = 0;
26089     int i, n2;
26090     ColModeOpts cmOpts = ColModeOpts_default;
26091     for(i=1; i<nArg; i++){
26092       const char *z = azArg[i];
26093       if( optionMatch(z,"wrap") && i+1<nArg ){
26094         cmOpts.iWrap = integerValue(azArg[++i]);
26095       }else if( optionMatch(z,"ww") ){
26096         cmOpts.bWordWrap = 1;
26097       }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
26098         cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
26099       }else if( optionMatch(z,"quote") ){
26100         cmOpts.bQuote = 1;
26101       }else if( optionMatch(z,"noquote") ){
26102         cmOpts.bQuote = 0;
26103       }else if( zMode==0 ){
26104         zMode = z;
26105         /* Apply defaults for qbox pseudo-mode.  If that
26106          * overwrites already-set values, user was informed of this.
26107          */
26108         if( cli_strcmp(z, "qbox")==0 ){
26109           ColModeOpts cmo = ColModeOpts_default_qbox;
26110           zMode = "box";
26111           cmOpts = cmo;
26112         }
26113       }else if( zTabname==0 ){
26114         zTabname = z;
26115       }else if( z[0]=='-' ){
26116         eputf("unknown option: %s\n", z);
26117         eputz("options:\n"
26118               "  --noquote\n"
26119               "  --quote\n"
26120               "  --wordwrap on/off\n"
26121               "  --wrap N\n"
26122               "  --ww\n");
26123         rc = 1;
26124         goto meta_command_exit;
26125       }else{
26126         eputf("extra argument: \"%s\"\n", z);
26127         rc = 1;
26128         goto meta_command_exit;
26129       }
26130     }
26131     if( zMode==0 ){
26132       if( p->mode==MODE_Column
26133        || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
26134       ){
26135         oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
26136               modeDescr[p->mode], p->cmOpts.iWrap,
26137               p->cmOpts.bWordWrap ? "on" : "off",
26138               p->cmOpts.bQuote ? "" : "no");
26139       }else{
26140         oputf("current output mode: %s\n", modeDescr[p->mode]);
26141       }
26142       zMode = modeDescr[p->mode];
26143     }
26144     n2 = strlen30(zMode);
26145     if( cli_strncmp(zMode,"lines",n2)==0 ){
26146       p->mode = MODE_Line;
26147       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26148     }else if( cli_strncmp(zMode,"columns",n2)==0 ){
26149       p->mode = MODE_Column;
26150       if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
26151         p->showHeader = 1;
26152       }
26153       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26154       p->cmOpts = cmOpts;
26155     }else if( cli_strncmp(zMode,"list",n2)==0 ){
26156       p->mode = MODE_List;
26157       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
26158       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26159     }else if( cli_strncmp(zMode,"html",n2)==0 ){
26160       p->mode = MODE_Html;
26161     }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
26162       p->mode = MODE_Tcl;
26163       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
26164       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26165     }else if( cli_strncmp(zMode,"csv",n2)==0 ){
26166       p->mode = MODE_Csv;
26167       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26168       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
26169     }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
26170       p->mode = MODE_List;
26171       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
26172     }else if( cli_strncmp(zMode,"insert",n2)==0 ){
26173       p->mode = MODE_Insert;
26174       set_table_name(p, zTabname ? zTabname : "table");
26175     }else if( cli_strncmp(zMode,"quote",n2)==0 ){
26176       p->mode = MODE_Quote;
26177       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26178       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
26179     }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
26180       p->mode = MODE_Ascii;
26181       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
26182       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
26183     }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
26184       p->mode = MODE_Markdown;
26185       p->cmOpts = cmOpts;
26186     }else if( cli_strncmp(zMode,"table",n2)==0 ){
26187       p->mode = MODE_Table;
26188       p->cmOpts = cmOpts;
26189     }else if( cli_strncmp(zMode,"box",n2)==0 ){
26190       p->mode = MODE_Box;
26191       p->cmOpts = cmOpts;
26192     }else if( cli_strncmp(zMode,"count",n2)==0 ){
26193       p->mode = MODE_Count;
26194     }else if( cli_strncmp(zMode,"off",n2)==0 ){
26195       p->mode = MODE_Off;
26196     }else if( cli_strncmp(zMode,"json",n2)==0 ){
26197       p->mode = MODE_Json;
26198     }else{
26199       eputz("Error: mode should be one of: "
26200             "ascii box column csv html insert json line list markdown "
26201             "qbox quote table tabs tcl\n");
26202       rc = 1;
26203     }
26204     p->cMode = p->mode;
26205   }else
26206 
26207 #ifndef SQLITE_SHELL_FIDDLE
26208   if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
26209     if( nArg!=2 ){
26210       eputz("Usage: .nonce NONCE\n");
26211       rc = 1;
26212     }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
26213       eputf("line %d: incorrect nonce: \"%s\"\n",
26214             p->lineno, azArg[1]);
26215       exit(1);
26216     }else{
26217       p->bSafeMode = 0;
26218       return 0;  /* Return immediately to bypass the safe mode reset
26219                  ** at the end of this procedure */
26220     }
26221   }else
26222 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26223 
26224   if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
26225     if( nArg==2 ){
26226       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
26227                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
26228     }else{
26229       eputz("Usage: .nullvalue STRING\n");
26230       rc = 1;
26231     }
26232   }else
26233 
26234   if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
26235     const char *zFN = 0;     /* Pointer to constant filename */
26236     char *zNewFilename = 0;  /* Name of the database file to open */
26237     int iName = 1;           /* Index in azArg[] of the filename */
26238     int newFlag = 0;         /* True to delete file before opening */
26239     int openMode = SHELL_OPEN_UNSPEC;
26240 
26241     /* Check for command-line arguments */
26242     for(iName=1; iName<nArg; iName++){
26243       const char *z = azArg[iName];
26244 #ifndef SQLITE_SHELL_FIDDLE
26245       if( optionMatch(z,"new") ){
26246         newFlag = 1;
26247 #ifdef SQLITE_HAVE_ZLIB
26248       }else if( optionMatch(z, "zip") ){
26249         openMode = SHELL_OPEN_ZIPFILE;
26250 #endif
26251       }else if( optionMatch(z, "append") ){
26252         openMode = SHELL_OPEN_APPENDVFS;
26253       }else if( optionMatch(z, "readonly") ){
26254         openMode = SHELL_OPEN_READONLY;
26255       }else if( optionMatch(z, "nofollow") ){
26256         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
26257 #ifndef SQLITE_OMIT_DESERIALIZE
26258       }else if( optionMatch(z, "deserialize") ){
26259         openMode = SHELL_OPEN_DESERIALIZE;
26260       }else if( optionMatch(z, "hexdb") ){
26261         openMode = SHELL_OPEN_HEXDB;
26262       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
26263         p->szMax = integerValue(azArg[++iName]);
26264 #endif /* SQLITE_OMIT_DESERIALIZE */
26265       }else
26266 #endif /* !SQLITE_SHELL_FIDDLE */
26267       if( z[0]=='-' ){
26268         eputf("unknown option: %s\n", z);
26269         rc = 1;
26270         goto meta_command_exit;
26271       }else if( zFN ){
26272         eputf("extra argument: \"%s\"\n", z);
26273         rc = 1;
26274         goto meta_command_exit;
26275       }else{
26276         zFN = z;
26277       }
26278     }
26279 
26280     /* Close the existing database */
26281     session_close_all(p, -1);
26282     close_db(p->db);
26283     p->db = 0;
26284     p->pAuxDb->zDbFilename = 0;
26285     sqlite3_free(p->pAuxDb->zFreeOnClose);
26286     p->pAuxDb->zFreeOnClose = 0;
26287     p->openMode = openMode;
26288     p->openFlags = 0;
26289     p->szMax = 0;
26290 
26291     /* If a filename is specified, try to open it first */
26292     if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
26293       if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
26294 #ifndef SQLITE_SHELL_FIDDLE
26295       if( p->bSafeMode
26296        && p->openMode!=SHELL_OPEN_HEXDB
26297        && zFN
26298        && cli_strcmp(zFN,":memory:")!=0
26299       ){
26300         failIfSafeMode(p, "cannot open disk-based database files in safe mode");
26301       }
26302 #else
26303       /* WASM mode has its own sandboxed pseudo-filesystem. */
26304 #endif
26305       if( zFN ){
26306         zNewFilename = sqlite3_mprintf("%s", zFN);
26307         shell_check_oom(zNewFilename);
26308       }else{
26309         zNewFilename = 0;
26310       }
26311       p->pAuxDb->zDbFilename = zNewFilename;
26312       open_db(p, OPEN_DB_KEEPALIVE);
26313       if( p->db==0 ){
26314         eputf("Error: cannot open '%s'\n", zNewFilename);
26315         sqlite3_free(zNewFilename);
26316       }else{
26317         p->pAuxDb->zFreeOnClose = zNewFilename;
26318       }
26319     }
26320     if( p->db==0 ){
26321       /* As a fall-back open a TEMP database */
26322       p->pAuxDb->zDbFilename = 0;
26323       open_db(p, 0);
26324     }
26325   }else
26326 
26327 #ifndef SQLITE_SHELL_FIDDLE
26328   if( (c=='o'
26329         && (cli_strncmp(azArg[0], "output", n)==0
26330             || cli_strncmp(azArg[0], "once", n)==0))
26331    || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
26332   ){
26333     char *zFile = 0;
26334     int bTxtMode = 0;
26335     int i;
26336     int eMode = 0;
26337     int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
26338     static const char *zBomUtf8 = "\xef\xbb\xbf";
26339     const char *zBom = 0;
26340 
26341     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
26342     if( c=='e' ){
26343       eMode = 'x';
26344       bOnce = 2;
26345     }else if( cli_strncmp(azArg[0],"once",n)==0 ){
26346       bOnce = 1;
26347     }
26348     for(i=1; i<nArg; i++){
26349       char *z = azArg[i];
26350       if( z[0]=='-' ){
26351         if( z[1]=='-' ) z++;
26352         if( cli_strcmp(z,"-bom")==0 ){
26353           zBom = zBomUtf8;
26354         }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
26355           eMode = 'x';  /* spreadsheet */
26356         }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
26357           eMode = 'e';  /* text editor */
26358         }else{
26359           oputf("ERROR: unknown option: \"%s\".  Usage:\n", azArg[i]);
26360           showHelp(p->out, azArg[0]);
26361           rc = 1;
26362           goto meta_command_exit;
26363         }
26364       }else if( zFile==0 && eMode!='e' && eMode!='x' ){
26365         zFile = sqlite3_mprintf("%s", z);
26366         if( zFile && zFile[0]=='|' ){
26367           while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
26368           break;
26369         }
26370       }else{
26371         oputf("ERROR: extra parameter: \"%s\".  Usage:\n", azArg[i]);
26372         showHelp(p->out, azArg[0]);
26373         rc = 1;
26374         sqlite3_free(zFile);
26375         goto meta_command_exit;
26376       }
26377     }
26378     if( zFile==0 ){
26379       zFile = sqlite3_mprintf("stdout");
26380     }
26381     if( bOnce ){
26382       p->outCount = 2;
26383     }else{
26384       p->outCount = 0;
26385     }
26386     output_reset(p);
26387 #ifndef SQLITE_NOHAVE_SYSTEM
26388     if( eMode=='e' || eMode=='x' ){
26389       p->doXdgOpen = 1;
26390       outputModePush(p);
26391       if( eMode=='x' ){
26392         /* spreadsheet mode.  Output as CSV. */
26393         newTempFile(p, "csv");
26394         ShellClearFlag(p, SHFLG_Echo);
26395         p->mode = MODE_Csv;
26396         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
26397         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
26398       }else{
26399         /* text editor mode */
26400         newTempFile(p, "txt");
26401         bTxtMode = 1;
26402       }
26403       sqlite3_free(zFile);
26404       zFile = sqlite3_mprintf("%s", p->zTempFile);
26405     }
26406 #endif /* SQLITE_NOHAVE_SYSTEM */
26407     shell_check_oom(zFile);
26408     if( zFile[0]=='|' ){
26409 #ifdef SQLITE_OMIT_POPEN
26410       eputz("Error: pipes are not supported in this OS\n");
26411       rc = 1;
26412       output_redir(p, stdout);
26413 #else
26414       FILE *pfPipe = popen(zFile + 1, "w");
26415       if( pfPipe==0 ){
26416         eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
26417         rc = 1;
26418       }else{
26419         output_redir(p, pfPipe);
26420         if( zBom ) oputz(zBom);
26421         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26422       }
26423 #endif
26424     }else{
26425       FILE *pfFile = output_file_open(zFile, bTxtMode);
26426       if( pfFile==0 ){
26427         if( cli_strcmp(zFile,"off")!=0 ){
26428           eputf("Error: cannot write to \"%s\"\n", zFile);
26429         }
26430         rc = 1;
26431       } else {
26432         output_redir(p, pfFile);
26433         if( zBom ) oputz(zBom);
26434         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
26435       }
26436     }
26437     sqlite3_free(zFile);
26438   }else
26439 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26440 
26441   if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
26442     open_db(p,0);
26443     if( nArg<=1 ) goto parameter_syntax_error;
26444 
26445     /* .parameter clear
26446     ** Clear all bind parameters by dropping the TEMP table that holds them.
26447     */
26448     if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
26449       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
26450                    0, 0, 0);
26451     }else
26452 
26453     /* .parameter list
26454     ** List all bind parameters.
26455     */
26456     if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
26457       sqlite3_stmt *pStmt = 0;
26458       int rx;
26459       int len = 0;
26460       rx = sqlite3_prepare_v2(p->db,
26461              "SELECT max(length(key)) "
26462              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
26463       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
26464         len = sqlite3_column_int(pStmt, 0);
26465         if( len>40 ) len = 40;
26466       }
26467       sqlite3_finalize(pStmt);
26468       pStmt = 0;
26469       if( len ){
26470         rx = sqlite3_prepare_v2(p->db,
26471              "SELECT key, quote(value) "
26472              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
26473         while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
26474           oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
26475                 sqlite3_column_text(pStmt,1));
26476         }
26477         sqlite3_finalize(pStmt);
26478       }
26479     }else
26480 
26481     /* .parameter init
26482     ** Make sure the TEMP table used to hold bind parameters exists.
26483     ** Create it if necessary.
26484     */
26485     if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
26486       bind_table_init(p);
26487     }else
26488 
26489     /* .parameter set NAME VALUE
26490     ** Set or reset a bind parameter.  NAME should be the full parameter
26491     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
26492     ** VALUE can be in either SQL literal notation, or if not it will be
26493     ** understood to be a text string.
26494     */
26495     if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
26496       int rx;
26497       char *zSql;
26498       sqlite3_stmt *pStmt;
26499       const char *zKey = azArg[2];
26500       const char *zValue = azArg[3];
26501       bind_table_init(p);
26502       zSql = sqlite3_mprintf(
26503                   "REPLACE INTO temp.sqlite_parameters(key,value)"
26504                   "VALUES(%Q,%s);", zKey, zValue);
26505       shell_check_oom(zSql);
26506       pStmt = 0;
26507       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26508       sqlite3_free(zSql);
26509       if( rx!=SQLITE_OK ){
26510         sqlite3_finalize(pStmt);
26511         pStmt = 0;
26512         zSql = sqlite3_mprintf(
26513                    "REPLACE INTO temp.sqlite_parameters(key,value)"
26514                    "VALUES(%Q,%Q);", zKey, zValue);
26515         shell_check_oom(zSql);
26516         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26517         sqlite3_free(zSql);
26518         if( rx!=SQLITE_OK ){
26519           oputf("Error: %s\n", sqlite3_errmsg(p->db));
26520           sqlite3_finalize(pStmt);
26521           pStmt = 0;
26522           rc = 1;
26523         }
26524       }
26525       sqlite3_step(pStmt);
26526       sqlite3_finalize(pStmt);
26527     }else
26528 
26529     /* .parameter unset NAME
26530     ** Remove the NAME binding from the parameter binding table, if it
26531     ** exists.
26532     */
26533     if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
26534       char *zSql = sqlite3_mprintf(
26535           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
26536       shell_check_oom(zSql);
26537       sqlite3_exec(p->db, zSql, 0, 0, 0);
26538       sqlite3_free(zSql);
26539     }else
26540     /* If no command name matches, show a syntax error */
26541     parameter_syntax_error:
26542     showHelp(p->out, "parameter");
26543   }else
26544 
26545   if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
26546     int i;
26547     for(i=1; i<nArg; i++){
26548       if( i>1 ) oputz(" ");
26549       oputz(azArg[i]);
26550     }
26551     oputz("\n");
26552   }else
26553 
26554 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
26555   if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
26556     int i;
26557     int nn = 0;
26558     p->flgProgress = 0;
26559     p->mxProgress = 0;
26560     p->nProgress = 0;
26561     for(i=1; i<nArg; i++){
26562       const char *z = azArg[i];
26563       if( z[0]=='-' ){
26564         z++;
26565         if( z[0]=='-' ) z++;
26566         if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
26567           p->flgProgress |= SHELL_PROGRESS_QUIET;
26568           continue;
26569         }
26570         if( cli_strcmp(z,"reset")==0 ){
26571           p->flgProgress |= SHELL_PROGRESS_RESET;
26572           continue;
26573         }
26574         if( cli_strcmp(z,"once")==0 ){
26575           p->flgProgress |= SHELL_PROGRESS_ONCE;
26576           continue;
26577         }
26578         if( cli_strcmp(z,"limit")==0 ){
26579           if( i+1>=nArg ){
26580             eputz("Error: missing argument on --limit\n");
26581             rc = 1;
26582             goto meta_command_exit;
26583           }else{
26584             p->mxProgress = (int)integerValue(azArg[++i]);
26585           }
26586           continue;
26587         }
26588         eputf("Error: unknown option: \"%s\"\n", azArg[i]);
26589         rc = 1;
26590         goto meta_command_exit;
26591       }else{
26592         nn = (int)integerValue(z);
26593       }
26594     }
26595     open_db(p, 0);
26596     sqlite3_progress_handler(p->db, nn, progress_handler, p);
26597   }else
26598 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
26599 
26600   if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
26601     if( nArg >= 2) {
26602       shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
26603     }
26604     if( nArg >= 3) {
26605       shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
26606     }
26607   }else
26608 
26609 #ifndef SQLITE_SHELL_FIDDLE
26610   if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
26611     rc = 2;
26612   }else
26613 #endif
26614 
26615 #ifndef SQLITE_SHELL_FIDDLE
26616   if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
26617     FILE *inSaved = p->in;
26618     int savedLineno = p->lineno;
26619     failIfSafeMode(p, "cannot run .read in safe mode");
26620     if( nArg!=2 ){
26621       eputz("Usage: .read FILE\n");
26622       rc = 1;
26623       goto meta_command_exit;
26624     }
26625     if( azArg[1][0]=='|' ){
26626 #ifdef SQLITE_OMIT_POPEN
26627       eputz("Error: pipes are not supported in this OS\n");
26628       rc = 1;
26629       p->out = stdout;
26630 #else
26631       p->in = popen(azArg[1]+1, "r");
26632       if( p->in==0 ){
26633         eputf("Error: cannot open \"%s\"\n", azArg[1]);
26634         rc = 1;
26635       }else{
26636         rc = process_input(p);
26637         pclose(p->in);
26638       }
26639 #endif
26640     }else if( (p->in = openChrSource(azArg[1]))==0 ){
26641       eputf("Error: cannot open \"%s\"\n", azArg[1]);
26642       rc = 1;
26643     }else{
26644       rc = process_input(p);
26645       fclose(p->in);
26646     }
26647     p->in = inSaved;
26648     p->lineno = savedLineno;
26649   }else
26650 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26651 
26652 #ifndef SQLITE_SHELL_FIDDLE
26653   if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
26654     const char *zSrcFile;
26655     const char *zDb;
26656     sqlite3 *pSrc;
26657     sqlite3_backup *pBackup;
26658     int nTimeout = 0;
26659 
26660     failIfSafeMode(p, "cannot run .restore in safe mode");
26661     if( nArg==2 ){
26662       zSrcFile = azArg[1];
26663       zDb = "main";
26664     }else if( nArg==3 ){
26665       zSrcFile = azArg[2];
26666       zDb = azArg[1];
26667     }else{
26668       eputz("Usage: .restore ?DB? FILE\n");
26669       rc = 1;
26670       goto meta_command_exit;
26671     }
26672     rc = sqlite3_open(zSrcFile, &pSrc);
26673     if( rc!=SQLITE_OK ){
26674       eputf("Error: cannot open \"%s\"\n", zSrcFile);
26675       close_db(pSrc);
26676       return 1;
26677     }
26678     open_db(p, 0);
26679     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
26680     if( pBackup==0 ){
26681       eputf("Error: %s\n", sqlite3_errmsg(p->db));
26682       close_db(pSrc);
26683       return 1;
26684     }
26685     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
26686           || rc==SQLITE_BUSY  ){
26687       if( rc==SQLITE_BUSY ){
26688         if( nTimeout++ >= 3 ) break;
26689         sqlite3_sleep(100);
26690       }
26691     }
26692     sqlite3_backup_finish(pBackup);
26693     if( rc==SQLITE_DONE ){
26694       rc = 0;
26695     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
26696       eputz("Error: source database is busy\n");
26697       rc = 1;
26698     }else{
26699       eputf("Error: %s\n", sqlite3_errmsg(p->db));
26700       rc = 1;
26701     }
26702     close_db(pSrc);
26703   }else
26704 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
26705 
26706   if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
26707     if( nArg==2 ){
26708       if( cli_strcmp(azArg[1], "vm")==0 ){
26709         p->scanstatsOn = 3;
26710       }else
26711       if( cli_strcmp(azArg[1], "est")==0 ){
26712         p->scanstatsOn = 2;
26713       }else{
26714         p->scanstatsOn = (u8)booleanValue(azArg[1]);
26715       }
26716       open_db(p, 0);
26717       sqlite3_db_config(
26718           p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
26719       );
26720 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
26721       eputz("Warning: .scanstats not available in this build.\n");
26722 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
26723       if( p->scanstatsOn==3 ){
26724         eputz("Warning: \".scanstats vm\" not available in this build.\n");
26725       }
26726 #endif
26727     }else{
26728       eputz("Usage: .scanstats on|off|est\n");
26729       rc = 1;
26730     }
26731   }else
26732 
26733   if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
26734     ShellText sSelect;
26735     ShellState data;
26736     char *zErrMsg = 0;
26737     const char *zDiv = "(";
26738     const char *zName = 0;
26739     int iSchema = 0;
26740     int bDebug = 0;
26741     int bNoSystemTabs = 0;
26742     int ii;
26743 
26744     open_db(p, 0);
26745     memcpy(&data, p, sizeof(data));
26746     data.showHeader = 0;
26747     data.cMode = data.mode = MODE_Semi;
26748     initText(&sSelect);
26749     for(ii=1; ii<nArg; ii++){
26750       if( optionMatch(azArg[ii],"indent") ){
26751         data.cMode = data.mode = MODE_Pretty;
26752       }else if( optionMatch(azArg[ii],"debug") ){
26753         bDebug = 1;
26754       }else if( optionMatch(azArg[ii],"nosys") ){
26755         bNoSystemTabs = 1;
26756       }else if( azArg[ii][0]=='-' ){
26757         eputf("Unknown option: \"%s\"\n", azArg[ii]);
26758         rc = 1;
26759         goto meta_command_exit;
26760       }else if( zName==0 ){
26761         zName = azArg[ii];
26762       }else{
26763         eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
26764         rc = 1;
26765         goto meta_command_exit;
26766       }
26767     }
26768     if( zName!=0 ){
26769       int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
26770                   || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
26771                   || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
26772                   || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
26773       if( isSchema ){
26774         char *new_argv[2], *new_colv[2];
26775         new_argv[0] = sqlite3_mprintf(
26776                       "CREATE TABLE %s (\n"
26777                       "  type text,\n"
26778                       "  name text,\n"
26779                       "  tbl_name text,\n"
26780                       "  rootpage integer,\n"
26781                       "  sql text\n"
26782                       ")", zName);
26783         shell_check_oom(new_argv[0]);
26784         new_argv[1] = 0;
26785         new_colv[0] = "sql";
26786         new_colv[1] = 0;
26787         callback(&data, 1, new_argv, new_colv);
26788         sqlite3_free(new_argv[0]);
26789       }
26790     }
26791     if( zDiv ){
26792       sqlite3_stmt *pStmt = 0;
26793       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
26794                               -1, &pStmt, 0);
26795       if( rc ){
26796         eputf("Error: %s\n", sqlite3_errmsg(p->db));
26797         sqlite3_finalize(pStmt);
26798         rc = 1;
26799         goto meta_command_exit;
26800       }
26801       appendText(&sSelect, "SELECT sql FROM", 0);
26802       iSchema = 0;
26803       while( sqlite3_step(pStmt)==SQLITE_ROW ){
26804         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
26805         char zScNum[30];
26806         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
26807         appendText(&sSelect, zDiv, 0);
26808         zDiv = " UNION ALL ";
26809         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
26810         if( sqlite3_stricmp(zDb, "main")!=0 ){
26811           appendText(&sSelect, zDb, '\'');
26812         }else{
26813           appendText(&sSelect, "NULL", 0);
26814         }
26815         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
26816         appendText(&sSelect, zScNum, 0);
26817         appendText(&sSelect, " AS snum, ", 0);
26818         appendText(&sSelect, zDb, '\'');
26819         appendText(&sSelect, " AS sname FROM ", 0);
26820         appendText(&sSelect, zDb, quoteChar(zDb));
26821         appendText(&sSelect, ".sqlite_schema", 0);
26822       }
26823       sqlite3_finalize(pStmt);
26824 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
26825       if( zName ){
26826         appendText(&sSelect,
26827            " UNION ALL SELECT shell_module_schema(name),"
26828            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
26829         0);
26830       }
26831 #endif
26832       appendText(&sSelect, ") WHERE ", 0);
26833       if( zName ){
26834         char *zQarg = sqlite3_mprintf("%Q", zName);
26835         int bGlob;
26836         shell_check_oom(zQarg);
26837         bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
26838                 strchr(zName, '[') != 0;
26839         if( strchr(zName, '.') ){
26840           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
26841         }else{
26842           appendText(&sSelect, "lower(tbl_name)", 0);
26843         }
26844         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
26845         appendText(&sSelect, zQarg, 0);
26846         if( !bGlob ){
26847           appendText(&sSelect, " ESCAPE '\\' ", 0);
26848         }
26849         appendText(&sSelect, " AND ", 0);
26850         sqlite3_free(zQarg);
26851       }
26852       if( bNoSystemTabs ){
26853         appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
26854       }
26855       appendText(&sSelect, "sql IS NOT NULL"
26856                            " ORDER BY snum, rowid", 0);
26857       if( bDebug ){
26858         oputf("SQL: %s;\n", sSelect.z);
26859       }else{
26860         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
26861       }
26862       freeText(&sSelect);
26863     }
26864     if( zErrMsg ){
26865       eputf("Error: %s\n", zErrMsg);
26866       sqlite3_free(zErrMsg);
26867       rc = 1;
26868     }else if( rc != SQLITE_OK ){
26869       eputz("Error: querying schema information\n");
26870       rc = 1;
26871     }else{
26872       rc = 0;
26873     }
26874   }else
26875 
26876   if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
26877    || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
26878   ){
26879     unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
26880     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
26881   }else
26882 
26883 #if defined(SQLITE_ENABLE_SESSION)
26884   if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
26885     struct AuxDb *pAuxDb = p->pAuxDb;
26886     OpenSession *pSession = &pAuxDb->aSession[0];
26887     char **azCmd = &azArg[1];
26888     int iSes = 0;
26889     int nCmd = nArg - 1;
26890     int i;
26891     if( nArg<=1 ) goto session_syntax_error;
26892     open_db(p, 0);
26893     if( nArg>=3 ){
26894       for(iSes=0; iSes<pAuxDb->nSession; iSes++){
26895         if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
26896       }
26897       if( iSes<pAuxDb->nSession ){
26898         pSession = &pAuxDb->aSession[iSes];
26899         azCmd++;
26900         nCmd--;
26901       }else{
26902         pSession = &pAuxDb->aSession[0];
26903         iSes = 0;
26904       }
26905     }
26906 
26907     /* .session attach TABLE
26908     ** Invoke the sqlite3session_attach() interface to attach a particular
26909     ** table so that it is never filtered.
26910     */
26911     if( cli_strcmp(azCmd[0],"attach")==0 ){
26912       if( nCmd!=2 ) goto session_syntax_error;
26913       if( pSession->p==0 ){
26914         session_not_open:
26915         eputz("ERROR: No sessions are open\n");
26916       }else{
26917         rc = sqlite3session_attach(pSession->p, azCmd[1]);
26918         if( rc ){
26919           eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
26920           rc = 0;
26921         }
26922       }
26923     }else
26924 
26925     /* .session changeset FILE
26926     ** .session patchset FILE
26927     ** Write a changeset or patchset into a file.  The file is overwritten.
26928     */
26929     if( cli_strcmp(azCmd[0],"changeset")==0
26930      || cli_strcmp(azCmd[0],"patchset")==0
26931     ){
26932       FILE *out = 0;
26933       failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
26934       if( nCmd!=2 ) goto session_syntax_error;
26935       if( pSession->p==0 ) goto session_not_open;
26936       out = fopen(azCmd[1], "wb");
26937       if( out==0 ){
26938         eputf("ERROR: cannot open \"%s\" for writing\n",
26939               azCmd[1]);
26940       }else{
26941         int szChng;
26942         void *pChng;
26943         if( azCmd[0][0]=='c' ){
26944           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
26945         }else{
26946           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
26947         }
26948         if( rc ){
26949           sputf(stdout, "Error: error code %d\n", rc);
26950           rc = 0;
26951         }
26952         if( pChng
26953           && fwrite(pChng, szChng, 1, out)!=1 ){
26954           eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
26955         }
26956         sqlite3_free(pChng);
26957         fclose(out);
26958       }
26959     }else
26960 
26961     /* .session close
26962     ** Close the identified session
26963     */
26964     if( cli_strcmp(azCmd[0], "close")==0 ){
26965       if( nCmd!=1 ) goto session_syntax_error;
26966       if( pAuxDb->nSession ){
26967         session_close(pSession);
26968         pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
26969       }
26970     }else
26971 
26972     /* .session enable ?BOOLEAN?
26973     ** Query or set the enable flag
26974     */
26975     if( cli_strcmp(azCmd[0], "enable")==0 ){
26976       int ii;
26977       if( nCmd>2 ) goto session_syntax_error;
26978       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
26979       if( pAuxDb->nSession ){
26980         ii = sqlite3session_enable(pSession->p, ii);
26981         oputf("session %s enable flag = %d\n", pSession->zName, ii);
26982       }
26983     }else
26984 
26985     /* .session filter GLOB ....
26986     ** Set a list of GLOB patterns of table names to be excluded.
26987     */
26988     if( cli_strcmp(azCmd[0], "filter")==0 ){
26989       int ii, nByte;
26990       if( nCmd<2 ) goto session_syntax_error;
26991       if( pAuxDb->nSession ){
26992         for(ii=0; ii<pSession->nFilter; ii++){
26993           sqlite3_free(pSession->azFilter[ii]);
26994         }
26995         sqlite3_free(pSession->azFilter);
26996         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
26997         pSession->azFilter = sqlite3_malloc( nByte );
26998         shell_check_oom( pSession->azFilter );
26999         for(ii=1; ii<nCmd; ii++){
27000           char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
27001           shell_check_oom(x);
27002         }
27003         pSession->nFilter = ii-1;
27004       }
27005     }else
27006 
27007     /* .session indirect ?BOOLEAN?
27008     ** Query or set the indirect flag
27009     */
27010     if( cli_strcmp(azCmd[0], "indirect")==0 ){
27011       int ii;
27012       if( nCmd>2 ) goto session_syntax_error;
27013       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
27014       if( pAuxDb->nSession ){
27015         ii = sqlite3session_indirect(pSession->p, ii);
27016         oputf("session %s indirect flag = %d\n", pSession->zName, ii);
27017       }
27018     }else
27019 
27020     /* .session isempty
27021     ** Determine if the session is empty
27022     */
27023     if( cli_strcmp(azCmd[0], "isempty")==0 ){
27024       int ii;
27025       if( nCmd!=1 ) goto session_syntax_error;
27026       if( pAuxDb->nSession ){
27027         ii = sqlite3session_isempty(pSession->p);
27028         oputf("session %s isempty flag = %d\n", pSession->zName, ii);
27029       }
27030     }else
27031 
27032     /* .session list
27033     ** List all currently open sessions
27034     */
27035     if( cli_strcmp(azCmd[0],"list")==0 ){
27036       for(i=0; i<pAuxDb->nSession; i++){
27037         oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
27038       }
27039     }else
27040 
27041     /* .session open DB NAME
27042     ** Open a new session called NAME on the attached database DB.
27043     ** DB is normally "main".
27044     */
27045     if( cli_strcmp(azCmd[0],"open")==0 ){
27046       char *zName;
27047       if( nCmd!=3 ) goto session_syntax_error;
27048       zName = azCmd[2];
27049       if( zName[0]==0 ) goto session_syntax_error;
27050       for(i=0; i<pAuxDb->nSession; i++){
27051         if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
27052           eputf("Session \"%s\" already exists\n", zName);
27053           goto meta_command_exit;
27054         }
27055       }
27056       if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
27057         eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
27058         goto meta_command_exit;
27059       }
27060       pSession = &pAuxDb->aSession[pAuxDb->nSession];
27061       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
27062       if( rc ){
27063         eputf("Cannot open session: error code=%d\n", rc);
27064         rc = 0;
27065         goto meta_command_exit;
27066       }
27067       pSession->nFilter = 0;
27068       sqlite3session_table_filter(pSession->p, session_filter, pSession);
27069       pAuxDb->nSession++;
27070       pSession->zName = sqlite3_mprintf("%s", zName);
27071       shell_check_oom(pSession->zName);
27072     }else
27073     /* If no command name matches, show a syntax error */
27074     session_syntax_error:
27075     showHelp(p->out, "session");
27076   }else
27077 #endif
27078 
27079 #ifdef SQLITE_DEBUG
27080   /* Undocumented commands for internal testing.  Subject to change
27081   ** without notice. */
27082   if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
27083     if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
27084       int i, v;
27085       for(i=1; i<nArg; i++){
27086         v = booleanValue(azArg[i]);
27087         oputf("%s: %d 0x%x\n", azArg[i], v, v);
27088       }
27089     }
27090     if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
27091       int i; sqlite3_int64 v;
27092       for(i=1; i<nArg; i++){
27093         char zBuf[200];
27094         v = integerValue(azArg[i]);
27095         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
27096         oputz(zBuf);
27097       }
27098     }
27099   }else
27100 #endif
27101 
27102   if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
27103     int bIsInit = 0;         /* True to initialize the SELFTEST table */
27104     int bVerbose = 0;        /* Verbose output */
27105     int bSelftestExists;     /* True if SELFTEST already exists */
27106     int i, k;                /* Loop counters */
27107     int nTest = 0;           /* Number of tests runs */
27108     int nErr = 0;            /* Number of errors seen */
27109     ShellText str;           /* Answer for a query */
27110     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
27111 
27112     open_db(p,0);
27113     for(i=1; i<nArg; i++){
27114       const char *z = azArg[i];
27115       if( z[0]=='-' && z[1]=='-' ) z++;
27116       if( cli_strcmp(z,"-init")==0 ){
27117         bIsInit = 1;
27118       }else
27119       if( cli_strcmp(z,"-v")==0 ){
27120         bVerbose++;
27121       }else
27122       {
27123         eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27124         eputz("Should be one of: --init -v\n");
27125         rc = 1;
27126         goto meta_command_exit;
27127       }
27128     }
27129     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
27130            != SQLITE_OK ){
27131       bSelftestExists = 0;
27132     }else{
27133       bSelftestExists = 1;
27134     }
27135     if( bIsInit ){
27136       createSelftestTable(p);
27137       bSelftestExists = 1;
27138     }
27139     initText(&str);
27140     appendText(&str, "x", 0);
27141     for(k=bSelftestExists; k>=0; k--){
27142       if( k==1 ){
27143         rc = sqlite3_prepare_v2(p->db,
27144             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
27145             -1, &pStmt, 0);
27146       }else{
27147         rc = sqlite3_prepare_v2(p->db,
27148           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
27149           "      (1,'run','PRAGMA integrity_check','ok')",
27150           -1, &pStmt, 0);
27151       }
27152       if( rc ){
27153         eputz("Error querying the selftest table\n");
27154         rc = 1;
27155         sqlite3_finalize(pStmt);
27156         goto meta_command_exit;
27157       }
27158       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
27159         int tno = sqlite3_column_int(pStmt, 0);
27160         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
27161         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
27162         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
27163 
27164         if( zOp==0 ) continue;
27165         if( zSql==0 ) continue;
27166         if( zAns==0 ) continue;
27167         k = 0;
27168         if( bVerbose>0 ){
27169           sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
27170         }
27171         if( cli_strcmp(zOp,"memo")==0 ){
27172           oputf("%s\n", zSql);
27173         }else
27174         if( cli_strcmp(zOp,"run")==0 ){
27175           char *zErrMsg = 0;
27176           str.n = 0;
27177           str.z[0] = 0;
27178           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
27179           nTest++;
27180           if( bVerbose ){
27181             oputf("Result: %s\n", str.z);
27182           }
27183           if( rc || zErrMsg ){
27184             nErr++;
27185             rc = 1;
27186             oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
27187             sqlite3_free(zErrMsg);
27188           }else if( cli_strcmp(zAns,str.z)!=0 ){
27189             nErr++;
27190             rc = 1;
27191             oputf("%d: Expected: [%s]\n", tno, zAns);
27192             oputf("%d:      Got: [%s]\n", tno, str.z);
27193           }
27194         }
27195         else{
27196           eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
27197           rc = 1;
27198           break;
27199         }
27200       } /* End loop over rows of content from SELFTEST */
27201       sqlite3_finalize(pStmt);
27202     } /* End loop over k */
27203     freeText(&str);
27204     oputf("%d errors out of %d tests\n", nErr, nTest);
27205   }else
27206 
27207   if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
27208     if( nArg<2 || nArg>3 ){
27209       eputz("Usage: .separator COL ?ROW?\n");
27210       rc = 1;
27211     }
27212     if( nArg>=2 ){
27213       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
27214                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
27215     }
27216     if( nArg>=3 ){
27217       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
27218                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
27219     }
27220   }else
27221 
27222   if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
27223     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
27224     int i;                   /* Loop counter */
27225     int bSchema = 0;         /* Also hash the schema */
27226     int bSeparate = 0;       /* Hash each table separately */
27227     int iSize = 224;         /* Hash algorithm to use */
27228     int bDebug = 0;          /* Only show the query that would have run */
27229     sqlite3_stmt *pStmt;     /* For querying tables names */
27230     char *zSql;              /* SQL to be run */
27231     char *zSep;              /* Separator */
27232     ShellText sSql;          /* Complete SQL for the query to run the hash */
27233     ShellText sQuery;        /* Set of queries used to read all content */
27234     open_db(p, 0);
27235     for(i=1; i<nArg; i++){
27236       const char *z = azArg[i];
27237       if( z[0]=='-' ){
27238         z++;
27239         if( z[0]=='-' ) z++;
27240         if( cli_strcmp(z,"schema")==0 ){
27241           bSchema = 1;
27242         }else
27243         if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
27244          || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
27245         ){
27246           iSize = atoi(&z[5]);
27247         }else
27248         if( cli_strcmp(z,"debug")==0 ){
27249           bDebug = 1;
27250         }else
27251         {
27252           eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
27253           showHelp(p->out, azArg[0]);
27254           rc = 1;
27255           goto meta_command_exit;
27256         }
27257       }else if( zLike ){
27258         eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
27259         rc = 1;
27260         goto meta_command_exit;
27261       }else{
27262         zLike = z;
27263         bSeparate = 1;
27264         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
27265       }
27266     }
27267     if( bSchema ){
27268       zSql = "SELECT lower(name) as tname FROM sqlite_schema"
27269              " WHERE type='table' AND coalesce(rootpage,0)>1"
27270              " UNION ALL SELECT 'sqlite_schema'"
27271              " ORDER BY 1 collate nocase";
27272     }else{
27273       zSql = "SELECT lower(name) as tname FROM sqlite_schema"
27274              " WHERE type='table' AND coalesce(rootpage,0)>1"
27275              " AND name NOT LIKE 'sqlite_%'"
27276              " ORDER BY 1 collate nocase";
27277     }
27278     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
27279     initText(&sQuery);
27280     initText(&sSql);
27281     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
27282     zSep = "VALUES(";
27283     while( SQLITE_ROW==sqlite3_step(pStmt) ){
27284       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
27285       if( zTab==0 ) continue;
27286       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
27287       if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
27288         appendText(&sQuery,"SELECT * FROM ", 0);
27289         appendText(&sQuery,zTab,'"');
27290         appendText(&sQuery," NOT INDEXED;", 0);
27291       }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
27292         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
27293                            " ORDER BY name;", 0);
27294       }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
27295         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
27296                            " ORDER BY name;", 0);
27297       }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
27298         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
27299                            " ORDER BY tbl,idx;", 0);
27300       }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
27301         appendText(&sQuery, "SELECT * FROM ", 0);
27302         appendText(&sQuery, zTab, 0);
27303         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
27304       }
27305       appendText(&sSql, zSep, 0);
27306       appendText(&sSql, sQuery.z, '\'');
27307       sQuery.n = 0;
27308       appendText(&sSql, ",", 0);
27309       appendText(&sSql, zTab, '\'');
27310       zSep = "),(";
27311     }
27312     sqlite3_finalize(pStmt);
27313     if( bSeparate ){
27314       zSql = sqlite3_mprintf(
27315           "%s))"
27316           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
27317           "   FROM [sha3sum$query]",
27318           sSql.z, iSize);
27319     }else{
27320       zSql = sqlite3_mprintf(
27321           "%s))"
27322           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
27323           "   FROM [sha3sum$query]",
27324           sSql.z, iSize);
27325     }
27326     shell_check_oom(zSql);
27327     freeText(&sQuery);
27328     freeText(&sSql);
27329     if( bDebug ){
27330       oputf("%s\n", zSql);
27331     }else{
27332       shell_exec(p, zSql, 0);
27333     }
27334 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27335     {
27336       int lrc;
27337       char *zRevText = /* Query for reversible to-blob-to-text check */
27338         "SELECT lower(name) as tname FROM sqlite_schema\n"
27339         "WHERE type='table' AND coalesce(rootpage,0)>1\n"
27340         "AND name NOT LIKE 'sqlite_%%'%s\n"
27341         "ORDER BY 1 collate nocase";
27342       zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
27343       zRevText = sqlite3_mprintf(
27344           /* lower-case query is first run, producing upper-case query. */
27345           "with tabcols as materialized(\n"
27346           "select tname, cname\n"
27347           "from ("
27348           " select printf('\"%%w\"',ss.tname) as tname,"
27349           " printf('\"%%w\"',ti.name) as cname\n"
27350           " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
27351           "select 'SELECT total(bad_text_count) AS bad_text_count\n"
27352           "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
27353           " from (select 'SELECT COUNT(*) AS bad_text_count\n"
27354           "FROM '||tname||' WHERE '\n"
27355           "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
27356           "|| ' AND typeof('||cname||')=''text'' ',\n"
27357           "' OR ') as query, tname from tabcols group by tname)"
27358           , zRevText);
27359       shell_check_oom(zRevText);
27360       if( bDebug ) oputf("%s\n", zRevText);
27361       lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
27362       if( lrc!=SQLITE_OK ){
27363         /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
27364         ** user does cruel and unnatural things like ".limit expr_depth 0". */
27365         rc = 1;
27366       }else{
27367         if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
27368         lrc = SQLITE_ROW==sqlite3_step(pStmt);
27369         if( lrc ){
27370           const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
27371           sqlite3_stmt *pCheckStmt;
27372           lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
27373           if( bDebug ) oputf("%s\n", zGenQuery);
27374           if( lrc!=SQLITE_OK ){
27375             rc = 1;
27376           }else{
27377             if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
27378               double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
27379               if( countIrreversible>0 ){
27380                 int sz = (int)(countIrreversible + 0.5);
27381                 eputf("Digest includes %d invalidly encoded text field%s.\n",
27382                       sz, (sz>1)? "s": "");
27383               }
27384             }
27385             sqlite3_finalize(pCheckStmt);
27386           }
27387           sqlite3_finalize(pStmt);
27388         }
27389       }
27390       if( rc ) eputz(".sha3sum failed.\n");
27391       sqlite3_free(zRevText);
27392     }
27393 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
27394     sqlite3_free(zSql);
27395   }else
27396 
27397 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
27398   if( c=='s'
27399    && (cli_strncmp(azArg[0], "shell", n)==0
27400        || cli_strncmp(azArg[0],"system",n)==0)
27401   ){
27402     char *zCmd;
27403     int i, x;
27404     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
27405     if( nArg<2 ){
27406       eputz("Usage: .system COMMAND\n");
27407       rc = 1;
27408       goto meta_command_exit;
27409     }
27410     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
27411     for(i=2; i<nArg && zCmd!=0; i++){
27412       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
27413                              zCmd, azArg[i]);
27414     }
27415     consoleRestore();
27416     x = zCmd!=0 ? system(zCmd) : 1;
27417     consoleRenewSetup();
27418     sqlite3_free(zCmd);
27419     if( x ) eputf("System command returns %d\n", x);
27420   }else
27421 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
27422 
27423   if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
27424     static const char *azBool[] = { "off", "on", "trigger", "full"};
27425     const char *zOut;
27426     int i;
27427     if( nArg!=1 ){
27428       eputz("Usage: .show\n");
27429       rc = 1;
27430       goto meta_command_exit;
27431     }
27432     oputf("%12.12s: %s\n","echo",
27433           azBool[ShellHasFlag(p, SHFLG_Echo)]);
27434     oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
27435     oputf("%12.12s: %s\n","explain",
27436           p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
27437     oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
27438     if( p->mode==MODE_Column
27439      || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
27440     ){
27441       oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
27442             modeDescr[p->mode], p->cmOpts.iWrap,
27443             p->cmOpts.bWordWrap ? "on" : "off",
27444             p->cmOpts.bQuote ? "" : "no");
27445     }else{
27446       oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
27447     }
27448     oputf("%12.12s: ", "nullvalue");
27449     output_c_string(p->nullValue);
27450     oputz("\n");
27451     oputf("%12.12s: %s\n","output",
27452           strlen30(p->outfile) ? p->outfile : "stdout");
27453     oputf("%12.12s: ", "colseparator");
27454      output_c_string(p->colSeparator);
27455      oputz("\n");
27456     oputf("%12.12s: ", "rowseparator");
27457      output_c_string(p->rowSeparator);
27458      oputz("\n");
27459     switch( p->statsOn ){
27460       case 0:  zOut = "off";     break;
27461       default: zOut = "on";      break;
27462       case 2:  zOut = "stmt";    break;
27463       case 3:  zOut = "vmstep";  break;
27464     }
27465     oputf("%12.12s: %s\n","stats", zOut);
27466     oputf("%12.12s: ", "width");
27467     for (i=0;i<p->nWidth;i++) {
27468       oputf("%d ", p->colWidth[i]);
27469     }
27470     oputz("\n");
27471     oputf("%12.12s: %s\n", "filename",
27472           p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
27473   }else
27474 
27475   if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
27476     if( nArg==2 ){
27477       if( cli_strcmp(azArg[1],"stmt")==0 ){
27478         p->statsOn = 2;
27479       }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
27480         p->statsOn = 3;
27481       }else{
27482         p->statsOn = (u8)booleanValue(azArg[1]);
27483       }
27484     }else if( nArg==1 ){
27485       display_stats(p->db, p, 0);
27486     }else{
27487       eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
27488       rc = 1;
27489     }
27490   }else
27491 
27492   if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
27493    || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
27494                  || cli_strncmp(azArg[0], "indexes", n)==0) )
27495   ){
27496     sqlite3_stmt *pStmt;
27497     char **azResult;
27498     int nRow, nAlloc;
27499     int ii;
27500     ShellText s;
27501     initText(&s);
27502     open_db(p, 0);
27503     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
27504     if( rc ){
27505       sqlite3_finalize(pStmt);
27506       return shellDatabaseError(p->db);
27507     }
27508 
27509     if( nArg>2 && c=='i' ){
27510       /* It is an historical accident that the .indexes command shows an error
27511       ** when called with the wrong number of arguments whereas the .tables
27512       ** command does not. */
27513       eputz("Usage: .indexes ?LIKE-PATTERN?\n");
27514       rc = 1;
27515       sqlite3_finalize(pStmt);
27516       goto meta_command_exit;
27517     }
27518     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
27519       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
27520       if( zDbName==0 ) continue;
27521       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
27522       if( sqlite3_stricmp(zDbName, "main")==0 ){
27523         appendText(&s, "SELECT name FROM ", 0);
27524       }else{
27525         appendText(&s, "SELECT ", 0);
27526         appendText(&s, zDbName, '\'');
27527         appendText(&s, "||'.'||name FROM ", 0);
27528       }
27529       appendText(&s, zDbName, '"');
27530       appendText(&s, ".sqlite_schema ", 0);
27531       if( c=='t' ){
27532         appendText(&s," WHERE type IN ('table','view')"
27533                       "   AND name NOT LIKE 'sqlite_%'"
27534                       "   AND name LIKE ?1", 0);
27535       }else{
27536         appendText(&s," WHERE type='index'"
27537                       "   AND tbl_name LIKE ?1", 0);
27538       }
27539     }
27540     rc = sqlite3_finalize(pStmt);
27541     if( rc==SQLITE_OK ){
27542       appendText(&s, " ORDER BY 1", 0);
27543       rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
27544     }
27545     freeText(&s);
27546     if( rc ) return shellDatabaseError(p->db);
27547 
27548     /* Run the SQL statement prepared by the above block. Store the results
27549     ** as an array of nul-terminated strings in azResult[].  */
27550     nRow = nAlloc = 0;
27551     azResult = 0;
27552     if( nArg>1 ){
27553       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
27554     }else{
27555       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
27556     }
27557     while( sqlite3_step(pStmt)==SQLITE_ROW ){
27558       if( nRow>=nAlloc ){
27559         char **azNew;
27560         int n2 = nAlloc*2 + 10;
27561         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
27562         shell_check_oom(azNew);
27563         nAlloc = n2;
27564         azResult = azNew;
27565       }
27566       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
27567       shell_check_oom(azResult[nRow]);
27568       nRow++;
27569     }
27570     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
27571       rc = shellDatabaseError(p->db);
27572     }
27573 
27574     /* Pretty-print the contents of array azResult[] to the output */
27575     if( rc==0 && nRow>0 ){
27576       int len, maxlen = 0;
27577       int i, j;
27578       int nPrintCol, nPrintRow;
27579       for(i=0; i<nRow; i++){
27580         len = strlen30(azResult[i]);
27581         if( len>maxlen ) maxlen = len;
27582       }
27583       nPrintCol = 80/(maxlen+2);
27584       if( nPrintCol<1 ) nPrintCol = 1;
27585       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
27586       for(i=0; i<nPrintRow; i++){
27587         for(j=i; j<nRow; j+=nPrintRow){
27588           char *zSp = j<nPrintRow ? "" : "  ";
27589           oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
27590         }
27591         oputz("\n");
27592       }
27593     }
27594 
27595     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
27596     sqlite3_free(azResult);
27597   }else
27598 
27599 #ifndef SQLITE_SHELL_FIDDLE
27600   /* Begin redirecting output to the file "testcase-out.txt" */
27601   if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
27602     output_reset(p);
27603     p->out = output_file_open("testcase-out.txt", 0);
27604     if( p->out==0 ){
27605       eputz("Error: cannot open 'testcase-out.txt'\n");
27606     }
27607     if( nArg>=2 ){
27608       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
27609     }else{
27610       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
27611     }
27612   }else
27613 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
27614 
27615 #ifndef SQLITE_UNTESTABLE
27616   if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
27617     static const struct {
27618        const char *zCtrlName;   /* Name of a test-control option */
27619        int ctrlCode;            /* Integer code for that option */
27620        int unSafe;              /* Not valid unless --unsafe-testing */
27621        const char *zUsage;      /* Usage notes */
27622     } aCtrl[] = {
27623     {"always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
27624     {"assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
27625   /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
27626   /*{"bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
27627     {"byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
27628     {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
27629   /*{"fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
27630     {"fk_no_action",       SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN"       },
27631     {"imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
27632     {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
27633     {"json_selfcheck",     SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN"      },
27634     {"localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
27635     {"never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
27636     {"optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
27637 #ifdef YYCOVERAGE
27638     {"parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
27639 #endif
27640     {"pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
27641     {"prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
27642     {"prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
27643     {"prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
27644     {"seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
27645     {"sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
27646     {"tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
27647     {"uselongdouble",  SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
27648     };
27649     int testctrl = -1;
27650     int iCtrl = -1;
27651     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
27652     int isOk = 0;
27653     int i, n2;
27654     const char *zCmd = 0;
27655 
27656     open_db(p, 0);
27657     zCmd = nArg>=2 ? azArg[1] : "help";
27658 
27659     /* The argument can optionally begin with "-" or "--" */
27660     if( zCmd[0]=='-' && zCmd[1] ){
27661       zCmd++;
27662       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
27663     }
27664 
27665     /* --help lists all test-controls */
27666     if( cli_strcmp(zCmd,"help")==0 ){
27667       oputz("Available test-controls:\n");
27668       for(i=0; i<ArraySize(aCtrl); i++){
27669         if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
27670         oputf("  .testctrl %s %s\n",
27671               aCtrl[i].zCtrlName, aCtrl[i].zUsage);
27672       }
27673       rc = 1;
27674       goto meta_command_exit;
27675     }
27676 
27677     /* convert testctrl text option to value. allow any unique prefix
27678     ** of the option name, or a numerical value. */
27679     n2 = strlen30(zCmd);
27680     for(i=0; i<ArraySize(aCtrl); i++){
27681       if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
27682       if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
27683         if( testctrl<0 ){
27684           testctrl = aCtrl[i].ctrlCode;
27685           iCtrl = i;
27686         }else{
27687           eputf("Error: ambiguous test-control: \"%s\"\n"
27688                 "Use \".testctrl --help\" for help\n", zCmd);
27689           rc = 1;
27690           goto meta_command_exit;
27691         }
27692       }
27693     }
27694     if( testctrl<0 ){
27695       eputf("Error: unknown test-control: %s\n"
27696             "Use \".testctrl --help\" for help\n", zCmd);
27697     }else{
27698       switch(testctrl){
27699 
27700         /* sqlite3_test_control(int, db, int) */
27701         case SQLITE_TESTCTRL_OPTIMIZATIONS:
27702         case SQLITE_TESTCTRL_FK_NO_ACTION:
27703           if( nArg==3 ){
27704             unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
27705             rc2 = sqlite3_test_control(testctrl, p->db, opt);
27706             isOk = 3;
27707           }
27708           break;
27709 
27710         /* sqlite3_test_control(int) */
27711         case SQLITE_TESTCTRL_PRNG_SAVE:
27712         case SQLITE_TESTCTRL_PRNG_RESTORE:
27713         case SQLITE_TESTCTRL_BYTEORDER:
27714           if( nArg==2 ){
27715             rc2 = sqlite3_test_control(testctrl);
27716             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
27717           }
27718           break;
27719 
27720         /* sqlite3_test_control(int, uint) */
27721         case SQLITE_TESTCTRL_PENDING_BYTE:
27722           if( nArg==3 ){
27723             unsigned int opt = (unsigned int)integerValue(azArg[2]);
27724             rc2 = sqlite3_test_control(testctrl, opt);
27725             isOk = 3;
27726           }
27727           break;
27728 
27729         /* sqlite3_test_control(int, int, sqlite3*) */
27730         case SQLITE_TESTCTRL_PRNG_SEED:
27731           if( nArg==3 || nArg==4 ){
27732             int ii = (int)integerValue(azArg[2]);
27733             sqlite3 *db;
27734             if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
27735               sqlite3_randomness(sizeof(ii),&ii);
27736               sputf(stdout, "-- random seed: %d\n", ii);
27737             }
27738             if( nArg==3 ){
27739               db = 0;
27740             }else{
27741               db = p->db;
27742               /* Make sure the schema has been loaded */
27743               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
27744             }
27745             rc2 = sqlite3_test_control(testctrl, ii, db);
27746             isOk = 3;
27747           }
27748           break;
27749 
27750         /* sqlite3_test_control(int, int) */
27751         case SQLITE_TESTCTRL_ASSERT:
27752         case SQLITE_TESTCTRL_ALWAYS:
27753           if( nArg==3 ){
27754             int opt = booleanValue(azArg[2]);
27755             rc2 = sqlite3_test_control(testctrl, opt);
27756             isOk = 1;
27757           }
27758           break;
27759 
27760         /* sqlite3_test_control(int, int) */
27761         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
27762         case SQLITE_TESTCTRL_NEVER_CORRUPT:
27763           if( nArg==3 ){
27764             int opt = booleanValue(azArg[2]);
27765             rc2 = sqlite3_test_control(testctrl, opt);
27766             isOk = 3;
27767           }
27768           break;
27769 
27770         /* sqlite3_test_control(int, int) */
27771         case SQLITE_TESTCTRL_USELONGDOUBLE: {
27772           int opt = -1;
27773           if( nArg==3 ){
27774             if( cli_strcmp(azArg[2],"default")==0 ){
27775               opt = 2;
27776             }else{
27777               opt = booleanValue(azArg[2]);
27778             }
27779           }
27780           rc2 = sqlite3_test_control(testctrl, opt);
27781           isOk = 1;
27782           break;
27783         }
27784 
27785         /* sqlite3_test_control(sqlite3*) */
27786         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
27787           rc2 = sqlite3_test_control(testctrl, p->db);
27788           isOk = 3;
27789           break;
27790 
27791         case SQLITE_TESTCTRL_IMPOSTER:
27792           if( nArg==5 ){
27793             rc2 = sqlite3_test_control(testctrl, p->db,
27794                           azArg[2],
27795                           integerValue(azArg[3]),
27796                           integerValue(azArg[4]));
27797             isOk = 3;
27798           }
27799           break;
27800 
27801         case SQLITE_TESTCTRL_SEEK_COUNT: {
27802           u64 x = 0;
27803           rc2 = sqlite3_test_control(testctrl, p->db, &x);
27804           oputf("%llu\n", x);
27805           isOk = 3;
27806           break;
27807         }
27808 
27809 #ifdef YYCOVERAGE
27810         case SQLITE_TESTCTRL_PARSER_COVERAGE: {
27811           if( nArg==2 ){
27812             sqlite3_test_control(testctrl, p->out);
27813             isOk = 3;
27814           }
27815           break;
27816         }
27817 #endif
27818 #ifdef SQLITE_DEBUG
27819         case SQLITE_TESTCTRL_TUNE: {
27820           if( nArg==4 ){
27821             int id = (int)integerValue(azArg[2]);
27822             int val = (int)integerValue(azArg[3]);
27823             sqlite3_test_control(testctrl, id, &val);
27824             isOk = 3;
27825           }else if( nArg==3 ){
27826             int id = (int)integerValue(azArg[2]);
27827             sqlite3_test_control(testctrl, -id, &rc2);
27828             isOk = 1;
27829           }else if( nArg==2 ){
27830             int id = 1;
27831             while(1){
27832               int val = 0;
27833               rc2 = sqlite3_test_control(testctrl, -id, &val);
27834               if( rc2!=SQLITE_OK ) break;
27835               if( id>1 ) oputz("  ");
27836               oputf("%d: %d", id, val);
27837               id++;
27838             }
27839             if( id>1 ) oputz("\n");
27840             isOk = 3;
27841           }
27842           break;
27843         }
27844 #endif
27845         case SQLITE_TESTCTRL_SORTER_MMAP:
27846           if( nArg==3 ){
27847             int opt = (unsigned int)integerValue(azArg[2]);
27848             rc2 = sqlite3_test_control(testctrl, p->db, opt);
27849             isOk = 3;
27850           }
27851           break;
27852         case SQLITE_TESTCTRL_JSON_SELFCHECK:
27853           if( nArg==2 ){
27854             rc2 = -1;
27855             isOk = 1;
27856           }else{
27857             rc2 = booleanValue(azArg[2]);
27858             isOk = 3;
27859           }
27860           sqlite3_test_control(testctrl, &rc2);
27861           break;
27862       }
27863     }
27864     if( isOk==0 && iCtrl>=0 ){
27865       oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
27866       rc = 1;
27867     }else if( isOk==1 ){
27868       oputf("%d\n", rc2);
27869     }else if( isOk==2 ){
27870       oputf("0x%08x\n", rc2);
27871     }
27872   }else
27873 #endif /* !defined(SQLITE_UNTESTABLE) */
27874 
27875   if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
27876     open_db(p, 0);
27877     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
27878   }else
27879 
27880   if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
27881     if( nArg==2 ){
27882       enableTimer = booleanValue(azArg[1]);
27883       if( enableTimer && !HAS_TIMER ){
27884         eputz("Error: timer not available on this system.\n");
27885         enableTimer = 0;
27886       }
27887     }else{
27888       eputz("Usage: .timer on|off\n");
27889       rc = 1;
27890     }
27891   }else
27892 
27893 #ifndef SQLITE_OMIT_TRACE
27894   if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
27895     int mType = 0;
27896     int jj;
27897     open_db(p, 0);
27898     for(jj=1; jj<nArg; jj++){
27899       const char *z = azArg[jj];
27900       if( z[0]=='-' ){
27901         if( optionMatch(z, "expanded") ){
27902           p->eTraceType = SHELL_TRACE_EXPANDED;
27903         }
27904 #ifdef SQLITE_ENABLE_NORMALIZE
27905         else if( optionMatch(z, "normalized") ){
27906           p->eTraceType = SHELL_TRACE_NORMALIZED;
27907         }
27908 #endif
27909         else if( optionMatch(z, "plain") ){
27910           p->eTraceType = SHELL_TRACE_PLAIN;
27911         }
27912         else if( optionMatch(z, "profile") ){
27913           mType |= SQLITE_TRACE_PROFILE;
27914         }
27915         else if( optionMatch(z, "row") ){
27916           mType |= SQLITE_TRACE_ROW;
27917         }
27918         else if( optionMatch(z, "stmt") ){
27919           mType |= SQLITE_TRACE_STMT;
27920         }
27921         else if( optionMatch(z, "close") ){
27922           mType |= SQLITE_TRACE_CLOSE;
27923         }
27924         else {
27925           eputf("Unknown option \"%s\" on \".trace\"\n", z);
27926           rc = 1;
27927           goto meta_command_exit;
27928         }
27929       }else{
27930         output_file_close(p->traceOut);
27931         p->traceOut = output_file_open(z, 0);
27932       }
27933     }
27934     if( p->traceOut==0 ){
27935       sqlite3_trace_v2(p->db, 0, 0, 0);
27936     }else{
27937       if( mType==0 ) mType = SQLITE_TRACE_STMT;
27938       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
27939     }
27940   }else
27941 #endif /* !defined(SQLITE_OMIT_TRACE) */
27942 
27943 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
27944   if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
27945     int ii;
27946     int lenOpt;
27947     char *zOpt;
27948     if( nArg<2 ){
27949       eputz("Usage: .unmodule [--allexcept] NAME ...\n");
27950       rc = 1;
27951       goto meta_command_exit;
27952     }
27953     open_db(p, 0);
27954     zOpt = azArg[1];
27955     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
27956     lenOpt = (int)strlen(zOpt);
27957     if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
27958       assert( azArg[nArg]==0 );
27959       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
27960     }else{
27961       for(ii=1; ii<nArg; ii++){
27962         sqlite3_create_module(p->db, azArg[ii], 0, 0);
27963       }
27964     }
27965   }else
27966 #endif
27967 
27968 #if SQLITE_USER_AUTHENTICATION
27969   if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
27970     if( nArg<2 ){
27971       eputz("Usage: .user SUBCOMMAND ...\n");
27972       rc = 1;
27973       goto meta_command_exit;
27974     }
27975     open_db(p, 0);
27976     if( cli_strcmp(azArg[1],"login")==0 ){
27977       if( nArg!=4 ){
27978         eputz("Usage: .user login USER PASSWORD\n");
27979         rc = 1;
27980         goto meta_command_exit;
27981       }
27982       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
27983                                      strlen30(azArg[3]));
27984       if( rc ){
27985         eputf("Authentication failed for user %s\n", azArg[2]);
27986         rc = 1;
27987       }
27988     }else if( cli_strcmp(azArg[1],"add")==0 ){
27989       if( nArg!=5 ){
27990         eputz("Usage: .user add USER PASSWORD ISADMIN\n");
27991         rc = 1;
27992         goto meta_command_exit;
27993       }
27994       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
27995                             booleanValue(azArg[4]));
27996       if( rc ){
27997         eputf("User-Add failed: %d\n", rc);
27998         rc = 1;
27999       }
28000     }else if( cli_strcmp(azArg[1],"edit")==0 ){
28001       if( nArg!=5 ){
28002         eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
28003         rc = 1;
28004         goto meta_command_exit;
28005       }
28006       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
28007                               booleanValue(azArg[4]));
28008       if( rc ){
28009         eputf("User-Edit failed: %d\n", rc);
28010         rc = 1;
28011       }
28012     }else if( cli_strcmp(azArg[1],"delete")==0 ){
28013       if( nArg!=3 ){
28014         eputz("Usage: .user delete USER\n");
28015         rc = 1;
28016         goto meta_command_exit;
28017       }
28018       rc = sqlite3_user_delete(p->db, azArg[2]);
28019       if( rc ){
28020         eputf("User-Delete failed: %d\n", rc);
28021         rc = 1;
28022       }
28023     }else{
28024       eputz("Usage: .user login|add|edit|delete ...\n");
28025       rc = 1;
28026       goto meta_command_exit;
28027     }
28028   }else
28029 #endif /* SQLITE_USER_AUTHENTICATION */
28030 
28031   if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
28032     char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
28033     oputf("SQLite %s %s\n" /*extra-version-info*/,
28034           sqlite3_libversion(), sqlite3_sourceid());
28035 #if SQLITE_HAVE_ZLIB
28036     oputf("zlib version %s\n", zlibVersion());
28037 #endif
28038 #define CTIMEOPT_VAL_(opt) #opt
28039 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
28040 #if defined(__clang__) && defined(__clang_major__)
28041     oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
28042           CTIMEOPT_VAL(__clang_minor__) "."
28043           CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
28044 #elif defined(_MSC_VER)
28045     oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
28046 #elif defined(__GNUC__) && defined(__VERSION__)
28047     oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
28048 #endif
28049   }else
28050 
28051   if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
28052     const char *zDbName = nArg==2 ? azArg[1] : "main";
28053     sqlite3_vfs *pVfs = 0;
28054     if( p->db ){
28055       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
28056       if( pVfs ){
28057         oputf("vfs.zName      = \"%s\"\n", pVfs->zName);
28058         oputf("vfs.iVersion   = %d\n", pVfs->iVersion);
28059         oputf("vfs.szOsFile   = %d\n", pVfs->szOsFile);
28060         oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28061       }
28062     }
28063   }else
28064 
28065   if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
28066     sqlite3_vfs *pVfs;
28067     sqlite3_vfs *pCurrent = 0;
28068     if( p->db ){
28069       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
28070     }
28071     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
28072       oputf("vfs.zName      = \"%s\"%s\n", pVfs->zName,
28073             pVfs==pCurrent ? "  <--- CURRENT" : "");
28074       oputf("vfs.iVersion   = %d\n", pVfs->iVersion);
28075       oputf("vfs.szOsFile   = %d\n", pVfs->szOsFile);
28076       oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
28077       if( pVfs->pNext ){
28078         oputz("-----------------------------------\n");
28079       }
28080     }
28081   }else
28082 
28083   if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
28084     const char *zDbName = nArg==2 ? azArg[1] : "main";
28085     char *zVfsName = 0;
28086     if( p->db ){
28087       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
28088       if( zVfsName ){
28089         oputf("%s\n", zVfsName);
28090         sqlite3_free(zVfsName);
28091       }
28092     }
28093   }else
28094 
28095   if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
28096     unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
28097     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
28098   }else
28099 
28100   if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
28101     int j;
28102     assert( nArg<=ArraySize(azArg) );
28103     p->nWidth = nArg-1;
28104     p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
28105     if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
28106     if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
28107     for(j=1; j<nArg; j++){
28108       p->colWidth[j-1] = (int)integerValue(azArg[j]);
28109     }
28110   }else
28111 
28112   {
28113     eputf("Error: unknown command or invalid arguments: "
28114           " \"%s\". Enter \".help\" for help\n", azArg[0]);
28115     rc = 1;
28116   }
28117 
28118 meta_command_exit:
28119   if( p->outCount ){
28120     p->outCount--;
28121     if( p->outCount==0 ) output_reset(p);
28122   }
28123   p->bSafeMode = p->bSafeModePersist;
28124   return rc;
28125 }
28126 
28127 /* Line scan result and intermediate states (supporting scan resumption)
28128 */
28129 #ifndef CHAR_BIT
28130 # define CHAR_BIT 8
28131 #endif
28132 typedef enum {
28133   QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
28134   QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
28135   QSS_Start = 0
28136 } QuickScanState;
28137 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
28138 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
28139 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
28140 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
28141 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
28142 
28143 /*
28144 ** Scan line for classification to guide shell's handling.
28145 ** The scan is resumable for subsequent lines when prior
28146 ** return values are passed as the 2nd argument.
28147 */
quickscan(char * zLine,QuickScanState qss,SCAN_TRACKER_REFTYPE pst)28148 static QuickScanState quickscan(char *zLine, QuickScanState qss,
28149                                 SCAN_TRACKER_REFTYPE pst){
28150   char cin;
28151   char cWait = (char)qss; /* intentional narrowing loss */
28152   if( cWait==0 ){
28153   PlainScan:
28154     assert( cWait==0 );
28155     while( (cin = *zLine++)!=0 ){
28156       if( IsSpace(cin) )
28157         continue;
28158       switch (cin){
28159       case '-':
28160         if( *zLine!='-' )
28161           break;
28162         while((cin = *++zLine)!=0 )
28163           if( cin=='\n')
28164             goto PlainScan;
28165         return qss;
28166       case ';':
28167         qss |= QSS_EndingSemi;
28168         continue;
28169       case '/':
28170         if( *zLine=='*' ){
28171           ++zLine;
28172           cWait = '*';
28173           CONTINUE_PROMPT_AWAITS(pst, "/*");
28174           qss = QSS_SETV(qss, cWait);
28175           goto TermScan;
28176         }
28177         break;
28178       case '[':
28179         cin = ']';
28180         deliberate_fall_through;
28181       case '`': case '\'': case '"':
28182         cWait = cin;
28183         qss = QSS_HasDark | cWait;
28184         CONTINUE_PROMPT_AWAITC(pst, cin);
28185         goto TermScan;
28186       case '(':
28187         CONTINUE_PAREN_INCR(pst, 1);
28188         break;
28189       case ')':
28190         CONTINUE_PAREN_INCR(pst, -1);
28191         break;
28192       default:
28193         break;
28194       }
28195       qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
28196     }
28197   }else{
28198   TermScan:
28199     while( (cin = *zLine++)!=0 ){
28200       if( cin==cWait ){
28201         switch( cWait ){
28202         case '*':
28203           if( *zLine != '/' )
28204             continue;
28205           ++zLine;
28206           cWait = 0;
28207           CONTINUE_PROMPT_AWAITC(pst, 0);
28208           qss = QSS_SETV(qss, 0);
28209           goto PlainScan;
28210         case '`': case '\'': case '"':
28211           if(*zLine==cWait){
28212             /* Swallow doubled end-delimiter.*/
28213             ++zLine;
28214             continue;
28215           }
28216           deliberate_fall_through;
28217         case ']':
28218           cWait = 0;
28219           CONTINUE_PROMPT_AWAITC(pst, 0);
28220           qss = QSS_SETV(qss, 0);
28221           goto PlainScan;
28222         default: assert(0);
28223         }
28224       }
28225     }
28226   }
28227   return qss;
28228 }
28229 
28230 /*
28231 ** Return TRUE if the line typed in is an SQL command terminator other
28232 ** than a semi-colon.  The SQL Server style "go" command is understood
28233 ** as is the Oracle "/".
28234 */
line_is_command_terminator(char * zLine)28235 static int line_is_command_terminator(char *zLine){
28236   while( IsSpace(zLine[0]) ){ zLine++; };
28237   if( zLine[0]=='/' )
28238     zLine += 1; /* Oracle */
28239   else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
28240     zLine += 2; /* SQL Server */
28241   else
28242     return 0;
28243   return quickscan(zLine, QSS_Start, 0)==QSS_Start;
28244 }
28245 
28246 /*
28247 ** The CLI needs a working sqlite3_complete() to work properly.  So error
28248 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
28249 */
28250 #ifdef SQLITE_OMIT_COMPLETE
28251 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
28252 #endif
28253 
28254 /*
28255 ** Return true if zSql is a complete SQL statement.  Return false if it
28256 ** ends in the middle of a string literal or C-style comment.
28257 */
line_is_complete(char * zSql,int nSql)28258 static int line_is_complete(char *zSql, int nSql){
28259   int rc;
28260   if( zSql==0 ) return 1;
28261   zSql[nSql] = ';';
28262   zSql[nSql+1] = 0;
28263   rc = sqlite3_complete(zSql);
28264   zSql[nSql] = 0;
28265   return rc;
28266 }
28267 
28268 /*
28269 ** This function is called after processing each line of SQL in the
28270 ** runOneSqlLine() function. Its purpose is to detect scenarios where
28271 ** defensive mode should be automatically turned off. Specifically, when
28272 **
28273 **   1. The first line of input is "PRAGMA foreign_keys=OFF;",
28274 **   2. The second line of input is "BEGIN TRANSACTION;",
28275 **   3. The database is empty, and
28276 **   4. The shell is not running in --safe mode.
28277 **
28278 ** The implementation uses the ShellState.eRestoreState to maintain state:
28279 **
28280 **    0: Have not seen any SQL.
28281 **    1: Have seen "PRAGMA foreign_keys=OFF;".
28282 **    2-6: Currently running .dump transaction. If the "2" bit is set,
28283 **         disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
28284 **    7: Nothing left to do. This function becomes a no-op.
28285 */
doAutoDetectRestore(ShellState * p,const char * zSql)28286 static int doAutoDetectRestore(ShellState *p, const char *zSql){
28287   int rc = SQLITE_OK;
28288 
28289   if( p->eRestoreState<7 ){
28290     switch( p->eRestoreState ){
28291       case 0: {
28292         const char *zExpect = "PRAGMA foreign_keys=OFF;";
28293         assert( strlen(zExpect)==24 );
28294         if( p->bSafeMode==0 && memcmp(zSql, zExpect, 25)==0 ){
28295           p->eRestoreState = 1;
28296         }else{
28297           p->eRestoreState = 7;
28298         }
28299         break;
28300       };
28301 
28302       case 1: {
28303         int bIsDump = 0;
28304         const char *zExpect = "BEGIN TRANSACTION;";
28305         assert( strlen(zExpect)==18 );
28306         if( memcmp(zSql, zExpect, 19)==0 ){
28307           /* Now check if the database is empty. */
28308           const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
28309           sqlite3_stmt *pStmt = 0;
28310 
28311           bIsDump = 1;
28312           shellPrepare(p->db, &rc, zQuery, &pStmt);
28313           if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
28314             bIsDump = 0;
28315           }
28316           shellFinalize(&rc, pStmt);
28317         }
28318         if( bIsDump && rc==SQLITE_OK ){
28319           int bDefense = 0;
28320           int bDqsDdl = 0;
28321           sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
28322           sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
28323           sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
28324           sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
28325           p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
28326         }else{
28327           p->eRestoreState = 7;
28328         }
28329         break;
28330       }
28331 
28332       default: {
28333         if( sqlite3_get_autocommit(p->db) ){
28334           if( (p->eRestoreState & 2) ){
28335             sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
28336           }
28337           if( (p->eRestoreState & 4) ){
28338             sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
28339           }
28340           p->eRestoreState = 7;
28341         }
28342         break;
28343       }
28344     }
28345   }
28346 
28347   return rc;
28348 }
28349 
28350 /*
28351 ** Run a single line of SQL.  Return the number of errors.
28352 */
runOneSqlLine(ShellState * p,char * zSql,FILE * in,int startline)28353 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
28354   int rc;
28355   char *zErrMsg = 0;
28356 
28357   open_db(p, 0);
28358   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
28359   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
28360   BEGIN_TIMER;
28361   rc = shell_exec(p, zSql, &zErrMsg);
28362   END_TIMER;
28363   if( rc || zErrMsg ){
28364     char zPrefix[100];
28365     const char *zErrorTail;
28366     const char *zErrorType;
28367     if( zErrMsg==0 ){
28368       zErrorType = "Error";
28369       zErrorTail = sqlite3_errmsg(p->db);
28370     }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
28371       zErrorType = "Parse error";
28372       zErrorTail = &zErrMsg[12];
28373     }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
28374       zErrorType = "Runtime error";
28375       zErrorTail = &zErrMsg[10];
28376     }else{
28377       zErrorType = "Error";
28378       zErrorTail = zErrMsg;
28379     }
28380     if( in!=0 || !stdin_is_interactive ){
28381       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
28382                        "%s near line %d:", zErrorType, startline);
28383     }else{
28384       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
28385     }
28386     eputf("%s %s\n", zPrefix, zErrorTail);
28387     sqlite3_free(zErrMsg);
28388     zErrMsg = 0;
28389     return 1;
28390   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
28391     char zLineBuf[2000];
28392     sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
28393             "changes: %lld   total_changes: %lld",
28394             sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
28395     oputf("%s\n", zLineBuf);
28396   }
28397 
28398   if( doAutoDetectRestore(p, zSql) ) return 1;
28399   return 0;
28400 }
28401 
echo_group_input(ShellState * p,const char * zDo)28402 static void echo_group_input(ShellState *p, const char *zDo){
28403   if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
28404 }
28405 
28406 #ifdef SQLITE_SHELL_FIDDLE
28407 /*
28408 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
28409 ** impl because we need the global shellState and cannot access it from that
28410 ** function without moving lots of code around (creating a larger/messier diff).
28411 */
one_input_line(FILE * in,char * zPrior,int isContinuation)28412 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
28413   /* Parse the next line from shellState.wasm.zInput. */
28414   const char *zBegin = shellState.wasm.zPos;
28415   const char *z = zBegin;
28416   char *zLine = 0;
28417   i64 nZ = 0;
28418 
28419   UNUSED_PARAMETER(in);
28420   UNUSED_PARAMETER(isContinuation);
28421   if(!z || !*z){
28422     return 0;
28423   }
28424   while(*z && isspace(*z)) ++z;
28425   zBegin = z;
28426   for(; *z && '\n'!=*z; ++nZ, ++z){}
28427   if(nZ>0 && '\r'==zBegin[nZ-1]){
28428     --nZ;
28429   }
28430   shellState.wasm.zPos = z;
28431   zLine = realloc(zPrior, nZ+1);
28432   shell_check_oom(zLine);
28433   memcpy(zLine, zBegin, nZ);
28434   zLine[nZ] = 0;
28435   return zLine;
28436 }
28437 #endif /* SQLITE_SHELL_FIDDLE */
28438 
28439 /*
28440 ** Read input from *in and process it.  If *in==0 then input
28441 ** is interactive - the user is typing it it.  Otherwise, input
28442 ** is coming from a file or device.  A prompt is issued and history
28443 ** is saved only if input is interactive.  An interrupt signal will
28444 ** cause this routine to exit immediately, unless input is interactive.
28445 **
28446 ** Return the number of errors.
28447 */
process_input(ShellState * p)28448 static int process_input(ShellState *p){
28449   char *zLine = 0;          /* A single input line */
28450   char *zSql = 0;           /* Accumulated SQL text */
28451   i64 nLine;                /* Length of current line */
28452   i64 nSql = 0;             /* Bytes of zSql[] used */
28453   i64 nAlloc = 0;           /* Allocated zSql[] space */
28454   int rc;                   /* Error code */
28455   int errCnt = 0;           /* Number of errors seen */
28456   i64 startline = 0;        /* Line number for start of current input */
28457   QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
28458 
28459   if( p->inputNesting==MAX_INPUT_NESTING ){
28460     /* This will be more informative in a later version. */
28461     eputf("Input nesting limit (%d) reached at line %d."
28462           " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
28463     return 1;
28464   }
28465   ++p->inputNesting;
28466   p->lineno = 0;
28467   CONTINUE_PROMPT_RESET;
28468   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
28469     fflush(p->out);
28470     zLine = one_input_line(p->in, zLine, nSql>0);
28471     if( zLine==0 ){
28472       /* End of input */
28473       if( p->in==0 && stdin_is_interactive ) oputz("\n");
28474       break;
28475     }
28476     if( seenInterrupt ){
28477       if( p->in!=0 ) break;
28478       seenInterrupt = 0;
28479     }
28480     p->lineno++;
28481     if( QSS_INPLAIN(qss)
28482         && line_is_command_terminator(zLine)
28483         && line_is_complete(zSql, nSql) ){
28484       memcpy(zLine,";",2);
28485     }
28486     qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
28487     if( QSS_PLAINWHITE(qss) && nSql==0 ){
28488       /* Just swallow single-line whitespace */
28489       echo_group_input(p, zLine);
28490       qss = QSS_Start;
28491       continue;
28492     }
28493     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
28494       CONTINUE_PROMPT_RESET;
28495       echo_group_input(p, zLine);
28496       if( zLine[0]=='.' ){
28497         rc = do_meta_command(zLine, p);
28498         if( rc==2 ){ /* exit requested */
28499           break;
28500         }else if( rc ){
28501           errCnt++;
28502         }
28503       }
28504       qss = QSS_Start;
28505       continue;
28506     }
28507     /* No single-line dispositions remain; accumulate line(s). */
28508     nLine = strlen(zLine);
28509     if( nSql+nLine+2>=nAlloc ){
28510       /* Grow buffer by half-again increments when big. */
28511       nAlloc = nSql+(nSql>>1)+nLine+100;
28512       zSql = realloc(zSql, nAlloc);
28513       shell_check_oom(zSql);
28514     }
28515     if( nSql==0 ){
28516       i64 i;
28517       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
28518       assert( nAlloc>0 && zSql!=0 );
28519       memcpy(zSql, zLine+i, nLine+1-i);
28520       startline = p->lineno;
28521       nSql = nLine-i;
28522     }else{
28523       zSql[nSql++] = '\n';
28524       memcpy(zSql+nSql, zLine, nLine+1);
28525       nSql += nLine;
28526     }
28527     if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
28528       echo_group_input(p, zSql);
28529       errCnt += runOneSqlLine(p, zSql, p->in, startline);
28530       CONTINUE_PROMPT_RESET;
28531       nSql = 0;
28532       if( p->outCount ){
28533         output_reset(p);
28534         p->outCount = 0;
28535       }else{
28536         clearTempFile(p);
28537       }
28538       p->bSafeMode = p->bSafeModePersist;
28539       qss = QSS_Start;
28540     }else if( nSql && QSS_PLAINWHITE(qss) ){
28541       echo_group_input(p, zSql);
28542       nSql = 0;
28543       qss = QSS_Start;
28544     }
28545   }
28546   if( nSql ){
28547     /* This may be incomplete. Let the SQL parser deal with that. */
28548     echo_group_input(p, zSql);
28549     errCnt += runOneSqlLine(p, zSql, p->in, startline);
28550     CONTINUE_PROMPT_RESET;
28551   }
28552   free(zSql);
28553   free(zLine);
28554   --p->inputNesting;
28555   return errCnt>0;
28556 }
28557 
28558 /*
28559 ** Return a pathname which is the user's home directory.  A
28560 ** 0 return indicates an error of some kind.
28561 */
find_home_dir(int clearFlag)28562 static char *find_home_dir(int clearFlag){
28563   static char *home_dir = NULL;
28564   if( clearFlag ){
28565     free(home_dir);
28566     home_dir = 0;
28567     return 0;
28568   }
28569   if( home_dir ) return home_dir;
28570 
28571 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
28572      && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
28573   {
28574     struct passwd *pwent;
28575     uid_t uid = getuid();
28576     if( (pwent=getpwuid(uid)) != NULL) {
28577       home_dir = pwent->pw_dir;
28578     }
28579   }
28580 #endif
28581 
28582 #if defined(_WIN32_WCE)
28583   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
28584    */
28585   home_dir = "/";
28586 #else
28587 
28588 #if defined(_WIN32) || defined(WIN32)
28589   if (!home_dir) {
28590     home_dir = getenv("USERPROFILE");
28591   }
28592 #endif
28593 
28594   if (!home_dir) {
28595     home_dir = getenv("HOME");
28596   }
28597 
28598 #if defined(_WIN32) || defined(WIN32)
28599   if (!home_dir) {
28600     char *zDrive, *zPath;
28601     int n;
28602     zDrive = getenv("HOMEDRIVE");
28603     zPath = getenv("HOMEPATH");
28604     if( zDrive && zPath ){
28605       n = strlen30(zDrive) + strlen30(zPath) + 1;
28606       home_dir = malloc( n );
28607       if( home_dir==0 ) return 0;
28608       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
28609       return home_dir;
28610     }
28611     home_dir = "c:\\";
28612   }
28613 #endif
28614 
28615 #endif /* !_WIN32_WCE */
28616 
28617   if( home_dir ){
28618     i64 n = strlen(home_dir) + 1;
28619     char *z = malloc( n );
28620     if( z ) memcpy(z, home_dir, n);
28621     home_dir = z;
28622   }
28623 
28624   return home_dir;
28625 }
28626 
28627 /*
28628 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
28629 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
28630 ** the path to it, else return 0. The result is cached for
28631 ** subsequent calls.
28632 */
find_xdg_config(void)28633 static const char *find_xdg_config(void){
28634 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
28635      || defined(__RTP__) || defined(_WRS_KERNEL)
28636   return 0;
28637 #else
28638   static int alreadyTried = 0;
28639   static char *zConfig = 0;
28640   const char *zXdgHome;
28641 
28642   if( alreadyTried!=0 ){
28643     return zConfig;
28644   }
28645   alreadyTried = 1;
28646   zXdgHome = getenv("XDG_CONFIG_HOME");
28647   if( zXdgHome==0 ){
28648     return 0;
28649   }
28650   zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
28651   shell_check_oom(zConfig);
28652   if( access(zConfig,0)!=0 ){
28653     sqlite3_free(zConfig);
28654     zConfig = 0;
28655   }
28656   return zConfig;
28657 #endif
28658 }
28659 
28660 /*
28661 ** Read input from the file given by sqliterc_override.  Or if that
28662 ** parameter is NULL, take input from the first of find_xdg_config()
28663 ** or ~/.sqliterc which is found.
28664 **
28665 ** Returns the number of errors.
28666 */
process_sqliterc(ShellState * p,const char * sqliterc_override)28667 static void process_sqliterc(
28668   ShellState *p,                  /* Configuration data */
28669   const char *sqliterc_override   /* Name of config file. NULL to use default */
28670 ){
28671   char *home_dir = NULL;
28672   const char *sqliterc = sqliterc_override;
28673   char *zBuf = 0;
28674   FILE *inSaved = p->in;
28675   int savedLineno = p->lineno;
28676 
28677   if( sqliterc == NULL ){
28678     sqliterc = find_xdg_config();
28679   }
28680   if( sqliterc == NULL ){
28681     home_dir = find_home_dir(0);
28682     if( home_dir==0 ){
28683       eputz("-- warning: cannot find home directory;"
28684             " cannot read ~/.sqliterc\n");
28685       return;
28686     }
28687     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
28688     shell_check_oom(zBuf);
28689     sqliterc = zBuf;
28690   }
28691   p->in = fopen(sqliterc,"rb");
28692   if( p->in ){
28693     if( stdin_is_interactive ){
28694       eputf("-- Loading resources from %s\n", sqliterc);
28695     }
28696     if( process_input(p) && bail_on_error ) exit(1);
28697     fclose(p->in);
28698   }else if( sqliterc_override!=0 ){
28699     eputf("cannot open: \"%s\"\n", sqliterc);
28700     if( bail_on_error ) exit(1);
28701   }
28702   p->in = inSaved;
28703   p->lineno = savedLineno;
28704   sqlite3_free(zBuf);
28705 }
28706 
28707 /*
28708 ** Show available command line options
28709 */
28710 static const char zOptions[] =
28711   "   --                   treat no subsequent arguments as options\n"
28712 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
28713   "   -A ARGS...           run \".archive ARGS\" and exit\n"
28714 #endif
28715   "   -append              append the database to the end of the file\n"
28716   "   -ascii               set output mode to 'ascii'\n"
28717   "   -bail                stop after hitting an error\n"
28718   "   -batch               force batch I/O\n"
28719   "   -box                 set output mode to 'box'\n"
28720   "   -column              set output mode to 'column'\n"
28721   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
28722   "   -csv                 set output mode to 'csv'\n"
28723 #if !defined(SQLITE_OMIT_DESERIALIZE)
28724   "   -deserialize         open the database using sqlite3_deserialize()\n"
28725 #endif
28726   "   -echo                print inputs before execution\n"
28727   "   -init FILENAME       read/process named file\n"
28728   "   -[no]header          turn headers on or off\n"
28729 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
28730   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
28731 #endif
28732   "   -help                show this message\n"
28733   "   -html                set output mode to HTML\n"
28734   "   -interactive         force interactive I/O\n"
28735   "   -json                set output mode to 'json'\n"
28736   "   -line                set output mode to 'line'\n"
28737   "   -list                set output mode to 'list'\n"
28738   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
28739   "   -markdown            set output mode to 'markdown'\n"
28740 #if !defined(SQLITE_OMIT_DESERIALIZE)
28741   "   -maxsize N           maximum size for a --deserialize database\n"
28742 #endif
28743   "   -memtrace            trace all memory allocations and deallocations\n"
28744   "   -mmap N              default mmap size set to N\n"
28745 #ifdef SQLITE_ENABLE_MULTIPLEX
28746   "   -multiplex           enable the multiplexor VFS\n"
28747 #endif
28748   "   -newline SEP         set output row separator. Default: '\\n'\n"
28749   "   -nofollow            refuse to open symbolic links to database files\n"
28750   "   -nonce STRING        set the safe-mode escape nonce\n"
28751   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
28752   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
28753   "   -pcachetrace         trace all page cache operations\n"
28754   "   -quote               set output mode to 'quote'\n"
28755   "   -readonly            open the database read-only\n"
28756   "   -safe                enable safe-mode\n"
28757   "   -separator SEP       set output column separator. Default: '|'\n"
28758 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
28759   "   -sorterref SIZE      sorter references threshold size\n"
28760 #endif
28761   "   -stats               print memory stats before each finalize\n"
28762   "   -table               set output mode to 'table'\n"
28763   "   -tabs                set output mode to 'tabs'\n"
28764   "   -unsafe-testing      allow unsafe commands and modes for testing\n"
28765   "   -version             show SQLite version\n"
28766   "   -vfs NAME            use NAME as the default VFS\n"
28767 #ifdef SQLITE_ENABLE_VFSTRACE
28768   "   -vfstrace            enable tracing of all VFS calls\n"
28769 #endif
28770 #ifdef SQLITE_HAVE_ZLIB
28771   "   -zip                 open the file as a ZIP Archive\n"
28772 #endif
28773 ;
usage(int showDetail)28774 static void usage(int showDetail){
28775   eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
28776        "FILENAME is the name of an SQLite database. A new database is created\n"
28777        "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
28778   if( showDetail ){
28779     eputf("OPTIONS include:\n%s", zOptions);
28780   }else{
28781     eputz("Use the -help option for additional information\n");
28782   }
28783   exit(1);
28784 }
28785 
28786 /*
28787 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
28788 ** error message if it is initialized.
28789 */
verify_uninitialized(void)28790 static void verify_uninitialized(void){
28791   if( sqlite3_config(-1)==SQLITE_MISUSE ){
28792     sputz(stdout, "WARNING: attempt to configure SQLite after"
28793           " initialization.\n");
28794   }
28795 }
28796 
28797 /*
28798 ** Initialize the state information in data
28799 */
main_init(ShellState * data)28800 static void main_init(ShellState *data) {
28801   memset(data, 0, sizeof(*data));
28802   data->normalMode = data->cMode = data->mode = MODE_List;
28803   data->autoExplain = 1;
28804   data->pAuxDb = &data->aAuxDb[0];
28805   memcpy(data->colSeparator,SEP_Column, 2);
28806   memcpy(data->rowSeparator,SEP_Row, 2);
28807   data->showHeader = 0;
28808   data->shellFlgs = SHFLG_Lookaside;
28809   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
28810 #if !defined(SQLITE_SHELL_FIDDLE)
28811   verify_uninitialized();
28812 #endif
28813   sqlite3_config(SQLITE_CONFIG_URI, 1);
28814   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
28815   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
28816   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
28817 }
28818 
28819 /*
28820 ** Output text to the console in a font that attracts extra attention.
28821 */
28822 #if defined(_WIN32) || defined(WIN32)
printBold(const char * zText)28823 static void printBold(const char *zText){
28824 #if !SQLITE_OS_WINRT
28825   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
28826   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
28827   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
28828   SetConsoleTextAttribute(out,
28829          FOREGROUND_RED|FOREGROUND_INTENSITY
28830   );
28831 #endif
28832   sputz(stdout, zText);
28833 #if !SQLITE_OS_WINRT
28834   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
28835 #endif
28836 }
28837 #else
printBold(const char * zText)28838 static void printBold(const char *zText){
28839   sputf(stdout, "\033[1m%s\033[0m", zText);
28840 }
28841 #endif
28842 
28843 /*
28844 ** Get the argument to an --option.  Throw an error and die if no argument
28845 ** is available.
28846 */
cmdline_option_value(int argc,char ** argv,int i)28847 static char *cmdline_option_value(int argc, char **argv, int i){
28848   if( i==argc ){
28849     eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
28850     exit(1);
28851   }
28852   return argv[i];
28853 }
28854 
sayAbnormalExit(void)28855 static void sayAbnormalExit(void){
28856   if( seenInterrupt ) eputz("Program interrupted.\n");
28857 }
28858 
28859 #ifndef SQLITE_SHELL_IS_UTF8
28860 #  if (defined(_WIN32) || defined(WIN32)) \
28861    && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
28862 #    define SQLITE_SHELL_IS_UTF8          (0)
28863 #  else
28864 #    define SQLITE_SHELL_IS_UTF8          (1)
28865 #  endif
28866 #endif
28867 
28868 #ifdef SQLITE_SHELL_FIDDLE
28869 #  define main fiddle_main
28870 #endif
28871 
28872 #if SQLITE_SHELL_IS_UTF8
main(int argc,char ** argv)28873 int SQLITE_CDECL main(int argc, char **argv){
28874 #else
28875 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
28876   char **argv;
28877 #endif
28878 #ifdef SQLITE_DEBUG
28879   sqlite3_int64 mem_main_enter = 0;
28880 #endif
28881   char *zErrMsg = 0;
28882 #ifdef SQLITE_SHELL_FIDDLE
28883 #  define data shellState
28884 #else
28885   ShellState data;
28886   StreamsAreConsole consStreams = SAC_NoConsole;
28887 #endif
28888   const char *zInitFile = 0;
28889   int i;
28890   int rc = 0;
28891   int warnInmemoryDb = 0;
28892   int readStdin = 1;
28893   int nCmd = 0;
28894   int nOptsEnd = argc;
28895   char **azCmd = 0;
28896   const char *zVfs = 0;           /* Value of -vfs command-line option */
28897 #if !SQLITE_SHELL_IS_UTF8
28898   char **argvToFree = 0;
28899   int argcToFree = 0;
28900 #endif
28901   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
28902 
28903 #ifdef SQLITE_SHELL_FIDDLE
28904   stdin_is_interactive = 0;
28905   stdout_is_console = 1;
28906   data.wasm.zDefaultDbName = "/fiddle.sqlite3";
28907 #else
28908   consStreams = consoleClassifySetup(stdin, stdout, stderr);
28909   stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
28910   stdout_is_console = (consStreams & SAC_OutConsole)!=0;
28911   atexit(consoleRestore);
28912 #endif
28913   atexit(sayAbnormalExit);
28914 #ifdef SQLITE_DEBUG
28915   mem_main_enter = sqlite3_memory_used();
28916 #endif
28917 #if !defined(_WIN32_WCE)
28918   if( getenv("SQLITE_DEBUG_BREAK") ){
28919     if( isatty(0) && isatty(2) ){
28920       eputf("attach debugger to process %d and press any key to continue.\n",
28921             GETPID());
28922       fgetc(stdin);
28923     }else{
28924 #if defined(_WIN32) || defined(WIN32)
28925 #if SQLITE_OS_WINRT
28926       __debugbreak();
28927 #else
28928       DebugBreak();
28929 #endif
28930 #elif defined(SIGTRAP)
28931       raise(SIGTRAP);
28932 #endif
28933     }
28934   }
28935 #endif
28936   /* Register a valid signal handler early, before much else is done. */
28937 #ifdef SIGINT
28938   signal(SIGINT, interrupt_handler);
28939 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
28940   if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
28941     eputz("No ^C handler.\n");
28942   }
28943 #endif
28944 
28945 #if USE_SYSTEM_SQLITE+0!=1
28946   if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
28947     eputf("SQLite header and source version mismatch\n%s\n%s\n",
28948           sqlite3_sourceid(), SQLITE_SOURCE_ID);
28949     exit(1);
28950   }
28951 #endif
28952   main_init(&data);
28953 
28954   /* On Windows, we must translate command-line arguments into UTF-8.
28955   ** The SQLite memory allocator subsystem has to be enabled in order to
28956   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
28957   ** subsequent sqlite3_config() calls will work.  So copy all results into
28958   ** memory that does not come from the SQLite memory allocator.
28959   */
28960 #if !SQLITE_SHELL_IS_UTF8
28961   sqlite3_initialize();
28962   argvToFree = malloc(sizeof(argv[0])*argc*2);
28963   shell_check_oom(argvToFree);
28964   argcToFree = argc;
28965   argv = argvToFree + argc;
28966   for(i=0; i<argc; i++){
28967     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
28968     i64 n;
28969     shell_check_oom(z);
28970     n = strlen(z);
28971     argv[i] = malloc( n+1 );
28972     shell_check_oom(argv[i]);
28973     memcpy(argv[i], z, n+1);
28974     argvToFree[i] = argv[i];
28975     sqlite3_free(z);
28976   }
28977   sqlite3_shutdown();
28978 #endif
28979 
28980   assert( argc>=1 && argv && argv[0] );
28981   Argv0 = argv[0];
28982 
28983 #ifdef SQLITE_SHELL_DBNAME_PROC
28984   {
28985     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
28986     ** of a C-function that will provide the name of the database file.  Use
28987     ** this compile-time option to embed this shell program in larger
28988     ** applications. */
28989     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
28990     SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
28991     warnInmemoryDb = 0;
28992   }
28993 #endif
28994 
28995   /* Do an initial pass through the command-line argument to locate
28996   ** the name of the database file, the name of the initialization file,
28997   ** the size of the alternative malloc heap, options affecting commands
28998   ** or SQL run from the command line, and the first command to execute.
28999   */
29000 #ifndef SQLITE_SHELL_FIDDLE
29001   verify_uninitialized();
29002 #endif
29003   for(i=1; i<argc; i++){
29004     char *z;
29005     z = argv[i];
29006     if( z[0]!='-' || i>nOptsEnd ){
29007       if( data.aAuxDb->zDbFilename==0 ){
29008         data.aAuxDb->zDbFilename = z;
29009       }else{
29010         /* Excess arguments are interpreted as SQL (or dot-commands) and
29011         ** mean that nothing is read from stdin */
29012         readStdin = 0;
29013         nCmd++;
29014         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
29015         shell_check_oom(azCmd);
29016         azCmd[nCmd-1] = z;
29017       }
29018       continue;
29019     }
29020     if( z[1]=='-' ) z++;
29021     if( cli_strcmp(z, "-")==0 ){
29022       nOptsEnd = i;
29023       continue;
29024     }else if( cli_strcmp(z,"-separator")==0
29025      || cli_strcmp(z,"-nullvalue")==0
29026      || cli_strcmp(z,"-newline")==0
29027      || cli_strcmp(z,"-cmd")==0
29028     ){
29029       (void)cmdline_option_value(argc, argv, ++i);
29030     }else if( cli_strcmp(z,"-init")==0 ){
29031       zInitFile = cmdline_option_value(argc, argv, ++i);
29032     }else if( cli_strcmp(z,"-interactive")==0 ){
29033     }else if( cli_strcmp(z,"-batch")==0 ){
29034       /* Need to check for batch mode here to so we can avoid printing
29035       ** informational messages (like from process_sqliterc) before
29036       ** we do the actual processing of arguments later in a second pass.
29037       */
29038       stdin_is_interactive = 0;
29039     }else if( cli_strcmp(z,"-utf8")==0 ){
29040     }else if( cli_strcmp(z,"-no-utf8")==0 ){
29041     }else if( cli_strcmp(z,"-heap")==0 ){
29042 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
29043       const char *zSize;
29044       sqlite3_int64 szHeap;
29045 
29046       zSize = cmdline_option_value(argc, argv, ++i);
29047       szHeap = integerValue(zSize);
29048       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
29049       verify_uninitialized();
29050       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
29051 #else
29052       (void)cmdline_option_value(argc, argv, ++i);
29053 #endif
29054     }else if( cli_strcmp(z,"-pagecache")==0 ){
29055       sqlite3_int64 n, sz;
29056       sz = integerValue(cmdline_option_value(argc,argv,++i));
29057       if( sz>70000 ) sz = 70000;
29058       if( sz<0 ) sz = 0;
29059       n = integerValue(cmdline_option_value(argc,argv,++i));
29060       if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
29061         n = 0xffffffffffffLL/sz;
29062       }
29063       verify_uninitialized();
29064       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
29065                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
29066       data.shellFlgs |= SHFLG_Pagecache;
29067     }else if( cli_strcmp(z,"-lookaside")==0 ){
29068       int n, sz;
29069       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
29070       if( sz<0 ) sz = 0;
29071       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
29072       if( n<0 ) n = 0;
29073       verify_uninitialized();
29074       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
29075       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
29076     }else if( cli_strcmp(z,"-threadsafe")==0 ){
29077       int n;
29078       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
29079       verify_uninitialized();
29080       switch( n ){
29081          case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
29082          case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
29083          default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
29084       }
29085 #ifdef SQLITE_ENABLE_VFSTRACE
29086     }else if( cli_strcmp(z,"-vfstrace")==0 ){
29087       extern int vfstrace_register(
29088          const char *zTraceName,
29089          const char *zOldVfsName,
29090          int (*xOut)(const char*,void*),
29091          void *pOutArg,
29092          int makeDefault
29093       );
29094       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
29095 #endif
29096 #ifdef SQLITE_ENABLE_MULTIPLEX
29097     }else if( cli_strcmp(z,"-multiplex")==0 ){
29098       extern int sqlite3_multiplex_initialize(const char*,int);
29099       sqlite3_multiplex_initialize(0, 1);
29100 #endif
29101     }else if( cli_strcmp(z,"-mmap")==0 ){
29102       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
29103       verify_uninitialized();
29104       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
29105 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
29106     }else if( cli_strcmp(z,"-sorterref")==0 ){
29107       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
29108       verify_uninitialized();
29109       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
29110 #endif
29111     }else if( cli_strcmp(z,"-vfs")==0 ){
29112       zVfs = cmdline_option_value(argc, argv, ++i);
29113 #ifdef SQLITE_HAVE_ZLIB
29114     }else if( cli_strcmp(z,"-zip")==0 ){
29115       data.openMode = SHELL_OPEN_ZIPFILE;
29116 #endif
29117     }else if( cli_strcmp(z,"-append")==0 ){
29118       data.openMode = SHELL_OPEN_APPENDVFS;
29119 #ifndef SQLITE_OMIT_DESERIALIZE
29120     }else if( cli_strcmp(z,"-deserialize")==0 ){
29121       data.openMode = SHELL_OPEN_DESERIALIZE;
29122     }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29123       data.szMax = integerValue(argv[++i]);
29124 #endif
29125     }else if( cli_strcmp(z,"-readonly")==0 ){
29126       data.openMode = SHELL_OPEN_READONLY;
29127     }else if( cli_strcmp(z,"-nofollow")==0 ){
29128       data.openFlags = SQLITE_OPEN_NOFOLLOW;
29129 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29130     }else if( cli_strncmp(z, "-A",2)==0 ){
29131       /* All remaining command-line arguments are passed to the ".archive"
29132       ** command, so ignore them */
29133       break;
29134 #endif
29135     }else if( cli_strcmp(z, "-memtrace")==0 ){
29136       sqlite3MemTraceActivate(stderr);
29137     }else if( cli_strcmp(z, "-pcachetrace")==0 ){
29138       sqlite3PcacheTraceActivate(stderr);
29139     }else if( cli_strcmp(z,"-bail")==0 ){
29140       bail_on_error = 1;
29141     }else if( cli_strcmp(z,"-nonce")==0 ){
29142       free(data.zNonce);
29143       data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
29144     }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29145       ShellSetFlag(&data,SHFLG_TestingMode);
29146     }else if( cli_strcmp(z,"-safe")==0 ){
29147       /* no-op - catch this on the second pass */
29148     }
29149   }
29150 #ifndef SQLITE_SHELL_FIDDLE
29151   verify_uninitialized();
29152 #endif
29153 
29154 
29155 #ifdef SQLITE_SHELL_INIT_PROC
29156   {
29157     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
29158     ** of a C-function that will perform initialization actions on SQLite that
29159     ** occur just before or after sqlite3_initialize(). Use this compile-time
29160     ** option to embed this shell program in larger applications. */
29161     extern void SQLITE_SHELL_INIT_PROC(void);
29162     SQLITE_SHELL_INIT_PROC();
29163   }
29164 #else
29165   /* All the sqlite3_config() calls have now been made. So it is safe
29166   ** to call sqlite3_initialize() and process any command line -vfs option. */
29167   sqlite3_initialize();
29168 #endif
29169 
29170   if( zVfs ){
29171     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
29172     if( pVfs ){
29173       sqlite3_vfs_register(pVfs, 1);
29174     }else{
29175       eputf("no such VFS: \"%s\"\n", zVfs);
29176       exit(1);
29177     }
29178   }
29179 
29180   if( data.pAuxDb->zDbFilename==0 ){
29181 #ifndef SQLITE_OMIT_MEMORYDB
29182     data.pAuxDb->zDbFilename = ":memory:";
29183     warnInmemoryDb = argc==1;
29184 #else
29185     eputf("%s: Error: no database filename specified\n", Argv0);
29186     return 1;
29187 #endif
29188   }
29189   data.out = stdout;
29190 #ifndef SQLITE_SHELL_FIDDLE
29191   sqlite3_appendvfs_init(0,0,0);
29192 #endif
29193 
29194   /* Go ahead and open the database file if it already exists.  If the
29195   ** file does not exist, delay opening it.  This prevents empty database
29196   ** files from being created if a user mistypes the database name argument
29197   ** to the sqlite command-line tool.
29198   */
29199   if( access(data.pAuxDb->zDbFilename, 0)==0 ){
29200     open_db(&data, 0);
29201   }
29202 
29203   /* Process the initialization file if there is one.  If no -init option
29204   ** is given on the command line, look for a file named ~/.sqliterc and
29205   ** try to process it.
29206   */
29207   process_sqliterc(&data,zInitFile);
29208 
29209   /* Make a second pass through the command-line argument and set
29210   ** options.  This second pass is delayed until after the initialization
29211   ** file is processed so that the command-line arguments will override
29212   ** settings in the initialization file.
29213   */
29214   for(i=1; i<argc; i++){
29215     char *z = argv[i];
29216     if( z[0]!='-' || i>=nOptsEnd ) continue;
29217     if( z[1]=='-' ){ z++; }
29218     if( cli_strcmp(z,"-init")==0 ){
29219       i++;
29220     }else if( cli_strcmp(z,"-html")==0 ){
29221       data.mode = MODE_Html;
29222     }else if( cli_strcmp(z,"-list")==0 ){
29223       data.mode = MODE_List;
29224     }else if( cli_strcmp(z,"-quote")==0 ){
29225       data.mode = MODE_Quote;
29226       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
29227       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
29228     }else if( cli_strcmp(z,"-line")==0 ){
29229       data.mode = MODE_Line;
29230     }else if( cli_strcmp(z,"-column")==0 ){
29231       data.mode = MODE_Column;
29232     }else if( cli_strcmp(z,"-json")==0 ){
29233       data.mode = MODE_Json;
29234     }else if( cli_strcmp(z,"-markdown")==0 ){
29235       data.mode = MODE_Markdown;
29236     }else if( cli_strcmp(z,"-table")==0 ){
29237       data.mode = MODE_Table;
29238     }else if( cli_strcmp(z,"-box")==0 ){
29239       data.mode = MODE_Box;
29240     }else if( cli_strcmp(z,"-csv")==0 ){
29241       data.mode = MODE_Csv;
29242       memcpy(data.colSeparator,",",2);
29243 #ifdef SQLITE_HAVE_ZLIB
29244     }else if( cli_strcmp(z,"-zip")==0 ){
29245       data.openMode = SHELL_OPEN_ZIPFILE;
29246 #endif
29247     }else if( cli_strcmp(z,"-append")==0 ){
29248       data.openMode = SHELL_OPEN_APPENDVFS;
29249 #ifndef SQLITE_OMIT_DESERIALIZE
29250     }else if( cli_strcmp(z,"-deserialize")==0 ){
29251       data.openMode = SHELL_OPEN_DESERIALIZE;
29252     }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29253       data.szMax = integerValue(argv[++i]);
29254 #endif
29255     }else if( cli_strcmp(z,"-readonly")==0 ){
29256       data.openMode = SHELL_OPEN_READONLY;
29257     }else if( cli_strcmp(z,"-nofollow")==0 ){
29258       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
29259     }else if( cli_strcmp(z,"-ascii")==0 ){
29260       data.mode = MODE_Ascii;
29261       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
29262       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
29263     }else if( cli_strcmp(z,"-tabs")==0 ){
29264       data.mode = MODE_List;
29265       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab);
29266       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row);
29267     }else if( cli_strcmp(z,"-separator")==0 ){
29268       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
29269                        "%s",cmdline_option_value(argc,argv,++i));
29270     }else if( cli_strcmp(z,"-newline")==0 ){
29271       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
29272                        "%s",cmdline_option_value(argc,argv,++i));
29273     }else if( cli_strcmp(z,"-nullvalue")==0 ){
29274       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
29275                        "%s",cmdline_option_value(argc,argv,++i));
29276     }else if( cli_strcmp(z,"-header")==0 ){
29277       data.showHeader = 1;
29278       ShellSetFlag(&data, SHFLG_HeaderSet);
29279      }else if( cli_strcmp(z,"-noheader")==0 ){
29280       data.showHeader = 0;
29281       ShellSetFlag(&data, SHFLG_HeaderSet);
29282     }else if( cli_strcmp(z,"-echo")==0 ){
29283       ShellSetFlag(&data, SHFLG_Echo);
29284     }else if( cli_strcmp(z,"-eqp")==0 ){
29285       data.autoEQP = AUTOEQP_on;
29286     }else if( cli_strcmp(z,"-eqpfull")==0 ){
29287       data.autoEQP = AUTOEQP_full;
29288     }else if( cli_strcmp(z,"-stats")==0 ){
29289       data.statsOn = 1;
29290     }else if( cli_strcmp(z,"-scanstats")==0 ){
29291       data.scanstatsOn = 1;
29292     }else if( cli_strcmp(z,"-backslash")==0 ){
29293       /* Undocumented command-line option: -backslash
29294       ** Causes C-style backslash escapes to be evaluated in SQL statements
29295       ** prior to sending the SQL into SQLite.  Useful for injecting
29296       ** crazy bytes in the middle of SQL statements for testing and debugging.
29297       */
29298       ShellSetFlag(&data, SHFLG_Backslash);
29299     }else if( cli_strcmp(z,"-bail")==0 ){
29300       /* No-op.  The bail_on_error flag should already be set. */
29301     }else if( cli_strcmp(z,"-version")==0 ){
29302       sputf(stdout, "%s %s (%d-bit)\n",
29303             sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
29304       return 0;
29305     }else if( cli_strcmp(z,"-interactive")==0 ){
29306       /* Need to check for interactive override here to so that it can
29307       ** affect console setup (for Windows only) and testing thereof.
29308       */
29309       stdin_is_interactive = 1;
29310     }else if( cli_strcmp(z,"-batch")==0 ){
29311       /* already handled */
29312     }else if( cli_strcmp(z,"-utf8")==0 ){
29313       /* already handled */
29314     }else if( cli_strcmp(z,"-no-utf8")==0 ){
29315       /* already handled */
29316     }else if( cli_strcmp(z,"-heap")==0 ){
29317       i++;
29318     }else if( cli_strcmp(z,"-pagecache")==0 ){
29319       i+=2;
29320     }else if( cli_strcmp(z,"-lookaside")==0 ){
29321       i+=2;
29322     }else if( cli_strcmp(z,"-threadsafe")==0 ){
29323       i+=2;
29324     }else if( cli_strcmp(z,"-nonce")==0 ){
29325       i += 2;
29326     }else if( cli_strcmp(z,"-mmap")==0 ){
29327       i++;
29328     }else if( cli_strcmp(z,"-memtrace")==0 ){
29329       i++;
29330     }else if( cli_strcmp(z,"-pcachetrace")==0 ){
29331       i++;
29332 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
29333     }else if( cli_strcmp(z,"-sorterref")==0 ){
29334       i++;
29335 #endif
29336     }else if( cli_strcmp(z,"-vfs")==0 ){
29337       i++;
29338 #ifdef SQLITE_ENABLE_VFSTRACE
29339     }else if( cli_strcmp(z,"-vfstrace")==0 ){
29340       i++;
29341 #endif
29342 #ifdef SQLITE_ENABLE_MULTIPLEX
29343     }else if( cli_strcmp(z,"-multiplex")==0 ){
29344       i++;
29345 #endif
29346     }else if( cli_strcmp(z,"-help")==0 ){
29347       usage(1);
29348     }else if( cli_strcmp(z,"-cmd")==0 ){
29349       /* Run commands that follow -cmd first and separately from commands
29350       ** that simply appear on the command-line.  This seems goofy.  It would
29351       ** be better if all commands ran in the order that they appear.  But
29352       ** we retain the goofy behavior for historical compatibility. */
29353       if( i==argc-1 ) break;
29354       z = cmdline_option_value(argc,argv,++i);
29355       if( z[0]=='.' ){
29356         rc = do_meta_command(z, &data);
29357         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
29358       }else{
29359         open_db(&data, 0);
29360         rc = shell_exec(&data, z, &zErrMsg);
29361         if( zErrMsg!=0 ){
29362           eputf("Error: %s\n", zErrMsg);
29363           if( bail_on_error ) return rc!=0 ? rc : 1;
29364         }else if( rc!=0 ){
29365           eputf("Error: unable to process SQL \"%s\"\n", z);
29366           if( bail_on_error ) return rc;
29367         }
29368       }
29369 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29370     }else if( cli_strncmp(z, "-A", 2)==0 ){
29371       if( nCmd>0 ){
29372         eputf("Error: cannot mix regular SQL or dot-commands"
29373               " with \"%s\"\n", z);
29374         return 1;
29375       }
29376       open_db(&data, OPEN_DB_ZIPFILE);
29377       if( z[2] ){
29378         argv[i] = &z[2];
29379         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
29380       }else{
29381         arDotCommand(&data, 1, argv+i, argc-i);
29382       }
29383       readStdin = 0;
29384       break;
29385 #endif
29386     }else if( cli_strcmp(z,"-safe")==0 ){
29387       data.bSafeMode = data.bSafeModePersist = 1;
29388     }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29389       /* Acted upon in first pass. */
29390     }else{
29391       eputf("%s: Error: unknown option: %s\n", Argv0, z);
29392       eputz("Use -help for a list of options.\n");
29393       return 1;
29394     }
29395     data.cMode = data.mode;
29396   }
29397 
29398   if( !readStdin ){
29399     /* Run all arguments that do not begin with '-' as if they were separate
29400     ** command-line inputs, except for the argToSkip argument which contains
29401     ** the database filename.
29402     */
29403     for(i=0; i<nCmd; i++){
29404       if( azCmd[i][0]=='.' ){
29405         rc = do_meta_command(azCmd[i], &data);
29406         if( rc ){
29407           free(azCmd);
29408           return rc==2 ? 0 : rc;
29409         }
29410       }else{
29411         open_db(&data, 0);
29412         echo_group_input(&data, azCmd[i]);
29413         rc = shell_exec(&data, azCmd[i], &zErrMsg);
29414         if( zErrMsg || rc ){
29415           if( zErrMsg!=0 ){
29416             eputf("Error: %s\n", zErrMsg);
29417           }else{
29418             eputf("Error: unable to process SQL: %s\n", azCmd[i]);
29419           }
29420           sqlite3_free(zErrMsg);
29421           free(azCmd);
29422           return rc!=0 ? rc : 1;
29423         }
29424       }
29425     }
29426   }else{
29427     /* Run commands received from standard input
29428     */
29429     if( stdin_is_interactive ){
29430       char *zHome;
29431       char *zHistory;
29432       int nHistory;
29433 #if CIO_WIN_WC_XLATE
29434 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
29435 #else
29436 # define SHELL_CIO_CHAR_SET ""
29437 #endif
29438       sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
29439             "Enter \".help\" for usage hints.\n",
29440             sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
29441       if( warnInmemoryDb ){
29442         sputz(stdout, "Connected to a ");
29443         printBold("transient in-memory database");
29444         sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
29445               " persistent database.\n");
29446       }
29447       zHistory = getenv("SQLITE_HISTORY");
29448       if( zHistory ){
29449         zHistory = strdup(zHistory);
29450       }else if( (zHome = find_home_dir(0))!=0 ){
29451         nHistory = strlen30(zHome) + 20;
29452         if( (zHistory = malloc(nHistory))!=0 ){
29453           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
29454         }
29455       }
29456       if( zHistory ){ shell_read_history(zHistory); }
29457 #if HAVE_READLINE || HAVE_EDITLINE
29458       rl_attempted_completion_function = readline_completion;
29459 #elif HAVE_LINENOISE
29460       linenoiseSetCompletionCallback(linenoise_completion);
29461 #endif
29462       data.in = 0;
29463       rc = process_input(&data);
29464       if( zHistory ){
29465         shell_stifle_history(2000);
29466         shell_write_history(zHistory);
29467         free(zHistory);
29468       }
29469     }else{
29470       data.in = stdin;
29471       rc = process_input(&data);
29472     }
29473   }
29474 #ifndef SQLITE_SHELL_FIDDLE
29475   /* In WASM mode we have to leave the db state in place so that
29476   ** client code can "push" SQL into it after this call returns. */
29477   free(azCmd);
29478   set_table_name(&data, 0);
29479   if( data.db ){
29480     session_close_all(&data, -1);
29481     close_db(data.db);
29482   }
29483   for(i=0; i<ArraySize(data.aAuxDb); i++){
29484     sqlite3_free(data.aAuxDb[i].zFreeOnClose);
29485     if( data.aAuxDb[i].db ){
29486       session_close_all(&data, i);
29487       close_db(data.aAuxDb[i].db);
29488     }
29489   }
29490   find_home_dir(1);
29491   output_reset(&data);
29492   data.doXdgOpen = 0;
29493   clearTempFile(&data);
29494 #if !SQLITE_SHELL_IS_UTF8
29495   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
29496   free(argvToFree);
29497 #endif
29498   free(data.colWidth);
29499   free(data.zNonce);
29500   /* Clear the global data structure so that valgrind will detect memory
29501   ** leaks */
29502   memset(&data, 0, sizeof(data));
29503 #ifdef SQLITE_DEBUG
29504   if( sqlite3_memory_used()>mem_main_enter ){
29505     eputf("Memory leaked: %u bytes\n",
29506           (unsigned int)(sqlite3_memory_used()-mem_main_enter));
29507   }
29508 #endif
29509 #endif /* !SQLITE_SHELL_FIDDLE */
29510   return rc;
29511 }
29512 
29513 
29514 #ifdef SQLITE_SHELL_FIDDLE
29515 /* Only for emcc experimentation purposes. */
29516 int fiddle_experiment(int a,int b){
29517   return a + b;
29518 }
29519 
29520 /*
29521 ** Returns a pointer to the current DB handle.
29522 */
29523 sqlite3 * fiddle_db_handle(){
29524   return globalDb;
29525 }
29526 
29527 /*
29528 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
29529 ** "main" is assumed. Returns 0 if no db with the given name is
29530 ** open.
29531 */
29532 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
29533   sqlite3_vfs * pVfs = 0;
29534   if(globalDb){
29535     sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
29536                          SQLITE_FCNTL_VFS_POINTER, &pVfs);
29537   }
29538   return pVfs;
29539 }
29540 
29541 /* Only for emcc experimentation purposes. */
29542 sqlite3 * fiddle_db_arg(sqlite3 *arg){
29543     printf("fiddle_db_arg(%p)\n", (const void*)arg);
29544     return arg;
29545 }
29546 
29547 /*
29548 ** Intended to be called via a SharedWorker() while a separate
29549 ** SharedWorker() (which manages the wasm module) is performing work
29550 ** which should be interrupted. Unfortunately, SharedWorker is not
29551 ** portable enough to make real use of.
29552 */
29553 void fiddle_interrupt(void){
29554   if( globalDb ) sqlite3_interrupt(globalDb);
29555 }
29556 
29557 /*
29558 ** Returns the filename of the given db name, assuming "main" if
29559 ** zDbName is NULL. Returns NULL if globalDb is not opened.
29560 */
29561 const char * fiddle_db_filename(const char * zDbName){
29562     return globalDb
29563       ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
29564       : NULL;
29565 }
29566 
29567 /*
29568 ** Completely wipes out the contents of the currently-opened database
29569 ** but leaves its storage intact for reuse.
29570 */
29571 void fiddle_reset_db(void){
29572   if( globalDb ){
29573     int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
29574     if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
29575     sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
29576   }
29577 }
29578 
29579 /*
29580 ** Uses the current database's VFS xRead to stream the db file's
29581 ** contents out to the given callback. The callback gets a single
29582 ** chunk of size n (its 2nd argument) on each call and must return 0
29583 ** on success, non-0 on error. This function returns 0 on success,
29584 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
29585 ** code from the callback. Note that this is not thread-friendly: it
29586 ** expects that it will be the only thread reading the db file and
29587 ** takes no measures to ensure that is the case.
29588 */
29589 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
29590   sqlite3_int64 nSize = 0;
29591   sqlite3_int64 nPos = 0;
29592   sqlite3_file * pFile = 0;
29593   unsigned char buf[1024 * 8];
29594   int nBuf = (int)sizeof(buf);
29595   int rc = shellState.db
29596     ? sqlite3_file_control(shellState.db, "main",
29597                            SQLITE_FCNTL_FILE_POINTER, &pFile)
29598     : SQLITE_NOTFOUND;
29599   if( rc ) return rc;
29600   rc = pFile->pMethods->xFileSize(pFile, &nSize);
29601   if( rc ) return rc;
29602   if(nSize % nBuf){
29603     /* DB size is not an even multiple of the buffer size. Reduce
29604     ** buffer size so that we do not unduly inflate the db size when
29605     ** exporting. */
29606     if(0 == nSize % 4096) nBuf = 4096;
29607     else if(0 == nSize % 2048) nBuf = 2048;
29608     else if(0 == nSize % 1024) nBuf = 1024;
29609     else nBuf = 512;
29610   }
29611   for( ; 0==rc && nPos<nSize; nPos += nBuf ){
29612     rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
29613     if(SQLITE_IOERR_SHORT_READ == rc){
29614       rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
29615     }
29616     if( 0==rc ) rc = xCallback(buf, nBuf);
29617   }
29618   return rc;
29619 }
29620 
29621 /*
29622 ** Trivial exportable function for emscripten. It processes zSql as if
29623 ** it were input to the sqlite3 shell and redirects all output to the
29624 ** wasm binding. fiddle_main() must have been called before this
29625 ** is called, or results are undefined.
29626 */
29627 void fiddle_exec(const char * zSql){
29628   if(zSql && *zSql){
29629     if('.'==*zSql) puts(zSql);
29630     shellState.wasm.zInput = zSql;
29631     shellState.wasm.zPos = zSql;
29632     process_input(&shellState);
29633     shellState.wasm.zInput = shellState.wasm.zPos = 0;
29634   }
29635 }
29636 #endif /* SQLITE_SHELL_FIDDLE */
29637