pycrc — a parametrizable Cyclic Redundancy Check (CRC) calculation utility and C source code generator written in Python
pycrc
[OPTIONS]
pycrc is a simple general-purpose tool to work with Cyclic Redundancy Checks (CRC). Although CRCs are not strong hashes from a cryptographic point of view, they are widespread in the telecommunication, because they are easily implemented in hardware permit some basic error corrections. pycrc aims to be a parametrisable reference implementation in Python and code generator for C The following functions are implemented:
generate the checksum of a string
generate the C header and source files for a client implementation. The algorithm can be chosen from fast but big implementation to slower but smaller implementations suitable especially for embedded applications.
The following variants of the CRC algorithm are supported:
bit_by_bit
: the basic algorithm which operates individually on every bit of the augmented message
(i.e. the input data with Width
0-bits attached to the end).
This algorithm is the easiest one to understand, because it's a direct implementation of the basic polynomial division,
but it is also the slowest among all possible variants.
bit_by_bit_fast
: a variation of the simple bit_by_bit
algorithm,
which doesn't need the augmented message.
This algorithm might be a good choice for embedded platforms, where code space is a major concern.
table_driven
: the standard table driven algorithm. This algorithm works only on models with multiples
of 8 as Width
.
This is the fastest variant, because if operates on bytes as opposed to bits, and uses a look-up table of 256 elements, which might
not be feasible for small embedded systems, though. Anyway, the number of elements in the look-up table can be reduced by means of
the --table_idx_with
command line switch. By using 4 bits (16 elements in the look-up table) a significant speed-up
can be measured with respect to the bit-by-bit algorithms.
--version
show program's version number and exit
-h
,
--help
show this help message and exit
--verbose
be more verbose; in particular, print informations about the parameters and the model
--algorithm=
ALGO
choose an algorithm from {bit_by_bit
, bit_by_bit_fast
,
table_driven
, all
}
--model=
MODEL
choose a model from {crc-8
, crc-16
, citt
,
xmodem
, crc-32
}
--width=
WIDTH
use WIDTH bits in the Poly
--poly=
HEX
use HEX as Polynom
--reflect_in=
BOOL
reflect input bytes
--xor_in=
HEX
use HEX as initial value
--reflect_out=
BOOL
reflect output bytes
--xor_out=
HEX
xor the final crc value with HEX
----table_idx_with=
WIDTH
use WIDTH bits to index the crc table; WIDTH one of {1
, 2
,
4
, 8
}
--check_string=
STRING
calculate the checksum of the given string ('123456789
' default)
--generate_c
generate a C source file
--generate_c_main
generate a C source file with main fucntion
--generate_h
generate a C header source file
-o
file
,
--output=
file
write the generated code to file instead to stdout
The CRC parameters are taken from Ross N. Williams' "A Painless Guide to CRC Error Detection Algorithms". The parameters are described as follows.
Width
The with of the CRC Poly
, in number of bits. This is also the width of the final CRC result.
For the bit_by_bit
and the bit_by_bit_fast
algorithms, any with
can be used, but for the table_driven
algorithm, which operate on 8 bits at a time,
Width
must be multiples of 8.
Poly
The polynom of the CRC. Although the Poly
may be specified with the leading 1 at the bit-position
Width
+ 1, this leading bit is silently ignored.
ReflectIn
Whether the input bits are reflected. A byte is reflected by inverting the position of all bits with respect to the middle position of the byte. Reflected algorithms are more efficient than straight-forward implementations. The reversed value of 0xa3 (10100010) is 0x45 (01000101), for example.
ReflectOut
Whether the output bytes are reflected. This opeartion (if any) takes place before XOR-ing the resulting the final CRC
value with the XorOut
parameter.
XorIn
The initial value (usually all 0 or all 1) of the algorithms which operate on the non-augmented data.
This value can be seen as a value which will be XOR-ed into the CRC register after the Width
iterations of the bit_by_bit
algorithm.
This means, the simple bit_by_bit algorithm must calculate the initial value with a sort of reverse CRC algorithm for with bits.
XorOut
A value (usually all 0 or all 1) which will be XOR-ed to the final CRC value.
Check
This value is not really a parameter of a model, but it is sometimes given together with the Rocksoft Model parameters.
It is the CRC value of the parametrized model over the standard string 123456789
and
may be used to validate a implementation.
pycrc.py --model crc-32 --check_string 123456789
pycrc.py --model crc-16 --algorithm table_driven --table_idx_with 4 --generate_h -o crc.h
pycrc.py --model crc-16 --algorithm table_driven --table_idx_with 4 --generate_c -o crc.c
This work is licensed under the Creative Commons Attribution-Share Alike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.