Difference between implementations of crc32 () <linux / crc32.h> and <zlib.h> in C
I call two functions on my char* s = "pratik"
as:
User Code:
#include <zlib.h> int main() { char *s = "pratik"; printf("%x\n",crc32(0x80000000, s, strlen(s))); return 0; }
Conclusion: 66fa3c99
Kernel Code:
#include <linux/crc32.h> int main() { char *s = "pratik"; u32 checksum = crc32(0x80000000, s, strlen(s)); printk("\nChecksum --> %x", checksum); return checksum; }
Output:
Checksum β d7389d3a
Why are the checksum values ββon the same lines different?
+6
1 answer
It seems that someone was worried that the standard Ethernet (PKZIP, ITU V.42, etc. etc.) CRC-32 performs preliminary and post-exclusive or with 0xffffffff
. Thus, the version in the Linux kernel leaves this and expects the application to do so. Hover over your mouse.
In any case, you can get the same result as the (correct) zlib crc32()
using the (non-standard) Linux crc32()
, namely:
crc_final = crc32(crc_initial ^ 0xffffffff, buf, len) ^ 0xffffffff;
In fact, the same code will allow you to duplicate Linux crc32()
, using zlib crc32()
.
+8