zipfile.c File Reference


Detailed Description

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.

#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...

Defines

#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))

Functions

static zip_filezip_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.


Define Documentation

#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_FLAGS   8

Definition at line 56 of file zipfile.c.

#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_FLAGS   6

Definition at line 50 of file zipfile.c.

#define ZIP_LOCAL_HEADER_LEN   30

Definition at line 53 of file zipfile.c.

Referenced by zip_vtab_column().

#define ZIP_LOCAL_HEADER_SIG   0x04034b50

Definition at line 49 of file zipfile.c.

#define ZIP_LOCAL_PATHLEN_OFFS   26

Definition at line 51 of file zipfile.c.

Referenced by zip_vtab_column().

#define zip_read_int (  )     ((p)[0] | ((p)[1] << 8) | ((p)[2] << 16) | ((p)[3] << 24))

Definition at line 78 of file zipfile.c.

Referenced by zip_open(), and zip_vtab_column().

#define zip_read_short (  )     ((p)[0] | ((p)[1] << 8))

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().


Function Documentation

int sqlite3_extension_init ( sqlite3 *  db,
char **  errmsg,
const sqlite3_api_routines *  api 
)

Initializer for SQLite extension load mechanism.

Parameters:
db SQLite database pointer
errmsg pointer receiving error message
api SQLite API routines
Returns:
SQLite error code

Definition at line 2293 of file zipfile.c.

References zip_vtab_init().

static char* unquote ( char const *  in  )  [static]

Strip off quotes given string.

Parameters:
in string to be processed
Returns:
new string to be free'd with sqlite3_free()

Definition at line 372 of file zipfile.c.

static void zip_close ( zip_file zip  )  [static]

Close ZIP file handle.

Parameters:
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 void zip_compress_func ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Compress data given blob and optional compression level.

Parameters:
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 void zip_crc32_func ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Compute CRC32 given blob.

Parameters:
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 void zip_deflate_func ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Deflate data given blob and optional compression level.

Parameters:
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 void zip_inflate_func ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Inflate data given blob.

Parameters:
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 zip_file* zip_open ( const char *  filename  )  [static]

static int zip_vtab_bestindex ( sqlite3_vtab *  vtab,
sqlite3_index_info *  info 
) [static]

Determines information for filter function according to constraints.

Parameters:
vtab virtual table
info index/constraint information
Returns:
SQLite error code

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 int zip_vtab_close ( sqlite3_vtab_cursor *  cursor  )  [static]

Close virtual table cursor.

Parameters:
cursor cursor pointer
Returns:
SQLite error code

Definition at line 631 of file zipfile.c.

References zip_cursor::matches.

static int zip_vtab_column ( sqlite3_vtab_cursor *  cursor,
sqlite3_context *  ctx,
int  n 
) [static]

static int zip_vtab_connect ( sqlite3 *  db,
void *  aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  vtabp,
char **  errp 
) [static]

Connect to virtual table.

Parameters:
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
Returns:
SQLite error code
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 int zip_vtab_create ( sqlite3 *  db,
void *  aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  vtabp,
char **  errp 
) [static]

Create virtual table.

Parameters:
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
Returns:
SQLite error code

Definition at line 473 of file zipfile.c.

References zip_vtab_connect().

static int zip_vtab_destroy ( sqlite3_vtab *  vtab  )  [static]

Destroy virtual table.

Parameters:
vtab virtual table pointer
Returns:
always SQLITE_OK

Definition at line 503 of file zipfile.c.

References zip_vtab_disconnect().

static int zip_vtab_disconnect ( sqlite3_vtab *  vtab  )  [static]

Disconnect virtual table.

Parameters:
vtab virtual table pointer
Returns:
always SQLITE_OK

Definition at line 487 of file zipfile.c.

References zip_vtab::zip, and zip_close().

Referenced by zip_vtab_destroy().

static int zip_vtab_eof ( sqlite3_vtab_cursor *  cursor  )  [static]

Return end of table state of virtual table cursor.

Parameters:
cursor virtual table cursor
Returns:
true/false

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 int zip_vtab_filter ( sqlite3_vtab_cursor *  cursor,
int  idxNum,
const char *  idxStr,
int  argc,
sqlite3_value **  argv 
) [static]

Filter function for virtual table.

Parameters:
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)
Returns:
SQLite error code

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 int zip_vtab_findfunc ( sqlite3_vtab *  vtab,
int  narg,
const char *  name,
void(**)(sqlite3_context *, int, sqlite3_value **)  pfunc,
void **  parg 
) [static]

Find overloaded function on virtual table.

Parameters:
vtab virtual table
narg number arguments
name function name
pfunc pointer to function (value return)
parg pointer to function's argument (value return)
Returns:
0 or 1

Definition at line 994 of file zipfile.c.

References zip_vtab_matchfunc().

static int zip_vtab_init ( sqlite3 *  db  )  [static]

Module initializer creating SQLite module and functions.

Parameters:
db database pointer
Returns:
SQLite error code

Definition at line 2243 of file zipfile.c.

References zip_compress_func(), zip_crc32_func(), zip_deflate_func(), and zip_inflate_func().

Referenced by sqlite3_extension_init().

static void zip_vtab_matchfunc ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
) [static]

Internal MATCH function for virtual table.

Parameters:
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 int zip_vtab_next ( sqlite3_vtab_cursor *  cursor  )  [static]

Retrieve next row from virtual table cursor.

Parameters:
cursor virtual table cursor
Returns:
SQLite error code

Definition at line 649 of file zipfile.c.

References zip_cursor::nmatches, and zip_cursor::pos.

Referenced by zip_vtab_filter().

static int zip_vtab_open ( sqlite3_vtab *  vtab,
sqlite3_vtab_cursor **  cursorp 
) [static]

Open virtual table and return cursor.

Parameters:
vtab virtual table pointer
cursorp pointer receiving cursor pointer
Returns:
SQLite error code

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 int zip_vtab_rowid ( sqlite3_vtab_cursor *  cursor,
sqlite_int64 *  rowidp 
) [static]

Return current rowid of virtual table cursor.

Parameters:
cursor virtual table cursor
rowidp value buffer to receive current rowid
Returns:
SQLite error code

Definition at line 931 of file zipfile.c.

References zip_cursor::matches, zip_cursor::nmatches, zip_cursor::pos, and zip_cursor::usematches.


Variable Documentation

const sqlite3_module zip_vtab_mod [static]


Generated on 9 Dec 2016 by doxygen.
Contact: chw@ch-werner.de