Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Update the zipfile module so that it matches the documentation. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
7e7e472fa91a2bad2e521d4d67f176c8 |
User & Date: | dan 2018-01-08 19:59:59.813 |
Context
2018-01-09
| ||
00:26 | Fix harmless compiler warnings. (check-in: 1adf4e6039 user: mistachkin tags: trunk) | |
2018-01-08
| ||
19:59 | Update the zipfile module so that it matches the documentation. (check-in: 7e7e472fa9 user: dan tags: trunk) | |
17:34 | Fix problems in the sqlite3expert.c code revealed by -fsanitize. (check-in: 7a93dd784b user: dan tags: trunk) | |
Changes
Changes to ext/misc/zipfile.c.
︙ | ︙ | |||
38 39 40 41 42 43 44 | typedef sqlite3_int64 i64; typedef unsigned char u8; typedef unsigned short u16; typedef unsigned long u32; #define MIN(a,b) ((a)<(b) ? (a) : (b)) #endif | | | | | | > | | | | | 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | typedef sqlite3_int64 i64; typedef unsigned char u8; typedef unsigned short u16; typedef unsigned long u32; #define MIN(a,b) ((a)<(b) ? (a) : (b)) #endif #define ZIPFILE_SCHEMA "CREATE TABLE y(" \ "name, /* 0: Name of file in zip archive */" \ "mode, /* 1: POSIX mode for file */" \ "mtime, /* 2: Last modification time in seconds since epoch */" \ "sz, /* 3: Size of object */" \ "rawdata, /* 4: Raw data */" \ "data, /* 5: Uncompressed data */" \ "method, /* 6: Compression method (integer) */" \ "file HIDDEN /* Name of zip file */" \ ");" #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "f" in the above */ #define ZIPFILE_BUFFER_SIZE (64*1024) /* ** Magic numbers used to read and write zip files. ** ** ZIPFILE_NEWENTRY_MADEBY: |
︙ | ︙ | |||
666 667 668 669 670 671 672 673 674 675 676 677 678 679 | (res.tm_hour << 11)); pCds->mDate = (u16)( (res.tm_mday-1) + ((res.tm_mon+1) << 5) + ((res.tm_year-80) << 9)); } /* ** Return values of columns for the row at which the series_cursor ** is currently pointing. */ static int zipfileColumn( sqlite3_vtab_cursor *cur, /* The cursor */ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | (res.tm_hour << 11)); pCds->mDate = (u16)( (res.tm_mday-1) + ((res.tm_mon+1) << 5) + ((res.tm_year-80) << 9)); } static void zipfileInflate( sqlite3_context *pCtx, /* Store error here, if any */ const u8 *aIn, /* Compressed data */ int nIn, /* Size of buffer aIn[] in bytes */ int nOut /* Expected output size */ ){ u8 *aRes = sqlite3_malloc(nOut); if( aRes==0 ){ sqlite3_result_error_nomem(pCtx); }else{ int err; z_stream str; memset(&str, 0, sizeof(str)); str.next_in = (Byte*)aIn; str.avail_in = nIn; str.next_out = (Byte*)aRes; str.avail_out = nOut; err = inflateInit2(&str, -15); if( err!=Z_OK ){ zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err); }else{ err = inflate(&str, Z_NO_FLUSH); if( err!=Z_STREAM_END ){ zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err); }else{ sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT); } } sqlite3_free(aRes); inflateEnd(&str); } } static int zipfileDeflate( ZipfileTab *pTab, /* Set error message here */ const u8 *aIn, int nIn, /* Input */ u8 **ppOut, int *pnOut /* Output */ ){ int nAlloc = (int)compressBound(nIn); u8 *aOut; int rc; aOut = (u8*)sqlite3_malloc(nAlloc); if( aOut==0 ){ rc = SQLITE_NOMEM; }else{ int res; z_stream str; memset(&str, 0, sizeof(str)); str.next_in = (z_const Bytef*)aIn; str.avail_in = nIn; str.next_out = aOut; str.avail_out = nAlloc; deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); res = deflate(&str, Z_FINISH); if( res==Z_STREAM_END ){ *ppOut = aOut; *pnOut = (int)str.total_out; }else{ sqlite3_free(aOut); pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error"); rc = SQLITE_ERROR; } deflateEnd(&str); } return rc; } /* ** Return values of columns for the row at which the series_cursor ** is currently pointing. */ static int zipfileColumn( sqlite3_vtab_cursor *cur, /* The cursor */ |
︙ | ︙ | |||
699 700 701 702 703 704 705 | } break; } case 3: { /* sz */ sqlite3_result_int64(ctx, pCsr->cds.szUncompressed); break; } | > | > | | | | | | | | | | | | > > > | > | | | > | | 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | } break; } case 3: { /* sz */ sqlite3_result_int64(ctx, pCsr->cds.szUncompressed); break; } case 4: /* rawdata */ case 5: { /* data */ if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){ int sz = pCsr->cds.szCompressed; if( sz>0 ){ u8 *aBuf = sqlite3_malloc(sz); if( aBuf==0 ){ rc = SQLITE_NOMEM; }else{ FILE *pFile = zipfileGetFd(pCsr); rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff, &pCsr->base.pVtab->zErrMsg ); } if( rc==SQLITE_OK ){ if( i==5 && pCsr->cds.iCompression ){ zipfileInflate(ctx, aBuf, sz, pCsr->cds.szUncompressed); }else{ sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT); } sqlite3_free(aBuf); } } } break; } case 6: /* method */ sqlite3_result_int(ctx, pCsr->cds.iCompression); break; } return SQLITE_OK; } |
︙ | ︙ | |||
1069 1070 1071 1072 1073 1074 1075 | return rc; } static int zipfileGetMode(ZipfileTab *pTab, sqlite3_value *pVal, int *pMode){ const char *z = (const char*)sqlite3_value_text(pVal); int mode = 0; | | > > | 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | return rc; } static int zipfileGetMode(ZipfileTab *pTab, sqlite3_value *pVal, int *pMode){ const char *z = (const char*)sqlite3_value_text(pVal); int mode = 0; if( z==0 ){ mode = 33188; /* -rw-r--r-- */ }else if( z[0]>=0 && z[0]<=9 ){ mode = sqlite3_value_int(pVal); }else{ const char zTemplate[11] = "-rwxrwxrwx"; int i; if( strlen(z)!=10 ) goto parse_error; switch( z[0] ){ case '-': mode |= S_IFREG; break; |
︙ | ︙ | |||
1114 1115 1116 1117 1118 1119 1120 | ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ int mode; /* Mode for new entry */ i64 mTime; /* Modification time for new entry */ i64 sz; /* Uncompressed size */ const char *zPath; /* Path for new entry */ int nPath; /* strlen(zPath) */ | | | | 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 | ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ int mode; /* Mode for new entry */ i64 mTime; /* Modification time for new entry */ i64 sz; /* Uncompressed size */ const char *zPath; /* Path for new entry */ int nPath; /* strlen(zPath) */ const u8 *pData = 0; /* Pointer to buffer containing content */ int nData = 0; /* Size of pData buffer in bytes */ int iMethod = 0; /* Compression method for new entry */ u8 *pFree = 0; /* Free this */ ZipfileCDS cds; /* New Central Directory Structure entry */ assert( pTab->zFile ); assert( pTab->pWriteFd ); |
︙ | ︙ | |||
1139 1140 1141 1142 1143 1144 1145 | if( nVal==1 ) return SQLITE_OK; } zPath = (const char*)sqlite3_value_text(apVal[2]); nPath = (int)strlen(zPath); rc = zipfileGetMode(pTab, apVal[3], &mode); if( rc!=SQLITE_OK ) return rc; | > > > | > | > > > > | | > | < < | | > > | > | > > > | < < < < < < < < < < | | < < < < | < | > | > > > > > > > > | > > | 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 | if( nVal==1 ) return SQLITE_OK; } zPath = (const char*)sqlite3_value_text(apVal[2]); nPath = (int)strlen(zPath); rc = zipfileGetMode(pTab, apVal[3], &mode); if( rc!=SQLITE_OK ) return rc; if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){ mTime = (sqlite3_int64)time(0); }else{ mTime = sqlite3_value_int64(apVal[4]); } if( sqlite3_value_type(apVal[5])==SQLITE_NULL /* sz */ && sqlite3_value_type(apVal[6])==SQLITE_NULL /* rawdata */ && sqlite3_value_type(apVal[7])!=SQLITE_NULL /* data */ ){ const u8 *aIn = sqlite3_value_blob(apVal[7]); int nIn = sqlite3_value_bytes(apVal[7]); int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL; iMethod = sqlite3_value_int(apVal[8]); sz = nIn; if( iMethod!=0 && iMethod!=8 ){ rc = SQLITE_CONSTRAINT; }else if( bAuto || iMethod ){ rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nData); if( rc==SQLITE_OK ){ if( iMethod || nData<nIn ){ iMethod = 8; pData = pFree; }else{ pData = aIn; nData = nIn; } } } }else if( sqlite3_value_type(apVal[5])!=SQLITE_NULL /* sz */ && sqlite3_value_type(apVal[6])!=SQLITE_NULL /* rawdata */ && sqlite3_value_type(apVal[7])==SQLITE_NULL /* data */ && sqlite3_value_type(apVal[8])!=SQLITE_NULL /* method */ ){ pData = sqlite3_value_blob(apVal[6]); nData = sqlite3_value_bytes(apVal[6]); sz = sqlite3_value_int(apVal[5]); iMethod = sqlite3_value_int(apVal[8]); if( iMethod<0 || iMethod>65535 ){ pTab->base.zErrMsg = sqlite3_mprintf( "zipfile: invalid compression method: %d", iMethod ); rc = SQLITE_ERROR; } }else{ rc = SQLITE_CONSTRAINT; } if( rc==SQLITE_OK ){ /* Create the new CDS record. */ memset(&cds, 0, sizeof(cds)); cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; |
︙ | ︙ | |||
1348 1349 1350 1351 1352 1353 1354 | int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0); return rc; } #else /* SQLITE_OMIT_VIRTUALTABLE */ # define zipfileRegister(x) SQLITE_OK #endif | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 | int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0); return rc; } #else /* SQLITE_OMIT_VIRTUALTABLE */ # define zipfileRegister(x) SQLITE_OK #endif #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_zipfile_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ return zipfileRegister(db); } |
Changes to src/shell.c.in.
︙ | ︙ | |||
4843 4844 4845 4846 4847 4848 4849 | "SELECT " " :1 || name, " " writefile(?1 || name, %s, mode, mtime) " "FROM %s WHERE (%s) AND (data IS NULL OR ?2 = 0)"; const char *azExtraArg[] = { "sqlar_uncompress(data, sz)", | | < < | 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 | "SELECT " " :1 || name, " " writefile(?1 || name, %s, mode, mtime) " "FROM %s WHERE (%s) AND (data IS NULL OR ?2 = 0)"; const char *azExtraArg[] = { "sqlar_uncompress(data, sz)", "data" }; const char *azSource[] = { "sqlar", "zipfile(?3)" }; sqlite3_stmt *pSql = 0; int rc = SQLITE_OK; char *zDir = 0; char *zWhere = 0; int i; |
︙ | ︙ |
Changes to test/zipfile.test.
︙ | ︙ | |||
24 25 26 27 28 29 30 | CREATE VIRTUAL TABLE temp.zz USING zipfile('test.zip'); PRAGMA table_info(zz); } { 0 name {} 0 {} 0 1 mode {} 0 {} 0 2 mtime {} 0 {} 0 3 sz {} 0 {} 0 | > | | > | > | | | < | > | | 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | CREATE VIRTUAL TABLE temp.zz USING zipfile('test.zip'); PRAGMA table_info(zz); } { 0 name {} 0 {} 0 1 mode {} 0 {} 0 2 mtime {} 0 {} 0 3 sz {} 0 {} 0 4 rawdata {} 0 {} 0 5 data {} 0 {} 0 6 method {} 0 {} 0 } do_execsql_test 1.1.1 { INSERT INTO zz(name, mode, mtime, sz, rawdata, method) VALUES('f.txt', '-rw-r--r--', 1000000000, 5, 'abcde', 0); } do_execsql_test 1.1.2 { INSERT INTO zz(name, mtime, sz, rawdata, method) VALUES('g.txt', 1000000002, 5, '12345', 0); } do_execsql_test 1.2 { SELECT name, mtime, data FROM zipfile('test.zip') } { f.txt 1000000000 abcde g.txt 1000000002 12345 } do_execsql_test 1.3 { INSERT INTO zz(name, mode, mtime, data) VALUES('h.txt', '-rw-r--r--', 1000000004, 'aaaaaaaaaabbbbbbbbbb' ); } do_execsql_test 1.4 { SELECT name, mtime, data, method FROM zipfile('test.zip'); } { f.txt 1000000000 abcde 0 g.txt 1000000002 12345 0 h.txt 1000000004 aaaaaaaaaabbbbbbbbbb 8 } do_execsql_test 1.5.1 { BEGIN; INSERT INTO zz(name, mode, mtime, data, method) VALUES('i.txt', '-rw-r--r--', 1000000006, 'zxcvb', 0); SELECT name FROM zz; COMMIT; } {f.txt g.txt h.txt i.txt} do_execsql_test 1.5.2 { SELECT name FROM zz; } {f.txt g.txt h.txt i.txt} |
︙ | ︙ |