diff --git a/source/durandal/crc.rs b/source/durandal/cksum.rs similarity index 53% rename from source/durandal/crc.rs rename to source/durandal/cksum.rs index 97ec35d..7441ae7 100644 --- a/source/durandal/crc.rs +++ b/source/durandal/cksum.rs @@ -3,7 +3,7 @@ fn crc_accum(a: u32, _: u32) -> u32 { if a & 1 == 1 { - 0xEDB8_8320 ^ a >> 1 + ISO_3309_POLYNOMIAL ^ a >> 1 } else { a >> 1 } @@ -18,13 +18,36 @@ fn crc_init() -> [u32; 256] t } +/// Creates an ADLER32 of all bytes in `b`. +/// +/// # Examples +/// +/// ``` +/// use maraiah::durandal::cksum::adler32; +/// +/// assert_eq!(adler32(b"Lorem ipsum dolor sit amet"), 0x83D5_09C5); +/// ``` +pub fn adler32(b: &[u8]) -> u32 +{ + let mut x = 1; + let mut y = 0; + + for &z in b { + let z = u32::from(z); + x = (x + z) % ADLER32_MODULO; + y = (y + x) % ADLER32_MODULO; + } + + (y << 16) | x +} + /// Creates a CRC-32 of all bytes in `b` with the starting sum `s`. The /// polynomial used is the one from ISO-3309. /// /// # Examples /// /// ``` -/// use maraiah::durandal::crc::crc32; +/// use maraiah::durandal::cksum::crc32; /// /// assert_eq!(crc32(b"Lorem ipsum dolor sit amet", !0), 0x5F29_D461); /// ``` @@ -34,4 +57,7 @@ pub fn crc32(b: &[u8], s: u32) -> u32 !b.iter().fold(s, |a, &o| a >> 8 ^ t[usize::from(a as u8 ^ o)]) } +const ISO_3309_POLYNOMIAL: u32 = 0xEDB8_8320; +const ADLER32_MODULO: u32 = 65521; + // EOF diff --git a/source/durandal/mod.rs b/source/durandal/mod.rs index 697ebff..8a3b530 100644 --- a/source/durandal/mod.rs +++ b/source/durandal/mod.rs @@ -8,7 +8,7 @@ pub mod cenum; pub mod bin; pub mod bit; -pub mod crc; +pub mod cksum; pub mod file; pub mod fixed; pub mod image;