I have the code and have to change 0x3B9ACA02432543 to some value that will make the result of this code equal to cksum result.
I tried to change it to cksum default value that is 0x04C11DB7 but the result values were different
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
} else {
FILE *f = NULL;
int c;
f = fopen(argv[1], "rb");
if (f == NULL)
perror("fopen()");
else {
int size = 0;
int crc;
int c;
int i, j;
int crc_table[256];
for (i = 0; i < 256; i++) {
crc = i;
for (j = 0; j < 8; j++)
crc = crc & 1 ? (crc >> 1) ^ 0x3B9ACA02432543 : crc >> 1;
crc_table[i] = crc;
}
crc = 0;
while ((c = fgetc(f)) != EOF) {
++size;
crc = crc_table[(crc ^ c) & 0xFF] ^ (crc >> 8);
}
crc ^= 0xFFFFFFFFUL;
printf("%u\n", crc);
}
}
return 0;
}
You did not say what operating system or source of
cksumyou are using. It is different on different systems. On my macOS (BSD-derived) system,cksum -o 3will compute the standard CRC-32 that you may be referring to.In that case, you need set the initial CRC to
0xffffffff, and you need to use the reflection of the polynomial,0x04c11db7, which is0xedb88320. Also you should use a type assured to be at least 32 bits in length.longis,intis not. You also need to use unsigned types for CRC calcuations, so that shifting down doesn't copy the high bit.The POSIX cksum is yet another variant. It uses the non-reflected CRC, starts with
0instead of0xffffffff, and appends the length of the data to the data for the CRC computation. That length is in little-endian order with the least number of bytes needed to represent the length. For convenience, I used the explicitly-sized integers provided by C99: