/ Check-in [a658f80c]
Login

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

Overview
Comment:Merge the latest enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA3-256: a658f80c8f3babb6f992f3c5a5b2f87d995239fb24a00f258bd615d39bfda0fa
User & Date: drh 2018-03-14 17:17:43
Context
2018-04-10
18:05
Merge all version 3.23.1 changes and enhancements from trunk. check-in: e20fcb51 user: drh tags: apple-osx
2018-03-14
17:17
Merge the latest enhancements from trunk. check-in: a658f80c user: drh tags: apple-osx
15:25
Add the SQLITE_DBSTATUS_CACHE_SPILL option to sqlite3_db_status(). check-in: 48a06eb0 user: drh tags: trunk
2018-01-22
19:04
Merge all version-3.22.0 changes. check-in: 27e20d69 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   176    176            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   177    177            fts3_tokenize_vtab.lo \
   178    178            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   179    179   	 fts5.lo \
   180    180            func.lo global.lo hash.lo \
   181    181            icu.lo insert.lo json1.lo legacy.lo loadext.lo \
   182    182            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   183         -         memjournal.lo \
          183  +         memdb.lo memjournal.lo \
   184    184            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   185    185            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   186    186            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   187    187            random.lo resolve.lo rowset.lo rtree.lo \
   188    188            sqlite3session.lo select.lo sqlite3rbu.lo status.lo stmt.lo \
   189    189            table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   190    190            update.lo util.lo vacuum.lo \
................................................................................
   236    236     $(TOP)/src/main.c \
   237    237     $(TOP)/src/malloc.c \
   238    238     $(TOP)/src/mem0.c \
   239    239     $(TOP)/src/mem1.c \
   240    240     $(TOP)/src/mem2.c \
   241    241     $(TOP)/src/mem3.c \
   242    242     $(TOP)/src/mem5.c \
          243  +  $(TOP)/src/memdb.c \
   243    244     $(TOP)/src/memjournal.c \
   244    245     $(TOP)/src/msvc.h \
   245    246     $(TOP)/src/mutex.c \
   246    247     $(TOP)/src/mutex.h \
   247    248     $(TOP)/src/mutex_noop.c \
   248    249     $(TOP)/src/mutex_unix.c \
   249    250     $(TOP)/src/mutex_w32.c \
................................................................................
   439    440     $(TOP)/ext/misc/fuzzer.c \
   440    441     $(TOP)/ext/fts5/fts5_tcl.c \
   441    442     $(TOP)/ext/fts5/fts5_test_mi.c \
   442    443     $(TOP)/ext/fts5/fts5_test_tok.c \
   443    444     $(TOP)/ext/misc/ieee754.c \
   444    445     $(TOP)/ext/misc/mmapwarm.c \
   445    446     $(TOP)/ext/misc/nextchar.c \
          447  +  $(TOP)/ext/misc/normalize.c \
   446    448     $(TOP)/ext/misc/percentile.c \
   447    449     $(TOP)/ext/misc/regexp.c \
   448    450     $(TOP)/ext/misc/remember.c \
   449    451     $(TOP)/ext/misc/series.c \
   450    452     $(TOP)/ext/misc/spellfix.c \
   451    453     $(TOP)/ext/misc/totype.c \
   452    454     $(TOP)/ext/misc/unionvtab.c \
................................................................................
   600    602   SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
   601    603   SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   602    604   SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC
   603    605   SHELL_OPT += -DSQLITE_INTROSPECTION_PRAGMAS
   604    606   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   605    607   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
   606    608   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
          609  +FUZZCHECK_OPT += -DSQLITE_PRINTF_PRECISION_LIMIT=1000
   607    610   FUZZCHECK_SRC = $(TOP)/test/fuzzcheck.c $(TOP)/test/ossfuzz.c
   608    611   DBFUZZ_OPT = 
   609    612   
   610    613   # This is the default Makefile target.  The objects listed here
   611    614   # are what get build when you type just "make" with no arguments.
   612    615   #
   613    616   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
................................................................................
   655    658   
   656    659   fuzzcheck$(TEXE):	$(FUZZCHECK_SRC) sqlite3.c sqlite3.h
   657    660   	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) sqlite3.c $(TLIBS)
   658    661   
   659    662   ossshell$(TEXE):	$(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c sqlite3.h
   660    663   	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c \
   661    664                $(TOP)/test/ossfuzz.c sqlite3.c $(TLIBS)
          665  +
          666  +sessionfuzz$(TEXE):	$(TOP)/test/sessionfuzz.c sqlite3.c sqlite3.h
          667  +	$(CC) $(CFLAGS) -I. -o $@ $(TOP)/test/sessionfuzz.c $(TLIBS)
   662    668   
   663    669   dbfuzz$(TEXE):	$(TOP)/test/dbfuzz.c sqlite3.c sqlite3.h
   664    670   	$(LTLINK) -o $@ $(DBFUZZ_OPT) $(TOP)/test/dbfuzz.c sqlite3.c $(TLIBS)
   665    671   
   666    672   mptester$(TEXE):	sqlite3.lo $(TOP)/mptest/mptest.c
   667    673   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.lo \
   668    674   		$(TLIBS) -rpath "$(libdir)"
................................................................................
   836    842   
   837    843   mem3.lo:	$(TOP)/src/mem3.c $(HDR)
   838    844   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem3.c
   839    845   
   840    846   mem5.lo:	$(TOP)/src/mem5.c $(HDR)
   841    847   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem5.c
   842    848   
          849  +memdb.lo:	$(TOP)/src/memdb.c $(HDR)
          850  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/memdb.c
          851  +
   843    852   memjournal.lo:	$(TOP)/src/memjournal.c $(HDR)
   844    853   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/memjournal.c
   845    854   
   846    855   mutex.lo:	$(TOP)/src/mutex.c $(HDR)
   847    856   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex.c
   848    857   
   849    858   mutex_noop.lo:	$(TOP)/src/mutex_noop.c $(HDR)
................................................................................
  1169   1178   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS)
  1170   1179   
  1171   1180   # Do extra testing but not everything.
  1172   1181   fulltestonly:	$(TESTPROGS) fuzztest
  1173   1182   	./testfixture$(TEXE) $(TOP)/test/full.test
  1174   1183   
  1175   1184   # Fuzz testing
  1176         -fuzztest:	fuzzcheck$(TEXE) $(FUZZDATA)
         1185  +fuzztest:	fuzzcheck$(TEXE) $(FUZZDATA) sessionfuzz$(TEXE) $(TOP)/test/sessionfuzz-data1.db
  1177   1186   	./fuzzcheck$(TEXE) $(FUZZDATA)
         1187  +	./sessionfuzz$(TEXE) run $(TOP)/test/sessionfuzz-data1.db
  1178   1188   
  1179         -fastfuzztest:	fuzzcheck$(TEXE) $(FUZZDATA)
         1189  +fastfuzztest:	fuzzcheck$(TEXE) $(FUZZDATA) sessionfuzz$(TEXE) $(TOP)/test/sessionfuzz-data1.db
  1180   1190   	./fuzzcheck$(TEXE) --limit-mem 100M $(FUZZDATA)
         1191  +	./sessionfuzz$(TEXE) run $(TOP)/test/sessionfuzz-data1.db
  1181   1192   
  1182         -valgrindfuzz:	fuzzcheck$(TEXT) $(FUZZDATA)
         1193  +valgrindfuzz:	fuzzcheck$(TEXT) $(FUZZDATA) sessionfuzz$(TEXE) $(TOP)/test/sessionfuzz-data1.db
  1183   1194   	valgrind ./fuzzcheck$(TEXE) --cell-size-check --limit-mem 10M --timeout 600 $(FUZZDATA)
         1195  +	valgrind ./sessionfuzz$(TEXE) run $(TOP)/test/sessionfuzz-data1.db
  1184   1196   
  1185   1197   # The veryquick.test TCL tests.
  1186   1198   #
  1187   1199   tcltest:	./testfixture$(TEXE)
  1188   1200   	./testfixture$(TEXE) $(TOP)/test/veryquick.test $(TESTOPTS)
  1189   1201   
  1190   1202   # Minimal testing that runs in less than 3 minutes

Changes to Makefile.msc.

  1182   1182            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
  1183   1183            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
  1184   1184            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
  1185   1185            fts5.lo \
  1186   1186            func.lo global.lo hash.lo \
  1187   1187            icu.lo insert.lo legacy.lo loadext.lo \
  1188   1188            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
  1189         -         memjournal.lo \
         1189  +         memdb.lo memjournal.lo \
  1190   1190            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
  1191   1191            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
  1192   1192            pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
  1193   1193            random.lo resolve.lo rowset.lo rtree.lo \
  1194   1194            sqlite3session.lo select.lo sqlite3rbu.lo status.lo \
  1195   1195            table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
  1196   1196            update.lo util.lo vacuum.lo \
................................................................................
  1255   1255     $(TOP)\src\main.c \
  1256   1256     $(TOP)\src\malloc.c \
  1257   1257     $(TOP)\src\mem0.c \
  1258   1258     $(TOP)\src\mem1.c \
  1259   1259     $(TOP)\src\mem2.c \
  1260   1260     $(TOP)\src\mem3.c \
  1261   1261     $(TOP)\src\mem5.c \
         1262  +  $(TOP)\src\memdb.c \
  1262   1263     $(TOP)\src\memjournal.c \
  1263   1264     $(TOP)\src\mutex.c \
  1264   1265     $(TOP)\src\mutex_noop.c \
  1265   1266     $(TOP)\src\mutex_unix.c \
  1266   1267     $(TOP)\src\mutex_w32.c \
  1267   1268     $(TOP)\src\notify.c \
  1268   1269     $(TOP)\src\os.c \
................................................................................
  1496   1497     $(TOP)\ext\misc\fuzzer.c \
  1497   1498     $(TOP)\ext\fts5\fts5_tcl.c \
  1498   1499     $(TOP)\ext\fts5\fts5_test_mi.c \
  1499   1500     $(TOP)\ext\fts5\fts5_test_tok.c \
  1500   1501     $(TOP)\ext\misc\ieee754.c \
  1501   1502     $(TOP)\ext\misc\mmapwarm.c \
  1502   1503     $(TOP)\ext\misc\nextchar.c \
         1504  +  $(TOP)\ext\misc\normalize.c \
  1503   1505     $(TOP)\ext\misc\percentile.c \
  1504   1506     $(TOP)\ext\misc\regexp.c \
  1505   1507     $(TOP)\ext\misc\remember.c \
  1506   1508     $(TOP)\ext\misc\series.c \
  1507   1509     $(TOP)\ext\misc\spellfix.c \
  1508   1510     $(TOP)\ext\misc\totype.c \
  1509   1511     $(TOP)\ext\misc\unionvtab.c \
................................................................................
  1611   1613   !ENDIF
  1612   1614   
  1613   1615   # <<mark>>
  1614   1616   # Extra compiler options for various test tools.
  1615   1617   #
  1616   1618   MPTESTER_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5
  1617   1619   FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1
  1618         -FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ -DSQLITE_MAX_MEMORY=50000000
         1620  +FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ -DSQLITE_MAX_MEMORY=50000000 -DSQLITE_PRINTF_PRECISION_LIMIT=1000
  1619   1621   FUZZCHECK_SRC = $(TOP)\test\fuzzcheck.c $(TOP)\test\ossfuzz.c
  1620   1622   OSSSHELL_SRC = $(TOP)\test\ossshell.c $(TOP)\test\ossfuzz.c
  1621   1623   DBFUZZ_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION
  1622   1624   KV_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
  1623         -DBSELFTEST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
  1624   1625   ST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0
  1625   1626   
  1626   1627   # Standard options to testfixture.
  1627   1628   #
  1628   1629   TESTOPTS = --verbose=file --output=test-out.txt
  1629   1630   
  1630   1631   # Extra targets for the "all" target that require Tcl.
................................................................................
  1684   1685   sqldiff.exe:	$(TOP)\tool\sqldiff.c $(SQLITE3C) $(SQLITE3H)
  1685   1686   	$(LTLINK) $(NO_WARN) $(TOP)\tool\sqldiff.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1686   1687   
  1687   1688   dbhash.exe:	$(TOP)\tool\dbhash.c $(SQLITE3C) $(SQLITE3H)
  1688   1689   	$(LTLINK) $(NO_WARN) $(TOP)\tool\dbhash.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1689   1690   
  1690   1691   scrub.exe:	$(TOP)\ext\misc\scrub.c $(SQLITE3C) $(SQLITE3H)
  1691         -	$(LTLINK) $(NO_WARN) $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         1692  +	$(LTLINK) $(NO_WARN) -DSCRUB_STANDALONE=1 $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1692   1693   
  1693   1694   srcck1.exe:	$(TOP)\tool\srcck1.c
  1694   1695   	$(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\srcck1.c
  1695   1696   
  1696   1697   sourcetest:	srcck1.exe sqlite3.c
  1697   1698   	srcck1.exe sqlite3.c
  1698   1699   
................................................................................
  1704   1705   
  1705   1706   fuzzcheck.exe:	$(FUZZCHECK_SRC) $(SQLITE3C) $(SQLITE3H)
  1706   1707   	$(LTLINK) $(NO_WARN) $(FUZZCHECK_COMPILE_OPTS) $(FUZZCHECK_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1707   1708   
  1708   1709   ossshell.exe:	$(OSSSHELL_SRC) $(SQLITE3C) $(SQLITE3H)
  1709   1710   	$(LTLINK) $(NO_WARN) $(FUZZCHECK_COMPILE_OPTS) $(OSSSHELL_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1710   1711   
         1712  +sessionfuzz.exe:	zlib $(TOP)\test\sessionfuzz.c $(SQLITE3C) $(SQLITE3H)
         1713  +	$(LTLINK) $(NO_WARN) -I$(ZLIBINCDIR) $(TOP)\test\sessionfuzz.c /link $(LDFLAGS) $(LTLINKOPTS) /LIBPATH:$(ZLIBLIBDIR) $(ZLIBLIB)
         1714  +
  1711   1715   mptester.exe:	$(TOP)\mptest\mptest.c $(SQLITE3C) $(SQLITE3H)
  1712   1716   	$(LTLINK) $(NO_WARN) $(MPTESTER_COMPILE_OPTS) $(TOP)\mptest\mptest.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1713   1717   
  1714   1718   MPTEST1 = mptester mptest.db $(TOP)\mptest\crash01.test --repeat 20
  1715   1719   MPTEST2 = mptester mptest.db $(TOP)\mptest\multiwrite01.test --repeat 20
  1716   1720   
  1717   1721   mptest:	mptester.exe
................................................................................
  1905   1909   
  1906   1910   mem3.lo:	$(TOP)\src\mem3.c $(HDR)
  1907   1911   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\mem3.c
  1908   1912   
  1909   1913   mem5.lo:	$(TOP)\src\mem5.c $(HDR)
  1910   1914   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\mem5.c
  1911   1915   
         1916  +memdb.lo:	$(TOP)\src\memdb.c $(HDR)
         1917  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\memdb.c
         1918  +
  1912   1919   memjournal.lo:	$(TOP)\src\memjournal.c $(HDR)
  1913   1920   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\memjournal.c
  1914   1921   
  1915   1922   mutex.lo:	$(TOP)\src\mutex.c $(HDR)
  1916   1923   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\mutex.c
  1917   1924   
  1918   1925   mutex_noop.lo:	$(TOP)\src\mutex_noop.c $(HDR)
................................................................................
  2248   2255   TESTFIXTURE_FLAGS = -DTCLSH_INIT_PROC=sqlite3TestInit -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
  2249   2256   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE=""
  2250   2257   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CORE $(NO_WARN)
  2251   2258   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  2252   2259   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_DEFAULT_PAGE_SIZE=1024
  2253   2260   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB
  2254   2261   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB
         2262  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_JSON1
  2255   2263   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) $(TEST_CCONV_OPTS)
  2256   2264   
  2257   2265   TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2)
  2258   2266   TESTFIXTURE_SRC1 = $(TESTEXT) $(SQLITE3C)
  2259   2267   !IF $(USE_AMALGAMATION)==0
  2260   2268   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
  2261   2269   !ELSE
................................................................................
  2361   2369   sqlite3_checker.c:	$(CHECKER_DEPS)
  2362   2370   	$(TCLSH_CMD) $(TOP)\tool\mkccode.tcl $(TOP)\ext\repair\sqlite3_checker.c.in > $@
  2363   2371   
  2364   2372   sqlite3_checker.exe:	sqlite3_checker.c $(LIBRESOBJS)
  2365   2373   	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_checker.c \
  2366   2374   		/link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS)
  2367   2375   
  2368         -dbdump.exe:	$(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H)
         2376  +dbdump.exe:	$(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS)
  2369   2377   	$(LTLINK) $(NO_WARN) -DDBDUMP_STANDALONE $(TOP)\ext\misc\dbdump.c $(SQLITE3C) \
  2370   2378   		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS)
  2371   2379   
  2372         -testloadext.lo:	$(TOP)\src\test_loadext.c
         2380  +testloadext.lo:	$(TOP)\src\test_loadext.c $(SQLITE3H)
  2373   2381   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  2374   2382   
  2375   2383   testloadext.dll:	testloadext.lo
  2376   2384   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo
  2377   2385   
  2378   2386   showdb.exe:	$(TOP)\tool\showdb.c $(SQLITE3C) $(SQLITE3H)
  2379   2387   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
................................................................................
  2418   2426   	$(LTLINK) $(NO_WARN) $(ST_COMPILE_OPTS) -DSQLITE_OMIT_LOAD_EXTENSION \
  2419   2427   		$(TOP)\test\speedtest1.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2420   2428   
  2421   2429   kvtest.exe:	$(TOP)\test\kvtest.c $(SQLITE3C) $(SQLITE3H)
  2422   2430   	$(LTLINK) $(NO_WARN) $(KV_COMPILE_OPTS) \
  2423   2431   		$(TOP)\test\kvtest.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2424   2432   
  2425         -dbselftest.exe:	$(TOP)\test\dbselftest.c $(SQLITE3C) $(SQLITE3H)
  2426         -	$(LTLINK) $(NO_WARN) $(DBSELFTEST_COMPILE_OPTS) $(TOP)\test\dbselftest.c $(SQLITE3C)
  2427         -
  2428   2433   rbu.exe:	$(TOP)\ext\rbu\rbu.c $(TOP)\ext\rbu\sqlite3rbu.c $(SQLITE3C) $(SQLITE3H)
  2429   2434   	$(LTLINK) $(NO_WARN) -DSQLITE_ENABLE_RBU \
  2430   2435   		$(TOP)\ext\rbu\rbu.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2431   2436   
  2432   2437   LSMDIR=$(TOP)\ext\lsm1
  2433   2438   !INCLUDE $(LSMDIR)\Makefile.msc
  2434   2439   
................................................................................
  2460   2465   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2461   2466   	del /Q sqlite3.c sqlite3-*.c 2>NUL
  2462   2467   	del /Q sqlite3rc.h 2>NUL
  2463   2468   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2464   2469   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2465   2470   	del /Q sqlite-*-output.vsix 2>NUL
  2466   2471   	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2467         -	del /Q sqltclsh.exe 2>NUL
         2472  +	del /Q sqltclsh.* 2>NUL
         2473  +	del /Q dbfuzz.exe sessionfuzz.exe 2>NUL
         2474  +	del /Q kvtest.exe ossshell.exe scrub.exe 2>NUL
         2475  +	del /Q showshm.exe sqlite3_checker.* sqlite3_expert.exe 2>NUL
  2468   2476   	del /Q fts5.* fts5parse.* 2>NUL
  2469   2477   	del /Q lsm.h lsm1.c 2>NUL
  2470   2478   # <</mark>>

Changes to README.md.

    10     10   
    11     11   ## Obtaining The Code
    12     12   
    13     13   SQLite sources are managed using the
    14     14   [Fossil](https://www.fossil-scm.org/), a distributed version control system
    15     15   that was specifically designed to support SQLite development.
    16     16   If you do not want to use Fossil, you can download tarballs or ZIP
    17         -archives as follows:
           17  +archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
    18     18   
    19         -  *  Lastest trunk check-in:
    20         -     <https://www.sqlite.org/src/tarball/sqlite.tar.gz> or
    21         -     <https://www.sqlite.org/src/zip/sqlite.zip>.
           19  +  *  Lastest trunk check-in as
           20  +     [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
           21  +     [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
           22  +     [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
    22     23   
    23         -  *  Latest release:
    24         -     <https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release> or
    25         -     <https://www.sqlite.org/src/zip/sqlite.zip?r=release>.
           24  +  *  Latest release as
           25  +     [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
           26  +     [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
           27  +     [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
    26     28   
    27     29     *  For other check-ins, substitute an appropriate branch name or
    28     30        tag or hash prefix for "release" in the URLs of the previous
    29     31        bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
    30     32        to locate the check-in desired, click on its information page link,
    31     33        then click on the "Tarball" or "ZIP Archive" links on the information
    32     34        page.

Changes to VERSION.

     1         -3.22.0
            1  +3.23.0

Changes to autoconf/Makefile.am.

     6      6   libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8
     7      7   
     8      8   bin_PROGRAMS = sqlite3
     9      9   sqlite3_SOURCES = shell.c sqlite3.h
    10     10   EXTRA_sqlite3_SOURCES = sqlite3.c
    11     11   sqlite3_LDADD = @EXTRA_SHELL_OBJ@ @READLINE_LIBS@
    12     12   sqlite3_DEPENDENCIES = @EXTRA_SHELL_OBJ@
    13         -sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB
           13  +sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB $(SHELL_CFLAGS)
    14     14   
    15     15   include_HEADERS = sqlite3.h sqlite3ext.h
    16     16   
    17     17   EXTRA_DIST = sqlite3.1 tea Makefile.msc sqlite3.rc README.txt Replace.cs
    18     18   pkgconfigdir = ${libdir}/pkgconfig
    19     19   pkgconfig_DATA = sqlite3.pc
    20     20   
    21     21   man_MANS = sqlite3.1

Changes to autoconf/Makefile.msc.

   927    927   # Additional compiler options for the shell.  These are only effective
   928    928   # when the shell is not being dynamically linked.
   929    929   #
   930    930   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
   931    931   SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
   932    932   SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
   933    933   SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_INTROSPECTION_PRAGMAS
          934  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_RTREE
   934    935   !ENDIF
   935    936   
   936    937   
   937    938   # This is the default Makefile target.  The objects listed here
   938    939   # are what get build when you type just "make" with no arguments.
   939    940   #
   940    941   core:	dll shell

Changes to autoconf/configure.ac.

   164    164   #-----------------------------------------------------------------------
   165    165   
   166    166   AC_CHECK_FUNCS(posix_fallocate)
   167    167   AC_CHECK_HEADERS(zlib.h,[
   168    168     AC_SEARCH_LIBS(deflate,z,[ZLIB_FLAGS="-DSQLITE_HAVE_ZLIB"])
   169    169   ])
   170    170   AC_SUBST(ZLIB_FLAGS)
          171  +
          172  +AC_SEARCH_LIBS(system,,,[SHELL_CFLAGS="-DSQLITE_NOHAVE_SYSTEM"])
          173  +AC_SUBST(SHELL_CFLAGS)
   171    174   
   172    175   #-----------------------------------------------------------------------
   173    176   # UPDATE: Maybe it's better if users just set CFLAGS before invoking
   174    177   # configure. This option doesn't really add much...
   175    178   #
   176    179   #   --enable-tempstore
   177    180   #

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.69 for sqlite 3.22.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.23.0.
     4      4   #
     5      5   #
     6      6   # Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
     7      7   #
     8      8   #
     9      9   # This configure script is free software; the Free Software Foundation
    10     10   # gives unlimited permission to copy, distribute and modify it.
................................................................................
   722    722   subdirs=
   723    723   MFLAGS=
   724    724   MAKEFLAGS=
   725    725   
   726    726   # Identity of this package.
   727    727   PACKAGE_NAME='sqlite'
   728    728   PACKAGE_TARNAME='sqlite'
   729         -PACKAGE_VERSION='3.22.0'
   730         -PACKAGE_STRING='sqlite 3.22.0'
          729  +PACKAGE_VERSION='3.23.0'
          730  +PACKAGE_STRING='sqlite 3.23.0'
   731    731   PACKAGE_BUGREPORT=''
   732    732   PACKAGE_URL=''
   733    733   
   734    734   # Factoring default headers for most tests.
   735    735   ac_includes_default="\
   736    736   #include <stdio.h>
   737    737   #ifdef HAVE_SYS_TYPES_H
................................................................................
  1461   1461   #
  1462   1462   # Report the --help message.
  1463   1463   #
  1464   1464   if test "$ac_init_help" = "long"; then
  1465   1465     # Omit some internal or obsolete options to make the list less imposing.
  1466   1466     # This message is too long to be a string in the A/UX 3.1 sh.
  1467   1467     cat <<_ACEOF
  1468         -\`configure' configures sqlite 3.22.0 to adapt to many kinds of systems.
         1468  +\`configure' configures sqlite 3.23.0 to adapt to many kinds of systems.
  1469   1469   
  1470   1470   Usage: $0 [OPTION]... [VAR=VALUE]...
  1471   1471   
  1472   1472   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1473   1473   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1474   1474   
  1475   1475   Defaults for the options are specified in brackets.
................................................................................
  1526   1526     --build=BUILD     configure for building on BUILD [guessed]
  1527   1527     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1528   1528   _ACEOF
  1529   1529   fi
  1530   1530   
  1531   1531   if test -n "$ac_init_help"; then
  1532   1532     case $ac_init_help in
  1533         -     short | recursive ) echo "Configuration of sqlite 3.22.0:";;
         1533  +     short | recursive ) echo "Configuration of sqlite 3.23.0:";;
  1534   1534      esac
  1535   1535     cat <<\_ACEOF
  1536   1536   
  1537   1537   Optional Features:
  1538   1538     --disable-option-checking  ignore unrecognized --enable/--with options
  1539   1539     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1540   1540     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1651   1651       cd "$ac_pwd" || { ac_status=$?; break; }
  1652   1652     done
  1653   1653   fi
  1654   1654   
  1655   1655   test -n "$ac_init_help" && exit $ac_status
  1656   1656   if $ac_init_version; then
  1657   1657     cat <<\_ACEOF
  1658         -sqlite configure 3.22.0
         1658  +sqlite configure 3.23.0
  1659   1659   generated by GNU Autoconf 2.69
  1660   1660   
  1661   1661   Copyright (C) 2012 Free Software Foundation, Inc.
  1662   1662   This configure script is free software; the Free Software Foundation
  1663   1663   gives unlimited permission to copy, distribute and modify it.
  1664   1664   _ACEOF
  1665   1665     exit
................................................................................
  2070   2070     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2071   2071   
  2072   2072   } # ac_fn_c_check_header_mongrel
  2073   2073   cat >config.log <<_ACEOF
  2074   2074   This file contains any messages produced by compilers while
  2075   2075   running configure, to aid debugging if configure makes a mistake.
  2076   2076   
  2077         -It was created by sqlite $as_me 3.22.0, which was
         2077  +It was created by sqlite $as_me 3.23.0, which was
  2078   2078   generated by GNU Autoconf 2.69.  Invocation command line was
  2079   2079   
  2080   2080     $ $0 $@
  2081   2081   
  2082   2082   _ACEOF
  2083   2083   exec 5>>config.log
  2084   2084   {
................................................................................
 12238  12238   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12239  12239   
 12240  12240   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12241  12241   # Save the log message, to keep $0 and so on meaningful, and to
 12242  12242   # report actual input values of CONFIG_FILES etc. instead of their
 12243  12243   # values after options handling.
 12244  12244   ac_log="
 12245         -This file was extended by sqlite $as_me 3.22.0, which was
        12245  +This file was extended by sqlite $as_me 3.23.0, which was
 12246  12246   generated by GNU Autoconf 2.69.  Invocation command line was
 12247  12247   
 12248  12248     CONFIG_FILES    = $CONFIG_FILES
 12249  12249     CONFIG_HEADERS  = $CONFIG_HEADERS
 12250  12250     CONFIG_LINKS    = $CONFIG_LINKS
 12251  12251     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12252  12252     $ $0 $@
................................................................................
 12304  12304   
 12305  12305   Report bugs to the package provider."
 12306  12306   
 12307  12307   _ACEOF
 12308  12308   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12309  12309   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12310  12310   ac_cs_version="\\
 12311         -sqlite config.status 3.22.0
        12311  +sqlite config.status 3.23.0
 12312  12312   configured by $0, generated by GNU Autoconf 2.69,
 12313  12313     with options \\"\$ac_cs_config\\"
 12314  12314   
 12315  12315   Copyright (C) 2012 Free Software Foundation, Inc.
 12316  12316   This config.status script is free software; the Free Software Foundation
 12317  12317   gives unlimited permission to copy, distribute and modify it."
 12318  12318   

Changes to ext/expert/expert.c.

    89     89     p = sqlite3_expert_new(db, &zErr);
    90     90     if( p==0 ){
    91     91       fprintf(stderr, "Cannot run analysis: %s\n", zErr);
    92     92       rc = 1;
    93     93     }else{
    94     94       for(i=1; i<(argc-1); i++){
    95     95         char *zArg = argv[i];
           96  +      int nArg;
    96     97         if( zArg[0]=='-' && zArg[1]=='-' && zArg[2]!=0 ) zArg++;
    97         -      int nArg = (int)strlen(zArg);
           98  +      nArg = (int)strlen(zArg);
    98     99         if( nArg>=2 && 0==sqlite3_strnicmp(zArg, "-file", nArg) ){
    99    100           if( ++i==(argc-1) ) option_requires_argument("-file");
   100    101           rc = readSqlFromFile(p, argv[i], &zErr);
   101    102         }
   102    103   
   103    104         else if( nArg>=3 && 0==sqlite3_strnicmp(zArg, "-sql", nArg) ){
   104    105           if( ++i==(argc-1) ) option_requires_argument("-sql");

Changes to ext/fts3/fts3_write.c.

  1904   1904     sqlite3_stmt *pStmt;
  1905   1905     int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
  1906   1906     if( rc==SQLITE_OK ){
  1907   1907       sqlite3_bind_int64(pStmt, 1, iBlock);
  1908   1908       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
  1909   1909       sqlite3_step(pStmt);
  1910   1910       rc = sqlite3_reset(pStmt);
         1911  +    sqlite3_bind_null(pStmt, 2);
  1911   1912     }
  1912   1913     return rc;
  1913   1914   }
  1914   1915   
  1915   1916   /*
  1916   1917   ** Find the largest relative level number in the table. If successful, set
  1917   1918   ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
................................................................................
  1960   1961         char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
  1961   1962         if( !zEnd ) return SQLITE_NOMEM;
  1962   1963         sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
  1963   1964       }
  1964   1965       sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
  1965   1966       sqlite3_step(pStmt);
  1966   1967       rc = sqlite3_reset(pStmt);
         1968  +    sqlite3_bind_null(pStmt, 6);
  1967   1969     }
  1968   1970     return rc;
  1969   1971   }
  1970   1972   
  1971   1973   /*
  1972   1974   ** Return the size of the common prefix (if any) shared by zPrev and
  1973   1975   ** zNext, in bytes. For example, 
................................................................................
  3439   3441       *pRC = rc;
  3440   3442       return;
  3441   3443     }
  3442   3444     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
  3443   3445     sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
  3444   3446     sqlite3_step(pStmt);
  3445   3447     *pRC = sqlite3_reset(pStmt);
         3448  +  sqlite3_bind_null(pStmt, 2);
  3446   3449     sqlite3_free(a);
  3447   3450   }
  3448   3451   
  3449   3452   /*
  3450   3453   ** Merge the entire database so that there is one segment for each 
  3451   3454   ** iIndex/iLangid combination.
  3452   3455   */
................................................................................
  4627   4630       if( rc==SQLITE_OK ){
  4628   4631         sqlite3_bind_int64(pChomp, 1, iNewStart);
  4629   4632         sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
  4630   4633         sqlite3_bind_int64(pChomp, 3, iAbsLevel);
  4631   4634         sqlite3_bind_int(pChomp, 4, iIdx);
  4632   4635         sqlite3_step(pChomp);
  4633   4636         rc = sqlite3_reset(pChomp);
         4637  +      sqlite3_bind_null(pChomp, 2);
  4634   4638       }
  4635   4639     }
  4636   4640   
  4637   4641     sqlite3_free(root.a);
  4638   4642     sqlite3_free(block.a);
  4639   4643     return rc;
  4640   4644   }
................................................................................
  4706   4710   
  4707   4711     rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
  4708   4712     if( rc==SQLITE_OK ){
  4709   4713       sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
  4710   4714       sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
  4711   4715       sqlite3_step(pReplace);
  4712   4716       rc = sqlite3_reset(pReplace);
         4717  +    sqlite3_bind_null(pReplace, 2);
  4713   4718     }
  4714   4719   
  4715   4720     return rc;
  4716   4721   }
  4717   4722   
  4718   4723   /*
  4719   4724   ** Load an incr-merge hint from the database. The incr-merge hint, if one 
................................................................................
  5520   5525     sqlite3_vtab *pVtab,            /* FTS3 vtab object */
  5521   5526     int nArg,                       /* Size of argument array */
  5522   5527     sqlite3_value **apVal,          /* Array of arguments */
  5523   5528     sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
  5524   5529   ){
  5525   5530     Fts3Table *p = (Fts3Table *)pVtab;
  5526   5531     int rc = SQLITE_OK;             /* Return Code */
  5527         -  int isRemove = 0;               /* True for an UPDATE or DELETE */
  5528   5532     u32 *aSzIns = 0;                /* Sizes of inserted documents */
  5529   5533     u32 *aSzDel = 0;                /* Sizes of deleted documents */
  5530   5534     int nChng = 0;                  /* Net change in number of documents */
  5531   5535     int bInsertDone = 0;
  5532   5536   
  5533   5537     /* At this point it must be known if the %_stat table exists or not.
  5534   5538     ** So bHasStat may not be 2.  */
................................................................................
  5618   5622       goto update_out;
  5619   5623     }
  5620   5624   
  5621   5625     /* If this is a DELETE or UPDATE operation, remove the old record. */
  5622   5626     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  5623   5627       assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
  5624   5628       rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
  5625         -    isRemove = 1;
  5626   5629     }
  5627   5630     
  5628   5631     /* If this is an INSERT or UPDATE operation, insert the new record. */
  5629   5632     if( nArg>1 && rc==SQLITE_OK ){
  5630   5633       int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
  5631   5634       if( bInsertDone==0 ){
  5632   5635         rc = fts3InsertData(p, apVal, pRowid);
  5633   5636         if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
  5634   5637           rc = FTS_CORRUPT_VTAB;
  5635   5638         }
  5636   5639       }
  5637         -    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
         5640  +    if( rc==SQLITE_OK ){
  5638   5641         rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
  5639   5642       }
  5640   5643       if( rc==SQLITE_OK ){
  5641   5644         assert( p->iPrevDocid==*pRowid );
  5642   5645         rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
  5643   5646       }
  5644   5647       if( p->bHasDocsize ){

Changes to ext/fts5/fts5_expr.c.

  1672   1672       }
  1673   1673   
  1674   1674       if( sCtx.pPhrase==0 ){
  1675   1675         /* This happens when parsing a token or quoted phrase that contains
  1676   1676         ** no token characters at all. (e.g ... MATCH '""'). */
  1677   1677         sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
  1678   1678       }else if( sCtx.pPhrase->nTerm ){
  1679         -      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
         1679  +      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
  1680   1680       }
  1681   1681       pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
  1682   1682     }
  1683   1683   
  1684   1684     return sCtx.pPhrase;
  1685   1685   }
  1686   1686   

Changes to ext/fts5/fts5_index.c.

   754    754       if( p->rc ) return;
   755    755     }
   756    756   
   757    757     sqlite3_bind_int64(p->pWriter, 1, iRowid);
   758    758     sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
   759    759     sqlite3_step(p->pWriter);
   760    760     p->rc = sqlite3_reset(p->pWriter);
          761  +  sqlite3_bind_null(p->pWriter, 2);
   761    762   }
   762    763   
   763    764   /*
   764    765   ** Execute the following SQL:
   765    766   **
   766    767   **     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
   767    768   */
................................................................................
  2382   2383     sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
  2383   2384     if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
  2384   2385       i64 val = sqlite3_column_int(pIdxSelect, 0);
  2385   2386       iPg = (int)(val>>1);
  2386   2387       bDlidx = (val & 0x0001);
  2387   2388     }
  2388   2389     p->rc = sqlite3_reset(pIdxSelect);
         2390  +  sqlite3_bind_null(pIdxSelect, 2);
  2389   2391   
  2390   2392     if( iPg<pSeg->pgnoFirst ){
  2391   2393       iPg = pSeg->pgnoFirst;
  2392   2394       bDlidx = 0;
  2393   2395     }
  2394   2396   
  2395   2397     pIter->iLeafPgno = iPg - 1;
................................................................................
  3594   3596           sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
  3595   3597           if( p->rc==SQLITE_OK ){
  3596   3598             u8 aBlob[2] = {0xff, 0xff};
  3597   3599             sqlite3_bind_int(pIdxSelect, 1, iSegid);
  3598   3600             sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
  3599   3601             assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
  3600   3602             p->rc = sqlite3_reset(pIdxSelect);
         3603  +          sqlite3_bind_null(pIdxSelect, 2);
  3601   3604           }
  3602   3605         }
  3603   3606   #endif
  3604   3607       }
  3605   3608     }
  3606   3609   
  3607   3610     return iSegid;
................................................................................
  3720   3723       const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
  3721   3724       /* The following was already done in fts5WriteInit(): */
  3722   3725       /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
  3723   3726       sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
  3724   3727       sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
  3725   3728       sqlite3_step(p->pIdxWriter);
  3726   3729       p->rc = sqlite3_reset(p->pIdxWriter);
         3730  +    sqlite3_bind_null(p->pIdxWriter, 2);
  3727   3731     }
  3728   3732     pWriter->iBtPage = 0;
  3729   3733   }
  3730   3734   
  3731   3735   /*
  3732   3736   ** This is called once for each leaf page except the first that contains
  3733   3737   ** at least one term. Argument (nTerm/pTerm) is the split-key - a term that

Changes to ext/fts5/fts5_storage.c.

   454    454       sqlite3_stmt *pReplace = 0;
   455    455       rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
   456    456       if( rc==SQLITE_OK ){
   457    457         sqlite3_bind_int64(pReplace, 1, iRowid);
   458    458         sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
   459    459         sqlite3_step(pReplace);
   460    460         rc = sqlite3_reset(pReplace);
          461  +      sqlite3_bind_null(pReplace, 2);
   461    462       }
   462    463     }
   463    464     return rc;
   464    465   }
   465    466   
   466    467   /*
   467    468   ** Load the contents of the "averages" record from disk into the 
................................................................................
  1114   1115       if( pVal ){
  1115   1116         sqlite3_bind_value(pReplace, 2, pVal);
  1116   1117       }else{
  1117   1118         sqlite3_bind_int(pReplace, 2, iVal);
  1118   1119       }
  1119   1120       sqlite3_step(pReplace);
  1120   1121       rc = sqlite3_reset(pReplace);
         1122  +    sqlite3_bind_null(pReplace, 1);
  1121   1123     }
  1122   1124     if( rc==SQLITE_OK && pVal ){
  1123   1125       int iNew = p->pConfig->iCookie + 1;
  1124   1126       rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
  1125   1127       if( rc==SQLITE_OK ){
  1126   1128         p->pConfig->iCookie = iNew;
  1127   1129       }
  1128   1130     }
  1129   1131     return rc;
  1130   1132   }

Changes to ext/fts5/test/fts5aa.test.

   589    589   
   590    590   do_execsql_test 22.1 {
   591    591     SELECT rowid FROM t9('a*')
   592    592   } {1}
   593    593   
   594    594   }
   595    595   
   596         -
          596  +expand_all_sql db
   597    597   finish_test

Changes to ext/misc/btreeinfo.c.

   335    335   ){
   336    336     BinfoCursor *pCsr = (BinfoCursor *)pCursor;
   337    337     if( i>=BINFO_COLUMN_HASROWID && i<=BINFO_COLUMN_SZPAGE && pCsr->hasRowid<0 ){
   338    338       int pgno = sqlite3_column_int(pCsr->pStmt, BINFO_COLUMN_ROOTPAGE+1);
   339    339       sqlite3 *db = sqlite3_context_db_handle(ctx);
   340    340       int rc = binfoCompute(db, pgno, pCsr);
   341    341       if( rc ){
   342         -      return rc;
          342  +      pCursor->pVtab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
          343  +      return SQLITE_ERROR;
   343    344       }
   344    345     }
   345    346     switch( i ){
   346    347       case BINFO_COLUMN_NAME:
   347    348       case BINFO_COLUMN_TYPE:
   348    349       case BINFO_COLUMN_TBL_NAME:
   349    350       case BINFO_COLUMN_ROOTPAGE:

Changes to ext/misc/dbdump.c.

   289    289           if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
   290    290         }
   291    291         if( i>nCol ){
   292    292           /* At this point, we know that azRowid[j] is not the name of any
   293    293           ** ordinary column in the table.  Verify that azRowid[j] is a valid
   294    294           ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
   295    295           ** tables will fail this last check */
   296         -        int rc;
   297    296           rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
   298    297           if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
   299    298           break;
   300    299         }
   301    300       }
   302    301     }
   303    302     return azCol;
................................................................................
   451    450       }
   452    451       p->xCallback(";\n", p->pArg);
   453    452     }
   454    453   
   455    454     if( strcmp(zType, "table")==0 ){
   456    455       DText sSelect;
   457    456       DText sTable;
   458         -    char **azCol;
          457  +    char **azTCol;
   459    458       int i;
   460    459       int nCol;
   461    460   
   462         -    azCol = tableColumnList(p, zTable);
   463         -    if( azCol==0 ) return 0;
          461  +    azTCol = tableColumnList(p, zTable);
          462  +    if( azTCol==0 ) return 0;
   464    463   
   465    464       initText(&sTable);
   466    465       appendText(&sTable, "INSERT INTO ", 0);
   467    466   
   468    467       /* Always quote the table name, even if it appears to be pure ascii,
   469    468       ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
   470    469       appendText(&sTable, zTable, quoteChar(zTable));
   471    470   
   472    471       /* If preserving the rowid, add a column list after the table name.
   473    472       ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
   474    473       ** instead of the usual "INSERT INTO tab VALUES(...)".
   475    474       */
   476         -    if( azCol[0] ){
          475  +    if( azTCol[0] ){
   477    476         appendText(&sTable, "(", 0);
   478         -      appendText(&sTable, azCol[0], 0);
   479         -      for(i=1; azCol[i]; i++){
          477  +      appendText(&sTable, azTCol[0], 0);
          478  +      for(i=1; azTCol[i]; i++){
   480    479           appendText(&sTable, ",", 0);
   481         -        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
          480  +        appendText(&sTable, azTCol[i], quoteChar(azTCol[i]));
   482    481         }
   483    482         appendText(&sTable, ")", 0);
   484    483       }
   485    484       appendText(&sTable, " VALUES(", 0);
   486    485   
   487    486       /* Build an appropriate SELECT statement */
   488    487       initText(&sSelect);
   489    488       appendText(&sSelect, "SELECT ", 0);
   490         -    if( azCol[0] ){
   491         -      appendText(&sSelect, azCol[0], 0);
          489  +    if( azTCol[0] ){
          490  +      appendText(&sSelect, azTCol[0], 0);
   492    491         appendText(&sSelect, ",", 0);
   493    492       }
   494         -    for(i=1; azCol[i]; i++){
   495         -      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
   496         -      if( azCol[i+1] ){
          493  +    for(i=1; azTCol[i]; i++){
          494  +      appendText(&sSelect, azTCol[i], quoteChar(azTCol[i]));
          495  +      if( azTCol[i+1] ){
   497    496           appendText(&sSelect, ",", 0);
   498    497         }
   499    498       }
   500    499       nCol = i;
   501         -    if( azCol[0]==0 ) nCol--;
   502         -    freeColumnList(azCol);
          500  +    if( azTCol[0]==0 ) nCol--;
          501  +    freeColumnList(azTCol);
   503    502       appendText(&sSelect, " FROM ", 0);
   504    503       appendText(&sSelect, zTable, quoteChar(zTable));
   505    504   
   506    505       rc = sqlite3_prepare_v2(p->db, sSelect.z, -1, &pStmt, 0);
   507    506       if( rc!=SQLITE_OK ){
   508    507         p->nErr++;
   509    508         if( p->rc==SQLITE_OK ) p->rc = rc;

Changes to ext/misc/fileio.c.

   266    266   #if defined(_WIN32)
   267    267       /* Windows */
   268    268       FILETIME lastAccess;
   269    269       FILETIME lastWrite;
   270    270       SYSTEMTIME currentTime;
   271    271       LONGLONG intervals;
   272    272       HANDLE hFile;
          273  +    LPWSTR zUnicodeName;
          274  +    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
          275  +
   273    276       GetSystemTime(&currentTime);
   274    277       SystemTimeToFileTime(&currentTime, &lastAccess);
   275    278       intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
   276    279       lastWrite.dwLowDateTime = (DWORD)intervals;
   277    280       lastWrite.dwHighDateTime = intervals >> 32;
   278         -    hFile = CreateFile(
   279         -      zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
          281  +    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
          282  +    hFile = CreateFileW(
          283  +      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
   280    284         FILE_FLAG_BACKUP_SEMANTICS, NULL
   281    285       );
          286  +    sqlite3_free(zUnicodeName);
   282    287       if( hFile!=INVALID_HANDLE_VALUE ){
   283    288         BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
   284    289         CloseHandle(hFile);
   285    290         return !bResult;
   286    291       }else{
   287    292         return 1;
   288    293       }
................................................................................
   482    487     int i;
   483    488     for(i=0; i<=pCur->iLvl; i++){
   484    489       FsdirLevel *pLvl = &pCur->aLvl[i];
   485    490       if( pLvl->pDir ) closedir(pLvl->pDir);
   486    491       sqlite3_free(pLvl->zDir);
   487    492     }
   488    493     sqlite3_free(pCur->zPath);
          494  +  sqlite3_free(pCur->aLvl);
   489    495     pCur->aLvl = 0;
   490    496     pCur->zPath = 0;
   491    497     pCur->zBase = 0;
   492    498     pCur->nBase = 0;
   493    499     pCur->iLvl = -1;
   494    500     pCur->iRowid = 1;
   495    501   }
................................................................................
   497    503   /*
   498    504   ** Destructor for an fsdir_cursor.
   499    505   */
   500    506   static int fsdirClose(sqlite3_vtab_cursor *cur){
   501    507     fsdir_cursor *pCur = (fsdir_cursor*)cur;
   502    508   
   503    509     fsdirResetCursor(pCur);
   504         -  sqlite3_free(pCur->aLvl);
   505    510     sqlite3_free(pCur);
   506    511     return SQLITE_OK;
   507    512   }
   508    513   
   509    514   /*
   510    515   ** Set the error message for the virtual table associated with cursor
   511    516   ** pCur to the results of vprintf(zFmt, ...).

Added ext/misc/normalize.c.

            1  +/*
            2  +** 2018-01-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains code to implement the sqlite3_normalize() function.
           14  +**
           15  +**    char *sqlite3_normalize(const char *zSql);
           16  +**
           17  +** This function takes an SQL string as input and returns a "normalized"
           18  +** version of that string in memory obtained from sqlite3_malloc64().  The
           19  +** caller is responsible for ensuring that the returned memory is freed.
           20  +**
           21  +** If a memory allocation error occurs, this routine returns NULL.
           22  +**
           23  +** The normalization consists of the following transformations:
           24  +**
           25  +**   (1)  Convert every literal (string, blob literal, numeric constant,
           26  +**        or "NULL" constant) into a ?
           27  +**
           28  +**   (2)  Remove all superfluous whitespace, including comments.  Change
           29  +**        all required whitespace to a single space character.
           30  +**
           31  +**   (3)  Lowercase all ASCII characters.
           32  +**
           33  +**   (4)  If an IN or NOT IN operator is followed by a list of 1 or more
           34  +**        values, convert that list into "(?,?,?)".
           35  +**
           36  +** The purpose of normalization is two-fold:
           37  +**
           38  +**   (1)  Sanitize queries by removing potentially private or sensitive
           39  +**        information contained in literals.
           40  +**
           41  +**   (2)  Identify structurally identical queries by comparing their
           42  +**        normalized forms.
           43  +**
           44  +** Command-Line Utility
           45  +** --------------------
           46  +**
           47  +** This file also contains code for a command-line utility that converts
           48  +** SQL queries in text files into their normalized forms.  To build the
           49  +** command-line program, compile this file with -DSQLITE_NORMALIZE_CLI
           50  +** and link it against the SQLite library.
           51  +*/
           52  +#include <sqlite3.h>
           53  +#include <string.h>
           54  +
           55  +/*
           56  +** Implementation note:
           57  +**
           58  +** Much of the tokenizer logic is copied out of the tokenize.c source file
           59  +** of SQLite.  That logic could be simplified for this particular application,
           60  +** but that would impose a risk of introducing subtle errors.  It is best to
           61  +** keep the code as close to the original as possible.
           62  +**
           63  +** The tokenize code is in sync with the SQLite core as of 2018-01-08.
           64  +** Any future changes to the core tokenizer might require corresponding
           65  +** adjustments to the tokenizer logic in this module.
           66  +*/
           67  +
           68  +
           69  +/* Character classes for tokenizing
           70  +**
           71  +** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
           72  +** using a lookup table, whereas a switch() directly on c uses a binary search.
           73  +** The lookup table is much faster.  To maximize speed, and to ensure that
           74  +** a lookup table is used, all of the classes need to be small integers and
           75  +** all of them need to be used within the switch.
           76  +*/
           77  +#define CC_X          0    /* The letter 'x', or start of BLOB literal */
           78  +#define CC_KYWD       1    /* Alphabetics or '_'.  Usable in a keyword */
           79  +#define CC_ID         2    /* unicode characters usable in IDs */
           80  +#define CC_DIGIT      3    /* Digits */
           81  +#define CC_DOLLAR     4    /* '$' */
           82  +#define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
           83  +#define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
           84  +#define CC_SPACE      7    /* Space characters */
           85  +#define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
           86  +#define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
           87  +#define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
           88  +#define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
           89  +#define CC_LT        12    /* '<'.  Part of < or <= or <> */
           90  +#define CC_GT        13    /* '>'.  Part of > or >= */
           91  +#define CC_EQ        14    /* '='.  Part of = or == */
           92  +#define CC_BANG      15    /* '!'.  Part of != */
           93  +#define CC_SLASH     16    /* '/'.  / or c-style comment */
           94  +#define CC_LP        17    /* '(' */
           95  +#define CC_RP        18    /* ')' */
           96  +#define CC_SEMI      19    /* ';' */
           97  +#define CC_PLUS      20    /* '+' */
           98  +#define CC_STAR      21    /* '*' */
           99  +#define CC_PERCENT   22    /* '%' */
          100  +#define CC_COMMA     23    /* ',' */
          101  +#define CC_AND       24    /* '&' */
          102  +#define CC_TILDA     25    /* '~' */
          103  +#define CC_DOT       26    /* '.' */
          104  +#define CC_ILLEGAL   27    /* Illegal character */
          105  +
          106  +static const unsigned char aiClass[] = {
          107  +/*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
          108  +/* 0x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  7,  7, 27,  7,  7, 27, 27,
          109  +/* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
          110  +/* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
          111  +/* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
          112  +/* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
          113  +/* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1,  9, 27, 27, 27,  1,
          114  +/* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
          115  +/* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 10, 27, 25, 27,
          116  +/* 8x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          117  +/* 9x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          118  +/* Ax */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          119  +/* Bx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          120  +/* Cx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          121  +/* Dx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          122  +/* Ex */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
          123  +/* Fx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2
          124  +};
          125  +
          126  +/* An array to map all upper-case characters into their corresponding
          127  +** lower-case character. 
          128  +**
          129  +** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
          130  +** handle case conversions for the UTF character set since the tables
          131  +** involved are nearly as big or bigger than SQLite itself.
          132  +*/
          133  +static const unsigned char sqlite3UpperToLower[] = {
          134  +      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
          135  +     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
          136  +     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
          137  +     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
          138  +    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
          139  +    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
          140  +    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
          141  +    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
          142  +    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
          143  +    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
          144  +    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
          145  +    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
          146  +    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
          147  +    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
          148  +    252,253,254,255
          149  +};
          150  +
          151  +/*
          152  +** The following 256 byte lookup table is used to support SQLites built-in
          153  +** equivalents to the following standard library functions:
          154  +**
          155  +**   isspace()                        0x01
          156  +**   isalpha()                        0x02
          157  +**   isdigit()                        0x04
          158  +**   isalnum()                        0x06
          159  +**   isxdigit()                       0x08
          160  +**   toupper()                        0x20
          161  +**   SQLite identifier character      0x40
          162  +**   Quote character                  0x80
          163  +**
          164  +** Bit 0x20 is set if the mapped character requires translation to upper
          165  +** case. i.e. if the character is a lower-case ASCII character.
          166  +** If x is a lower-case ASCII character, then its upper-case equivalent
          167  +** is (x - 0x20). Therefore toupper() can be implemented as:
          168  +**
          169  +**   (x & ~(map[x]&0x20))
          170  +**
          171  +** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
          172  +** array. tolower() is used more often than toupper() by SQLite.
          173  +**
          174  +** Bit 0x40 is set if the character is non-alphanumeric and can be used in an 
          175  +** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
          176  +** non-ASCII UTF character. Hence the test for whether or not a character is
          177  +** part of an identifier is 0x46.
          178  +*/
          179  +static const unsigned char sqlite3CtypeMap[256] = {
          180  +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
          181  +  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
          182  +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
          183  +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
          184  +  0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
          185  +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
          186  +  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
          187  +  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
          188  +
          189  +  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
          190  +  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
          191  +  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
          192  +  0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
          193  +  0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
          194  +  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
          195  +  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
          196  +  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
          197  +
          198  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
          199  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
          200  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
          201  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
          202  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
          203  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
          204  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
          205  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
          206  +
          207  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
          208  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
          209  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
          210  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
          211  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
          212  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
          213  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
          214  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
          215  +};
          216  +#define sqlite3Toupper(x)   ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
          217  +#define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
          218  +#define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
          219  +#define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
          220  +#define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
          221  +#define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
          222  +#define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
          223  +#define sqlite3Isquote(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
          224  +
          225  +
          226  +/*
          227  +** If X is a character that can be used in an identifier then
          228  +** IdChar(X) will be true.  Otherwise it is false.
          229  +**
          230  +** For ASCII, any character with the high-order bit set is
          231  +** allowed in an identifier.  For 7-bit characters, 
          232  +** sqlite3IsIdChar[X] must be 1.
          233  +**
          234  +** For EBCDIC, the rules are more complex but have the same
          235  +** end result.
          236  +**
          237  +** Ticket #1066.  the SQL standard does not allow '$' in the
          238  +** middle of identifiers.  But many SQL implementations do. 
          239  +** SQLite will allow '$' in identifiers for compatibility.
          240  +** But the feature is undocumented.
          241  +*/
          242  +#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
          243  +
          244  +/*
          245  +** Ignore testcase() macros
          246  +*/
          247  +#define testcase(X)
          248  +
          249  +/*
          250  +** Token values
          251  +*/
          252  +#define TK_SPACE    0
          253  +#define TK_NAME     1
          254  +#define TK_LITERAL  2
          255  +#define TK_PUNCT    3
          256  +#define TK_ERROR    4
          257  +
          258  +#define TK_MINUS    TK_PUNCT
          259  +#define TK_LP       TK_PUNCT
          260  +#define TK_RP       TK_PUNCT
          261  +#define TK_SEMI     TK_PUNCT
          262  +#define TK_PLUS     TK_PUNCT
          263  +#define TK_STAR     TK_PUNCT
          264  +#define TK_SLASH    TK_PUNCT
          265  +#define TK_REM      TK_PUNCT
          266  +#define TK_EQ       TK_PUNCT
          267  +#define TK_LE       TK_PUNCT
          268  +#define TK_NE       TK_PUNCT
          269  +#define TK_LSHIFT   TK_PUNCT
          270  +#define TK_LT       TK_PUNCT
          271  +#define TK_GE       TK_PUNCT
          272  +#define TK_RSHIFT   TK_PUNCT
          273  +#define TK_GT       TK_PUNCT
          274  +#define TK_GE       TK_PUNCT
          275  +#define TK_BITOR    TK_PUNCT
          276  +#define TK_CONCAT   TK_PUNCT
          277  +#define TK_COMMA    TK_PUNCT
          278  +#define TK_BITAND   TK_PUNCT
          279  +#define TK_BITNOT   TK_PUNCT
          280  +#define TK_STRING   TK_LITERAL
          281  +#define TK_ID       TK_NAME
          282  +#define TK_ILLEGAL  TK_ERROR
          283  +#define TK_DOT      TK_PUNCT
          284  +#define TK_INTEGER  TK_LITERAL
          285  +#define TK_FLOAT    TK_LITERAL
          286  +#define TK_VARIABLE TK_LITERAL
          287  +#define TK_BLOB     TK_LITERAL
          288  +
          289  +/*
          290  +** Return the length (in bytes) of the token that begins at z[0]. 
          291  +** Store the token type in *tokenType before returning.
          292  +*/
          293  +static int sqlite3GetToken(const unsigned char *z, int *tokenType){
          294  +  int i, c;
          295  +  switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
          296  +                          ** of the token. See the comment on the CC_ defines
          297  +                          ** above. */
          298  +    case CC_SPACE: {
          299  +      for(i=1; sqlite3Isspace(z[i]); i++){}
          300  +      *tokenType = TK_SPACE;
          301  +      return i;
          302  +    }
          303  +    case CC_MINUS: {
          304  +      if( z[1]=='-' ){
          305  +        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
          306  +        *tokenType = TK_SPACE;
          307  +        return i;
          308  +      }
          309  +      *tokenType = TK_MINUS;
          310  +      return 1;
          311  +    }
          312  +    case CC_LP: {
          313  +      *tokenType = TK_LP;
          314  +      return 1;
          315  +    }
          316  +    case CC_RP: {
          317  +      *tokenType = TK_RP;
          318  +      return 1;
          319  +    }
          320  +    case CC_SEMI: {
          321  +      *tokenType = TK_SEMI;
          322  +      return 1;
          323  +    }
          324  +    case CC_PLUS: {
          325  +      *tokenType = TK_PLUS;
          326  +      return 1;
          327  +    }
          328  +    case CC_STAR: {
          329  +      *tokenType = TK_STAR;
          330  +      return 1;
          331  +    }
          332  +    case CC_SLASH: {
          333  +      if( z[1]!='*' || z[2]==0 ){
          334  +        *tokenType = TK_SLASH;
          335  +        return 1;
          336  +      }
          337  +      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
          338  +      if( c ) i++;
          339  +      *tokenType = TK_SPACE;
          340  +      return i;
          341  +    }
          342  +    case CC_PERCENT: {
          343  +      *tokenType = TK_REM;
          344  +      return 1;
          345  +    }
          346  +    case CC_EQ: {
          347  +      *tokenType = TK_EQ;
          348  +      return 1 + (z[1]=='=');
          349  +    }
          350  +    case CC_LT: {
          351  +      if( (c=z[1])=='=' ){
          352  +        *tokenType = TK_LE;
          353  +        return 2;
          354  +      }else if( c=='>' ){
          355  +        *tokenType = TK_NE;
          356  +        return 2;
          357  +      }else if( c=='<' ){
          358  +        *tokenType = TK_LSHIFT;
          359  +        return 2;
          360  +      }else{
          361  +        *tokenType = TK_LT;
          362  +        return 1;
          363  +      }
          364  +    }
          365  +    case CC_GT: {
          366  +      if( (c=z[1])=='=' ){
          367  +        *tokenType = TK_GE;
          368  +        return 2;
          369  +      }else if( c=='>' ){
          370  +        *tokenType = TK_RSHIFT;
          371  +        return 2;
          372  +      }else{
          373  +        *tokenType = TK_GT;
          374  +        return 1;
          375  +      }
          376  +    }
          377  +    case CC_BANG: {
          378  +      if( z[1]!='=' ){
          379  +        *tokenType = TK_ILLEGAL;
          380  +        return 1;
          381  +      }else{
          382  +        *tokenType = TK_NE;
          383  +        return 2;
          384  +      }
          385  +    }
          386  +    case CC_PIPE: {
          387  +      if( z[1]!='|' ){
          388  +        *tokenType = TK_BITOR;
          389  +        return 1;
          390  +      }else{
          391  +        *tokenType = TK_CONCAT;
          392  +        return 2;
          393  +      }
          394  +    }
          395  +    case CC_COMMA: {
          396  +      *tokenType = TK_COMMA;
          397  +      return 1;
          398  +    }
          399  +    case CC_AND: {
          400  +      *tokenType = TK_BITAND;
          401  +      return 1;
          402  +    }
          403  +    case CC_TILDA: {
          404  +      *tokenType = TK_BITNOT;
          405  +      return 1;
          406  +    }
          407  +    case CC_QUOTE: {
          408  +      int delim = z[0];
          409  +      testcase( delim=='`' );
          410  +      testcase( delim=='\'' );
          411  +      testcase( delim=='"' );
          412  +      for(i=1; (c=z[i])!=0; i++){
          413  +        if( c==delim ){
          414  +          if( z[i+1]==delim ){
          415  +            i++;
          416  +          }else{
          417  +            break;
          418  +          }
          419  +        }
          420  +      }
          421  +      if( c=='\'' ){
          422  +        *tokenType = TK_STRING;
          423  +        return i+1;
          424  +      }else if( c!=0 ){
          425  +        *tokenType = TK_ID;
          426  +        return i+1;
          427  +      }else{
          428  +        *tokenType = TK_ILLEGAL;
          429  +        return i;
          430  +      }
          431  +    }
          432  +    case CC_DOT: {
          433  +      if( !sqlite3Isdigit(z[1]) ){
          434  +        *tokenType = TK_DOT;
          435  +        return 1;
          436  +      }
          437  +      /* If the next character is a digit, this is a floating point
          438  +      ** number that begins with ".".  Fall thru into the next case */
          439  +    }
          440  +    case CC_DIGIT: {
          441  +      *tokenType = TK_INTEGER;
          442  +      if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
          443  +        for(i=3; sqlite3Isxdigit(z[i]); i++){}
          444  +        return i;
          445  +      }
          446  +      for(i=0; sqlite3Isdigit(z[i]); i++){}
          447  +      if( z[i]=='.' ){
          448  +        i++;
          449  +        while( sqlite3Isdigit(z[i]) ){ i++; }
          450  +        *tokenType = TK_FLOAT;
          451  +      }
          452  +      if( (z[i]=='e' || z[i]=='E') &&
          453  +           ( sqlite3Isdigit(z[i+1]) 
          454  +            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
          455  +           )
          456  +      ){
          457  +        i += 2;
          458  +        while( sqlite3Isdigit(z[i]) ){ i++; }
          459  +        *tokenType = TK_FLOAT;
          460  +      }
          461  +      while( IdChar(z[i]) ){
          462  +        *tokenType = TK_ILLEGAL;
          463  +        i++;
          464  +      }
          465  +      return i;
          466  +    }
          467  +    case CC_QUOTE2: {
          468  +      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
          469  +      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
          470  +      return i;
          471  +    }
          472  +    case CC_VARNUM: {
          473  +      *tokenType = TK_VARIABLE;
          474  +      for(i=1; sqlite3Isdigit(z[i]); i++){}
          475  +      return i;
          476  +    }
          477  +    case CC_DOLLAR:
          478  +    case CC_VARALPHA: {
          479  +      int n = 0;
          480  +      testcase( z[0]=='$' );  testcase( z[0]=='@' );
          481  +      testcase( z[0]==':' );  testcase( z[0]=='#' );
          482  +      *tokenType = TK_VARIABLE;
          483  +      for(i=1; (c=z[i])!=0; i++){
          484  +        if( IdChar(c) ){
          485  +          n++;
          486  +        }else if( c=='(' && n>0 ){
          487  +          do{
          488  +            i++;
          489  +          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
          490  +          if( c==')' ){
          491  +            i++;
          492  +          }else{
          493  +            *tokenType = TK_ILLEGAL;
          494  +          }
          495  +          break;
          496  +        }else if( c==':' && z[i+1]==':' ){
          497  +          i++;
          498  +        }else{
          499  +          break;
          500  +        }
          501  +      }
          502  +      if( n==0 ) *tokenType = TK_ILLEGAL;
          503  +      return i;
          504  +    }
          505  +    case CC_KYWD: {
          506  +      for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
          507  +      if( IdChar(z[i]) ){
          508  +        /* This token started out using characters that can appear in keywords,
          509  +        ** but z[i] is a character not allowed within keywords, so this must
          510  +        ** be an identifier instead */
          511  +        i++;
          512  +        break;
          513  +      }
          514  +      *tokenType = TK_ID;
          515  +      return i;
          516  +    }
          517  +    case CC_X: {
          518  +      testcase( z[0]=='x' ); testcase( z[0]=='X' );
          519  +      if( z[1]=='\'' ){
          520  +        *tokenType = TK_BLOB;
          521  +        for(i=2; sqlite3Isxdigit(z[i]); i++){}
          522  +        if( z[i]!='\'' || i%2 ){
          523  +          *tokenType = TK_ILLEGAL;
          524  +          while( z[i] && z[i]!='\'' ){ i++; }
          525  +        }
          526  +        if( z[i] ) i++;
          527  +        return i;
          528  +      }
          529  +      /* If it is not a BLOB literal, then it must be an ID, since no
          530  +      ** SQL keywords start with the letter 'x'.  Fall through */
          531  +    }
          532  +    case CC_ID: {
          533  +      i = 1;
          534  +      break;
          535  +    }
          536  +    default: {
          537  +      *tokenType = TK_ILLEGAL;
          538  +      return 1;
          539  +    }
          540  +  }
          541  +  while( IdChar(z[i]) ){ i++; }
          542  +  *tokenType = TK_ID;
          543  +  return i;
          544  +}
          545  +
          546  +char *sqlite3_normalize(const char *zSql){
          547  +  char *z;              /* The output string */
          548  +  sqlite3_int64 nZ;     /* Size of the output string in bytes */
          549  +  sqlite3_int64 nSql;   /* Size of the input string in bytes */
          550  +  int i;                /* Next character to read from zSql[] */
          551  +  int j;                /* Next slot to fill in on z[] */
          552  +  int tokenType;        /* Type of the next token */
          553  +  int n;                /* Size of the next token */
          554  +  int k;                /* Loop counter */
          555  +
          556  +  nSql = strlen(zSql);
          557  +  nZ = nSql;
          558  +  z = sqlite3_malloc64( nZ+2 );
          559  +  if( z==0 ) return 0;
          560  +  for(i=j=0; zSql[i]; i += n){
          561  +    n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
          562  +    switch( tokenType ){
          563  +      case TK_SPACE: {
          564  +        break;
          565  +      }
          566  +      case TK_ERROR: {
          567  +        sqlite3_free(z);
          568  +        return 0;
          569  +      }
          570  +      case TK_LITERAL: {
          571  +        z[j++] = '?';
          572  +        break;
          573  +      }
          574  +      case TK_PUNCT:
          575  +      case TK_NAME: {
          576  +        if( n==4 && sqlite3_strnicmp(zSql+i,"NULL",4)==0 ){
          577  +          if( (j>=3 && strncmp(z+j-2,"is",2)==0 && !IdChar(z[j-3]))
          578  +           || (j>=4 && strncmp(z+j-3,"not",3)==0 && !IdChar(z[j-4]))
          579  +          ){
          580  +            /* NULL is a keyword in this case, not a literal value */
          581  +          }else{
          582  +            /* Here the NULL is a literal value */
          583  +            z[j++] = '?';
          584  +            break;
          585  +          }
          586  +        }
          587  +        if( j>0 && IdChar(z[j-1]) && IdChar(zSql[i]) ) z[j++] = ' ';
          588  +        for(k=0; k<n; k++){
          589  +          z[j++] = sqlite3Tolower(zSql[i+k]);
          590  +        }
          591  +        break;
          592  +      }
          593  +    }
          594  +  }
          595  +  while( j>0 && z[j-1]==' ' ){ j--; }
          596  +  if( i>0 && z[j-1]!=';' ){ z[j++] = ';'; }
          597  +  z[j] = 0;
          598  +
          599  +  /* Make a second pass converting "in(...)" where the "..." is not a
          600  +  ** SELECT statement into "in(?,?,?)" */
          601  +  for(i=0; i<j; i=n){
          602  +    char *zIn = strstr(z+i, "in(");
          603  +    int nParen;
          604  +    if( zIn==0 ) break;
          605  +    n = (int)(zIn-z)+3;  /* Index of first char past "in(" */
          606  +    if( n && IdChar(zIn[-1]) ) continue;
          607  +    if( strncmp(zIn, "in(select",9)==0 && !IdChar(zIn[9]) ) continue;
          608  +    if( strncmp(zIn, "in(with",7)==0 && !IdChar(zIn[7]) ) continue;
          609  +    for(nParen=1, k=0; z[n+k]; k++){
          610  +      if( z[n+k]=='(' ) nParen++;
          611  +      if( z[n+k]==')' ){
          612  +        nParen--;
          613  +        if( nParen==0 ) break;
          614  +      }
          615  +    }
          616  +    /* k is the number of bytes in the "..." within "in(...)" */
          617  +    if( k<5 ){
          618  +      z = sqlite3_realloc64(z, j+(5-k)+1);
          619  +      if( z==0 ) return 0;
          620  +      memmove(z+n+5, z+n+k, j-(n+k));
          621  +    }else if( k>5 ){
          622  +      memmove(z+n+5, z+n+k, j-(n+k));
          623  +    }
          624  +    j = j-k+5;
          625  +    z[j] = 0;
          626  +    memcpy(z+n, "?,?,?", 5);
          627  +  }
          628  +  return z;
          629  +}
          630  +
          631  +/*
          632  +** For testing purposes, or to build a stand-alone SQL normalizer program,
          633  +** compile this one source file with the -DSQLITE_NORMALIZE_CLI and link
          634  +** it against any SQLite library.  The resulting command-line program will
          635  +** run sqlite3_normalize() over the text of all files named on the command-
          636  +** line and show the result on standard output.
          637  +*/
          638  +#ifdef SQLITE_NORMALIZE_CLI
          639  +#include <stdio.h>
          640  +#include <stdlib.h>
          641  +
          642  +/*
          643  +** Break zIn up into separate SQL statements and run sqlite3_normalize()
          644  +** on each one.  Print the result of each run.
          645  +*/
          646  +static void normalizeFile(char *zIn){
          647  +  int i;
          648  +  if( zIn==0 ) return;
          649  +  for(i=0; zIn[i]; i++){
          650  +    char cSaved;
          651  +    if( zIn[i]!=';' ) continue;
          652  +    cSaved = zIn[i+1];
          653  +    zIn[i+1] = 0;
          654  +    if( sqlite3_complete(zIn) ){
          655  +      char *zOut = sqlite3_normalize(zIn);
          656  +      if( zOut ){
          657  +        printf("%s\n", zOut);
          658  +        sqlite3_free(zOut);
          659  +      }else{
          660  +        fprintf(stderr, "ERROR: %s\n", zIn);
          661  +      }
          662  +      zIn[i+1] = cSaved;
          663  +      zIn += i+1;
          664  +      i = -1;
          665  +    }else{
          666  +      zIn[i+1] = cSaved;
          667  +    }
          668  +  }
          669  +}
          670  +
          671  +/*
          672  +** The main routine for "sql_normalize".  Read files named on the
          673  +** command-line and run the text of each through sqlite3_normalize().
          674  +*/
          675  +int main(int argc, char **argv){
          676  +  int i;
          677  +  FILE *in;
          678  +  char *zBuf = 0;
          679  +  sqlite3_int64 sz, got;
          680  +
          681  +  for(i=1; i<argc; i++){
          682  +    in = fopen(argv[i], "rb");
          683  +    if( in==0 ){
          684  +      fprintf(stderr, "cannot open \"%s\"\n", argv[i]);
          685  +      continue;
          686  +    }
          687  +    fseek(in, 0, SEEK_END);
          688  +    sz = ftell(in);
          689  +    rewind(in);
          690  +    zBuf = sqlite3_realloc64(zBuf, sz+1);
          691  +    if( zBuf==0 ){
          692  +      fprintf(stderr, "failed to malloc for %lld bytes\n", sz);
          693  +      exit(1);
          694  +    }
          695  +    got = fread(zBuf, 1, sz, in);
          696  +    fclose(in);
          697  +    if( got!=sz ){
          698  +      fprintf(stderr, "only able to read %lld of %lld bytes from \"%s\"\n",
          699  +              got, sz, argv[i]);
          700  +    }else{
          701  +      zBuf[got] = 0;
          702  +      normalizeFile(zBuf);
          703  +    }
          704  +  }
          705  +  sqlite3_free(zBuf);
          706  +}
          707  +#endif /* SQLITE_NORMALIZE_CLI */

Changes to ext/misc/scrub.c.

   127    127     if( p->rcErr ) return;
   128    128     iOff = (pgno-1)*(sqlite3_int64)p->szPage;
   129    129     rc = p->pDest->pMethods->xWrite(p->pDest, pData, p->szPage, iOff);
   130    130     if( rc!=SQLITE_OK ){
   131    131       scrubBackupErr(p, "write failed for page %d", pgno);
   132    132       p->rcErr = SQLITE_IOERR;
   133    133     }
   134         -  if( pgno>p->iLastPage ) p->iLastPage = pgno;
          134  +  if( (u32)pgno>p->iLastPage ) p->iLastPage = pgno;
   135    135   }
   136    136   
   137    137   /* Prepare a statement against the "db" database. */
   138    138   static sqlite3_stmt *scrubBackupPrepare(
   139    139     ScrubState *p,      /* Backup context */
   140    140     sqlite3 *db,        /* Database to prepare against */
   141    141     const char *zSql    /* SQL statement */
................................................................................
   455    455       if( aTop[0]==0x0d ){
   456    456         pc += scrubBackupVarintSize(&a[pc]);
   457    457         if( pc > (p->szUsable-4) ){ ln=__LINE__; goto btree_corrupt; }
   458    458       }
   459    459       nLocal = K<=X ? K : M;
   460    460       if( pc+nLocal > p->szUsable-4 ){ ln=__LINE__; goto btree_corrupt; }
   461    461       iChild = scrubBackupInt32(&a[pc+nLocal]);
   462         -    scrubBackupOverflow(p, iChild, P-nLocal);
          462  +    scrubBackupOverflow(p, iChild, (u32)(P-nLocal));
   463    463     }
   464    464   
   465    465     /* Walk the right-most tree */
   466    466     if( aTop[0]==0x05 || aTop[0]==0x02 ){
   467    467       iChild = scrubBackupInt32(&aTop[8]);
   468    468       scrubBackupBtree(p, iChild, iDepth+1);
   469    469     }

Changes to ext/misc/spellfix.c.

    14     14   ** to search a large vocabulary for close matches.  See separate
    15     15   ** documentation (http://www.sqlite.org/spellfix1.html) for details.
    16     16   */
    17     17   #include "sqlite3ext.h"
    18     18   SQLITE_EXTENSION_INIT1
    19     19   
    20     20   #ifndef SQLITE_AMALGAMATION
           21  +# if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
           22  +#  define NDEBUG 1
           23  +# endif
           24  +# if defined(NDEBUG) && defined(SQLITE_DEBUG)
           25  +#  undef NDEBUG
           26  +# endif
    21     27   # include <string.h>
    22     28   # include <stdio.h>
    23     29   # include <stdlib.h>
    24     30   # include <assert.h>
    25     31   # define ALWAYS(X)  1
    26     32   # define NEVER(X)   0
    27     33     typedef unsigned char u8;
................................................................................
   647    653     memset(p, 0, sizeof(*p));
   648    654   }
   649    655   static void editDist3ConfigDelete(void *pIn){
   650    656     EditDist3Config *p = (EditDist3Config*)pIn;
   651    657     editDist3ConfigClear(p);
   652    658     sqlite3_free(p);
   653    659   }
          660  +
          661  +/* Compare the FROM values of two EditDist3Cost objects, for sorting.
          662  +** Return negative, zero, or positive if the A is less than, equal to,
          663  +** or greater than B.
          664  +*/
          665  +static int editDist3CostCompare(EditDist3Cost *pA, EditDist3Cost *pB){
          666  +  int n = pA->nFrom;
          667  +  int rc;
          668  +  if( n>pB->nFrom ) n = pB->nFrom;
          669  +  rc = strncmp(pA->a, pB->a, n);
          670  +  if( rc==0 ) rc = pA->nFrom - pB->nFrom;
          671  +  return rc;
          672  +}
          673  +
          674  +/*
          675  +** Merge together two sorted lists of EditDist3Cost objects, in order
          676  +** of increasing FROM.
          677  +*/
          678  +static EditDist3Cost *editDist3CostMerge(
          679  +  EditDist3Cost *pA,
          680  +  EditDist3Cost *pB
          681  +){
          682  +  EditDist3Cost *pHead = 0;
          683  +  EditDist3Cost **ppTail = &pHead;
          684  +  EditDist3Cost *p;
          685  +  while( pA && pB ){
          686  +    if( editDist3CostCompare(pA,pB)<=0 ){
          687  +      p = pA;
          688  +      pA = pA->pNext;
          689  +    }else{
          690  +      p = pB;
          691  +      pB = pB->pNext;
          692  +    }
          693  +    *ppTail = p;
          694  +    ppTail =  &p->pNext;
          695  +  }
          696  +  if( pA ){
          697  +    *ppTail = pA;
          698  +  }else{
          699  +    *ppTail = pB;
          700  +  }
          701  +  return pHead;
          702  +}
          703  +
          704  +/*
          705  +** Sort a list of EditDist3Cost objects into order of increasing FROM
          706  +*/
          707  +static EditDist3Cost *editDist3CostSort(EditDist3Cost *pList){
          708  +  EditDist3Cost *ap[60], *p;
          709  +  int i;
          710  +  int mx = 0;
          711  +  ap[0] = 0;
          712  +  ap[1] = 0;
          713  +  while( pList ){
          714  +    p = pList;
          715  +    pList = p->pNext;
          716  +    p->pNext = 0;
          717  +    for(i=0; ap[i]; i++){
          718  +      p = editDist3CostMerge(ap[i],p);
          719  +      ap[i] = 0;
          720  +    }
          721  +    ap[i] = p;
          722  +    if( i>mx ){
          723  +      mx = i;
          724  +      ap[i+1] = 0;
          725  +    }
          726  +  }
          727  +  p = 0;
          728  +  for(i=0; i<=mx; i++){
          729  +    if( ap[i] ) p = editDist3CostMerge(p,ap[i]);
          730  +  }
          731  +  return p;
          732  +}
   654    733   
   655    734   /*
   656    735   ** Load all edit-distance weights from a table.
   657    736   */
   658    737   static int editDist3ConfigLoad(
   659    738     EditDist3Config *p,      /* The edit distance configuration to load */
   660    739     sqlite3 *db,            /* Load from this database */
................................................................................
   681    760       int nTo = zTo ? sqlite3_column_bytes(pStmt, 2) : 0;
   682    761       int iCost = sqlite3_column_int(pStmt, 3);
   683    762   
   684    763       assert( zFrom!=0 || nFrom==0 );
   685    764       assert( zTo!=0 || nTo==0 );
   686    765       if( nFrom>100 || nTo>100 ) continue;
   687    766       if( iCost<0 ) continue;
          767  +    if( iCost>10000 ) continue;   /* Costs above 10K are considered infinite */
   688    768       if( pLang==0 || iLang!=iLangPrev ){
   689    769         EditDist3Lang *pNew;
   690    770         pNew = sqlite3_realloc64(p->a, (p->nLang+1)*sizeof(p->a[0]));
   691    771         if( pNew==0 ){ rc = SQLITE_NOMEM; break; }
   692    772         p->a = pNew;
   693    773         pLang = &p->a[p->nLang];
   694    774         p->nLang++;
................................................................................
   718    798         memcpy(pCost->a + nFrom, zTo, nTo);
   719    799         pCost->pNext = pLang->pCost;
   720    800         pLang->pCost = pCost; 
   721    801       }
   722    802     }
   723    803     rc2 = sqlite3_finalize(pStmt);
   724    804     if( rc==SQLITE_OK ) rc = rc2;
          805  +  if( rc==SQLITE_OK ){
          806  +    int iLang;
          807  +    for(iLang=0; iLang<p->nLang; iLang++){
          808  +      p->a[iLang].pCost = editDist3CostSort(p->a[iLang].pCost);
          809  +    }
          810  +  }
   725    811     return rc;
   726    812   }
   727    813   
   728    814   /*
   729    815   ** Return the length (in bytes) of a utf-8 character.  Or return a maximum
   730    816   ** of N.
   731    817   */
................................................................................
   745    831   }
   746    832   
   747    833   /*
   748    834   ** Return TRUE (non-zero) if the To side of the given cost matches
   749    835   ** the given string.
   750    836   */
   751    837   static int matchTo(EditDist3Cost *p, const char *z, int n){
          838  +  if( p->a[p->nFrom]!=z[0] ) return 0;
   752    839     if( p->nTo>n ) return 0;
   753    840     if( strncmp(p->a+p->nFrom, z, p->nTo)!=0 ) return 0;
   754    841     return 1;
   755    842   }
   756    843   
   757    844   /*
   758    845   ** Return TRUE (non-zero) if the From side of the given cost matches
   759    846   ** the given string.
   760    847   */
   761    848   static int matchFrom(EditDist3Cost *p, const char *z, int n){
   762    849     assert( p->nFrom<=n );
          850  +  if( p->a[0]!=z[0] ) return 0;
   763    851     if( strncmp(p->a, z, p->nFrom)!=0 ) return 0;
   764    852     return 1;
   765    853   }
   766    854   
   767    855   /*
   768    856   ** Return TRUE (non-zero) of the next FROM character and the next TO
   769    857   ** character are the same.
................................................................................
   772    860     EditDist3FromString *pStr,  /* Left hand string */
   773    861     int n1,                     /* Index of comparison character on the left */
   774    862     const char *z2,             /* Right-handl comparison character */
   775    863     int n2                      /* Bytes remaining in z2[] */
   776    864   ){
   777    865     int b1 = pStr->a[n1].nByte;
   778    866     if( b1>n2 ) return 0;
   779         -  if( memcmp(pStr->z+n1, z2, b1)!=0 ) return 0;
          867  +  if( pStr->z[n1]!=z2[0] ) return 0;
          868  +  if( strncmp(pStr->z+n1, z2, b1)!=0 ) return 0;
   780    869     return 1;
   781    870   }
   782    871   
   783    872   /*
   784    873   ** Delete an EditDist3FromString objecct
   785    874   */
   786    875   static void editDist3FromStringDelete(EditDist3FromString *p){
................................................................................
   854    943     }
   855    944     return pStr;
   856    945   }
   857    946   
   858    947   /*
   859    948   ** Update entry m[i] such that it is the minimum of its current value
   860    949   ** and m[j]+iCost.
   861         -**
   862         -** If the iCost is 1,000,000 or greater, then consider the cost to be
   863         -** infinite and skip the update.
   864    950   */
   865    951   static void updateCost(
   866    952     unsigned int *m,
   867    953     int i,
   868    954     int j,
   869    955     int iCost
   870    956   ){
          957  +  unsigned int b;
   871    958     assert( iCost>=0 );
   872         -  if( iCost<10000 ){
   873         -    unsigned int b = m[j] + iCost;
   874         -    if( b<m[i] ) m[i] = b;
   875         -  }
          959  +  assert( iCost<10000 );
          960  +  b = m[j] + iCost;
          961  +  if( b<m[i] ) m[i] = b;
   876    962   }
   877    963   
   878    964   /*
   879    965   ** How much stack space (int bytes) to use for Wagner matrix in 
   880    966   ** editDist3Core().  If more space than this is required, the entire
   881    967   ** matrix is taken from the heap.  To reduce the load on the memory
   882    968   ** allocator, make this value as large as practical for the
................................................................................
   932   1018     memset(a2, 0, sizeof(a2[0])*n2);
   933   1019   
   934   1020     /* Fill in the a1[] matrix for all characters of the TO string */
   935   1021     for(i2=0; i2<n2; i2++){
   936   1022       a2[i2].nByte = utf8Len((unsigned char)z2[i2], n2-i2);
   937   1023       for(p=pLang->pCost; p; p=p->pNext){
   938   1024         EditDist3Cost **apNew;
   939         -      if( p->nFrom>0 ) continue;
         1025  +      if( p->nFrom>0 ) break;
   940   1026         if( i2+p->nTo>n2 ) continue;
         1027  +      if( p->a[0]>z2[i2] ) break;
   941   1028         if( matchTo(p, z2+i2, n2-i2)==0 ) continue;
   942   1029         a2[i2].nIns++;
   943   1030         apNew = sqlite3_realloc64(a2[i2].apIns, sizeof(*apNew)*a2[i2].nIns);
   944   1031         if( apNew==0 ){
   945   1032           res = -1;  /* Out of memory */
   946   1033           goto editDist3Abort;
   947   1034         }
................................................................................
  1196   1283       int sz;
  1197   1284       utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
  1198   1285       i += sz;
  1199   1286     }
  1200   1287     return nChar;
  1201   1288   }
  1202   1289   
         1290  +typedef struct Transliteration Transliteration;
         1291  +struct Transliteration {
         1292  + unsigned short int cFrom;
         1293  + unsigned char cTo0, cTo1, cTo2, cTo3;
         1294  +};
         1295  +
  1203   1296   /*
  1204   1297   ** Table of translations from unicode characters into ASCII.
  1205   1298   */
  1206         -static const struct {
  1207         - unsigned short int cFrom;
  1208         - unsigned char cTo0, cTo1;
  1209         -} translit[] = {
  1210         -  { 0x00A0,  0x20, 0x00 },  /*   to   */
  1211         -  { 0x00B5,  0x75, 0x00 },  /* µ to u */
  1212         -  { 0x00C0,  0x41, 0x00 },  /* À to A */
  1213         -  { 0x00C1,  0x41, 0x00 },  /* Á to A */
  1214         -  { 0x00C2,  0x41, 0x00 },  /* Â to A */
  1215         -  { 0x00C3,  0x41, 0x00 },  /* Ã to A */
  1216         -  { 0x00C4,  0x41, 0x65 },  /* Ä to Ae */
  1217         -  { 0x00C5,  0x41, 0x61 },  /* Å to Aa */
  1218         -  { 0x00C6,  0x41, 0x45 },  /* Æ to AE */
  1219         -  { 0x00C7,  0x43, 0x00 },  /* Ç to C */
  1220         -  { 0x00C8,  0x45, 0x00 },  /* È to E */
  1221         -  { 0x00C9,  0x45, 0x00 },  /* É to E */
  1222         -  { 0x00CA,  0x45, 0x00 },  /* Ê to E */
  1223         -  { 0x00CB,  0x45, 0x00 },  /* Ë to E */
  1224         -  { 0x00CC,  0x49, 0x00 },  /* Ì to I */
  1225         -  { 0x00CD,  0x49, 0x00 },  /* Í to I */
  1226         -  { 0x00CE,  0x49, 0x00 },  /* Î to I */
  1227         -  { 0x00CF,  0x49, 0x00 },  /* Ï to I */
  1228         -  { 0x00D0,  0x44, 0x00 },  /* Ð to D */
  1229         -  { 0x00D1,  0x4E, 0x00 },  /* Ñ to N */
  1230         -  { 0x00D2,  0x4F, 0x00 },  /* Ò to O */
  1231         -  { 0x00D3,  0x4F, 0x00 },  /* Ó to O */
  1232         -  { 0x00D4,  0x4F, 0x00 },  /* Ô to O */
  1233         -  { 0x00D5,  0x4F, 0x00 },  /* Õ to O */
  1234         -  { 0x00D6,  0x4F, 0x65 },  /* Ö to Oe */
  1235         -  { 0x00D7,  0x78, 0x00 },  /* × to x */
  1236         -  { 0x00D8,  0x4F, 0x00 },  /* Ø to O */
  1237         -  { 0x00D9,  0x55, 0x00 },  /* Ù to U */
  1238         -  { 0x00DA,  0x55, 0x00 },  /* Ú to U */
  1239         -  { 0x00DB,  0x55, 0x00 },  /* Û to U */
  1240         -  { 0x00DC,  0x55, 0x65 },  /* Ü to Ue */
  1241         -  { 0x00DD,  0x59, 0x00 },  /* Ý to Y */
  1242         -  { 0x00DE,  0x54, 0x68 },  /* Þ to Th */
  1243         -  { 0x00DF,  0x73, 0x73 },  /* ß to ss */
  1244         -  { 0x00E0,  0x61, 0x00 },  /* à to a */
  1245         -  { 0x00E1,  0x61, 0x00 },  /* á to a */
  1246         -  { 0x00E2,  0x61, 0x00 },  /* â to a */
  1247         -  { 0x00E3,  0x61, 0x00 },  /* ã to a */
  1248         -  { 0x00E4,  0x61, 0x65 },  /* ä to ae */
  1249         -  { 0x00E5,  0x61, 0x61 },  /* å to aa */
  1250         -  { 0x00E6,  0x61, 0x65 },  /* æ to ae */
  1251         -  { 0x00E7,  0x63, 0x00 },  /* ç to c */
  1252         -  { 0x00E8,  0x65, 0x00 },  /* è to e */
  1253         -  { 0x00E9,  0x65, 0x00 },  /* é to e */
  1254         -  { 0x00EA,  0x65, 0x00 },  /* ê to e */
  1255         -  { 0x00EB,  0x65, 0x00 },  /* ë to e */
  1256         -  { 0x00EC,  0x69, 0x00 },  /* ì to i */
  1257         -  { 0x00ED,  0x69, 0x00 },  /* í to i */
  1258         -  { 0x00EE,  0x69, 0x00 },  /* î to i */
  1259         -  { 0x00EF,  0x69, 0x00 },  /* ï to i */
  1260         -  { 0x00F0,  0x64, 0x00 },  /* ð to d */
  1261         -  { 0x00F1,  0x6E, 0x00 },  /* ñ to n */
  1262         -  { 0x00F2,  0x6F, 0x00 },  /* ò to o */
  1263         -  { 0x00F3,  0x6F, 0x00 },  /* ó to o */
  1264         -  { 0x00F4,  0x6F, 0x00 },  /* ô to o */
  1265         -  { 0x00F5,  0x6F, 0x00 },  /* õ to o */
  1266         -  { 0x00F6,  0x6F, 0x65 },  /* ö to oe */
  1267         -  { 0x00F7,  0x3A, 0x00 },  /* ÷ to : */
  1268         -  { 0x00F8,  0x6F, 0x00 },  /* ø to o */
  1269         -  { 0x00F9,  0x75, 0x00 },  /* ù to u */
  1270         -  { 0x00FA,  0x75, 0x00 },  /* ú to u */
  1271         -  { 0x00FB,  0x75, 0x00 },  /* û to u */
  1272         -  { 0x00FC,  0x75, 0x65 },  /* ü to ue */
  1273         -  { 0x00FD,  0x79, 0x00 },  /* ý to y */
  1274         -  { 0x00FE,  0x74, 0x68 },  /* þ to th */
  1275         -  { 0x00FF,  0x79, 0x00 },  /* ÿ to y */
  1276         -  { 0x0100,  0x41, 0x00 },  /* Ā to A */
  1277         -  { 0x0101,  0x61, 0x00 },  /* ā to a */
  1278         -  { 0x0102,  0x41, 0x00 },  /* Ă to A */
  1279         -  { 0x0103,  0x61, 0x00 },  /* ă to a */
  1280         -  { 0x0104,  0x41, 0x00 },  /* Ą to A */
  1281         -  { 0x0105,  0x61, 0x00 },  /* ą to a */
  1282         -  { 0x0106,  0x43, 0x00 },  /* Ć to C */
  1283         -  { 0x0107,  0x63, 0x00 },  /* ć to c */
  1284         -  { 0x0108,  0x43, 0x68 },  /* Ĉ to Ch */
  1285         -  { 0x0109,  0x63, 0x68 },  /* ĉ to ch */
  1286         -  { 0x010A,  0x43, 0x00 },  /* Ċ to C */
  1287         -  { 0x010B,  0x63, 0x00 },  /* ċ to c */
  1288         -  { 0x010C,  0x43, 0x00 },  /* Č to C */
  1289         -  { 0x010D,  0x63, 0x00 },  /* č to c */
  1290         -  { 0x010E,  0x44, 0x00 },  /* Ď to D */
  1291         -  { 0x010F,  0x64, 0x00 },  /* ď to d */
  1292         -  { 0x0110,  0x44, 0x00 },  /* Đ to D */
  1293         -  { 0x0111,  0x64, 0x00 },  /* đ to d */
  1294         -  { 0x0112,  0x45, 0x00 },  /* Ē to E */
  1295         -  { 0x0113,  0x65, 0x00 },  /* ē to e */
  1296         -  { 0x0114,  0x45, 0x00 },  /* Ĕ to E */
  1297         -  { 0x0115,  0x65, 0x00 },  /* ĕ to e */
  1298         -  { 0x0116,  0x45, 0x00 },  /* Ė to E */
  1299         -  { 0x0117,  0x65, 0x00 },  /* ė to e */
  1300         -  { 0x0118,  0x45, 0x00 },  /* Ę to E */
  1301         -  { 0x0119,  0x65, 0x00 },  /* ę to e */
  1302         -  { 0x011A,  0x45, 0x00 },  /* Ě to E */
  1303         -  { 0x011B,  0x65, 0x00 },  /* ě to e */
  1304         -  { 0x011C,  0x47, 0x68 },  /* Ĝ to Gh */
  1305         -  { 0x011D,  0x67, 0x68 },  /* ĝ to gh */
  1306         -  { 0x011E,  0x47, 0x00 },  /* Ğ to G */
  1307         -  { 0x011F,  0x67, 0x00 },  /* ğ to g */
  1308         -  { 0x0120,  0x47, 0x00 },  /* Ġ to G */
  1309         -  { 0x0121,  0x67, 0x00 },  /* ġ to g */
  1310         -  { 0x0122,  0x47, 0x00 },  /* Ģ to G */
  1311         -  { 0x0123,  0x67, 0x00 },  /* ģ to g */
  1312         -  { 0x0124,  0x48, 0x68 },  /* Ĥ to Hh */
  1313         -  { 0x0125,  0x68, 0x68 },  /* ĥ to hh */
  1314         -  { 0x0126,  0x48, 0x00 },  /* Ħ to H */
  1315         -  { 0x0127,  0x68, 0x00 },  /* ħ to h */
  1316         -  { 0x0128,  0x49, 0x00 },  /* Ĩ to I */
  1317         -  { 0x0129,  0x69, 0x00 },  /* ĩ to i */
  1318         -  { 0x012A,  0x49, 0x00 },  /* Ī to I */
  1319         -  { 0x012B,  0x69, 0x00 },  /* ī to i */
  1320         -  { 0x012C,  0x49, 0x00 },  /* Ĭ to I */
  1321         -  { 0x012D,  0x69, 0x00 },  /* ĭ to i */
  1322         -  { 0x012E,  0x49, 0x00 },  /* Į to I */
  1323         -  { 0x012F,  0x69, 0x00 },  /* į to i */
  1324         -  { 0x0130,  0x49, 0x00 },  /* İ to I */
  1325         -  { 0x0131,  0x69, 0x00 },  /* ı to i */
  1326         -  { 0x0132,  0x49, 0x4A },  /* IJ to IJ */
  1327         -  { 0x0133,  0x69, 0x6A },  /* ij to ij */
  1328         -  { 0x0134,  0x4A, 0x68 },  /* Ĵ to Jh */
  1329         -  { 0x0135,  0x6A, 0x68 },  /* ĵ to jh */
  1330         -  { 0x0136,  0x4B, 0x00 },  /* Ķ to K */
  1331         -  { 0x0137,  0x6B, 0x00 },  /* ķ to k */
  1332         -  { 0x0138,  0x6B, 0x00 },  /* ĸ to k */
  1333         -  { 0x0139,  0x4C, 0x00 },  /* Ĺ to L */
  1334         -  { 0x013A,  0x6C, 0x00 },  /* ĺ to l */
  1335         -  { 0x013B,  0x4C, 0x00 },  /* Ļ to L */
  1336         -  { 0x013C,  0x6C, 0x00 },  /* ļ to l */
  1337         -  { 0x013D,  0x4C, 0x00 },  /* Ľ to L */
  1338         -  { 0x013E,  0x6C, 0x00 },  /* ľ to l */
  1339         -  { 0x013F,  0x4C, 0x2E },  /* Ŀ to L. */
  1340         -  { 0x0140,  0x6C, 0x2E },  /* ŀ to l. */
  1341         -  { 0x0141,  0x4C, 0x00 },  /* Ł to L */
  1342         -  { 0x0142,  0x6C, 0x00 },  /* ł to l */
  1343         -  { 0x0143,  0x4E, 0x00 },  /* Ń to N */
  1344         -  { 0x0144,  0x6E, 0x00 },  /* ń to n */
  1345         -  { 0x0145,  0x4E, 0x00 },  /* Ņ to N */
  1346         -  { 0x0146,  0x6E, 0x00 },  /* ņ to n */
  1347         -  { 0x0147,  0x4E, 0x00 },  /* Ň to N */
  1348         -  { 0x0148,  0x6E, 0x00 },  /* ň to n */
  1349         -  { 0x0149,  0x27, 0x6E },  /* ʼn to 'n */
  1350         -  { 0x014A,  0x4E, 0x47 },  /* Ŋ to NG */
  1351         -  { 0x014B,  0x6E, 0x67 },  /* ŋ to ng */
  1352         -  { 0x014C,  0x4F, 0x00 },  /* Ō to O */
  1353         -  { 0x014D,  0x6F, 0x00 },  /* ō to o */
  1354         -  { 0x014E,  0x4F, 0x00 },  /* Ŏ to O */
  1355         -  { 0x014F,  0x6F, 0x00 },  /* ŏ to o */
  1356         -  { 0x0150,  0x4F, 0x00 },  /* Ő to O */
  1357         -  { 0x0151,  0x6F, 0x00 },  /* ő to o */
  1358         -  { 0x0152,  0x4F, 0x45 },  /* Πto OE */
  1359         -  { 0x0153,  0x6F, 0x65 },  /* œ to oe */
  1360         -  { 0x0154,  0x52, 0x00 },  /* Ŕ to R */
  1361         -  { 0x0155,  0x72, 0x00 },  /* ŕ to r */
  1362         -  { 0x0156,  0x52, 0x00 },  /* Ŗ to R */
  1363         -  { 0x0157,  0x72, 0x00 },  /* ŗ to r */
  1364         -  { 0x0158,  0x52, 0x00 },  /* Ř to R */
  1365         -  { 0x0159,  0x72, 0x00 },  /* ř to r */
  1366         -  { 0x015A,  0x53, 0x00 },  /* Ś to S */
  1367         -  { 0x015B,  0x73, 0x00 },  /* ś to s */
  1368         -  { 0x015C,  0x53, 0x68 },  /* Ŝ to Sh */
  1369         -  { 0x015D,  0x73, 0x68 },  /* ŝ to sh */
  1370         -  { 0x015E,  0x53, 0x00 },  /* Ş to S */
  1371         -  { 0x015F,  0x73, 0x00 },  /* ş to s */
  1372         -  { 0x0160,  0x53, 0x00 },  /* Š to S */
  1373         -  { 0x0161,  0x73, 0x00 },  /* š to s */
  1374         -  { 0x0162,  0x54, 0x00 },  /* Ţ to T */
  1375         -  { 0x0163,  0x74, 0x00 },  /* ţ to t */
  1376         -  { 0x0164,  0x54, 0x00 },  /* Ť to T */
  1377         -  { 0x0165,  0x74, 0x00 },  /* ť to t */
  1378         -  { 0x0166,  0x54, 0x00 },  /* Ŧ to T */
  1379         -  { 0x0167,  0x74, 0x00 },  /* ŧ to t */
  1380         -  { 0x0168,  0x55, 0x00 },  /* Ũ to U */
  1381         -  { 0x0169,  0x75, 0x00 },  /* ũ to u */
  1382         -  { 0x016A,  0x55, 0x00 },  /* Ū to U */
  1383         -  { 0x016B,  0x75, 0x00 },  /* ū to u */
  1384         -  { 0x016C,  0x55, 0x00 },  /* Ŭ to U */
  1385         -  { 0x016D,  0x75, 0x00 },  /* ŭ to u */
  1386         -  { 0x016E,  0x55, 0x00 },  /* Ů to U */
  1387         -  { 0x016F,  0x75, 0x00 },  /* ů to u */
  1388         -  { 0x0170,  0x55, 0x00 },  /* Ű to U */
  1389         -  { 0x0171,  0x75, 0x00 },  /* ű to u */
  1390         -  { 0x0172,  0x55, 0x00 },  /* Ų to U */
  1391         -  { 0x0173,  0x75, 0x00 },  /* ų to u */
  1392         -  { 0x0174,  0x57, 0x00 },  /* Ŵ to W */
  1393         -  { 0x0175,  0x77, 0x00 },  /* ŵ to w */
  1394         -  { 0x0176,  0x59, 0x00 },  /* Ŷ to Y */
  1395         -  { 0x0177,  0x79, 0x00 },  /* ŷ to y */
  1396         -  { 0x0178,  0x59, 0x00 },  /* Ÿ to Y */
  1397         -  { 0x0179,  0x5A, 0x00 },  /* Ź to Z */
  1398         -  { 0x017A,  0x7A, 0x00 },  /* ź to z */
  1399         -  { 0x017B,  0x5A, 0x00 },  /* Ż to Z */
  1400         -  { 0x017C,  0x7A, 0x00 },  /* ż to z */
  1401         -  { 0x017D,  0x5A, 0x00 },  /* Ž to Z */
  1402         -  { 0x017E,  0x7A, 0x00 },  /* ž to z */
  1403         -  { 0x017F,  0x73, 0x00 },  /* ſ to s */
  1404         -  { 0x0192,  0x66, 0x00 },  /* ƒ to f */
  1405         -  { 0x0218,  0x53, 0x00 },  /* Ș to S */
  1406         -  { 0x0219,  0x73, 0x00 },  /* ș to s */
  1407         -  { 0x021A,  0x54, 0x00 },  /* Ț to T */
  1408         -  { 0x021B,  0x74, 0x00 },  /* ț to t */
  1409         -  { 0x0386,  0x41, 0x00 },  /* Ά to A */
  1410         -  { 0x0388,  0x45, 0x00 },  /* Έ to E */
  1411         -  { 0x0389,  0x49, 0x00 },  /* Ή to I */
  1412         -  { 0x038A,  0x49, 0x00 },  /* Ί to I */
  1413         -  { 0x038C,  0x4f, 0x00 },  /* Ό to O */
  1414         -  { 0x038E,  0x59, 0x00 },  /* Ύ to Y */
  1415         -  { 0x038F,  0x4f, 0x00 },  /* Ώ to O */
  1416         -  { 0x0390,  0x69, 0x00 },  /* ΐ to i */
  1417         -  { 0x0391,  0x41, 0x00 },  /* Α to A */
  1418         -  { 0x0392,  0x42, 0x00 },  /* Β to B */
  1419         -  { 0x0393,  0x47, 0x00 },  /* Γ to G */
  1420         -  { 0x0394,  0x44, 0x00 },  /* Δ to D */
  1421         -  { 0x0395,  0x45, 0x00 },  /* Ε to E */
  1422         -  { 0x0396,  0x5a, 0x00 },  /* Ζ to Z */
  1423         -  { 0x0397,  0x49, 0x00 },  /* Η to I */
  1424         -  { 0x0398,  0x54, 0x68 },  /* Θ to Th */
  1425         -  { 0x0399,  0x49, 0x00 },  /* Ι to I */
  1426         -  { 0x039A,  0x4b, 0x00 },  /* Κ to K */
  1427         -  { 0x039B,  0x4c, 0x00 },  /* Λ to L */
  1428         -  { 0x039C,  0x4d, 0x00 },  /* Μ to M */
  1429         -  { 0x039D,  0x4e, 0x00 },  /* Ν to N */
  1430         -  { 0x039E,  0x58, 0x00 },  /* Ξ to X */
  1431         -  { 0x039F,  0x4f, 0x00 },  /* Ο to O */
  1432         -  { 0x03A0,  0x50, 0x00 },  /* Π to P */
  1433         -  { 0x03A1,  0x52, 0x00 },  /* Ρ to R */
  1434         -  { 0x03A3,  0x53, 0x00 },  /* Σ to S */
  1435         -  { 0x03A4,  0x54, 0x00 },  /* Τ to T */
  1436         -  { 0x03A5,  0x59, 0x00 },  /* Υ to Y */
  1437         -  { 0x03A6,  0x46, 0x00 },  /* Φ to F */
  1438         -  { 0x03A7,  0x43, 0x68 },  /* Χ to Ch */
  1439         -  { 0x03A8,  0x50, 0x73 },  /* Ψ to Ps */
  1440         -  { 0x03A9,  0x4f, 0x00 },  /* Ω to O */
  1441         -  { 0x03AA,  0x49, 0x00 },  /* Ϊ to I */
  1442         -  { 0x03AB,  0x59, 0x00 },  /* Ϋ to Y */
  1443         -  { 0x03AC,  0x61, 0x00 },  /* ά to a */
  1444         -  { 0x03AD,  0x65, 0x00 },  /* έ to e */
  1445         -  { 0x03AE,  0x69, 0x00 },  /* ή to i */
  1446         -  { 0x03AF,  0x69, 0x00 },  /* ί to i */
  1447         -  { 0x03B1,  0x61, 0x00 },  /* α to a */
  1448         -  { 0x03B2,  0x62, 0x00 },  /* β to b */
  1449         -  { 0x03B3,  0x67, 0x00 },  /* γ to g */
  1450         -  { 0x03B4,  0x64, 0x00 },  /* δ to d */
  1451         -  { 0x03B5,  0x65, 0x00 },  /* ε to e */
  1452         -  { 0x03B6,  0x7a, 0x00 },  /* ζ to z */
  1453         -  { 0x03B7,  0x69, 0x00 },  /* η to i */
  1454         -  { 0x03B8,  0x74, 0x68 },  /* θ to th */
  1455         -  { 0x03B9,  0x69, 0x00 },  /* ι to i */
  1456         -  { 0x03BA,  0x6b, 0x00 },  /* κ to k */
  1457         -  { 0x03BB,  0x6c, 0x00 },  /* λ to l */
  1458         -  { 0x03BC,  0x6d, 0x00 },  /* μ to m */
  1459         -  { 0x03BD,  0x6e, 0x00 },  /* ν to n */
  1460         -  { 0x03BE,  0x78, 0x00 },  /* ξ to x */
  1461         -  { 0x03BF,  0x6f, 0x00 },  /* ο to o */
  1462         -  { 0x03C0,  0x70, 0x00 },  /* π to p */
  1463         -  { 0x03C1,  0x72, 0x00 },  /* ρ to r */
  1464         -  { 0x03C3,  0x73, 0x00 },  /* σ to s */
  1465         -  { 0x03C4,  0x74, 0x00 },  /* τ to t */
  1466         -  { 0x03C5,  0x79, 0x00 },  /* υ to y */
  1467         -  { 0x03C6,  0x66, 0x00 },  /* φ to f */
  1468         -  { 0x03C7,  0x63, 0x68 },  /* χ to ch */
  1469         -  { 0x03C8,  0x70, 0x73 },  /* ψ to ps */
  1470         -  { 0x03C9,  0x6f, 0x00 },  /* ω to o */
  1471         -  { 0x03CA,  0x69, 0x00 },  /* ϊ to i */
  1472         -  { 0x03CB,  0x79, 0x00 },  /* ϋ to y */
  1473         -  { 0x03CC,  0x6f, 0x00 },  /* ό to o */
  1474         -  { 0x03CD,  0x79, 0x00 },  /* ύ to y */
  1475         -  { 0x03CE,  0x69, 0x00 },  /* ώ to i */
  1476         -  { 0x0400,  0x45, 0x00 },  /* Ѐ to E */
  1477         -  { 0x0401,  0x45, 0x00 },  /* Ё to E */
  1478         -  { 0x0402,  0x44, 0x00 },  /* Ђ to D */
  1479         -  { 0x0403,  0x47, 0x00 },  /* Ѓ to G */
  1480         -  { 0x0404,  0x45, 0x00 },  /* Є to E */
  1481         -  { 0x0405,  0x5a, 0x00 },  /* Ѕ to Z */
  1482         -  { 0x0406,  0x49, 0x00 },  /* І to I */
  1483         -  { 0x0407,  0x49, 0x00 },  /* Ї to I */
  1484         -  { 0x0408,  0x4a, 0x00 },  /* Ј to J */
  1485         -  { 0x0409,  0x49, 0x00 },  /* Љ to I */
  1486         -  { 0x040A,  0x4e, 0x00 },  /* Њ to N */
  1487         -  { 0x040B,  0x44, 0x00 },  /* Ћ to D */
  1488         -  { 0x040C,  0x4b, 0x00 },  /* Ќ to K */
  1489         -  { 0x040D,  0x49, 0x00 },  /* Ѝ to I */
  1490         -  { 0x040E,  0x55, 0x00 },  /* Ў to U */
  1491         -  { 0x040F,  0x44, 0x00 },  /* Џ to D */
  1492         -  { 0x0410,  0x41, 0x00 },  /* А to A */
  1493         -  { 0x0411,  0x42, 0x00 },  /* Б to B */
  1494         -  { 0x0412,  0x56, 0x00 },  /* В to V */
  1495         -  { 0x0413,  0x47, 0x00 },  /* Г to G */
  1496         -  { 0x0414,  0x44, 0x00 },  /* Д to D */
  1497         -  { 0x0415,  0x45, 0x00 },  /* Е to E */
  1498         -  { 0x0416,  0x5a, 0x68 },  /* Ж to Zh */
  1499         -  { 0x0417,  0x5a, 0x00 },  /* З to Z */
  1500         -  { 0x0418,  0x49, 0x00 },  /* И to I */
  1501         -  { 0x0419,  0x49, 0x00 },  /* Й to I */
  1502         -  { 0x041A,  0x4b, 0x00 },  /* К to K */
  1503         -  { 0x041B,  0x4c, 0x00 },  /* Л to L */
  1504         -  { 0x041C,  0x4d, 0x00 },  /* М to M */
  1505         -  { 0x041D,  0x4e, 0x00 },  /* Н to N */
  1506         -  { 0x041E,  0x4f, 0x00 },  /* О to O */
  1507         -  { 0x041F,  0x50, 0x00 },  /* П to P */
  1508         -  { 0x0420,  0x52, 0x00 },  /* Р to R */
  1509         -  { 0x0421,  0x53, 0x00 },  /* С to S */
  1510         -  { 0x0422,  0x54, 0x00 },  /* Т to T */
  1511         -  { 0x0423,  0x55, 0x00 },  /* У to U */
  1512         -  { 0x0424,  0x46, 0x00 },  /* Ф to F */
  1513         -  { 0x0425,  0x4b, 0x68 },  /* Х to Kh */
  1514         -  { 0x0426,  0x54, 0x63 },  /* Ц to Tc */
  1515         -  { 0x0427,  0x43, 0x68 },  /* Ч to Ch */
  1516         -  { 0x0428,  0x53, 0x68 },  /* Ш to Sh */
  1517         -  { 0x0429,  0x53, 0x68 },  /* Щ to Shch */
  1518         -  { 0x042A,  0x61, 0x00 },  /*  to A */
  1519         -  { 0x042B,  0x59, 0x00 },  /* Ы to Y */
  1520         -  { 0x042C,  0x59, 0x00 },  /*  to Y */
  1521         -  { 0x042D,  0x45, 0x00 },  /* Э to E */
  1522         -  { 0x042E,  0x49, 0x75 },  /* Ю to Iu */
  1523         -  { 0x042F,  0x49, 0x61 },  /* Я to Ia */
  1524         -  { 0x0430,  0x61, 0x00 },  /* а to a */
  1525         -  { 0x0431,  0x62, 0x00 },  /* б to b */
  1526         -  { 0x0432,  0x76, 0x00 },  /* в to v */
  1527         -  { 0x0433,  0x67, 0x00 },  /* г to g */
  1528         -  { 0x0434,  0x64, 0x00 },  /* д to d */
  1529         -  { 0x0435,  0x65, 0x00 },  /* е to e */
  1530         -  { 0x0436,  0x7a, 0x68 },  /* ж to zh */
  1531         -  { 0x0437,  0x7a, 0x00 },  /* з to z */
  1532         -  { 0x0438,  0x69, 0x00 },  /* и to i */
  1533         -  { 0x0439,  0x69, 0x00 },  /* й to i */
  1534         -  { 0x043A,  0x6b, 0x00 },  /* к to k */
  1535         -  { 0x043B,  0x6c, 0x00 },  /* л to l */
  1536         -  { 0x043C,  0x6d, 0x00 },  /* м to m */
  1537         -  { 0x043D,  0x6e, 0x00 },  /* н to n */
  1538         -  { 0x043E,  0x6f, 0x00 },  /* о to o */
  1539         -  { 0x043F,  0x70, 0x00 },  /* п to p */
  1540         -  { 0x0440,  0x72, 0x00 },  /* р to r */
  1541         -  { 0x0441,  0x73, 0x00 },  /* с to s */
  1542         -  { 0x0442,  0x74, 0x00 },  /* т to t */
  1543         -  { 0x0443,  0x75, 0x00 },  /* у to u */
  1544         -  { 0x0444,  0x66, 0x00 },  /* ф to f */
  1545         -  { 0x0445,  0x6b, 0x68 },  /* х to kh */
  1546         -  { 0x0446,  0x74, 0x63 },  /* ц to tc */
  1547         -  { 0x0447,  0x63, 0x68 },  /* ч to ch */
  1548         -  { 0x0448,  0x73, 0x68 },  /* ш to sh */
  1549         -  { 0x0449,  0x73, 0x68 },  /* щ to shch */
  1550         -  { 0x044A,  0x61, 0x00 },  /*  to a */
  1551         -  { 0x044B,  0x79, 0x00 },  /* ы to y */
  1552         -  { 0x044C,  0x79, 0x00 },  /*  to y */
  1553         -  { 0x044D,  0x65, 0x00 },  /* э to e */
  1554         -  { 0x044E,  0x69, 0x75 },  /* ю to iu */
  1555         -  { 0x044F,  0x69, 0x61 },  /* я to ia */
  1556         -  { 0x0450,  0x65, 0x00 },  /* ѐ to e */
  1557         -  { 0x0451,  0x65, 0x00 },  /* ё to e */
  1558         -  { 0x0452,  0x64, 0x00 },  /* ђ to d */
  1559         -  { 0x0453,  0x67, 0x00 },  /* ѓ to g */
  1560         -  { 0x0454,  0x65, 0x00 },  /* є to e */
  1561         -  { 0x0455,  0x7a, 0x00 },  /* ѕ to z */
  1562         -  { 0x0456,  0x69, 0x00 },  /* і to i */
  1563         -  { 0x0457,  0x69, 0x00 },  /* ї to i */
  1564         -  { 0x0458,  0x6a, 0x00 },  /* ј to j */
  1565         -  { 0x0459,  0x69, 0x00 },  /* љ to i */
  1566         -  { 0x045A,  0x6e, 0x00 },  /* њ to n */
  1567         -  { 0x045B,  0x64, 0x00 },  /* ћ to d */
  1568         -  { 0x045C,  0x6b, 0x00 },  /* ќ to k */
  1569         -  { 0x045D,  0x69, 0x00 },  /* ѝ to i */
  1570         -  { 0x045E,  0x75, 0x00 },  /* ў to u */
  1571         -  { 0x045F,  0x64, 0x00 },  /* џ to d */
  1572         -  { 0x1E02,  0x42, 0x00 },  /* Ḃ to B */
  1573         -  { 0x1E03,  0x62, 0x00 },  /* ḃ to b */
  1574         -  { 0x1E0A,  0x44, 0x00 },  /* Ḋ to D */
  1575         -  { 0x1E0B,  0x64, 0x00 },  /* ḋ to d */
  1576         -  { 0x1E1E,  0x46, 0x00 },  /* Ḟ to F */
  1577         -  { 0x1E1F,  0x66, 0x00 },  /* ḟ to f */
  1578         -  { 0x1E40,  0x4D, 0x00 },  /* Ṁ to M */
  1579         -  { 0x1E41,  0x6D, 0x00 },  /* ṁ to m */
  1580         -  { 0x1E56,  0x50, 0x00 },  /* Ṗ to P */
  1581         -  { 0x1E57,  0x70, 0x00 },  /* ṗ to p */
  1582         -  { 0x1E60,  0x53, 0x00 },  /* Ṡ to S */
  1583         -  { 0x1E61,  0x73, 0x00 },  /* ṡ to s */
  1584         -  { 0x1E6A,  0x54, 0x00 },  /* Ṫ to T */
  1585         -  { 0x1E6B,  0x74, 0x00 },  /* ṫ to t */
  1586         -  { 0x1E80,  0x57, 0x00 },  /* Ẁ to W */
  1587         -  { 0x1E81,  0x77, 0x00 },  /* ẁ to w */
  1588         -  { 0x1E82,  0x57, 0x00 },  /* Ẃ to W */
  1589         -  { 0x1E83,  0x77, 0x00 },  /* ẃ to w */
  1590         -  { 0x1E84,  0x57, 0x00 },  /* Ẅ to W */
  1591         -  { 0x1E85,  0x77, 0x00 },  /* ẅ to w */
  1592         -  { 0x1EF2,  0x59, 0x00 },  /* Ỳ to Y */
  1593         -  { 0x1EF3,  0x79, 0x00 },  /* ỳ to y */
  1594         -  { 0xFB00,  0x66, 0x66 },  /* ff to ff */
  1595         -  { 0xFB01,  0x66, 0x69 },  /* fi to fi */
  1596         -  { 0xFB02,  0x66, 0x6C },  /* fl to fl */
  1597         -  { 0xFB05,  0x73, 0x74 },  /* ſt to st */
  1598         -  { 0xFB06,  0x73, 0x74 },  /* st to st */
         1299  +static const Transliteration translit[] = {
         1300  +  { 0x00A0,  0x20, 0x00, 0x00, 0x00 },  /*   to   */
         1301  +  { 0x00B5,  0x75, 0x00, 0x00, 0x00 },  /* µ to u */
         1302  +  { 0x00C0,  0x41, 0x00, 0x00, 0x00 },  /* À to A */
         1303  +  { 0x00C1,  0x41, 0x00, 0x00, 0x00 },  /* Á to A */
         1304  +  { 0x00C2,  0x41, 0x00, 0x00, 0x00 },  /* Â to A */
         1305  +  { 0x00C3,  0x41, 0x00, 0x00, 0x00 },  /* Ã to A */
         1306  +  { 0x00C4,  0x41, 0x65, 0x00, 0x00 },  /* Ä to Ae */
         1307  +  { 0x00C5,  0x41, 0x61, 0x00, 0x00 },  /* Å to Aa */
         1308  +  { 0x00C6,  0x41, 0x45, 0x00, 0x00 },  /* Æ to AE */
         1309  +  { 0x00C7,  0x43, 0x00, 0x00, 0x00 },  /* Ç to C */
         1310  +  { 0x00C8,  0x45, 0x00, 0x00, 0x00 },  /* È to E */
         1311  +  { 0x00C9,  0x45, 0x00, 0x00, 0x00 },  /* É to E */
         1312  +  { 0x00CA,  0x45, 0x00, 0x00, 0x00 },  /* Ê to E */
         1313  +  { 0x00CB,  0x45, 0x00, 0x00, 0x00 },  /* Ë to E */
         1314  +  { 0x00CC,  0x49, 0x00, 0x00, 0x00 },  /* Ì to I */
         1315  +  { 0x00CD,  0x49, 0x00, 0x00, 0x00 },  /* Í to I */
         1316  +  { 0x00CE,  0x49, 0x00, 0x00, 0x00 },  /* Î to I */
         1317  +  { 0x00CF,  0x49, 0x00, 0x00, 0x00 },  /* Ï to I */
         1318  +  { 0x00D0,  0x44, 0x00, 0x00, 0x00 },  /* Ð to D */
         1319  +  { 0x00D1,  0x4E, 0x00, 0x00, 0x00 },  /* Ñ to N */
         1320  +  { 0x00D2,  0x4F, 0x00, 0x00, 0x00 },  /* Ò to O */
         1321  +  { 0x00D3,  0x4F, 0x00, 0x00, 0x00 },  /* Ó to O */
         1322  +  { 0x00D4,  0x4F, 0x00, 0x00, 0x00 },  /* Ô to O */
         1323  +  { 0x00D5,  0x4F, 0x00, 0x00, 0x00 },  /* Õ to O */
         1324  +  { 0x00D6,  0x4F, 0x65, 0x00, 0x00 },  /* Ö to Oe */
         1325  +  { 0x00D7,  0x78, 0x00, 0x00, 0x00 },  /* × to x */
         1326  +  { 0x00D8,  0x4F, 0x00, 0x00, 0x00 },  /* Ø to O */
         1327  +  { 0x00D9,  0x55, 0x00, 0x00, 0x00 },  /* Ù to U */
         1328  +  { 0x00DA,  0x55, 0x00, 0x00, 0x00 },  /* Ú to U */
         1329  +  { 0x00DB,  0x55, 0x00, 0x00, 0x00 },  /* Û to U */
         1330  +  { 0x00DC,  0x55, 0x65, 0x00, 0x00 },  /* Ü to Ue */
         1331  +  { 0x00DD,  0x59, 0x00, 0x00, 0x00 },  /* Ý to Y */
         1332  +  { 0x00DE,  0x54, 0x68, 0x00, 0x00 },  /* Þ to Th */
         1333  +  { 0x00DF,  0x73, 0x73, 0x00, 0x00 },  /* ß to ss */
         1334  +  { 0x00E0,  0x61, 0x00, 0x00, 0x00 },  /* à to a */
         1335  +  { 0x00E1,  0x61, 0x00, 0x00, 0x00 },  /* á to a */
         1336  +  { 0x00E2,  0x61, 0x00, 0x00, 0x00 },  /* â to a */
         1337  +  { 0x00E3,  0x61, 0x00, 0x00, 0x00 },  /* ã to a */
         1338  +  { 0x00E4,  0x61, 0x65, 0x00, 0x00 },  /* ä to ae */
         1339  +  { 0x00E5,  0x61, 0x61, 0x00, 0x00 },  /* å to aa */
         1340  +  { 0x00E6,  0x61, 0x65, 0x00, 0x00 },  /* æ to ae */
         1341  +  { 0x00E7,  0x63, 0x00, 0x00, 0x00 },  /* ç to c */
         1342  +  { 0x00E8,  0x65, 0x00, 0x00, 0x00 },  /* è to e */
         1343  +  { 0x00E9,  0x65, 0x00, 0x00, 0x00 },  /* é to e */
         1344  +  { 0x00EA,  0x65, 0x00, 0x00, 0x00 },  /* ê to e */
         1345  +  { 0x00EB,  0x65, 0x00, 0x00, 0x00 },  /* ë to e */
         1346  +  { 0x00EC,  0x69, 0x00, 0x00, 0x00 },  /* ì to i */
         1347  +  { 0x00ED,  0x69, 0x00, 0x00, 0x00 },  /* í to i */
         1348  +  { 0x00EE,  0x69, 0x00, 0x00, 0x00 },  /* î to i */
         1349  +  { 0x00EF,  0x69, 0x00, 0x00, 0x00 },  /* ï to i */
         1350  +  { 0x00F0,  0x64, 0x00, 0x00, 0x00 },  /* ð to d */
         1351  +  { 0x00F1,  0x6E, 0x00, 0x00, 0x00 },  /* ñ to n */
         1352  +  { 0x00F2,  0x6F, 0x00, 0x00, 0x00 },  /* ò to o */
         1353  +  { 0x00F3,  0x6F, 0x00, 0x00, 0x00 },  /* ó to o */
         1354  +  { 0x00F4,  0x6F, 0x00, 0x00, 0x00 },  /* ô to o */
         1355  +  { 0x00F5,  0x6F, 0x00, 0x00, 0x00 },  /* õ to o */
         1356  +  { 0x00F6,  0x6F, 0x65, 0x00, 0x00 },  /* ö to oe */
         1357  +  { 0x00F7,  0x3A, 0x00, 0x00, 0x00 },  /* ÷ to : */
         1358  +  { 0x00F8,  0x6F, 0x00, 0x00, 0x00 },  /* ø to o */
         1359  +  { 0x00F9,  0x75, 0x00, 0x00, 0x00 },  /* ù to u */
         1360  +  { 0x00FA,  0x75, 0x00, 0x00, 0x00 },  /* ú to u */
         1361  +  { 0x00FB,  0x75, 0x00, 0x00, 0x00 },  /* û to u */
         1362  +  { 0x00FC,  0x75, 0x65, 0x00, 0x00 },  /* ü to ue */
         1363  +  { 0x00FD,  0x79, 0x00, 0x00, 0x00 },  /* ý to y */
         1364  +  { 0x00FE,  0x74, 0x68, 0x00, 0x00 },  /* þ to th */
         1365  +  { 0x00FF,  0x79, 0x00, 0x00, 0x00 },  /* ÿ to y */
         1366  +  { 0x0100,  0x41, 0x00, 0x00, 0x00 },  /* Ā to A */
         1367  +  { 0x0101,  0x61, 0x00, 0x00, 0x00 },  /* ā to a */
         1368  +  { 0x0102,  0x41, 0x00, 0x00, 0x00 },  /* Ă to A */
         1369  +  { 0x0103,  0x61, 0x00, 0x00, 0x00 },  /* ă to a */
         1370  +  { 0x0104,  0x41, 0x00, 0x00, 0x00 },  /* Ą to A */
         1371  +  { 0x0105,  0x61, 0x00, 0x00, 0x00 },  /* ą to a */
         1372  +  { 0x0106,  0x43, 0x00, 0x00, 0x00 },  /* Ć to C */
         1373  +  { 0x0107,  0x63, 0x00, 0x00, 0x00 },  /* ć to c */
         1374  +  { 0x0108,  0x43, 0x68, 0x00, 0x00 },  /* Ĉ to Ch */
         1375  +  { 0x0109,  0x63, 0x68, 0x00, 0x00 },  /* ĉ to ch */
         1376  +  { 0x010A,  0x43, 0x00, 0x00, 0x00 },  /* Ċ to C */
         1377  +  { 0x010B,  0x63, 0x00, 0x00, 0x00 },  /* ċ to c */
         1378  +  { 0x010C,  0x43, 0x00, 0x00, 0x00 },  /* Č to C */
         1379  +  { 0x010D,  0x63, 0x00, 0x00, 0x00 },  /* č to c */
         1380  +  { 0x010E,  0x44, 0x00, 0x00, 0x00 },  /* Ď to D */
         1381  +  { 0x010F,  0x64, 0x00, 0x00, 0x00 },  /* ď to d */
         1382  +  { 0x0110,  0x44, 0x00, 0x00, 0x00 },  /* Đ to D */
         1383  +  { 0x0111,  0x64, 0x00, 0x00, 0x00 },  /* đ to d */
         1384  +  { 0x0112,  0x45, 0x00, 0x00, 0x00 },  /* Ē to E */
         1385  +  { 0x0113,  0x65, 0x00, 0x00, 0x00 },  /* ē to e */
         1386  +  { 0x0114,  0x45, 0x00, 0x00, 0x00 },  /* Ĕ to E */
         1387  +  { 0x0115,  0x65, 0x00, 0x00, 0x00 },  /* ĕ to e */
         1388  +  { 0x0116,  0x45, 0x00, 0x00, 0x00 },  /* Ė to E */
         1389  +  { 0x0117,  0x65, 0x00, 0x00, 0x00 },  /* ė to e */
         1390  +  { 0x0118,  0x45, 0x00, 0x00, 0x00 },  /* Ę to E */
         1391  +  { 0x0119,  0x65, 0x00, 0x00, 0x00 },  /* ę to e */
         1392  +  { 0x011A,  0x45, 0x00, 0x00, 0x00 },  /* Ě to E */
         1393  +  { 0x011B,  0x65, 0x00, 0x00, 0x00 },  /* ě to e */
         1394  +  { 0x011C,  0x47, 0x68, 0x00, 0x00 },  /* Ĝ to Gh */
         1395  +  { 0x011D,  0x67, 0x68, 0x00, 0x00 },  /* ĝ to gh */
         1396  +  { 0x011E,  0x47, 0x00, 0x00, 0x00 },  /* Ğ to G */
         1397  +  { 0x011F,  0x67, 0x00, 0x00, 0x00 },  /* ğ to g */
         1398  +  { 0x0120,  0x47, 0x00, 0x00, 0x00 },  /* Ġ to G */
         1399  +  { 0x0121,  0x67, 0x00, 0x00, 0x00 },  /* ġ to g */
         1400  +  { 0x0122,  0x47, 0x00, 0x00, 0x00 },  /* Ģ to G */
         1401  +  { 0x0123,  0x67, 0x00, 0x00, 0x00 },  /* ģ to g */
         1402  +  { 0x0124,  0x48, 0x68, 0x00, 0x00 },  /* Ĥ to Hh */
         1403  +  { 0x0125,  0x68, 0x68, 0x00, 0x00 },  /* ĥ to hh */
         1404  +  { 0x0126,  0x48, 0x00, 0x00, 0x00 },  /* Ħ to H */
         1405  +  { 0x0127,  0x68, 0x00, 0x00, 0x00 },  /* ħ to h */
         1406  +  { 0x0128,  0x49, 0x00, 0x00, 0x00 },  /* Ĩ to I */
         1407  +  { 0x0129,  0x69, 0x00, 0x00, 0x00 },  /* ĩ to i */
         1408  +  { 0x012A,  0x49, 0x00, 0x00, 0x00 },  /* Ī to I */
         1409  +  { 0x012B,  0x69, 0x00, 0x00, 0x00 },  /* ī to i */
         1410  +  { 0x012C,  0x49, 0x00, 0x00, 0x00 },  /* Ĭ to I */
         1411  +  { 0x012D,  0x69, 0x00, 0x00, 0x00 },  /* ĭ to i */
         1412  +  { 0x012E,  0x49, 0x00, 0x00, 0x00 },  /* Į to I */
         1413  +  { 0x012F,  0x69, 0x00, 0x00, 0x00 },  /* į to i */
         1414  +  { 0x0130,  0x49, 0x00, 0x00, 0x00 },  /* İ to I */
         1415  +  { 0x0131,  0x69, 0x00, 0x00, 0x00 },  /* ı to i */
         1416  +  { 0x0132,  0x49, 0x4A, 0x00, 0x00 },  /* IJ to IJ */
         1417  +  { 0x0133,  0x69, 0x6A, 0x00, 0x00 },  /* ij to ij */
         1418  +  { 0x0134,  0x4A, 0x68, 0x00, 0x00 },  /* Ĵ to Jh */
         1419  +  { 0x0135,  0x6A, 0x68, 0x00, 0x00 },  /* ĵ to jh */
         1420  +  { 0x0136,  0x4B, 0x00, 0x00, 0x00 },  /* Ķ to K */
         1421  +  { 0x0137,  0x6B, 0x00, 0x00, 0x00 },  /* ķ to k */
         1422  +  { 0x0138,  0x6B, 0x00, 0x00, 0x00 },  /* ĸ to k */
         1423  +  { 0x0139,  0x4C, 0x00, 0x00, 0x00 },  /* Ĺ to L */
         1424  +  { 0x013A,  0x6C, 0x00, 0x00, 0x00 },  /* ĺ to l */
         1425  +  { 0x013B,  0x4C, 0x00, 0x00, 0x00 },  /* Ļ to L */
         1426  +  { 0x013C,  0x6C, 0x00, 0x00, 0x00 },  /* ļ to l */
         1427  +  { 0x013D,  0x4C, 0x00, 0x00, 0x00 },  /* Ľ to L */
         1428  +  { 0x013E,  0x6C, 0x00, 0x00, 0x00 },  /* ľ to l */
         1429  +  { 0x013F,  0x4C, 0x2E, 0x00, 0x00 },  /* Ŀ to L. */
         1430  +  { 0x0140,  0x6C, 0x2E, 0x00, 0x00 },  /* ŀ to l. */
         1431  +  { 0x0141,  0x4C, 0x00, 0x00, 0x00 },  /* Ł to L */
         1432  +  { 0x0142,  0x6C, 0x00, 0x00, 0x00 },  /* ł to l */
         1433  +  { 0x0143,  0x4E, 0x00, 0x00, 0x00 },  /* Ń to N */
         1434  +  { 0x0144,  0x6E, 0x00, 0x00, 0x00 },  /* ń to n */
         1435  +  { 0x0145,  0x4E, 0x00, 0x00, 0x00 },  /* Ņ to N */
         1436  +  { 0x0146,  0x6E, 0x00, 0x00, 0x00 },  /* ņ to n */
         1437  +  { 0x0147,  0x4E, 0x00, 0x00, 0x00 },  /* Ň to N */
         1438  +  { 0x0148,  0x6E, 0x00, 0x00, 0x00 },  /* ň to n */
         1439  +  { 0x0149,  0x27, 0x6E, 0x00, 0x00 },  /* ʼn to 'n */
         1440  +  { 0x014A,  0x4E, 0x47, 0x00, 0x00 },  /* Ŋ to NG */
         1441  +  { 0x014B,  0x6E, 0x67, 0x00, 0x00 },  /* ŋ to ng */
         1442  +  { 0x014C,  0x4F, 0x00, 0x00, 0x00 },  /* Ō to O */
         1443  +  { 0x014D,  0x6F, 0x00, 0x00, 0x00 },  /* ō to o */
         1444  +  { 0x014E,  0x4F, 0x00, 0x00, 0x00 },  /* Ŏ to O */
         1445  +  { 0x014F,  0x6F, 0x00, 0x00, 0x00 },  /* ŏ to o */
         1446  +  { 0x0150,  0x4F, 0x00, 0x00, 0x00 },  /* Ő to O */
         1447  +  { 0x0151,  0x6F, 0x00, 0x00, 0x00 },  /* ő to o */
         1448  +  { 0x0152,  0x4F, 0x45, 0x00, 0x00 },  /* Πto OE */
         1449  +  { 0x0153,  0x6F, 0x65, 0x00, 0x00 },  /* œ to oe */
         1450  +  { 0x0154,  0x52, 0x00, 0x00, 0x00 },  /* Ŕ to R */
         1451  +  { 0x0155,  0x72, 0x00, 0x00, 0x00 },  /* ŕ to r */
         1452  +  { 0x0156,  0x52, 0x00, 0x00, 0x00 },  /* Ŗ to R */
         1453  +  { 0x0157,  0x72, 0x00, 0x00, 0x00 },  /* ŗ to r */
         1454  +  { 0x0158,  0x52, 0x00, 0x00, 0x00 },  /* Ř to R */
         1455  +  { 0x0159,  0x72, 0x00, 0x00, 0x00 },  /* ř to r */
         1456  +  { 0x015A,  0x53, 0x00, 0x00, 0x00 },  /* Ś to S */
         1457  +  { 0x015B,  0x73, 0x00, 0x00, 0x00 },  /* ś to s */
         1458  +  { 0x015C,  0x53, 0x68, 0x00, 0x00 },  /* Ŝ to Sh */
         1459  +  { 0x015D,  0x73, 0x68, 0x00, 0x00 },  /* ŝ to sh */
         1460  +  { 0x015E,  0x53, 0x00, 0x00, 0x00 },  /* Ş to S */
         1461  +  { 0x015F,  0x73, 0x00, 0x00, 0x00 },  /* ş to s */
         1462  +  { 0x0160,  0x53, 0x00, 0x00, 0x00 },  /* Š to S */
         1463  +  { 0x0161,  0x73, 0x00, 0x00, 0x00 },  /* š to s */
         1464  +  { 0x0162,  0x54, 0x00, 0x00, 0x00 },  /* Ţ to T */
         1465  +  { 0x0163,  0x74, 0x00, 0x00, 0x00 },  /* ţ to t */
         1466  +  { 0x0164,  0x54, 0x00, 0x00, 0x00 },  /* Ť to T */
         1467  +  { 0x0165,  0x74, 0x00, 0x00, 0x00 },  /* ť to t */
         1468  +  { 0x0166,  0x54, 0x00, 0x00, 0x00 },  /* Ŧ to T */
         1469  +  { 0x0167,  0x74, 0x00, 0x00, 0x00 },  /* ŧ to t */
         1470  +  { 0x0168,  0x55, 0x00, 0x00, 0x00 },  /* Ũ to U */
         1471  +  { 0x0169,  0x75, 0x00, 0x00, 0x00 },  /* ũ to u */
         1472  +  { 0x016A,  0x55, 0x00, 0x00, 0x00 },  /* Ū to U */
         1473  +  { 0x016B,  0x75, 0x00, 0x00, 0x00 },  /* ū to u */
         1474  +  { 0x016C,  0x55, 0x00, 0x00, 0x00 },  /* Ŭ to U */
         1475  +  { 0x016D,  0x75, 0x00, 0x00, 0x00 },  /* ŭ to u */
         1476  +  { 0x016E,  0x55, 0x00, 0x00, 0x00 },  /* Ů to U */
         1477  +  { 0x016F,  0x75, 0x00, 0x00, 0x00 },  /* ů to u */
         1478  +  { 0x0170,  0x55, 0x00, 0x00, 0x00 },  /* Ű to U */
         1479  +  { 0x0171,  0x75, 0x00, 0x00, 0x00 },  /* ű to u */
         1480  +  { 0x0172,  0x55, 0x00, 0x00, 0x00 },  /* Ų to U */
         1481  +  { 0x0173,  0x75, 0x00, 0x00, 0x00 },  /* ų to u */
         1482  +  { 0x0174,  0x57, 0x00, 0x00, 0x00 },  /* Ŵ to W */
         1483  +  { 0x0175,  0x77, 0x00, 0x00, 0x00 },  /* ŵ to w */
         1484  +  { 0x0176,  0x59, 0x00, 0x00, 0x00 },  /* Ŷ to Y */
         1485  +  { 0x0177,  0x79, 0x00, 0x00, 0x00 },  /* ŷ to y */
         1486  +  { 0x0178,  0x59, 0x00, 0x00, 0x00 },  /* Ÿ to Y */
         1487  +  { 0x0179,  0x5A, 0x00, 0x00, 0x00 },  /* Ź to Z */
         1488  +  { 0x017A,  0x7A, 0x00, 0x00, 0x00 },  /* ź to z */
         1489  +  { 0x017B,  0x5A, 0x00, 0x00, 0x00 },  /* Ż to Z */
         1490  +  { 0x017C,  0x7A, 0x00, 0x00, 0x00 },  /* ż to z */
         1491  +  { 0x017D,  0x5A, 0x00, 0x00, 0x00 },  /* Ž to Z */
         1492  +  { 0x017E,  0x7A, 0x00, 0x00, 0x00 },  /* ž to z */
         1493  +  { 0x017F,  0x73, 0x00, 0x00, 0x00 },  /* ſ to s */
         1494  +  { 0x0192,  0x66, 0x00, 0x00, 0x00 },  /* ƒ to f */
         1495  +  { 0x0218,  0x53, 0x00, 0x00, 0x00 },  /* Ș to S */
         1496  +  { 0x0219,  0x73, 0x00, 0x00, 0x00 },  /* ș to s */
         1497  +  { 0x021A,  0x54, 0x00, 0x00, 0x00 },  /* Ț to T */
         1498  +  { 0x021B,  0x74, 0x00, 0x00, 0x00 },  /* ț to t */
         1499  +  { 0x0386,  0x41, 0x00, 0x00, 0x00 },  /* Ά to A */
         1500  +  { 0x0388,  0x45, 0x00, 0x00, 0x00 },  /* Έ to E */
         1501  +  { 0x0389,  0x49, 0x00, 0x00, 0x00 },  /* Ή to I */
         1502  +  { 0x038A,  0x49, 0x00, 0x00, 0x00 },  /* Ί to I */
         1503  +  { 0x038C,  0x4f, 0x00, 0x00, 0x00 },  /* Ό to O */
         1504  +  { 0x038E,  0x59, 0x00, 0x00, 0x00 },  /* Ύ to Y */
         1505  +  { 0x038F,  0x4f, 0x00, 0x00, 0x00 },  /* Ώ to O */
         1506  +  { 0x0390,  0x69, 0x00, 0x00, 0x00 },  /* ΐ to i */
         1507  +  { 0x0391,  0x41, 0x00, 0x00, 0x00 },  /* Α to A */
         1508  +  { 0x0392,  0x42, 0x00, 0x00, 0x00 },  /* Β to B */
         1509  +  { 0x0393,  0x47, 0x00, 0x00, 0x00 },  /* Γ to G */
         1510  +  { 0x0394,  0x44, 0x00, 0x00, 0x00 },  /* Δ to D */
         1511  +  { 0x0395,  0x45, 0x00, 0x00, 0x00 },  /* Ε to E */
         1512  +  { 0x0396,  0x5a, 0x00, 0x00, 0x00 },  /* Ζ to Z */
         1513  +  { 0x0397,  0x49, 0x00, 0x00, 0x00 },  /* Η to I */
         1514  +  { 0x0398,  0x54, 0x68, 0x00, 0x00 },  /* Θ to Th */
         1515  +  { 0x0399,  0x49, 0x00, 0x00, 0x00 },  /* Ι to I */
         1516  +  { 0x039A,  0x4b, 0x00, 0x00, 0x00 },  /* Κ to K */
         1517  +  { 0x039B,  0x4c, 0x00, 0x00, 0x00 },  /* Λ to L */
         1518  +  { 0x039C,  0x4d, 0x00, 0x00, 0x00 },  /* Μ to M */
         1519  +  { 0x039D,  0x4e, 0x00, 0x00, 0x00 },  /* Ν to N */
         1520  +  { 0x039E,  0x58, 0x00, 0x00, 0x00 },  /* Ξ to X */
         1521  +  { 0x039F,  0x4f, 0x00, 0x00, 0x00 },  /* Ο to O */
         1522  +  { 0x03A0,  0x50, 0x00, 0x00, 0x00 },  /* Π to P */
         1523  +  { 0x03A1,  0x52, 0x00, 0x00, 0x00 },  /* Ρ to R */
         1524  +  { 0x03A3,  0x53, 0x00, 0x00, 0x00 },  /* Σ to S */
         1525  +  { 0x03A4,  0x54, 0x00, 0x00, 0x00 },  /* Τ to T */
         1526  +  { 0x03A5,  0x59, 0x00, 0x00, 0x00 },  /* Υ to Y */
         1527  +  { 0x03A6,  0x46, 0x00, 0x00, 0x00 },  /* Φ to F */
         1528  +  { 0x03A7,  0x43, 0x68, 0x00, 0x00 },  /* Χ to Ch */
         1529  +  { 0x03A8,  0x50, 0x73, 0x00, 0x00 },  /* Ψ to Ps */
         1530  +  { 0x03A9,  0x4f, 0x00, 0x00, 0x00 },  /* Ω to O */
         1531  +  { 0x03AA,  0x49, 0x00, 0x00, 0x00 },  /* Ϊ to I */
         1532  +  { 0x03AB,  0x59, 0x00, 0x00, 0x00 },  /* Ϋ to Y */
         1533  +  { 0x03AC,  0x61, 0x00, 0x00, 0x00 },  /* ά to a */
         1534  +  { 0x03AD,  0x65, 0x00, 0x00, 0x00 },  /* έ to e */
         1535  +  { 0x03AE,  0x69, 0x00, 0x00, 0x00 },  /* ή to i */
         1536  +  { 0x03AF,  0x69, 0x00, 0x00, 0x00 },  /* ί to i */
         1537  +  { 0x03B1,  0x61, 0x00, 0x00, 0x00 },  /* α to a */
         1538  +  { 0x03B2,  0x62, 0x00, 0x00, 0x00 },  /* β to b */
         1539  +  { 0x03B3,  0x67, 0x00, 0x00, 0x00 },  /* γ to g */
         1540  +  { 0x03B4,  0x64, 0x00, 0x00, 0x00 },  /* δ to d */
         1541  +  { 0x03B5,  0x65, 0x00, 0x00, 0x00 },  /* ε to e */
         1542  +  { 0x03B6,  0x7a, 0x00, 0x00, 0x00 },  /* ζ to z */
         1543  +  { 0x03B7,  0x69, 0x00, 0x00, 0x00 },  /* η to i */
         1544  +  { 0x03B8,  0x74, 0x68, 0x00, 0x00 },  /* θ to th */
         1545  +  { 0x03B9,  0x69, 0x00, 0x00, 0x00 },  /* ι to i */
         1546  +  { 0x03BA,  0x6b, 0x00, 0x00, 0x00 },  /* κ to k */
         1547  +  { 0x03BB,  0x6c, 0x00, 0x00, 0x00 },  /* λ to l */
         1548  +  { 0x03BC,  0x6d, 0x00, 0x00, 0x00 },  /* μ to m */
         1549  +  { 0x03BD,  0x6e, 0x00, 0x00, 0x00 },  /* ν to n */
         1550  +  { 0x03BE,  0x78, 0x00, 0x00, 0x00 },  /* ξ to x */
         1551  +  { 0x03BF,  0x6f, 0x00, 0x00, 0x00 },  /* ο to o */
         1552  +  { 0x03C0,  0x70, 0x00, 0x00, 0x00 },  /* π to p */
         1553  +  { 0x03C1,  0x72, 0x00, 0x00, 0x00 },  /* ρ to r */
         1554  +  { 0x03C3,  0x73, 0x00, 0x00, 0x00 },  /* σ to s */
         1555  +  { 0x03C4,  0x74, 0x00, 0x00, 0x00 },  /* τ to t */
         1556  +  { 0x03C5,  0x79, 0x00, 0x00, 0x00 },  /* υ to y */
         1557  +  { 0x03C6,  0x66, 0x00, 0x00, 0x00 },  /* φ to f */
         1558  +  { 0x03C7,  0x63, 0x68, 0x00, 0x00 },  /* χ to ch */
         1559  +  { 0x03C8,  0x70, 0x73, 0x00, 0x00 },  /* ψ to ps */
         1560  +  { 0x03C9,  0x6f, 0x00, 0x00, 0x00 },  /* ω to o */
         1561  +  { 0x03CA,  0x69, 0x00, 0x00, 0x00 },  /* ϊ to i */
         1562  +  { 0x03CB,  0x79, 0x00, 0x00, 0x00 },  /* ϋ to y */
         1563  +  { 0x03CC,  0x6f, 0x00, 0x00, 0x00 },  /* ό to o */
         1564  +  { 0x03CD,  0x79, 0x00, 0x00, 0x00 },  /* ύ to y */
         1565  +  { 0x03CE,  0x69, 0x00, 0x00, 0x00 },  /* ώ to i */
         1566  +  { 0x0400,  0x45, 0x00, 0x00, 0x00 },  /* Ѐ to E */
         1567  +  { 0x0401,  0x45, 0x00, 0x00, 0x00 },  /* Ё to E */
         1568  +  { 0x0402,  0x44, 0x00, 0x00, 0x00 },  /* Ђ to D */
         1569  +  { 0x0403,  0x47, 0x00, 0x00, 0x00 },  /* Ѓ to G */
         1570  +  { 0x0404,  0x45, 0x00, 0x00, 0x00 },  /* Є to E */
         1571  +  { 0x0405,  0x5a, 0x00, 0x00, 0x00 },  /* Ѕ to Z */
         1572  +  { 0x0406,  0x49, 0x00, 0x00, 0x00 },  /* І to I */
         1573  +  { 0x0407,  0x49, 0x00, 0x00, 0x00 },  /* Ї to I */
         1574  +  { 0x0408,  0x4a, 0x00, 0x00, 0x00 },  /* Ј to J */
         1575  +  { 0x0409,  0x49, 0x00, 0x00, 0x00 },  /* Љ to I */
         1576  +  { 0x040A,  0x4e, 0x00, 0x00, 0x00 },  /* Њ to N */
         1577  +  { 0x040B,  0x44, 0x00, 0x00, 0x00 },  /* Ћ to D */
         1578  +  { 0x040C,  0x4b, 0x00, 0x00, 0x00 },  /* Ќ to K */
         1579  +  { 0x040D,  0x49, 0x00, 0x00, 0x00 },  /* Ѝ to I */
         1580  +  { 0x040E,  0x55, 0x00, 0x00, 0x00 },  /* Ў to U */
         1581  +  { 0x040F,  0x44, 0x00, 0x00, 0x00 },  /* Џ to D */
         1582  +  { 0x0410,  0x41, 0x00, 0x00, 0x00 },  /* А to A */
         1583  +  { 0x0411,  0x42, 0x00, 0x00, 0x00 },  /* Б to B */
         1584  +  { 0x0412,  0x56, 0x00, 0x00, 0x00 },  /* В to V */
         1585  +  { 0x0413,  0x47, 0x00, 0x00, 0x00 },  /* Г to G */
         1586  +  { 0x0414,  0x44, 0x00, 0x00, 0x00 },  /* Д to D */
         1587  +  { 0x0415,  0x45, 0x00, 0x00, 0x00 },  /* Е to E */
         1588  +  { 0x0416,  0x5a, 0x68, 0x00, 0x00 },  /* Ж to Zh */
         1589  +  { 0x0417,  0x5a, 0x00, 0x00, 0x00 },  /* З to Z */
         1590  +  { 0x0418,  0x49, 0x00, 0x00, 0x00 },  /* И to I */
         1591  +  { 0x0419,  0x49, 0x00, 0x00, 0x00 },  /* Й to I */
         1592  +  { 0x041A,  0x4b, 0x00, 0x00, 0x00 },  /* К to K */
         1593  +  { 0x041B,  0x4c, 0x00, 0x00, 0x00 },  /* Л to L */
         1594  +  { 0x041C,  0x4d, 0x00, 0x00, 0x00 },  /* М to M */
         1595  +  { 0x041D,  0x4e, 0x00, 0x00, 0x00 },  /* Н to N */
         1596  +  { 0x041E,  0x4f, 0x00, 0x00, 0x00 },  /* О to O */
         1597  +  { 0x041F,  0x50, 0x00, 0x00, 0x00 },  /* П to P */
         1598  +  { 0x0420,  0x52, 0x00, 0x00, 0x00 },  /* Р to R */
         1599  +  { 0x0421,  0x53, 0x00, 0x00, 0x00 },  /* С to S */
         1600  +  { 0x0422,  0x54, 0x00, 0x00, 0x00 },  /* Т to T */
         1601  +  { 0x0423,  0x55, 0x00, 0x00, 0x00 },  /* У to U */
         1602  +  { 0x0424,  0x46, 0x00, 0x00, 0x00 },  /* Ф to F */
         1603  +  { 0x0425,  0x4b, 0x68, 0x00, 0x00 },  /* Х to Kh */
         1604  +  { 0x0426,  0x54, 0x63, 0x00, 0x00 },  /* Ц to Tc */
         1605  +  { 0x0427,  0x43, 0x68, 0x00, 0x00 },  /* Ч to Ch */
         1606  +  { 0x0428,  0x53, 0x68, 0x00, 0x00 },  /* Ш to Sh */
         1607  +  { 0x0429,  0x53, 0x68, 0x63, 0x68 },  /* Щ to Shch */
         1608  +  { 0x042A,  0x61, 0x00, 0x00, 0x00 },  /*  to A */
         1609  +  { 0x042B,  0x59, 0x00, 0x00, 0x00 },  /* Ы to Y */
         1610  +  { 0x042C,  0x59, 0x00, 0x00, 0x00 },  /*  to Y */
         1611  +  { 0x042D,  0x45, 0x00, 0x00, 0x00 },  /* Э to E */
         1612  +  { 0x042E,  0x49, 0x75, 0x00, 0x00 },  /* Ю to Iu */
         1613  +  { 0x042F,  0x49, 0x61, 0x00, 0x00 },  /* Я to Ia */
         1614  +  { 0x0430,  0x61, 0x00, 0x00, 0x00 },  /* а to a */
         1615  +  { 0x0431,  0x62, 0x00, 0x00, 0x00 },  /* б to b */
         1616  +  { 0x0432,  0x76, 0x00, 0x00, 0x00 },  /* в to v */
         1617  +  { 0x0433,  0x67, 0x00, 0x00, 0x00 },  /* г to g */
         1618  +  { 0x0434,  0x64, 0x00, 0x00, 0x00 },  /* д to d */
         1619  +  { 0x0435,  0x65, 0x00, 0x00, 0x00 },  /* е to e */
         1620  +  { 0x0436,  0x7a, 0x68, 0x00, 0x00 },  /* ж to zh */
         1621  +  { 0x0437,  0x7a, 0x00, 0x00, 0x00 },  /* з to z */
         1622  +  { 0x0438,  0x69, 0x00, 0x00, 0x00 },  /* и to i */
         1623  +  { 0x0439,  0x69, 0x00, 0x00, 0x00 },  /* й to i */
         1624  +  { 0x043A,  0x6b, 0x00, 0x00, 0x00 },  /* к to k */
         1625  +  { 0x043B,  0x6c, 0x00, 0x00, 0x00 },  /* л to l */
         1626  +  { 0x043C,  0x6d, 0x00, 0x00, 0x00 },  /* м to m */
         1627  +  { 0x043D,  0x6e, 0x00, 0x00, 0x00 },  /* н to n */
         1628  +  { 0x043E,  0x6f, 0x00, 0x00, 0x00 },  /* о to o */
         1629  +  { 0x043F,  0x70, 0x00, 0x00, 0x00 },  /* п to p */
         1630  +  { 0x0440,  0x72, 0x00, 0x00, 0x00 },  /* р to r */
         1631  +  { 0x0441,  0x73, 0x00, 0x00, 0x00 },  /* с to s */
         1632  +  { 0x0442,  0x74, 0x00, 0x00, 0x00 },  /* т to t */
         1633  +  { 0x0443,  0x75, 0x00, 0x00, 0x00 },  /* у to u */
         1634  +  { 0x0444,  0x66, 0x00, 0x00, 0x00 },  /* ф to f */
         1635  +  { 0x0445,  0x6b, 0x68, 0x00, 0x00 },  /* х to kh */
         1636  +  { 0x0446,  0x74, 0x63, 0x00, 0x00 },  /* ц to tc */
         1637  +  { 0x0447,  0x63, 0x68, 0x00, 0x00 },  /* ч to ch */
         1638  +  { 0x0448,  0x73, 0x68, 0x00, 0x00 },  /* ш to sh */
         1639  +  { 0x0449,  0x73, 0x68, 0x63, 0x68 },  /* щ to shch */
         1640  +  { 0x044A,  0x61, 0x00, 0x00, 0x00 },  /*  to a */
         1641  +  { 0x044B,  0x79, 0x00, 0x00, 0x00 },  /* ы to y */
         1642  +  { 0x044C,  0x79, 0x00, 0x00, 0x00 },  /*  to y */
         1643  +  { 0x044D,  0x65, 0x00, 0x00, 0x00 },  /* э to e */
         1644  +  { 0x044E,  0x69, 0x75, 0x00, 0x00 },  /* ю to iu */
         1645  +  { 0x044F,  0x69, 0x61, 0x00, 0x00 },  /* я to ia */
         1646  +  { 0x0450,  0x65, 0x00, 0x00, 0x00 },  /* ѐ to e */
         1647  +  { 0x0451,  0x65, 0x00, 0x00, 0x00 },  /* ё to e */
         1648  +  { 0x0452,  0x64, 0x00, 0x00, 0x00 },  /* ђ to d */
         1649  +  { 0x0453,  0x67, 0x00, 0x00, 0x00 },  /* ѓ to g */
         1650  +  { 0x0454,  0x65, 0x00, 0x00, 0x00 },  /* є to e */
         1651  +  { 0x0455,  0x7a, 0x00, 0x00, 0x00 },  /* ѕ to z */
         1652  +  { 0x0456,  0x69, 0x00, 0x00, 0x00 },  /* і to i */
         1653  +  { 0x0457,  0x69, 0x00, 0x00, 0x00 },  /* ї to i */
         1654  +  { 0x0458,  0x6a, 0x00, 0x00, 0x00 },  /* ј to j */
         1655  +  { 0x0459,  0x69, 0x00, 0x00, 0x00 },  /* љ to i */
         1656  +  { 0x045A,  0x6e, 0x00, 0x00, 0x00 },  /* њ to n */
         1657  +  { 0x045B,  0x64, 0x00, 0x00, 0x00 },  /* ћ to d */
         1658  +  { 0x045C,  0x6b, 0x00, 0x00, 0x00 },  /* ќ to k */
         1659  +  { 0x045D,  0x69, 0x00, 0x00, 0x00 },  /* ѝ to i */
         1660  +  { 0x045E,  0x75, 0x00, 0x00, 0x00 },  /* ў to u */
         1661  +  { 0x045F,  0x64, 0x00, 0x00, 0x00 },  /* џ to d */
         1662  +  { 0x1E02,  0x42, 0x00, 0x00, 0x00 },  /* Ḃ to B */
         1663  +  { 0x1E03,  0x62, 0x00, 0x00, 0x00 },  /* ḃ to b */
         1664  +  { 0x1E0A,  0x44, 0x00, 0x00, 0x00 },  /* Ḋ to D */
         1665  +  { 0x1E0B,  0x64, 0x00, 0x00, 0x00 },  /* ḋ to d */
         1666  +  { 0x1E1E,  0x46, 0x00, 0x00, 0x00 },  /* Ḟ to F */
         1667  +  { 0x1E1F,  0x66, 0x00, 0x00, 0x00 },  /* ḟ to f */
         1668  +  { 0x1E40,  0x4D, 0x00, 0x00, 0x00 },  /* Ṁ to M */
         1669  +  { 0x1E41,  0x6D, 0x00, 0x00, 0x00 },  /* ṁ to m */
         1670  +  { 0x1E56,  0x50, 0x00, 0x00, 0x00 },  /* Ṗ to P */
         1671  +  { 0x1E57,  0x70, 0x00, 0x00, 0x00 },  /* ṗ to p */
         1672  +  { 0x1E60,  0x53, 0x00, 0x00, 0x00 },  /* Ṡ to S */
         1673  +  { 0x1E61,  0x73, 0x00, 0x00, 0x00 },  /* ṡ to s */
         1674  +  { 0x1E6A,  0x54, 0x00, 0x00, 0x00 },  /* Ṫ to T */
         1675  +  { 0x1E6B,  0x74, 0x00, 0x00, 0x00 },  /* ṫ to t */
         1676  +  { 0x1E80,  0x57, 0x00, 0x00, 0x00 },  /* Ẁ to W */
         1677  +  { 0x1E81,  0x77, 0x00, 0x00, 0x00 },  /* ẁ to w */
         1678  +  { 0x1E82,  0x57, 0x00, 0x00, 0x00 },  /* Ẃ to W */
         1679  +  { 0x1E83,  0x77, 0x00, 0x00, 0x00 },  /* ẃ to w */
         1680  +  { 0x1E84,  0x57, 0x00, 0x00, 0x00 },  /* Ẅ to W */
         1681  +  { 0x1E85,  0x77, 0x00, 0x00, 0x00 },  /* ẅ to w */
         1682  +  { 0x1EF2,  0x59, 0x00, 0x00, 0x00 },  /* Ỳ to Y */
         1683  +  { 0x1EF3,  0x79, 0x00, 0x00, 0x00 },  /* ỳ to y */
         1684  +  { 0xFB00,  0x66, 0x66, 0x00, 0x00 },  /* ff to ff */
         1685  +  { 0xFB01,  0x66, 0x69, 0x00, 0x00 },  /* fi to fi */
         1686  +  { 0xFB02,  0x66, 0x6C, 0x00, 0x00 },  /* fl to fl */
         1687  +  { 0xFB05,  0x73, 0x74, 0x00, 0x00 },  /* ſt to st */
         1688  +  { 0xFB06,  0x73, 0x74, 0x00, 0x00 },  /* st to st */
  1599   1689   };
         1690  +
         1691  +static const Transliteration *spellfixFindTranslit(int c, int *pxTop){
         1692  +  *pxTop = (sizeof(translit)/sizeof(translit[0])) - 1;
         1693  +  return translit;
         1694  +}
  1600   1695   
  1601   1696   /*
  1602   1697   ** Convert the input string from UTF-8 into pure ASCII by converting
  1603   1698   ** all non-ASCII characters to some combination of characters in the
  1604   1699   ** ASCII subset.
  1605   1700   **
  1606   1701   ** The returned string might contain more characters than the input.
................................................................................
  1617   1712       c = utf8Read(zIn, nIn, &sz);
  1618   1713       zIn += sz;
  1619   1714       nIn -= sz;
  1620   1715       if( c<=127 ){
  1621   1716         zOut[nOut++] = (unsigned char)c;
  1622   1717       }else{
  1623   1718         int xTop, xBtm, x;
  1624         -      xTop = sizeof(translit)/sizeof(translit[0]) - 1;
         1719  +      const Transliteration *tbl = spellfixFindTranslit(c, &xTop);
  1625   1720         xBtm = 0;
  1626   1721         while( xTop>=xBtm ){
  1627   1722           x = (xTop + xBtm)/2;
  1628         -        if( translit[x].cFrom==c ){
  1629         -          zOut[nOut++] = translit[x].cTo0;
  1630         -          if( translit[x].cTo1 ){
  1631         -            zOut[nOut++] = translit[x].cTo1;
  1632         -            /* Add an extra "ch" after the "sh" for Щ and щ */
  1633         -            if( c==0x0429 || c== 0x0449 ){
  1634         -              zOut[nOut++] = 'c';
  1635         -              zOut[nOut++] = 'h';
         1723  +        if( tbl[x].cFrom==c ){
         1724  +          zOut[nOut++] = tbl[x].cTo0;
         1725  +          if( tbl[x].cTo1 ){
         1726  +            zOut[nOut++] = tbl[x].cTo1;
         1727  +            if( tbl[x].cTo2 ){
         1728  +              zOut[nOut++] = tbl[x].cTo2;
         1729  +              if( tbl[x].cTo3 ){
         1730  +                zOut[nOut++] = tbl[x].cTo3;
         1731  +              }
  1636   1732               }
  1637   1733             }
  1638   1734             c = 0;
  1639   1735             break;
  1640         -        }else if( translit[x].cFrom>c ){
         1736  +        }else if( tbl[x].cFrom>c ){
  1641   1737             xTop = x-1;
  1642   1738           }else{
  1643   1739             xBtm = x+1;
  1644   1740           }
  1645   1741         }
  1646   1742         if( c ) zOut[nOut++] = '?';
  1647   1743       }
................................................................................
  1664   1760     for(nChar=0; i<nIn && nOut<nTrans; nChar++){
  1665   1761       c = utf8Read((const unsigned char *)&zIn[i], nIn-i, &sz);
  1666   1762       i += sz;
  1667   1763   
  1668   1764       nOut++;
  1669   1765       if( c>=128 ){
  1670   1766         int xTop, xBtm, x;
  1671         -      xTop = sizeof(translit)/sizeof(translit[0]) - 1;
         1767  +      const Transliteration *tbl = spellfixFindTranslit(c, &xTop);
  1672   1768         xBtm = 0;
  1673   1769         while( xTop>=xBtm ){
  1674   1770           x = (xTop + xBtm)/2;
  1675         -        if( translit[x].cFrom==c ){
  1676         -          if( translit[x].cTo1 ) nOut++;
  1677         -          if( c==0x0429 || c== 0x0449 ) nOut += 2;
         1771  +        if( tbl[x].cFrom==c ){
         1772  +          if( tbl[x].cTo1 ){
         1773  +            nOut++;
         1774  +            if( tbl[x].cTo2 ){
         1775  +              nOut++;
         1776  +              if( tbl[x].cTo3 ){
         1777  +                nOut++;
         1778  +              }
         1779  +            }
         1780  +          }
  1678   1781             break;
  1679         -        }else if( translit[x].cFrom>c ){
         1782  +        }else if( tbl[x].cFrom>c ){
  1680   1783             xTop = x-1;
  1681   1784           }else{
  1682   1785             xBtm = x+1;
  1683   1786           }
  1684   1787         }
  1685   1788       }
  1686   1789     }
................................................................................
  2472   2575     if( zPattern==0 ){
  2473   2576       x.rc = SQLITE_NOMEM;
  2474   2577       goto filter_exit;
  2475   2578     }
  2476   2579     nPattern = (int)strlen(zPattern);
  2477   2580     if( zPattern[nPattern-1]=='*' ) nPattern--;
  2478   2581     zSql = sqlite3_mprintf(
  2479         -     "SELECT id, word, rank, k1"
         2582  +     "SELECT id, word, rank, coalesce(k1,word)"
  2480   2583        "  FROM \"%w\".\"%w_vocab\""
  2481   2584        " WHERE langid=%d AND k2>=?1 AND k2<?2",
  2482   2585        p->zDbName, p->zTableName, iLang
  2483   2586     );
  2484   2587     if( zSql==0 ){
  2485   2588       x.rc = SQLITE_NOMEM;
  2486   2589       pStmt = 0;
................................................................................
  2806   2909         sqlite3_free(zK1);
  2807   2910         return SQLITE_NOMEM;
  2808   2911       }
  2809   2912       if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
  2810   2913         if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
  2811   2914           spellfix1DbExec(&rc, db,
  2812   2915                  "INSERT INTO \"%w\".\"%w_vocab\"(rank,langid,word,k1,k2) "
  2813         -               "VALUES(%d,%d,%Q,%Q,%Q)",
         2916  +               "VALUES(%d,%d,%Q,nullif(%Q,%Q),%Q)",
  2814   2917                  p->zDbName, p->zTableName,
  2815         -               iRank, iLang, zWord, zK1, zK2
         2918  +               iRank, iLang, zWord, zK1, zWord, zK2
  2816   2919           );
  2817   2920         }else{
  2818   2921           newRowid = sqlite3_value_int64(argv[1]);
  2819   2922           spellfix1DbExec(&rc, db,
  2820   2923               "INSERT OR %s INTO \"%w\".\"%w_vocab\"(id,rank,langid,word,k1,k2) "
  2821         -            "VALUES(%lld,%d,%d,%Q,%Q,%Q)",
         2924  +            "VALUES(%lld,%d,%d,%Q,nullif(%Q,%Q),%Q)",
  2822   2925               zConflict, p->zDbName, p->zTableName,
  2823         -            newRowid, iRank, iLang, zWord, zK1, zK2
         2926  +            newRowid, iRank, iLang, zWord, zK1, zWord, zK2
  2824   2927           );
  2825   2928         }
  2826   2929         *pRowid = sqlite3_last_insert_rowid(db);
  2827   2930       }else{
  2828   2931         rowid = sqlite3_value_int64(argv[0]);
  2829   2932         newRowid = *pRowid = sqlite3_value_int64(argv[1]);
  2830   2933         spellfix1DbExec(&rc, db,
  2831   2934                "UPDATE OR %s \"%w\".\"%w_vocab\" SET id=%lld, rank=%d, langid=%d,"
  2832         -             " word=%Q, k1=%Q, k2=%Q WHERE id=%lld",
         2935  +             " word=%Q, k1=nullif(%Q,%Q), k2=%Q WHERE id=%lld",
  2833   2936                zConflict, p->zDbName, p->zTableName, newRowid, iRank, iLang,
  2834         -             zWord, zK1, zK2, rowid
         2937  +             zWord, zK1, zWord, zK2, rowid
  2835   2938         );
  2836   2939       }
  2837   2940       sqlite3_free(zK1);
  2838   2941       sqlite3_free(zK2);
  2839   2942     }
  2840   2943     return rc;
  2841   2944   }

Changes to ext/misc/zipfile.c.

    26     26   */
    27     27   #include "sqlite3ext.h"
    28     28   SQLITE_EXTENSION_INIT1
    29     29   #include <stdio.h>
    30     30   #include <string.h>
    31     31   #include <assert.h>
    32     32   
    33         -#include <sys/types.h>
    34         -#include <sys/stat.h>
    35         -#include <fcntl.h>
    36         -#if !defined(_WIN32) && !defined(WIN32)
    37         -#  include <unistd.h>
    38         -#  include <dirent.h>
    39         -#  include <utime.h>
    40         -#else
    41         -#  include <io.h>
    42         -#endif
    43         -#include <time.h>
    44         -#include <errno.h>
    45         -
    46     33   #include <zlib.h>
    47     34   
    48     35   #ifndef SQLITE_OMIT_VIRTUALTABLE
    49     36   
    50     37   #ifndef SQLITE_AMALGAMATION
           38  +
    51     39   typedef sqlite3_int64 i64;
    52     40   typedef unsigned char u8;
    53     41   typedef unsigned short u16;
    54     42   typedef unsigned long u32;
    55     43   #define MIN(a,b) ((a)<(b) ? (a) : (b))
           44  +
           45  +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
           46  +# define ALWAYS(X)      (1)
           47  +# define NEVER(X)       (0)
           48  +#elif !defined(NDEBUG)
           49  +# define ALWAYS(X)      ((X)?1:(assert(0),0))
           50  +# define NEVER(X)       ((X)?(assert(0),1):0)
           51  +#else
           52  +# define ALWAYS(X)      (X)
           53  +# define NEVER(X)       (X)
           54  +#endif
           55  +
           56  +#endif   /* SQLITE_AMALGAMATION */
           57  +
           58  +/*
           59  +** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
           60  +**
           61  +** In some ways it would be better to obtain these values from system 
           62  +** header files. But, the dependency is undesirable and (a) these
           63  +** have been stable for decades, (b) the values are part of POSIX and
           64  +** are also made explicit in [man stat], and (c) are part of the 
           65  +** file format for zip archives.
           66  +*/
           67  +#ifndef S_IFDIR
           68  +# define S_IFDIR 0040000
           69  +#endif
           70  +#ifndef S_IFREG
           71  +# define S_IFREG 0100000
           72  +#endif
           73  +#ifndef S_IFLNK
           74  +# define S_IFLNK 0120000
    56     75   #endif
    57     76   
    58     77   static const char ZIPFILE_SCHEMA[] = 
    59     78     "CREATE TABLE y("
    60     79       "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
    61     80       "mode,"              /* 1: POSIX mode for file */
    62     81       "mtime,"             /* 2: Last modification time (secs since 1970)*/
................................................................................
    89    108   **   11 means "utf-8 filename and comment".
    90    109   **
    91    110   ** ZIPFILE_SIGNATURE_CDS:
    92    111   **   First 4 bytes of a valid CDS record.
    93    112   **
    94    113   ** ZIPFILE_SIGNATURE_LFH:
    95    114   **   First 4 bytes of a valid LFH record.
          115  +**
          116  +** ZIPFILE_SIGNATURE_EOCD
          117  +**   First 4 bytes of a valid EOCD record.
    96    118   */
    97    119   #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
    98    120   #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
    99    121   #define ZIPFILE_NEWENTRY_REQUIRED 20
   100    122   #define ZIPFILE_NEWENTRY_FLAGS    0x800
   101    123   #define ZIPFILE_SIGNATURE_CDS     0x02014b50
   102    124   #define ZIPFILE_SIGNATURE_LFH     0x04034b50
   103    125   #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
          126  +
          127  +/*
          128  +** The sizes of the fixed-size part of each of the three main data 
          129  +** structures in a zip archive.
          130  +*/
   104    131   #define ZIPFILE_LFH_FIXED_SZ      30
   105         -
   106         -/*
   107         -** Set the error message contained in context ctx to the results of
   108         -** vprintf(zFmt, ...).
   109         -*/
   110         -static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
   111         -  char *zMsg = 0;
   112         -  va_list ap;
   113         -  va_start(ap, zFmt);
   114         -  zMsg = sqlite3_vmprintf(zFmt, ap);
   115         -  sqlite3_result_error(ctx, zMsg, -1);
   116         -  sqlite3_free(zMsg);
   117         -  va_end(ap);
   118         -}
   119         -
          132  +#define ZIPFILE_EOCD_FIXED_SZ     22
          133  +#define ZIPFILE_CDS_FIXED_SZ      46
   120    134   
   121    135   /*
   122    136   *** 4.3.16  End of central directory record:
   123    137   ***
   124    138   ***   end of central dir signature    4 bytes  (0x06054b50)
   125    139   ***   number of this disk             2 bytes
   126    140   ***   number of the disk with the
................................................................................
   218    232     u32 szUncompressed;
   219    233     u16 nFile;
   220    234     u16 nExtra;
   221    235   };
   222    236   
   223    237   typedef struct ZipfileEntry ZipfileEntry;
   224    238   struct ZipfileEntry {
   225         -  char *zPath;               /* Path of zipfile entry */
   226         -  u8 *aCdsEntry;             /* Buffer containing entire CDS entry */
   227         -  int nCdsEntry;             /* Size of buffer aCdsEntry[] in bytes */
   228         -  int bDeleted;              /* True if entry has been deleted */
          239  +  ZipfileCDS cds;            /* Parsed CDS record */
          240  +  u32 mUnixTime;             /* Modification time, in UNIX format */
          241  +  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
          242  +  i64 iDataOff;              /* Offset to data in file (if aData==0) */
          243  +  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
   229    244     ZipfileEntry *pNext;       /* Next element in in-memory CDS */
   230    245   };
   231    246   
   232    247   /* 
   233         -** Cursor type for recursively iterating through a directory structure.
          248  +** Cursor type for zipfile tables.
   234    249   */
   235    250   typedef struct ZipfileCsr ZipfileCsr;
   236    251   struct ZipfileCsr {
   237    252     sqlite3_vtab_cursor base;  /* Base class - must be first */
   238    253     i64 iId;                   /* Cursor ID */
   239         -  int bEof;                  /* True when at EOF */
          254  +  u8 bEof;                   /* True when at EOF */
          255  +  u8 bNoop;                  /* If next xNext() call is no-op */
   240    256   
   241    257     /* Used outside of write transactions */
   242    258     FILE *pFile;               /* Zip file */
   243    259     i64 iNextOff;              /* Offset of next record in central directory */
   244    260     ZipfileEOCD eocd;          /* Parse of central directory record */
   245    261   
   246         -  /* Used inside write transactions */
   247         -  ZipfileEntry *pCurrent;
   248         -
   249         -  ZipfileCDS cds;            /* Central Directory Structure */
   250         -  ZipfileLFH lfh;            /* Local File Header for current entry */
   251         -  i64 iDataOff;              /* Offset in zipfile to data */
   252         -  u32 mTime;                 /* Extended mtime value */
   253         -  int flags;                 /* Flags byte (see below for bits) */
          262  +  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
          263  +  ZipfileEntry *pCurrent;    /* Current entry */
   254    264     ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
   255    265   };
   256    266   
   257         -/*
   258         -** Values for ZipfileCsr.flags.
   259         -*/
   260         -#define ZIPFILE_MTIME_VALID 0x0001
   261         -
   262    267   typedef struct ZipfileTab ZipfileTab;
   263    268   struct ZipfileTab {
   264    269     sqlite3_vtab base;         /* Base class - must be first */
   265    270     char *zFile;               /* Zip file this table accesses (may be NULL) */
          271  +  sqlite3 *db;               /* Host database connection */
   266    272     u8 *aBuffer;               /* Temporary buffer used for various tasks */
   267    273   
   268    274     ZipfileCsr *pCsrList;      /* List of cursors */
   269    275     i64 iNextCsrid;
   270    276   
   271    277     /* The following are used by write transactions only */
   272    278     ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
   273    279     ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
   274    280     FILE *pWriteFd;            /* File handle open on zip archive */
   275    281     i64 szCurrent;             /* Current size of zip archive */
   276    282     i64 szOrig;                /* Size of archive at start of transaction */
   277    283   };
   278    284   
          285  +/*
          286  +** Set the error message contained in context ctx to the results of
          287  +** vprintf(zFmt, ...).
          288  +*/
          289  +static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
          290  +  char *zMsg = 0;
          291  +  va_list ap;
          292  +  va_start(ap, zFmt);
          293  +  zMsg = sqlite3_vmprintf(zFmt, ap);
          294  +  sqlite3_result_error(ctx, zMsg, -1);
          295  +  sqlite3_free(zMsg);
          296  +  va_end(ap);
          297  +}
          298  +
          299  +/*
          300  +** If string zIn is quoted, dequote it in place. Otherwise, if the string
          301  +** is not quoted, do nothing.
          302  +*/
   279    303   static void zipfileDequote(char *zIn){
   280    304     char q = zIn[0];
   281    305     if( q=='"' || q=='\'' || q=='`' || q=='[' ){
   282         -    char c;
   283    306       int iIn = 1;
   284    307       int iOut = 0;
   285    308       if( q=='[' ) q = ']';
   286         -    while( (c = zIn[iIn++]) ){
   287         -      if( c==q ){
   288         -        if( zIn[iIn++]!=q ) break;
   289         -      }
          309  +    while( ALWAYS(zIn[iIn]) ){
          310  +      char c = zIn[iIn++];
          311  +      if( c==q && zIn[iIn++]!=q ) break;
   290    312         zIn[iOut++] = c;
   291    313       }
   292    314       zIn[iOut] = '\0';
   293    315     }
   294    316   }
   295    317   
   296    318   /*
................................................................................
   309    331     char **pzErr
   310    332   ){
   311    333     int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
   312    334     int nFile = 0;
   313    335     const char *zFile = 0;
   314    336     ZipfileTab *pNew = 0;
   315    337     int rc;
          338  +
          339  +  /* If the table name is not "zipfile", require that the argument be
          340  +  ** specified. This stops zipfile tables from being created as:
          341  +  **
          342  +  **   CREATE VIRTUAL TABLE zzz USING zipfile();
          343  +  **
          344  +  ** It does not prevent:
          345  +  **
          346  +  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
          347  +  */
          348  +  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
          349  +  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
          350  +    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
          351  +    return SQLITE_ERROR;
          352  +  }
   316    353   
   317    354     if( argc>3 ){
   318    355       zFile = argv[3];
   319    356       nFile = (int)strlen(zFile)+1;
   320    357     }
   321    358   
   322    359     rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
   323    360     if( rc==SQLITE_OK ){
   324    361       pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
   325    362       if( pNew==0 ) return SQLITE_NOMEM;
   326    363       memset(pNew, 0, nByte+nFile);
          364  +    pNew->db = db;
   327    365       pNew->aBuffer = (u8*)&pNew[1];
   328    366       if( zFile ){
   329    367         pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
   330    368         memcpy(pNew->zFile, zFile, nFile);
   331    369         zipfileDequote(pNew->zFile);
   332    370       }
   333    371     }
   334    372     *ppVtab = (sqlite3_vtab*)pNew;
   335    373     return rc;
   336    374   }
          375  +
          376  +/*
          377  +** Free the ZipfileEntry structure indicated by the only argument.
          378  +*/
          379  +static void zipfileEntryFree(ZipfileEntry *p){
          380  +  if( p ){
          381  +    sqlite3_free(p->cds.zFile);
          382  +    sqlite3_free(p);
          383  +  }
          384  +}
          385  +
          386  +/*
          387  +** Release resources that should be freed at the end of a write 
          388  +** transaction.
          389  +*/
          390  +static void zipfileCleanupTransaction(ZipfileTab *pTab){
          391  +  ZipfileEntry *pEntry;
          392  +  ZipfileEntry *pNext;
          393  +
          394  +  if( pTab->pWriteFd ){
          395  +    fclose(pTab->pWriteFd);
          396  +    pTab->pWriteFd = 0;
          397  +  }
          398  +  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
          399  +    pNext = pEntry->pNext;
          400  +    zipfileEntryFree(pEntry);
          401  +  }
          402  +  pTab->pFirstEntry = 0;
          403  +  pTab->pLastEntry = 0;
          404  +  pTab->szCurrent = 0;
          405  +  pTab->szOrig = 0;
          406  +}
   337    407   
   338    408   /*
   339    409   ** This method is the destructor for zipfile vtab objects.
   340    410   */
   341    411   static int zipfileDisconnect(sqlite3_vtab *pVtab){
          412  +  zipfileCleanupTransaction((ZipfileTab*)pVtab);
   342    413     sqlite3_free(pVtab);
   343    414     return SQLITE_OK;
   344    415   }
   345    416   
   346    417   /*
   347    418   ** Constructor for a new ZipfileCsr object.
   348    419   */
................................................................................
   362    433   }
   363    434   
   364    435   /*
   365    436   ** Reset a cursor back to the state it was in when first returned
   366    437   ** by zipfileOpen().
   367    438   */
   368    439   static void zipfileResetCursor(ZipfileCsr *pCsr){
   369         -  sqlite3_free(pCsr->cds.zFile);
   370         -  pCsr->cds.zFile = 0;
          440  +  ZipfileEntry *p;
          441  +  ZipfileEntry *pNext;
          442  +
   371    443     pCsr->bEof = 0;
   372    444     if( pCsr->pFile ){
   373    445       fclose(pCsr->pFile);
   374    446       pCsr->pFile = 0;
          447  +    zipfileEntryFree(pCsr->pCurrent);
          448  +    pCsr->pCurrent = 0;
          449  +  }
          450  +
          451  +  for(p=pCsr->pFreeEntry; p; p=pNext){
          452  +    pNext = p->pNext;
          453  +    zipfileEntryFree(p);
   375    454     }
   376    455   }
   377    456   
   378    457   /*
   379    458   ** Destructor for an ZipfileCsr.
   380    459   */
   381    460   static int zipfileClose(sqlite3_vtab_cursor *cur){
   382    461     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
   383    462     ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
   384    463     ZipfileCsr **pp;
   385    464     zipfileResetCursor(pCsr);
   386    465   
   387    466     /* Remove this cursor from the ZipfileTab.pCsrList list. */
   388         -  for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
   389         -    if( *pp==pCsr ){ 
   390         -      *pp = pCsr->pCsrNext;
   391         -      break;
   392         -    }
   393         -  }
          467  +  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
          468  +  *pp = pCsr->pCsrNext;
   394    469   
   395    470     sqlite3_free(pCsr);
   396    471     return SQLITE_OK;
   397    472   }
   398    473   
   399    474   /*
   400    475   ** Set the error message for the virtual table associated with cursor
   401    476   ** pCsr to the results of vprintf(zFmt, ...).
   402    477   */
   403         -static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
          478  +static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
          479  +  va_list ap;
          480  +  va_start(ap, zFmt);
          481  +  sqlite3_free(pTab->base.zErrMsg);
          482  +  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
          483  +  va_end(ap);
          484  +}
          485  +static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
   404    486     va_list ap;
   405    487     va_start(ap, zFmt);
          488  +  sqlite3_free(pCsr->base.pVtab->zErrMsg);
   406    489     pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
   407    490     va_end(ap);
   408    491   }
   409    492   
          493  +/*
          494  +** Read nRead bytes of data from offset iOff of file pFile into buffer
          495  +** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
          496  +** otherwise. 
          497  +**
          498  +** If an error does occur, output variable (*pzErrmsg) may be set to point
          499  +** to an English language error message. It is the responsibility of the
          500  +** caller to eventually free this buffer using
          501  +** sqlite3_free().
          502  +*/
   410    503   static int zipfileReadData(
   411    504     FILE *pFile,                    /* Read from this file */
   412    505     u8 *aRead,                      /* Read into this buffer */
   413    506     int nRead,                      /* Number of bytes to read */
   414    507     i64 iOff,                       /* Offset to read from */
   415    508     char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
   416    509   ){
................................................................................
   436    529       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
   437    530       return SQLITE_ERROR;
   438    531     }
   439    532     pTab->szCurrent += nWrite;
   440    533     return SQLITE_OK;
   441    534   }
   442    535   
          536  +/*
          537  +** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
          538  +*/
   443    539   static u16 zipfileGetU16(const u8 *aBuf){
   444    540     return (aBuf[1] << 8) + aBuf[0];
   445    541   }
          542  +
          543  +/*
          544  +** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
          545  +*/
   446    546   static u32 zipfileGetU32(const u8 *aBuf){
   447    547     return ((u32)(aBuf[3]) << 24)
   448    548          + ((u32)(aBuf[2]) << 16)
   449    549          + ((u32)(aBuf[1]) <<  8)
   450    550          + ((u32)(aBuf[0]) <<  0);
   451    551   }
   452    552   
          553  +/*
          554  +** Write a 16-bit little endiate integer into buffer aBuf.
          555  +*/
   453    556   static void zipfilePutU16(u8 *aBuf, u16 val){
   454    557     aBuf[0] = val & 0xFF;
   455    558     aBuf[1] = (val>>8) & 0xFF;
   456    559   }
          560  +
          561  +/*
          562  +** Write a 32-bit little endiate integer into buffer aBuf.
          563  +*/
   457    564   static void zipfilePutU32(u8 *aBuf, u32 val){
   458    565     aBuf[0] = val & 0xFF;
   459    566     aBuf[1] = (val>>8) & 0xFF;
   460    567     aBuf[2] = (val>>16) & 0xFF;
   461    568     aBuf[3] = (val>>24) & 0xFF;
   462    569   }
   463    570   
   464    571   #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
   465    572   #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
   466    573   
   467    574   #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
   468    575   #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
   469    576   
   470         -static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
   471         -  return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
   472         -}
   473         -
   474    577   /*
   475    578   ** Magic numbers used to read CDS records.
   476    579   */
   477         -#define ZIPFILE_CDS_FIXED_SZ         46
   478    580   #define ZIPFILE_CDS_NFILE_OFF        28
          581  +#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
   479    582   
   480    583   /*
   481    584   ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
   482    585   ** if the record is not well-formed, or SQLITE_OK otherwise.
   483    586   */
   484    587   static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
   485    588     u8 *aRead = aBuf;
................................................................................
   508    611       assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
   509    612     }
   510    613   
   511    614     return rc;
   512    615   }
   513    616   
   514    617   /*
   515         -** Read the CDS record for the current entry from disk into pCsr->cds.
          618  +** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
          619  +** if the record is not well-formed, or SQLITE_OK otherwise.
   516    620   */
   517         -static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
   518         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
   519         -  u8 *aRead;
          621  +static int zipfileReadLFH(
          622  +  u8 *aBuffer,
          623  +  ZipfileLFH *pLFH
          624  +){
          625  +  u8 *aRead = aBuffer;
   520    626     int rc = SQLITE_OK;
   521    627   
   522         -  sqlite3_free(pCsr->cds.zFile);
   523         -  pCsr->cds.zFile = 0;
   524         -
   525         -  if( pCsr->pCurrent==0 ){
   526         -    aRead = zipfileCsrBuffer(pCsr);
   527         -    rc = zipfileReadData(
   528         -        pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
   529         -    );
   530         -  }else{
   531         -    aRead = pCsr->pCurrent->aCdsEntry;
   532         -  }
   533         -
   534         -  if( rc==SQLITE_OK ){
   535         -    rc = zipfileReadCDS(aRead, &pCsr->cds);
   536         -    if( rc!=SQLITE_OK ){
   537         -      assert( pCsr->pCurrent==0 );
   538         -      zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
   539         -    }else{
   540         -      int nRead;
   541         -      if( pCsr->pCurrent==0 ){
   542         -        nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
   543         -        aRead = zipfileCsrBuffer(pCsr);
   544         -        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
   545         -        rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
   546         -      }else{
   547         -        aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
   548         -      }
   549         -
   550         -      if( rc==SQLITE_OK ){
   551         -        pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
   552         -        pCsr->iNextOff += pCsr->cds.nFile;
   553         -        pCsr->iNextOff += pCsr->cds.nExtra;
   554         -        pCsr->iNextOff += pCsr->cds.nComment;
   555         -      }
   556         -
   557         -      /* Scan the cds.nExtra bytes of "extra" fields for any that can
   558         -      ** be interpreted. The general format of an extra field is:
   559         -      **
   560         -      **   Header ID    2 bytes
   561         -      **   Data Size    2 bytes
   562         -      **   Data         N bytes
   563         -      **
   564         -      */
   565         -      if( rc==SQLITE_OK ){
   566         -        u8 *p = &aRead[pCsr->cds.nFile];
   567         -        u8 *pEnd = &p[pCsr->cds.nExtra];
   568         -
   569         -        while( p<pEnd ){
   570         -          u16 id = zipfileRead16(p);
   571         -          u16 nByte = zipfileRead16(p);
   572         -
   573         -          switch( id ){
   574         -            case ZIPFILE_EXTRA_TIMESTAMP: {
   575         -              u8 b = p[0];
   576         -              if( b & 0x01 ){     /* 0x01 -> modtime is present */
   577         -                pCsr->mTime = zipfileGetU32(&p[1]);
   578         -                pCsr->flags |= ZIPFILE_MTIME_VALID;
   579         -              }
   580         -              break;
   581         -            }
   582         -          }
   583         -
   584         -          p += nByte;
          628  +  u32 sig = zipfileRead32(aRead);
          629  +  if( sig!=ZIPFILE_SIGNATURE_LFH ){
          630  +    rc = SQLITE_ERROR;
          631  +  }else{
          632  +    pLFH->iVersionExtract = zipfileRead16(aRead);
          633  +    pLFH->flags = zipfileRead16(aRead);
          634  +    pLFH->iCompression = zipfileRead16(aRead);
          635  +    pLFH->mTime = zipfileRead16(aRead);
          636  +    pLFH->mDate = zipfileRead16(aRead);
          637  +    pLFH->crc32 = zipfileRead32(aRead);
          638  +    pLFH->szCompressed = zipfileRead32(aRead);
          639  +    pLFH->szUncompressed = zipfileRead32(aRead);
          640  +    pLFH->nFile = zipfileRead16(aRead);
          641  +    pLFH->nExtra = zipfileRead16(aRead);
          642  +  }
          643  +  return rc;
          644  +}
          645  +
          646  +
          647  +/*
          648  +** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
          649  +** Scan through this buffer to find an "extra-timestamp" field. If one
          650  +** exists, extract the 32-bit modification-timestamp from it and store
          651  +** the value in output parameter *pmTime.
          652  +**
          653  +** Zero is returned if no extra-timestamp record could be found (and so
          654  +** *pmTime is left unchanged), or non-zero otherwise.
          655  +**
          656  +** The general format of an extra field is:
          657  +**
          658  +**   Header ID    2 bytes
          659  +**   Data Size    2 bytes
          660  +**   Data         N bytes
          661  +*/
          662  +static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
          663  +  int ret = 0;
          664  +  u8 *p = aExtra;
          665  +  u8 *pEnd = &aExtra[nExtra];
          666  +
          667  +  while( p<pEnd ){
          668  +    u16 id = zipfileRead16(p);
          669  +    u16 nByte = zipfileRead16(p);
          670  +
          671  +    switch( id ){
          672  +      case ZIPFILE_EXTRA_TIMESTAMP: {
          673  +        u8 b = p[0];
          674  +        if( b & 0x01 ){     /* 0x01 -> modtime is present */
          675  +          *pmTime = zipfileGetU32(&p[1]);
          676  +          ret = 1;
   585    677           }
   586         -      }
   587         -    }
   588         -  }
   589         -
   590         -  return rc;
   591         -}
   592         -
   593         -static FILE *zipfileGetFd(ZipfileCsr *pCsr){
   594         -  if( pCsr->pFile ) return pCsr->pFile;
   595         -  return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
   596         -}
   597         -
   598         -static int zipfileReadLFH(
   599         -  FILE *pFd, 
   600         -  i64 iOffset,
   601         -  u8 *aTmp, 
   602         -  ZipfileLFH *pLFH, 
   603         -  char **pzErr
   604         -){
   605         -  u8 *aRead = aTmp;
   606         -  static const int szFix = ZIPFILE_LFH_FIXED_SZ;
   607         -  int rc;
   608         -
   609         -  rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
   610         -  if( rc==SQLITE_OK ){
   611         -    u32 sig = zipfileRead32(aRead);
   612         -    if( sig!=ZIPFILE_SIGNATURE_LFH ){
   613         -      *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
   614         -      rc = SQLITE_ERROR;
   615         -    }else{
   616         -      pLFH->iVersionExtract = zipfileRead16(aRead);
   617         -      pLFH->flags = zipfileRead16(aRead);
   618         -      pLFH->iCompression = zipfileRead16(aRead);
   619         -      pLFH->mTime = zipfileRead16(aRead);
   620         -      pLFH->mDate = zipfileRead16(aRead);
   621         -      pLFH->crc32 = zipfileRead32(aRead);
   622         -      pLFH->szCompressed = zipfileRead32(aRead);
   623         -      pLFH->szUncompressed = zipfileRead32(aRead);
   624         -      pLFH->nFile = zipfileRead16(aRead);
   625         -      pLFH->nExtra = zipfileRead16(aRead);
   626         -      assert( aRead==&aTmp[szFix] );
   627         -    }
   628         -  }
   629         -  return rc;
   630         -}
   631         -
   632         -static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
   633         -  FILE *pFile = zipfileGetFd(pCsr);
   634         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
   635         -  u8 *aRead = zipfileCsrBuffer(pCsr);
   636         -  int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
   637         -  pCsr->iDataOff =  pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
   638         -  pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
   639         -  return rc;
   640         -}
   641         -
   642         -
   643         -/*
   644         -** Advance an ZipfileCsr to its next row of output.
   645         -*/
   646         -static int zipfileNext(sqlite3_vtab_cursor *cur){
   647         -  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
   648         -  int rc = SQLITE_OK;
   649         -  pCsr->flags = 0;
   650         -
   651         -  if( pCsr->pCurrent==0 ){
   652         -    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
   653         -    if( pCsr->iNextOff>=iEof ){
   654         -      pCsr->bEof = 1;
   655         -    }
   656         -  }else{
   657         -    assert( pCsr->pFile==0 );
   658         -    do {
   659         -      pCsr->pCurrent = pCsr->pCurrent->pNext;
   660         -    }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
   661         -    if( pCsr->pCurrent==0 ){
   662         -      pCsr->bEof = 1;
   663         -    }
   664         -  }
   665         -
   666         -  if( pCsr->bEof==0 ){
   667         -    rc = zipfileCsrReadCDS(pCsr);
   668         -    if( rc==SQLITE_OK ){
   669         -      rc = zipfileCsrReadLFH(pCsr);
   670         -    }
   671         -  }
   672         -
   673         -  return rc;
   674         -}
   675         -
   676         -/*
          678  +        break;
          679  +      }
          680  +    }
          681  +
          682  +    p += nByte;
          683  +  }
          684  +  return ret;
          685  +}
          686  +
          687  +/*
          688  +** Convert the standard MS-DOS timestamp stored in the mTime and mDate
          689  +** fields of the CDS structure passed as the only argument to a 32-bit
          690  +** UNIX seconds-since-the-epoch timestamp. Return the result.
          691  +**
   677    692   ** "Standard" MS-DOS time format:
   678    693   **
   679    694   **   File modification time:
   680    695   **     Bits 00-04: seconds divided by 2
   681    696   **     Bits 05-10: minute
   682    697   **     Bits 11-15: hour
   683    698   **   File modification date:
   684    699   **     Bits 00-04: day
   685    700   **     Bits 05-08: month (1-12)
   686    701   **     Bits 09-15: years from 1980 
   687         -*/
   688         -static time_t zipfileMtime(ZipfileCsr *pCsr){
   689         -  struct tm t;
   690         -  memset(&t, 0, sizeof(t));
   691         -  t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
   692         -  t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
   693         -  t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
   694         -
   695         -  t.tm_mday = (pCsr->cds.mDate & 0x1F);
   696         -  t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
   697         -  t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
   698         -
   699         -  return mktime(&t);
   700         -}
   701         -
   702         -static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
   703         -  time_t t = (time_t)mTime;
   704         -  struct tm res;
   705         -
   706         -#if !defined(_WIN32) && !defined(WIN32)
   707         -  localtime_r(&t, &res);
   708         -#else
   709         -  memcpy(&res, localtime(&t), sizeof(struct tm));
   710         -#endif
   711         -
   712         -  pCds->mTime = (u16)(
   713         -    (res.tm_sec / 2) + 
   714         -    (res.tm_min << 5) +
   715         -    (res.tm_hour << 11));
   716         -
   717         -  pCds->mDate = (u16)(
   718         -    (res.tm_mday-1) +
   719         -    ((res.tm_mon+1) << 5) +
   720         -    ((res.tm_year-80) << 9));
   721         -}
   722         -
          702  +**
          703  +** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
          704  +*/
          705  +static u32 zipfileMtime(ZipfileCDS *pCDS){
          706  +  int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
          707  +  int M = ((pCDS->mDate >> 5) & 0x0F);
          708  +  int D = (pCDS->mDate & 0x1F);
          709  +  int B = -13;
          710  +
          711  +  int sec = (pCDS->mTime & 0x1F)*2;
          712  +  int min = (pCDS->mTime >> 5) & 0x3F;
          713  +  int hr = (pCDS->mTime >> 11) & 0x1F;
          714  +  i64 JD;
          715  +
          716  +  /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
          717  +
          718  +  /* Calculate the JD in seconds for noon on the day in question */
          719  +  if( M<3 ){
          720  +    Y = Y-1;
          721  +    M = M+12;
          722  +  }
          723  +  JD = (i64)(24*60*60) * (
          724  +      (int)(365.25 * (Y + 4716))
          725  +    + (int)(30.6001 * (M + 1))
          726  +    + D + B - 1524
          727  +  );
          728  +
          729  +  /* Correct the JD for the time within the day */
          730  +  JD += (hr-12) * 3600 + min * 60 + sec;
          731  +
          732  +  /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
          733  +  return (u32)(JD - (i64)(24405875) * 24*60*6);
          734  +}
          735  +
          736  +/*
          737  +** The opposite of zipfileMtime(). This function populates the mTime and
          738  +** mDate fields of the CDS structure passed as the first argument according
          739  +** to the UNIX timestamp value passed as the second.
          740  +*/
          741  +static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
          742  +  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
          743  +  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
          744  +
          745  +  int A, B, C, D, E;
          746  +  int yr, mon, day;
          747  +  int hr, min, sec;
          748  +
          749  +  A = (int)((JD - 1867216.25)/36524.25);
          750  +  A = (int)(JD + 1 + A - (A/4));
          751  +  B = A + 1524;
          752  +  C = (int)((B - 122.1)/365.25);
          753  +  D = (36525*(C&32767))/100;
          754  +  E = (int)((B-D)/30.6001);
          755  +
          756  +  day = B - D - (int)(30.6001*E);
          757  +  mon = (E<14 ? E-1 : E-13);
          758  +  yr = mon>2 ? C-4716 : C-4715;
          759  +
          760  +  hr = (mUnixTime % (24*60*60)) / (60*60);
          761  +  min = (mUnixTime % (60*60)) / 60;
          762  +  sec = (mUnixTime % 60);
          763  +
          764  +  if( yr>=1980 ){
          765  +    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
          766  +    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
          767  +  }else{
          768  +    pCds->mDate = pCds->mTime = 0;
          769  +  }
          770  +
          771  +  assert( mUnixTime<315507600 
          772  +       || mUnixTime==zipfileMtime(pCds) 
          773  +       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
          774  +       /* || (mUnixTime % 2) */
          775  +  );
          776  +}
          777  +
          778  +/*
          779  +** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
          780  +** size) containing an entire zip archive image. Or, if aBlob is NULL,
          781  +** then pFile is a file-handle open on a zip file. In either case, this
          782  +** function creates a ZipfileEntry object based on the zip archive entry
          783  +** for which the CDS record is at offset iOff.
          784  +**
          785  +** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
          786  +** the new object. Otherwise, an SQLite error code is returned and the
          787  +** final value of (*ppEntry) undefined.
          788  +*/
          789  +static int zipfileGetEntry(
          790  +  ZipfileTab *pTab,               /* Store any error message here */
          791  +  const u8 *aBlob,                /* Pointer to in-memory file image */
          792  +  int nBlob,                      /* Size of aBlob[] in bytes */
          793  +  FILE *pFile,                    /* If aBlob==0, read from this file */
          794  +  i64 iOff,                       /* Offset of CDS record */
          795  +  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
          796  +){
          797  +  u8 *aRead;
          798  +  char **pzErr = &pTab->base.zErrMsg;
          799  +  int rc = SQLITE_OK;
          800  +
          801  +  if( aBlob==0 ){
          802  +    aRead = pTab->aBuffer;
          803  +    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
          804  +  }else{
          805  +    aRead = (u8*)&aBlob[iOff];
          806  +  }
          807  +
          808  +  if( rc==SQLITE_OK ){
          809  +    int nAlloc;
          810  +    ZipfileEntry *pNew;
          811  +
          812  +    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
          813  +    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
          814  +    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
          815  +
          816  +    nAlloc = sizeof(ZipfileEntry) + nExtra;
          817  +    if( aBlob ){
          818  +      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
          819  +    }
          820  +
          821  +    pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
          822  +    if( pNew==0 ){
          823  +      rc = SQLITE_NOMEM;
          824  +    }else{
          825  +      memset(pNew, 0, sizeof(ZipfileEntry));
          826  +      rc = zipfileReadCDS(aRead, &pNew->cds);
          827  +      if( rc!=SQLITE_OK ){
          828  +        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
          829  +      }else if( aBlob==0 ){
          830  +        rc = zipfileReadData(
          831  +            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
          832  +        );
          833  +      }else{
          834  +        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
          835  +      }
          836  +    }
          837  +
          838  +    if( rc==SQLITE_OK ){
          839  +      u32 *pt = &pNew->mUnixTime;
          840  +      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
          841  +      pNew->aExtra = (u8*)&pNew[1];
          842  +      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
          843  +      if( pNew->cds.zFile==0 ){
          844  +        rc = SQLITE_NOMEM;
          845  +      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
          846  +        pNew->mUnixTime = zipfileMtime(&pNew->cds);
          847  +      }
          848  +    }
          849  +
          850  +    if( rc==SQLITE_OK ){
          851  +      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
          852  +      ZipfileLFH lfh;
          853  +      if( pFile ){
          854  +        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
          855  +      }else{
          856  +        aRead = (u8*)&aBlob[pNew->cds.iOffset];
          857  +      }
          858  +
          859  +      rc = zipfileReadLFH(aRead, &lfh);
          860  +      if( rc==SQLITE_OK ){
          861  +        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
          862  +        pNew->iDataOff += lfh.nFile + lfh.nExtra;
          863  +        if( aBlob && pNew->cds.szCompressed ){
          864  +          pNew->aData = &pNew->aExtra[nExtra];
          865  +          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
          866  +        }
          867  +      }else{
          868  +        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
          869  +            (int)pNew->cds.iOffset
          870  +        );
          871  +      }
          872  +    }
          873  +
          874  +    if( rc!=SQLITE_OK ){
          875  +      zipfileEntryFree(pNew);
          876  +    }else{
          877  +      *ppEntry = pNew;
          878  +    }
          879  +  }
          880  +
          881  +  return rc;
          882  +}
          883  +
          884  +/*
          885  +** Advance an ZipfileCsr to its next row of output.
          886  +*/
          887  +static int zipfileNext(sqlite3_vtab_cursor *cur){
          888  +  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
          889  +  int rc = SQLITE_OK;
          890  +
          891  +  if( pCsr->pFile ){
          892  +    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
          893  +    zipfileEntryFree(pCsr->pCurrent);
          894  +    pCsr->pCurrent = 0;
          895  +    if( pCsr->iNextOff>=iEof ){
          896  +      pCsr->bEof = 1;
          897  +    }else{
          898  +      ZipfileEntry *p = 0;
          899  +      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
          900  +      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
          901  +      if( rc==SQLITE_OK ){
          902  +        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
          903  +        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
          904  +      }
          905  +      pCsr->pCurrent = p;
          906  +    }
          907  +  }else{
          908  +    if( !pCsr->bNoop ){
          909  +      pCsr->pCurrent = pCsr->pCurrent->pNext;
          910  +    }
          911  +    if( pCsr->pCurrent==0 ){
          912  +      pCsr->bEof = 1;
          913  +    }
          914  +  }
          915  +
          916  +  pCsr->bNoop = 0;
          917  +  return rc;
          918  +}
          919  +
          920  +static void zipfileFree(void *p) { 
          921  +  sqlite3_free(p); 
          922  +}
          923  +
          924  +/*
          925  +** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
          926  +** size is nOut bytes. This function uncompresses the data and sets the
          927  +** return value in context pCtx to the result (a blob).
          928  +**
          929  +** If an error occurs, an error code is left in pCtx instead.
          930  +*/
   723    931   static void zipfileInflate(
   724         -  sqlite3_context *pCtx,          /* Store error here, if any */
          932  +  sqlite3_context *pCtx,          /* Store result here */
   725    933     const u8 *aIn,                  /* Compressed data */
   726    934     int nIn,                        /* Size of buffer aIn[] in bytes */
   727    935     int nOut                        /* Expected output size */
   728    936   ){
   729    937     u8 *aRes = sqlite3_malloc(nOut);
   730    938     if( aRes==0 ){
   731    939       sqlite3_result_error_nomem(pCtx);
................................................................................
   743    951       if( err!=Z_OK ){
   744    952         zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
   745    953       }else{
   746    954         err = inflate(&str, Z_NO_FLUSH);
   747    955         if( err!=Z_STREAM_END ){
   748    956           zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
   749    957         }else{
   750         -        sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
          958  +        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
          959  +        aRes = 0;
   751    960         }
   752    961       }
   753    962       sqlite3_free(aRes);
   754    963       inflateEnd(&str);
   755    964     }
   756    965   }
   757    966   
          967  +/*
          968  +** Buffer aIn (size nIn bytes) contains uncompressed data. This function
          969  +** compresses it and sets (*ppOut) to point to a buffer containing the
          970  +** compressed data. The caller is responsible for eventually calling
          971  +** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
          972  +** is set to the size of buffer (*ppOut) in bytes.
          973  +**
          974  +** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
          975  +** code is returned and an error message left in virtual-table handle
          976  +** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
          977  +** case.
          978  +*/
   758    979   static int zipfileDeflate(
   759         -  ZipfileTab *pTab,               /* Set error message here */
   760    980     const u8 *aIn, int nIn,         /* Input */
   761         -  u8 **ppOut, int *pnOut          /* Output */
          981  +  u8 **ppOut, int *pnOut,         /* Output */
          982  +  char **pzErr                    /* OUT: Error message */
   762    983   ){
   763    984     int nAlloc = (int)compressBound(nIn);
   764    985     u8 *aOut;
   765    986     int rc = SQLITE_OK;
   766    987   
   767    988     aOut = (u8*)sqlite3_malloc(nAlloc);
   768    989     if( aOut==0 ){
................................................................................
   780   1001       res = deflate(&str, Z_FINISH);
   781   1002   
   782   1003       if( res==Z_STREAM_END ){
   783   1004         *ppOut = aOut;
   784   1005         *pnOut = (int)str.total_out;
   785   1006       }else{
   786   1007         sqlite3_free(aOut);
   787         -      pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
         1008  +      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
   788   1009         rc = SQLITE_ERROR;
   789   1010       }
   790   1011       deflateEnd(&str);
   791   1012     }
   792   1013   
   793   1014     return rc;
   794   1015   }
................................................................................
   800   1021   */
   801   1022   static int zipfileColumn(
   802   1023     sqlite3_vtab_cursor *cur,   /* The cursor */
   803   1024     sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
   804   1025     int i                       /* Which column to return */
   805   1026   ){
   806   1027     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
         1028  +  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
   807   1029     int rc = SQLITE_OK;
   808   1030     switch( i ){
   809   1031       case 0:   /* name */
   810         -      sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
         1032  +      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
   811   1033         break;
   812   1034       case 1:   /* mode */
   813   1035         /* TODO: Whether or not the following is correct surely depends on
   814   1036         ** the platform on which the archive was created.  */
   815         -      sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
         1037  +      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
   816   1038         break;
   817   1039       case 2: { /* mtime */
   818         -      if( pCsr->flags & ZIPFILE_MTIME_VALID ){
   819         -        sqlite3_result_int64(ctx, pCsr->mTime);
   820         -      }else{
   821         -        sqlite3_result_int64(ctx, zipfileMtime(pCsr));
   822         -      }
         1040  +      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
   823   1041         break;
   824   1042       }
   825   1043       case 3: { /* sz */
   826   1044         if( sqlite3_vtab_nochange(ctx)==0 ){
   827         -        sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
         1045  +        sqlite3_result_int64(ctx, pCDS->szUncompressed);
   828   1046         }
   829   1047         break;
   830   1048       }
   831   1049       case 4:   /* rawdata */
   832   1050         if( sqlite3_vtab_nochange(ctx) ) break;
   833   1051       case 5: { /* data */
   834         -      if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
   835         -        int sz = pCsr->cds.szCompressed;
   836         -        int szFinal = pCsr->cds.szUncompressed;
         1052  +      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
         1053  +        int sz = pCDS->szCompressed;
         1054  +        int szFinal = pCDS->szUncompressed;
   837   1055           if( szFinal>0 ){
   838         -          u8 *aBuf = sqlite3_malloc(sz);
   839         -          if( aBuf==0 ){
   840         -            rc = SQLITE_NOMEM;
         1056  +          u8 *aBuf;
         1057  +          u8 *aFree = 0;
         1058  +          if( pCsr->pCurrent->aData ){
         1059  +            aBuf = pCsr->pCurrent->aData;
   841   1060             }else{
   842         -            FILE *pFile = zipfileGetFd(pCsr);
   843         -            rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
   844         -                &pCsr->base.pVtab->zErrMsg
   845         -            );
         1061  +            aBuf = aFree = sqlite3_malloc(sz);
         1062  +            if( aBuf==0 ){
         1063  +              rc = SQLITE_NOMEM;
         1064  +            }else{
         1065  +              FILE *pFile = pCsr->pFile;
         1066  +              if( pFile==0 ){
         1067  +                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
         1068  +              }
         1069  +              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
         1070  +                  &pCsr->base.pVtab->zErrMsg
         1071  +              );
         1072  +            }
   846   1073             }
   847   1074             if( rc==SQLITE_OK ){
   848         -            if( i==5 && pCsr->cds.iCompression ){
         1075  +            if( i==5 && pCDS->iCompression ){
   849   1076                 zipfileInflate(ctx, aBuf, sz, szFinal);
   850   1077               }else{
   851   1078                 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
   852   1079               }
   853         -            sqlite3_free(aBuf);
   854   1080             }
         1081  +          sqlite3_free(aFree);
   855   1082           }else{
   856   1083             /* Figure out if this is a directory or a zero-sized file. Consider
   857   1084             ** it to be a directory either if the mode suggests so, or if
   858   1085             ** the final character in the name is '/'.  */
   859         -          u32 mode = pCsr->cds.iExternalAttr >> 16;
   860         -          if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
         1086  +          u32 mode = pCDS->iExternalAttr >> 16;
         1087  +          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
   861   1088               sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
   862   1089             }
   863   1090           }
   864   1091         }
   865   1092         break;
   866   1093       }
   867   1094       case 6:   /* method */
   868         -      sqlite3_result_int(ctx, pCsr->cds.iCompression);
         1095  +      sqlite3_result_int(ctx, pCDS->iCompression);
   869   1096         break;
   870         -    case 7:   /* z */
         1097  +    default:  /* z */
         1098  +      assert( i==7 );
   871   1099         sqlite3_result_int64(ctx, pCsr->iId);
   872   1100         break;
   873   1101     }
   874   1102   
   875   1103     return rc;
   876   1104   }
   877   1105   
   878   1106   /*
   879         -** Return the rowid for the current row.
   880         -*/
   881         -static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
   882         -  assert( 0 );
   883         -  return SQLITE_OK;
   884         -}
   885         -
   886         -/*
   887         -** Return TRUE if the cursor has been moved off of the last
   888         -** row of output.
         1107  +** Return TRUE if the cursor is at EOF.
   889   1108   */
   890   1109   static int zipfileEof(sqlite3_vtab_cursor *cur){
   891   1110     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
   892   1111     return pCsr->bEof;
   893   1112   }
   894   1113   
   895   1114   /*
         1115  +** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
         1116  +** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
         1117  +** is guaranteed to be a file-handle open on a zip file.
         1118  +**
         1119  +** This function attempts to locate the EOCD record within the zip archive
         1120  +** and populate *pEOCD with the results of decoding it. SQLITE_OK is
         1121  +** returned if successful. Otherwise, an SQLite error code is returned and
         1122  +** an English language error message may be left in virtual-table pTab.
   896   1123   */
   897   1124   static int zipfileReadEOCD(
   898   1125     ZipfileTab *pTab,               /* Return errors here */
   899         -  FILE *pFile,                    /* Read from this file */
         1126  +  const u8 *aBlob,                /* Pointer to in-memory file image */
         1127  +  int nBlob,                      /* Size of aBlob[] in bytes */
         1128  +  FILE *pFile,                    /* Read from this file if aBlob==0 */
   900   1129     ZipfileEOCD *pEOCD              /* Object to populate */
   901   1130   ){
   902   1131     u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
   903         -  i64 szFile;                     /* Total size of file in bytes */
   904   1132     int nRead;                      /* Bytes to read from file */
   905         -  i64 iOff;                       /* Offset to read from */
   906         -  int rc;
         1133  +  int rc = SQLITE_OK;
   907   1134   
   908         -  fseek(pFile, 0, SEEK_END);
   909         -  szFile = (i64)ftell(pFile);
   910         -  if( szFile==0 ){
   911         -    memset(pEOCD, 0, sizeof(ZipfileEOCD));
   912         -    return SQLITE_OK;
         1135  +  if( aBlob==0 ){
         1136  +    i64 iOff;                     /* Offset to read from */
         1137  +    i64 szFile;                   /* Total size of file in bytes */
         1138  +    fseek(pFile, 0, SEEK_END);
         1139  +    szFile = (i64)ftell(pFile);
         1140  +    if( szFile==0 ){
         1141  +      memset(pEOCD, 0, sizeof(ZipfileEOCD));
         1142  +      return SQLITE_OK;
         1143  +    }
         1144  +    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
         1145  +    iOff = szFile - nRead;
         1146  +    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
         1147  +  }else{
         1148  +    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
         1149  +    aRead = (u8*)&aBlob[nBlob-nRead];
   913   1150     }
   914         -  nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
   915         -  iOff = szFile - nRead;
   916   1151   
   917         -  rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
   918   1152     if( rc==SQLITE_OK ){
   919   1153       int i;
   920   1154   
   921   1155       /* Scan backwards looking for the signature bytes */
   922   1156       for(i=nRead-20; i>=0; i--){
   923   1157         if( aRead[i]==0x50 && aRead[i+1]==0x4b 
   924   1158          && aRead[i+2]==0x05 && aRead[i+3]==0x06 
................................................................................
   936   1170       aRead += i+4;
   937   1171       pEOCD->iDisk = zipfileRead16(aRead);
   938   1172       pEOCD->iFirstDisk = zipfileRead16(aRead);
   939   1173       pEOCD->nEntry = zipfileRead16(aRead);
   940   1174       pEOCD->nEntryTotal = zipfileRead16(aRead);
   941   1175       pEOCD->nSize = zipfileRead32(aRead);
   942   1176       pEOCD->iOffset = zipfileRead32(aRead);
   943         -
   944         -#if 0
   945         -    printf("iDisk=%d  iFirstDisk=%d  nEntry=%d  "
   946         -           "nEntryTotal=%d  nSize=%d  iOffset=%d", 
   947         -           (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
   948         -           (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
   949         -    );
   950         -#endif
   951         -  }
   952         -
   953         -  return SQLITE_OK;
         1177  +  }
         1178  +
         1179  +  return rc;
         1180  +}
         1181  +
         1182  +/*
         1183  +** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
         1184  +** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
         1185  +** to the end of the list. Otherwise, it is added to the list immediately
         1186  +** before pBefore (which is guaranteed to be a part of said list).
         1187  +*/
         1188  +static void zipfileAddEntry(
         1189  +  ZipfileTab *pTab, 
         1190  +  ZipfileEntry *pBefore, 
         1191  +  ZipfileEntry *pNew
         1192  +){
         1193  +  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
         1194  +  assert( pNew->pNext==0 );
         1195  +  if( pBefore==0 ){
         1196  +    if( pTab->pFirstEntry==0 ){
         1197  +      pTab->pFirstEntry = pTab->pLastEntry = pNew;
         1198  +    }else{
         1199  +      assert( pTab->pLastEntry->pNext==0 );
         1200  +      pTab->pLastEntry->pNext = pNew;
         1201  +      pTab->pLastEntry = pNew;
         1202  +    }
         1203  +  }else{
         1204  +    ZipfileEntry **pp;
         1205  +    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
         1206  +    pNew->pNext = pBefore;
         1207  +    *pp = pNew;
         1208  +  }
         1209  +}
         1210  +
         1211  +static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
         1212  +  ZipfileEOCD eocd;
         1213  +  int rc;
         1214  +  int i;
         1215  +  i64 iOff;
         1216  +
         1217  +  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
         1218  +  iOff = eocd.iOffset;
         1219  +  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
         1220  +    ZipfileEntry *pNew = 0;
         1221  +    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
         1222  +
         1223  +    if( rc==SQLITE_OK ){
         1224  +      zipfileAddEntry(pTab, 0, pNew);
         1225  +      iOff += ZIPFILE_CDS_FIXED_SZ;
         1226  +      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
         1227  +    }
         1228  +  }
         1229  +  return rc;
   954   1230   }
   955   1231   
   956   1232   /*
   957   1233   ** xFilter callback.
   958   1234   */
   959   1235   static int zipfileFilter(
   960   1236     sqlite3_vtab_cursor *cur, 
   961   1237     int idxNum, const char *idxStr,
   962   1238     int argc, sqlite3_value **argv
   963   1239   ){
   964   1240     ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
   965   1241     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
   966         -  const char *zFile;              /* Zip file to scan */
         1242  +  const char *zFile = 0;          /* Zip file to scan */
   967   1243     int rc = SQLITE_OK;             /* Return Code */
         1244  +  int bInMemory = 0;              /* True for an in-memory zipfile */
   968   1245   
   969   1246     zipfileResetCursor(pCsr);
   970   1247   
   971   1248     if( pTab->zFile ){
   972   1249       zFile = pTab->zFile;
   973   1250     }else if( idxNum==0 ){
   974         -    /* Error. This is an eponymous virtual table and the user has not 
   975         -    ** supplied a file name. */
   976         -    zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
         1251  +    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
   977   1252       return SQLITE_ERROR;
         1253  +  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
         1254  +    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
         1255  +    int nBlob = sqlite3_value_bytes(argv[0]);
         1256  +    assert( pTab->pFirstEntry==0 );
         1257  +    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
         1258  +    pCsr->pFreeEntry = pTab->pFirstEntry;
         1259  +    pTab->pFirstEntry = pTab->pLastEntry = 0;
         1260  +    if( rc!=SQLITE_OK ) return rc;
         1261  +    bInMemory = 1;
   978   1262     }else{
   979   1263       zFile = (const char*)sqlite3_value_text(argv[0]);
   980   1264     }
   981   1265   
   982         -  if( pTab->pWriteFd==0 ){
         1266  +  if( 0==pTab->pWriteFd && 0==bInMemory ){
   983   1267       pCsr->pFile = fopen(zFile, "rb");
   984   1268       if( pCsr->pFile==0 ){
   985         -      zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
         1269  +      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
   986   1270         rc = SQLITE_ERROR;
   987   1271       }else{
   988         -      rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
         1272  +      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
   989   1273         if( rc==SQLITE_OK ){
   990   1274           if( pCsr->eocd.nEntry==0 ){
   991   1275             pCsr->bEof = 1;
   992   1276           }else{
   993   1277             pCsr->iNextOff = pCsr->eocd.iOffset;
   994   1278             rc = zipfileNext(cur);
   995   1279           }
   996   1280         }
   997   1281       }
   998   1282     }else{
   999         -    ZipfileEntry e;
  1000         -    memset(&e, 0, sizeof(e));
  1001         -    e.pNext = pTab->pFirstEntry;
  1002         -    pCsr->pCurrent = &e;
         1283  +    pCsr->bNoop = 1;
         1284  +    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
  1003   1285       rc = zipfileNext(cur);
  1004         -    assert( pCsr->pCurrent!=&e );
  1005   1286     }
  1006   1287   
  1007   1288     return rc;
  1008   1289   }
  1009   1290   
  1010   1291   /*
  1011   1292   ** xBestIndex callback.
................................................................................
  1033   1314       pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
  1034   1315       pIdxInfo->idxNum = 0;
  1035   1316     }
  1036   1317   
  1037   1318     return SQLITE_OK;
  1038   1319   }
  1039   1320   
  1040         -/*
  1041         -** Add object pNew to the end of the linked list that begins at
  1042         -** ZipfileTab.pFirstEntry and ends with pLastEntry.
  1043         -*/
  1044         -static void zipfileAddEntry(
  1045         -  ZipfileTab *pTab, 
  1046         -  ZipfileEntry *pBefore, 
  1047         -  ZipfileEntry *pNew
  1048         -){
  1049         -  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
  1050         -  assert( pNew->pNext==0 );
  1051         -  if( pBefore==0 ){
  1052         -    if( pTab->pFirstEntry==0 ){
  1053         -      pTab->pFirstEntry = pTab->pLastEntry = pNew;
  1054         -    }else{
  1055         -      assert( pTab->pLastEntry->pNext==0 );
  1056         -      pTab->pLastEntry->pNext = pNew;
  1057         -      pTab->pLastEntry = pNew;
  1058         -    }
  1059         -  }else{
  1060         -    ZipfileEntry **pp;
  1061         -    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
  1062         -    pNew->pNext = pBefore;
  1063         -    *pp = pNew;
  1064         -  }
  1065         -}
  1066         -
  1067         -static int zipfileLoadDirectory(ZipfileTab *pTab){
  1068         -  ZipfileEOCD eocd;
  1069         -  int rc;
  1070         -
  1071         -  rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
  1072         -  if( rc==SQLITE_OK && eocd.nEntry>0 ){
  1073         -    int i;
  1074         -    int iOff = 0;
  1075         -    u8 *aBuf = sqlite3_malloc(eocd.nSize);
  1076         -    if( aBuf==0 ){
  1077         -      rc = SQLITE_NOMEM;
  1078         -    }else{
  1079         -      rc = zipfileReadData(
  1080         -          pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
  1081         -      );
  1082         -    }
  1083         -
  1084         -    for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
  1085         -      u16 nFile;
  1086         -      u16 nExtra;
  1087         -      u16 nComment;
  1088         -      ZipfileEntry *pNew;
  1089         -      u8 *aRec = &aBuf[iOff];
  1090         -
  1091         -      nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
  1092         -      nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
  1093         -      nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
  1094         -
  1095         -      pNew = sqlite3_malloc(
  1096         -          sizeof(ZipfileEntry) 
  1097         -        + nFile+1 
  1098         -        + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
  1099         -      );
  1100         -      if( pNew==0 ){
  1101         -        rc = SQLITE_NOMEM;
  1102         -      }else{
  1103         -        memset(pNew, 0, sizeof(ZipfileEntry));
  1104         -        pNew->zPath = (char*)&pNew[1];
  1105         -        memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
  1106         -        pNew->zPath[nFile] = '\0';
  1107         -        pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
  1108         -        pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  1109         -        memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
  1110         -        zipfileAddEntry(pTab, 0, pNew);
  1111         -      }
  1112         -
  1113         -      iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  1114         -    }
  1115         -
  1116         -    sqlite3_free(aBuf);
  1117         -  }
  1118         -
  1119         -  return rc;
  1120         -}
  1121         -
  1122         -static ZipfileEntry *zipfileNewEntry(
  1123         -  ZipfileCDS *pCds,               /* Values for fixed size part of CDS */
  1124         -  const char *zPath,              /* Path for new entry */
  1125         -  int nPath,                      /* strlen(zPath) */
  1126         -  u32 mTime                       /* Modification time (or 0) */
  1127         -){
  1128         -  u8 *aWrite;
         1321  +static ZipfileEntry *zipfileNewEntry(const char *zPath){
  1129   1322     ZipfileEntry *pNew;
  1130         -  pCds->nFile = (u16)nPath;
  1131         -  pCds->nExtra = mTime ? 9 : 0;
  1132         -  pNew = (ZipfileEntry*)sqlite3_malloc(
  1133         -    sizeof(ZipfileEntry) + 
  1134         -    nPath+1 + 
  1135         -    ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
  1136         -  );
  1137         -
         1323  +  pNew = sqlite3_malloc(sizeof(ZipfileEntry));
  1138   1324     if( pNew ){
  1139   1325       memset(pNew, 0, sizeof(ZipfileEntry));
  1140         -    pNew->zPath = (char*)&pNew[1];
  1141         -    pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
  1142         -    pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
  1143         -    memcpy(pNew->zPath, zPath, nPath+1);
  1144         -
  1145         -    aWrite = pNew->aCdsEntry;
  1146         -    zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
  1147         -    zipfileWrite16(aWrite, pCds->iVersionMadeBy);
  1148         -    zipfileWrite16(aWrite, pCds->iVersionExtract);
  1149         -    zipfileWrite16(aWrite, pCds->flags);
  1150         -    zipfileWrite16(aWrite, pCds->iCompression);
  1151         -    zipfileWrite16(aWrite, pCds->mTime);
  1152         -    zipfileWrite16(aWrite, pCds->mDate);
  1153         -    zipfileWrite32(aWrite, pCds->crc32);
  1154         -    zipfileWrite32(aWrite, pCds->szCompressed);
  1155         -    zipfileWrite32(aWrite, pCds->szUncompressed);
  1156         -    zipfileWrite16(aWrite, pCds->nFile);
  1157         -    zipfileWrite16(aWrite, pCds->nExtra);
  1158         -    zipfileWrite16(aWrite, pCds->nComment);      assert( pCds->nComment==0 );
  1159         -    zipfileWrite16(aWrite, pCds->iDiskStart);
  1160         -    zipfileWrite16(aWrite, pCds->iInternalAttr);
  1161         -    zipfileWrite32(aWrite, pCds->iExternalAttr);
  1162         -    zipfileWrite32(aWrite, pCds->iOffset);
  1163         -    assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
  1164         -    memcpy(aWrite, zPath, nPath);
  1165         -    if( pCds->nExtra ){
  1166         -      aWrite += nPath;
  1167         -      zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
  1168         -      zipfileWrite16(aWrite, 5);
  1169         -      *aWrite++ = 0x01;
  1170         -      zipfileWrite32(aWrite, mTime);
         1326  +    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
         1327  +    if( pNew->cds.zFile==0 ){
         1328  +      sqlite3_free(pNew);
         1329  +      pNew = 0;
  1171   1330       }
  1172   1331     }
  1173         -
  1174   1332     return pNew;
  1175   1333   }
         1334  +
         1335  +static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
         1336  +  ZipfileCDS *pCds = &pEntry->cds;
         1337  +  u8 *a = aBuf;
         1338  +
         1339  +  pCds->nExtra = 9;
         1340  +
         1341  +  /* Write the LFH itself */
         1342  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
         1343  +  zipfileWrite16(a, pCds->iVersionExtract);
         1344  +  zipfileWrite16(a, pCds->flags);
         1345  +  zipfileWrite16(a, pCds->iCompression);
         1346  +  zipfileWrite16(a, pCds->mTime);
         1347  +  zipfileWrite16(a, pCds->mDate);
         1348  +  zipfileWrite32(a, pCds->crc32);
         1349  +  zipfileWrite32(a, pCds->szCompressed);
         1350  +  zipfileWrite32(a, pCds->szUncompressed);
         1351  +  zipfileWrite16(a, (u16)pCds->nFile);
         1352  +  zipfileWrite16(a, pCds->nExtra);
         1353  +  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
         1354  +
         1355  +  /* Add the file name */
         1356  +  memcpy(a, pCds->zFile, (int)pCds->nFile);
         1357  +  a += (int)pCds->nFile;
         1358  +
         1359  +  /* The "extra" data */
         1360  +  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         1361  +  zipfileWrite16(a, 5);
         1362  +  *a++ = 0x01;
         1363  +  zipfileWrite32(a, pEntry->mUnixTime);
         1364  +
         1365  +  return a-aBuf;
         1366  +}
  1176   1367   
  1177   1368   static int zipfileAppendEntry(
  1178   1369     ZipfileTab *pTab,
  1179         -  ZipfileCDS *pCds,
  1180         -  const char *zPath,              /* Path for new entry */
  1181         -  int nPath,                      /* strlen(zPath) */
         1370  +  ZipfileEntry *pEntry,
  1182   1371     const u8 *pData,
  1183         -  int nData,
  1184         -  u32 mTime
         1372  +  int nData
  1185   1373   ){
  1186   1374     u8 *aBuf = pTab->aBuffer;
         1375  +  int nBuf;
  1187   1376     int rc;
  1188   1377   
  1189         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
  1190         -  zipfileWrite16(aBuf, pCds->iVersionExtract);
  1191         -  zipfileWrite16(aBuf, pCds->flags);
  1192         -  zipfileWrite16(aBuf, pCds->iCompression);
  1193         -  zipfileWrite16(aBuf, pCds->mTime);
  1194         -  zipfileWrite16(aBuf, pCds->mDate);
  1195         -  zipfileWrite32(aBuf, pCds->crc32);
  1196         -  zipfileWrite32(aBuf, pCds->szCompressed);
  1197         -  zipfileWrite32(aBuf, pCds->szUncompressed);
  1198         -  zipfileWrite16(aBuf, (u16)nPath);
  1199         -  zipfileWrite16(aBuf, pCds->nExtra);
  1200         -  assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
  1201         -  rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
         1378  +  nBuf = zipfileSerializeLFH(pEntry, aBuf);
         1379  +  rc = zipfileAppendData(pTab, aBuf, nBuf);
  1202   1380     if( rc==SQLITE_OK ){
  1203         -    rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
  1204         -  }
  1205         -
  1206         -  if( rc==SQLITE_OK && pCds->nExtra ){
  1207         -    aBuf = pTab->aBuffer;
  1208         -    zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
  1209         -    zipfileWrite16(aBuf, 5);
  1210         -    *aBuf++ = 0x01;
  1211         -    zipfileWrite32(aBuf, mTime);
  1212         -    rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
  1213         -  }
  1214         -
  1215         -  if( rc==SQLITE_OK ){
         1381  +    pEntry->iDataOff = pTab->szCurrent;
  1216   1382       rc = zipfileAppendData(pTab, pData, nData);
  1217   1383     }
  1218   1384   
  1219   1385     return rc;
  1220   1386   }
  1221   1387   
  1222   1388   static int zipfileGetMode(
  1223         -  ZipfileTab *pTab, 
  1224   1389     sqlite3_value *pVal, 
  1225         -  u32 defaultMode,                /* Value to use if pVal IS NULL */
  1226         -  u32 *pMode
         1390  +  int bIsDir,                     /* If true, default to directory */
         1391  +  u32 *pMode,                     /* OUT: Mode value */
         1392  +  char **pzErr                    /* OUT: Error message */
  1227   1393   ){
  1228   1394     const char *z = (const char*)sqlite3_value_text(pVal);
  1229   1395     u32 mode = 0;
  1230   1396     if( z==0 ){
  1231         -    mode = defaultMode;
         1397  +    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
  1232   1398     }else if( z[0]>='0' && z[0]<='9' ){
  1233   1399       mode = (unsigned int)sqlite3_value_int(pVal);
  1234   1400     }else{
  1235   1401       const char zTemplate[11] = "-rwxrwxrwx";
  1236   1402       int i;
  1237   1403       if( strlen(z)!=10 ) goto parse_error;
  1238   1404       switch( z[0] ){
  1239   1405         case '-': mode |= S_IFREG; break;
  1240   1406         case 'd': mode |= S_IFDIR; break;
  1241         -#if !defined(_WIN32) && !defined(WIN32)
  1242   1407         case 'l': mode |= S_IFLNK; break;
  1243         -#endif
  1244   1408         default: goto parse_error;
  1245   1409       }
  1246   1410       for(i=1; i<10; i++){
  1247   1411         if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
  1248   1412         else if( z[i]!='-' ) goto parse_error;
  1249   1413       }
  1250   1414     }
         1415  +  if( ((mode & S_IFDIR)==0)==bIsDir ){
         1416  +    /* The "mode" attribute is a directory, but data has been specified.
         1417  +    ** Or vice-versa - no data but "mode" is a file or symlink.  */
         1418  +    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
         1419  +    return SQLITE_CONSTRAINT;
         1420  +  }
  1251   1421     *pMode = mode;
  1252   1422     return SQLITE_OK;
  1253   1423   
  1254   1424    parse_error:
  1255         -  pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
         1425  +  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
  1256   1426     return SQLITE_ERROR;
  1257   1427   }
  1258   1428   
  1259   1429   /*
  1260   1430   ** Both (const char*) arguments point to nul-terminated strings. Argument
  1261   1431   ** nB is the value of strlen(zB). This function returns 0 if the strings are
  1262   1432   ** identical, ignoring any trailing '/' character in either path.  */
................................................................................
  1263   1433   static int zipfileComparePath(const char *zA, const char *zB, int nB){
  1264   1434     int nA = (int)strlen(zA);
  1265   1435     if( zA[nA-1]=='/' ) nA--;
  1266   1436     if( zB[nB-1]=='/' ) nB--;
  1267   1437     if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
  1268   1438     return 1;
  1269   1439   }
         1440  +
         1441  +static int zipfileBegin(sqlite3_vtab *pVtab){
         1442  +  ZipfileTab *pTab = (ZipfileTab*)pVtab;
         1443  +  int rc = SQLITE_OK;
         1444  +
         1445  +  assert( pTab->pWriteFd==0 );
         1446  +
         1447  +  /* Open a write fd on the file. Also load the entire central directory
         1448  +  ** structure into memory. During the transaction any new file data is 
         1449  +  ** appended to the archive file, but the central directory is accumulated
         1450  +  ** in main-memory until the transaction is committed.  */
         1451  +  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
         1452  +  if( pTab->pWriteFd==0 ){
         1453  +    pTab->base.zErrMsg = sqlite3_mprintf(
         1454  +        "zipfile: failed to open file %s for writing", pTab->zFile
         1455  +        );
         1456  +    rc = SQLITE_ERROR;
         1457  +  }else{
         1458  +    fseek(pTab->pWriteFd, 0, SEEK_END);
         1459  +    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
         1460  +    rc = zipfileLoadDirectory(pTab, 0, 0);
         1461  +  }
         1462  +
         1463  +  if( rc!=SQLITE_OK ){
         1464  +    zipfileCleanupTransaction(pTab);
         1465  +  }
         1466  +
         1467  +  return rc;
         1468  +}
         1469  +
         1470  +/*
         1471  +** Return the current time as a 32-bit timestamp in UNIX epoch format (like
         1472  +** time(2)).
         1473  +*/
         1474  +static u32 zipfileTime(void){
         1475  +  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
         1476  +  u32 ret;
         1477  +  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
         1478  +    i64 ms;
         1479  +    pVfs->xCurrentTimeInt64(pVfs, &ms);
         1480  +    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
         1481  +  }else{
         1482  +    double day;
         1483  +    pVfs->xCurrentTime(pVfs, &day);
         1484  +    ret = (u32)((day - 2440587.5) * 86400);
         1485  +  }
         1486  +  return ret;
         1487  +}
         1488  +
         1489  +/*
         1490  +** Return a 32-bit timestamp in UNIX epoch format.
         1491  +**
         1492  +** If the value passed as the only argument is either NULL or an SQL NULL,
         1493  +** return the current time. Otherwise, return the value stored in (*pVal)
         1494  +** cast to a 32-bit unsigned integer.
         1495  +*/
         1496  +static u32 zipfileGetTime(sqlite3_value *pVal){
         1497  +  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
         1498  +    return zipfileTime();
         1499  +  }
         1500  +  return (u32)sqlite3_value_int64(pVal);
         1501  +}
  1270   1502   
  1271   1503   /*
  1272   1504   ** xUpdate method.
  1273   1505   */
  1274   1506   static int zipfileUpdate(
  1275   1507     sqlite3_vtab *pVtab, 
  1276   1508     int nVal, 
................................................................................
  1278   1510     sqlite_int64 *pRowid
  1279   1511   ){
  1280   1512     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  1281   1513     int rc = SQLITE_OK;             /* Return Code */
  1282   1514     ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
  1283   1515   
  1284   1516     u32 mode = 0;                   /* Mode for new entry */
  1285         -  i64 mTime = 0;                  /* Modification time for new entry */
         1517  +  u32 mTime = 0;                  /* Modification time for new entry */
  1286   1518     i64 sz = 0;                     /* Uncompressed size */
  1287   1519     const char *zPath = 0;          /* Path for new entry */
  1288   1520     int nPath = 0;                  /* strlen(zPath) */
  1289   1521     const u8 *pData = 0;            /* Pointer to buffer containing content */
  1290   1522     int nData = 0;                  /* Size of pData buffer in bytes */
  1291   1523     int iMethod = 0;                /* Compression method for new entry */
  1292   1524     u8 *pFree = 0;                  /* Free this */
  1293   1525     char *zFree = 0;                /* Also free this */
  1294         -  ZipfileCDS cds;                 /* New Central Directory Structure entry */
  1295   1526     ZipfileEntry *pOld = 0;
  1296   1527     int bIsDir = 0;
  1297   1528     u32 iCrc32 = 0;
  1298   1529   
  1299         -  assert( pTab->zFile );
  1300         -  assert( pTab->pWriteFd );
         1530  +  if( pTab->pWriteFd==0 ){
         1531  +    rc = zipfileBegin(pVtab);
         1532  +    if( rc!=SQLITE_OK ) return rc;
         1533  +  }
  1301   1534   
         1535  +  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
  1302   1536     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  1303   1537       const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
  1304   1538       int nDelete = (int)strlen(zDelete);
  1305   1539       for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
  1306         -      if( pOld->bDeleted ) continue;
  1307         -      if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
  1308         -        pOld->bDeleted = 1;
         1540  +      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
  1309   1541           break;
  1310   1542         }
  1311   1543         assert( pOld->pNext );
  1312   1544       }
  1313         -    if( nVal==1 ) return SQLITE_OK;
  1314         -  }
  1315         -
  1316         -  /* Check that "sz" and "rawdata" are both NULL: */
  1317         -  if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
  1318         -   || sqlite3_value_type(apVal[6])!=SQLITE_NULL
  1319         -  ){
  1320         -    rc = SQLITE_CONSTRAINT;
  1321         -  }
  1322         -
  1323         -  if( rc==SQLITE_OK ){
  1324         -    if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
  1325         -      /* data=NULL. A directory */
  1326         -      bIsDir = 1;
  1327         -    }else{
  1328         -      /* Value specified for "data", and possibly "method". This must be
  1329         -      ** a regular file or a symlink. */
  1330         -      const u8 *aIn = sqlite3_value_blob(apVal[7]);
  1331         -      int nIn = sqlite3_value_bytes(apVal[7]);
  1332         -      int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
  1333         -
  1334         -      iMethod = sqlite3_value_int(apVal[8]);
  1335         -      sz = nIn;
  1336         -      pData = aIn;
  1337         -      nData = nIn;
  1338         -      if( iMethod!=0 && iMethod!=8 ){
  1339         -        rc = SQLITE_CONSTRAINT;
  1340         -      }else{
  1341         -        if( bAuto || iMethod ){
  1342         -          int nCmp;
  1343         -          rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
  1344         -          if( rc==SQLITE_OK ){
  1345         -            if( iMethod || nCmp<nIn ){
  1346         -              iMethod = 8;
  1347         -              pData = pFree;
  1348         -              nData = nCmp;
  1349         -            }
  1350         -          }
  1351         -        }
  1352         -        iCrc32 = crc32(0, aIn, nIn);
  1353         -      }
  1354         -    }
  1355         -  }
  1356         -
  1357         -  if( rc==SQLITE_OK ){
  1358         -    rc = zipfileGetMode(pTab, apVal[3], 
  1359         -        (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
  1360         -    );
  1361         -    if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
  1362         -      /* The "mode" attribute is a directory, but data has been specified.
  1363         -      ** Or vice-versa - no data but "mode" is a file or symlink.  */
         1545  +  }
         1546  +
         1547  +  if( nVal>1 ){
         1548  +    /* Check that "sz" and "rawdata" are both NULL: */
         1549  +    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
         1550  +      zipfileTableErr(pTab, "sz must be NULL");
         1551  +      rc = SQLITE_CONSTRAINT;
         1552  +    }
         1553  +    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
         1554  +      zipfileTableErr(pTab, "rawdata must be NULL"); 
  1364   1555         rc = SQLITE_CONSTRAINT;
  1365   1556       }
  1366         -  }
  1367         -
  1368         -  if( rc==SQLITE_OK ){
  1369         -    zPath = (const char*)sqlite3_value_text(apVal[2]);
  1370         -    nPath = (int)strlen(zPath);
  1371         -    if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
  1372         -      mTime = (sqlite3_int64)time(0);
  1373         -    }else{
  1374         -      mTime = sqlite3_value_int64(apVal[4]);
         1557  +
         1558  +    if( rc==SQLITE_OK ){
         1559  +      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
         1560  +        /* data=NULL. A directory */
         1561  +        bIsDir = 1;
         1562  +      }else{
         1563  +        /* Value specified for "data", and possibly "method". This must be
         1564  +        ** a regular file or a symlink. */
         1565  +        const u8 *aIn = sqlite3_value_blob(apVal[7]);
         1566  +        int nIn = sqlite3_value_bytes(apVal[7]);
         1567  +        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
         1568  +
         1569  +        iMethod = sqlite3_value_int(apVal[8]);
         1570  +        sz = nIn;
         1571  +        pData = aIn;
         1572  +        nData = nIn;
         1573  +        if( iMethod!=0 && iMethod!=8 ){
         1574  +          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
         1575  +          rc = SQLITE_CONSTRAINT;
         1576  +        }else{
         1577  +          if( bAuto || iMethod ){
         1578  +            int nCmp;
         1579  +            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
         1580  +            if( rc==SQLITE_OK ){
         1581  +              if( iMethod || nCmp<nIn ){
         1582  +                iMethod = 8;
         1583  +                pData = pFree;
         1584  +                nData = nCmp;
         1585  +              }
         1586  +            }
         1587  +          }
         1588  +          iCrc32 = crc32(0, aIn, nIn);
         1589  +        }
         1590  +      }
         1591  +    }
         1592  +
         1593  +    if( rc==SQLITE_OK ){
         1594  +      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
         1595  +    }
         1596  +
         1597  +    if( rc==SQLITE_OK ){
         1598  +      zPath = (const char*)sqlite3_value_text(apVal[2]);
         1599  +      nPath = (int)strlen(zPath);
         1600  +      mTime = zipfileGetTime(apVal[4]);
         1601  +    }
         1602  +
         1603  +    if( rc==SQLITE_OK && bIsDir ){
         1604  +      /* For a directory, check that the last character in the path is a
         1605  +      ** '/'. This appears to be required for compatibility with info-zip
         1606  +      ** (the unzip command on unix). It does not create directories
         1607  +      ** otherwise.  */
         1608  +      if( zPath[nPath-1]!='/' ){
         1609  +        zFree = sqlite3_mprintf("%s/", zPath);
         1610  +        if( zFree==0 ){ rc = SQLITE_NOMEM; }
         1611  +        zPath = (const char*)zFree;
         1612  +        nPath++;
         1613  +      }
         1614  +    }
         1615  +
         1616  +    /* Check that we're not inserting a duplicate entry */
         1617  +    if( pOld==0 && rc==SQLITE_OK ){
         1618  +      ZipfileEntry *p;
         1619  +      for(p=pTab->pFirstEntry; p; p=p->pNext){
         1620  +        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
         1621  +          switch( sqlite3_vtab_on_conflict(pTab->db) ){
         1622  +            case SQLITE_IGNORE: {
         1623  +              goto zipfile_update_done;
         1624  +            }
         1625  +            case SQLITE_REPLACE: {
         1626  +              pOld = p;
         1627  +              break;
         1628  +            }
         1629  +            default: {
         1630  +              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
         1631  +              rc = SQLITE_CONSTRAINT;
         1632  +              break;
         1633  +            }
         1634  +          }
         1635  +          break;
         1636  +        }
         1637  +      }
  1375   1638       }
  1376         -  }
  1377   1639   
  1378         -  if( rc==SQLITE_OK && bIsDir ){
  1379         -    /* For a directory, check that the last character in the path is a
  1380         -    ** '/'. This appears to be required for compatibility with info-zip
  1381         -    ** (the unzip command on unix). It does not create directories
  1382         -    ** otherwise.  */
  1383         -    if( zPath[nPath-1]!='/' ){
  1384         -      zFree = sqlite3_mprintf("%s/", zPath);
  1385         -      if( zFree==0 ){ rc = SQLITE_NOMEM; }
  1386         -      zPath = (const char*)zFree;
  1387         -      nPath++;
         1640  +    if( rc==SQLITE_OK ){
         1641  +      /* Create the new CDS record. */
         1642  +      pNew = zipfileNewEntry(zPath);
         1643  +      if( pNew==0 ){
         1644  +        rc = SQLITE_NOMEM;
         1645  +      }else{
         1646  +        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         1647  +        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         1648  +        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         1649  +        pNew->cds.iCompression = (u16)iMethod;
         1650  +        zipfileMtimeToDos(&pNew->cds, mTime);
         1651  +        pNew->cds.crc32 = iCrc32;
         1652  +        pNew->cds.szCompressed = nData;
         1653  +        pNew->cds.szUncompressed = (u32)sz;
         1654  +        pNew->cds.iExternalAttr = (mode<<16);
         1655  +        pNew->cds.iOffset = (u32)pTab->szCurrent;
         1656  +        pNew->cds.nFile = (u16)nPath;
         1657  +        pNew->mUnixTime = (u32)mTime;
         1658  +        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
         1659  +        zipfileAddEntry(pTab, pOld, pNew);
         1660  +      }
  1388   1661       }
  1389   1662     }
  1390   1663   
  1391         -  /* Check that we're not inserting a duplicate entry */
  1392         -  if( rc==SQLITE_OK ){
  1393         -    ZipfileEntry *p;
  1394         -    for(p=pTab->pFirstEntry; p; p=p->pNext){
  1395         -      if( p->bDeleted ) continue;
  1396         -      if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
  1397         -        rc = SQLITE_CONSTRAINT;
  1398         -        break;
         1664  +  if( rc==SQLITE_OK && pOld ){
         1665  +    ZipfileEntry **pp;
         1666  +    ZipfileCsr *pCsr;
         1667  +    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
         1668  +      if( pCsr->pCurrent==pOld ){
         1669  +        pCsr->pCurrent = pOld->pNext;
         1670  +        pCsr->bNoop = 1;
  1399   1671         }
  1400   1672       }
         1673  +    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
         1674  +    *pp = (*pp)->pNext;
         1675  +    zipfileEntryFree(pOld);
  1401   1676     }
  1402   1677   
  1403         -  if( rc==SQLITE_OK ){
  1404         -    /* Create the new CDS record. */
  1405         -    memset(&cds, 0, sizeof(cds));
  1406         -    cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
  1407         -    cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
  1408         -    cds.flags = ZIPFILE_NEWENTRY_FLAGS;
  1409         -    cds.iCompression = (u16)iMethod;
  1410         -    zipfileMtimeToDos(&cds, (u32)mTime);
  1411         -    cds.crc32 = iCrc32;
  1412         -    cds.szCompressed = nData;
  1413         -    cds.szUncompressed = (u32)sz;
  1414         -    cds.iExternalAttr = (mode<<16);
  1415         -    cds.iOffset = (u32)pTab->szCurrent;
  1416         -    pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
  1417         -    if( pNew==0 ){
  1418         -      rc = SQLITE_NOMEM;
  1419         -    }else{
  1420         -      zipfileAddEntry(pTab, pOld, pNew);
  1421         -    }
  1422         -  }
  1423         -
  1424         -  /* Append the new header+file to the archive */
  1425         -  if( rc==SQLITE_OK ){
  1426         -    rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
  1427         -  }
  1428         -
  1429         -  if( rc!=SQLITE_OK && pOld ){
  1430         -    pOld->bDeleted = 0;
  1431         -  }
         1678  +zipfile_update_done:
  1432   1679     sqlite3_free(pFree);
  1433   1680     sqlite3_free(zFree);
  1434   1681     return rc;
  1435   1682   }
         1683  +
         1684  +static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
         1685  +  u8 *a = aBuf;
         1686  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
         1687  +  zipfileWrite16(a, p->iDisk);
         1688  +  zipfileWrite16(a, p->iFirstDisk);
         1689  +  zipfileWrite16(a, p->nEntry);
         1690  +  zipfileWrite16(a, p->nEntryTotal);
         1691  +  zipfileWrite32(a, p->nSize);
         1692  +  zipfileWrite32(a, p->iOffset);
         1693  +  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
         1694  +
         1695  +  return a-aBuf;
         1696  +}
  1436   1697   
  1437   1698   static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
  1438         -  u8 *aBuf = pTab->aBuffer;
  1439         -
  1440         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
  1441         -  zipfileWrite16(aBuf, p->iDisk);
  1442         -  zipfileWrite16(aBuf, p->iFirstDisk);
  1443         -  zipfileWrite16(aBuf, p->nEntry);
  1444         -  zipfileWrite16(aBuf, p->nEntryTotal);
  1445         -  zipfileWrite32(aBuf, p->nSize);
  1446         -  zipfileWrite32(aBuf, p->iOffset);
  1447         -  zipfileWrite16(aBuf, 0);        /* Size of trailing comment in bytes*/
  1448         -
  1449         -  assert( (aBuf-pTab->aBuffer)==22 );
  1450         -  return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
  1451         -}
  1452         -
  1453         -static void zipfileCleanupTransaction(ZipfileTab *pTab){
  1454         -  ZipfileEntry *pEntry;
  1455         -  ZipfileEntry *pNext;
  1456         -
  1457         -  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
  1458         -    pNext = pEntry->pNext;
  1459         -    sqlite3_free(pEntry);
  1460         -  }
  1461         -  pTab->pFirstEntry = 0;
  1462         -  pTab->pLastEntry = 0;
  1463         -  fclose(pTab->pWriteFd);
  1464         -  pTab->pWriteFd = 0;
  1465         -  pTab->szCurrent = 0;
  1466         -  pTab->szOrig = 0;
         1699  +  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
         1700  +  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
         1701  +  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
  1467   1702   }
  1468   1703   
  1469         -static int zipfileBegin(sqlite3_vtab *pVtab){
  1470         -  ZipfileTab *pTab = (ZipfileTab*)pVtab;
  1471         -  int rc = SQLITE_OK;
  1472         -
  1473         -  assert( pTab->pWriteFd==0 );
  1474         -
  1475         -  /* This table is only writable if a default archive path was specified 
  1476         -  ** as part of the CREATE VIRTUAL TABLE statement. */
  1477         -  if( pTab->zFile==0 ){
  1478         -    pTab->base.zErrMsg = sqlite3_mprintf(
  1479         -        "zipfile: writing requires a default archive"
  1480         -    );
  1481         -    return SQLITE_ERROR;
  1482         -  }
  1483         -
  1484         -  /* Open a write fd on the file. Also load the entire central directory
  1485         -  ** structure into memory. During the transaction any new file data is 
  1486         -  ** appended to the archive file, but the central directory is accumulated
  1487         -  ** in main-memory until the transaction is committed.  */
  1488         -  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
  1489         -  if( pTab->pWriteFd==0 ){
  1490         -    pTab->base.zErrMsg = sqlite3_mprintf(
  1491         -        "zipfile: failed to open file %s for writing", pTab->zFile
  1492         -    );
  1493         -    rc = SQLITE_ERROR;
         1704  +/*
         1705  +** Serialize the CDS structure into buffer aBuf[]. Return the number
         1706  +** of bytes written.
         1707  +*/
         1708  +static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
         1709  +  u8 *a = aBuf;
         1710  +  ZipfileCDS *pCDS = &pEntry->cds;
         1711  +
         1712  +  if( pEntry->aExtra==0 ){
         1713  +    pCDS->nExtra = 9;
         1714  +  }
         1715  +
         1716  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
         1717  +  zipfileWrite16(a, pCDS->iVersionMadeBy);
         1718  +  zipfileWrite16(a, pCDS->iVersionExtract);
         1719  +  zipfileWrite16(a, pCDS->flags);
         1720  +  zipfileWrite16(a, pCDS->iCompression);
         1721  +  zipfileWrite16(a, pCDS->mTime);
         1722  +  zipfileWrite16(a, pCDS->mDate);
         1723  +  zipfileWrite32(a, pCDS->crc32);
         1724  +  zipfileWrite32(a, pCDS->szCompressed);
         1725  +  zipfileWrite32(a, pCDS->szUncompressed);
         1726  +  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
         1727  +  zipfileWrite16(a, pCDS->nFile);
         1728  +  zipfileWrite16(a, pCDS->nExtra);
         1729  +  zipfileWrite16(a, pCDS->nComment);
         1730  +  zipfileWrite16(a, pCDS->iDiskStart);
         1731  +  zipfileWrite16(a, pCDS->iInternalAttr);
         1732  +  zipfileWrite32(a, pCDS->iExternalAttr);
         1733  +  zipfileWrite32(a, pCDS->iOffset);
         1734  +
         1735  +  memcpy(a, pCDS->zFile, pCDS->nFile);
         1736  +  a += pCDS->nFile;
         1737  +
         1738  +  if( pEntry->aExtra ){
         1739  +    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
         1740  +    memcpy(a, pEntry->aExtra, n);
         1741  +    a += n;
  1494   1742     }else{
  1495         -    fseek(pTab->pWriteFd, 0, SEEK_END);
  1496         -    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
  1497         -    rc = zipfileLoadDirectory(pTab);
         1743  +    assert( pCDS->nExtra==9 );
         1744  +    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         1745  +    zipfileWrite16(a, 5);
         1746  +    *a++ = 0x01;
         1747  +    zipfileWrite32(a, pEntry->mUnixTime);
  1498   1748     }
  1499   1749   
  1500         -  if( rc!=SQLITE_OK ){
  1501         -    zipfileCleanupTransaction(pTab);
  1502         -  }
  1503         -
  1504         -  return rc;
         1750  +  return a-aBuf;
  1505   1751   }
  1506   1752   
  1507   1753   static int zipfileCommit(sqlite3_vtab *pVtab){
  1508   1754     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  1509   1755     int rc = SQLITE_OK;
  1510   1756     if( pTab->pWriteFd ){
  1511   1757       i64 iOffset = pTab->szCurrent;
  1512   1758       ZipfileEntry *p;
  1513   1759       ZipfileEOCD eocd;
  1514   1760       int nEntry = 0;
  1515   1761   
  1516         -    /* Write out all undeleted entries */
         1762  +    /* Write out all entries */
  1517   1763       for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
  1518         -      if( p->bDeleted ) continue;
  1519         -      rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
         1764  +      int n = zipfileSerializeCDS(p, pTab->aBuffer);
         1765  +      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
  1520   1766         nEntry++;
  1521   1767       }
  1522   1768   
  1523   1769       /* Write out the EOCD record */
  1524   1770       eocd.iDisk = 0;
  1525   1771       eocd.iFirstDisk = 0;
  1526   1772       eocd.nEntry = (u16)nEntry;
................................................................................
  1553   1799   ){
  1554   1800     ZipfileCsr *pCsr;
  1555   1801     ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
  1556   1802     assert( argc>0 );
  1557   1803   
  1558   1804     pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
  1559   1805     if( pCsr ){
  1560         -    ZipfileCDS *p = &pCsr->cds;
         1806  +    ZipfileCDS *p = &pCsr->pCurrent->cds;
  1561   1807       char *zRes = sqlite3_mprintf("{"
  1562   1808           "\"version-made-by\" : %u, "
  1563   1809           "\"version-to-extract\" : %u, "
  1564   1810           "\"flags\" : %u, "
  1565   1811           "\"compression\" : %u, "
  1566   1812           "\"time\" : %u, "
  1567   1813           "\"date\" : %u, "
................................................................................
  1590   1836       }else{
  1591   1837         sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
  1592   1838         sqlite3_free(zRes);
  1593   1839       }
  1594   1840     }
  1595   1841   }
  1596   1842   
  1597         -
  1598   1843   /*
  1599   1844   ** xFindFunction method.
  1600   1845   */
  1601   1846   static int zipfileFindFunction(
  1602   1847     sqlite3_vtab *pVtab,            /* Virtual table handle */
  1603   1848     int nArg,                       /* Number of SQL function arguments */
  1604   1849     const char *zName,              /* Name of SQL function */
  1605   1850     void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
  1606   1851     void **ppArg                    /* OUT: User data for *pxFunc */
  1607   1852   ){
  1608         -  if( nArg>0 ){
  1609         -    if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
  1610         -      *pxFunc = zipfileFunctionCds;
  1611         -      *ppArg = (void*)pVtab;
  1612         -      return 1;
         1853  +  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
         1854  +    *pxFunc = zipfileFunctionCds;
         1855  +    *ppArg = (void*)pVtab;
         1856  +    return 1;
         1857  +  }
         1858  +  return 0;
         1859  +}
         1860  +
         1861  +typedef struct ZipfileBuffer ZipfileBuffer;
         1862  +struct ZipfileBuffer {
         1863  +  u8 *a;                          /* Pointer to buffer */
         1864  +  int n;                          /* Size of buffer in bytes */
         1865  +  int nAlloc;                     /* Byte allocated at a[] */
         1866  +};
         1867  +
         1868  +typedef struct ZipfileCtx ZipfileCtx;
         1869  +struct ZipfileCtx {
         1870  +  int nEntry;
         1871  +  ZipfileBuffer body;
         1872  +  ZipfileBuffer cds;
         1873  +};
         1874  +
         1875  +static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
         1876  +  if( pBuf->n+nByte>pBuf->nAlloc ){
         1877  +    u8 *aNew;
         1878  +    int nNew = pBuf->n ? pBuf->n*2 : 512;
         1879  +    int nReq = pBuf->n + nByte;
         1880  +
         1881  +    while( nNew<nReq ) nNew = nNew*2;
         1882  +    aNew = sqlite3_realloc(pBuf->a, nNew);
         1883  +    if( aNew==0 ) return SQLITE_NOMEM;
         1884  +    pBuf->a = aNew;
         1885  +    pBuf->nAlloc = nNew;
         1886  +  }
         1887  +  return SQLITE_OK;
         1888  +}
         1889  +
         1890  +/*
         1891  +** xStep() callback for the zipfile() aggregate. This can be called in
         1892  +** any of the following ways:
         1893  +**
         1894  +**   SELECT zipfile(name,data) ...
         1895  +**   SELECT zipfile(name,mode,mtime,data) ...
         1896  +**   SELECT zipfile(name,mode,mtime,data,method) ...
         1897  +*/
         1898  +void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
         1899  +  ZipfileCtx *p;                  /* Aggregate function context */
         1900  +  ZipfileEntry e;                 /* New entry to add to zip archive */
         1901  +
         1902  +  sqlite3_value *pName = 0;
         1903  +  sqlite3_value *pMode = 0;
         1904  +  sqlite3_value *pMtime = 0;
         1905  +  sqlite3_value *pData = 0;
         1906  +  sqlite3_value *pMethod = 0;
         1907  +
         1908  +  int bIsDir = 0;
         1909  +  u32 mode;
         1910  +  int rc = SQLITE_OK;
         1911  +  char *zErr = 0;
         1912  +
         1913  +  int iMethod = -1;               /* Compression method to use (0 or 8) */
         1914  +
         1915  +  const u8 *aData = 0;            /* Possibly compressed data for new entry */
         1916  +  int nData = 0;                  /* Size of aData[] in bytes */
         1917  +  int szUncompressed = 0;         /* Size of data before compression */
         1918  +  u8 *aFree = 0;                  /* Free this before returning */
         1919  +  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
         1920  +
         1921  +  char *zName = 0;                /* Path (name) of new entry */
         1922  +  int nName = 0;                  /* Size of zName in bytes */
         1923  +  char *zFree = 0;                /* Free this before returning */
         1924  +  int nByte;
         1925  +
         1926  +  memset(&e, 0, sizeof(e));
         1927  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         1928  +  if( p==0 ) return;
         1929  +
         1930  +  /* Martial the arguments into stack variables */
         1931  +  if( nVal!=2 && nVal!=4 && nVal!=5 ){
         1932  +    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
         1933  +    rc = SQLITE_ERROR;
         1934  +    goto zipfile_step_out;
         1935  +  }
         1936  +  pName = apVal[0];
         1937  +  if( nVal==2 ){
         1938  +    pData = apVal[1];
         1939  +  }else{
         1940  +    pMode = apVal[1];
         1941  +    pMtime = apVal[2];
         1942  +    pData = apVal[3];
         1943  +    if( nVal==5 ){
         1944  +      pMethod = apVal[4];
         1945  +    }
         1946  +  }
         1947  +
         1948  +  /* Check that the 'name' parameter looks ok. */
         1949  +  zName = (char*)sqlite3_value_text(pName);
         1950  +  nName = sqlite3_value_bytes(pName);
         1951  +  if( zName==0 ){
         1952  +    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
         1953  +    rc = SQLITE_ERROR;
         1954  +    goto zipfile_step_out;
         1955  +  }
         1956  +
         1957  +  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
         1958  +  ** deflate compression) or NULL (choose automatically).  */
         1959  +  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
         1960  +    iMethod = (int)sqlite3_value_int64(pMethod);
         1961  +    if( iMethod!=0 && iMethod!=8 ){
         1962  +      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
         1963  +      rc = SQLITE_ERROR;
         1964  +      goto zipfile_step_out;
         1965  +    }
         1966  +  }
         1967  +
         1968  +  /* Now inspect the data. If this is NULL, then the new entry must be a
         1969  +  ** directory.  Otherwise, figure out whether or not the data should
         1970  +  ** be deflated or simply stored in the zip archive. */
         1971  +  if( sqlite3_value_type(pData)==SQLITE_NULL ){
         1972  +    bIsDir = 1;
         1973  +    iMethod = 0;
         1974  +  }else{
         1975  +    aData = sqlite3_value_blob(pData);
         1976  +    szUncompressed = nData = sqlite3_value_bytes(pData);
         1977  +    iCrc32 = crc32(0, aData, nData);
         1978  +    if( iMethod<0 || iMethod==8 ){
         1979  +      int nOut = 0;
         1980  +      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
         1981  +      if( rc!=SQLITE_OK ){
         1982  +        goto zipfile_step_out;
         1983  +      }
         1984  +      if( iMethod==8 || nOut<nData ){
         1985  +        aData = aFree;
         1986  +        nData = nOut;
         1987  +        iMethod = 8;
         1988  +      }else{
         1989  +        iMethod = 0;
         1990  +      }
         1991  +    }
         1992  +  }
         1993  +
         1994  +  /* Decode the "mode" argument. */
         1995  +  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
         1996  +  if( rc ) goto zipfile_step_out;
         1997  +
         1998  +  /* Decode the "mtime" argument. */
         1999  +  e.mUnixTime = zipfileGetTime(pMtime);
         2000  +
         2001  +  /* If this is a directory entry, ensure that there is exactly one '/'
         2002  +  ** at the end of the path. Or, if this is not a directory and the path
         2003  +  ** ends in '/' it is an error. */
         2004  +  if( bIsDir==0 ){
         2005  +    if( zName[nName-1]=='/' ){
         2006  +      zErr = sqlite3_mprintf("non-directory name must not end with /");
         2007  +      rc = SQLITE_ERROR;
         2008  +      goto zipfile_step_out;
         2009  +    }
         2010  +  }else{
         2011  +    if( zName[nName-1]!='/' ){
         2012  +      zName = zFree = sqlite3_mprintf("%s/", zName);
         2013  +      nName++;
         2014  +      if( zName==0 ){
         2015  +        rc = SQLITE_NOMEM;
         2016  +        goto zipfile_step_out;
         2017  +      }
         2018  +    }else{
         2019  +      while( nName>1 && zName[nName-2]=='/' ) nName--;
         2020  +    }
         2021  +  }
         2022  +
         2023  +  /* Assemble the ZipfileEntry object for the new zip archive entry */
         2024  +  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         2025  +  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         2026  +  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         2027  +  e.cds.iCompression = (u16)iMethod;
         2028  +  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
         2029  +  e.cds.crc32 = iCrc32;
         2030  +  e.cds.szCompressed = nData;
         2031  +  e.cds.szUncompressed = szUncompressed;
         2032  +  e.cds.iExternalAttr = (mode<<16);
         2033  +  e.cds.iOffset = p->body.n;
         2034  +  e.cds.nFile = (u16)nName;
         2035  +  e.cds.zFile = zName;
         2036  +
         2037  +  /* Append the LFH to the body of the new archive */
         2038  +  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
         2039  +  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
         2040  +  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
         2041  +
         2042  +  /* Append the data to the body of the new archive */
         2043  +  if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
         2044  +  memcpy(&p->body.a[p->body.n], aData, nData);
         2045  +  p->body.n += nData;
         2046  +
         2047  +  /* Append the CDS record to the directory of the new archive */
         2048  +  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
         2049  +  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
         2050  +  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
         2051  +
         2052  +  /* Increment the count of entries in the archive */
         2053  +  p->nEntry++;
         2054  +
         2055  + zipfile_step_out:
         2056  +  sqlite3_free(aFree);
         2057  +  sqlite3_free(zFree);
         2058  +  if( rc ){
         2059  +    if( zErr ){
         2060  +      sqlite3_result_error(pCtx, zErr, -1);
         2061  +    }else{
         2062  +      sqlite3_result_error_code(pCtx, rc);
         2063  +    }
         2064  +  }
         2065  +  sqlite3_free(zErr);
         2066  +}
         2067  +
         2068  +/*
         2069  +** xFinalize() callback for zipfile aggregate function.
         2070  +*/
         2071  +void zipfileFinal(sqlite3_context *pCtx){
         2072  +  ZipfileCtx *p;
         2073  +  ZipfileEOCD eocd;
         2074  +  int nZip;
         2075  +  u8 *aZip;
         2076  +
         2077  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         2078  +  if( p==0 ) return;
         2079  +  if( p->nEntry>0 ){
         2080  +    memset(&eocd, 0, sizeof(eocd));
         2081  +    eocd.nEntry = (u16)p->nEntry;
         2082  +    eocd.nEntryTotal = (u16)p->nEntry;
         2083  +    eocd.nSize = p->cds.n;
         2084  +    eocd.iOffset = p->body.n;
         2085  +
         2086  +    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
         2087  +    aZip = (u8*)sqlite3_malloc(nZip);
         2088  +    if( aZip==0 ){
         2089  +      sqlite3_result_error_nomem(pCtx);
         2090  +    }else{
         2091  +      memcpy(aZip, p->body.a, p->body.n);
         2092  +      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
         2093  +      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
         2094  +      sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
  1613   2095       }
  1614   2096     }
  1615   2097   
  1616         -  return 0;
         2098  +  sqlite3_free(p->body.a);
         2099  +  sqlite3_free(p->cds.a);
  1617   2100   }
         2101  +
  1618   2102   
  1619   2103   /*
  1620   2104   ** Register the "zipfile" virtual table.
  1621   2105   */
  1622   2106   static int zipfileRegister(sqlite3 *db){
  1623   2107     static sqlite3_module zipfileModule = {
  1624   2108       1,                         /* iVersion */
................................................................................
  1629   2113       zipfileDisconnect,         /* xDestroy */
  1630   2114       zipfileOpen,               /* xOpen - open a cursor */
  1631   2115       zipfileClose,              /* xClose - close a cursor */
  1632   2116       zipfileFilter,             /* xFilter - configure scan constraints */
  1633   2117       zipfileNext,               /* xNext - advance a cursor */
  1634   2118       zipfileEof,                /* xEof - check for end of scan */
  1635   2119       zipfileColumn,             /* xColumn - read data */
  1636         -    zipfileRowid,              /* xRowid - read data */
         2120  +    0,                         /* xRowid - read data */
  1637   2121       zipfileUpdate,             /* xUpdate */
  1638   2122       zipfileBegin,              /* xBegin */
  1639   2123       0,                         /* xSync */
  1640   2124       zipfileCommit,             /* xCommit */
  1641   2125       zipfileRollback,           /* xRollback */
  1642   2126       zipfileFindFunction,       /* xFindMethod */
  1643   2127       0,                         /* xRename */
  1644   2128     };
  1645   2129   
  1646   2130     int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
         2131  +  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
  1647   2132     if( rc==SQLITE_OK ){
  1648         -    rc = sqlite3_overload_function(db, "zipfile_cds", -1);
         2133  +    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
         2134  +        zipfileStep, zipfileFinal
         2135  +    );
  1649   2136     }
  1650   2137     return rc;
  1651   2138   }
  1652   2139   #else         /* SQLITE_OMIT_VIRTUALTABLE */
  1653   2140   # define zipfileRegister(x) SQLITE_OK
  1654   2141   #endif
  1655   2142   

Added ext/misc/zorder.c.

            1  +/*
            2  +** 2018-02-09
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** SQL functions for z-order (Morton code) transformations.
           14  +**
           15  +**      zorder(X0,X0,..,xN)      Generate an N+1 dimension Morton code
           16  +**
           17  +**      unzorder(Z,N,I)          Extract the I-th dimension from N-dimensional
           18  +**                               Morton code Z.
           19  +*/
           20  +#include "sqlite3ext.h"
           21  +SQLITE_EXTENSION_INIT1
           22  +#include <assert.h>
           23  +#include <string.h>
           24  +
           25  +/*
           26  +** Functions:     zorder(X0,X1,....)
           27  +**
           28  +** Convert integers X0, X1, ... into morton code.
           29  +**
           30  +** The output is a signed 64-bit integer.  If any argument is too large,
           31  +** an error is thrown.
           32  +*/
           33  +static void zorderFunc(
           34  +  sqlite3_context *context,
           35  +  int argc,
           36  +  sqlite3_value **argv
           37  +){
           38  +  sqlite3_int64 z, x[63];
           39  +  int i, j;
           40  +  z = 0;
           41  +  for(i=0; i<argc; i++){
           42  +    x[i] = sqlite3_value_int64(argv[i]);
           43  +  }
           44  +  if( argc>0 ){
           45  +    for(i=0; i<63; i++){
           46  +      j = i%argc;
           47  +      z |= (x[j]&1)<<i;
           48  +      x[j] >>= 1;
           49  +    }
           50  +  }
           51  +  sqlite3_result_int64(context, z);
           52  +  for(i=0; i<argc; i++){
           53  +    if( x[i] ){
           54  +      sqlite3_result_error(context, "parameter too large", -1);
           55  +    }
           56  +  }
           57  +}
           58  +
           59  +
           60  +/*
           61  +** Functions:     unzorder(Z,N,I)
           62  +**
           63  +** Assuming that Z is an N-dimensional Morton code, extract the I-th
           64  +** dimension.
           65  +*/
           66  +static void unzorderFunc(
           67  +  sqlite3_context *context,
           68  +  int argc,
           69  +  sqlite3_value **argv
           70  +){
           71  +  sqlite3_int64 z, n, i, x;
           72  +  int j, k;
           73  +  z = sqlite3_value_int64(argv[0]);
           74  +  n = sqlite3_value_int64(argv[1]);
           75  +  i = sqlite3_value_int64(argv[2]);
           76  +  x = 0;
           77  +  for(k=0, j=i; j<63; j+=n, k++){
           78  +    x |= ((z>>j)&1)<<k;
           79  +  }
           80  +  sqlite3_result_int64(context, x);
           81  +}
           82  +
           83  +
           84  +#ifdef _WIN32
           85  +__declspec(dllexport)
           86  +#endif
           87  +int sqlite3_zorder_init(
           88  +  sqlite3 *db, 
           89  +  char **pzErrMsg, 
           90  +  const sqlite3_api_routines *pApi
           91  +){
           92  +  int rc = SQLITE_OK;
           93  +  SQLITE_EXTENSION_INIT2(pApi);
           94  +  (void)pzErrMsg;  /* Unused parameter */
           95  +  rc = sqlite3_create_function(db, "zorder", -1, SQLITE_UTF8, 0,
           96  +                               zorderFunc, 0, 0);
           97  +  if( rc==SQLITE_OK ){
           98  +    rc = sqlite3_create_function(db, "unzorder", 3, SQLITE_UTF8, 0,
           99  +                               unzorderFunc, 0, 0);
          100  +  }
          101  +  return rc;
          102  +}

Changes to ext/repair/sqlite3_checker.tcl.

   216    216     puts stderr "Cannot open datababase $root_filename: $res"
   217    217     exit 1
   218    218   }
   219    219   
   220    220   if {$bFreelistCheck || $bAll} {
   221    221     puts -nonewline "freelist-check: "
   222    222     flush stdout
          223  +  db eval BEGIN
   223    224     puts [db one {SELECT checkfreelist('main')}]
          225  +  db eval END
   224    226   }
   225    227   if {$bSummary} {
   226    228     set scale 0
   227    229     set pgsz [db one {PRAGMA page_size}]
   228    230     db eval {SELECT nPage*$pgsz AS sz, name, tbl_name
   229    231                FROM sqlite_btreeinfo
   230    232               WHERE type='index'

Changes to ext/rtree/rtree.c.

   782    782       }else{
   783    783         sqlite3_bind_null(p, 1);
   784    784       }
   785    785       sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
   786    786       sqlite3_step(p);
   787    787       pNode->isDirty = 0;
   788    788       rc = sqlite3_reset(p);
          789  +    sqlite3_bind_null(p, 2);
   789    790       if( pNode->iNode==0 && rc==SQLITE_OK ){
   790    791         pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
   791    792         nodeHashInsert(pRtree, pNode);
   792    793       }
   793    794     }
   794    795     return rc;
   795    796   }

Changes to ext/rtree/rtree1.test.

   605    605     INSERT INTO rt VALUES(1,2,3,4,5);
   606    606   }
   607    607   do_execsql_test 15.2 {
   608    608     DROP TABLE t13;
   609    609     COMMIT;
   610    610   }
   611    611   
          612  +expand_all_sql db
   612    613   finish_test

Changes to ext/rtree/rtree4.test.

   246    246         list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
   247    247       } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
   248    248     }
   249    249   
   250    250     do_rtree_integrity_test rtree4-$nDim.3 rx
   251    251   }
   252    252   
          253  +expand_all_sql db
   253    254   finish_test

Changes to ext/rtree/rtree5.test.

    75     75       SELECT * FROM t1 WHERE 
    76     76           x1=2147483643 AND x2=2147483647 AND 
    77     77           y1=-2147483648 AND y2=-2147483643
    78     78     }
    79     79   } {2 2147483643 2147483647 -2147483648 -2147483643}
    80     80   do_rtree_integrity_test rtree5-1.14 t1
    81     81   
           82  +expand_all_sql db
    82     83   finish_test

Changes to ext/rtree/rtree6.test.

   154    154       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND 
   155    155       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND 
   156    156       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND 
   157    157       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND 
   158    158       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>1.1
   159    159   } {}
   160    160   
   161         -
          161  +expand_all_sql db
   162    162   finish_test

Changes to ext/rtree/rtreeG.test.

    55     55   do_execsql_test rtreeG-1.4 {
    56     56     DROP TABLE t1;
    57     57   } {}
    58     58   do_test rtreeG-1.4log {
    59     59     set ::log
    60     60   } {}
    61     61   
           62  +expand_all_sql db
    62     63   db close
    63     64   sqlite3_shutdown
    64     65   test_sqlite3_log
    65     66   sqlite3_initialize
    66     67   sqlite3 db test.db
    67     68   
    68     69   finish_test

Changes to ext/session/session4.test.

    59     59     list [catch { sqlite3changeset_invert $x } msg] $msg
    60     60   } {1 SQLITE_CORRUPT}
    61     61   
    62     62   do_test 1.3 {
    63     63     set x [binary format "ca*" 0 [string range $changeset 1 end]]
    64     64     list [catch { sqlite3changeset_apply db $x xConflict } msg] $msg
    65     65   } {1 SQLITE_CORRUPT}
           66  +
           67  +#-------------------------------------------------------------------------
           68  +reset_db
           69  +do_execsql_test 2.0 {
           70  +  CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
           71  +  CREATE TABLE t2(e TEXT PRIMARY KEY NOT NULL,f,g);
           72  +  CREATE TABLE t3(w REAL PRIMARY KEY NOT NULL,x,y);
           73  +  CREATE TABLE t4(z PRIMARY KEY) WITHOUT ROWID;
           74  +}
           75  +
           76  +foreach {tn blob} {
           77  +  1 54010174340012000000
           78  +  2 54fefe8bcb0012000300
           79  +  3 5480809280808001017434001200fb
           80  +  4 50af9c939c9c9cb09c9c6400b09c9c6400
           81  +  5 12000300
           82  +  6 09847304
           83  +  7 5401017434001208
           84  +  8 54010174340012fc0386868600
           85  +  9 54010174340012FC0386868600
           86  +  10 548894FEFE
           87  +  11 54010171340012E703ABFA7433FD1200
           88  +  12 540101743400120003FFED00010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           89  +  13 540101743400120003001200010000000000000002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           90  +  14 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F03FC87797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           91  +  15 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003FC8738790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           92  +  16 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A00540301000074320009000303783879010000000080000000020000000000000000090003FC87327902400C0000000000000304666F7572
           93  +  17 540101743400120003FFE3000412F7010000E600000000021202120002400C0000000000005B0401000000743100171C0304646F750002400C000000000000540401000000D3310017000100000000000000050100000000000378797A405403000002F10100000100000000000004090001000100000007030378797A0100000000000D0007000001000000002300000F1B0378797A405403013900743200090003038C3879010000000000000000000002120002400C0000000000005B0401000000743117170003047C5E00FF
           94  +  18 54010174340012000300120001000000E6FF100000120002401E00000000000054040100000074310017000100040000010000000000000004FFFF7FFF0000000000010000010000001000000007030378797A01000000000000000F000000000000FA0304666F7572
           95  +  19 540101743400120003001200010000000000000002121B02400C00000000000054040000000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378817A01000000000000000F000001000000000100000F030378797A005403010000743200090003FFE809000303780000000000000304666F7572
           96  +  20 5401017D3400120003001200010000000000000002120002400CFC00000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FFFF797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378326C02400C0000000000000304666F7572
           97  +  21 5401017434001200030012000100FFE20000000002120002400C00000000000054040100E0007431001700010000E99D000000020000000003FFE70009000303783279020004000001030000000000002117000003001700012701000100000000743100000100000000008000090003037F387901000000008000000002000000000400000009005303010A00FF7FFFFF00000000000304664F6572
           98  +  22 540101743400120003FFFF7FFF0000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100010000000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
           99  +  23 540101742700120100120003F5FF0300
          100  +  24 5401017434E312540101743400120003FFFC00
          101  +  25 540101743400540101743D3D3D3D3D3D3D3D3D3D3D3D3D3400120003FFED000300
          102  +  26 5401017446EA5301743D1D3D3D01743D1D3D3DCF3D3D3D1A3D3D3D3D3400120003FFFF000000
          103  +  27 540101743400540101743D3D3D3D3D3D3D3D3D3D251000120003FF81000000000000
          104  +  28 540101340012000397FF3D7F3D3400120003001200540101743D3D3D3D3D3D393D3D3D12000300
          105  +  29 500174340050010F74340012000300120003FFE5
          106  +  30 5004007233E900177FEF0054257F0002EF001200031E12000300
          107  +  31 5001015001015252525250010174340012EF039A9A0100E351525D52525252525252525252525252525252525250010174340012EF039A0100009A9A9A9A9A9BA3B200120003010040743400
          108  +  32 5401017400123400120003FFFC00
          109  +  33 540101743400120003001200010000000000004002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572
          110  +  34 54040100000074310017000100000002000015050100000000000000030100000000140000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A0054030100007432000900030378387901000000008E000000020000000000000000090003FFFF000002400C0000000000000304666F7572
          111  +  35 540101743400120003001200010000000000000002120002400C00000000000050060100000074310017000100000000000000050100000000000000030100000003001700010000666F7572
          112  +  36 540101743400120003001200010000000000000002120002400C00000000000050050100000074310017000100000000000000050100000000000000030100000003001700010000666F7572
          113  +  37 540101743400120003001200010000000000000002120002400C00000000000050040100008074310017000100000000000000050100000000000000030100000003001700010000666F7572
          114  +  38 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000000000000000000050100000000000000030100000003001700010000666F7572
          115  +  39 540101743400120003001200010000000000000002120002400C00000000000050040100018074310017000100000000000000050100000000000000030100000003001700010000666F7572
          116  +  40 540101743400120003001200010000000000000002120002400C0000000000005004FEFFFFFF74310017000100000000000000050100000000000000030100000003001700010000666F7572
          117  +  41 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000004000000000000050100000000000000030100000003001700010000666F7572
          118  +  42 540101743400120003001200010000000000000002120002400C0000000000005005FFFF050074310017000100000000000000050100000000000000030100000003001700010000666F7572
          119  +  43 540101743400120003001200010000000000000002120002400C000000000000500401006E0074310017000300000000001221050100000000000000030100000003001700010000666F7572
          120  +  44 540101743400120003001200010000000000020000120002400C00000000000050050100000074310017000100000000000000050100004000000000030100000025001700010000666F7572
          121  +  45 540101743400120003001200010000000000ECFF02120002400C000000000000500401F9FF00743100170001000000000000000500E1000000000000030100000003000000000000666F7572
          122  +  46 54010174340B0B0B0B0B0B0B0B0B0B0B0B0B0B0B00120003001200010000000000000002120002400C00000000000050040100000074310017010000000000000000050100FFE900000000030100000003007F00000000666F7572
          123  +  47 54010103001200010000000000020002120002400C0000000000005004010000F374310017000100000000000000050100000000000000030100000003001700010000666F8E72
          124  +  48 540101743400120003001200010000000000000002120002400C00000000000050030012000174310017000700000000000000050100002000000001000000000003001700010000666F7572
          125  +  49 540101743400120004001200010000000000000002120002400C0000000000005004010000FC733100170001000000000000000501000000000000000301000000F6FF17000100007C6F7572
          126  +  50 54010174FFDDFF8003001200010000100000000002120002400C000000000000500401000000743100170000000005010000000000000000000003010072
          127  +  51 540101743200120003001200010000000000000002120002400C00000000000050040100001074310017000000000003010000120300170100000000000000050100000000000000030100000003001700010000666F7572
          128  +  52 540101745401017434001200010000000000001702120002400C00000000000050040100001A74310017000100000000000100000100000000000000030100000003001700010000666F7572
          129  +  53 540101743400120003001200010000000000000002120002400C000000000000500401000000743100170001000002400C00000000000050040110000074310017000000000000050100000000000000030100000003001700010000666F7572
          130  +  54 540101743400120003001200010000000000000002120002400C000000000002120002400C00000000000050040100000074310017FF0050040100000074310017FF7F00000000000000050100000000000000030100000003001700010000666F7572
          131  +  55 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000100010080000001000000020003010100000300170100000003001700010000666F7572
          132  +  56 5487ffffff7f
          133  +} {
          134  +  do_test 2.$tn {
          135  +    set changeset [binary decode hex $blob]
          136  +#set fd [open x.change w+]
          137  +#fconfigure $fd -encoding binary -translation binary
          138  +#puts -nonewline $fd $changeset
          139  +#close $fd
          140  +    list [catch { sqlite3changeset_apply db $changeset xConflict } msg] $msg
          141  +  } {1 SQLITE_CORRUPT}
          142  +}
    66    143   
    67    144   finish_test

Changes to ext/session/sqlite3session.c.

  1111   1111     int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
  1112   1112     sqlite3_session *pSession,      /* Session object pTab is attached to */
  1113   1113     SessionTable *pTab              /* Table that change applies to */
  1114   1114   ){
  1115   1115     int iHash; 
  1116   1116     int bNull = 0; 
  1117   1117     int rc = SQLITE_OK;
  1118         -  SessionStat1Ctx stat1;
         1118  +  SessionStat1Ctx stat1 = {0};
  1119   1119   
  1120   1120     if( pSession->rc ) return;
  1121   1121   
  1122   1122     /* Load table details if required */
  1123   1123     if( sessionInitTable(pSession, pTab) ) return;
  1124   1124   
  1125   1125     /* Check the number of columns in this xPreUpdate call matches the 
................................................................................
  2716   2716     int rc = SQLITE_OK;
  2717   2717   
  2718   2718     for(i=0; i<nCol && rc==SQLITE_OK; i++){
  2719   2719       int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
  2720   2720       if( abPK && abPK[i]==0 ) continue;
  2721   2721       rc = sessionInputBuffer(pIn, 9);
  2722   2722       if( rc==SQLITE_OK ){
  2723         -      eType = pIn->aData[pIn->iNext++];
  2724         -    }
  2725         -
  2726         -    assert( apOut[i]==0 );
  2727         -    if( eType ){
  2728         -      apOut[i] = sqlite3ValueNew(0);
  2729         -      if( !apOut[i] ) rc = SQLITE_NOMEM;
         2723  +      if( pIn->iNext>=pIn->nData ){
         2724  +        rc = SQLITE_CORRUPT_BKPT;
         2725  +      }else{
         2726  +        eType = pIn->aData[pIn->iNext++];
         2727  +        assert( apOut[i]==0 );
         2728  +        if( eType ){
         2729  +          apOut[i] = sqlite3ValueNew(0);
         2730  +          if( !apOut[i] ) rc = SQLITE_NOMEM;
         2731  +        }
         2732  +      }
  2730   2733       }
  2731   2734   
  2732   2735       if( rc==SQLITE_OK ){
  2733   2736         u8 *aVal = &pIn->aData[pIn->iNext];
  2734   2737         if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
  2735   2738           int nByte;
  2736   2739           pIn->iNext += sessionVarintGet(aVal, &nByte);
  2737   2740           rc = sessionInputBuffer(pIn, nByte);
  2738   2741           if( rc==SQLITE_OK ){
  2739         -          u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
  2740         -          rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
         2742  +          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
         2743  +            rc = SQLITE_CORRUPT_BKPT;
         2744  +          }else{
         2745  +            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
         2746  +            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
         2747  +            pIn->iNext += nByte;
         2748  +          }
  2741   2749           }
  2742         -        pIn->iNext += nByte;
  2743   2750         }
  2744   2751         if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
  2745   2752           sqlite3_int64 v = sessionGetI64(aVal);
  2746   2753           if( eType==SQLITE_INTEGER ){
  2747   2754             sqlite3VdbeMemSetInt64(apOut[i], v);
  2748   2755           }else{
  2749   2756             double d;
................................................................................
  2775   2782     int rc = SQLITE_OK;
  2776   2783     int nCol = 0;
  2777   2784     int nRead = 0;
  2778   2785   
  2779   2786     rc = sessionInputBuffer(pIn, 9);
  2780   2787     if( rc==SQLITE_OK ){
  2781   2788       nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
  2782         -    rc = sessionInputBuffer(pIn, nRead+nCol+100);
  2783         -    nRead += nCol;
         2789  +    /* The hard upper limit for the number of columns in an SQLite
         2790  +    ** database table is, according to sqliteLimit.h, 32676. So 
         2791  +    ** consider any table-header that purports to have more than 65536 
         2792  +    ** columns to be corrupt. This is convenient because otherwise, 
         2793  +    ** if the (nCol>65536) condition below were omitted, a sufficiently 
         2794  +    ** large value for nCol may cause nRead to wrap around and become 
         2795  +    ** negative. Leading to a crash. */
         2796  +    if( nCol<0 || nCol>65536 ){
         2797  +      rc = SQLITE_CORRUPT_BKPT;
         2798  +    }else{
         2799  +      rc = sessionInputBuffer(pIn, nRead+nCol+100);
         2800  +      nRead += nCol;
         2801  +    }
  2784   2802     }
  2785   2803   
  2786   2804     while( rc==SQLITE_OK ){
  2787   2805       while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
  2788   2806         nRead++;
  2789   2807       }
  2790   2808       if( (pIn->iNext + nRead)<pIn->nData ) break;
................................................................................
  2853   2871     assert( p->rc==SQLITE_OK );
  2854   2872   
  2855   2873     rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
  2856   2874     if( rc==SQLITE_OK ){
  2857   2875       int nByte;
  2858   2876       int nVarint;
  2859   2877       nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
  2860         -    nCopy -= nVarint;
  2861         -    p->in.iNext += nVarint;
  2862         -    nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
  2863         -    p->tblhdr.nBuf = 0;
  2864         -    sessionBufferGrow(&p->tblhdr, nByte, &rc);
         2878  +    if( p->nCol>0 ){
         2879  +      nCopy -= nVarint;
         2880  +      p->in.iNext += nVarint;
         2881  +      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
         2882  +      p->tblhdr.nBuf = 0;
         2883  +      sessionBufferGrow(&p->tblhdr, nByte, &rc);
         2884  +    }else{
         2885  +      rc = SQLITE_CORRUPT_BKPT;
         2886  +    }
  2865   2887     }
  2866   2888   
  2867   2889     if( rc==SQLITE_OK ){
  2868   2890       int iPK = sizeof(sqlite3_value*)*p->nCol*2;
  2869   2891       memset(p->tblhdr.aBuf, 0, iPK);
  2870   2892       memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
  2871   2893       p->in.iNext += nCopy;
................................................................................
  2934   2956       p->bPatchset = (op=='P');
  2935   2957       if( sessionChangesetReadTblhdr(p) ) return p->rc;
  2936   2958       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
  2937   2959       p->in.iCurrent = p->in.iNext;
  2938   2960       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
  2939   2961       op = p->in.aData[p->in.iNext++];
  2940   2962     }
         2963  +
         2964  +  if( p->zTab==0 ){
         2965  +    /* The first record in the changeset is not a table header. Must be a
         2966  +    ** corrupt changeset. */
         2967  +    assert( p->in.iNext==1 );
         2968  +    return (p->rc = SQLITE_CORRUPT_BKPT);
         2969  +  }
  2941   2970   
  2942   2971     p->op = op;
  2943   2972     p->bIndirect = p->in.aData[p->in.iNext++];
  2944   2973     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
  2945   2974       return (p->rc = SQLITE_CORRUPT_BKPT);
  2946   2975     }
  2947   2976   
................................................................................
  2977   3006       if( p->bPatchset && p->op==SQLITE_UPDATE ){
  2978   3007         /* If this is an UPDATE that is part of a patchset, then all PK and
  2979   3008         ** modified fields are present in the new.* record. The old.* record
  2980   3009         ** is currently completely empty. This block shifts the PK fields from
  2981   3010         ** new.* to old.*, to accommodate the code that reads these arrays.  */
  2982   3011         for(i=0; i<p->nCol; i++){
  2983   3012           assert( p->apValue[i]==0 );
  2984         -        assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
  2985   3013           if( p->abPK[i] ){
  2986   3014             p->apValue[i] = p->apValue[i+p->nCol];
         3015  +          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
  2987   3016             p->apValue[i+p->nCol] = 0;
  2988   3017           }
  2989   3018         }
  2990   3019       }
  2991   3020     }
  2992   3021   
  2993   3022     return SQLITE_ROW;
................................................................................
  3704   3733     ** in the code below. */
  3705   3734     assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
  3706   3735   
  3707   3736     for(i=0; rc==SQLITE_OK && i<nCol; i++){
  3708   3737       if( !abPK || abPK[i] ){
  3709   3738         sqlite3_value *pVal;
  3710   3739         (void)xValue(pIter, i, &pVal);
  3711         -      rc = sessionBindValue(pStmt, i+1, pVal);
         3740  +      if( pVal==0 ){
         3741  +        /* The value in the changeset was "undefined". This indicates a
         3742  +        ** corrupt changeset blob.  */
         3743  +        rc = SQLITE_CORRUPT_BKPT;
         3744  +      }else{
         3745  +        rc = sessionBindValue(pStmt, i+1, pVal);
         3746  +      }
  3712   3747       }
  3713   3748     }
  3714   3749     return rc;
  3715   3750   }
  3716   3751   
  3717   3752   /*
  3718   3753   ** SQL statement pSelect is as generated by the sessionSelectRow() function.

Changes to ext/session/sqlite3session.h.

     9      9   extern "C" {
    10     10   #endif
    11     11   
    12     12   #include "sqlite3.h"
    13     13   
    14     14   /*
    15     15   ** CAPI3REF: Session Object Handle
           16  +**
           17  +** An instance of this object is a [session] that can be used to
           18  +** record changes to a database.
    16     19   */
    17     20   typedef struct sqlite3_session sqlite3_session;
    18     21   
    19     22   /*
    20     23   ** CAPI3REF: Changeset Iterator Handle
           24  +**
           25  +** An instance of this object acts as a cursor for iterating
           26  +** over the elements of a [changeset] or [patchset].
    21     27   */
    22     28   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
    23     29   
    24     30   /*
    25     31   ** CAPI3REF: Create A New Session Object
           32  +** CONSTRUCTOR: sqlite3_session
    26     33   **
    27     34   ** Create a new session object attached to database handle db. If successful,
    28     35   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
    29     36   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
    30     37   ** error code (e.g. SQLITE_NOMEM) is returned.
    31     38   **
    32     39   ** It is possible to create multiple session objects attached to a single
................................................................................
    55     62     sqlite3 *db,                    /* Database handle */
    56     63     const char *zDb,                /* Name of db (e.g. "main") */
    57     64     sqlite3_session **ppSession     /* OUT: New session object */
    58     65   );
    59     66   
    60     67   /*
    61     68   ** CAPI3REF: Delete A Session Object
           69  +** DESTRUCTOR: sqlite3_session
    62     70   **
    63     71   ** Delete a session object previously allocated using 
    64     72   ** [sqlite3session_create()]. Once a session object has been deleted, the
    65     73   ** results of attempting to use pSession with any other session module
    66     74   ** function are undefined.
    67     75   **
    68     76   ** Session objects must be deleted before the database handle to which they
................................................................................
    70     78   ** [sqlite3session_create()] for details.
    71     79   */
    72     80   void sqlite3session_delete(sqlite3_session *pSession);
    73     81   
    74     82   
    75     83   /*
    76     84   ** CAPI3REF: Enable Or Disable A Session Object
           85  +** METHOD: sqlite3_session
    77     86   **
    78     87   ** Enable or disable the recording of changes by a session object. When
    79     88   ** enabled, a session object records changes made to the database. When
    80     89   ** disabled - it does not. A newly created session object is enabled.
    81     90   ** Refer to the documentation for [sqlite3session_changeset()] for further
    82     91   ** details regarding how enabling and disabling a session object affects
    83     92   ** the eventual changesets.
................................................................................
    89     98   ** The return value indicates the final state of the session object: 0 if 
    90     99   ** the session is disabled, or 1 if it is enabled.
    91    100   */
    92    101   int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
    93    102   
    94    103   /*
    95    104   ** CAPI3REF: Set Or Clear the Indirect Change Flag
          105  +** METHOD: sqlite3_session
    96    106   **
    97    107   ** Each change recorded by a session object is marked as either direct or
    98    108   ** indirect. A change is marked as indirect if either:
    99    109   **
   100    110   ** <ul>
   101    111   **   <li> The session object "indirect" flag is set when the change is
   102    112   **        made, or
................................................................................
   118    128   ** The return value indicates the final state of the indirect flag: 0 if 
   119    129   ** it is clear, or 1 if it is set.
   120    130   */
   121    131   int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
   122    132   
   123    133   /*
   124    134   ** CAPI3REF: Attach A Table To A Session Object
          135  +** METHOD: sqlite3_session
   125    136   **
   126    137   ** If argument zTab is not NULL, then it is the name of a table to attach
   127    138   ** to the session object passed as the first argument. All subsequent changes 
   128    139   ** made to the table while the session object is enabled will be recorded. See 
   129    140   ** documentation for [sqlite3session_changeset()] for further details.
   130    141   **
   131    142   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
   180    191   int sqlite3session_attach(
   181    192     sqlite3_session *pSession,      /* Session object */
   182    193     const char *zTab                /* Table name */
   183    194   );
   184    195   
   185    196   /*
   186    197   ** CAPI3REF: Set a table filter on a Session Object.
          198  +** METHOD: sqlite3_session
   187    199   **
   188    200   ** The second argument (xFilter) is the "filter callback". For changes to rows 
   189    201   ** in tables that are not attached to the Session object, the filter is called
   190    202   ** to determine whether changes to the table's rows should be tracked or not. 
   191    203   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
   192    204   ** attached, xFilter will not be called again.
   193    205   */
................................................................................
   198    210       const char *zTab              /* Table name */
   199    211     ),
   200    212     void *pCtx                      /* First argument passed to xFilter */
   201    213   );
   202    214   
   203    215   /*
   204    216   ** CAPI3REF: Generate A Changeset From A Session Object
          217  +** METHOD: sqlite3_session
   205    218   **
   206    219   ** Obtain a changeset containing changes to the tables attached to the 
   207    220   ** session object passed as the first argument. If successful, 
   208    221   ** set *ppChangeset to point to a buffer containing the changeset 
   209    222   ** and *pnChangeset to the size of the changeset in bytes before returning
   210    223   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
   211    224   ** zero and return an SQLite error code.
................................................................................
   307    320   int sqlite3session_changeset(
   308    321     sqlite3_session *pSession,      /* Session object */
   309    322     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
   310    323     void **ppChangeset              /* OUT: Buffer containing changeset */
   311    324   );
   312    325   
   313    326   /*
   314         -** CAPI3REF: Load The Difference Between Tables Into A Session 
          327  +** CAPI3REF: Load The Difference Between Tables Into A Session
          328  +** METHOD: sqlite3_session
   315    329   **
   316    330   ** If it is not already attached to the session object passed as the first
   317    331   ** argument, this function attaches table zTbl in the same manner as the
   318    332   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
   319    333   ** does not have a primary key, this function is a no-op (but does not return
   320    334   ** an error).
   321    335   **
................................................................................
   372    386     const char *zTbl,
   373    387     char **pzErrMsg
   374    388   );
   375    389   
   376    390   
   377    391   /*
   378    392   ** CAPI3REF: Generate A Patchset From A Session Object
          393  +** METHOD: sqlite3_session
   379    394   **
   380    395   ** The differences between a patchset and a changeset are that:
   381    396   **
   382    397   ** <ul>
   383    398   **   <li> DELETE records consist of the primary key fields only. The 
   384    399   **        original values of other fields are omitted.
   385    400   **   <li> The original values of any modified fields are omitted from 
................................................................................
   423    438   ** guaranteed that a call to sqlite3session_changeset() will return a 
   424    439   ** changeset containing zero changes.
   425    440   */
   426    441   int sqlite3session_isempty(sqlite3_session *pSession);
   427    442   
   428    443   /*
   429    444   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
          445  +** CONSTRUCTOR: sqlite3_changeset_iter
   430    446   **
   431    447   ** Create an iterator used to iterate through the contents of a changeset.
   432    448   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
   433    449   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
   434    450   ** SQLite error code is returned.
   435    451   **
   436    452   ** The following functions can be used to advance and query a changeset 
................................................................................
   463    479     int nChangeset,                 /* Size of changeset blob in bytes */
   464    480     void *pChangeset                /* Pointer to blob containing changeset */
   465    481   );
   466    482   
   467    483   
   468    484   /*
   469    485   ** CAPI3REF: Advance A Changeset Iterator
          486  +** METHOD: sqlite3_changeset_iter
   470    487   **
   471    488   ** This function may only be used with iterators created by function
   472    489   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
   473    490   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
   474    491   ** is returned and the call has no effect.
   475    492   **
   476    493   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
   487    504   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
   488    505   ** SQLITE_NOMEM.
   489    506   */
   490    507   int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
   491    508   
   492    509   /*
   493    510   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
          511  +** METHOD: sqlite3_changeset_iter
   494    512   **
   495    513   ** The pIter argument passed to this function may either be an iterator
   496    514   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   497    515   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
   498    516   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
   499    517   ** is not the case, this function returns [SQLITE_MISUSE].
   500    518   **
................................................................................
   521    539     int *pnCol,                     /* OUT: Number of columns in table */
   522    540     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
   523    541     int *pbIndirect                 /* OUT: True for an 'indirect' change */
   524    542   );
   525    543   
   526    544   /*
   527    545   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
          546  +** METHOD: sqlite3_changeset_iter
   528    547   **
   529    548   ** For each modified table, a changeset includes the following:
   530    549   **
   531    550   ** <ul>
   532    551   **   <li> The number of columns in the table, and
   533    552   **   <li> Which of those columns make up the tables PRIMARY KEY.
   534    553   ** </ul>
................................................................................
   552    571     sqlite3_changeset_iter *pIter,  /* Iterator object */
   553    572     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
   554    573     int *pnCol                      /* OUT: Number of entries in output array */
   555    574   );
   556    575   
   557    576   /*
   558    577   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
          578  +** METHOD: sqlite3_changeset_iter
   559    579   **
   560    580   ** The pIter argument passed to this function may either be an iterator
   561    581   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   562    582   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
   563    583   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   564    584   ** Furthermore, it may only be called if the type of change that the iterator
   565    585   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
   582    602     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   583    603     int iVal,                       /* Column number */
   584    604     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
   585    605   );
   586    606   
   587    607   /*
   588    608   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
          609  +** METHOD: sqlite3_changeset_iter
   589    610   **
   590    611   ** The pIter argument passed to this function may either be an iterator
   591    612   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   592    613   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
   593    614   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   594    615   ** Furthermore, it may only be called if the type of change that the iterator
   595    616   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
   615    636     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   616    637     int iVal,                       /* Column number */
   617    638     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
   618    639   );
   619    640   
   620    641   /*
   621    642   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
          643  +** METHOD: sqlite3_changeset_iter
   622    644   **
   623    645   ** This function should only be used with iterator objects passed to a
   624    646   ** conflict-handler callback by [sqlite3changeset_apply()] with either
   625    647   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
   626    648   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
   627    649   ** is set to NULL.
   628    650   **
................................................................................
   642    664     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   643    665     int iVal,                       /* Column number */
   644    666     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
   645    667   );
   646    668   
   647    669   /*
   648    670   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
          671  +** METHOD: sqlite3_changeset_iter
   649    672   **
   650    673   ** This function may only be called with an iterator passed to an
   651    674   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
   652    675   ** it sets the output variable to the total number of known foreign key
   653    676   ** violations in the destination database and returns SQLITE_OK.
   654    677   **
   655    678   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
   658    681     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   659    682     int *pnOut                      /* OUT: Number of FK violations */
   660    683   );
   661    684   
   662    685   
   663    686   /*
   664    687   ** CAPI3REF: Finalize A Changeset Iterator
          688  +** METHOD: sqlite3_changeset_iter
   665    689   **
   666    690   ** This function is used to finalize an iterator allocated with
   667    691   ** [sqlite3changeset_start()].
   668    692   **
   669    693   ** This function should only be called on iterators created using the
   670    694   ** [sqlite3changeset_start()] function. If an application calls this
   671    695   ** function with an iterator passed to a conflict-handler by
................................................................................
   674    698   **
   675    699   ** If an error was encountered within a call to an sqlite3changeset_xxx()
   676    700   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
   677    701   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
   678    702   ** to that error is returned by this function. Otherwise, SQLITE_OK is
   679    703   ** returned. This is to allow the following pattern (pseudo-code):
   680    704   **
          705  +** <pre>
   681    706   **   sqlite3changeset_start();
   682    707   **   while( SQLITE_ROW==sqlite3changeset_next() ){
   683    708   **     // Do something with change.
   684    709   **   }
   685    710   **   rc = sqlite3changeset_finalize();
   686    711   **   if( rc!=SQLITE_OK ){
   687    712   **     // An error has occurred 
   688    713   **   }
          714  +** </pre>
   689    715   */
   690    716   int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
   691    717   
   692    718   /*
   693    719   ** CAPI3REF: Invert A Changeset
   694    720   **
   695    721   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
   729    755   ** single changeset. The result is a changeset equivalent to applying
   730    756   ** changeset A followed by changeset B. 
   731    757   **
   732    758   ** This function combines the two input changesets using an 
   733    759   ** sqlite3_changegroup object. Calling it produces similar results as the
   734    760   ** following code fragment:
   735    761   **
          762  +** <pre>
   736    763   **   sqlite3_changegroup *pGrp;
   737    764   **   rc = sqlite3_changegroup_new(&pGrp);
   738    765   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
   739    766   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
   740    767   **   if( rc==SQLITE_OK ){
   741    768   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
   742    769   **   }else{
   743    770   **     *ppOut = 0;
   744    771   **     *pnOut = 0;
   745    772   **   }
          773  +** </pre>
   746    774   **
   747    775   ** Refer to the sqlite3_changegroup documentation below for details.
   748    776   */
   749    777   int sqlite3changeset_concat(
   750    778     int nA,                         /* Number of bytes in buffer pA */
   751    779     void *pA,                       /* Pointer to buffer containing changeset A */
   752    780     int nB,                         /* Number of bytes in buffer pB */
................................................................................
   754    782     int *pnOut,                     /* OUT: Number of bytes in output changeset */
   755    783     void **ppOut                    /* OUT: Buffer containing output changeset */
   756    784   );
   757    785   
   758    786   
   759    787   /*
   760    788   ** CAPI3REF: Changegroup Handle
          789  +**
          790  +** A changegroup is an object used to combine two or more 
          791  +** [changesets] or [patchsets]
   761    792   */
   762    793   typedef struct sqlite3_changegroup sqlite3_changegroup;
   763    794   
   764    795   /*
   765    796   ** CAPI3REF: Create A New Changegroup Object
          797  +** CONSTRUCTOR: sqlite3_changegroup
   766    798   **
   767    799   ** An sqlite3_changegroup object is used to combine two or more changesets
   768    800   ** (or patchsets) into a single changeset (or patchset). A single changegroup
   769    801   ** object may combine changesets or patchsets, but not both. The output is
   770    802   ** always in the same format as the input.
   771    803   **
   772    804   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
   796    828   ** sqlite3changegroup_output() functions, also available are the streaming
   797    829   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
   798    830   */
   799    831   int sqlite3changegroup_new(sqlite3_changegroup **pp);
   800    832   
   801    833   /*
   802    834   ** CAPI3REF: Add A Changeset To A Changegroup
          835  +** METHOD: sqlite3_changegroup
   803    836   **
   804    837   ** Add all changes within the changeset (or patchset) in buffer pData (size
   805    838   ** nData bytes) to the changegroup. 
   806    839   **
   807    840   ** If the buffer contains a patchset, then all prior calls to this function
   808    841   ** on the same changegroup object must also have specified patchsets. Or, if
   809    842   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
   873    906   **
   874    907   ** If no error occurs, SQLITE_OK is returned.
   875    908   */
   876    909   int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
   877    910   
   878    911   /*
   879    912   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
          913  +** METHOD: sqlite3_changegroup
   880    914   **
   881    915   ** Obtain a buffer containing a changeset (or patchset) representing the
   882    916   ** current contents of the changegroup. If the inputs to the changegroup
   883    917   ** were themselves changesets, the output is a changeset. Or, if the
   884    918   ** inputs were patchsets, the output is also a patchset.
   885    919   **
   886    920   ** As with the output of the sqlite3session_changeset() and
................................................................................
   903    937     sqlite3_changegroup*,
   904    938     int *pnData,                    /* OUT: Size of output buffer in bytes */
   905    939     void **ppData                   /* OUT: Pointer to output buffer */
   906    940   );
   907    941   
   908    942   /*
   909    943   ** CAPI3REF: Delete A Changegroup Object
          944  +** DESTRUCTOR: sqlite3_changegroup
   910    945   */
   911    946   void sqlite3changegroup_delete(sqlite3_changegroup*);
   912    947   
   913    948   /*
   914    949   ** CAPI3REF: Apply A Changeset To A Database
   915    950   **
   916    951   ** Apply a changeset to a database. This function attempts to update the

Changes to main.mk.

    61     61            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    62     62            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    63     63            fts3_tokenize_vtab.o \
    64     64   	 fts3_unicode.o fts3_unicode2.o \
    65     65            fts3_write.o fts5.o func.o global.o hash.o \
    66     66            icu.o insert.o json1.o legacy.o loadext.o \
    67     67            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    68         -         memjournal.o \
           68  +         memdb.o memjournal.o \
    69     69            mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
    70     70            notify.o opcodes.o os.o os_unix.o os_win.o \
    71     71            pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    72     72            random.o resolve.o rowset.o rtree.o \
    73     73            select.o sqlite3rbu.o status.o stmt.o \
    74     74            table.o threads.o tokenize.o treeview.o trigger.o \
    75     75            update.o userauth.o util.o vacuum.o \
................................................................................
   114    114     $(TOP)/src/main.c \
   115    115     $(TOP)/src/malloc.c \
   116    116     $(TOP)/src/mem0.c \
   117    117     $(TOP)/src/mem1.c \
   118    118     $(TOP)/src/mem2.c \
   119    119     $(TOP)/src/mem3.c \
   120    120     $(TOP)/src/mem5.c \
          121  +  $(TOP)/src/memdb.c \
   121    122     $(TOP)/src/memjournal.c \
   122    123     $(TOP)/src/msvc.h \
   123    124     $(TOP)/src/mutex.c \
   124    125     $(TOP)/src/mutex.h \
   125    126     $(TOP)/src/mutex_noop.c \
   126    127     $(TOP)/src/mutex_unix.c \
   127    128     $(TOP)/src/mutex_w32.c \
................................................................................
   360    361     $(TOP)/ext/misc/csv.c \
   361    362     $(TOP)/ext/misc/eval.c \
   362    363     $(TOP)/ext/misc/fileio.c \
   363    364     $(TOP)/ext/misc/fuzzer.c \
   364    365     $(TOP)/ext/misc/ieee754.c \
   365    366     $(TOP)/ext/misc/mmapwarm.c \
   366    367     $(TOP)/ext/misc/nextchar.c \
          368  +  $(TOP)/ext/misc/normalize.c \
   367    369     $(TOP)/ext/misc/percentile.c \
   368    370     $(TOP)/ext/misc/regexp.c \
   369    371     $(TOP)/ext/misc/remember.c \
   370    372     $(TOP)/ext/misc/series.c \
   371    373     $(TOP)/ext/misc/spellfix.c \
   372    374     $(TOP)/ext/misc/totype.c \
   373    375     $(TOP)/ext/misc/unionvtab.c \
................................................................................
   502    504   SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
   503    505   SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   504    506   SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC
   505    507   SHELL_OPT += -DSQLITE_INTROSPECTION_PRAGMAS
   506    508   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   507    509   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
   508    510   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
          511  +FUZZCHECK_OPT += -DSQLITE_PRINTF_PRECISION_LIMIT=1000
   509    512   DBFUZZ_OPT =
   510    513   KV_OPT = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
   511    514   ST_OPT = -DSQLITE_THREADSAFE=0
   512    515   
   513    516   # This is the default Makefile target.  The objects listed here
   514    517   # are what get build when you type just "make" with no arguments.
   515    518   #
................................................................................
   556    559   		$(TOP)/test/fuzzcheck.c $(TOP)/test/ossfuzz.c sqlite3.c $(TLIBS) $(THREADLIB)
   557    560   
   558    561   ossshell$(EXE):	$(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c sqlite3.h
   559    562   	$(TCCX) -o ossshell$(EXE) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
   560    563   		-DSQLITE_ENABLE_MEMSYS5 $(FUZZCHECK_OPT) \
   561    564   		$(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c $(TLIBS) $(THREADLIB)
   562    565   
          566  +sessionfuzz$(EXE):	$(TOP)/test/sessionfuzz.c sqlite3.c sqlite3.h
          567  +	$(TCC) -o sessionfuzz$(EXE) $(TOP)/test/sessionfuzz.c -lz $(TLIBS) $(THREADLIB)
          568  +
   563    569   mptester$(EXE):	sqlite3.c $(TOP)/mptest/mptest.c
   564    570   	$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
   565    571   		$(TLIBS) $(THREADLIB)
   566    572   
   567    573   MPTEST1=./mptester$(EXE) mptest1.db $(TOP)/mptest/crash01.test --repeat 20
   568    574   MPTEST2=./mptester$(EXE) mptest2.db $(TOP)/mptest/multiwrite01.test --repeat 20
   569    575   mptest:	mptester$(EXE)
................................................................................
   876    882   
   877    883   fulltestonly:	$(TESTPROGS) fuzztest
   878    884   	./testfixture$(EXE) $(TOP)/test/full.test $(TESTOPTS)
   879    885   
   880    886   queryplantest:	testfixture$(EXE) sqlite3$(EXE)
   881    887   	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner $(TESTOPTS)
   882    888   
   883         -fuzztest:	fuzzcheck$(EXE) $(FUZZDATA)
          889  +fuzztest:	fuzzcheck$(EXE) $(FUZZDATA) sessionfuzz$(EXE) $(TOP)/test/sessionfuzz-data1.db
   884    890   	./fuzzcheck$(EXE) $(FUZZDATA)
          891  +	./sessionfuzz run $(TOP)/test/sessionfuzz-data1.db
   885    892   
   886         -fastfuzztest:	fuzzcheck$(EXE) $(FUZZDATA)
          893  +fastfuzztest:	fuzzcheck$(EXE) $(FUZZDATA) sessionfuzz$(EXE) $(TOP)/test/sessionfuzz-data1.db
   887    894   	./fuzzcheck$(EXE) --limit-mem 100M $(FUZZDATA)
          895  +	./sessionfuzz run $(TOP)/test/sessionfuzz-data1.db
   888    896   
   889         -valgrindfuzz:	fuzzcheck$(EXE) $(FUZZDATA)
          897  +valgrindfuzz:	fuzzcheck$(EXE) $(FUZZDATA) sessionfuzz$(EXE) $(TOP)/test/sessionfuzz-data1.db
   890    898   	valgrind ./fuzzcheck$(EXE) --cell-size-check --limit-mem 10M --timeout 600 $(FUZZDATA)
          899  +	valgrind ./sessionfuzz run $(TOP)/test/sessionfuzz-data1.db
   891    900   
   892    901   # The veryquick.test TCL tests.
   893    902   #
   894    903   tcltest:	./testfixture$(EXE)
   895    904   	./testfixture$(EXE) $(TOP)/test/veryquick.test $(TESTOPTS)
   896    905   
   897    906   # A very quick test using only testfixture and omitting all the slower
................................................................................
  1047   1056   	rm -f shell.c sqlite3ext.h
  1048   1057   	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
  1049   1058   	rm -f sqlite3_expert sqlite3_expert.exe 
  1050   1059   	rm -f sqlite-*-output.vsix
  1051   1060   	rm -f mptester mptester.exe
  1052   1061   	rm -f fuzzershell fuzzershell.exe
  1053   1062   	rm -f fuzzcheck fuzzcheck.exe
         1063  +	rm -f sessionfuzz
  1054   1064   	rm -f sqldiff sqldiff.exe
  1055   1065   	rm -f fts5.* fts5parse.*
  1056   1066   	rm -f lsm.h lsm1.c

Changes to src/attach.c.

    51     51   **
    52     52   **     ATTACH DATABASE x AS y KEY z
    53     53   **
    54     54   **     SELECT sqlite_attach(x, y, z)
    55     55   **
    56     56   ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
    57     57   ** third argument.
           58  +**
           59  +** If the db->init.reopenMemdb flags is set, then instead of attaching a
           60  +** new database, close the database on db->init.iDb and reopen it as an
           61  +** empty MemDB.
    58     62   */
    59     63   static void attachFunc(
    60     64     sqlite3_context *context,
    61     65     int NotUsed,
    62     66     sqlite3_value **argv
    63     67   ){
    64     68     int i;
................................................................................
    71     75     unsigned int flags;
    72     76     Db *aNew;                 /* New array of Db pointers */
    73     77     Db *pNew;                 /* Db object for the newly attached database */
    74     78     char *zErrDyn = 0;
    75     79     sqlite3_vfs *pVfs;
    76     80   
    77     81     UNUSED_PARAMETER(NotUsed);
    78         -
    79     82     zFile = (const char *)sqlite3_value_text(argv[0]);
    80     83     zName = (const char *)sqlite3_value_text(argv[1]);
    81     84     if( zFile==0 ) zFile = "";
    82     85     if( zName==0 ) zName = "";
    83     86   
    84         -  /* Check for the following errors:
    85         -  **
    86         -  **     * Too many attached databases,
    87         -  **     * Transaction currently open
    88         -  **     * Specified database name already being used.
    89         -  */
    90         -  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
    91         -    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
    92         -      db->aLimit[SQLITE_LIMIT_ATTACHED]
    93         -    );
    94         -    goto attach_error;
    95         -  }
    96         -  for(i=0; i<db->nDb; i++){
    97         -    char *z = db->aDb[i].zDbSName;
    98         -    assert( z && zName );
    99         -    if( sqlite3StrICmp(z, zName)==0 ){
   100         -      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
           87  +#ifdef SQLITE_ENABLE_DESERIALIZE
           88  +# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
           89  +#else
           90  +# define REOPEN_AS_MEMDB(db)  (0)
           91  +#endif
           92  +
           93  +  if( REOPEN_AS_MEMDB(db) ){
           94  +    /* This is not a real ATTACH.  Instead, this routine is being called
           95  +    ** from sqlite3_deserialize() to close database db->init.iDb and
           96  +    ** reopen it as a MemDB */
           97  +    pVfs = sqlite3_vfs_find("memdb");
           98  +    if( pVfs==0 ) return;
           99  +    pNew = &db->aDb[db->init.iDb];
          100  +    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
          101  +    pNew->pBt = 0;
          102  +    pNew->pSchema = 0;
          103  +    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
          104  +  }else{
          105  +    /* This is a real ATTACH
          106  +    **
          107  +    ** Check for the following errors:
          108  +    **
          109  +    **     * Too many attached databases,
          110  +    **     * Transaction currently open
          111  +    **     * Specified database name already being used.
          112  +    */
          113  +    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
          114  +      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
          115  +        db->aLimit[SQLITE_LIMIT_ATTACHED]
          116  +      );
   101    117         goto attach_error;
   102    118       }
   103         -  }
   104         -
   105         -  /* Allocate the new entry in the db->aDb[] array and initialize the schema
   106         -  ** hash tables.
   107         -  */
   108         -  if( db->aDb==db->aDbStatic ){
   109         -    aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
   110         -    if( aNew==0 ) return;
   111         -    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
   112         -  }else{
   113         -    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
   114         -    if( aNew==0 ) return;
   115         -  }
   116         -  db->aDb = aNew;
   117         -  pNew = &db->aDb[db->nDb];
   118         -  memset(pNew, 0, sizeof(*pNew));
   119         -
   120         -  /* Open the database file. If the btree is successfully opened, use
   121         -  ** it to obtain the database schema. At this point the schema may
   122         -  ** or may not be initialized.
   123         -  */
   124         -  flags = db->openFlags;
   125         -  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
   126         -  if( rc!=SQLITE_OK ){
   127         -    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
   128         -    sqlite3_result_error(context, zErr, -1);
   129         -    sqlite3_free(zErr);
   130         -    return;
   131         -  }
   132         -  assert( pVfs );
   133         -  flags |= SQLITE_OPEN_MAIN_DB;
   134         -  rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
   135         -  sqlite3_free( zPath );
   136         -  db->nDb++;
          119  +    for(i=0; i<db->nDb; i++){
          120  +      char *z = db->aDb[i].zDbSName;
          121  +      assert( z && zName );
          122  +      if( sqlite3StrICmp(z, zName)==0 ){
          123  +        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
          124  +        goto attach_error;
          125  +      }
          126  +    }
          127  +  
          128  +    /* Allocate the new entry in the db->aDb[] array and initialize the schema
          129  +    ** hash tables.
          130  +    */
          131  +    if( db->aDb==db->aDbStatic ){
          132  +      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
          133  +      if( aNew==0 ) return;
          134  +      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
          135  +    }else{
          136  +      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
          137  +      if( aNew==0 ) return;
          138  +    }
          139  +    db->aDb = aNew;
          140  +    pNew = &db->aDb[db->nDb];
          141  +    memset(pNew, 0, sizeof(*pNew));
          142  +  
          143  +    /* Open the database file. If the btree is successfully opened, use
          144  +    ** it to obtain the database schema. At this point the schema may
          145  +    ** or may not be initialized.
          146  +    */
          147  +    flags = db->openFlags;
          148  +    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
          149  +    if( rc!=SQLITE_OK ){
          150  +      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
          151  +      sqlite3_result_error(context, zErr, -1);
          152  +      sqlite3_free(zErr);
          153  +      return;
          154  +    }
          155  +    assert( pVfs );
          156  +    flags |= SQLITE_OPEN_MAIN_DB;
          157  +    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
          158  +    sqlite3_free( zPath );
          159  +    db->nDb++;
          160  +  }
   137    161     db->skipBtreeMutex = 0;
   138    162     if( rc==SQLITE_CONSTRAINT ){
   139    163       rc = SQLITE_ERROR;
   140    164       zErrDyn = sqlite3MPrintf(db, "database is already attached");
   141    165     }else if( rc==SQLITE_OK ){
   142    166       Pager *pPager;
   143    167       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
................................................................................
   156    180   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   157    181       sqlite3BtreeSetPagerFlags(pNew->pBt,
   158    182                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
   159    183   #endif
   160    184       sqlite3BtreeLeave(pNew->pBt);
   161    185     }
   162    186     pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
   163         -  pNew->zDbSName = sqlite3DbStrDup(db, zName);
          187  +  if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
   164    188     if( rc==SQLITE_OK && pNew->zDbSName==0 ){
   165    189       rc = SQLITE_NOMEM_BKPT;
   166    190     }
   167    191   
   168    192   
   169    193   #ifdef SQLITE_HAS_CODEC
   170    194     if( rc==SQLITE_OK ){
................................................................................
   196    220           break;
   197    221       }
   198    222     }
   199    223   #endif
   200    224   
   201    225     /* If the file was opened successfully, read the schema for the new database.
   202    226     ** If this fails, or if opening the file failed, then close the file and 
   203         -  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
   204         -  ** we found it.
          227  +  ** remove the entry from the db->aDb[] array. i.e. put everything back the
          228  +  ** way we found it.
   205    229     */
   206    230     if( rc==SQLITE_OK ){
   207    231       sqlite3BtreeEnterAll(db);
          232  +    db->init.iDb = 0;
   208    233       rc = sqlite3Init(db, &zErrDyn);
   209    234       sqlite3BtreeLeaveAll(db);
          235  +    assert( zErrDyn==0 || rc!=SQLITE_OK );
   210    236     }
   211    237   #ifdef SQLITE_USER_AUTHENTICATION
   212    238     if( rc==SQLITE_OK ){
   213    239       u8 newAuth = 0;
   214    240       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
   215    241       if( newAuth<db->auth.authLevel ){
   216    242         rc = SQLITE_AUTH_USER;
   217    243       }
   218    244     }
   219    245   #endif
   220    246     if( rc ){
   221         -    int iDb = db->nDb - 1;
   222         -    assert( iDb>=2 );
   223         -    if( db->aDb[iDb].pBt ){
   224         -      sqlite3BtreeClose(db->aDb[iDb].pBt);
   225         -      db->aDb[iDb].pBt = 0;
   226         -      db->aDb[iDb].pSchema = 0;
   227         -    }
   228         -    sqlite3ResetAllSchemasOfConnection(db);
   229         -    db->nDb = iDb;
   230         -    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   231         -      sqlite3OomFault(db);
   232         -      sqlite3DbFree(db, zErrDyn);
   233         -      zErrDyn = sqlite3MPrintf(db, "out of memory");
   234         -    }else if( zErrDyn==0 ){
   235         -      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
          247  +    if( !REOPEN_AS_MEMDB(db) ){
          248  +      int iDb = db->nDb - 1;
          249  +      assert( iDb>=2 );
          250  +      if( db->aDb[iDb].pBt ){
          251  +        sqlite3BtreeClose(db->aDb[iDb].pBt);
          252  +        db->aDb[iDb].pBt = 0;
          253  +        db->aDb[iDb].pSchema = 0;
          254  +      }
          255  +      sqlite3ResetAllSchemasOfConnection(db);
          256  +      db->nDb = iDb;
          257  +      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
          258  +        sqlite3OomFault(db);
          259  +        sqlite3DbFree(db, zErrDyn);
          260  +        zErrDyn = sqlite3MPrintf(db, "out of memory");
          261  +      }else if( zErrDyn==0 ){
          262  +        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
          263  +      }
   236    264       }
   237    265       goto attach_error;
   238    266     }
   239    267     
   240    268     return;
   241    269   
   242    270   attach_error:
................................................................................
   499    527         return 1;
   500    528       }
   501    529       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
   502    530         return 1;
   503    531       }
   504    532       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
   505    533         return 1;
          534  +    }
          535  +    if( pSelect->pWith ){
          536  +      int i;
          537  +      for(i=0; i<pSelect->pWith->nCte; i++){
          538  +        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
          539  +          return 1;
          540  +        }
          541  +      }
   506    542       }
   507    543       pSelect = pSelect->pPrior;
   508    544     }
   509    545     return 0;
   510    546   }
   511    547   int sqlite3FixExpr(
   512    548     DbFixer *pFix,     /* Context of the fixation */

Changes to src/btree.c.

  4347   4347   **
  4348   4348   ** The simple approach here would be to memset() the entire object
  4349   4349   ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
  4350   4350   ** do not need to be zeroed and they are large, so we can save a lot
  4351   4351   ** of run-time by skipping the initialization of those elements.
  4352   4352   */
  4353   4353   void sqlite3BtreeCursorZero(BtCursor *p){
  4354         -  memset(p, 0, offsetof(BtCursor, iPage));
         4354  +  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
  4355   4355   }
  4356   4356   
  4357   4357   /*
  4358   4358   ** Close a cursor.  The read lock on the database file is released
  4359   4359   ** when the last cursor is closed.
  4360   4360   */
  4361   4361   int sqlite3BtreeCloseCursor(BtCursor *pCur){
................................................................................
  4390   4390   ** BtCursor.info structure.  If it is not already valid, call
  4391   4391   ** btreeParseCell() to fill it in.
  4392   4392   **
  4393   4393   ** BtCursor.info is a cache of the information in the current cell.
  4394   4394   ** Using this cache reduces the number of calls to btreeParseCell().
  4395   4395   */
  4396   4396   #ifndef NDEBUG
         4397  +  static int cellInfoEqual(CellInfo *a, CellInfo *b){
         4398  +    if( a->nKey!=b->nKey ) return 0;
         4399  +    if( a->pPayload!=b->pPayload ) return 0;
         4400  +    if( a->nPayload!=b->nPayload ) return 0;
         4401  +    if( a->nLocal!=b->nLocal ) return 0;
         4402  +    if( a->nSize!=b->nSize ) return 0;
         4403  +    return 1;
         4404  +  }
  4397   4405     static void assertCellInfo(BtCursor *pCur){
  4398   4406       CellInfo info;
  4399   4407       memset(&info, 0, sizeof(info));
  4400   4408       btreeParseCell(pCur->pPage, pCur->ix, &info);
  4401         -    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
         4409  +    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
  4402   4410     }
  4403   4411   #else
  4404   4412     #define assertCellInfo(x)
  4405   4413   #endif
  4406   4414   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
  4407   4415     if( pCur->info.nSize==0 ){
  4408   4416       pCur->curFlags |= BTCF_ValidNKey;
................................................................................
  4670   4678       ** The aOverflow[] array is sized at one entry for each overflow page
  4671   4679       ** in the overflow chain. The page number of the first overflow page is
  4672   4680       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
  4673   4681       ** means "not yet known" (the cache is lazily populated).
  4674   4682       */
  4675   4683       if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
  4676   4684         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
  4677         -      if( nOvfl>pCur->nOvflAlloc ){
         4685  +      if( pCur->aOverflow==0
         4686  +       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
         4687  +      ){
  4678   4688           Pgno *aNew = (Pgno*)sqlite3Realloc(
  4679   4689               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  4680   4690           );
  4681   4691           if( aNew==0 ){
  4682   4692             return SQLITE_NOMEM_BKPT;
  4683   4693           }else{
  4684         -          pCur->nOvflAlloc = nOvfl*2;
  4685   4694             pCur->aOverflow = aNew;
  4686   4695           }
  4687   4696         }
  4688   4697         memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
  4689   4698         pCur->curFlags |= BTCF_ValidOvfl;
  4690   4699       }else{
  4691   4700         /* If the overflow page-list cache has been allocated and the
................................................................................
  6191   6200   static void freePage(MemPage *pPage, int *pRC){
  6192   6201     if( (*pRC)==SQLITE_OK ){
  6193   6202       *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
  6194   6203     }
  6195   6204   }
  6196   6205   
  6197   6206   /*
  6198         -** Free any overflow pages associated with the given Cell.  Write the
  6199         -** local Cell size (the number of bytes on the original page, omitting
  6200         -** overflow) into *pnSize.
         6207  +** Free any overflow pages associated with the given Cell.  Store
         6208  +** size information about the cell in pInfo.
  6201   6209   */
  6202   6210   static int clearCell(
  6203   6211     MemPage *pPage,          /* The page that contains the Cell */
  6204   6212     unsigned char *pCell,    /* First byte of the Cell */
  6205   6213     CellInfo *pInfo          /* Size information about the cell */
  6206   6214   ){
  6207   6215     BtShared *pBt;
................................................................................
  7397   7405       */
  7398   7406       if( pOld->aData[0]!=apOld[0]->aData[0] ){
  7399   7407         rc = SQLITE_CORRUPT_BKPT;
  7400   7408         goto balance_cleanup;
  7401   7409       }
  7402   7410   
  7403   7411       /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
  7404         -    ** constains overflow cells, include them in the b.apCell[] array
         7412  +    ** contains overflow cells, include them in the b.apCell[] array
  7405   7413       ** in the correct spot.
  7406   7414       **
  7407   7415       ** Note that when there are multiple overflow cells, it is always the
  7408   7416       ** case that they are sequential and adjacent.  This invariant arises
  7409   7417       ** because multiple overflows can only occurs when inserting divider
  7410   7418       ** cells into a parent on a prior balance, and divider cells are always
  7411   7419       ** adjacent and are inserted in order.  There is an assert() tagged

Changes to src/btreeInt.h.

   499    499   **    eState==FAULT:                   Cursor fault with skipNext as error code.
   500    500   */
   501    501   struct BtCursor {
   502    502     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   503    503     u8 curFlags;              /* zero or more BTCF_* flags defined below */
   504    504     u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
   505    505     u8 hints;                 /* As configured by CursorSetHints() */
   506         -  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
   507         -  Btree *pBtree;            /* The Btree to which this cursor belongs */
   508         -  BtShared *pBt;            /* The BtShared this cursor points to */
   509         -  BtCursor *pNext;          /* Forms a linked list of all cursors */
   510         -  Pgno *aOverflow;          /* Cache of overflow page locations */
   511         -  CellInfo info;            /* A parse of the cell we are pointing at */
   512         -  i64 nKey;                 /* Size of pKey, or last integer key */
   513         -  void *pKey;               /* Saved key that was cursor last known position */
   514         -  Pgno pgnoRoot;            /* The root page of this tree */
   515    506     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
   516    507                      ** Error code if eState==CURSOR_FAULT */
          508  +  Btree *pBtree;            /* The Btree to which this cursor belongs */
          509  +  Pgno *aOverflow;          /* Cache of overflow page locations */
          510  +  void *pKey;               /* Saved key that was cursor last known position */
   517    511     /* All fields above are zeroed when the cursor is allocated.  See
   518    512     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
   519    513     ** initialized. */
          514  +#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
          515  +  BtShared *pBt;            /* The BtShared this cursor points to */
          516  +  BtCursor *pNext;          /* Forms a linked list of all cursors */
          517  +  CellInfo info;            /* A parse of the cell we are pointing at */
          518  +  i64 nKey;                 /* Size of pKey, or last integer key */
          519  +  Pgno pgnoRoot;            /* The root page of this tree */
   520    520     i8 iPage;                 /* Index of current page in apPage */
   521    521     u8 curIntKey;             /* Value of apPage[0]->intKey */
   522    522     u16 ix;                   /* Current index for apPage[iPage] */
   523    523     u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
   524    524     struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
   525    525     MemPage *pPage;                        /* Current page */
   526    526     MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
................................................................................
   562    562   ** CURSOR_FAULT:
   563    563   **   An unrecoverable error (an I/O error or a malloc failure) has occurred
   564    564   **   on a different connection that shares the BtShared cache with this
   565    565   **   cursor.  The error has left the cache in an inconsistent state.
   566    566   **   Do nothing else with this cursor.  Any attempt to use the cursor
   567    567   **   should return the error code stored in BtCursor.skipNext
   568    568   */
   569         -#define CURSOR_INVALID           0
   570         -#define CURSOR_VALID             1
          569  +#define CURSOR_VALID             0
          570  +#define CURSOR_INVALID           1
   571    571   #define CURSOR_SKIPNEXT          2
   572    572   #define CURSOR_REQUIRESEEK       3
   573    573   #define CURSOR_FAULT             4
   574    574   
   575    575   /* 
   576    576   ** The database page the PENDING_BYTE occupies. This page is never used.
   577    577   */

Changes to src/build.c.

  1114   1114   ** This routine is called by the parser while in the middle of
  1115   1115   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
  1116   1116   ** been seen on a column.  This routine sets the notNull flag on
  1117   1117   ** the column currently under construction.
  1118   1118   */
  1119   1119   void sqlite3AddNotNull(Parse *pParse, int onError){
  1120   1120     Table *p;
         1121  +  Column *pCol;
  1121   1122     p = pParse->pNewTable;
  1122   1123     if( p==0 || NEVER(p->nCol<1) ) return;
  1123         -  p->aCol[p->nCol-1].notNull = (u8)onError;
         1124  +  pCol = &p->aCol[p->nCol-1];
         1125  +  pCol->notNull = (u8)onError;
  1124   1126     p->tabFlags |= TF_HasNotNull;
         1127  +
         1128  +  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
         1129  +  ** on this column.  */
         1130  +  if( pCol->colFlags & COLFLAG_UNIQUE ){
         1131  +    Index *pIdx;
         1132  +    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
         1133  +      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
         1134  +      if( pIdx->aiColumn[0]==p->nCol-1 ){
         1135  +        pIdx->uniqNotNull = 1;
         1136  +      }
         1137  +    }
         1138  +  }
  1125   1139   }
  1126   1140   
  1127   1141   /*
  1128   1142   ** Scan the column type name zType (length nType) and return the
  1129   1143   ** associated affinity type.
  1130   1144   **
  1131   1145   ** This routine does a case-independent search of zType for the 
................................................................................
  3081   3095   
  3082   3096     /* If pList==0, it means this routine was called to make a primary
  3083   3097     ** key out of the last column added to the table under construction.
  3084   3098     ** So create a fake list to simulate this.
  3085   3099     */
  3086   3100     if( pList==0 ){
  3087   3101       Token prevCol;
  3088         -    sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
         3102  +    Column *pCol = &pTab->aCol[pTab->nCol-1];
         3103  +    pCol->colFlags |= COLFLAG_UNIQUE;
         3104  +    sqlite3TokenInit(&prevCol, pCol->zName);
  3089   3105       pList = sqlite3ExprListAppend(pParse, 0,
  3090   3106                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
  3091   3107       if( pList==0 ) goto exit_create_index;
  3092   3108       assert( pList->nExpr==1 );
  3093   3109       sqlite3ExprListSetSortOrder(pList, sortOrder);
  3094   3110     }else{
  3095   3111       sqlite3ExprListCheckLength(pParse, pList, "index");

Changes to src/expr.c.

  1727   1727   ** This callback is used by multiple expression walkers.
  1728   1728   */
  1729   1729   int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
  1730   1730     UNUSED_PARAMETER(NotUsed);
  1731   1731     pWalker->eCode = 0;
  1732   1732     return WRC_Abort;
  1733   1733   }
         1734  +
         1735  +/*
         1736  +** If the input expression is an ID with the name "true" or "false"
         1737  +** then convert it into an TK_TRUEFALSE term.  Return non-zero if
         1738  +** the conversion happened, and zero if the expression is unaltered.
         1739  +*/
         1740  +int sqlite3ExprIdToTrueFalse(Expr *pExpr){
         1741  +  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
         1742  +  if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
         1743  +   || sqlite3StrICmp(pExpr->u.zToken, "false")==0
         1744  +  ){
         1745  +    pExpr->op = TK_TRUEFALSE;
         1746  +    return 1;
         1747  +  }
         1748  +  return 0;
         1749  +}
         1750  +
         1751  +/*
         1752  +** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
         1753  +** and 0 if it is FALSE.
         1754  +*/
         1755  +int sqlite3ExprTruthValue(const Expr *pExpr){
         1756  +  assert( pExpr->op==TK_TRUEFALSE );
         1757  +  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
         1758  +       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
         1759  +  return pExpr->u.zToken[4]==0;
         1760  +}
         1761  +
  1734   1762   
  1735   1763   /*
  1736   1764   ** These routines are Walker callbacks used to check expressions to
  1737   1765   ** see if they are "constant" for some definition of constant.  The
  1738   1766   ** Walker.eCode value determines the type of "constant" we are looking
  1739   1767   ** for.
  1740   1768   **
................................................................................
  1775   1803         if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
  1776   1804           return WRC_Continue;
  1777   1805         }else{
  1778   1806           pWalker->eCode = 0;
  1779   1807           return WRC_Abort;
  1780   1808         }
  1781   1809       case TK_ID:
         1810  +      /* Convert "true" or "false" in a DEFAULT clause into the
         1811  +      ** appropriate TK_TRUEFALSE operator */
         1812  +      if( sqlite3ExprIdToTrueFalse(pExpr) ){
         1813  +        return WRC_Prune;
         1814  +      }
         1815  +      /* Fall thru */
  1782   1816       case TK_COLUMN:
  1783   1817       case TK_AGG_FUNCTION:
  1784   1818       case TK_AGG_COLUMN:
  1785   1819         testcase( pExpr->op==TK_ID );
  1786   1820         testcase( pExpr->op==TK_COLUMN );
  1787   1821         testcase( pExpr->op==TK_AGG_FUNCTION );
  1788   1822         testcase( pExpr->op==TK_AGG_COLUMN );
................................................................................
  3538   3572         return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
  3539   3573                                  pExpr->iColumn, iTab, target,
  3540   3574                                  pExpr->op2);
  3541   3575       }
  3542   3576       case TK_INTEGER: {
  3543   3577         codeInteger(pParse, pExpr, 0, target);
  3544   3578         return target;
         3579  +    }
         3580  +    case TK_TRUEFALSE: {
         3581  +      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
         3582  +      return target;
  3545   3583       }
  3546   3584   #ifndef SQLITE_OMIT_FLOATING_POINT
  3547   3585       case TK_FLOAT: {
  3548   3586         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3549   3587         codeReal(v, pExpr->u.zToken, 0, target);
  3550   3588         return target;
  3551   3589       }
................................................................................
  3693   3731       case TK_NOT: {
  3694   3732         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
  3695   3733         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
  3696   3734         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3697   3735         testcase( regFree1==0 );
  3698   3736         sqlite3VdbeAddOp2(v, op, r1, inReg);
  3699   3737         break;
         3738  +    }
         3739  +    case TK_TRUTH: {
         3740  +      int isTrue;    /* IS TRUE or IS NOT TRUE */
         3741  +      int bNormal;   /* IS TRUE or IS FALSE */
         3742  +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
         3743  +      testcase( regFree1==0 );
         3744  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         3745  +      bNormal = pExpr->op2==TK_IS;
         3746  +      testcase( isTrue && bNormal);
         3747  +      testcase( !isTrue && bNormal);
         3748  +      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
         3749  +      break;
  3700   3750       }
  3701   3751       case TK_ISNULL:
  3702   3752       case TK_NOTNULL: {
  3703   3753         int addr;
  3704   3754         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
  3705   3755         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
  3706   3756         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
................................................................................
  4468   4518         sqlite3ExprCachePop(pParse);
  4469   4519         break;
  4470   4520       }
  4471   4521       case TK_NOT: {
  4472   4522         testcase( jumpIfNull==0 );
  4473   4523         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4474   4524         break;
         4525  +    }
         4526  +    case TK_TRUTH: {
         4527  +      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
         4528  +      int isTrue;     /* IS TRUE or IS NOT TRUE */
         4529  +      testcase( jumpIfNull==0 );
         4530  +      isNot = pExpr->op2==TK_ISNOT;
         4531  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         4532  +      testcase( isTrue && isNot );
         4533  +      testcase( !isTrue && isNot );
         4534  +      if( isTrue ^ isNot ){
         4535  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
         4536  +                          isNot ? SQLITE_JUMPIFNULL : 0);
         4537  +      }else{
         4538  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
         4539  +                           isNot ? SQLITE_JUMPIFNULL : 0);
         4540  +      }
         4541  +      break;
  4475   4542       }
  4476   4543       case TK_IS:
  4477   4544       case TK_ISNOT:
  4478   4545         testcase( op==TK_IS );
  4479   4546         testcase( op==TK_ISNOT );
  4480   4547         op = (op==TK_IS) ? TK_EQ : TK_NE;
  4481   4548         jumpIfNull = SQLITE_NULLEQ;
................................................................................
  4622   4689         sqlite3ExprCachePop(pParse);
  4623   4690         break;
  4624   4691       }
  4625   4692       case TK_NOT: {
  4626   4693         testcase( jumpIfNull==0 );
  4627   4694         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4628   4695         break;
         4696  +    }
         4697  +    case TK_TRUTH: {
         4698  +      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
         4699  +      int isTrue;  /* IS TRUE or IS NOT TRUE */
         4700  +      testcase( jumpIfNull==0 );
         4701  +      isNot = pExpr->op2==TK_ISNOT;
         4702  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         4703  +      testcase( isTrue && isNot );
         4704  +      testcase( !isTrue && isNot );
         4705  +      if( isTrue ^ isNot ){
         4706  +        /* IS TRUE and IS NOT FALSE */
         4707  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
         4708  +                           isNot ? 0 : SQLITE_JUMPIFNULL);
         4709  +
         4710  +      }else{
         4711  +        /* IS FALSE and IS NOT TRUE */
         4712  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
         4713  +                          isNot ? 0 : SQLITE_JUMPIFNULL);
         4714  +      }
         4715  +      break;
  4629   4716       }
  4630   4717       case TK_IS:
  4631   4718       case TK_ISNOT:
  4632   4719         testcase( pExpr->op==TK_IS );
  4633   4720         testcase( pExpr->op==TK_ISNOT );
  4634   4721         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
  4635   4722         jumpIfNull = SQLITE_NULLEQ;

Changes to src/func.c.

    31     31   }
    32     32   
    33     33   /*
    34     34   ** Indicate that the accumulator load should be skipped on this
    35     35   ** iteration of the aggregate loop.
    36     36   */
    37     37   static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
           38  +  assert( context->isError<=0 );
           39  +  context->isError = -1;
    38     40     context->skipFlag = 1;
    39     41   }
    40     42   
    41     43   /*
    42     44   ** Implementation of the non-aggregate min() and max() functions
    43     45   */
    44     46   static void minmaxFunc(
................................................................................
    97     99   ** Implementation of the length() function
    98    100   */
    99    101   static void lengthFunc(
   100    102     sqlite3_context *context,
   101    103     int argc,
   102    104     sqlite3_value **argv
   103    105   ){
   104         -  int len;
   105         -
   106    106     assert( argc==1 );
   107    107     UNUSED_PARAMETER(argc);
   108    108     switch( sqlite3_value_type(argv[0]) ){
   109    109       case SQLITE_BLOB:
   110    110       case SQLITE_INTEGER:
   111    111       case SQLITE_FLOAT: {
   112    112         sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
   113    113         break;
   114    114       }
   115    115       case SQLITE_TEXT: {
   116    116         const unsigned char *z = sqlite3_value_text(argv[0]);
          117  +      const unsigned char *z0;
          118  +      unsigned char c;
   117    119         if( z==0 ) return;
   118         -      len = 0;
   119         -      while( *z ){
   120         -        len++;
   121         -        SQLITE_SKIP_UTF8(z);
          120  +      z0 = z;
          121  +      while( (c = *z)!=0 ){
          122  +        z++;
          123  +        if( c>=0xc0 ){
          124  +          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
          125  +        }
   122    126         }
   123         -      sqlite3_result_int(context, len);
          127  +      sqlite3_result_int(context, (int)(z-z0));
   124    128         break;
   125    129       }
   126    130       default: {
   127    131         sqlite3_result_null(context);
   128    132         break;
   129    133       }
   130    134     }
................................................................................
  1191   1195     unsigned char *zOut;              /* The output */
  1192   1196     int nStr;                /* Size of zStr */
  1193   1197     int nPattern;            /* Size of zPattern */
  1194   1198     int nRep;                /* Size of zRep */
  1195   1199     i64 nOut;                /* Maximum size of zOut */
  1196   1200     int loopLimit;           /* Last zStr[] that might match zPattern[] */
  1197   1201     int i, j;                /* Loop counters */
         1202  +  unsigned cntExpand;      /* Number zOut expansions */
         1203  +  sqlite3 *db = sqlite3_context_db_handle(context);
  1198   1204   
  1199   1205     assert( argc==3 );
  1200   1206     UNUSED_PARAMETER(argc);
  1201   1207     zStr = sqlite3_value_text(argv[0]);
  1202   1208     if( zStr==0 ) return;
  1203   1209     nStr = sqlite3_value_bytes(argv[0]);
  1204   1210     assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
................................................................................
  1222   1228     nOut = nStr + 1;
  1223   1229     assert( nOut<SQLITE_MAX_LENGTH );
  1224   1230     zOut = contextMalloc(context, (i64)nOut);
  1225   1231     if( zOut==0 ){
  1226   1232       return;
  1227   1233     }
  1228   1234     loopLimit = nStr - nPattern;  
         1235  +  cntExpand = 0;
  1229   1236     for(i=j=0; i<=loopLimit; i++){
  1230   1237       if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
  1231   1238         zOut[j++] = zStr[i];
  1232   1239       }else{
  1233         -      u8 *zOld;
  1234         -      sqlite3 *db = sqlite3_context_db_handle(context);
  1235         -      nOut += nRep - nPattern;
  1236         -      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1237         -      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
  1238         -      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1239         -        sqlite3_result_error_toobig(context);
  1240         -        sqlite3_free(zOut);
  1241         -        return;
  1242         -      }
  1243         -      zOld = zOut;
  1244         -      zOut = sqlite3_realloc64(zOut, (int)nOut);
  1245         -      if( zOut==0 ){
  1246         -        sqlite3_result_error_nomem(context);
  1247         -        sqlite3_free(zOld);
  1248         -        return;
         1240  +      if( nRep>nPattern ){
         1241  +        nOut += nRep - nPattern;
         1242  +        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
         1243  +        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
         1244  +        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
         1245  +          sqlite3_result_error_toobig(context);
         1246  +          sqlite3_free(zOut);
         1247  +          return;
         1248  +        }
         1249  +        cntExpand++;
         1250  +        if( (cntExpand&(cntExpand-1))==0 ){
         1251  +          /* Grow the size of the output buffer only on substitutions
         1252  +          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
         1253  +          u8 *zOld;
         1254  +          zOld = zOut;
         1255  +          zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
         1256  +          if( zOut==0 ){
         1257  +            sqlite3_result_error_nomem(context);
         1258  +            sqlite3_free(zOld);
         1259  +            return;
         1260  +          }
         1261  +        }
  1249   1262         }
  1250   1263         memcpy(&zOut[j], zRep, nRep);
  1251   1264         j += nRep;
  1252   1265         i += nPattern-1;
  1253   1266       }
  1254   1267     }
  1255         -  assert( j+nStr-i+1==nOut );
         1268  +  assert( j+nStr-i+1<=nOut );
  1256   1269     memcpy(&zOut[j], &zStr[i], nStr-i);
  1257   1270     j += nStr - i;
  1258   1271     assert( j<=nOut );
  1259   1272     zOut[j] = 0;
  1260   1273     sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
  1261   1274   }
  1262   1275   

Changes to src/global.c.

   254    254   ** Constant tokens for values 0 and 1.
   255    255   */
   256    256   const Token sqlite3IntTokens[] = {
   257    257      { "0", 1 },
   258    258      { "1", 1 }
   259    259   };
   260    260   
          261  +#ifdef VDBE_PROFILE
          262  +/*
          263  +** The following performance counter can be used in place of
          264  +** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
          265  +*/
          266  +sqlite3_uint64 sqlite3NProfileCnt = 0;
          267  +#endif
   261    268   
   262    269   /*
   263    270   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
   264    271   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
   265    272   ** the database page that contains the pending byte.  It never attempts
   266    273   ** to read or write that page.  The pending byte page is set aside
   267    274   ** for use by the VFS layers as space for managing file locks.

Changes to src/main.c.

   238    238       if( sqlite3GlobalConfig.isPCacheInit==0 ){
   239    239         rc = sqlite3PcacheInitialize();
   240    240       }
   241    241       if( rc==SQLITE_OK ){
   242    242         sqlite3GlobalConfig.isPCacheInit = 1;
   243    243         rc = sqlite3OsInit();
   244    244       }
          245  +#ifdef SQLITE_ENABLE_DESERIALIZE
          246  +    if( rc==SQLITE_OK ){
          247  +      rc = sqlite3MemdbInit();
          248  +    }
          249  +#endif
   245    250       if( rc==SQLITE_OK ){
   246    251         sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
   247    252             sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
   248    253         sqlite3GlobalConfig.isInit = 1;
   249    254   #ifdef SQLITE_EXTRA_INIT
   250    255         bRunExtraInit = 1;
   251    256   #endif
................................................................................
   270    275     ** been compiled correctly.  It is important to run this code, but
   271    276     ** we don't want to run it too often and soak up CPU cycles for no
   272    277     ** reason.  So we run it once during initialization.
   273    278     */
   274    279   #ifndef NDEBUG
   275    280   #ifndef SQLITE_OMIT_FLOATING_POINT
   276    281     /* This section of code's only "output" is via assert() statements. */
   277         -  if ( rc==SQLITE_OK ){
          282  +  if( rc==SQLITE_OK ){
   278    283       u64 x = (((u64)1)<<63)-1;
   279    284       double y;
   280    285       assert(sizeof(x)==8);
   281    286       assert(sizeof(x)==sizeof(y));
   282    287       memcpy(&y, &x, 8);
   283    288       assert( sqlite3IsNaN(y) );
   284    289     }
................................................................................
  1442   1447   #else
  1443   1448       /* SQLITE_NOLFS       */ 0,
  1444   1449   #endif
  1445   1450       /* SQLITE_AUTH        */ "authorization denied",
  1446   1451       /* SQLITE_FORMAT      */ 0,
  1447   1452       /* SQLITE_RANGE       */ "column index out of range",
  1448   1453       /* SQLITE_NOTADB      */ "file is not a database",
         1454  +    /* SQLITE_NOTICE      */ "notification message",
         1455  +    /* SQLITE_WARNING     */ "warning message",
  1449   1456     };
  1450   1457     const char *zErr = "unknown error";
  1451   1458     switch( rc ){
  1452   1459       case SQLITE_ABORT_ROLLBACK: {
  1453   1460         zErr = "abort due to ROLLBACK";
  1454   1461         break;
         1462  +    }
         1463  +    case SQLITE_ROW: {
         1464  +      zErr = "another row available";
         1465  +      break;
         1466  +    }
         1467  +    case SQLITE_DONE: {
         1468  +      zErr = "no more rows available";
         1469  +      break;
  1455   1470       }
  1456   1471       default: {
  1457   1472         rc &= 0xff;
  1458   1473         if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
  1459   1474           zErr = aMsg[rc];
  1460   1475         }
  1461   1476         break;

Added src/memdb.c.

            1  +/*
            2  +** 2016-09-07
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file implements in-memory VFS.  A database is held as a contiguous
           14  +** block of memory.
           15  +**
           16  +** This file also implements interface sqlite3_serialize() and
           17  +** sqlite3_deserialize().
           18  +*/
           19  +#ifdef SQLITE_ENABLE_DESERIALIZE
           20  +#include "sqliteInt.h"
           21  +
           22  +/*
           23  +** Forward declaration of objects used by this utility
           24  +*/
           25  +typedef struct sqlite3_vfs MemVfs;
           26  +typedef struct MemFile MemFile;
           27  +
           28  +/* Access to a lower-level VFS that (might) implement dynamic loading,
           29  +** access to randomness, etc.
           30  +*/
           31  +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
           32  +
           33  +/* An open file */
           34  +struct MemFile {
           35  +  sqlite3_file base;              /* IO methods */
           36  +  sqlite3_int64 sz;               /* Size of the file */
           37  +  sqlite3_int64 szMax;            /* Space allocated to aData */
           38  +  unsigned char *aData;           /* content of the file */
           39  +  int nMmap;                      /* Number of memory mapped pages */
           40  +  unsigned mFlags;                /* Flags */
           41  +  int eLock;                      /* Most recent lock against this file */
           42  +};
           43  +
           44  +/*
           45  +** Methods for MemFile
           46  +*/
           47  +static int memdbClose(sqlite3_file*);
           48  +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
           49  +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
           50  +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
           51  +static int memdbSync(sqlite3_file*, int flags);
           52  +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
           53  +static int memdbLock(sqlite3_file*, int);
           54  +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
           55  +static int memdbFileControl(sqlite3_file*, int op, void *pArg);
           56  +/* static int memdbSectorSize(sqlite3_file*); // not used */
           57  +static int memdbDeviceCharacteristics(sqlite3_file*);
           58  +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
           59  +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
           60  +
           61  +/*
           62  +** Methods for MemVfs
           63  +*/
           64  +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
           65  +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
           66  +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
           67  +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
           68  +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
           69  +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
           70  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
           71  +static void memdbDlClose(sqlite3_vfs*, void*);
           72  +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
           73  +static int memdbSleep(sqlite3_vfs*, int microseconds);
           74  +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
           75  +static int memdbGetLastError(sqlite3_vfs*, int, char *);
           76  +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
           77  +
           78  +static sqlite3_vfs memdb_vfs = {
           79  +  2,                           /* iVersion */
           80  +  0,                           /* szOsFile (set when registered) */
           81  +  1024,                        /* mxPathname */
           82  +  0,                           /* pNext */
           83  +  "memdb",                     /* zName */
           84  +  0,                           /* pAppData (set when registered) */ 
           85  +  memdbOpen,                   /* xOpen */
           86  +  0, /* memdbDelete, */        /* xDelete */
           87  +  memdbAccess,                 /* xAccess */
           88  +  memdbFullPathname,           /* xFullPathname */
           89  +  memdbDlOpen,                 /* xDlOpen */
           90  +  memdbDlError,                /* xDlError */
           91  +  memdbDlSym,                  /* xDlSym */
           92  +  memdbDlClose,                /* xDlClose */
           93  +  memdbRandomness,             /* xRandomness */
           94  +  memdbSleep,                  /* xSleep */
           95  +  0, /* memdbCurrentTime, */   /* xCurrentTime */
           96  +  memdbGetLastError,           /* xGetLastError */
           97  +  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
           98  +};
           99  +
          100  +static const sqlite3_io_methods memdb_io_methods = {
          101  +  3,                              /* iVersion */
          102  +  memdbClose,                      /* xClose */
          103  +  memdbRead,                       /* xRead */
          104  +  memdbWrite,                      /* xWrite */
          105  +  memdbTruncate,                   /* xTruncate */
          106  +  memdbSync,                       /* xSync */
          107  +  memdbFileSize,                   /* xFileSize */
          108  +  memdbLock,                       /* xLock */
          109  +  memdbLock,                       /* xUnlock - same as xLock in this case */ 
          110  +  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
          111  +  memdbFileControl,                /* xFileControl */
          112  +  0, /* memdbSectorSize,*/         /* xSectorSize */
          113  +  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
          114  +  0,                               /* xShmMap */
          115  +  0,                               /* xShmLock */
          116  +  0,                               /* xShmBarrier */
          117  +  0,                               /* xShmUnmap */
          118  +  memdbFetch,                      /* xFetch */
          119  +  memdbUnfetch                     /* xUnfetch */
          120  +};
          121  +
          122  +
          123  +
          124  +/*
          125  +** Close an memdb-file.
          126  +**
          127  +** The pData pointer is owned by the application, so there is nothing
          128  +** to free.
          129  +*/
          130  +static int memdbClose(sqlite3_file *pFile){
          131  +  MemFile *p = (MemFile *)pFile;
          132  +  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
          133  +  return SQLITE_OK;
          134  +}
          135  +
          136  +/*
          137  +** Read data from an memdb-file.
          138  +*/
          139  +static int memdbRead(
          140  +  sqlite3_file *pFile, 
          141  +  void *zBuf, 
          142  +  int iAmt, 
          143  +  sqlite_int64 iOfst
          144  +){
          145  +  MemFile *p = (MemFile *)pFile;
          146  +  if( iOfst+iAmt>p->sz ){
          147  +    memset(zBuf, 0, iAmt);
          148  +    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
          149  +    return SQLITE_IOERR_SHORT_READ;
          150  +  }
          151  +  memcpy(zBuf, p->aData+iOfst, iAmt);
          152  +  return SQLITE_OK;
          153  +}
          154  +
          155  +/*
          156  +** Try to enlarge the memory allocation to hold at least sz bytes
          157  +*/
          158  +static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
          159  +  unsigned char *pNew;
          160  +  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
          161  +    return SQLITE_FULL;
          162  +  }
          163  +  pNew = sqlite3_realloc64(p->aData, newSz);
          164  +  if( pNew==0 ) return SQLITE_NOMEM;
          165  +  p->aData = pNew;
          166  +  p->szMax = newSz;
          167  +  return SQLITE_OK;
          168  +}
          169  +
          170  +/*
          171  +** Write data to an memdb-file.
          172  +*/
          173  +static int memdbWrite(
          174  +  sqlite3_file *pFile,
          175  +  const void *z,
          176  +  int iAmt,
          177  +  sqlite_int64 iOfst
          178  +){
          179  +  MemFile *p = (MemFile *)pFile;
          180  +  if( iOfst+iAmt>p->sz ){
          181  +    int rc;
          182  +    if( iOfst+iAmt>p->szMax
          183  +     && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
          184  +    ){
          185  +      return rc;
          186  +    }
          187  +    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
          188  +    p->sz = iOfst+iAmt;
          189  +  }
          190  +  memcpy(p->aData+iOfst, z, iAmt);
          191  +  return SQLITE_OK;
          192  +}
          193  +
          194  +/*
          195  +** Truncate an memdb-file.
          196  +**
          197  +** In rollback mode (which is always the case for memdb, as it does not
          198  +** support WAL mode) the truncate() method is only used to reduce
          199  +** the size of a file, never to increase the size.
          200  +*/
          201  +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
          202  +  MemFile *p = (MemFile *)pFile;
          203  +  if( NEVER(size>p->sz) ) return SQLITE_FULL;
          204  +  p->sz = size; 
          205  +  return SQLITE_OK;
          206  +}
          207  +
          208  +/*
          209  +** Sync an memdb-file.
          210  +*/
          211  +static int memdbSync(sqlite3_file *pFile, int flags){
          212  +  return SQLITE_OK;
          213  +}
          214  +
          215  +/*
          216  +** Return the current file-size of an memdb-file.
          217  +*/
          218  +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
          219  +  MemFile *p = (MemFile *)pFile;
          220  +  *pSize = p->sz;
          221  +  return SQLITE_OK;
          222  +}
          223  +
          224  +/*
          225  +** Lock an memdb-file.
          226  +*/
          227  +static int memdbLock(sqlite3_file *pFile, int eLock){
          228  +  MemFile *p = (MemFile *)pFile;
          229  +  p->eLock = eLock;
          230  +  return SQLITE_OK;
          231  +}
          232  +
          233  +#if 0 /* Never used because memdbAccess() always returns false */
          234  +/*
          235  +** Check if another file-handle holds a RESERVED lock on an memdb-file.
          236  +*/
          237  +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
          238  +  *pResOut = 0;
          239  +  return SQLITE_OK;
          240  +}
          241  +#endif
          242  +
          243  +/*
          244  +** File control method. For custom operations on an memdb-file.
          245  +*/
          246  +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
          247  +  MemFile *p = (MemFile *)pFile;
          248  +  int rc = SQLITE_NOTFOUND;
          249  +  if( op==SQLITE_FCNTL_VFSNAME ){
          250  +    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
          251  +    rc = SQLITE_OK;
          252  +  }
          253  +  return rc;
          254  +}
          255  +
          256  +#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
          257  +/*
          258  +** Return the sector-size in bytes for an memdb-file.
          259  +*/
          260  +static int memdbSectorSize(sqlite3_file *pFile){
          261  +  return 1024;
          262  +}
          263  +#endif
          264  +
          265  +/*
          266  +** Return the device characteristic flags supported by an memdb-file.
          267  +*/
          268  +static int memdbDeviceCharacteristics(sqlite3_file *pFile){
          269  +  return SQLITE_IOCAP_ATOMIC | 
          270  +         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
          271  +         SQLITE_IOCAP_SAFE_APPEND |
          272  +         SQLITE_IOCAP_SEQUENTIAL;
          273  +}
          274  +
          275  +/* Fetch a page of a memory-mapped file */
          276  +static int memdbFetch(
          277  +  sqlite3_file *pFile,
          278  +  sqlite3_int64 iOfst,
          279  +  int iAmt,
          280  +  void **pp
          281  +){
          282  +  MemFile *p = (MemFile *)pFile;
          283  +  p->nMmap++;
          284  +  *pp = (void*)(p->aData + iOfst);
          285  +  return SQLITE_OK;
          286  +}
          287  +
          288  +/* Release a memory-mapped page */
          289  +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
          290  +  MemFile *p = (MemFile *)pFile;
          291  +  p->nMmap--;
          292  +  return SQLITE_OK;
          293  +}
          294  +
          295  +/*
          296  +** Open an mem file handle.
          297  +*/
          298  +static int memdbOpen(
          299  +  sqlite3_vfs *pVfs,
          300  +  const char *zName,
          301  +  sqlite3_file *pFile,
          302  +  int flags,
          303  +  int *pOutFlags
          304  +){
          305  +  MemFile *p = (MemFile*)pFile;
          306  +  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
          307  +    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
          308  +  }
          309  +  memset(p, 0, sizeof(*p));
          310  +  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
          311  +  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
          312  +  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
          313  +  p->base.pMethods = &memdb_io_methods;
          314  +  return SQLITE_OK;
          315  +}
          316  +
          317  +#if 0 /* Only used to delete rollback journals, master journals, and WAL
          318  +      ** files, none of which exist in memdb.  So this routine is never used */
          319  +/*
          320  +** Delete the file located at zPath. If the dirSync argument is true,
          321  +** ensure the file-system modifications are synced to disk before
          322  +** returning.
          323  +*/
          324  +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
          325  +  return SQLITE_IOERR_DELETE;
          326  +}
          327  +#endif
          328  +
          329  +/*
          330  +** Test for access permissions. Return true if the requested permission
          331  +** is available, or false otherwise.
          332  +**
          333  +** With memdb, no files ever exist on disk.  So always return false.
          334  +*/
          335  +static int memdbAccess(
          336  +  sqlite3_vfs *pVfs, 
          337  +  const char *zPath, 
          338  +  int flags, 
          339  +  int *pResOut
          340  +){
          341  +  *pResOut = 0;
          342  +  return SQLITE_OK;
          343  +}
          344  +
          345  +/*
          346  +** Populate buffer zOut with the full canonical pathname corresponding
          347  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
          348  +** of at least (INST_MAX_PATHNAME+1) bytes.
          349  +*/
          350  +static int memdbFullPathname(
          351  +  sqlite3_vfs *pVfs, 
          352  +  const char *zPath, 
          353  +  int nOut, 
          354  +  char *zOut
          355  +){
          356  +  sqlite3_snprintf(nOut, zOut, "%s", zPath);
          357  +  return SQLITE_OK;
          358  +}
          359  +
          360  +/*
          361  +** Open the dynamic library located at zPath and return a handle.
          362  +*/
          363  +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
          364  +  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
          365  +}
          366  +
          367  +/*
          368  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
          369  +** utf-8 string describing the most recent error encountered associated 
          370  +** with dynamic libraries.
          371  +*/
          372  +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
          373  +  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
          374  +}
          375  +
          376  +/*
          377  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
          378  +*/
          379  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
          380  +  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
          381  +}
          382  +
          383  +/*
          384  +** Close the dynamic library handle pHandle.
          385  +*/
          386  +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
          387  +  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
          388  +}
          389  +
          390  +/*
          391  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
          392  +** random data.
          393  +*/
          394  +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
          395  +  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
          396  +}
          397  +
          398  +/*
          399  +** Sleep for nMicro microseconds. Return the number of microseconds 
          400  +** actually slept.
          401  +*/
          402  +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
          403  +  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
          404  +}
          405  +
          406  +#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
          407  +/*
          408  +** Return the current time as a Julian Day number in *pTimeOut.
          409  +*/
          410  +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
          411  +  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
          412  +}
          413  +#endif
          414  +
          415  +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
          416  +  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
          417  +}
          418  +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
          419  +  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
          420  +}
          421  +
          422  +/*
          423  +** Translate a database connection pointer and schema name into a
          424  +** MemFile pointer.
          425  +*/
          426  +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
          427  +  MemFile *p = 0;
          428  +  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
          429  +  if( rc ) return 0;
          430  +  if( p->base.pMethods!=&memdb_io_methods ) return 0;
          431  +  return p;
          432  +}
          433  +
          434  +/*
          435  +** Return the serialization of a database
          436  +*/
          437  +unsigned char *sqlite3_serialize(
          438  +  sqlite3 *db,              /* The database connection */
          439  +  const char *zSchema,      /* Which database within the connection */
          440  +  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
          441  +  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
          442  +){
          443  +  MemFile *p;
          444  +  int iDb;
          445  +  Btree *pBt;
          446  +  sqlite3_int64 sz;
          447  +  int szPage = 0;
          448  +  sqlite3_stmt *pStmt = 0;
          449  +  unsigned char *pOut;
          450  +  char *zSql;
          451  +  int rc;
          452  +
          453  +#ifdef SQLITE_ENABLE_API_ARMOR
          454  +  if( !sqlite3SafetyCheckOk(db) ){
          455  +    (void)SQLITE_MISUSE_BKPT;
          456  +    return 0;
          457  +  }
          458  +#endif
          459  +
          460  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
          461  +  p = memdbFromDbSchema(db, zSchema);
          462  +  iDb = sqlite3FindDbName(db, zSchema);
          463  +  if( piSize ) *piSize = -1;
          464  +  if( iDb<0 ) return 0;
          465  +  if( p ){
          466  +    if( piSize ) *piSize = p->sz;
          467  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
          468  +      pOut = p->aData;
          469  +    }else{
          470  +      pOut = sqlite3_malloc64( p->sz );
          471  +      if( pOut ) memcpy(pOut, p->aData, p->sz);
          472  +    }
          473  +    return pOut;
          474  +  }
          475  +  pBt = db->aDb[iDb].pBt;
          476  +  if( pBt==0 ) return 0;
          477  +  szPage = sqlite3BtreeGetPageSize(pBt);
          478  +  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
          479  +  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
          480  +  sqlite3_free(zSql);
          481  +  if( rc ) return 0;
          482  +  rc = sqlite3_step(pStmt);
          483  +  if( rc!=SQLITE_ROW ){
          484  +    pOut = 0;
          485  +  }else{
          486  +    sz = sqlite3_column_int64(pStmt, 0)*szPage;
          487  +    if( piSize ) *piSize = sz;
          488  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
          489  +      pOut = 0;
          490  +    }else{
          491  +      pOut = sqlite3_malloc64( sz );
          492  +      if( pOut ){
          493  +        int nPage = sqlite3_column_int(pStmt, 0);
          494  +        Pager *pPager = sqlite3BtreePager(pBt);
          495  +        int pgno;
          496  +        for(pgno=1; pgno<=nPage; pgno++){
          497  +          DbPage *pPage = 0;
          498  +          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
          499  +          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
          500  +          if( rc==SQLITE_OK ){
          501  +            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
          502  +          }else{
          503  +            memset(pTo, 0, szPage);
          504  +          }
          505  +          sqlite3PagerUnref(pPage);       
          506  +        }
          507  +      }
          508  +    }
          509  +  }
          510  +  sqlite3_finalize(pStmt);
          511  +  return pOut;
          512  +}
          513  +
          514  +/* Convert zSchema to a MemDB and initialize its content.
          515  +*/
          516  +int sqlite3_deserialize(
          517  +  sqlite3 *db,            /* The database connection */
          518  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
          519  +  unsigned char *pData,   /* The serialized database content */
          520  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
          521  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
          522  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
          523  +){
          524  +  MemFile *p;
          525  +  char *zSql;
          526  +  sqlite3_stmt *pStmt = 0;
          527  +  int rc;
          528  +  int iDb;
          529  +
          530  +#ifdef SQLITE_ENABLE_API_ARMOR
          531  +  if( !sqlite3SafetyCheckOk(db) ){
          532  +    return SQLITE_MISUSE_BKPT;
          533  +  }
          534  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
          535  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
          536  +#endif
          537  +
          538  +  sqlite3_mutex_enter(db->mutex);
          539  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
          540  +  iDb = sqlite3FindDbName(db, zSchema);
          541  +  if( iDb<0 ){
          542  +    rc = SQLITE_ERROR;
          543  +    goto end_deserialize;
          544  +  }    
          545  +  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
          546  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
          547  +  sqlite3_free(zSql);
          548  +  if( rc ) goto end_deserialize;
          549  +  db->init.iDb = (u8)iDb;
          550  +  db->init.reopenMemdb = 1;
          551  +  rc = sqlite3_step(pStmt);
          552  +  db->init.reopenMemdb = 0;
          553  +  if( rc!=SQLITE_DONE ){
          554  +    rc = SQLITE_ERROR;
          555  +    goto end_deserialize;
          556  +  }
          557  +  p = memdbFromDbSchema(db, zSchema);
          558  +  if( p==0 ){
          559  +    rc = SQLITE_ERROR;
          560  +  }else{
          561  +    p->aData = pData;
          562  +    p->sz = szDb;
          563  +    p->szMax = szBuf;
          564  +    p->mFlags = mFlags;
          565  +    rc = SQLITE_OK;
          566  +  }
          567  +
          568  +end_deserialize:
          569  +  sqlite3_finalize(pStmt);
          570  +  sqlite3_mutex_leave(db->mutex);
          571  +  return rc;
          572  +}
          573  +
          574  +/* 
          575  +** This routine is called when the extension is loaded.
          576  +** Register the new VFS.
          577  +*/
          578  +int sqlite3MemdbInit(void){
          579  +  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
          580  +  int sz = pLower->szOsFile;
          581  +  memdb_vfs.pAppData = pLower;
          582  +  /* In all known configurations of SQLite, the size of a default
          583  +  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
          584  +  ** Should that ever change, remove the following NEVER() */
          585  +  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
          586  +  memdb_vfs.szOsFile = sz;
          587  +  return sqlite3_vfs_register(&memdb_vfs, 0);
          588  +}
          589  +#endif /* SQLITE_ENABLE_DESERIALIZE */

Changes to src/mutex_unix.c.

    46     46   #if SQLITE_MUTEX_NREF
    47     47     volatile int nRef;         /* Number of entrances */
    48     48     volatile pthread_t owner;  /* Thread that is within this mutex */
    49     49     int trace;                 /* True to trace changes */
    50     50   #endif
    51     51   };
    52     52   #if SQLITE_MUTEX_NREF
    53         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
           53  +# define SQLITE3_MUTEX_INITIALIZER(id) \
           54  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
    54     55   #elif defined(SQLITE_ENABLE_API_ARMOR)
    55         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
           56  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
    56     57   #else
    57         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
           58  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
    58     59   #endif
    59     60   
    60     61   /*
    61     62   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    62     63   ** intended for use only inside assert() statements.  On some platforms,
    63     64   ** there might be race conditions that can cause these routines to
    64     65   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
   147    148   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   148    149   ** returns a different mutex on every call.  But for the static 
   149    150   ** mutex types, the same mutex is returned on every call that has
   150    151   ** the same type number.
   151    152   */
   152    153   static sqlite3_mutex *pthreadMutexAlloc(int iType){
   153    154     static sqlite3_mutex staticMutexes[] = {
   154         -    SQLITE3_MUTEX_INITIALIZER,
   155         -    SQLITE3_MUTEX_INITIALIZER,
   156         -    SQLITE3_MUTEX_INITIALIZER,
   157         -    SQLITE3_MUTEX_INITIALIZER,
   158         -    SQLITE3_MUTEX_INITIALIZER,
   159         -    SQLITE3_MUTEX_INITIALIZER,
   160         -    SQLITE3_MUTEX_INITIALIZER,
   161         -    SQLITE3_MUTEX_INITIALIZER,
   162         -    SQLITE3_MUTEX_INITIALIZER,
   163         -    SQLITE3_MUTEX_INITIALIZER,
   164         -    SQLITE3_MUTEX_INITIALIZER,
   165         -    SQLITE3_MUTEX_INITIALIZER
          155  +    SQLITE3_MUTEX_INITIALIZER(2),
          156  +    SQLITE3_MUTEX_INITIALIZER(3),
          157  +    SQLITE3_MUTEX_INITIALIZER(4),
          158  +    SQLITE3_MUTEX_INITIALIZER(5),
          159  +    SQLITE3_MUTEX_INITIALIZER(6),
          160  +    SQLITE3_MUTEX_INITIALIZER(7),
          161  +    SQLITE3_MUTEX_INITIALIZER(8),
          162  +    SQLITE3_MUTEX_INITIALIZER(9),
          163  +    SQLITE3_MUTEX_INITIALIZER(10),
          164  +    SQLITE3_MUTEX_INITIALIZER(11),
          165  +    SQLITE3_MUTEX_INITIALIZER(12),
          166  +    SQLITE3_MUTEX_INITIALIZER(13)
   166    167     };
   167    168     sqlite3_mutex *p;
   168    169     switch( iType ){
   169    170       case SQLITE_MUTEX_RECURSIVE: {
   170    171         p = sqlite3MallocZero( sizeof(*p) );
   171    172         if( p ){
   172    173   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
   177    178           /* Use a recursive mutex if it is available */
   178    179           pthread_mutexattr_t recursiveAttr;
   179    180           pthread_mutexattr_init(&recursiveAttr);
   180    181           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
   181    182           pthread_mutex_init(&p->mutex, &recursiveAttr);
   182    183           pthread_mutexattr_destroy(&recursiveAttr);
   183    184   #endif
          185  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
          186  +        p->id = SQLITE_MUTEX_RECURSIVE;
          187  +#endif
   184    188         }
   185    189         break;
   186    190       }
   187    191       case SQLITE_MUTEX_FAST: {
   188    192         p = sqlite3MallocZero( sizeof(*p) );
   189    193         if( p ){
   190    194           pthread_mutex_init(&p->mutex, 0);
          195  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
          196  +        p->id = SQLITE_MUTEX_FAST;
          197  +#endif
   191    198         }
   192    199         break;
   193    200       }
   194    201       default: {
   195    202   #ifdef SQLITE_ENABLE_API_ARMOR
   196    203         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
   197    204           (void)SQLITE_MISUSE_BKPT;
................................................................................
   199    206         }
   200    207   #endif
   201    208         p = &staticMutexes[iType-2];
   202    209         break;
   203    210       }
   204    211     }
   205    212   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
   206         -  if( p ) p->id = iType;
          213  +  assert( p==0 || p->id==iType );
   207    214   #endif
   208    215     return p;
   209    216   }
   210    217   
   211    218   
   212    219   /*
   213    220   ** This routine deallocates a previously

Changes to src/mutex_w32.c.

    36     36   */
    37     37   struct sqlite3_mutex {
    38     38     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
    39     39     int id;                    /* Mutex type */
    40     40   #ifdef SQLITE_DEBUG
    41     41     volatile int nRef;         /* Number of enterances */
    42     42     volatile DWORD owner;      /* Thread holding this mutex */
    43         -  volatile int trace;        /* True to trace changes */
           43  +  volatile LONG trace;       /* True to trace changes */
    44     44   #endif
    45     45   };
    46     46   
    47     47   /*
    48     48   ** These are the initializer values used when declaring a "static" mutex
    49     49   ** on Win32.  It should be noted that all mutexes require initialization
    50     50   ** on the Win32 platform.
    51     51   */
    52     52   #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
    53     53   
    54     54   #ifdef SQLITE_DEBUG
    55         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
           55  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
    56     56                                       0L, (DWORD)0, 0 }
    57     57   #else
    58         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
           58  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
    59     59   #endif
    60     60   
    61     61   #ifdef SQLITE_DEBUG
    62     62   /*
    63     63   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    64     64   ** intended for use only inside assert() statements.
    65     65   */
................................................................................
    94     94   #endif
    95     95   }
    96     96   
    97     97   /*
    98     98   ** Initialize and deinitialize the mutex subsystem.
    99     99   */
   100    100   static sqlite3_mutex winMutex_staticMutexes[] = {
   101         -  SQLITE3_MUTEX_INITIALIZER,
   102         -  SQLITE3_MUTEX_INITIALIZER,
   103         -  SQLITE3_MUTEX_INITIALIZER,
   104         -  SQLITE3_MUTEX_INITIALIZER,
   105         -  SQLITE3_MUTEX_INITIALIZER,
   106         -  SQLITE3_MUTEX_INITIALIZER,
   107         -  SQLITE3_MUTEX_INITIALIZER,
   108         -  SQLITE3_MUTEX_INITIALIZER,
   109         -  SQLITE3_MUTEX_INITIALIZER,
   110         -  SQLITE3_MUTEX_INITIALIZER,
   111         -  SQLITE3_MUTEX_INITIALIZER,
   112         -  SQLITE3_MUTEX_INITIALIZER
          101  +  SQLITE3_MUTEX_INITIALIZER(2),
          102  +  SQLITE3_MUTEX_INITIALIZER(3),
          103  +  SQLITE3_MUTEX_INITIALIZER(4),
          104  +  SQLITE3_MUTEX_INITIALIZER(5),
          105  +  SQLITE3_MUTEX_INITIALIZER(6),
          106  +  SQLITE3_MUTEX_INITIALIZER(7),
          107  +  SQLITE3_MUTEX_INITIALIZER(8),
          108  +  SQLITE3_MUTEX_INITIALIZER(9),
          109  +  SQLITE3_MUTEX_INITIALIZER(10),
          110  +  SQLITE3_MUTEX_INITIALIZER(11),
          111  +  SQLITE3_MUTEX_INITIALIZER(12),
          112  +  SQLITE3_MUTEX_INITIALIZER(13)
   113    113   };
   114    114   
   115    115   static int winMutex_isInit = 0;
   116    116   static int winMutex_isNt = -1; /* <0 means "need to query" */
   117    117   
   118    118   /* As the winMutexInit() and winMutexEnd() functions are called as part
   119    119   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
................................................................................
   235    235   #ifdef SQLITE_ENABLE_API_ARMOR
   236    236         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
   237    237           (void)SQLITE_MISUSE_BKPT;
   238    238           return 0;
   239    239         }
   240    240   #endif
   241    241         p = &winMutex_staticMutexes[iType-2];
   242         -      p->id = iType;
   243    242   #ifdef SQLITE_DEBUG
   244    243   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
   245         -      p->trace = 1;
          244  +      InterlockedCompareExchange(&p->trace, 1, 0);
   246    245   #endif
   247    246   #endif
   248    247         break;
   249    248       }
   250    249     }
          250  +  assert( p==0 || p->id==iType );
   251    251     return p;
   252    252   }
   253    253   
   254    254   
   255    255   /*
   256    256   ** This routine deallocates a previously
   257    257   ** allocated mutex.  SQLite is careful to deallocate every

Changes to src/os_unix.c.

   752    752   #if defined(HAVE_FCHOWN)
   753    753     { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
   754    754   #else
   755    755     { "fchown",       (sqlite3_syscall_ptr)0,               0 },
   756    756   #endif
   757    757   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
   758    758   
          759  +#if defined(HAVE_FCHOWN)
   759    760     { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
          761  +#else
          762  +  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
          763  +#endif
   760    764   #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
   761    765   
   762    766   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
   763    767     { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
   764    768   #else
   765    769     { "mmap",         (sqlite3_syscall_ptr)0,               0 },
   766    770   #endif
................................................................................
   980    984   ** is held when required. This function is only used as part of assert() 
   981    985   ** statements. e.g.
   982    986   **
   983    987   **   unixEnterMutex()
   984    988   **     assert( unixMutexHeld() );
   985    989   **   unixEnterLeave()
   986    990   */
          991  +static sqlite3_mutex *unixBigLock = 0;
   987    992   static void unixEnterMutex(void){
   988         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
          993  +  sqlite3_mutex_enter(unixBigLock);
   989    994   }
   990    995   static void unixLeaveMutex(void){
   991         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
          996  +  sqlite3_mutex_leave(unixBigLock);
   992    997   }
   993    998   #ifdef SQLITE_DEBUG
   994    999   static int unixMutexHeld(void) {
   995         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
         1000  +  return sqlite3_mutex_held(unixBigLock);
   996   1001   }
   997   1002   #endif
   998   1003   
   999   1004   
  1000   1005   #ifdef SQLITE_HAVE_OS_TRACE
  1001   1006   /*
  1002   1007   ** Helper function for printing out trace information from debugging
................................................................................
  4902   4907     assert( n==1 || lockType!=F_RDLCK );
  4903   4908   
  4904   4909     /* Locks are within range */
  4905   4910     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
  4906   4911   
  4907   4912     if( pShmNode->h>=0 ){
  4908   4913       /* Initialize the locking parameters */
  4909         -    memset(&f, 0, sizeof(f));
  4910   4914       f.l_type = lockType;
  4911   4915       f.l_whence = SEEK_SET;
  4912   4916       f.l_start = ofst;
  4913   4917       f.l_len = n;
  4914         -
  4915   4918       rc = osFcntl(pShmNode->h, F_SETLK, &f);
  4916   4919       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
  4917   4920     }
  4918   4921   
  4919   4922     /* Update the global lock state and do debug tracing */
  4920   4923   #ifdef SQLITE_DEBUG
  4921   4924     { u16 mask;
................................................................................
  7017   7020     ** the same instant might all reset the PRNG.  But multiple resets
  7018   7021     ** are harmless.
  7019   7022     */
  7020   7023     if( randomnessPid!=osGetpid(0) ){
  7021   7024       randomnessPid = osGetpid(0);
  7022   7025       sqlite3_randomness(0,0);
  7023   7026     }
  7024         -
  7025   7027     memset(p, 0, sizeof(unixFile));
  7026   7028   
  7027   7029     if( eType==SQLITE_OPEN_MAIN_DB ){
  7028   7030       UnixUnusedFd *pUnused;
  7029   7031       pUnused = findReusableFd(zName, flags);
  7030   7032       if( pUnused ){
  7031   7033         fd = pUnused->fd;
................................................................................
  8969   8971     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  8970   8972     assert( ArraySize(aSyscall)==29 );
  8971   8973   
  8972   8974     /* Register all VFSes defined in the aVfs[] array */
  8973   8975     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  8974   8976       sqlite3_vfs_register(&aVfs[i], i==0);
  8975   8977     }
         8978  +  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
  8976   8979     return SQLITE_OK; 
  8977   8980   }
  8978   8981   
  8979   8982   /*
  8980   8983   ** Shutdown the operating system interface.
  8981   8984   **
  8982   8985   ** Some operating systems might need to do some cleanup in this routine,
  8983   8986   ** to release dynamically allocated objects.  But not on unix.
  8984   8987   ** This routine is a no-op for unix.
  8985   8988   */
  8986   8989   int sqlite3_os_end(void){ 
         8990  +  unixBigLock = 0;
  8987   8991     return SQLITE_OK; 
  8988   8992   }
  8989   8993    
  8990   8994   #endif /* SQLITE_OS_UNIX */

Changes to src/os_win.c.

  3636   3636   ** is held when required. This function is only used as part of assert()
  3637   3637   ** statements. e.g.
  3638   3638   **
  3639   3639   **   winShmEnterMutex()
  3640   3640   **     assert( winShmMutexHeld() );
  3641   3641   **   winShmLeaveMutex()
  3642   3642   */
         3643  +static sqlite3_mutex *winBigLock = 0;
  3643   3644   static void winShmEnterMutex(void){
  3644         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
         3645  +  sqlite3_mutex_enter(winBigLock);
  3645   3646   }
  3646   3647   static void winShmLeaveMutex(void){
  3647         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
         3648  +  sqlite3_mutex_leave(winBigLock);
  3648   3649   }
  3649   3650   #ifndef NDEBUG
  3650   3651   static int winShmMutexHeld(void) {
  3651         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
         3652  +  return sqlite3_mutex_held(winBigLock);
  3652   3653   }
  3653   3654   #endif
  3654   3655   
  3655   3656   /*
  3656   3657   ** Object used to represent a single file opened and mmapped to provide
  3657   3658   ** shared memory.  When multiple threads all reference the same
  3658   3659   ** log-summary, each thread has its own winFile object, but they all
................................................................................
  6066   6067   #endif
  6067   6068   
  6068   6069     sqlite3_vfs_register(&winNolockVfs, 0);
  6069   6070   
  6070   6071   #if defined(SQLITE_WIN32_HAS_WIDE)
  6071   6072     sqlite3_vfs_register(&winLongPathNolockVfs, 0);
  6072   6073   #endif
         6074  +
         6075  +#ifndef SQLITE_OMIT_WAL
         6076  +  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
         6077  +#endif
  6073   6078   
  6074   6079     return SQLITE_OK;
  6075   6080   }
  6076   6081   
  6077   6082   int sqlite3_os_end(void){
  6078   6083   #if SQLITE_OS_WINRT
  6079   6084     if( sleepObj!=NULL ){
  6080   6085       osCloseHandle(sleepObj);
  6081   6086       sleepObj = NULL;
  6082   6087     }
  6083   6088   #endif
         6089  +
         6090  +#ifndef SQLITE_OMIT_WAL
         6091  +  winBigLock = 0;
         6092  +#endif
         6093  +
  6084   6094     return SQLITE_OK;
  6085   6095   }
  6086   6096   
  6087   6097   #endif /* SQLITE_OS_WIN */

Changes to src/pager.c.

   695    695     int pageSize;               /* Number of bytes in a page */
   696    696     Pgno mxPgno;                /* Maximum allowed size of the database */
   697    697     i64 journalSizeLimit;       /* Size limit for persistent journal files */
   698    698     char *zFilename;            /* Name of the database file */
   699    699     char *zJournal;             /* Name of the journal file */
   700    700     int (*xBusyHandler)(void*); /* Function to call when busy */
   701    701     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
   702         -  int aStat[3];               /* Total cache hits, misses and writes */
          702  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
   703    703   #ifdef SQLITE_TEST
   704    704     int nRead;                  /* Database pages read */
   705    705   #endif
   706    706     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
   707    707     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
   708    708   #ifdef SQLITE_HAS_CODEC
   709    709     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
   723    723   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
   724    724   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
   725    725   ** or CACHE_WRITE to sqlite3_db_status().
   726    726   */
   727    727   #define PAGER_STAT_HIT   0
   728    728   #define PAGER_STAT_MISS  1
   729    729   #define PAGER_STAT_WRITE 2
          730  +#define PAGER_STAT_SPILL 3
   730    731   
   731    732   /*
   732    733   ** The following global variables hold counters used for
   733    734   ** testing purposes only.  These variables do not exist in
   734    735   ** a non-testing build.  These variables are not thread-safe.
   735    736   */
   736    737   #ifdef SQLITE_TEST
................................................................................
  1209   1210     assert( isOpen(pPager->fd) );
  1210   1211     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1211   1212   #else
  1212   1213     UNUSED_PARAMETER(pPager);
  1213   1214   #endif
  1214   1215   
  1215   1216   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
  1216         -  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
         1217  +  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
  1217   1218       return -1;
  1218   1219     }
  1219   1220   #endif
  1220   1221   
  1221   1222   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1222   1223     {
  1223   1224       int nSector = pPager->sectorSize;
................................................................................
  4110   4111     PgHdr *pNext;
  4111   4112     for(p=pPager->pMmapFreelist; p; p=pNext){
  4112   4113       pNext = p->pDirty;
  4113   4114       sqlite3_free(p);
  4114   4115     }
  4115   4116   }
  4116   4117   
         4118  +/* Verify that the database file has not be deleted or renamed out from
         4119  +** under the pager.  Return SQLITE_OK if the database is still where it ought
         4120  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
         4121  +** code from sqlite3OsAccess()) if the database has gone missing.
         4122  +*/
         4123  +static int databaseIsUnmoved(Pager *pPager){
         4124  +  int bHasMoved = 0;
         4125  +  int rc;
         4126  +
         4127  +  if( pPager->tempFile ) return SQLITE_OK;
         4128  +  if( pPager->dbSize==0 ) return SQLITE_OK;
         4129  +  assert( pPager->zFilename && pPager->zFilename[0] );
         4130  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
         4131  +  if( rc==SQLITE_NOTFOUND ){
         4132  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
         4133  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
         4134  +    ** version 3.8.3, it never checked */
         4135  +    rc = SQLITE_OK;
         4136  +  }else if( rc==SQLITE_OK && bHasMoved ){
         4137  +    rc = SQLITE_READONLY_DBMOVED;
         4138  +  }
         4139  +  return rc;
         4140  +}
         4141  +
  4117   4142   
  4118   4143   /*
  4119   4144   ** Shutdown the page cache.  Free all memory and close all files.
  4120   4145   **
  4121   4146   ** If a transaction was in progress when this routine is called, that
  4122   4147   ** transaction is rolled back.  All outstanding pages are invalidated
  4123   4148   ** and their memory is freed.  Any attempt to use a page associated
................................................................................
  4126   4151   **
  4127   4152   ** This function always succeeds. If a transaction is active an attempt
  4128   4153   ** is made to roll it back. If an error occurs during the rollback 
  4129   4154   ** a hot journal may be left in the filesystem but no error is returned
  4130   4155   ** to the caller.
  4131   4156   */
  4132   4157   int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
  4133         -  u8 *pTmp = (u8 *)pPager->pTmpSpace;
  4134         -
         4158  +  u8 *pTmp = (u8*)pPager->pTmpSpace;
  4135   4159     assert( db || pagerUseWal(pPager)==0 );
  4136   4160     assert( assert_pager_state(pPager) );
  4137   4161     disable_simulated_io_errors();
  4138   4162     sqlite3BeginBenignMalloc();
  4139   4163     pagerFreeMapHdrs(pPager);
  4140   4164     /* pPager->errCode = 0; */
  4141   4165     pPager->exclusiveMode = 0;
  4142   4166   #ifndef SQLITE_OMIT_WAL
  4143         -  assert( db || pPager->pWal==0 );
  4144         -  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
  4145         -      (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
  4146         -  );
  4147         -  pPager->pWal = 0;
         4167  +  {
         4168  +    u8 *a = 0;
         4169  +    assert( db || pPager->pWal==0 );
         4170  +    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
         4171  +     && SQLITE_OK==databaseIsUnmoved(pPager)
         4172  +    ){
         4173  +      a = pTmp;
         4174  +    }
         4175  +    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
         4176  +    pPager->pWal = 0;
         4177  +  }
  4148   4178   #endif
  4149   4179     pager_reset(pPager);
  4150   4180     if( MEMDB ){
  4151   4181       pager_unlock(pPager);
  4152   4182     }else{
  4153   4183       /* If it is open, sync the journal file before calling UnlockAndRollback.
  4154   4184       ** If this is not done, then an unsynced portion of the open journal 
................................................................................
  4597   4627     if( pPager->doNotSpill
  4598   4628      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
  4599   4629         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
  4600   4630     ){
  4601   4631       return SQLITE_OK;
  4602   4632     }
  4603   4633   
         4634  +  pPager->aStat[PAGER_STAT_SPILL]++;
  4604   4635     pPg->pDirty = 0;
  4605   4636     if( pagerUseWal(pPager) ){
  4606   4637       /* Write a single frame for this page to the log. */
  4607   4638       rc = subjournalPageIfRequired(pPg); 
  4608   4639       if( rc==SQLITE_OK ){
  4609   4640         rc = pagerWalFrames(pPager, pPg, 0, 0);
  4610   4641       }
................................................................................
  4702   4733     void (*xReinit)(DbPage*) /* Function to reinitialize pages */
  4703   4734   ){
  4704   4735     u8 *pPtr;
  4705   4736     Pager *pPager = 0;       /* Pager object to allocate and return */
  4706   4737     int rc = SQLITE_OK;      /* Return code */
  4707   4738     int tempFile = 0;        /* True for temp files (incl. in-memory files) */
  4708   4739     int memDb = 0;           /* True if this is an in-memory file */
         4740  +#ifdef SQLITE_ENABLE_DESERIALIZE
         4741  +  int memJM = 0;           /* Memory journal mode */
         4742  +#else
         4743  +# define memJM 0
         4744  +#endif
  4709   4745     int readOnly = 0;        /* True if this is a read-only file */
  4710   4746     int journalFileSize;     /* Bytes to allocate for each journal fd */
  4711   4747     char *zPathname = 0;     /* Full path to database file */
  4712   4748     int nPathname = 0;       /* Number of bytes in zPathname */
  4713   4749     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
  4714   4750     int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
  4715   4751     u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
................................................................................
  4829   4865   
  4830   4866     /* Open the pager file.
  4831   4867     */
  4832   4868     if( zFilename && zFilename[0] ){
  4833   4869       int fout = 0;                    /* VFS flags returned by xOpen() */
  4834   4870       rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
  4835   4871       assert( !memDb );
  4836         -    readOnly = (fout&SQLITE_OPEN_READONLY);
         4872  +#ifdef SQLITE_ENABLE_DESERIALIZE
         4873  +    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
         4874  +#endif
         4875  +    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
  4837   4876   
  4838   4877       /* If the file was successfully opened for read/write access,
  4839   4878       ** choose a default page size in case we have to create the
  4840   4879       ** database file. The default page size is the maximum of:
  4841   4880       **
  4842   4881       **    + SQLITE_DEFAULT_PAGE_SIZE,
  4843   4882       **    + The value returned by sqlite3OsSectorSize()
................................................................................
  4960   4999     /* pPager->pLast = 0; */
  4961   5000     pPager->nExtra = (u16)nExtra;
  4962   5001     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
  4963   5002     assert( isOpen(pPager->fd) || tempFile );
  4964   5003     setSectorSize(pPager);
  4965   5004     if( !useJournal ){
  4966   5005       pPager->journalMode = PAGER_JOURNALMODE_OFF;
  4967         -  }else if( memDb ){
         5006  +  }else if( memDb || memJM ){
  4968   5007       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
  4969   5008     }
  4970   5009     /* pPager->xBusyHandler = 0; */
  4971   5010     /* pPager->pBusyHandlerArg = 0; */
  4972   5011     pPager->xReiniter = xReinit;
  4973   5012     setGetterMethod(pPager);
  4974   5013     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
................................................................................
  4975   5014     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
  4976   5015   
  4977   5016     *ppPager = pPager;
  4978   5017     return SQLITE_OK;
  4979   5018   }
  4980   5019   
  4981   5020   
  4982         -/* Verify that the database file has not be deleted or renamed out from
  4983         -** under the pager.  Return SQLITE_OK if the database is still were it ought
  4984         -** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
  4985         -** code from sqlite3OsAccess()) if the database has gone missing.
  4986         -*/
  4987         -static int databaseIsUnmoved(Pager *pPager){
  4988         -  int bHasMoved = 0;
  4989         -  int rc;
  4990         -
  4991         -  if( pPager->tempFile ) return SQLITE_OK;
  4992         -  if( pPager->dbSize==0 ) return SQLITE_OK;
  4993         -  assert( pPager->zFilename && pPager->zFilename[0] );
  4994         -  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
  4995         -  if( rc==SQLITE_NOTFOUND ){
  4996         -    /* If the HAS_MOVED file-control is unimplemented, assume that the file
  4997         -    ** has not been moved.  That is the historical behavior of SQLite: prior to
  4998         -    ** version 3.8.3, it never checked */
  4999         -    rc = SQLITE_OK;
  5000         -  }else if( rc==SQLITE_OK && bHasMoved ){
  5001         -    rc = SQLITE_READONLY_DBMOVED;
  5002         -  }
  5003         -  return rc;
  5004         -}
  5005         -
  5006   5021   
  5007   5022   /*
  5008   5023   ** This function is called after transitioning from PAGER_UNLOCK to
  5009   5024   ** PAGER_SHARED state. It tests if there is a hot journal present in
  5010   5025   ** the file-system for the given pager. A hot journal is one that 
  5011   5026   ** needs to be played back. According to this function, a hot-journal
  5012   5027   ** file exists if the following criteria are met:
................................................................................
  6518   6533           rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
  6519   6534           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  6520   6535         }
  6521   6536         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
  6522   6537         if( bBatch ){
  6523   6538           if( rc==SQLITE_OK ){
  6524   6539             rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
  6525         -        }else{
  6526         -          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
         6540  +        }
         6541  +        if( rc!=SQLITE_OK ){
         6542  +          sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
  6527   6543           }
  6528   6544         }
  6529   6545   
  6530   6546         if( rc!=SQLITE_OK ){
  6531   6547           assert( rc!=SQLITE_IOERR_BLOCKED );
  6532   6548           goto commit_phase_one_exit;
  6533   6549         }
................................................................................
  6743   6759     a[9] = pPager->nRead;
  6744   6760     a[10] = pPager->aStat[PAGER_STAT_WRITE];
  6745   6761     return a;
  6746   6762   }
  6747   6763   #endif
  6748   6764   
  6749   6765   /*
  6750         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
  6751         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
         6766  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
         6767  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
         6768  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
         6769  +** it was added later.
         6770  +**
         6771  +** Before returning, *pnVal is incremented by the
  6752   6772   ** current cache hit or miss count, according to the value of eStat. If the 
  6753   6773   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
  6754   6774   ** returning.
  6755   6775   */
  6756   6776   void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
  6757   6777   
  6758   6778     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
  6759   6779          || eStat==SQLITE_DBSTATUS_CACHE_MISS
  6760   6780          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
         6781  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
  6761   6782     );
  6762   6783   
  6763   6784     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
  6764   6785     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
  6765         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
         6786  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
         6787  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
  6766   6788   
  6767         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
         6789  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
         6790  +  *pnVal += pPager->aStat[eStat];
  6768   6791     if( reset ){
  6769         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
         6792  +    pPager->aStat[eStat] = 0;
  6770   6793     }
  6771   6794   }
  6772   6795   
  6773   6796   /*
  6774   6797   ** Return true if this is an in-memory or temp-file backed pager.
  6775   6798   */
  6776   6799   int sqlite3PagerIsMemdb(Pager *pPager){

Changes to src/parse.y.

   309    309                               {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
   310    310   ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z).      {
   311    311     Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
   312    312     sqlite3AddDefaultValue(pParse,p,A.z,Z);
   313    313   }
   314    314   ccons ::= DEFAULT scanpt id(X).       {
   315    315     Expr *p = tokenExpr(pParse, TK_STRING, X);
          316  +  if( p ){
          317  +    sqlite3ExprIdToTrueFalse(p);
          318  +    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
          319  +  }
   316    320     sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
   317    321   }
   318    322   
   319    323   // In addition to the type name, we also care about the primary key and
   320    324   // UNIQUE constraints.
   321    325   //
   322    326   ccons ::= NULL onconf.

Changes to src/pcache.c.

   427    427       }
   428    428       if( pPg ){
   429    429         int rc;
   430    430   #ifdef SQLITE_LOG_CACHE_SPILL
   431    431         sqlite3_log(SQLITE_FULL, 
   432    432                     "spill page %d making room for %d - cache used: %d/%d",
   433    433                     pPg->pgno, pgno,
   434         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
          434  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
   435    435                   numberOfCachePages(pCache));
   436    436   #endif
   437    437         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
   438    438         rc = pCache->xStress(pCache->pStress, pPg);
   439    439         pcacheDump(pCache);
   440    440         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   441    441           return rc;

Changes to src/printf.c.

   202    202     double rounder;            /* Used for rounding floating point values */
   203    203     etByte flag_dp;            /* True if decimal point should be shown */
   204    204     etByte flag_rtz;           /* True if trailing zeros should be removed */
   205    205   #endif
   206    206     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
   207    207     char buf[etBUFSIZE];       /* Conversion buffer */
   208    208   
          209  +  /* pAccum never starts out with an empty buffer that was obtained from 
          210  +  ** malloc().  This precondition is required by the mprintf("%z...")
          211  +  ** optimization. */
          212  +  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
          213  +
   209    214     bufpt = 0;
   210    215     if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
   211    216       pArgList = va_arg(ap, PrintfArguments*);
   212    217       bArgList = 1;
   213    218     }else{
   214    219       bArgList = 0;
   215    220     }
................................................................................
   620    625           buf[0] = '%';
   621    626           bufpt = buf;
   622    627           length = 1;
   623    628           break;
   624    629         case etCHARX:
   625    630           if( bArgList ){
   626    631             bufpt = getTextArg(pArgList);
   627         -          c = bufpt ? bufpt[0] : 0;
          632  +          length = 1;
          633  +          if( bufpt ){
          634  +            buf[0] = c = *(bufpt++);
          635  +            if( (c&0xc0)==0xc0 ){
          636  +              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
          637  +                buf[length++] = *(bufpt++);
          638  +              }
          639  +            }
          640  +          }else{
          641  +            buf[0] = 0;
          642  +          }
   628    643           }else{
   629         -          c = va_arg(ap,int);
          644  +          unsigned int ch = va_arg(ap,unsigned int);
          645  +          if( ch<0x00080 ){
          646  +            buf[0] = ch & 0xff;
          647  +            length = 1;
          648  +          }else if( ch<0x00800 ){
          649  +            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
          650  +            buf[1] = 0x80 + (u8)(ch & 0x3f);
          651  +            length = 2;
          652  +          }else if( ch<0x10000 ){
          653  +            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
          654  +            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
          655  +            buf[2] = 0x80 + (u8)(ch & 0x3f);
          656  +            length = 3;
          657  +          }else{
          658  +            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
          659  +            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
          660  +            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
          661  +            buf[3] = 0x80 + (u8)(ch & 0x3f);
          662  +            length = 4;
          663  +          }
   630    664           }
   631    665           if( precision>1 ){
   632    666             width -= precision-1;
   633    667             if( width>1 && !flag_leftjustify ){
   634    668               sqlite3AppendChar(pAccum, width-1, ' ');
   635    669               width = 0;
   636    670             }
   637         -          sqlite3AppendChar(pAccum, precision-1, c);
          671  +          while( precision-- > 1 ){
          672  +            sqlite3StrAccumAppend(pAccum, buf, length);
          673  +          }
   638    674           }
   639         -        length = 1;
   640         -        buf[0] = c;
   641    675           bufpt = buf;
   642         -        break;
          676  +        flag_altform2 = 1;
          677  +        goto adjust_width_for_utf8;
   643    678         case etSTRING:
   644    679         case etDYNSTRING:
   645    680           if( bArgList ){
   646    681             bufpt = getTextArg(pArgList);
   647    682             xtype = etSTRING;
   648    683           }else{
   649    684             bufpt = va_arg(ap,char*);
   650    685           }
   651    686           if( bufpt==0 ){
   652    687             bufpt = "";
   653    688           }else if( xtype==etDYNSTRING ){
          689  +          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
          690  +            /* Special optimization for sqlite3_mprintf("%z..."):
          691  +            ** Extend an existing memory allocation rather than creating
          692  +            ** a new one. */
          693  +            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
          694  +            pAccum->zText = bufpt;
          695  +            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
          696  +            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
          697  +            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
          698  +            length = 0;
          699  +            break;
          700  +          }
   654    701             zExtra = bufpt;
   655    702           }
   656    703           if( precision>=0 ){
   657         -          for(length=0; length<precision && bufpt[length]; length++){}
          704  +          if( flag_altform2 ){
          705  +            /* Set length to the number of bytes needed in order to display
          706  +            ** precision characters */
          707  +            unsigned char *z = (unsigned char*)bufpt;
          708  +            while( precision-- > 0 && z[0] ){
          709  +              SQLITE_SKIP_UTF8(z);
          710  +            }
          711  +            length = (int)(z - (unsigned char*)bufpt);
          712  +          }else{
          713  +            for(length=0; length<precision && bufpt[length]; length++){}
          714  +          }
   658    715           }else{
   659    716             length = 0x7fffffff & (int)strlen(bufpt);
   660    717           }
          718  +      adjust_width_for_utf8:
          719  +        if( flag_altform2 && width>0 ){
          720  +          /* Adjust width to account for extra bytes in UTF-8 characters */
          721  +          int ii = length - 1;
          722  +          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
          723  +        }
   661    724           break;
   662         -      case etSQLESCAPE:           /* Escape ' characters */
   663         -      case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
   664         -      case etSQLESCAPE3: {        /* Escape " characters */
          725  +      case etSQLESCAPE:           /* %q: Escape ' characters */
          726  +      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
          727  +      case etSQLESCAPE3: {        /* %w: Escape " characters */
   665    728           int i, j, k, n, isnull;
   666    729           int needQuote;
   667    730           char ch;
   668    731           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
   669    732           char *escarg;
   670    733   
   671    734           if( bArgList ){
   672    735             escarg = getTextArg(pArgList);
   673    736           }else{
   674    737             escarg = va_arg(ap,char*);
   675    738           }
   676    739           isnull = escarg==0;
   677    740           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
          741  +        /* For %q, %Q, and %w, the precision is the number of byte (or
          742  +        ** characters if the ! flags is present) to use from the input.
          743  +        ** Because of the extra quoting characters inserted, the number
          744  +        ** of output characters may be larger than the precision.
          745  +        */
   678    746           k = precision;
   679    747           for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
   680    748             if( ch==q )  n++;
          749  +          if( flag_altform2 && (ch&0xc0)==0xc0 ){
          750  +            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
          751  +          }
   681    752           }
   682    753           needQuote = !isnull && xtype==etSQLESCAPE2;
   683    754           n += i + 3;
   684    755           if( n>etBUFSIZE ){
   685    756             bufpt = zExtra = sqlite3Malloc( n );
   686    757             if( bufpt==0 ){
   687    758               setStrAccumError(pAccum, STRACCUM_NOMEM);
................................................................................
   696    767           for(i=0; i<k; i++){
   697    768             bufpt[j++] = ch = escarg[i];
   698    769             if( ch==q ) bufpt[j++] = ch;
   699    770           }
   700    771           if( needQuote ) bufpt[j++] = q;
   701    772           bufpt[j] = 0;
   702    773           length = j;
   703         -        /* The precision in %q and %Q means how many input characters to
   704         -        ** consume, not the length of the output...
   705         -        ** if( precision>=0 && precision<length ) length = precision; */
   706         -        break;
          774  +        goto adjust_width_for_utf8;
   707    775         }
   708    776         case etTOKEN: {
   709    777           Token *pToken;
   710    778           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
   711    779           pToken = va_arg(ap, Token*);
   712    780           assert( bArgList==0 );
   713    781           if( pToken && pToken->n ){
................................................................................
   738    806           assert( xtype==etINVALID );
   739    807           return;
   740    808         }
   741    809       }/* End switch over the format type */
   742    810       /*
   743    811       ** The text of the conversion is pointed to by "bufpt" and is
   744    812       ** "length" characters long.  The field width is "width".  Do
   745         -    ** the output.
          813  +    ** the output.  Both length and width are in bytes, not characters,
          814  +    ** at this point.  If the "!" flag was present on string conversions
          815  +    ** indicating that width and precision should be expressed in characters,
          816  +    ** then the values have been translated prior to reaching this point.
   746    817       */
   747    818       width -= length;
   748    819       if( width>0 ){
   749    820         if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   750    821         sqlite3StrAccumAppend(pAccum, bufpt, length);
   751    822         if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   752    823       }else{

Changes to src/resolve.c.

   427    427     ** Z is a string literal if it doesn't match any column names.  In that
   428    428     ** case, we need to return right away and not make any changes to
   429    429     ** pExpr.
   430    430     **
   431    431     ** Because no reference was made to outer contexts, the pNC->nRef
   432    432     ** fields are not changed in any context.
   433    433     */
   434         -  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
   435         -    pExpr->op = TK_STRING;
   436         -    pExpr->pTab = 0;
   437         -    return WRC_Prune;
          434  +  if( cnt==0 && zTab==0 ){
          435  +    assert( pExpr->op==TK_ID );
          436  +    if( ExprHasProperty(pExpr,EP_DblQuoted) ){
          437  +      pExpr->op = TK_STRING;
          438  +      pExpr->pTab = 0;
          439  +      return WRC_Prune;
          440  +    }
          441  +    if( sqlite3ExprIdToTrueFalse(pExpr) ){
          442  +      return WRC_Prune;
          443  +    }
   438    444     }
   439    445   
   440    446     /*
   441    447     ** cnt==0 means there was not match.  cnt>1 means there were two or
   442    448     ** more matches.  Either way, we have an error.
   443    449     */
   444    450     if( cnt!=1 ){
................................................................................
   779    785         }
   780    786         break;
   781    787       }
   782    788       case TK_VARIABLE: {
   783    789         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
   784    790         break;
   785    791       }
          792  +    case TK_IS:
          793  +    case TK_ISNOT: {
          794  +      Expr *pRight;
          795  +      assert( !ExprHasProperty(pExpr, EP_Reduced) );
          796  +      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
          797  +      ** and "x IS NOT FALSE". */
          798  +      if( (pRight = pExpr->pRight)->op==TK_ID ){
          799  +        int rc = resolveExprStep(pWalker, pRight);
          800  +        if( rc==WRC_Abort ) return WRC_Abort;
          801  +        if( pRight->op==TK_TRUEFALSE ){
          802  +          pExpr->op2 = pExpr->op;
          803  +          pExpr->op = TK_TRUTH;
          804  +          return WRC_Continue;
          805  +        }
          806  +      }
          807  +      /* Fall thru */
          808  +    }
   786    809       case TK_BETWEEN:
   787    810       case TK_EQ:
   788    811       case TK_NE:
   789    812       case TK_LT:
   790    813       case TK_LE:
   791    814       case TK_GT:
   792         -    case TK_GE:
   793         -    case TK_IS:
   794         -    case TK_ISNOT: {
          815  +    case TK_GE: {
   795    816         int nLeft, nRight;
   796    817         if( pParse->db->mallocFailed ) break;
   797    818         assert( pExpr->pLeft!=0 );
   798    819         nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
   799    820         if( pExpr->op==TK_BETWEEN ){
   800    821           nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
   801    822           if( nRight==nLeft ){

Changes to src/select.c.

  1262   1262       bSeq = 0;
  1263   1263     }else{
  1264   1264       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
  1265   1265       codeOffset(v, p->iOffset, addrContinue);
  1266   1266       iSortTab = iTab;
  1267   1267       bSeq = 1;
  1268   1268     }
  1269         -  for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
         1269  +  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
         1270  +    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
         1271  +  }
         1272  +  for(i=nSortData-1; i>=0; i--){
  1270   1273       int iRead;
  1271   1274       if( aOutEx[i].u.x.iOrderByCol ){
  1272   1275         iRead = aOutEx[i].u.x.iOrderByCol-1;
  1273   1276       }else{
  1274         -      iRead = iCol++;
         1277  +      iRead = iCol--;
  1275   1278       }
  1276   1279       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
  1277   1280       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
  1278   1281     }
  1279   1282     switch( eDest ){
  1280   1283       case SRT_Table:
  1281   1284       case SRT_EphemTab: {

Changes to src/shell.c.in.

  1065   1065   
  1066   1066   /* Allowed values for ShellState.openMode
  1067   1067   */
  1068   1068   #define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
  1069   1069   #define SHELL_OPEN_NORMAL     1      /* Normal database file */
  1070   1070   #define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
  1071   1071   #define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
         1072  +#define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
  1072   1073   
  1073   1074   /*
  1074   1075   ** These are the allowed shellFlgs values
  1075   1076   */
  1076   1077   #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
  1077   1078   #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
  1078   1079   #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
................................................................................
  1171   1172   **     (4) Delete the temporary file
  1172   1173   **
  1173   1174   ** If the EDITOR argument is omitted, use the value in the VISUAL
  1174   1175   ** environment variable.  If still there is no EDITOR, through an error.
  1175   1176   **
  1176   1177   ** Also throw an error if the EDITOR program returns a non-zero exit code.
  1177   1178   */
         1179  +#ifndef SQLITE_NOHAVE_SYSTEM
  1178   1180   static void editFunc(
  1179   1181     sqlite3_context *context,
  1180   1182     int argc,
  1181   1183     sqlite3_value **argv
  1182   1184   ){
  1183   1185     const char *zEditor;
  1184   1186     char *zTempFile = 0;
................................................................................
  1268   1270     fclose(f);
  1269   1271     f = 0;
  1270   1272     if( x!=sz ){
  1271   1273       sqlite3_result_error(context, "could not read back the whole file", -1);
  1272   1274       goto edit_func_end;
  1273   1275     }
  1274   1276     if( bBin ){
  1275         -    sqlite3_result_blob(context, p, sz, sqlite3_free);
         1277  +    sqlite3_result_blob64(context, p, sz, sqlite3_free);
  1276   1278     }else{
  1277         -    sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
         1279  +    sqlite3_result_text64(context, (const char*)p, sz,
         1280  +                          sqlite3_free, SQLITE_UTF8);
  1278   1281     }
  1279   1282     p = 0;
  1280   1283   
  1281   1284   edit_func_end:
  1282   1285     if( f ) fclose(f);
  1283   1286     unlink(zTempFile);
  1284   1287     sqlite3_free(zTempFile);
  1285   1288     sqlite3_free(p);
  1286   1289   }
         1290  +#endif /* SQLITE_NOHAVE_SYSTEM */
  1287   1291   
  1288   1292   /*
  1289   1293   ** Save or restore the current output mode
  1290   1294   */
  1291   1295   static void outputModePush(ShellState *p){
  1292   1296     p->modePrior = p->mode;
  1293   1297     memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
................................................................................
  2265   2269   static int display_stats(
  2266   2270     sqlite3 *db,                /* Database to query */
  2267   2271     ShellState *pArg,           /* Pointer to ShellState */
  2268   2272     int bReset                  /* True to reset the stats */
  2269   2273   ){
  2270   2274     int iCur;
  2271   2275     int iHiwtr;
  2272         -
  2273         -  if( pArg && pArg->out ){
  2274         -    displayStatLine(pArg, "Memory Used:",
  2275         -       "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
  2276         -    displayStatLine(pArg, "Number of Outstanding Allocations:",
  2277         -       "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
  2278         -    if( pArg->shellFlgs & SHFLG_Pagecache ){
  2279         -      displayStatLine(pArg, "Number of Pcache Pages Used:",
  2280         -         "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
  2281         -    }
  2282         -    displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
  2283         -       "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
  2284         -    displayStatLine(pArg, "Largest Allocation:",
  2285         -       "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
  2286         -    displayStatLine(pArg, "Largest Pcache Allocation:",
  2287         -       "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
         2276  +  FILE *out;
         2277  +  if( pArg==0 || pArg->out==0 ) return 0;
         2278  +  out = pArg->out;
         2279  +
         2280  +  if( pArg->pStmt && (pArg->statsOn & 2) ){
         2281  +    int nCol, i, x;
         2282  +    sqlite3_stmt *pStmt = pArg->pStmt;
         2283  +    char z[100];
         2284  +    nCol = sqlite3_column_count(pStmt);
         2285  +    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
         2286  +    for(i=0; i<nCol; i++){
         2287  +      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
         2288  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
         2289  +      sqlite3_snprintf(30, z+x, "declared type:");
         2290  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
         2291  +  #ifdef SQLITE_ENABLE_COLUMN_METADATA
         2292  +      sqlite3_snprintf(30, z+x, "database name:");
         2293  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
         2294  +      sqlite3_snprintf(30, z+x, "table name:");
         2295  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
         2296  +      sqlite3_snprintf(30, z+x, "origin name:");
         2297  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
         2298  +  #endif
         2299  +    }
         2300  +    }
         2301  +
         2302  +  displayStatLine(pArg, "Memory Used:",
         2303  +     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
         2304  +  displayStatLine(pArg, "Number of Outstanding Allocations:",
         2305  +     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
         2306  +  if( pArg->shellFlgs & SHFLG_Pagecache ){
         2307  +    displayStatLine(pArg, "Number of Pcache Pages Used:",
         2308  +       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
         2309  +  }
         2310  +  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
         2311  +     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
         2312  +  displayStatLine(pArg, "Largest Allocation:",
         2313  +     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
         2314  +  displayStatLine(pArg, "Largest Pcache Allocation:",
         2315  +     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
  2288   2316   #ifdef YYTRACKMAXSTACKDEPTH
  2289         -    displayStatLine(pArg, "Deepest Parser Stack:",
  2290         -       "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
         2317  +  displayStatLine(pArg, "Deepest Parser Stack:",
         2318  +     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
  2291   2319   #endif
  2292         -  }
  2293   2320   
  2294         -  if( pArg && pArg->out && db ){
         2321  +  if( db ){
  2295   2322       if( pArg->shellFlgs & SHFLG_Lookaside ){
  2296   2323         iHiwtr = iCur = -1;
  2297   2324         sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
  2298   2325                           &iCur, &iHiwtr, bReset);
  2299   2326         raw_printf(pArg->out,
  2300   2327                 "Lookaside Slots Used:                %d (max %d)\n",
  2301   2328                 iCur, iHiwtr);
................................................................................
  2322   2349       iHiwtr = iCur = -1;
  2323   2350       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
  2324   2351       raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
  2325   2352       iHiwtr = iCur = -1;
  2326   2353       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
  2327   2354       raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
  2328   2355       iHiwtr = iCur = -1;
         2356  +    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
         2357  +    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
         2358  +    iHiwtr = iCur = -1;
  2329   2359       sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
  2330   2360       raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
  2331   2361               iCur);
  2332   2362       iHiwtr = iCur = -1;
  2333   2363       sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
  2334   2364       raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
  2335   2365               iCur);
  2336   2366     }
  2337   2367   
  2338         -  if( pArg && pArg->out && db && pArg->pStmt ){
         2368  +  if( pArg->pStmt ){
  2339   2369       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
  2340   2370                                  bReset);
  2341   2371       raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
  2342   2372       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
  2343   2373       raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
  2344   2374       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
  2345   2375       raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
  2346   2376       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
  2347   2377       raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
         2378  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
         2379  +    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
         2380  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
         2381  +    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
         2382  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
         2383  +    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
  2348   2384     }
  2349   2385   
  2350   2386   #ifdef __linux__
  2351   2387     displayLinuxIoStats(pArg->out);
  2352   2388   #endif
  2353   2389   
  2354   2390     /* Do not remove this machine readable comment: extra-stats-output-here */
................................................................................
  2556   2592   }
  2557   2593   
  2558   2594   /*
  2559   2595   ** Run a prepared statement
  2560   2596   */
  2561   2597   static void exec_prepared_stmt(
  2562   2598     ShellState *pArg,                                /* Pointer to ShellState */
  2563         -  sqlite3_stmt *pStmt,                             /* Statment to run */
  2564         -  int (*xCallback)(void*,int,char**,char**,int*)   /* Callback function */
         2599  +  sqlite3_stmt *pStmt                              /* Statment to run */
  2565   2600   ){
  2566   2601     int rc;
  2567   2602   
  2568   2603     /* perform the first step.  this will tell us if we
  2569   2604     ** have a result set or not and how wide it is.
  2570   2605     */
  2571   2606     rc = sqlite3_step(pStmt);
  2572   2607     /* if we have a result set... */
  2573   2608     if( SQLITE_ROW == rc ){
  2574         -    /* if we have a callback... */
  2575         -    if( xCallback ){
  2576         -      /* allocate space for col name ptr, value ptr, and type */
  2577         -      int nCol = sqlite3_column_count(pStmt);
  2578         -      void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
  2579         -      if( !pData ){
  2580         -        rc = SQLITE_NOMEM;
  2581         -      }else{
  2582         -        char **azCols = (char **)pData;      /* Names of result columns */
  2583         -        char **azVals = &azCols[nCol];       /* Results */
  2584         -        int *aiTypes = (int *)&azVals[nCol]; /* Result types */
  2585         -        int i, x;
  2586         -        assert(sizeof(int) <= sizeof(char *));
  2587         -        /* save off ptrs to column names */
         2609  +    /* allocate space for col name ptr, value ptr, and type */
         2610  +    int nCol = sqlite3_column_count(pStmt);
         2611  +    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
         2612  +    if( !pData ){
         2613  +      rc = SQLITE_NOMEM;
         2614  +    }else{
         2615  +      char **azCols = (char **)pData;      /* Names of result columns */
         2616  +      char **azVals = &azCols[nCol];       /* Results */
         2617  +      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
         2618  +      int i, x;
         2619  +      assert(sizeof(int) <= sizeof(char *));
         2620  +      /* save off ptrs to column names */
         2621  +      for(i=0; i<nCol; i++){
         2622  +        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
         2623  +      }
         2624  +      do{
         2625  +        /* extract the data and data types */
  2588   2626           for(i=0; i<nCol; i++){
  2589         -          azCols[i] = (char *)sqlite3_column_name(pStmt, i);
  2590         -        }
  2591         -        do{
  2592         -          /* extract the data and data types */
  2593         -          for(i=0; i<nCol; i++){
  2594         -            aiTypes[i] = x = sqlite3_column_type(pStmt, i);
  2595         -            if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
  2596         -              azVals[i] = "";
  2597         -            }else{
  2598         -              azVals[i] = (char*)sqlite3_column_text(pStmt, i);
  2599         -            }
  2600         -            if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
  2601         -              rc = SQLITE_NOMEM;
  2602         -              break; /* from for */
  2603         -            }
  2604         -          } /* end for */
  2605         -
  2606         -          /* if data and types extracted successfully... */
  2607         -          if( SQLITE_ROW == rc ){
  2608         -            /* call the supplied callback with the result row data */
  2609         -            if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
  2610         -              rc = SQLITE_ABORT;
  2611         -            }else{
  2612         -              rc = sqlite3_step(pStmt);
  2613         -            }
  2614         -          }
  2615         -        } while( SQLITE_ROW == rc );
  2616         -        sqlite3_free(pData);
  2617         -      }
  2618         -    }else{
  2619         -      do{
  2620         -        rc = sqlite3_step(pStmt);
  2621         -      } while( rc == SQLITE_ROW );
         2627  +          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
         2628  +          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
         2629  +            azVals[i] = "";
         2630  +          }else{
         2631  +            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
         2632  +          }
         2633  +          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
         2634  +            rc = SQLITE_NOMEM;
         2635  +            break; /* from for */
         2636  +          }
         2637  +        } /* end for */
         2638  +
         2639  +        /* if data and types extracted successfully... */
         2640  +        if( SQLITE_ROW == rc ){
         2641  +          /* call the supplied callback with the result row data */
         2642  +          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
         2643  +            rc = SQLITE_ABORT;
         2644  +          }else{
         2645  +            rc = sqlite3_step(pStmt);
         2646  +          }
         2647  +        }
         2648  +      } while( SQLITE_ROW == rc );
         2649  +      sqlite3_free(pData);
  2622   2650       }
  2623   2651     }
  2624   2652   }
  2625   2653   
  2626   2654   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2627   2655   /*
  2628   2656   ** This function is called to process SQL if the previous shell command
................................................................................
  2760   2788   ** set via the supplied callback.
  2761   2789   **
  2762   2790   ** This is very similar to SQLite's built-in sqlite3_exec()
  2763   2791   ** function except it takes a slightly different callback
  2764   2792   ** and callback data argument.
  2765   2793   */
  2766   2794   static int shell_exec(
  2767         -  sqlite3 *db,                              /* An open database */
  2768         -  const char *zSql,                         /* SQL to be evaluated */
  2769         -  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
  2770         -                                            /* (not the same as sqlite3_exec) */
  2771   2795     ShellState *pArg,                         /* Pointer to ShellState */
         2796  +  const char *zSql,                         /* SQL to be evaluated */
  2772   2797     char **pzErrMsg                           /* Error msg written here */
  2773   2798   ){
  2774   2799     sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  2775   2800     int rc = SQLITE_OK;             /* Return Code */
  2776   2801     int rc2;
  2777   2802     const char *zLeftover;          /* Tail of unprocessed SQL */
         2803  +  sqlite3 *db = pArg->db;
  2778   2804   
  2779   2805     if( pzErrMsg ){
  2780   2806       *pzErrMsg = NULL;
  2781   2807     }
  2782   2808   
  2783   2809   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2784   2810     if( pArg->expert.pExpert ){
................................................................................
  2841   2867           if( pArg->autoEQP>=AUTOEQP_full ){
  2842   2868             /* Also do an EXPLAIN for ".eqp full" mode */
  2843   2869             zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
  2844   2870             rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  2845   2871             if( rc==SQLITE_OK ){
  2846   2872               pArg->cMode = MODE_Explain;
  2847   2873               explain_data_prepare(pArg, pExplain);
  2848         -            exec_prepared_stmt(pArg, pExplain, xCallback);
         2874  +            exec_prepared_stmt(pArg, pExplain);
  2849   2875               explain_data_delete(pArg);
  2850   2876             }
  2851   2877             sqlite3_finalize(pExplain);
  2852   2878             sqlite3_free(zEQP);
  2853   2879           }
  2854   2880           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
  2855   2881           restore_debug_trace_modes();
................................................................................
  2867   2893           /* If the shell is currently in ".explain" mode, gather the extra
  2868   2894           ** data required to add indents to the output.*/
  2869   2895           if( pArg->cMode==MODE_Explain ){
  2870   2896             explain_data_prepare(pArg, pStmt);
  2871   2897           }
  2872   2898         }
  2873   2899   
  2874         -      exec_prepared_stmt(pArg, pStmt, xCallback);
         2900  +      exec_prepared_stmt(pArg, pStmt);
  2875   2901         explain_data_delete(pArg);
  2876   2902   
  2877   2903         /* print usage stats if stats on */
  2878   2904         if( pArg && pArg->statsOn ){
  2879   2905           display_stats(db, pArg, 0);
  2880   2906         }
  2881   2907   
................................................................................
  3130   3156       appendText(&sSelect, " FROM ", 0);
  3131   3157       appendText(&sSelect, zTable, quoteChar(zTable));
  3132   3158   
  3133   3159       savedDestTable = p->zDestTable;
  3134   3160       savedMode = p->mode;
  3135   3161       p->zDestTable = sTable.z;
  3136   3162       p->mode = p->cMode = MODE_Insert;
  3137         -    rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
         3163  +    rc = shell_exec(p, sSelect.z, 0);
  3138   3164       if( (rc&0xff)==SQLITE_CORRUPT ){
  3139   3165         raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
  3140   3166         toggleSelectOrder(p->db);
  3141         -      shell_exec(p->db, sSelect.z, shell_callback, p, 0);
         3167  +      shell_exec(p, sSelect.z, 0);
  3142   3168         toggleSelectOrder(p->db);
  3143   3169       }
  3144   3170       p->zDestTable = savedDestTable;
  3145   3171       p->mode = savedMode;
  3146   3172       freeText(&sTable);
  3147   3173       freeText(&sSelect);
  3148   3174       if( rc ) p->nErr++;
................................................................................
  3251   3277     "                         tcl      TCL list elements\n"
  3252   3278     ".nullvalue STRING      Use STRING in place of NULL values\n"
  3253   3279     ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
  3254   3280     "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
  3255   3281     "                         on the output.\n"
  3256   3282     ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
  3257   3283     "                         The --new option starts with an empty file\n"
         3284  +  "                         Other options: --readonly --append --zip\n"
  3258   3285     ".output ?FILE?         Send output to FILE or stdout\n"
  3259   3286     ".print STRING...       Print literal STRING\n"
  3260   3287     ".prompt MAIN CONTINUE  Replace the standard prompts\n"
  3261   3288     ".quit                  Exit this program\n"
  3262   3289     ".read FILENAME         Execute SQL in FILENAME\n"
  3263   3290     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
  3264   3291     ".save FILE             Write in-memory database into FILE\n"
................................................................................
  3268   3295     ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
  3269   3296     ".separator COL ?ROW?   Change the column separator and optionally the row\n"
  3270   3297     "                         separator for both the output mode and .import\n"
  3271   3298   #if defined(SQLITE_ENABLE_SESSION)
  3272   3299     ".session CMD ...       Create or control sessions\n"
  3273   3300   #endif
  3274   3301     ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
         3302  +#ifndef SQLITE_NOHAVE_SYSTEM
  3275   3303     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
         3304  +#endif
  3276   3305     ".show                  Show the current values for various settings\n"
  3277   3306     ".stats ?on|off?        Show stats or turn stats on or off\n"
         3307  +#ifndef SQLITE_NOHAVE_SYSTEM
  3278   3308     ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
         3309  +#endif
  3279   3310     ".tables ?TABLE?        List names of tables\n"
  3280   3311     "                         If TABLE specified, only list tables matching\n"
  3281   3312     "                         LIKE pattern TABLE.\n"
  3282   3313     ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
  3283   3314     ".timeout MS            Try opening locked tables for MS milliseconds\n"
  3284   3315     ".timer on|off          Turn SQL timer on or off\n"
  3285   3316     ".trace FILE|off        Output each SQL statement as it is run\n"
................................................................................
  3400   3431     return 1;
  3401   3432   }
  3402   3433   #endif
  3403   3434   
  3404   3435   /*
  3405   3436   ** Try to deduce the type of file for zName based on its content.  Return
  3406   3437   ** one of the SHELL_OPEN_* constants.
         3438  +**
         3439  +** If the file does not exist or is empty but its name looks like a ZIP
         3440  +** archive and the dfltZip flag is true, then assume it is a ZIP archive.
         3441  +** Otherwise, assume an ordinary database regardless of the filename if
         3442  +** the type cannot be determined from content.
  3407   3443   */
  3408         -static int deduceDatabaseType(const char *zName){
         3444  +static int deduceDatabaseType(const char *zName, int dfltZip){
  3409   3445     FILE *f = fopen(zName, "rb");
  3410   3446     size_t n;
  3411   3447     int rc = SHELL_OPEN_UNSPEC;
  3412   3448     char zBuf[100];
  3413         -  if( f==0 ) return SHELL_OPEN_NORMAL;
         3449  +  if( f==0 ){
         3450  +    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
         3451  +    return SHELL_OPEN_NORMAL;
         3452  +  }
  3414   3453     fseek(f, -25, SEEK_END);
  3415   3454     n = fread(zBuf, 25, 1, f);
  3416   3455     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
  3417   3456       rc = SHELL_OPEN_APPENDVFS;
  3418   3457     }else{
  3419   3458       fseek(f, -22, SEEK_END);
  3420   3459       n = fread(zBuf, 22, 1, f);
  3421   3460       if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
  3422   3461          && zBuf[3]==0x06 ){
  3423   3462         rc = SHELL_OPEN_ZIPFILE;
         3463  +    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
         3464  +      return SHELL_OPEN_ZIPFILE;
  3424   3465       }
  3425   3466     }
  3426   3467     fclose(f);
  3427   3468     return rc;  
  3428   3469   }
  3429   3470   
  3430   3471   /*
................................................................................
  3431   3472   ** Make sure the database is open.  If it is not, then open it.  If
  3432   3473   ** the database fails to open, print an error message and exit.
  3433   3474   */
  3434   3475   static void open_db(ShellState *p, int keepAlive){
  3435   3476     if( p->db==0 ){
  3436   3477       sqlite3_initialize();
  3437   3478       if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
  3438         -      p->openMode = deduceDatabaseType(p->zDbFilename);
         3479  +      p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
  3439   3480       }
  3440   3481       switch( p->openMode ){
  3441   3482         case SHELL_OPEN_APPENDVFS: {
  3442   3483           sqlite3_open_v2(p->zDbFilename, &p->db, 
  3443   3484              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
  3444   3485           break;
  3445   3486         }
  3446   3487         case SHELL_OPEN_ZIPFILE: {
  3447   3488           sqlite3_open(":memory:", &p->db);
  3448   3489           break;
         3490  +      }
         3491  +      case SHELL_OPEN_READONLY: {
         3492  +        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
         3493  +        break;
  3449   3494         }
  3450   3495         case SHELL_OPEN_UNSPEC:
  3451   3496         case SHELL_OPEN_NORMAL: {
  3452   3497           sqlite3_open(p->zDbFilename, &p->db);
  3453   3498           break;
  3454   3499         }
  3455   3500       }
................................................................................
  3472   3517   #endif
  3473   3518       sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
  3474   3519                               shellAddSchemaName, 0, 0);
  3475   3520       sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
  3476   3521                               shellModuleSchema, 0, 0);
  3477   3522       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
  3478   3523                               shellPutsFunc, 0, 0);
         3524  +#ifndef SQLITE_NOHAVE_SYSTEM
  3479   3525       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
  3480   3526                               editFunc, 0, 0);
  3481   3527       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
  3482   3528                               editFunc, 0, 0);
         3529  +#endif
  3483   3530       if( p->openMode==SHELL_OPEN_ZIPFILE ){
  3484   3531         char *zSql = sqlite3_mprintf(
  3485   3532            "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
  3486   3533         sqlite3_exec(p->db, zSql, 0, 0, 0);
  3487   3534         sqlite3_free(zSql);
  3488   3535       }
  3489   3536     }
................................................................................
  4090   4137   static void output_reset(ShellState *p){
  4091   4138     if( p->outfile[0]=='|' ){
  4092   4139   #ifndef SQLITE_OMIT_POPEN
  4093   4140       pclose(p->out);
  4094   4141   #endif
  4095   4142     }else{
  4096   4143       output_file_close(p->out);
         4144  +#ifndef SQLITE_NOHAVE_SYSTEM
  4097   4145       if( p->doXdgOpen ){
  4098   4146         const char *zXdgOpenCmd =
  4099   4147   #if defined(_WIN32)
  4100   4148         "start";
  4101   4149   #elif defined(__APPLE__)
  4102   4150         "open";
  4103   4151   #else
................................................................................
  4108   4156         if( system(zCmd) ){
  4109   4157           utf8_printf(stderr, "Failed: [%s]\n", zCmd);
  4110   4158         }
  4111   4159         sqlite3_free(zCmd);
  4112   4160         outputModePop(p);
  4113   4161         p->doXdgOpen = 0;
  4114   4162       }
         4163  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
  4115   4164     }
  4116   4165     p->outfile[0] = 0;
  4117   4166     p->out = stdout;
  4118   4167   }
  4119   4168   
  4120   4169   /*
  4121   4170   ** Run an SQL command and return the single integer result.
................................................................................
  5218   5267         "  name TEXT PRIMARY KEY,  -- name of the file\n"
  5219   5268         "  mode INT,               -- access permissions\n"
  5220   5269         "  mtime INT,              -- last modification time\n"
  5221   5270         "  sz INT,                 -- original file size\n"
  5222   5271         "  data BLOB               -- compressed content\n"
  5223   5272         ")";
  5224   5273     const char *zDrop = "DROP TABLE IF EXISTS sqlar";
  5225         -  const char *zInsertFmt = 
  5226         -     "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
         5274  +  const char *zInsertFmt[2] = {
         5275  +     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
  5227   5276        "  SELECT\n"
  5228   5277        "    %s,\n"
  5229   5278        "    mode,\n"
  5230   5279        "    mtime,\n"
  5231   5280        "    CASE substr(lsmode(mode),1,1)\n"
  5232   5281        "      WHEN '-' THEN length(data)\n"
  5233   5282        "      WHEN 'd' THEN 0\n"
  5234   5283        "      ELSE -1 END,\n"
  5235         -     "    CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
         5284  +     "    sqlar_compress(data)\n"
         5285  +     "  FROM fsdir(%Q,%Q)\n"
         5286  +     "  WHERE lsmode(mode) NOT LIKE '?%%';",
         5287  +     "REPLACE INTO %s(name,mode,mtime,data)\n"
         5288  +     "  SELECT\n"
         5289  +     "    %s,\n"
         5290  +     "    mode,\n"
         5291  +     "    mtime,\n"
         5292  +     "    data\n"
  5236   5293        "  FROM fsdir(%Q,%Q)\n"
  5237         -     "  WHERE lsmode(mode) NOT LIKE '?%%';";
         5294  +     "  WHERE lsmode(mode) NOT LIKE '?%%';"
         5295  +  };
  5238   5296     int i;                          /* For iterating through azFile[] */
  5239   5297     int rc;                         /* Return code */
         5298  +  const char *zTab = 0;           /* SQL table into which to insert */
         5299  +  char *zSql;
         5300  +  char zTemp[50];
  5240   5301   
         5302  +  arExecSql(pAr, "PRAGMA page_size=512");
  5241   5303     rc = arExecSql(pAr, "SAVEPOINT ar;");
  5242   5304     if( rc!=SQLITE_OK ) return rc;
  5243         -  if( bUpdate==0 ){
  5244         -    rc = arExecSql(pAr, zDrop);
  5245         -    if( rc!=SQLITE_OK ) return rc;
         5305  +  zTemp[0] = 0; 
         5306  +  if( pAr->bZip ){
         5307  +    /* Initialize the zipfile virtual table, if necessary */
         5308  +    if( pAr->zFile ){
         5309  +      sqlite3_uint64 r;
         5310  +      sqlite3_randomness(sizeof(r),&r);
         5311  +      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
         5312  +      zTab = zTemp;
         5313  +      zSql = sqlite3_mprintf(
         5314  +         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
         5315  +         zTab, pAr->zFile
         5316  +      );
         5317  +      rc = arExecSql(pAr, zSql);
         5318  +      sqlite3_free(zSql);
         5319  +    }else{
         5320  +      zTab = "zip";
         5321  +    }
         5322  +  }else{
         5323  +    /* Initialize the table for an SQLAR */
         5324  +    zTab = "sqlar";
         5325  +    if( bUpdate==0 ){
         5326  +      rc = arExecSql(pAr, zDrop);
         5327  +      if( rc!=SQLITE_OK ) goto end_ar_transaction;
         5328  +    }
         5329  +    rc = arExecSql(pAr, zCreate);
  5246   5330     }
  5247         -  rc = arExecSql(pAr, zCreate);
  5248   5331     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
  5249         -    char *zSql = sqlite3_mprintf(zInsertFmt,
         5332  +    char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
  5250   5333           pAr->bVerbose ? "shell_putsnl(name)" : "name",
  5251   5334           pAr->azArg[i], pAr->zDir);
  5252   5335       rc = arExecSql(pAr, zSql);
  5253   5336       sqlite3_free(zSql);
  5254   5337     }
         5338  +end_ar_transaction:
  5255   5339     if( rc!=SQLITE_OK ){
  5256   5340       arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
  5257   5341     }else{
  5258   5342       rc = arExecSql(pAr, "RELEASE ar;");
         5343  +    if( pAr->bZip && pAr->zFile ){
         5344  +      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
         5345  +      arExecSql(pAr, zSql);
         5346  +      sqlite3_free(zSql);
         5347  +    }
  5259   5348     }
  5260   5349     return rc;
  5261   5350   }
  5262   5351   
  5263   5352   /*
  5264   5353   ** Implementation of ".ar" dot command.
  5265   5354   */
................................................................................
  5273   5362     memset(&cmd, 0, sizeof(cmd));
  5274   5363     rc = arParseCommand(azArg, nArg, &cmd);
  5275   5364     if( rc==SQLITE_OK ){
  5276   5365       int eDbType = SHELL_OPEN_UNSPEC;
  5277   5366       cmd.p = pState;
  5278   5367       cmd.db = pState->db;
  5279   5368       if( cmd.zFile ){
  5280         -      eDbType = deduceDatabaseType(cmd.zFile);
         5369  +      eDbType = deduceDatabaseType(cmd.zFile, 1);
  5281   5370       }else{
  5282   5371         eDbType = pState->openMode;
  5283   5372       }
  5284   5373       if( eDbType==SHELL_OPEN_ZIPFILE ){
  5285         -      if( cmd.zFile==0 ){
  5286         -        cmd.zSrcTable = sqlite3_mprintf("zip");
  5287         -      }else{
  5288         -        cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
  5289         -      }
  5290         -      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
  5291         -        utf8_printf(stderr, "zip archives are read-only\n");
  5292         -        rc = SQLITE_ERROR;
  5293         -        goto end_ar_command;
         5374  +      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
         5375  +        if( cmd.zFile==0 ){
         5376  +          cmd.zSrcTable = sqlite3_mprintf("zip");
         5377  +        }else{
         5378  +          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
         5379  +        }
  5294   5380         }
  5295   5381         cmd.bZip = 1;
  5296   5382       }else if( cmd.zFile ){
  5297   5383         int flags;
  5298   5384         if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
  5299   5385         if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
  5300   5386           flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
................................................................................
  5311   5397         if( rc!=SQLITE_OK ){
  5312   5398           utf8_printf(stderr, "cannot open file: %s (%s)\n", 
  5313   5399               cmd.zFile, sqlite3_errmsg(cmd.db)
  5314   5400           );
  5315   5401           goto end_ar_command;
  5316   5402         }
  5317   5403         sqlite3_fileio_init(cmd.db, 0, 0);
  5318         -#ifdef SQLITE_HAVE_ZLIB
  5319   5404         sqlite3_sqlar_init(cmd.db, 0, 0);
  5320         -#endif
  5321   5405         sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
  5322   5406                                 shellPutsFunc, 0, 0);
  5323   5407   
  5324   5408       }
  5325         -    if( cmd.zSrcTable==0 ){
         5409  +    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
  5326   5410         if( cmd.eCmd!=AR_CMD_CREATE
  5327   5411          && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
  5328   5412         ){
  5329   5413           utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
  5330   5414           rc = SQLITE_ERROR;
  5331   5415           goto end_ar_command;
  5332   5416         }
................................................................................
  5711   5795     if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
  5712   5796       if( nArg==2 ){
  5713   5797         if( strcmp(azArg[1],"full")==0 ){
  5714   5798           p->autoEQP = AUTOEQP_full;
  5715   5799         }else if( strcmp(azArg[1],"trigger")==0 ){
  5716   5800           p->autoEQP = AUTOEQP_trigger;
  5717   5801         }else{
  5718         -        p->autoEQP = booleanValue(azArg[1]);
         5802  +        p->autoEQP = (u8)booleanValue(azArg[1]);
  5719   5803         }
  5720   5804       }else{
  5721   5805         raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
  5722   5806         rc = 1;
  5723   5807       }
  5724   5808     }else
  5725   5809   
................................................................................
  5798   5882         raw_printf(p->out, "/* No STAT tables available */\n");
  5799   5883       }else{
  5800   5884         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  5801   5885         sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
  5802   5886                      callback, &data, &zErrMsg);
  5803   5887         data.cMode = data.mode = MODE_Insert;
  5804   5888         data.zDestTable = "sqlite_stat1";
  5805         -      shell_exec(p->db, "SELECT * FROM sqlite_stat1",
  5806         -                 shell_callback, &data,&zErrMsg);
         5889  +      shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
  5807   5890         data.zDestTable = "sqlite_stat3";
  5808         -      shell_exec(p->db, "SELECT * FROM sqlite_stat3",
  5809         -                 shell_callback, &data,&zErrMsg);
         5891  +      shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
  5810   5892         data.zDestTable = "sqlite_stat4";
  5811         -      shell_exec(p->db, "SELECT * FROM sqlite_stat4",
  5812         -                 shell_callback, &data, &zErrMsg);
         5893  +      shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
  5813   5894         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  5814   5895       }
  5815   5896     }else
  5816   5897   
  5817   5898     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
  5818   5899       if( nArg==2 ){
  5819   5900         p->showHeader = booleanValue(azArg[1]);
................................................................................
  6286   6367       p->zFreeOnClose = 0;
  6287   6368       p->openMode = SHELL_OPEN_UNSPEC;
  6288   6369       /* Check for command-line arguments */
  6289   6370       for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
  6290   6371         const char *z = azArg[iName];
  6291   6372         if( optionMatch(z,"new") ){
  6292   6373           newFlag = 1;
  6293         -#ifdef SQLITE_HAVE_ZIP
         6374  +#ifdef SQLITE_HAVE_ZLIB
  6294   6375         }else if( optionMatch(z, "zip") ){
  6295   6376           p->openMode = SHELL_OPEN_ZIPFILE;
  6296   6377   #endif
  6297   6378         }else if( optionMatch(z, "append") ){
  6298   6379           p->openMode = SHELL_OPEN_APPENDVFS;
         6380  +      }else if( optionMatch(z, "readonly") ){
         6381  +        p->openMode = SHELL_OPEN_READONLY;
  6299   6382         }else if( z[0]=='-' ){
  6300   6383           utf8_printf(stderr, "unknown option: %s\n", z);
  6301   6384           rc = 1;
  6302   6385           goto meta_command_exit;
  6303   6386         }
  6304   6387       }
  6305   6388       /* If a filename is specified, try to open it first */
................................................................................
  6348   6431         }
  6349   6432         p->outCount = 2;
  6350   6433       }else{
  6351   6434         p->outCount = 0;
  6352   6435       }
  6353   6436       output_reset(p);
  6354   6437       if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
         6438  +#ifndef SQLITE_NOHAVE_SYSTEM
  6355   6439       if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
  6356   6440         p->doXdgOpen = 1;
  6357   6441         outputModePush(p);
  6358   6442         if( zFile[1]=='x' ){
  6359   6443           newTempFile(p, "csv");
  6360   6444           p->mode = MODE_Csv;
  6361   6445           sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
................................................................................
  6362   6446           sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
  6363   6447         }else{
  6364   6448           newTempFile(p, "txt");
  6365   6449           bTxtMode = 1;
  6366   6450         }
  6367   6451         zFile = p->zTempFile;
  6368   6452       }
         6453  +#endif /* SQLITE_NOHAVE_SYSTEM */
  6369   6454       if( zFile[0]=='|' ){
  6370   6455   #ifdef SQLITE_OMIT_POPEN
  6371   6456         raw_printf(stderr, "Error: pipes are not supported in this OS\n");
  6372   6457         rc = 1;
  6373   6458         p->out = stdout;
  6374   6459   #else
  6375   6460         p->out = popen(zFile + 1, "w");
................................................................................
  6481   6566       }else{
  6482   6567         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  6483   6568         rc = 1;
  6484   6569       }
  6485   6570       sqlite3_close(pSrc);
  6486   6571     }else
  6487   6572   
  6488         -
  6489   6573     if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
  6490   6574       if( nArg==2 ){
  6491         -      p->scanstatsOn = booleanValue(azArg[1]);
         6575  +      p->scanstatsOn = (u8)booleanValue(azArg[1]);
  6492   6576   #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
  6493   6577         raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
  6494   6578   #endif
  6495   6579       }else{
  6496   6580         raw_printf(stderr, "Usage: .scanstats on|off\n");
  6497   6581         rc = 1;
  6498   6582       }
................................................................................
  7072   7156             sSql.z, iSize);
  7073   7157       }
  7074   7158       freeText(&sQuery);
  7075   7159       freeText(&sSql);
  7076   7160       if( bDebug ){
  7077   7161         utf8_printf(p->out, "%s\n", zSql);
  7078   7162       }else{
  7079         -      shell_exec(p->db, zSql, shell_callback, p, 0);
         7163  +      shell_exec(p, zSql, 0);
  7080   7164       }
  7081   7165       sqlite3_free(zSql);
  7082   7166     }else
  7083   7167   
         7168  +#ifndef SQLITE_NOHAVE_SYSTEM
  7084   7169     if( c=='s'
  7085   7170      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
  7086   7171     ){
  7087   7172       char *zCmd;
  7088   7173       int i, x;
  7089   7174       if( nArg<2 ){
  7090   7175         raw_printf(stderr, "Usage: .system COMMAND\n");
................................................................................
  7096   7181         zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
  7097   7182                                zCmd, azArg[i]);
  7098   7183       }
  7099   7184       x = system(zCmd);
  7100   7185       sqlite3_free(zCmd);
  7101   7186       if( x ) raw_printf(stderr, "System command returns %d\n", x);
  7102   7187     }else
         7188  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
  7103   7189   
  7104   7190     if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
  7105   7191       static const char *azBool[] = { "off", "on", "trigger", "full"};
  7106   7192       int i;
  7107   7193       if( nArg!=1 ){
  7108   7194         raw_printf(stderr, "Usage: .show\n");
  7109   7195         rc = 1;
................................................................................
  7135   7221       raw_printf(p->out, "\n");
  7136   7222       utf8_printf(p->out, "%12.12s: %s\n", "filename",
  7137   7223                   p->zDbFilename ? p->zDbFilename : "");
  7138   7224     }else
  7139   7225   
  7140   7226     if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
  7141   7227       if( nArg==2 ){
  7142         -      p->statsOn = booleanValue(azArg[1]);
         7228  +      p->statsOn = (u8)booleanValue(azArg[1]);
  7143   7229       }else if( nArg==1 ){
  7144   7230         display_stats(p->db, p, 0);
  7145   7231       }else{
  7146   7232         raw_printf(stderr, "Usage: .stats ?on|off?\n");
  7147   7233         rc = 1;
  7148   7234       }
  7149   7235     }else
................................................................................
  7675   7761     if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
  7676   7762            && _all_whitespace(&zLine[2]) ){
  7677   7763       return 1;  /* SQL Server */
  7678   7764     }
  7679   7765     return 0;
  7680   7766   }
  7681   7767   
         7768  +/*
         7769  +** We need a default sqlite3_complete() implementation to use in case
         7770  +** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
         7771  +** any arbitrary text is a complete SQL statement.  This is not very
         7772  +** user-friendly, but it does seem to work.
         7773  +*/
         7774  +#ifdef SQLITE_OMIT_COMPLETE
         7775  +int sqlite3_complete(const char *zSql){ return 1; }
         7776  +#endif
         7777  +
  7682   7778   /*
  7683   7779   ** Return true if zSql is a complete SQL statement.  Return false if it
  7684   7780   ** ends in the middle of a string literal or C-style comment.
  7685   7781   */
  7686   7782   static int line_is_complete(char *zSql, int nSql){
  7687   7783     int rc;
  7688   7784     if( zSql==0 ) return 1;
................................................................................
  7699   7795   static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
  7700   7796     int rc;
  7701   7797     char *zErrMsg = 0;
  7702   7798   
  7703   7799     open_db(p, 0);
  7704   7800     if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
  7705   7801     BEGIN_TIMER;
  7706         -  rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
         7802  +  rc = shell_exec(p, zSql, &zErrMsg);
  7707   7803     END_TIMER;
  7708   7804     if( rc || zErrMsg ){
  7709   7805       char zPrefix[100];
  7710   7806       if( in!=0 || !stdin_is_interactive ){
  7711   7807         sqlite3_snprintf(sizeof(zPrefix), zPrefix,
  7712   7808                          "Error: near line %d:", startline);
  7713   7809       }else{
................................................................................
  7931   8027     sqlite3_free(zBuf);
  7932   8028   }
  7933   8029   
  7934   8030   /*
  7935   8031   ** Show available command line options
  7936   8032   */
  7937   8033   static const char zOptions[] =
         8034  +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
         8035  +  "   -A ARGS...           run \".archive ARGS\" and exit\n"
         8036  +#endif
         8037  +  "   -append              append the database to the end of the file\n"
  7938   8038     "   -ascii               set output mode to 'ascii'\n"
  7939   8039     "   -bail                stop after hitting an error\n"
  7940   8040     "   -batch               force batch I/O\n"
  7941   8041     "   -column              set output mode to 'column'\n"
  7942   8042     "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
  7943   8043     "   -csv                 set output mode to 'csv'\n"
  7944   8044     "   -echo                print commands before execution\n"
................................................................................
  7957   8057   #ifdef SQLITE_ENABLE_MULTIPLEX
  7958   8058     "   -multiplex           enable the multiplexor VFS\n"
  7959   8059   #endif
  7960   8060     "   -newline SEP         set output row separator. Default: '\\n'\n"
  7961   8061     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
  7962   8062     "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
  7963   8063     "   -quote               set output mode to 'quote'\n"
         8064  +  "   -readonly            open the database read-only\n"
  7964   8065     "   -separator SEP       set output column separator. Default: '|'\n"
  7965   8066     "   -stats               print memory stats before each finalize\n"
  7966   8067     "   -version             show SQLite version\n"
  7967   8068     "   -vfs NAME            use NAME as the default VFS\n"
  7968   8069   #ifdef SQLITE_ENABLE_VFSTRACE
  7969   8070     "   -vfstrace            enable tracing of all VFS calls\n"
  7970   8071   #endif
         8072  +#ifdef SQLITE_HAVE_ZLIB
         8073  +  "   -zip                 open the file as a ZIP Archive\n"
         8074  +#endif
  7971   8075   ;
  7972   8076   static void usage(int showDetail){
  7973   8077     utf8_printf(stderr,
  7974   8078         "Usage: %s [OPTIONS] FILENAME [SQL]\n"
  7975   8079         "FILENAME is the name of an SQLite database. A new database is created\n"
  7976   8080         "if the file does not previously exist.\n", Argv0);
  7977   8081     if( showDetail ){
................................................................................
  8066   8170     if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
  8067   8171       utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
  8068   8172               sqlite3_sourceid(), SQLITE_SOURCE_ID);
  8069   8173       exit(1);
  8070   8174     }
  8071   8175   #endif
  8072   8176     main_init(&data);
         8177  +
         8178  +  /* On Windows, we must translate command-line arguments into UTF-8.
         8179  +  ** The SQLite memory allocator subsystem has to be enabled in order to
         8180  +  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
         8181  +  ** subsequent sqlite3_config() calls will work.  So copy all results into
         8182  +  ** memory that does not come from the SQLite memory allocator.
         8183  +  */
  8073   8184   #if !SQLITE_SHELL_IS_UTF8
  8074   8185     sqlite3_initialize();
  8075         -  argv = sqlite3_malloc64(sizeof(argv[0])*argc);
         8186  +  argv = malloc(sizeof(argv[0])*argc);
  8076   8187     if( argv==0 ){
  8077   8188       raw_printf(stderr, "out of memory\n");
  8078   8189       exit(1);
  8079   8190     }
  8080   8191     for(i=0; i<argc; i++){
  8081         -    argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
         8192  +    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
         8193  +    int n;
         8194  +    if( z==0 ){
         8195  +      raw_printf(stderr, "out of memory\n");
         8196  +      exit(1);
         8197  +    }
         8198  +    n = (int)strlen(z);
         8199  +    argv[i] = malloc( n+1 );
  8082   8200       if( argv[i]==0 ){
  8083   8201         raw_printf(stderr, "out of memory\n");
  8084   8202         exit(1);
  8085   8203       }
         8204  +    memcpy(argv[i], z, n+1);
         8205  +    sqlite3_free(z);
  8086   8206     }
         8207  +  sqlite3_shutdown();
  8087   8208   #endif
         8209  +
  8088   8210     assert( argc>=1 && argv && argv[0] );
  8089   8211     Argv0 = argv[0];
  8090   8212   
  8091   8213     /* Make sure we have a valid signal handler early, before anything
  8092   8214     ** else is done.
  8093   8215     */
  8094   8216   #ifdef SIGINT
................................................................................
  8200   8322         sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
  8201   8323         if( pVfs ){
  8202   8324           sqlite3_vfs_register(pVfs, 1);
  8203   8325         }else{
  8204   8326           utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
  8205   8327           exit(1);
  8206   8328         }
  8207         -#ifdef SQLITE_HAVE_ZIP
         8329  +#ifdef SQLITE_HAVE_ZLIB
  8208   8330       }else if( strcmp(z,"-zip")==0 ){
  8209   8331         data.openMode = SHELL_OPEN_ZIPFILE;
  8210   8332   #endif
  8211   8333       }else if( strcmp(z,"-append")==0 ){
  8212   8334         data.openMode = SHELL_OPEN_APPENDVFS;
         8335  +    }else if( strcmp(z,"-readonly")==0 ){
         8336  +      data.openMode = SHELL_OPEN_READONLY;
         8337  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
         8338  +    }else if( strncmp(z, "-A",2)==0 ){
         8339  +      /* All remaining command-line arguments are passed to the ".archive"
         8340  +      ** command, so ignore them */
         8341  +      break;
         8342  +#endif
  8213   8343       }
  8214   8344     }
  8215   8345     if( data.zDbFilename==0 ){
  8216   8346   #ifndef SQLITE_OMIT_MEMORYDB
  8217   8347       data.zDbFilename = ":memory:";
  8218   8348       warnInmemoryDb = argc==1;
  8219   8349   #else
................................................................................
  8259   8389       }else if( strcmp(z,"-line")==0 ){
  8260   8390         data.mode = MODE_Line;
  8261   8391       }else if( strcmp(z,"-column")==0 ){
  8262   8392         data.mode = MODE_Column;
  8263   8393       }else if( strcmp(z,"-csv")==0 ){
  8264   8394         data.mode = MODE_Csv;
  8265   8395         memcpy(data.colSeparator,",",2);
  8266         -#ifdef SQLITE_HAVE_ZIP
         8396  +#ifdef SQLITE_HAVE_ZLIB
  8267   8397       }else if( strcmp(z,"-zip")==0 ){
  8268   8398         data.openMode = SHELL_OPEN_ZIPFILE;
  8269   8399   #endif
  8270   8400       }else if( strcmp(z,"-append")==0 ){
  8271   8401         data.openMode = SHELL_OPEN_APPENDVFS;
  8272   8402       }else if( strcmp(z,"-ascii")==0 ){
  8273   8403         data.mode = MODE_Ascii;
................................................................................
  8342   8472         if( i==argc-1 ) break;
  8343   8473         z = cmdline_option_value(argc,argv,++i);
  8344   8474         if( z[0]=='.' ){
  8345   8475           rc = do_meta_command(z, &data);
  8346   8476           if( rc && bail_on_error ) return rc==2 ? 0 : rc;
  8347   8477         }else{
  8348   8478           open_db(&data, 0);
  8349         -        rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
         8479  +        rc = shell_exec(&data, z, &zErrMsg);
  8350   8480           if( zErrMsg!=0 ){
  8351   8481             utf8_printf(stderr,"Error: %s\n", zErrMsg);
  8352   8482             if( bail_on_error ) return rc!=0 ? rc : 1;
  8353   8483           }else if( rc!=0 ){
  8354   8484             utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
  8355   8485             if( bail_on_error ) return rc;
  8356   8486           }
  8357   8487         }
         8488  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
         8489  +    }else if( strncmp(z, "-A", 2)==0 ){
         8490  +      if( nCmd>0 ){
         8491  +        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
         8492  +                            " with \"%s\"\n", z);
         8493  +        return 1;
         8494  +      }
         8495  +      open_db(&data, 0);
         8496  +      if( z[2] ){
         8497  +        argv[i] = &z[2];
         8498  +        arDotCommand(&data, argv+(i-1), argc-(i-1));
         8499  +      }else{
         8500  +        arDotCommand(&data, argv+i, argc-i);
         8501  +      }
         8502  +      readStdin = 0;
         8503  +      break;
         8504  +#endif
  8358   8505       }else{
  8359   8506         utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
  8360   8507         raw_printf(stderr,"Use -help for a list of options.\n");
  8361   8508         return 1;
  8362   8509       }
  8363   8510       data.cMode = data.mode;
  8364   8511     }
................................................................................
  8370   8517       */
  8371   8518       for(i=0; i<nCmd; i++){
  8372   8519         if( azCmd[i][0]=='.' ){
  8373   8520           rc = do_meta_command(azCmd[i], &data);
  8374   8521           if( rc ) return rc==2 ? 0 : rc;
  8375   8522         }else{
  8376   8523           open_db(&data, 0);
  8377         -        rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
         8524  +        rc = shell_exec(&data, azCmd[i], &zErrMsg);
  8378   8525           if( zErrMsg!=0 ){
  8379   8526             utf8_printf(stderr,"Error: %s\n", zErrMsg);
  8380   8527             return rc!=0 ? rc : 1;
  8381   8528           }else if( rc!=0 ){
  8382   8529             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
  8383   8530             return rc;
  8384   8531           }
................................................................................
  8433   8580     }
  8434   8581     sqlite3_free(data.zFreeOnClose);
  8435   8582     find_home_dir(1);
  8436   8583     output_reset(&data);
  8437   8584     data.doXdgOpen = 0;
  8438   8585     clearTempFile(&data);
  8439   8586   #if !SQLITE_SHELL_IS_UTF8
  8440         -  for(i=0; i<argc; i++) sqlite3_free(argv[i]);
  8441         -  sqlite3_free(argv);
         8587  +  for(i=0; i<argc; i++) free(argv[i]);
         8588  +  free(argv);
  8442   8589   #endif
  8443   8590     return rc;
  8444   8591   }

Changes to src/sqlite.h.in.

  2493   2493   void sqlite3_free_table(char **result);
  2494   2494   
  2495   2495   /*
  2496   2496   ** CAPI3REF: Formatted String Printing Functions
  2497   2497   **
  2498   2498   ** These routines are work-alikes of the "printf()" family of functions
  2499   2499   ** from the standard C library.
  2500         -** These routines understand most of the common K&R formatting options,
  2501         -** plus some additional non-standard formats, detailed below.
  2502         -** Note that some of the more obscure formatting options from recent
  2503         -** C-library standards are omitted from this implementation.
         2500  +** These routines understand most of the common formatting options from
         2501  +** the standard library printf() 
         2502  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         2503  +** See the [built-in printf()] documentation for details.
  2504   2504   **
  2505   2505   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2506         -** results into memory obtained from [sqlite3_malloc()].
         2506  +** results into memory obtained from [sqlite3_malloc64()].
  2507   2507   ** The strings returned by these two routines should be
  2508   2508   ** released by [sqlite3_free()].  ^Both routines return a
  2509         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         2509  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  2510   2510   ** memory to hold the resulting string.
  2511   2511   **
  2512   2512   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2513   2513   ** the standard C library.  The result is written into the
  2514   2514   ** buffer supplied as the second parameter whose size is given by
  2515   2515   ** the first parameter. Note that the order of the
  2516   2516   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  2526   2526   ** guarantees that the buffer is always zero-terminated.  ^The first
  2527   2527   ** parameter "n" is the total size of the buffer, including space for
  2528   2528   ** the zero terminator.  So the longest string that can be completely
  2529   2529   ** written will be n-1 characters.
  2530   2530   **
  2531   2531   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2532   2532   **
  2533         -** These routines all implement some additional formatting
  2534         -** options that are useful for constructing SQL statements.
  2535         -** All of the usual printf() formatting options apply.  In addition, there
  2536         -** is are "%q", "%Q", "%w" and "%z" options.
  2537         -**
  2538         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  2539         -** string from the argument list.  But %q also doubles every '\'' character.
  2540         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  2541         -** character it escapes that character and allows it to be inserted into
  2542         -** the string.
  2543         -**
  2544         -** For example, assume the string variable zText contains text as follows:
  2545         -**
  2546         -** <blockquote><pre>
  2547         -**  char *zText = "It's a happy day!";
  2548         -** </pre></blockquote>
  2549         -**
  2550         -** One can use this text in an SQL statement as follows:
  2551         -**
  2552         -** <blockquote><pre>
  2553         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2554         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2555         -**  sqlite3_free(zSQL);
  2556         -** </pre></blockquote>
  2557         -**
  2558         -** Because the %q format string is used, the '\'' character in zText
  2559         -** is escaped and the SQL generated is as follows:
  2560         -**
  2561         -** <blockquote><pre>
  2562         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  2563         -** </pre></blockquote>
  2564         -**
  2565         -** This is correct.  Had we used %s instead of %q, the generated SQL
  2566         -** would have looked like this:
  2567         -**
  2568         -** <blockquote><pre>
  2569         -**  INSERT INTO table1 VALUES('It's a happy day!');
  2570         -** </pre></blockquote>
  2571         -**
  2572         -** This second example is an SQL syntax error.  As a general rule you should
  2573         -** always use %q instead of %s when inserting text into a string literal.
  2574         -**
  2575         -** ^(The %Q option works like %q except it also adds single quotes around
  2576         -** the outside of the total string.  Additionally, if the parameter in the
  2577         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2578         -** single quotes).)^  So, for example, one could say:
  2579         -**
  2580         -** <blockquote><pre>
  2581         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2582         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2583         -**  sqlite3_free(zSQL);
  2584         -** </pre></blockquote>
  2585         -**
  2586         -** The code above will render a correct SQL statement in the zSQL
  2587         -** variable even if the zText variable is a NULL pointer.
  2588         -**
  2589         -** ^(The "%w" formatting option is like "%q" except that it expects to
  2590         -** be contained within double-quotes instead of single quotes, and it
  2591         -** escapes the double-quote character instead of the single-quote
  2592         -** character.)^  The "%w" formatting option is intended for safely inserting
  2593         -** table and column names into a constructed SQL statement.
  2594         -**
  2595         -** ^(The "%z" formatting option works like "%s" but with the
  2596         -** addition that after the string has been read and copied into
  2597         -** the result, [sqlite3_free()] is called on the input string.)^
         2533  +** See also:  [built-in printf()], [printf() SQL function]
  2598   2534   */
  2599   2535   char *sqlite3_mprintf(const char*,...);
  2600   2536   char *sqlite3_vmprintf(const char*, va_list);
  2601   2537   char *sqlite3_snprintf(int,char*,const char*, ...);
  2602   2538   char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2603   2539   
  2604   2540   /*
................................................................................
  3656   3592   ** a schema change, on the first  [sqlite3_step()] call following any change
  3657   3593   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3658   3594   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3659   3595   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3660   3596   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3661   3597   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3662   3598   ** </li>
         3599  +** </ol>
  3663   3600   **
  3664   3601   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3665   3602   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3666   3603   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3667   3604   ** sqlite3_prepare_v2() interface works exactly the same as
  3668   3605   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  3669         -** </ol>
  3670   3606   */
  3671   3607   int sqlite3_prepare(
  3672   3608     sqlite3 *db,            /* Database handle */
  3673   3609     const char *zSql,       /* SQL statement, UTF-8 encoded */
  3674   3610     int nByte,              /* Maximum length of zSql in bytes. */
  3675   3611     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3676   3612     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  7290   7226   ** wal file in wal mode databases, or the number of pages written to the
  7291   7227   ** database file in rollback mode databases. Any pages written as part of
  7292   7228   ** transaction rollback or database recovery operations are not included.
  7293   7229   ** If an IO or other error occurs while writing a page to disk, the effect
  7294   7230   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  7295   7231   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  7296   7232   ** </dd>
         7233  +**
         7234  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         7235  +** <dd>This parameter returns the number of dirty cache entries that have
         7236  +** been written to disk in the middle of a transaction due to the page
         7237  +** cache overflowing. Transactions are more efficient if they are written
         7238  +** to disk all at once. When pages spill mid-transaction, that introduces
         7239  +** additional overhead. This parameter can be used help identify
         7240  +** inefficiencies that can be resolve by increasing the cache size.
         7241  +** </dd>
  7297   7242   **
  7298   7243   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  7299   7244   ** <dd>This parameter returns zero for the current value if and only if
  7300   7245   ** all foreign key constraints (deferred or immediate) have been
  7301   7246   ** resolved.)^  ^The highwater mark is always 0.
  7302   7247   ** </dd>
  7303   7248   ** </dl>
................................................................................
  7310   7255   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7311   7256   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7312   7257   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7313   7258   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7314   7259   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7315   7260   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7316   7261   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  7317         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         7262  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         7263  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  7318   7264   
  7319   7265   
  7320   7266   /*
  7321   7267   ** CAPI3REF: Prepared Statement Status
  7322   7268   ** METHOD: sqlite3_stmt
  7323   7269   **
  7324   7270   ** ^(Each prepared statement maintains various
................................................................................
  8790   8736   ** transaction open on the database, or if the database is not a wal mode
  8791   8737   ** database.
  8792   8738   **
  8793   8739   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  8794   8740   */
  8795   8741   SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  8796   8742   
         8743  +/*
         8744  +** CAPI3REF: Serialize a database
         8745  +**
         8746  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         8747  +** that is a serialization of the S database on [database connection] D.
         8748  +** If P is not a NULL pointer, then the size of the database in bytes
         8749  +** is written into *P.
         8750  +**
         8751  +** For an ordinary on-disk database file, the serialization is just a
         8752  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         8753  +** the serialization is the same sequence of bytes which would be written
         8754  +** to disk if that database where backed up to disk.
         8755  +**
         8756  +** The usual case is that sqlite3_serialize() copies the serialization of
         8757  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         8758  +** a pointer to that memory.  The caller is responsible for freeing the
         8759  +** returned value to avoid a memory leak.  However, if the F argument
         8760  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         8761  +** are made, and the sqlite3_serialize() function will return a pointer
         8762  +** to the contiguous memory representation of the database that SQLite
         8763  +** is currently using for that database, or NULL if the no such contiguous
         8764  +** memory representation of the database exists.  A contiguous memory
         8765  +** representation of the database will usually only exist if there has
         8766  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         8767  +** values of D and S.
         8768  +** The size of the database is written into *P even if the 
         8769  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         8770  +** of the database exists.
         8771  +**
         8772  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         8773  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         8774  +** allocation error occurs.
         8775  +**
         8776  +** This interface is only available if SQLite is compiled with the
         8777  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8778  +*/
         8779  +unsigned char *sqlite3_serialize(
         8780  +  sqlite3 *db,           /* The database connection */
         8781  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         8782  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         8783  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         8784  +);
         8785  +
         8786  +/*
         8787  +** CAPI3REF: Flags for sqlite3_serialize
         8788  +**
         8789  +** Zero or more of the following constants can be OR-ed together for
         8790  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         8791  +**
         8792  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         8793  +** a pointer to contiguous in-memory database that it is currently using,
         8794  +** without making a copy of the database.  If SQLite is not currently using
         8795  +** a contiguous in-memory database, then this option causes
         8796  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         8797  +** using a contiguous in-memory database if it has been initialized by a
         8798  +** prior call to [sqlite3_deserialize()].
         8799  +*/
         8800  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         8801  +
         8802  +/*
         8803  +** CAPI3REF: Deserialize a database
         8804  +**
         8805  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         8806  +** [database connection] D to disconnection from database S and then
         8807  +** reopen S as an in-memory database based on the serialization contained
         8808  +** in P.  The serialized database P is N bytes in size.  M is the size of
         8809  +** the buffer P, which might be larger than N.  If M is larger than N, and
         8810  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         8811  +** permitted to add content to the in-memory database as long as the total
         8812  +** size does not exceed M bytes.
         8813  +**
         8814  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         8815  +** invoke sqlite3_free() on the serialization buffer when the database
         8816  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         8817  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         8818  +** if writes on the database cause it to grow larger than M bytes.
         8819  +**
         8820  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         8821  +** database is currently in a read transaction or is involved in a backup
         8822  +** operation.
         8823  +**
         8824  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         8825  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         8826  +** [sqlite3_free()] is invoked on argument P prior to returning.
         8827  +**
         8828  +** This interface is only available if SQLite is compiled with the
         8829  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8830  +*/
         8831  +int sqlite3_deserialize(
         8832  +  sqlite3 *db,            /* The database connection */
         8833  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         8834  +  unsigned char *pData,   /* The serialized database content */
         8835  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         8836  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         8837  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         8838  +);
         8839  +
         8840  +/*
         8841  +** CAPI3REF: Flags for sqlite3_deserialize()
         8842  +**
         8843  +** The following are allowed values for 6th argument (the F argument) to
         8844  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         8845  +**
         8846  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         8847  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         8848  +** and that SQLite should take ownership of this memory and automatically
         8849  +** free it when it has finished using it.  Without this flag, the caller
         8850  +** is resposible for freeing any dynamically allocated memory.
         8851  +**
         8852  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         8853  +** grow the size of the database usign calls to [sqlite3_realloc64()].  This
         8854  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         8855  +** Without this flag, the deserialized database cannot increase in size beyond
         8856  +** the number of bytes specified by the M parameter.
         8857  +**
         8858  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         8859  +** should be treated as read-only.
         8860  +*/
         8861  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         8862  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         8863  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         8864  +
  8797   8865   /*
  8798   8866   ** Undo the hack that converts floating point types to integer for
  8799   8867   ** builds on processors without floating point support.
  8800   8868   */
  8801   8869   #ifdef SQLITE_OMIT_FLOATING_POINT
  8802   8870   # undef double
  8803   8871   #endif
  8804   8872   
  8805   8873   #ifdef __cplusplus
  8806   8874   }  /* End of the 'extern "C"' block */
  8807   8875   #endif
  8808   8876   #endif /* SQLITE3_H */

Changes to src/sqlite3ext.h.

   559    559   #define sqlite3_prepare_v3             sqlite3_api->prepare_v3
   560    560   #define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
   561    561   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
   562    562   #define sqlite3_result_pointer         sqlite3_api->result_pointer
   563    563   #define sqlite3_value_pointer          sqlite3_api->value_pointer
   564    564   /* Version 3.22.0 and later */
   565    565   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
   566         -#define sqlite3_value_nochange         sqltie3_api->value_nochange
   567         -#define sqlite3_vtab_collation         sqltie3_api->vtab_collation
          566  +#define sqlite3_value_nochange         sqlite3_api->value_nochange
          567  +#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
   568    568   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   569    569   
   570    570   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   571    571     /* This case when the file really is being compiled as a loadable 
   572    572     ** extension */
   573    573   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   574    574   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

  1361   1361     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
  1362   1362     int aLimit[SQLITE_N_LIMIT];   /* Limits */
  1363   1363     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
  1364   1364     struct sqlite3InitInfo {      /* Information used during initialization */
  1365   1365       int newTnum;                /* Rootpage of table being initialized */
  1366   1366       u8 iDb;                     /* Which db file is being initialized */
  1367   1367       u8 busy;                    /* TRUE if currently initializing */
  1368         -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
  1369         -    u8 imposterTable;           /* Building an imposter table */
         1368  +    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
         1369  +    unsigned imposterTable : 1; /* Building an imposter table */
         1370  +    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
  1370   1371     } init;
  1371   1372     int nVdbeActive;              /* Number of VDBEs currently running */
  1372   1373     int nVdbeRead;                /* Number of active VDBEs that read or write */
  1373   1374     int nVdbeWrite;               /* Number of active VDBEs that read and write */
  1374   1375     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
  1375   1376     int nVDestroy;                /* Number of active OP_VDestroy operations */
  1376   1377     int nExtension;               /* Number of loaded extensions */
................................................................................
  1750   1751   };
  1751   1752   
  1752   1753   /* Allowed values for Column.colFlags:
  1753   1754   */
  1754   1755   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
  1755   1756   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
  1756   1757   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
         1758  +#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
  1757   1759   
  1758   1760   /*
  1759   1761   ** A "Collating Sequence" is defined by an instance of the following
  1760   1762   ** structure. Conceptually, a collating sequence consists of a name and
  1761   1763   ** a comparison routine that defines the order of that sequence.
  1762   1764   **
  1763   1765   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
  3834   3836   void sqlite3CodeVerifySchema(Parse*, int);
  3835   3837   void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  3836   3838   void sqlite3BeginTransaction(Parse*, int);
  3837   3839   void sqlite3EndTransaction(Parse*,int);
  3838   3840   void sqlite3Savepoint(Parse*, int, Token*);
  3839   3841   void sqlite3CloseSavepoints(sqlite3 *);
  3840   3842   void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
         3843  +int sqlite3ExprIdToTrueFalse(Expr*);
         3844  +int sqlite3ExprTruthValue(const Expr*);
  3841   3845   int sqlite3ExprIsConstant(Expr*);
  3842   3846   int sqlite3ExprIsConstantNotJoin(Expr*);
  3843   3847   int sqlite3ExprIsConstantOrFunction(Expr*, u8);
  3844   3848   int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
  3845   3849   int sqlite3ExprIsTableConstant(Expr*,int);
  3846   3850   #ifdef SQLITE_ENABLE_CURSOR_HINTS
  3847   3851   int sqlite3ExprContainsSubquery(Expr*);
................................................................................
  4015   4019   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  4016   4020   u8 sqlite3HexToInt(int h);
  4017   4021   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  4018   4022   
  4019   4023   #if defined(SQLITE_NEED_ERR_NAME)
  4020   4024   const char *sqlite3ErrName(int);
  4021   4025   #endif
         4026  +
         4027  +#ifdef SQLITE_ENABLE_DESERIALIZE
         4028  +int sqlite3MemdbInit(void);
         4029  +#endif
  4022   4030   
  4023   4031   const char *sqlite3ErrStr(int);
  4024   4032   int sqlite3ReadSchema(Parse *pParse);
  4025   4033   CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  4026   4034   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  4027   4035   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  4028   4036   CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
................................................................................
  4064   4072   extern const Token sqlite3IntTokens[];
  4065   4073   extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  4066   4074   extern FuncDefHash sqlite3BuiltinFunctions;
  4067   4075   #ifndef SQLITE_OMIT_WSD
  4068   4076   extern int sqlite3PendingByte;
  4069   4077   #endif
  4070   4078   #endif
         4079  +#ifdef VDBE_PROFILE
         4080  +extern sqlite3_uint64 sqlite3NProfileCnt;
         4081  +#endif
  4071   4082   void sqlite3RootPageMoved(sqlite3*, int, int, int);
  4072   4083   void sqlite3Reindex(Parse*, Token*, Token*);
  4073   4084   void sqlite3AlterFunctions(void);
  4074   4085   void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  4075   4086   int sqlite3GetToken(const unsigned char *, int *);
  4076   4087   void sqlite3NestedParse(Parse*, const char*, ...);
  4077   4088   void sqlite3ExpirePreparedStatements(sqlite3*);

Changes to src/status.c.

   333    333       }
   334    334   
   335    335       /*
   336    336       ** Set *pCurrent to the total cache hits or misses encountered by all
   337    337       ** pagers the database handle is connected to. *pHighwater is always set 
   338    338       ** to zero.
   339    339       */
          340  +    case SQLITE_DBSTATUS_CACHE_SPILL:
          341  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
          342  +      /* Fall through into the next case */
   340    343       case SQLITE_DBSTATUS_CACHE_HIT:
   341    344       case SQLITE_DBSTATUS_CACHE_MISS:
   342    345       case SQLITE_DBSTATUS_CACHE_WRITE:{
   343    346         int i;
   344    347         int nRet = 0;
   345    348         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
   346    349         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );

Changes to src/tclsqlite.c.

    60     60   
    61     61   /* Used to get the current process ID */
    62     62   #if !defined(_WIN32)
    63     63   # include <unistd.h>
    64     64   # define GETPID getpid
    65     65   #elif !defined(_WIN32_WCE)
    66     66   # ifndef SQLITE_AMALGAMATION
    67         -#  define WIN32_LEAN_AND_MEAN
           67  +#  ifndef WIN32_LEAN_AND_MEAN
           68  +#   define WIN32_LEAN_AND_MEAN
           69  +#  endif
    68     70   #  include <windows.h>
    69     71   # endif
    70     72   # define GETPID (int)GetCurrentProcessId
    71     73   #endif
    72     74   
    73     75   /*
    74     76    * Windows needs to know which symbols to export.  Unix does not.
................................................................................
   642    644         Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
   643    645         Tcl_DecrRefCount(pCmd);
   644    646         Tcl_ResetResult(pDb->interp);
   645    647         break;
   646    648       }
   647    649       case SQLITE_TRACE_PROFILE: {
   648    650         sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
   649         -      sqlite3_int64 ns = (sqlite3_int64)xd;
          651  +      sqlite3_int64 ns = *(sqlite3_int64*)xd;
   650    652   
   651    653         pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
   652    654         Tcl_IncrRefCount(pCmd);
   653    655         Tcl_ListObjAppendElement(pDb->interp, pCmd,
   654    656                                  Tcl_NewWideIntObj((Tcl_WideInt)pStmt));
   655    657         Tcl_ListObjAppendElement(pDb->interp, pCmd,
   656    658                                  Tcl_NewWideIntObj((Tcl_WideInt)ns));
................................................................................
  1842   1844     int objc,
  1843   1845     Tcl_Obj *const*objv
  1844   1846   ){
  1845   1847     SqliteDb *pDb = (SqliteDb*)cd;
  1846   1848     int choice;
  1847   1849     int rc = TCL_OK;
  1848   1850     static const char *DB_strs[] = {
  1849         -    "authorizer",         "backup",            "busy",
  1850         -    "cache",              "changes",           "close",
  1851         -    "collate",            "collation_needed",  "commit_hook",
  1852         -    "complete",           "copy",              "enable_load_extension",
  1853         -    "errorcode",          "eval",              "exists",
  1854         -    "function",           "incrblob",          "interrupt",
  1855         -    "last_insert_rowid",  "nullvalue",         "onecolumn",
  1856         -    "preupdate",          "profile",           "progress",
  1857         -    "rekey",              "restore",           "rollback_hook",
  1858         -    "status",             "timeout",           "total_changes",
  1859         -    "trace",              "trace_v2",          "transaction",
  1860         -    "unlock_notify",      "update_hook",       "version",
  1861         -    "wal_hook",
  1862         -    0
         1851  +    "authorizer",             "backup",                "busy",
         1852  +    "cache",                  "changes",               "close",
         1853  +    "collate",                "collation_needed",      "commit_hook",
         1854  +    "complete",               "copy",                  "deserialize",
         1855  +    "enable_load_extension",  "errorcode",             "eval",
         1856  +    "exists",                 "function",              "incrblob",
         1857  +    "interrupt",              "last_insert_rowid",     "nullvalue",
         1858  +    "onecolumn",              "preupdate",             "profile",
         1859  +    "progress",               "rekey",                 "restore",
         1860  +