diff --git a/api/crc16/index.html b/api/crc16/index.html index d50a64f..55df2b4 100644 --- a/api/crc16/index.html +++ b/api/crc16/index.html @@ -470,6 +470,51 @@ + + +
IBM = Configuration(width=16, polynomial=32773, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+MAXIM = Configuration(width=16, polynomial=32773, init_value=0, final_xor_value=65535, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+USB = Configuration(width=16, polynomial=32773, init_value=65535, final_xor_value=65535, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+X25 = Configuration(width=16, polynomial=4129, init_value=65535, final_xor_value=65535, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+DNP = Configuration(width=16, polynomial=15717, init_value=0, final_xor_value=65535, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+ITU = Configuration(width=8, polynomial=7, init_value=0, final_xor_value=85, reverse_input=False, reverse_output=False)
+
+
+ class-attribute
+ instance-attribute
+
+
+ROHC = Configuration(width=8, polynomial=7, init_value=255, final_xor_value=0, reverse_input=True, reverse_output=True)
+
+
+ class-attribute
+ instance-attribute
+
+
+Add support for the following Crc8 configurations:
+Add support for the following Crc16 configurations:
+Calculate CRC checksums, verify CRC checksum, predefined CRC configurations, custom CRC configurations
The library includes a variety of common CRC configurations for convenience. To explore the full range of available CRC configurations, please checkout the configurations section of the documentation. If you need a new configuration to be readily available, consider submitting a PR or raising an issue.
"},{"location":"#custom-configurations","title":"Custom Configurations","text":"If you want to create a custom configuration, you should have the following information available:
In case you only have a name of a specific crc configuration/algorithm and you are unsure what are the specific parameters of it, a look into this crc-catalogue might help.
NoteThis library currently only supports bit widths of full bytes 8, 16, 24, 32, ...
"},{"location":"#requirements","title":"Requirements","text":"pip install crc\n
"},{"location":"#examples","title":"Examples","text":""},{"location":"#create-a-calculator","title":"Create a Calculator","text":"Pre defined configurationCustom configuration from crc import Calculator, Crc8\n\ncalculator = Calculator(Crc8.CCITT)\n
from crc import Calculator, Configuration\n\nconfig = Configuration(\n width=8,\n poly=0x07,\n init_value=0x00,\n final_xor_value=0x00,\n reverse_input=False,\n reverse_output=False,\n)\n\ncalculator = Calculator(config)\n
"},{"location":"#calculate-a-checksum","title":"Calculate a checksum","text":"StandardOptimized for speed from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT)\n\nassert expected == calculator.checksum(data)\n
from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert expected == calculator.checksum(data)\n
"},{"location":"#verify-a-checksum","title":"Verify a checksum","text":"StandardOptimized for speed from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT)\n\nassert calculator.verify(data, expected)\n
from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.verify(data, expected)\n
"},{"location":"#supported-data-types","title":"Supported data types","text":"intbytesbytearrayFileByteIoIterable of bytesByte convertibles from crc import Calculator, Crc8\n\nexpected = 0x20\ndata = 97\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = b\"123456789\"\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = bytearray(b\"123456789\")\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nwith open(\"afile.txt\", \"rb\") as f:\n assert calculator.checksum(f) == expected\n
import io\n\nfrom crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = io.ByteIo(b\"123456789\")\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\ndata = (data for data in [b\"12\", b\"34\", b\"56\", b\"78\", b\"9\"])\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\n\nclass ByteConvertible:\n def __init__(self, data):\n self._data = data\n\n def __bytes__(self):\n return self._data.encode(\"utf-8\")\n\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\ndata = ByteConvertible(\"123456789\")\n\nassert calculator.checksum(bytes(data)) == expected\n
"},{"location":"#calculate-a-checksum-with-raw-registers","title":"Calculate a checksum with raw registers","text":"RegisterTableBasedRegister from crc import Crc8, Register\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\nregister = Register(Crc8.CCITT)\n\nregister.init()\nregister.update(data)\nassert expected == register.digest()\n
from crc import Crc8, TableBasedRegister\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\nregister = TableBasedRegister(Crc8.CCITT)\n\nregister.init()\nregister.update(data)\nassert expected == register.digest()\n
"},{"location":"#references-resources","title":"References & Resources","text":"The crc library comes with a small command line tool which can generate crc lookup tables if needed.
usage: crc table [-h] <width> <polynomial>\n\npositional arguments:\n <width> width of the crc algorithm, common width's are 8, 16, 32, 64\n <polynomial> hex value of the polynomial used for calculating the crc table\n\noptional arguments:\n -h, --help show this help message and exit\n
Example Usage:
user@host ~$ crc table 8 0x7D\n
"},{"location":"configurations/","title":"Configurations","text":""},{"location":"configurations/#crc8","title":"Crc8","text":"CCITTSAEJ1850SAEJ1850_ZEROAUTOSARBLUETOOTHMAXIM_DOW Thank you to all contributors for your help in improving this project. \ud83d\ude80
Abstract base class / Interface a crc register needs to implement.
Workflowdigest()
abstractmethod
","text":"Final crc checksum will be calculated.
Returns:
Type Descriptionint
Final crc result/value (applies pending operations like final xor).
"},{"location":"api/abstract_register/#crc.AbstractRegister.init","title":"init()
abstractmethod
","text":"Initializes the crc register.
"},{"location":"api/abstract_register/#crc.AbstractRegister.reverse","title":"reverse()
abstractmethod
","text":"Calculates the reversed value of the crc register.
Returns:
Type Descriptionint
The reversed value of the crc register.
"},{"location":"api/abstract_register/#crc.AbstractRegister.update","title":"update(data)
abstractmethod
","text":"Feeds data into the register.
Parameters:
Name Type Description Defaultdata
bytes
which will be feed into the register.
requiredReturns:
Type Descriptionint
Register content after the update.
"},{"location":"api/basic_register/","title":"BasicRegister","text":" Bases: AbstractRegister
Implements the common crc algorithm, assuming a user of this base class will provide an overwrite for the _process_byte method.
"},{"location":"api/basic_register/#crc.BasicRegister.__getitem__","title":"__getitem__(index)
","text":"Gets a single byte of the register.
Parameters:
Name Type Description Defaultindex
int
byte which shall be returned.
requiredReturns:
Type Descriptionint
The byte at the specified index.
Raises:
Type DescriptionIndexError
Invalid index for this register.
"},{"location":"api/basic_register/#crc.BasicRegister.__init__","title":"__init__(configuration)
","text":"Create a new BasicRegister.
Parameters:
Name Type Description Defaultconfiguration
Configuration
Used to configure the crc algorithm.
required"},{"location":"api/basic_register/#crc.BasicRegister.__len__","title":"__len__()
","text":"Returns:
Type Descriptionint
The width of the register.
"},{"location":"api/basic_register/#crc.BasicRegister.digest","title":"digest()
","text":"See AbstractRegister.digest
init()
","text":"See AbstractRegister.init
reverse()
","text":"See AbstractRegister.digest
update(data)
","text":"See AbstractRegister.update
InputType = Union[int, bytes, bytearray, memoryview, BinaryIO, Iterable[Union[bytes, bytearray, memoryview]]]
module-attribute
","text":"Type alias for acceptable input types for Calculator.
"},{"location":"api/calculator/#crc.Calculator.__init__","title":"__init__(configuration, optimized=False)
","text":"Creates a new Calculator.
Parameters:
Name Type Description Defaultconfiguration
Configuration
for the crc algorithm.
requiredoptimized
bool
whether a register optimized for speed shall be used.
False
initializing an optimized calculator might take some extra time, calculation itself will be faster though.
"},{"location":"api/calculator/#crc.Calculator.checksum","title":"checksum(data)
","text":"Calculates the checksum for the given data.
Parameters:
Name Type Description Defaultdata
InputType
which will be used as input for the checksum.
requiredReturns:
Type Descriptionint
Checksum for the given input data.
"},{"location":"api/calculator/#crc.Calculator.verify","title":"verify(data, expected)
","text":"Verifies that the checksum for the given data is the expected one.
Parameters:
Name Type Description Defaultdata
InputType
which will be used as input for the checksum.
requiredexpected
int
checksum.
requiredReturns:
Type Descriptionbool
True if the expected checksum matches the actual checksum for the given data, False otherwise.
"},{"location":"api/configuration/","title":"Configuration","text":"A Configuration provides all settings necessary to determine the concrete implementation of a specific crc algorithm/register.
ExampleCreate a custom configuration
from crc import Configuration\n\nsaej1850 = Configuration(\n width=8,\n polynomial=0x1D,\n init_value=0xFF,\n final_xor_value=0xFF,\n reverse_input=False,\n reverse_output=False\n)\n
"},{"location":"api/configuration/#crc.Configuration.width","title":"width: int
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.polynomial","title":"polynomial: int
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.init_value","title":"init_value: int = 0
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.final_xor_value","title":"final_xor_value: int = 0
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.reverse_input","title":"reverse_input: bool = False
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.reverse_output","title":"reverse_output: bool = False
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/","title":"Crc16","text":""},{"location":"api/crc16/#available-configurations","title":"Available Configurations","text":" Bases: Enum
GSM = Configuration(width=16, polynomial=4129, init_value=0, final_xor_value=65535, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.PROFIBUS","title":"PROFIBUS = Configuration(width=16, polynomial=7631, init_value=65535, final_xor_value=65535, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/","title":"Crc32","text":""},{"location":"api/crc32/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CRC32 = Configuration(width=32, polynomial=79764919, init_value=4294967295, final_xor_value=4294967295, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.AUTOSAR","title":"AUTOSAR = Configuration(width=32, polynomial=4104977171, init_value=4294967295, final_xor_value=4294967295, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.BZIP2","title":"BZIP2 = Configuration(width=32, polynomial=79764919, init_value=4294967295, final_xor_value=4294967295, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.POSIX","title":"POSIX = Configuration(width=32, polynomial=79764919, init_value=0, final_xor_value=4294967295, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc64/","title":"Crc64","text":""},{"location":"api/crc64/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CRC64 = Configuration(width=64, polynomial=4823603603198064275, init_value=0, final_xor_value=0, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/","title":"Crc8","text":""},{"location":"api/crc8/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CCITT = Configuration(width=8, polynomial=7, init_value=0, final_xor_value=0, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.AUTOSAR","title":"AUTOSAR = Configuration(width=8, polynomial=47, init_value=255, final_xor_value=255, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.BLUETOOTH","title":"BLUETOOTH = Configuration(width=8, polynomial=167, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.SAEJ1850","title":"SAEJ1850 = Configuration(width=8, polynomial=29, init_value=255, final_xor_value=255, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.MAXIM_DOW","title":"MAXIM_DOW = Configuration(width=8, polynomial=49, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/register/","title":"Register","text":" Bases: BasicRegister
Simple crc register, which will process one bit at the time.
NoteIf performance is an important issue for the crc calculation use a table based register.
"},{"location":"api/table_based_register/","title":"TableBasedRegister","text":" Bases: BasicRegister
Lookup table based crc register.
InfoThis register type will be much faster than a simple bit by bit based crc register like Register
.
__init__(configuration)
","text":"Creates a new table based crc register.
Parameters:
Name Type Description Defaultconfiguration
Configuration
used for the crc algorithm.
required AttentionCreating a table based register initially might take some extra time, due to the fact that some lookup tables need to be calculated/initialized.
"},{"location":"changelog/changes_2.0.0/","title":"2.0.0 - 2022-11-27","text":""},{"location":"changelog/changes_2.0.0/#breaking-api-changes","title":"\ud83d\udea8 Breaking API Changes","text":"** Renamed **
Classes:
AbstractCrcRegister
-> AbstractRegister
CrcRegisterBase
-> BasicRegister
CrcRegister
-> Register
TableBasedCrcRegister
-> TableBasedRegister
CrcCalculator
-> Calculator
Functions & Methods:
CrcCalculator.calculate_checksum
-> Calculator.checksum
CrcCalculator.verify_checksum
-> Calculator.verify
argument_parser()
-> [private] _argument_parser()
Arguments:
CrcCalculator(configuration, table_based=False)
-> Calculator(configuration, optimized=False)
** Removed **
CRC_TYPES
mappingchecksum
function/cli-entry-pointCRC_TYPES
mapping** Renamed **
Functions & Methods:
Renamed keyword argument expected_checksum
of method Calculator.verify
to expected
def verify(self, data: bytes, expected_checksum: int ) -> bool:\n ...\n
def verify(self, data: Union[int, ByteString, BinaryIO, Iterable[ByteString]], expected: int ) -> bool:\n ...\n
bytes
to Calculator.checksum
and Calculator.verify
(int
, ByteString
, BinaryIO
, Iterable[ByteString]
)
\ud83d\ude80 This release is powered by Gert van Dijk, thank you for your contribution!
"},{"location":"changelog/changes_5.0.0/#breaking-changes","title":"\ud83d\udea8 Breaking Changes","text":"Move from a single-file/module to an \u2018src-based layout\u2019 package.
The layout of the source repository has changed from single file crc.py
in the root into a package where crc.py
is now a private module _crc.py
of package crc
.
src/\n\u2514\u2500\u2500 crc/\n \u251c\u2500\u2500 _crc.py\n \u2514\u2500\u2500 __init__.py\n
__init__.py
re-exports all the public entities from the private module, so that typical imports like from crc import Calculator
remain working as-is.
The need and choice for this new layout is discussed in Issue #110.
The shebang line on the main src/crc/_crc.py
file (formerly crc.py
) has been removed.
This shouldn't affect anyone installing crc
as package. Both the entrypoint crc
and the package-level __main__
remain in place.
However, in case you were to obtain the src/crc/_crc.py
file (formerly crc.py
) and mark it as executable afterwards to run it directly, then this change may affect you. Running the main()
function can then still be achieved by either:
python path/to/crc.py
python -m crc
(if the module is on your PYTHON_PATH
)py.typed
marker to indicate the crc
package ships with type information. The type information was added inline since 2.0.0, but the marker file was missing in the package, so type checkers did not consider the type information. This eliminates the use of ignore_missing_imports
(mypy) or reportMissingTypeStubs
(Pylance/Pyright) in downstream projects.start
argument in range()
.Adjusted the SAE-J1850 configuration to match the specification
\ud83d\udea8\ufe0f For users which do rely on the previously misconfigured SAEJ1850
settings a configuration named SAEJ1850_ZERO
was added.
python 3.12
to test matrix\ud83d\ude80 This release is powered by Riccardo Malavolti, thank you for your contribution!
"},{"location":"changelog/changes_6.1.0/#features","title":"\u2728 Features","text":"\ud83d\ude80 This release is powered by Ramya-Ab, thank you for your contribution!
"},{"location":"changelog/changes_6.1.1/#documentation","title":"\ud83d\udcda Documentation","text":"Fixed unstable return values of digest
function. For more details, see issue #151.
Bug
This issue specifically affected scenarios where the CRC register was manually manipulated. Standard usages of the Calculator
class were not impacted. Furthermore, this issue primarily occurred in configurations that required reverse output.
Crc16.CCITT
configuration to Crc16.XMODEM
.Crc16.KERMIT
, which matches the official configuration for Crc16.CCITT
.It was intentionally decided not to reintroduce Crc16.CCITT
with the updated configuration. While it could have been added as an alias for Crc16.KERMIT
or a replacement, omitting Crc16.CCITT
ensures that client code will break upon update, thereby forcing maintainers to take notice and react accordingly.
Below are solutions to the two common scenarios that need to be addressed due to this change:
If you previously used Crc16.CCITT
and expected the configuration defined here:
Solution: Replace all usages of Crc16.CCITT
in your code with Crc16.KERMIT
.
If you depended on or wanted to use the configuration values that Crc16.CCITT
provided so far:
Solution: Replace all usages of Crc16.CCITT
in your code with Crc16.XMODEM
.
python-environment
actionpyproject.toml
unreleased.md
to changes_{X.Y.Z}.md
changes_{X.Y.Z}.md
to reflect the release version# X.Y.Z - YYYY-MM-DD\n
invoke release.prepare X.Y.Z\n
release.workflow
task and follow potential instructions. invoke release.workflow X.Y.Z\n
Follow the poetry installation instructions.
"},{"location":"development/setup/#2-install-gh","title":"2. Install gh","text":"Follow the gh installation instructions.
"},{"location":"development/setup/#3-checkout-the-project","title":"3. Checkout the project","text":"GitHub CLISSHHTTPSgh repo clone Nicoretti/crc\n
git clone git@github.com:Nicoretti/crc.git\n
git clone https://github.com/Nicoretti/crc.git\n
"},{"location":"development/setup/#4-switch-into-the-directory","title":"4. Switch into the directory","text":"cd crc\n
"},{"location":"development/setup/#5-the-poetry-environment","title":"5. The Poetry environment","text":"Make sure the poetry environment is setup properly and all dependencies are installed.
Activate the Poetry shell
poetry shell\n
Install the project dependencies
poetry install\n
init
task","text":"In order to bootstrap the remaining parts of the workspace setup, just execute the following command:
invoke init\n
Note
Follow potential instructions.
"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":"CRCCalculate CRC checksums, verify CRC checksum, predefined CRC configurations, custom CRC configurations
The library includes a variety of common CRC configurations for convenience. To explore the full range of available CRC configurations, please checkout the configurations section of the documentation. If you need a new configuration to be readily available, consider submitting a PR or raising an issue.
"},{"location":"#custom-configurations","title":"Custom Configurations","text":"If you want to create a custom configuration, you should have the following information available:
In case you only have a name of a specific crc configuration/algorithm and you are unsure what are the specific parameters of it, a look into this crc-catalogue might help.
NoteThis library currently only supports bit widths of full bytes 8, 16, 24, 32, ...
"},{"location":"#requirements","title":"Requirements","text":"pip install crc\n
"},{"location":"#examples","title":"Examples","text":""},{"location":"#create-a-calculator","title":"Create a Calculator","text":"Pre defined configurationCustom configuration from crc import Calculator, Crc8\n\ncalculator = Calculator(Crc8.CCITT)\n
from crc import Calculator, Configuration\n\nconfig = Configuration(\n width=8,\n poly=0x07,\n init_value=0x00,\n final_xor_value=0x00,\n reverse_input=False,\n reverse_output=False,\n)\n\ncalculator = Calculator(config)\n
"},{"location":"#calculate-a-checksum","title":"Calculate a checksum","text":"StandardOptimized for speed from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT)\n\nassert expected == calculator.checksum(data)\n
from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert expected == calculator.checksum(data)\n
"},{"location":"#verify-a-checksum","title":"Verify a checksum","text":"StandardOptimized for speed from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT)\n\nassert calculator.verify(data, expected)\n
from crc import Calculator, Crc8\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.verify(data, expected)\n
"},{"location":"#supported-data-types","title":"Supported data types","text":"intbytesbytearrayFileByteIoIterable of bytesByte convertibles from crc import Calculator, Crc8\n\nexpected = 0x20\ndata = 97\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = b\"123456789\"\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = bytearray(b\"123456789\")\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nwith open(\"afile.txt\", \"rb\") as f:\n assert calculator.checksum(f) == expected\n
import io\n\nfrom crc import Calculator, Crc8\n\nexpected = 0xF4\ndata = io.ByteIo(b\"123456789\")\ncalculator = Calculator(Crc8.CCITT, optimized=True)\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\ndata = (data for data in [b\"12\", b\"34\", b\"56\", b\"78\", b\"9\"])\n\nassert calculator.checksum(data) == expected\n
from crc import Calculator, Crc8\n\n\nclass ByteConvertible:\n def __init__(self, data):\n self._data = data\n\n def __bytes__(self):\n return self._data.encode(\"utf-8\")\n\n\nexpected = 0xF4\ncalculator = Calculator(Crc8.CCITT, optimized=True)\ndata = ByteConvertible(\"123456789\")\n\nassert calculator.checksum(bytes(data)) == expected\n
"},{"location":"#calculate-a-checksum-with-raw-registers","title":"Calculate a checksum with raw registers","text":"RegisterTableBasedRegister from crc import Crc8, Register\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\nregister = Register(Crc8.CCITT)\n\nregister.init()\nregister.update(data)\nassert expected == register.digest()\n
from crc import Crc8, TableBasedRegister\n\nexpected = 0xBC\ndata = bytes([0, 1, 2, 3, 4, 5])\nregister = TableBasedRegister(Crc8.CCITT)\n\nregister.init()\nregister.update(data)\nassert expected == register.digest()\n
"},{"location":"#references-resources","title":"References & Resources","text":"The crc library comes with a small command line tool which can generate crc lookup tables if needed.
usage: crc table [-h] <width> <polynomial>\n\npositional arguments:\n <width> width of the crc algorithm, common width's are 8, 16, 32, 64\n <polynomial> hex value of the polynomial used for calculating the crc table\n\noptional arguments:\n -h, --help show this help message and exit\n
Example Usage:
user@host ~$ crc table 8 0x7D\n
"},{"location":"configurations/","title":"Configurations","text":""},{"location":"configurations/#crc8","title":"Crc8","text":"CCITTSAEJ1850SAEJ1850_ZEROAUTOSARBLUETOOTHMAXIM_DOWITUROHC Thank you to all contributors for your help in improving this project. \ud83d\ude80
Abstract base class / Interface a crc register needs to implement.
Workflowdigest()
abstractmethod
","text":"Final crc checksum will be calculated.
Returns:
Type Descriptionint
Final crc result/value (applies pending operations like final xor).
"},{"location":"api/abstract_register/#crc.AbstractRegister.init","title":"init()
abstractmethod
","text":"Initializes the crc register.
"},{"location":"api/abstract_register/#crc.AbstractRegister.reverse","title":"reverse()
abstractmethod
","text":"Calculates the reversed value of the crc register.
Returns:
Type Descriptionint
The reversed value of the crc register.
"},{"location":"api/abstract_register/#crc.AbstractRegister.update","title":"update(data)
abstractmethod
","text":"Feeds data into the register.
Parameters:
Name Type Description Defaultdata
bytes
which will be feed into the register.
requiredReturns:
Type Descriptionint
Register content after the update.
"},{"location":"api/basic_register/","title":"BasicRegister","text":" Bases: AbstractRegister
Implements the common crc algorithm, assuming a user of this base class will provide an overwrite for the _process_byte method.
"},{"location":"api/basic_register/#crc.BasicRegister.__getitem__","title":"__getitem__(index)
","text":"Gets a single byte of the register.
Parameters:
Name Type Description Defaultindex
int
byte which shall be returned.
requiredReturns:
Type Descriptionint
The byte at the specified index.
Raises:
Type DescriptionIndexError
Invalid index for this register.
"},{"location":"api/basic_register/#crc.BasicRegister.__init__","title":"__init__(configuration)
","text":"Create a new BasicRegister.
Parameters:
Name Type Description Defaultconfiguration
Configuration
Used to configure the crc algorithm.
required"},{"location":"api/basic_register/#crc.BasicRegister.__len__","title":"__len__()
","text":"Returns:
Type Descriptionint
The width of the register.
"},{"location":"api/basic_register/#crc.BasicRegister.digest","title":"digest()
","text":"See AbstractRegister.digest
init()
","text":"See AbstractRegister.init
reverse()
","text":"See AbstractRegister.digest
update(data)
","text":"See AbstractRegister.update
InputType = Union[int, bytes, bytearray, memoryview, BinaryIO, Iterable[Union[bytes, bytearray, memoryview]]]
module-attribute
","text":"Type alias for acceptable input types for Calculator.
"},{"location":"api/calculator/#crc.Calculator.__init__","title":"__init__(configuration, optimized=False)
","text":"Creates a new Calculator.
Parameters:
Name Type Description Defaultconfiguration
Configuration
for the crc algorithm.
requiredoptimized
bool
whether a register optimized for speed shall be used.
False
initializing an optimized calculator might take some extra time, calculation itself will be faster though.
"},{"location":"api/calculator/#crc.Calculator.checksum","title":"checksum(data)
","text":"Calculates the checksum for the given data.
Parameters:
Name Type Description Defaultdata
InputType
which will be used as input for the checksum.
requiredReturns:
Type Descriptionint
Checksum for the given input data.
"},{"location":"api/calculator/#crc.Calculator.verify","title":"verify(data, expected)
","text":"Verifies that the checksum for the given data is the expected one.
Parameters:
Name Type Description Defaultdata
InputType
which will be used as input for the checksum.
requiredexpected
int
checksum.
requiredReturns:
Type Descriptionbool
True if the expected checksum matches the actual checksum for the given data, False otherwise.
"},{"location":"api/configuration/","title":"Configuration","text":"A Configuration provides all settings necessary to determine the concrete implementation of a specific crc algorithm/register.
ExampleCreate a custom configuration
from crc import Configuration\n\nsaej1850 = Configuration(\n width=8,\n polynomial=0x1D,\n init_value=0xFF,\n final_xor_value=0xFF,\n reverse_input=False,\n reverse_output=False\n)\n
"},{"location":"api/configuration/#crc.Configuration.width","title":"width: int
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.polynomial","title":"polynomial: int
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.init_value","title":"init_value: int = 0
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.final_xor_value","title":"final_xor_value: int = 0
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.reverse_input","title":"reverse_input: bool = False
class-attribute
instance-attribute
","text":""},{"location":"api/configuration/#crc.Configuration.reverse_output","title":"reverse_output: bool = False
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/","title":"Crc16","text":""},{"location":"api/crc16/#available-configurations","title":"Available Configurations","text":" Bases: Enum
GSM = Configuration(width=16, polynomial=4129, init_value=0, final_xor_value=65535, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.PROFIBUS","title":"PROFIBUS = Configuration(width=16, polynomial=7631, init_value=65535, final_xor_value=65535, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.IBM","title":"IBM = Configuration(width=16, polynomial=32773, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.MAXIM","title":"MAXIM = Configuration(width=16, polynomial=32773, init_value=0, final_xor_value=65535, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.USB","title":"USB = Configuration(width=16, polynomial=32773, init_value=65535, final_xor_value=65535, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.X25","title":"X25 = Configuration(width=16, polynomial=4129, init_value=65535, final_xor_value=65535, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc16/#crc.Crc16.DNP","title":"DNP = Configuration(width=16, polynomial=15717, init_value=0, final_xor_value=65535, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/","title":"Crc32","text":""},{"location":"api/crc32/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CRC32 = Configuration(width=32, polynomial=79764919, init_value=4294967295, final_xor_value=4294967295, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.AUTOSAR","title":"AUTOSAR = Configuration(width=32, polynomial=4104977171, init_value=4294967295, final_xor_value=4294967295, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.BZIP2","title":"BZIP2 = Configuration(width=32, polynomial=79764919, init_value=4294967295, final_xor_value=4294967295, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc32/#crc.Crc32.POSIX","title":"POSIX = Configuration(width=32, polynomial=79764919, init_value=0, final_xor_value=4294967295, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc64/","title":"Crc64","text":""},{"location":"api/crc64/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CRC64 = Configuration(width=64, polynomial=4823603603198064275, init_value=0, final_xor_value=0, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/","title":"Crc8","text":""},{"location":"api/crc8/#available-configurations","title":"Available Configurations","text":" Bases: Enum
CCITT = Configuration(width=8, polynomial=7, init_value=0, final_xor_value=0, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.AUTOSAR","title":"AUTOSAR = Configuration(width=8, polynomial=47, init_value=255, final_xor_value=255, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.BLUETOOTH","title":"BLUETOOTH = Configuration(width=8, polynomial=167, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.SAEJ1850","title":"SAEJ1850 = Configuration(width=8, polynomial=29, init_value=255, final_xor_value=255, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.MAXIM_DOW","title":"MAXIM_DOW = Configuration(width=8, polynomial=49, init_value=0, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.ITU","title":"ITU = Configuration(width=8, polynomial=7, init_value=0, final_xor_value=85, reverse_input=False, reverse_output=False)
class-attribute
instance-attribute
","text":""},{"location":"api/crc8/#crc.Crc8.ROHC","title":"ROHC = Configuration(width=8, polynomial=7, init_value=255, final_xor_value=0, reverse_input=True, reverse_output=True)
class-attribute
instance-attribute
","text":""},{"location":"api/register/","title":"Register","text":" Bases: BasicRegister
Simple crc register, which will process one bit at the time.
NoteIf performance is an important issue for the crc calculation use a table based register.
"},{"location":"api/table_based_register/","title":"TableBasedRegister","text":" Bases: BasicRegister
Lookup table based crc register.
InfoThis register type will be much faster than a simple bit by bit based crc register like Register
.
__init__(configuration)
","text":"Creates a new table based crc register.
Parameters:
Name Type Description Defaultconfiguration
Configuration
used for the crc algorithm.
required AttentionCreating a table based register initially might take some extra time, due to the fact that some lookup tables need to be calculated/initialized.
"},{"location":"changelog/changes_2.0.0/","title":"2.0.0 - 2022-11-27","text":""},{"location":"changelog/changes_2.0.0/#breaking-api-changes","title":"\ud83d\udea8 Breaking API Changes","text":"** Renamed **
Classes:
AbstractCrcRegister
-> AbstractRegister
CrcRegisterBase
-> BasicRegister
CrcRegister
-> Register
TableBasedCrcRegister
-> TableBasedRegister
CrcCalculator
-> Calculator
Functions & Methods:
CrcCalculator.calculate_checksum
-> Calculator.checksum
CrcCalculator.verify_checksum
-> Calculator.verify
argument_parser()
-> [private] _argument_parser()
Arguments:
CrcCalculator(configuration, table_based=False)
-> Calculator(configuration, optimized=False)
** Removed **
CRC_TYPES
mappingchecksum
function/cli-entry-pointCRC_TYPES
mapping** Renamed **
Functions & Methods:
Renamed keyword argument expected_checksum
of method Calculator.verify
to expected
def verify(self, data: bytes, expected_checksum: int ) -> bool:\n ...\n
def verify(self, data: Union[int, ByteString, BinaryIO, Iterable[ByteString]], expected: int ) -> bool:\n ...\n
bytes
to Calculator.checksum
and Calculator.verify
(int
, ByteString
, BinaryIO
, Iterable[ByteString]
)
\ud83d\ude80 This release is powered by Gert van Dijk, thank you for your contribution!
"},{"location":"changelog/changes_5.0.0/#breaking-changes","title":"\ud83d\udea8 Breaking Changes","text":"Move from a single-file/module to an \u2018src-based layout\u2019 package.
The layout of the source repository has changed from single file crc.py
in the root into a package where crc.py
is now a private module _crc.py
of package crc
.
src/\n\u2514\u2500\u2500 crc/\n \u251c\u2500\u2500 _crc.py\n \u2514\u2500\u2500 __init__.py\n
__init__.py
re-exports all the public entities from the private module, so that typical imports like from crc import Calculator
remain working as-is.
The need and choice for this new layout is discussed in Issue #110.
The shebang line on the main src/crc/_crc.py
file (formerly crc.py
) has been removed.
This shouldn't affect anyone installing crc
as package. Both the entrypoint crc
and the package-level __main__
remain in place.
However, in case you were to obtain the src/crc/_crc.py
file (formerly crc.py
) and mark it as executable afterwards to run it directly, then this change may affect you. Running the main()
function can then still be achieved by either:
python path/to/crc.py
python -m crc
(if the module is on your PYTHON_PATH
)py.typed
marker to indicate the crc
package ships with type information. The type information was added inline since 2.0.0, but the marker file was missing in the package, so type checkers did not consider the type information. This eliminates the use of ignore_missing_imports
(mypy) or reportMissingTypeStubs
(Pylance/Pyright) in downstream projects.start
argument in range()
.Adjusted the SAE-J1850 configuration to match the specification
\ud83d\udea8\ufe0f For users which do rely on the previously misconfigured SAEJ1850
settings a configuration named SAEJ1850_ZERO
was added.
python 3.12
to test matrix\ud83d\ude80 This release is powered by Riccardo Malavolti, thank you for your contribution!
"},{"location":"changelog/changes_6.1.0/#features","title":"\u2728 Features","text":"\ud83d\ude80 This release is powered by Ramya-Ab, thank you for your contribution!
"},{"location":"changelog/changes_6.1.1/#documentation","title":"\ud83d\udcda Documentation","text":"Fixed unstable return values of digest
function. For more details, see issue #151.
Bug
This issue specifically affected scenarios where the CRC register was manually manipulated. Standard usages of the Calculator
class were not impacted. Furthermore, this issue primarily occurred in configurations that required reverse output.
Crc16.CCITT
configuration to Crc16.XMODEM
.Crc16.KERMIT
, which matches the official configuration for Crc16.CCITT
.It was intentionally decided not to reintroduce Crc16.CCITT
with the updated configuration. While it could have been added as an alias for Crc16.KERMIT
or a replacement, omitting Crc16.CCITT
ensures that client code will break upon update, thereby forcing maintainers to take notice and react accordingly.
Below are solutions to the two common scenarios that need to be addressed due to this change:
If you previously used Crc16.CCITT
and expected the configuration defined here:
Solution: Replace all usages of Crc16.CCITT
in your code with Crc16.KERMIT
.
If you depended on or wanted to use the configuration values that Crc16.CCITT
provided so far:
Solution: Replace all usages of Crc16.CCITT
in your code with Crc16.XMODEM
.
python-environment
actionpyproject.toml
Add support for the following Crc8 configurations:
Add support for the following Crc16 configurations:
unreleased.md
to changes_{X.Y.Z}.md
changes_{X.Y.Z}.md
to reflect the release version# X.Y.Z - YYYY-MM-DD\n
invoke release.prepare X.Y.Z\n
release.workflow
task and follow potential instructions. invoke release.workflow X.Y.Z\n
Follow the poetry installation instructions.
"},{"location":"development/setup/#2-install-gh","title":"2. Install gh","text":"Follow the gh installation instructions.
"},{"location":"development/setup/#3-checkout-the-project","title":"3. Checkout the project","text":"GitHub CLISSHHTTPSgh repo clone Nicoretti/crc\n
git clone git@github.com:Nicoretti/crc.git\n
git clone https://github.com/Nicoretti/crc.git\n
"},{"location":"development/setup/#4-switch-into-the-directory","title":"4. Switch into the directory","text":"cd crc\n
"},{"location":"development/setup/#5-the-poetry-environment","title":"5. The Poetry environment","text":"Make sure the poetry environment is setup properly and all dependencies are installed.
Activate the Poetry shell
poetry shell\n
Install the project dependencies
poetry install\n
init
task","text":"In order to bootstrap the remaining parts of the workspace setup, just execute the following command:
invoke init\n
Note
Follow potential instructions.
"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 27f3233..6b69326 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,152 +2,152 @@