From f02291d07046e95a3b68c854e784382314e023a1 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Sat, 20 Jul 2024 19:15:37 +0200 Subject: [PATCH] remove crcengine dependency --- freedata_server/helpers.py | 158 ++++++++++++++++++++++---------- freedata_server/selftest.py | 1 - freedata_server/serial_ports.py | 6 +- requirements.txt | 1 - 4 files changed, 111 insertions(+), 55 deletions(-) diff --git a/freedata_server/helpers.py b/freedata_server/helpers.py index 1b414215..79d777e7 100644 --- a/freedata_server/helpers.py +++ b/freedata_server/helpers.py @@ -6,7 +6,6 @@ Created on Fri Dec 25 21:25:14 2020 """ import time from datetime import datetime,timezone -import crcengine import structlog import numpy as np import threading @@ -40,85 +39,146 @@ def wait(seconds: float) -> bool: def get_crc_8(data: str) -> bytes: - """Author: DJ2LS - - Get the CRC8 of a byte string - - param: data = bytes() + """ + Calculate CRC-8-CCITT checksum for the given data using the ITU I.432.1 specification. Args: - data: + data (str): Input data as a string. Returns: - CRC-8 (CCITT) of the provided data as bytes + bytes: CRC-8-CCITT checksum of the provided data. """ - if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): - data = bytes(data,"utf-8") + crc = 0x00 + polynomial = 0x07 + xor_out = 0x55 - crc_algorithm = crcengine.new("crc8-ccitt") # load crc8 library - crc_data = crc_algorithm(data) - crc_data = crc_data.to_bytes(1, byteorder="big") - return crc_data + if not isinstance(data, (bytes, bytearray)): + data = bytes(data, "utf-8") + + for byte in data: + crc ^= byte + for _ in range(8): + if crc & 0x80: + crc = (crc << 1) ^ polynomial + else: + crc <<= 1 + crc &= 0xFF + + # Final XOR value + crc ^= xor_out + return crc.to_bytes(1, byteorder="big") def get_crc_16(data: str) -> bytes: - """Author: DJ2LS - - Get the CRC16 of a byte string - - param: data = bytes() + """ + Calculate CRC-16-CCITT-FALSE checksum for the given data using the provided specification. Args: - data: + data (str): Input data as a string. Returns: - CRC-16 (CCITT) of the provided data as bytes + bytes: CRC-16-CCITT-FALSE checksum of the provided data. """ - if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): - data = bytes(data,"utf-8") - - crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc16 library - return crc_algorithm(data).to_bytes(2, byteorder="big") + crc = 0xFFFF + polynomial = 0x1021 + xor_out = 0 + if not isinstance(data, (bytes, bytearray)): + data = bytes(data, "utf-8") + + for byte in data: + crc ^= byte << 8 + for _ in range(8): + if crc & 0x8000: + crc = (crc << 1) ^ polynomial + else: + crc <<= 1 + crc &= 0xFFFF + + # Final XOR value + crc ^= xor_out + return crc.to_bytes(2, byteorder="big") + def get_crc_24(data: str) -> bytes: - """Author: DJ2LS - - Get the CRC24-OPENPGP of a byte string - https://github.com/GardenTools/CrcEngine#examples - - param: data = bytes() + """ + Calculate CRC-24-OPENPGP checksum for the given data using the provided specification. Args: - data: + data (str): Input data as a string. Returns: - CRC-24 (OpenPGP) of the provided data as bytes + bytes: CRC-24-OPENPGP checksum of the provided data. """ - if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): - data = bytes(data,'utf-8') + crc = 0xB704CE + polynomial = 0x864CFB + xor_out = 0 - params = crcengine.CrcParams(0x864cfb, 24, 0xb704ce, reflect_in=False, reflect_out=False, xor_out=0) - crc_algorithm = crcengine.create(params=params) - return crc_algorithm(data).to_bytes(3,byteorder="big") + if not isinstance(data, (bytes, bytearray)): + data = bytes(data, "utf-8") + + for byte in data: + crc ^= byte << 16 + for _ in range(8): + if crc & 0x800000: + crc = (crc << 1) ^ polynomial + else: + crc <<= 1 + crc &= 0xFFFFFF + + # Final XOR value + crc ^= xor_out + return crc.to_bytes(3, byteorder="big") def get_crc_32(data: str) -> bytes: - """Author: DJ2LS - - Get the CRC32 of a byte string - - param: data = bytes() + """ + Calculate CRC-32 checksum for the given data using the Ethernet specification. Args: - data: + data (str): Input data as a string. Returns: - CRC-32 of the provided data as bytes + bytes: CRC-32 checksum of the provided data. """ - if not isinstance(data, (bytes)) or isinstance(data, (bytearray)): + + def reflect(data, width): + """ + Reflects the bits in the given data. + + Args: + data (int): The data to reflect. + width (int): The bit width of the data. + + Returns: + int: The reflected data. + """ + reflected_data = 0 + for i in range(width): + if data & (1 << i): + reflected_data |= (1 << (width - 1 - i)) + return reflected_data + + crc = 0xFFFFFFFF + polynomial = 0x04C11DB7 + xor_out = 0 + + if not isinstance(data, (bytes, bytearray)): data = bytes(data, "utf-8") - crc_algorithm = crcengine.new("crc32") # load crc32 library - return crc_algorithm(data).to_bytes(4, byteorder="big") + + for byte in data: + byte = reflect(byte, 8) + crc ^= byte << 24 + for _ in range(8): + if crc & 0x80000000: + crc = (crc << 1) ^ polynomial + else: + crc <<= 1 + crc &= 0xFFFFFFFF + + crc = reflect(crc, 32) + crc ^= 0xFFFFFFFF + crc ^= xor_out + return crc.to_bytes(4, byteorder="big") from datetime import datetime, timezone diff --git a/freedata_server/selftest.py b/freedata_server/selftest.py index d3f4b1e9..fb271ef9 100644 --- a/freedata_server/selftest.py +++ b/freedata_server/selftest.py @@ -41,7 +41,6 @@ class TEST(): import threading import time import structlog - import crcengine import ctypes import glob import enum diff --git a/freedata_server/serial_ports.py b/freedata_server/serial_ports.py index 8c3a8ef9..3e2b3942 100644 --- a/freedata_server/serial_ports.py +++ b/freedata_server/serial_ports.py @@ -1,14 +1,12 @@ import serial.tools.list_ports -import crcengine - +import helpers def get_ports(): - crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc16 library serial_devices = [] ports = serial.tools.list_ports.comports(include_links=False) for port, desc, hwid in ports: # calculate hex of hwid if we have unique names - crc_hwid = crc_algorithm(bytes(hwid, encoding="utf-8")) + crc_hwid = helpers.get_crc_16(bytes(hwid, encoding="utf-8")) crc_hwid = crc_hwid.to_bytes(2, byteorder="big") crc_hwid = crc_hwid.hex() description = f"{desc} [{crc_hwid}]" diff --git a/requirements.txt b/requirements.txt index 07f2b1a8..bfc96205 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -crcengine numpy psutil PyAudio