c - 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);
}
它输出一个十六进制值,但它肯定是错误的。我猜它肯定没有正确读取文件。
解决方案
推荐阅读
- javascript - 如何在 iframe 中更改音乐音量?
- asp.net-core - Asp.Net Core 3.1 - BackgroundService 在 IIS 启动/重新启动后不会自动启动
- sql - 算术溢出 nvarchar
- javascript - 服务器套接字 java 和 xmlHttpreuest javaScript 未收到响应
- reactjs - 函数隐含返回类型“任何”错误
- database - 基于 Postgres 中另一个表中的整数列限制关系行数的最佳方法
- merge - 数据仓库事实表 - MERGE OR DROP & INSERT
- powerbi - 磁盘上的存储大小可以比压缩后的大小减少 20%
- c++ - clang 不支持使用模板的显式构造函数调用
- storage - 游戏如何存储故事情节之类的数据?