Same crc32 for Python and C

c, crc32, python

Solution

I'm using right now zlib.crc32, but for C there is no such library

Um, yes, there is. It's called zlib. zlib is written in C, and it's what Python is using! Hence the name of the class.

You can use the `crc32()` function in zlib. That implementation is a fair bit faster than others you might find. Read zlib.h for the interface information.

You can compile zlib yourself, or it may already be installed on your system.

Update:

I now see your comment (which should be edited into the question since it is critical to getting the right answer) that you have extremely limited memory. Then you can use this:

static uint32_t crc32(uint32_t crc, unsigned char *buf, size_t len)
{
    int k;

    crc = ~crc;
    while (len--) {
        crc ^= *buf++;
        for (k = 0; k < 8; k++)
            crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
    }
    return ~crc;
}

The crc is initially set to zero.

The use of `~` will give the correct result, since the `uint32_t` type in `stdint.h` is assured to be 32 bits.

If you can afford a little more code space, then unrolling the loop will likely speed it up (if the compiler doesn't already do this):

static uint32_t crc32(uint32_t crc, unsigned char *buf, size_t len)
{
    crc = ~crc;
    while (len--) {
        crc ^= *buf++;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
        crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
    }
    return ~crc;
}

You said that you only have 4 KBytes of "memory". Is that just working memory for the program, or does the program have to live there as well? If you have more space in flash for example for the code, then the table can be precomputed and stored with the code. A table-driven CRC will be much faster. The zlib code provides table-driven CRCs that do one byte at a time and four-bytes at a time, requiring respectively a 1Kbyte or 4Kbyte table.

Update 2:

Since you answered in a comment that the 4KBytes are just working memory, then you should use a table-driven CRC. You can simply use the `crc32()` function in zlib's `crc32.c` and the table in `crc32.h` with `BYFOUR` undefined.

Problem

I need script that will calculate crc32 with the same output for both Python and C. I'm using right now zlib.crc32, but for C there is no such library and we are writing it on our own basing on Wikipedia. But it doesn't return the same value. This is our code of C script (copied from wikipedia, based on RFC): ``` unsigned int crc32( unsigned char *message, unsigned int n ) { //int i, crc; unsigned int crc; unsigned int i; unsigned int byte, c; const unsigned int g0 = 0xEDB88320, g1 = g0>>1, g2 = g0>>2, g3 = g0>>3, g4 = g0>>4, g5 = g0>>5, g6 = (g0>>6)^g0, g7 = ((g0>>6)^g0)>>1; i = 0; crc = 0xFFFFFFFF; //while ((byte = message[i]) != 0) while( i != n) { byte = message[i]; // Get next byte. // byte = FrmReadByte( i ); // Get next byte. crc = crc ^ byte; c = ((crc<<31>>31) & g7) ^ ((crc<<30>>31) & g6) ^ ((crc<<29>>31) & g5) ^ ((crc<<28>>31) & g4) ^ ((crc<<27>>31) & g3) ^ ((crc<<26>>31) & g2) ^ ((crc<<25>>31) & g1) ^ ((crc<<24>>31) & g0); crc = ((unsigned)crc >> 8) ^ c; i = i + 1; } return ~crc; } ``` EDIT: We've got only 4KB of ram memory, program itself doesn't live there. Taking 1KB of memory by crc32 script is probably too much and wouldn't fit there. Thanks for pointing out that ZLIB library exists for C as well.

Original source