Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

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_cursor
 Structure to describe ZIP virtual table cursor. More...

struct  zip_file
 Structure to implement ZIP file handle. More...

struct  zip_vtab
 Structure to describe a ZIP virtual table. 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))

Typedefs

typedef zip_file zip_file
typedef zip_vtab zip_vtab

Functions

zip_filezip_open (const char *filename)
 Memory map ZIP file for reading and return handle to it.

void zip_close (zip_file *zip)
 Close ZIP file handle.

char * unquote (char const *in)
 Strip off quotes given string.

int zip_vtab_connect (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
 Connect to virtual table.

int zip_vtab_create (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp)
 Create virtual table.

int zip_vtab_disconnect (sqlite3_vtab *vtab)
 Disconnect virtual table.

int zip_vtab_destroy (sqlite3_vtab *vtab)
 Destroy virtual table.

int zip_vtab_bestindex (sqlite3_vtab *vtab, sqlite3_index_info *info)
 Determines information for filter function according to constraints.

int zip_vtab_open (sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp)
 Open virtual table and return cursor.

int zip_vtab_close (sqlite3_vtab_cursor *cursor)
 Close virtual table cursor.

int zip_vtab_next (sqlite3_vtab_cursor *cursor)
 Retrieve next row from virtual table cursor.

int zip_vtab_filter (sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
 Filter function for virtual table.

int zip_vtab_eof (sqlite3_vtab_cursor *cursor)
 Return end of table state of virtual table cursor.

int zip_vtab_column (sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n)
 Return column data of virtual table.

int zip_vtab_rowid (sqlite3_vtab_cursor *cursor, sqlite_int64 *rowidp)
 Return current rowid of virtual table cursor.

void zip_vtab_matchfunc (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Internal MATCH function for virtual table.

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.

void zip_crc32_func (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Compute CRC32 given blob.

void zip_inflate_func (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Inflate data given blob.

void zip_deflate_func (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Deflate data given blob and optional compression level.

void zip_compress_func (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Compress data given blob and optional compression level.

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

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


Typedef Documentation

typedef struct zip_file zip_file
 

typedef struct zip_vtab zip_vtab
 


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

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.

Referenced by csv_vtab_connect(), drvcolumns(), drvforeignkeys(), and zip_vtab_connect().

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

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

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

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

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.

Referenced by zip_vtab_init().

zip_file* zip_open const char *    filename [static]
 

Memory map ZIP file for reading and return handle to it.

Parameters:
filename  name of ZIP file
Returns:
ZIP file handle

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

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.

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.

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

Return column data of virtual table.

Parameters:
cursor  virtual table cursor
ctx  SQLite function context
n  column index
Returns:
SQLite error code

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.

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

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

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

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

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.

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

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

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(), zip_inflate_func(), and zip_vtab_mod.

Referenced by sqlite3_extension_init().

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

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

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.

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]
 

Initial value:

 {
    1,                   
    zip_vtab_create,     
    zip_vtab_connect,    
    zip_vtab_bestindex,  
    zip_vtab_disconnect, 
    zip_vtab_destroy,    
    zip_vtab_open,       
    zip_vtab_close,      
    zip_vtab_filter,     
    zip_vtab_next,       
    zip_vtab_eof,        
    zip_vtab_column,     
    zip_vtab_rowid,      
    0,                   
    0,                   
    0,                   
    0,                   
    0,                   
    zip_vtab_findfunc,   



}
SQLite module descriptor.

Definition at line 1026 of file zipfile.c.

Referenced by zip_vtab_init().


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