diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/zune-inflate/src/crc.rs | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/zune-inflate/src/crc.rs')
-rw-r--r-- | vendor/zune-inflate/src/crc.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/vendor/zune-inflate/src/crc.rs b/vendor/zune-inflate/src/crc.rs new file mode 100644 index 0000000..7c01127 --- /dev/null +++ b/vendor/zune-inflate/src/crc.rs @@ -0,0 +1,35 @@ +#![cfg(feature = "gzip")] + +use crate::crc::crc_tables::{CRC32_SLICE1_TABLE, CRC32_SLICE8_TABLE}; + +mod crc_tables; + +/// Calculate crc for a data and an initial crc value +#[allow(clippy::identity_op, clippy::zero_prefixed_literal)] +pub fn crc32(data: &[u8], mut crc: u32) -> u32 +{ + // main loop + for chunk in data.chunks_exact(8) + { + let chunk_loaded = u64::from_le_bytes(chunk.try_into().unwrap()); + + let v1 = (chunk_loaded & u64::from(u32::MAX)) as u32; + let v2 = (chunk_loaded >> 32) as u32; + + crc = CRC32_SLICE8_TABLE[0x700 + (((crc ^ v1) >> 00) & 0xFF) as usize] + ^ CRC32_SLICE8_TABLE[0x600 + (((crc ^ v1) >> 08) & 0xFF) as usize] + ^ CRC32_SLICE8_TABLE[0x500 + (((crc ^ v1) >> 16) & 0xFF) as usize] + ^ CRC32_SLICE8_TABLE[0x400 + (((crc ^ v1) >> 24) & 0xFF) as usize] + ^ CRC32_SLICE8_TABLE[0x300 + (((v2 >> 00) & 0xFF) as usize)] + ^ CRC32_SLICE8_TABLE[0x200 + (((v2 >> 08) & 0xFF) as usize)] + ^ CRC32_SLICE8_TABLE[0x100 + (((v2 >> 16) & 0xFF) as usize)] + ^ CRC32_SLICE8_TABLE[0x000 + (((v2 >> 24) & 0xFF) as usize)]; + } + // handle remainder + for remainder in data.chunks_exact(8).remainder() + { + crc = (crc >> 8) ^ CRC32_SLICE1_TABLE[((crc & 0xFF) ^ u32::from(*remainder)) as usize]; + } + + crc +} |