Skip to content

Instantly share code, notes, and snippets.

@kornelski
Created October 5, 2017 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kornelski/710db9d30a64db0807c5bfbdbdecf85e to your computer and use it in GitHub Desktop.
Save kornelski/710db9d30a64db0807c5bfbdbdecf85e to your computer and use it in GitHub Desktop.
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