File: EDASQLiteCipher.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (65 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifdef ECERE_STATIC
public import static "ecere"
public import static "EDA"
#else
public import "ecere"
public import "EDA"
#endif

#include "sqlite3.h"

import "EDASQLite.ec"

static class SQLiteCipherDataSource : SQLiteDataSource
{
   class_property(name) = "SQLiteCipher";
   class_property(databaseFileExtension) = "sqlcipher";

   Database OpenDatabase(const String name, CreateOptions createOptions, DataSource ds)
   {
      Database result = null;
      if(name && name[0])
      {
         String path = MakeDatabasePath(name);
         sqlite3 * db;

         // sqlite3_open(path, &db);
         // sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY /*SQLITE_OPEN_READWRITE*/ /*SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE*/, null );

         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));
         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);
               rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
               if(rc == SQLITE_NOTADB)
               {
                  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);
               }
               if(rc == SQLITE_OK)
               {
                  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));
            }
         }
         if(!result)
            sqlite3_close(db);
         delete path;
      }
      return result;
   }
}