Debian Codegraph @

ext/misc/zipfile.c

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