sqlite_core/lib.rs
1//! `sqlite-core` — native, read-only, panic-free `SQLite` file-format reader.
2//!
3//! Parses the 100-byte file header (magic + page size), walks table b-trees
4//! (interior + leaf) yielding rows as typed [`Value`]s, reassembles
5//! overflow-page chains for large payloads, walks the freelist
6//! ([`Database::freelist_pages`]), and applies a read-only `-wal` overlay
7//! ([`Database::open_with_wal`]) — all bounds-checked and panic-free on crafted
8//! input. [`Database::carve_cells`] recognizes record-shaped cells in
9//! free/unallocated space for the analyzer's deleted-record recovery. The bespoke
10//! [`WalTimeline`] ([`Database::wal_timeline`]) models a `-wal` as a salt-bounded
11//! segment of materializable [`CommitSnapshot`]s for "carve all snapshots".
12//!
13//! Format constants are consumed from [`forensicnomicon::sqlite`] (the KNOWLEDGE
14//! leaf), including the page-1 header field offsets (reserved-space 20, in-header
15//! DB-size 28, freelist-count 36, text-encoding 56) promoted there in §3.1.
16//! Index-b-tree LEAF reading is a foundation
17//! ([`Database::index_leaf_cells`], roadmap §1.4) — the second substrate for a
18//! table's data and the storage of `WITHOUT ROWID` rows; carving DELETED index
19//! entries and following index-key overflow remain follow-ups.
20//! (UTF-16 text decoding and WAL frame-checksum verification are implemented.)
21
22#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
23
24pub mod attribution;
25pub mod rebuild;
26pub mod row_history;
27pub mod sqlcipher;
28
29// The page-1 header field offsets are consumed from the KNOWLEDGE leaf
30// (forensicnomicon::sqlite ≥ 1.5.0); the previously-local duplicates were promoted
31// there (roadmap §3.1). Aliased to the historical local names so every use site is
32// unchanged and the names read naturally in context.
33use forensicnomicon::sqlite::{
34 SQLITE_DB_SIZE_OFFSET as DB_SIZE_IN_PAGES_OFFSET,
35 SQLITE_FREELIST_COUNT_OFFSET as FREELIST_COUNT_OFFSET, SQLITE_FREELIST_TRUNK_OFFSET,
36 SQLITE_HEADER_SIZE, SQLITE_MAGIC, SQLITE_PAGE_SIZE_OFFSET,
37 SQLITE_RESERVED_SPACE_OFFSET as RESERVED_SPACE_OFFSET,
38 SQLITE_TEXT_ENCODING_OFFSET as TEXT_ENCODING_OFFSET,
39};
40
41/// Errors that can arise while reading a `SQLite` database, all recoverable —
42/// the reader never panics on malformed input.
43///
44/// `#[non_exhaustive]` so naming a newly-recognised malformation is an additive
45/// change. It is applied in the same release that adds
46/// [`Error::MalformedSerialType`], because that addition is breaking either way
47/// and doing both at once spends the break once instead of twice.
48#[non_exhaustive]
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum Error {
51 /// File is shorter than the 100-byte header.
52 TooShort,
53 /// First 16 bytes are not the `SQLite format 3\0` magic.
54 BadMagic,
55 /// Page-size field is not a power of two in `[512, 65536]`.
56 BadPageSize(u32),
57 /// A page number referenced by the b-tree is out of range for the file.
58 PageOutOfRange(u32),
59 /// A b-tree page had an unexpected type byte where a table page was required.
60 NotATablePage(u8),
61 /// A cell pointer or payload ran past the end of its page.
62 TruncatedCell,
63 /// A record's serial type was negative, so it names no value at all. A
64 /// serial type is a varint, and a damaged record decodes one as a negative
65 /// `i64`. Carries the offending value and the body offset it was read for
66 /// (Show-the-unrecognized-value).
67 MalformedSerialType {
68 /// The serial type as decoded, verbatim.
69 serial: i64,
70 /// Offset into the cell body the value would have started at.
71 offset: usize,
72 },
73 /// The b-tree was deeper / wider than the safety cap allows.
74 TooManyPages,
75 /// The freelist trunk chain cycled or exceeded the file's page count.
76 MalformedFreelist,
77 /// An overflow-page chain cycled or exceeded the file's page count.
78 MalformedOverflow,
79 /// A rollback-journal page size was not a power of two in `[512, 65536]`.
80 /// Carries the offending value (Show-the-unrecognized-value).
81 BadJournalPageSize(u32),
82 /// A rollback journal was applied to a database opened WAL-applied, or whose
83 /// page size disagrees with the journal's. WAL and rollback-journal modes are
84 /// mutually exclusive timelines and must not be overlaid.
85 JournalModeConflict,
86 /// The file could not be opened or read (an I/O failure via
87 /// [`Database::open_path`], not a malformed database). Carries the
88 /// [`std::io::ErrorKind`] (show-the-unrecognized-value).
89 Io(std::io::ErrorKind),
90 /// `SQLCipher` decryption failed (wrong key, unsupported cipher parameters, or
91 /// a failed page authentication) via [`Database::open_encrypted`]. Carries
92 /// the underlying [`sqlcipher::DecryptError`] (show-the-unrecognized-value).
93 Decrypt(sqlcipher::DecryptError),
94}
95
96impl From<std::io::Error> for Error {
97 fn from(e: std::io::Error) -> Self {
98 Error::Io(e.kind())
99 }
100}
101
102impl From<sqlcipher::DecryptError> for Error {
103 fn from(e: sqlcipher::DecryptError) -> Self {
104 Error::Decrypt(e)
105 }
106}
107
108/// A freed overflow-page chain could not be followed to a complete, trustworthy
109/// payload (task #73): a chain page that is not a freelist leaf (live / trunk /
110/// unreachable), a cycle, a premature terminator with bytes still owed, an
111/// out-of-range page, or a declared payload exceeding the freelist's capacity.
112/// Carries no detail by design — any break is a uniform "this chain is not
113/// recoverable as a Tier-1 row", and the candidate degrades to a Tier-2 fragment.
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub struct ChainBreak;
116
117/// A single decoded column value from a table row. Mirrors `SQLite`'s storage
118/// classes.
119#[derive(Debug, Clone, PartialEq)]
120pub enum Value {
121 Null,
122 Integer(i64),
123 Real(f64),
124 Text(String),
125 Blob(Vec<u8>),
126}
127
128/// One table row: its rowid plus decoded column values, in column order.
129#[derive(Debug, Clone, PartialEq)]
130pub struct Row {
131 pub rowid: i64,
132 pub values: Vec<Value>,
133}
134
135/// A live user table dumped for export: its name, the column header to present,
136/// and every live row in rowid order. Produced by [`Database::live_table_rows`].
137///
138/// `column_names` are the table's **real** column names parsed from its
139/// `CREATE TABLE` when available, falling back to generic `c0..c{N-1}` (sized to
140/// the widest row) when the schema parse was low-confidence — so a header is
141/// always present and never a fabricated guess. `rows` preserves b-tree order,
142/// which for an integer-rowid table is ascending rowid order.
143#[derive(Debug, Clone, PartialEq)]
144pub struct LiveTableDump {
145 /// Table name from `sqlite_master.name`.
146 pub name: String,
147 /// Header column names: real names from the schema, or `c0..c{N-1}`.
148 pub column_names: Vec<String>,
149 /// Every live row (rowid + decoded values), in b-tree (rowid) order.
150 pub rows: Vec<Row>,
151}
152
153/// A `WITHOUT ROWID` user table's live rows, produced by
154/// [`Database::without_rowid_table_rows`]. Such a table's data lives entirely in
155/// an index b-tree (there is no rowid), so `rows` holds the decoded index records
156/// in the table's declared column order, in index (primary-key) order.
157#[derive(Debug, Clone, PartialEq)]
158pub struct WithoutRowidTable {
159 /// Table name from `sqlite_master.name`.
160 pub name: String,
161 /// Every live row's decoded column values, in the table's column order.
162 pub rows: Vec<Vec<Value>>,
163}
164
165/// A record-shaped cell recovered from unallocated / free space by
166/// [`Database::carve_cells`]. Carries the decoded row plus enough provenance for
167/// the analyzer to grade it as a "consistent with a deleted row" observation.
168#[derive(Debug, Clone, PartialEq)]
169pub struct CarvedCell {
170 /// Byte offset of the cell within the page slice that was scanned.
171 pub offset: usize,
172 /// Total bytes the candidate cell occupies (cell header + payload), so the
173 /// scanner can skip past a recovered record.
174 pub byte_len: usize,
175 /// Decoded rowid varint.
176 pub rowid: i64,
177 /// Decoded column values, in column order.
178 pub values: Vec<Value>,
179 /// Heuristic confidence in `(0.0, 1.0]` that these bytes are a real record
180 /// rather than a coincidental match.
181 pub confidence: f32,
182}
183
184/// A **partial** deleted record salvaged from a freed-cell reconstruction that
185/// failed full-row validation: the maximal decodable column prefix at a
186/// structural anchor [`Database::reconstruct_freeblock_records`] already trusts.
187///
188/// Deliberately NOT a [`CarvedCell`]: it has no rowid (clobbered) and an
189/// incomplete value set, so the type system keeps it out of the full-row output
190/// — a fragment can never be silently rendered as a recovered row. Emitted only
191/// at an anchor where full reconstruction failed but at least one *distinctive*
192/// cell (TEXT ≥ 4 bytes of valid UTF-8, or REAL) decoded cleanly, so a lone
193/// coincidental integer pattern never anchors a fragment. Graded
194/// `FRAGMENT_CONFIDENCE` — strictly below every full-row class.
195#[derive(Debug, Clone, PartialEq)]
196pub struct CellFragment {
197 /// Byte offset of the failed cell's anchor within the scanned page slice.
198 pub offset: usize,
199 /// Bytes covered by the decoded prefix (anchor to the last decoded body byte).
200 pub byte_len: usize,
201 /// `(column_index, value)` for each column that decoded cleanly, ascending by
202 /// index. Column indexes come from the page's schema template, so they are
203 /// meaningful against the table's column order.
204 pub surviving: Vec<(usize, Value)>,
205 /// Number of the template's columns that did NOT decode (`column_count` minus
206 /// the number of surviving columns).
207 pub missing: usize,
208 /// Always `FRAGMENT_CONFIDENCE` for now; the field is kept so future
209 /// per-fragment grading does not change the public type.
210 pub confidence: f32,
211}
212
213/// A freed table-leaf cell whose declared payload **spills onto an overflow-page
214/// chain** (task #73). Recognized by `try_carve_spilled_cell_at` from the
215/// cell's intact local prefix; the chain itself is resolved separately
216/// ([`Database::read_freed_overflow_chain`]) because that needs whole-database
217/// access. A `SpilledCell` is deliberately NOT a [`CarvedCell`]: until its chain
218/// is walked and validated it cannot masquerade as a recovered row (secure by
219/// design — the type system keeps an unresolved spill out of the full-row output).
220#[derive(Debug, Clone, PartialEq)]
221pub struct SpilledCell {
222 /// Byte offset of the cell within the scanned slice.
223 pub offset: usize,
224 /// On-page footprint of the cell prefix: `n1 + n2 + local_len + 4`.
225 pub byte_len: usize,
226 /// Declared total payload length `P` (header + full body).
227 pub payload_len: usize,
228 /// Decoded rowid varint (intact-prefix anchors); `0` when the prefix was
229 /// clobbered and the rowid is unrecoverable (template path).
230 pub rowid: i64,
231 /// Full serial-type array, decoded from the local record header.
232 pub serials: Vec<i64>,
233 /// Local payload bytes kept on the leaf page (`local_payload_len(P, usable)`).
234 pub local_len: usize,
235 /// Offset, within the scanned slice, at which the local payload begins.
236 pub local_payload_off: usize,
237 /// First overflow-page number (big-endian u32 at `local_payload_off + local_len`).
238 pub first_overflow: u32,
239}
240
241/// Database text encoding (file-format §1.3, header byte 56). Determines how
242/// `TEXT` column bytes are decoded; a fixed property set at database creation.
243#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
244pub enum TextEncoding {
245 /// `1` (and `0`, an unwritten database): UTF-8.
246 #[default]
247 Utf8,
248 /// `2`: UTF-16 little-endian.
249 Utf16Le,
250 /// `3`: UTF-16 big-endian.
251 Utf16Be,
252}
253
254impl TextEncoding {
255 /// Decode a `TEXT` value's raw bytes per this encoding. Lossy so a corrupt
256 /// byte sequence yields U+FFFD rather than a panic or an error.
257 fn decode(self, bytes: &[u8]) -> String {
258 match self {
259 Self::Utf8 => String::from_utf8_lossy(bytes).into_owned(),
260 Self::Utf16Le => Self::decode_utf16(bytes, u16::from_le_bytes),
261 Self::Utf16Be => Self::decode_utf16(bytes, u16::from_be_bytes),
262 }
263 }
264
265 fn decode_utf16(bytes: &[u8], conv: fn([u8; 2]) -> u16) -> String {
266 // The DB-encoding path keeps its lossy-by-default contract: it discards
267 // the flag, so a truncated or corrupt unit still yields U+FFFD as before.
268 // The pairing itself lives in `decode_utf16_units` (DRY — the Local
269 // Storage decode reuses it and keeps the flag).
270 decode_utf16_units(bytes, conv).0
271 }
272}
273
274/// Shared UTF-16 → `String` pairing: pairs 2-byte code units via `conv`, resolves
275/// surrogate pairs, and reports whether the decode was **lossy**. A trailing odd
276/// byte (half a code unit) or an unpaired surrogate emits U+FFFD and sets the
277/// flag; it never panics or errors. Endianness is the caller's via `conv`.
278fn decode_utf16_units(bytes: &[u8], conv: fn([u8; 2]) -> u16) -> (String, bool) {
279 // An odd trailing byte is half a code unit — real data was truncated. It is
280 // dropped by `chunks_exact`; the flag records that a byte was lost.
281 let mut lossy = bytes.len() % 2 != 0;
282 let units = bytes.chunks_exact(2).map(|c| conv([c[0], c[1]]));
283 let mut text = String::new();
284 for unit in char::decode_utf16(units) {
285 if let Ok(c) = unit {
286 text.push(c);
287 } else {
288 lossy = true;
289 text.push(char::REPLACEMENT_CHARACTER);
290 }
291 }
292 (text, lossy)
293}
294
295/// A WebKit/Chrome Local Storage `ItemTable.value` decoded to text, plus whether
296/// the decode was lossy. `lossy` is a struct field, not a side-channel warning,
297/// so a caller cannot render a lossy value as if it were faithfully recovered
298/// (secure by design).
299#[derive(Debug, Clone, PartialEq, Eq, Default)]
300pub struct LocalStorageValue {
301 /// The decoded string; any code unit that could not be decoded is a U+FFFD.
302 pub text: String,
303 /// `true` when at least one input byte/unit could not be decoded cleanly (an
304 /// odd-length BLOB or an unpaired surrogate).
305 pub lossy: bool,
306}
307
308/// Decode a WebKit/Chromium Local Storage `ItemTable.value` BLOB to a `String`.
309///
310/// A `.localstorage` file is a standard `SQLite` database this crate already
311/// reads; the one artifact-specific quirk is that the `value` column is a BLOB
312/// holding the string as raw **UTF-16 little-endian** code units — no BOM, no
313/// type-prefix byte — so a normal dump surfaces it as opaque hex. This turns
314/// such a BLOB back into readable text.
315///
316/// Panic-free and lossy-by-report: an odd-length BLOB (a trailing half code
317/// unit) or an unpaired surrogate yields U+FFFD and sets
318/// [`LocalStorageValue::lossy`] rather than erroring or panicking. An empty BLOB
319/// decodes to the empty string with `lossy == false`.
320#[must_use]
321pub fn decode_localstorage_value(blob: &[u8]) -> LocalStorageValue {
322 let (text, lossy) = decode_utf16_units(blob, u16::from_le_bytes);
323 LocalStorageValue { text, lossy }
324}
325
326/// Recognize the WebKit/Chromium Local Storage `ItemTable(key TEXT, value BLOB)`
327/// table, so a caller knows when [`decode_localstorage_value`] applies to a
328/// dumped table's `value` column.
329///
330/// Keyed on the distinctive table name `ItemTable` — the name WebKit/Chromium
331/// create for Local Storage. The column names are deliberately NOT part of the
332/// test: the real schema declares them with `ON CONFLICT` clauses
333/// (`key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL`)
334/// that a lightweight `CREATE TABLE` parse does not always split cleanly, so a
335/// name match is the robust signal. The row shape (a TEXT key, a BLOB value)
336/// still surfaces positionally in each [`Row`].
337#[must_use]
338pub fn is_local_storage_item_table(table_name: &str) -> bool {
339 table_name == "ItemTable"
340}
341
342/// Parsed 100-byte `SQLite` file header.
343#[derive(Debug, Clone, Copy, PartialEq, Eq)]
344pub struct Header {
345 /// Logical page size in bytes (512..=65536).
346 pub page_size: u32,
347 /// Reserved bytes at the end of each page (usually 0).
348 pub reserved: u8,
349 /// Text encoding for `TEXT` columns (header byte 56).
350 pub text_encoding: TextEncoding,
351}
352
353impl Header {
354 /// Usable bytes per page = `page_size` − reserved (file-format §1.3.4).
355 #[must_use]
356 pub fn usable_size(self) -> u32 {
357 self.page_size.saturating_sub(u32::from(self.reserved))
358 }
359}
360
361/// A read-only view over the raw bytes of a `SQLite` database file.
362///
363/// Holds the whole file in memory — adequate for the spike and for browser
364/// evidence DBs (tens of MB). A `Read + Seek` / mmap backend is a later
365/// refinement and does not change the parsing logic proven here.
366pub struct Database {
367 /// Page byte source: the whole file in memory ([`Database::open`]) or a
368 /// paged, LRU-cached file reader ([`Database::open_path`], roadmap §3.1).
369 source: ByteSource,
370 /// The 100-byte file header, kept resident so fixed-offset header-field reads
371 /// (page count, freelist count/trunk) never touch the byte source.
372 head: Box<[u8]>,
373 header: Header,
374 /// Read-only WAL overlay: newest committed page versions from a `-wal`
375 /// sidecar, applied without checkpointing (never mutates the main file).
376 /// `None` when opened without a WAL.
377 wal: Option<WalOverlay>,
378}
379
380/// A page image handed back by the byte source: a slice borrowed from an
381/// in-memory buffer, or a reference-counted page from the paged LRU cache.
382/// Derefs to `[u8]` so callers treat it as a page slice regardless of origin.
383///
384/// A page-*handle* rather than a `with_page(|bytes| …)` closure because the walk
385/// uses `&dyn PageSource` (a generic closure method would make that trait
386/// non-object-safe) and the recursive b-tree descent cannot hold a pinning
387/// closure across its own recursion. The `Shared` variant keeps a cached page
388/// alive while held, so LRU eviction can never dangle it.
389pub enum PageBytes<'a> {
390 /// Borrowed from an in-memory buffer (the `open` / WAL-overlay path).
391 Borrowed(&'a [u8]),
392 /// Shared out of the paged LRU cache (the `open_path` path).
393 Shared(std::rc::Rc<[u8]>),
394}
395
396impl std::ops::Deref for PageBytes<'_> {
397 type Target = [u8];
398 fn deref(&self) -> &[u8] {
399 match self {
400 PageBytes::Borrowed(s) => s,
401 PageBytes::Shared(r) => r,
402 }
403 }
404}
405
406/// Where a [`Database`]'s page bytes come from.
407enum ByteSource {
408 /// The whole file resident in memory.
409 Mem(Vec<u8>),
410 /// A file read page-by-page through a bounded LRU cache.
411 Paged(Paged),
412}
413
414impl ByteSource {
415 /// Total byte length of the underlying file.
416 fn len(&self) -> usize {
417 match self {
418 ByteSource::Mem(b) => b.len(),
419 ByteSource::Paged(p) => p.len,
420 }
421 }
422
423 /// The 1-based `page`'s bytes, or `None` for page 0 / out of range / an I/O
424 /// error. Bounded and panic-free.
425 fn page(&self, page: u32, page_size: usize) -> Option<PageBytes<'_>> {
426 let start = (page as usize).checked_sub(1)?.checked_mul(page_size)?;
427 let end = start.checked_add(page_size)?;
428 match self {
429 ByteSource::Mem(b) => b.get(start..end).map(PageBytes::Borrowed),
430 ByteSource::Paged(p) if end <= p.len => {
431 p.read_page(start, page_size).map(PageBytes::Shared)
432 }
433 ByteSource::Paged(_) => None,
434 }
435 }
436
437 /// The whole file as one slice when resident in memory; `None` for a paged
438 /// source (which never materializes the whole file). Used only on the
439 /// WAL-overlay path, which is in-memory by construction.
440 fn whole(&self) -> Option<&[u8]> {
441 match self {
442 ByteSource::Mem(b) => Some(b),
443 ByteSource::Paged(_) => None, // cov:unreachable: WAL overlay is in-memory only
444 }
445 }
446}
447
448/// A file read page-by-page through a small LRU cache, so resident memory stays
449/// bounded regardless of file size (roadmap §3.1).
450struct Paged {
451 file: std::cell::RefCell<std::fs::File>,
452 len: usize,
453 cache: std::cell::RefCell<PageCache>,
454}
455
456impl Paged {
457 /// Read `page_size` bytes at `start`, serving from and populating the LRU
458 /// cache. `None` on any I/O error (panic-free).
459 fn read_page(&self, start: usize, page_size: usize) -> Option<std::rc::Rc<[u8]>> {
460 use std::io::{Read, Seek, SeekFrom};
461 if let Some(hit) = self.cache.borrow_mut().get(start) {
462 return Some(hit);
463 }
464 let mut buf = vec![0u8; page_size];
465 {
466 let mut file = self.file.borrow_mut();
467 file.seek(SeekFrom::Start(start as u64)).ok()?;
468 file.read_exact(&mut buf).ok()?;
469 }
470 let rc: std::rc::Rc<[u8]> = std::rc::Rc::from(buf);
471 self.cache.borrow_mut().put(start, std::rc::Rc::clone(&rc));
472 Some(rc)
473 }
474}
475
476/// A tiny bounded LRU of page images keyed by file offset, capping resident
477/// memory to [`PageCache::CAP`] pages so a multi-GB database never loads whole.
478struct PageCache {
479 map: std::collections::HashMap<usize, std::rc::Rc<[u8]>>,
480 order: std::collections::VecDeque<usize>,
481}
482
483impl PageCache {
484 /// Maximum resident pages (`CAP` × `page_size` bytes; 256 pages is about one
485 /// megabyte at a 4-kilobyte page), so a multi-gigabyte database never loads whole.
486 const CAP: usize = 256;
487
488 fn new() -> Self {
489 Self {
490 map: std::collections::HashMap::new(),
491 order: std::collections::VecDeque::new(),
492 }
493 }
494
495 fn get(&mut self, key: usize) -> Option<std::rc::Rc<[u8]>> {
496 let hit = self.map.get(&key).map(std::rc::Rc::clone)?;
497 self.touch(key);
498 Some(hit)
499 }
500
501 fn put(&mut self, key: usize, value: std::rc::Rc<[u8]>) {
502 if self.map.insert(key, value).is_some() {
503 self.touch(key);
504 } else {
505 self.order.push_back(key);
506 if self.order.len() > Self::CAP {
507 if let Some(evicted) = self.order.pop_front() {
508 self.map.remove(&evicted);
509 }
510 }
511 }
512 }
513
514 fn touch(&mut self, key: usize) {
515 if let Some(pos) = self.order.iter().position(|&k| k == key) {
516 self.order.remove(pos);
517 self.order.push_back(key);
518 }
519 }
520}
521
522/// The newest committed version of each WAL page, materialized into owned bytes.
523///
524/// Built once at open; `page_slice` consults it before the main file so a table
525/// walk transparently sees the WAL-applied view. Read-only: building it copies
526/// frame data out of the `-wal` sidecar and never writes back to either file.
527struct WalOverlay {
528 /// page number (1-based) → that page's newest committed contents.
529 pages: std::collections::BTreeMap<u32, Vec<u8>>,
530 /// Every committed frame's page image, in file order, with provenance. Unlike
531 /// `pages` (newest version per page, the consistent view), this keeps EACH
532 /// committed frame so the carver can recover deleted residue that a later
533 /// frame for the same page superseded in `pages` but that still survives in an
534 /// earlier frame's slack — the genuinely-different records an on-disk-only
535 /// carve cannot see.
536 frames: Vec<WalFramePage>,
537 /// The original `-wal` sidecar bytes, retained so [`Database::wal_timeline`]
538 /// can re-parse them into the richer segmented temporal model without the
539 /// caller re-supplying the file. Held read-only; never mutated.
540 raw: Vec<u8>,
541}
542
543/// One committed WAL frame's full page image plus its provenance, exposed by
544/// [`Database::wal_frame_pages`] so the deleted-record carver can scan the
545/// uncheckpointed WAL frames the main file does not yet reflect.
546///
547/// The `(salt1, salt2, frame_index)` triple is the WAL log-sequence identity that
548/// task #55 will formalize: `salt1`/`salt2` pin the checkpoint generation and
549/// `frame_index` the position within it.
550#[derive(Debug, Clone, PartialEq, Eq)]
551pub struct WalFramePage {
552 /// 0-based position of this frame within the `-wal` file (its LSN ordinal).
553 pub frame_index: usize,
554 /// 1-based database page number this frame rewrites.
555 pub page_no: u32,
556 /// WAL header salt-1 (checkpoint generation), shared by every live frame.
557 pub salt1: u32,
558 /// WAL header salt-2 (checkpoint generation), shared by every live frame.
559 pub salt2: u32,
560 /// Whether this is a COMMIT frame (`db_size_after_commit != 0`).
561 pub is_commit: bool,
562 /// The frame's full page image (`page_size` bytes).
563 pub page: Vec<u8>,
564}
565
566/// Hard cap on b-tree pages visited in one table walk, to bound work on a
567/// crafted file with cyclic interior pointers.
568const MAX_PAGES_PER_WALK: usize = 1_000_000;
569
570/// Minimum column count accepted when **inferring** a record's width during
571/// dropped-table carving. A coincidental byte run can look like a self-consistent
572/// 1-column record far too easily; requiring at least two columns (the smallest a
573/// real rowid table with a non-rowid column has) suppresses that false-positive
574/// class without losing real records.
575const MIN_INFERRED_COLUMNS: usize = 2;
576
577/// Confidence multiplier applied to records carved from an allocated page's
578/// in-page free space. Such residue is more often partially overwritten (its
579/// freeblock may have been reused) than whole-page freelist recovery, so it is
580/// graded a notch lower even when it parses cleanly.
581const IN_PAGE_CONFIDENCE_FACTOR: f32 = 0.8;
582
583/// Confidence multiplier applied to a **chain-reassembled overflow** full row
584/// (task #73, [`Database::carve_overflow_records`]). Overflow Tier-1 is NOT part
585/// of the structural 0-false-positive guarantee (Codex ruling #1): a freelist
586/// *leaf* page can be stale — allocated, overwritten, freed, now a leaf holding
587/// unrelated bytes that happen to decode. The freelist-leaf requirement plus the
588/// strict-UTF-8 gate make a clean decode strong evidence, but one indirection
589/// weaker than a contiguous in-page span, so it is graded below the in-page
590/// full-row tier (0.9 × this factor). The residual stale-leaf risk is documented
591/// and the row remains a "consistent with a deleted row" observation, never a
592/// verdict.
593const OVERFLOW_CHAIN_CONFIDENCE_FACTOR: f32 = 0.75;
594
595/// Confidence assigned to a record rebuilt by **freeblock reconstruction**
596/// ([`Database::reconstruct_freeblock_records`]). The cell's first four bytes
597/// (payload-length + rowid varints, the record `header_len`, and the leading
598/// serial type) were destroyed by freeblock conversion, so the record is rebuilt
599/// from its surviving serial-type tail plus a schema-derived header template — a
600/// weaker reconstruction than an intact-header carve, hence graded LOW (a
601/// "consistent with a deleted row" lead the examiner weighs, never a certainty).
602const FREEBLOCK_RECONSTRUCT_CONFIDENCE: f32 = 0.4;
603
604/// Confidence assigned to a Tier-2 [`CellFragment`] — a partial recovery whose
605/// full row could not be reconstructed but at least one distinctive cell survived.
606/// Flat 0.2 = the `MinConfidence::Low` threshold, one notch below freeblock
607/// reconstruction's 0.4 (= Medium): a fragment is the weakest lead in the ladder,
608/// "consistent with a partial deleted row", never a recovered row.
609const FRAGMENT_CONFIDENCE: f32 = 0.2;
610
611/// Upper bound on the number of freeblocks walked on a single page, to cap work
612/// on a crafted file whose freeblock `next` pointers form a long or cyclic chain.
613/// Real pages hold at most a few hundred cells.
614const MAX_FREEBLOCKS_PER_PAGE: usize = 4096;
615
616/// WAL magic, big-endian variant (native byte order in the page checksums; the
617/// little-endian variant `0x377f_0683` differs only in checksum endianness,
618/// which the overlay does not verify). file-format §4.1.
619const WAL_MAGIC_BE: u32 = 0x377f_0682;
620/// WAL magic, little-endian-checksum variant.
621const WAL_MAGIC_LE: u32 = 0x377f_0683;
622
623/// Byte order in which the WAL checksum reads its 32-bit words (file-format
624/// §4.2). NOT the same as the constant names above: per the spec, magic
625/// `0x377f0683` selects **big-endian** words and `0x377f0682` **little-endian**
626/// words. (The legacy `WAL_MAGIC_*` constant names predate this checksum work
627/// and are used only as a "valid magic" set; this enum is the spec-faithful
628/// source of truth for checksum endianness.)
629#[derive(Debug, Clone, Copy, PartialEq, Eq)]
630enum WalChecksumEndian {
631 Big,
632 Little,
633}
634
635impl WalChecksumEndian {
636 /// The checksum word order selected by the WAL header magic (offset 0), or
637 /// `None` for a magic that is neither WAL variant (file-format §4.2).
638 fn from_magic(magic: u32) -> Option<Self> {
639 match magic {
640 0x377f_0683 => Some(Self::Big),
641 0x377f_0682 => Some(Self::Little),
642 _ => None,
643 }
644 }
645
646 /// Read one 32-bit word from `b` (exactly 4 bytes) in this endianness.
647 fn read_word(self, b: [u8; 4]) -> u32 {
648 match self {
649 Self::Big => u32::from_be_bytes(b),
650 Self::Little => u32::from_le_bytes(b),
651 }
652 }
653}
654
655/// Advance the cumulative WAL checksum `(s0, s1)` over `data` (file-format
656/// §4.2). `data` is interpreted as 32-bit words in the given endianness and
657/// consumed 8 bytes (two words) at a time via the Fibonacci-weighted recurrence
658/// `s0 += x[i] + s1; s1 += x[i+1] + s0;`
659/// using wrapping (u32) arithmetic. A trailing partial group (< 8 bytes) is
660/// ignored — the spec defines the checksum only over an even number of words,
661/// and every real WAL input (24-byte header prefix, 8-byte frame-header prefix,
662/// page data) is a multiple of 8 bytes.
663fn wal_checksum(endian: WalChecksumEndian, mut s0: u32, mut s1: u32, data: &[u8]) -> (u32, u32) {
664 let mut chunks = data.chunks_exact(8);
665 for c in &mut chunks {
666 let x0 = endian.read_word([c[0], c[1], c[2], c[3]]);
667 let x1 = endian.read_word([c[4], c[5], c[6], c[7]]);
668 s0 = s0.wrapping_add(x0).wrapping_add(s1);
669 s1 = s1.wrapping_add(x1).wrapping_add(s0);
670 }
671 (s0, s1)
672}
673
674impl Database {
675 /// Parse the file header and validate magic + page size. No WAL overlay.
676 pub fn open(bytes: Vec<u8>) -> Result<Self, Error> {
677 let header = parse_header(&bytes)?;
678 let head = header_prefix(&bytes);
679 Ok(Self {
680 source: ByteSource::Mem(bytes),
681 head,
682 header,
683 wal: None,
684 })
685 }
686
687 /// Decrypt a **`SQLCipher`** database with `key` and open the resulting
688 /// plaintext, detecting the cipher version automatically (see
689 /// [`sqlcipher::decrypt`]). The reader then consumes the decrypted byte
690 /// stream exactly as for a plaintext file — the encryption is transparent
691 /// past this call.
692 ///
693 /// Secure-by-default and read-only: a wrong key or unsupported cipher
694 /// parameters is a loud [`Error::Decrypt`], never a silently-misread
695 /// database; nothing is written back to the evidence file.
696 pub fn open_encrypted(bytes: &[u8], key: &sqlcipher::SqlCipherKey) -> Result<Self, Error> {
697 let decrypted = sqlcipher::decrypt(bytes, key)?;
698 Self::open(decrypted.plaintext)
699 }
700
701 /// Open a database from a filesystem path with a **bounded-memory paged
702 /// read** (roadmap §3.1): pages are streamed on demand through a small LRU
703 /// cache instead of loading the whole file into a `Vec<u8>`, so a multi-GB
704 /// database opens without proportional RAM. Main file only — for the
705 /// WAL-applied view use [`Database::open_with_wal`] (WAL sidecars are small
706 /// and stay in memory).
707 ///
708 /// Read-only and panic-free: an unreadable file or a malformed header is a
709 /// typed [`Error`] ([`Error::Io`] carries the [`std::io::ErrorKind`]); nothing
710 /// is written back.
711 pub fn open_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Error> {
712 use std::io::{Read, Seek, SeekFrom};
713 let mut file = std::fs::File::open(path)?;
714 let len = file.metadata()?.len();
715 // Read just the header prefix to parse page size / encoding; the rest of
716 // the file is read page-by-page on demand.
717 let prefix_len = usize::try_from(len)
718 .unwrap_or(usize::MAX)
719 .min(SQLITE_HEADER_SIZE);
720 let mut head = vec![0u8; prefix_len];
721 file.seek(SeekFrom::Start(0))?;
722 file.read_exact(&mut head)?;
723 let header = parse_header(&head)?;
724 let source = ByteSource::Paged(Paged {
725 file: std::cell::RefCell::new(file),
726 len: usize::try_from(len).unwrap_or(usize::MAX),
727 cache: std::cell::RefCell::new(PageCache::new()),
728 });
729 Ok(Self {
730 source,
731 head: head.into(),
732 header,
733 wal: None,
734 })
735 }
736
737 /// Parse the main database plus a `-wal` sidecar, overlaying the newest
738 /// **committed** page versions from the WAL on top of the main file.
739 ///
740 /// This is the forensic-safe alternative to libsqlite checkpointing: neither
741 /// file is mutated. The resulting [`Database`] answers `read_table` with the
742 /// WAL-applied view (use [`Database::open`] for the main-only view). Frames
743 /// past the last commit frame, or whose salt does not match the WAL header,
744 /// are ignored — they are uncommitted / superseded and not part of the
745 /// consistent snapshot.
746 pub fn open_with_wal(bytes: Vec<u8>, wal: &[u8]) -> Result<Self, Error> {
747 let header = parse_header(&bytes)?;
748 let overlay = WalOverlay::parse(wal, header.page_size)?;
749 let head = header_prefix(&bytes);
750 Ok(Self {
751 source: ByteSource::Mem(bytes),
752 head,
753 header,
754 wal: overlay,
755 })
756 }
757
758 /// Materialize the single pre-transaction state from a rollback `-journal`,
759 /// binding it to THIS database (design §5). The journal's page images (the
760 /// bytes BEFORE the last transaction) are overlaid on the live pages, yielding
761 /// a [`PriorSnapshot`] — a DISTINCT read-only view, never a [`Database`], so a
762 /// prior/deleted row can never be read as "live" (secure-by-design).
763 ///
764 /// The main db's page size is authoritative (a PERSIST journal has a zeroed
765 /// header). **Errors with [`Error::JournalModeConflict`]** when `self` was
766 /// opened WAL-applied ([`Database::open_with_wal`]): WAL and rollback-journal
767 /// modes are mutually exclusive timelines and must not be overlaid.
768 ///
769 /// Robust and panic-free: a malformed/truncated journal yields a prior
770 /// snapshot with fewer overlaid pages (degrading toward the live image), never
771 /// a panic; a non-power-of-two page size is a typed
772 /// [`Error::BadJournalPageSize`].
773 pub fn rollback_prior(&self, journal: &[u8]) -> Result<PriorSnapshot, Error> {
774 if self.wal_applied() {
775 return Err(Error::JournalModeConflict);
776 }
777 let page_size = self.header.page_size;
778 let parsed = RollbackJournal::parse(journal, page_size)?;
779
780 // Start from the live main pages, then overlay the journal's prior images.
781 let main_pages = self.file_page_count();
782 let mut overlaid: std::collections::BTreeMap<u32, Vec<u8>> =
783 std::collections::BTreeMap::new();
784 for pgno in 1..=main_pages {
785 if let Some(slice) = self.raw_page(pgno) {
786 overlaid.insert(pgno, slice.to_vec());
787 }
788 }
789 let mut grew_db = false;
790 for img in parsed.page_images() {
791 if img.pgno > main_pages {
792 grew_db = true;
793 }
794 overlaid.insert(img.pgno, img.bytes.clone());
795 }
796
797 // Usable bytes per page from the PRIOR page-1 header (reserved byte @ 20),
798 // so a reserved-space change in the last txn is honored. Fall back to the
799 // live header when page 1 is not in the snapshot.
800 let reserved = overlaid
801 .get(&1)
802 .and_then(|p| p.get(RESERVED_SPACE_OFFSET).copied())
803 .unwrap_or(self.header.reserved);
804 let usable = page_size.saturating_sub(u32::from(reserved));
805 let page_bound = overlaid.keys().copied().next_back().unwrap_or(main_pages);
806
807 Ok(PriorSnapshot {
808 overlaid,
809 usable,
810 page_bound,
811 grew_db,
812 })
813 }
814
815 /// Whether a non-empty WAL overlay is in effect (at least one committed
816 /// frame was applied on top of the main file).
817 #[must_use]
818 pub fn wal_applied(&self) -> bool {
819 self.wal.as_ref().is_some_and(|w| !w.pages.is_empty())
820 }
821
822 /// Every committed `-wal` frame's page image, in file order, with provenance.
823 ///
824 /// Empty when the database was opened without a WAL (or the WAL held no
825 /// committed frames). The carver scans these page images for deleted-cell
826 /// residue that lives ONLY in the uncheckpointed WAL — the genuinely-different
827 /// records the on-disk pages do not hold — tagging each with the
828 /// `(salt1, salt2, frame_index)` log-sequence identity.
829 #[must_use]
830 pub fn wal_frame_pages(&self) -> &[WalFramePage] {
831 self.wal.as_ref().map_or(&[], |w| w.frames.as_slice())
832 }
833
834 /// Build the bespoke, format-exact [`WalTimeline`] for this database's `-wal`
835 /// sidecar, if one was supplied to [`Database::open_with_wal`].
836 ///
837 /// Returns `None` when the database was opened without a WAL, or the WAL held
838 /// no committed frame (no materializable state). The timeline enumerates the
839 /// segment's [`CommitSnapshot`]s — the only materializable database states —
840 /// each addressable by [`CommitId`]; see [`WalTimeline`].
841 ///
842 /// This consults the original `-wal` bytes retained at open time, re-parsing
843 /// them into the richer temporal model (the on-open `WalOverlay` keeps only
844 /// the consistent-view pages; the timeline keeps every segment, snapshot, and
845 /// residue tail). A page-size mismatch or malformed header surfaces as `None`
846 /// here — use [`Database::wal_timeline_from`] when you need the typed
847 /// [`WalValidationError`].
848 #[must_use]
849 pub fn wal_timeline(&self) -> Option<WalTimeline> {
850 let raw = self.wal.as_ref()?.raw.as_slice();
851 WalTimeline::parse(self.source.whole()?, raw, self.header.page_size).ok()
852 }
853
854 /// Parse a main database + `-wal` sidecar directly into a [`WalTimeline`],
855 /// surfacing the typed [`WalValidationError`] when the WAL is malformed.
856 ///
857 /// This is the validation-tier entry point: a page-size mismatch between the DB
858 /// header and the WAL header is a HARD STOP ([`WalValidationError::PageSizeMismatch`]),
859 /// not a silently mis-sliced overlay; a bad magic / unparsable header is
860 /// [`WalValidationError::BadMagic`]. Both are caught at the physical-validation
861 /// tier before any replay.
862 pub fn wal_timeline_from(bytes: &[u8], wal: &[u8]) -> Result<WalTimeline, WalValidationError> {
863 let header = parse_header(bytes).map_err(WalValidationError::Header)?;
864 WalTimeline::parse(bytes, wal, header.page_size)
865 }
866
867 #[must_use]
868 pub fn header(&self) -> Header {
869 self.header
870 }
871
872 /// Number of pages in the database file.
873 ///
874 /// Prefers the in-header DB size (offset 28) when it is a valid, non-zero
875 /// value that is consistent with the file length; otherwise falls back to
876 /// `file_len / page_size`. A mismatch between the two is itself a forensic
877 /// signal (see [`Database::header_page_count`] / [`Database::file_page_count`]).
878 #[must_use]
879 pub fn page_count(&self) -> u32 {
880 let header = self.header_page_count();
881 let file = self.file_page_count();
882 if header != 0 && header == file {
883 header
884 } else {
885 file
886 }
887 }
888
889 /// The page count recorded in the file header (offset 28). May be 0 (legacy
890 /// "size not valid" sentinel) or disagree with the file length after an
891 /// out-of-band truncation/extension.
892 #[must_use]
893 pub fn header_page_count(&self) -> u32 {
894 be_u32(&self.head, DB_SIZE_IN_PAGES_OFFSET)
895 }
896
897 /// The page count implied by the raw file length (`file_len / page_size`).
898 #[must_use]
899 pub fn file_page_count(&self) -> u32 {
900 let ps = self.header.page_size as usize;
901 u32::try_from(self.source.len() / ps).unwrap_or(u32::MAX)
902 }
903
904 /// The freelist page **count** recorded in the file header (offset 36).
905 #[must_use]
906 pub fn freelist_count(&self) -> u32 {
907 be_u32(&self.head, FREELIST_COUNT_OFFSET)
908 }
909
910 /// Walk the freelist trunk/leaf chain and return every free (unallocated)
911 /// page number, in trunk order. Free pages retain the bytes of whatever they
912 /// last held — on a `secure_delete=OFF` database that includes deleted
913 /// records, which the analyzer can carve.
914 ///
915 /// Bounded against crafted cyclic trunk chains: a page already visited, an
916 /// out-of-range page, or a leaf-pointer count larger than a trunk page can
917 /// hold aborts with [`Error::MalformedFreelist`] rather than looping.
918 pub fn freelist_pages(&self) -> Result<Vec<u32>, Error> {
919 let (leaves, trunks) = self.freelist_pages_split()?;
920 // Preserve the historical order: each trunk's leaves, then the trunk.
921 // The split sets are ordered, which is sufficient for every caller (they
922 // treat the result as a set), and keeps a single source of truth.
923 let mut free: Vec<u32> = leaves.into_iter().collect();
924 free.extend(trunks);
925 Ok(free)
926 }
927
928 /// Walk the freelist and return its **leaf** and **trunk** page numbers
929 /// separately (task #73). The distinction is load-bearing for chain-aware
930 /// overflow recovery: a freed page that became a freelist *leaf* keeps its
931 /// former content byte-for-byte, while a *trunk* page has its head
932 /// (next-trunk pointer + leaf count + leaf-number array) written over the
933 /// former content (file-format §"The Freelist"). Only leaves are
934 /// content-preserving, so [`Database::read_freed_overflow_chain`] accepts a
935 /// chain page only when it is a leaf.
936 ///
937 /// Bounded identically to [`Database::freelist_pages`]: a cyclic trunk chain,
938 /// an out-of-range page, or an over-large leaf count aborts with
939 /// [`Error::MalformedFreelist`] rather than looping.
940 pub fn freelist_pages_split(
941 &self,
942 ) -> Result<
943 (
944 std::collections::BTreeSet<u32>,
945 std::collections::BTreeSet<u32>,
946 ),
947 Error,
948 > {
949 let mut leaves = std::collections::BTreeSet::new();
950 let mut trunks = std::collections::BTreeSet::new();
951 let mut trunk = be_u32(&self.head, SQLITE_FREELIST_TRUNK_OFFSET);
952 let total_pages = self.file_page_count();
953 // Each trunk page holds at most (page_size/4 - 2) leaf pointers.
954 let max_leaves = (self.header.page_size as usize / 4).saturating_sub(2);
955 let mut visited = 0usize;
956 let cap = total_pages as usize + 1;
957
958 while trunk != 0 {
959 visited += 1;
960 if visited > cap {
961 return Err(Error::MalformedFreelist);
962 }
963 if trunk > total_pages {
964 return Err(Error::MalformedFreelist);
965 }
966 let slice = self.page_slice(trunk)?;
967 let slice = &*slice;
968 let next = be_u32(slice, 0);
969 let leaf_count = be_u32(slice, 4) as usize;
970 if leaf_count > max_leaves {
971 return Err(Error::MalformedFreelist);
972 }
973 for i in 0..leaf_count {
974 let leaf = be_u32(slice, 8 + i * 4);
975 if leaf == 0 || leaf > total_pages {
976 return Err(Error::MalformedFreelist);
977 }
978 leaves.insert(leaf);
979 }
980 trunks.insert(trunk);
981 trunk = next;
982 }
983 Ok((leaves, trunks))
984 }
985
986 /// Follow a **freed** overflow-page chain starting at `first`, reading raw
987 /// main-file pages only (carving wants on-disk residue, not the WAL view),
988 /// and assemble up to `remaining` content bytes (task #73). The carve-side
989 /// dual of `Database::read_overflow_chain`, with one extra discipline that
990 /// makes it the 0-FP-relevant guard: **every chain page must be a freelist
991 /// leaf** (`freed_leaves`). A page that is not a leaf is live, a trunk, or
992 /// unreachable — following its pointer would risk reading reused or clobbered
993 /// content, so it is a [`ChainBreak`] (Codex ruling #2: the leaf requirement,
994 /// not the UTF-8 gate, is what rejects a destroyed chain).
995 ///
996 /// Returns the assembled content and the ordered list of chain pages on
997 /// success. Robustness (Paranoid Gatekeeper, design §4.2): the anti-bomb cap
998 /// rejects upfront any `remaining` above what the freelist leaves can deliver
999 /// (`(usable - 4) × freed_leaves.len()`), so an attacker-declared huge
1000 /// payload dies before any allocation; cycles are caught by a visited set;
1001 /// a premature `next == 0` with bytes still wanted, an out-of-range page, or
1002 /// page 0 mid-chain all break. Never panics — every read is bounds-checked.
1003 pub fn read_freed_overflow_chain(
1004 &self,
1005 first: u32,
1006 remaining: usize,
1007 usable: usize,
1008 freed_leaves: &std::collections::BTreeSet<u32>,
1009 ) -> Result<(Vec<u8>, Vec<u32>), ChainBreak> {
1010 let per_page = usable.checked_sub(4).filter(|&p| p > 0).ok_or(ChainBreak)?;
1011 // Anti-bomb cap: the chain can deliver at most this many bytes. Reject an
1012 // absurd declared payload before allocating (design §4.2).
1013 let max_deliverable = per_page.checked_mul(freed_leaves.len()).ok_or(ChainBreak)?;
1014 if remaining > max_deliverable {
1015 return Err(ChainBreak);
1016 }
1017 let total_pages = self.file_page_count();
1018 let mut content = Vec::with_capacity(remaining);
1019 let mut chain = Vec::new();
1020 let mut visited = std::collections::BTreeSet::new();
1021 let mut page = first;
1022 let mut left = remaining;
1023 while left > 0 {
1024 if page == 0 || page > total_pages {
1025 return Err(ChainBreak);
1026 }
1027 // The load-bearing guard: a chain page must be a freelist LEAF.
1028 if !freed_leaves.contains(&page) {
1029 return Err(ChainBreak);
1030 }
1031 if !visited.insert(page) {
1032 return Err(ChainBreak); // cycle
1033 }
1034 let slice = self.raw_page(page).ok_or(ChainBreak)?;
1035 let slice = &*slice;
1036 let next = be_u32(slice, 0);
1037 let take = left.min(per_page);
1038 let chunk = slice.get(4..4 + take).ok_or(ChainBreak)?;
1039 content.extend_from_slice(chunk);
1040 chain.push(page);
1041 left -= take;
1042 page = next;
1043 }
1044 Ok((content, chain))
1045 }
1046
1047 /// Raw bytes of the 1-based `page` from the **main file only**, ignoring any
1048 /// WAL overlay. Carving wants the on-disk page (where deleted residue lives),
1049 /// not the WAL-applied view. Returns `None` for page 0 or out-of-range pages.
1050 #[must_use]
1051 pub fn raw_page(&self, page: u32) -> Option<PageBytes<'_>> {
1052 if page == 0 {
1053 return None;
1054 }
1055 self.source.page(page, self.header.page_size as usize)
1056 }
1057
1058 /// Scan a slice of page bytes for record-shaped table-leaf cells of exactly
1059 /// `column_count` columns, recovering each as a [`CarvedCell`].
1060 ///
1061 /// This is the carving primitive the forensic analyzer drives over free /
1062 /// unallocated regions: at every byte offset it speculatively parses a
1063 /// `payload_len` varint, a `rowid` varint, and a record header, accepting the
1064 /// candidate only when the serial-type count matches `column_count`, the
1065 /// declared lengths stay within the slice, and every value decodes. Strict
1066 /// validation keeps the false-positive rate low; `confidence` reflects how
1067 /// strongly the bytes are record-shaped. Bounded: each offset does O(record)
1068 /// work and the scan is linear in the slice length.
1069 #[must_use]
1070 pub fn carve_cells(&self, page_bytes: &[u8], column_count: usize) -> Vec<CarvedCell> {
1071 let mut out = Vec::new();
1072 if column_count == 0 {
1073 return out;
1074 }
1075 let mut off = 0usize;
1076 while off < page_bytes.len() {
1077 if let Some(cell) = try_carve_cell_at(
1078 page_bytes,
1079 off,
1080 Some(column_count),
1081 self.header.text_encoding,
1082 ) {
1083 // Skip past this record to avoid re-reporting sub-slices of it.
1084 off += cell.byte_len.max(1);
1085 out.push(cell);
1086 } else {
1087 off += 1;
1088 }
1089 }
1090 out
1091 }
1092
1093 /// Carve record-shaped cells from a page slice **inferring** each record's
1094 /// column count from its own serial-type array, instead of requiring a fixed
1095 /// count. This is what makes **dropped-table / schema-gone** recovery
1096 /// possible: the page's table was `DROP`ped, so `sqlite_master` no longer
1097 /// records a column count, but each record still self-describes its columns.
1098 ///
1099 /// Inferring the count removes one validity check, so the remaining
1100 /// self-consistency checks are kept strict to hold the false-positive rate
1101 /// down: `header_len + body_len == payload_len`, every serial type legal,
1102 /// `rowid > 0`, the payload fully in-bounds, and at least
1103 /// `MIN_INFERRED_COLUMNS` columns. Records carved this way are graded a
1104 /// notch lower in confidence than fixed-count carving.
1105 #[must_use]
1106 pub fn carve_cells_inferred(&self, page_bytes: &[u8]) -> Vec<CarvedCell> {
1107 let mut out = Vec::new();
1108 let mut off = 0usize;
1109 while off < page_bytes.len() {
1110 if let Some(cell) = try_carve_cell_at(page_bytes, off, None, self.header.text_encoding)
1111 {
1112 off += cell.byte_len.max(1);
1113 out.push(cell);
1114 } else {
1115 off += 1;
1116 }
1117 }
1118 out
1119 }
1120
1121 /// Decode **every cell present in a table-leaf page image** (type `0x0D`) by
1122 /// walking its cell-pointer array, inferring each record's column count from
1123 /// its own serial-type array. Unlike [`Database::carve_free_regions`] (which
1124 /// scans only free space and excludes live cells), this returns the cells the
1125 /// page itself records as allocated.
1126 ///
1127 /// This is the primitive WAL-frame recovery needs: a `-wal` frame is a full
1128 /// page snapshot at one point in time, so a cell that is allocated in an
1129 /// EARLIER frame's image but absent from the final WAL-applied view is a row
1130 /// that was deleted later and survives ONLY in that superseded frame. The
1131 /// caller filters the returned cells against the final live view to isolate
1132 /// exactly those genuinely-deleted rows (so a still-live row is never
1133 /// re-surfaced — the filter is the caller's responsibility, mirroring the
1134 /// freeblock-reconstruction discipline).
1135 ///
1136 /// Bounded and panic-free: a malformed cell pointer or record simply yields
1137 /// fewer cells. Non-leaf pages yield nothing.
1138 #[must_use]
1139 pub fn carve_leaf_cells(&self, page_bytes: &[u8]) -> Vec<CarvedCell> {
1140 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1141 SQLITE_HEADER_SIZE
1142 } else {
1143 0
1144 };
1145 let Some(&page_type) = page_bytes.get(hdr_off) else {
1146 return Vec::new();
1147 };
1148 if page_type != 0x0d {
1149 return Vec::new(); // only table-leaf pages hold decodable cells here
1150 }
1151 let cell_count = be_u16(page_bytes, hdr_off + 3) as usize;
1152 let cell_ptr_array = hdr_off + 8; // leaf b-tree header is 8 bytes
1153 let mut out = Vec::new();
1154 for i in 0..cell_count {
1155 let cell_off = be_u16(page_bytes, cell_ptr_array + i * 2) as usize;
1156 if cell_off == 0 || cell_off >= page_bytes.len() {
1157 continue; // cov:unreachable: a valid leaf points cells within page
1158 }
1159 if let Some(cell) =
1160 try_carve_cell_at(page_bytes, cell_off, None, self.header.text_encoding)
1161 {
1162 out.push(cell);
1163 }
1164 }
1165 out
1166 }
1167
1168 /// Carve deleted records from the **free (unallocated) regions** of an
1169 /// allocated table-leaf page (type `0x0D`), never re-surfacing a live cell.
1170 ///
1171 /// On an allocated leaf, deleted-cell residue survives in two places: the
1172 /// unallocated gap between the cell-pointer array and the cell-content area,
1173 /// and the slack between/after live cells (a former freeblock whose chain
1174 /// pointer may already be gone). This method computes the exact byte ranges
1175 /// occupied by **live** cells and carves only the complement — so a live
1176 /// (allocated) cell can never be returned as a deleted record. That is the
1177 /// 0-false-positive guarantee, enforced structurally rather than by a filter.
1178 ///
1179 /// `page_bytes` is one whole page. `column_count_hint`, when non-zero, is the
1180 /// table's known column count (matched exactly); pass 0 to infer the count
1181 /// per record (for a page whose schema is gone). Non-leaf pages yield nothing.
1182 #[must_use]
1183 pub fn carve_free_regions(
1184 &self,
1185 page_bytes: &[u8],
1186 column_count_hint: usize,
1187 ) -> Vec<CarvedCell> {
1188 // Page 1 carries the 100-byte file header before its b-tree header; for a
1189 // standalone page slice we assume hdr_off 0 unless it starts with the
1190 // file magic (page 1 passed whole).
1191 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1192 SQLITE_HEADER_SIZE
1193 } else {
1194 0
1195 };
1196 let Some(&page_type) = page_bytes.get(hdr_off) else {
1197 return Vec::new();
1198 };
1199 if page_type != 0x0d {
1200 return Vec::new(); // only table-leaf pages have carvable cell residue
1201 }
1202 // Carve each maximal free region (complement of the live cell extents),
1203 // within the cell-content area only — so no allocated cell is ever
1204 // re-surfaced (the 0-false-positive guarantee, enforced structurally).
1205 let mut out = Vec::new();
1206 let regions = self.free_regions_of_leaf(page_bytes, hdr_off);
1207 for (lo, hi) in regions {
1208 let Some(region) = page_bytes.get(lo..hi) else {
1209 continue; // cov:unreachable: free_regions yields in-bounds spans
1210 };
1211 let cells = if column_count_hint == 0 {
1212 self.carve_cells_inferred(region)
1213 } else {
1214 self.carve_cells(region, column_count_hint)
1215 };
1216 for mut cell in cells {
1217 // Translate the offset from region-local to page-local, and grade
1218 // in-page recovery a notch lower (residue here is more often
1219 // partially overwritten than freed-page recovery).
1220 cell.offset += lo;
1221 cell.confidence *= IN_PAGE_CONFIDENCE_FACTOR;
1222 out.push(cell);
1223 }
1224 }
1225 out
1226 }
1227
1228 /// Recover **spilled** deleted records on a table-leaf page whose payload
1229 /// continued onto a freed overflow-page chain (task #73). Scans the page's
1230 /// free regions (the complement of the live cells — same discipline as
1231 /// [`Database::carve_free_regions`], so a live cell is never re-surfaced) for
1232 /// a [`SpilledCell`], then resolves each chain through freelist **leaf** pages
1233 /// only and assembles the full payload.
1234 ///
1235 /// A resolved record is returned only when ALL hold (design §5):
1236 /// 1. the chain is intact through freelist leaves (Codex ruling #2: the leaf
1237 /// requirement is the load-bearing 0-FP guard — a trunk/live/off-freelist
1238 /// chain page is rejected);
1239 /// 2. the assembled bytes total exactly the declared `P` and decode cleanly;
1240 /// 3. **strict UTF-8 on chain-resident TEXT** — an EXTRA reject signal, not a
1241 /// correctness proof (Codex ruling #2: a clobbered chain can still be valid
1242 /// UTF-8, so this cannot prove integrity; it only catches the cases where
1243 /// the lossy decoder would otherwise mask an overwrite as `U+FFFD`).
1244 ///
1245 /// Each returned tuple is `(cell, chain)` where `chain` is the ordered list of
1246 /// overflow pages the bytes came from (for provenance). Confidence is graded
1247 /// BELOW the in-page full-row tier (Codex ruling #1: overflow Tier-1 is a
1248 /// graded recovery, NOT part of the structural 0-FP guarantee — a freelist
1249 /// leaf can be stale, holding unrelated bytes that happen to decode). Bounded
1250 /// and panic-free; a malformed page or chain simply yields fewer records.
1251 #[must_use]
1252 pub fn carve_overflow_records(&self, page_bytes: &[u8]) -> Vec<(CarvedCell, Vec<u32>)> {
1253 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1254 SQLITE_HEADER_SIZE
1255 } else {
1256 0
1257 };
1258 let Some(&page_type) = page_bytes.get(hdr_off) else {
1259 return Vec::new();
1260 };
1261 if page_type != 0x0d {
1262 return Vec::new(); // only table-leaf pages carry spilled-cell residue
1263 }
1264 let Ok((freed_leaves, _trunks)) = self.freelist_pages_split() else {
1265 return Vec::new();
1266 };
1267 let usable = self.header.usable_size() as usize;
1268
1269 let mut out = Vec::new();
1270 let regions = self.free_regions_of_leaf(page_bytes, hdr_off);
1271 for (lo, hi) in regions {
1272 let Some(region) = page_bytes.get(lo..hi) else {
1273 continue; // cov:unreachable: free_regions yields in-bounds spans
1274 };
1275 // Scan every offset for a spilled cell (recognizer abstains on in-page
1276 // payloads, so the two carve classes never overlap).
1277 let mut off = 0usize;
1278 while off < region.len() {
1279 let Some(sc) = try_carve_spilled_cell_at(region, off, usable, None) else {
1280 off += 1;
1281 continue;
1282 };
1283 if let Some((mut cell, chain)) =
1284 self.resolve_spilled(region, &sc, usable, &freed_leaves)
1285 {
1286 // Translate the region-local offset to page-local.
1287 cell.offset = lo + sc.offset;
1288 out.push((cell, chain));
1289 off += sc.byte_len.max(1);
1290 } else {
1291 off += 1;
1292 }
1293 }
1294 }
1295 out
1296 }
1297
1298 /// Resolve a recognized [`SpilledCell`] to a full [`CarvedCell`] by walking
1299 /// its freed overflow chain and decoding the assembled payload, applying the
1300 /// strict-UTF-8 chain gate. Returns `Some((cell, chain))` on a fully-validated
1301 /// recovery, `None` on any chain break or gate failure (the candidate then
1302 /// degrades to a Tier-2 fragment elsewhere).
1303 fn resolve_spilled(
1304 &self,
1305 region: &[u8],
1306 sc: &SpilledCell,
1307 usable: usize,
1308 freed_leaves: &std::collections::BTreeSet<u32>,
1309 ) -> Option<(CarvedCell, Vec<u32>)> {
1310 let remaining = sc.payload_len.checked_sub(sc.local_len)?;
1311 let local_payload =
1312 region.get(sc.local_payload_off..sc.local_payload_off + sc.local_len)?;
1313 let (chain_content, chain) = self
1314 .read_freed_overflow_chain(sc.first_overflow, remaining, usable, freed_leaves)
1315 .ok()?;
1316 let mut payload = Vec::with_capacity(sc.payload_len);
1317 payload.extend_from_slice(local_payload);
1318 payload.extend_from_slice(&chain_content);
1319 if payload.len() != sc.payload_len {
1320 return None; // cov:unreachable: chain delivers exactly `remaining` bytes
1321 }
1322
1323 let values = decode_record(
1324 &payload,
1325 sc.serials.len(),
1326 sc.rowid,
1327 self.header.text_encoding,
1328 )
1329 .ok()?;
1330 if values.len() != sc.serials.len() {
1331 return None; // cov:unreachable: decode_record yields one value per serial
1332 }
1333 // Strict-UTF-8 gate on chain-resident TEXT (extra reject signal): the
1334 // lossy decoder turns a clobbered byte into U+FFFD, so any replacement
1335 // char in a decoded TEXT value means the chain-supplied bytes did not
1336 // decode cleanly — reject. NOT a proof of integrity (a stale leaf can hold
1337 // valid UTF-8); the freelist-leaf requirement is the load-bearing guard.
1338 let any_replacement = values.iter().any(|v| match v {
1339 Value::Text(t) => t.contains('\u{FFFD}'),
1340 _ => false,
1341 });
1342 if any_replacement {
1343 return None;
1344 }
1345 // Require at least one distinctive column so a coincidental decode of stale
1346 // bytes does not anchor a full row (the same identity bar as fragments).
1347 if !values.iter().any(is_distinctive) {
1348 return None; // cov:unreachable: the spilled corpus rows carry distinctive TEXT
1349 }
1350
1351 let cell = CarvedCell {
1352 offset: sc.offset,
1353 byte_len: sc.byte_len,
1354 rowid: sc.rowid,
1355 values,
1356 // Graded below the in-page full-row tier (0.9): an overflow chain adds
1357 // one indirection of stale-leaf exposure (Codex ruling #1).
1358 confidence: 0.9 * OVERFLOW_CHAIN_CONFIDENCE_FACTOR,
1359 };
1360 Some((cell, chain))
1361 }
1362
1363 /// Reconstruct **freeblock-clobbered spilled** cells (task #73, design §2.2 /
1364 /// Codex ruling #5). When a freed cell whose payload spilled is also
1365 /// freeblock-clobbered, its declared `P` is destroyed but **re-derivable** from
1366 /// the surviving structure: `P = header_len + Σ serial_body_len` over the full
1367 /// (template + surviving) serial array. When that `P` exceeds `usable - 35` the
1368 /// record is spilled by construction, so we read the 4-byte first-overflow
1369 /// pointer that follows the local payload and resolve the chain through
1370 /// freelist leaves, exactly as the intact-prefix path does — but with
1371 /// `rowid = 0` (the prefix's rowid varint was clobbered, never invented).
1372 ///
1373 /// UNPROVEN-BY-CORPUS (Codex ruling #5): no real Nemetz `0E` cell is *both*
1374 /// freeblock-clobbered *and* spilled — every measured spilled cell kept an
1375 /// intact prefix in the unallocated gap. This path is therefore validated
1376 /// against a **synthetic** fixture only; it is the general solution the
1377 /// no-special-case rule requires (it applies the same spill formula to the
1378 /// clobbered class), but its real-data behavior is not yet observed.
1379 ///
1380 /// Returns `(cell, chain)` per fully-resolved record. Bounded and panic-free.
1381 #[must_use]
1382 pub fn carve_overflow_template_records(
1383 &self,
1384 page_bytes: &[u8],
1385 ) -> Vec<(CarvedCell, Vec<u32>)> {
1386 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1387 SQLITE_HEADER_SIZE
1388 } else {
1389 0
1390 };
1391 if page_bytes.get(hdr_off) != Some(&0x0d) {
1392 return Vec::new();
1393 }
1394 let Some(template) = freeblock_template(page_bytes, hdr_off, self.header.text_encoding)
1395 else {
1396 return Vec::new();
1397 };
1398 let Ok((freed_leaves, _trunks)) = self.freelist_pages_split() else {
1399 return Vec::new();
1400 };
1401 let usable = self.header.usable_size() as usize;
1402
1403 let mut out = Vec::new();
1404 // Walk the freeblock chain; at each freeblock head, try a clobbered-spill
1405 // reconstruction (the chain pass reaches the clobbered prefix the
1406 // intact-prefix recognizer cannot read).
1407 let first_freeblock = be_u16(page_bytes, hdr_off + 1) as usize;
1408 let mut fb = first_freeblock;
1409 let mut walked = 0usize;
1410 let mut visited = std::collections::BTreeSet::new();
1411 while fb != 0 && walked < MAX_FREEBLOCKS_PER_PAGE {
1412 walked += 1;
1413 if !visited.insert(fb) {
1414 break; // cyclic next pointer
1415 }
1416 let next = be_u16(page_bytes, fb) as usize;
1417 if let Some((cell, chain)) =
1418 template.reconstruct_spilled(self, page_bytes, fb, usable, &freed_leaves)
1419 {
1420 out.push((cell, chain));
1421 }
1422 fb = next;
1423 }
1424 out
1425 }
1426
1427 /// Tier-2 salvage for **spilled** cells whose overflow chain is broken (task
1428 /// #73, Codex ruling #4): when [`Database::carve_overflow_records`] rejects a
1429 /// recognized spilled cell because its chain failed (a trunk-clobbered or
1430 /// reused chain page), the cell's intact LOCAL prefix still holds the columns
1431 /// whose bodies fit entirely on the leaf page. Those are salvaged as a
1432 /// [`CellFragment`] — the same Tier-2 surface freeblock reconstruction uses.
1433 ///
1434 /// Only columns whose body lies wholly within the local payload are kept; the
1435 /// chain-resident columns are lost (untrusted by definition — the chain that
1436 /// would supply them is the thing that failed). A fragment is emitted only
1437 /// when the salvaged prefix carries ≥ 1 distinctive cell (TEXT ≥ 4 bytes of
1438 /// valid UTF-8, or REAL — the §3.1 gate), so a lone integer prefix never
1439 /// anchors one. Bounded and panic-free.
1440 #[must_use]
1441 pub fn carve_overflow_fragments(&self, page_bytes: &[u8]) -> Vec<CellFragment> {
1442 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1443 SQLITE_HEADER_SIZE
1444 } else {
1445 0
1446 };
1447 let Some(&page_type) = page_bytes.get(hdr_off) else {
1448 return Vec::new();
1449 };
1450 if page_type != 0x0d {
1451 return Vec::new();
1452 }
1453 let Ok((freed_leaves, _trunks)) = self.freelist_pages_split() else {
1454 return Vec::new();
1455 };
1456 let usable = self.header.usable_size() as usize;
1457
1458 let mut out = Vec::new();
1459 let regions = self.free_regions_of_leaf(page_bytes, hdr_off);
1460 for (lo, hi) in regions {
1461 let Some(region) = page_bytes.get(lo..hi) else {
1462 continue; // cov:unreachable: free_regions yields in-bounds spans
1463 };
1464 let mut off = 0usize;
1465 while off < region.len() {
1466 let Some(sc) = try_carve_spilled_cell_at(region, off, usable, None) else {
1467 off += 1;
1468 continue;
1469 };
1470 // Only broken chains degrade to a fragment — an intact chain is a
1471 // Tier-1 row (handled by carve_overflow_records), never both.
1472 let remaining = sc.payload_len.saturating_sub(sc.local_len);
1473 let chain_ok = self
1474 .read_freed_overflow_chain(sc.first_overflow, remaining, usable, &freed_leaves)
1475 .is_ok();
1476 if !chain_ok {
1477 if let Some(mut frag) =
1478 salvage_local_prefix(region, &sc, self.header.text_encoding)
1479 {
1480 frag.offset += lo;
1481 out.push(frag);
1482 }
1483 }
1484 off += sc.byte_len.max(1);
1485 }
1486 }
1487 out
1488 }
1489
1490 /// Reconstruct deleted records from the **freeblock chain** of an allocated
1491 /// table-leaf page (type `0x0d`) — the records a forward parse cannot recover
1492 /// because their first four bytes were destroyed by freeblock conversion.
1493 ///
1494 /// When SQLite frees an in-page cell it converts it into a **freeblock**
1495 /// (file-format §1.6): the cell's first two bytes become the next-freeblock
1496 /// offset and the next two the freeblock size, **overwriting the cell's
1497 /// payload-length + rowid varints, the record `header_len` varint, and the
1498 /// leading serial type(s)**. The record's surviving serial-type tail and its
1499 /// whole value body remain intact *after* those four bytes.
1500 ///
1501 /// This method rebuilds each freed cell from that surviving tail plus a
1502 /// **schema template** derived from a LIVE cell on the same page (the table's
1503 /// column count, header length, and the serial types of the leading columns
1504 /// that fall inside the clobbered prefix). The destroyed rowid is surfaced as
1505 /// unknown (`0`) — never invented — and the record is graded LOW.
1506 ///
1507 /// Precision discipline (task #56): a candidate is emitted only when its body
1508 /// decodes cleanly with every serial type legal AND the record fits within
1509 /// the freeblock's `[offset, offset + size)` bounds. Implausible or
1510 /// out-of-bounds candidates are rejected, so reconstruction does not
1511 /// manufacture phantom rows. (The forensic layer additionally drops any
1512 /// reconstruction whose values match a live row, so a live row is never
1513 /// re-surfaced.)
1514 ///
1515 /// Bounded and panic-free: every freeblock pointer, size, and serial length
1516 /// is range-checked against the page before use, and the chain walk is capped
1517 /// at `MAX_FREEBLOCKS_PER_PAGE` to defeat a crafted cyclic `next` chain.
1518 /// Non-leaf pages, pages with no freeblock chain, and pages with no usable
1519 /// schema template yield an empty result.
1520 #[must_use]
1521 pub fn reconstruct_freeblock_records(&self, page_bytes: &[u8]) -> Vec<CarvedCell> {
1522 // Tier-1 cells are the `.0` of the shared two-tier walker, so the full-row
1523 // output and the fragment output ([`Database::reconstruct_freeblock_fragments`])
1524 // can never diverge. The walk (freeblock-chain pass + unallocated-gap pass)
1525 // and its precision discipline live in [`reconstruct_freeblock_inner`].
1526 let _ = self;
1527 reconstruct_freeblock_inner(page_bytes, self.header.text_encoding).0
1528 }
1529
1530 /// Tier-2 partial salvage: the [`CellFragment`]s abandoned by
1531 /// [`Database::reconstruct_freeblock_records`] on this page.
1532 ///
1533 /// At every anchor where full reconstruction failed — an illegal serial in
1534 /// the surviving tail, a tail that overruns the span, or a body that does not
1535 /// fit — the columns that DID decode cleanly before the failure are salvaged
1536 /// as the maximal decodable prefix. A fragment is emitted only when that
1537 /// prefix contains at least one *distinctive* cell (TEXT ≥ 4 bytes of valid
1538 /// UTF-8, or REAL): a lone surviving integer pattern is coincidence-prone and
1539 /// never anchors a fragment.
1540 ///
1541 /// Mutually exclusive with the full reconstructions of
1542 /// [`Database::reconstruct_freeblock_records`] **by construction**: an anchor
1543 /// yields a cell or a fragment, never both. Inherits the same anchor
1544 /// discipline — no sliding scan, no strings-style hunt — so Tier-2 carries
1545 /// Tier-1's precision architecture. Bounded and panic-free identically.
1546 #[must_use]
1547 pub fn reconstruct_freeblock_fragments(&self, page_bytes: &[u8]) -> Vec<CellFragment> {
1548 let _ = self;
1549 reconstruct_freeblock_inner(page_bytes, self.header.text_encoding).1
1550 }
1551
1552 /// Parse the LIVE cells of an index-b-tree **leaf** page (type `0x0a`) into
1553 /// their decoded key records (roadmap §1.4 foundation). A regular index on a
1554 /// rowid table stores each entry as `(indexed columns…, rowid)`; a
1555 /// `WITHOUT ROWID` table stores its whole row here (the row IS the key). This
1556 /// is the structural read every later index-carve / `WITHOUT ROWID` recovery
1557 /// builds on — the second substrate for a table's data, where key columns
1558 /// survive even when the table-leaf residue is gone.
1559 ///
1560 /// Reads live cells only (via the cell-pointer array); returns empty for any
1561 /// non-index-leaf page, so a table page is never mis-read. Bounded and
1562 /// panic-free — every read is bounds-checked; a cell whose payload does not
1563 /// decode is skipped rather than panicking.
1564 ///
1565 /// SCOPE (foundation): decodes the LOCAL payload only. An index key large
1566 /// enough to spill onto an overflow-page chain is decoded up to its on-page
1567 /// bytes (the leading key columns still resolve); full overflow following, and
1568 /// carving DELETED index entries from index-page freeblocks, are follow-ups.
1569 #[must_use]
1570 pub fn index_leaf_cells(&self, page_bytes: &[u8]) -> Vec<Vec<Value>> {
1571 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
1572 SQLITE_HEADER_SIZE
1573 } else {
1574 0
1575 };
1576 if page_bytes.get(hdr_off) != Some(&0x0a) {
1577 return Vec::new(); // only index-b-tree leaf pages carry index cells
1578 }
1579 let cell_count = be_u16(page_bytes, hdr_off + 3) as usize;
1580 let cell_ptr_array = hdr_off + 8; // an index-leaf header is 8 bytes
1581 let mut out = Vec::with_capacity(cell_count);
1582 for i in 0..cell_count {
1583 let ptr_off = cell_ptr_array + i * 2;
1584 if ptr_off + 1 >= page_bytes.len() {
1585 break;
1586 }
1587 let cell_off = be_u16(page_bytes, ptr_off) as usize;
1588 if cell_off == 0 || cell_off >= page_bytes.len() {
1589 continue;
1590 }
1591 // An index-leaf cell is [payload-length varint][payload][overflow?].
1592 if let Some(values) = self.index_record_at(page_bytes, cell_off) {
1593 out.push(values);
1594 }
1595 }
1596 out
1597 }
1598
1599 /// Decode the index record whose `[payload-length varint][payload]` begins at
1600 /// `off` within `page_bytes`, or `None` if it does not decode. Shared by the
1601 /// leaf read ([`index_leaf_cells`](Self::index_leaf_cells)) and the interior
1602 /// walk (whose cells also carry a key record, after the 4-byte child pointer).
1603 /// Decodes the LOCAL payload only — a key spilled to an overflow chain is
1604 /// decoded up to its on-page bytes (the leading key columns still resolve).
1605 fn index_record_at(&self, page_bytes: &[u8], off: usize) -> Option<Vec<Value>> {
1606 let (payload_len, n) = read_varint(page_bytes, off).ok()?;
1607 let payload_start = off + n;
1608 let payload_len = usize::try_from(payload_len).ok()?;
1609 let end = payload_start
1610 .saturating_add(payload_len)
1611 .min(page_bytes.len());
1612 let payload = page_bytes.get(payload_start..end)?;
1613 decode_index_payload(payload, self.header.text_encoding).ok()
1614 }
1615
1616 /// The live rows of every `WITHOUT ROWID` user table (roadmap §1.4).
1617 ///
1618 /// A `WITHOUT ROWID` table stores its whole row in an **index b-tree** — there
1619 /// is no separate table b-tree and no rowid — so the ordinary
1620 /// [`read_table`](Self::read_table) reader (which walks table pages 0x0d/0x05)
1621 /// is blind to it. This resolves each such table from `sqlite_master`, walks
1622 /// its index b-tree (interior 0x02 → leaf 0x0a), and returns its live rows,
1623 /// keyed by table name. Ordinary rowid tables are not returned.
1624 ///
1625 /// Bounded and panic-free: a malformed/cyclic b-tree stops the walk (visited
1626 /// set + page cap) rather than looping; an unreadable schema yields an empty
1627 /// result. Rows are the decoded index records, in the table's column order.
1628 #[must_use]
1629 pub fn without_rowid_table_rows(&self) -> Vec<WithoutRowidTable> {
1630 let Ok(schema) = self.read_table(1, 5) else {
1631 return Vec::new(); // cov:unreachable: a validly-opened DB has a readable page-1 schema
1632 };
1633 let mut out = Vec::new();
1634 for row in schema {
1635 // sqlite_master row: (type, name, tbl_name, rootpage, sql).
1636 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
1637 if !is_table {
1638 continue;
1639 }
1640 let Some(Value::Text(name)) = row.values.get(1) else {
1641 continue; // cov:unreachable: a 'table' schema row has a TEXT name
1642 };
1643 if name.starts_with("sqlite_") {
1644 continue;
1645 }
1646 let sql = match row.values.get(4) {
1647 Some(Value::Text(s)) => s.as_str(),
1648 _ => "", // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
1649 };
1650 if !without_rowid_sql(sql) {
1651 continue; // ordinary rowid table — read_table handles those
1652 }
1653 let Some(Value::Integer(root)) = row.values.get(3) else {
1654 continue; // cov:unreachable: a 'table' schema row has an integer rootpage
1655 };
1656 let Ok(root) = u32::try_from(*root) else {
1657 continue; // cov:unreachable: a real rootpage is a small positive page number
1658 };
1659 let mut rows = Vec::new();
1660 let mut seen = std::collections::BTreeSet::new();
1661 self.collect_index_rows(root, &mut rows, &mut seen);
1662 out.push(WithoutRowidTable {
1663 name: name.clone(),
1664 rows,
1665 });
1666 }
1667 out
1668 }
1669
1670 /// Walk the index b-tree rooted at `page`, appending every leaf cell's decoded
1671 /// record to `rows`. Interior pages (0x02) recurse through their child pointers
1672 /// and rightmost child; leaf pages (0x0a) yield their cells via
1673 /// [`index_leaf_cells`](Self::index_leaf_cells). Bounded identically to
1674 /// [`collect_rows`](Self::collect_rows): a page is visited at most once and the
1675 /// walk is capped, so a crafted cyclic/oversized tree cannot loop.
1676 fn collect_index_rows(
1677 &self,
1678 page: u32,
1679 rows: &mut Vec<Vec<Value>>,
1680 seen: &mut std::collections::BTreeSet<u32>,
1681 ) {
1682 if page == 0 || seen.len() > MAX_PAGES_PER_WALK || !seen.insert(page) {
1683 return;
1684 }
1685 let Ok(slice) = self.page_slice(page) else {
1686 return; // cov:unreachable: schema rootpages and their children are in range
1687 };
1688 let slice = &*slice;
1689 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
1690 let Some(&page_type) = slice.get(hdr_off) else {
1691 return; // cov:unreachable: a full page slice always has its header byte
1692 };
1693 match page_type {
1694 0x0a => rows.extend(self.index_leaf_cells(slice)),
1695 0x02 => {
1696 let cell_count = be_u16(slice, hdr_off + 3) as usize;
1697 let cell_ptr_array = hdr_off + 12; // an index-interior header is 12 bytes
1698 for i in 0..cell_count {
1699 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
1700 // An interior cell is [4-byte left-child page][key record]. In
1701 // an INDEX b-tree the key IS a real entry (a WITHOUT ROWID row),
1702 // so decode it too — not just the child pointer, unlike a table
1703 // b-tree where interior cells are pure navigation.
1704 let child = be_u32(slice, cell_off);
1705 self.collect_index_rows(child, rows, seen);
1706 if let Some(values) = self.index_record_at(slice, cell_off + 4) {
1707 rows.push(values);
1708 }
1709 }
1710 let right = be_u32(slice, hdr_off + 8);
1711 self.collect_index_rows(right, rows, seen);
1712 }
1713 _ => {} // cov:unreachable: a WITHOUT ROWID b-tree page is index leaf (0x0a) or interior (0x02)
1714 }
1715 }
1716
1717 /// The maximal FREE (unallocated) byte ranges of a table-leaf page — the
1718 /// complement of its live cells within the cell-content area. Shared by
1719 /// [`Database::carve_free_regions`] and
1720 /// [`Database::reconstruct_freeblock_records`] so both scan exactly the same
1721 /// ranges and never touch a live cell. Returns empty for a non-leaf page.
1722 fn free_regions_of_leaf(&self, page_bytes: &[u8], hdr_off: usize) -> Vec<(usize, usize)> {
1723 if page_bytes.get(hdr_off) != Some(&0x0d) {
1724 return Vec::new(); // cov:unreachable: callers gate on page_type == 0x0d
1725 }
1726 let cell_count = be_u16(page_bytes, hdr_off + 3) as usize;
1727 let cell_ptr_array = hdr_off + 8; // leaf header is 8 bytes
1728 let usable = self.header.usable_size() as usize;
1729 let mut live: Vec<(usize, usize)> = Vec::with_capacity(cell_count);
1730 for i in 0..cell_count {
1731 let cell_off = be_u16(page_bytes, cell_ptr_array + i * 2) as usize;
1732 if cell_off == 0 || cell_off >= page_bytes.len() {
1733 continue; // cov:unreachable: a valid leaf points cells within page
1734 }
1735 if let Some(len) = live_cell_len(page_bytes, cell_off, usable) {
1736 live.push((cell_off, cell_off.saturating_add(len)));
1737 }
1738 }
1739 live.sort_unstable_by_key(|&(s, _)| s);
1740 let content_lo = cell_ptr_array + cell_count * 2;
1741 free_regions(&live, content_lo, page_bytes.len())
1742 }
1743
1744 /// Whether `sqlite_master` (the schema table rooted at page 1) lists at least
1745 /// one **user** table — i.e. a `type='table'` row whose name is not an
1746 /// internal `sqlite_*` table. A database where every table was `DROP`ped (or
1747 /// that never had one) returns `false`; the forensic carver uses this to label
1748 /// freed content as dropped-table residue. Errors (unreadable schema) are
1749 /// treated as "no user table" so the carver degrades safely.
1750 #[must_use]
1751 pub fn has_user_table(&self) -> bool {
1752 // sqlite_master is a 5-column table: (type, name, tbl_name, rootpage, sql).
1753 let Ok(rows) = self.read_table(1, 5) else {
1754 return false; // cov:unreachable: a validly-opened DB has a readable page-1 schema
1755 };
1756 rows.iter().any(|row| {
1757 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
1758 let user = matches!(
1759 row.values.get(1),
1760 Some(Value::Text(n)) if !n.starts_with("sqlite_")
1761 );
1762 is_table && user
1763 })
1764 }
1765
1766 /// Collect the rowids of every **currently-live** row across all user table
1767 /// b-trees (the roots listed in `sqlite_master`). The forensic carver uses
1768 /// this to drop any carved "deleted" record whose rowid is in fact still live
1769 /// — a stale copy of a live row can linger in free space after a b-tree
1770 /// rebalance moved the row to another page, and reporting it as deleted would
1771 /// be a false positive. Rowid collection ignores the column count (the rowid
1772 /// is in the cell prefix), so it works even when a schema row is malformed.
1773 ///
1774 /// Bounded and panic-free: unreadable schema or a malformed b-tree yields a
1775 /// partial (possibly empty) set rather than an error.
1776 #[must_use]
1777 pub fn live_rowids(&self) -> std::collections::BTreeSet<i64> {
1778 let mut ids = std::collections::BTreeSet::new();
1779 let Ok(schema) = self.read_table(1, 5) else {
1780 return ids; // cov:unreachable: a validly-opened DB has a readable page-1 schema
1781 };
1782 for row in schema {
1783 // sqlite_master row: (type, name, tbl_name, rootpage, sql).
1784 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
1785 if !is_table {
1786 continue; // cov:unreachable: the test fixtures' schemas hold only table rows
1787 }
1788 let Some(Value::Integer(root)) = row.values.get(3) else {
1789 continue; // cov:unreachable: a 'table' schema row always has an integer rootpage
1790 };
1791 let Ok(root) = u32::try_from(*root) else {
1792 continue; // cov:unreachable: a real rootpage is a small positive page number
1793 };
1794 let mut seen = std::collections::BTreeSet::new();
1795 self.collect_rowids(root, &mut ids, &mut seen);
1796 }
1797 ids
1798 }
1799
1800 /// Collect every **currently-live** row's decoded column values, keyed by
1801 /// rowid, across all user table b-trees. This is the value-aware companion to
1802 /// [`Database::live_rowids`]: the forensic carver uses it to tell a stale
1803 /// rebalance copy (same rowid AND same values → drop) from a deleted prior
1804 /// version (same rowid but DIFFERENT values → recover, e.g. an edited message
1805 /// or a changed amount).
1806 ///
1807 /// Column values are decoded by inferring the column count from each live
1808 /// cell's own serial-type array (the same self-describing record format the
1809 /// carver uses), so no schema column count is required. Best-effort,
1810 /// bounded, and panic-free: a malformed b-tree yields a partial map.
1811 #[must_use]
1812 pub fn live_rows(&self) -> std::collections::BTreeMap<i64, Vec<Value>> {
1813 let mut rows = std::collections::BTreeMap::new();
1814 let Ok(schema) = self.read_table(1, 5) else {
1815 return rows; // cov:unreachable: a validly-opened DB has a readable page-1 schema
1816 };
1817 for row in schema {
1818 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
1819 if !is_table {
1820 continue; // cov:unreachable: the test fixtures' schemas hold only table rows
1821 }
1822 let Some(Value::Integer(root)) = row.values.get(3) else {
1823 continue; // cov:unreachable: a 'table' schema row always has an integer rootpage
1824 };
1825 let Ok(root) = u32::try_from(*root) else {
1826 continue; // cov:unreachable: a real rootpage is a small positive page number
1827 };
1828 let mut seen = std::collections::BTreeSet::new();
1829 self.collect_rows(root, &mut rows, &mut seen);
1830 }
1831 rows
1832 }
1833
1834 /// Decode every **currently-live** `sqlite_master` row (the schema table
1835 /// rooted at page 1) into its column values: `(type, name, tbl_name,
1836 /// rootpage, sql)`. This is the schema-table companion to
1837 /// [`Database::live_rows`], which collects only USER-table b-trees and so
1838 /// never sees the schema rows themselves.
1839 ///
1840 /// The forensic carver folds these into the same value-based live set it uses
1841 /// to drop stale copies of live user rows: a record carved from a materialized
1842 /// page 1 whose values equal a CURRENT schema row is the live schema entry
1843 /// re-surfaced (drop it), whereas a genuinely-deleted PRIOR schema version has
1844 /// different values (e.g. an old `CREATE TABLE`) and is still recovered.
1845 ///
1846 /// Best-effort, bounded, and panic-free: an unreadable schema yields an empty
1847 /// vector rather than an error.
1848 #[must_use]
1849 pub fn live_schema_rows(&self) -> Vec<Vec<Value>> {
1850 match self.read_table(1, 5) {
1851 Ok(rows) => rows.into_iter().map(|row| row.values).collect(),
1852 Err(_) => Vec::new(), // cov:unreachable: a validly-opened DB has a readable page-1 schema
1853 }
1854 }
1855
1856 /// Every live (schema-present) **user** table, as [`attribution::LiveTable`]:
1857 /// name, rootpage, parsed column names (or `None` when low-confidence), and
1858 /// declared column affinities. Internal `sqlite_*` tables are excluded.
1859 ///
1860 /// The forensic attribution step uses this to know each table's real column
1861 /// names (Tier-1) and its shape signature (Tier-2). Best-effort, bounded,
1862 /// panic-free: an unreadable schema yields an empty vector.
1863 #[must_use]
1864 pub fn live_tables(&self) -> Vec<attribution::LiveTable> {
1865 let mut tables = Vec::new();
1866 let Ok(schema) = self.read_table(1, 5) else {
1867 return tables; // cov:unreachable: a validly-opened DB has a readable page-1 schema
1868 };
1869 for row in schema {
1870 // sqlite_master row: (type, name, tbl_name, rootpage, sql).
1871 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
1872 if !is_table {
1873 continue;
1874 }
1875 let Some(Value::Text(name)) = row.values.get(1) else {
1876 continue; // cov:unreachable: a 'table' schema row always has a TEXT name
1877 };
1878 if name.starts_with("sqlite_") {
1879 continue;
1880 }
1881 let Some(Value::Integer(root)) = row.values.get(3) else {
1882 continue; // cov:unreachable: a 'table' schema row always has an integer rootpage
1883 };
1884 let Ok(rootpage) = u32::try_from(*root) else {
1885 continue; // cov:unreachable: a real rootpage is a small positive page number
1886 };
1887 // The CREATE TABLE statement (column 5). A non-TEXT/absent sql is
1888 // possible on a damaged schema — degrade to no parsed columns.
1889 let sql = match row.values.get(4) {
1890 Some(Value::Text(s)) => s.as_str(),
1891 _ => "", // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
1892 };
1893 let defs = attribution::column_defs(sql);
1894 let affinities = defs.as_ref().map_or_else(Vec::new, |d| {
1895 d.iter()
1896 .map(|(_, ty)| attribution::column_affinity(ty))
1897 .collect()
1898 });
1899 // Only trust parsed names; if parsing failed, the caller uses c0..cN.
1900 let column_names = defs.map(|d| d.into_iter().map(|(n, _)| n).collect());
1901 tables.push(attribution::LiveTable {
1902 name: name.clone(),
1903 rootpage,
1904 column_names,
1905 affinities,
1906 create_sql: sql.to_string(),
1907 });
1908 }
1909 tables
1910 }
1911
1912 /// The live `sqlite_master` as a `name -> CREATE SQL` map for every **user**
1913 /// table (internal `sqlite_*` tables excluded) — the CURRENT-schema half of
1914 /// the Detector-B sidecar schema-change comparison
1915 /// (`docs/design/drop-recreate-attribution.md`).
1916 ///
1917 /// Reads the same page-1 schema b-tree as [`Self::live_tables`] but keeps the
1918 /// raw CREATE SQL text (not just parsed columns), so a caller can compare the
1919 /// verbatim schema against a sidecar's prior `sqlite_master`. Best-effort,
1920 /// bounded, panic-free: an unreadable schema yields an empty map.
1921 #[must_use]
1922 pub fn schema_sql(&self) -> std::collections::BTreeMap<String, String> {
1923 let mut out = std::collections::BTreeMap::new();
1924 let Ok(schema) = self.read_table(1, 5) else {
1925 return out; // cov:unreachable: a validly-opened DB has a readable page-1 schema
1926 };
1927 for row in schema {
1928 schema_sql_insert(&mut out, &row.values);
1929 }
1930 out
1931 }
1932
1933 /// Per-table, per-rowid VERSION HISTORY reconstructed from this database's WAL
1934 /// temporal model (or just the live view when no `-wal` is present).
1935 ///
1936 /// See [`row_history`] for the full model. Walks each salt epoch's commit
1937 /// snapshots in commit order, then the final live view, and emits — per rowid
1938 /// — the sequence of distinct record values it held (insert / update / delete /
1939 /// reinsert), with evidence-based [`row_history::ViewState`] and NO timestamps.
1940 /// Degrades cleanly to live-only history when [`Database::wal_timeline`] is
1941 /// `None`. `WITHOUT ROWID` tables are recorded with `without_rowid = true` and
1942 /// no versions (they have no rowid to key a history on).
1943 #[must_use]
1944 pub fn row_histories(&self) -> Vec<row_history::TableHistory> {
1945 use row_history::{RowView, VersionOrigin};
1946
1947 // Live tables: name, header columns, live rows, and a WITHOUT ROWID flag
1948 // read from the live schema (a WITHOUT ROWID table has no rowid history).
1949 let live_dumps = self.live_table_rows();
1950 let without_rowid = self.live_without_rowid_map();
1951 // WITHOUT ROWID tables' live rows (index-b-tree read); folded into each
1952 // matching history below (§1.4).
1953 let wr_rows = self.without_rowid_table_rows();
1954
1955 // Per table, build the chronological views: each WAL commit snapshot (in
1956 // epoch order, commit_seq = per-epoch ordinal) then the final live view.
1957 let mut histories = Vec::with_capacity(live_dumps.len());
1958 for dump in live_dumps {
1959 let wr = without_rowid.get(&dump.name).copied().unwrap_or(false);
1960 let mut views: Vec<RowView> = Vec::new();
1961
1962 // Historical views from the WAL timeline, if any.
1963 if let Some(timeline) = self.wal_timeline() {
1964 // commit_seq is monotonic WITHIN a salt epoch only — count per
1965 // segment, never one global sequence spanning a salt reset.
1966 let mut seq_in_segment: std::collections::BTreeMap<WalSegmentId, u32> =
1967 std::collections::BTreeMap::new();
1968 for snapshot in timeline.commit_snapshots() {
1969 let seg = snapshot.id().segment;
1970 let seq = seq_in_segment.entry(seg).or_insert(0);
1971 let commit_seq = *seq;
1972 *seq += 1;
1973
1974 // Resolve THIS table from the snapshot's OWN schema (a rootpage
1975 // can be reused by a different table across commits).
1976 let snap_tables = snapshot.tables();
1977 let Some(st) = snap_tables.iter().find(|t| t.name == dump.name) else {
1978 continue; // table did not exist at this commit
1979 };
1980 if st.without_rowid {
1981 continue; // no rowid history for a WITHOUT ROWID table
1982 }
1983 // schema_known: the snapshot's CREATE TABLE parsed to columns.
1984 let schema_known = !st.columns.is_empty();
1985 let rows = match snapshot.read_table(st.rootpage, st.columns.len()) {
1986 Ok(rows) => rows.into_iter().collect(),
1987 // An unreadable historical b-tree contributes no rows but
1988 // must not abort the whole history.
1989 Err(_) => std::collections::BTreeMap::new(),
1990 };
1991 views.push(RowView {
1992 commit_seq: Some(commit_seq),
1993 is_final: false,
1994 checksum_valid: snapshot.checksum_valid(),
1995 schema_known,
1996 origin: VersionOrigin::Commit(snapshot.id()),
1997 rows,
1998 });
1999 }
2000 }
2001
2002 // The final live view (current on-disk ⊕ WAL state).
2003 let live_rows: std::collections::BTreeMap<i64, Vec<Value>> = dump
2004 .rows
2005 .iter()
2006 .map(|r| (r.rowid, r.values.clone()))
2007 .collect();
2008 views.push(RowView {
2009 commit_seq: None,
2010 is_final: true,
2011 checksum_valid: true,
2012 schema_known: true,
2013 origin: VersionOrigin::Live,
2014 rows: live_rows,
2015 });
2016
2017 let mut history = row_history::table_history(dump.name, dump.column_names, wr, &views);
2018 // A WITHOUT ROWID table has no rowid version history, but its live rows
2019 // live in the index b-tree (§1.4) — read them so the carve output shows
2020 // the table's data, not just a "not version-tracked" note.
2021 if wr {
2022 if let Some(t) = wr_rows.iter().find(|t| t.name == history.table) {
2023 history.without_rowid_rows.clone_from(&t.rows);
2024 }
2025 }
2026 histories.push(history);
2027 }
2028 histories
2029 }
2030
2031 /// Map each live user table's name to whether it is a `WITHOUT ROWID` table,
2032 /// read from the live `sqlite_master` schema. Best-effort and panic-free.
2033 fn live_without_rowid_map(&self) -> std::collections::BTreeMap<String, bool> {
2034 let mut map = std::collections::BTreeMap::new();
2035 let Ok(schema) = self.read_table(1, 5) else {
2036 return map; // cov:unreachable: a validly-opened DB has a readable page-1 schema
2037 };
2038 for row in schema {
2039 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
2040 if !is_table {
2041 continue;
2042 }
2043 let Some(Value::Text(name)) = row.values.get(1) else {
2044 continue; // cov:unreachable: a 'table' schema row has a TEXT name
2045 };
2046 if name.starts_with("sqlite_") {
2047 continue;
2048 }
2049 let sql = match row.values.get(4) {
2050 Some(Value::Text(s)) => s.as_str(),
2051 _ => "", // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
2052 };
2053 map.insert(name.clone(), without_rowid_sql(sql));
2054 }
2055 map
2056 }
2057
2058 /// The `sqlite_sequence` table `SQLite` maintains for `AUTOINCREMENT` tables,
2059 /// as `name → seq` — `seq` being the highest rowid ever assigned to that table
2060 /// (its monotonic INSERT high-water mark).
2061 ///
2062 /// `sqlite_sequence` exists **only** once at least one `AUTOINCREMENT` table
2063 /// has been created; a database with none returns an **empty** map (never a
2064 /// fabricated `seq = 0`), so a caller can distinguish "no high-water mark" from
2065 /// "high-water mark of 0". Best-effort, bounded, panic-free: an unreadable
2066 /// `sqlite_sequence` b-tree, or a malformed row, is omitted rather than
2067 /// erroring. Note `sqlite_sequence` is a mutable user table — `seq` tracks the
2068 /// INSERT high-water mark, not live rowid assignment — so this is a forensic
2069 /// HINT input, not proof of any row's provenance.
2070 #[must_use]
2071 pub fn sqlite_sequence(&self) -> std::collections::BTreeMap<String, i64> {
2072 let mut map = std::collections::BTreeMap::new();
2073 let Ok(schema) = self.read_table(1, 5) else {
2074 return map; // cov:unreachable: a validly-opened DB has a readable page-1 schema
2075 };
2076 // Locate the sqlite_sequence table's rootpage from the schema.
2077 let mut rootpage: Option<u32> = None;
2078 for row in &schema {
2079 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
2080 if !is_table {
2081 continue;
2082 }
2083 if !matches!(row.values.get(1), Some(Value::Text(n)) if n == "sqlite_sequence") {
2084 continue;
2085 }
2086 if let Some(Value::Integer(root)) = row.values.get(3) {
2087 rootpage = u32::try_from(*root).ok();
2088 }
2089 break;
2090 }
2091 let Some(root) = rootpage else {
2092 return map; // no AUTOINCREMENT table ⟹ no sqlite_sequence ⟹ empty
2093 };
2094 let Ok(rows) = self.read_table(root, 2) else {
2095 return map; // cov:unreachable: a present sqlite_sequence has a readable b-tree
2096 };
2097 for row in rows {
2098 // sqlite_sequence row: (name TEXT, seq INTEGER). A malformed row (wrong
2099 // types) is skipped — never a fabricated entry.
2100 let (Some(Value::Text(name)), Some(Value::Integer(seq))) =
2101 (row.values.first(), row.values.get(1))
2102 else {
2103 continue;
2104 };
2105 map.insert(name.clone(), *seq);
2106 }
2107 map
2108 }
2109
2110 /// Dump every live user table for export: name, header columns, and all live
2111 /// rows in rowid order. The base layer the combined live + recovered workbook
2112 /// is built over.
2113 ///
2114 /// For each [`Database::live_tables`] entry, the b-tree is read via
2115 /// [`Database::read_table`] (so rows arrive in ascending-rowid b-tree order).
2116 /// The header is the table's **real** column names when the schema parse was
2117 /// confident, otherwise generic `c0..c{N-1}` sized to the widest row — a
2118 /// header is always present and never a fabricated name. Best-effort and
2119 /// panic-free: a table whose b-tree is unreadable contributes an empty row set
2120 /// rather than erroring.
2121 #[must_use]
2122 pub fn live_table_rows(&self) -> Vec<LiveTableDump> {
2123 self.live_tables()
2124 .into_iter()
2125 .map(|table| {
2126 // `read_table`'s column_count drives only the INTEGER PRIMARY KEY
2127 // rowid-alias rule; use the declared arity when known, else 0
2128 // (no alias substitution) so a low-confidence schema still dumps.
2129 let declared = table.column_names.as_ref().map_or(0, Vec::len);
2130 let rows = self
2131 .read_table(table.rootpage, declared)
2132 .unwrap_or_default();
2133 let widest = rows.iter().map(|r| r.values.len()).max().unwrap_or(0);
2134 let column_names = match table.column_names {
2135 // Confident schema parse: use the table's real column names.
2136 // Live rows legitimately omit trailing NULLs, so `widest` may
2137 // be < declared — the real header still governs (a recovered
2138 // row pads/truncates to it).
2139 Some(names) => names,
2140 // Low-confidence parse (malformed/unparseable CREATE TABLE):
2141 // generic header sized to the widest row, never a fabricated
2142 // real name. This is the schema-damage robustness guard.
2143 None => (0..widest).map(|i| format!("c{i}")).collect(),
2144 };
2145 LiveTableDump {
2146 name: table.name,
2147 column_names,
2148 rows,
2149 }
2150 })
2151 .collect()
2152 }
2153
2154 /// A map from each **allocated** page that belongs to a live table's b-tree
2155 /// to that table's name. Built by walking every live table's b-tree page set
2156 /// from its rootpage (interior + leaf pages). A page carved as Tier-1
2157 /// in-page residue resolves to its owning table through this map.
2158 ///
2159 /// Best-effort and bounded, mirroring `live_rowids`'s b-tree walk: a
2160 /// malformed b-tree contributes fewer entries rather than erroring.
2161 #[must_use]
2162 pub fn page_to_table_map(&self) -> std::collections::BTreeMap<u32, String> {
2163 let mut map = std::collections::BTreeMap::new();
2164 for table in self.live_tables() {
2165 let mut pages = std::collections::BTreeSet::new();
2166 let mut visited = 0usize;
2167 self.collect_pages(table.rootpage, &mut pages, &mut visited);
2168 for page in pages {
2169 map.insert(page, table.name.clone());
2170 }
2171 }
2172 map
2173 }
2174
2175 /// Walk the table b-tree rooted at `page`, inserting every page it visits
2176 /// (interior + leaf) into `pages`. Best-effort and bounded, mirroring
2177 /// `collect_rowids`.
2178 fn collect_pages(
2179 &self,
2180 page: u32,
2181 pages: &mut std::collections::BTreeSet<u32>,
2182 visited: &mut usize,
2183 ) {
2184 *visited += 1;
2185 if *visited > MAX_PAGES_PER_WALK {
2186 return; // cov:unreachable: test b-trees are far below the 1M-page cap
2187 }
2188 if page == 0 || !pages.insert(page) {
2189 return; // page 0 sentinel, or already visited (cycle guard)
2190 }
2191 let Ok(slice) = self.page_slice(page) else {
2192 return; // cov:unreachable: schema rootpages and their children are in range
2193 };
2194 let slice = &*slice; // PageBytes -> &[u8]; body below is source-agnostic
2195 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
2196 let Some(&page_type) = slice.get(hdr_off) else {
2197 return; // cov:unreachable: a full page slice always has its header byte
2198 };
2199 if page_type != 0x05 {
2200 return; // leaf (0x0d) or non-interior: no children to descend
2201 }
2202 let cell_count = be_u16(slice, hdr_off + 3) as usize;
2203 let cell_ptr_array = hdr_off + 12;
2204 for i in 0..cell_count {
2205 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
2206 let child = be_u32(slice, cell_off);
2207 self.collect_pages(child, pages, visited);
2208 }
2209 let right = be_u32(slice, hdr_off + 8);
2210 self.collect_pages(right, pages, visited);
2211 }
2212
2213 /// Walk the table b-tree rooted at `page`, decoding every live leaf cell's
2214 /// values (column count inferred per cell) into `rows` keyed by rowid.
2215 /// Best-effort and bounded, mirroring [`Database::collect_rowids`].
2216 fn collect_rows(
2217 &self,
2218 page: u32,
2219 rows: &mut std::collections::BTreeMap<i64, Vec<Value>>,
2220 seen: &mut std::collections::BTreeSet<u32>,
2221 ) {
2222 // Visit each page at most once. A manipulated interior left-child or
2223 // right-most pointer (anti-forensic corpus category 12) can point back
2224 // into an already-visited page, and a counter-only guard would still
2225 // recurse a million frames deep before stopping — a stack overflow. The
2226 // visited-set bounds recursion DEPTH to the number of distinct pages,
2227 // mirroring `collect_pages`'s cycle guard.
2228 if page == 0 || seen.len() > MAX_PAGES_PER_WALK || !seen.insert(page) {
2229 return;
2230 }
2231 let Ok(slice) = self.page_slice(page) else {
2232 return; // cov:unreachable: schema rootpages and their children are in range
2233 };
2234 let slice = &*slice; // PageBytes -> &[u8]; body below is source-agnostic
2235 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
2236 let Some(&page_type) = slice.get(hdr_off) else {
2237 return; // cov:unreachable: a full page slice always has its header byte
2238 };
2239 let cell_count = be_u16(slice, hdr_off + 3) as usize;
2240 match page_type {
2241 0x0d => {
2242 let cell_ptr_array = hdr_off + 8;
2243 for i in 0..cell_count {
2244 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
2245 // Decode the live cell with an inferred column count; on any
2246 // parse hiccup (e.g. a table narrower than MIN_INFERRED_COLUMNS),
2247 // fall back to the rowid alone (empty values) so the row is
2248 // still known to be live.
2249 if let Some(cell) =
2250 try_carve_cell_at(slice, cell_off, None, self.header.text_encoding)
2251 {
2252 rows.insert(cell.rowid, cell.values);
2253 } else if let Some(rowid) = live_cell_rowid(slice, cell_off) {
2254 rows.entry(rowid).or_default(); // cov:unreachable: a >=2-col live cell always decodes above
2255 }
2256 }
2257 }
2258 0x05 => {
2259 let cell_ptr_array = hdr_off + 12;
2260 for i in 0..cell_count {
2261 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
2262 let child = be_u32(slice, cell_off);
2263 self.collect_rows(child, rows, seen);
2264 }
2265 let right = be_u32(slice, hdr_off + 8);
2266 self.collect_rows(right, rows, seen);
2267 }
2268 _ => {} // cov:unreachable: a table b-tree root/child is leaf (0x0d) or interior (0x05)
2269 }
2270 }
2271
2272 /// Walk the table b-tree rooted at `page`, inserting every live leaf cell's
2273 /// rowid into `ids`. Best-effort and bounded: a malformed/cyclic structure
2274 /// stops the walk rather than erroring or looping.
2275 fn collect_rowids(
2276 &self,
2277 page: u32,
2278 ids: &mut std::collections::BTreeSet<i64>,
2279 seen: &mut std::collections::BTreeSet<u32>,
2280 ) {
2281 // Visit each page at most once (see `collect_rows` for the rationale): a
2282 // manipulated child pointer that revisits a page must not recurse
2283 // unboundedly. The visited-set bounds recursion depth to distinct pages.
2284 if page == 0 || seen.len() > MAX_PAGES_PER_WALK || !seen.insert(page) {
2285 return;
2286 }
2287 let Ok(slice) = self.page_slice(page) else {
2288 return; // cov:unreachable: schema rootpages and their children are in range
2289 };
2290 let slice = &*slice; // PageBytes -> &[u8]; body below is source-agnostic
2291 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
2292 let Some(&page_type) = slice.get(hdr_off) else {
2293 return; // cov:unreachable: a full page slice always has its header byte
2294 };
2295 let cell_count = be_u16(slice, hdr_off + 3) as usize;
2296 match page_type {
2297 0x0d => {
2298 let cell_ptr_array = hdr_off + 8;
2299 for i in 0..cell_count {
2300 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
2301 if let Some(rowid) = live_cell_rowid(slice, cell_off) {
2302 ids.insert(rowid);
2303 }
2304 }
2305 }
2306 0x05 => {
2307 let cell_ptr_array = hdr_off + 12;
2308 for i in 0..cell_count {
2309 let cell_off = be_u16(slice, cell_ptr_array + i * 2) as usize;
2310 let child = be_u32(slice, cell_off);
2311 self.collect_rowids(child, ids, seen);
2312 }
2313 let right = be_u32(slice, hdr_off + 8);
2314 self.collect_rowids(right, ids, seen);
2315 }
2316 _ => {} // cov:unreachable: a table b-tree root/child is leaf (0x0d) or interior (0x05)
2317 }
2318 }
2319
2320 /// Walk a single table b-tree rooted at `root_page` (1-based) and collect
2321 /// every leaf row as typed values. `column_count` is the table's declared
2322 /// column count, used to apply the `INTEGER PRIMARY KEY` rowid-alias rule.
2323 ///
2324 /// Shares ONE b-tree/overflow walk with the snapshot-scoped read
2325 /// ([`CommitSnapshot::read_table`]) via an internal page-source abstraction, so
2326 /// the live and historical paths can never diverge.
2327 pub fn read_table(&self, root_page: u32, column_count: usize) -> Result<Vec<Row>, Error> {
2328 read_table_via(self, root_page, column_count)
2329 }
2330
2331 /// Bytes of the 1-based `page` number, or `PageOutOfRange`.
2332 ///
2333 /// When a WAL overlay is in effect and holds a committed version of this
2334 /// page, the overlaid bytes are returned in preference to the main file —
2335 /// this is what makes a table walk see the WAL-applied view. The main file
2336 /// is never mutated.
2337 fn page_slice(&self, page: u32) -> Result<PageBytes<'_>, Error> {
2338 if page == 0 {
2339 return Err(Error::PageOutOfRange(0));
2340 }
2341 if let Some(wal) = &self.wal {
2342 if let Some(overlaid) = wal.pages.get(&page) {
2343 return Ok(PageBytes::Borrowed(overlaid.as_slice()));
2344 }
2345 }
2346 self.source
2347 .page(page, self.header.page_size as usize)
2348 .ok_or(Error::PageOutOfRange(page))
2349 }
2350}
2351
2352/// A source of page images for the shared b-tree / overflow walk — the seam that
2353/// lets the live [`Database`] (main file ⊕ WAL overlay) and a historical
2354/// [`CommitSnapshot`] (materialized commit pages) share ONE table-read
2355/// implementation instead of forking parallel copies.
2356///
2357/// All page numbers are 1-based. Implementations resolve page 1 with the
2358/// 100-byte file header in place (so the walk reads the b-tree header at offset
2359/// `SQLITE_HEADER_SIZE` for page 1, 0 otherwise).
2360trait PageSource {
2361 /// The 1-based `page`'s full image, or `None` for page 0 / out of range.
2362 fn page(&self, page: u32) -> Option<PageBytes<'_>>;
2363 /// Usable bytes per page (`page_size` − reserved-space), for the overflow and
2364 /// local-payload computations.
2365 fn usable(&self) -> usize;
2366 /// The highest valid 1-based page number (the cycle/over-range bound).
2367 fn page_bound(&self) -> u32;
2368 /// The database text encoding, for decoding TEXT values.
2369 fn encoding(&self) -> TextEncoding;
2370}
2371
2372impl PageSource for Database {
2373 fn page(&self, page: u32) -> Option<PageBytes<'_>> {
2374 self.page_slice(page).ok()
2375 }
2376 fn usable(&self) -> usize {
2377 self.header.usable_size() as usize
2378 }
2379 fn page_bound(&self) -> u32 {
2380 self.file_page_count()
2381 }
2382 fn encoding(&self) -> TextEncoding {
2383 self.header.text_encoding
2384 }
2385}
2386
2387impl PageSource for CommitSnapshot {
2388 fn page(&self, page: u32) -> Option<PageBytes<'_>> {
2389 self.overlaid
2390 .get(&page)
2391 .map(|v| PageBytes::Borrowed(v.as_slice()))
2392 }
2393 fn usable(&self) -> usize {
2394 self.usable as usize
2395 }
2396 fn page_bound(&self) -> u32 {
2397 // The committed page count at this snapshot — the cycle/over-range bound
2398 // for an overflow walk over the snapshot's materialized pages.
2399 self.id.db_size_after_commit
2400 }
2401 fn encoding(&self) -> TextEncoding {
2402 // Text encoding from the snapshot's OWN page-1 header (byte 56), so a
2403 // historical read decodes TEXT per the encoding as of this commit.
2404 self.overlaid
2405 .get(&1)
2406 .map(|p| match be_u32(p, TEXT_ENCODING_OFFSET) {
2407 2 => TextEncoding::Utf16Le,
2408 3 => TextEncoding::Utf16Be,
2409 _ => TextEncoding::Utf8,
2410 })
2411 .unwrap_or_default()
2412 }
2413}
2414
2415/// Walk a single table b-tree rooted at `root_page` over any [`PageSource`],
2416/// collecting every leaf row as typed values. The one implementation shared by
2417/// the live and snapshot-scoped reads.
2418/// Insert a `sqlite_master` row's `name -> CREATE SQL` into `out` when the row is
2419/// a **user** table (`type='table'`, name not `sqlite_*`). Shared by
2420/// [`Database::schema_sql`] and [`PriorSnapshot::schema_sql`] so the live and
2421/// prior reads classify schema rows identically. A row that is not a user-table
2422/// row (an index/view/trigger, an internal table, or a malformed row) is skipped.
2423fn schema_sql_insert(out: &mut std::collections::BTreeMap<String, String>, values: &[Value]) {
2424 // sqlite_master row: (type, name, tbl_name, rootpage, sql).
2425 let is_table = matches!(values.first(), Some(Value::Text(t)) if t == "table");
2426 if !is_table {
2427 return;
2428 }
2429 let Some(Value::Text(name)) = values.get(1) else {
2430 return; // cov:unreachable: a 'table' schema row has a TEXT name
2431 };
2432 if name.starts_with("sqlite_") {
2433 return;
2434 }
2435 let sql = match values.get(4) {
2436 Some(Value::Text(s)) => s.clone(),
2437 _ => String::new(), // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
2438 };
2439 out.insert(name.clone(), sql);
2440}
2441
2442fn read_table_via(
2443 src: &dyn PageSource,
2444 root_page: u32,
2445 column_count: usize,
2446) -> Result<Vec<Row>, Error> {
2447 let mut rows = Vec::new();
2448 let mut seen = std::collections::BTreeSet::new();
2449 walk_table_page(src, root_page, column_count, &mut rows, &mut seen)?;
2450 Ok(rows)
2451}
2452
2453fn walk_table_page(
2454 src: &dyn PageSource,
2455 page: u32,
2456 column_count: usize,
2457 rows: &mut Vec<Row>,
2458 seen: &mut std::collections::BTreeSet<u32>,
2459) -> Result<(), Error> {
2460 // Visit each page at most once. A manipulated interior child pointer
2461 // (anti-forensic corpus category 12) can revisit an already-walked page; a
2462 // counter-only guard still recurses up to the cap deep before stopping,
2463 // overflowing the stack. The visited-set bounds recursion DEPTH to the
2464 // number of distinct pages. A revisited page is silently skipped (Ok) so a
2465 // crafted cycle yields the partial-but-valid rows already collected rather
2466 // than an error.
2467 if seen.len() > MAX_PAGES_PER_WALK {
2468 return Err(Error::TooManyPages);
2469 }
2470 if !seen.insert(page) {
2471 return Ok(());
2472 }
2473 let slice = src.page(page).ok_or(Error::PageOutOfRange(page))?;
2474 let slice = &*slice;
2475
2476 // Page 1 carries the 100-byte file header before its b-tree header.
2477 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
2478
2479 let page_type = *slice.get(hdr_off).ok_or(Error::TruncatedCell)?;
2480 let cell_count = be_u16(slice, hdr_off + 3) as usize;
2481
2482 match page_type {
2483 0x0d => read_leaf_cells(src, slice, hdr_off, cell_count, column_count, rows),
2484 0x05 => {
2485 // Interior table page: 12-byte header; cell = 4-byte child ptr +
2486 // varint key. Recurse into every child plus the right-most ptr.
2487 let cell_ptr_array = hdr_off + 12;
2488 for i in 0..cell_count {
2489 let p = cell_ptr_array + i * 2;
2490 let cell_off = be_u16(slice, p) as usize;
2491 let child = be_u32(slice, cell_off);
2492 walk_table_page(src, child, column_count, rows, seen)?;
2493 }
2494 let right = be_u32(slice, hdr_off + 8);
2495 walk_table_page(src, right, column_count, rows, seen)
2496 }
2497 other => Err(Error::NotATablePage(other)),
2498 }
2499}
2500
2501fn read_leaf_cells(
2502 src: &dyn PageSource,
2503 slice: &[u8],
2504 hdr_off: usize,
2505 cell_count: usize,
2506 column_count: usize,
2507 rows: &mut Vec<Row>,
2508) -> Result<(), Error> {
2509 let cell_ptr_array = hdr_off + 8; // leaf b-tree header is 8 bytes
2510 for i in 0..cell_count {
2511 let p = cell_ptr_array + i * 2;
2512 let cell_off = be_u16(slice, p) as usize;
2513 let row = decode_leaf_cell(src, slice, cell_off, column_count)?;
2514 rows.push(row);
2515 }
2516 Ok(())
2517}
2518
2519/// Decode one table-leaf cell at `off` into a [`Row`], reassembling the payload
2520/// from its overflow-page chain (resolved through the SAME [`PageSource`]) when
2521/// it spills past the leaf page.
2522fn decode_leaf_cell(
2523 src: &dyn PageSource,
2524 slice: &[u8],
2525 off: usize,
2526 column_count: usize,
2527) -> Result<Row, Error> {
2528 let (payload_len, n1) = read_varint(slice, off)?;
2529 let (rowid, n2) = read_varint(slice, off + n1)?;
2530 let payload_start = off + n1 + n2;
2531 let total = usize::try_from(payload_len).map_err(|_| Error::TruncatedCell)?;
2532
2533 let usable = src.usable();
2534 let local = local_payload_len(total, usable);
2535
2536 let payload = if local >= total {
2537 // Whole payload is on the leaf page (no spill).
2538 slice
2539 .get(payload_start..payload_start + total)
2540 .ok_or(Error::TruncatedCell)?
2541 .to_vec()
2542 } else {
2543 // Spilled: `local` bytes on the leaf, then a 4-byte overflow page
2544 // pointer, then the remainder follows the overflow chain.
2545 let head = slice
2546 .get(payload_start..payload_start + local)
2547 .ok_or(Error::TruncatedCell)?;
2548 let first_overflow = be_u32(slice, payload_start + local);
2549 // Cap the pre-allocation against the untrusted payload length: a payload
2550 // cannot exceed the bytes the file can physically supply — the `local`
2551 // bytes on the leaf plus the content bytes of every page reachable
2552 // through the overflow chain (`per_page * page_bound`). A crafted cell
2553 // that declares a multi-exabyte `payload_len` would otherwise reach
2554 // `Vec::with_capacity(total)` and abort the process with an allocation
2555 // bomb. This is the same over-range condition `read_overflow_chain`
2556 // rejects, pulled ahead of the allocation.
2557 let per_page = usable.saturating_sub(4);
2558 let max_overflow = per_page.saturating_mul(src.page_bound() as usize);
2559 let max_payload = local.saturating_add(max_overflow);
2560 if total > max_payload {
2561 return Err(Error::MalformedOverflow);
2562 }
2563 let mut buf = Vec::with_capacity(total);
2564 buf.extend_from_slice(head);
2565 read_overflow_chain(src, first_overflow, total - local, &mut buf)?;
2566 buf
2567 };
2568
2569 let values = decode_record(&payload, column_count, rowid, src.encoding())?;
2570 Ok(Row { rowid, values })
2571}
2572
2573/// Follow an overflow-page chain starting at `first` (1-based page number) over
2574/// a [`PageSource`], appending up to `remaining` payload bytes to `buf`. Each
2575/// overflow page is a 4-byte big-endian "next page" pointer (0 ends the chain)
2576/// followed by up to `usable - 4` content bytes.
2577///
2578/// Bounded against cyclic/over-long chains via [`Error::MalformedOverflow`].
2579fn read_overflow_chain(
2580 src: &dyn PageSource,
2581 first: u32,
2582 mut remaining: usize,
2583 buf: &mut Vec<u8>,
2584) -> Result<(), Error> {
2585 let usable = src.usable();
2586 let per_page = usable.saturating_sub(4);
2587 if per_page == 0 {
2588 return Err(Error::MalformedOverflow);
2589 }
2590 let total_pages = src.page_bound();
2591 let cap = total_pages as usize + 1;
2592
2593 let mut page = first;
2594 let mut visited = 0usize;
2595 while remaining > 0 {
2596 if page == 0 || page > total_pages {
2597 return Err(Error::MalformedOverflow);
2598 }
2599 visited += 1;
2600 if visited > cap {
2601 return Err(Error::MalformedOverflow);
2602 }
2603 let slice = src.page(page).ok_or(Error::PageOutOfRange(page))?;
2604 let slice = &*slice;
2605 let next = be_u32(slice, 0);
2606 let take = remaining.min(per_page);
2607 let chunk = slice.get(4..4 + take).ok_or(Error::TruncatedCell)?;
2608 buf.extend_from_slice(chunk);
2609 remaining -= take;
2610 page = next;
2611 }
2612 Ok(())
2613}
2614
2615/// Number of payload bytes stored locally on a table-leaf page for a record of
2616/// `total` bytes, given the page's `usable` size (file-format §1.6 overflow
2617/// rule). When the return value equals `total`, the record does not spill.
2618pub(crate) fn local_payload_len(total: usize, usable: usize) -> usize {
2619 let max_local = usable - 35; // X: largest payload kept entirely local
2620 if total <= max_local {
2621 return total;
2622 }
2623 let min_local = (usable - 12) * 32 / 255 - 23; // M
2624 let k = min_local + (total - min_local) % (usable - 4);
2625 if k <= max_local {
2626 k
2627 } else {
2628 min_local
2629 }
2630}
2631
2632impl WalOverlay {
2633 /// Parse a `-wal` sidecar into the newest committed page versions.
2634 ///
2635 /// Returns `Ok(None)` when `wal` is absent of a usable header / has no
2636 /// frames (a no-op overlay). Iterates frames in file order, accumulating the
2637 /// page data of each frame whose salt matches the WAL header; on reaching a
2638 /// COMMIT frame (`db_size_after_commit != 0`) the accumulated pages are
2639 /// promoted into the committed snapshot. Frames after the last commit are
2640 /// uncommitted and dropped. Bounds-checked and breadth-capped against a
2641 /// crafted WAL (a frame whose declared page data runs past the file ends the
2642 /// scan rather than panicking).
2643 fn parse(wal: &[u8], page_size: u32) -> Result<Option<Self>, Error> {
2644 use forensicnomicon::sqlite::{SQLITE_WAL_FRAME_HEADER_SIZE, SQLITE_WAL_HEADER_SIZE};
2645
2646 // No header → no overlay (treat a too-short WAL as empty, not an error:
2647 // a missing/zero-length sidecar is normal and must not fail the open).
2648 let Some(hdr) = wal.get(..SQLITE_WAL_HEADER_SIZE) else {
2649 return Ok(None);
2650 };
2651 let magic = be_u32(hdr, 0);
2652 if magic != WAL_MAGIC_BE && magic != WAL_MAGIC_LE {
2653 return Ok(None);
2654 }
2655 // The WAL records its own page size (offset 8); trust the DB header's
2656 // page size but require agreement to avoid mis-slicing frames.
2657 let wal_page_size = be_u32(hdr, 8);
2658 if wal_page_size != page_size {
2659 return Ok(None);
2660 }
2661 // WAL header layout (file-format §4.1): salt-1 at offset 16, salt-2 at
2662 // offset 20 (the two checksum words follow at 24 and 28).
2663 let salt1 = be_u32(hdr, 16);
2664 let salt2 = be_u32(hdr, 20);
2665
2666 let ps = page_size as usize;
2667 let frame_stride = SQLITE_WAL_FRAME_HEADER_SIZE + ps;
2668
2669 let mut committed: std::collections::BTreeMap<u32, Vec<u8>> =
2670 std::collections::BTreeMap::new();
2671 let mut pending: std::collections::BTreeMap<u32, Vec<u8>> =
2672 std::collections::BTreeMap::new();
2673 // Every committed frame's page image (file order), and the pending frames
2674 // not yet promoted by a COMMIT. Mirrors the page promotion above so
2675 // uncommitted trailing frames are dropped from BOTH the view and the carve.
2676 let mut frames: Vec<WalFramePage> = Vec::new();
2677 let mut pending_frames: Vec<WalFramePage> = Vec::new();
2678
2679 let mut off = SQLITE_WAL_HEADER_SIZE;
2680 // One frame per page in the file is the natural breadth cap; allow a
2681 // generous multiple for repeated rewrites, but keep it bounded.
2682 let max_frames = wal.len() / frame_stride + 1;
2683 let mut frame_no = 0usize;
2684
2685 while let Some(frame) = wal.get(off..off + frame_stride) {
2686 frame_no += 1;
2687 if frame_no > max_frames {
2688 break; // cov:unreachable: the slice walk already bounds frame_no
2689 }
2690 let page_no = be_u32(frame, 0);
2691 let db_size = be_u32(frame, 4);
2692 let fsalt1 = be_u32(frame, 8);
2693 let fsalt2 = be_u32(frame, 12);
2694 // A frame from a different checkpoint generation (salt mismatch) is
2695 // stale residue, not part of this WAL's live content — stop here.
2696 if fsalt1 != salt1 || fsalt2 != salt2 {
2697 break;
2698 }
2699 if page_no == 0 {
2700 break; // malformed frame; stop rather than mis-index
2701 }
2702 let data = frame
2703 .get(SQLITE_WAL_FRAME_HEADER_SIZE..)
2704 .ok_or(Error::TruncatedCell)?;
2705 pending.insert(page_no, data.to_vec());
2706 let is_commit = db_size != 0;
2707 pending_frames.push(WalFramePage {
2708 frame_index: frame_no - 1, // 0-based file order
2709 page_no,
2710 salt1,
2711 salt2,
2712 is_commit,
2713 page: data.to_vec(),
2714 });
2715
2716 if is_commit {
2717 // COMMIT frame: promote everything pending into the snapshot AND
2718 // into the committed frame list (keeping every frame, not just the
2719 // newest version of each page).
2720 for (p, d) in std::mem::take(&mut pending) {
2721 committed.insert(p, d);
2722 }
2723 frames.append(&mut pending_frames);
2724 }
2725 off += frame_stride;
2726 }
2727
2728 if committed.is_empty() {
2729 Ok(None)
2730 } else {
2731 Ok(Some(WalOverlay {
2732 pages: committed,
2733 frames,
2734 raw: wal.to_vec(),
2735 }))
2736 }
2737 }
2738}
2739
2740// ===========================================================================
2741// Bespoke, format-exact WAL temporal model (task #55)
2742// ===========================================================================
2743//
2744// A `-wal` sidecar is NOT an open-ended event log. It is a BOUNDED SEGMENT under a
2745// single salt epoch: every live frame shares the WAL header's (salt1, salt2). A
2746// checkpoint reset renumbers frames and rolls the salts — a DISCONTINUITY, not a
2747// continuation. The only materializable database states are the COMMIT snapshots:
2748// the replay of all valid frames up to a commit frame. A frame BETWEEN commits is
2749// not independently materializable, so it is never surfaced as a snapshot. Tails
2750// past the last commit, or after a salt reset, are WAL residue — forensic leads,
2751// never committed history.
2752//
2753// This model is self-contained in sqlite-core. The future state-history-forensic
2754// [H] adapter attaches at the seam exposed here (WalLsn + CohortTopology +
2755// `checksums_are_tamper_evident`), but sqlite-core does NOT depend on it.
2756
2757/// Cap on the number of salt segments and frames the timeline parser will walk on a
2758/// crafted `-wal`, bounding work against an attacker-supplied file. A real WAL holds
2759/// one segment with at most a few frames per database page.
2760const MAX_WAL_SEGMENTS: usize = 1024;
2761
2762/// Identity of one salt epoch within a `-wal` file: its 0-based segment ordinal.
2763/// A fresh segment begins at file start and after every checkpoint salt reset.
2764#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2765pub struct WalSegmentId(pub usize);
2766
2767/// One salt epoch of a `-wal` file — a single bounded segment.
2768///
2769/// A `-wal` is a bounded segment, not an open-ended log: every live frame here shares
2770/// `(salt1, salt2)`. A checkpoint reset (salt change + frame renumber) starts a NEW
2771/// `WalSegment`; it is a discontinuity, never another epoch of the same segment.
2772#[derive(Debug, Clone, PartialEq, Eq)]
2773pub struct WalSegment {
2774 /// This segment's ordinal within the WAL (0 = the segment at file start).
2775 pub id: WalSegmentId,
2776 /// WAL salt-1 (checkpoint generation), shared by every frame in the segment.
2777 pub salt1: u32,
2778 /// WAL salt-2 (checkpoint generation), shared by every frame in the segment.
2779 pub salt2: u32,
2780 /// Page size declared by the segment's frames (bytes).
2781 pub page_size: u32,
2782 /// Number of frames belonging to this segment.
2783 pub frame_count: usize,
2784 /// The checkpoint sequence number recorded in the WAL header (offset 12). For a
2785 /// segment discovered after a reset within the same file this is the header's
2786 /// value; per-segment sequence is otherwise not separately recorded.
2787 pub checkpoint_seq: u32,
2788}
2789
2790/// Address of a materializable database state: the replay of all valid frames up to
2791/// a COMMIT frame. `CommitId = (segment, commit_frame_index, db_size_after_commit)`.
2792#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2793pub struct CommitId {
2794 /// The salt segment this commit belongs to.
2795 pub segment: WalSegmentId,
2796 /// 0-based file-order index of the COMMIT frame within the segment.
2797 pub commit_frame_index: usize,
2798 /// `db_size_after_commit` recorded in the COMMIT frame header — the database's
2799 /// page count once this commit is materialized.
2800 pub db_size_after_commit: u32,
2801}
2802
2803/// The salt-qualified log-sequence identity of a WAL position — the seam the future
2804/// `state-history-forensic` `[H]` adapter maps onto `LsnKind::SqliteWal`.
2805///
2806/// A bare `frame_index` is meaningless across checkpoint resets (frames renumber), so
2807/// ordering is ALWAYS qualified by `(salt1, salt2)`. The adapter must reconstruct
2808/// `LsnKind::SqliteWal { salt1, salt2, frame_index }` from exactly this triple — never
2809/// from a bare index.
2810#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2811pub struct WalLsn {
2812 /// Salt-1 of the owning segment (checkpoint generation).
2813 pub salt1: u32,
2814 /// Salt-2 of the owning segment (checkpoint generation).
2815 pub salt2: u32,
2816 /// 0-based frame index within that segment.
2817 pub frame_index: usize,
2818}
2819
2820/// Topology of the temporal cohort the WAL exposes — the shape the `[H]` adapter maps
2821/// to `state-history-forensic::CohortTopology`.
2822#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2823pub enum CohortTopology {
2824 /// A single salt epoch: the commit snapshots form one linearly-ordered chain.
2825 LinearSegment,
2826 /// Multiple salt epochs (checkpoint resets) with no replay continuity between
2827 /// them — each segment is linear internally but the segments are disconnected.
2828 Disconnected,
2829}
2830
2831/// One page's image at a particular [`CommitSnapshot`].
2832#[derive(Debug, Clone, PartialEq, Eq)]
2833pub struct CommittedPageVersion {
2834 /// 1-based database page number.
2835 pub page_no: u32,
2836 /// The page's full image (`page_size` bytes) as of this commit.
2837 pub bytes: Vec<u8>,
2838}
2839
2840/// A materializable database state: the replay of all valid frames up to a COMMIT.
2841///
2842/// This is the ONLY independently-materializable WAL state. `page_version` resolves a
2843/// page to its image as of this commit (the newest frame ≤ this commit that rewrote
2844/// the page, else the acquired base image). A frame between commits is never a
2845/// snapshot.
2846#[derive(Debug, Clone, PartialEq, Eq)]
2847pub struct CommitSnapshot {
2848 id: CommitId,
2849 /// Salt-1 of the owning segment, carried so [`CommitSnapshot::lsn`] is
2850 /// self-contained without a back-reference to the segment.
2851 salt1: u32,
2852 /// Salt-2 of the owning segment.
2853 salt2: u32,
2854 /// The materialized page images at this commit: base image overlaid with every
2855 /// committed frame up to and including this commit (newest version per page),
2856 /// capped to `db_size_after_commit` pages. `page_version` reads from this map.
2857 overlaid: std::collections::BTreeMap<u32, Vec<u8>>,
2858 /// Whether the whole frame chain up to and including this commit's COMMIT frame
2859 /// passed the WAL cumulative checksum (file-format §4.2). `false` marks a commit
2860 /// the salt+commit-marker admission would otherwise accept but whose checksum
2861 /// chain is broken (post-reset residue, tampering, or corruption) — kept, not
2862 /// dropped, so the forensic layer can label it.
2863 checksum_valid: bool,
2864 /// Usable bytes per page (`page_size` − reserved), parsed from the snapshot's
2865 /// OWN page-1 header, so a snapshot-scoped read uses the reserved-space value
2866 /// as of this commit rather than the live database's.
2867 usable: u32,
2868}
2869
2870/// One user table as of a [`CommitSnapshot`] — its schema parsed from the
2871/// snapshot's OWN materialized page 1, NOT from the live database. A rootpage can
2872/// be dropped and reused by a different table across commits, so reading the
2873/// schema from the snapshot is the only correct way to interpret its b-trees.
2874#[derive(Debug, Clone, PartialEq, Eq)]
2875pub struct SnapshotTable {
2876 /// The table's `sqlite_master.name`.
2877 pub name: String,
2878 /// 1-based root page of the table's b-tree as of this commit.
2879 pub rootpage: u32,
2880 /// Parsed column names from the table's `CREATE TABLE`, in declared order.
2881 /// Empty when the schema SQL could not be parsed with confidence.
2882 pub columns: Vec<String>,
2883 /// Whether this is a `WITHOUT ROWID` table (file-format §2.4). Such a table
2884 /// uses an INDEX b-tree with no rowid key, so the rowid-based snapshot read
2885 /// does not apply — flagged so a caller never mis-reads it as a rowid table.
2886 pub without_rowid: bool,
2887}
2888
2889/// Whether a `CREATE TABLE` statement declares a `WITHOUT ROWID` table
2890/// (file-format §2.4). Detection keys off the trailing `WITHOUT ROWID` clause,
2891/// case-insensitively and tolerant of internal whitespace, while ignoring any
2892/// occurrence inside a quoted identifier/string so a column literally named
2893/// "without rowid" is not a false positive.
2894/// A `CREATE TABLE` statement with quoted spans removed and whitespace collapsed,
2895/// uppercased — so a clause search sees only unquoted SQL tokens. Strips
2896/// `'...'` / `"..."` / `` `...` `` / `[...]` spans (the four `SQLite` identifier /
2897/// string quotings) exactly as the clause detectors require, so the keyword
2898/// appearing inside a quoted identifier or string literal can never false-match.
2899fn normalized_unquoted_sql(create_sql: &str) -> String {
2900 let bytes = create_sql.as_bytes();
2901 let mut unquoted = String::with_capacity(create_sql.len());
2902 let mut quote: Option<u8> = None;
2903 for &c in bytes {
2904 match quote {
2905 Some(q) => {
2906 if c == q {
2907 quote = None;
2908 }
2909 }
2910 None => match c {
2911 b'\'' | b'"' | b'`' => quote = Some(c),
2912 b'[' => quote = Some(b']'),
2913 _ => unquoted.push(c as char),
2914 },
2915 }
2916 }
2917 unquoted
2918 .split_whitespace()
2919 .collect::<Vec<_>>()
2920 .join(" ")
2921 .to_ascii_uppercase()
2922}
2923
2924fn without_rowid_sql(create_sql: &str) -> bool {
2925 // Look for the clause as a discrete token sequence, ignoring quoted spans and
2926 // case/whitespace (file-format §2.4).
2927 normalized_unquoted_sql(create_sql).contains("WITHOUT ROWID")
2928}
2929
2930/// Whether `create_sql` declares an ordinary rowid table with an
2931/// `INTEGER PRIMARY KEY AUTOINCREMENT` column — the only form for which `SQLite`
2932/// maintains a monotonic `sqlite_sequence` high-water mark.
2933///
2934/// Per the file format, `AUTOINCREMENT` is valid **only** immediately after
2935/// `INTEGER PRIMARY KEY`, and **never** on a `WITHOUT ROWID` table (which has no
2936/// rowid to auto-increment). So this is true iff the normalized, unquoted CREATE
2937/// text contains the exact token run `INTEGER PRIMARY KEY AUTOINCREMENT` and does
2938/// NOT carry the `WITHOUT ROWID` clause. Quoted identifiers / string literals /
2939/// comments are stripped first (mirroring `without_rowid_sql`), so a column
2940/// merely named `"autoincrement"`, or the keyword inside a string, never matches.
2941///
2942/// This is a HINT input only: a true result means the table has an AUTOINCREMENT
2943/// high-water mark the forensic layer can reconcile against, not that any
2944/// particular row predates the current instance.
2945#[must_use]
2946pub fn is_autoincrement(create_sql: &str) -> bool {
2947 let normalized = normalized_unquoted_sql(create_sql);
2948 normalized.contains("INTEGER PRIMARY KEY AUTOINCREMENT")
2949 && !normalized.contains("WITHOUT ROWID")
2950}
2951
2952impl CommitSnapshot {
2953 /// This snapshot's [`CommitId`].
2954 #[must_use]
2955 pub fn id(&self) -> CommitId {
2956 self.id
2957 }
2958
2959 /// The database page count once this commit is materialized.
2960 #[must_use]
2961 pub fn db_size_after_commit(&self) -> u32 {
2962 self.id.db_size_after_commit
2963 }
2964
2965 /// Whether the WAL frame chain up to and including this commit's COMMIT frame
2966 /// validated against the cumulative WAL checksum (file-format §4.2).
2967 ///
2968 /// `true` is the spec-conformant case: every frame's stored `(checksum1,
2969 /// checksum2)` equalled the running checksum advanced over the frame's first
2970 /// 8 header bytes plus its full page data, seeded from the WAL header
2971 /// checksum. `false` means the chain broke at or before this commit — the
2972 /// salt + commit-marker admission accepted it, but it is residue (post-reset
2973 /// leftover, tampering, or corruption). Such a commit is deliberately KEPT
2974 /// (not dropped) so the forensic layer can mark it; a consumer that wants only
2975 /// trustworthy state filters on this flag.
2976 #[must_use]
2977 pub fn checksum_valid(&self) -> bool {
2978 self.checksum_valid
2979 }
2980
2981 /// The salt-qualified [`WalLsn`] of this commit (the `[H]` adapter seam).
2982 #[must_use]
2983 pub fn lsn(&self) -> WalLsn {
2984 WalLsn {
2985 salt1: self.salt1,
2986 salt2: self.salt2,
2987 frame_index: self.id.commit_frame_index,
2988 }
2989 }
2990
2991 /// The 1-based page numbers this commit materialized (base ∪ committed frames
2992 /// up to this commit, capped to `db_size_after_commit`), ascending.
2993 ///
2994 /// The carve-at-snapshot primitive iterates these to drive the carving
2995 /// primitives over each page image, WITHOUT assuming the pages form a
2996 /// contiguous `1..=db_size` range (a truncating commit or a sparse base image
2997 /// can leave gaps). Every returned page resolves via [`Self::page_version`].
2998 #[must_use]
2999 pub fn page_numbers(&self) -> Vec<u32> {
3000 self.overlaid.keys().copied().collect()
3001 }
3002
3003 /// The image of `page_no` as of this commit, or `None` for a page beyond the
3004 /// committed database size that the WAL never rewrote.
3005 #[must_use]
3006 pub fn page_version(&self, page_no: u32) -> Option<CommittedPageVersion> {
3007 let bytes = self.overlaid.get(&page_no)?.clone();
3008 Some(CommittedPageVersion { page_no, bytes })
3009 }
3010
3011 /// The user tables AS OF this commit, parsed from the snapshot's OWN page 1
3012 /// (the `sqlite_master` b-tree), NOT from the live database.
3013 ///
3014 /// A rootpage can be dropped and reused by a different table across commits,
3015 /// so the schema MUST come from the snapshot itself — reading today's live
3016 /// schema would mis-attribute a historical b-tree. Returns one
3017 /// [`SnapshotTable`] per `type='table'` row whose name is not an internal
3018 /// `sqlite_*` table, carrying its rootpage, parsed column names, and a
3019 /// `WITHOUT ROWID` flag (file-format §2.4). Best-effort and panic-free: an
3020 /// unreadable page-1 schema yields an empty vector.
3021 #[must_use]
3022 pub fn tables(&self) -> Vec<SnapshotTable> {
3023 // sqlite_master is a 5-column table rooted at page 1:
3024 // (type, name, tbl_name, rootpage, sql). Walk it through THIS snapshot's
3025 // pages via the shared b-tree reader.
3026 let Ok(schema) = read_table_via(self, 1, 5) else {
3027 return Vec::new(); // cov:unreachable: a committed snapshot has a readable page 1
3028 };
3029 let mut out = Vec::new();
3030 for row in schema {
3031 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
3032 if !is_table {
3033 continue;
3034 }
3035 let Some(Value::Text(name)) = row.values.get(1) else {
3036 continue; // cov:unreachable: a 'table' schema row has a TEXT name
3037 };
3038 if name.starts_with("sqlite_") {
3039 continue;
3040 }
3041 let Some(Value::Integer(root)) = row.values.get(3) else {
3042 continue; // cov:unreachable: a 'table' schema row has an integer rootpage
3043 };
3044 let Ok(rootpage) = u32::try_from(*root) else {
3045 continue; // cov:unreachable: a real rootpage is a small positive page number
3046 };
3047 let sql = match row.values.get(4) {
3048 Some(Value::Text(s)) => s.as_str(),
3049 _ => "", // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
3050 };
3051 let columns = attribution::column_names(sql).unwrap_or_default();
3052 out.push(SnapshotTable {
3053 name: name.clone(),
3054 rootpage,
3055 columns,
3056 without_rowid: without_rowid_sql(sql),
3057 });
3058 }
3059 out
3060 }
3061
3062 /// Read every row of the table b-tree rooted at `rootpage` AS OF this commit,
3063 /// resolving overflow chains through the snapshot's OWN materialized pages, in
3064 /// rowid order.
3065 ///
3066 /// This is the snapshot-scoped counterpart to [`Database::read_table`]: it
3067 /// shares the SAME b-tree/overflow walk via an internal page-source
3068 /// abstraction, so a large row
3069 /// decodes with the page content as of this commit (not stale/future content
3070 /// the live view would supply). `column_count` drives only the
3071 /// `INTEGER PRIMARY KEY` rowid-alias rule (pass the table's declared arity,
3072 /// e.g. `SnapshotTable::columns.len()`). Returns `(rowid, values)` per row.
3073 ///
3074 /// Bounded and panic-free on hostile input, exactly as the live path: a
3075 /// cyclic/over-deep b-tree or overflow chain surfaces a typed [`Error`] rather
3076 /// than looping or panicking.
3077 pub fn read_table(
3078 &self,
3079 rootpage: u32,
3080 column_count: usize,
3081 ) -> Result<Vec<(i64, Vec<Value>)>, Error> {
3082 let rows = read_table_via(self, rootpage, column_count)?;
3083 Ok(rows.into_iter().map(|r| (r.rowid, r.values)).collect())
3084 }
3085}
3086
3087/// A page-level delta between two materialized states.
3088#[derive(Debug, Clone, PartialEq, Eq)]
3089pub struct WalDiff {
3090 changed: Vec<u32>,
3091}
3092
3093impl WalDiff {
3094 /// The 1-based page numbers whose bytes differ between the two states, ascending.
3095 #[must_use]
3096 pub fn changed_pages(&self) -> &[u32] {
3097 &self.changed
3098 }
3099}
3100
3101/// A stale WAL tail surfaced for forensics — NOT committed history.
3102///
3103/// Frames past the last COMMIT of a segment, frames after a salt reset that cannot be
3104/// replayed into the current segment, or a header/page-size break: all are residue.
3105/// The examiner weighs them; they are never part of a consistent snapshot.
3106#[derive(Debug, Clone, PartialEq, Eq)]
3107pub struct WalResidue {
3108 /// The segment the residue trails (the segment whose last commit it follows).
3109 pub segment: WalSegmentId,
3110 /// 0-based frame index (within the file) of the first residual frame.
3111 pub first_frame_index: usize,
3112 /// Number of residual frames.
3113 pub frame_count: usize,
3114 /// Why these frames are residue rather than committed history.
3115 pub reason: ResidueReason,
3116}
3117
3118/// Why a WAL tail is [`WalResidue`] (an invalidated-frame candidate), not history.
3119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3120pub enum ResidueReason {
3121 /// Frames written after the segment's last COMMIT (uncommitted tail).
3122 BeyondLastCommit,
3123 /// Frames whose salt no longer matches the segment header (post-reset residue).
3124 SaltReset,
3125}
3126
3127/// Validation tier a WAL has cleared — strictly increasing assurance.
3128///
3129/// `PhysicalValidation` < `CommitValidation` < `ReplaySafe`. The timeline reports the
3130/// highest tier reached; a page-size mismatch never even produces a timeline (it is a
3131/// hard stop at parse, surfaced as [`WalValidationError::PageSizeMismatch`]).
3132#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
3133pub enum MaterializationSafety {
3134 /// Header magic / format / page-size / salts / frame boundaries are well-formed,
3135 /// but no committed snapshot was found (nothing to replay).
3136 PhysicalValidated,
3137 /// A last valid commit and committed frame ranges were established, but the
3138 /// read-only replay overlay was not (or could not be) built.
3139 CommitValidated,
3140 /// A read-only replay overlay to the last commit is available — safe to
3141 /// materialize without mutating either file.
3142 ReplaySafe,
3143}
3144
3145/// A WAL that cannot be admitted to the timeline at all (physical-validation hard
3146/// stops). Distinct from "no committed snapshot", which is a valid empty timeline.
3147#[derive(Debug, Clone, PartialEq, Eq)]
3148pub enum WalValidationError {
3149 /// The `-wal` is shorter than its 32-byte header, or carries the wrong magic.
3150 BadMagic,
3151 /// The WAL header's page size disagrees with the DB header's — a HARD STOP, since
3152 /// every frame would be mis-sliced. `db` and `wal` are the two declared sizes.
3153 PageSizeMismatch { db: u32, wal: u32 },
3154 /// The main database header itself failed to parse.
3155 Header(Error),
3156}
3157
3158/// The bespoke, format-exact temporal model of a `-wal` sidecar.
3159///
3160/// Enumerates the salt segments, the materializable [`CommitSnapshot`]s within them
3161/// (CommitId-addressable), and the [`WalResidue`] tails. Materialize a snapshot's page
3162/// images via [`CommitSnapshot::page_version`]; diff the acquired base against the last
3163/// valid commit via [`WalTimeline::diff_base_to_last_commit`].
3164#[derive(Debug, Clone, PartialEq, Eq)]
3165pub struct WalTimeline {
3166 page_size: u32,
3167 base_pages: std::collections::BTreeMap<u32, Vec<u8>>,
3168 segments: Vec<WalSegment>,
3169 snapshots: Vec<CommitSnapshot>,
3170 residue: Vec<WalResidue>,
3171 safety: MaterializationSafety,
3172}
3173
3174impl WalTimeline {
3175 /// Physical-validation tier: header magic + format check.
3176 ///
3177 /// Parses `bytes` (the acquired main DB) and `wal` (the `-wal` sidecar) into the
3178 /// segmented temporal model. A page-size mismatch between the DB header and the
3179 /// WAL header is a HARD STOP; a bad/short header is [`WalValidationError::BadMagic`].
3180 fn parse(bytes: &[u8], wal: &[u8], page_size: u32) -> Result<Self, WalValidationError> {
3181 use forensicnomicon::sqlite::{SQLITE_WAL_FRAME_HEADER_SIZE, SQLITE_WAL_HEADER_SIZE};
3182
3183 // --- PhysicalValidation: header magic / format / page-size / salts -------
3184 let hdr = wal
3185 .get(..SQLITE_WAL_HEADER_SIZE)
3186 .ok_or(WalValidationError::BadMagic)?;
3187 let magic = be_u32(hdr, 0);
3188 if magic != WAL_MAGIC_BE && magic != WAL_MAGIC_LE {
3189 return Err(WalValidationError::BadMagic);
3190 }
3191 let wal_page_size = be_u32(hdr, 8);
3192 if wal_page_size != page_size {
3193 return Err(WalValidationError::PageSizeMismatch {
3194 db: page_size,
3195 wal: wal_page_size,
3196 });
3197 }
3198 let checkpoint_seq = be_u32(hdr, 12);
3199 let mut salt1 = be_u32(hdr, 16);
3200 let mut salt2 = be_u32(hdr, 20);
3201
3202 // Checksum chain seed (file-format §4.2): the running (s0, s1) starts from
3203 // the WAL header's stored checksum (bytes 24..32, always big-endian),
3204 // which is itself the checksum over the first 24 header bytes. The word
3205 // endianness for advancing over frames comes from the magic. `from_magic`
3206 // cannot return None here — the magic was admitted above.
3207 let endian = WalChecksumEndian::from_magic(magic).unwrap_or(WalChecksumEndian::Big);
3208 let header_s0 = be_u32(hdr, 24);
3209 let header_s1 = be_u32(hdr, 28);
3210 // Per-segment running checksum state and whether the chain is still valid.
3211 let mut run_s0 = header_s0;
3212 let mut run_s1 = header_s1;
3213 let mut chain_valid = true;
3214
3215 let ps = page_size as usize;
3216 let frame_stride = SQLITE_WAL_FRAME_HEADER_SIZE + ps;
3217
3218 // The acquired main DB image: the pre-WAL base for replay within the current
3219 // validated segment (NOT "epoch 0" — just the base each commit overlays onto).
3220 let mut base_pages: std::collections::BTreeMap<u32, Vec<u8>> =
3221 std::collections::BTreeMap::new();
3222 // `chunks_exact` yields only whole pages (infallible by construction — no
3223 // out-of-bounds slice to guard); cap at `u32::MAX` pages so the 1-based page
3224 // number never overflows on a pathologically large image.
3225 for (idx, page) in bytes
3226 .chunks_exact(ps)
3227 .take(u32::MAX as usize - 1)
3228 .enumerate()
3229 {
3230 let pno = idx as u32 + 1; // 1-based page number
3231 base_pages.insert(pno, page.to_vec());
3232 }
3233
3234 let mut segments: Vec<WalSegment> = Vec::new();
3235 let mut snapshots: Vec<CommitSnapshot> = Vec::new();
3236 let mut residue: Vec<WalResidue> = Vec::new();
3237
3238 // Per-segment running state.
3239 let mut seg_ordinal = 0usize;
3240 let mut seg_frame_count = 0usize;
3241 // Cumulative newest-page map across all COMMITTED frames of the segment, so a
3242 // snapshot's `overlaid` is base ∪ committed-up-to-this-commit.
3243 let mut committed_pages: std::collections::BTreeMap<u32, Vec<u8>> = base_pages.clone();
3244 let mut pending: std::collections::BTreeMap<u32, Vec<u8>> =
3245 std::collections::BTreeMap::new();
3246 let mut last_commit_global_frame: Option<usize> = None;
3247 let mut uncommitted_tail_start: Option<usize> = None;
3248
3249 let mut off = SQLITE_WAL_HEADER_SIZE;
3250 let max_frames = wal.len() / frame_stride + 1;
3251 let mut frame_no = 0usize;
3252
3253 while let Some(frame) = wal.get(off..off + frame_stride) {
3254 if frame_no >= max_frames {
3255 break; // cov:unreachable: the slice walk already bounds frame_no
3256 }
3257 let page_no = be_u32(frame, 0);
3258 let db_size = be_u32(frame, 4);
3259 let fsalt1 = be_u32(frame, 8);
3260 let fsalt2 = be_u32(frame, 12);
3261
3262 // A salt change opens a NEW segment (checkpoint reset = discontinuity).
3263 // Anything between the prior segment's last commit and here is residue.
3264 if fsalt1 != salt1 || fsalt2 != salt2 {
3265 if segments.len() >= MAX_WAL_SEGMENTS {
3266 break; // cov:unreachable: real WALs hold far fewer than 1024 salt epochs
3267 }
3268 // Close the current segment, recording its residue tail (if any).
3269 Self::close_segment(
3270 &mut segments,
3271 &mut residue,
3272 WalSegmentId(seg_ordinal),
3273 salt1,
3274 salt2,
3275 page_size,
3276 checkpoint_seq,
3277 seg_frame_count,
3278 uncommitted_tail_start,
3279 );
3280 // Begin the next segment under the new salts. Its base for replay is
3281 // the prior committed view (a checkpoint would have flushed it, but on
3282 // a forensic image we keep what we can replay).
3283 seg_ordinal += 1;
3284 salt1 = fsalt1;
3285 salt2 = fsalt2;
3286 seg_frame_count = 0;
3287 pending.clear();
3288 uncommitted_tail_start = None;
3289 // The post-reset frames replay onto the latest committed view.
3290 // committed_pages carries forward.
3291 // The checksum chain for a post-reset segment threads from a WAL
3292 // header we do NOT hold (the new generation's own 32-byte header
3293 // was overwritten), so its frames cannot be validated against our
3294 // seed. Mark the chain broken for this segment: its commits are
3295 // checksum-residue, surfaced for forensics but not trusted.
3296 chain_valid = false;
3297 }
3298
3299 if page_no == 0 {
3300 break; // malformed frame; stop rather than mis-index
3301 }
3302 let data = match frame.get(SQLITE_WAL_FRAME_HEADER_SIZE..) {
3303 Some(d) => d.to_vec(),
3304 None => break, // cov:unreachable: frame slice is exactly frame_stride
3305 };
3306
3307 // Advance the cumulative checksum over this frame (file-format §4.2):
3308 // the first 8 bytes of the frame header (page-no ++ db-size) followed
3309 // by the full page data — NOT the salt/checksum bytes (frame[8..24]).
3310 // Then compare against the frame's stored checksum (frame[16..24], big-
3311 // endian). A mismatch breaks the chain for the rest of the segment.
3312 // Only advance while the chain is still intact (a post-reset segment is
3313 // pre-marked broken and is not re-seedable from our header).
3314 if chain_valid {
3315 let (n0, n1) = wal_checksum(endian, run_s0, run_s1, &frame[0..8]);
3316 let (n0, n1) = wal_checksum(endian, n0, n1, &data);
3317 run_s0 = n0;
3318 run_s1 = n1;
3319 let stored0 = be_u32(frame, 16);
3320 let stored1 = be_u32(frame, 20);
3321 if stored0 != run_s0 || stored1 != run_s1 {
3322 chain_valid = false;
3323 }
3324 }
3325
3326 let frame_index_in_seg = seg_frame_count;
3327 seg_frame_count += 1;
3328 pending.insert(page_no, data);
3329 let is_commit = db_size != 0;
3330
3331 if is_commit {
3332 for (p, d) in std::mem::take(&mut pending) {
3333 committed_pages.insert(p, d);
3334 }
3335 // Drop base/committed pages beyond the committed size so a snapshot
3336 // reflects the database's page count at that commit. `db_size` is
3337 // non-zero here (that is what makes this a COMMIT frame).
3338 committed_pages.retain(|&p, _| p <= db_size);
3339 let id = CommitId {
3340 segment: WalSegmentId(seg_ordinal),
3341 commit_frame_index: frame_index_in_seg,
3342 db_size_after_commit: db_size,
3343 };
3344 let overlaid = committed_pages.clone();
3345 // Usable bytes per page from the snapshot's OWN page-1 header
3346 // (reserved-space byte at offset 20), so a snapshot-scoped read
3347 // honors the reserved value as of this commit. Page 1 is always
3348 // materialized; a missing/short page-1 image degrades to 0 reserved.
3349 let reserved = overlaid
3350 .get(&1)
3351 .and_then(|p| p.get(RESERVED_SPACE_OFFSET).copied())
3352 .unwrap_or(0);
3353 let usable = page_size.saturating_sub(u32::from(reserved));
3354 snapshots.push(CommitSnapshot {
3355 id,
3356 overlaid,
3357 salt1,
3358 salt2,
3359 checksum_valid: chain_valid,
3360 usable,
3361 });
3362 last_commit_global_frame = Some(frame_no);
3363 uncommitted_tail_start = None;
3364 } else if uncommitted_tail_start.is_none() {
3365 uncommitted_tail_start = Some(frame_index_in_seg);
3366 }
3367
3368 frame_no += 1;
3369 off += frame_stride;
3370 }
3371
3372 // Close the final segment (it may have an uncommitted tail).
3373 Self::close_segment(
3374 &mut segments,
3375 &mut residue,
3376 WalSegmentId(seg_ordinal),
3377 salt1,
3378 salt2,
3379 page_size,
3380 checkpoint_seq,
3381 seg_frame_count,
3382 uncommitted_tail_start,
3383 );
3384
3385 let safety = if snapshots.is_empty() {
3386 MaterializationSafety::PhysicalValidated
3387 } else if last_commit_global_frame.is_some() {
3388 MaterializationSafety::ReplaySafe
3389 } else {
3390 MaterializationSafety::CommitValidated // cov:unreachable: a snapshot implies a commit
3391 };
3392
3393 Ok(Self {
3394 page_size,
3395 base_pages,
3396 segments,
3397 snapshots,
3398 residue,
3399 safety,
3400 })
3401 }
3402
3403 #[allow(clippy::too_many_arguments)]
3404 fn close_segment(
3405 segments: &mut Vec<WalSegment>,
3406 residue: &mut Vec<WalResidue>,
3407 id: WalSegmentId,
3408 salt1: u32,
3409 salt2: u32,
3410 page_size: u32,
3411 checkpoint_seq: u32,
3412 frame_count: usize,
3413 uncommitted_tail_start: Option<usize>,
3414 ) {
3415 if frame_count == 0 {
3416 return;
3417 }
3418 segments.push(WalSegment {
3419 id,
3420 salt1,
3421 salt2,
3422 page_size,
3423 frame_count,
3424 checkpoint_seq,
3425 });
3426 if let Some(start) = uncommitted_tail_start {
3427 residue.push(WalResidue {
3428 segment: id,
3429 first_frame_index: start,
3430 frame_count: frame_count - start,
3431 reason: ResidueReason::BeyondLastCommit,
3432 });
3433 }
3434 }
3435
3436 /// The salt segments of this WAL, in file order (one per salt epoch).
3437 #[must_use]
3438 pub fn segments(&self) -> &[WalSegment] {
3439 &self.segments
3440 }
3441
3442 /// Every materializable [`CommitSnapshot`] across all segments, in commit order.
3443 #[must_use]
3444 pub fn commit_snapshots(&self) -> &[CommitSnapshot] {
3445 &self.snapshots
3446 }
3447
3448 /// The stale WAL tails surfaced for forensics (not committed history).
3449 #[must_use]
3450 pub fn residue(&self) -> &[WalResidue] {
3451 &self.residue
3452 }
3453
3454 /// Resolve a [`CommitId`] back to its [`CommitSnapshot`].
3455 #[must_use]
3456 pub fn snapshot_at(&self, id: CommitId) -> Option<&CommitSnapshot> {
3457 self.snapshots.iter().find(|s| s.id == id)
3458 }
3459
3460 /// The highest validation tier this WAL cleared (see [`MaterializationSafety`]).
3461 #[must_use]
3462 pub fn safety(&self) -> MaterializationSafety {
3463 self.safety
3464 }
3465
3466 /// The temporal-cohort topology — `LinearSegment` for one salt epoch, else
3467 /// `Disconnected` across checkpoint resets. The `[H]` adapter maps this onto
3468 /// `state-history-forensic::CohortTopology`.
3469 #[must_use]
3470 pub fn topology(&self) -> CohortTopology {
3471 if self.segments.len() <= 1 {
3472 CohortTopology::LinearSegment
3473 } else {
3474 CohortTopology::Disconnected
3475 }
3476 }
3477
3478 /// Whether the WAL's integrity checks are tamper-EVIDENT. Always `false`: WAL
3479 /// frame checksums are non-cryptographic (corruption detection, not tamper proof),
3480 /// so the `[H]` adapter must record `tamper_resistance = LOW`.
3481 #[must_use]
3482 pub fn checksums_are_tamper_evident(&self) -> bool {
3483 false
3484 }
3485
3486 /// Diff the acquired base image against the last valid commit snapshot, returning
3487 /// the page numbers whose bytes changed. `None` when there is no committed snapshot.
3488 #[must_use]
3489 pub fn diff_base_to_last_commit(&self) -> Option<WalDiff> {
3490 let last = self.snapshots.last()?;
3491 let mut changed = Vec::new();
3492 let mut pages: std::collections::BTreeSet<u32> = std::collections::BTreeSet::new();
3493 pages.extend(self.base_pages.keys().copied());
3494 pages.extend(last.overlaid.keys().copied());
3495 for p in pages {
3496 let base = self.base_pages.get(&p);
3497 let now = last.overlaid.get(&p);
3498 if base != now {
3499 changed.push(p);
3500 }
3501 }
3502 Some(WalDiff { changed })
3503 }
3504
3505 /// The page size (bytes) common to the base image and the WAL frames.
3506 #[must_use]
3507 pub fn page_size(&self) -> u32 {
3508 self.page_size
3509 }
3510
3511 /// Map this WAL timeline onto the canonical `forensicnomicon::history` cohort
3512 /// vocabulary — the `[H]` adapter (#43 / WS-F).
3513 ///
3514 /// Each materializable [`CommitSnapshot`] becomes one `TemporalState<CommitId>`:
3515 /// - **ordering key** — a salt-qualified `LsnKind::SqliteWalFrame` (`frame_seq` is the
3516 /// COMMIT frame index; `commit_seq` is the 0-based commit ordinal within the salt
3517 /// segment). The `(salt1, salt2)` pair keeps the key meaningful across a checkpoint
3518 /// reset, which renumbers frames and rolls the salts.
3519 /// - **clock + safety** — the canonical SQLite-WAL profile, single-sourced from
3520 /// [`forensicnomicon::history::profiles`], so no consumer re-asserts the four
3521 /// classifications locally.
3522 /// - **handle** — the snapshot's [`CommitId`]; resolve it back via [`Self::snapshot_at`].
3523 ///
3524 /// The topology is uniformly `SubJournalCommits`: every state is a committed
3525 /// transaction, and a checkpoint reset is visible as a salt change *inside* the
3526 /// ordering key — there is no separate "disconnected" topology to special-case. The
3527 /// cohort is `PathStable` (a `-wal` belongs to exactly one database path), so the
3528 /// caller supplies the path identity via `artifact`.
3529 #[must_use]
3530 pub fn to_temporal_cohort(
3531 &self,
3532 artifact: forensicnomicon::history::identity::ArtifactRef,
3533 ) -> forensicnomicon::history::cohort::TemporalCohort<CommitId> {
3534 use forensicnomicon::history::cohort::{TemporalCohort, TemporalState};
3535 use forensicnomicon::history::epoch::{CohortTopology, EpochTag, LsnKind};
3536 use forensicnomicon::history::identity::IdentityDiscipline;
3537 use forensicnomicon::history::profiles;
3538
3539 // One canonical profile drives every state's clock + safety — read from
3540 // forensicnomicon, never re-asserted here, so the fleet cannot drift.
3541 let profile = profiles::SourceTemporalProfile::sqlite_wal();
3542 let mut commit_seq_in_segment: std::collections::HashMap<WalSegmentId, u32> =
3543 std::collections::HashMap::new();
3544
3545 let states = self
3546 .snapshots
3547 .iter()
3548 .map(|snap| {
3549 let id = snap.id();
3550 let lsn = snap.lsn();
3551 let seq = commit_seq_in_segment.entry(id.segment).or_insert(0);
3552 let commit_seq = *seq;
3553 *seq += 1;
3554
3555 // Deterministic and collision-free within a cohort: the
3556 // (salt1, salt2, commit_frame_index, db_size_after_commit) quadruple is
3557 // unique per commit state. Packed big-endian into the leading 16 bytes.
3558 let mut tag = [0u8; 32];
3559 tag[0..4].copy_from_slice(&lsn.salt1.to_be_bytes());
3560 tag[4..8].copy_from_slice(&lsn.salt2.to_be_bytes());
3561 tag[8..12].copy_from_slice(&(id.commit_frame_index as u32).to_be_bytes());
3562 tag[12..16].copy_from_slice(&id.db_size_after_commit.to_be_bytes());
3563
3564 TemporalState {
3565 epoch: EpochTag::from_bytes(tag),
3566 ordering_key: Some(LsnKind::SqliteWalFrame {
3567 salt1: lsn.salt1,
3568 salt2: lsn.salt2,
3569 frame_seq: lsn.frame_index as u32,
3570 commit_seq,
3571 }),
3572 wall_time: None,
3573 clock: profile.clock.clone(),
3574 safety: profile.safety.clone(),
3575 handle: id,
3576 }
3577 })
3578 .collect();
3579
3580 TemporalCohort {
3581 artifact,
3582 discipline: IdentityDiscipline::PathStable,
3583 topology: CohortTopology::SubJournalCommits,
3584 states,
3585 }
3586 }
3587}
3588
3589/// Whether a decoded [`Value`] is **distinctive** enough to anchor a Tier-2
3590/// fragment emission (the §3.1 gate): TEXT of ≥ 4 bytes of valid UTF-8 (no
3591/// replacement char), or a REAL. Bare integers (1–8-byte serial patterns),
3592/// NULL, and BLOBs are NOT distinctive alone — a short integer byte-pattern
3593/// coincides far too often in a 4 `KiB` page to serve as identity, so it can ride
3594/// along inside a fragment but never justify emitting one.
3595fn is_distinctive(value: &Value) -> bool {
3596 match value {
3597 Value::Text(t) => t.len() >= 4 && !t.contains('\u{FFFD}'),
3598 Value::Real(_) => true,
3599 Value::Null | Value::Integer(_) | Value::Blob(_) => false,
3600 }
3601}
3602
3603/// The body byte-width of a serial type (file-format §2.1), or `None` for a
3604/// serial value that cannot legally appear in a record body.
3605fn serial_body_len(serial: i64) -> Option<usize> {
3606 match serial {
3607 0 | 8 | 9 | 10 | 11 => Some(0),
3608 1 => Some(1),
3609 2 => Some(2),
3610 3 => Some(3),
3611 4 => Some(4),
3612 5 => Some(6),
3613 6 | 7 => Some(8),
3614 n if n >= 12 => Some(((n - 12) / 2) as usize),
3615 _ => None, // negative serial: impossible
3616 }
3617}
3618
3619/// Byte length of a **live** table-leaf cell at `off`, for computing the byte
3620/// extent the cell occupies (so [`Database::carve_free_regions`] can exclude it).
3621/// Returns `None` if the cell header does not parse in bounds.
3622///
3623/// Mirrors the live cell layout: payload-length varint, rowid varint, then the
3624/// local payload (capped at the spill threshold) plus a 4-byte overflow pointer
3625/// when the payload spills. We only need the on-page footprint, so for a spilled
3626/// cell that is `local + 4` bytes, not the full reassembled payload.
3627fn live_cell_len(buf: &[u8], off: usize, usable: usize) -> Option<usize> {
3628 let (payload_len, n1) = read_varint(buf, off).ok()?;
3629 let (_rowid, n2) = read_varint(buf, off + n1).ok()?;
3630 let total = usize::try_from(payload_len).ok()?;
3631 let local = local_payload_len(total, usable);
3632 let on_page = if local >= total {
3633 n1 + n2 + total
3634 } else {
3635 n1 + n2 + local + 4 // 4-byte first-overflow-page pointer
3636 };
3637 Some(on_page)
3638}
3639
3640/// The rowid of a table-leaf cell at `off` — its 2nd varint (after the
3641/// payload-length varint). `None` if either varint is out of bounds. Used to
3642/// identify a live row even when its full record cannot be decoded.
3643fn live_cell_rowid(buf: &[u8], off: usize) -> Option<i64> {
3644 let (_payload_len, n1) = read_varint(buf, off).ok()?;
3645 let (rowid, _) = read_varint(buf, off + n1).ok()?;
3646 Some(rowid)
3647}
3648
3649/// Given the sorted byte extents of live cells, return the maximal **free**
3650/// (unallocated) spans within `[lo, hi)` — the complement of the live extents.
3651/// These are the only ranges [`Database::carve_free_regions`] scans, so a live
3652/// cell can never be re-surfaced.
3653fn free_regions(live: &[(usize, usize)], lo: usize, hi: usize) -> Vec<(usize, usize)> {
3654 let mut regions = Vec::new();
3655 // An inverted or empty range (lo >= hi) has no free regions. Guard before the
3656 // `clamp(lo, hi)` calls below, which panic when lo > hi (untrusted-input path).
3657 if lo >= hi {
3658 return regions;
3659 }
3660 let mut cursor = lo;
3661 for &(s, e) in live {
3662 let s = s.clamp(lo, hi);
3663 let e = e.clamp(lo, hi);
3664 if s > cursor {
3665 regions.push((cursor, s));
3666 }
3667 if e > cursor {
3668 cursor = e;
3669 }
3670 }
3671 if cursor < hi {
3672 regions.push((cursor, hi));
3673 }
3674 regions
3675}
3676
3677/// Derive a [`FreeblockTemplate`] from the first live cell on a table-leaf page:
3678/// the record's header length, its serial-type array, and the byte width of the
3679/// cell prefix (payload-length + rowid varints) that the freeblock header
3680/// overwrites. Returns `None` when no live cell parses or the prefix is wider
3681/// than the 4 bytes a freeblock header clobbers (the simple template cannot then
3682/// place the surviving serial tail).
3683/// Shared internal walker producing BOTH recovery tiers in one pass so the cell
3684/// and fragment outputs can never diverge: `(full_cells, fragments)`.
3685/// [`Database::reconstruct_freeblock_records`] takes `.0`,
3686/// [`Database::reconstruct_freeblock_fragments`] takes `.1`. A free function (it
3687/// needs no `Database` state — only the page bytes and the page-derived
3688/// template), keeping the two public entry points a thin projection of one walk.
3689fn reconstruct_freeblock_inner(
3690 page_bytes: &[u8],
3691 enc: TextEncoding,
3692) -> (Vec<CarvedCell>, Vec<CellFragment>) {
3693 let mut cells = Vec::new();
3694 let mut frags = Vec::new();
3695 let hdr_off = if page_bytes.starts_with(SQLITE_MAGIC) {
3696 SQLITE_HEADER_SIZE
3697 } else {
3698 0
3699 };
3700 let Some(&page_type) = page_bytes.get(hdr_off) else {
3701 return (cells, frags);
3702 };
3703 if page_type != 0x0d {
3704 return (cells, frags); // only table-leaf pages have freeblock residue
3705 }
3706 let Some(template) = freeblock_template(page_bytes, hdr_off, enc) else {
3707 return (cells, frags);
3708 };
3709
3710 let first_freeblock = be_u16(page_bytes, hdr_off + 1) as usize;
3711 let mut fb = first_freeblock;
3712 let mut walked = 0usize;
3713 let mut visited = std::collections::BTreeSet::new();
3714 while fb != 0 && walked < MAX_FREEBLOCKS_PER_PAGE {
3715 walked += 1;
3716 if !visited.insert(fb) {
3717 break; // cyclic next pointer
3718 }
3719 let next = be_u16(page_bytes, fb) as usize;
3720 let size = be_u16(page_bytes, fb + 2) as usize;
3721 let Some(fb_end) = fb.checked_add(size) else {
3722 break; // cov:unreachable: usize add of two u16-range values
3723 };
3724 if size >= 4 && fb_end <= page_bytes.len() {
3725 if template.known_lead_serials.is_empty() {
3726 // Empty-lead (2-byte-rowid) page: each freeblock is a single freed
3727 // cell whose serial array fully survives. Reconstruct it ONLY if
3728 // the record tiles the freeblock exactly — the precision gate that
3729 // rejects the misaligned runs a loose walk would manufacture.
3730 cells.extend(template.reconstruct_span_exact(page_bytes, fb, fb_end));
3731 } else {
3732 template
3733 .reconstruct_span_tiered(page_bytes, fb, fb_end, false, &mut cells, &mut frags);
3734 }
3735 }
3736 fb = next;
3737 }
3738
3739 let cell_count = be_u16(page_bytes, hdr_off + 3) as usize;
3740 let cptr_end = hdr_off + 8 + cell_count * 2;
3741 let cca = be_u16(page_bytes, hdr_off + 5) as usize;
3742 // The unallocated-gap pass anchors off a surviving forward cell and a known
3743 // leading serial; it is meaningful only for the (non-empty-lead) span-walk
3744 // templates. Empty-lead pages recover solely through the exact-tile chain pass.
3745 if !template.known_lead_serials.is_empty() && cca > cptr_end && cca <= page_bytes.len() {
3746 for anchor_off in cptr_end..cca {
3747 let Some(anchor) =
3748 try_carve_cell_at(page_bytes, anchor_off, Some(template.column_count), enc)
3749 else {
3750 continue;
3751 };
3752 let has_text = anchor
3753 .values
3754 .iter()
3755 .any(|v| matches!(v, Value::Text(t) if !t.is_empty() && !t.contains('\u{FFFD}')));
3756 if !has_text {
3757 continue;
3758 }
3759 let tail_start = anchor.offset + anchor.byte_len;
3760 template
3761 .reconstruct_span_tiered(page_bytes, tail_start, cca, true, &mut cells, &mut frags);
3762 break; // one anchored run per page — the contiguous freed tail
3763 }
3764 }
3765 (cells, frags)
3766}
3767
3768fn freeblock_template(
3769 page_bytes: &[u8],
3770 hdr_off: usize,
3771 enc: TextEncoding,
3772) -> Option<FreeblockTemplate> {
3773 let cell_count = be_u16(page_bytes, hdr_off + 3) as usize;
3774 let cell_ptr_array = hdr_off + 8;
3775 for i in 0..cell_count {
3776 let cell_off = be_u16(page_bytes, cell_ptr_array + i * 2) as usize;
3777 if cell_off == 0 || cell_off >= page_bytes.len() {
3778 continue;
3779 }
3780 // Prefix: payload-length varint, rowid varint.
3781 let Ok((_payload_len, n1)) = read_varint(page_bytes, cell_off) else {
3782 continue; // cov:unreachable: a live cell-pointer addresses an in-bounds prefix
3783 };
3784 let Ok((_rowid, n2)) = read_varint(page_bytes, cell_off + n1) else {
3785 continue; // cov:unreachable: the rowid varint follows the payload-len varint in-page
3786 };
3787 let prefix_len = n1 + n2;
3788 // The freeblock header overwrites exactly 4 bytes. If the prefix alone is
3789 // wider, no record-header byte is clobbered in a way this simple template
3790 // handles — skip (those tables keep an intact header tail the forward
3791 // carver already reaches).
3792 if prefix_len > 4 {
3793 continue; // cov:unreachable: the corpus tables all encode a <=4-byte cell prefix
3794 }
3795 let payload_start = cell_off + n1 + n2;
3796 let Ok((header_len, hn)) = read_varint(page_bytes, payload_start) else {
3797 continue; // cov:unreachable: a live cell's record header follows its prefix in-page
3798 };
3799 let header_len = usize::try_from(header_len).ok()?;
3800 if header_len < hn {
3801 continue; // cov:unreachable: a live record's header_len covers its own varint
3802 }
3803 // Read the template's serial-type array, recording each serial's byte
3804 // offset within the header so we can split clobbered vs surviving.
3805 let mut serials = Vec::new();
3806 let mut hpos = hn;
3807 let mut ok = true;
3808 while hpos < header_len {
3809 let Ok((s, used)) = read_varint(page_bytes, payload_start + hpos) else {
3810 ok = false; // cov:unreachable: header_len bounds the serial array within the page
3811 break; // cov:unreachable: paired with the read failure above
3812 };
3813 serials.push((s, hpos, used));
3814 hpos += used;
3815 }
3816 if !ok || hpos != header_len || serials.len() < MIN_INFERRED_COLUMNS {
3817 continue; // cov:unreachable: a live cell's header parses cleanly with >= 2 columns
3818 }
3819 return FreeblockTemplate::build(prefix_len, header_len, hn, &serials, enc);
3820 }
3821 None
3822}
3823
3824/// A record-header template derived from a live cell on a table-leaf page, used
3825/// to rebuild freeblock-clobbered records (see
3826/// [`Database::reconstruct_freeblock_records`]).
3827///
3828/// Freeblock conversion overwrites the freed cell's first four bytes — the
3829/// payload-length + rowid varints, the record `header_len`, and the leading
3830/// serial type(s). The surviving serial-type tail and the value body remain. The
3831/// template supplies what was destroyed: the total column count, the serial types
3832/// of the leading (clobbered) columns, and the page offset, relative to the
3833/// freeblock start, at which the surviving serial tail begins.
3834struct FreeblockTemplate {
3835 /// Total number of columns in a record of this table.
3836 column_count: usize,
3837 /// Serial types of the leading columns whose header bytes the freeblock
3838 /// header clobbered (taken from the template; e.g. the fixed-width `id`).
3839 known_lead_serials: Vec<i64>,
3840 /// Offset, relative to the freeblock start, at which the **surviving** serial
3841 /// tail begins (== `prefix_len + first_surviving_serial_header_offset`).
3842 surviving_serials_off: usize,
3843 /// Text encoding of the owning database, so reconstructed text decodes per
3844 /// the header (UTF-8 / UTF-16) rather than assuming UTF-8.
3845 text_encoding: TextEncoding,
3846}
3847
3848impl FreeblockTemplate {
3849 /// Build a template from a parsed live-cell header. `serials` is the list of
3850 /// `(serial_type, header_offset, varint_width)` tuples for every column.
3851 /// Returns `None` when the 4-byte freeblock clobber boundary cannot be
3852 /// resolved to a clean split between leading and surviving serials.
3853 fn build(
3854 prefix_len: usize,
3855 _header_len: usize,
3856 _hn: usize,
3857 serials: &[(i64, usize, usize)],
3858 enc: TextEncoding,
3859 ) -> Option<FreeblockTemplate> {
3860 // Bytes of the record header the 4-byte freeblock header destroys.
3861 let clobbered_header_bytes = 4usize.checked_sub(prefix_len)?;
3862 // The first column whose header bytes survive intact is the first serial
3863 // whose header offset is at or beyond the clobber boundary. Everything
3864 // before it is supplied from the template.
3865 let mut known_lead = Vec::new();
3866 let mut surviving_serials_off = None;
3867 for &(serial, hpos, _used) in serials {
3868 if hpos >= clobbered_header_bytes {
3869 surviving_serials_off = Some(prefix_len + hpos);
3870 break;
3871 }
3872 known_lead.push(serial);
3873 }
3874 // At least one serial must survive to anchor the reconstruction. The
3875 // leading (clobbered) serial list MAY be empty: a 2-byte-or-wider rowid
3876 // varint (rowid >= 128) widens the cell prefix so the 4-byte freeblock
3877 // clobber stops at `header_len`, destroying NO serial type — the whole
3878 // serial array survives. Such pages reconstruct via the exact-tile
3879 // single-cell path (`reconstruct_freeblock_inner` routes on
3880 // `known_lead_serials.is_empty()`), which requires each freed cell to fill
3881 // its freeblock exactly; that precision check keeps the empty-lead case
3882 // phantom-free where a loose span walk would mis-align columns.
3883 let surviving_serials_off = surviving_serials_off?;
3884 Some(FreeblockTemplate {
3885 column_count: serials.len(),
3886 known_lead_serials: known_lead,
3887 surviving_serials_off,
3888 text_encoding: enc,
3889 })
3890 }
3891
3892 /// Reconstruct **every** clobbered cell coalesced into the free span
3893 /// `[lo, hi)` — a chained freeblock or a page's unallocated gap — and append
3894 /// each to `out`.
3895 ///
3896 /// When SQLite frees adjacent cells it coalesces them into one freeblock whose
3897 /// interior still holds the freed cells back-to-back, **each** prefixed by a
3898 /// stale 4-byte freeblock header (`next`/`size`) that clobbers that cell's
3899 /// payload-length + rowid varints and leading serial(s). A single-shot
3900 /// reconstruction at `lo` recovers only the span's first cell; the trailing
3901 /// cells are intact records sitting at the previous record's end. This walks
3902 /// the template across the span: reconstruct at `lo`, advance to that record's
3903 /// end, repeat to `hi`. Every value is derived from the span bounds and the
3904 /// page's own schema template — no per-cell or per-database constant.
3905 ///
3906 /// Each candidate is validated identically to the single-cell case (legal
3907 /// serial types, record fits within `[cell_start, hi)`). The walk is
3908 /// **structural, not a sliding scan**: SQLite coalesces freed cells exactly
3909 /// back-to-back (each freed record's end abuts the next freed cell's clobbered
3910 /// 4-byte prefix), so the next cell begins precisely at the previous record's
3911 /// end. The walk therefore reconstructs at `lo`, advances to that record's
3912 /// end, and repeats — and STOPS the moment a position does not reconstruct
3913 /// cleanly. It never slides forward byte-by-byte hunting for the next cell:
3914 /// that fallback would synthesize a record from any run of bytes that happens
3915 /// to satisfy the legal-serial + fits-in-span checks, manufacturing phantoms
3916 /// in non-cell free space. Anchoring every cell at the prior record's exact
3917 /// end is what keeps the broader span-walk at single-cell precision. Bounded:
3918 /// the walk strictly advances (a record is non-empty) and is capped at
3919 /// [`MAX_FREEBLOCKS_PER_PAGE`] reconstructions per span.
3920 ///
3921 /// Follower precision (the coalesced-freeblock signature): the span's FIRST
3922 /// cell at `lo` is reconstructed unconditionally — `lo` is a real boundary (a
3923 /// freeblock-chain entry, or the gap anchor's first follower). Every SUBSEQUENT
3924 /// follower must carry the structural mark of a freed-and-coalesced cell: its
3925 /// clobbered 4-byte prefix is a stale freeblock header whose 2-byte `next`
3926 /// field is `0x0000` (a terminal/orphaned freeblock — what SQLite leaves when
3927 /// it coalesces freed cells back-to-back). A position whose leading two bytes
3928 /// are non-zero is a byte-shifted remnant, not a coalesced cell, so the run
3929 /// ends there. This is the check that separates a true coalesced tail (0D-06's
3930 /// `00 00 NN NN`-prefixed followers) from a misaligned fragment (0B-02's
3931 /// `24 09 …` remnant), keeping the gap pass phantom-free.
3932 ///
3933 /// `enforce_follower_mark` is `true` for the unallocated-gap pass, where the
3934 /// span is bounded only by `cellContentArea` (not by a page-recorded freeblock
3935 /// size) and so a byte-shifted remnant could otherwise be mistaken for a
3936 /// follower: there EVERY position must carry the `next == 0` mark. It is `false`
3937 /// for the freeblock-chain pass, whose span bounds are the page-recorded
3938 /// `[fb, fb + size)` — a strong boundary that already pins the coalesced run, so
3939 /// the interior followers (whose clobbered bytes are the original record's own
3940 /// varints, not necessarily `00 00 …`) are accepted on the fit-in-span check
3941 /// alone.
3942 ///
3943 /// Tiered walk: it pushes each reconstructed full cell into `cells`, and at the
3944 /// anchor where `reconstruct_one` would `break` it salvages the maximal
3945 /// decodable column prefix into `frags` as a [`CellFragment`] (when the §3.1
3946 /// distinctiveness gate passes) before stopping. Fragment salvage does NOT
3947 /// extend the walk — it stops at exactly the position the full walk does,
3948 /// preserving Tier-1's phantom discipline. Callers that want only the full
3949 /// cells (the Tier-1 [`Database::reconstruct_freeblock_records`]) discard
3950 /// `frags`; both tiers therefore come from one walk and can never diverge.
3951 fn reconstruct_span_tiered(
3952 &self,
3953 page: &[u8],
3954 lo: usize,
3955 hi: usize,
3956 enforce_follower_mark: bool,
3957 cells: &mut Vec<CarvedCell>,
3958 frags: &mut Vec<CellFragment>,
3959 ) {
3960 let mut cell_start = lo;
3961 let mut built = 0usize;
3962 while cell_start < hi && built < MAX_FREEBLOCKS_PER_PAGE {
3963 if enforce_follower_mark && be_u16(page, cell_start) != 0 {
3964 break; // not a coalesced freeblock follower — the contiguous run ends
3965 }
3966 let Some((cell, record_end)) = self.reconstruct_one(page, cell_start, hi) else {
3967 // Full reconstruction failed at this anchor; try to salvage the
3968 // decodable prefix as a fragment, then stop (do not extend the
3969 // walk past the failed anchor).
3970 if let Some(frag) = self.salvage_fragment(page, cell_start, hi) {
3971 frags.push(frag);
3972 }
3973 break;
3974 };
3975 cells.push(cell);
3976 built += 1;
3977 cell_start = record_end;
3978 }
3979 }
3980
3981 /// Salvage the maximal decodable column prefix at `cell_start` (bounded by
3982 /// `span_end`) when full reconstruction failed there. Walks the template +
3983 /// surviving serial array forward, decoding each column's body while it fits
3984 /// in the span; the first illegal serial, out-of-bounds read, or body that
3985 /// overruns the span ends the prefix. Returns a [`CellFragment`] **only** when
3986 /// the salvaged prefix contains at least one distinctive cell (TEXT ≥ 4 bytes
3987 /// of valid UTF-8, or REAL) — the §3.1 emission gate — otherwise `None`.
3988 fn salvage_fragment(
3989 &self,
3990 page: &[u8],
3991 cell_start: usize,
3992 span_end: usize,
3993 ) -> Option<CellFragment> {
3994 let surviving_count = self.column_count - self.known_lead_serials.len();
3995 let tail_start = cell_start.checked_add(self.surviving_serials_off)?;
3996
3997 // Read as many legal surviving serials as decode in-bounds within the span.
3998 // The template's leading serials are always legal (they came from a live
3999 // cell), so the full serial array is `known_lead ++ legal_surviving`.
4000 let mut serials = self.known_lead_serials.clone();
4001 let mut pos = tail_start;
4002 for _ in 0..surviving_count {
4003 let Ok((s, used)) = read_varint(page, pos) else {
4004 break; // cov:unreachable: the surviving serials sit near the cell start, inside the freeblock/gap span the inner walker already bounds to the page; this read mirrors reconstruct_one's bounds guard so a truncated tail ends the prefix rather than panicking
4005 };
4006 if serial_body_len(s).is_none() {
4007 break; // cov:unreachable: serial_body_len is None only for a negative serial, which read_varint yields only from a crafted 9-byte varint; kept as a defence-in-depth guard so a malformed surviving tail ends the prefix rather than mis-decoding
4008 }
4009 let Some(next) = pos.checked_add(used) else {
4010 break; // cov:unreachable: usize add of an in-page varint width
4011 };
4012 if next > span_end {
4013 break; // serial tail overran the span
4014 }
4015 serials.push(s);
4016 pos = next;
4017 }
4018
4019 // Decode column bodies left-to-right, keeping each whose body ends within
4020 // the span. The body begins right after the surviving serial tail.
4021 let body_start = pos;
4022 let mut surviving: Vec<(usize, Value)> = Vec::new();
4023 let mut bpos = body_start;
4024 for (idx, &s) in serials.iter().enumerate() {
4025 let Some(blen) = serial_body_len(s) else {
4026 break; // cov:unreachable: only legal serials were pushed above
4027 };
4028 let Some(body_end) = bpos.checked_add(blen) else {
4029 break; // cov:unreachable: usize add of an in-page body length
4030 };
4031 if body_end > span_end {
4032 break; // this column's body overruns the span — prefix ends here
4033 }
4034 let Some(body) = page.get(bpos..body_end) else {
4035 break; // cov:unreachable: body_end <= span_end <= page.len()
4036 };
4037 let Ok((val, _)) = decode_value(body, 0, s, self.text_encoding) else {
4038 break; // cov:unreachable: serial_body_len-legal serials decode in-bounds
4039 };
4040 surviving.push((idx, val));
4041 bpos = body_end;
4042 }
4043
4044 // Emission gate: at least one distinctive cell (TEXT >= 4 UTF-8 bytes, or
4045 // REAL). A lone integer/NULL/blob prefix is coincidence-prone — no fragment.
4046 if !surviving.iter().any(|(_, v)| is_distinctive(v)) {
4047 return None;
4048 }
4049 let last_body_end = bpos;
4050 Some(CellFragment {
4051 offset: cell_start,
4052 byte_len: last_body_end.saturating_sub(cell_start),
4053 missing: self.column_count - surviving.len(),
4054 surviving,
4055 confidence: FRAGMENT_CONFIDENCE,
4056 })
4057 }
4058
4059 /// Rebuild the single record whose clobbered cell begins at `cell_start`,
4060 /// bounded by the enclosing span end `span_end`: read the surviving serial
4061 /// tail, prepend the template's leading serials, decode the body, and validate
4062 /// the whole record fits within `[cell_start, span_end)`. Returns the carved
4063 /// cell and the record's end offset (the next coalesced cell's start), or
4064 /// `None` on any out-of-bounds or implausible parse.
4065 fn reconstruct_one(
4066 &self,
4067 page: &[u8],
4068 cell_start: usize,
4069 span_end: usize,
4070 ) -> Option<(CarvedCell, usize)> {
4071 let surviving_count = self.column_count - self.known_lead_serials.len();
4072 let tail_start = cell_start.checked_add(self.surviving_serials_off)?;
4073
4074 // Read the surviving serial tail from the freeblock.
4075 let mut serials = self.known_lead_serials.clone();
4076 let mut pos = tail_start;
4077 for _ in 0..surviving_count {
4078 let (s, used) = read_varint(page, pos).ok()?;
4079 // A serial type must be legal; reject the candidate otherwise.
4080 serial_body_len(s)?;
4081 serials.push(s);
4082 pos = pos.checked_add(used)?;
4083 if pos > span_end {
4084 return None;
4085 }
4086 }
4087
4088 // The body begins right after the surviving serial tail. Compute its
4089 // length from the full (template + surviving) serial array.
4090 let mut body_len = 0usize;
4091 for &s in &serials {
4092 body_len = body_len.checked_add(serial_body_len(s)?)?;
4093 }
4094 let body_start = pos;
4095 let record_end = body_start.checked_add(body_len)?;
4096 // The reconstructed record MUST fit within the enclosing span — the core
4097 // precision check that rejects coincidental/garbage reconstructions.
4098 if record_end > span_end {
4099 return None;
4100 }
4101
4102 // Synthesize a record payload (header + body) for the shared decoder so
4103 // values are decoded with the same storage-class fidelity as live rows.
4104 // The rowid is destroyed; pass 0 so a serial-0 column reads as NULL rather
4105 // than a fabricated rowid.
4106 let body = page.get(body_start..record_end)?;
4107 let values = decode_synthetic_record(&serials, body, self.text_encoding)?;
4108 if values.len() != self.column_count {
4109 return None; // cov:unreachable: one value per serial by construction
4110 }
4111
4112 Some((
4113 CarvedCell {
4114 offset: cell_start,
4115 byte_len: record_end - cell_start,
4116 rowid: 0, // destroyed by freeblock conversion — surfaced as unknown
4117 values,
4118 confidence: FREEBLOCK_RECONSTRUCT_CONFIDENCE,
4119 },
4120 record_end,
4121 ))
4122 }
4123
4124 /// Reconstruct ONE freeblock-clobbered empty-leading-serial cell at
4125 /// `cell_start` — a 2-byte-or-wider rowid, so the 4-byte clobber destroyed no
4126 /// serial type and the whole serial array survives at
4127 /// `cell_start + surviving_serials_off`. Returns the carved cell (rowid
4128 /// destroyed → 0) **and the record's end offset**, or `None` on any
4129 /// out-of-bounds parse or a record that overruns `span_end`. Does NOT enforce
4130 /// an exact tile — the span walker [`Self::reconstruct_span_exact`] does.
4131 fn reconstruct_cell_empty_lead(
4132 &self,
4133 page: &[u8],
4134 cell_start: usize,
4135 span_end: usize,
4136 ) -> Option<(CarvedCell, usize)> {
4137 let tail_start = cell_start.checked_add(self.surviving_serials_off)?;
4138 // The whole serial array survives (no clobbered leading serial); read all
4139 // `column_count` serials from the freeblock.
4140 let mut serials = Vec::with_capacity(self.column_count);
4141 let mut pos = tail_start;
4142 for _ in 0..self.column_count {
4143 let (s, used) = read_varint(page, pos).ok()?;
4144 serial_body_len(s)?;
4145 serials.push(s);
4146 pos = pos.checked_add(used)?;
4147 if pos > span_end {
4148 return None;
4149 }
4150 }
4151 let mut body_len = 0usize;
4152 for &s in &serials {
4153 body_len = body_len.checked_add(serial_body_len(s)?)?;
4154 }
4155 let body_start = pos;
4156 let record_end = body_start.checked_add(body_len)?;
4157 if record_end > span_end {
4158 return None;
4159 }
4160 let body = page.get(body_start..record_end)?;
4161 let values = decode_synthetic_record(&serials, body, self.text_encoding)?;
4162 if values.len() != self.column_count {
4163 return None; // cov:unreachable: one value per serial by construction
4164 }
4165 Some((
4166 CarvedCell {
4167 offset: cell_start,
4168 byte_len: record_end - cell_start,
4169 rowid: 0, // destroyed by freeblock conversion — surfaced as unknown
4170 values,
4171 confidence: FREEBLOCK_RECONSTRUCT_CONFIDENCE,
4172 },
4173 record_end,
4174 ))
4175 }
4176
4177 /// Reconstruct every empty-leading-serial cell coalesced into the freeblock
4178 /// `[lo, hi)`, returned ONLY when they tile the freeblock **exactly** (the
4179 /// walk reaches `hi` with no leftover bytes).
4180 ///
4181 /// A single freed cell fills its freeblock exactly; adjacent deletions
4182 /// coalesce into one freeblock whose interior holds the freed cells
4183 /// back-to-back, each clobbered in its first 4 bytes. Walking cell-to-cell and
4184 /// requiring the run to land precisely on `hi` is the precision gate: a
4185 /// misaligned read (a deleted cell whose destroyed rowid width differs from the
4186 /// template's) fails to reach `hi` exactly, so the whole span is rejected
4187 /// rather than emitted as column-shifted phantoms. Bounded by
4188 /// [`MAX_FREEBLOCKS_PER_PAGE`]; a record always advances `cell_start`.
4189 fn reconstruct_span_exact(&self, page: &[u8], lo: usize, hi: usize) -> Vec<CarvedCell> {
4190 let mut cells = Vec::new();
4191 let mut cell_start = lo;
4192 let mut guard = 0usize;
4193 while cell_start < hi && guard < MAX_FREEBLOCKS_PER_PAGE {
4194 guard += 1;
4195 let Some((cell, record_end)) = self.reconstruct_cell_empty_lead(page, cell_start, hi)
4196 else {
4197 return Vec::new(); // a cell did not reconstruct → not a clean tiling
4198 };
4199 if record_end <= cell_start {
4200 return Vec::new(); // cov:unreachable: a non-empty record advances cell_start
4201 }
4202 cells.push(cell);
4203 cell_start = record_end;
4204 }
4205 // Exact tile: leftover bytes (or a walk stopped by the bound) mean a
4206 // misaligned run — emit nothing.
4207 if cell_start == hi {
4208 cells
4209 } else {
4210 Vec::new()
4211 }
4212 }
4213
4214 /// Reconstruct a freeblock-clobbered **spilled** cell at `cell_start` (task
4215 /// #73, design §2.2). A spilled cell always carries a multi-byte
4216 /// `payload_len` varint, so the 4-byte freeblock clobber destroys the
4217 /// `payload_len` + `rowid` varints and the record's `header_len` varint —
4218 /// **but not the serial-type array**, which survives intact immediately after
4219 /// the clobber. We therefore read the full serial array directly from
4220 /// `cell_start + CLOBBER` (using the template only for the column count),
4221 /// re-derive `header_len` and `P = header_len + Σ serial_body_len`, and — when
4222 /// `P > usable - 35` — resolve the spill: `local_payload_len(P, usable)` bytes
4223 /// of payload sit locally (the destroyed header counted within them), the
4224 /// 4-byte first-overflow pointer follows, and the chain is resolved through
4225 /// freelist leaves. Returns `(cell, chain)` with `rowid = 0`, or `None`.
4226 ///
4227 /// UNPROVEN-BY-CORPUS (Codex ruling #5): synthetic-fixture validation only.
4228 /// No real Nemetz cell is both freeblock-clobbered and spilled.
4229 fn reconstruct_spilled(
4230 &self,
4231 db: &Database,
4232 page: &[u8],
4233 cell_start: usize,
4234 usable: usize,
4235 freed_leaves: &std::collections::BTreeSet<u32>,
4236 ) -> Option<(CarvedCell, Vec<u32>)> {
4237 // The freeblock header clobbers exactly 4 bytes. For a spilled cell those
4238 // 4 bytes are payload_len(>=2) + rowid(>=1) + header_len(>=1) varints, so
4239 // the serial array begins right after the clobber.
4240 const CLOBBER: usize = 4;
4241 let serials_start = cell_start.checked_add(CLOBBER)?;
4242 let mut serials = Vec::with_capacity(self.column_count);
4243 let mut pos = serials_start;
4244 for _ in 0..self.column_count {
4245 let (s, used) = read_varint(page, pos).ok()?;
4246 serial_body_len(s)?;
4247 serials.push(s);
4248 pos = pos.checked_add(used)?;
4249 }
4250
4251 // Re-derive the record header bytes that were destroyed: header_len is a
4252 // varint counting itself plus the serial array.
4253 let mut serial_bytes_len = 0usize;
4254 for &s in &serials {
4255 serial_bytes_len += varint_len(s);
4256 }
4257 let mut header_len = serial_bytes_len + 1;
4258 while varint_len(header_len as i64) + serial_bytes_len != header_len {
4259 header_len += 1;
4260 }
4261 // The clobber removed `header_len`'s own varint plus the prefix; verify the
4262 // surviving serial array aligns with the reconstructed header (the bytes
4263 // from serials_start to `pos` are the serial array, length serial_bytes_len).
4264 if pos.checked_sub(serials_start)? != serial_bytes_len {
4265 return None; // cov:unreachable: read_varint widths sum to serial_bytes_len
4266 }
4267 let mut body_len = 0usize;
4268 for &s in &serials {
4269 body_len = body_len.checked_add(serial_body_len(s)?)?;
4270 }
4271 let payload_len = header_len.checked_add(body_len)?;
4272 // Only the spilled class — an in-page payload is the existing template path.
4273 if payload_len <= usable.checked_sub(35)? {
4274 return None;
4275 }
4276 let local_len = local_payload_len(payload_len, usable);
4277
4278 // The body starts right after the surviving serial array. The local payload
4279 // spans `local_len` bytes of (header ++ body); the destroyed header is
4280 // `header_len` of those, so `local_len - header_len` body bytes are present
4281 // locally before the 4-byte first-overflow pointer.
4282 let body_start = pos;
4283 let local_body = local_len.checked_sub(header_len)?;
4284 let local_body_end = body_start.checked_add(local_body)?;
4285 let ptr_off = local_body_end;
4286 let ptr_slice = page.get(ptr_off..ptr_off + 4)?;
4287 let first_overflow =
4288 u32::from_be_bytes([ptr_slice[0], ptr_slice[1], ptr_slice[2], ptr_slice[3]]);
4289 let local_body_bytes = page.get(body_start..local_body_end)?;
4290
4291 let remaining = payload_len - local_len;
4292 let (chain_content, chain) = db
4293 .read_freed_overflow_chain(first_overflow, remaining, usable, freed_leaves)
4294 .ok()?;
4295
4296 // Assemble the full payload: reconstructed header ++ local body ++ chain.
4297 let mut header = enc_varint_into(header_len);
4298 for &s in &serials {
4299 header.extend(enc_varint_into(usize::try_from(s).ok()?));
4300 }
4301 if header.len() != header_len {
4302 return None; // cov:unreachable: header_len was solved to this width
4303 }
4304 let mut payload = Vec::with_capacity(payload_len);
4305 payload.extend_from_slice(&header);
4306 payload.extend_from_slice(local_body_bytes);
4307 payload.extend_from_slice(&chain_content);
4308 if payload.len() != payload_len {
4309 return None; // cov:unreachable: local_body + chain == body_len by construction
4310 }
4311
4312 let values = decode_record(&payload, self.column_count, 0, db.header.text_encoding).ok()?;
4313 if values.len() != self.column_count {
4314 return None; // cov:unreachable: one value per serial
4315 }
4316 let any_replacement = values.iter().any(|v| match v {
4317 Value::Text(t) => t.contains('\u{FFFD}'),
4318 _ => false,
4319 });
4320 if any_replacement {
4321 return None;
4322 }
4323 if !values.iter().any(is_distinctive) {
4324 return None;
4325 }
4326
4327 Some((
4328 CarvedCell {
4329 offset: cell_start,
4330 byte_len: ptr_off + 4 - cell_start,
4331 rowid: 0,
4332 values,
4333 confidence: FREEBLOCK_RECONSTRUCT_CONFIDENCE * OVERFLOW_CHAIN_CONFIDENCE_FACTOR,
4334 },
4335 chain,
4336 ))
4337 }
4338}
4339
4340/// Decode a record body given an explicit serial-type array (the freeblock
4341/// reconstructor supplies the array; the on-disk `header_len` + leading serials
4342/// were destroyed). Mirrors [`decode_record`]'s body pass. Returns `None` on any
4343/// out-of-bounds read so a malformed reconstruction is rejected, never panics.
4344fn decode_synthetic_record(serials: &[i64], body: &[u8], enc: TextEncoding) -> Option<Vec<Value>> {
4345 let mut values = Vec::with_capacity(serials.len());
4346 let mut bpos = 0usize;
4347 for &serial in serials {
4348 let (val, size) = decode_value(body, bpos, serial, enc).ok()?;
4349 values.push(val);
4350 bpos = bpos.checked_add(size)?;
4351 }
4352 Some(values)
4353}
4354
4355/// Attempt to recognize a table-leaf cell at `off` in `buf` as a record.
4356///
4357/// `expected_columns` is `Some(n)` to require exactly `n` columns (fixed-schema
4358/// carving), or `None` to **infer** the column count from the record's own
4359/// serial-type array (dropped-table / schema-gone carving). Returns a
4360/// [`CarvedCell`] only when the bytes are self-consistently record-shaped;
4361/// otherwise `None`. Never panics — every access is bounds-checked.
4362fn try_carve_cell_at(
4363 buf: &[u8],
4364 off: usize,
4365 expected_columns: Option<usize>,
4366 enc: TextEncoding,
4367) -> Option<CarvedCell> {
4368 // Cell prefix: payload_len varint, rowid varint.
4369 let (payload_len, n1) = read_varint(buf, off).ok()?;
4370 let payload_len = usize::try_from(payload_len).ok()?;
4371 if payload_len == 0 {
4372 return None;
4373 }
4374 let (rowid, n2) = read_varint(buf, off + n1).ok()?;
4375 // A negative rowid is legal but vanishingly rare for browser tables; treat a
4376 // non-positive rowid as a non-match to suppress coincidental hits.
4377 if rowid <= 0 {
4378 return None;
4379 }
4380 let payload_start = off + n1 + n2;
4381 let payload = buf.get(payload_start..payload_start + payload_len)?;
4382
4383 // Record header: header_len varint, then one serial type per column.
4384 let (header_len, hn) = read_varint(payload, 0).ok()?;
4385 let header_len = usize::try_from(header_len).ok()?;
4386 if header_len > payload.len() || header_len < hn {
4387 return None;
4388 }
4389 let cap = expected_columns.unwrap_or(0);
4390 let mut serials = Vec::with_capacity(cap);
4391 let mut hpos = hn;
4392 while hpos < header_len {
4393 let (s, used) = read_varint(payload, hpos).ok()?;
4394 serials.push(s);
4395 hpos += used;
4396 }
4397 // The header must consume cleanly, and match the expected column count when
4398 // one was given. When inferring, require a minimum plausible column count to
4399 // suppress coincidental 1-column matches.
4400 if hpos != header_len {
4401 return None;
4402 }
4403 match expected_columns {
4404 Some(n) if serials.len() != n => return None,
4405 None if serials.len() < MIN_INFERRED_COLUMNS => return None,
4406 _ => {}
4407 }
4408 let column_count = serials.len();
4409
4410 // Body length implied by the serial types must equal payload_len - header_len
4411 // — a strong self-consistency check that rejects coincidental matches.
4412 let mut body_len = 0usize;
4413 for &s in &serials {
4414 // Checked: a serial from free-space bytes can declare a body length near
4415 // usize::MAX; summing must reject (None) on overflow, never panic/wrap.
4416 body_len = body_len.checked_add(serial_body_len(s)?)?;
4417 }
4418 if header_len + body_len != payload_len {
4419 return None;
4420 }
4421
4422 // Decode the record (reusing the live decoder for storage-class fidelity).
4423 let values = decode_record(payload, column_count, rowid, enc).ok()?;
4424 if values.len() != column_count {
4425 return None; // cov:unreachable: decode_record yields one value per serial
4426 }
4427
4428 // Confidence: a fully self-consistent record already passed strong checks;
4429 // raise confidence when at least one column is a non-empty, valid-UTF-8 TEXT
4430 // (record-shaped *and* human-meaningful), which coincidental byte runs rarely
4431 // satisfy.
4432 let has_real_text = values.iter().any(|v| match v {
4433 Value::Text(t) => !t.is_empty() && !t.contains('\u{FFFD}'),
4434 _ => false,
4435 });
4436 let confidence = if has_real_text { 0.9 } else { 0.6 };
4437
4438 Some(CarvedCell {
4439 offset: off,
4440 byte_len: (payload_start + payload_len) - off,
4441 rowid,
4442 values,
4443 confidence,
4444 })
4445}
4446
4447/// Recognize a freed **spilled** table-leaf cell at `off` whose payload exceeds
4448/// the in-page threshold (`usable - 35`) and therefore continues on an
4449/// overflow-page chain (task #73). The sibling of [`try_carve_cell_at`] for the
4450/// overflow class: the two partition the candidate space by the spec spill
4451/// threshold, so a cell is recognized by exactly one of them.
4452///
4453/// `expected_columns` is `Some(n)` to require exactly `n` columns, or `None` to
4454/// infer the count (≥ [`MIN_INFERRED_COLUMNS`]). Returns a [`SpilledCell`]
4455/// (recognition only — the chain is resolved later) when the local prefix is
4456/// self-consistent: header fits in the local payload, the serial array consumes
4457/// the header cleanly, `header_len + Σ serial_body_len == P` (length closure
4458/// over the *declared* P), and the local payload plus its 4-byte overflow
4459/// pointer are in-bounds. Never panics — every access is bounds-checked.
4460fn try_carve_spilled_cell_at(
4461 buf: &[u8],
4462 off: usize,
4463 usable: usize,
4464 expected_columns: Option<usize>,
4465) -> Option<SpilledCell> {
4466 let (payload_len, n1) = read_varint(buf, off).ok()?;
4467 let payload_len = usize::try_from(payload_len).ok()?;
4468 // Only the overflow class — in-page payloads belong to `try_carve_cell_at`.
4469 if payload_len <= usable.checked_sub(35)? {
4470 return None;
4471 }
4472 let (rowid, n2) = read_varint(buf, off + n1).ok()?;
4473 if rowid <= 0 {
4474 return None;
4475 }
4476 let payload_start = off + n1 + n2;
4477 let local_len = local_payload_len(payload_len, usable);
4478 // The local payload prefix plus the 4-byte first-overflow pointer must be in
4479 // bounds of the scanned slice.
4480 let prefix = buf.get(payload_start..payload_start + local_len + 4)?;
4481
4482 // The record header must fit entirely within the local prefix — otherwise the
4483 // serial array is not addressable locally and we abstain rather than guess.
4484 let (header_len, hn) = read_varint(prefix, 0).ok()?;
4485 let header_len = usize::try_from(header_len).ok()?;
4486 if header_len > local_len || header_len < hn {
4487 return None;
4488 }
4489 let mut serials = Vec::new();
4490 let mut hpos = hn;
4491 while hpos < header_len {
4492 let (s, used) = read_varint(prefix, hpos).ok()?;
4493 serials.push(s);
4494 hpos += used;
4495 }
4496 if hpos != header_len {
4497 return None;
4498 }
4499 match expected_columns {
4500 Some(n) if serials.len() != n => return None,
4501 None if serials.len() < MIN_INFERRED_COLUMNS => return None,
4502 _ => {}
4503 }
4504
4505 // Length closure over the DECLARED payload: header + body must equal P.
4506 let mut body_len = 0usize;
4507 for &s in &serials {
4508 // Checked: a serial from free-space bytes can declare a body length near
4509 // usize::MAX; summing must reject (None) on overflow, never panic/wrap.
4510 body_len = body_len.checked_add(serial_body_len(s)?)?;
4511 }
4512 if header_len + body_len != payload_len {
4513 return None;
4514 }
4515
4516 let first_overflow = be_u32(prefix, local_len);
4517 Some(SpilledCell {
4518 offset: off,
4519 byte_len: n1 + n2 + local_len + 4,
4520 payload_len,
4521 rowid,
4522 serials,
4523 local_len,
4524 local_payload_off: payload_start,
4525 first_overflow,
4526 })
4527}
4528
4529/// Salvage the columns of a recognized [`SpilledCell`] whose bodies lie wholly
4530/// within the local payload (task #73, Codex ruling #4): the chain-resident
4531/// columns are dropped (the chain that would supply them failed), and the
4532/// surviving local columns become a [`CellFragment`]. Returns `None` unless the
4533/// salvaged prefix carries ≥ 1 distinctive cell (the §3.1 emission gate). The
4534/// returned fragment's `offset` is region-local; the caller translates it.
4535fn salvage_local_prefix(
4536 region: &[u8],
4537 sc: &SpilledCell,
4538 enc: TextEncoding,
4539) -> Option<CellFragment> {
4540 // The body begins right after the local header; decode each column while its
4541 // body ends within the local payload bytes (`local_payload_off + local_len`).
4542 let local_end = sc.local_payload_off.checked_add(sc.local_len)?;
4543 // Recompute the record header length to find where the body starts.
4544 let (header_len, _hn) = read_varint(region, sc.local_payload_off).ok()?;
4545 let header_len = usize::try_from(header_len).ok()?;
4546 let mut bpos = sc.local_payload_off.checked_add(header_len)?;
4547
4548 let mut surviving: Vec<(usize, Value)> = Vec::new();
4549 for (idx, &serial) in sc.serials.iter().enumerate() {
4550 let Some(blen) = serial_body_len(serial) else {
4551 break; // cov:unreachable: recognizer accepted only legal serials
4552 };
4553 let Some(body_end) = bpos.checked_add(blen) else {
4554 break; // cov:unreachable: usize add of an in-page body length
4555 };
4556 if body_end > local_end {
4557 break; // this column's body spills into the chain — local prefix ends
4558 }
4559 let Some(body) = region.get(bpos..body_end) else {
4560 break; // cov:unreachable: body_end <= local_end <= region.len()
4561 };
4562 // Column 0 of a rowid-alias table reads as the rowid when serial 0; here a
4563 // spilled cell's id column is a stored integer, so decode it directly.
4564 let Ok((val, _)) = decode_value(body, 0, serial, enc) else {
4565 break; // cov:unreachable: legal serials decode in-bounds
4566 };
4567 surviving.push((idx, val));
4568 bpos = body_end;
4569 }
4570
4571 if !surviving.iter().any(|(_, v)| is_distinctive(v)) {
4572 return None;
4573 }
4574 Some(CellFragment {
4575 offset: sc.offset,
4576 byte_len: bpos.saturating_sub(sc.local_payload_off),
4577 missing: sc.serials.len() - surviving.len(),
4578 surviving,
4579 confidence: FRAGMENT_CONFIDENCE,
4580 })
4581}
4582
4583/// Parse + validate the 100-byte file header.
4584/// The first up-to-100 bytes (the SQLite header region), kept resident so
4585/// fixed-offset header-field reads never touch the byte source.
4586fn header_prefix(bytes: &[u8]) -> Box<[u8]> {
4587 let n = bytes.len().min(SQLITE_HEADER_SIZE);
4588 bytes[..n].into()
4589}
4590
4591fn parse_header(bytes: &[u8]) -> Result<Header, Error> {
4592 let head = bytes.get(..SQLITE_HEADER_SIZE).ok_or(Error::TooShort)?;
4593 if !head.starts_with(SQLITE_MAGIC) {
4594 return Err(Error::BadMagic);
4595 }
4596 let raw = be_u16(head, SQLITE_PAGE_SIZE_OFFSET);
4597 let page_size: u32 = if raw == 1 { 65536 } else { u32::from(raw) };
4598 let valid = (512..=65536).contains(&page_size) && page_size.is_power_of_two();
4599 if !valid {
4600 return Err(Error::BadPageSize(page_size));
4601 }
4602 let reserved = *head.get(RESERVED_SPACE_OFFSET).ok_or(Error::TooShort)?;
4603 // Header byte 56 (BE u32): 1/0 = UTF-8, 2 = UTF-16LE, 3 = UTF-16BE
4604 // (file-format §1.3.1). Tolerant: an unexpected value degrades to UTF-8
4605 // rather than rejecting the database.
4606 let text_encoding = match be_u32(head, TEXT_ENCODING_OFFSET) {
4607 2 => TextEncoding::Utf16Le,
4608 3 => TextEncoding::Utf16Be,
4609 _ => TextEncoding::Utf8,
4610 };
4611 Ok(Header {
4612 page_size,
4613 reserved,
4614 text_encoding,
4615 })
4616}
4617
4618/// Decode a record (payload) into values. Serial type 0 on the first column of
4619/// a rowid table is the `INTEGER PRIMARY KEY` alias → the cell's rowid.
4620fn decode_record(
4621 payload: &[u8],
4622 _column_count: usize,
4623 rowid: i64,
4624 enc: TextEncoding,
4625) -> Result<Vec<Value>, Error> {
4626 // A table-b-tree record: column 0 is the INTEGER PRIMARY KEY alias, so a
4627 // serial-0 there reads the rowid rather than NULL.
4628 decode_record_inner(payload, enc, Some(rowid))
4629}
4630
4631/// Decode an index-b-tree record payload (roadmap §1.4). Unlike a table record it
4632/// has NO `INTEGER PRIMARY KEY` alias — every column is stored literally, so a
4633/// serial-0 first column is a genuine NULL key, never a rowid.
4634fn decode_index_payload(payload: &[u8], enc: TextEncoding) -> Result<Vec<Value>, Error> {
4635 decode_record_inner(payload, enc, None)
4636}
4637
4638/// Decode a SQLite record payload (header + serial array + body) into its column
4639/// values. `rowid_alias` supplies the rowid for a table record's column-0
4640/// `INTEGER PRIMARY KEY` alias (serial 0 → the rowid); `None` (index records)
4641/// leaves a serial-0 column as NULL.
4642fn decode_record_inner(
4643 payload: &[u8],
4644 enc: TextEncoding,
4645 rowid_alias: Option<i64>,
4646) -> Result<Vec<Value>, Error> {
4647 let (header_len, n) = read_varint(payload, 0)?;
4648 let header_len = header_len as usize;
4649 if header_len > payload.len() {
4650 return Err(Error::TruncatedCell);
4651 }
4652 // Pass 1: read serial types from the record header.
4653 let mut serials = Vec::new();
4654 let mut hpos = n;
4655 while hpos < header_len {
4656 let (s, used) = read_varint(payload, hpos)?;
4657 serials.push(s);
4658 hpos += used;
4659 }
4660 // Pass 2: read the body, one value per serial type.
4661 let mut values = Vec::with_capacity(serials.len());
4662 let mut bpos = header_len;
4663 for (idx, &serial) in serials.iter().enumerate() {
4664 let (val, size) = decode_value(payload, bpos, serial, enc)?;
4665 let val = match (idx, serial, rowid_alias) {
4666 // INTEGER PRIMARY KEY alias: NULL in column 0 reads the rowid.
4667 (0, 0, Some(rowid)) => Value::Integer(rowid),
4668 _ => val,
4669 };
4670 values.push(val);
4671 bpos += size;
4672 }
4673 Ok(values)
4674}
4675
4676/// Decode a single value of the given serial type at `off`. Returns the value
4677/// and the number of body bytes it consumed.
4678fn decode_value(
4679 buf: &[u8],
4680 off: usize,
4681 serial: i64,
4682 enc: TextEncoding,
4683) -> Result<(Value, usize), Error> {
4684 Ok(match serial {
4685 // 0 = NULL; 10/11 are reserved for internal use and surfaced as NULL.
4686 0 | 10 | 11 => (Value::Null, 0),
4687 1 => (
4688 Value::Integer(i64::from(read_be_u64(buf, off, 1)? as i8)),
4689 1,
4690 ),
4691 2 => (
4692 Value::Integer(i64::from(read_be_u64(buf, off, 2)? as i16)),
4693 2,
4694 ),
4695 3 => (Value::Integer(sign_extend(read_be_u64(buf, off, 3)?, 3)), 3),
4696 4 => (
4697 Value::Integer(i64::from(read_be_u64(buf, off, 4)? as i32)),
4698 4,
4699 ),
4700 5 => (Value::Integer(sign_extend(read_be_u64(buf, off, 6)?, 6)), 6),
4701 6 => (Value::Integer(read_be_u64(buf, off, 8)? as i64), 8),
4702 7 => {
4703 let bits = read_be_u64(buf, off, 8)?;
4704 (Value::Real(f64::from_bits(bits)), 8)
4705 }
4706 8 => (Value::Integer(0), 0),
4707 9 => (Value::Integer(1), 0),
4708 n if n >= 12 && n % 2 == 0 => {
4709 let len = ((n - 12) / 2) as usize;
4710 let bytes = span(buf, off, len)?;
4711 (Value::Blob(bytes.to_vec()), len)
4712 }
4713 // odd, >= 13: text, decoded per the database's text encoding
4714 // (UTF-8 / UTF-16LE / UTF-16BE). Lossy so a corrupt byte can't panic.
4715 //
4716 // The `>= 13` guard is what keeps the subtraction below in range. It was
4717 // previously a catch-all `n =>`, which also swallowed every NEGATIVE
4718 // serial type — and a serial type is a varint, so a damaged record
4719 // produces those. `i64::MIN - 13` underflows outright, and `-1` yields
4720 // `((-1 - 13) / 2) as usize` = 18446744073709551609.
4721 n if n >= 13 => {
4722 let len = ((n - 13) / 2) as usize;
4723 let bytes = span(buf, off, len)?;
4724 (Value::Text(enc.decode(bytes)), len)
4725 }
4726 // Only negatives reach here: 0..=11 are named above and everything from
4727 // 12 up is claimed by the two arms. A negative serial type identifies no
4728 // value at all, so it is a malformed record rather than a short one —
4729 // reported with the offending value and its offset rather than folded
4730 // into TruncatedCell, which would send a reader looking for a truncation
4731 // that is not there.
4732 n => {
4733 return Err(Error::MalformedSerialType {
4734 serial: n,
4735 offset: off,
4736 })
4737 }
4738 })
4739}
4740
4741/// Take `buf[off..off + len]`, refusing rather than forming the range unchecked.
4742///
4743/// `buf.get(off..off + len)` reads as though the bounds check covers everything,
4744/// and it does not: the range is constructed *before* `get` is given it, so a
4745/// length taken from the evidence overflows the add. Under overflow checks that
4746/// panics; in a release build it wraps to a small number, `get` succeeds, and
4747/// the caller is handed a slice that is not the value it asked for — wrong bytes
4748/// reported as fact, which is worse than the crash.
4749///
4750/// Every span whose length comes from the file goes through here.
4751fn span(buf: &[u8], off: usize, len: usize) -> Result<&[u8], Error> {
4752 let end = off.checked_add(len).ok_or(Error::TruncatedCell)?;
4753 buf.get(off..end).ok_or(Error::TruncatedCell)
4754}
4755
4756/// Read `width` (1..=8) big-endian bytes into a raw u64 (no sign extension).
4757fn read_be_u64(buf: &[u8], off: usize, width: usize) -> Result<u64, Error> {
4758 let bytes = span(buf, off, width)?;
4759 let mut acc: u64 = 0;
4760 for &b in bytes {
4761 acc = (acc << 8) | u64::from(b);
4762 }
4763 Ok(acc)
4764}
4765
4766/// Sign-extend a `width`-byte (3 or 6) value held in the low bits of `raw`.
4767fn sign_extend(raw: u64, width: usize) -> i64 {
4768 let bits = width * 8;
4769 let shift = 64 - bits;
4770 ((raw as i64) << shift) >> shift
4771}
4772
4773/// Read a `SQLite` varint (1..=9 bytes) at `off`. Returns value + bytes consumed.
4774fn read_varint(buf: &[u8], off: usize) -> Result<(i64, usize), Error> {
4775 let mut result: u64 = 0;
4776 for i in 0..8 {
4777 let b = *buf.get(off + i).ok_or(Error::TruncatedCell)?;
4778 result = (result << 7) | u64::from(b & 0x7f);
4779 if b & 0x80 == 0 {
4780 return Ok((result as i64, i + 1));
4781 }
4782 }
4783 // 9th byte contributes all 8 bits.
4784 let b = *buf.get(off + 8).ok_or(Error::TruncatedCell)?;
4785 result = (result << 8) | u64::from(b);
4786 Ok((result as i64, 9))
4787}
4788
4789/// Bounds-checked big-endian u16; out-of-range yields 0 (never panics).
4790fn be_u16(buf: &[u8], off: usize) -> u16 {
4791 let mut b = [0u8; 2];
4792 if let Some(s) = buf.get(off..off + 2) {
4793 b.copy_from_slice(s);
4794 }
4795 u16::from_be_bytes(b)
4796}
4797
4798/// Byte width of the minimal `SQLite` varint encoding of a non-negative `value`
4799/// (task #73, used to re-derive a clobbered record's `header_len`). Mirrors the
4800/// 7-bit big-endian grouping of [`enc_varint_into`]; a value needing more than 8
4801/// groups uses the 9-byte form. Negative inputs (illegal serial types) are
4802/// treated as a single byte and rejected upstream by `serial_body_len`.
4803fn varint_len(value: i64) -> usize {
4804 if value < 0 {
4805 return 1; // cov:unreachable: callers pass only non-negative serials/lengths
4806 }
4807 enc_varint_into(value as usize).len()
4808}
4809
4810/// Minimal `SQLite` varint encoding of a non-negative `value` (task #73). 7-bit
4811/// big-endian groups, high bit set on every group but the last (file-format §2).
4812pub(crate) fn enc_varint_into(value: usize) -> Vec<u8> {
4813 if value == 0 {
4814 return vec![0];
4815 }
4816 let mut groups = Vec::new();
4817 let mut n = value as u64;
4818 while n > 0 {
4819 groups.push((n & 0x7f) as u8);
4820 n >>= 7;
4821 }
4822 groups.reverse();
4823 let last = groups.len() - 1;
4824 for (i, g) in groups.iter_mut().enumerate() {
4825 if i != last {
4826 *g |= 0x80;
4827 }
4828 }
4829 groups
4830}
4831
4832/// The 8-byte rollback-journal segment magic (`pager.c` `aJournalMagic`).
4833const JOURNAL_MAGIC: [u8; 8] = [0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7];
4834
4835/// Hard cap on page records walked in one journal segment, to bound work on a
4836/// crafted/garbage journal whose stride scan would otherwise run the file length.
4837const MAX_JOURNAL_RECORDS: usize = 1_000_000;
4838
4839/// Sector-size candidates probed when reconstructing a zeroed (PERSIST) journal
4840/// header. Real VFS sector sizes exceed 512, so 512 is a candidate, not an
4841/// assumption; the page size is also tried (file-format §"Rollback Journal").
4842const SECTOR_CANDIDATES: [u32; 3] = [512, 4096, 0]; // 0 = "use page_size"
4843
4844/// Parsed (or reconstructed) rollback-journal header (design §5).
4845///
4846/// `Valid` is a header whose magic is intact (Tier A — hot journal / crash
4847/// residue): every parameter, including the checksum `nonce`, is authoritative.
4848/// `ReconstructedZeroed` is the PERSIST post-commit case (Tier B): the first
4849/// sector was zeroed on commit, so the page size comes from the main database
4850/// and the sector size from candidate scoring — the nonce is gone, so page
4851/// checksums cannot be verified.
4852#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4853pub enum JournalHeader {
4854 /// Tier A: header magic present; all fields trusted (`pager.c` offsets).
4855 Valid {
4856 /// Page records declared in this segment (`0xFFFFFFFF`/`0` ⇒ walk to EOF).
4857 n_rec: u32,
4858 /// Database page count at transaction start (`dbOrigSize`).
4859 mx_page: u32,
4860 /// Checksum initializer (`cksumInit`), offset 12.
4861 nonce: u32,
4862 /// VFS sector size the header is padded to.
4863 sector_size: u32,
4864 /// Database page size at transaction start.
4865 page_size: u32,
4866 },
4867 /// Tier B: header zeroed (PERSIST post-commit); parameters reconstructed.
4868 ReconstructedZeroed {
4869 /// Page size taken from the main database header (authoritative).
4870 page_size: u32,
4871 /// Sector size selected by candidate scoring (record offset stride).
4872 sector_size: u32,
4873 },
4874}
4875
4876/// One pre-transaction page image recovered from a rollback journal (design §5).
4877#[derive(Debug, Clone, PartialEq, Eq)]
4878pub struct JournalPageImage {
4879 /// 1-based database page number this image restores.
4880 pub pgno: u32,
4881 /// 0-based segment index this record came from.
4882 pub segment: usize,
4883 /// The original page content (`page_size` bytes).
4884 pub bytes: Vec<u8>,
4885 /// `Some(true/false)` in Tier A (nonce known) — whether the stored checksum
4886 /// matched; `None` in Tier B (nonce zeroed, unverifiable).
4887 pub checksum_valid: Option<bool>,
4888}
4889
4890/// A parsed rollback journal: its header tier plus the ordered, first-wins
4891/// page images (design §3/§5). The temporal inverse of the WAL overlay —
4892/// these images are the database as it was BEFORE the last transaction.
4893#[derive(Debug, Clone, PartialEq, Eq)]
4894pub struct RollbackJournal {
4895 header: JournalHeader,
4896 images: Vec<JournalPageImage>,
4897 /// Page numbers that appeared more than once (first occurrence kept), each
4898 /// listed once in first-seen order. Empty for a well-formed journal.
4899 duplicate_pgnos: Vec<u32>,
4900}
4901
4902/// The journal page checksum (`pager.c` `pager_cksum`): `nonce` plus every-200th
4903/// byte from the tail, starting at `page_size - 200` and stepping down by 200
4904/// while the index is positive, using wrapping u32 arithmetic. It detects torn
4905/// page writes; it is not a cryptographic integrity guarantee.
4906fn journal_cksum(nonce: u32, page: &[u8]) -> u32 {
4907 let mut sum = nonce;
4908 let mut x = page.len() as i64 - 200;
4909 while x > 0 {
4910 // x is in (0, page.len()) by the loop bound, so indexing is in-range.
4911 if let Some(&b) = page.get(x as usize) {
4912 sum = sum.wrapping_add(u32::from(b));
4913 }
4914 x -= 200;
4915 }
4916 sum
4917}
4918
4919/// Walk page records of `page_size` bytes from `start`, with the checksum
4920/// `nonce` (`None` ⇒ Tier B, unverifiable), stopping at EOF or after `limit`
4921/// records. Returns the images in file order; a partial trailing record is
4922/// dropped (truncation tolerance). Bounded by [`MAX_JOURNAL_RECORDS`].
4923fn walk_journal_records(
4924 bytes: &[u8],
4925 start: usize,
4926 page_size: usize,
4927 nonce: Option<u32>,
4928 segment: usize,
4929 limit: usize,
4930) -> Vec<JournalPageImage> {
4931 let stride = 4usize.saturating_add(page_size).saturating_add(4);
4932 let mut out = Vec::new();
4933 let mut off = start;
4934 let cap = limit.min(MAX_JOURNAL_RECORDS);
4935 while out.len() < cap {
4936 let Some(rec) = bytes.get(off..off.saturating_add(stride)) else {
4937 break; // EOF or partial trailing record: stop (truncation tolerant).
4938 };
4939 let pgno = u32::from_be_bytes([rec[0], rec[1], rec[2], rec[3]]);
4940 if pgno == 0 {
4941 break; // page 0 is not a valid record; treat as end-of-segment.
4942 }
4943 let page = &rec[4..4 + page_size];
4944 let stored = u32::from_be_bytes([
4945 rec[4 + page_size],
4946 rec[5 + page_size],
4947 rec[6 + page_size],
4948 rec[7 + page_size],
4949 ]);
4950 let checksum_valid = nonce.map(|n| journal_cksum(n, page) == stored);
4951 out.push(JournalPageImage {
4952 pgno,
4953 segment,
4954 bytes: page.to_vec(),
4955 checksum_valid,
4956 });
4957 off = off.saturating_add(stride);
4958 }
4959 out
4960}
4961
4962/// Score a candidate record walk for the Tier-B sector reconstruction: more
4963/// records and all page numbers within `1..=page_bound` rank higher; a record
4964/// count of zero scores zero so an off-stride candidate never wins.
4965fn score_journal_candidate(images: &[JournalPageImage], page_bound: u32) -> usize {
4966 if images.is_empty() {
4967 return 0;
4968 }
4969 let in_range = images
4970 .iter()
4971 .filter(|i| i.pgno >= 1 && i.pgno <= page_bound)
4972 .count();
4973 // All-in-range walks are strongly preferred; weight the in-range fraction so
4974 // a candidate that mostly decodes to impossible page numbers loses to one
4975 // that decodes cleanly even with fewer records.
4976 if in_range == images.len() {
4977 1000 + images.len()
4978 } else {
4979 in_range
4980 }
4981}
4982
4983impl RollbackJournal {
4984 /// LOWER-LEVEL, UNAUTHENTICATED parse (design §5): interpret `bytes` as a
4985 /// rollback journal given an externally-supplied `page_size`. Does NOT bind
4986 /// the journal to a particular database — prefer [`Database::rollback_prior`],
4987 /// which supplies the authoritative page size from the main db.
4988 ///
4989 /// Tier A (magic present) trusts the header and verifies each checksum. Tier B
4990 /// (magic absent — PERSIST post-commit) reconstructs the sector size by
4991 /// candidate scoring and walks records (checksums unverifiable). Robust: a
4992 /// malformed/truncated journal yields fewer images, never a panic; a page size
4993 /// that is not a power of two in `[512, 65536]` is a typed
4994 /// [`Error::BadJournalPageSize`] carrying the offending value.
4995 pub fn parse(bytes: &[u8], page_size: u32) -> Result<Self, Error> {
4996 if !(512..=65536).contains(&page_size) || !page_size.is_power_of_two() {
4997 return Err(Error::BadJournalPageSize(page_size));
4998 }
4999 let ps = page_size as usize;
5000 let page_bound = u32::try_from(bytes.len() / ps.max(1)).unwrap_or(u32::MAX);
5001
5002 let header_valid = bytes.len() >= 28 && bytes.starts_with(&JOURNAL_MAGIC);
5003 if header_valid {
5004 // Tier A: trust the header.
5005 let n_rec = be_u32(bytes, 8);
5006 let nonce = be_u32(bytes, 12);
5007 let mx_page = be_u32(bytes, 16);
5008 let sector_size = be_u32(bytes, 20);
5009 let hdr_page_size = be_u32(bytes, 24);
5010 // nRec ∈ {0, 0xFFFFFFFF} ⇒ walk to EOF; else exactly n_rec records.
5011 let limit = if n_rec == 0 || n_rec == u32::MAX {
5012 MAX_JOURNAL_RECORDS
5013 } else {
5014 n_rec as usize
5015 };
5016 let start = sector_size.max(1) as usize;
5017 let imgs = walk_journal_records(bytes, start, ps, Some(nonce), 0, limit);
5018 let header = JournalHeader::Valid {
5019 n_rec,
5020 mx_page,
5021 nonce,
5022 sector_size,
5023 // The journal's pages are images of THIS db, so the externally
5024 // supplied page size is authoritative; expose it even if the
5025 // header field disagrees (a tampered/mismatched header field).
5026 page_size: if hdr_page_size == page_size {
5027 hdr_page_size
5028 } else {
5029 page_size
5030 },
5031 };
5032 return Ok(Self::from_walk(header, imgs));
5033 }
5034
5035 // Tier B: header zeroed/absent (PERSIST post-commit). Score sector
5036 // candidates and pick the best; checksums are unverifiable (nonce gone).
5037 let mut best: Option<(usize, u32, Vec<JournalPageImage>)> = None;
5038 for cand in SECTOR_CANDIDATES {
5039 let sector = if cand == 0 { page_size } else { cand };
5040 let imgs =
5041 walk_journal_records(bytes, sector as usize, ps, None, 0, MAX_JOURNAL_RECORDS);
5042 let score = score_journal_candidate(&imgs, page_bound);
5043 // `map_or(true, …)` not `is_none_or` to keep the library MSRV at 1.80
5044 // (`Option::is_none_or` stabilised in 1.82); clippy is MSRV-aware.
5045 let better = best.as_ref().map_or(true, |(bs, _, _)| score > *bs);
5046 if better && score > 0 {
5047 best = Some((score, sector, imgs));
5048 }
5049 }
5050 // No candidate decoded a single in-range record (garbage, or a journal too
5051 // short for one record): an empty Tier-B journal, sector size unknown →
5052 // page size. Degrade gracefully rather than erroring.
5053 let (sector_size, imgs) = best
5054 .map(|(_, s, i)| (s, i))
5055 .unwrap_or((page_size, Vec::new()));
5056 let header = JournalHeader::ReconstructedZeroed {
5057 page_size,
5058 sector_size,
5059 };
5060 Ok(Self::from_walk(header, imgs))
5061 }
5062
5063 /// Apply first-wins dedup to a walked record set, recording whether any
5064 /// `pgno` repeated (the duplicate-page anomaly, design §3).
5065 fn from_walk(header: JournalHeader, walked: Vec<JournalPageImage>) -> Self {
5066 let mut seen = std::collections::BTreeSet::new();
5067 let mut images = Vec::with_capacity(walked.len());
5068 let mut duplicate_pgnos: Vec<u32> = Vec::new();
5069 for img in walked {
5070 if seen.insert(img.pgno) {
5071 images.push(img);
5072 } else if !duplicate_pgnos.contains(&img.pgno) {
5073 // Keep the FIRST occurrence as the truest pre-transaction image;
5074 // record WHICH page repeated (once) rather than a bare flag, so the
5075 // anomaly can name the offending page number.
5076 duplicate_pgnos.push(img.pgno);
5077 }
5078 }
5079 Self {
5080 header,
5081 images,
5082 duplicate_pgnos,
5083 }
5084 }
5085
5086 /// The parsed (or reconstructed) header.
5087 #[must_use]
5088 pub fn header(&self) -> &JournalHeader {
5089 &self.header
5090 }
5091
5092 /// The ordered, first-wins pre-transaction page images.
5093 #[must_use]
5094 pub fn page_images(&self) -> &[JournalPageImage] {
5095 &self.images
5096 }
5097
5098 /// Whether a `pgno` appeared more than once across the parsed segments — the
5099 /// spec says a page is journaled at most once, so a repeat is consistent with
5100 /// corruption, a savepoint/super-journal artifact, or tampering (design §3).
5101 #[must_use]
5102 pub fn has_duplicate_pgno(&self) -> bool {
5103 !self.duplicate_pgnos.is_empty()
5104 }
5105
5106 /// The page numbers that appeared more than once (first occurrence kept), each
5107 /// listed once in first-seen order — the offending values behind
5108 /// [`Self::has_duplicate_pgno`]. Empty for a well-formed journal.
5109 #[must_use]
5110 pub fn duplicate_pgnos(&self) -> &[u32] {
5111 &self.duplicate_pgnos
5112 }
5113}
5114
5115/// A read-only, page-addressable image of the database AS IT WAS BEFORE the last
5116/// transaction (design §4/§5). The temporal inverse of [`CommitSnapshot`]:
5117/// `prior[pgno]` is the rollback-journal image where present, else the live main
5118/// page. Diffing this against the current database yields the last transaction's
5119/// deletions (rowid present here, absent now) and modifications (present in both,
5120/// values differ — the journal carries the OLD value).
5121///
5122/// Returned by [`Database::rollback_prior`] as a DISTINCT type, never a
5123/// [`Database`], so prior/deleted rows can never be read as "live"
5124/// (secure-by-design). Shares ONE b-tree/overflow walk with the live and
5125/// commit-snapshot reads via the internal `PageSource` seam.
5126#[derive(Debug, Clone, PartialEq, Eq)]
5127pub struct PriorSnapshot {
5128 /// The pre-transaction page images: journal-where-present overlaid on the main
5129 /// db. Materializes EVERY valid journal page type (interior, leaf, overflow,
5130 /// page 1, freelist trunk, pointer-map) so a prior table can be walked through
5131 /// its interior pages and overflow chains reassembled.
5132 overlaid: std::collections::BTreeMap<u32, Vec<u8>>,
5133 /// Usable bytes per page, parsed from the prior snapshot's OWN page-1 header
5134 /// (so a reserved-space change in the last txn is honored).
5135 usable: u32,
5136 /// The 1-based page count bound (max overlaid page), for cycle/over-range
5137 /// guards in the b-tree / overflow walk.
5138 page_bound: u32,
5139 /// Whether any journal page image's number exceeded the current main-db page
5140 /// count — diagnostic only (the txn grew the db).
5141 grew_db: bool,
5142}
5143
5144impl PageSource for PriorSnapshot {
5145 fn page(&self, page: u32) -> Option<PageBytes<'_>> {
5146 self.overlaid
5147 .get(&page)
5148 .map(|v| PageBytes::Borrowed(v.as_slice()))
5149 }
5150 fn usable(&self) -> usize {
5151 self.usable as usize
5152 }
5153 fn page_bound(&self) -> u32 {
5154 self.page_bound
5155 }
5156 fn encoding(&self) -> TextEncoding {
5157 // Encoding from the prior snapshot's OWN page-1 header (byte 56), so a
5158 // historical read decodes TEXT per the encoding as of the prior state.
5159 self.overlaid
5160 .get(&1)
5161 .map(|p| match be_u32(p, TEXT_ENCODING_OFFSET) {
5162 2 => TextEncoding::Utf16Le,
5163 3 => TextEncoding::Utf16Be,
5164 _ => TextEncoding::Utf8,
5165 })
5166 .unwrap_or_default()
5167 }
5168}
5169
5170impl PriorSnapshot {
5171 /// The user tables AS OF the prior state, parsed from the snapshot's OWN page 1
5172 /// (the prior `sqlite_master`), NOT the live database — so a DROP/CREATE in the
5173 /// last transaction is interpreted against the prior schema. Best-effort and
5174 /// panic-free: an unreadable page-1 schema yields an empty vector.
5175 #[must_use]
5176 pub fn tables(&self) -> Vec<SnapshotTable> {
5177 let Ok(schema) = read_table_via(self, 1, 5) else {
5178 return Vec::new(); // cov:unreachable: the prior snapshot has a readable page 1
5179 };
5180 let mut out = Vec::new();
5181 for row in schema {
5182 let is_table = matches!(row.values.first(), Some(Value::Text(t)) if t == "table");
5183 if !is_table {
5184 continue;
5185 }
5186 let Some(Value::Text(name)) = row.values.get(1) else {
5187 continue; // cov:unreachable: a 'table' schema row has a TEXT name
5188 };
5189 if name.starts_with("sqlite_") {
5190 continue;
5191 }
5192 let Some(Value::Integer(root)) = row.values.get(3) else {
5193 continue; // cov:unreachable: a 'table' schema row has an integer rootpage
5194 };
5195 let Ok(rootpage) = u32::try_from(*root) else {
5196 continue; // cov:unreachable: a real rootpage is a small positive page number
5197 };
5198 let sql = match row.values.get(4) {
5199 Some(Value::Text(s)) => s.as_str(),
5200 _ => "", // cov:unreachable: a 'table' schema row carries its CREATE TABLE sql
5201 };
5202 let columns = attribution::column_names(sql).unwrap_or_default();
5203 out.push(SnapshotTable {
5204 name: name.clone(),
5205 rootpage,
5206 columns,
5207 without_rowid: without_rowid_sql(sql),
5208 });
5209 }
5210 out
5211 }
5212
5213 /// The PRIOR `sqlite_master` as a `name -> CREATE SQL` map for every **user**
5214 /// table, parsed from the snapshot's OWN page 1 — the prior-schema half of the
5215 /// Detector-B sidecar schema-change comparison
5216 /// (`docs/design/drop-recreate-attribution.md`).
5217 ///
5218 /// The counterpart to [`Database::schema_sql`] read against the pre-transaction
5219 /// state the `-journal` preserves, so a DROP/CREATE/ALTER in the last
5220 /// transaction is interpreted against the prior schema. Best-effort and
5221 /// panic-free: an unreadable prior page-1 schema yields an empty map.
5222 #[must_use]
5223 pub fn schema_sql(&self) -> std::collections::BTreeMap<String, String> {
5224 let mut out = std::collections::BTreeMap::new();
5225 let Ok(schema) = read_table_via(self, 1, 5) else {
5226 return out; // cov:unreachable: the prior snapshot has a readable page 1
5227 };
5228 for row in schema {
5229 schema_sql_insert(&mut out, &row.values);
5230 }
5231 out
5232 }
5233
5234 /// Read every row of the table b-tree rooted at `rootpage` AS OF the prior
5235 /// state, in rowid order, resolving overflow chains through the snapshot's OWN
5236 /// pages. The snapshot-scoped counterpart to [`Database::read_table`]: a typed
5237 /// [`Error`] (never a panic) on a cyclic/over-deep b-tree or overflow chain.
5238 pub fn read_table(
5239 &self,
5240 rootpage: u32,
5241 column_count: usize,
5242 ) -> Result<Vec<(i64, Vec<Value>)>, Error> {
5243 let rows = read_table_via(self, rootpage, column_count)?;
5244 Ok(rows.into_iter().map(|r| (r.rowid, r.values)).collect())
5245 }
5246
5247 /// Whether the last transaction GREW the database (a journal page number
5248 /// exceeded the current main-db page count). Pages beyond the prior size are
5249 /// new — their pre-images were not journaled — which bounds what rolls back.
5250 #[must_use]
5251 pub fn grew_db(&self) -> bool {
5252 self.grew_db
5253 }
5254
5255 /// Read the table rooted at `rootpage` AS OF the prior state, returning each
5256 /// row's rowid, values, AND the 1-based LEAF page it was decoded from — the
5257 /// per-row page provenance the forensic diff attaches to a recovered prior
5258 /// row. Shares `decode_leaf_cell` with the standard read; a typed [`Error`]
5259 /// (never a panic) on a cyclic/over-deep b-tree.
5260 pub fn read_table_with_pages(
5261 &self,
5262 rootpage: u32,
5263 column_count: usize,
5264 ) -> Result<Vec<(i64, Vec<Value>, u32)>, Error> {
5265 let mut out = Vec::new();
5266 let mut seen = std::collections::BTreeSet::new();
5267 walk_table_page_with_leaf(self, rootpage, column_count, &mut out, &mut seen)?;
5268 Ok(out)
5269 }
5270}
5271
5272/// Walk a table b-tree like [`walk_table_page`] but record each row's LEAF page,
5273/// for the rollback-journal per-row provenance. Bounded identically (visited-set
5274/// caps recursion depth; a revisited page is silently skipped).
5275fn walk_table_page_with_leaf(
5276 src: &dyn PageSource,
5277 page: u32,
5278 column_count: usize,
5279 out: &mut Vec<(i64, Vec<Value>, u32)>,
5280 seen: &mut std::collections::BTreeSet<u32>,
5281) -> Result<(), Error> {
5282 if seen.len() > MAX_PAGES_PER_WALK {
5283 return Err(Error::TooManyPages);
5284 }
5285 if !seen.insert(page) {
5286 return Ok(());
5287 }
5288 let slice = src.page(page).ok_or(Error::PageOutOfRange(page))?;
5289 let slice = &*slice;
5290 let hdr_off = if page == 1 { SQLITE_HEADER_SIZE } else { 0 };
5291 let page_type = *slice.get(hdr_off).ok_or(Error::TruncatedCell)?;
5292 let cell_count = be_u16(slice, hdr_off + 3) as usize;
5293 match page_type {
5294 0x0d => {
5295 let cell_ptr_array = hdr_off + 8;
5296 for i in 0..cell_count {
5297 let p = cell_ptr_array + i * 2;
5298 let cell_off = be_u16(slice, p) as usize;
5299 let row = decode_leaf_cell(src, slice, cell_off, column_count)?;
5300 out.push((row.rowid, row.values, page));
5301 }
5302 Ok(())
5303 }
5304 0x05 => {
5305 let cell_ptr_array = hdr_off + 12;
5306 for i in 0..cell_count {
5307 let p = cell_ptr_array + i * 2;
5308 let cell_off = be_u16(slice, p) as usize;
5309 let child = be_u32(slice, cell_off);
5310 walk_table_page_with_leaf(src, child, column_count, out, seen)?;
5311 }
5312 let right = be_u32(slice, hdr_off + 8);
5313 walk_table_page_with_leaf(src, right, column_count, out, seen)
5314 }
5315 other => Err(Error::NotATablePage(other)),
5316 }
5317}
5318
5319/// Bounds-checked big-endian u32; out-of-range yields 0 (never panics).
5320fn be_u32(buf: &[u8], off: usize) -> u32 {
5321 let mut b = [0u8; 4];
5322 if let Some(s) = buf.get(off..off + 4) {
5323 b.copy_from_slice(s);
5324 }
5325 u32::from_be_bytes(b)
5326}
5327
5328#[cfg(test)]
5329mod tests {
5330 use super::*;
5331
5332 /// A serial type is a varint, and a varint in a damaged record can decode to
5333 /// a **negative** i64. Every arm above 11 is written for the positive cases,
5334 /// so a negative one falls through to the catch-all text arm, where
5335 /// `((n - 13) / 2) as usize` wraps: serial `-1` becomes a length of
5336 /// 18446744073709551609.
5337 ///
5338 /// `buf.get(off..off + len)` reads as though the bounds check makes that
5339 /// safe, but the range is built *before* `get` sees it. With overflow checks
5340 /// the add panics; in a release build it wraps to a small number, `get`
5341 /// succeeds, and the caller is handed bytes that are not the value —
5342 /// silently wrong evidence, which is the worse of the two.
5343 #[test]
5344 fn a_negative_serial_type_is_refused_rather_than_wrapping() {
5345 let buf = [0u8; 64];
5346
5347 for serial in [-1_i64, -3, -14, -4096, i64::MIN + 1] {
5348 let result = decode_value(&buf, 0, serial, TextEncoding::Utf8);
5349 assert!(
5350 result.is_err(),
5351 "serial {serial} decoded to {result:?}; a negative serial type \
5352 identifies no value and must be refused, never length-wrapped"
5353 );
5354 }
5355 }
5356
5357 /// The boundary the arms actually turn on, pinned so a fix for the negative
5358 /// case cannot quietly reject the smallest legal blob/text instead.
5359 #[test]
5360 fn the_smallest_legal_blob_and_text_serials_still_decode_empty() {
5361 let buf = [0u8; 8];
5362
5363 let (blob, used) = decode_value(&buf, 0, 12, TextEncoding::Utf8).unwrap();
5364 assert_eq!((blob, used), (Value::Blob(Vec::new()), 0));
5365
5366 let (text, used) = decode_value(&buf, 0, 13, TextEncoding::Utf8).unwrap();
5367 assert_eq!((text, used), (Value::Text(String::new()), 0));
5368 }
5369
5370 fn page_rc(byte: u8) -> std::rc::Rc<[u8]> {
5371 std::rc::Rc::from(vec![byte].into_boxed_slice())
5372 }
5373
5374 /// Encode `v` as a 9-byte SQLite varint (round-trips through `read_varint`).
5375 fn varint9(v: u64) -> [u8; 9] {
5376 let mut out = [0u8; 9];
5377 let top56 = v >> 8;
5378 for (i, b) in out.iter_mut().take(8).enumerate() {
5379 *b = (((top56 >> (7 * (7 - i))) & 0x7f) as u8) | 0x80;
5380 }
5381 out[8] = (v & 0xff) as u8;
5382 out
5383 }
5384
5385 #[test]
5386 fn inferred_carve_does_not_overflow_on_huge_serials() {
5387 // A record whose serial array declares column body lengths summing past
5388 // usize::MAX must be REJECTED, never panic (debug) or wrap (release). Real
5389 // free-space bytes (Belkasoft corpus) hit this; here we craft it minimally:
5390 // five maximal (i64::MAX) serials, each a text/blob length ~(i64::MAX-12)/2.
5391 let big = varint9(i64::MAX as u64); // serial_body_len ~4.6e18; five overflow usize
5392 let n_serials = 5usize;
5393 let header_len = 1 + n_serials * 9; // 1-byte header_len varint + 5 serials
5394 let payload_len = header_len; // reach the body-sum loop before any body exists
5395 let mut buf = Vec::new();
5396 buf.push(payload_len as u8); // payload_len varint (small, 1 byte)
5397 buf.push(1u8); // rowid varint = 1 (positive)
5398 buf.push(header_len as u8); // header_len varint (1 byte, < 128)
5399 for _ in 0..n_serials {
5400 buf.extend_from_slice(&big);
5401 }
5402 // Must return None (rejected), and above all must not panic/overflow.
5403 let got = try_carve_cell_at(&buf, 0, None, TextEncoding::Utf8);
5404 assert!(
5405 got.is_none(),
5406 "a body-length-overflowing record must be rejected"
5407 );
5408 }
5409
5410 #[test]
5411 fn page_cache_hits_reorders_and_evicts_past_cap() {
5412 let mut cache = PageCache::new();
5413 // Fill exactly to CAP, then one more → the oldest (key 0) is evicted.
5414 for i in 0..=PageCache::CAP {
5415 cache.put(i, page_rc(i as u8));
5416 }
5417 assert!(cache.get(0).is_none(), "oldest entry evicted once past CAP");
5418 assert!(
5419 cache.get(PageCache::CAP).is_some(),
5420 "the newest entry is retained (get-hit + touch)"
5421 );
5422 // Re-put an existing key → the already-present branch (touch, no growth).
5423 let before = cache.order.len();
5424 cache.put(PageCache::CAP, page_rc(0xff));
5425 assert_eq!(cache.order.len(), before, "re-put must not grow the order");
5426 assert_eq!(cache.get(PageCache::CAP).as_deref(), Some(&[0xff][..]));
5427 }
5428
5429 #[test]
5430 fn varint_single_byte() {
5431 assert_eq!(read_varint(&[0x05], 0).unwrap(), (5, 1));
5432 }
5433
5434 #[test]
5435 fn varint_two_bytes() {
5436 // 0x81 0x00 => (1<<7) = 128
5437 assert_eq!(read_varint(&[0x81, 0x00], 0).unwrap(), (128, 2));
5438 }
5439
5440 #[test]
5441 fn varint_truncated_is_err() {
5442 assert_eq!(read_varint(&[0x81], 0), Err(Error::TruncatedCell));
5443 }
5444
5445 #[test]
5446 fn sign_extend_three_byte_negative() {
5447 // 0xFFFFFF as 3-byte => -1
5448 assert_eq!(sign_extend(0x00FF_FFFF, 3), -1);
5449 }
5450
5451 #[test]
5452 fn decode_value_text_and_blob() {
5453 let (v, n) = decode_value(b"hi", 0, 17, TextEncoding::Utf8).unwrap(); // 17 => text len (17-13)/2 =2
5454 assert_eq!(v, Value::Text("hi".into()));
5455 assert_eq!(n, 2);
5456 let (v, n) = decode_value(&[0xAA, 0xBB], 0, 16, TextEncoding::Utf8).unwrap(); // 16 => blob len 2
5457 assert_eq!(v, Value::Blob(vec![0xAA, 0xBB]));
5458 assert_eq!(n, 2);
5459 }
5460
5461 #[test]
5462 fn decode_value_text_utf16_le_and_be() {
5463 // The TEXT decode path honors the database encoding (file-format §1.3.1):
5464 // the same code points must round-trip from both byte orders. This drives
5465 // `decode_utf16` deterministically, without depending on an external
5466 // `sqlite3`-minted fixture (the integration tests skip when absent).
5467 // Serial 21 => text byte length (21-13)/2 = 4 = two UTF-16 code units.
5468 let le = [b'h', 0x00, b'i', 0x00];
5469 let (v, n) = decode_value(&le, 0, 21, TextEncoding::Utf16Le).unwrap();
5470 assert_eq!(v, Value::Text("hi".into()));
5471 assert_eq!(n, 4);
5472 let be = [0x00, b'h', 0x00, b'i'];
5473 let (v, n) = decode_value(&be, 0, 21, TextEncoding::Utf16Be).unwrap();
5474 assert_eq!(v, Value::Text("hi".into()));
5475 assert_eq!(n, 4);
5476 }
5477
5478 #[test]
5479 fn localstorage_decodes_known_utf16le_bytes() {
5480 // Independent oracle: these UTF-16-LE bytes are derived from the Unicode
5481 // code points and the surrogate-pair formula, NOT from Rust's encoder, so
5482 // a matching round-trip validates the decoder against the documented
5483 // construction (Evidence-Based Rigor tier 2).
5484 // 'A' U+0041 -> 41 00
5485 // '中' U+4E2D -> 2D 4E
5486 // '😀' U+1F600 -> surrogate pair D83D DE00 -> 3D D8 00 DE
5487 let bytes = [0x41, 0x00, 0x2D, 0x4E, 0x3D, 0xD8, 0x00, 0xDE];
5488 let out = decode_localstorage_value(&bytes);
5489 assert_eq!(out.text, "A中😀");
5490 assert!(!out.lossy, "a fully-paired BLOB is not lossy");
5491 }
5492
5493 #[test]
5494 fn localstorage_empty_blob_is_empty_not_lossy() {
5495 let out = decode_localstorage_value(&[]);
5496 assert_eq!(out.text, "");
5497 assert!(!out.lossy);
5498 }
5499
5500 #[test]
5501 fn localstorage_odd_length_blob_is_lossy_not_panic() {
5502 // 'A' (41 00) then a lone trailing byte 42 — half a code unit was cut off.
5503 let out = decode_localstorage_value(&[0x41, 0x00, 0x42]);
5504 assert_eq!(out.text, "A");
5505 assert!(out.lossy, "a trailing half code unit is a lossy truncation");
5506 }
5507
5508 #[test]
5509 fn localstorage_lone_surrogate_is_replacement_and_lossy() {
5510 // High surrogate D83D (LE 3D D8) with no following low surrogate.
5511 let out = decode_localstorage_value(&[0x3D, 0xD8]);
5512 assert_eq!(out.text, "\u{FFFD}");
5513 assert!(out.lossy);
5514 }
5515
5516 #[test]
5517 fn item_table_schema_recognized_and_others_rejected() {
5518 assert!(is_local_storage_item_table("ItemTable"));
5519 assert!(!is_local_storage_item_table("moz_places"));
5520 assert!(!is_local_storage_item_table("itemtable"));
5521 assert!(!is_local_storage_item_table(""));
5522 }
5523
5524 #[test]
5525 fn decode_value_int_literals() {
5526 assert_eq!(
5527 decode_value(&[], 0, 8, TextEncoding::Utf8).unwrap(),
5528 (Value::Integer(0), 0)
5529 );
5530 assert_eq!(
5531 decode_value(&[], 0, 9, TextEncoding::Utf8).unwrap(),
5532 (Value::Integer(1), 0)
5533 );
5534 }
5535
5536 #[test]
5537 fn bad_magic_rejected() {
5538 let mut b = vec![0u8; 100];
5539 b[..16].copy_from_slice(b"NOT SQLITE 3\0\0\0\0");
5540 assert_eq!(parse_header(&b), Err(Error::BadMagic));
5541 }
5542
5543 #[test]
5544 fn too_short_rejected() {
5545 assert_eq!(parse_header(&[0u8; 10]), Err(Error::TooShort));
5546 }
5547
5548 /// The deleted-record carving fixture (see `docs/corpus-catalog.md`).
5549 const DELETED_DB: &[u8] = include_bytes!("../../tests/data/deleted_places.db");
5550 /// A clean DB with one live `moz_places` table and no deletions.
5551 const CLEAN_DB: &[u8] = include_bytes!("../../tests/data/places.db");
5552
5553 #[test]
5554 fn free_regions_is_complement_of_live_extents() {
5555 // Live cells [10,20) and [30,40) within content area [5, 50).
5556 let live = [(10, 20), (30, 40)];
5557 let regions = free_regions(&live, 5, 50);
5558 assert_eq!(regions, vec![(5, 10), (20, 30), (40, 50)]);
5559 // No live cells -> the whole span is free.
5560 assert_eq!(free_regions(&[], 5, 50), vec![(5, 50)]);
5561 // Live cell covering the whole span -> no free region.
5562 assert!(free_regions(&[(0, 100)], 5, 50).is_empty());
5563 }
5564
5565 #[test]
5566 fn live_cell_len_reads_on_page_footprint() {
5567 // Cell: payload_len=3 (varint 0x03), rowid=1 (varint 0x01), 3 payload bytes.
5568 let buf = [0x03, 0x01, 0xAA, 0xBB, 0xCC];
5569 let usable = 4096;
5570 assert_eq!(live_cell_len(&buf, 0, usable), Some(1 + 1 + 3));
5571 // Truncated prefix -> None, never panics.
5572 assert_eq!(live_cell_len(&[0x81], 0, usable), None);
5573 }
5574
5575 #[test]
5576 fn carve_free_regions_recovers_in_page_remnant() {
5577 let db = Database::open(DELETED_DB.to_vec()).unwrap();
5578 // Page 8 is an allocated leaf (live ids 181..=200) whose free gap holds
5579 // deleted-row residue including rowid 237.
5580 let page = db.raw_page(8).unwrap();
5581 let carved = db.carve_free_regions(&page, 6);
5582 assert!(carved.iter().any(|c| c.rowid == 237));
5583 // 0-FP: never a live (id<=200) rowid.
5584 assert!(carved.iter().all(|c| c.rowid > 200));
5585 // A non-leaf page yields nothing.
5586 assert!(db.carve_free_regions(&[0x05u8; 4096], 6).is_empty());
5587 // An empty / too-short slice yields nothing (no panic).
5588 assert!(db.carve_free_regions(&[], 6).is_empty());
5589 }
5590
5591 #[test]
5592 fn carve_leaf_cells_reads_allocated_cells_and_rejects_non_leaf() {
5593 let db = Database::open(DELETED_DB.to_vec()).unwrap();
5594 // Page 8 is an allocated table-leaf (live ids 181..=200); carve_leaf_cells
5595 // decodes every cell the page records as allocated, so the live ids appear
5596 // (unlike carve_free_regions, which excludes them).
5597 let page = db.raw_page(8).unwrap();
5598 let cells = db.carve_leaf_cells(&page);
5599 assert!(
5600 cells.iter().any(|c| c.rowid == 181),
5601 "must read the allocated cells of the leaf"
5602 );
5603 // Page 1 is passed whole (starts with the file magic) → header read at 100.
5604 let _ = db.carve_leaf_cells(&db.raw_page(1).unwrap());
5605 // A non-leaf page (interior 0x05) and an empty/too-short slice yield nothing
5606 // (no panic) — the same defensive arms carve_free_regions guards.
5607 assert!(db.carve_leaf_cells(&[0x05u8; 4096]).is_empty());
5608 assert!(db.carve_leaf_cells(&[]).is_empty());
5609 }
5610
5611 #[test]
5612 fn carve_free_regions_handles_page_one_and_inferred() {
5613 let db = Database::open(DELETED_DB.to_vec()).unwrap();
5614 // Page 1 is passed whole (starts with the file magic) -> the b-tree header
5615 // is read at offset 100, exercising the page-1 branch.
5616 let page1 = db.raw_page(1).unwrap();
5617 let _ = db.carve_free_regions(&page1, 6);
5618 // With column_count_hint = 0, the inferred path runs over the free regions.
5619 let page8 = db.raw_page(8).unwrap();
5620 let inferred = db.carve_free_regions(&page8, 0);
5621 assert!(inferred.iter().any(|c| c.rowid == 237));
5622 }
5623
5624 #[test]
5625 fn live_cell_len_accounts_for_overflow_pointer() {
5626 let usable = 4096usize;
5627 // Non-spilling cell: payload_len small -> footprint = prefix + payload.
5628 // varint 0x03 (payload_len=3), 0x01 (rowid=1), 3 payload bytes.
5629 assert_eq!(live_cell_len(&[0x03, 0x01, 0, 0, 0], 0, usable), Some(5));
5630
5631 // Spilling cell: a payload_len far above the local threshold takes the
5632 // overflow branch -> footprint = prefix + local + 4 (overflow pointer).
5633 // Encode payload_len = 5000 as a 2-byte varint (0xA7 0x08), rowid = 1.
5634 let mut buf = vec![0xA7, 0x08, 0x01];
5635 buf.extend(std::iter::repeat_n(0u8, 5000));
5636 let total = 5000usize;
5637 let local = local_payload_len(total, usable);
5638 assert!(local < total, "this payload must spill");
5639 assert_eq!(live_cell_len(&buf, 0, usable), Some(2 + 1 + local + 4));
5640 }
5641
5642 #[test]
5643 fn carve_cells_inferred_matches_fixed_count() {
5644 let db = Database::open(DELETED_DB.to_vec()).unwrap();
5645 // A freed leaf page body carves the same rows whether the column count is
5646 // fixed at 6 or inferred.
5647 let page = db.raw_page(10).unwrap();
5648 let fixed = db.carve_cells(&page, 6);
5649 let inferred = db.carve_cells_inferred(&page);
5650 assert!(!fixed.is_empty());
5651 let fixed_ids: std::collections::BTreeSet<i64> = fixed.iter().map(|c| c.rowid).collect();
5652 let inf_ids: std::collections::BTreeSet<i64> = inferred.iter().map(|c| c.rowid).collect();
5653 assert!(fixed_ids.is_subset(&inf_ids));
5654 }
5655
5656 #[test]
5657 fn has_user_table_distinguishes_live_and_dropped() {
5658 let live = Database::open(CLEAN_DB.to_vec()).unwrap();
5659 assert!(live.has_user_table());
5660 let with_deletions = Database::open(DELETED_DB.to_vec()).unwrap();
5661 assert!(with_deletions.has_user_table());
5662 }
5663
5664 #[test]
5665 fn live_rowids_collects_live_rows_only() {
5666 let db = Database::open(CLEAN_DB.to_vec()).unwrap();
5667 let ids = db.live_rowids();
5668 // places.db has 5 live rows, rowids 1..=5.
5669 assert_eq!(ids.len(), 5);
5670 assert!(ids.contains(&1) && ids.contains(&5));
5671
5672 // On the deletions fixture, live rowids are 1..=200; none of the deleted
5673 // 201..=400 appear.
5674 let del = Database::open(DELETED_DB.to_vec()).unwrap();
5675 let live = del.live_rowids();
5676 assert!(live.contains(&1) && live.contains(&200));
5677 assert!(!live.contains(&201) && !live.contains(&400));
5678 }
5679
5680 #[test]
5681 fn live_rows_decodes_current_values() {
5682 let db = Database::open(CLEAN_DB.to_vec()).unwrap();
5683 let rows = db.live_rows();
5684 // places.db has 5 live rows keyed by rowid 1..=5, each decoded to values.
5685 assert_eq!(rows.len(), 5);
5686 // Row 1's url column (index 1) is the rust-lang URL (cross-checks that
5687 // values are decoded, not just rowids collected).
5688 let r1 = rows.get(&1).expect("row 1 present");
5689 assert!(
5690 matches!(r1.get(1), Some(Value::Text(t)) if t.contains("rust-lang")),
5691 "row 1 values must be decoded: {r1:?}"
5692 );
5693 // The value map and the rowid set agree on which rows are live.
5694 let ids = db.live_rowids();
5695 assert_eq!(
5696 rows.keys().copied().collect::<Vec<_>>(),
5697 ids.into_iter().collect::<Vec<_>>()
5698 );
5699
5700 // The deletions fixture's table b-tree has an INTERIOR root page (0x05),
5701 // so this exercises the interior-walk branch of collect_rows and confirms
5702 // values are decoded for all 200 live rows.
5703 let del = Database::open(DELETED_DB.to_vec()).unwrap();
5704 let del_rows = del.live_rows();
5705 assert_eq!(del_rows.len(), 200);
5706 let r1 = del_rows.get(&1).expect("live row 1");
5707 assert!(
5708 matches!(r1.get(1), Some(Value::Text(t)) if t.contains("site-1.example")),
5709 "interior-walked live row 1 must decode its url: {r1:?}"
5710 );
5711 }
5712
5713 #[test]
5714 fn live_table_rows_dumps_each_user_table_in_rowid_order() {
5715 let db = Database::open(CLEAN_DB.to_vec()).unwrap();
5716 let dumps = db.live_table_rows();
5717 // places.db has exactly one user table (moz_places); sqlite_* excluded.
5718 assert_eq!(dumps.len(), 1, "one user-table dump expected: {dumps:?}");
5719 let t = &dumps[0];
5720 assert_eq!(t.name, "moz_places");
5721 // Real column names come from the CREATE TABLE, not generic c0..cN.
5722 assert!(
5723 t.column_names.iter().any(|c| c == "url"),
5724 "real column names expected: {:?}",
5725 t.column_names
5726 );
5727 // The rowids must be the live set, in ascending order.
5728 let rowids: Vec<i64> = t.rows.iter().map(|r| r.rowid).collect();
5729 assert_eq!(rowids, vec![1, 2, 3, 4, 5], "rowid order: {rowids:?}");
5730 // The url cell of row 1 decodes (cross-check values are real).
5731 assert!(
5732 matches!(t.rows[0].values.get(1), Some(Value::Text(s)) if s.contains("rust-lang")),
5733 "row 1 url must decode: {:?}",
5734 t.rows[0].values
5735 );
5736 }
5737
5738 #[test]
5739 fn live_table_rows_excludes_internal_tables_and_handles_interior_btree() {
5740 // The deletions fixture has an INTERIOR root page; all 200 live rows dump
5741 // in ascending rowid order, and no sqlite_* table appears.
5742 let db = Database::open(DELETED_DB.to_vec()).unwrap();
5743 let dumps = db.live_table_rows();
5744 assert!(
5745 dumps.iter().all(|t| !t.name.starts_with("sqlite_")),
5746 "internal tables excluded: {:?}",
5747 dumps.iter().map(|t| &t.name).collect::<Vec<_>>()
5748 );
5749 let places = dumps
5750 .iter()
5751 .find(|t| t.name == "moz_places")
5752 .expect("moz_places dump");
5753 assert_eq!(places.rows.len(), 200, "all live rows dumped");
5754 let ids: Vec<i64> = places.rows.iter().map(|r| r.rowid).collect();
5755 assert!(
5756 ids.windows(2).all(|w| w[0] < w[1]),
5757 "rows in ascending rowid order"
5758 );
5759 assert_eq!(*ids.first().unwrap(), 1);
5760 assert_eq!(*ids.last().unwrap(), 200);
5761 }
5762
5763 #[test]
5764 fn live_table_rows_falls_back_to_generic_columns_on_unparseable_schema() {
5765 // Robustness: a damaged CREATE TABLE whose column list cannot be parsed
5766 // must dump the table with generic c0..cN columns (never a fabricated
5767 // real header), while its rows still read. Mint a valid db, then blank out
5768 // the `( ... )` column list in the stored schema SQL in place (same byte
5769 // length), so column_defs yields None for that table.
5770 use crate::rebuild::{build_recovered_db_tables, RecoveredTable as RT};
5771 let seed = vec![RT {
5772 name: "people".to_string(),
5773 columns: vec!["id".to_string(), "name".to_string()],
5774 rows: vec![vec![Value::Integer(1), Value::Text("alice".into())]],
5775 }];
5776 let mut bytes = build_recovered_db_tables(&seed);
5777
5778 // Find the stored `CREATE TABLE "people" (...)` text and overwrite from the
5779 // first '(' through the matching ')' with spaces, leaving `CREATE TABLE
5780 // "people"` (no column list) — unparseable to column_defs.
5781 let needle = b"CREATE TABLE \"people\"";
5782 let start = bytes
5783 .windows(needle.len())
5784 .position(|w| w == needle)
5785 .expect("schema SQL present");
5786 let open = bytes[start..]
5787 .iter()
5788 .position(|&b| b == b'(')
5789 .map(|p| start + p)
5790 .expect("column list open paren");
5791 let close = bytes[open..]
5792 .iter()
5793 .position(|&b| b == b')')
5794 .map(|p| open + p)
5795 .expect("column list close paren");
5796 for b in &mut bytes[open..=close] {
5797 *b = b' ';
5798 }
5799
5800 let db = Database::open(bytes).expect("corrupted-schema db still opens");
5801 let dumps = db.live_table_rows();
5802 let people = dumps
5803 .iter()
5804 .find(|t| t.name == "people")
5805 .expect("people dump present");
5806 // Generic columns sized to the row width (2), never the real id/name.
5807 assert_eq!(
5808 people.column_names,
5809 vec!["c0".to_string(), "c1".to_string()]
5810 );
5811 // The row still decoded despite the schema damage.
5812 assert_eq!(people.rows.len(), 1);
5813 assert_eq!(people.rows[0].values.first(), Some(&Value::Integer(1)));
5814 }
5815
5816 /// Real-corpus freeblock reconstruction: 0C-01 page 2 has six freeblock-head
5817 /// cells the forward parser cannot reach; reconstruction recovers them
5818 /// (including the destroyed-rowid `id` column) from the surviving serial tail.
5819 const NEMETZ_0C_01: &[u8] = include_bytes!("../../tests/data/nemetz/0C/0C-01.db");
5820
5821 #[test]
5822 fn reconstruct_freeblock_records_recovers_clobbered_rows() {
5823 let db = Database::open(NEMETZ_0C_01.to_vec()).unwrap();
5824 let page = db.raw_page(2).unwrap();
5825 let recovered = db.reconstruct_freeblock_records(&page);
5826 // Row 20005 is a freeblock-head cell only reconstruction can recover.
5827 assert!(recovered.iter().any(|c| c.values
5828 == vec![
5829 Value::Integer(20005),
5830 Value::Integer(3_780_322_152),
5831 Value::Integer(3_909_007_646),
5832 Value::Integer(120_462_986),
5833 Value::Integer(1_290_558_629),
5834 ]));
5835 assert!(recovered
5836 .iter()
5837 .all(|c| c.rowid == 0 && c.confidence <= 0.5));
5838 }
5839
5840 /// Real-corpus span-walking reconstruction (task #66): 0D-07 page 3 coalesces
5841 /// three deleted cells into a single freeblock `[0xf79,0xfe0)` —
5842 /// `Luca|Schumacher` (the head), then `Kurt|Schubert`, then `Georg|Schulz`,
5843 /// each prefixed by a stale `00 00 00 NN` freeblock header that clobbers its
5844 /// leading four bytes. A single-shot head reconstruction recovers only the
5845 /// first; walking the template across the whole span recovers all three.
5846 const NEMETZ_0D_07: &[u8] = include_bytes!("../../tests/data/nemetz/0D/0D-07.db");
5847
5848 #[test]
5849 fn reconstruct_freeblock_records_walks_coalesced_cells() {
5850 let db = Database::open(NEMETZ_0D_07.to_vec()).unwrap();
5851 let page = db.raw_page(3).unwrap();
5852 let recovered = db.reconstruct_freeblock_records(&page);
5853 let has = |name: &str, surname: &str| {
5854 recovered.iter().any(|c| {
5855 matches!(c.values.get(1), Some(Value::Text(t)) if t == name)
5856 && matches!(c.values.get(2), Some(Value::Text(t)) if t == surname)
5857 })
5858 };
5859 // The span-head cell a single-shot reconstruction already reached.
5860 assert!(has("Luca", "Schumacher"), "head cell must be recovered");
5861 // The two trailing cells deeper inside the same freeblock — only a
5862 // span-walk reaches these.
5863 assert!(
5864 has("Kurt", "Schubert"),
5865 "second coalesced cell must be recovered"
5866 );
5867 assert!(
5868 has("Georg", "Schulz"),
5869 "third coalesced cell must be recovered"
5870 );
5871 // Every reconstruction carries a destroyed rowid and low confidence.
5872 assert!(recovered
5873 .iter()
5874 .all(|c| c.rowid == 0 && c.confidence <= 0.5));
5875 }
5876
5877 /// Helper: a real opened DB to call the page-slice methods against crafted
5878 /// page byte slices (the methods take `page_bytes` explicitly).
5879 fn opened() -> Database {
5880 Database::open(NEMETZ_0C_01.to_vec()).unwrap()
5881 }
5882
5883 /// A leaf page advertising a freeblock chain but whose cells do not parse
5884 /// yields no template, so reconstruction returns empty (covers the
5885 /// `freeblock_template` rejection arms and the final `None`).
5886 #[test]
5887 fn reconstruct_freeblock_records_without_template_is_empty() {
5888 let db = opened();
5889 let mut page = vec![0u8; 256];
5890 page[0] = 0x0d; // table-leaf
5891 page[1] = 0x00;
5892 page[2] = 0x40; // first freeblock at offset 64
5893 page[3] = 0x00;
5894 page[4] = 0x01; // cell_count = 1
5895 // The single cell pointer (offset 8) points at 0 -> cell_off == 0 -> skipped,
5896 // so no template can be derived.
5897 page[8] = 0x00;
5898 page[9] = 0x00;
5899 // A freeblock at 64: next=0, size=8 (in-bounds), but no template anyway.
5900 page[64] = 0x00;
5901 page[65] = 0x00;
5902 page[66] = 0x00;
5903 page[67] = 0x08;
5904 assert!(db.reconstruct_freeblock_records(&page).is_empty());
5905 }
5906
5907 /// A cyclic freeblock `next` chain terminates (covers the cycle-break guard)
5908 /// and a freeblock whose size runs past the page is skipped — all without a
5909 /// panic.
5910 #[test]
5911 fn reconstruct_freeblock_records_breaks_cyclic_chain() {
5912 let db = opened();
5913 // Build a page WITH a usable template by copying 0C-01 page 2's header +
5914 // first live cell, then point the freeblock chain at itself.
5915 let src = db.raw_page(2).unwrap().to_vec();
5916 let mut page = src.clone();
5917 // Repoint first-freeblock to a self-cycle at offset 100: next -> 100.
5918 page[1] = 0x00;
5919 page[2] = 100;
5920 page[100] = 0x00;
5921 page[101] = 100; // next = 100 (points to itself)
5922 page[102] = 0xff;
5923 page[103] = 0xff; // size huge -> runs past page -> skipped
5924 // Must not panic and must terminate.
5925 let _ = db.reconstruct_freeblock_records(&page);
5926 }
5927
5928 // ---- Tier-2 fragment salvage (task #72) --------------------------------
5929
5930 #[test]
5931 fn is_distinctive_classifies_every_storage_class() {
5932 // TEXT >= 4 UTF-8 bytes and REAL are distinctive; everything else is not.
5933 assert!(is_distinctive(&Value::Text("Anja".into())));
5934 assert!(is_distinctive(&Value::Text("\u{00e4}\u{00f6}".into()))); // 4 UTF-8 bytes
5935 assert!(is_distinctive(&Value::Real(3.5)));
5936 assert!(!is_distinctive(&Value::Text("abc".into()))); // 3 bytes
5937 assert!(!is_distinctive(&Value::Text(String::new())));
5938 assert!(!is_distinctive(&Value::Text("ab\u{fffd}x".into()))); // replacement char
5939 assert!(!is_distinctive(&Value::Integer(20004)));
5940 assert!(!is_distinctive(&Value::Null));
5941 assert!(!is_distinctive(&Value::Blob(vec![1, 2, 3, 4, 5])));
5942 }
5943
5944 /// Build a synthetic 256-byte table-leaf (0x0d) page for the fragment tests.
5945 ///
5946 /// Schema implied by the template live cell: 3 columns
5947 /// `(c0: 1-byte int, c1: TEXT-4, c2: TEXT-4)` → serials `[1, 21, 21]`,
5948 /// `header_len = 4`. The live cell (the freeblock template source) is placed
5949 /// at `live_off`. A single freeblock spanning `[fb, fb + fb_size)` holds the
5950 /// freed-cell payload `freed`, whose leading 4 bytes are the stale freeblock
5951 /// header (`next`, `size`) — exactly what freeblock conversion clobbers.
5952 fn synth_frag_page(live_off: usize, fb: usize, fb_size: usize, freed: &[u8]) -> Vec<u8> {
5953 let mut page = vec![0u8; 256];
5954 page[0] = 0x0d; // table-leaf
5955 page[1] = (fb >> 8) as u8;
5956 page[2] = (fb & 0xff) as u8;
5957 page[3] = 0x00;
5958 page[4] = 0x01; // cell_count = 1
5959 page[5] = (live_off >> 8) as u8;
5960 page[6] = (live_off & 0xff) as u8; // cellContentArea = live_off
5961 page[8] = (live_off >> 8) as u8;
5962 page[9] = (live_off & 0xff) as u8; // cell pointer -> live_off
5963
5964 // Live template cell: payload_len=13, rowid=5, header_len=4, serials
5965 // [int1, text4, text4], body 1+4+4.
5966 let live = [
5967 13u8, 5u8, 0x04, 0x01, 0x15, 0x15, 0x09, b'L', b'i', b'v', b'e', b'R', b'o', b'w', b'!',
5968 ];
5969 page[live_off..live_off + live.len()].copy_from_slice(&live);
5970
5971 // Lay the freed-cell bytes first, then stamp the stale freeblock header
5972 // (next=0, size=fb_size) over its first 4 bytes — exactly what freeblock
5973 // conversion does (the header clobbers the freed cell's leading 4 bytes).
5974 page[fb..fb + freed.len()].copy_from_slice(freed);
5975 page[fb] = 0x00;
5976 page[fb + 1] = 0x00;
5977 page[fb + 2] = (fb_size >> 8) as u8;
5978 page[fb + 3] = (fb_size & 0xff) as u8;
5979 page
5980 }
5981
5982 /// (a) Truncated tail: the freed cell's body overruns the freeblock span, so
5983 /// full reconstruction fails — salvage emits the decodable column prefix
5984 /// (incl. a distinctive TEXT cell) with correct `missing`/confidence, while
5985 /// `reconstruct_freeblock_records` recovers nothing from that anchor.
5986 #[test]
5987 fn fragment_salvage_truncated_tail() {
5988 let db = opened();
5989 // surviving serials [21,21] at fb+4,fb+5; body c0(1)+c1(4)+c2(4) at fb+6.
5990 // A full record needs fb+15. Span size 12 ends at fb+12: c0,c1 fit, c2
5991 // overruns → salvage keeps [c0, c1].
5992 let mut freed = vec![0u8; 16];
5993 freed[4] = 0x15;
5994 freed[5] = 0x15;
5995 freed[6] = 0x07;
5996 freed[7..11].copy_from_slice(b"Anja");
5997 freed[11..15].copy_from_slice(b"Frnk");
5998 let page = synth_frag_page(96, 64, 12, &freed);
5999
6000 let frags = db.reconstruct_freeblock_fragments(&page);
6001 assert_eq!(frags.len(), 1, "exactly one fragment salvaged");
6002 let f = &frags[0];
6003 assert_eq!(f.offset, 64);
6004 assert_eq!(
6005 f.surviving,
6006 vec![(0, Value::Integer(7)), (1, Value::Text("Anja".into()))]
6007 );
6008 assert_eq!(f.missing, 1, "c2 did not decode");
6009 assert!((f.confidence - 0.2).abs() < f32::EPSILON);
6010 let cells = db.reconstruct_freeblock_records(&page);
6011 // The page's only freeblock anchor is the truncated one at offset 64, and
6012 // full reconstruction recovers nothing from it — so the full-record set is
6013 // empty. Asserting emptiness is the precise, deterministic intent.
6014 assert!(
6015 cells.is_empty(),
6016 "the truncated anchor yields no full record, got {}",
6017 cells.len()
6018 );
6019 }
6020
6021 /// (b) A surviving column whose body cannot fit ends the prefix early —
6022 /// salvage keeps the columns decoded before the failure.
6023 #[test]
6024 fn fragment_salvage_partial_tail() {
6025 let db = opened();
6026 let mut freed = vec![0u8; 16];
6027 freed[4] = 0x15;
6028 freed[5] = 0x15;
6029 freed[6] = 0x07;
6030 freed[7..11].copy_from_slice(b"Lena");
6031 let page = synth_frag_page(96, 64, 11, &freed); // c1 fits, c2 overruns
6032 let frags = db.reconstruct_freeblock_fragments(&page);
6033 assert_eq!(frags.len(), 1);
6034 assert_eq!(
6035 frags[0].surviving,
6036 vec![(0, Value::Integer(7)), (1, Value::Text("Lena".into()))]
6037 );
6038 }
6039
6040 /// (c) A fully reconstructable freeblock yields NO fragment (mutual exclusion).
6041 #[test]
6042 fn fragment_salvage_full_record_yields_no_fragment() {
6043 let db = opened();
6044 let mut freed = vec![0u8; 16];
6045 freed[4] = 0x15;
6046 freed[5] = 0x15;
6047 freed[6] = 0x07;
6048 freed[7..11].copy_from_slice(b"Whol");
6049 freed[11..15].copy_from_slice(b"Erow");
6050 let page = synth_frag_page(96, 64, 15, &freed);
6051 let cells = db.reconstruct_freeblock_records(&page);
6052 assert!(
6053 cells.iter().any(|c| c.offset == 64),
6054 "full record recovered"
6055 );
6056 assert!(
6057 db.reconstruct_freeblock_fragments(&page).is_empty(),
6058 "no fragment when the full record is recoverable"
6059 );
6060 }
6061
6062 /// (d) Salvage yielding only non-distinctive (INTEGER) cells emits NO fragment.
6063 #[test]
6064 fn fragment_salvage_integer_only_is_rejected() {
6065 let db = opened();
6066 let mut freed = vec![0u8; 12];
6067 freed[4] = 0x01; // surviving 1-byte int
6068 freed[5] = 0x01; // surviving 1-byte int
6069 freed[6] = 0x07;
6070 freed[7] = 0x08;
6071 let page = synth_frag_page(96, 64, 8, &freed); // c2 overruns; only ints decode
6072 assert!(
6073 db.reconstruct_freeblock_fragments(&page).is_empty(),
6074 "integer-only prefix is not distinctive — no fragment"
6075 );
6076 }
6077
6078 /// (e) Fragment salvage does NOT extend the span walk: a failed head stops
6079 /// the walk, emitting at most one fragment, never sliding forward.
6080 #[test]
6081 fn fragment_salvage_does_not_extend_walk() {
6082 let db = opened();
6083 let mut freed = vec![0u8; 16];
6084 freed[4] = 0x15;
6085 freed[5] = 0x15;
6086 freed[6] = 0x07;
6087 freed[7..11].copy_from_slice(b"Stop");
6088 freed[11..15].copy_from_slice(b"Here");
6089 let page = synth_frag_page(96, 64, 12, &freed);
6090 assert_eq!(db.reconstruct_freeblock_fragments(&page).len(), 1);
6091 }
6092
6093 /// (Step 2) Real-artifact validation: 0D-01 page 2 salvages the genuine
6094 /// partial deleted row for id 20004 — `Text("Anja")`/`Text("Frank")` survive
6095 /// in a freeblock whose full-row reconstruction fails. Full pass unchanged.
6096 const NEMETZ_0D_01: &[u8] = include_bytes!("../../tests/data/nemetz/0D/0D-01.db");
6097
6098 #[test]
6099 fn fragment_salvage_recovers_anja_on_0d01() {
6100 let db = Database::open(NEMETZ_0D_01.to_vec()).unwrap();
6101 let page = db.raw_page(2).unwrap();
6102 let frags = db.reconstruct_freeblock_fragments(&page);
6103 let f = frags
6104 .iter()
6105 .find(|f| {
6106 f.surviving
6107 .iter()
6108 .any(|(_, v)| matches!(v, Value::Text(t) if t == "Anja"))
6109 })
6110 .expect("0D-01 page 2 must salvage the Anja fragment");
6111 assert!(f
6112 .surviving
6113 .iter()
6114 .any(|(_, v)| matches!(v, Value::Text(t) if t == "Frank")));
6115 assert!((f.confidence - 0.2).abs() < f32::EPSILON);
6116 let cells = db.reconstruct_freeblock_records(&page);
6117 assert!(cells.iter().all(|c| !c
6118 .values
6119 .iter()
6120 .any(|v| matches!(v, Value::Text(t) if t == "Anja"))));
6121 }
6122
6123 // ---- task #73: chain-aware overflow recovery — spilled-cell recognition ----
6124
6125 /// Encode a SQLite varint (minimal big-endian 7-bit groups).
6126 fn enc_varint(mut n: u64) -> Vec<u8> {
6127 if n == 0 {
6128 return vec![0];
6129 }
6130 let mut groups = Vec::new();
6131 while n > 0 {
6132 groups.push((n & 0x7f) as u8);
6133 n >>= 7;
6134 }
6135 groups.reverse();
6136 let last = groups.len() - 1;
6137 for (i, g) in groups.iter_mut().enumerate() {
6138 if i != last {
6139 *g |= 0x80;
6140 }
6141 }
6142 groups
6143 }
6144
6145 /// Build the **local prefix** bytes of a freed spilled table-leaf cell:
6146 /// `payload_len varint, rowid varint, record header, local payload bytes,
6147 /// 4-byte big-endian first-overflow pointer`. Returns `(bytes, P, local,
6148 /// serials)`. The record is `(id INTEGER, name TEXT, code TEXT)` with `code`
6149 /// large enough to force a spill past `usable - 35`.
6150 fn synth_spilled_prefix(
6151 rowid: i64,
6152 id: i64,
6153 name: &str,
6154 code_len: usize,
6155 usable: usize,
6156 first_overflow: u32,
6157 ) -> (Vec<u8>, usize, usize, Vec<i64>) {
6158 let id_serial = 1i64; // 1-byte integer
6159 let name_serial = 13 + 2 * name.len() as i64; // TEXT
6160 let code_serial = 13 + 2 * code_len as i64; // TEXT
6161 let serials = vec![id_serial, name_serial, code_serial];
6162 let mut serial_bytes = Vec::new();
6163 for &s in &serials {
6164 serial_bytes.extend(enc_varint(s as u64));
6165 }
6166 // header_len varint counts itself — solve the fixed point.
6167 let mut header_len = serial_bytes.len() + 1;
6168 while enc_varint(header_len as u64).len() + serial_bytes.len() != header_len {
6169 header_len += 1;
6170 }
6171 let mut header = enc_varint(header_len as u64);
6172 header.extend(&serial_bytes);
6173 let body_len = 1 + name.len() + code_len;
6174 let payload_len = header.len() + body_len;
6175 let local = local_payload_len(payload_len, usable);
6176
6177 // Full payload = header ++ id-body ++ name-body ++ code-body.
6178 let mut payload = header.clone();
6179 payload.push(id as u8); // 1-byte id
6180 payload.extend(name.as_bytes());
6181 payload.extend(std::iter::repeat_n(b'C', code_len));
6182 assert_eq!(payload.len(), payload_len);
6183
6184 // Cell = prefix varints ++ local payload prefix ++ 4-byte overflow ptr.
6185 let mut cell = enc_varint(payload_len as u64);
6186 cell.extend(enc_varint(rowid as u64));
6187 cell.extend(&payload[..local]);
6188 cell.extend(first_overflow.to_be_bytes());
6189 (cell, payload_len, local, serials)
6190 }
6191
6192 #[test]
6193 fn spilled_recognizer_reads_intact_prefix() {
6194 let usable = 4096usize;
6195 let (cell, p, local, serials) = synth_spilled_prefix(20012, 42, "Ella", 4200, usable, 13);
6196 assert!(p > usable - 35, "this record must spill");
6197 // Place the cell inside a larger scanned slice at a nonzero offset.
6198 let off = 50usize;
6199 let mut buf = vec![0u8; off];
6200 buf.extend(&cell);
6201 let sc = try_carve_spilled_cell_at(&buf, off, usable, Some(3))
6202 .expect("must recognize the intact-prefix spilled cell");
6203 assert_eq!(sc.payload_len, p);
6204 assert_eq!(sc.local_len, local);
6205 assert_eq!(sc.rowid, 20012);
6206 assert_eq!(sc.first_overflow, 13);
6207 assert_eq!(sc.serials, serials);
6208 assert_eq!(sc.offset, off);
6209 }
6210
6211 #[test]
6212 fn spilled_recognizer_abstains_for_in_page_payload() {
6213 let usable = 4096usize;
6214 // A small (in-page) payload: the existing carve path owns it.
6215 // header (3 serials) + body for a tiny code -> P <= usable-35.
6216 let (cell, p, _local, _s) = synth_spilled_prefix(7, 1, "Bob", 10, usable, 9);
6217 assert!(p <= usable - 35, "this record must NOT spill");
6218 assert!(try_carve_spilled_cell_at(&cell, 0, usable, Some(3)).is_none());
6219 }
6220
6221 #[test]
6222 fn spilled_recognizer_abstains_on_truncated_pointer() {
6223 let usable = 4096usize;
6224 let (cell, _p, _local, _s) = synth_spilled_prefix(20012, 42, "Ella", 4200, usable, 13);
6225 // Drop the final 2 bytes so the 4-byte overflow pointer is out of bounds.
6226 let truncated = &cell[..cell.len() - 2];
6227 assert!(try_carve_spilled_cell_at(truncated, 0, usable, Some(3)).is_none());
6228 }
6229
6230 #[test]
6231 fn spilled_recognizer_abstains_on_column_mismatch() {
6232 let usable = 4096usize;
6233 let (cell, _p, _local, _s) = synth_spilled_prefix(20012, 42, "Ella", 4200, usable, 13);
6234 // Expect 5 columns but the record has 3.
6235 assert!(try_carve_spilled_cell_at(&cell, 0, usable, Some(5)).is_none());
6236 // Inferred (None) still recognizes it.
6237 assert!(try_carve_spilled_cell_at(&cell, 0, usable, None).is_some());
6238 }
6239
6240 #[test]
6241 fn spilled_recognizer_abstains_on_nonpositive_rowid() {
6242 let usable = 4096usize;
6243 let (cell, _p, _local, _s) = synth_spilled_prefix(0, 42, "Ella", 4000, usable, 13);
6244 assert!(try_carve_spilled_cell_at(&cell, 0, usable, Some(3)).is_none());
6245 }
6246
6247 // ---- task #73: freed overflow-chain walk + freelist leaf/trunk split ----
6248
6249 /// Build a minimal multi-page `SQLite` DB image with `page_count` pages of
6250 /// `page_size` bytes. Page 1 carries a valid 100-byte header (so
6251 /// `Database::open` succeeds) with the given freelist trunk pointer and count
6252 /// at offsets 32/36. All pages are zero-filled; the caller writes overflow /
6253 /// trunk content afterwards. Returns the byte vector.
6254 fn synth_db(page_size: usize, page_count: usize, trunk: u32, fl_count: u32) -> Vec<u8> {
6255 let mut b = vec![0u8; page_size * page_count];
6256 b[..16].copy_from_slice(SQLITE_MAGIC);
6257 b[16..18].copy_from_slice(&(page_size as u16).to_be_bytes());
6258 b[18] = 1; // file format write version
6259 b[19] = 1; // file format read version
6260 b[20] = 0; // reserved space
6261 b[21] = 64;
6262 b[22] = 32;
6263 b[23] = 32;
6264 b[32..36].copy_from_slice(&trunk.to_be_bytes());
6265 b[36..40].copy_from_slice(&fl_count.to_be_bytes());
6266 // A minimal table-leaf page-1 body (type 0x0d, 0 cells) so header parsing
6267 // and page-count helpers behave.
6268 b[100] = 0x0d;
6269 b
6270 }
6271
6272 /// Write a freelist trunk page at `page` listing `leaves` and chaining to
6273 /// `next_trunk` (0 = end).
6274 fn write_trunk(b: &mut [u8], page_size: usize, page: u32, next_trunk: u32, leaves: &[u32]) {
6275 let base = (page as usize - 1) * page_size;
6276 b[base..base + 4].copy_from_slice(&next_trunk.to_be_bytes());
6277 b[base + 4..base + 8].copy_from_slice(&(leaves.len() as u32).to_be_bytes());
6278 for (i, &lf) in leaves.iter().enumerate() {
6279 b[base + 8 + i * 4..base + 12 + i * 4].copy_from_slice(&lf.to_be_bytes());
6280 }
6281 }
6282
6283 /// Write an overflow page at `page`: 4-byte big-endian `next` then `content`.
6284 fn write_overflow(b: &mut [u8], page_size: usize, page: u32, next: u32, content: &[u8]) {
6285 let base = (page as usize - 1) * page_size;
6286 b[base..base + 4].copy_from_slice(&next.to_be_bytes());
6287 b[base + 4..base + 4 + content.len()].copy_from_slice(content);
6288 }
6289
6290 #[test]
6291 fn freelist_split_separates_leaves_and_trunks() {
6292 let ps = 512usize;
6293 // Pages: 1 header, 2 trunk, leaves 3,4,5.
6294 let mut b = synth_db(ps, 6, 2, 4);
6295 write_trunk(&mut b, ps, 2, 0, &[3, 4, 5]);
6296 let db = Database::open(b).unwrap();
6297 let (leaves, trunks) = db.freelist_pages_split().unwrap();
6298 assert_eq!(leaves, [3u32, 4, 5].into_iter().collect());
6299 assert_eq!(trunks, [2u32].into_iter().collect());
6300 // The legacy combined accessor still returns leaves ++ trunk.
6301 let all: std::collections::BTreeSet<u32> =
6302 db.freelist_pages().unwrap().into_iter().collect();
6303 assert_eq!(all, [2u32, 3, 4, 5].into_iter().collect());
6304 }
6305
6306 #[test]
6307 fn freed_chain_assembles_single_leaf_page() {
6308 let ps = 512usize;
6309 let usable = ps; // reserved 0
6310 let mut b = synth_db(ps, 6, 2, 4);
6311 write_trunk(&mut b, ps, 2, 0, &[3, 4, 5]);
6312 // Chain content on leaf page 3: a single page holds `remaining` bytes.
6313 let remaining = 100usize;
6314 let content: Vec<u8> = (0..remaining).map(|i| (i % 251) as u8).collect();
6315 write_overflow(&mut b, ps, 3, 0, &content);
6316 let db = Database::open(b).unwrap();
6317 let (leaves, _trunks) = db.freelist_pages_split().unwrap();
6318 let (bytes, chain) = db
6319 .read_freed_overflow_chain(3, remaining, usable, &leaves)
6320 .expect("intact single-leaf chain must assemble");
6321 assert_eq!(bytes, content);
6322 assert_eq!(chain, vec![3]);
6323 }
6324
6325 #[test]
6326 fn freed_chain_assembles_multi_leaf_pages() {
6327 let ps = 512usize;
6328 let usable = ps;
6329 let per_page = usable - 4;
6330 let mut b = synth_db(ps, 8, 2, 5);
6331 write_trunk(&mut b, ps, 2, 0, &[3, 4, 5, 6]);
6332 // 2-page chain: page 3 -> page 4. remaining spans into page 4.
6333 let remaining = per_page + 50;
6334 let content: Vec<u8> = (0..remaining).map(|i| (i % 251) as u8).collect();
6335 write_overflow(&mut b, ps, 3, 4, &content[..per_page]);
6336 write_overflow(&mut b, ps, 4, 0, &content[per_page..]);
6337 let db = Database::open(b).unwrap();
6338 let (leaves, _t) = db.freelist_pages_split().unwrap();
6339 let (bytes, chain) = db
6340 .read_freed_overflow_chain(3, remaining, usable, &leaves)
6341 .expect("intact 2-leaf chain must assemble");
6342 assert_eq!(bytes, content);
6343 assert_eq!(chain, vec![3, 4]);
6344 }
6345
6346 #[test]
6347 fn freed_chain_breaks_on_non_freelist_page() {
6348 let ps = 512usize;
6349 let usable = ps;
6350 let mut b = synth_db(ps, 6, 2, 2);
6351 write_trunk(&mut b, ps, 2, 0, &[3]); // only page 3 is a leaf
6352 let content = vec![7u8; 100];
6353 // The pointer targets page 4, which is NOT on the freelist.
6354 write_overflow(&mut b, ps, 4, 0, &content);
6355 let db = Database::open(b).unwrap();
6356 let (leaves, _t) = db.freelist_pages_split().unwrap();
6357 assert!(db
6358 .read_freed_overflow_chain(4, 100, usable, &leaves)
6359 .is_err());
6360 }
6361
6362 #[test]
6363 fn freed_chain_breaks_on_trunk_page() {
6364 let ps = 512usize;
6365 let usable = ps;
6366 let mut b = synth_db(ps, 6, 2, 2);
6367 write_trunk(&mut b, ps, 2, 0, &[3]);
6368 let db = Database::open(b).unwrap();
6369 let (leaves, _t) = db.freelist_pages_split().unwrap();
6370 // Page 2 is the trunk — a chain page that is a trunk must break.
6371 assert!(db
6372 .read_freed_overflow_chain(2, 100, usable, &leaves)
6373 .is_err());
6374 }
6375
6376 #[test]
6377 fn freed_chain_breaks_on_cycle() {
6378 let ps = 512usize;
6379 let usable = ps;
6380 let per_page = usable - 4;
6381 let mut b = synth_db(ps, 6, 2, 3);
6382 write_trunk(&mut b, ps, 2, 0, &[3, 4]);
6383 // 3 -> 4 -> 3 cycle; remaining never satisfied.
6384 write_overflow(&mut b, ps, 3, 4, &vec![1u8; per_page]);
6385 write_overflow(&mut b, ps, 4, 3, &vec![2u8; per_page]);
6386 let db = Database::open(b).unwrap();
6387 let (leaves, _t) = db.freelist_pages_split().unwrap();
6388 assert!(db
6389 .read_freed_overflow_chain(3, per_page * 10, usable, &leaves)
6390 .is_err());
6391 }
6392
6393 #[test]
6394 fn freed_chain_breaks_on_premature_zero_pointer() {
6395 let ps = 512usize;
6396 let usable = ps;
6397 let per_page = usable - 4;
6398 let mut b = synth_db(ps, 6, 2, 2);
6399 write_trunk(&mut b, ps, 2, 0, &[3]);
6400 // Page 3 ends the chain (next=0) but `remaining` still wants more bytes.
6401 write_overflow(&mut b, ps, 3, 0, &vec![9u8; per_page]);
6402 let db = Database::open(b).unwrap();
6403 let (leaves, _t) = db.freelist_pages_split().unwrap();
6404 assert!(db
6405 .read_freed_overflow_chain(3, per_page + 10, usable, &leaves)
6406 .is_err());
6407 }
6408
6409 #[test]
6410 fn freed_chain_breaks_on_capacity_overflow() {
6411 let ps = 512usize;
6412 let usable = ps;
6413 let mut b = synth_db(ps, 6, 2, 2);
6414 write_trunk(&mut b, ps, 2, 0, &[3]);
6415 write_overflow(&mut b, ps, 3, 0, &vec![1u8; usable - 4]);
6416 let db = Database::open(b).unwrap();
6417 let (leaves, _t) = db.freelist_pages_split().unwrap();
6418 // remaining far exceeds what one leaf page can deliver — rejected upfront,
6419 // never allocating an attacker-declared payload.
6420 let absurd = (usable - 4) * leaves.len() + 1;
6421 assert!(db
6422 .read_freed_overflow_chain(3, absurd, usable, &leaves)
6423 .is_err());
6424 }
6425
6426 // ---- task #73 step 5: freeblock-clobbered spilled cell (SYNTHETIC ONLY) ----
6427 // Codex ruling #5: there is NO corpus instance for a freeblock-clobbered
6428 // *spilled* cell — this path is validated against a synthetic fixture only
6429 // and is marked unproven-by-corpus in the production code + docs.
6430
6431 /// Build a synthetic 4096-byte-page DB with an allocated table-leaf page 2
6432 /// holding (a) a LIVE template cell of the `(id INTEGER 1-byte, name TEXT,
6433 /// code TEXT)` schema and (b) a freeblock-clobbered SPILLED cell whose 4-byte
6434 /// prefix is overwritten by a stale freeblock header, with its overflow chain
6435 /// on a freed leaf page. Returns the bytes. `break_chain` routes the chain
6436 /// pointer at the freelist trunk instead of a leaf to exercise the rejection.
6437 fn synth_clobbered_spill_db(break_chain: bool) -> Vec<u8> {
6438 let ps = 4096usize;
6439 let usable = ps;
6440 // Pages: 1 header, 2 allocated leaf, 3 trunk, 4 leaf (chain), 5 leaf spare.
6441 let mut b = synth_db(ps, 6, 3, 2);
6442 write_trunk(&mut b, ps, 3, 0, &[4, 5]);
6443
6444 // Record geometry: id=7 (1-byte), name="Zoe", code 4200×'C'.
6445 let name = b"Zoe";
6446 let code_len = 4200usize;
6447 let serials: [i64; 3] = [1, 13 + 2 * name.len() as i64, 13 + 2 * code_len as i64];
6448 let mut serial_bytes = Vec::new();
6449 for &s in &serials {
6450 serial_bytes.extend(enc_varint(s as u64));
6451 }
6452 let mut header_len = serial_bytes.len() + 1;
6453 while enc_varint(header_len as u64).len() + serial_bytes.len() != header_len {
6454 header_len += 1;
6455 }
6456 let mut header = enc_varint(header_len as u64);
6457 header.extend(&serial_bytes);
6458 let mut full_payload = header.clone();
6459 full_payload.push(7u8); // id body
6460 full_payload.extend(name);
6461 full_payload.extend(std::iter::repeat_n(b'C', code_len));
6462 let payload_len = full_payload.len();
6463 let local = local_payload_len(payload_len, usable);
6464 let remaining = payload_len - local;
6465
6466 // --- LIVE template cell at offset 200 on page 2 (a small non-spilling row
6467 // of the SAME schema so freeblock_template derives the column layout).
6468 let base2 = ps; // page 2 starts at byte 4096
6469 let tmpl_name = b"Al";
6470 let tmpl_code = b"xy";
6471 let tser: [i64; 3] = [
6472 1,
6473 13 + 2 * tmpl_name.len() as i64,
6474 13 + 2 * tmpl_code.len() as i64,
6475 ];
6476 let mut tsb = Vec::new();
6477 for &s in &tser {
6478 tsb.extend(enc_varint(s as u64));
6479 }
6480 let mut thl = tsb.len() + 1;
6481 while enc_varint(thl as u64).len() + tsb.len() != thl {
6482 thl += 1;
6483 }
6484 let mut tpayload = enc_varint(thl as u64);
6485 tpayload.extend(&tsb);
6486 tpayload.push(1u8);
6487 tpayload.extend(tmpl_name);
6488 tpayload.extend(tmpl_code);
6489 let live_off = 200usize;
6490 let mut live_cell = enc_varint(tpayload.len() as u64);
6491 live_cell.extend(enc_varint(1u64)); // rowid 1
6492 live_cell.extend(&tpayload);
6493 b[base2 + live_off..base2 + live_off + live_cell.len()].copy_from_slice(&live_cell);
6494
6495 // Page-2 leaf header (type 0x0d), 1 live cell, freeblock at 0x100, content
6496 // area covering both the live cell and the clobbered spilled cell.
6497 b[base2] = 0x0d;
6498 // first freeblock pointer (offset 1) -> the clobbered spilled cell at 1000.
6499 b[base2 + 1..base2 + 3].copy_from_slice(&1000u16.to_be_bytes());
6500 // cell count (offset 3) = 1
6501 b[base2 + 3..base2 + 5].copy_from_slice(&1u16.to_be_bytes());
6502 // cell content area start (offset 5) — low so both regions are "content".
6503 b[base2 + 5..base2 + 7].copy_from_slice(&100u16.to_be_bytes());
6504 // cell pointer array (1 entry) at offset 8 -> live cell offset.
6505 b[base2 + 8..base2 + 10].copy_from_slice(&(live_off as u16).to_be_bytes());
6506
6507 // --- Clobbered SPILLED cell at offset 1000 on page 2. Lay down the FULL
6508 // prefix (payload_len varint, rowid varint, header, local payload,
6509 // overflow ptr), then OVERWRITE the first 4 bytes with a stale
6510 // freeblock header (next=0x0000, size) to simulate freeblock clobber.
6511 let spill_off = 1000usize;
6512 let mut spill_cell = enc_varint(payload_len as u64);
6513 spill_cell.extend(enc_varint(1u64)); // rowid (will be clobbered)
6514 let prefix_len = spill_cell.len();
6515 spill_cell.extend(&full_payload[..local]);
6516 let chain_first = if break_chain { 3u32 } else { 4u32 };
6517 spill_cell.extend(chain_first.to_be_bytes());
6518 b[base2 + spill_off..base2 + spill_off + spill_cell.len()].copy_from_slice(&spill_cell);
6519 // Clobber the first 4 bytes with a freeblock header: next=0, size=4.
6520 b[base2 + spill_off] = 0;
6521 b[base2 + spill_off + 1] = 0;
6522 b[base2 + spill_off + 2..base2 + spill_off + 4].copy_from_slice(&4u16.to_be_bytes());
6523
6524 // --- The overflow chain content on freed leaf page 4 (next=0).
6525 write_overflow(&mut b, ps, 4, 0, &full_payload[local..local + remaining]);
6526
6527 let _ = prefix_len;
6528 b
6529 }
6530
6531 #[test]
6532 fn clobbered_spilled_cell_reconstructs_with_unknown_rowid() {
6533 let db = Database::open(synth_clobbered_spill_db(false)).unwrap();
6534 let page2 = db.raw_page(2).unwrap();
6535 let recovered = db.carve_overflow_template_records(&page2);
6536 let (cell, chain) = recovered
6537 .iter()
6538 .find(|(c, _)| matches!(c.values.get(1), Some(Value::Text(t)) if t == "Zoe"))
6539 .expect("synthetic clobbered spilled cell must reconstruct");
6540 // rowid destroyed by the freeblock clobber -> surfaced as 0.
6541 assert_eq!(cell.rowid, 0);
6542 // code fully reassembled across the chain.
6543 assert!(matches!(cell.values.get(2), Some(Value::Text(t)) if t.len() == 4200));
6544 assert_eq!(chain, &vec![4u32]);
6545 }
6546
6547 #[test]
6548 fn clobbered_spilled_broken_chain_yields_no_full_row() {
6549 // Chain pointer routed at the freelist TRUNK (page 3) -> rejected.
6550 let db = Database::open(synth_clobbered_spill_db(true)).unwrap();
6551 let page2 = db.raw_page(2).unwrap();
6552 let recovered = db.carve_overflow_template_records(&page2);
6553 // A chain routed through the freelist trunk is rejected outright, so the
6554 // template carve recovers no full row at all (not merely no "Zoe" row).
6555 assert!(
6556 recovered.is_empty(),
6557 "a trunk-routed broken chain must yield no full row, got {} rows",
6558 recovered.len()
6559 );
6560 }
6561
6562 #[test]
6563 fn enc_varint_into_round_trips_zero_and_multibyte() {
6564 // Zero -> single 0 byte (the NULL-serial / empty-header path).
6565 assert_eq!(enc_varint_into(0), vec![0]);
6566 assert_eq!(varint_len(0), 1);
6567 // Multi-byte: 8413 -> 2-byte varint; round-trips via read_varint.
6568 let v = enc_varint_into(8413);
6569 assert_eq!(varint_len(8413), v.len());
6570 assert_eq!(read_varint(&v, 0).unwrap(), (8413, v.len()));
6571 // Negative input (illegal serial) treated as 1 byte (defensive).
6572 assert_eq!(varint_len(-1), 1);
6573 }
6574
6575 /// Build a 4096-byte-page DB with an allocated table-leaf page 2 holding an
6576 /// **intact-prefix** spilled cell in its unallocated gap, with the overflow
6577 /// chain on a freed leaf page (page 4). Mirrors the real 0E geometry so
6578 /// `carve_overflow_records` (and its fragment dual) can be unit-covered without
6579 /// the corpus. `break_chain` routes the pointer at the freelist trunk.
6580 fn synth_gap_spill_db(break_chain: bool, code_len: usize, name: &str) -> Vec<u8> {
6581 let ps = 4096usize;
6582 let usable = ps;
6583 let mut b = synth_db(ps, 6, 3, 2);
6584 write_trunk(&mut b, ps, 3, 0, &[4, 5]);
6585 let base2 = ps;
6586
6587 // Record: (id INTEGER 1-byte, name TEXT, code TEXT) spilled.
6588 let serials: [i64; 3] = [1, 13 + 2 * name.len() as i64, 13 + 2 * code_len as i64];
6589 let mut serial_bytes = Vec::new();
6590 for &s in &serials {
6591 serial_bytes.extend(enc_varint(s as u64));
6592 }
6593 let mut header_len = serial_bytes.len() + 1;
6594 while enc_varint(header_len as u64).len() + serial_bytes.len() != header_len {
6595 header_len += 1;
6596 }
6597 let mut payload = enc_varint(header_len as u64);
6598 payload.extend(&serial_bytes);
6599 payload.push(9u8); // id body
6600 payload.extend(name.as_bytes());
6601 payload.extend(std::iter::repeat_n(b'C', code_len));
6602 let payload_len = payload.len();
6603 let local = local_payload_len(payload_len, usable);
6604 let remaining = payload_len - local;
6605
6606 // Spilled cell at gap offset 1500 on page 2 (intact prefix).
6607 let spill_off = 1500usize;
6608 let mut cell = enc_varint(payload_len as u64);
6609 cell.extend(enc_varint(5u64)); // rowid 5
6610 cell.extend(&payload[..local]);
6611 let first = if break_chain { 3u32 } else { 4u32 };
6612 cell.extend(first.to_be_bytes());
6613 b[base2 + spill_off..base2 + spill_off + cell.len()].copy_from_slice(&cell);
6614
6615 // Page-2 leaf header: 0 live cells, content area at 100 so the gap [8,100..]
6616 // is scanned. No live cells keeps free_regions = the whole content area.
6617 b[base2] = 0x0d;
6618 b[base2 + 1] = 0; // first freeblock = 0
6619 b[base2 + 2] = 0;
6620 b[base2 + 3..base2 + 5].copy_from_slice(&0u16.to_be_bytes()); // 0 cells
6621 b[base2 + 5..base2 + 7].copy_from_slice(&8u16.to_be_bytes()); // cca low
6622
6623 // Chain content on freed leaf page 4.
6624 write_overflow(&mut b, ps, 4, 0, &payload[local..local + remaining]);
6625 b
6626 }
6627
6628 #[test]
6629 fn carve_overflow_records_resolves_gap_spill() {
6630 let db = Database::open(synth_gap_spill_db(false, 4200, "Nora")).unwrap();
6631 let page2 = db.raw_page(2).unwrap();
6632 let recovered = db.carve_overflow_records(&page2);
6633 let (cell, chain) = recovered
6634 .iter()
6635 .find(|(c, _)| matches!(c.values.get(1), Some(Value::Text(t)) if t == "Nora"))
6636 .expect("gap-resident spilled cell must resolve to a full row");
6637 assert_eq!(cell.rowid, 5);
6638 assert!(matches!(cell.values.get(2), Some(Value::Text(t)) if t.len() == 4200));
6639 assert_eq!(chain, &vec![4u32]);
6640 // Graded below the in-page full-row tier (0.9 * factor).
6641 assert!(cell.confidence < 0.72);
6642 // Non-leaf page yields nothing; empty slice yields nothing.
6643 assert!(db.carve_overflow_records(&[0x05u8; 4096]).is_empty());
6644 assert!(db.carve_overflow_records(&[]).is_empty());
6645 }
6646
6647 #[test]
6648 fn carve_overflow_records_rejects_trunk_chain() {
6649 let db = Database::open(synth_gap_spill_db(true, 4200, "Nora")).unwrap();
6650 let page2 = db.raw_page(2).unwrap();
6651 // Chain routed at the trunk -> no full row recovered at all.
6652 let recovered = db.carve_overflow_records(&page2);
6653 assert!(
6654 recovered.is_empty(),
6655 "a trunk-routed chain must yield no full overflow row, got {} rows",
6656 recovered.len()
6657 );
6658 }
6659
6660 #[test]
6661 fn stale_leaf_chain_with_invalid_utf8_is_rejected() {
6662 // NEGATIVE test (the stale-leaf residual): a chain page that IS a freelist
6663 // leaf and assembles to the exact declared length, but whose content is
6664 // unrelated bytes (invalid UTF-8 in the TEXT column). The freelist-leaf
6665 // requirement passes; the strict-UTF-8 extra-signal gate rejects it from
6666 // Tier-1. This documents the design's limit (Codex ruling #2): the leaf
6667 // requirement cannot prove the bytes are the record — only the UTF-8 gate
6668 // catches the cases the lossy decoder would otherwise mask.
6669 let ps = 4096usize;
6670 let usable = ps;
6671 let mut b = synth_db(ps, 6, 3, 2);
6672 write_trunk(&mut b, ps, 3, 0, &[4, 5]);
6673 let base2 = ps;
6674 let name = "Stale";
6675 let code_len = 4200usize;
6676 let serials: [i64; 3] = [1, 13 + 2 * name.len() as i64, 13 + 2 * code_len as i64];
6677 let mut serial_bytes = Vec::new();
6678 for &s in &serials {
6679 serial_bytes.extend(enc_varint(s as u64));
6680 }
6681 let mut header_len = serial_bytes.len() + 1;
6682 while enc_varint(header_len as u64).len() + serial_bytes.len() != header_len {
6683 header_len += 1;
6684 }
6685 let mut payload = enc_varint(header_len as u64);
6686 payload.extend(&serial_bytes);
6687 payload.push(9u8);
6688 payload.extend(name.as_bytes());
6689 payload.extend(std::iter::repeat_n(b'C', code_len));
6690 let payload_len = payload.len();
6691 let local = local_payload_len(payload_len, usable);
6692 let remaining = payload_len - local;
6693
6694 let spill_off = 1500usize;
6695 let mut cell = enc_varint(payload_len as u64);
6696 cell.extend(enc_varint(5u64));
6697 cell.extend(&payload[..local]);
6698 cell.extend(4u32.to_be_bytes());
6699 b[base2 + spill_off..base2 + spill_off + cell.len()].copy_from_slice(&cell);
6700 b[base2] = 0x0d;
6701 b[base2 + 3..base2 + 5].copy_from_slice(&0u16.to_be_bytes());
6702 b[base2 + 5..base2 + 7].copy_from_slice(&8u16.to_be_bytes());
6703
6704 // Stale leaf content: invalid UTF-8 (0xff bytes) where the TEXT body lands.
6705 let stale = vec![0xffu8; remaining];
6706 write_overflow(&mut b, ps, 4, 0, &stale);
6707
6708 let db = Database::open(b).unwrap();
6709 let page2 = db.raw_page(2).unwrap();
6710 // Decodes mechanically (the leaf assembles exactly), but the strict-UTF-8
6711 // gate rejects it -> NOT a Tier-1 full row.
6712 assert!(db.carve_overflow_records(&page2).is_empty());
6713 }
6714
6715 #[test]
6716 fn carve_overflow_fragments_salvages_broken_gap_spill() {
6717 // Broken chain (trunk) -> the local prefix (id + name) salvages as a fragment.
6718 let db = Database::open(synth_gap_spill_db(true, 4200, "Nora")).unwrap();
6719 let page2 = db.raw_page(2).unwrap();
6720 let frags = db.carve_overflow_fragments(&page2);
6721 let f = frags
6722 .iter()
6723 .find(|f| {
6724 f.surviving
6725 .iter()
6726 .any(|(_, v)| matches!(v, Value::Text(t) if t == "Nora"))
6727 })
6728 .expect("broken-chain gap spill must salvage a fragment");
6729 // id (col 0) survives locally too.
6730 assert!(f
6731 .surviving
6732 .iter()
6733 .any(|(i, v)| *i == 0 && matches!(v, Value::Integer(9))));
6734 // An intact chain produces NO fragment (it is a full row instead), so the
6735 // fragment set is empty — assert that directly rather than over a vacuous
6736 // per-fragment predicate.
6737 let ok = Database::open(synth_gap_spill_db(false, 4200, "Nora")).unwrap();
6738 let ok_page = ok.raw_page(2).unwrap();
6739 assert!(
6740 ok.carve_overflow_fragments(&ok_page).is_empty(),
6741 "an intact chain yields a full row, not a fragment"
6742 );
6743 // Non-leaf / empty inputs yield nothing.
6744 assert!(db.carve_overflow_fragments(&[0x05u8; 4096]).is_empty());
6745 assert!(db.carve_overflow_fragments(&[]).is_empty());
6746 }
6747
6748 // --- WAL frame checksum (file-format §4.2) -------------------------------
6749
6750 #[test]
6751 fn wal_checksum_known_vector_both_endiannesses() {
6752 // The §4.2 algorithm over a hand-constructed 8-byte input, from a zero
6753 // seed. Input is two 32-bit words x0, x1; the recurrence is
6754 // s0 += x0 + s1; s1 += x1 + s0;
6755 // From (s0,s1)=(0,0): s0 = x0; s1 = x1 + x0.
6756 //
6757 // BIG-ENDIAN words (magic 0x377f0683 per the spec): bytes
6758 // [00 00 00 02][00 00 00 03] -> x0=2, x1=3 -> s0=2, s1=5.
6759 let data_be = [0, 0, 0, 2, 0, 0, 0, 3];
6760 assert_eq!(wal_checksum(WalChecksumEndian::Big, 0, 0, &data_be), (2, 5));
6761
6762 // LITTLE-ENDIAN words (magic 0x377f0682): the SAME bytes read LE give
6763 // x0=0x02000000, x1=0x03000000 -> s0=0x02000000,
6764 // s1 = 0x03000000 + 0x02000000 = 0x05000000 (wrapping u32).
6765 assert_eq!(
6766 wal_checksum(WalChecksumEndian::Little, 0, 0, &data_be),
6767 (0x0200_0000, 0x0500_0000)
6768 );
6769
6770 // Seed carries forward: from (s0,s1)=(2,5) over the same BE input ->
6771 // s0 = 2 + (2 + 5) = 9; s1 = 5 + (3 + 9) = 17.
6772 assert_eq!(
6773 wal_checksum(WalChecksumEndian::Big, 2, 5, &data_be),
6774 (9, 17)
6775 );
6776
6777 // Wrapping arithmetic must not panic on overflow (u32 wrap, not i32).
6778 let big = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
6779 let _ = wal_checksum(WalChecksumEndian::Big, u32::MAX, u32::MAX, &big);
6780 }
6781
6782 #[test]
6783 fn wal_checksum_endian_from_magic_matches_spec() {
6784 // file-format §4.2: 0x377f0683 = BIG-endian words, 0x377f0682 = LITTLE.
6785 assert_eq!(
6786 WalChecksumEndian::from_magic(0x377f_0683),
6787 Some(WalChecksumEndian::Big)
6788 );
6789 assert_eq!(
6790 WalChecksumEndian::from_magic(0x377f_0682),
6791 Some(WalChecksumEndian::Little)
6792 );
6793 assert_eq!(WalChecksumEndian::from_magic(0xdead_beef), None);
6794 }
6795
6796 // --- per-commit schema (CommitSnapshot::tables) -------------------------
6797
6798 /// Wrap a minted main-db image into a `(main, wal)` pair whose WAL commits a
6799 /// full rewrite of every page in ONE commit, with correct §4.2 checksums (so
6800 /// the snapshot is checksum-valid). The snapshot then materializes exactly the
6801 /// minted db, with its real page-1 `sqlite_master` b-tree — the no-sqlite3 way
6802 /// to drive `CommitSnapshot::tables` / snapshot reads against a genuine schema.
6803 fn wrap_db_in_wal(main: &[u8], page_size: u32) -> Vec<u8> {
6804 let ps = page_size as usize;
6805 let n_pages = main.len() / ps;
6806 let endian = WalChecksumEndian::Little; // arbitrary; matches magic below.
6807 let (salt1, salt2) = (0x1234_5678u32, 0x9abc_def0u32);
6808
6809 let mut wal = vec![0u8; 32];
6810 wal[0..4].copy_from_slice(&0x377f_0682u32.to_be_bytes()); // little-endian magic
6811 wal[4..8].copy_from_slice(&3_007_000u32.to_be_bytes());
6812 wal[8..12].copy_from_slice(&page_size.to_be_bytes());
6813 wal[12..16].copy_from_slice(&1u32.to_be_bytes());
6814 wal[16..20].copy_from_slice(&salt1.to_be_bytes());
6815 wal[20..24].copy_from_slice(&salt2.to_be_bytes());
6816 // Header checksum over the first 24 bytes (the seed for the frame chain).
6817 let (mut s0, mut s1) = wal_checksum(endian, 0, 0, &wal[0..24]);
6818 wal[24..28].copy_from_slice(&s0.to_be_bytes());
6819 wal[28..32].copy_from_slice(&s1.to_be_bytes());
6820
6821 for i in 0..n_pages {
6822 let page_no = (i + 1) as u32;
6823 let db_size = if i + 1 == n_pages { n_pages as u32 } else { 0 };
6824 let mut fh = [0u8; 24];
6825 fh[0..4].copy_from_slice(&page_no.to_be_bytes());
6826 fh[4..8].copy_from_slice(&db_size.to_be_bytes());
6827 fh[8..12].copy_from_slice(&salt1.to_be_bytes());
6828 fh[12..16].copy_from_slice(&salt2.to_be_bytes());
6829 let data = &main[i * ps..(i + 1) * ps];
6830 let (n0, n1) = wal_checksum(endian, s0, s1, &fh[0..8]);
6831 let (n0, n1) = wal_checksum(endian, n0, n1, data);
6832 s0 = n0;
6833 s1 = n1;
6834 fh[16..20].copy_from_slice(&s0.to_be_bytes());
6835 fh[20..24].copy_from_slice(&s1.to_be_bytes());
6836 wal.extend_from_slice(&fh);
6837 wal.extend_from_slice(data);
6838 }
6839 wal
6840 }
6841
6842 #[test]
6843 fn snapshot_tables_reads_schema_from_its_own_page_one() {
6844 use crate::rebuild::{build_recovered_db_tables, RecoveredTable as RT};
6845 let seed = vec![RT {
6846 name: "people".to_string(),
6847 columns: vec!["id".to_string(), "name".to_string()],
6848 rows: vec![
6849 vec![Value::Integer(1), Value::Text("alice".into())],
6850 vec![Value::Integer(2), Value::Text("bob".into())],
6851 ],
6852 }];
6853 let main = build_recovered_db_tables(&seed);
6854 let ps = parse_header(&main).unwrap().page_size;
6855 let wal = wrap_db_in_wal(&main, ps);
6856
6857 let db = Database::open_with_wal(main, &wal).unwrap();
6858 let tl = db.wal_timeline().unwrap();
6859 let snap = tl.commit_snapshots().last().unwrap();
6860 assert!(snap.checksum_valid(), "minted WAL must be checksum-valid");
6861
6862 let tables = snap.tables();
6863 let people = tables
6864 .iter()
6865 .find(|t| t.name == "people")
6866 .expect("table 'people' present in snapshot schema");
6867 assert!(people.rootpage >= 2, "rootpage points past page 1");
6868 assert_eq!(people.columns, vec!["id".to_string(), "name".to_string()]);
6869 assert!(!people.without_rowid, "an ordinary rowid table");
6870 // Internal sqlite_* tables are excluded.
6871 assert!(tables.iter().all(|t| !t.name.starts_with("sqlite_")));
6872 }
6873
6874 #[test]
6875 fn snapshot_read_resolves_overflow_through_snapshot_pages_not_live_view() {
6876 // The DEFINING property of the snapshot-scoped read: a spilled (overflow)
6877 // row must decode from the snapshot's OWN pages, even when the live view
6878 // would supply different overflow content. Build a db whose table `t` holds
6879 // one large-blob row (forcing an overflow chain), capture it as the
6880 // snapshot, then CLOBBER the overflow pages in the live main-file image.
6881 // The snapshot read still returns the original blob; a live read sees the
6882 // clobbered bytes — proving the snapshot path does not consult the live view.
6883 use crate::rebuild::{build_recovered_db_tables, RecoveredTable as RT};
6884 let blob: Vec<u8> = (0..9000u32).map(|i| (i % 251) as u8).collect();
6885 let seed = vec![RT {
6886 name: "t".to_string(),
6887 columns: vec!["id".to_string(), "big".to_string()],
6888 rows: vec![vec![Value::Integer(1), Value::Blob(blob.clone())]],
6889 }];
6890 let minted = build_recovered_db_tables(&seed);
6891 let ps = parse_header(&minted).unwrap().page_size;
6892 // The WAL commits the TRUE pages; the snapshot materializes them.
6893 let wal = wrap_db_in_wal(&minted, ps);
6894
6895 // Now clobber the live main image's overflow pages (every page after the
6896 // first two: page 1 schema, page 2 table-leaf, page 3+ overflow) to a
6897 // distinct byte so a live read would mis-decode the blob.
6898 let mut clobbered_main = minted.clone();
6899 for p in clobbered_main.iter_mut().skip(2 * ps as usize) {
6900 *p = 0xEE;
6901 }
6902
6903 let db = Database::open_with_wal(clobbered_main, &wal).unwrap();
6904 let tl = db.wal_timeline().unwrap();
6905 let snap = tl.commit_snapshots().last().unwrap();
6906 let t = snap
6907 .tables()
6908 .into_iter()
6909 .find(|t| t.name == "t")
6910 .expect("table t in snapshot");
6911
6912 let rows = snap.read_table(t.rootpage, t.columns.len()).unwrap();
6913 assert_eq!(rows.len(), 1, "one row at this commit");
6914 let (rowid, values) = &rows[0];
6915 assert_eq!(*rowid, 1);
6916 // The 9000-byte blob reassembles from the SNAPSHOT's overflow pages, intact.
6917 assert_eq!(
6918 values.get(1),
6919 Some(&Value::Blob(blob)),
6920 "overflow blob must reassemble from the snapshot's pages, not the clobbered live view"
6921 );
6922 }
6923
6924 #[test]
6925 fn snapshot_read_walks_interior_btree_in_rowid_order() {
6926 // Many rows force an interior (0x05) table b-tree; the snapshot read must
6927 // descend it and return rows in ascending rowid order — exercising the
6928 // shared walk's interior branch through the snapshot page source.
6929 use crate::rebuild::{build_recovered_db_tables, RecoveredTable as RT};
6930 let rows_seed: Vec<Vec<Value>> = (1..=500i64)
6931 .map(|i| vec![Value::Integer(i), Value::Text(format!("name-{i}"))])
6932 .collect();
6933 let seed = vec![RT {
6934 name: "big".to_string(),
6935 columns: vec!["id".to_string(), "name".to_string()],
6936 rows: rows_seed,
6937 }];
6938 let minted = build_recovered_db_tables(&seed);
6939 let ps = parse_header(&minted).unwrap().page_size;
6940 let wal = wrap_db_in_wal(&minted, ps);
6941
6942 let db = Database::open_with_wal(minted, &wal).unwrap();
6943 let tl = db.wal_timeline().unwrap();
6944 let snap = tl.commit_snapshots().last().unwrap();
6945 let t = snap
6946 .tables()
6947 .into_iter()
6948 .find(|t| t.name == "big")
6949 .expect("table big");
6950 let rows = snap.read_table(t.rootpage, t.columns.len()).unwrap();
6951 assert_eq!(rows.len(), 500, "all rows across the interior b-tree");
6952 let ids: Vec<i64> = rows.iter().map(|(r, _)| *r).collect();
6953 assert!(ids.windows(2).all(|w| w[0] < w[1]), "ascending rowid order");
6954 assert_eq!(*ids.first().unwrap(), 1);
6955 assert_eq!(*ids.last().unwrap(), 500);
6956 }
6957
6958 #[test]
6959 fn without_rowid_sql_detects_the_clause() {
6960 // The WITHOUT ROWID detector keys off the CREATE TABLE tail, tolerant of
6961 // case and whitespace, and does NOT misfire on the literal appearing inside
6962 // a quoted string / column name (file-format §2.4). A WITHOUT ROWID b-tree
6963 // has no rowid key, so this flag gates the snapshot-scoped rowid read.
6964 assert!(without_rowid_sql(
6965 "CREATE TABLE kv(k TEXT PRIMARY KEY, v TEXT) WITHOUT ROWID"
6966 ));
6967 assert!(without_rowid_sql(
6968 "CREATE TABLE kv(k TEXT PRIMARY KEY, v TEXT) without rowid"
6969 ));
6970 // Ordinary tables are NOT flagged.
6971 assert!(!without_rowid_sql(
6972 "CREATE TABLE t(id INTEGER PRIMARY KEY, n TEXT)"
6973 ));
6974 // A column literally named with the words, but not the trailing clause, is
6975 // not a false positive.
6976 assert!(!without_rowid_sql(
6977 "CREATE TABLE t(\"without rowid\" TEXT, x INT)"
6978 ));
6979 }
6980
6981 #[test]
6982 fn is_autoincrement_detects_only_the_real_clause() {
6983 // Positive: an ordinary rowid table declaring INTEGER PRIMARY KEY
6984 // AUTOINCREMENT — case-insensitive and whitespace-tolerant.
6985 assert!(is_autoincrement(
6986 "CREATE TABLE students(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"
6987 ));
6988 assert!(is_autoincrement(
6989 "create table t( id integer primary key autoincrement )"
6990 ));
6991 // Negative: a plain INTEGER PRIMARY KEY is NOT autoincrement.
6992 assert!(!is_autoincrement(
6993 "CREATE TABLE students(id INTEGER PRIMARY KEY, name TEXT)"
6994 ));
6995 // Negative: a WITHOUT ROWID table cannot be AUTOINCREMENT (no rowid).
6996 assert!(!is_autoincrement(
6997 "CREATE TABLE kv(k INTEGER PRIMARY KEY AUTOINCREMENT, v TEXT) WITHOUT ROWID"
6998 ));
6999 // Negative: a column merely NAMED autoincrement is not the clause.
7000 assert!(!is_autoincrement(
7001 "CREATE TABLE t(\"autoincrement\" INTEGER PRIMARY KEY, x INT)"
7002 ));
7003 // Negative: the keyword inside a quoted string / comment does not qualify.
7004 assert!(!is_autoincrement(
7005 "CREATE TABLE t(id INTEGER PRIMARY KEY, note TEXT DEFAULT 'autoincrement')"
7006 ));
7007 // Negative: AUTOINCREMENT without INTEGER PRIMARY KEY is not a valid clause.
7008 assert!(!is_autoincrement(
7009 "CREATE TABLE t(id INTEGER AUTOINCREMENT, name TEXT)"
7010 ));
7011 }
7012
7013 #[test]
7014 fn sqlite_sequence_reads_present_absent_and_multi() {
7015 // A db with no AUTOINCREMENT table has no sqlite_sequence: empty map
7016 // (NOT seq=0), so callers never invent a high-water mark.
7017 let plain = Database::open(crate::rebuild::build_recovered_db_tables(&[
7018 crate::rebuild::RecoveredTable {
7019 name: "plain".to_string(),
7020 columns: vec!["c0".to_string()],
7021 rows: vec![vec![Value::Integer(1)]],
7022 },
7023 ]))
7024 .expect("minted db opens");
7025 assert!(
7026 plain.sqlite_sequence().is_empty(),
7027 "no AUTOINCREMENT table ⟹ empty sqlite_sequence map"
7028 );
7029
7030 // The b_autoinc fixture maintains sqlite_sequence(students)=5.
7031 let auto =
7032 Database::open(include_bytes!("../../tests/data/drop_recreate/b_autoinc.db").to_vec())
7033 .expect("open b_autoinc.db");
7034 let seq = auto.sqlite_sequence();
7035 assert_eq!(seq.get("students"), Some(&5), "students high-water = 5");
7036
7037 // The upd_autoinc fixture: a single AUTOINCREMENT table t at seq=5.
7038 let upd = Database::open(
7039 include_bytes!("../../tests/data/drop_recreate/upd_autoinc.db").to_vec(),
7040 )
7041 .expect("open upd_autoinc.db");
7042 assert_eq!(upd.sqlite_sequence().get("t"), Some(&5), "t high-water = 5");
7043 }
7044
7045 #[test]
7046 fn schema_sql_reads_current_name_to_create_sql() {
7047 // The live `name -> CREATE SQL` map mirrors live_tables, keyed by name.
7048 let auto =
7049 Database::open(include_bytes!("../../tests/data/drop_recreate/b_autoinc.db").to_vec())
7050 .expect("open b_autoinc.db");
7051 let schema = auto.schema_sql();
7052 let sql = schema.get("students").expect("students present");
7053 assert!(
7054 sql.contains("AUTOINCREMENT"),
7055 "current CREATE SQL carried verbatim: {sql}"
7056 );
7057 }
7058
7059 #[test]
7060 fn prior_snapshot_schema_sql_reads_prior_create_sql() {
7061 // b_journal_altered: the prior (-journal) schema for `students` has NO
7062 // `extra` column, the current schema does → the CREATE SQL texts differ.
7063 let main = include_bytes!("../../tests/data/drop_recreate/b_journal_altered.db").to_vec();
7064 let journal = include_bytes!("../../tests/data/drop_recreate/b_journal_altered.db-journal");
7065 let db = Database::open(main).expect("open b_journal_altered.db");
7066 let prior = db
7067 .rollback_prior(journal)
7068 .expect("rollback_prior parses the PERSIST journal");
7069 let prior_sql = prior.schema_sql();
7070 let prior_students = prior_sql.get("students").expect("prior students present");
7071 assert!(
7072 !prior_students.contains("extra"),
7073 "prior CREATE SQL lacks the ALTER-added column: {prior_students}"
7074 );
7075 let current = db.schema_sql();
7076 assert_ne!(
7077 current.get("students"),
7078 prior_sql.get("students"),
7079 "prior vs current CREATE SQL differ (the ALTER)"
7080 );
7081 }
7082
7083 #[test]
7084 fn prior_snapshot_schema_sql_dml_only_matches_current() {
7085 // b_journal_dml: the last transaction is DML only, so the prior (-journal)
7086 // CREATE SQL for `students` EQUALS the current schema (anti-FP ground truth).
7087 let main = include_bytes!("../../tests/data/drop_recreate/b_journal_dml.db").to_vec();
7088 let journal = include_bytes!("../../tests/data/drop_recreate/b_journal_dml.db-journal");
7089 let db = Database::open(main).expect("open b_journal_dml.db");
7090 let prior = db
7091 .rollback_prior(journal)
7092 .expect("rollback_prior parses the PERSIST journal");
7093 assert_eq!(
7094 db.schema_sql().get("students"),
7095 prior.schema_sql().get("students"),
7096 "DML-only ⟹ prior and current CREATE SQL are identical"
7097 );
7098 }
7099}