Why is the CRC calculation different between C and the C++ version of the code?

0

I have some code in C++ which I am porting to C. When I compute the CRC in the C code and for some reason, it returns a wrong CRC value while the C++ code works great. I am a novice when it comes to C++. I need some help understanding what I am doing wrong in the C code that's returning a wrong CRC value.

I created two separate files, one for C and one for C++ trying to achieve the same outcome in both.

/* 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);
}

The expected result of the CRC = 2637331102 (works in C++) Error result in C = 18446744072051915422

c++
c
asked on Stack Overflow Jul 4, 2019 by Vinay Divakar

1 Answer

10

This is almost definitely a 32-bit vs 64-bit compiler issue. Or at least sizeof(long) appears to be different between the two compiler modes.

Proof:

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

Notice that the latter half of the "C" result does match the C++ result. It's just that the "C" result is a 64-bit number. Presumably, 9D327A9E got sign-extended from a 32-bit value to a 64-bit signed value.

Change all these declarations in both implementations to be explicitly 32-bit:

From this:

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

To this:

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

You can #include <stdint.h> to get the uint32_t type.

As suggested in the comments below, change the return type of the function to be uint32_t as well. Better:

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);
}
answered on Stack Overflow Jul 4, 2019 by selbie • edited Jul 4, 2019 by selbie

User contributions licensed under CC BY-SA 3.0