1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! SQLite WAL checksum algorithm.
//!
//! Implements the checksum algorithm from SQLite's `walChecksumBytes()` in `wal.c`:
//!
//! ```text
//! for i from 0 to n-1 step 2:
//! s0 += x[i] + s1;
//! s1 += x[i+1] + s0;
//! ```
//!
//! Key details:
//! - **Byte order**: Determined by the WAL magic number. `0x377f0682` = little-endian
//! (native on x86), `0x377f0683` = big-endian. Controlled by the `big_endian` parameter.
//! - **Initial values**: Header checksum starts at `(0, 0)`. Frame checksums are cumulative,
//! using the previous frame's (or header's) checksum as the initial values.
//! - **No final XOR**: Unlike some checksum schemes, there are no chunk constants or
//! final XOR steps. The raw `s0` and `s1` values are the checksum.
//! - **Input**: The header checksum covers bytes 0..24 of the WAL header. Frame checksums
//! cover the first 8 bytes of the frame header (page_number + db_size_after) plus the
//! page data.
//!
//! Note: use `wal_checksum_pair` for correct WAL frame construction and validation.
/// Compute both halves of the SQLite WAL checksum.
///
/// `big_endian` should be `true` when WAL magic is `0x377f0683`, `false` for `0x377f0682`.