././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 011452 x ustar 00 0000000 0000000 28 mtime=1600722827.4327836
crccheck-1.0/ 0000777 0000000 0000000 00000000000 00000000000 011252 5 ustar 00 0000000 0000000 ././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 011452 x ustar 00 0000000 0000000 28 mtime=1600722827.4327836
crccheck-1.0/PKG-INFO 0000666 0000000 0000000 00000013015 00000000000 012347 0 ustar 00 0000000 0000000 Metadata-Version: 1.1
Name: crccheck
Version: 1.0
Summary: Calculation library for CRCs and checksums
Home-page: https://sourceforge.net/projects/crccheck/
Author: Martin Scharrer
Author-email: martin@scharrer-online.de
License: GPL v3+
Download-URL: https://sourceforge.net/projects/crccheck/files/
Description: crccheck - Classes to calculate CRCs and checksums from binary data
===================================================================
The ``crccheck.crc`` module implements all CRCs listed in the
`Catalogue of parametrised CRC algorithms `_:
CRC-3/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU,
CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7,
CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A,
CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC,
CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250,
CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY,
CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC,
CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000,
CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757,
CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740,
CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B,
X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT,
KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5,
CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU,
CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE,
CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD,
CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP,
CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D,
CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX,
CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32,
CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM,
CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, CRC-82/DARC
For the class names simply remove all dashes and slashes from the above names and apply CamelCase, e.g.
"CRC-32/MPEG-2" is implemented by ``Crc32Mpeg2``. Other CRC can be calculated by using the general class
``crccheck.crc.Crc`` by providing all required CRC parameters.
The ``crccheck.checksum`` module implements additive and XOR checksums with 8, 16 and 32 bit:
``Checksum8``, ``Checksum16``, ``Checksum32`` and ``ChecksumXor8``, ``ChecksumXor16``, ``ChecksumXor32``
Usage example::
from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32
# Quick calculation
data = bytearray.fromhex("DEADBEEF")
crc = Crc32.calc(data)
checksum = Checksum32.calc(data)
# Procsss multiple data buffers
data1 = b"Binary string" # or use .encode(..) on normal sring - Python 3 only
data2 = bytes.fromhex("1234567890") # Python 3 only, use bytearray for older versions
data3 = (0x0, 255, 12, 99) # Iterable which returns ints in byte range (0..255)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])
crcbytes = crcinst.finalbytes()
crchex = crcinst.finalhex()
crcint = crcinst.final()
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Embedded Systems
Classifier: Topic :: Utilities
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 011453 x ustar 00 0000000 0000000 22 mtime=1600722381.0
crccheck-1.0/README.rst 0000666 0000000 0000000 00000007676 00000000000 012761 0 ustar 00 0000000 0000000 crccheck - Classes to calculate CRCs and checksums from binary data
===================================================================
The ``crccheck.crc`` module implements all CRCs listed in the
`Catalogue of parametrised CRC algorithms `_:
CRC-3/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU,
CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7,
CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A,
CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC,
CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250,
CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY,
CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC,
CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000,
CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757,
CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740,
CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B,
X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT,
KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5,
CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU,
CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE,
CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD,
CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP,
CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D,
CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX,
CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32,
CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM,
CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, CRC-82/DARC
For the class names simply remove all dashes and slashes from the above names and apply CamelCase, e.g.
"CRC-32/MPEG-2" is implemented by ``Crc32Mpeg2``. Other CRC can be calculated by using the general class
``crccheck.crc.Crc`` by providing all required CRC parameters.
The ``crccheck.checksum`` module implements additive and XOR checksums with 8, 16 and 32 bit:
``Checksum8``, ``Checksum16``, ``Checksum32`` and ``ChecksumXor8``, ``ChecksumXor16``, ``ChecksumXor32``
Usage example::
from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32
# Quick calculation
data = bytearray.fromhex("DEADBEEF")
crc = Crc32.calc(data)
checksum = Checksum32.calc(data)
# Procsss multiple data buffers
data1 = b"Binary string" # or use .encode(..) on normal sring - Python 3 only
data2 = bytes.fromhex("1234567890") # Python 3 only, use bytearray for older versions
data3 = (0x0, 255, 12, 99) # Iterable which returns ints in byte range (0..255)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])
crcbytes = crcinst.finalbytes()
crchex = crcinst.finalhex()
crcint = crcinst.final()
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 011452 x ustar 00 0000000 0000000 28 mtime=1600722827.3887978
crccheck-1.0/crccheck/ 0000777 0000000 0000000 00000000000 00000000000 013017 5 ustar 00 0000000 0000000 ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 011453 x ustar 00 0000000 0000000 22 mtime=1600722381.0
crccheck-1.0/crccheck/__init__.py 0000666 0000000 0000000 00000011733 00000000000 015135 0 ustar 00 0000000 0000000 """ Classes to calculate CRCs and checksums from binary data
========================================================
The :mod:`crccheck.crc` module implements all CRCs listed in the
`Catalogue of parametrised CRC algorithms `_:
CRC-3/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU,
CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7,
CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A,
CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC,
CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250,
CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY,
CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC,
CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000,
CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757,
CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740,
CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B,
X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT,
KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5,
CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU,
CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE,
CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD,
CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP,
CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D,
CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX,
CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32,
CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM,
CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, CRC-82/DARC.
For the class names simply remove all dashes and slashes from the above names and apply CamelCase, e.g.
"CRC-32/MPEG-2" is implemented by :class:`.Crc32Mpeg2`. Other CRC can be calculated by using the general class
:class:`crccheck.crc.Crc` by providing all required CRC parameters.
The :mod:`crccheck.checksum` module implements additive and XOR checksums with 8, 16 and 32 bit:
:class:`.Checksum8`, :class:`.Checksum16`, :class:`.Checksum32` and
:class:`.ChecksumXor8`, :class:`.ChecksumXor16`, :class:`.ChecksumXor32`.
Usage example::
from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32
# Quick calculation
data = bytearray.fromhex("DEADBEEF")
crc = Crc32.calc(data)
checksum = Checksum32.calc(data)
# Procsss multiple data buffers
data1 = b"Binary string" # or use .encode(..) on normal sring - Python 3 only
data2 = bytes.fromhex("1234567890") # Python 3 only, use bytearray for older versions
data3 = (0x0, 255, 12, 99) # Iterable which returns ints in byte range (0..255)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])
crcbytes = crcinst.finalbytes()
crchex = crcinst.finalhex()
crcint = crcinst.final()
License::
Copyright (C) 2015-2020 by Martin Scharrer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
from crccheck import crc, checksum
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 011453 x ustar 00 0000000 0000000 22 mtime=1600722381.0
crccheck-1.0/crccheck/base.py 0000666 0000000 0000000 00000020661 00000000000 014310 0 ustar 00 0000000 0000000 """ Base class for CRC and checksum classes.
License::
Copyright (C) 2015-2020 by Martin Scharrer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
import math
REFLECT_BIT_ORDER_TABLE = (
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF,
)
def reflectbitorder(width, value):
""" Reflects the bit order of the given value according to the given bit width.
Args:
width (int): bitwidth
value (int): value to reflect
"""
binstr = ("0"*width + bin(value)[2:])[-width:]
return int(binstr[::-1], 2)
class CrccheckError(Exception):
"""General checksum error exception"""
pass
class CrccheckBase(object):
""" Abstract base class for checksumming classes.
Args:
initvalue (int): Initial value. If None then the default value for the class is used.
"""
_initvalue = 0x00
_check_result = None
_check_data = None
_file_chunksize = 512
_width = 0
def __init__(self, initvalue=None):
if initvalue is None:
self._value = self._initvalue
else:
self._value = initvalue
def reset(self, value=None):
""" Reset instance.
Resets the instance state to the initial value.
This is not required for a just created instance.
Args:
value (int): Set internal value. If None then the default initial value for the class is used.
Returns:
self
"""
if value is None:
self._value = self._initvalue
else:
self._value = value
return self
def process(self, data):
""" Process given data.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
Returns:
self
"""
raise NotImplementedError
def final(self):
"""Return final check value.
The internal state is not modified by this so further data can be processed afterwards.
Return:
int: final value
"""
return self._value
def finalhex(self, byteorder='big'):
"""Return final checksum value as hexadecimal string (without leading "0x"). The hex value is zero padded to bitwidth/8.
The internal state is not modified by this so further data can be processed afterwards.
Return:
str: final value as hex string without leading '0x'.
"""
asbytes = self.finalbytes(byteorder)
try:
return asbytes.hex()
except AttributeError:
return "".join(["{:02x}".format(b) for b in asbytes])
def finalbytes(self, byteorder='big'):
"""Return final checksum value as bytes.
The internal state is not modified by this so further data can be processed afterwards.
Return:
bytes: final value as bytes
"""
bytelength = int(math.ceil(self._width / 8.0))
asint = self.final()
try:
return asint.to_bytes(bytelength, byteorder)
except AttributeError:
asbytes = bytearray(bytelength)
for i in range(0, bytelength):
asbytes[i] = asint & 0xFF
asint >>= 8
if byteorder == 'big':
asbytes.reverse()
return asbytes
def value(self):
"""Returns current intermediate value.
Note that in general final() must be used to get the a final value.
Return:
int: current value
"""
return self._value
@classmethod
def calc(cls, data, initvalue=None, **kwargs):
""" Fully calculate CRC/checksum over given data.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
initvalue (int): Initial value. If None then the default value for the class is used.
Return:
int: final value
"""
inst = cls(initvalue, **kwargs)
inst.process(data)
return inst.final()
@classmethod
def calchex(cls, data, initvalue=None, byteorder='big', **kwargs):
"""Fully calculate checksum over given data. Return result as hex string.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
initvalue (int): Initial value. If None then the default value for the class is used.
byteorder ('big' or 'little'): order (endianness) of returned bytes.
Return:
str: final value as hex string without leading '0x'.
"""
inst = cls(initvalue, **kwargs)
inst.process(data)
return inst.finalhex(byteorder)
@classmethod
def calcbytes(cls, data, initvalue=None, byteorder='big', **kwargs):
"""Fully calculate checksum over given data. Return result as bytearray.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
initvalue (int): Initial value. If None then the default value for the class is used.
byteorder ('big' or 'little'): order (endianness) of returned bytes.
Return:
bytes: final value as bytes
"""
inst = cls(initvalue, **kwargs)
inst.process(data)
return inst.finalbytes(byteorder)
@classmethod
def selftest(cls, data=None, expectedresult=None, **kwargs):
""" Selftest method for automated tests.
Args:
data (bytes, bytearray or list of int [0-255]): data to process
expectedresult (int): expected result
Raises:
CrccheckError: if result is not as expected
"""
if data is None:
data = cls._check_data
expectedresult = cls._check_result
result = cls.calc(data, **kwargs)
if result != expectedresult:
raise CrccheckError("{:s}: expected {:s}, got {:s}".format(cls.__name__, hex(expectedresult), hex(result)))
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 011453 x ustar 00 0000000 0000000 22 mtime=1600722381.0
crccheck-1.0/crccheck/checksum.py 0000666 0000000 0000000 00000017005 00000000000 015176 0 ustar 00 0000000 0000000 """ Classes to calculated additive and XOR checksums.
License::
Copyright (C) 2015-2020 by Martin Scharrer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
from crccheck.base import CrccheckBase, CrccheckError
class ChecksumBase(CrccheckBase):
""" Base class for all checksum classes.
Args:
initvalue (int): Initial value. If None then the default value for the class is used.
byteorder ('big' or 'little'): byte order (endianness) used when reading the input bytes.
"""
_width = 0
_mask = 0
_check_data = (0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0x55, 0xC2, 0x8C)
_check_result_littleendian = None
def __init__(self, initvalue=0, byteorder='big'):
super(ChecksumBase, self).__init__(initvalue)
self._byteorder = byteorder
def process(self, data):
""" Process given data.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
Returns:
self
"""
dataword = 0
n = 0
bigendian = (self._byteorder == 'big')
width = self._width
mask = self._mask
value = self._value
for byte in data:
if bigendian:
dataword = (dataword << 8) | byte
else:
dataword |= (byte << n)
n += 8
if n == width:
value = mask & (value + dataword)
dataword = 0
n = 0
self._value = value
return self
@classmethod
def selftest(cls, data=None, expectedresult=None, byteorder='big'):
""" Selftest method for automated tests.
Args:
data (bytes, bytearray or list of int [0-255]): data to process
expectedresult (int): expected result
byteorder ('big' or 'little'): byte order (endianness) used when reading the input bytes.
Raises:
CrccheckError: if result is not as expected
"""
if data is None:
data = cls._check_data
if expectedresult is None:
if byteorder == 'big':
expectedresult = cls._check_result
else:
expectedresult = cls._check_result_littleendian
result = cls.calc(data, byteorder=byteorder)
if result != expectedresult:
raise CrccheckError(hex(result))
class Checksum32(ChecksumBase):
""" 32-bit checksum.
Calculates 32-bit checksum by adding the input bytes in groups of four.
Input data length must be a multiple of four, otherwise the last bytes are not used.
"""
_width = 32
_mask = 0xFFffFFff
_check_result = 0x8903817B
_check_result_littleendian = 0x7C810388
class Checksum16(ChecksumBase):
""" 16-bit checksum.
Calculates 16-bit checksum by adding the input bytes in groups of two.
Input data length must be a multiple of two, otherwise the last byte is not used.
"""
_width = 16
_mask = 0xFFff
_check_result = 0x0A7D
_check_result_littleendian = 0x8008
class Checksum8(ChecksumBase):
""" 8-bit checksum.
Calculates 8-bit checksum by adding the input bytes.
"""
_width = 8
_mask = 0xFF
_check_result = 0x85
_check_result_littleendian = _check_result
class ChecksumXorBase(ChecksumBase):
""" Base class for all XOR checksum classes. """
def process(self, data):
""" Process given data.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
Returns:
self
"""
dataword = 0
n = 0
bigendian = (self._byteorder == 'big')
width = self._width
mask = self._mask
value = self._value
for byte in data:
if bigendian:
dataword = (dataword << 8) | byte
else:
dataword |= (byte << n)
n += 8
if n == width:
value = mask & (value ^ dataword)
dataword = 0
n = 0
self._value = value
return self
class ChecksumXor32(ChecksumXorBase):
""" 32-bit XOR checksum.
Calculates 32-bit checksum by XOR-ing the input bytes in groups of four.
Input data length must be a multiple of four, otherwise the last bytes are not used.
"""
_width = 32
_mask = 0xFFffFFff
_check_result = 0x74F87C63
_check_result_littleendian = 0x637CF874
class ChecksumXor16(ChecksumXorBase):
""" 16-bit XOR checksum.
Calculates 16-bit checksum by XOR-ing the input bytes in groups of two.
Input data length must be a multiple of two, otherwise the last byte is not used.
"""
_width = 16
_mask = 0xFFff
_check_result = 0x089B
_check_result_littleendian = 0x9B08
class ChecksumXor8(ChecksumXorBase):
""" 8-bit XOR checksum.
Calculates 8-bit checksum by XOR-ing the input bytes.
"""
_width = 8
_mask = 0xFF
_check_result = 0x93
_check_result_littleendian = _check_result
class Checksum(ChecksumBase):
""" General additive checksum.
Args:
width (int): bit width of checksum. Must be positive and a multiple of 8.
initvalue (int): Initial value. If None then the default value for the class is used.
byteorder ('big' or 'little'): byte order (endianness) used when reading the input bytes.
"""
_check_result = None
_check_result_littleendian = None
def __init__(self, width, initvalue=0, byteorder='big'):
super(Checksum, self).__init__(initvalue, byteorder)
width = int(width)
if width <= 0 or width % 8 != 0:
raise ValueError("width must be postive and a multiple of 8")
self._width = width
self._mask = (1 << width) - 1
class ChecksumXor(ChecksumXorBase):
""" General XOR checksum.
Args:
width (int): bit width of checksum. Must be positive and a multiple of 8.
initvalue (int): Initial value. If None then the default value for the class is used.
byteorder ('big' or 'little'): byte order (endianness) used when reading the input bytes.
"""
_check_result = None
_check_result_littleendian = None
def __init__(self, width, initvalue=0, byteorder='big'):
super(ChecksumXor, self).__init__(initvalue, byteorder)
width = int(width)
if width <= 0 or width % 8 != 0:
raise ValueError("width must be postive and a multiple of 8")
self._width = width
self._mask = (1 << width) - 1
ALLCHECKSUMCLASSES = (
Checksum8, Checksum16, Checksum32,
ChecksumXor8, ChecksumXor16, ChecksumXor32,
)
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 011453 x ustar 00 0000000 0000000 22 mtime=1600722381.0
crccheck-1.0/crccheck/crc.py 0000666 0000000 0000000 00000151465 00000000000 014154 0 ustar 00 0000000 0000000 """ Classes to calculate CRCs (Cyclic Redundancy Check).
License::
Copyright (C) 2015-2020 by Martin Scharrer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
from crccheck.base import CrccheckBase, reflectbitorder, REFLECT_BIT_ORDER_TABLE, CrccheckError
class CrcBase(CrccheckBase):
"""Abstract base class for all Cyclic Redundancy Checks (CRC) checksums"""
_names = ()
_width = 0
_poly = 0x00
_initvalue = 0x00
_reflect_input = False
_reflect_output = False
_xor_output = 0x00
_check_result = None
_check_data = bytearray(b"123456789")
_residue = None
def process(self, data):
""" Process given data.
Args:
data (bytes, bytearray or list of ints [0-255]): input data to process.
Returns:
self
"""
crc = self._value
highbit = 1 << (self._width - 1)
mask = ((highbit - 1) << 1) | 0x1 # done this way to avoid overrun for 64-bit values
poly = self._poly
shift = self._width - 8
diff8 = -shift
if diff8 > 0:
# enlarge temporary to fit 8-bit
mask = 0xFF
crc <<= diff8
shift = 0
highbit = 0x80
poly = self._poly << diff8
reflect = self._reflect_input
for byte in data:
if reflect:
byte = REFLECT_BIT_ORDER_TABLE[byte]
crc ^= (byte << shift)
for i in range(0, 8):
if crc & highbit:
crc = (crc << 1) ^ poly
else:
crc = (crc << 1)
crc &= mask
if diff8 > 0:
crc >>= diff8
self._value = crc
return self
def final(self):
""" Return final CRC value.
Return:
int: final CRC value
"""
crc = self._value
if self._reflect_output:
crc = reflectbitorder(self._width, crc)
crc ^= self._xor_output
return crc
def __eq__(self, other):
return self._width == other._width and \
self._poly == other._poly and \
self._initvalue == other._initvalue and \
self._reflect_input == other._reflect_input and \
self._reflect_output == other._reflect_output and \
self._xor_output == other._xor_output
def __repr__(self):
residue = hex(self._residue) if self._residue is not None else 'None'
check_result = hex(self._check_result) if self._check_result is not None else 'None'
return ("Crc(width={:d}, poly=0x{:x}, initvalue=0x{:X}, reflect_input={!s:s}, reflect_output={!s:s}, " +
"xor_output=0x{:x}, check_result={}, residue={})").format(
self._width, self._poly, self._initvalue, self._reflect_input, self._reflect_output,
self._xor_output, check_result, residue)
def find(classes=None, width=None, poly=None, initvalue=None, reflect_input=None, reflect_output=None, xor_output=None,
check_result=None, residue=None):
"""Find CRC classes which the matching properties.
Args:
classes (None or list): List of classes to search in. If None the list ALLCRCCLASSES will be used.
width (None or int): number of bits of the CRC classes to find
poly (None or int): polygon to find
initvalue (None or int): initvalue to find
reflect_input (None or bool): reflect_input to find
reflect_output (None or bool): reflect_output to find
xor_output (None or int): xor_output to find
check_result (None or int): check_result to find
residue (None or int): residue to find
Returns:
List of CRC classes with the selected properties.
Examples:
Find all CRC16 classes:
$ find(width=16)
Find all CRC32 classes with all-1 init value and XOR output:
$ find(width=32, initvalue=0xFFFF, xor_output=0xFFFF)
"""
found = list()
if classes is None:
classes = ALLCRCCLASSES
for cls in classes:
if width is not None and width != cls._width:
continue
if poly is not None and poly != cls._poly:
continue
if initvalue is not None and initvalue != cls._initvalue:
continue
if reflect_input is not None and reflect_input != cls._reflect_input:
continue
if reflect_output is not None and reflect_output != cls._reflect_output:
continue
if xor_output is not None and xor_output != cls._xor_output:
continue
if check_result is not None and check_result != cls._check_result:
continue
if residue is not None and residue != cls._residue:
continue
found.append(cls)
return found
def identify(data, crc, width=None, classes=None, one=True):
"""
Identify the used CRC algorithm which was used to calculate the CRC from some data.
This function can be used to identify a suitable CRC class if the exact CRC algorithm/parameters
are not known, but a CRC value is known from some data. Note that this function can be quite
time consuming on large data, especially if the given width is not known.
Args:
data (bytes): Data to compare with the `crc`.
crc (int): Known CRC of the given `data`.
width (int or None): Known bit width of given `crc`.
Providing the width will speed up the identification of the CRC algorithm.
classes (iterable or None): Listing of classes to check. If None then ALLCRCCLASSES is used.
one (bool): If True then only the first found CRC class is retunred.
Otherwise a list of all suitable CRC classes.
Returns:
If `one` is True:
CRC class which instances produce the given CRC from the given data.
If no CRC class could be found `None` is returned.
If `one` is False:
List of CRC classes which instances produce the given CRC from the given data.
The list may be empty.
"""
if classes is None:
classes = ALLCRCCLASSES
if width is not None:
classes = (cls for cls in classes if cls._width == width)
found = []
for cls in classes:
if cls().calc(data) == crc:
if one:
return cls
found.append(cls)
if one:
return None
return found
class Crc(CrcBase):
""" Creates a new general (user-defined) CRC calculator instance.
Arguments:
width (int): bit width of CRC.
poly (int): polynomial of CRC with the top bit omitted.
initvalue (int): initial value of internal running CRC value. Usually either 0 or (1<