Skip to content

Commit 987b1e9

Browse files
committed
Apply adjustments for WASM support
- Change order of checks for getentropy availability (check symbol __WASM__ first) - Fix implementation of VFS I/O method xSectorSize (must check for null pointer) - Use same I/O methods version as underlying implementation
1 parent 85be37e commit 987b1e9

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

src/chacha20poly1305.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,16 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
235235
/*
236236
* Platform-specific entropy functions for seeding RNG
237237
*/
238-
#if defined(_WIN32) || defined(__CYGWIN__)
238+
#if defined(__WASM__)
239+
240+
extern size_t getentropy(void* buf, size_t n);
241+
242+
static size_t entropy(void* buf, size_t n)
243+
{
244+
return (getentropy(buf, n) == 0) ? n : 0;
245+
}
246+
247+
#elif defined(_WIN32) || defined(__CYGWIN__)
239248

240249
#if SQLITE3MC_USE_RAND_S
241250

@@ -389,15 +398,6 @@ static size_t entropy(void* buf, size_t n)
389398
return read_urandom(buf, n);
390399
}
391400

392-
#elif defined(__WASM__)
393-
394-
extern size_t getentropy(void* buf, size_t n);
395-
396-
static size_t entropy(void* buf, size_t n)
397-
{
398-
return (getentropy(buf, n) == 0) ? n : 0;
399-
}
400-
401401
#else
402402
# error "Secure pseudorandom number generator not implemented for this OS"
403403
#endif

src/sqlite3mc_vfs.c

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,57 @@ static const int walFileHeaderSize = 32;
107107
/*
108108
** Global I/O method structure of SQLite3 Multiple Ciphers VFS
109109
*/
110-
static sqlite3_io_methods mcIoMethodsGlobal =
110+
111+
#define IOMETHODS_VERSION_MIN 1
112+
#define IOMETHODS_VERSION_MAX 3
113+
114+
static sqlite3_io_methods mcIoMethodsGlobal1 =
115+
{
116+
1, /* iVersion */
117+
mcIoClose, /* xClose */
118+
mcIoRead, /* xRead */
119+
mcIoWrite, /* xWrite */
120+
mcIoTruncate, /* xTruncate */
121+
mcIoSync, /* xSync */
122+
mcIoFileSize, /* xFileSize */
123+
mcIoLock, /* xLock */
124+
mcIoUnlock, /* xUnlock */
125+
mcIoCheckReservedLock, /* xCheckReservedLock */
126+
mcIoFileControl, /* xFileControl */
127+
mcIoSectorSize, /* xSectorSize */
128+
mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
129+
0, /* xShmMap */
130+
0, /* xShmLock */
131+
0, /* xShmBarrier */
132+
0, /* xShmUnmap */
133+
0, /* xFetch */
134+
0, /* xUnfetch */
135+
};
136+
137+
static sqlite3_io_methods mcIoMethodsGlobal2 =
138+
{
139+
2, /* iVersion */
140+
mcIoClose, /* xClose */
141+
mcIoRead, /* xRead */
142+
mcIoWrite, /* xWrite */
143+
mcIoTruncate, /* xTruncate */
144+
mcIoSync, /* xSync */
145+
mcIoFileSize, /* xFileSize */
146+
mcIoLock, /* xLock */
147+
mcIoUnlock, /* xUnlock */
148+
mcIoCheckReservedLock, /* xCheckReservedLock */
149+
mcIoFileControl, /* xFileControl */
150+
mcIoSectorSize, /* xSectorSize */
151+
mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
152+
mcIoShmMap, /* xShmMap */
153+
mcIoShmLock, /* xShmLock */
154+
mcIoShmBarrier, /* xShmBarrier */
155+
mcIoShmUnmap, /* xShmUnmap */
156+
0, /* xFetch */
157+
0, /* xUnfetch */
158+
};
159+
160+
static sqlite3_io_methods mcIoMethodsGlobal3 =
111161
{
112162
3, /* iVersion */
113163
mcIoClose, /* xClose */
@@ -130,6 +180,9 @@ static sqlite3_io_methods mcIoMethodsGlobal =
130180
mcIoUnfetch, /* xUnfetch */
131181
};
132182

183+
static sqlite3_io_methods* mcIoMethodsGlobal[] =
184+
{ 0, &mcIoMethodsGlobal1 , &mcIoMethodsGlobal2 , &mcIoMethodsGlobal3 };
185+
133186
/*
134187
** Internal functions
135188
*/
@@ -304,7 +357,9 @@ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdrMC* pPg)
304357
{
305358
sqlite3_file* pFile = sqlite3PagerFile(pPg->pPager);
306359
void* aData = 0;
307-
if (pFile->pMethods == &mcIoMethodsGlobal)
360+
if (pFile->pMethods == &mcIoMethodsGlobal1 ||
361+
pFile->pMethods == &mcIoMethodsGlobal2 ||
362+
pFile->pMethods == &mcIoMethodsGlobal3)
308363
{
309364
sqlite3mc_file* mcFile = (sqlite3mc_file*) pFile;
310365
Codec* codec = mcFile->codec;
@@ -406,9 +461,18 @@ static int mcVfsOpen(sqlite3_vfs* pVfs, const char* zName, sqlite3_file* pFile,
406461
if (rc == SQLITE_OK)
407462
{
408463
/*
409-
** Real open succeeded, initialize methods, register main database files
464+
** Real open succeeded
465+
** Initialize methods (use same version number as underlying implementation
466+
** Register main database files
410467
*/
411-
pFile->pMethods = &mcIoMethodsGlobal;
468+
int ioMethodsVersion = mcFile->pFile->pMethods->iVersion;
469+
if (ioMethodsVersion < IOMETHODS_VERSION_MIN ||
470+
ioMethodsVersion > IOMETHODS_VERSION_MAX)
471+
{
472+
/* If version out of range, use highest known version */
473+
ioMethodsVersion = IOMETHODS_VERSION_MAX;
474+
}
475+
pFile->pMethods = mcIoMethodsGlobal[ioMethodsVersion];
412476
if (flags & SQLITE_OPEN_MAIN_DB)
413477
{
414478
mcMainListAdd(mcFile);
@@ -1194,7 +1258,10 @@ static int mcIoFileControl(sqlite3_file* pFile, int op, void* pArg)
11941258

11951259
static int mcIoSectorSize(sqlite3_file* pFile)
11961260
{
1197-
return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
1261+
if (REALFILE(pFile)->pMethods->xSectorSize)
1262+
return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
1263+
else
1264+
return SQLITE_DEFAULT_SECTOR_SIZE;
11981265
}
11991266

12001267
static int mcIoDeviceCharacteristics(sqlite3_file* pFile)

0 commit comments

Comments
 (0)