Skip to content

Instantly share code, notes, and snippets.

@rakisaionji
Created January 7, 2021 12:30
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 rakisaionji/fb187ac6f11e44410b2a0425c1c08c64 to your computer and use it in GitHub Desktop.
Save rakisaionji/fb187ac6f11e44410b2a0425c1c08c64 to your computer and use it in GitHub Desktop.
Fast implementation of popular checksum algorithms in simple C language.
#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