Skip to main content

Module checksum

Module checksum 

Source
Expand description

CRC32C helpers used by both the page-0 header and the page trailer.

The choice of CRC32C (Castagnoli) is fixed at format-major 0; see docs/format.md § Checksum algorithm for the rationale. This module is the only place that calls into the crc32c crate so the algorithm can be revisited in one edit.

§Trailer formats

Two trailer interpretations live side by side:

  • v0 (write_page_trailer / page_trailer_valid) — the full 32-bit CRC32C of bytes [0..PAGE_SIZE - PAGE_TRAILER_SIZE]. Used by every non-header page in a format_minor = 0 file, and by the in-memory representation of every page in every file (the pager re-stamps the on-disk trailer on write_back_page).
  • v1 (write_page_trailer_v1 / page_trailer_valid_v1 / page_trailer_flag_v1) — bit 31 of the trailer is the per-page compression flag; bits 0..30 are the 31-bit CRC32C of the on-disk bytes. Phase 3 (issue #8) — see docs/format.md § Page trailer for the rationale of the 32→31-bit precision tradeoff.

Constants§

TRAILER_OFFSET
Byte offset of the page trailer inside any non-header page.
V1_CRC_MASK
Mask isolating the lower 31 bits of the v1 trailer (CRC region).
V1_FLAG_MASK
Mask isolating bit 31 of the v1 trailer (compression flag).

Functions§

crc32c
Compute the CRC32C of bytes using the Castagnoli polynomial.
crc32c_append
Continue a CRC32C computation: fold bytes into a running CRC produced by a prior [crc32c] / crc32c_append call. The result is byte-identical to crc32c(prefix ++ bytes) where prefix was the input that produced crc. This is the only place that calls into the crc32c crate’s incremental API, so the WAL frame CRC can be computed over discontiguous segments (header sans-CRC, zeroed CRC field, page body) without allocating a contiguous scratch buffer.
page_trailer_flag_v1
Phase 3 (issue #8): read the v1 compression flag (bit 31) from the trailer. The caller is responsible for verifying the trailer with page_trailer_valid_v1 BEFORE consulting the flag — an unverified buffer’s flag is meaningless.
page_trailer_valid
true iff the page trailer matches the recomputed CRC32C of the body (v0 interpretation). Caller decides what error to surface on mismatch.
page_trailer_valid_v1
Phase 3 (issue #8): v1 trailer verifier. Compares the lower 31 bits of the trailer against the recomputed 31-bit CRC32C of bytes [0..TRAILER_OFFSET]. The flag bit (bit 31) is NOT part of the checksum.
write_page_trailer
Compute the page trailer for page and write it into the last PAGE_TRAILER_SIZE bytes (v0 interpretation: full 32-bit CRC).
write_page_trailer_v1
Phase 3 (issue #8): v1 trailer writer. Stamps the per-page compressed flag into bit 31 and the 31-bit CRC32C of bytes [0..TRAILER_OFFSET] into bits 0..30.