-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlite3mc.c
More file actions
executable file
·434 lines (387 loc) · 11.2 KB
/
sqlite3mc.c
File metadata and controls
executable file
·434 lines (387 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
** Name: sqlite3mc.c
** Purpose: Amalgamation of the SQLite3 Multiple Ciphers encryption extension for SQLite
** Author: Ulrich Telle
** Created: 2020-02-28
** Copyright: (c) 2006-2021 Ulrich Telle
** License: MIT
*/
/*
** Enable SQLite debug assertions if requested
*/
#ifndef SQLITE_DEBUG
#if defined(SQLITE_ENABLE_DEBUG) && (SQLITE_ENABLE_DEBUG == 1)
#define SQLITE_DEBUG 1
#endif
#endif
/*
** Define function for extra initialization and extra shutdown
**
** The extra initialization function registers an extension function
** which will be automatically executed for each new database connection.
**
** The extra shutdown function will be executed on the invocation of sqlite3_shutdown.
** All created multiple ciphers VFSs will be unregistered and destroyed.
*/
#define SQLITE_EXTRA_INIT sqlite3mc_initialize
#define SQLITE_EXTRA_SHUTDOWN sqlite3mc_shutdown
int sqlite3mc_initialize(const char* arg);
void sqlite3mc_shutdown(void);
/*
** To enable the extension functions define SQLITE_ENABLE_EXTFUNC on compiling this module
** To enable the reading CSV files define SQLITE_ENABLE_CSV on compiling this module
** To enable the SHA3 support define SQLITE_ENABLE_SHA3 on compiling this module
** To enable the CARRAY support define SQLITE_ENABLE_CARRAY on compiling this module
** To enable the FILEIO support define SQLITE_ENABLE_FILEIO on compiling this module
** To enable the SERIES support define SQLITE_ENABLE_SERIES on compiling this module
** To enable the UUID support define SQLITE_ENABLE_UUID on compiling this module
*/
/*
** Enable the user authentication feature
*/
#if !SQLITE_USER_AUTHENTICATION
/* Option not defined or explicitly disabled */
#ifndef SQLITE_USER_AUTHENTICATION
/* Option not defined, therefore enable by default */
#define SQLITE_USER_AUTHENTICATION 1
#else
/* Option defined and disabled, therefore undefine option */
#undef SQLITE_USER_AUTHENTICATION
#endif
#endif
#if defined(_WIN32) || defined(WIN32)
#ifndef SQLITE3MC_USE_RAND_S
#define SQLITE3MC_USE_RAND_S 1
#endif
#if SQLITE3MC_USE_RAND_S
/* Force header stdlib.h to define rand_s() */
#if !defined(_CRT_RAND_S)
#define _CRT_RAND_S
#endif
#endif
#ifndef SQLITE_API
#define SQLITE_API
#endif
#include <windows.h>
/* SQLite functions only needed on Win32 */
extern SQLITE_API void sqlite3_win32_write_debug(const char*, int);
extern SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
extern SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char*);
extern SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char*, int);
extern SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char*);
extern SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char*, int);
extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
#endif
/*
** Include SQLite3 amalgamation
*/
#include "wxsqlite3/sqlite3patched.c"
/*
** Include SQLite3MultiCipher components
*/
#include "wxsqlite3/sqlite3mc_config.h"
#include "wxsqlite3/sqlite3mc.h"
SQLITE_API const char*
sqlite3mc_version()
{
static const char* version = SQLITE3MC_VERSION_STRING;
return version;
}
SQLITE_PRIVATE void
sqlite3mcVersion(sqlite3_context* context, int argc, sqlite3_value** argv)
{
assert(argc == 0);
sqlite3_result_text(context, sqlite3mc_version(), -1, 0);
}
/*
** Crypto algorithms
*/
#include "wxsqlite3/md5.c"
#include "wxsqlite3/sha1.c"
#include "wxsqlite3/sha2.c"
#if HAVE_CIPHER_CHACHA20 || HAVE_CIPHER_SQLCIPHER
#include "wxsqlite3/fastpbkdf2.c"
/* Prototypes for several crypto functions to make pedantic compilers happy */
void chacha20_xor(void* data, size_t n, const uint8_t key[32], const uint8_t nonce[12], uint32_t counter);
void poly1305(const uint8_t* msg, size_t n, const uint8_t key[32], uint8_t tag[16]);
int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16]);
void chacha20_rng(void* out, size_t n);
#include "wxsqlite3/chacha20poly1305.c"
#endif
#ifdef SQLITE_USER_AUTHENTICATION
#include "wxsqlite3/userauth.c"
#endif
/*
** Declare function prototype for registering the codec extension functions
*/
static int
mcRegisterCodecExtensions(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
/*
** Codec implementation
*/
#if HAVE_CIPHER_AES_128_CBC || HAVE_CIPHER_AES_256_CBC || HAVE_CIPHER_SQLCIPHER
#include "wxsqlite3/rijndael.c"
#endif
#include "wxsqlite3/codec_algos.c"
#include "wxsqlite3/cipher_wxaes128.c"
#include "wxsqlite3/cipher_wxaes256.c"
#include "wxsqlite3/cipher_chacha20.c"
#include "wxsqlite3/cipher_sqlcipher.c"
#include "wxsqlite3/cipher_sds_rc4.c"
#include "wxsqlite3/cipher_common.c"
#include "wxsqlite3/cipher_config.c"
#include "wxsqlite3/codecext.c"
/*
** Extension functions
*/
#ifdef SQLITE_ENABLE_EXTFUNC
/* Prototype for initialization function of EXTENSIONFUNCTIONS extension */
int RegisterExtensionFunctions(sqlite3* db);
#include "wxsqlite3/extensionfunctions.c"
#endif
/*
** CSV import
*/
#ifdef SQLITE_ENABLE_CSV
/* Prototype for initialization function of CSV extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_csv_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/csv.c"
#endif
/*
** VSV import
*/
#ifdef SQLITE_ENABLE_VSV
/* Prototype for initialization function of VSV extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_vsv_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/vsv.c"
#endif
/*
** SHA3
*/
#ifdef SQLITE_ENABLE_SHA3
/* Prototype for initialization function of SHA3 extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_shathree_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/shathree.c"
#endif
/*
** CARRAY
*/
#ifdef SQLITE_ENABLE_CARRAY
/* Prototype for initialization function of CARRAY extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_carray_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/carray.c"
#endif
/*
** FILEIO
*/
#ifdef SQLITE_ENABLE_FILEIO
/* Prototype for initialization function of FILEIO extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_fileio_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
/* MinGW specifics */
#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
# include <unistd.h>
# include <dirent.h>
# if defined(__MINGW32__)
# define DIRENT dirent
# ifndef S_ISLNK
# define S_ISLNK(mode) (0)
# endif
# endif
#endif
#include "wxsqlite3/test_windirent.c"
#include "wxsqlite3/fileio.c"
#endif
/*
** SERIES
*/
#ifdef SQLITE_ENABLE_SERIES
/* Prototype for initialization function of SERIES extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_series_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/series.c"
#endif
/*
** UUID
*/
#ifdef SQLITE_ENABLE_UUID
/* Prototype for initialization function of UUID extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_uuid_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/uuid.c"
#endif
/*
** REGEXP
*/
#ifdef SQLITE_ENABLE_REGEXP
/* Prototype for initialization function of REGEXP extension */
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_regexp_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi);
#include "wxsqlite3/regexp.c"
#endif
/*
** Multi cipher VFS
*/
#include "wxsqlite3/sqlite3mc_vfs.c"
static int
mcRegisterCodecExtensions(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi)
{
int rc = SQLITE_OK;
CodecParameter* codecParameterTable = NULL;
if (sqlite3FindFunction(db, "sqlite3mc_config_table", 1, SQLITE_UTF8, 0) != NULL)
{
/* Return if codec extension functions are already defined */
return rc;
}
/* Generate copy of global codec parameter table */
codecParameterTable = sqlite3mcCloneCodecParameterTable();
rc = (codecParameterTable != NULL) ? SQLITE_OK : SQLITE_NOMEM;
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function_v2(db, "sqlite3mc_config_table", 0, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
codecParameterTable, sqlite3mcConfigTable, 0, 0, (void(*)(void*)) sqlite3mcFreeCodecParameterTable);
}
rc = (codecParameterTable != NULL) ? SQLITE_OK : SQLITE_NOMEM;
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_config", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
codecParameterTable, sqlite3mcConfigParams, 0, 0);
}
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_config", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
codecParameterTable, sqlite3mcConfigParams, 0, 0);
}
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_config", 3, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
codecParameterTable, sqlite3mcConfigParams, 0, 0);
}
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_codec_data", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
NULL, sqlite3mcCodecDataSql, 0, 0);
}
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_codec_data", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
NULL, sqlite3mcCodecDataSql, 0, 0);
}
if (rc == SQLITE_OK)
{
rc = sqlite3_create_function(db, "sqlite3mc_version", 0, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
NULL, sqlite3mcVersion, 0, 0);
}
return rc;
}
#ifdef SQLITE_ENABLE_EXTFUNC
static int
sqlite3_extfunc_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi)
{
return RegisterExtensionFunctions(db);
}
#endif
int
sqlite3mc_initialize(const char* arg)
{
int rc = SQLITE_OK;
/*
** Initialize and register MultiCipher VFS as default VFS
** if it isn't already registered
*/
rc = sqlite3mc_vfs_create(NULL, 1);
/*
** Register Multi Cipher extension
*/
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) mcRegisterCodecExtensions);
}
#ifdef SQLITE_ENABLE_EXTFUNC
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_extfunc_init);
}
#endif
#ifdef SQLITE_ENABLE_CSV
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_csv_init);
}
#endif
#ifdef SQLITE_ENABLE_VSV
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_vsv_init);
}
#endif
#ifdef SQLITE_ENABLE_SHA3
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_shathree_init);
}
#endif
#ifdef SQLITE_ENABLE_CARRAY
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_carray_init);
}
#endif
#ifdef SQLITE_ENABLE_FILEIO
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_fileio_init);
}
#endif
#ifdef SQLITE_ENABLE_SERIES
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_series_init);
}
#endif
#ifdef SQLITE_ENABLE_UUID
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_uuid_init);
}
#endif
#ifdef SQLITE_ENABLE_REGEXP
if (rc == SQLITE_OK)
{
rc = sqlite3_auto_extension((void(*)(void)) sqlite3_regexp_init);
}
#endif
return rc;
}
void
sqlite3mc_shutdown(void)
{
sqlite3mc_vfs_shutdown();
}
/*
** TCL/TK Shell
*/
#ifdef TCLSH
#define BUILD_tcl
#include "wxsqlite3/tclsqlite.c"
#endif