#include #include #include "crc16tab.h" /* * Calculates CRC * * Copyright (c) 2022 - 2023 R.J. van der Putten, Leiden, Holland, * rob at sput dot nl. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ int main(void) { char line[4096]; unsigned char t; int i, len; unsigned int r; memset(line, 0, 4096); while (fgets(line + 2, 4088, stdin)) { i = 0; /* Cleanup */ len = strlen(line) - 1; if (line[len] == 10) { line[len] = 0; len--; } if (line[len] == 13) { line[len] = 0; len--; } len++; /* Pad for length of CRC */ line[len] = 0; len++; line[len] = 0; len++; t = 0; r = 0xFFFF; while (i < len) { /* Shift CRC width - 8 */ t = (r >> 8) & 0xFF; r = (r << 8) | (unsigned char) line[i]; r ^= crctab[t]; r = r & 0xFFFF; i++; } printf("Crc: %04X\n", r); } return(0); }