Created
January 7, 2021 12:30
-
-
Save rakisaionji/fb187ac6f11e44410b2a0425c1c08c64 to your computer and use it in GitHub Desktop.
Fast implementation of popular checksum algorithms in simple C language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
unsigned int adler32(unsigned int adler, const unsigned char* buf, size_t len) | |
{ | |
unsigned int a, b; | |
if (adler) | |
{ | |
a = adler & 0x0000ffff; | |
b = (adler >> 16) & 0x0000ffff; | |
} | |
else | |
{ | |
a = 1; | |
b = 0; | |
} | |
while (len--) | |
{ | |
a = (a + *buf++) % 65521; | |
b = (b + a) % 65521; | |
} | |
return (b << 16) + a; | |
} | |
unsigned int crc32(unsigned int crc, const unsigned char* buf, size_t len) | |
{ | |
int k; | |
crc = ~crc; | |
while (len--) | |
{ | |
crc ^= *buf++; | |
for (k = 0; k < 8; k++) | |
crc = (crc >> 1) ^ (0xedb88320 & (0 - (crc & 1))); | |
} | |
return ~crc; | |
} | |
unsigned int crc32c(unsigned int crc, const unsigned char* buf, size_t len) | |
{ | |
int k; | |
crc = ~crc; | |
while (len--) | |
{ | |
crc ^= *buf++; | |
for (k = 0; k < 8; k++) | |
crc = (crc >> 1) ^ (0x82f63b78 & (0 - (crc & 1))); | |
} | |
return ~crc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment