首页 > 解决方案 > ELF文件的CRC32?任意长度?

问题描述

所以我需要计算一个 ELF 文件的 CRC32 校验和,而我只是在用 C 苦苦挣扎。我需要弄清楚的第一件事是将数据输入校验和算法的最佳方法。如果 ELF 文件是任意大小,并且我试图以二进制形式读取它,那么存储该数据的最佳方法是什么,以便我可以将其提供给校验和公式?谢谢。

这是我现在所拥有的。

#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);
}

它输出一个十六进制值,但它肯定是错误的。我猜它肯定没有正确读取文件。

标签: cchecksumcrccrc32

解决方案


推荐阅读