Skip to content

Commit

Permalink
port: net: replace useless checksum with slightly more useful crc16
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Jan 14, 2024
1 parent 562e763 commit 4cb9950
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions port/src/net/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 6 additions & 3 deletions tools/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<H", data)[0]
Expand Down

0 comments on commit 4cb9950

Please sign in to comment.