diff --git a/src/cipher_common.c b/src/cipher_common.c index 7ceeba6..8963fc3 100644 --- a/src/cipher_common.c +++ b/src/cipher_common.c @@ -55,6 +55,7 @@ typedef struct _CipherName char m_name[CIPHER_NAME_MAXLEN]; } CipherName; +static char globalConfigTableName[CIPHER_NAME_MAXLEN] = ""; static int globalCipherCount = 0; static char* globalSentinelName = ""; static CipherName globalCipherNameTable[CODEC_COUNT_LIMIT + 2] = { 0 }; diff --git a/src/cipher_config.c b/src/cipher_config.c index bb1b828..73eb927 100644 --- a/src/cipher_config.c +++ b/src/cipher_config.c @@ -29,18 +29,7 @@ sqlite3mcConfigTable(sqlite3_context* context, int argc, sqlite3_value** argv) SQLITE_PRIVATE CodecParameter* sqlite3mcGetCodecParams(sqlite3* db) { - CodecParameter* codecParams = NULL; - sqlite3_stmt* pStmt = 0; - int rc = sqlite3_prepare_v2(db, "SELECT sqlite3mc_config_table();", -1, &pStmt, 0); - if (rc == SQLITE_OK) - { - if (SQLITE_ROW == sqlite3_step(pStmt)) - { - sqlite3_value* ptrValue = sqlite3_column_value(pStmt, 0); - codecParams = (CodecParameter*) sqlite3_value_pointer(ptrValue, "sqlite3mc_codec_params"); - } - sqlite3_finalize(pStmt); - } + CodecParameter* codecParams = (CodecParameter*) sqlite3_get_clientdata(db, globalConfigTableName); return codecParams; } @@ -909,8 +898,16 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg) { value = sqlite3mc_config(db, "cipher", cipherId); } - rc = SQLITE_OK; - ((char**)pArg)[0] = sqlite3_mprintf("%s", globalCodecDescriptorTable[value - 1].m_name); + if (value > 0) + { + ((char**)pArg)[0] = sqlite3_mprintf("%s", globalCodecDescriptorTable[value - 1].m_name); + rc = SQLITE_OK; + } + else + { + ((char**)pArg)[0] = sqlite3_mprintf("Cipher '%s' could not be located.", pragmaValue); + rc = SQLITE_ERROR; + } } else { diff --git a/src/codecext.c b/src/codecext.c index e7fe618..28173ec 100644 --- a/src/codecext.c +++ b/src/codecext.c @@ -360,7 +360,8 @@ sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey) return rc; } /* Configure cipher from URI parameters if requested */ - if (sqlite3FindFunction(db, "sqlite3mc_config_table", 0, SQLITE_UTF8, 0) == NULL) + void* codecParamTable = sqlite3_get_clientdata(db, globalConfigTableName); + if (codecParamTable == NULL) { /* ** Encryption extension of database connection not yet initialized; diff --git a/src/sqlite3mc.c b/src/sqlite3mc.c index a5d8350..0dd1aba 100644 --- a/src/sqlite3mc.c +++ b/src/sqlite3mc.c @@ -381,7 +381,8 @@ mcRegisterCodecExtensions(sqlite3* db, char** pzErrMsg, const sqlite3_api_routin int rc = SQLITE_OK; CodecParameter* codecParameterTable = NULL; - if (sqlite3FindFunction(db, "sqlite3mc_config_table", 1, SQLITE_UTF8, 0) != NULL) + void* codecParamTable = sqlite3_get_clientdata(db, globalConfigTableName); + if (codecParamTable) { /* Return if codec extension functions are already defined */ return rc; @@ -392,8 +393,7 @@ mcRegisterCodecExtensions(sqlite3* db, char** pzErrMsg, const sqlite3_api_routin rc = (codecParameterTable != NULL) ? SQLITE_OK : SQLITE_NOMEM; if (rc == SQLITE_OK) { - rc = sqlite3_create_function_v2(db, "sqlite3mc_config_table", 0, SQLITE_UTF8 | SQLITE_DETERMINISTIC, - codecParameterTable, sqlite3mcConfigTable, 0, 0, sqlite3mcFreeCodecParameterTable); + sqlite3_set_clientdata(db, globalConfigTableName, codecParameterTable, sqlite3mcFreeCodecParameterTable); } rc = (codecParameterTable != NULL) ? SQLITE_OK : SQLITE_NOMEM; @@ -610,6 +610,15 @@ sqlite3mcInitCipherTables() { size_t n; + /* Initialize global configuration table name */ + sqlite3_randomness(CIPHER_NAME_MAXLEN, globalConfigTableName); + for (n = 0; n < CIPHER_NAME_MAXLEN-1; ++n) + { + if (globalConfigTableName[n] == 0) + globalConfigTableName[n] = '@'; + } + globalConfigTableName[CIPHER_NAME_MAXLEN-1] = 0; + /* Initialize cipher name table */ strcpy(globalCipherNameTable[0].m_name, "global"); for (n = 1; n < CODEC_COUNT_MAX + 2; ++n)