///|
// CRC-32C uses the reflected Castagnoli polynomial.
let crc32c_table : FixedArray[UInt32] = FixedArray::makei(256, i => {
let mut value = i.reinterpret_as_uint()
for _ in 0..<8 {
value = if (value & 1) == 1 {
(value >> 1) ^ 0x82f63b78U
} else {
value >> 1
}
}
value
})
///|
fn crc_update(initial : UInt32, bytes : BytesView) -> UInt32 {
let mut crc = initial
for byte in bytes {
let index = ((crc ^ byte.to_uint()) & 255).reinterpret_as_int()
crc = crc32c_table[index] ^ (crc >> 8)
}
crc
}
///|
// The checksum field at offsets 28–31 is zero during computation.
fn record_crc(fixed : BytesView, sections : Array[BytesView]) -> UInt32 {
let mut crc = crc_update(0xffffffffU, fixed[:28])
crc = crc_update(crc, b"\x00\x00\x00\x00")
crc = crc_update(crc, fixed[32:40])
for section in sections {
crc = crc_update(crc, section)
}
crc ^ 0xffffffffU
}