diff --git a/port/src/net/net.c b/port/src/net/net.c index 8f9689050..f2e4d9399 100644 --- a/port/src/net/net.c +++ b/port/src/net/net.c @@ -368,11 +368,14 @@ static void netServerQueryResponse(ENetAddress *address) // calculate and rewrite checksum buf.wp = ebuf.dataLength - sizeof(u16); - u32 chk = 1; + u16 crc = 0xFFFF; + u16 x; for (u32 i = 0; i < buf.wp; ++i) { - chk = (chk + buf.data[i]) % 65521; + x = crc >> 8 ^ buf.data[i]; + x ^= x >> 4; + crc += (crc << 8) ^ (x << 12) ^ (x << 5) ^ x; } - netbufWriteU16(&buf, chk); + netbufWriteU16(&buf, crc); enet_socket_send(g_NetHost->socket, address, &ebuf, 1); } diff --git a/tools/query.py b/tools/query.py index 7f976a038..4187e18ad 100644 --- a/tools/query.py +++ b/tools/query.py @@ -9,10 +9,13 @@ MAX_WAIT = 5.0 def checksum(data): - chk = 1 + crc = 0xFFFF for b in data[:-2]: - chk = (chk + b) % 65521 - return chk + x = crc >> 8 ^ b + x ^= x >> 4 + crc += (crc << 8) ^ (x << 12) ^ (x << 5) ^ x + crc &= 0xFFFF + return crc def eat_string(data): strlen = struct.unpack_from("