@@ -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-
7854static 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 }
0 commit comments