[英]Why is the CRC calculation different between C and the C++ version of the code?
我有一些要移植到C的C ++代碼。當我在C代碼中計算CRC時,由於某種原因,當C ++代碼運行良好時,它將返回錯誤的CRC值。 我是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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.