Skip to content

Commit 47bb328

Browse files
committed
trace logging will automatically append newline
1 parent a1e5f72 commit 47bb328

File tree

6 files changed

+192
-193
lines changed

6 files changed

+192
-193
lines changed

src/crypto.c

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,30 @@ static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ct
5353
page_sz = sqlcipher_codec_ctx_get_pagesize(ctx);
5454
reserve_sz = sqlcipher_codec_ctx_get_reservesize(ctx);
5555

56-
CODEC_TRACE("codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d\n", page_sz, reserve_sz);
56+
CODEC_TRACE("codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d", page_sz, reserve_sz);
5757

58-
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: entering database mutex %p\n", db->mutex);
58+
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: entering database mutex %p", db->mutex);
5959
sqlite3_mutex_enter(db->mutex);
60-
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: entered database mutex %p\n", db->mutex);
60+
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: entered database mutex %p", db->mutex);
6161
db->nextPagesize = page_sz;
6262

6363
/* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else
6464
sqliteBtreeSetPageSize will block the change */
6565
pDb->pBt->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
6666
rc = sqlite3BtreeSetPageSize(pDb->pBt, page_sz, reserve_sz, 0);
6767

68-
CODEC_TRACE("codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d\n", rc);
68+
CODEC_TRACE("codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d", rc);
6969

70-
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: leaving database mutex %p\n", db->mutex);
70+
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: leaving database mutex %p", db->mutex);
7171
sqlite3_mutex_leave(db->mutex);
72-
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: left database mutex %p\n", db->mutex);
72+
CODEC_TRACE_MUTEX("codec_set_btree_to_codec_pagesize: left database mutex %p", db->mutex);
7373

7474
return rc;
7575
}
7676

7777
static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
7878
struct Db *pDb = &db->aDb[nDb];
79-
CODEC_TRACE("codec_set_pass_key: entered db=%p nDb=%d zKey=%p nKey=%d for_ctx=%d\n", db, nDb, zKey, nKey, for_ctx);
79+
CODEC_TRACE("codec_set_pass_key: entered db=%p nDb=%d zKey=%p nKey=%d for_ctx=%d", db, nDb, zKey, nKey, for_ctx);
8080
if(pDb->pBt) {
8181
codec_ctx *ctx = (codec_ctx*) sqlite3PagerGetCodec(pDb->pBt->pBt->pPager);
8282

@@ -94,7 +94,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
9494
ctx = (codec_ctx*) sqlite3PagerGetCodec(pDb->pBt->pBt->pPager);
9595
}
9696

97-
CODEC_TRACE("sqlcipher_codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx);
97+
CODEC_TRACE("sqlcipher_codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p", db, iDb, pParse, zLeft, zRight, ctx);
9898

9999
#ifdef SQLCIPHER_EXT
100100
if( sqlite3_stricmp(zLeft, "cipher_license")==0 && zRight ){
@@ -191,7 +191,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
191191
char *migrate_status = sqlite3_mprintf("%d", status);
192192
codec_vdbe_return_string(pParse, "cipher_migrate", migrate_status, P4_DYNAMIC);
193193
if(status != SQLITE_OK) {
194-
CODEC_TRACE("sqlcipher_codec_pragma: error occurred during cipher_migrate: %d\n", status);
194+
CODEC_TRACE("sqlcipher_codec_pragma: error occurred during cipher_migrate: %d", status);
195195
sqlcipher_codec_ctx_set_error(ctx, status);
196196
}
197197
}
@@ -726,15 +726,15 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
726726
int plaintext_header_sz = sqlcipher_codec_ctx_get_plaintext_header_size(ctx);
727727
int cctx = CIPHER_READ_CTX;
728728

729-
CODEC_TRACE("sqlite3Codec: entered pgno=%d, mode=%d, page_sz=%d\n", pgno, mode, page_sz);
729+
CODEC_TRACE("sqlite3Codec: entered pgno=%d, mode=%d, page_sz=%d", pgno, mode, page_sz);
730730

731731
#ifdef SQLCIPHER_EXT
732732
if(sqlcipher_license_check(ctx) != SQLITE_OK) return NULL;
733733
#endif
734734

735735
/* call to derive keys if not present yet */
736736
if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) {
737-
CODEC_TRACE("sqlite3Codec: error occurred during key derivation: %d\n", rc);
737+
CODEC_TRACE("sqlite3Codec: error occurred during key derivation: %d", rc);
738738
sqlcipher_codec_ctx_set_error(ctx, rc);
739739
return NULL;
740740
}
@@ -743,7 +743,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
743743
PRAGMA. We can't set the error state on the pager at that point because the pager
744744
may not be open yet. However, this is a fatal error state, so abort the codec */
745745
if(plaintext_header_sz < 0) {
746-
CODEC_TRACE("sqlite3Codec: error invalid plaintext_header_sz: %d\n", plaintext_header_sz);
746+
CODEC_TRACE("sqlite3Codec: error invalid plaintext_header_sz: %d", plaintext_header_sz);
747747
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
748748
return NULL;
749749
}
@@ -752,7 +752,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
752752
offset = plaintext_header_sz ? plaintext_header_sz : FILE_HEADER_SZ;
753753

754754

755-
CODEC_TRACE("sqlite3Codec: switch mode=%d offset=%d\n", mode, offset);
755+
CODEC_TRACE("sqlite3Codec: switch mode=%d offset=%d", mode, offset);
756756
switch(mode) {
757757
case CODEC_READ_OP: /* decrypt */
758758
if(pgno == 1) /* copy initial part of file header or SQLite magic to buffer */
@@ -768,7 +768,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
768768
if(rc != SQLITE_OK) {
769769
/* failure to decrypt a page is considered a permanent error and will render the pager unusable
770770
in order to prevent inconsistent data being loaded into page cache */
771-
CODEC_TRACE("sqlite3Codec: error decrypting page data: %d\n", rc);
771+
CODEC_TRACE("sqlite3Codec: error decrypting page data: %d", rc);
772772
sqlcipher_memset((unsigned char*) buffer+offset, 0, page_sz-offset);
773773
sqlcipher_codec_ctx_set_error(ctx, rc);
774774
}
@@ -784,7 +784,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
784784
void *kdf_salt = NULL;
785785
/* retrieve the kdf salt */
786786
if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &kdf_salt)) != SQLITE_OK) {
787-
CODEC_TRACE("sqlite3Codec: error retrieving salt: %d\n", rc);
787+
CODEC_TRACE("sqlite3Codec: error retrieving salt: %d", rc);
788788
sqlcipher_codec_ctx_set_error(ctx, rc);
789789
return NULL;
790790
}
@@ -800,7 +800,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
800800
if(rc != SQLITE_OK) {
801801
/* failure to encrypt a page is considered a permanent error and will render the pager unusable
802802
in order to prevent corrupted pages from being written to the main databased when using WAL */
803-
CODEC_TRACE("sqlite3Codec: error encrypting page data: %d\n", rc);
803+
CODEC_TRACE("sqlite3Codec: error encrypting page data: %d", rc);
804804
sqlcipher_memset((unsigned char*)buffer+offset, 0, page_sz-offset);
805805
sqlcipher_codec_ctx_set_error(ctx, rc);
806806
return NULL;
@@ -809,7 +809,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
809809
break;
810810

811811
default:
812-
CODEC_TRACE("sqlite3Codec: error unsupported codec mode %d\n", mode);
812+
CODEC_TRACE("sqlite3Codec: error unsupported codec mode %d", mode);
813813
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); /* unsupported mode, set error */
814814
return pData;
815815
break;
@@ -826,7 +826,7 @@ static void sqlite3FreeCodecArg(void *pCodecArg) {
826826
int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
827827
struct Db *pDb = &db->aDb[nDb];
828828

829-
CODEC_TRACE("sqlite3CodecAttach: entered db=%p, nDb=%d zKey=%p, nKey=%d\n", db, nDb, zKey, nKey);
829+
CODEC_TRACE("sqlite3CodecAttach: entered db=%p, nDb=%d zKey=%p, nKey=%d", db, nDb, zKey, nKey);
830830

831831

832832
if(nKey && zKey && pDb->pBt) {
@@ -838,12 +838,12 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
838838
/* check if the sqlite3_file is open, and if not force handle to NULL */
839839
if((fd = sqlite3PagerFile(pPager))->pMethods == 0) fd = NULL;
840840

841-
CODEC_TRACE("sqlite3CodecAttach: calling sqlcipher_activate()\n");
841+
CODEC_TRACE("sqlite3CodecAttach: calling sqlcipher_activate()");
842842
sqlcipher_activate(); /* perform internal initialization for sqlcipher */
843843

844-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: entering database mutex %p\n", db->mutex);
844+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: entering database mutex %p", db->mutex);
845845
sqlite3_mutex_enter(db->mutex);
846-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: entered database mutex %p\n", db->mutex);
846+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: entered database mutex %p", db->mutex);
847847

848848
#ifdef SQLCIPHER_EXT
849849
if((rc = sqlite3_set_authorizer(db, sqlcipher_license_authorizer, db)) != SQLITE_OK) {
@@ -853,44 +853,44 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
853853
#endif
854854

855855
/* point the internal codec argument against the contet to be prepared */
856-
CODEC_TRACE("sqlite3CodecAttach: calling sqlcipher_codec_ctx_init()\n");
856+
CODEC_TRACE("sqlite3CodecAttach: calling sqlcipher_codec_ctx_init()");
857857
rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, zKey, nKey);
858858

859859
if(rc != SQLITE_OK) {
860860
/* initialization failed, do not attach potentially corrupted context */
861-
CODEC_TRACE("sqlite3CodecAttach: context initialization failed with rc=%d\n", rc);
861+
CODEC_TRACE("sqlite3CodecAttach: context initialization failed with rc=%d", rc);
862862
/* force an error at the pager level, such that even the upstream caller ignores the return code
863863
the pager will be in an error state and will process no further operations */
864864
sqlite3pager_error(pPager, rc);
865865
pDb->pBt->pBt->db->errCode = rc;
866-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: leaving database mutex %p (early return on rc=%d)\n", db->mutex, rc);
866+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: leaving database mutex %p (early return on rc=%d)", db->mutex, rc);
867867
sqlite3_mutex_leave(db->mutex);
868-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: left database mutex %p (early return on rc=%d)\n", db->mutex, rc);
868+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: left database mutex %p (early return on rc=%d)", db->mutex, rc);
869869
return rc;
870870
}
871871

872-
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3PagerSetCodec()\n");
872+
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3PagerSetCodec()");
873873
sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
874874

875-
CODEC_TRACE("sqlite3CodecAttach: calling codec_set_btree_to_codec_pagesize()\n");
875+
CODEC_TRACE("sqlite3CodecAttach: calling codec_set_btree_to_codec_pagesize()");
876876
codec_set_btree_to_codec_pagesize(db, pDb, ctx);
877877

878878
/* force secure delete. This has the benefit of wiping internal data when deleted
879879
and also ensures that all pages are written to disk (i.e. not skipped by
880880
sqlite3PagerDontWrite optimizations) */
881-
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3BtreeSecureDelete()\n");
881+
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3BtreeSecureDelete()");
882882
sqlite3BtreeSecureDelete(pDb->pBt, 1);
883883

884884
/* if fd is null, then this is an in-memory database and
885885
we dont' want to overwrite the AutoVacuum settings
886886
if not null, then set to the default */
887887
if(fd != NULL) {
888-
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3BtreeSetAutoVacuum()\n");
888+
CODEC_TRACE("sqlite3CodecAttach: calling sqlite3BtreeSetAutoVacuum()");
889889
sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM);
890890
}
891-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: leaving database mutex %p\n", db->mutex);
891+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: leaving database mutex %p", db->mutex);
892892
sqlite3_mutex_leave(db->mutex);
893-
CODEC_TRACE_MUTEX("sqlite3CodecAttach: left database mutex %p\n", db->mutex);
893+
CODEC_TRACE_MUTEX("sqlite3CodecAttach: left database mutex %p", db->mutex);
894894
}
895895
return SQLITE_OK;
896896
}
@@ -914,12 +914,12 @@ void sqlite3_activate_see(const char* in) {
914914
}
915915

916916
int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
917-
CODEC_TRACE("sqlite3_key entered: db=%p pKey=%p nKey=%d\n", db, pKey, nKey);
917+
CODEC_TRACE("sqlite3_key entered: db=%p pKey=%p nKey=%d", db, pKey, nKey);
918918
return sqlite3_key_v2(db, "main", pKey, nKey);
919919
}
920920

921921
int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
922-
CODEC_TRACE("sqlite3_key_v2: entered db=%p zDb=%s pKey=%p nKey=%d\n", db, zDb, pKey, nKey);
922+
CODEC_TRACE("sqlite3_key_v2: entered db=%p zDb=%s pKey=%p nKey=%d", db, zDb, pKey, nKey);
923923
/* attach key if db and pKey are not null and nKey is > 0 */
924924
if(db && pKey && nKey) {
925925
int db_index = sqlcipher_find_db_index(db, zDb);
@@ -929,7 +929,7 @@ int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
929929
}
930930

931931
int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
932-
CODEC_TRACE("sqlite3_rekey entered: db=%p pKey=%p nKey=%d\n", db, pKey, nKey);
932+
CODEC_TRACE("sqlite3_rekey entered: db=%p pKey=%p nKey=%d", db, pKey, nKey);
933933
return sqlite3_rekey_v2(db, "main", pKey, nKey);
934934
}
935935

@@ -944,11 +944,11 @@ int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
944944
** 3. If there is a key present, re-encrypt the database with the new key
945945
*/
946946
int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
947-
CODEC_TRACE("sqlite3_rekey_v2: entered db=%p zDb=%s pKey=%p, nKey=%d\n", db, zDb, pKey, nKey);
947+
CODEC_TRACE("sqlite3_rekey_v2: entered db=%p zDb=%s pKey=%p, nKey=%d", db, zDb, pKey, nKey);
948948
if(db && pKey && nKey) {
949949
int db_index = sqlcipher_find_db_index(db, zDb);
950950
struct Db *pDb = &db->aDb[db_index];
951-
CODEC_TRACE("sqlite3_rekey_v2: database pDb=%p db_index:%d\n", pDb, db_index);
951+
CODEC_TRACE("sqlite3_rekey_v2: database pDb=%p db_index:%d", pDb, db_index);
952952
if(pDb->pBt) {
953953
codec_ctx *ctx;
954954
int rc, page_count;
@@ -960,13 +960,13 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
960960

961961
if(ctx == NULL) {
962962
/* there was no codec attached to this database, so this should do nothing! */
963-
CODEC_TRACE("sqlite3_rekey_v2: no codec attached to db, exiting\n");
963+
CODEC_TRACE("sqlite3_rekey_v2: no codec attached to db, exiting");
964964
return SQLITE_OK;
965965
}
966966

967-
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: entering database mutex %p\n", db->mutex);
967+
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: entering database mutex %p", db->mutex);
968968
sqlite3_mutex_enter(db->mutex);
969-
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: entered database mutex %p\n", db->mutex);
969+
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: entered database mutex %p", db->mutex);
970970

971971
codec_set_pass_key(db, db_index, pKey, nKey, CIPHER_WRITE_CTX);
972972

@@ -986,27 +986,27 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
986986
if(rc == SQLITE_OK) {
987987
sqlite3PagerUnref(page);
988988
} else {
989-
CODEC_TRACE("sqlite3_rekey_v2: error %d occurred writing page %d\n", rc, pgno);
989+
CODEC_TRACE("sqlite3_rekey_v2: error %d occurred writing page %d", rc, pgno);
990990
}
991991
} else {
992-
CODEC_TRACE("sqlite3_rekey_v2: error %d occurred getting page %d\n", rc, pgno);
992+
CODEC_TRACE("sqlite3_rekey_v2: error %d occurred getting page %d", rc, pgno);
993993
}
994994
}
995995
}
996996

997997
/* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */
998998
if(rc == SQLITE_OK) {
999-
CODEC_TRACE("sqlite3_rekey_v2: committing\n");
999+
CODEC_TRACE("sqlite3_rekey_v2: committing");
10001000
rc = sqlite3BtreeCommit(pDb->pBt);
10011001
sqlcipher_codec_key_copy(ctx, CIPHER_WRITE_CTX);
10021002
} else {
1003-
CODEC_TRACE("sqlite3_rekey_v2: rollback\n");
1003+
CODEC_TRACE("sqlite3_rekey_v2: rollback");
10041004
sqlite3BtreeRollback(pDb->pBt, SQLITE_ABORT_ROLLBACK, 0);
10051005
}
10061006

1007-
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: leaving database mutex %p\n", db->mutex);
1007+
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: leaving database mutex %p", db->mutex);
10081008
sqlite3_mutex_leave(db->mutex);
1009-
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: left database mutex %p\n", db->mutex);
1009+
CODEC_TRACE_MUTEX("sqlite3_rekey_v2: left database mutex %p", db->mutex);
10101010
}
10111011
return SQLITE_OK;
10121012
}
@@ -1015,7 +1015,7 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
10151015

10161016
void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
10171017
struct Db *pDb = &db->aDb[nDb];
1018-
CODEC_TRACE("sqlite3CodecGetKey: entered db=%p, nDb=%d\n", db, nDb);
1018+
CODEC_TRACE("sqlite3CodecGetKey: entered db=%p, nDb=%d", db, nDb);
10191019
if( pDb->pBt ) {
10201020
codec_ctx *ctx = (codec_ctx*) sqlite3PagerGetCodec(pDb->pBt->pBt->pPager);
10211021

src/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void sqlcipher_trace(unsigned int tag, const char *message, ...);
317317
#ifdef __ANDROID__
318318
#define CODEC_TRACE(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
319319
#else
320-
#define CODEC_TRACE(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
320+
#define CODEC_TRACE(...) {fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr);}
321321
#endif
322322
#else
323323
#ifdef SQLCIPHER_OMIT_TRACE

0 commit comments

Comments
 (0)