table of contents
PYCRC(1) | pycrc | PYCRC(1) |
NAME¶
pycrc - a free, easy to use Cyclic Redundancy Check (CRC) calculator and C source code generator.
SYNOPSIS¶
python pycrc.py [OPTIONS]
DESCRIPTION¶
pycrc[1] is a CRC reference implementation in Python and a C source code generator for parametrised CRC models. The generated C source code can be optimised for simplicity, speed or small memory footprint, as required on small embedded systems. The following operations are implemented:
pycrc supports the following variants of the CRC algorithm:
The --slice-by option enables a variant of the table-driven algorithm that operates on 32 bits of data or more at a time rather than 8 bits. This can dramatically speed-up the calculation of the CRC, at the cost of increased code and data size. Note: this option is experimental and not well-tested. Check your results and please raise bugs if you find problems.
OPTIONS¶
--version
-h, --help
--verbose
--check-string=STRING
--check-hexstring=STRING
--check-file=FILE
--generate=CODE
--std=STD
--algorithm=ALGO
--model=MODEL
--width=NUM
--poly=HEX
--reflect-in=BOOL
--xor-in=HEX
--reflect-out=BOOL
--xor-out=HEX
--slice-by=NUM
--table-idx-width=NUM
--force-poly
--symbol-prefix=STRING
--crc-type=STRING
--include-file=FILE
-oFILE, --output=FILE
THE CRC PARAMETRIC MODEL¶
The parametric model follows Ross N. Williams' convention described in A Painless Guide to CRC Error Detection Algorithms[2], often called the Rocksoft Model. Since most people are familiar with this kind of parameters, pycrc follows this convention, described as follows:
Width
Polynomial
The Polynomial may be specified in its standard form, i.e. with bit Width+1 set to 1, but the most significant bit may also be omitted. For example, both numbers 0x18005 and 0x8005 are accepted for a 16-bit Polynomial.
Most polynomials used in real world applications are odd (the least significant bit is 1), but there are some good even ones. pycrc allows the use of even polynomials with the --force-poly option. Some even polynomials may yield incorrect checksums depending on the used algorithm. Use at your own risk and if at all possible use a well-known MODEL above.
ReflectIn
A word is reflected or reversed by “flipping” its bits around the mid-point of the word. The most significant bit of the word is moved to the least significant position, the second-most significant bit is moved to the second-least significant position and so on. The reflected value of 0xa2 (10100010b) is 0x45 (01000101b), for example.
Some CRC algorithms can be implemented more efficiently in a bit reversed version, that's why many of the standard CRC models use reflected input octets.
XorIn
ReflectOut
XorOut
Check
CODE GENERATION¶
In the default configuration, the generated code is strict ISO C99. A minimal set of three functions are defined for each algorithm: crc_init(), crc_update() and crc_finalize(). Depending on the number of parameters given to pycrc, a different interface will be defined. A fully parametrised model has a simpler API, while the generated code for a runtime-specified implementation requires a pointer to a configuration structure as first parameter to all functions.
The generated source code uses the type crc_t, which is used throughout the code to hold intermediate results and also the final CRC value. It is defined in the generated header file and its type may be overridden with the --crc-type option.
Fully parametrised models¶
The prototypes of the CRC functions are normally generated by pycrc using the --generate h option. The CRC functions for a fully parametrised model will look like:
#include <stdlib.h> typedef uint16_t crc_t; /* pycrc will use an appropriate size here */
crc_t crc_init(void);
crc_t crc_update(crc_t crc, const unsigned char *data, size_t data_len);
crc_t crc_finalize(crc_t crc);
The code snippet below shows how to use the generated functions.
#include "pycrc_generated_crc.h" #include <stdio.h> int main(void) {
static const unsigned char str1[] = "1234";
static const unsigned char str2[] = "56789";
crc_t crc;
crc = crc_init();
crc = crc_update(crc, str1, sizeof(str1) - 1);
crc = crc_update(crc, str2, sizeof(str2) - 1);
/* more calls to crc_update... */
crc = crc_finalize(crc);
printf("0x%lx\n", (long)crc);
return 0; }
Models with runtime-configurable parameters¶
When the model is not fully defined then the missing parameters are stored in a structure of type crc_cfg_t. If a CRC function requires a value from the crc_cfg_t structure, then the first function argument is always a pointer to that structure. All fields of the configuration structure must be properly initialised before the first call to any CRC function.
If the Width was not specified when the code was generated, then the crc_cfg_t structure will contain three more fields: msb_mask, crc_mask and crc_shift. They are defined for performance reasons and must be initialised to the value given next to the field definition.
For example, a completely undefined CRC implementation will generate a crc_cfg_t structure as below:
typedef struct {
unsigned int width;
crc_t poly;
bool reflect_in;
crc_t xor_in;
bool reflect_out;
crc_t xor_out;
// internal parameters
crc_t msb_mask; // initialise as (crc_t)1u << (cfg->width - 1)
crc_t crc_mask; // initialise as (cfg->msb_mask - 1) | cfg->msb_mask
unsigned int crc_shift; // initialise as cfg->width < 8 ? 8 - cfg->width : 0 } crc_cfg_t;
msb_mask is a bitmask with the most significant bit of a Width bits wide data type set to 1. crc_mask is a bitmask with all bits of a Width bits wide data type set to 1. crc_shift is a shift counter that is used when Width is less than 8. It is the number of bits to shift the CRC register to align its top bit to a byte boundary.
The file test/main.c in the source package of pycrc contains a fully featured example of how to use the generated source code. A shorter, more compact main() function can be generated with the --generate c-main option. This second variant is the better option as it will always output valid code when some of the CRC parameters are known and some are unknown during code generation.
EXAMPLES¶
Calculate the CRC-32 checksum of the string “123456789”:
Generate the source code of the table-driven algorithm for an embedded application.
python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate h -o crc.h
python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c -o crc.c
A variant of the c target is c-main: this target will generate a simple main() function in addition to the CRC functions:
python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c-main -o crc.c
Generate the CRC table only:
SEE ALSO¶
The homepage of pycrc is https://pycrc.org.
A list of common CRC models is at https://pycrc.org/models.html. For a long list of known CRC models, see Greg Cook's Catalogue of Parameterised CRC Algorithms[3].
COPYRIGHT¶
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International[4].
AUTHOR¶
Thomas Pircher <tehpeh-web@tty1.net>
NOTES¶
- 1.
- pycrc
- 2.
- A Painless Guide to CRC Error Detection Algorithms
- 3.
- Catalogue of Parameterised CRC Algorithms
- 4.
- Creative Commons Attribution-ShareAlike 4.0 International
2017-08-11 | pycrc 0.9.1 |