eda/sqlCipher; use 'PRAGMA cipher_use_hmac = OFF;'. to support sqlcipher version...
authorRejean Loyer <rejean.loyer@gmail.com>
Mon, 4 Mar 2013 20:04:48 +0000 (15:04 -0500)
committerJerome St-Louis <jerome@ecere.com>
Wed, 6 Mar 2013 13:23:09 +0000 (08:23 -0500)
note from sqlcipher api documentation:
   SQLCipher 2.0 introduced a per-page HMAC to validate that the page data has not be tampered
   with. By default, when creating or opening a database using SQLCipher 2, SQLCipher will
   attempt to use an HMAC check. This change in database format means that SQLCipher 2 can't
   operate on version 1.1.x databases by default. Thus, in order to provide backward
   compatibility with SQLCipher 1.1.x, PRAGMA cipher_use_hmac can be used to disable the HMAC
   functionality on specific databases.

eda/drivers/sqliteCipher/EDASQLiteCipher.ec

index 3c153ed..32a990c 100644 (file)
@@ -40,32 +40,36 @@ static class SQLiteCipherDataSource : SQLiteDataSource
          
          if(sqlite3_open_v2(path, &db, (createOptions == readOnly) ? SQLITE_OPEN_READONLY :
             (SQLITE_OPEN_READWRITE | ((createOptions == create) ? SQLITE_OPEN_CREATE : 0)), null))
-         {
             // fprintf(stderr, "%s\n", s); // interesting
             printf($"Can't open database (%s): %s\n", path, sqlite3_errmsg(db));
-            sqlite3_close(db);
-         }
          else
          {
+            int rc = SQLITE_ERROR;
             char command[1024];
             if(ds.pass && ds.pass[0])
             {
                sprintf(command, "PRAGMA key = '%s';", ds.pass);
                sqlite3_exec(db, command, null, null, null);
-               if(sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null) == SQLITE_OK)
+               rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
+               if(rc == SQLITE_NOTADB)
                {
-                  PrintLn($"EDASQLiteCipher: password is correct, database has been initialized");
+                  printf($"EDASQLiteCipher: database (%s) format not recognized, disabling cipher_use_hmac to support version 1.1.x databases\n", path);
+                  strcpy(command, "PRAGMA cipher_use_hmac = OFF;");
+                  sqlite3_exec(db, command, null, null, null);
+                  rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
                }
-               else
+               if(rc == SQLITE_OK)
                {
-                  PrintLn($"EDASQLiteCipher: incorrect password!");
+                  sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
+                  sqlite3_exec(db, command, null, null, null);
+                  result = SQLiteDatabase { db = db };
                }
+               else
+                  printf($"Can't open database (%s): %s -- password may be incorrect\n", path, sqlite3_errstr(rc));
             }
-            sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
-            sqlite3_exec(db, command, null, null, null);
-
-            result = SQLiteDatabase { db = db };
          }            
+         if(!result)
+            sqlite3_close(db);
          delete path;
       }
       return result;