diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
commit | a0aa2307322cd47bbf416810ac0292925e03be87 (patch) | |
tree | 37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/vendor/crc/src | |
parent | Initial commit. (diff) | |
download | suricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip |
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/vendor/crc/src')
-rw-r--r-- | rust/vendor/crc/src/crc16.rs | 77 | ||||
-rw-r--r-- | rust/vendor/crc/src/crc32.rs | 81 | ||||
-rw-r--r-- | rust/vendor/crc/src/crc64.rs | 77 | ||||
-rw-r--r-- | rust/vendor/crc/src/lib.rs | 70 | ||||
-rw-r--r-- | rust/vendor/crc/src/util.rs | 47 |
5 files changed, 352 insertions, 0 deletions
diff --git a/rust/vendor/crc/src/crc16.rs b/rust/vendor/crc/src/crc16.rs new file mode 100644 index 0000000..f933dd5 --- /dev/null +++ b/rust/vendor/crc/src/crc16.rs @@ -0,0 +1,77 @@ +#[cfg(feature = "std")] +use std::hash::Hasher; +#[cfg(not(feature = "std"))] +use core::hash::Hasher; + +pub use util::make_table_crc16 as make_table; + +include!(concat!(env!("OUT_DIR"), "/crc16_constants.rs")); + +pub struct Digest { + table: [u16; 256], + initial: u16, + value: u16 +} + +pub trait Hasher16 { + fn reset(&mut self); + fn write(&mut self, bytes: &[u8]); + fn sum16(&self) -> u16; +} + +pub fn update(mut value: u16, table: &[u16; 256], bytes: &[u8]) -> u16 { + value = !value; + for &i in bytes.iter() { + value = table[((value as u8) ^ i) as usize] ^ (value >> 8) + } + !value +} + +pub fn checksum_x25(bytes: &[u8]) -> u16 { + return update(0, &X25_TABLE, bytes); +} + +pub fn checksum_usb(bytes: &[u8]) -> u16 { + return update(0, &USB_TABLE, bytes); +} + +impl Digest { + pub fn new(poly: u16) -> Digest { + Digest { + table: make_table(poly), + initial: 0, + value: 0 + } + } + + pub fn new_with_initial(poly: u16, initial: u16) -> Digest { + Digest { + table: make_table(poly), + initial: initial, + value: initial + } + } +} + +impl Hasher16 for Digest { + fn reset(&mut self) { + self.value = self.initial; + } + fn write(&mut self, bytes: &[u8]) { + self.value = update(self.value, &self.table, bytes); + } + fn sum16(&self) -> u16 { + self.value + } +} + +/// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. +impl Hasher for Digest { + fn write(&mut self, bytes: &[u8]) { + Hasher16::write(self, bytes); + } + + fn finish(&self) -> u64 { + self.sum16() as u64 + } +} diff --git a/rust/vendor/crc/src/crc32.rs b/rust/vendor/crc/src/crc32.rs new file mode 100644 index 0000000..6093d3b --- /dev/null +++ b/rust/vendor/crc/src/crc32.rs @@ -0,0 +1,81 @@ +#[cfg(feature = "std")] +use std::hash::Hasher; +#[cfg(not(feature = "std"))] +use core::hash::Hasher; + +pub use util::make_table_crc32 as make_table; + +include!(concat!(env!("OUT_DIR"), "/crc32_constants.rs")); + +pub struct Digest { + table: [u32; 256], + initial: u32, + value: u32 +} + +pub trait Hasher32 { + fn reset(&mut self); + fn write(&mut self, bytes: &[u8]); + fn sum32(&self) -> u32; +} + +pub fn update(mut value: u32, table: &[u32; 256], bytes: &[u8]) -> u32 { + value = !value; + for &i in bytes.iter() { + value = table[((value as u8) ^ i) as usize] ^ (value >> 8) + } + !value +} + +pub fn checksum_ieee(bytes: &[u8]) -> u32 { + return update(0, &IEEE_TABLE, bytes); +} + +pub fn checksum_castagnoli(bytes: &[u8]) -> u32 { + return update(0, &CASTAGNOLI_TABLE, bytes); +} + +pub fn checksum_koopman(bytes: &[u8]) -> u32 { + return update(0, &KOOPMAN_TABLE, bytes); +} + +impl Digest { + pub fn new(poly: u32) -> Digest { + Digest { + table: make_table(poly), + initial: 0, + value: 0 + } + } + + pub fn new_with_initial(poly: u32, initial: u32) -> Digest { + Digest { + table: make_table(poly), + initial: initial, + value: initial + } + } +} + +impl Hasher32 for Digest { + fn reset(&mut self) { + self.value = self.initial; + } + fn write(&mut self, bytes: &[u8]) { + self.value = update(self.value, &self.table, bytes); + } + fn sum32(&self) -> u32 { + self.value + } +} + +/// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. +impl Hasher for Digest { + fn write(&mut self, bytes: &[u8]) { + Hasher32::write(self, bytes); + } + + fn finish(&self) -> u64 { + self.sum32() as u64 + } +} diff --git a/rust/vendor/crc/src/crc64.rs b/rust/vendor/crc/src/crc64.rs new file mode 100644 index 0000000..8404030 --- /dev/null +++ b/rust/vendor/crc/src/crc64.rs @@ -0,0 +1,77 @@ +#[cfg(feature = "std")] +use std::hash::Hasher; +#[cfg(not(feature = "std"))] +use core::hash::Hasher; + +pub use util::make_table_crc64 as make_table; + +include!(concat!(env!("OUT_DIR"), "/crc64_constants.rs")); + +pub struct Digest { + table: [u64; 256], + initial: u64, + value: u64 +} + +pub trait Hasher64 { + fn reset(&mut self); + fn write(&mut self, bytes: &[u8]); + fn sum64(&self) -> u64; +} + +pub fn update(mut value: u64, table: &[u64; 256], bytes: &[u8]) -> u64 { + value = !value; + for &i in bytes.iter() { + value = table[((value as u8) ^ i) as usize] ^ (value >> 8) + } + !value +} + +pub fn checksum_ecma(bytes: &[u8]) -> u64 { + return update(0, &ECMA_TABLE, bytes); +} + +pub fn checksum_iso(bytes: &[u8]) -> u64 { + return update(0, &ISO_TABLE, bytes); +} + +impl Digest { + pub fn new(poly: u64) -> Digest { + Digest { + table: make_table(poly), + initial: 0, + value: 0 + } + } + + pub fn new_with_initial(poly: u64, initial: u64) -> Digest { + Digest { + table: make_table(poly), + initial: initial, + value: initial + } + } +} + +impl Hasher64 for Digest { + fn reset(&mut self) { + self.value = self.initial; + } + fn write(&mut self, bytes: &[u8]) { + self.value = update(self.value, &self.table, bytes); + } + fn sum64(&self) -> u64 { + self.value + } +} + +/// Implementation of std::hash::Hasher so that types which #[derive(Hash)] can hash with Digest. +impl Hasher for Digest { + fn write(&mut self, bytes: &[u8]) { + Hasher64::write(self, bytes); + } + + fn finish(&self) -> u64 { + self.sum64() + } +} diff --git a/rust/vendor/crc/src/lib.rs b/rust/vendor/crc/src/lib.rs new file mode 100644 index 0000000..c803d7c --- /dev/null +++ b/rust/vendor/crc/src/lib.rs @@ -0,0 +1,70 @@ +//! # crc +//! Rust implementation of CRC(16, 32, 64) +//! +//! ## Usage +//! ### Compute CRC16 +//! ```rust +//! use crc::{crc16, Hasher16}; +//! +//! assert_eq!(crc16::checksum_x25(b"123456789"), 0x906e); +//! assert_eq!(crc16::checksum_usb(b"123456789"), 0xb4c8); +//! +//! // use provided or custom polynomial +//! let mut digest = crc16::Digest::new(crc16::X25); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum16(), 0x906e); +//! +//! // with initial +//! let mut digest = crc16::Digest::new_with_initial(crc16::X25, 0u16); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum16(), 0x906e); +//! ``` +//! +//! ### Compute CRC32 +//! ```rust +//! use crc::{crc32, Hasher32}; +//! +//! // CRC-32-IEEE being the most commonly used one +//! assert_eq!(crc32::checksum_ieee(b"123456789"), 0xcbf43926); +//! assert_eq!(crc32::checksum_castagnoli(b"123456789"), 0xe3069283); +//! assert_eq!(crc32::checksum_koopman(b"123456789"), 0x2d3dd0ae); +//! +//! // use provided or custom polynomial +//! let mut digest = crc32::Digest::new(crc32::IEEE); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum32(), 0xcbf43926); +//! +//! // with initial +//! let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, 0u32); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum32(), 0xcbf43926); +//! ``` +//! +//! ### Compute CRC64 +//! ```rust +//! use crc::{crc64, Hasher64}; +//! +//! assert_eq!(crc64::checksum_ecma(b"123456789"), 0x995dc9bbdf1939fa); +//! assert_eq!(crc64::checksum_iso(b"123456789"), 0xb90956c775a41001); +//! +//! // use provided or custom polynomial +//! let mut digest = crc64::Digest::new(crc64::ECMA); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); +//! +//! // with initial +//! let mut digest = crc64::Digest::new_with_initial(crc64::ECMA, 0u64); +//! digest.write(b"123456789"); +//! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa); +//! ``` + +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod crc16; +pub mod crc32; +pub mod crc64; +mod util; + +pub use self::crc16::Hasher16; +pub use self::crc32::Hasher32; +pub use self::crc64::Hasher64; diff --git a/rust/vendor/crc/src/util.rs b/rust/vendor/crc/src/util.rs new file mode 100644 index 0000000..f5b2f29 --- /dev/null +++ b/rust/vendor/crc/src/util.rs @@ -0,0 +1,47 @@ +pub fn make_table_crc16(poly: u16) -> [u16; 256] { + let mut table = [0u16; 256]; + for i in 0..256 { + let mut value = i as u16; + for _ in 0..8 { + value = if (value & 1) == 1 { + (value >> 1) ^ poly + } else { + value >> 1 + } + } + table[i] = value; + } + table +} + +pub fn make_table_crc32(poly: u32) -> [u32; 256] { + let mut table = [0u32; 256]; + for i in 0..256 { + let mut value = i as u32; + for _ in 0..8 { + value = if (value & 1) == 1 { + (value >> 1) ^ poly + } else { + value >> 1 + } + } + table[i] = value; + } + table +} + +pub fn make_table_crc64(poly: u64) -> [u64; 256] { + let mut table = [0u64; 256]; + for i in 0..256 { + let mut value = i as u64; + for _ in 0..8 { + value = if (value & 1) == 1 { + (value >> 1) ^ poly + } else { + value >> 1 + } + } + table[i] = value; + } + table +} |