Skip to content

Instantly share code, notes, and snippets.

@kornelski kornelski/adler32.c
Created Oct 5, 2017

Embed
What would you like to do?
static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) {
unsigned s1 = adler & 0xffff;
unsigned s2 = (adler >> 16) & 0xffff;
while(len > 0) {
/*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/
unsigned amount = len > 5550 ? 5550 : len;
len -= amount;
while(amount > 0) {
s1 += (*data++);
s2 += s1;
--amount;
}
s1 %= 65521;
s2 %= 65521;
}
return (s2 << 16) | s1;
}
fn update_adler32(adler: u32, data: &[u8]) -> u32 {
let mut s1 = adler & 0xffff;
let mut s2 = (adler >> 16) & 0xffff;
/*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/
for chunk in data.chunks(5550) {
for b in chunk {
s1 += *b as u32;
s2 += s1;
}
s1 %= 65521;
s2 %= 65521;
}
return (s2 << 16) | s1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.