Add mg_crc16(), make PPP use it

This commit is contained in:
Sergio R. Caprile committed 2026-07-29 17:57:30 -03:00
1 parent 03be16803c
commit 7821fb9ab4
7 files changed
+65 -38

No files matched your search

+14
View File
@@ -128,6 +128,20 @@ uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) {
return ~crc;
}
uint16_t mg_crc16(uint16_t crc, const char *buf, size_t len) {
static const uint16_t crclut[16] = {
// table for polynomial 0x8408 (reflected)
0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387,
0x8408, 0x9489, 0xA50A, 0xB58B, 0xC60C, 0xD68D, 0xE70E, 0xF78F};
unsigned int c = (unsigned int) ~crc & 0xffff;
while (len--) {
uint8_t b = *(uint8_t *) buf++;
c = crclut[(c ^ b) & 0x0F] ^ (c >> 4);
c = crclut[(c ^ (b >> 4)) & 0x0F] ^ (c >> 4);
}
return (uint16_t) ~c;
}
static int isbyte(int n) {
return n >= 0 && n <= 255;
}