Move variable declaration in zipfile extension to fix MSVC compiler error.
[sqlite.git] / ext / misc / zipfile.c
blobf88b7e22d65d53b1dfca3c149dd7e406ed8e8fa3
1 /*
2 ** 2017-12-26
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.
11 ******************************************************************************
13 ** This file implements a virtual table for reading and writing ZIP archive
14 ** files.
16 ** Usage example:
18 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
20 ** Current limitations:
22 ** * No support for encryption
23 ** * No support for ZIP archives spanning multiple files
24 ** * No support for zip64 extensions
25 ** * Only the "inflate/deflate" (zlib) compression method is supported
27 #include "sqlite3ext.h"
28 SQLITE_EXTENSION_INIT1
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
33 #include <zlib.h>
35 #ifndef SQLITE_OMIT_VIRTUALTABLE
37 #ifndef SQLITE_AMALGAMATION
39 typedef sqlite3_int64 i64;
40 typedef unsigned char u8;
41 typedef unsigned short u16;
42 typedef unsigned long u32;
43 #define MIN(a,b) ((a)<(b) ? (a) : (b))
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
56 #endif /* SQLITE_AMALGAMATION */
59 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
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.
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
75 #endif
77 static const char ZIPFILE_SCHEMA[] =
78 "CREATE TABLE y("
79 "name PRIMARY KEY," /* 0: Name of file in zip archive */
80 "mode," /* 1: POSIX mode for file */
81 "mtime," /* 2: Last modification time (secs since 1970)*/
82 "sz," /* 3: Size of object */
83 "rawdata," /* 4: Raw data */
84 "data," /* 5: Uncompressed data */
85 "method," /* 6: Compression method (integer) */
86 "z HIDDEN" /* 7: Name of zip file */
87 ") WITHOUT ROWID;";
89 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
90 #define ZIPFILE_BUFFER_SIZE (64*1024)
94 ** Magic numbers used to read and write zip files.
96 ** ZIPFILE_NEWENTRY_MADEBY:
97 ** Use this value for the "version-made-by" field in new zip file
98 ** entries. The upper byte indicates "unix", and the lower byte
99 ** indicates that the zip file matches pkzip specification 3.0.
100 ** This is what info-zip seems to do.
102 ** ZIPFILE_NEWENTRY_REQUIRED:
103 ** Value for "version-required-to-extract" field of new entries.
104 ** Version 2.0 is required to support folders and deflate compression.
106 ** ZIPFILE_NEWENTRY_FLAGS:
107 ** Value for "general-purpose-bit-flags" field of new entries. Bit
108 ** 11 means "utf-8 filename and comment".
110 ** ZIPFILE_SIGNATURE_CDS:
111 ** First 4 bytes of a valid CDS record.
113 ** ZIPFILE_SIGNATURE_LFH:
114 ** First 4 bytes of a valid LFH record.
116 ** ZIPFILE_SIGNATURE_EOCD
117 ** First 4 bytes of a valid EOCD record.
119 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
120 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
121 #define ZIPFILE_NEWENTRY_REQUIRED 20
122 #define ZIPFILE_NEWENTRY_FLAGS 0x800
123 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
124 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
125 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
128 ** The sizes of the fixed-size part of each of the three main data
129 ** structures in a zip archive.
131 #define ZIPFILE_LFH_FIXED_SZ 30
132 #define ZIPFILE_EOCD_FIXED_SZ 22
133 #define ZIPFILE_CDS_FIXED_SZ 46
136 *** 4.3.16 End of central directory record:
138 *** end of central dir signature 4 bytes (0x06054b50)
139 *** number of this disk 2 bytes
140 *** number of the disk with the
141 *** start of the central directory 2 bytes
142 *** total number of entries in the
143 *** central directory on this disk 2 bytes
144 *** total number of entries in
145 *** the central directory 2 bytes
146 *** size of the central directory 4 bytes
147 *** offset of start of central
148 *** directory with respect to
149 *** the starting disk number 4 bytes
150 *** .ZIP file comment length 2 bytes
151 *** .ZIP file comment (variable size)
153 typedef struct ZipfileEOCD ZipfileEOCD;
154 struct ZipfileEOCD {
155 u16 iDisk;
156 u16 iFirstDisk;
157 u16 nEntry;
158 u16 nEntryTotal;
159 u32 nSize;
160 u32 iOffset;
164 *** 4.3.12 Central directory structure:
166 *** ...
168 *** central file header signature 4 bytes (0x02014b50)
169 *** version made by 2 bytes
170 *** version needed to extract 2 bytes
171 *** general purpose bit flag 2 bytes
172 *** compression method 2 bytes
173 *** last mod file time 2 bytes
174 *** last mod file date 2 bytes
175 *** crc-32 4 bytes
176 *** compressed size 4 bytes
177 *** uncompressed size 4 bytes
178 *** file name length 2 bytes
179 *** extra field length 2 bytes
180 *** file comment length 2 bytes
181 *** disk number start 2 bytes
182 *** internal file attributes 2 bytes
183 *** external file attributes 4 bytes
184 *** relative offset of local header 4 bytes
186 typedef struct ZipfileCDS ZipfileCDS;
187 struct ZipfileCDS {
188 u16 iVersionMadeBy;
189 u16 iVersionExtract;
190 u16 flags;
191 u16 iCompression;
192 u16 mTime;
193 u16 mDate;
194 u32 crc32;
195 u32 szCompressed;
196 u32 szUncompressed;
197 u16 nFile;
198 u16 nExtra;
199 u16 nComment;
200 u16 iDiskStart;
201 u16 iInternalAttr;
202 u32 iExternalAttr;
203 u32 iOffset;
204 char *zFile; /* Filename (sqlite3_malloc()) */
208 *** 4.3.7 Local file header:
210 *** local file header signature 4 bytes (0x04034b50)
211 *** version needed to extract 2 bytes
212 *** general purpose bit flag 2 bytes
213 *** compression method 2 bytes
214 *** last mod file time 2 bytes
215 *** last mod file date 2 bytes
216 *** crc-32 4 bytes
217 *** compressed size 4 bytes
218 *** uncompressed size 4 bytes
219 *** file name length 2 bytes
220 *** extra field length 2 bytes
221 ***
223 typedef struct ZipfileLFH ZipfileLFH;
224 struct ZipfileLFH {
225 u16 iVersionExtract;
226 u16 flags;
227 u16 iCompression;
228 u16 mTime;
229 u16 mDate;
230 u32 crc32;
231 u32 szCompressed;
232 u32 szUncompressed;
233 u16 nFile;
234 u16 nExtra;
237 typedef struct ZipfileEntry ZipfileEntry;
238 struct ZipfileEntry {
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 */
244 ZipfileEntry *pNext; /* Next element in in-memory CDS */
248 ** Cursor type for zipfile tables.
250 typedef struct ZipfileCsr ZipfileCsr;
251 struct ZipfileCsr {
252 sqlite3_vtab_cursor base; /* Base class - must be first */
253 i64 iId; /* Cursor ID */
254 u8 bEof; /* True when at EOF */
255 u8 bNoop; /* If next xNext() call is no-op */
257 /* Used outside of write transactions */
258 FILE *pFile; /* Zip file */
259 i64 iNextOff; /* Offset of next record in central directory */
260 ZipfileEOCD eocd; /* Parse of central directory record */
262 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
263 ZipfileEntry *pCurrent; /* Current entry */
264 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
267 typedef struct ZipfileTab ZipfileTab;
268 struct ZipfileTab {
269 sqlite3_vtab base; /* Base class - must be first */
270 char *zFile; /* Zip file this table accesses (may be NULL) */
271 u8 *aBuffer; /* Temporary buffer used for various tasks */
273 ZipfileCsr *pCsrList; /* List of cursors */
274 i64 iNextCsrid;
276 /* The following are used by write transactions only */
277 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
278 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
279 FILE *pWriteFd; /* File handle open on zip archive */
280 i64 szCurrent; /* Current size of zip archive */
281 i64 szOrig; /* Size of archive at start of transaction */
285 ** Set the error message contained in context ctx to the results of
286 ** vprintf(zFmt, ...).
288 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
289 char *zMsg = 0;
290 va_list ap;
291 va_start(ap, zFmt);
292 zMsg = sqlite3_vmprintf(zFmt, ap);
293 sqlite3_result_error(ctx, zMsg, -1);
294 sqlite3_free(zMsg);
295 va_end(ap);
299 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
300 ** is not quoted, do nothing.
302 static void zipfileDequote(char *zIn){
303 char q = zIn[0];
304 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
305 int iIn = 1;
306 int iOut = 0;
307 if( q=='[' ) q = ']';
308 while( ALWAYS(zIn[iIn]) ){
309 char c = zIn[iIn++];
310 if( c==q && zIn[iIn++]!=q ) break;
311 zIn[iOut++] = c;
313 zIn[iOut] = '\0';
318 ** Construct a new ZipfileTab virtual table object.
320 ** argv[0] -> module name ("zipfile")
321 ** argv[1] -> database name
322 ** argv[2] -> table name
323 ** argv[...] -> "column name" and other module argument fields.
325 static int zipfileConnect(
326 sqlite3 *db,
327 void *pAux,
328 int argc, const char *const*argv,
329 sqlite3_vtab **ppVtab,
330 char **pzErr
332 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
333 int nFile = 0;
334 const char *zFile = 0;
335 ZipfileTab *pNew = 0;
336 int rc;
338 /* If the table name is not "zipfile", require that the argument be
339 ** specified. This stops zipfile tables from being created as:
341 ** CREATE VIRTUAL TABLE zzz USING zipfile();
343 ** It does not prevent:
345 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
347 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
348 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
349 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
350 return SQLITE_ERROR;
353 if( argc>3 ){
354 zFile = argv[3];
355 nFile = (int)strlen(zFile)+1;
358 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
359 if( rc==SQLITE_OK ){
360 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
361 if( pNew==0 ) return SQLITE_NOMEM;
362 memset(pNew, 0, nByte+nFile);
363 pNew->aBuffer = (u8*)&pNew[1];
364 if( zFile ){
365 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
366 memcpy(pNew->zFile, zFile, nFile);
367 zipfileDequote(pNew->zFile);
370 *ppVtab = (sqlite3_vtab*)pNew;
371 return rc;
375 ** Free the ZipfileEntry structure indicated by the only argument.
377 static void zipfileEntryFree(ZipfileEntry *p){
378 if( p ){
379 sqlite3_free(p->cds.zFile);
380 sqlite3_free(p);
385 ** Release resources that should be freed at the end of a write
386 ** transaction.
388 static void zipfileCleanupTransaction(ZipfileTab *pTab){
389 ZipfileEntry *pEntry;
390 ZipfileEntry *pNext;
392 if( pTab->pWriteFd ){
393 fclose(pTab->pWriteFd);
394 pTab->pWriteFd = 0;
396 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
397 pNext = pEntry->pNext;
398 zipfileEntryFree(pEntry);
400 pTab->pFirstEntry = 0;
401 pTab->pLastEntry = 0;
402 pTab->szCurrent = 0;
403 pTab->szOrig = 0;
407 ** This method is the destructor for zipfile vtab objects.
409 static int zipfileDisconnect(sqlite3_vtab *pVtab){
410 zipfileCleanupTransaction((ZipfileTab*)pVtab);
411 sqlite3_free(pVtab);
412 return SQLITE_OK;
416 ** Constructor for a new ZipfileCsr object.
418 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
419 ZipfileTab *pTab = (ZipfileTab*)p;
420 ZipfileCsr *pCsr;
421 pCsr = sqlite3_malloc(sizeof(*pCsr));
422 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
423 if( pCsr==0 ){
424 return SQLITE_NOMEM;
426 memset(pCsr, 0, sizeof(*pCsr));
427 pCsr->iId = ++pTab->iNextCsrid;
428 pCsr->pCsrNext = pTab->pCsrList;
429 pTab->pCsrList = pCsr;
430 return SQLITE_OK;
434 ** Reset a cursor back to the state it was in when first returned
435 ** by zipfileOpen().
437 static void zipfileResetCursor(ZipfileCsr *pCsr){
438 ZipfileEntry *p;
439 ZipfileEntry *pNext;
441 pCsr->bEof = 0;
442 if( pCsr->pFile ){
443 fclose(pCsr->pFile);
444 pCsr->pFile = 0;
445 zipfileEntryFree(pCsr->pCurrent);
446 pCsr->pCurrent = 0;
449 for(p=pCsr->pFreeEntry; p; p=pNext){
450 pNext = p->pNext;
451 zipfileEntryFree(p);
456 ** Destructor for an ZipfileCsr.
458 static int zipfileClose(sqlite3_vtab_cursor *cur){
459 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
460 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
461 ZipfileCsr **pp;
462 zipfileResetCursor(pCsr);
464 /* Remove this cursor from the ZipfileTab.pCsrList list. */
465 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
466 *pp = pCsr->pCsrNext;
468 sqlite3_free(pCsr);
469 return SQLITE_OK;
473 ** Set the error message for the virtual table associated with cursor
474 ** pCsr to the results of vprintf(zFmt, ...).
476 static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
477 va_list ap;
478 va_start(ap, zFmt);
479 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
480 va_end(ap);
484 ** Read nRead bytes of data from offset iOff of file pFile into buffer
485 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
486 ** otherwise.
488 ** If an error does occur, output variable (*pzErrmsg) may be set to point
489 ** to an English language error message. It is the responsibility of the
490 ** caller to eventually free this buffer using
491 ** sqlite3_free().
493 static int zipfileReadData(
494 FILE *pFile, /* Read from this file */
495 u8 *aRead, /* Read into this buffer */
496 int nRead, /* Number of bytes to read */
497 i64 iOff, /* Offset to read from */
498 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
500 size_t n;
501 fseek(pFile, (long)iOff, SEEK_SET);
502 n = fread(aRead, 1, nRead, pFile);
503 if( (int)n!=nRead ){
504 *pzErrmsg = sqlite3_mprintf("error in fread()");
505 return SQLITE_ERROR;
507 return SQLITE_OK;
510 static int zipfileAppendData(
511 ZipfileTab *pTab,
512 const u8 *aWrite,
513 int nWrite
515 size_t n;
516 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
517 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
518 if( (int)n!=nWrite ){
519 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
520 return SQLITE_ERROR;
522 pTab->szCurrent += nWrite;
523 return SQLITE_OK;
527 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
529 static u16 zipfileGetU16(const u8 *aBuf){
530 return (aBuf[1] << 8) + aBuf[0];
534 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
536 static u32 zipfileGetU32(const u8 *aBuf){
537 return ((u32)(aBuf[3]) << 24)
538 + ((u32)(aBuf[2]) << 16)
539 + ((u32)(aBuf[1]) << 8)
540 + ((u32)(aBuf[0]) << 0);
544 ** Write a 16-bit little endiate integer into buffer aBuf.
546 static void zipfilePutU16(u8 *aBuf, u16 val){
547 aBuf[0] = val & 0xFF;
548 aBuf[1] = (val>>8) & 0xFF;
552 ** Write a 32-bit little endiate integer into buffer aBuf.
554 static void zipfilePutU32(u8 *aBuf, u32 val){
555 aBuf[0] = val & 0xFF;
556 aBuf[1] = (val>>8) & 0xFF;
557 aBuf[2] = (val>>16) & 0xFF;
558 aBuf[3] = (val>>24) & 0xFF;
561 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
562 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
564 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
565 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
568 ** Magic numbers used to read CDS records.
570 #define ZIPFILE_CDS_NFILE_OFF 28
571 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
574 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
575 ** if the record is not well-formed, or SQLITE_OK otherwise.
577 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
578 u8 *aRead = aBuf;
579 u32 sig = zipfileRead32(aRead);
580 int rc = SQLITE_OK;
581 if( sig!=ZIPFILE_SIGNATURE_CDS ){
582 rc = SQLITE_ERROR;
583 }else{
584 pCDS->iVersionMadeBy = zipfileRead16(aRead);
585 pCDS->iVersionExtract = zipfileRead16(aRead);
586 pCDS->flags = zipfileRead16(aRead);
587 pCDS->iCompression = zipfileRead16(aRead);
588 pCDS->mTime = zipfileRead16(aRead);
589 pCDS->mDate = zipfileRead16(aRead);
590 pCDS->crc32 = zipfileRead32(aRead);
591 pCDS->szCompressed = zipfileRead32(aRead);
592 pCDS->szUncompressed = zipfileRead32(aRead);
593 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
594 pCDS->nFile = zipfileRead16(aRead);
595 pCDS->nExtra = zipfileRead16(aRead);
596 pCDS->nComment = zipfileRead16(aRead);
597 pCDS->iDiskStart = zipfileRead16(aRead);
598 pCDS->iInternalAttr = zipfileRead16(aRead);
599 pCDS->iExternalAttr = zipfileRead32(aRead);
600 pCDS->iOffset = zipfileRead32(aRead);
601 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
604 return rc;
608 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
609 ** if the record is not well-formed, or SQLITE_OK otherwise.
611 static int zipfileReadLFH(
612 u8 *aBuffer,
613 ZipfileLFH *pLFH
615 u8 *aRead = aBuffer;
616 int rc = SQLITE_OK;
618 u32 sig = zipfileRead32(aRead);
619 if( sig!=ZIPFILE_SIGNATURE_LFH ){
620 rc = SQLITE_ERROR;
621 }else{
622 pLFH->iVersionExtract = zipfileRead16(aRead);
623 pLFH->flags = zipfileRead16(aRead);
624 pLFH->iCompression = zipfileRead16(aRead);
625 pLFH->mTime = zipfileRead16(aRead);
626 pLFH->mDate = zipfileRead16(aRead);
627 pLFH->crc32 = zipfileRead32(aRead);
628 pLFH->szCompressed = zipfileRead32(aRead);
629 pLFH->szUncompressed = zipfileRead32(aRead);
630 pLFH->nFile = zipfileRead16(aRead);
631 pLFH->nExtra = zipfileRead16(aRead);
633 return rc;
638 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
639 ** Scan through this buffer to find an "extra-timestamp" field. If one
640 ** exists, extract the 32-bit modification-timestamp from it and store
641 ** the value in output parameter *pmTime.
643 ** Zero is returned if no extra-timestamp record could be found (and so
644 ** *pmTime is left unchanged), or non-zero otherwise.
646 ** The general format of an extra field is:
648 ** Header ID 2 bytes
649 ** Data Size 2 bytes
650 ** Data N bytes
652 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
653 int ret = 0;
654 u8 *p = aExtra;
655 u8 *pEnd = &aExtra[nExtra];
657 while( p<pEnd ){
658 u16 id = zipfileRead16(p);
659 u16 nByte = zipfileRead16(p);
661 switch( id ){
662 case ZIPFILE_EXTRA_TIMESTAMP: {
663 u8 b = p[0];
664 if( b & 0x01 ){ /* 0x01 -> modtime is present */
665 *pmTime = zipfileGetU32(&p[1]);
666 ret = 1;
668 break;
672 p += nByte;
674 return ret;
678 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
679 ** fields of the CDS structure passed as the only argument to a 32-bit
680 ** UNIX seconds-since-the-epoch timestamp. Return the result.
682 ** "Standard" MS-DOS time format:
684 ** File modification time:
685 ** Bits 00-04: seconds divided by 2
686 ** Bits 05-10: minute
687 ** Bits 11-15: hour
688 ** File modification date:
689 ** Bits 00-04: day
690 ** Bits 05-08: month (1-12)
691 ** Bits 09-15: years from 1980
693 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
695 static u32 zipfileMtime(ZipfileCDS *pCDS){
696 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
697 int M = ((pCDS->mDate >> 5) & 0x0F);
698 int D = (pCDS->mDate & 0x1F);
699 int B = -13;
701 int sec = (pCDS->mTime & 0x1F)*2;
702 int min = (pCDS->mTime >> 5) & 0x3F;
703 int hr = (pCDS->mTime >> 11) & 0x1F;
704 i64 JD;
706 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
708 /* Calculate the JD in seconds for noon on the day in question */
709 if( M<3 ){
710 Y = Y-1;
711 M = M+12;
713 JD = (i64)(24*60*60) * (
714 (int)(365.25 * (Y + 4716))
715 + (int)(30.6001 * (M + 1))
716 + D + B - 1524
719 /* Correct the JD for the time within the day */
720 JD += (hr-12) * 3600 + min * 60 + sec;
722 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
723 return (u32)(JD - (i64)(24405875) * 24*60*6);
727 ** The opposite of zipfileMtime(). This function populates the mTime and
728 ** mDate fields of the CDS structure passed as the first argument according
729 ** to the UNIX timestamp value passed as the second.
731 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
732 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
733 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
735 int A, B, C, D, E;
736 int yr, mon, day;
737 int hr, min, sec;
739 A = (int)((JD - 1867216.25)/36524.25);
740 A = JD + 1 + A - (A/4);
741 B = A + 1524;
742 C = (int)((B - 122.1)/365.25);
743 D = (36525*(C&32767))/100;
744 E = (int)((B-D)/30.6001);
746 day = B - D - (int)(30.6001*E);
747 mon = (E<14 ? E-1 : E-13);
748 yr = mon>2 ? C-4716 : C-4715;
750 hr = (mUnixTime % (24*60*60)) / (60*60);
751 min = (mUnixTime % (60*60)) / 60;
752 sec = (mUnixTime % 60);
754 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
755 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
757 assert( mUnixTime<315507600
758 || mUnixTime==zipfileMtime(pCds)
759 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
760 /* || (mUnixTime % 2) */
765 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
766 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
767 ** then pFile is a file-handle open on a zip file. In either case, this
768 ** function creates a ZipfileEntry object based on the zip archive entry
769 ** for which the CDS record is at offset iOff.
771 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
772 ** the new object. Otherwise, an SQLite error code is returned and the
773 ** final value of (*ppEntry) undefined.
775 static int zipfileGetEntry(
776 ZipfileTab *pTab, /* Store any error message here */
777 const u8 *aBlob, /* Pointer to in-memory file image */
778 int nBlob, /* Size of aBlob[] in bytes */
779 FILE *pFile, /* If aBlob==0, read from this file */
780 i64 iOff, /* Offset of CDS record */
781 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
783 u8 *aRead;
784 char **pzErr = &pTab->base.zErrMsg;
785 int rc = SQLITE_OK;
787 if( aBlob==0 ){
788 aRead = pTab->aBuffer;
789 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
790 }else{
791 aRead = (u8*)&aBlob[iOff];
794 if( rc==SQLITE_OK ){
795 int nAlloc;
796 ZipfileEntry *pNew;
798 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
799 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
800 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
802 nAlloc = sizeof(ZipfileEntry) + nExtra;
803 if( aBlob ){
804 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
807 pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
808 if( pNew==0 ){
809 rc = SQLITE_NOMEM;
810 }else{
811 memset(pNew, 0, sizeof(ZipfileEntry));
812 rc = zipfileReadCDS(aRead, &pNew->cds);
813 if( rc!=SQLITE_OK ){
814 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
815 }else if( aBlob==0 ){
816 rc = zipfileReadData(
817 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
819 }else{
820 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
824 if( rc==SQLITE_OK ){
825 u32 *pt = &pNew->mUnixTime;
826 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
827 pNew->aExtra = (u8*)&pNew[1];
828 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
829 if( pNew->cds.zFile==0 ){
830 rc = SQLITE_NOMEM;
831 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
832 pNew->mUnixTime = zipfileMtime(&pNew->cds);
836 if( rc==SQLITE_OK ){
837 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
838 ZipfileLFH lfh;
839 if( pFile ){
840 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
841 }else{
842 aRead = (u8*)&aBlob[pNew->cds.iOffset];
845 rc = zipfileReadLFH(aRead, &lfh);
846 if( rc==SQLITE_OK ){
847 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
848 pNew->iDataOff += lfh.nFile + lfh.nExtra;
849 if( aBlob && pNew->cds.szCompressed ){
850 pNew->aData = &pNew->aExtra[nExtra];
851 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
853 }else{
854 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
855 (int)pNew->cds.iOffset
860 if( rc!=SQLITE_OK ){
861 zipfileEntryFree(pNew);
862 }else{
863 *ppEntry = pNew;
867 return rc;
871 ** Advance an ZipfileCsr to its next row of output.
873 static int zipfileNext(sqlite3_vtab_cursor *cur){
874 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
875 int rc = SQLITE_OK;
877 if( pCsr->pFile ){
878 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
879 zipfileEntryFree(pCsr->pCurrent);
880 pCsr->pCurrent = 0;
881 if( pCsr->iNextOff>=iEof ){
882 pCsr->bEof = 1;
883 }else{
884 ZipfileEntry *p = 0;
885 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
886 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
887 if( rc==SQLITE_OK ){
888 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
889 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
891 pCsr->pCurrent = p;
893 }else{
894 if( !pCsr->bNoop ){
895 pCsr->pCurrent = pCsr->pCurrent->pNext;
897 if( pCsr->pCurrent==0 ){
898 pCsr->bEof = 1;
902 pCsr->bNoop = 0;
903 return rc;
906 static void zipfileFree(void *p) {
907 sqlite3_free(p);
911 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
912 ** size is nOut bytes. This function uncompresses the data and sets the
913 ** return value in context pCtx to the result (a blob).
915 ** If an error occurs, an error code is left in pCtx instead.
917 static void zipfileInflate(
918 sqlite3_context *pCtx, /* Store result here */
919 const u8 *aIn, /* Compressed data */
920 int nIn, /* Size of buffer aIn[] in bytes */
921 int nOut /* Expected output size */
923 u8 *aRes = sqlite3_malloc(nOut);
924 if( aRes==0 ){
925 sqlite3_result_error_nomem(pCtx);
926 }else{
927 int err;
928 z_stream str;
929 memset(&str, 0, sizeof(str));
931 str.next_in = (Byte*)aIn;
932 str.avail_in = nIn;
933 str.next_out = (Byte*)aRes;
934 str.avail_out = nOut;
936 err = inflateInit2(&str, -15);
937 if( err!=Z_OK ){
938 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
939 }else{
940 err = inflate(&str, Z_NO_FLUSH);
941 if( err!=Z_STREAM_END ){
942 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
943 }else{
944 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
945 aRes = 0;
948 sqlite3_free(aRes);
949 inflateEnd(&str);
954 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
955 ** compresses it and sets (*ppOut) to point to a buffer containing the
956 ** compressed data. The caller is responsible for eventually calling
957 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
958 ** is set to the size of buffer (*ppOut) in bytes.
960 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
961 ** code is returned and an error message left in virtual-table handle
962 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
963 ** case.
965 static int zipfileDeflate(
966 const u8 *aIn, int nIn, /* Input */
967 u8 **ppOut, int *pnOut, /* Output */
968 char **pzErr /* OUT: Error message */
970 int nAlloc = (int)compressBound(nIn);
971 u8 *aOut;
972 int rc = SQLITE_OK;
974 aOut = (u8*)sqlite3_malloc(nAlloc);
975 if( aOut==0 ){
976 rc = SQLITE_NOMEM;
977 }else{
978 int res;
979 z_stream str;
980 memset(&str, 0, sizeof(str));
981 str.next_in = (Bytef*)aIn;
982 str.avail_in = nIn;
983 str.next_out = aOut;
984 str.avail_out = nAlloc;
986 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
987 res = deflate(&str, Z_FINISH);
989 if( res==Z_STREAM_END ){
990 *ppOut = aOut;
991 *pnOut = (int)str.total_out;
992 }else{
993 sqlite3_free(aOut);
994 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
995 rc = SQLITE_ERROR;
997 deflateEnd(&str);
1000 return rc;
1005 ** Return values of columns for the row at which the series_cursor
1006 ** is currently pointing.
1008 static int zipfileColumn(
1009 sqlite3_vtab_cursor *cur, /* The cursor */
1010 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1011 int i /* Which column to return */
1013 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
1014 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
1015 int rc = SQLITE_OK;
1016 switch( i ){
1017 case 0: /* name */
1018 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
1019 break;
1020 case 1: /* mode */
1021 /* TODO: Whether or not the following is correct surely depends on
1022 ** the platform on which the archive was created. */
1023 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
1024 break;
1025 case 2: { /* mtime */
1026 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
1027 break;
1029 case 3: { /* sz */
1030 if( sqlite3_vtab_nochange(ctx)==0 ){
1031 sqlite3_result_int64(ctx, pCDS->szUncompressed);
1033 break;
1035 case 4: /* rawdata */
1036 if( sqlite3_vtab_nochange(ctx) ) break;
1037 case 5: { /* data */
1038 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
1039 int sz = pCDS->szCompressed;
1040 int szFinal = pCDS->szUncompressed;
1041 if( szFinal>0 ){
1042 u8 *aBuf;
1043 u8 *aFree = 0;
1044 if( pCsr->pCurrent->aData ){
1045 aBuf = pCsr->pCurrent->aData;
1046 }else{
1047 aBuf = aFree = sqlite3_malloc(sz);
1048 if( aBuf==0 ){
1049 rc = SQLITE_NOMEM;
1050 }else{
1051 FILE *pFile = pCsr->pFile;
1052 if( pFile==0 ){
1053 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
1055 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
1056 &pCsr->base.pVtab->zErrMsg
1060 if( rc==SQLITE_OK ){
1061 if( i==5 && pCDS->iCompression ){
1062 zipfileInflate(ctx, aBuf, sz, szFinal);
1063 }else{
1064 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
1067 sqlite3_free(aFree);
1068 }else{
1069 /* Figure out if this is a directory or a zero-sized file. Consider
1070 ** it to be a directory either if the mode suggests so, or if
1071 ** the final character in the name is '/'. */
1072 u32 mode = pCDS->iExternalAttr >> 16;
1073 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
1074 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
1078 break;
1080 case 6: /* method */
1081 sqlite3_result_int(ctx, pCDS->iCompression);
1082 break;
1083 default: /* z */
1084 assert( i==7 );
1085 sqlite3_result_int64(ctx, pCsr->iId);
1086 break;
1089 return rc;
1093 ** Return TRUE if the cursor is at EOF.
1095 static int zipfileEof(sqlite3_vtab_cursor *cur){
1096 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
1097 return pCsr->bEof;
1101 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
1102 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
1103 ** is guaranteed to be a file-handle open on a zip file.
1105 ** This function attempts to locate the EOCD record within the zip archive
1106 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
1107 ** returned if successful. Otherwise, an SQLite error code is returned and
1108 ** an English language error message may be left in virtual-table pTab.
1110 static int zipfileReadEOCD(
1111 ZipfileTab *pTab, /* Return errors here */
1112 const u8 *aBlob, /* Pointer to in-memory file image */
1113 int nBlob, /* Size of aBlob[] in bytes */
1114 FILE *pFile, /* Read from this file if aBlob==0 */
1115 ZipfileEOCD *pEOCD /* Object to populate */
1117 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
1118 int nRead; /* Bytes to read from file */
1119 int rc = SQLITE_OK;
1121 if( aBlob==0 ){
1122 i64 iOff; /* Offset to read from */
1123 i64 szFile; /* Total size of file in bytes */
1124 fseek(pFile, 0, SEEK_END);
1125 szFile = (i64)ftell(pFile);
1126 if( szFile==0 ){
1127 memset(pEOCD, 0, sizeof(ZipfileEOCD));
1128 return SQLITE_OK;
1130 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
1131 iOff = szFile - nRead;
1132 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
1133 }else{
1134 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
1135 aRead = (u8*)&aBlob[nBlob-nRead];
1138 if( rc==SQLITE_OK ){
1139 int i;
1141 /* Scan backwards looking for the signature bytes */
1142 for(i=nRead-20; i>=0; i--){
1143 if( aRead[i]==0x50 && aRead[i+1]==0x4b
1144 && aRead[i+2]==0x05 && aRead[i+3]==0x06
1146 break;
1149 if( i<0 ){
1150 pTab->base.zErrMsg = sqlite3_mprintf(
1151 "cannot find end of central directory record"
1153 return SQLITE_ERROR;
1156 aRead += i+4;
1157 pEOCD->iDisk = zipfileRead16(aRead);
1158 pEOCD->iFirstDisk = zipfileRead16(aRead);
1159 pEOCD->nEntry = zipfileRead16(aRead);
1160 pEOCD->nEntryTotal = zipfileRead16(aRead);
1161 pEOCD->nSize = zipfileRead32(aRead);
1162 pEOCD->iOffset = zipfileRead32(aRead);
1165 return rc;
1169 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
1170 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
1171 ** to the end of the list. Otherwise, it is added to the list immediately
1172 ** before pBefore (which is guaranteed to be a part of said list).
1174 static void zipfileAddEntry(
1175 ZipfileTab *pTab,
1176 ZipfileEntry *pBefore,
1177 ZipfileEntry *pNew
1179 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
1180 assert( pNew->pNext==0 );
1181 if( pBefore==0 ){
1182 if( pTab->pFirstEntry==0 ){
1183 pTab->pFirstEntry = pTab->pLastEntry = pNew;
1184 }else{
1185 assert( pTab->pLastEntry->pNext==0 );
1186 pTab->pLastEntry->pNext = pNew;
1187 pTab->pLastEntry = pNew;
1189 }else{
1190 ZipfileEntry **pp;
1191 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
1192 pNew->pNext = pBefore;
1193 *pp = pNew;
1197 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
1198 ZipfileEOCD eocd;
1199 int rc;
1200 int i;
1201 i64 iOff;
1203 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
1204 iOff = eocd.iOffset;
1205 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
1206 ZipfileEntry *pNew = 0;
1207 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
1209 if( rc==SQLITE_OK ){
1210 zipfileAddEntry(pTab, 0, pNew);
1211 iOff += ZIPFILE_CDS_FIXED_SZ;
1212 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
1215 return rc;
1219 ** xFilter callback.
1221 static int zipfileFilter(
1222 sqlite3_vtab_cursor *cur,
1223 int idxNum, const char *idxStr,
1224 int argc, sqlite3_value **argv
1226 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
1227 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
1228 const char *zFile = 0; /* Zip file to scan */
1229 int rc = SQLITE_OK; /* Return Code */
1230 int bInMemory = 0; /* True for an in-memory zipfile */
1232 zipfileResetCursor(pCsr);
1234 if( pTab->zFile ){
1235 zFile = pTab->zFile;
1236 }else if( idxNum==0 ){
1237 zipfileSetErrmsg(pCsr, "zipfile() function requires an argument");
1238 return SQLITE_ERROR;
1239 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
1240 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
1241 int nBlob = sqlite3_value_bytes(argv[0]);
1242 assert( pTab->pFirstEntry==0 );
1243 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
1244 pCsr->pFreeEntry = pTab->pFirstEntry;
1245 pTab->pFirstEntry = pTab->pLastEntry = 0;
1246 if( rc!=SQLITE_OK ) return rc;
1247 bInMemory = 1;
1248 }else{
1249 zFile = (const char*)sqlite3_value_text(argv[0]);
1252 if( 0==pTab->pWriteFd && 0==bInMemory ){
1253 pCsr->pFile = fopen(zFile, "rb");
1254 if( pCsr->pFile==0 ){
1255 zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
1256 rc = SQLITE_ERROR;
1257 }else{
1258 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
1259 if( rc==SQLITE_OK ){
1260 if( pCsr->eocd.nEntry==0 ){
1261 pCsr->bEof = 1;
1262 }else{
1263 pCsr->iNextOff = pCsr->eocd.iOffset;
1264 rc = zipfileNext(cur);
1268 }else{
1269 pCsr->bNoop = 1;
1270 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
1271 rc = zipfileNext(cur);
1274 return rc;
1278 ** xBestIndex callback.
1280 static int zipfileBestIndex(
1281 sqlite3_vtab *tab,
1282 sqlite3_index_info *pIdxInfo
1284 int i;
1286 for(i=0; i<pIdxInfo->nConstraint; i++){
1287 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
1288 if( pCons->usable==0 ) continue;
1289 if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1290 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
1291 break;
1294 if( i<pIdxInfo->nConstraint ){
1295 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
1296 pIdxInfo->aConstraintUsage[i].omit = 1;
1297 pIdxInfo->estimatedCost = 1000.0;
1298 pIdxInfo->idxNum = 1;
1299 }else{
1300 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
1301 pIdxInfo->idxNum = 0;
1304 return SQLITE_OK;
1307 static ZipfileEntry *zipfileNewEntry(const char *zPath){
1308 ZipfileEntry *pNew;
1309 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
1310 if( pNew ){
1311 memset(pNew, 0, sizeof(ZipfileEntry));
1312 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
1313 if( pNew->cds.zFile==0 ){
1314 sqlite3_free(pNew);
1315 pNew = 0;
1318 return pNew;
1321 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
1322 ZipfileCDS *pCds = &pEntry->cds;
1323 u8 *a = aBuf;
1325 pCds->nExtra = 9;
1327 /* Write the LFH itself */
1328 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
1329 zipfileWrite16(a, pCds->iVersionExtract);
1330 zipfileWrite16(a, pCds->flags);
1331 zipfileWrite16(a, pCds->iCompression);
1332 zipfileWrite16(a, pCds->mTime);
1333 zipfileWrite16(a, pCds->mDate);
1334 zipfileWrite32(a, pCds->crc32);
1335 zipfileWrite32(a, pCds->szCompressed);
1336 zipfileWrite32(a, pCds->szUncompressed);
1337 zipfileWrite16(a, (u16)pCds->nFile);
1338 zipfileWrite16(a, pCds->nExtra);
1339 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
1341 /* Add the file name */
1342 memcpy(a, pCds->zFile, (int)pCds->nFile);
1343 a += (int)pCds->nFile;
1345 /* The "extra" data */
1346 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
1347 zipfileWrite16(a, 5);
1348 *a++ = 0x01;
1349 zipfileWrite32(a, pEntry->mUnixTime);
1351 return a-aBuf;
1354 static int zipfileAppendEntry(
1355 ZipfileTab *pTab,
1356 ZipfileEntry *pEntry,
1357 const u8 *pData,
1358 int nData
1360 u8 *aBuf = pTab->aBuffer;
1361 int nBuf;
1362 int rc;
1364 nBuf = zipfileSerializeLFH(pEntry, aBuf);
1365 rc = zipfileAppendData(pTab, aBuf, nBuf);
1366 if( rc==SQLITE_OK ){
1367 pEntry->iDataOff = pTab->szCurrent;
1368 rc = zipfileAppendData(pTab, pData, nData);
1371 return rc;
1374 static int zipfileGetMode(
1375 sqlite3_value *pVal,
1376 int bIsDir, /* If true, default to directory */
1377 u32 *pMode, /* OUT: Mode value */
1378 char **pzErr /* OUT: Error message */
1380 const char *z = (const char*)sqlite3_value_text(pVal);
1381 u32 mode = 0;
1382 if( z==0 ){
1383 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
1384 }else if( z[0]>='0' && z[0]<='9' ){
1385 mode = (unsigned int)sqlite3_value_int(pVal);
1386 }else{
1387 const char zTemplate[11] = "-rwxrwxrwx";
1388 int i;
1389 if( strlen(z)!=10 ) goto parse_error;
1390 switch( z[0] ){
1391 case '-': mode |= S_IFREG; break;
1392 case 'd': mode |= S_IFDIR; break;
1393 #if !defined(_WIN32) && !defined(WIN32)
1394 case 'l': mode |= S_IFLNK; break;
1395 #endif
1396 default: goto parse_error;
1398 for(i=1; i<10; i++){
1399 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
1400 else if( z[i]!='-' ) goto parse_error;
1403 if( (bIsDir == ((mode & S_IFDIR)==0)) ){
1404 /* The "mode" attribute is a directory, but data has been specified.
1405 ** Or vice-versa - no data but "mode" is a file or symlink. */
1406 return SQLITE_CONSTRAINT;
1408 *pMode = mode;
1409 return SQLITE_OK;
1411 parse_error:
1412 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
1413 return SQLITE_ERROR;
1417 ** Both (const char*) arguments point to nul-terminated strings. Argument
1418 ** nB is the value of strlen(zB). This function returns 0 if the strings are
1419 ** identical, ignoring any trailing '/' character in either path. */
1420 static int zipfileComparePath(const char *zA, const char *zB, int nB){
1421 int nA = (int)strlen(zA);
1422 if( zA[nA-1]=='/' ) nA--;
1423 if( zB[nB-1]=='/' ) nB--;
1424 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
1425 return 1;
1428 static int zipfileBegin(sqlite3_vtab *pVtab){
1429 ZipfileTab *pTab = (ZipfileTab*)pVtab;
1430 int rc = SQLITE_OK;
1432 assert( pTab->pWriteFd==0 );
1434 /* Open a write fd on the file. Also load the entire central directory
1435 ** structure into memory. During the transaction any new file data is
1436 ** appended to the archive file, but the central directory is accumulated
1437 ** in main-memory until the transaction is committed. */
1438 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
1439 if( pTab->pWriteFd==0 ){
1440 pTab->base.zErrMsg = sqlite3_mprintf(
1441 "zipfile: failed to open file %s for writing", pTab->zFile
1443 rc = SQLITE_ERROR;
1444 }else{
1445 fseek(pTab->pWriteFd, 0, SEEK_END);
1446 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
1447 rc = zipfileLoadDirectory(pTab, 0, 0);
1450 if( rc!=SQLITE_OK ){
1451 zipfileCleanupTransaction(pTab);
1454 return rc;
1458 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
1459 ** time(2)).
1461 static u32 zipfileTime(void){
1462 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
1463 u32 ret;
1464 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
1465 i64 ms;
1466 pVfs->xCurrentTimeInt64(pVfs, &ms);
1467 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
1468 }else{
1469 double day;
1470 pVfs->xCurrentTime(pVfs, &day);
1471 ret = (u32)((day - 2440587.5) * 86400);
1473 return ret;
1477 ** Return a 32-bit timestamp in UNIX epoch format.
1479 ** If the value passed as the only argument is either NULL or an SQL NULL,
1480 ** return the current time. Otherwise, return the value stored in (*pVal)
1481 ** cast to a 32-bit unsigned integer.
1483 static u32 zipfileGetTime(sqlite3_value *pVal){
1484 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
1485 return zipfileTime();
1487 return (u32)sqlite3_value_int64(pVal);
1491 ** xUpdate method.
1493 static int zipfileUpdate(
1494 sqlite3_vtab *pVtab,
1495 int nVal,
1496 sqlite3_value **apVal,
1497 sqlite_int64 *pRowid
1499 ZipfileTab *pTab = (ZipfileTab*)pVtab;
1500 int rc = SQLITE_OK; /* Return Code */
1501 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
1503 u32 mode = 0; /* Mode for new entry */
1504 u32 mTime = 0; /* Modification time for new entry */
1505 i64 sz = 0; /* Uncompressed size */
1506 const char *zPath = 0; /* Path for new entry */
1507 int nPath = 0; /* strlen(zPath) */
1508 const u8 *pData = 0; /* Pointer to buffer containing content */
1509 int nData = 0; /* Size of pData buffer in bytes */
1510 int iMethod = 0; /* Compression method for new entry */
1511 u8 *pFree = 0; /* Free this */
1512 char *zFree = 0; /* Also free this */
1513 ZipfileEntry *pOld = 0;
1514 int bIsDir = 0;
1515 u32 iCrc32 = 0;
1517 if( pTab->pWriteFd==0 ){
1518 rc = zipfileBegin(pVtab);
1519 if( rc!=SQLITE_OK ) return rc;
1522 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
1523 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
1524 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
1525 int nDelete = (int)strlen(zDelete);
1526 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
1527 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
1528 break;
1530 assert( pOld->pNext );
1534 if( nVal>1 ){
1535 /* Check that "sz" and "rawdata" are both NULL: */
1536 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
1537 || sqlite3_value_type(apVal[6])!=SQLITE_NULL
1539 rc = SQLITE_CONSTRAINT;
1542 if( rc==SQLITE_OK ){
1543 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
1544 /* data=NULL. A directory */
1545 bIsDir = 1;
1546 }else{
1547 /* Value specified for "data", and possibly "method". This must be
1548 ** a regular file or a symlink. */
1549 const u8 *aIn = sqlite3_value_blob(apVal[7]);
1550 int nIn = sqlite3_value_bytes(apVal[7]);
1551 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
1553 iMethod = sqlite3_value_int(apVal[8]);
1554 sz = nIn;
1555 pData = aIn;
1556 nData = nIn;
1557 if( iMethod!=0 && iMethod!=8 ){
1558 rc = SQLITE_CONSTRAINT;
1559 }else{
1560 if( bAuto || iMethod ){
1561 int nCmp;
1562 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
1563 if( rc==SQLITE_OK ){
1564 if( iMethod || nCmp<nIn ){
1565 iMethod = 8;
1566 pData = pFree;
1567 nData = nCmp;
1571 iCrc32 = crc32(0, aIn, nIn);
1576 if( rc==SQLITE_OK ){
1577 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
1580 if( rc==SQLITE_OK ){
1581 zPath = (const char*)sqlite3_value_text(apVal[2]);
1582 nPath = (int)strlen(zPath);
1583 mTime = zipfileGetTime(apVal[4]);
1586 if( rc==SQLITE_OK && bIsDir ){
1587 /* For a directory, check that the last character in the path is a
1588 ** '/'. This appears to be required for compatibility with info-zip
1589 ** (the unzip command on unix). It does not create directories
1590 ** otherwise. */
1591 if( zPath[nPath-1]!='/' ){
1592 zFree = sqlite3_mprintf("%s/", zPath);
1593 if( zFree==0 ){ rc = SQLITE_NOMEM; }
1594 zPath = (const char*)zFree;
1595 nPath++;
1599 /* Check that we're not inserting a duplicate entry */
1600 if( pOld==0 && rc==SQLITE_OK ){
1601 ZipfileEntry *p;
1602 for(p=pTab->pFirstEntry; p; p=p->pNext){
1603 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
1604 rc = SQLITE_CONSTRAINT;
1605 break;
1610 if( rc==SQLITE_OK ){
1611 /* Create the new CDS record. */
1612 pNew = zipfileNewEntry(zPath);
1613 if( pNew==0 ){
1614 rc = SQLITE_NOMEM;
1615 }else{
1616 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
1617 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
1618 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
1619 pNew->cds.iCompression = (u16)iMethod;
1620 zipfileMtimeToDos(&pNew->cds, mTime);
1621 pNew->cds.crc32 = iCrc32;
1622 pNew->cds.szCompressed = nData;
1623 pNew->cds.szUncompressed = (u32)sz;
1624 pNew->cds.iExternalAttr = (mode<<16);
1625 pNew->cds.iOffset = (u32)pTab->szCurrent;
1626 pNew->cds.nFile = nPath;
1627 pNew->mUnixTime = (u32)mTime;
1628 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
1629 zipfileAddEntry(pTab, pOld, pNew);
1634 if( rc==SQLITE_OK && pOld ){
1635 ZipfileEntry **pp;
1636 ZipfileCsr *pCsr;
1637 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
1638 if( pCsr->pCurrent==pOld ){
1639 pCsr->pCurrent = pOld->pNext;
1640 pCsr->bNoop = 1;
1643 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
1644 *pp = (*pp)->pNext;
1645 zipfileEntryFree(pOld);
1648 sqlite3_free(pFree);
1649 sqlite3_free(zFree);
1650 return rc;
1653 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
1654 u8 *a = aBuf;
1655 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
1656 zipfileWrite16(a, p->iDisk);
1657 zipfileWrite16(a, p->iFirstDisk);
1658 zipfileWrite16(a, p->nEntry);
1659 zipfileWrite16(a, p->nEntryTotal);
1660 zipfileWrite32(a, p->nSize);
1661 zipfileWrite32(a, p->iOffset);
1662 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
1664 return a-aBuf;
1667 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
1668 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
1669 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
1670 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
1674 ** Serialize the CDS structure into buffer aBuf[]. Return the number
1675 ** of bytes written.
1677 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
1678 u8 *a = aBuf;
1679 ZipfileCDS *pCDS = &pEntry->cds;
1681 if( pEntry->aExtra==0 ){
1682 pCDS->nExtra = 9;
1685 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
1686 zipfileWrite16(a, pCDS->iVersionMadeBy);
1687 zipfileWrite16(a, pCDS->iVersionExtract);
1688 zipfileWrite16(a, pCDS->flags);
1689 zipfileWrite16(a, pCDS->iCompression);
1690 zipfileWrite16(a, pCDS->mTime);
1691 zipfileWrite16(a, pCDS->mDate);
1692 zipfileWrite32(a, pCDS->crc32);
1693 zipfileWrite32(a, pCDS->szCompressed);
1694 zipfileWrite32(a, pCDS->szUncompressed);
1695 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
1696 zipfileWrite16(a, pCDS->nFile);
1697 zipfileWrite16(a, pCDS->nExtra);
1698 zipfileWrite16(a, pCDS->nComment);
1699 zipfileWrite16(a, pCDS->iDiskStart);
1700 zipfileWrite16(a, pCDS->iInternalAttr);
1701 zipfileWrite32(a, pCDS->iExternalAttr);
1702 zipfileWrite32(a, pCDS->iOffset);
1704 memcpy(a, pCDS->zFile, pCDS->nFile);
1705 a += pCDS->nFile;
1707 if( pEntry->aExtra ){
1708 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
1709 memcpy(a, pEntry->aExtra, n);
1710 a += n;
1711 }else{
1712 assert( pCDS->nExtra==9 );
1713 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
1714 zipfileWrite16(a, 5);
1715 *a++ = 0x01;
1716 zipfileWrite32(a, pEntry->mUnixTime);
1719 return a-aBuf;
1722 static int zipfileCommit(sqlite3_vtab *pVtab){
1723 ZipfileTab *pTab = (ZipfileTab*)pVtab;
1724 int rc = SQLITE_OK;
1725 if( pTab->pWriteFd ){
1726 i64 iOffset = pTab->szCurrent;
1727 ZipfileEntry *p;
1728 ZipfileEOCD eocd;
1729 int nEntry = 0;
1731 /* Write out all entries */
1732 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
1733 int n = zipfileSerializeCDS(p, pTab->aBuffer);
1734 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
1735 nEntry++;
1738 /* Write out the EOCD record */
1739 eocd.iDisk = 0;
1740 eocd.iFirstDisk = 0;
1741 eocd.nEntry = (u16)nEntry;
1742 eocd.nEntryTotal = (u16)nEntry;
1743 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
1744 eocd.iOffset = (u32)iOffset;
1745 rc = zipfileAppendEOCD(pTab, &eocd);
1747 zipfileCleanupTransaction(pTab);
1749 return rc;
1752 static int zipfileRollback(sqlite3_vtab *pVtab){
1753 return zipfileCommit(pVtab);
1756 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
1757 ZipfileCsr *pCsr;
1758 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
1759 if( iId==pCsr->iId ) break;
1761 return pCsr;
1764 static void zipfileFunctionCds(
1765 sqlite3_context *context,
1766 int argc,
1767 sqlite3_value **argv
1769 ZipfileCsr *pCsr;
1770 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
1771 assert( argc>0 );
1773 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
1774 if( pCsr ){
1775 ZipfileCDS *p = &pCsr->pCurrent->cds;
1776 char *zRes = sqlite3_mprintf("{"
1777 "\"version-made-by\" : %u, "
1778 "\"version-to-extract\" : %u, "
1779 "\"flags\" : %u, "
1780 "\"compression\" : %u, "
1781 "\"time\" : %u, "
1782 "\"date\" : %u, "
1783 "\"crc32\" : %u, "
1784 "\"compressed-size\" : %u, "
1785 "\"uncompressed-size\" : %u, "
1786 "\"file-name-length\" : %u, "
1787 "\"extra-field-length\" : %u, "
1788 "\"file-comment-length\" : %u, "
1789 "\"disk-number-start\" : %u, "
1790 "\"internal-attr\" : %u, "
1791 "\"external-attr\" : %u, "
1792 "\"offset\" : %u }",
1793 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
1794 (u32)p->flags, (u32)p->iCompression,
1795 (u32)p->mTime, (u32)p->mDate,
1796 (u32)p->crc32, (u32)p->szCompressed,
1797 (u32)p->szUncompressed, (u32)p->nFile,
1798 (u32)p->nExtra, (u32)p->nComment,
1799 (u32)p->iDiskStart, (u32)p->iInternalAttr,
1800 (u32)p->iExternalAttr, (u32)p->iOffset
1803 if( zRes==0 ){
1804 sqlite3_result_error_nomem(context);
1805 }else{
1806 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
1807 sqlite3_free(zRes);
1813 ** xFindFunction method.
1815 static int zipfileFindFunction(
1816 sqlite3_vtab *pVtab, /* Virtual table handle */
1817 int nArg, /* Number of SQL function arguments */
1818 const char *zName, /* Name of SQL function */
1819 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
1820 void **ppArg /* OUT: User data for *pxFunc */
1822 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
1823 *pxFunc = zipfileFunctionCds;
1824 *ppArg = (void*)pVtab;
1825 return 1;
1827 return 0;
1830 typedef struct ZipfileBuffer ZipfileBuffer;
1831 struct ZipfileBuffer {
1832 u8 *a; /* Pointer to buffer */
1833 int n; /* Size of buffer in bytes */
1834 int nAlloc; /* Byte allocated at a[] */
1837 typedef struct ZipfileCtx ZipfileCtx;
1838 struct ZipfileCtx {
1839 int nEntry;
1840 ZipfileBuffer body;
1841 ZipfileBuffer cds;
1844 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
1845 if( pBuf->n+nByte>pBuf->nAlloc ){
1846 u8 *aNew;
1847 int nNew = pBuf->n ? pBuf->n*2 : 512;
1848 int nReq = pBuf->n + nByte;
1850 while( nNew<nReq ) nNew = nNew*2;
1851 aNew = sqlite3_realloc(pBuf->a, nNew);
1852 if( aNew==0 ) return SQLITE_NOMEM;
1853 pBuf->a = aNew;
1854 pBuf->nAlloc = nNew;
1856 return SQLITE_OK;
1860 ** xStep() callback for the zipfile() aggregate. This can be called in
1861 ** any of the following ways:
1863 ** SELECT zipfile(name,data) ...
1864 ** SELECT zipfile(name,mode,mtime,data) ...
1865 ** SELECT zipfile(name,mode,mtime,data,method) ...
1867 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
1868 ZipfileCtx *p; /* Aggregate function context */
1869 ZipfileEntry e; /* New entry to add to zip archive */
1871 sqlite3_value *pName = 0;
1872 sqlite3_value *pMode = 0;
1873 sqlite3_value *pMtime = 0;
1874 sqlite3_value *pData = 0;
1875 sqlite3_value *pMethod = 0;
1877 int bIsDir = 0;
1878 u32 mode;
1879 int rc = SQLITE_OK;
1880 char *zErr = 0;
1882 int iMethod = -1; /* Compression method to use (0 or 8) */
1884 const u8 *aData = 0; /* Possibly compressed data for new entry */
1885 int nData = 0; /* Size of aData[] in bytes */
1886 int szUncompressed = 0; /* Size of data before compression */
1887 u8 *aFree = 0; /* Free this before returning */
1888 u32 iCrc32 = 0; /* crc32 of uncompressed data */
1890 char *zName = 0; /* Path (name) of new entry */
1891 int nName = 0; /* Size of zName in bytes */
1892 char *zFree = 0; /* Free this before returning */
1893 int nByte;
1895 memset(&e, 0, sizeof(e));
1896 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
1897 if( p==0 ) return;
1899 /* Martial the arguments into stack variables */
1900 if( nVal!=2 && nVal!=4 && nVal!=5 ){
1901 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
1902 rc = SQLITE_ERROR;
1903 goto zipfile_step_out;
1905 pName = apVal[0];
1906 if( nVal==2 ){
1907 pData = apVal[1];
1908 }else{
1909 pMode = apVal[1];
1910 pMtime = apVal[2];
1911 pData = apVal[3];
1912 if( nVal==5 ){
1913 pMethod = apVal[4];
1917 /* Check that the 'name' parameter looks ok. */
1918 zName = (char*)sqlite3_value_text(pName);
1919 nName = sqlite3_value_bytes(pName);
1920 if( zName==0 ){
1921 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
1922 rc = SQLITE_ERROR;
1923 goto zipfile_step_out;
1926 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
1927 ** deflate compression) or NULL (choose automatically). */
1928 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
1929 iMethod = sqlite3_value_int64(pMethod);
1930 if( iMethod!=0 && iMethod!=8 ){
1931 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
1932 rc = SQLITE_ERROR;
1933 goto zipfile_step_out;
1937 /* Now inspect the data. If this is NULL, then the new entry must be a
1938 ** directory. Otherwise, figure out whether or not the data should
1939 ** be deflated or simply stored in the zip archive. */
1940 if( sqlite3_value_type(pData)==SQLITE_NULL ){
1941 bIsDir = 1;
1942 iMethod = 0;
1943 }else{
1944 aData = sqlite3_value_blob(pData);
1945 szUncompressed = nData = sqlite3_value_bytes(pData);
1946 iCrc32 = crc32(0, aData, nData);
1947 if( iMethod<0 || iMethod==8 ){
1948 int nOut = 0;
1949 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
1950 if( rc!=SQLITE_OK ){
1951 goto zipfile_step_out;
1953 if( iMethod==8 || nOut<nData ){
1954 aData = aFree;
1955 nData = nOut;
1956 iMethod = 8;
1957 }else{
1958 iMethod = 0;
1963 /* Decode the "mode" argument. */
1964 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
1965 if( rc ) goto zipfile_step_out;
1967 /* Decode the "mtime" argument. */
1968 e.mUnixTime = zipfileGetTime(pMtime);
1970 /* If this is a directory entry, ensure that there is exactly one '/'
1971 ** at the end of the path. Or, if this is not a directory and the path
1972 ** ends in '/' it is an error. */
1973 if( bIsDir==0 ){
1974 if( zName[nName-1]=='/' ){
1975 zErr = sqlite3_mprintf("non-directory name must not end with /");
1976 rc = SQLITE_ERROR;
1977 goto zipfile_step_out;
1979 }else{
1980 if( zName[nName-1]!='/' ){
1981 zName = zFree = sqlite3_mprintf("%s/", zName);
1982 nName++;
1983 if( zName==0 ){
1984 rc = SQLITE_NOMEM;
1985 goto zipfile_step_out;
1987 }else{
1988 while( nName>1 && zName[nName-2]=='/' ) nName--;
1992 /* Assemble the ZipfileEntry object for the new zip archive entry */
1993 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
1994 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
1995 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
1996 e.cds.iCompression = iMethod;
1997 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
1998 e.cds.crc32 = iCrc32;
1999 e.cds.szCompressed = nData;
2000 e.cds.szUncompressed = szUncompressed;
2001 e.cds.iExternalAttr = (mode<<16);
2002 e.cds.iOffset = p->body.n;
2003 e.cds.nFile = nName;
2004 e.cds.zFile = zName;
2006 /* Append the LFH to the body of the new archive */
2007 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
2008 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
2009 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
2011 /* Append the data to the body of the new archive */
2012 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
2013 memcpy(&p->body.a[p->body.n], aData, nData);
2014 p->body.n += nData;
2016 /* Append the CDS record to the directory of the new archive */
2017 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
2018 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
2019 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
2021 /* Increment the count of entries in the archive */
2022 p->nEntry++;
2024 zipfile_step_out:
2025 sqlite3_free(aFree);
2026 sqlite3_free(zFree);
2027 if( rc ){
2028 if( zErr ){
2029 sqlite3_result_error(pCtx, zErr, -1);
2030 }else{
2031 sqlite3_result_error_code(pCtx, rc);
2034 sqlite3_free(zErr);
2038 ** xFinalize() callback for zipfile aggregate function.
2040 void zipfileFinal(sqlite3_context *pCtx){
2041 ZipfileCtx *p;
2042 ZipfileEOCD eocd;
2043 int nZip;
2044 u8 *aZip;
2046 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
2047 if( p==0 ) return;
2048 if( p->nEntry>0 ){
2049 memset(&eocd, 0, sizeof(eocd));
2050 eocd.nEntry = p->nEntry;
2051 eocd.nEntryTotal = p->nEntry;
2052 eocd.nSize = p->cds.n;
2053 eocd.iOffset = p->body.n;
2055 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
2056 aZip = (u8*)sqlite3_malloc(nZip);
2057 if( aZip==0 ){
2058 sqlite3_result_error_nomem(pCtx);
2059 }else{
2060 memcpy(aZip, p->body.a, p->body.n);
2061 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
2062 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
2063 sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
2067 sqlite3_free(p->body.a);
2068 sqlite3_free(p->cds.a);
2073 ** Register the "zipfile" virtual table.
2075 static int zipfileRegister(sqlite3 *db){
2076 static sqlite3_module zipfileModule = {
2077 1, /* iVersion */
2078 zipfileConnect, /* xCreate */
2079 zipfileConnect, /* xConnect */
2080 zipfileBestIndex, /* xBestIndex */
2081 zipfileDisconnect, /* xDisconnect */
2082 zipfileDisconnect, /* xDestroy */
2083 zipfileOpen, /* xOpen - open a cursor */
2084 zipfileClose, /* xClose - close a cursor */
2085 zipfileFilter, /* xFilter - configure scan constraints */
2086 zipfileNext, /* xNext - advance a cursor */
2087 zipfileEof, /* xEof - check for end of scan */
2088 zipfileColumn, /* xColumn - read data */
2089 0, /* xRowid - read data */
2090 zipfileUpdate, /* xUpdate */
2091 zipfileBegin, /* xBegin */
2092 0, /* xSync */
2093 zipfileCommit, /* xCommit */
2094 zipfileRollback, /* xRollback */
2095 zipfileFindFunction, /* xFindMethod */
2096 0, /* xRename */
2099 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
2100 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
2101 if( rc==SQLITE_OK ){
2102 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
2103 zipfileStep, zipfileFinal
2106 return rc;
2108 #else /* SQLITE_OMIT_VIRTUALTABLE */
2109 # define zipfileRegister(x) SQLITE_OK
2110 #endif
2112 #ifdef _WIN32
2113 __declspec(dllexport)
2114 #endif
2115 int sqlite3_zipfile_init(
2116 sqlite3 *db,
2117 char **pzErrMsg,
2118 const sqlite3_api_routines *pApi
2120 SQLITE_EXTENSION_INIT2(pApi);
2121 (void)pzErrMsg; /* Unused parameter */
2122 return zipfileRegister(db);