| 1 |
#![cfg(feature = "gzip")] |
| 2 |
|
| 3 |
use crate::crc::crc_tables::{CRC32_SLICE1_TABLE, CRC32_SLICE8_TABLE}; |
| 4 |
|
| 5 |
mod crc_tables; |
| 6 |
|
| 7 |
/// Calculate crc for a data and an initial crc value |
| 8 |
#[allow(clippy::identity_op, clippy::zero_prefixed_literal)] |
| 9 |
pub fn crc32(data: &[u8], mut crc: u32) -> u32 |
| 10 |
{ |
| 11 |
// main loop |
| 12 |
for chunk in data.chunks_exact(8) |
| 13 |
{ |
| 14 |
let chunk_loaded = u64::from_le_bytes(chunk.try_into().unwrap()); |
| 15 |
|
| 16 |
let v1 = (chunk_loaded & u64::from(u32::MAX)) as u32; |
| 17 |
let v2 = (chunk_loaded >> 32) as u32; |
| 18 |
|
| 19 |
crc = CRC32_SLICE8_TABLE[0x700 + (((crc ^ v1) >> 00) & 0xFF) as usize] |
| 20 |
^ CRC32_SLICE8_TABLE[0x600 + (((crc ^ v1) >> 08) & 0xFF) as usize] |
| 21 |
^ CRC32_SLICE8_TABLE[0x500 + (((crc ^ v1) >> 16) & 0xFF) as usize] |
| 22 |
^ CRC32_SLICE8_TABLE[0x400 + (((crc ^ v1) >> 24) & 0xFF) as usize] |
| 23 |
^ CRC32_SLICE8_TABLE[0x300 + (((v2 >> 00) & 0xFF) as usize)] |
| 24 |
^ CRC32_SLICE8_TABLE[0x200 + (((v2 >> 08) & 0xFF) as usize)] |
| 25 |
^ CRC32_SLICE8_TABLE[0x100 + (((v2 >> 16) & 0xFF) as usize)] |
| 26 |
^ CRC32_SLICE8_TABLE[0x000 + (((v2 >> 24) & 0xFF) as usize)]; |
| 27 |
} |
| 28 |
// handle remainder |
| 29 |
for remainder in data.chunks_exact(8).remainder() |
| 30 |
{ |
| 31 |
crc = (crc >> 8) ^ CRC32_SLICE1_TABLE[((crc & 0xFF) ^ u32::from(*remainder)) as usize]; |
| 32 |
} |
| 33 |
|
| 34 |
crc |
| 35 |
} |