move crc to ckum and add ADLER function

png-branch
an 2019-03-18 08:17:59 -04:00
parent bf8fa42e26
commit 7d02f6460f
2 changed files with 29 additions and 3 deletions

View File

@ -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

View File

@ -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;