diff options
author | eihrul <eihrul> | 2009-03-27 06:48:52 +0000 |
---|---|---|
committer | eihrul <eihrul> | 2009-03-27 06:48:52 +0000 |
commit | 1dc1a6372d9c437451a59cb0144ae78144862f85 (patch) | |
tree | 2c7a0c2889adb36af7eb6c218dadd095ac225334 /packet.c | |
parent | 7851c6305b2e90cf0083ffa01eaa9cabf311a2e1 (diff) | |
download | enet-1dc1a6372d9c437451a59cb0144ae78144862f85.tar.gz enet-1dc1a6372d9c437451a59cb0144ae78144862f85.zip |
fixed broken crc calculation
Diffstat (limited to 'packet.c')
-rw-r--r-- | packet.c | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -91,13 +91,28 @@ enet_packet_resize (ENetPacket * packet, size_t dataLength) static int initializedCRC32 = 0; static enet_uint32 crcTable [256]; -static void initialize_crc32 () +static enet_uint32 +reflect_crc (int val, int bits) +{ + int result = 0, bit; + + for (bit = 0; bit < bits; bit ++) + { + if(val & 1) result |= 1 << (bits - 1 - bit); + val >>= 1; + } + + return result; +} + +static void +initialize_crc32 () { int byte; for (byte = 0; byte < 256; ++ byte) { - enet_uint32 crc = byte << 24; + enet_uint32 crc = reflect_crc (byte, 8) << 24; int offset; for(offset = 0; offset < 8; ++ offset) @@ -108,7 +123,7 @@ static void initialize_crc32 () crc <<= 1; } - crcTable [byte] = crc; + crcTable [byte] = reflect_crc (crc, 32); } initializedCRC32 = 1; @@ -128,7 +143,7 @@ enet_crc32 (const ENetBuffer * buffers, size_t bufferCount) while (data < dataEnd) { - crc = ((crc << 8) | * data ++) ^ crcTable [crc >> 24]; + crc = (crc >> 8) ^ crcTable [(crc & 0xFF) ^ *data++]; } ++ buffers; |