Why yet another CRC tool?
Because I couldn't find any program which met my needs. I wanted an Open Source tool with the following features:
- an easy to read, fully parametrisable reference implementation in a scripting language
- a clean C source code generator whose output could be easily included in embedded programs
Over the years zillions of different CRC code snippets have accumulated in the Web, many of them are not compatible one with another, even if they are called the same (let's say CRC 16). pycrc tries to bring some clarity into this mess, by religiously using the Rocksoft Model, described by Ross Williams in Painless Guide to CRC Error Detection Algorithms.
How can I find out the parameters of a CRC?
It happens often that one has a few bytes of sample data and the CRC but a few parameters are not given. How can the missing CRC parameters be found?
Gregory Ewing wrote up an article on his experience of Reverse-Engineering a CRC Algorithm. It outlines an algorithm how to get the CRC parameters from raw data. Greg Cook's Perl script CRC RevEng does a good job in finding the parameters when a few data points are given.
What version of Python is supported?
pycrc requires Python 2.6 or later. Python 3.x is supported. The last version compatible with Python 2.4 is pycrc v0.7.10.
I get an error: "No such file or directory" when trying to run
pycrc.py on Linux
This happens if the file
pycrc.py has MS-DOS line endings.
This is the case if you download the ZIP archive.
You can either download the tar.gz version from the download page
or convert the file to UNIX file ending:
How do I calculate a CRC from python?
It is very straightforward to use the CRC implementation in python.
from crc_algorithms import Crc crc = Crc(width = 16, poly = 0x8005, reflect_in = True, xor_in = 0xffff, reflect_out = True, xor_out = 0x0000) data = "01020304A12B".decode("hex") # your data is probably already in binary form, so you won't have to convert it again. my_crc = crc.bit_by_bit_fast(data) # calculate the CRC, using the bit-by-bit-fast algorithm. print("0x%04x" % my_crc)
But the Python implementation is extremely inefficient!
This is correct. And I'm not going to change this. The Python implementation is thought to provide a correct, stable and easy to read code bases for further experiments. The right place to do performance tuning is the generated C code. I'm happy to consider every suggestion to improve the generated code.
CRC source generator
What is the best model?
This depends. For occasional computations on small amounts of data the optimised bit-by-bit implementation might be a feasible solution and might also have a speed which is comparable to other models. On desktop computers and with larger amounts of data, the table driven model is a good choice, whereas on embedded platforms where code space is a major concern, the bit-by-bit-fast implementation might be the better choice.
|Parameters||Width||Platform||Data quantity||Possible algorithms|
|Variable, Fixed||1-16 bits||Embedded, Desktop||Low||bit-by-bit, bit-by-bit-fast|
|Fixed||8, 16 bits||Embedded, Desktop||Medium, Low||bit-by-bit, bit-by-bit-fast, table-driven (table index width: 4)|
|Fixed||8 or more bits||Desktop, Server||Medium, High||table-driven (possibly with slice-by 4, 8 or 16)|
How fast are the algorithms?
The following numbers are collected using an unspecified model on a unspecified computer using an unspecified compiler (at maximum optimisation level). The numbers are only meaningful to get a rough idea how the algorithms compare against each other in terms of performance.
|bit-by-bit||1024 bytes||50 MiB/s|
|bit-by-bit-fast||1024 bytes||40 MiB/s|
|table-driven (table index width: 4)||1024 bytes||150 MiB/s|
|table-driven||1024 bytes||300 MiB/s|
|table-driven (slice-by: 4)||1024 bytes||700 MiB/s|
Can pycrc generate C++ code?
Yes and no. pycrc generates code that can be compiled by a C++ compiler, but does not generate classes or use any C++ feature.
Who the copyright of the generated code?
The sloppy answer is: you are free to do whatever you like with the generated code, as long as you don't blame the author for any malfunction or damage caused by the program.
However, please keep the line that stating that the code was generated by pycrc.