-
Notifications
You must be signed in to change notification settings - Fork 36
/
zipfile.c
2300 lines (2110 loc) · 52 KB
/
zipfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file zipfile.c
* SQLite extension module for mapping a ZIP file as a read-only
* SQLite virtual table plus some supporting SQLite functions.
*
* 2012 September 12
*
* The author disclaims copyright to this source code.
* In place of a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*
********************************************************************
*/
#ifdef linux
#define _GNU_SOURCE
#endif
#ifdef STANDALONE
#include <sqlite3.h>
#else
#include <sqlite3ext.h>
static SQLITE_EXTENSION_INIT1
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <zlib.h>
#undef snprintf
#define ZIP_SIG_LEN 4
#define ZIP_LOCAL_HEADER_SIG 0x04034b50
#define ZIP_LOCAL_HEADER_FLAGS 6
#define ZIP_LOCAL_PATHLEN_OFFS 26
#define ZIP_LOCAL_EXTRA_OFFS 28
#define ZIP_LOCAL_HEADER_LEN 30
#define ZIP_CENTRAL_HEADER_SIG 0x02014b50
#define ZIP_CENTRAL_HEADER_FLAGS 8
#define ZIP_CENTRAL_HEADER_LEN 46
#define ZIP_CENTRAL_COMPMETH_OFFS 10
#define ZIP_CENTRAL_MTIME_OFFS 12
#define ZIP_CENTRAL_MDATE_OFFS 14
#define ZIP_CENTRAL_CRC32_OFFS 16
#define ZIP_CENTRAL_COMPLEN_OFFS 20
#define ZIP_CENTRAL_UNCOMPLEN_OFFS 24
#define ZIP_CENTRAL_PATHLEN_OFFS 28
#define ZIP_CENTRAL_EXTRALEN_OFFS 30
#define ZIP_CENTRAL_COMMENTLEN_OFFS 32
#define ZIP_CENTRAL_LOCALHDR_OFFS 42
#define ZIP_CENTRAL_END_SIG 0x06054b50
#define ZIP_CENTRAL_END_LEN 22
#define ZIP_CENTRAL_ENTS_OFFS 8
#define ZIP_CENTRAL_DIRSIZE_OFFS 12
#define ZIP_CENTRAL_DIRSTART_OFFS 16
#define ZIP_COMPMETH_STORED 0
#define ZIP_COMPMETH_DEFLATED 8
#define zip_read_int(p) \
((p)[0] | ((p)[1] << 8) | ((p)[2] << 16) | ((p)[3] << 24))
#define zip_read_short(p) \
((p)[0] | ((p)[1] << 8))
/**
* @typedef zip_file
* @struct zip_file
* Structure to implement ZIP file handle.
*/
typedef struct zip_file {
off_t length; /**< length of ZIP file */
unsigned char *data; /**< mmap()'ed ZIP file */
#if defined(_WIN32) || defined(_WIN64)
HANDLE h; /**< Windows file handle */
HANDLE mh; /**< Windows file mapping */
#endif
int baseoffs; /**< Global offset for embedded ZIP files */
int nentries; /**< Number of directory entries */
unsigned char *entries[1]; /**< Pointer to first entry */
} zip_file;
/**
* @typedef zip_vtab
* @struct zip_vtab
* Structure to describe a ZIP virtual table.
*/
typedef struct zip_vtab {
sqlite3_vtab vtab; /**< SQLite virtual table */
sqlite3 *db; /**< Open database */
zip_file *zip; /**< ZIP file handle */
int sorted; /**< 1 = sorted by path, -1 = sorting, 0 = unsorted */
char tblname[1]; /**< Name, format "database"."table" */
} zip_vtab;
/**
* @typedef zip_cursor
* @struct zip_cursor
* Structure to describe ZIP virtual table cursor.
*/
typedef struct {
sqlite3_vtab_cursor cursor; /**< SQLite virtual table cursor */
int pos; /**< ZIP file position */
int usematches; /**< For filter EQ */
int nmatches; /**< For filter EQ */
int *matches; /**< For filter EQ */
} zip_cursor;
#ifdef SQLITE_OPEN_URI
/**
* @typedef mem_blk
* @struct mem_blk
* Structure to describe in-core SQLite database read from BLOB.
*/
typedef struct mem_blk {
#define MEM_MAGIC "MVFS"
char magic[4]; /**< magic number */
int opened; /**< open counter */
#if defined(_WIN32) || defined(_WIN64)
HANDLE mh; /**< handle for memory mapping */
#else
long psize; /**< page size */
#ifdef linux
sqlite3_mutex *mutex; /**< mutex to protect mapping */
int lcnt; /**< lock counter */
#endif
#endif
unsigned long size; /**< size of memory mapped area */
unsigned long length; /**< real length of data area */
unsigned char *data; /**< data area */
} mem_blk;
/**
* @typedef mem_file
* @struct mem_file
* SQLite3 file structure enhanced by mem_blk.
*/
typedef struct mem_file {
sqlite3_file base; /**< sqlite3_file base structure */
#ifdef linux
int lock; /**< lock state */
#endif
mem_blk *mb; /**< pointer to memory block */
} mem_file;
/*
* Private VFS name
*/
static char mem_vfs_name[64];
#endif /* SQLITE_OPEN_URI */
/**
* Memory map ZIP file for reading and return handle to it.
* @param filename name of ZIP file
* @result ZIP file handle
*/
static zip_file *
zip_open(const char *filename)
{
#if defined(_WIN32) || defined(_WIN64)
HANDLE h, mh = INVALID_HANDLE_VALUE;
DWORD length;
unsigned char *data = 0;
#else
int fd;
off_t length;
unsigned char *data = MAP_FAILED;
#endif
int nentries, baseoffs = 0, i;
zip_file *zip = 0;
unsigned char *p, *q;
if (!filename) {
return 0;
}
#if defined(_WIN32) || defined(_WIN64)
h = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (h == INVALID_HANDLE_VALUE) {
goto error;
}
length = GetFileSize(h, 0);
if ((length == INVALID_FILE_SIZE) || (length < ZIP_CENTRAL_END_LEN)) {
goto error;
}
mh = CreateFileMapping(h, 0, PAGE_READONLY, 0, length, 0);
if (mh == INVALID_HANDLE_VALUE) {
goto error;
}
data = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, length);
if (!data) {
goto error;
}
#else
fd = open(filename, O_RDONLY);
if (fd < 0) {
goto error;
}
length = lseek(fd, 0, SEEK_END);
if ((length == -1) || (length < ZIP_CENTRAL_END_LEN)) {
goto error;
}
data = (unsigned char *) mmap(0, length, PROT_READ,
MAP_FILE | MAP_PRIVATE, fd, 0);
if (data == MAP_FAILED) {
goto error;
}
close(fd);
fd = -1;
#endif
p = data + length - ZIP_CENTRAL_END_LEN;
while (p >= data) {
if (*p == (ZIP_CENTRAL_END_SIG & 0xFF)) {
if (zip_read_int(p) == ZIP_CENTRAL_END_SIG) {
break;
}
p -= ZIP_SIG_LEN;
} else {
--p;
}
}
if (p < data) {
goto error;
}
nentries = zip_read_short(p + ZIP_CENTRAL_ENTS_OFFS);
if (nentries == 0) {
goto error;
}
q = data + zip_read_int(p + ZIP_CENTRAL_DIRSTART_OFFS);
p -= zip_read_int(p + ZIP_CENTRAL_DIRSIZE_OFFS);
if (p < data || p > data + length || q < data || q > data + length) {
goto error;
}
baseoffs = p - q;
q = p;
for (i = 0; i < nentries; i++) {
int pathlen, comlen, extra;
if ((q + ZIP_CENTRAL_HEADER_LEN) > (data + length)) {
goto error;
}
if (zip_read_int(q) != ZIP_CENTRAL_HEADER_SIG) {
goto error;
}
pathlen = zip_read_short(q + ZIP_CENTRAL_PATHLEN_OFFS);
comlen = zip_read_short(q + ZIP_CENTRAL_COMMENTLEN_OFFS);
extra = zip_read_short(q + ZIP_CENTRAL_EXTRALEN_OFFS);
q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;
}
zip = sqlite3_malloc(sizeof (zip_file) +
nentries * sizeof (unsigned char *));
if (!zip) {
goto error;
}
#if defined(_WIN32) || defined(_WIN64)
zip->h = zip->mh = INVALID_HANDLE_VALUE;
#endif
zip->length = length;
zip->data = data;
zip->baseoffs = baseoffs;
zip->nentries = nentries;
q = p;
for (i = 0; i < nentries; i++) {
int pathlen, comlen, extra;
if ((q + ZIP_CENTRAL_HEADER_LEN) > (data + length)) {
goto error;
}
if (zip_read_int(q) != ZIP_CENTRAL_HEADER_SIG) {
goto error;
}
zip->entries[i] = q;
pathlen = zip_read_short(q + ZIP_CENTRAL_PATHLEN_OFFS);
comlen = zip_read_short(q + ZIP_CENTRAL_COMMENTLEN_OFFS);
extra = zip_read_short(q + ZIP_CENTRAL_EXTRALEN_OFFS);
q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;
}
zip->entries[i] = 0;
#if defined(_WIN32) || defined(_WIN64)
zip->h = h;
zip->mh = mh;
#endif
return zip;
error:
if (zip) {
sqlite3_free(zip);
}
#if defined(_WIN32) || defined(_WIN64)
if (data) {
UnmapViewOfFile(data);
}
if (mh != INVALID_HANDLE_VALUE) {
CloseHandle(mh);
}
if (h != INVALID_HANDLE_VALUE) {
CloseHandle(h);
}
#else
if (data != MAP_FAILED) {
munmap(data, length);
}
if (fd >= 0) {
close(fd);
}
#endif
return 0;
}
/**
* Close ZIP file handle.
* @param zip ZIP file handle
*/
static void
zip_close(zip_file *zip)
{
if (zip) {
#if defined(_WIN32) || defined(_WIN64)
if (zip->data) {
UnmapViewOfFile(zip->data);
}
if (zip->mh != INVALID_HANDLE_VALUE) {
CloseHandle(zip->mh);
}
if (zip->h != INVALID_HANDLE_VALUE) {
CloseHandle(zip->h);
}
#else
if (zip->data) {
munmap(zip->data, zip->length);
}
#endif
zip->length = 0;
zip->data = 0;
zip->nentries = 0;
sqlite3_free(zip);
}
}
/**
* Strip off quotes given string.
* @param in string to be processed
* @result new string to be free'd with sqlite3_free()
*/
static char *
unquote(char const *in)
{
char c, *ret;
int i;
ret = sqlite3_malloc(strlen(in) + 1);
if (ret) {
c = in[0];
if ((c == '"') || (c == '\'')) {
i = strlen(in + 1);
if ((i > 0) && (in[i] == c)) {
strcpy(ret, in + 1);
ret[i - 1] = '\0';
return ret;
}
}
strcpy(ret, in);
}
return ret;
}
/**
* Connect to virtual table
* @param db SQLite database pointer
* @param aux user specific pointer (unused)
* @param argc argument count
* @param argv argument vector
* @param vtabp pointer receiving virtual table pointer
* @param errp pointer receiving error messag
* @result SQLite error code
*
* Argument vector contains:
*
* argv[0] - module name<br>
* argv[1] - database name<br>
* argv[2] - table name (virtual table)<br>
* argv[3] - filename of ZIP file<br>
*/
static int
zip_vtab_connect(sqlite3* db, void *aux, int argc, const char * const *argv,
sqlite3_vtab **vtabp, char **errp)
{
zip_file *zip = 0;
int rc = SQLITE_ERROR;
char *filename;
zip_vtab *vtab;
if (argc < 4) {
*errp = sqlite3_mprintf("input file name missing");
return SQLITE_ERROR;
}
filename = unquote(argv[3]);
if (filename) {
zip = zip_open(filename);
sqlite3_free(filename);
}
if (!zip) {
*errp = sqlite3_mprintf("unable to open input file");
return rc;
}
vtab = sqlite3_malloc(sizeof(zip_vtab) + 6 +
strlen(argv[1]) + strlen(argv[2]));
if (!vtab) {
zip_close(zip);
*errp = sqlite3_mprintf("out of memory");
return rc;
}
memset(vtab, 0, sizeof (*vtab));
strcpy(vtab->tblname, "\"");
strcat(vtab->tblname, argv[1]);
strcat(vtab->tblname, "\".\"");
strcat(vtab->tblname, argv[2]);
strcat(vtab->tblname, "\"");
vtab->db = db;
vtab->zip = zip;
rc = sqlite3_declare_vtab(db, "CREATE TABLE x(path, comp, mtime, "
"crc32, length, data, clength, cdata, isdir)");
if (rc != SQLITE_OK) {
zip_close(zip);
sqlite3_free(vtab);
*errp = sqlite3_mprintf("table definition failed (error %d)", rc);
return rc;
}
*vtabp = &vtab->vtab;
*errp = 0;
return SQLITE_OK;
}
/**
* Create virtual table
* @param db SQLite database pointer
* @param aux user specific pointer (unused)
* @param argc argument count
* @param argv argument vector
* @param vtabp pointer receiving virtual table pointer
* @param errp pointer receiving error messag
* @result SQLite error code
*/
static int
zip_vtab_create(sqlite3* db, void *aux, int argc,
const char *const *argv,
sqlite3_vtab **vtabp, char **errp)
{
return zip_vtab_connect(db, aux, argc, argv, vtabp, errp);
}
/**
* Disconnect virtual table.
* @param vtab virtual table pointer
* @result always SQLITE_OK
*/
static int
zip_vtab_disconnect(sqlite3_vtab *vtab)
{
zip_vtab *tab = (zip_vtab *) vtab;
zip_close(tab->zip);
sqlite3_free(tab);
return SQLITE_OK;
}
/**
* Destroy virtual table.
* @param vtab virtual table pointer
* @result always SQLITE_OK
*/
static int
zip_vtab_destroy(sqlite3_vtab *vtab)
{
return zip_vtab_disconnect(vtab);
}
/**
* Determines information for filter function according to constraints.
* @param vtab virtual table
* @param info index/constraint information
* @result SQLite error code
*/
static int
zip_vtab_bestindex(sqlite3_vtab *vtab, sqlite3_index_info *info)
{
zip_vtab *tab = (zip_vtab *) vtab;
int i;
info->idxNum = 0;
if (tab->sorted == 0) {
char *sql = 0;
unsigned char **entries = 0;
sqlite3_stmt *stmt = 0;
int rc, count, i;
size_t tmp;
/* perform sorting on 0th column (path, string) */
tab->sorted = -1;
entries = sqlite3_malloc(tab->zip->nentries * sizeof (entries));
sql = sqlite3_mprintf("SELECT rowid FROM %s ORDER BY path",
tab->tblname);
if (sql && entries) {
rc = sqlite3_prepare_v2(tab->db, sql, -1, &stmt, 0);
if ((rc == SQLITE_OK) && stmt) {
count = 0;
while (1) {
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) {
break;
}
tmp = sqlite3_column_int(stmt, 0);
entries[count++] = (unsigned char *) tmp;
}
if ((rc == SQLITE_DONE) && (count == tab->zip->nentries)) {
for (i = 0; i < count; i++) {
tmp = (size_t) entries[i];
tmp = (size_t) tab->zip->entries[tmp];
entries[i] = (unsigned char *) tmp;
}
memcpy(tab->zip->entries, entries, i * sizeof (entries));
tab->sorted = 1;
}
}
}
if (stmt) {
sqlite3_finalize(stmt);
}
if (sql) {
sqlite3_free(sql);
}
if (entries) {
sqlite3_free(entries);
}
}
/* no optimization while table is being sorted or is unsorted */
if (tab->sorted != 1) {
return SQLITE_OK;
}
/* support EQ or simple MATCH constraint on 0th column (path) */
for (i = 0; i < info->nConstraint; i++) {
if (info->aConstraint[i].usable &&
(info->aConstraint[i].iColumn == 0)) {
if (info->aConstraint[i].op == SQLITE_INDEX_CONSTRAINT_EQ) {
info->idxNum = 1;
info->aConstraintUsage[i].argvIndex = 1;
info->aConstraintUsage[i].omit = 1;
info->estimatedCost = 1.0;
break;
} else if (info->aConstraint[i].op ==
SQLITE_INDEX_CONSTRAINT_MATCH) {
info->idxNum = 2;
info->aConstraintUsage[i].argvIndex = 1;
info->aConstraintUsage[i].omit = 1;
info->estimatedCost = 2.0;
break;
}
}
}
/* ORDER BY is ascending on 0th column (path) when table is sorted */
if (info->nOrderBy > 0) {
if ((info->aOrderBy[0].iColumn == 0) && !info->aOrderBy[0].desc) {
info->orderByConsumed = 1;
}
}
return SQLITE_OK;
}
/**
* Open virtual table and return cursor.
* @param vtab virtual table pointer
* @param cursorp pointer receiving cursor pointer
* @result SQLite error code
*/
static int
zip_vtab_open(sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
{
zip_cursor *cur = sqlite3_malloc(sizeof(*cur));
if (!cur) {
return SQLITE_ERROR;
}
cur->cursor.pVtab = vtab;
cur->pos = -1;
cur->usematches = 0;
cur->nmatches = 0;
cur->matches = 0;
*cursorp = &cur->cursor;
return SQLITE_OK;
}
/**
* Close virtual table cursor.
* @param cursor cursor pointer
* @result SQLite error code
*/
static int
zip_vtab_close(sqlite3_vtab_cursor *cursor)
{
zip_cursor *cur = (zip_cursor *) cursor;
if (cur->matches) {
sqlite3_free(cur->matches);
}
sqlite3_free(cur);
return SQLITE_OK;
}
/**
* Retrieve next row from virtual table cursor
* @param cursor virtual table cursor
* @result SQLite error code
*/
static int
zip_vtab_next(sqlite3_vtab_cursor *cursor)
{
zip_cursor *cur = (zip_cursor *) cursor;
if (cur->nmatches >= 0) {
cur->pos++;
}
return SQLITE_OK;
}
/**
* Filter function for virtual table.
* @param cursor virtual table cursor
* @param idxNum used for expression (1 -> EQ, 2 -> MATCH, 0 else)
* @param idxStr nod used
* @param argc number arguments (1 -> EQ/MATCH, 0 else)
* @param argv argument (nothing or RHS of filter expression)
* @result SQLite error code
*/
static int
zip_vtab_filter(sqlite3_vtab_cursor *cursor, int idxNum,
const char *idxStr, int argc, sqlite3_value **argv)
{
zip_cursor *cur = (zip_cursor *) cursor;
zip_vtab *tab = (zip_vtab *) cur->cursor.pVtab;
if (cur->matches) {
sqlite3_free(cur->matches);
cur->matches = 0;
}
cur->usematches = 0;
cur->nmatches = 0;
/* if EQ or MATCH constraint is active, add match array to cursor */
if (idxNum && (argc > 0)) {
int i, k, d, found, leneq, len;
unsigned char *eq;
eq = (unsigned char *) sqlite3_value_text(argv[0]);
if (!eq) {
cur->nmatches = -1;
goto done;
}
if (idxNum > 1) {
unsigned char *p = (unsigned char *) strrchr((char *) eq, '*');
if (!p || (p[1] != '\0')) {
return SQLITE_ERROR;
}
leneq = p - eq;
} else {
leneq = sqlite3_value_bytes(argv[0]);
if (leneq == 0) {
cur->nmatches = -1;
goto done;
}
}
cur->matches = sqlite3_malloc(tab->zip->nentries * sizeof (int));
if (!cur->matches) {
return SQLITE_NOMEM;
}
cur->usematches = 1;
memset(cur->matches, 0, tab->zip->nentries * sizeof (int));
for (k = found = 0; k < tab->zip->nentries; k++) {
len = zip_read_short(tab->zip->entries[k] +
ZIP_CENTRAL_PATHLEN_OFFS);
if (idxNum > 1) {
if (len < leneq) {
continue;
}
} else if (len != leneq) {
if (found) {
break;
}
continue;
}
d = memcmp(tab->zip->entries[k] + ZIP_CENTRAL_HEADER_LEN,
eq, leneq);
if (d == 0) {
found++;
cur->matches[k] = 1;
} else if (d > 0) {
break;
}
}
for (i = k = 0; i < tab->zip->nentries; i++) {
if (cur->matches[i]) {
cur->matches[k++] = i;
}
}
cur->nmatches = k;
}
done:
cur->pos = -1;
return zip_vtab_next(cursor);
}
/**
* Return end of table state of virtual table cursor
* @param cursor virtual table cursor
* @result true/false
*/
static int
zip_vtab_eof(sqlite3_vtab_cursor *cursor)
{
zip_cursor *cur = (zip_cursor *) cursor;
zip_vtab *tab = (zip_vtab *) cur->cursor.pVtab;
if (cur->nmatches < 0) {
return 1;
}
if (cur->usematches) {
return cur->pos >= cur->nmatches;
}
return cur->pos >= tab->zip->nentries;
}
/**
* Return column data of virtual table.
* @param cursor virtual table cursor
* @param ctx SQLite function context
* @param n column index
* @result SQLite error code
*/
static int
zip_vtab_column(sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
{
zip_cursor *cur = (zip_cursor *) cursor;
zip_vtab *tab = (zip_vtab *) cur->cursor.pVtab;
unsigned char *data = 0;
unsigned char *dest = 0;
int length;
if (cur->usematches) {
int pos;
if ((cur->pos < 0) || (cur->pos >= cur->nmatches)) {
sqlite3_result_error(ctx, "out of bounds", -1);
return SQLITE_ERROR;
}
pos = cur->matches[cur->pos];
data = tab->zip->entries[pos];
} else {
if ((cur->pos < 0) || (cur->pos >= tab->zip->nentries)) {
sqlite3_result_error(ctx, "out of bounds", -1);
return SQLITE_ERROR;
}
data = tab->zip->entries[cur->pos];
}
switch (n) {
case 0: /* "path": pathname */
length = zip_read_short(data + ZIP_CENTRAL_PATHLEN_OFFS);
data += ZIP_CENTRAL_HEADER_LEN;
sqlite3_result_text(ctx, (char *) data, length, SQLITE_TRANSIENT);
return SQLITE_OK;
case 1: /* "comp": compression method */
length = zip_read_short(data + ZIP_CENTRAL_COMPMETH_OFFS);
sqlite3_result_int(ctx, length);
return SQLITE_OK;
case 2: /* "mtime": modification time/date */
{
int time = zip_read_short(data + ZIP_CENTRAL_MTIME_OFFS);
int date = zip_read_short(data + ZIP_CENTRAL_MDATE_OFFS);
char mtbuf[64];
sprintf(mtbuf, "%04d-%02d-%02d %02d:%02d:%02d",
(date >> 9) + 1980, (date >> 5) & 0xf, date & 0x1f,
time >> 11, (time >> 5) & 0x3f, (time & 0x1f) << 1);
sqlite3_result_text(ctx, mtbuf, -1, SQLITE_TRANSIENT);
return SQLITE_OK;
}
case 3: /* "crc32": CRC32 of uncompress data */
length = zip_read_int(data + ZIP_CENTRAL_CRC32_OFFS);
sqlite3_result_int(ctx, length);
return SQLITE_OK;
case 4: /* "length": uncompress length of data */
length = zip_read_int(data + ZIP_CENTRAL_UNCOMPLEN_OFFS);
sqlite3_result_int(ctx, length);
return SQLITE_OK;
case 5: /* "data": uncompressed data */
{
int clength, offs, extra, pathlen, cmeth;
offs = tab->zip->baseoffs +
zip_read_int(data + ZIP_CENTRAL_LOCALHDR_OFFS);
if ((offs + ZIP_LOCAL_HEADER_LEN) > tab->zip->length) {
goto donull;
}
extra = zip_read_short(tab->zip->data + offs +
ZIP_LOCAL_EXTRA_OFFS);
pathlen = zip_read_short(tab->zip->data + offs +
ZIP_LOCAL_PATHLEN_OFFS);
length = zip_read_int(data + ZIP_CENTRAL_UNCOMPLEN_OFFS);
clength = zip_read_int(data + ZIP_CENTRAL_COMPLEN_OFFS);
cmeth = zip_read_short(data + ZIP_CENTRAL_COMPMETH_OFFS);
offs += ZIP_LOCAL_HEADER_LEN + pathlen + extra;
if ((offs + clength) > tab->zip->length) {
goto donull;
}
data = tab->zip->data + offs;
if (cmeth == ZIP_COMPMETH_STORED) {
sqlite3_result_blob(ctx, data, clength, SQLITE_TRANSIENT);
return SQLITE_OK;
} else if (cmeth == ZIP_COMPMETH_DEFLATED) {
z_stream stream;
int err;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.next_in = data;
stream.avail_in = clength;
stream.next_out = dest = sqlite3_malloc(length);
stream.avail_out = length;
stream.opaque = 0;
if (!dest) {
goto donull;
}
if (inflateInit2(&stream, -15) != Z_OK) {
goto donull;
}
err = inflate(&stream, Z_SYNC_FLUSH);
inflateEnd(&stream);
if ((err == Z_STREAM_END) ||
((err == Z_OK) && (stream.avail_in == 0))) {
sqlite3_result_blob(ctx, dest, length, sqlite3_free);
return SQLITE_OK;
}
}
donull:
if (dest) {
sqlite3_free(dest);
}
sqlite3_result_null(ctx);
return SQLITE_OK;
}
case 6: /* "clength": compressed length */
length = zip_read_int(data + ZIP_CENTRAL_COMPLEN_OFFS);
sqlite3_result_int(ctx, length);
return SQLITE_OK;
case 7: /* "cdata": raw data */
{
int clength, offs, extra, pathlen;
offs = tab->zip->baseoffs +
zip_read_int(data + ZIP_CENTRAL_LOCALHDR_OFFS);
if ((offs + ZIP_LOCAL_HEADER_LEN) > tab->zip->length) {
goto donull;
}
extra = zip_read_short(tab->zip->data + offs +
ZIP_LOCAL_EXTRA_OFFS);
pathlen = zip_read_short(tab->zip->data + offs +
ZIP_LOCAL_PATHLEN_OFFS);
length = zip_read_int(data + ZIP_CENTRAL_UNCOMPLEN_OFFS);
clength = zip_read_int(data + ZIP_CENTRAL_COMPLEN_OFFS);
offs += ZIP_LOCAL_HEADER_LEN + pathlen + extra;
if ((offs + clength) > tab->zip->length) {
goto donull;
}
data = tab->zip->data + offs;
sqlite3_result_blob(ctx, data, clength, SQLITE_TRANSIENT);
return SQLITE_OK;
}
case 8: /* "isdir": directory indicator */
length = zip_read_short(data + ZIP_CENTRAL_PATHLEN_OFFS);
data += ZIP_CENTRAL_HEADER_LEN;
sqlite3_result_int(ctx, (length > 0 && data[length - 1] == '/'));
return SQLITE_OK;
}
sqlite3_result_error(ctx, "invalid column number", -1);
return SQLITE_ERROR;
}
/**
* Return current rowid of virtual table cursor
* @param cursor virtual table cursor
* @param rowidp value buffer to receive current rowid
* @result SQLite error code
*/
static int
zip_vtab_rowid(sqlite3_vtab_cursor *cursor, sqlite_int64 *rowidp)
{
zip_cursor *cur = (zip_cursor *) cursor;
if (cur->nmatches < 0) {
*rowidp = -1;
} else if ((cur->pos >= 0) && (cur->usematches > 0)) {
if (cur->pos < cur->nmatches) {
*rowidp = cur->matches[cur->pos];
} else {
*rowidp = -1;
}
} else {
*rowidp = cur->pos;
}
return SQLITE_OK;
}
/**
* Internal MATCH function for virtual table.
* @param ctx SQLite function context
* @param argc number of arguments
* @param argv argument vector
*/
static void
zip_vtab_matchfunc(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
int ret = 0;
if (argc == 2) {
unsigned char *q = (unsigned char *) sqlite3_value_text(argv[0]);
unsigned char *p = (unsigned char *) sqlite3_value_text(argv[1]);
if (p && q) {
unsigned char *eq = (unsigned char *) strrchr((char *) q, '*');
int lenq, lenp;
if (eq && (eq[1] == '\0')) {
lenq = eq - q;
if (lenq) {
lenp = strlen((char *) p);
if ((lenp >= lenq) && !memcmp(p, q, lenq)) {
ret = 1;
}
}
}
}
}
sqlite3_result_int(ctx, ret);
}
/**
* Find overloaded function on virtual table.
* @param vtab virtual table
* @param narg number arguments
* @param name function name
* @param pfunc pointer to function (value return)
* @param parg pointer to function's argument (value return)
* @result 0 or 1
*/
static int
zip_vtab_findfunc(sqlite3_vtab *vtab, int narg, const char *name,
void (**pfunc)(sqlite3_context *, int, sqlite3_value **),
void **parg)
{
if ((narg == 2) && !strcmp(name, "match")) {
*pfunc = zip_vtab_matchfunc;
*parg = 0;