Raunaq Kochar Raunaq Kochar - 1 year ago 68
C Question

Difference between crc32() implementations of <linux/crc32.h> and <zlib.h> in C

I am calling two functions on my

char* s = "pratik"

User code:

#include <zlib.h>
int main()
char *s = "pratik";
printf("%x\n",crc32(0x80000000, s, strlen(s)));
return 0;


Kernel code:

#include <linux/crc32.h>

int main()
char *s = "pratik";

u32 checksum = crc32(0x80000000, s, strlen(s));
printk("\nChecksum --> %x", checksum);

return checksum;


Checksum --> d7389d3a

Why are the values of the checksums on the same strings different?

Answer Source

It appears that someone was disturbed by the fact that the standard Ethernet (PKZIP, ITU V.42 etc. etc.) CRC-32 does a pre- and post-exclusive-or with 0xffffffff. So the version in the Linux kernel leaves that out, and expects the application to do that. Go figure.

Anyway, you can get the same result as the (correct) zlib crc32(), using the (non-standard) Linux crc32() instead, thusly:

crc_final = crc32(crc_initial ^ 0xffffffff, buf, len) ^ 0xffffffff;

In fact, that exact same code would allow you to duplicate the Linux crc32() using the zlib crc32() as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download