zipfile.c File Reference

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

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.

Macro Definition Documentation

◆ ZIP_CENTRAL_COMMENTLEN_OFFS

#define ZIP_CENTRAL_COMMENTLEN_OFFS   32

Definition at line 66 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_COMPLEN_OFFS

#define ZIP_CENTRAL_COMPLEN_OFFS   20

Definition at line 62 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_COMPMETH_OFFS

#define ZIP_CENTRAL_COMPMETH_OFFS   10

Definition at line 58 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_CRC32_OFFS

#define ZIP_CENTRAL_CRC32_OFFS   16

Definition at line 61 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_DIRSIZE_OFFS

#define ZIP_CENTRAL_DIRSIZE_OFFS   12

Definition at line 72 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_DIRSTART_OFFS

#define ZIP_CENTRAL_DIRSTART_OFFS   16

Definition at line 73 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_END_LEN

#define ZIP_CENTRAL_END_LEN   22

Definition at line 70 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_END_SIG

#define ZIP_CENTRAL_END_SIG   0x06054b50

Definition at line 69 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_ENTS_OFFS

#define ZIP_CENTRAL_ENTS_OFFS   8

Definition at line 71 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_EXTRALEN_OFFS

#define ZIP_CENTRAL_EXTRALEN_OFFS   30

Definition at line 65 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_HEADER_FLAGS

#define ZIP_CENTRAL_HEADER_FLAGS   8

Definition at line 56 of file zipfile.c.

◆ ZIP_CENTRAL_HEADER_LEN

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

◆ ZIP_CENTRAL_HEADER_SIG

#define ZIP_CENTRAL_HEADER_SIG   0x02014b50

Definition at line 55 of file zipfile.c.

Referenced by zip_open().

◆ ZIP_CENTRAL_LOCALHDR_OFFS

#define ZIP_CENTRAL_LOCALHDR_OFFS   42

Definition at line 67 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_MDATE_OFFS

#define ZIP_CENTRAL_MDATE_OFFS   14

Definition at line 60 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_MTIME_OFFS

#define ZIP_CENTRAL_MTIME_OFFS   12

Definition at line 59 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_CENTRAL_PATHLEN_OFFS

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

◆ ZIP_CENTRAL_UNCOMPLEN_OFFS

#define ZIP_CENTRAL_UNCOMPLEN_OFFS   24

Definition at line 63 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_COMPMETH_DEFLATED

#define ZIP_COMPMETH_DEFLATED   8

Definition at line 76 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_COMPMETH_STORED

#define ZIP_COMPMETH_STORED   0

Definition at line 75 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_LOCAL_EXTRA_OFFS

#define ZIP_LOCAL_EXTRA_OFFS   28

Definition at line 52 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_LOCAL_HEADER_FLAGS

#define ZIP_LOCAL_HEADER_FLAGS   6

Definition at line 50 of file zipfile.c.

◆ ZIP_LOCAL_HEADER_LEN

#define ZIP_LOCAL_HEADER_LEN   30

Definition at line 53 of file zipfile.c.

Referenced by zip_vtab_column().

◆ ZIP_LOCAL_HEADER_SIG

#define ZIP_LOCAL_HEADER_SIG   0x04034b50

Definition at line 49 of file zipfile.c.

◆ ZIP_LOCAL_PATHLEN_OFFS

#define ZIP_LOCAL_PATHLEN_OFFS   26

Definition at line 51 of file zipfile.c.

Referenced by zip_vtab_column().

◆ zip_read_int

#define zip_read_int ( p)
Value:
((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().

◆ zip_read_short

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

Definition at line 80 of file zipfile.c.

Referenced by zip_open(), zip_vtab_column(), and zip_vtab_filter().

◆ ZIP_SIG_LEN

#define ZIP_SIG_LEN   4

Definition at line 47 of file zipfile.c.

Referenced by zip_open().

Typedef Documentation

◆ zip_file

typedef struct zip_file zip_file

◆ zip_vtab

typedef struct zip_vtab zip_vtab

Function Documentation

◆ sqlite3_extension_init()

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

Initializer for SQLite extension load mechanism.

Parameters
dbSQLite database pointer
errmsgpointer receiving error message
apiSQLite API routines
Returns
SQLite error code

Definition at line 2293 of file zipfile.c.

References zip_vtab_init().

◆ unquote()

static char * unquote ( char const * in)
static

Strip off quotes given string.

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

Definition at line 372 of file zipfile.c.

Referenced by zip_vtab_connect().

◆ zip_close()

static void zip_close ( zip_file * zip)
static

Close ZIP file handle.

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

◆ zip_compress_func()

static void zip_compress_func ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

Compress data given blob and optional compression level.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1222 of file zipfile.c.

Referenced by zip_vtab_init().

◆ zip_crc32_func()

static void zip_crc32_func ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

Compute CRC32 given blob.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1059 of file zipfile.c.

Referenced by zip_vtab_init().

◆ zip_deflate_func()

static void zip_deflate_func ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

Deflate data given blob and optional compression level.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1158 of file zipfile.c.

Referenced by zip_vtab_init().

◆ zip_inflate_func()

static void zip_inflate_func ( sqlite3_context * ctx,
int argc,
sqlite3_value ** argv )
static

Inflate data given blob.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 1084 of file zipfile.c.

References nomem().

Referenced by zip_vtab_init().

◆ zip_open()

static zip_file * zip_open ( const char * filename)
static

◆ zip_vtab_bestindex()

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

Determines information for filter function according to constraints.

Parameters
vtabvirtual table
infoindex/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.

◆ zip_vtab_close()

static int zip_vtab_close ( sqlite3_vtab_cursor * cursor)
static

Close virtual table cursor.

Parameters
cursorcursor pointer
Returns
SQLite error code

Definition at line 631 of file zipfile.c.

References zip_cursor::matches.

◆ zip_vtab_column()

◆ zip_vtab_connect()

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
dbSQLite database pointer
auxuser specific pointer (unused)
argcargument count
argvargument vector
vtabppointer receiving virtual table pointer
errppointer 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().

◆ 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
dbSQLite database pointer
auxuser specific pointer (unused)
argcargument count
argvargument vector
vtabppointer receiving virtual table pointer
errppointer receiving error messag
Returns
SQLite error code

Definition at line 473 of file zipfile.c.

References zip_vtab_connect().

◆ zip_vtab_destroy()

static int zip_vtab_destroy ( sqlite3_vtab * vtab)
static

Destroy virtual table.

Parameters
vtabvirtual table pointer
Returns
always SQLITE_OK

Definition at line 503 of file zipfile.c.

References zip_vtab_disconnect().

◆ zip_vtab_disconnect()

static int zip_vtab_disconnect ( sqlite3_vtab * vtab)
static

Disconnect virtual table.

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

◆ zip_vtab_eof()

static int zip_vtab_eof ( sqlite3_vtab_cursor * cursor)
static

Return end of table state of virtual table cursor.

Parameters
cursorvirtual 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.

◆ zip_vtab_filter()

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
cursorvirtual table cursor
idxNumused for expression (1 -> EQ, 2 -> MATCH, 0 else)
idxStrnod used
argcnumber arguments (1 -> EQ/MATCH, 0 else)
argvargument (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().

◆ zip_vtab_findfunc()

static int zip_vtab_findfunc ( sqlite3_vtab * vtab,
int narg,
const char * name,
void(** pfunc )(sqlite3_context *, int, sqlite3_value **),
void ** parg )
static

Find overloaded function on virtual table.

Parameters
vtabvirtual table
nargnumber arguments
namefunction name
pfuncpointer to function (value return)
pargpointer to function's argument (value return)
Returns
0 or 1

Definition at line 994 of file zipfile.c.

References zip_vtab_matchfunc().

◆ zip_vtab_init()

static int zip_vtab_init ( sqlite3 * db)
static

Module initializer creating SQLite module and functions.

Parameters
dbdatabase pointer
Returns
SQLite error code

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

◆ zip_vtab_matchfunc()

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

Internal MATCH function for virtual table.

Parameters
ctxSQLite function context
argcnumber of arguments
argvargument vector

Definition at line 957 of file zipfile.c.

Referenced by zip_vtab_findfunc().

◆ zip_vtab_next()

static int zip_vtab_next ( sqlite3_vtab_cursor * cursor)
static

Retrieve next row from virtual table cursor.

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

◆ zip_vtab_open()

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

Open virtual table and return cursor.

Parameters
vtabvirtual table pointer
cursorppointer 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.

◆ zip_vtab_rowid()

static int zip_vtab_rowid ( sqlite3_vtab_cursor * cursor,
sqlite_int64 * rowidp )
static

Return current rowid of virtual table cursor.

Parameters
cursorvirtual table cursor
rowidpvalue 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

◆ zip_vtab_mod

const sqlite3_module zip_vtab_mod
static
Initial value:
= {
1,
0,
0,
0,
0,
0,
}
static int zip_vtab_filter(sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
Filter function for virtual table.
Definition zipfile.c:670
static int zip_vtab_close(sqlite3_vtab_cursor *cursor)
Close virtual table cursor.
Definition zipfile.c:631
static int zip_vtab_rowid(sqlite3_vtab_cursor *cursor, sqlite_int64 *rowidp)
Return current rowid of virtual table cursor.
Definition zipfile.c:931
static int zip_vtab_next(sqlite3_vtab_cursor *cursor)
Retrieve next row from virtual table cursor.
Definition zipfile.c:649
static int zip_vtab_bestindex(sqlite3_vtab *vtab, sqlite3_index_info *info)
Determines information for filter function according to constraints.
Definition zipfile.c:516
static int zip_vtab_connect(sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
Connect to virtual table.
Definition zipfile.c:412
static int zip_vtab_open(sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
Open virtual table and return cursor.
Definition zipfile.c:608
static int zip_vtab_disconnect(sqlite3_vtab *vtab)
Disconnect virtual table.
Definition zipfile.c:487
static int zip_vtab_destroy(sqlite3_vtab *vtab)
Destroy virtual table.
Definition zipfile.c:503
static int zip_vtab_create(sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
Create virtual table.
Definition zipfile.c:473
static int zip_vtab_eof(sqlite3_vtab_cursor *cursor)
Return end of table state of virtual table cursor.
Definition zipfile.c:753
static int zip_vtab_column(sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
Return column data of virtual table.
Definition zipfile.c:776
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.
Definition zipfile.c:994

SQLite module descriptor.

Definition at line 1026 of file zipfile.c.

Referenced by zip_vtab_init().


Generated on Sun Nov 26 2023 by doxygen.
Contact: chw@ch-werner.de