SQLite extension module for mapping a ZIP file as a read-only SQLite virtual table plus some supporting SQLite functions. More...
#include <sqlite3ext.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <zlib.h>
Go to the source code of this file.
Data Structures | |
struct | zip_file |
Structure to implement ZIP file handle. More... | |
struct | zip_vtab |
Structure to describe a ZIP virtual table. More... | |
struct | zip_cursor |
Structure to describe ZIP virtual table cursor. More... | |
Macros | |
#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) |
#define | zip_read_short(p) |
Typedefs | |
typedef struct zip_file | zip_file |
typedef struct zip_vtab | zip_vtab |
Functions | |
static zip_file * | zip_open (const char *filename) |
Memory map ZIP file for reading and return handle to it. | |
static void | zip_close (zip_file *zip) |
Close ZIP file handle. | |
static char * | unquote (char const *in) |
Strip off quotes given string. | |
static int | zip_vtab_connect (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) |
Connect to virtual table. | |
static int | zip_vtab_create (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) |
Create virtual table. | |
static int | zip_vtab_disconnect (sqlite3_vtab *vtab) |
Disconnect virtual table. | |
static int | zip_vtab_destroy (sqlite3_vtab *vtab) |
Destroy virtual table. | |
static int | zip_vtab_bestindex (sqlite3_vtab *vtab, sqlite3_index_info *info) |
Determines information for filter function according to constraints. | |
static int | zip_vtab_open (sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp) |
Open virtual table and return cursor. | |
static int | zip_vtab_close (sqlite3_vtab_cursor *cursor) |
Close virtual table cursor. | |
static int | zip_vtab_next (sqlite3_vtab_cursor *cursor) |
Retrieve next row from virtual table cursor. | |
static int | zip_vtab_filter (sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) |
Filter function for virtual table. | |
static int | zip_vtab_eof (sqlite3_vtab_cursor *cursor) |
Return end of table state of virtual table cursor. | |
static int | zip_vtab_column (sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n) |
Return column data of virtual table. | |
static int | zip_vtab_rowid (sqlite3_vtab_cursor *cursor, sqlite_int64 *rowidp) |
Return current rowid of virtual table cursor. | |
static void | zip_vtab_matchfunc (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Internal MATCH function for virtual table. | |
static int | zip_vtab_findfunc (sqlite3_vtab *vtab, int narg, const char *name, void(**pfunc)(sqlite3_context *, int, sqlite3_value **), void **parg) |
Find overloaded function on virtual table. | |
static void | zip_crc32_func (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Compute CRC32 given blob. | |
static void | zip_inflate_func (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Inflate data given blob. | |
static void | zip_deflate_func (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Deflate data given blob and optional compression level. | |
static void | zip_compress_func (sqlite3_context *ctx, int argc, sqlite3_value **argv) |
Compress data given blob and optional compression level. | |
static int | zip_vtab_init (sqlite3 *db) |
Module initializer creating SQLite module and functions. | |
int | sqlite3_extension_init (sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) |
Initializer for SQLite extension load mechanism. | |
Variables | |
static const sqlite3_module | zip_vtab_mod |
SQLite module descriptor. | |
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.
Definition in file zipfile.c.
#define ZIP_CENTRAL_COMMENTLEN_OFFS 32 |
Definition at line 66 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_COMPLEN_OFFS 20 |
Definition at line 62 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_COMPMETH_OFFS 10 |
Definition at line 58 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_CRC32_OFFS 16 |
Definition at line 61 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_DIRSIZE_OFFS 12 |
Definition at line 72 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_DIRSTART_OFFS 16 |
Definition at line 73 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_END_LEN 22 |
Definition at line 70 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_END_SIG 0x06054b50 |
Definition at line 69 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_ENTS_OFFS 8 |
Definition at line 71 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_EXTRALEN_OFFS 30 |
Definition at line 65 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_HEADER_LEN 46 |
Definition at line 57 of file zipfile.c.
Referenced by zip_open(), zip_vtab_column(), and zip_vtab_filter().
#define ZIP_CENTRAL_HEADER_SIG 0x02014b50 |
Definition at line 55 of file zipfile.c.
Referenced by zip_open().
#define ZIP_CENTRAL_LOCALHDR_OFFS 42 |
Definition at line 67 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_MDATE_OFFS 14 |
Definition at line 60 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_MTIME_OFFS 12 |
Definition at line 59 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_CENTRAL_PATHLEN_OFFS 28 |
Definition at line 64 of file zipfile.c.
Referenced by zip_open(), zip_vtab_column(), and zip_vtab_filter().
#define ZIP_CENTRAL_UNCOMPLEN_OFFS 24 |
Definition at line 63 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_COMPMETH_DEFLATED 8 |
Definition at line 76 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_COMPMETH_STORED 0 |
Definition at line 75 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_LOCAL_EXTRA_OFFS 28 |
Definition at line 52 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_LOCAL_HEADER_LEN 30 |
Definition at line 53 of file zipfile.c.
Referenced by zip_vtab_column().
#define ZIP_LOCAL_PATHLEN_OFFS 26 |
Definition at line 51 of file zipfile.c.
Referenced by zip_vtab_column().
#define zip_read_int | ( | p | ) |
Definition at line 78 of file zipfile.c.
Referenced by zip_open(), and zip_vtab_column().
#define zip_read_short | ( | p | ) |
Definition at line 80 of file zipfile.c.
Referenced by zip_open(), zip_vtab_column(), and zip_vtab_filter().
#define ZIP_SIG_LEN 4 |
Definition at line 47 of file zipfile.c.
Referenced by zip_open().
typedef struct zip_file zip_file |
typedef struct zip_vtab zip_vtab |
int sqlite3_extension_init | ( | sqlite3 * | db, |
char ** | errmsg, | ||
const sqlite3_api_routines * | api ) |
Initializer for SQLite extension load mechanism.
db | SQLite database pointer |
errmsg | pointer receiving error message |
api | SQLite API routines |
Definition at line 2293 of file zipfile.c.
References zip_vtab_init().
|
static |
Strip off quotes given string.
in | string to be processed |
Definition at line 372 of file zipfile.c.
Referenced by zip_vtab_connect().
|
static |
Close ZIP file handle.
zip | ZIP file handle |
Definition at line 340 of file zipfile.c.
References zip_file::data, zip_file::length, and zip_file::nentries.
Referenced by zip_vtab_connect(), and zip_vtab_disconnect().
|
static |
Compress data given blob and optional compression level.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1222 of file zipfile.c.
Referenced by zip_vtab_init().
|
static |
Compute CRC32 given blob.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1059 of file zipfile.c.
Referenced by zip_vtab_init().
|
static |
Deflate data given blob and optional compression level.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1158 of file zipfile.c.
Referenced by zip_vtab_init().
|
static |
Inflate data given blob.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 1084 of file zipfile.c.
References nomem().
Referenced by zip_vtab_init().
|
static |
Memory map ZIP file for reading and return handle to it.
filename | name of ZIP file |
Definition at line 184 of file zipfile.c.
References zip_file::baseoffs, zip_file::data, zip_file::entries, zip_file::length, zip_file::nentries, ZIP_CENTRAL_COMMENTLEN_OFFS, ZIP_CENTRAL_DIRSIZE_OFFS, ZIP_CENTRAL_DIRSTART_OFFS, ZIP_CENTRAL_END_LEN, ZIP_CENTRAL_END_SIG, ZIP_CENTRAL_ENTS_OFFS, ZIP_CENTRAL_EXTRALEN_OFFS, ZIP_CENTRAL_HEADER_LEN, ZIP_CENTRAL_HEADER_SIG, ZIP_CENTRAL_PATHLEN_OFFS, zip_read_int, zip_read_short, and ZIP_SIG_LEN.
Referenced by zip_vtab_connect().
|
static |
Determines information for filter function according to constraints.
vtab | virtual table |
info | index/constraint information |
Definition at line 516 of file zipfile.c.
References zip_vtab::db, zip_file::entries, zip_file::nentries, zip_vtab::sorted, zip_vtab::tblname, and zip_vtab::zip.
|
static |
Close virtual table cursor.
cursor | cursor pointer |
Definition at line 631 of file zipfile.c.
References zip_cursor::matches.
|
static |
Return column data of virtual table.
cursor | virtual table cursor |
ctx | SQLite function context |
n | column index |
Definition at line 776 of file zipfile.c.
References zip_file::baseoffs, zip_cursor::cursor, zip_file::data, zip_file::entries, zip_file::length, zip_cursor::matches, zip_file::nentries, zip_cursor::nmatches, zip_cursor::pos, zip_cursor::usematches, zip_vtab::zip, ZIP_CENTRAL_COMPLEN_OFFS, ZIP_CENTRAL_COMPMETH_OFFS, ZIP_CENTRAL_CRC32_OFFS, ZIP_CENTRAL_HEADER_LEN, ZIP_CENTRAL_LOCALHDR_OFFS, ZIP_CENTRAL_MDATE_OFFS, ZIP_CENTRAL_MTIME_OFFS, ZIP_CENTRAL_PATHLEN_OFFS, ZIP_CENTRAL_UNCOMPLEN_OFFS, ZIP_COMPMETH_DEFLATED, ZIP_COMPMETH_STORED, ZIP_LOCAL_EXTRA_OFFS, ZIP_LOCAL_HEADER_LEN, ZIP_LOCAL_PATHLEN_OFFS, zip_read_int, and zip_read_short.
|
static |
Connect to virtual table.
db | SQLite database pointer |
aux | user specific pointer (unused) |
argc | argument count |
argv | argument vector |
vtabp | pointer receiving virtual table pointer |
errp | pointer receiving error messag |
Argument vector contains:
argv[0] - module name
argv[1] - database name
argv[2] - table name (virtual table)
argv[3] - filename of ZIP file
Definition at line 412 of file zipfile.c.
References zip_vtab::db, zip_vtab::tblname, unquote(), zip_vtab::vtab, zip_vtab::zip, zip_close(), and zip_open().
Referenced by zip_vtab_create().
|
static |
Create virtual table.
db | SQLite database pointer |
aux | user specific pointer (unused) |
argc | argument count |
argv | argument vector |
vtabp | pointer receiving virtual table pointer |
errp | pointer receiving error messag |
Definition at line 473 of file zipfile.c.
References zip_vtab_connect().
|
static |
Destroy virtual table.
vtab | virtual table pointer |
Definition at line 503 of file zipfile.c.
References zip_vtab_disconnect().
|
static |
Disconnect virtual table.
vtab | virtual table pointer |
Definition at line 487 of file zipfile.c.
References zip_vtab::zip, and zip_close().
Referenced by zip_vtab_destroy().
|
static |
Return end of table state of virtual table cursor.
cursor | virtual table cursor |
Definition at line 753 of file zipfile.c.
References zip_cursor::cursor, zip_file::nentries, zip_cursor::nmatches, zip_cursor::pos, zip_cursor::usematches, and zip_vtab::zip.
|
static |
Filter function for virtual table.
cursor | virtual table cursor |
idxNum | used for expression (1 -> EQ, 2 -> MATCH, 0 else) |
idxStr | nod used |
argc | number arguments (1 -> EQ/MATCH, 0 else) |
argv | argument (nothing or RHS of filter expression) |
Definition at line 670 of file zipfile.c.
References zip_cursor::cursor, zip_file::entries, zip_cursor::matches, zip_file::nentries, zip_cursor::nmatches, zip_cursor::pos, zip_cursor::usematches, zip_vtab::zip, ZIP_CENTRAL_HEADER_LEN, ZIP_CENTRAL_PATHLEN_OFFS, zip_read_short, and zip_vtab_next().
|
static |
Find overloaded function on virtual table.
vtab | virtual table |
narg | number arguments |
name | function name |
pfunc | pointer to function (value return) |
parg | pointer to function's argument (value return) |
Definition at line 994 of file zipfile.c.
References zip_vtab_matchfunc().
|
static |
Module initializer creating SQLite module and functions.
db | database pointer |
Definition at line 2243 of file zipfile.c.
References zip_compress_func(), zip_crc32_func(), zip_deflate_func(), zip_inflate_func(), and zip_vtab_mod.
Referenced by sqlite3_extension_init().
|
static |
Internal MATCH function for virtual table.
ctx | SQLite function context |
argc | number of arguments |
argv | argument vector |
Definition at line 957 of file zipfile.c.
Referenced by zip_vtab_findfunc().
|
static |
Retrieve next row from virtual table cursor.
cursor | virtual table cursor |
Definition at line 649 of file zipfile.c.
References zip_cursor::nmatches, and zip_cursor::pos.
Referenced by zip_vtab_filter().
|
static |
Open virtual table and return cursor.
vtab | virtual table pointer |
cursorp | pointer receiving cursor pointer |
Definition at line 608 of file zipfile.c.
References zip_cursor::cursor, zip_cursor::matches, zip_cursor::nmatches, zip_cursor::pos, and zip_cursor::usematches.
|
static |
Return current rowid of virtual table cursor.
cursor | virtual table cursor |
rowidp | value buffer to receive current rowid |
Definition at line 931 of file zipfile.c.
References zip_cursor::matches, zip_cursor::nmatches, zip_cursor::pos, and zip_cursor::usematches.
|
static |
SQLite module descriptor.
Definition at line 1026 of file zipfile.c.
Referenced by zip_vtab_init().