Debian Codegraph @

ext/misc/cksumvfs.c

raw
1 /*
2 ** 2020-04-20
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file implements a VFS shim that writes a checksum on each page
14 ** of an SQLite database file. When reading pages, the checksum is verified
15 ** and an error is raised if the checksum is incorrect.
16 **
17 ** COMPILING
18 **
19 ** This extension requires SQLite 3.32.0 or later. It uses the
20 ** sqlite3_database_file_object() interface which was added in
21 ** version 3.32.0, so it will not link with an earlier version of
22 ** SQLite.
23 **
24 ** To build this extension as a separately loaded shared library or
25 ** DLL, use compiler command-lines similar to the following:
26 **
27 ** (linux) gcc -fPIC -shared cksumvfs.c -o cksumvfs.so
28 ** (mac) clang -fPIC -dynamiclib cksumvfs.c -o cksumvfs.dylib
29 ** (windows) cl cksumvfs.c -link -dll -out:cksumvfs.dll
30 **
31 ** You may want to add additional compiler options, of course,
32 ** according to the needs of your project.
33 **
34 ** If you want to statically link this extension with your product,
35 ** then compile it like any other C-language module but add the
36 ** "-DSQLITE_CKSUMVFS_STATIC" option so that this module knows that
37 ** it is being statically linked rather than dynamically linked
38 **
39 ** LOADING
40 **
41 ** To load this extension as a shared library, you first have to
42 ** bring up a dummy SQLite database connection to use as the argument
43 ** to the sqlite3_load_extension() API call. Then you invoke the
44 ** sqlite3_load_extension() API and shutdown the dummy database
45 ** connection. All subsequent database connections that are opened
46 ** will include this extension. For example:
47 **
48 ** sqlite3 *db;
49 ** sqlite3_open(":memory:", &db);
50 ** sqlite3_load_extension(db, "./cksumvfs");
51 ** sqlite3_close(db);
52 **
53 ** If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and
54 ** statically linked against the application, initialize it using
55 ** a single API call as follows:
56 **
57 ** sqlite3_register_cksumvfs();
58 **
59 ** Cksumvfs is a VFS Shim. When loaded, "cksmvfs" becomes the new
60 ** default VFS and it uses the prior default VFS as the next VFS
61 ** down in the stack. This is normally what you want. However, in
62 ** complex situations where multiple VFS shims are being loaded,
63 ** it might be important to ensure that cksumvfs is loaded in the
64 ** correct order so that it sequences itself into the default VFS
65 ** Shim stack in the right order.
66 **
67 ** USING
68 **
69 ** Open database connections using the sqlite3_open() or
70 ** sqlite3_open_v2() interfaces, as normal. Ordinary database files
71 ** (without a checksum) will operate normally. Databases with
72 ** checksums will return an SQLITE_IOERR_DATA error if a page is
73 ** encountered that contains an invalid checksum.
74 **
75 ** Checksumming only works on databases that have a reserve-bytes
76 ** value of exactly 8. The default value for reserve-bytes is 0.
77 ** Hence, newly created database files will omit the checksum by
78 ** default. To create a database that includes a checksum, change
79 ** the reserve-bytes value to 8 by runing:
80 **
81 ** int n = 8;
82 ** sqlite3_file_control(db, 0, SQLITE_FCNTL_RESERVE_BYTES, &n);
83 **
84 ** If you do this immediately after creating a new database file,
85 ** before anything else has been written into the file, then that
86 ** might be all that you need to do. Otherwise, the API call
87 ** above should be followed by:
88 **
89 ** sqlite3_exec(db, "VACUUM", 0, 0, 0);
90 **
91 ** It never hurts to run the VACUUM, even if you don't need it.
92 ** If the database is in WAL mode, you should shutdown and
93 ** reopen all database connections before continuing.
94 **
95 ** From the CLI, use the ".filectrl reserve_bytes 8" command,
96 ** followed by "VACUUM;".
97 **
98 ** Note that SQLite allows the number of reserve-bytes to be
99 ** increased but not decreased. So if a database file already
100 ** has a reserve-bytes value greater than 8, there is no way to
101 ** activate checksumming on that database, other than to dump
102 ** and restore the database file. Note also that other extensions
103 ** might also make use of the reserve-bytes. Checksumming will
104 ** be incompatible with those other extensions.
105 **
106 ** VERIFICATION OF CHECKSUMS
107 **
108 ** If any checksum is incorrect, the "PRAGMA quick_check" command
109 ** will find it. To verify that checksums are actually enabled
110 ** and running, use the following query:
111 **
112 ** SELECT count(*), verify_checksum(data)
113 ** FROM sqlite_dbpage
114 ** GROUP BY 2;
115 **
116 ** There are three possible outputs form the verify_checksum()
117 ** function: 1, 0, and NULL. 1 is returned if the checksum is
118 ** correct. 0 is returned if the checksum is incorrect. NULL
119 ** is returned if the page is unreadable. If checksumming is
120 ** enabled, the read will fail if the checksum is wrong, so the
121 ** usual result from verify_checksum() on a bad checksum is NULL.
122 **
123 ** If everything is OK, the query above should return a single
124 ** row where the second column is 1. Any other result indicates
125 ** either that there is a checksum error, or checksum validation
126 ** is disabled.
127 **
128 ** CONTROLLING CHECKSUM VERIFICATION
129 **
130 ** The cksumvfs extension implements a new PRAGMA statement that can
131 ** be used to disable, re-enable, or query the status of checksum
132 ** verification:
133 **
134 ** PRAGMA checksum_verification; -- query status
135 ** PRAGMA checksum_verification=OFF; -- disable verification
136 ** PRAGMA checksum_verification=ON; -- re-enable verification
137 **
138 ** The "checksum_verification" pragma will return "1" (true) or "0"
139 ** (false) if checksum verification is enabled or disabled, respectively.
140 ** "Verification" in this context means the feature that causes
141 ** SQLITE_IOERR_DATA errors if a checksum mismatch is detected while
142 ** reading. Checksums are always kept up-to-date as long as the
143 ** reserve-bytes value of the database is 8, regardless of the setting
144 ** of this pragma. Checksum verification can be disabled (for example)
145 ** to do forensic analysis of a database that has previously reported
146 ** a checksum error.
147 **
148 ** The "checksum_verification" pragma will always respond with "0" if
149 ** the database file does not have a reserve-bytes value of 8. The
150 ** pragma will return no rows at all if the cksumvfs extension is
151 ** not loaded.
152 **
153 ** IMPLEMENTATION NOTES
154 **
155 ** The checksum is stored in the last 8 bytes of each page. This
156 ** module only operates if the "bytes of reserved space on each page"
157 ** value at offset 20 the SQLite database header is exactly 8. If
158 ** the reserved-space value is not 8, this module is a no-op.
159 */
160 #if defined(SQLITE_AMALGAMATION) && !defined(SQLITE_CKSUMVFS_STATIC)
161 # define SQLITE_CKSUMVFS_STATIC
162 #endif
163 #ifdef SQLITE_CKSUMVFS_STATIC
164 # include "sqlite3.h"
165 #else
166 # include "sqlite3ext.h"
167 SQLITE_EXTENSION_INIT1
168 #endif
169 #include <string.h>
170 #include <assert.h>
171
172
173 /*
174 ** Forward declaration of objects used by this utility
175 */
176 typedef struct sqlite3_vfs CksmVfs;
177 typedef struct CksmFile CksmFile;
178
179 /*
180 ** Useful datatype abbreviations
181 */
182 #if !defined(SQLITE_AMALGAMATION)
183 typedef unsigned char u8;
184 typedef unsigned int u32;
185 #endif
186
187 /* Access to a lower-level VFS that (might) implement dynamic loading,
188 ** access to randomness, etc.
189 */
190 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
191 #define ORIGFILE(p) ((sqlite3_file*)(((CksmFile*)(p))+1))
192
193 /* An open file */
194 struct CksmFile {
195 sqlite3_file base; /* IO methods */
196 const char *zFName; /* Original name of the file */
197 char computeCksm; /* True to compute checksums.
198 ** Always true if reserve size is 8. */
199 char verifyCksm; /* True to verify checksums */
200 CksmFile *pPartner; /* Ptr from WAL to main-db, or from main-db to WAL */
201 };
202
203 /*
204 ** Methods for CksmFile
205 */
206 static int cksmClose(sqlite3_file*);
207 static int cksmRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
208 static int cksmWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
209 static int cksmTruncate(sqlite3_file*, sqlite3_int64 size);
210 static int cksmSync(sqlite3_file*, int flags);
211 static int cksmFileSize(sqlite3_file*, sqlite3_int64 *pSize);
212 static int cksmLock(sqlite3_file*, int);
213 static int cksmUnlock(sqlite3_file*, int);
214 static int cksmCheckReservedLock(sqlite3_file*, int *pResOut);
215 static int cksmFileControl(sqlite3_file*, int op, void *pArg);
216 static int cksmSectorSize(sqlite3_file*);
217 static int cksmDeviceCharacteristics(sqlite3_file*);
218 static int cksmShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
219 static int cksmShmLock(sqlite3_file*, int offset, int n, int flags);
220 static void cksmShmBarrier(sqlite3_file*);
221 static int cksmShmUnmap(sqlite3_file*, int deleteFlag);
222 static int cksmFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
223 static int cksmUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
224
225 /*
226 ** Methods for CksmVfs
227 */
228 static int cksmOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
229 static int cksmDelete(sqlite3_vfs*, const char *zName, int syncDir);
230 static int cksmAccess(sqlite3_vfs*, const char *zName, int flags, int *);
231 static int cksmFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
232 static void *cksmDlOpen(sqlite3_vfs*, const char *zFilename);
233 static void cksmDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
234 static void (*cksmDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
235 static void cksmDlClose(sqlite3_vfs*, void*);
236 static int cksmRandomness(sqlite3_vfs*, int nByte, char *zOut);
237 static int cksmSleep(sqlite3_vfs*, int microseconds);
238 static int cksmCurrentTime(sqlite3_vfs*, double*);
239 static int cksmGetLastError(sqlite3_vfs*, int, char *);
240 static int cksmCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
241 static int cksmSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
242 static sqlite3_syscall_ptr cksmGetSystemCall(sqlite3_vfs*, const char *z);
243 static const char *cksmNextSystemCall(sqlite3_vfs*, const char *zName);
244
245 static sqlite3_vfs cksm_vfs = {
246 3, /* iVersion (set when registered) */
247 0, /* szOsFile (set when registered) */
248 1024, /* mxPathname */
249 0, /* pNext */
250 "cksmvfs", /* zName */
251 0, /* pAppData (set when registered) */
252 cksmOpen, /* xOpen */
253 cksmDelete, /* xDelete */
254 cksmAccess, /* xAccess */
255 cksmFullPathname, /* xFullPathname */
256 cksmDlOpen, /* xDlOpen */
257 cksmDlError, /* xDlError */
258 cksmDlSym, /* xDlSym */
259 cksmDlClose, /* xDlClose */
260 cksmRandomness, /* xRandomness */
261 cksmSleep, /* xSleep */
262 cksmCurrentTime, /* xCurrentTime */
263 cksmGetLastError, /* xGetLastError */
264 cksmCurrentTimeInt64, /* xCurrentTimeInt64 */
265 cksmSetSystemCall, /* xSetSystemCall */
266 cksmGetSystemCall, /* xGetSystemCall */
267 cksmNextSystemCall /* xNextSystemCall */
268 };
269
270 static const sqlite3_io_methods cksm_io_methods = {
271 3, /* iVersion */
272 cksmClose, /* xClose */
273 cksmRead, /* xRead */
274 cksmWrite, /* xWrite */
275 cksmTruncate, /* xTruncate */
276 cksmSync, /* xSync */
277 cksmFileSize, /* xFileSize */
278 cksmLock, /* xLock */
279 cksmUnlock, /* xUnlock */
280 cksmCheckReservedLock, /* xCheckReservedLock */
281 cksmFileControl, /* xFileControl */
282 cksmSectorSize, /* xSectorSize */
283 cksmDeviceCharacteristics, /* xDeviceCharacteristics */
284 cksmShmMap, /* xShmMap */
285 cksmShmLock, /* xShmLock */
286 cksmShmBarrier, /* xShmBarrier */
287 cksmShmUnmap, /* xShmUnmap */
288 cksmFetch, /* xFetch */
289 cksmUnfetch /* xUnfetch */
290 };
291
292 /* Do byte swapping on a unsigned 32-bit integer */
293 #define BYTESWAP32(x) ( \
294 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
295 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
296 )
297
298 /* Compute a checksum on a buffer */
299 static void cksmCompute(
300 u8 *a, /* Content to be checksummed */
301 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
302 u8 *aOut /* OUT: Final 8-byte checksum value output */
303 ){
304 u32 s1 = 0, s2 = 0;
305 u32 *aData = (u32*)a;
306 u32 *aEnd = (u32*)&a[nByte];
307 u32 x = 1;
308
309 assert( nByte>=8 );
310 assert( (nByte&0x00000007)==0 );
311 assert( nByte<=65536 );
312
313 if( 1 == *(u8*)&x ){
314 /* Little-endian */
315 do {
316 s1 += *aData++ + s2;
317 s2 += *aData++ + s1;
318 }while( aData<aEnd );
319 }else{
320 /* Big-endian */
321 do {
322 s1 += BYTESWAP32(aData[0]) + s2;
323 s2 += BYTESWAP32(aData[1]) + s1;
324 aData += 2;
325 }while( aData<aEnd );
326 s1 = BYTESWAP32(s1);
327 s2 = BYTESWAP32(s2);
328 }
329 memcpy(aOut, &s1, 4);
330 memcpy(aOut+4, &s2, 4);
331 }
332
333 /*
334 ** SQL function: verify_checksum(BLOB)
335 **
336 ** Return 0 or 1 if the checksum is invalid or valid. Or return
337 ** NULL if the input is not a BLOB that is the right size for a
338 ** database page.
339 */
340 static void cksmVerifyFunc(
341 sqlite3_context *context,
342 int argc,
343 sqlite3_value **argv
344 ){
345 int nByte;
346 u8 *data;
347 u8 cksum[8];
348 data = (u8*)sqlite3_value_blob(argv[0]);
349 if( data==0 ) return;
350 if( sqlite3_value_type(argv[0])!=SQLITE_BLOB ) return;
351 nByte = sqlite3_value_bytes(argv[0]);
352 if( nByte<512 || nByte>65536 || (nByte & (nByte-1))!=0 ) return;
353 cksmCompute(data, nByte-8, cksum);
354 sqlite3_result_int(context, memcmp(data+nByte-8,cksum,8)==0);
355 }
356
357 #ifdef SQLITE_CKSUMVFS_INIT_FUNCNAME
358 /*
359 ** SQL function: initialize_cksumvfs(SCHEMANAME)
360 **
361 ** This SQL functions (whose name is actually determined at compile-time
362 ** by the value of the SQLITE_CKSUMVFS_INIT_FUNCNAME macro) invokes:
363 **
364 ** sqlite3_file_control(db, SCHEMANAME, SQLITE_FCNTL_RESERVE_BYTE, &n);
365 **
366 ** In order to set the reserve bytes value to 8, so that cksumvfs will
367 ** operation. This feature is provided (if and only if the
368 ** SQLITE_CKSUMVFS_INIT_FUNCNAME compile-time option is set to a string
369 ** which is the name of the SQL function) so as to provide the ability
370 ** to invoke the file-control in programming languages that lack
371 ** direct access to the sqlite3_file_control() interface (ex: Java).
372 **
373 ** This interface is undocumented, apart from this comment. Usage
374 ** example:
375 **
376 ** 1. Compile with -DSQLITE_CKSUMVFS_INIT_FUNCNAME="ckvfs_init"
377 ** 2. Run: "SELECT cksum_init('main'); VACUUM;"
378 */
379 static void cksmInitFunc(
380 sqlite3_context *context,
381 int argc,
382 sqlite3_value **argv
383 ){
384 int nByte = 8;
385 const char *zSchemaName = (const char*)sqlite3_value_text(argv[0]);
386 sqlite3 *db = sqlite3_context_db_handle(context);
387 sqlite3_file_control(db, zSchemaName, SQLITE_FCNTL_RESERVE_BYTES, &nByte);
388 /* Return NULL */
389 }
390 #endif /* SQLITE_CKSUMBFS_INIT_FUNCNAME */
391
392 /*
393 ** Close a cksm-file.
394 */
395 static int cksmClose(sqlite3_file *pFile){
396 CksmFile *p = (CksmFile *)pFile;
397 if( p->pPartner ){
398 assert( p->pPartner->pPartner==p );
399 p->pPartner->pPartner = 0;
400 p->pPartner = 0;
401 }
402 pFile = ORIGFILE(pFile);
403 return pFile->pMethods->xClose(pFile);
404 }
405
406 /*
407 ** Set the computeCkSm and verifyCksm flags, if they need to be
408 ** changed.
409 */
410 static void cksmSetFlags(CksmFile *p, int hasCorrectReserveSize){
411 if( hasCorrectReserveSize!=p->computeCksm ){
412 p->computeCksm = p->verifyCksm = hasCorrectReserveSize;
413 if( p->pPartner ){
414 p->pPartner->verifyCksm = hasCorrectReserveSize;
415 p->pPartner->computeCksm = hasCorrectReserveSize;
416 }
417 }
418 }
419
420 /*
421 ** Read data from a cksm-file.
422 */
423 static int cksmRead(
424 sqlite3_file *pFile,
425 void *zBuf,
426 int iAmt,
427 sqlite_int64 iOfst
428 ){
429 int rc;
430 CksmFile *p = (CksmFile *)pFile;
431 pFile = ORIGFILE(pFile);
432 rc = pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst);
433 if( rc==SQLITE_OK ){
434 if( iOfst==0 && iAmt>=100 && (
435 memcmp(zBuf,"SQLite format 3",16)==0 || memcmp(zBuf,"ZV-",3)==0
436 )){
437 u8 *d = (u8*)zBuf;
438 char hasCorrectReserveSize = (d[20]==8);
439 cksmSetFlags(p, hasCorrectReserveSize);
440 }
441 /* Verify the checksum if
442 ** (1) the size indicates that we are dealing with a complete
443 ** database page
444 ** (2) checksum verification is enabled
445 */
446 if( iAmt>=512 && (iAmt & (iAmt-1))==0 /* (1) */
447 && p->verifyCksm /* (2) */
448 ){
449 u8 cksum[8];
450 cksmCompute((u8*)zBuf, iAmt-8, cksum);
451 if( memcmp((u8*)zBuf+iAmt-8, cksum, 8)!=0 ){
452 sqlite3_log(SQLITE_IOERR_DATA,
453 "checksum fault offset %lld of \"%s\"",
454 iOfst, p->zFName);
455 rc = SQLITE_IOERR_DATA;
456 }
457 }
458 }
459 return rc;
460 }
461
462 /*
463 ** Write data to a cksm-file.
464 */
465 static int cksmWrite(
466 sqlite3_file *pFile,
467 const void *zBuf,
468 int iAmt,
469 sqlite_int64 iOfst
470 ){
471 CksmFile *p = (CksmFile *)pFile;
472 pFile = ORIGFILE(pFile);
473 if( iOfst==0 && iAmt>=100 && (
474 memcmp(zBuf,"SQLite format 3",16)==0 || memcmp(zBuf,"ZV-",3)==0
475 )){
476 u8 *d = (u8*)zBuf;
477 char hasCorrectReserveSize = (d[20]==8);
478 cksmSetFlags(p, hasCorrectReserveSize);
479 }
480 /* If the write size is appropriate for a database page and if
481 ** checksums where ever enabled, then it will be safe to compute
482 ** the checksums. The reserve byte size might have increased, but
483 ** it will never decrease. And because it cannot decrease, the
484 ** checksum will not overwrite anything.
485 */
486 if( iAmt>=512
487 && p->computeCksm
488 ){
489 cksmCompute((u8*)zBuf, iAmt-8, ((u8*)zBuf)+iAmt-8);
490 }
491 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst);
492 }
493
494 /*
495 ** Truncate a cksm-file.
496 */
497 static int cksmTruncate(sqlite3_file *pFile, sqlite_int64 size){
498 pFile = ORIGFILE(pFile);
499 return pFile->pMethods->xTruncate(pFile, size);
500 }
501
502 /*
503 ** Sync a cksm-file.
504 */
505 static int cksmSync(sqlite3_file *pFile, int flags){
506 pFile = ORIGFILE(pFile);
507 return pFile->pMethods->xSync(pFile, flags);
508 }
509
510 /*
511 ** Return the current file-size of a cksm-file.
512 */
513 static int cksmFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
514 CksmFile *p = (CksmFile *)pFile;
515 pFile = ORIGFILE(p);
516 return pFile->pMethods->xFileSize(pFile, pSize);
517 }
518
519 /*
520 ** Lock a cksm-file.
521 */
522 static int cksmLock(sqlite3_file *pFile, int eLock){
523 pFile = ORIGFILE(pFile);
524 return pFile->pMethods->xLock(pFile, eLock);
525 }
526
527 /*
528 ** Unlock a cksm-file.
529 */
530 static int cksmUnlock(sqlite3_file *pFile, int eLock){
531 pFile = ORIGFILE(pFile);
532 return pFile->pMethods->xUnlock(pFile, eLock);
533 }
534
535 /*
536 ** Check if another file-handle holds a RESERVED lock on a cksm-file.
537 */
538 static int cksmCheckReservedLock(sqlite3_file *pFile, int *pResOut){
539 pFile = ORIGFILE(pFile);
540 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
541 }
542
543 /*
544 ** File control method. For custom operations on a cksm-file.
545 */
546 static int cksmFileControl(sqlite3_file *pFile, int op, void *pArg){
547 int rc;
548 CksmFile *p = (CksmFile*)pFile;
549 pFile = ORIGFILE(pFile);
550 if( op==SQLITE_FCNTL_PRAGMA ){
551 char **azArg = (char**)pArg;
552 assert( azArg[1]!=0 );
553 if( sqlite3_stricmp(azArg[1],"checksum_verification")==0 ){
554 char *zArg = azArg[2];
555 if( zArg!=0 ){
556 if( (zArg[0]>='1' && zArg[0]<='9')
557 || sqlite3_strlike("enable%",zArg,0)==0
558 || sqlite3_stricmp("yes",zArg)==0
559 || sqlite3_stricmp("on",zArg)==0
560 ){
561 p->verifyCksm = p->computeCksm;
562 }else{
563 p->verifyCksm = 0;
564 }
565 if( p->pPartner ) p->pPartner->verifyCksm = p->verifyCksm;
566 }
567 azArg[0] = sqlite3_mprintf("%d",p->verifyCksm);
568 return SQLITE_OK;
569 }else if( p->computeCksm && azArg[2]!=0
570 && sqlite3_stricmp(azArg[1], "page_size")==0 ){
571 /* Do not allow page size changes on a checksum database */
572 return SQLITE_OK;
573 }
574 }
575 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
576 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
577 *(char**)pArg = sqlite3_mprintf("cksm/%z", *(char**)pArg);
578 }
579 return rc;
580 }
581
582 /*
583 ** Return the sector-size in bytes for a cksm-file.
584 */
585 static int cksmSectorSize(sqlite3_file *pFile){
586 pFile = ORIGFILE(pFile);
587 return pFile->pMethods->xSectorSize(pFile);
588 }
589
590 /*
591 ** Return the device characteristic flags supported by a cksm-file.
592 */
593 static int cksmDeviceCharacteristics(sqlite3_file *pFile){
594 int devchar = 0;
595 pFile = ORIGFILE(pFile);
596 devchar = pFile->pMethods->xDeviceCharacteristics(pFile);
597 return (devchar & ~SQLITE_IOCAP_SUBPAGE_READ);
598 }
599
600 /* Create a shared memory file mapping */
601 static int cksmShmMap(
602 sqlite3_file *pFile,
603 int iPg,
604 int pgsz,
605 int bExtend,
606 void volatile **pp
607 ){
608 pFile = ORIGFILE(pFile);
609 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
610 }
611
612 /* Perform locking on a shared-memory segment */
613 static int cksmShmLock(sqlite3_file *pFile, int offset, int n, int flags){
614 pFile = ORIGFILE(pFile);
615 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
616 }
617
618 /* Memory barrier operation on shared memory */
619 static void cksmShmBarrier(sqlite3_file *pFile){
620 pFile = ORIGFILE(pFile);
621 pFile->pMethods->xShmBarrier(pFile);
622 }
623
624 /* Unmap a shared memory segment */
625 static int cksmShmUnmap(sqlite3_file *pFile, int deleteFlag){
626 pFile = ORIGFILE(pFile);
627 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
628 }
629
630 /* Fetch a page of a memory-mapped file */
631 static int cksmFetch(
632 sqlite3_file *pFile,
633 sqlite3_int64 iOfst,
634 int iAmt,
635 void **pp
636 ){
637 CksmFile *p = (CksmFile *)pFile;
638 if( p->computeCksm ){
639 *pp = 0;
640 return SQLITE_OK;
641 }
642 pFile = ORIGFILE(pFile);
643 if( pFile->pMethods->iVersion>2 && pFile->pMethods->xFetch ){
644 return pFile->pMethods->xFetch(pFile, iOfst, iAmt, pp);
645 }
646 *pp = 0;
647 return SQLITE_OK;
648 }
649
650 /* Release a memory-mapped page */
651 static int cksmUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
652 pFile = ORIGFILE(pFile);
653 if( pFile->pMethods->iVersion>2 && pFile->pMethods->xUnfetch ){
654 return pFile->pMethods->xUnfetch(pFile, iOfst, pPage);
655 }
656 return SQLITE_OK;
657 }
658
659 /*
660 ** Open a cksm file handle.
661 */
662 static int cksmOpen(
663 sqlite3_vfs *pVfs,
664 const char *zName,
665 sqlite3_file *pFile,
666 int flags,
667 int *pOutFlags
668 ){
669 CksmFile *p;
670 sqlite3_file *pSubFile;
671 sqlite3_vfs *pSubVfs;
672 int rc;
673 pSubVfs = ORIGVFS(pVfs);
674 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
675 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
676 }
677 p = (CksmFile*)pFile;
678 memset(p, 0, sizeof(*p));
679 pSubFile = ORIGFILE(pFile);
680 pFile->pMethods = &cksm_io_methods;
681 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
682 if( rc ) goto cksm_open_done;
683 p->zFName = zName;
684 cksm_open_done:
685 if( rc ) pFile->pMethods = 0;
686 return rc;
687 }
688
689 /*
690 ** All other VFS methods are pass-thrus.
691 */
692 static int cksmDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
693 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
694 }
695 static int cksmAccess(
696 sqlite3_vfs *pVfs,
697 const char *zPath,
698 int flags,
699 int *pResOut
700 ){
701 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
702 }
703 static int cksmFullPathname(
704 sqlite3_vfs *pVfs,
705 const char *zPath,
706 int nOut,
707 char *zOut
708 ){
709 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
710 }
711 static void *cksmDlOpen(sqlite3_vfs *pVfs, const char *zPath){
712 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
713 }
714 static void cksmDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
715 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
716 }
717 static void (*cksmDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
718 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
719 }
720 static void cksmDlClose(sqlite3_vfs *pVfs, void *pHandle){
721 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
722 }
723 static int cksmRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
724 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
725 }
726 static int cksmSleep(sqlite3_vfs *pVfs, int nMicro){
727 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
728 }
729 static int cksmCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
730 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
731 }
732 static int cksmGetLastError(sqlite3_vfs *pVfs, int a, char *b){
733 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
734 }
735 static int cksmCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
736 sqlite3_vfs *pOrig = ORIGVFS(pVfs);
737 int rc;
738 assert( pOrig->iVersion>=2 );
739 if( pOrig->xCurrentTimeInt64 ){
740 rc = pOrig->xCurrentTimeInt64(pOrig, p);
741 }else{
742 double r;
743 rc = pOrig->xCurrentTime(pOrig, &r);
744 *p = (sqlite3_int64)(r*86400000.0);
745 }
746 return rc;
747 }
748 static int cksmSetSystemCall(
749 sqlite3_vfs *pVfs,
750 const char *zName,
751 sqlite3_syscall_ptr pCall
752 ){
753 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
754 }
755 static sqlite3_syscall_ptr cksmGetSystemCall(
756 sqlite3_vfs *pVfs,
757 const char *zName
758 ){
759 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
760 }
761 static const char *cksmNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
762 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
763 }
764
765 /* Register the verify_checksum() SQL function.
766 */
767 static int cksmRegisterFunc(
768 sqlite3 *db,
769 char **pzErrMsg,
770 const sqlite3_api_routines *pApi
771 ){
772 int rc;
773 if( db==0 ) return SQLITE_OK;
774 rc = sqlite3_create_function(db, "verify_checksum", 1,
775 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
776 0, cksmVerifyFunc, 0, 0);
777 #ifdef SQLITE_CKSUMVFS_INIT_FUNCNAME
778 (void)sqlite3_create_function(db, SQLITE_CKSUMVFS_INIT_FUNCNAME, 1,
779 SQLITE_UTF8|SQLITE_DIRECTONLY,
780 0, cksmInitFunc, 0, 0);
781 #endif
782 return rc;
783 }
784
785 /*
786 ** Register the cksum VFS as the default VFS for the system.
787 ** Also make arrangements to automatically register the "verify_checksum()"
788 ** SQL function on each new database connection.
789 */
790 static int cksmRegisterVfs(void){
791 int rc = SQLITE_OK;
792 sqlite3_vfs *pOrig = sqlite3_vfs_find(0);
793 if( pOrig==0 ) return SQLITE_ERROR;
794 cksm_vfs.iVersion = pOrig->iVersion;
795 cksm_vfs.pAppData = pOrig;
796 cksm_vfs.szOsFile = pOrig->szOsFile + sizeof(CksmFile);
797 rc = sqlite3_vfs_register(&cksm_vfs, 1);
798 if( rc==SQLITE_OK ){
799 rc = sqlite3_auto_extension((void(*)(void))cksmRegisterFunc);
800 }
801 return rc;
802 }
803
804 #if defined(SQLITE_CKSUMVFS_STATIC)
805 /* This variant of the initializer runs when the extension is
806 ** statically linked.
807 */
808 int sqlite3_register_cksumvfs(const char *NotUsed){
809 (void)NotUsed;
810 return cksmRegisterVfs();
811 }
812 int sqlite3_unregister_cksumvfs(void){
813 if( sqlite3_vfs_find("cksmvfs") ){
814 sqlite3_vfs_unregister(&cksm_vfs);
815 sqlite3_cancel_auto_extension((void(*)(void))cksmRegisterFunc);
816 }
817 return SQLITE_OK;
818 }
819 #endif /* defined(SQLITE_CKSUMVFS_STATIC */
820
821 #if !defined(SQLITE_CKSUMVFS_STATIC)
822 /* This variant of the initializer function is used when the
823 ** extension is shared library to be loaded at run-time.
824 */
825 #ifdef _WIN32
826 __declspec(dllexport)
827 #endif
828 /*
829 ** This routine is called by sqlite3_load_extension() when the
830 ** extension is first loaded.
831 ***/
832 int sqlite3_cksumvfs_init(
833 sqlite3 *db,
834 char **pzErrMsg,
835 const sqlite3_api_routines *pApi
836 ){
837 int rc;
838 SQLITE_EXTENSION_INIT2(pApi);
839 (void)pzErrMsg; /* not used */
840 rc = cksmRegisterFunc(db, 0, 0);
841 if( rc==SQLITE_OK ){
842 rc = cksmRegisterVfs();
843 }
844 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
845 return rc;
846 }
847 #endif /* !defined(SQLITE_CKSUMVFS_STATIC) */