Question

I am helping out a friend of mine who is a bit stuck and my own c++ skills are very rusty. My interest and curiosity is quite picked by this. so I shall try and explain this as best I can. Note its a 32 bit check.

uint32_t CRC32::calculate_CRC32(const uint32_t* plData, uint32_t lLength, uint32_t     previousCrc32)
 {
    uint32_t lCount;
    const uint32_t lPolynomial = 0x04C11DB7;
    uint32_t lCrc = previousCrc32;
    unsigned char* plCurrent = (unsigned char*) plData;
    lCrc ^= *plCurrent++;

    while (lLength-- != 0)
     {
       for (lCount = 0 ; lCount < lLength; lCount++)
       {
        if (lCrc & 1)
        lCrc = (lCrc >> 8) ^ lPolynomial;
           else
             lCrc = lCrc >> 8;
        }
    }

    return lCrc;
}

Now ILength is the number of bytes that the packet contains. plData is the packet for which data needs to be checked. As it is, the function works. But it works bit for bit. It needs to be improved to work byte for byte. So to all genius c++ developers out there who far surpasses my knowledge. Any ideas will be really helpful. Thanks in advance guys.

Was it helpful?

Solution

Read Ross Williams excellent tutorial on CRCs, especially section 9 on "A Table-Driven Implementation", which calculates the CRC a byte at a time instead of a bit at a time. You can also look at the somewhat more involved CRC implementation in zlib, which calculates it four bytes at a time. You can also calculate it eight bytes at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top