@@ -906,6 +906,43 @@ const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) {
906906 return ctx -> read_ctx -> provider -> get_provider_name (ctx -> read_ctx );
907907}
908908
909+
910+ static int sqlcipher_check_connection (const char * filename , char * key , int key_sz , char * sql ) {
911+ int rc ;
912+ sqlite3 * db = NULL ;
913+ sqlite3_stmt * statement = NULL ;
914+ char * query_sqlite_master = "SELECT count(*) FROM sqlite_master;" ;
915+
916+ rc = sqlite3_open (filename , & db );
917+ if (rc != SQLITE_OK ){
918+ goto cleanup ;
919+ }
920+ rc = sqlite3_key (db , key , key_sz );
921+ if (rc != SQLITE_OK ){
922+ goto cleanup ;
923+ }
924+ rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
925+ if (rc != SQLITE_OK ){
926+ goto cleanup ;
927+ }
928+ rc = sqlite3_prepare (db , query_sqlite_master , -1 , & statement , NULL );
929+ if (rc != SQLITE_OK ){
930+ goto cleanup ;
931+ }
932+ if (sqlite3_step (statement ) == SQLITE_ROW ){
933+ rc = SQLITE_OK ;
934+ }
935+
936+ cleanup :
937+ if (statement ){
938+ sqlite3_finalize (statement );
939+ }
940+ if (db ){
941+ sqlite3_close (db );
942+ }
943+ return rc ;
944+ }
945+
909946int sqlcipher_codec_ctx_migrate (codec_ctx * ctx ) {
910947 u32 meta ;
911948 int rc = 0 ;
@@ -922,11 +959,11 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
922959 char * query_sqlite_master = "SELECT count(*) from sqlite_master;" ;
923960 char * pragma_hmac_off = "PRAGMA cipher_use_hmac = OFF;" ;
924961 char * pragma_4k_kdf_iter = "PRAGMA kdf_iter = 4000;" ;
962+ char * pragma_1x_and_4k ;
925963 char * key ;
926964 int key_sz ;
927965 int upgrade_1x_format = 0 ;
928966 int upgrade_4k_format = 0 ;
929- sqlite3 * test ;
930967 char * err = 0 ;
931968 static const unsigned char aCopy [] = {
932969 BTREE_SCHEMA_VERSION , 1 , /* Add one to the old schema cookie */
@@ -935,6 +972,8 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
935972 BTREE_USER_VERSION , 0 , /* Preserve the user version */
936973 BTREE_APPLICATION_ID , 0 , /* Preserve the application id */
937974 };
975+
976+
938977 key_sz = ctx -> read_ctx -> pass_sz + 1 ;
939978 key = sqlcipher_malloc (key_sz );
940979 memset (key , 0 , key_sz );
@@ -944,6 +983,12 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
944983
945984 char * attach_command = sqlite3_mprintf ("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';" ,
946985 db_filename , key );
986+ const char * commands [] = {
987+ upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "" ,
988+ upgrade_1x_format == 1 ? pragma_hmac_off : "" ,
989+ attach_command ,
990+ "SELECT sqlcipher_export('migrate');" ,
991+ };
947992
948993 int rc = sqlcipher_check_connection (db_filename , key , key_sz , "" );
949994 if (rc == SQLITE_OK ){
@@ -959,7 +1004,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
9591004 }
9601005
9611006 // Version 1 - check both no hmac and 4k together
962- char * pragma_1x_and_4k = sqlite3_mprintf ("%s%s" , pragma_hmac_off ,
1007+ pragma_1x_and_4k = sqlite3_mprintf ("%s%s" , pragma_hmac_off ,
9631008 pragma_4k_kdf_iter );
9641009 rc = sqlcipher_check_connection (db_filename , key , key_sz , pragma_1x_and_4k );
9651010 sqlite3_free (pragma_1x_and_4k );
@@ -974,13 +1019,8 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
9741019 goto handle_error ;
9751020 }
9761021
977- const char * commands [] = {
978- upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "" ,
979- upgrade_1x_format == 1 ? pragma_hmac_off : "" ,
980- attach_command ,
981- "SELECT sqlcipher_export('migrate');" ,
982- };
983- for (command_idx = 0 ; command_idx < ArraySize (commands ); command_idx ++ ){
1022+
1023+ for (command_idx = 0 ; command_idx < (sizeof (commands )/sizeof (commands [0 ])); command_idx ++ ){
9841024 const char * command = commands [command_idx ];
9851025 if (strcmp (command , "" ) == 0 ){
9861026 continue ;
@@ -994,6 +1034,10 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
9941034 sqlcipher_free (key , key_sz );
9951035
9961036 if (rc == SQLITE_OK ){
1037+ Btree * pDest ;
1038+ Btree * pSrc ;
1039+ int i = 0 ;
1040+
9971041 if ( !db -> autoCommit ){
9981042 CODEC_TRACE (("cannot migrate from within a transaction" ));
9991043 goto handle_error ;
@@ -1014,9 +1058,9 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10141058 db -> flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder );
10151059 db -> xTrace = 0 ;
10161060
1017- Btree * pDest = db -> aDb [0 ].pBt ;
1061+ pDest = db -> aDb [0 ].pBt ;
10181062 pDb = & (db -> aDb [db -> nDb - 1 ]);
1019- Btree * pSrc = pDb -> pBt ;
1063+ pSrc = pDb -> pBt ;
10201064
10211065 rc = sqlite3_exec (db , "BEGIN;" , NULL , NULL , NULL );
10221066 rc = sqlite3BtreeBeginTrans (pSrc , 2 );
@@ -1029,8 +1073,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10291073 sqlite3CodecGetKey (db , db -> nDb - 1 , (void * * )& key , & password_sz );
10301074 sqlite3CodecAttach (db , 0 , key , password_sz );
10311075
1032- int i = 0 ;
1033- for (i = 0 ; i < ArraySize (aCopy ); i += 2 ){
1076+ for (i = 0 ; i < (sizeof (aCopy )/sizeof (aCopy [0 ])); i += 2 ){
10341077 sqlite3BtreeGetMeta (pSrc , aCopy [i ], & meta );
10351078 rc = sqlite3BtreeUpdateMeta (pDest , aCopy [i ], meta + aCopy [i + 1 ]);
10361079 if ( NEVER (rc != SQLITE_OK ) ) goto handle_error ;
@@ -1068,46 +1111,6 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10681111 return rc ;
10691112}
10701113
1071- int sqlcipher_check_connection (char * filename , char * key , int key_sz , char * sql ) {
1072- int rc ;
1073- sqlite3 * db ;
1074- char * errMsg ;
1075- sqlite3_stmt * statement ;
1076- char * query_sqlite_master = "SELECT count(*) FROM sqlite_master;" ;
1077-
1078- rc = sqlite3_open (filename , & db );
1079- if (rc != SQLITE_OK ){
1080- goto cleanup ;
1081- }
1082- rc = sqlite3_key (db , key , key_sz );
1083- if (rc != SQLITE_OK ){
1084- goto cleanup ;
1085- }
1086- rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
1087- if (rc != SQLITE_OK ){
1088- goto cleanup ;
1089- }
1090- rc = sqlite3_prepare (db , query_sqlite_master , -1 , & statement , NULL );
1091- if (rc != SQLITE_OK ){
1092- goto cleanup ;
1093- }
1094- if (sqlite3_step (statement ) == SQLITE_ROW ){
1095- rc = SQLITE_OK ;
1096- }
1097- goto cleanup ;
1098-
1099- cleanup :
1100- if (statement ){
1101- sqlite3_finalize (statement );
1102- }
1103- if (db ){
1104- sqlite3_close (db );
1105- }
1106-
1107- exit :
1108- return rc ;
1109-
1110- }
11111114
11121115#endif
11131116/* END SQLCIPHER */
0 commit comments