Skip to content

Commit

Permalink
Remove code for support of older SQLite versions
Browse files Browse the repository at this point in the history
Only SQLite version 3.32.0 and higher will be supported. Therefore code relevant only to older versions could be removed.

Additionally, fixed an issue with attached databases implicitly using the encryption key of the main database.
  • Loading branch information
utelle committed Aug 10, 2020
1 parent c9aeba0 commit e4d8814
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 70 deletions.
62 changes: 3 additions & 59 deletions src/codecext.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ static int
sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath, const void* zKey, int nKey)
{
/* Attach a key to a database. */
const char* zDbName = db->aDb[nDb].zDbSName;
const char* dbFileName = sqlite3_db_filename(db, zDbName);
Codec* codec = (Codec*) sqlite3_malloc(sizeof(Codec));
int rc = (codec != NULL) ? sqlite3mcCodecInit(codec) : SQLITE_NOMEM;
if (rc != SQLITE_OK)
Expand Down Expand Up @@ -188,7 +190,7 @@ sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath, const void* zKey,
sqlite3mcSetBtree(codec, db->aDb[nDb].pBt);
mcAdjustBtree(db->aDb[nDb].pBt, pageSize, reserved, sqlite3mcGetLegacyWriteCipher(codec));
sqlite3mcCodecSizeChange(codec, pageSize, reserved);
sqlite3mcSetCodec(db, zPath, codec);
sqlite3mcSetCodec(db, dbFileName, codec);
}
else
{
Expand All @@ -210,12 +212,6 @@ sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath, const void* zKey,
}
else
{
#if (SQLITE_VERSION_NUMBER >= 3015000)
const char* zDbName = db->aDb[nDb].zDbSName;
#else
const char* zDbName = db->aDb[nDb].zName;
#endif
const char* dbFileName = sqlite3_db_filename(db, zDbName);
if (dbFileName != NULL)
{
/* Check whether key salt is provided in URI */
Expand Down Expand Up @@ -245,9 +241,6 @@ sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath, const void* zKey,
int reserved = sqlite3mcGetReservedWriteCipher(codec);
mcAdjustBtree(db->aDb[nDb].pBt, pageSize, reserved, sqlite3mcGetLegacyWriteCipher(codec));
sqlite3mcCodecSizeChange(codec, pageSize, reserved);
#if 0
const char* dbFileName = sqlite3_db_filename(db, zDbName);
#endif
sqlite3mcSetCodec(db, dbFileName, codec);
}
else
Expand Down Expand Up @@ -395,11 +388,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
char* err = NULL;
sqlite3mcSetReadReserved(codec, nReserved);
sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
#if (SQLITE_VERSION_NUMBER >= 3027000)
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
#else
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, nReservedWriteCipher);
#endif
goto leave_rekey;
}
}
Expand All @@ -426,11 +415,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
char* err = NULL;
sqlite3mcSetReadReserved(codec, nReserved);
sqlite3mcSetWriteReserved(codec, 0);
#if (SQLITE_VERSION_NUMBER >= 3027000)
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, 0);
#else
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, 0);
#endif
goto leave_rekey;
}
}
Expand All @@ -451,11 +436,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
char* err = NULL;
sqlite3mcSetReadReserved(codec, nReserved);
sqlite3mcSetWriteReserved(codec, nReservedWriteCipher);
#if (SQLITE_VERSION_NUMBER >= 3027000)
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, NULL, nReservedWriteCipher);
#else
rc = sqlite3mcRunVacuumForRekey(&err, db, dbIndex, nReservedWriteCipher);
#endif
goto leave_rekey;
}
}
Expand All @@ -474,56 +455,27 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
}

/* Start transaction */
#if (SQLITE_VERSION_NUMBER >= 3025000)
rc = sqlite3BtreeBeginTrans(pBt, 1, 0);
#else
rc = sqlite3BtreeBeginTrans(pBt, 1);
#endif
if (!rc)
{
int pageSize = sqlite3BtreeGetPageSize(pBt);
Pgno nSkip = WX_PAGER_MJ_PGNO(pageSize);
#if (SQLITE_VERSION_NUMBER >= 3003014)
DbPage *pPage;
#else
void *pPage;
#endif
Pgno n;
/* Rewrite all pages using the new encryption key (if specified) */
#if (SQLITE_VERSION_NUMBER >= 3007001)
Pgno nPage;
int nPageCount = -1;
sqlite3PagerPagecount(pPager, &nPageCount);
nPage = nPageCount;
#elif (SQLITE_VERSION_NUMBER >= 3006000)
int nPageCount = -1;
int rc = sqlite3PagerPagecount(pPager, &nPageCount);
Pgno nPage = (Pgno) nPageCount;
#elif (SQLITE_VERSION_NUMBER >= 3003014)
Pgno nPage = sqlite3PagerPagecount(pPager);
#else
Pgno nPage = sqlite3pager_pagecount(pPager);
#endif

for (n = 1; rc == SQLITE_OK && n <= nPage; n++)
{
if (n == nSkip) continue;
#if (SQLITE_VERSION_NUMBER >= 3010000)
rc = sqlite3PagerGet(pPager, n, &pPage, 0);
#elif (SQLITE_VERSION_NUMBER >= 3003014)
rc = sqlite3PagerGet(pPager, n, &pPage);
#else
rc = sqlite3pager_get(pPager, n, &pPage);
#endif
if (!rc)
{
#if (SQLITE_VERSION_NUMBER >= 3003014)
rc = sqlite3PagerWrite(pPage);
sqlite3PagerUnref(pPage);
#else
rc = sqlite3pager_write(pPage);
sqlite3pager_unref(pPage);
#endif
}
}
}
Expand All @@ -536,15 +488,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
if (rc != SQLITE_OK)
{
/* Rollback in case of error */
#if (SQLITE_VERSION_NUMBER >= 3008007)
/* Unfortunately this change was introduced in version 3.8.7.2 which cannot be detected using the SQLITE_VERSION_NUMBER */
/* That is, compilation will fail for version 3.8.7 or 3.8.7.1 ==> Please change manually ... or upgrade to 3.8.7.2 or higher */
sqlite3BtreeRollback(pBt, SQLITE_OK, 0);
#elif (SQLITE_VERSION_NUMBER >= 3007011)
sqlite3BtreeRollback(pbt, SQLITE_OK);
#else
sqlite3BtreeRollback(pbt);
#endif
}

leave_rekey:
Expand Down
8 changes: 1 addition & 7 deletions src/sqlite3mc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "sqlite3.h"

#ifdef SQLITE_USER_AUHENTICATION
#ifdef SQLITE_USER_AUTHENTICATION
#include "sqlite3userauth.h"
#endif

Expand Down Expand Up @@ -95,24 +95,18 @@
/*
** Define Windows specific SQLite API functions (not defined in sqlite3.h)
*/
#if SQLITE_VERSION_NUMBER >= 3007014
#if SQLITE_OS_WIN == 1

#ifdef __cplusplus
extern "C" {
#endif

#if SQLITE_VERSION_NUMBER >= 3024000
SQLITE_API int sqlite3_win32_set_directory(unsigned long type, void* zValue);
#else
SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue);
#endif

#ifdef __cplusplus
}
#endif

#endif
#endif

#ifdef __cplusplus
Expand Down
4 changes: 0 additions & 4 deletions src/userauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ int sqlite3_user_authenticate(
db->auth.nAuthPW = nPW;
rc = sqlite3UserAuthCheckLogin(db, "main", &authLevel);
db->auth.authLevel = authLevel;
#if (SQLITE_VERSION_NUMBER >= 3025000)
sqlite3ExpirePreparedStatements(db, 0);
#else
sqlite3ExpirePreparedStatements(db);
#endif
if( rc ){
return rc; /* OOM error, I/O error, etc. */
}
Expand Down

0 comments on commit e4d8814

Please sign in to comment.