Skip to content

Commit 7f2b44c

Browse files
committed
DRY up and inline pragma settings
1 parent 96a7738 commit 7f2b44c

File tree

3 files changed

+48
-96
lines changed

3 files changed

+48
-96
lines changed

src/crypto.c

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,6 @@ void codec_vdbe_return_static_string(Parse *pParse, const char *zLabel, const ch
5151
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
5252
}
5353

54-
int codec_set_kdf_iter(sqlite3* db, int nDb, int kdf_iter, int for_ctx) {
55-
struct Db *pDb = &db->aDb[nDb];
56-
CODEC_TRACE(("codec_set_kdf_iter: entered db=%p nDb=%d kdf_iter=%d for_ctx=%d\n", db, nDb, kdf_iter, for_ctx));
57-
58-
if(pDb->pBt) {
59-
codec_ctx *ctx;
60-
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
61-
if(ctx) return sqlcipher_codec_ctx_set_kdf_iter(ctx, kdf_iter, for_ctx);
62-
}
63-
return SQLITE_ERROR;
64-
}
65-
66-
int codec_set_fast_kdf_iter(sqlite3* db, int nDb, int kdf_iter, int for_ctx) {
67-
struct Db *pDb = &db->aDb[nDb];
68-
CODEC_TRACE(("codec_set_kdf_iter: entered db=%p nDb=%d kdf_iter=%d for_ctx=%d\n", db, nDb, kdf_iter, for_ctx));
69-
70-
if(pDb->pBt) {
71-
codec_ctx *ctx;
72-
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
73-
if(ctx) return sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, kdf_iter, for_ctx);
74-
}
75-
return SQLITE_ERROR;
76-
}
77-
7854
static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ctx) {
7955
int rc, page_sz, reserve_sz;
8056

@@ -93,103 +69,66 @@ static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ct
9369
return rc;
9470
}
9571

96-
void codec_set_default_use_hmac(int use) {
97-
sqlcipher_set_default_use_hmac(use);
98-
}
99-
100-
int codec_set_use_hmac(sqlite3* db, int nDb, int use) {
101-
struct Db *pDb = &db->aDb[nDb];
102-
103-
CODEC_TRACE(("codec_set_use_hmac: entered db=%p nDb=%d use=%d\n", db, nDb, use));
104-
105-
if(pDb->pBt) {
106-
int rc;
107-
codec_ctx *ctx;
108-
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
109-
if(ctx) {
110-
rc = sqlcipher_codec_ctx_set_use_hmac(ctx, use);
111-
if(rc != SQLITE_OK) return rc;
112-
/* since the use of hmac has changed, the page size may also change */
113-
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
114-
}
115-
}
116-
return SQLITE_ERROR;
117-
}
118-
119-
int codec_set_page_size(sqlite3* db, int nDb, int size) {
72+
int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
12073
struct Db *pDb = &db->aDb[nDb];
121-
CODEC_TRACE(("codec_set_page_size: entered db=%p nDb=%d size=%d\n", db, nDb, size));
122-
74+
CODEC_TRACE(("codec_set_pass_key: entered db=%p nDb=%d zKey=%s nKey=%d for_ctx=%d\n", db, nDb, (char *)zKey, nKey, for_ctx));
12375
if(pDb->pBt) {
124-
int rc;
12576
codec_ctx *ctx;
12677
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
127-
128-
if(ctx) {
129-
rc = sqlcipher_codec_ctx_set_pagesize(ctx, size);
130-
if(rc != SQLITE_OK) return rc;
131-
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
132-
}
78+
if(ctx) return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx);
13379
}
13480
return SQLITE_ERROR;
135-
}
81+
}
13682

137-
/**
138-
*
139-
* when for_ctx == 0 then it will change for read
140-
* when for_ctx == 1 then it will change for write
141-
* when for_ctx == 2 then it will change for both
142-
*/
143-
int codec_set_cipher_name(sqlite3* db, int nDb, const char *cipher_name, int for_ctx) {
144-
struct Db *pDb = &db->aDb[nDb];
145-
CODEC_TRACE(("codec_set_cipher_name: entered db=%p nDb=%d cipher_name=%s for_ctx=%d\n", db, nDb, cipher_name, for_ctx));
83+
int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) {
84+
struct Db *pDb = &db->aDb[iDb];
85+
codec_ctx *ctx = NULL;
86+
int rc;
14687

14788
if(pDb->pBt) {
148-
codec_ctx *ctx;
14989
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
150-
if(ctx) return sqlcipher_codec_ctx_set_cipher(ctx, cipher_name, for_ctx);
15190
}
152-
return SQLITE_ERROR;
153-
}
15491

155-
int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
156-
struct Db *pDb = &db->aDb[nDb];
157-
CODEC_TRACE(("codec_set_pass_key: entered db=%p nDb=%d zKey=%s nKey=%d for_ctx=%d\n", db, nDb, (char *)zKey, nKey, for_ctx));
158-
if(pDb->pBt) {
159-
codec_ctx *ctx;
160-
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
161-
if(ctx) return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx);
162-
}
163-
return SQLITE_ERROR;
164-
}
92+
CODEC_TRACE(("codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx));
16593

166-
int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) {
16794
if( sqlite3StrICmp(zLeft, "cipher_version")==0 && !zRight ){
16895
codec_vdbe_return_static_string(pParse, "cipher_version", codec_get_cipher_version());
16996
}else
17097
if( sqlite3StrICmp(zLeft, "cipher")==0 && zRight ){
171-
codec_set_cipher_name(db, iDb, zRight, 2); // change cipher for both
98+
if(ctx) sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both
17299
}else
173100
if( sqlite3StrICmp(zLeft, "rekey_cipher")==0 && zRight ){
174-
codec_set_cipher_name(db, iDb, zRight, 1); // change write cipher only
101+
if(ctx) sqlcipher_codec_ctx_set_cipher(ctx, zRight, 1); // change write cipher only
175102
}else
176103
if( sqlite3StrICmp(zLeft, "kdf_iter")==0 && zRight ){
177-
codec_set_kdf_iter(db, iDb, atoi(zRight), 2); // change of RW PBKDF2 iteration
104+
if(ctx) sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration
178105
}else
179106
if( sqlite3StrICmp(zLeft, "fast_kdf_iter")==0 && zRight ){
180-
codec_set_fast_kdf_iter(db, iDb, atoi(zRight), 2); // change of RW PBKDF2 iteration
107+
if(ctx) sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration
181108
}else
182109
if( sqlite3StrICmp(zLeft, "rekey_kdf_iter")==0 && zRight ){
183-
codec_set_kdf_iter(db, iDb, atoi(zRight), 1); // change # if W iterations
110+
if(ctx) sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 1); // write iterations only
184111
}else
185112
if( sqlite3StrICmp(zLeft,"cipher_page_size")==0 ){
186-
codec_set_page_size(db, iDb, atoi(zRight)); // change page size
113+
if(ctx) {
114+
int size = atoi(zRight);
115+
rc = sqlcipher_codec_ctx_set_pagesize(ctx, size);
116+
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
117+
rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
118+
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
119+
}
187120
}else
188121
if( sqlite3StrICmp(zLeft,"cipher_default_use_hmac")==0 ){
189-
codec_set_default_use_hmac(sqlite3GetBoolean(zRight,1));
122+
sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1));
190123
}else
191124
if( sqlite3StrICmp(zLeft,"cipher_use_hmac")==0 ){
192-
codec_set_use_hmac(db, iDb, sqlite3GetBoolean(zRight,1));
125+
if(ctx) {
126+
rc = sqlcipher_codec_ctx_set_use_hmac(ctx, sqlite3GetBoolean(zRight,1));
127+
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
128+
/* since the use of hmac has changed, the page size may also change */
129+
rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
130+
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
131+
}
193132
}else {
194133
return 0;
195134
}

src/crypto.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ void sqlcipher_codec_ctx_set_error(codec_ctx *, int);
159159
int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int);
160160
void sqlcipher_codec_get_pass(codec_ctx *, void **zKey, int *nKey);
161161

162-
int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight);
163-
164162
int sqlcipher_codec_ctx_set_pagesize(codec_ctx *, int);
165163
int sqlcipher_codec_ctx_get_pagesize(codec_ctx *);
166164
int sqlcipher_codec_ctx_get_reservesize(codec_ctx *);
@@ -179,6 +177,10 @@ void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
179177
void sqlcipher_set_default_use_hmac(int use);
180178

181179
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
180+
181+
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
182+
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
183+
182184
/* end extensions defined in crypto_impl.c */
183185

184186
#endif

src/crypto_impl.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,29 @@ int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
363363

364364

365365
if(use) {
366-
ctx->write_ctx->flags |= CIPHER_FLAG_HMAC;
367-
ctx->read_ctx->flags |= CIPHER_FLAG_HMAC;
366+
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_HMAC);
368367
} else {
369-
ctx->write_ctx->flags &= ~CIPHER_FLAG_HMAC;
370-
ctx->read_ctx->flags &= ~CIPHER_FLAG_HMAC;
368+
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_HMAC);
371369
}
372370

373371
ctx->write_ctx->reserve_sz = ctx->read_ctx->reserve_sz = reserve;
374372

375373
return SQLITE_OK;
376374
}
377375

376+
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag) {
377+
ctx->write_ctx->flags |= flag;
378+
ctx->read_ctx->flags |= flag;
379+
return SQLITE_OK;
380+
}
381+
382+
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag) {
383+
ctx->write_ctx->flags &= ~flag;
384+
ctx->read_ctx->flags &= ~flag;
385+
return SQLITE_OK;
386+
}
387+
388+
378389
void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) {
379390
CODEC_TRACE(("sqlcipher_codec_ctx_set_error: ctx=%p, error=%d\n", ctx, error));
380391
sqlite3pager_sqlite3PagerSetError(ctx->pBt->pBt->pPager, error);

0 commit comments

Comments
 (0)