I have to do a lookup in a table, and I have a string identifier and the CRC32 of the string. If there is a miss, I have to decrement the size and lookup for a prefix of the identifier. Therefore, I would need to calculate the checksum of the prefix and repeat the process for each prefix.
My C code of this algorithm is something like this:
find_prefix(char* string, uint16_t size, uint32_t crc, hash_table_t *hash_table){
do{
if(perform_lookup(string, size, crc, hash_table)==HIT){
return size;
}
size--;
crc=calculate_crc(string, size, 0xFFFFFFFF); //Is there a better way?
} while(size);
}
My question is: can I avoid the crc calculation and derive the crc of the prefix, given the crc of the whole string and the string itself?
I found some related questions here and here, but I have a constraint: when the table is previosly populated, the crc of the identifier is calculated in hardware, so I can't modify the algorithm, and both the links provide as answer a different way of calculating the checksum (basically using XOR of all the components).
Thank you very much.
No, you'd need the bits that were shifted out while the last character was integrated into the CRC.
You could unroll the loop a bit, and calculate the CRC of
(string, size - 8)
,(string, size - 7)
, ... in a single iteration.Yes, it is possible. (Aside from the trivial observation that if you have the string as stated in the question, then you can simply compute the CRC of the prefix.)
To rephrase your question, I have two strings A and B, where their concatenation is AB. If I have only the CRC of AB and I have the string B, can I compute the CRC of A?
You can just reverse the process of computing the CRC, using the high byte of the CRC to determine which table entry was used. It's almost as fast as computing the CRC of B. Example code for the standard CRC-32 used in zip, gzip, etc.: