1
votes

I have problem with understanding how CRC32 should work in normal way.
I've implemented mechanism from wiki and other sites: https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Computation where you xor elements bit by bit.
For CRC32 I've used Polynomial from wiki, which is also everywhere:

x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
with binary representation: 1 0000 0100 1100 0001 0001 1101 1011 0111

I was calculating CRC32 for input string "1234" only for testing. This is the output of program:
https://i.stack.imgur.com/tG4wk.png
as you can see the xor is calculated properly and CRC32 is "619119D1".
When I calculate it using online calculator or even c++ boost lib, the answer is "9BE3E0A3".
What is wrong with normal XORing input string bit by bit? Should I add something at the end or what? I don't want to use libs and any other magic code to compute this, because I have to implement it in that way for my study project. I've tried also polynomial without x^32, negate bits at the end, starting from 1s instead of 0s (where you have to add 32 zeros), and the answer is also different. I have no idea what should I do now to fix this. This is the part of the code (a bit changed), I have buffor 3parts * 32bits, I'm loading 4 Chars from file to the middle part and xor from beggining to the middle, at the end I xor the middle part and the end -> the end is CRC32.

My pseudo schema:
1) Load 8 chars
2) | First part | Middle Part | CRC32 = 0 |
3) XOR
4) | 0 0 0 0 | XXXXXXX | 0 0 0 0 |
5) memcpy - middle part to first part
6) | XXXXXXX | XXXXXXX | 0 0 0 0  |
7) Load 4 chars
8) | XXXXXXX | loaded 4chars | 0 0 0 0 |
9) repeat from point 4 to the end of file
10) now we have: | 0 0 0 0 | XXXXXX | 0 0 0 0 |
11) last xor from middle part to end
12) Result: | 0 0 0 0 |  0 0 0 0 | CRC32 |

Probably screen with output will be more helpful.
I will use smart pointers etc. later ;)

bool xorBuffer(unsigned char *buffer) {
    bool * binaryTab = nullptr;
    try {
        // CRC-32
        // 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
        //  1  0  0  0  0  0  1  0  0  1  1  0  0  0  0  0  1  0  0  0  1  1  1  0  1  1  0  1  1  0  1  1  1
        const int dividerSizeBits = 33;
        const bool binaryDivider[dividerSizeBits] = { 1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1 };

        const int dividerLength = countLength(binaryDivider, dividerSizeBits);
        const int dividerOffset = dividerSizeBits - dividerLength;  // when divider < 33 bits

        bool * binaryTab = charTabToBits(buffer);

        // check tab if first part = 0
        while (!checkTabIfEmpty(binaryTab)) {
            // set the beginnning
            int start = 0;
            for (start = 0; start < 32; start++)
                if (binaryTab[start] == true)
                    break;
            for (int i = 0; i < dividerLength; i++)
                binaryTab[i + start] = binaryTab[i + start] ^ binaryDivider[i + dividerOffset];    
        }
        // binaryTab -> charTab
        convertBinaryTabToCharTab(binaryTab, buffer);
    }
    catch (exception e) {
        delete[] binaryTab;
        return false;
    }
    delete[] binaryTab;
    return true;
}

std::string CRC::countCRC(std::string fileName){
    // create variables
    int bufferOnePartSize = 4;
    int bufferSize = bufferOnePartSize * 3;
    bool EOFFlag = false;
    unsigned char *buffer = new unsigned char[bufferSize];

    for (int i = 0; i < 3 * bufferOnePartSize; i++)
        buffer[i] = 0;

    // open file
    ifstream fin;
    fin.open(fileName.c_str(), ios_base::in | ios_base::binary);

    int position = 0;
    int count = 0;
    // while -> EOF
    if (fin.is_open()) {
        // TODO check if file <= 4 -> another solution
        char ch;
        int multiply = 2;
        bool skipNormalXor = false;
        while (true) {
            count = 0;
            if (multiply == 2)
                position = 0;
            else
                position = bufferOnePartSize;
            // copy part form file to tab
            while (count < bufferOnePartSize * multiply && fin.get(ch)) {
                buffer[position] = (unsigned char)ch;
                ++count;
                ++position;
            }
            cout << endl;
            // if EOF write zeros to end of tab
            if (count == 0) {
                cout << "TODO: end of file" << endl;
                EOFFlag = true;
                skipNormalXor = true;
            }
            else if (count != bufferOnePartSize * multiply) {
                for (int i = count; i < bufferOnePartSize * multiply; i++) {
                    buffer[position] = 0;
                    position++;
                }
                EOFFlag = true;
            }

            if (!skipNormalXor) {
                // -- first part
                multiply = 1;
                // xor the buffer
                xorBuffer(buffer);
            }

            if (EOFFlag) {  // xor to the end
                xorBuffer(buffer + bufferOnePartSize);
                break;
            }
            else {
                // copy memory
                for (int i = 0; i < bufferOnePartSize; i++)
                    buffer[i] = buffer[i + bufferOnePartSize];
            }
        }
        cout << "\n End\n";
        fin.close();
    }

    stringstream crcSum;
    for (int i = 2 * bufferOnePartSize; i < bufferSize; i++) {
        //buffer[i] = ~buffer[i];
        crcSum << std::hex << (unsigned int)buffer[i];
    }
    cout << endl << "CRC: " << crcSum.str() << endl;
    delete[] buffer;
    return crcSum.str();
}
1
Standard crc32 calculation begins with value of 0xffffffff not 0. Then at end invert all bits by xor with 0xffffffff.Cmaster

1 Answers

2
votes

A CRC is not defined by just the polynomial. You need to define the bit ordering, the initial value of the CRC register, and the final exclusive-or of the CRC. For the standard CRC-32, which gives 0x9be3e0a3 for "1234", the bits are processed starting with the least significant bit, the initial value of the register is 0xffffffff, and you exclusive-or the final results with 0xffffffff.