So I need to compute a CRC32 checksum of an ELF file, and I'm just struggling with C a bit. First thing I need to figure out is the best way to get the data into the checksum algorithm. If the ELF file is arbitrary size, and I'm trying to read it in binary, what's the best way to store that data so I can give it to the checksum formula? Thanks.
Here's what I have as of now.
#include <stdio.h>
#include <stdint.h>
typedef uint32_t crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
#define POLYNOMIAL 0x04C11DB7
crc crc32(uint32_t const message[], int nBytes)
{
int byte;
crc remainder = 0;
for (byte = 0; byte < nBytes; ++byte)
{
remainder ^= (message[byte] << (WIDTH - 8));
uint32_t bit;
for (bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
}
printf("%X",remainder);
return (remainder);
}
int main(int argc, char* argv[])
{
FILE *elf;
elf=fopen(argv[1],"rb");
uint32_t buffer[10000];
fread(buffer,sizeof(char),sizeof(buffer),elf);
crc32(buffer,10000);
}
It outputs a hex value, but its definitely wrong. I'm guessing its definitely not reading in the file correctly.