#ifndef SQLITE_OMIT_VIRTUALTABLE
#ifndef SQLITE_AMALGAMATION
+
typedef sqlite3_int64 i64;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
#define MIN(a,b) ((a)<(b) ? (a) : (b))
+
+#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
+# define ALWAYS(X) (1)
+# define NEVER(X) (0)
+#elif !defined(NDEBUG)
+# define ALWAYS(X) ((X)?1:(assert(0),0))
+# define NEVER(X) ((X)?(assert(0),1):0)
+#else
+# define ALWAYS(X) (X)
+# define NEVER(X) (X)
#endif
+#endif /* SQLITE_AMALGAMATION */
+
static const char ZIPFILE_SCHEMA[] =
"CREATE TABLE y("
"name PRIMARY KEY," /* 0: Name of file in zip archive */
static void zipfileDequote(char *zIn){
char q = zIn[0];
if( q=='"' || q=='\'' || q=='`' || q=='[' ){
- char c;
int iIn = 1;
int iOut = 0;
if( q=='[' ) q = ']';
- while( (c = zIn[iIn++]) ){
- if( c==q ){
- if( zIn[iIn++]!=q ) break;
- }
+ while( ALWAYS(zIn[iIn]) ){
+ char c = zIn[iIn++];
+ if( c==q && zIn[iIn++]!=q ) break;
zIn[iOut++] = c;
}
zIn[iOut] = '\0';
zipfileResetCursor(pCsr);
/* Remove this cursor from the ZipfileTab.pCsrList list. */
- for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
- if( *pp==pCsr ){
- *pp = pCsr->pCsrNext;
- break;
- }
- }
+ for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
+ *pp = pCsr->pCsrNext;
sqlite3_free(pCsr);
return SQLITE_OK;
** If an error occurs, an error code is left in pCtx instead.
*/
static void zipfileInflate(
- sqlite3_context *pCtx, /* Store error here, if any */
+ sqlite3_context *pCtx, /* Store result here */
const u8 *aIn, /* Compressed data */
int nIn, /* Size of buffer aIn[] in bytes */
int nOut /* Expected output size */
pEOCD->iOffset = zipfileRead32(aRead);
}
- return SQLITE_OK;
+ return rc;
}
/*
-C Prevent\susers\sfrom\screating\szipfile()\svirtual\stables\swithout\san\sargument.
-D 2018-01-30T14:07:55.726
+C Add\stests\scases\sfor\sthe\szipfile\sextension.
+D 2018-01-30T17:43:22.142
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F Makefile.in 7a3f714b4fcf793108042b7b0a5c720b0b310ec84314d61ba7f3f49f27e550ea
F ext/misc/vtablog.c 31d0d8f4406795679dcd3a67917c213d3a2a5fb3ea5de35f6e773491ed7e13c9
F ext/misc/vtshim.c 1976e6dd68dd0d64508c91a6dfab8e75f8aaf6cd
F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212
-F ext/misc/zipfile.c ead25f062cee790b7c764ce8d2c6ad32a7ac82fc31ea80f69be99948f96f2d19
+F ext/misc/zipfile.c aa10ec6a235030cd368b511e7c78e40251008dddddeb0eb006ad1f344f78b690
F ext/rbu/rbu.c ea7d1b7eb44c123a2a619332e19fe5313500705c4a58aaa1887905c0d83ffc2e
F ext/rbu/rbu1.test 43836fac8c7179a358eaf38a8a1ef3d6e6285842
F ext/rbu/rbu10.test 1846519a438697f45e9dcb246908af81b551c29e1078d0304fae83f1fed7e9ee
F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa
F test/zerodamage.test 9c41628db7e8d9e8a0181e59ea5f189df311a9f6ce99cc376dc461f66db6f8dc
F test/zipfile.test 368a5a0c97be0caaf8c3efa8293bfe18436d546805678fa00b6aa81bc98727ec
+F test/zipfile2.test 2855c993641935b51dfb3f9005173114538c7a2eb2a60fe3fc47a802e6794406
+F test/zipfilefault.test df4fa9e16116e0cb21d09037a6b0a7d93fecd8767d82154b66044b4ca43fca58
F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5
F tool/GetTclKit.bat 8995df40c4209808b31f24de0b58f90930239a234f7591e3675d45bfbb990c5d
F tool/Replace.cs 02c67258801c2fb5f63231e0ac0f220b4b36ba91
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 438c5c5237a801ae78809bf324bb9251fb50250addfc1f8e36659442b0e26ab6
-R 0f8eb13d715c2195926ed8684b288375
+P 81fdbe0cc5a360f818078d47a5888d0a29d555927da279a9a0213702d74ef09a
+R 63c2b54c1a4defd96e22435ea8f5b625
U dan
-Z 6708ef6af1eb582ebfe61496df2754b7
+Z 6717becc48c95c87eb42213e9c88ecb3
-81fdbe0cc5a360f818078d47a5888d0a29d555927da279a9a0213702d74ef09a
\ No newline at end of file
+13b786dafd308b7aef42328f26fe64981c83d524e4bf82ceb7d58afc7146c7d3
\ No newline at end of file
--- /dev/null
+# 2018 January 30
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix zipfile2
+
+ifcapable !vtab {
+ finish_test; return
+}
+if {[catch {load_static_extension db zipfile} error]} {
+ puts "Skipping zipfile2 tests, hit load error: $error"
+ finish_test; return
+}
+
+proc blobliteral {str} {
+ set concat [string map {" " "" "\n" ""} $str]
+ return "X'$concat'"
+}
+
+proc blob {str} {
+ binary decode hex $str
+}
+
+proc findall {needle haystack} {
+ set L [list]
+ set start 0
+ while { [set idx [string first $needle $haystack $start]]>=0 } {
+ lappend L $idx
+ set start [expr $idx+1]
+ }
+ set L
+}
+
+do_execsql_test 1.0 {
+ CREATE VIRTUAL TABLE aaa USING zipfile('testzip');
+ CREATE VIRTUAL TABLE bbb USING zipfile("testzip");
+ CREATE VIRTUAL TABLE ccc USING zipfile(`testzip`);
+ CREATE VIRTUAL TABLE ddd USING zipfile([testzip]);
+ CREATE VIRTUAL TABLE eee USING zipfile(testzip);
+ CREATE VIRTUAL TABLE fff USING zipfile('test''zip');
+}
+
+do_test 2.0 {
+ forcedelete testdir
+ file mkdir testdir
+ execsql { CREATE VIRTUAL TABLE hhh USING zipfile('testdir') }
+ catchsql { SELECT * FROM hhh }
+} {1 {error in fread()}}
+
+
+set archive {
+ 504B0304140000080000D4A52BEC09F3B6E0110000001100000005000900612E
+ 747874555405000140420F00636F6E74656E7473206F6620612E747874504B03
+ 04140000080000D4A52BECD98916A7110000001100000005000900622E747874
+ 555405000140420F00636F6E74656E7473206F6620622E747874504B01021E03
+ 140000080000D4A52BEC09F3B6E0110000001100000005000900000000000000
+ 0000A48100000000612E747874555405000140420F00504B01021E0314000008
+ 0000D4A52BECD98916A71100000011000000050009000000000000000000A481
+ 3D000000622E747874555405000140420F00504B050600000000020002007800
+ 00007A0000000000
+}
+
+do_execsql_test 3.1 {
+ WITH contents(name,mtime,data) AS (
+ VALUES('a.txt', 1000000, 'contents of a.txt') UNION ALL
+ VALUES('b.txt', 1000000, 'contents of b.txt')
+ ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
+} [blobliteral $archive]
+
+set blob [blob $archive]
+do_execsql_test 3.2 {
+ SELECT name,mtime,data FROM zipfile($blob)
+} {
+ a.txt 1000000 {contents of a.txt}
+ b.txt 1000000 {contents of b.txt}
+}
+
+# Corrupt each of the 0x50 0x4B (ascii "PK") headers in the file
+# Test that in each case this causes an error.
+#
+set L [findall 504B $archive]
+for {set i 0} {$i < [llength $L]} {incr i} {
+ set idx [lindex $L $i]
+ set a [string replace $archive $idx [expr $idx+3] 0000]
+ set blob [blob $a]
+ do_catchsql_test 3.3.$i {
+ SELECT name,mtime,data FROM zipfile($blob)
+ } {/1 .*/}
+}
+
+set L [findall 5554 $archive]
+for {set i 0} {$i < [llength $L]} {incr i} {
+ set idx [lindex $L $i]
+ set a [string replace $archive $idx [expr $idx+3] 1234]
+ set blob [blob $a]
+ do_execsql_test 3.4.$i {
+ SELECT name,data FROM zipfile($blob)
+ } {
+ a.txt {contents of a.txt}
+ b.txt {contents of b.txt}
+ }
+}
+
+for {set i 0} {$i < [llength $L]} {incr i} {
+ set idx [lindex $L $i]
+ set a [string replace $archive [expr $idx+8] [expr $idx+9] 00]
+ set blob [blob $a]
+ do_execsql_test 3.5.$i {
+ SELECT name,data FROM zipfile($blob)
+ } {
+ a.txt {contents of a.txt}
+ b.txt {contents of b.txt}
+ }
+}
+
+if 0 {
+set blob [db one {
+ WITH contents(name,mtime,data) AS (
+ VALUES('a.txt', 1000000, 'aaaaaaaaaaaaaaaaaaaaaaa')
+ ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
+}]
+set blob [string range $blob 2 end]
+set blob [string range $blob 0 end-1]
+while {[string length $blob]>0} {
+ puts [string range $blob 0 63]
+ set blob [string range $blob 64 end]
+}
+exit
+}
+
+set archive2 {
+ 504B0304140000080800D4A52BEC08F54C6E050000001700000005000900612E
+ 747874555405000140420F004B4CC40A00504B01021E03140000080800D4A52B
+ EC08F54C6E0500000017000000050009000000000000000000A4810000000061
+ 2E747874555405000140420F00504B050600000000010001003C000000310000
+ 000000
+}
+set blob [blob $archive2]
+do_execsql_test 4.0 {
+ SELECT name,mtime,data,method FROM zipfile($blob)
+} {
+ a.txt 1000000 aaaaaaaaaaaaaaaaaaaaaaa 8
+}
+
+breakpoint
+set L [findall 17000000 $archive2]
+set a $archive2
+foreach i $L { set a [string replace $a $i [expr $i+7] 16000000] }
+set blob [blob $a]
+do_catchsql_test 4.1 {
+ SELECT name,mtime,data,method FROM zipfile($blob)
+} {1 {SQL logic error}}
+
+
+
+finish_test
+
--- /dev/null
+# 2018 January 30
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/malloc_common.tcl
+set testprefix zipfilefault
+
+ifcapable !vtab {
+ finish_test; return
+}
+if {[catch {load_static_extension db zipfile} error]} {
+ puts "Skipping zipfile2 tests, hit load error: $error"
+ finish_test; return
+}
+
+faultsim_save_and_close
+do_faultsim_test 1 -prep {
+ faultsim_restore_and_reopen
+ load_static_extension db zipfile
+ execsql { DROP TABLE IF EXISTS aaa }
+} -body {
+ execsql { CREATE VIRTUAL TABLE aaa USING zipfile('test.zip') }
+} -test {
+ faultsim_test_result {0 {}}
+}
+
+forcedelete test.zip
+sqlite3 db test.db
+load_static_extension db zipfile
+do_execsql_test 2.0 {
+ CREATE VIRTUAL TABLE setup USING zipfile('test.zip');
+ INSERT INTO setup(name, data) VALUES('a.txt', '1234567890');
+}
+
+do_faultsim_test 2 -faults oom* -body {
+ execsql { SELECT name,data FROM zipfile('test.zip') }
+} -test {
+ faultsim_test_result {0 {a.txt 1234567890}}
+}
+
+forcedelete test.zip
+reset_db
+load_static_extension db zipfile
+do_execsql_test 3.0 {
+ CREATE VIRTUAL TABLE setup USING zipfile('test.zip');
+ INSERT INTO setup(name, data) VALUES('a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa');
+}
+
+do_faultsim_test 2 -faults oom* -body {
+ execsql { SELECT name,data FROM zipfile('test.zip') }
+} -test {
+ faultsim_test_result {0 {a.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaa}}
+}
+
+
+finish_test
+