CFP
CFP

Reputation: 389

how to create database with reserved bytes?

It is possible to reserve bytes at the end of each page in a SQLite database.

According to the documentation:

SQLite has the ability to set aside a small number of extra bytes at the end of every page for use by extensions. These extra bytes are used, for example, by the SQLite Encryption Extension to store a nonce and/or cryptographic checksum associated with each page. The "reserved space" size in the 1-byte integer at offset 20 is the number of bytes of space at the end of each page to reserve for extensions. This value is usually 0. The value can be odd.

I want to use this feature and tried it with creating a normal, empty database and manually changed the byte (header, at offset 20; used a hex editor) to a non-null value (-> x10 for sixteen reserved bytes).

Using .dbinfo in sqlite3:

(…) reserved bytes: 16 (…)

So everything seems to be correct, but no command to edit the database will be successful. Each command (e.g. CREATE TABLE example('id' int);) results in this error message:

Error: database disk image is malformed

So my question is: How can I create a SQLite database with this reserved space (which will not be rejected as malformed)?

Upvotes: 1

Views: 629

Answers (1)

CL.
CL.

Reputation: 180080

The first database page contains not only the header, but also the first B-tree page of the sqlite_master table. You have to ensure that none of that data points to outside of the usable area of the page. If the master table is empty, the only thing to adjust is the pointer to the start of the cell content area at offset 5 of the B-tree header (which in this case should simply point to the end of the usable area).


Anyway, if the SQLite library was not compiled with SQLITE_OMIT_BUILTIN_TEST, you can use the sqlite3_test_control() function to set this value (for an empty database):

sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)

Upvotes: 2

Related Questions