""" minimateplus/crc.py — Instantel's record checksum, shared by every reader that checks one. CRC-16/AUG-CCITT: poly 0x1021, init 0x1D0F, no reflection, no final XOR. The same rule covers a Micromate's setup blocks (0x1A, 0x08, 0x2E; stored big-endian), a Blastware event file's footer (big-endian) and every .MLG record (little-endian). Each block says where its checksum sits; this module only computes it. """ from __future__ import annotations _POLY = 0x1021 _INIT = 0x1D0F def crc16_aug_ccitt(data: bytes) -> int: """CRC-16/AUG-CCITT of `data` (check value: b"123456789" -> 0xE5CC).""" c = _INIT for b in data: c ^= b << 8 for _ in range(8): c = ((c << 1) ^ _POLY) & 0xFFFF if c & 0x8000 else (c << 1) & 0xFFFF return c