Skip to content

Commit 2ecb37e

Browse files
committed
Add pragma hexkey/hexrekey to resolve issue #70
1 parent b0c82a6 commit 2ecb37e

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/cipher_config.c

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,26 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
823823
((char**)pArg)[0] = sqlite3_mprintf("ok");
824824
}
825825
}
826+
else if (sqlite3StrICmp(pragmaName, "hexkey") == 0)
827+
{
828+
int nValue = sqlite3Strlen30(pragmaValue);
829+
if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
830+
{
831+
char* zHexKey = sqlite3_malloc(nValue/2);
832+
sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
833+
rc = sqlite3_key_v2(db, zDbName, zHexKey, nValue/2);
834+
sqlite3_free(zHexKey);
835+
if (rc == SQLITE_OK)
836+
{
837+
((char**)pArg)[0] = sqlite3_mprintf("ok");
838+
}
839+
}
840+
else
841+
{
842+
rc = SQLITE_ERROR;
843+
((char**)pArg)[0] = sqlite3_mprintf("Malformed hex string");
844+
}
845+
}
826846
else if (sqlite3StrICmp(pragmaName, "rekey") == 0)
827847
{
828848
rc = sqlite3_rekey_v2(db, zDbName, pragmaValue, -1);
@@ -842,6 +862,37 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
842862
}
843863
}
844864
}
865+
else if (sqlite3StrICmp(pragmaName, "hexrekey") == 0)
866+
{
867+
int nValue = sqlite3Strlen30(pragmaValue);
868+
if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
869+
{
870+
char* zHexKey = sqlite3_malloc(nValue/2);
871+
sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
872+
rc = sqlite3_rekey_v2(db, zDbName, zHexKey, nValue/2);
873+
sqlite3_free(zHexKey);
874+
if (rc == SQLITE_OK)
875+
{
876+
((char**)pArg)[0] = sqlite3_mprintf("ok");
877+
}
878+
else
879+
{
880+
if (db->pErr)
881+
{
882+
const char* z = (const char*)sqlite3_value_text(db->pErr);
883+
if (z && sqlite3Strlen30(z) > 0)
884+
{
885+
((char**)pArg)[0] = sqlite3_mprintf(z);
886+
}
887+
}
888+
}
889+
}
890+
else
891+
{
892+
rc = SQLITE_ERROR;
893+
((char**)pArg)[0] = sqlite3_mprintf("Malformed hex string");
894+
}
895+
}
845896
else
846897
{
847898
int j;
@@ -906,13 +957,15 @@ sqlite3mcCodecQueryParameters(sqlite3* db, const char* zDb, const char* zUri)
906957
{
907958
u8 iByte;
908959
int i;
909-
char zDecoded[40];
910-
for (i = 0, iByte = 0; i < sizeof(zDecoded) * 2 && sqlite3Isxdigit(zKey[i]); i++)
960+
int nKey = sqlite3Strlen30(zKey);
961+
char* zDecoded = sqlite3_malloc(nKey);
962+
for (i = 0, iByte = 0; i < nKey && sqlite3Isxdigit(zKey[i]); i++)
911963
{
912964
iByte = (iByte << 4) + sqlite3HexToInt(zKey[i]);
913-
if ((i & 1) != 0) zDecoded[i / 2] = iByte;
965+
if ((i & 1) != 0) zDecoded[i/2] = iByte;
914966
}
915-
sqlite3_key_v2(db, zDb, zDecoded, i / 2);
967+
sqlite3_key_v2(db, zDb, zDecoded, i/2);
968+
sqlite3_free(zDecoded);
916969
}
917970
else if ((zKey = sqlite3_uri_parameter(zUri, "key")) != 0)
918971
{

0 commit comments

Comments
 (0)