Skip to content

Commit ce6381b

Browse files
committed
cipher_trace pragma to allow logging of sqlcipher trace information
1 parent 39a3668 commit ce6381b

3 files changed

Lines changed: 102 additions & 43 deletions

File tree

src/crypto.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
680680
if(ctx) {
681681
sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check");
682682
}
683+
} else
684+
if( sqlite3StrICmp(zLeft, "cipher_trace")== 0 && zRight ){
685+
char *profile_status = sqlite3_mprintf("%d", sqlcipher_set_trace(zRight));
686+
codec_vdbe_return_string(pParse, "cipher_trace", profile_status, P4_DYNAMIC);
683687
}else {
684688
return 0;
685689
}

src/crypto.h

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@
3939
#include "btreeInt.h"
4040
#include "pager.h"
4141

42+
#ifdef __ANDROID__
43+
#include <android/log.h>
44+
#endif
45+
4246
/* extensions defined in pager.c */
4347
void *sqlite3PagerGetCodec(Pager*);
4448
void sqlite3PagerSetCodec(Pager*, void *(*)(void*,void*,Pgno,int), void (*)(void*,int,int), void (*)(void*), void *);
4549
int sqlite3pager_is_mj_pgno(Pager*, Pgno);
4650
void sqlite3pager_error(Pager*, int);
4751
void sqlite3pager_reset(Pager *pPager);
52+
/* end extensions defined in pager.c */
4853

4954
#if !defined (SQLCIPHER_CRYPTO_CC) \
5055
&& !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
@@ -111,49 +116,6 @@ void sqlite3pager_reset(Pager *pPager);
111116
#define CIPHER_MAX_KEY_SZ 64
112117
#endif
113118

114-
#ifdef __ANDROID__
115-
#include <android/log.h>
116-
#endif
117-
118-
#ifdef CODEC_DEBUG
119-
#ifdef __ANDROID__
120-
#define CODEC_TRACE(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
121-
#else
122-
#define CODEC_TRACE(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
123-
#endif
124-
#else
125-
#define CODEC_TRACE(...)
126-
#endif
127-
128-
#ifdef CODEC_DEBUG_MUTEX
129-
#define CODEC_TRACE_MUTEX(...) CODEC_TRACE(__VA_ARGS__)
130-
#else
131-
#define CODEC_TRACE_MUTEX(...)
132-
#endif
133-
134-
#ifdef CODEC_DEBUG_MEMORY
135-
#define CODEC_TRACE_MEMORY(...) CODEC_TRACE(__VA_ARGS__)
136-
#else
137-
#define CODEC_TRACE_MEMORY(...)
138-
#endif
139-
140-
#ifdef CODEC_DEBUG_PAGEDATA
141-
#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
142-
{ \
143-
int __pctr; \
144-
printf(DESC); \
145-
for(__pctr=0; __pctr < LEN; __pctr++) { \
146-
if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \
147-
printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \
148-
} \
149-
printf("\n"); \
150-
fflush(stdout); \
151-
}
152-
#else
153-
#define CODEC_HEXDUMP(DESC,BUFFER,LEN)
154-
#endif
155-
156-
/* end extensions defined in pager.c */
157119

158120
/*
159121
** Simple shared routines for converting hex char strings to binary data
@@ -341,6 +303,51 @@ int sqlcipher_find_db_index(sqlite3 *db, const char *zDb);
341303

342304
int sqlcipher_codec_ctx_integrity_check(codec_ctx *, Parse *, char *);
343305

306+
int sqlcipher_set_trace(const char *destination);
307+
void sqlcipher_trace(const char *message, ...);
308+
309+
#ifdef CODEC_DEBUG
310+
#ifdef __ANDROID__
311+
#define CODEC_TRACE(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
312+
#else
313+
#define CODEC_TRACE(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
314+
#endif
315+
#else
316+
#ifdef SQLCIPHER_OMIT_TRACE
317+
#define CODEC_TRACE(...)
318+
#else
319+
#define CODEC_TRACE(...) {sqlcipher_trace(__VA_ARGS__);};
320+
#endif
321+
#endif
322+
323+
#ifdef CODEC_DEBUG_MUTEX
324+
#define CODEC_TRACE_MUTEX(...) CODEC_TRACE(__VA_ARGS__)
325+
#else
326+
#define CODEC_TRACE_MUTEX(...)
327+
#endif
328+
329+
#ifdef CODEC_DEBUG_MEMORY
330+
#define CODEC_TRACE_MEMORY(...) CODEC_TRACE(__VA_ARGS__)
331+
#else
332+
#define CODEC_TRACE_MEMORY(...)
333+
#endif
334+
335+
#ifdef CODEC_DEBUG_PAGEDATA
336+
#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
337+
{ \
338+
int __pctr; \
339+
printf(DESC); \
340+
for(__pctr=0; __pctr < LEN; __pctr++) { \
341+
if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \
342+
printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \
343+
} \
344+
printf("\n"); \
345+
fflush(stdout); \
346+
}
347+
#else
348+
#define CODEC_HEXDUMP(DESC,BUFFER,LEN)
349+
#endif
350+
344351
#endif
345352
#endif
346353
/* END SQLCIPHER */

src/crypto_impl.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static volatile sqlite3_mem_methods default_mem_methods;
8888
static sqlcipher_provider *default_provider = NULL;
8989

9090
static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
91+
static volatile FILE* sqlcipher_trace_file = NULL;
92+
static volatile int sqlcipher_trace_logcat = 0;
9193

9294
sqlite3_mutex* sqlcipher_mutex(int mutex) {
9395
if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
@@ -1638,5 +1640,51 @@ const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
16381640
return ctx->provider->get_provider_version(ctx->provider_ctx);
16391641
}
16401642

1643+
#ifndef SQLCIPHER_OMIT_TRACE
1644+
void sqlcipher_trace(const char *message, ...) {
1645+
va_list params;
1646+
va_start(params, message);
1647+
if(sqlcipher_trace_file != NULL){
1648+
vfprintf((FILE*)sqlcipher_trace_file, message, params);
1649+
}
1650+
#ifdef __ANDROID__
1651+
if(sqlcipher_trace_logcat) {
1652+
__android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params);
1653+
}
1654+
#endif
1655+
va_end(params);
1656+
}
1657+
#endif
1658+
1659+
int sqlcipher_set_trace(const char *destination){
1660+
#ifdef SQLCIPHER_OMIT_TRACE
1661+
return SQLITE_ERROR;
1662+
#else
1663+
/* close open trace file if it is not stdout or stderr, then
1664+
reset trace settings */
1665+
if(sqlcipher_trace_file != NULL && sqlcipher_trace_file != stdout && sqlcipher_trace_file != stderr) {
1666+
fclose((FILE*)sqlcipher_trace_file);
1667+
}
1668+
sqlcipher_trace_file = NULL;
1669+
sqlcipher_trace_logcat = 0;
1670+
1671+
if(sqlite3StrICmp(destination, "logcat") == 0){
1672+
sqlcipher_trace_logcat = 1;
1673+
} else if(sqlite3StrICmp(destination, "stdout") == 0){
1674+
sqlcipher_trace_file = stdout;
1675+
}else if(sqlite3StrICmp(destination, "stderr") == 0){
1676+
sqlcipher_trace_file = stderr;
1677+
}else if(sqlite3StrICmp(destination, "off") != 0){
1678+
#if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT))
1679+
if(fopen_s(&sqlcipher_trace_file, destination, "a") != 0) return SQLITE_ERROR;
1680+
#else
1681+
if((sqlcipher_trace_file = fopen(destination, "a")) == 0) return SQLITE_ERROR;
1682+
#endif
1683+
}
1684+
sqlcipher_trace("sqlcipher_set_trace: set trace to %s\n", destination);
1685+
return SQLITE_OK;
1686+
#endif
1687+
}
1688+
16411689
#endif
16421690
/* END SQLCIPHER */

0 commit comments

Comments
 (0)