adler32.nx
buildroot/runtime/adler32.nx
about
adler32.nx -- Adler-32 checksum (Mark Adler, zlib/gzip).
32-bit rolling checksum used inside zlib streams (RFC 1950)
and pigz / gzip integrity checks. Faster than CRC-32 at the
cost of worse collision resistance -- acceptable for short
frames, weak for megabyte-scale files.
Algorithm:
a = 1; b = 0
for each byte x:
a = (a + x) mod 65521
b = (b + a) mod 65521
checksum = (b << 16) | a
65521 is the largest prime less than 2^16. The running
quantities a,b never exceed 2^32 if we reduce after every
NMAX = 5552 bytes (Adler's tuning so we can defer the modulo).
We do the naive per-byte modulo here; simplest; still trivially
beats a CRC table.
Invariants:
A1 Empty input gives checksum = 1 (a=1, b=0).
A2 Output fits in a 32-bit unsigned; we return it in an i64
to avoid sign issues in downstream callers.
A3 adler32_combine not implemented (zlib has a closed-form
but it needs 64-bit modular arithmetic tricks -- future
commit if we need to concatenate streams).
dependencies 1 imports · 0 importers
imports: syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 31 | const ADLER_MOD: i64 = 65521 |
functions
| 33 | func adler32(data: *u8, n: i64) -> i64 {
called by 1: main |
| 48 | func adler32_init() -> i64 {
called by 1: main |
| 52 | func adler32_update(state: i64, data: *u8, n: i64) -> i64 {
called by 1: main |
| 67 | func main() -> i64 { |