i'm really rusty when it comes with C and i need to understand code made by another developer, based around CRC32. I've got a ulong array
static const ulong crc32_table[256] =
{
0x00000000ul, 0x04c11db7ul, 0x09823b6eul, 0x0d4326d9ul,
0x130476dcul, 0x17c56b6bul, 0x1a864db2ul, 0x1e475005ul,
0x2608edb8ul, 0x22c9f00ful, 0x2f8ad6d6ul, 0x2b4bcb61ul,
0x350c9b64ul, 0x31cd86d3ul, 0x3c8ea00aul, 0x384fbdbdul,
...
};
This array is then used in ciphering data, this way:
void CRC32(const byte *buf, uint len, const byte init[4], byte crc[4]) {
memcpy(crc, init, 4);
while (len--) {
const byte * tmp = (const byte *)(crc32_table + (crc[3] ^ *buf));
crc[3] = crc[2] ^ tmp[3];
crc[2] = crc[1] ^ tmp[2];
crc[1] = crc[0] ^ tmp[1];
crc[0] = tmp[0];
++buf;
}
}
What i don't understand is the line :
const byte * tmp = (const byte *)(crc32_table + (crc[3] ^ *buf));
It seems the whole array is used in addition and casted to byte (uint in fact) but i'm not used to this kind of operation.
Can someone help me?
I need to write an equivalent to the CRC32 function but in C# Would this work:
uint[] crc = sharedkey;
uint[] buff = new uint[] { 0x4fu, 0xaeu, 0x07u, 0x0bu, 0x68u, 0x56u, 0x34u, 0x12u };
for(int i=0; i < len; i++)
{
byte[] tmp = BitConverter.GetBytes(crc32_table[(int)(crc[3] ^ buff[i])]);
crc[3] = crc[2] ^ tmp[3];
crc[2] = crc[1] ^ tmp[2];
crc[1] = crc[0] ^ tmp[1];
crc[0] = tmp[0];
}