diff --git a/src/chacha20poly1305.c b/src/chacha20poly1305.c index 5107f5c..1f381dd 100644 --- a/src/chacha20poly1305.c +++ b/src/chacha20poly1305.c @@ -235,7 +235,16 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16]) /* * Platform-specific entropy functions for seeding RNG */ -#if defined(_WIN32) || defined(__CYGWIN__) +#if defined(__WASM__) + +extern size_t getentropy(void* buf, size_t n); + +static size_t entropy(void* buf, size_t n) +{ + return (getentropy(buf, n) == 0) ? n : 0; +} + +#elif defined(_WIN32) || defined(__CYGWIN__) #if SQLITE3MC_USE_RAND_S @@ -389,15 +398,6 @@ static size_t entropy(void* buf, size_t n) return read_urandom(buf, n); } -#elif defined(__WASM__) - -extern size_t getentropy(void* buf, size_t n); - -static size_t entropy(void* buf, size_t n) -{ - return (getentropy(buf, n) == 0) ? n : 0; -} - #else # error "Secure pseudorandom number generator not implemented for this OS" #endif diff --git a/src/sqlite3mc_vfs.c b/src/sqlite3mc_vfs.c index 9ecd1a6..ed0fc83 100644 --- a/src/sqlite3mc_vfs.c +++ b/src/sqlite3mc_vfs.c @@ -107,7 +107,57 @@ static const int walFileHeaderSize = 32; /* ** Global I/O method structure of SQLite3 Multiple Ciphers VFS */ -static sqlite3_io_methods mcIoMethodsGlobal = + +#define IOMETHODS_VERSION_MIN 1 +#define IOMETHODS_VERSION_MAX 3 + +static sqlite3_io_methods mcIoMethodsGlobal1 = +{ + 1, /* iVersion */ + mcIoClose, /* xClose */ + mcIoRead, /* xRead */ + mcIoWrite, /* xWrite */ + mcIoTruncate, /* xTruncate */ + mcIoSync, /* xSync */ + mcIoFileSize, /* xFileSize */ + mcIoLock, /* xLock */ + mcIoUnlock, /* xUnlock */ + mcIoCheckReservedLock, /* xCheckReservedLock */ + mcIoFileControl, /* xFileControl */ + mcIoSectorSize, /* xSectorSize */ + mcIoDeviceCharacteristics, /* xDeviceCharacteristics */ + 0, /* xShmMap */ + 0, /* xShmLock */ + 0, /* xShmBarrier */ + 0, /* xShmUnmap */ + 0, /* xFetch */ + 0, /* xUnfetch */ +}; + +static sqlite3_io_methods mcIoMethodsGlobal2 = +{ + 2, /* iVersion */ + mcIoClose, /* xClose */ + mcIoRead, /* xRead */ + mcIoWrite, /* xWrite */ + mcIoTruncate, /* xTruncate */ + mcIoSync, /* xSync */ + mcIoFileSize, /* xFileSize */ + mcIoLock, /* xLock */ + mcIoUnlock, /* xUnlock */ + mcIoCheckReservedLock, /* xCheckReservedLock */ + mcIoFileControl, /* xFileControl */ + mcIoSectorSize, /* xSectorSize */ + mcIoDeviceCharacteristics, /* xDeviceCharacteristics */ + mcIoShmMap, /* xShmMap */ + mcIoShmLock, /* xShmLock */ + mcIoShmBarrier, /* xShmBarrier */ + mcIoShmUnmap, /* xShmUnmap */ + 0, /* xFetch */ + 0, /* xUnfetch */ +}; + +static sqlite3_io_methods mcIoMethodsGlobal3 = { 3, /* iVersion */ mcIoClose, /* xClose */ @@ -130,6 +180,9 @@ static sqlite3_io_methods mcIoMethodsGlobal = mcIoUnfetch, /* xUnfetch */ }; +static sqlite3_io_methods* mcIoMethodsGlobal[] = + { 0, &mcIoMethodsGlobal1 , &mcIoMethodsGlobal2 , &mcIoMethodsGlobal3 }; + /* ** Internal functions */ @@ -304,7 +357,9 @@ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdrMC* pPg) { sqlite3_file* pFile = sqlite3PagerFile(pPg->pPager); void* aData = 0; - if (pFile->pMethods == &mcIoMethodsGlobal) + if (pFile->pMethods == &mcIoMethodsGlobal1 || + pFile->pMethods == &mcIoMethodsGlobal2 || + pFile->pMethods == &mcIoMethodsGlobal3) { sqlite3mc_file* mcFile = (sqlite3mc_file*) pFile; Codec* codec = mcFile->codec; @@ -406,9 +461,18 @@ static int mcVfsOpen(sqlite3_vfs* pVfs, const char* zName, sqlite3_file* pFile, if (rc == SQLITE_OK) { /* - ** Real open succeeded, initialize methods, register main database files + ** Real open succeeded + ** Initialize methods (use same version number as underlying implementation + ** Register main database files */ - pFile->pMethods = &mcIoMethodsGlobal; + int ioMethodsVersion = mcFile->pFile->pMethods->iVersion; + if (ioMethodsVersion < IOMETHODS_VERSION_MIN || + ioMethodsVersion > IOMETHODS_VERSION_MAX) + { + /* If version out of range, use highest known version */ + ioMethodsVersion = IOMETHODS_VERSION_MAX; + } + pFile->pMethods = mcIoMethodsGlobal[ioMethodsVersion]; if (flags & SQLITE_OPEN_MAIN_DB) { mcMainListAdd(mcFile); @@ -1194,7 +1258,10 @@ static int mcIoFileControl(sqlite3_file* pFile, int op, void* pArg) static int mcIoSectorSize(sqlite3_file* pFile) { - return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile)); + if (REALFILE(pFile)->pMethods->xSectorSize) + return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile)); + else + return SQLITE_DEFAULT_SECTOR_SIZE; } static int mcIoDeviceCharacteristics(sqlite3_file* pFile)