@@ -31,38 +31,86 @@ SOFTWARE.
3131#include "cevfs.h"
3232#include "xMethods.c"
3333
34- typedef uint8_t u8 ;
35-
3634extern const char * fileTail (const char * z );
37- extern void * sqlite3HexToBlob (sqlite3 * db , const char * z , int n );
35+ typedef unsigned char u8 ;
36+
37+ /*
38+ ** Taken from SQLite source code.
39+ ** Check to see if this machine uses EBCDIC. (Yes, believe it or
40+ ** not, there are still machines out there that use EBCDIC.)
41+ */
42+ #if 'A' == '\301'
43+ # define SQLITE_EBCDIC 1
44+ #else
45+ # define SQLITE_ASCII 1
46+ #endif
47+
48+ /*
49+ ** Taken from SQLite source code.
50+ ** Translate a single byte of Hex into an integer.
51+ ** This routine only works if h really is a valid hexadecimal
52+ ** character: 0..9a..fA..F
53+ */
54+ static u8 hexToInt (int h ){
55+ assert ( (h >='0' && h <='9' ) || (h >='a' && h <='f' ) || (h >='A' && h <='F' ) );
56+ #ifdef SQLITE_ASCII
57+ h += 9 * (1 & (h >>6 ));
58+ #endif
59+ #ifdef SQLITE_EBCDIC
60+ h += 9 * (1 & ~(h >>4 ));
61+ #endif
62+ return (u8 )(h & 0xf );
63+ }
64+
65+ /*
66+ ** Taken from SQLite source code.
67+ ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
68+ ** value. Return a pointer to its binary value. Space to hold the
69+ ** binary value has been obtained from malloc and must be freed by
70+ ** the calling routine.
71+ */
72+ static void * hexToBlob (const char * z , int n ){
73+ char * zBlob ;
74+ int i ;
75+
76+ zBlob = (char * )malloc (n /2 + 1 );
77+ n -- ;
78+ if ( zBlob ){
79+ for (i = 0 ; i < n ; i += 2 ){
80+ zBlob [i /2 ] = (hexToInt (z [i ])<<4 ) | hexToInt (z [i + 1 ]);
81+ }
82+ zBlob [i /2 ] = 0 ;
83+ }
84+ return zBlob ;
85+ }
86+
87+ // This context will be passed to xAutoDetect and the functions defined within.
88+ struct context ctx ;
3889
3990int main (int argc , const char * argv []) {
40- if ( argc != 4 ){
91+ if ( argc != 5 ){
4192 printf ("Usage: %s UNCOMPRESSED COMPRESSED KEY\n" , fileTail (argv [0 ]));
4293 printf (" UNCOMPRESSED: URI of uncompressed SQLite DB file.\n" );
4394 printf (" COMPRESSED: URI of new compressed DB with optional ?block_size=<block_size>\n" );
95+ printf (" VFS_NAME: Name of VFS to embed in database file.\n" );
4496 printf (" KEY: Encryption key in the form: x'<hex1><hex2>...<hex32>'\n" );
4597 return EXIT_FAILURE ;
4698 }
4799
48100 int rc ;
49101
50- // This context will be passed to xAutoDetect and the functions defined within.
51- struct context ctx ;
52-
53102 // Convert encryption key string to hex blob.
54103 // This assumes that the key is in the form of x'<hex-string>'
55104 // You should, of course, implement proper error checking.
56- const char * key = arvg [ 3 ]+ 2 ;
57- char * keyBytes = sqlite3HexToBlob ( NULL , key , strlen (key )- 1 );
105+ const char * key = argv [ 4 ]+ 2 ;
106+ char * keyBytes = hexToBlob ( key , ( int ) strlen (key )- 1 );
58107
59108 ctx .pKey = keyBytes ; // 32-bit encryption hex key
60109 ctx .nKeySz = kCCKeySizeAES256 ; // key size in bytes
61110 ctx .nIvSz = kCCBlockSizeAES128 ; // size of IV in bytes
62111
63- // If you will be using the VFS name to determine how you set up your xMethods,
64- // you may want to pass the VFS name as a command line parameter as well.
65- // For now, we'll just pass "CEVFS-default".
66- rc = cevfs_build (argv [1 ], argv [2 ], "CEVFS-default" , & ctx , cevfsAutoDetect );
112+ // You can use the VFS name to determine how you set up your xMethods,
113+ // so we pass the VFS name as a command line parameter as well.
114+ rc = cevfs_build (argv [1 ], argv [2 ], argv [3 ], & ctx , cevfsAutoDetect );
67115 return rc ;
68116}
0 commit comments