gpt4 book ai didi

c++ - 为什么 CRC 和 C++ 代码版本的 CRC 计算不同?

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:35 26 4
gpt4 key购买 nike

我有一些 C++ 代码,我正在将其移植到 C。当我在 C 代码中计算 CRC 时,出于某种原因,它返回错误的 CRC 值,而 C++ 代码运行良好。我是 C++ 的新手。我需要一些帮助来理解我在返回错误 CRC 值的 C 代码中做错了什么。

我创建了两个单独的文件,一个用于 C,一个用于 C++,试图在两者中实现相同的结果。

/* C++ */

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

// Calculate crc32 checksum the way CC2538 and CC2650 does it.
int calcCrcLikeChip(const unsigned char *pData, unsigned long ulByteCount)
{
unsigned long d, ind;
unsigned long acc = 0xFFFFFFFF;
const unsigned long ulCrcRand32Lut[] =
{
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC,
0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C,
0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
};

while ( ulByteCount-- )
{
d = *pData++;
ind = (acc & 0x0F) ^ (d & 0x0F);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
ind = (acc & 0x0F) ^ (d >> 4);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
}

return (acc ^ 0xFFFFFFFF);
}

std::string fileName; // File name
int main ()
{
uint32_t byteCount = 0; // File size in bytes
static std::vector<char> pvWrite(1);// Vector to application firmware in.
static std::ifstream file; // File stream
uint32_t fileCrc;
fileName = "multi_role.bin";
file.open(fileName.c_str(), std::ios::binary);
if(file.is_open())
{
//
// Get file size:
//
file.seekg(0, std::ios::end);
byteCount = (uint32_t)file.tellg();
printf("%u\r\n", byteCount);
file.seekg(0, std::ios::beg);

//
// Read data
//
pvWrite.resize(byteCount);
file.read((char*) &pvWrite[0], byteCount);
}
else
{
cout << "Unable to open file " << fileName.c_str();
}

fileCrc = calcCrcLikeChip((unsigned char *)&pvWrite[0], byteCount);
printf("%u\r\n", fileCrc);
}

/* C */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

const char *filename = "multi_role.bin";

int calcCrcLikeChip(const unsigned char *pData, unsigned long ulByteCount)
{
unsigned long d, ind;
unsigned long acc = 0xFFFFFFFF;
const unsigned long ulCrcRand32Lut[] =
{
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC,
0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C,
0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
};

while (ulByteCount--)
{
d = *pData++;
ind = (acc & 0x0F) ^ (d & 0x0F);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
ind = (acc & 0x0F) ^ (d >> 4);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
}

return (acc ^ 0xFFFFFFFF);
}

/****************************************************************
* Function Name : openFile
* Description : Opens the file
* Returns : NULL on failure
* Params @file: Path to the file to be opened
****************************************************************/
FILE *openFile(const char *file)
{
FILE *fp = fopen(file, "rb");
return(fp);
}

/****************************************************************
* Function Name : getFileSize
* Description : Gets the size of the file to be read
* Returns : 0 on failure
* Params @fp: File descriptor
****************************************************************/
long int getFileSize(FILE *fp)
{
/* Go to end of file */
if(fseek(fp, 0L, SEEK_END))
return (0);

/* Get the size */
long int sz = ftell(fp);

/* Put the curser back to 0 */
if(fseek(fp,0L,SEEK_SET))
return (0);
return sz;
}


int main()
{
static FILE *fPtr = NULL;
unsigned char *memPtr = NULL; /* Ptr to hold read data */
unsigned long fileCrc;
long fileSz = 0;

fPtr = openFile(filename);
fileSz = getFileSize(fPtr);
printf("%lu\n", fileSz);
memPtr = (unsigned char*)calloc(fileSz, sizeof(unsigned char));
fread((char*)memPtr,1 , fileSz, fPtr);
fileCrc = calcCrcLikeChip((const unsigned char*)memPtr, fileSz);
printf("fileCrc: %lu\n", fileCrc);
}

CRC 的预期结果 = 2637331102(适用于 C++)C中的错误结果=18446744072051915422

最佳答案

这几乎肯定是 32 位与 64 位编译器的问题。或者至少 sizeof(long)两种编译器模式之间似乎有所不同。

证明:

C++:              2637331102 == ‭         9D327A9E
C : 18446744072051915422 == FFFFFFFF 9D327A9E

请注意,“C”结果的后半部分确实与 C++ 结果匹配。只是“C”结果是一个 64 位数字。据推测,9D327A9E 已从 32 位值符号扩展为 64 位有符号值。

将两个实现中的所有这些声明显式更改为 32 位:

来自这里:

unsigned long d, ind;
unsigned long acc = 0xFFFFFFFF;
const unsigned long ulCrcRand32Lut[] =

对此:

uint32_t d, ind;
uint32_t acc = 0xFFFFFFFF;
const uint32_t ulCrcRand32Lut[] =

您可以 #include <stdint.h>得到uint32_t类型。

按照下面评论中的建议,将函数的返回类型更改为 uint32_t以及。更好:

uint32_t calcCrcLikeChip(const unsigned char *pData, unsigned long ulByteCount)
{
uint32_t d, ind;
uint32_t acc = 0xFFFFFFFF;
const uint32_t ulCrcRand32Lut[] =
{
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC,
0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C,
0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
};

while ( ulByteCount > 0 )
{
ulByteCount--;
d = *pData++;
ind = (acc & 0x0F) ^ (d & 0x0F);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
ind = (acc & 0x0F) ^ (d >> 4);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
}

return (acc ^ 0xFFFFFFFF);
}

关于c++ - 为什么 CRC 和 C++ 代码版本的 CRC 计算不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com