dvb_common/crc32_mpeg2.rs
1//! CRC-32 MPEG-2 — Annex C of ETSI EN 300 468, Annex A of ETSI TS 102 773.
2//!
3//! Polynomial `0x04C1_1DB7`, initial shift-register value `0xFFFF_FFFF`,
4//! MSB-first bit order, no reflection, no final XOR. Used by every PSI/SI
5//! section trailer and every T2-MI packet trailer.
6
7/// CRC-32 MPEG-2 generator polynomial.
8pub const POLY: u32 = 0x04C1_1DB7;
9
10/// Precomputed 256-entry forward table, built at compile time — zero
11/// runtime initialisation cost.
12pub const TABLE: [u32; 256] = {
13 let mut t = [0u32; 256];
14 let mut i = 0u32;
15 while i < 256 {
16 let mut c = i << 24;
17 let mut j = 0;
18 while j < 8 {
19 c = if c & 0x8000_0000 != 0 {
20 (c << 1) ^ POLY
21 } else {
22 c << 1
23 };
24 j += 1;
25 }
26 t[i as usize] = c;
27 i += 1;
28 }
29 t
30};
31
32/// Compute CRC-32 MPEG-2 over `bytes`. Initial shift-register value is the
33/// canonical `0xFFFF_FFFF`.
34#[inline]
35pub fn compute(bytes: &[u8]) -> u32 {
36 let mut crc: u32 = 0xFFFF_FFFF;
37 for &b in bytes {
38 crc = (crc << 8) ^ TABLE[((crc >> 24) as u8 ^ b) as usize];
39 }
40 crc
41}