CANLibrary/pycrc/doc/pycrc.html

248 lines
23 KiB
HTML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pycrc</title><meta name="generator" content="DocBook XSL Stylesheets V1.76.1" /></head><body><div class="refentry" title="pycrc"><a id="pycrc"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>pycrc — a free, easy to use Cyclic Redundancy Check (CRC) calculator and source code generator.</p></div><div class="refsynopsisdiv" title="Synopsis"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">pycrc</code> [OPTIONS]</p></div></div><div class="refsect1" title="Description"><a id="idp66576"></a><h2>Description</h2><p>
<a class="ulink" href="http://www.tty1.net/pycrc/" target="_top">pycrc</a>
provides a parametrised CRC reference implementation written in Python and a source code generator for C.
The generated C source code can be optimised for simplicity, speed or tight memory constraints for embedded platforms.
The following operations are implemented:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
generate the checksum of a string (ASCII or hex)
</p></li><li class="listitem"><p>
generate the checksum of a file
</p></li><li class="listitem"><p>
generate the C header and source files for a client implementation.
</p></li></ul></div><p>
</p><p>
The following variants of the CRC algorithm are supported:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
<em class="replaceable"><code>bit-by-bit</code></em>: the basic algorithm which operates individually on every bit of the augmented message
(i.e. the input data with <em class="replaceable"><code>Width</code></em> 0-bits attached to the end).
This algorithm is the easiest to understand because it is a straightforward implementation of the basic polynomial division,
but it is also the slowest among all possible variants.
</p></li><li class="listitem"><p>
<em class="replaceable"><code>bit-by-bit-fast</code></em>: a variation of the simple <em class="replaceable"><code>bit-by-bit</code></em> algorithm,
with the difference that it does not need to augment the data, i.e. it does not add <em class="replaceable"><code>Width</code></em> zero
bits at the end of the message.
This algorithm might be a good choice for embedded platforms, where code space is a major concern.
</p></li><li class="listitem"><p>
<em class="replaceable"><code>table-driven</code></em>: the standard table driven algorithm.
This is the fastest variant because it operates on one byte at a time, 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 <code class="option">--table-idx-width</code> 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.
</p></li></ul></div><p>
</p></div><div class="refsect1" title="Options"><a id="idp80400"></a><h2>Options</h2><div class="variablelist"><dl><dt><span class="term">
<code class="option">--version</code>
</span></dt><dd><p>show program's version number and exit</p></dd><dt><span class="term">
<code class="option">-h</code>
, </span><span class="term">
<code class="option">--help</code>
</span></dt><dd><p>show this help message and exit</p></dd><dt><span class="term">
<code class="option">--verbose</code>
</span></dt><dd><p>be more verbose; in particular, print the value of the parameters and the chosen model</p></dd><dt><span class="term">
<code class="option">--check-string=</code><em class="replaceable"><code>STRING</code></em>
</span></dt><dd><p>calculate the checksum of the given string ('<em class="replaceable"><code>123456789</code></em>' default)</p></dd><dt><span class="term">
<code class="option">--check-hexstring=</code><em class="replaceable"><code>STRING</code></em>
</span></dt><dd><p>calculate the checksum of the given hexadecimal string</p></dd><dt><span class="term">
<code class="option">--check-file=</code><em class="replaceable"><code>FILE</code></em>
</span></dt><dd><p>calculate the checksum of the given file</p></dd><dt><span class="term">
<code class="option">--generate=</code><em class="replaceable"><code>CODE</code></em>
</span></dt><dd><p>generate the source code type as a choice from {c, h, c-main, table}</p></dd><dt><span class="term">
<code class="option">--std=</code><em class="replaceable"><code>STD</code></em>
</span></dt><dd><p>C standard style of the generated code from {C89, ANSI, C99}</p></dd><dt><span class="term">
<code class="option">--algorithm=</code><em class="replaceable"><code>ALGO</code></em>
</span></dt><dd><p>choose an algorithm from {<em class="replaceable"><code>bit-by-bit</code></em>, <em class="replaceable"><code>bit-by-bit-fast</code></em>,
<em class="replaceable"><code>table-driven</code></em>, <em class="replaceable"><code>all</code></em>}</p></dd><dt><span class="term">
<code class="option">--model=</code><em class="replaceable"><code>MODEL</code></em>
</span></dt><dd><p>choose a parameter set from
{<em class="replaceable"><code>crc-5</code></em>,
<em class="replaceable"><code>crc-8</code></em>,
<em class="replaceable"><code>dallas-1-wire</code></em>,
<em class="replaceable"><code>crc-12-3gpp</code></em>,
<em class="replaceable"><code>crc-15</code></em>,
<em class="replaceable"><code>crc-16</code></em>,
<em class="replaceable"><code>crc-16-usb</code></em>,
<em class="replaceable"><code>crc-16-modbus</code></em>,
<em class="replaceable"><code>crc-16-genibus</code></em>,
<em class="replaceable"><code>ccitt</code></em>,
<em class="replaceable"><code>r-crc-16</code></em>,
<em class="replaceable"><code>kermit</code></em>,
<em class="replaceable"><code>x-25</code></em>,
<em class="replaceable"><code>xmodem</code></em>,
<em class="replaceable"><code>zmodem</code></em>,
<em class="replaceable"><code>crc-24</code></em>,
<em class="replaceable"><code>crc-32</code></em>,
<em class="replaceable"><code>crc-32c</code></em>,
<em class="replaceable"><code>crc-32-mpeg</code></em>,
<em class="replaceable"><code>crc-32-bzip2</code></em>,
<em class="replaceable"><code>posix</code></em>,
<em class="replaceable"><code>jam</code></em>,
<em class="replaceable"><code>xfer</code></em>,
<em class="replaceable"><code>crc-64</code></em>,
<em class="replaceable"><code>crc-64-jones</code></em>,
<em class="replaceable"><code>crc-64-xz</code></em>}</p></dd><dt><span class="term">
<code class="option">--width=</code><em class="replaceable"><code>NUM</code></em>
</span></dt><dd><p>use <em class="replaceable"><code>NUM</code></em> bits in the <em class="replaceable"><code>Polynomial</code></em></p></dd><dt><span class="term">
<code class="option">--poly=</code><em class="replaceable"><code>HEX</code></em>
</span></dt><dd><p>use <em class="replaceable"><code>HEX</code></em> as <em class="replaceable"><code>Polynomial</code></em></p></dd><dt><span class="term">
<code class="option">--reflect-in=</code><em class="replaceable"><code>BOOL</code></em>
</span></dt><dd><p>reflect input bytes</p></dd><dt><span class="term">
<code class="option">--xor-in=</code><em class="replaceable"><code>HEX</code></em>
</span></dt><dd><p>use <em class="replaceable"><code>HEX</code></em> as initial value</p></dd><dt><span class="term">
<code class="option">--reflect-out=</code><em class="replaceable"><code>BOOL</code></em>
</span></dt><dd><p>reflect output bytes</p></dd><dt><span class="term">
<code class="option">--xor-out=</code><em class="replaceable"><code>HEX</code></em>
</span></dt><dd><p>xor the final CRC value with <em class="replaceable"><code>HEX</code></em></p></dd><dt><span class="term">
<code class="option">--table-idx-width=</code><em class="replaceable"><code>NUM</code></em>
</span></dt><dd><p>use <em class="replaceable"><code>NUM</code></em> bits to index the CRC table; <em class="replaceable"><code>NUM</code></em> must be one of the values
{<em class="replaceable"><code>1</code></em>, <em class="replaceable"><code>2</code></em>, <em class="replaceable"><code>4</code></em>, <em class="replaceable"><code>8</code></em>}</p></dd><dt><span class="term">
<code class="option">--symbol-prefix=</code><em class="replaceable"><code>STRING</code></em>
</span></dt><dd><p>when generating source code, use <em class="replaceable"><code>STRING</code></em> as prefix to the generated symbols</p></dd><dt><span class="term">
<code class="option">--crc-type=</code><em class="replaceable"><code>STRING</code></em>
</span></dt><dd><p>when generating source code, use <em class="replaceable"><code>STRING</code></em> as crc_t type</p></dd><dt><span class="term">
<code class="option">--include-file=</code><em class="replaceable"><code>FILE</code></em>
</span></dt><dd><p>when generating source code, include also <em class="replaceable"><code>FILE</code></em> as header file</p></dd><dt><span class="term">
<code class="option">-o</code><em class="replaceable"><code>FILE</code></em>
, </span><span class="term">
<code class="option">--output=</code><em class="replaceable"><code>FILE</code></em>
</span></dt><dd><p>write the generated code to <em class="replaceable"><code>FILE</code></em> instead to stdout</p></dd></dl></div></div><div class="refsect1" title="The CRC Parametric Model"><a id="idp170688"></a><h2>The CRC Parametric Model</h2><p>
The parametric model follows Ross N. Williams' convention described in
<a class="ulink" href="http://www.ross.net/crc/crcpaper.html" target="_top">A Painless Guide to CRC Error Detection Algorithms</a>,
commonly called the Rocksoft Model.
Since most people are familiar with this kind of parameters, pycrc follows this convention, described as follows:
</p><div class="glosslist"><dl><dt><em class="replaceable"><code>Width</code></em></dt><dd><p>
The width of the CRC <em class="replaceable"><code>Polynomial</code></em>, in number of bits. This is also the width of the final CRC result.
Previous versions of pycrc only multiples of 8 could be be used as <em class="replaceable"><code>Width</code></em> for the
<em class="replaceable"><code>table-driven</code></em> algorithm. As of version 0.7.5, any <em class="replaceable"><code>Width</code></em> is accepted
on all algorithms.
</p></dd><dt><em class="replaceable"><code>Polynomial</code></em></dt><dd><p>
The unreflected polynomial of the CRC algorithm.
</p><p>
The <em class="replaceable"><code>Polynomial</code></em> may be specified in its standard form, i.e. with bit <em class="replaceable"><code>Width</code></em>+1
set to 1, but the most significant bit may also be omitted. For example, for a <em class="replaceable"><code>Width</code></em> of 16,
both forms 0x18005 and 0x8005 are accepted.
</p></dd><dt><em class="replaceable"><code>ReflectIn</code></em></dt><dd><p>
Reflect the bytes of the message before processing them. A word is reflected by inverting the position of its bits with
respect to the middle axis of the word.
The reversed value of 0xa3 (10100010b) is 0x45 (01000101b), for example.
Some CRC algorithms can be implemented more efficiently in a bit reversed version.
</p><p>
Reflected algorithms are more efficient than straight-forward implementations, thus many of the standard algorithmic
variants use reflected input bytes.
</p></dd><dt><em class="replaceable"><code>XorIn</code></em></dt><dd><p>
The initial value (usually all 0 or all 1) in the algorithms which operate on the non-augmented message.
This value can be seen as a value which will be XOR-ed into the CRC register after <em class="replaceable"><code>Width</code></em>
iterations of the <em class="replaceable"><code>bit-by-bit</code></em> algorithm.
This means the simple <em class="replaceable"><code>bit-by-bit</code></em> algorithm must calculate the initial value using some sort of
reverse CRC algorithm on the <em class="replaceable"><code>XorIn</code></em> value.
</p></dd><dt><em class="replaceable"><code>ReflectOut</code></em></dt><dd><p>
Reflect the final CRC result. This operation takes place before XOR-ing the final CRC
value with the <em class="replaceable"><code>XorOut</code></em> parameter.
</p></dd><dt><em class="replaceable"><code>XorOut</code></em></dt><dd><p>
A value (usually all bits 0 or all 1) which will be XOR-ed to the final CRC value.
</p></dd><dt><em class="replaceable"><code>Check</code></em></dt><dd><p>
This value is not exactly a parameter of a model but it is sometimes given together with the Rocksoft Model parameters.
It is the CRC value of the parametrised model over the string "<em class="replaceable"><code>123456789</code></em>" and
may be used to validate an implementation.
</p></dd></dl></div><p>
</p></div><div class="refsect1" title="Code generation"><a id="idp203248"></a><h2>Code generation</h2><p>
In the default configuration, the generated code is strict ISO C99 code.
A minimal set of three functions are defined for each algorithm:
<code class="function">crc_init()</code>, <code class="function">crc_update()</code> and <code class="function">crc_finalize()</code>.
According to the number of parameters given to pycrc, a different interface definition is generated.
Fully parametrised models have a simpler API, while the generated code for runtime-specified implementations adds a
pointer to a configuration structure as first parameter to all functions.
</p><p>
The generated source code uses the type <span class="type">crc_t</span>, which is used throughout the code. It may be redefined in the generated header file.
</p><div class="refsect2" title="Fully parametrised models"><a id="idp206896"></a><h3>Fully parametrised models</h3><p>
The prototypes of these functions are normally generated by pycrc using the <em class="replaceable"><code>--generate h</code></em> option.
The prototypes of the
</p><div class="funcsynopsis"><pre class="funcsynopsisinfo">
#include &lt;stdlib.h&gt;
/* pycrc will define the appropriate type
* when generating the header file. */
typedef XXXX crc_t;
</pre><table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table"><tr><td><code class="funcdef">crc_t <strong>fsfunccrc_init</strong>(</code></td><td><code>void)</code>;</td><td> </td></tr></table><div class="funcprototype-spacer"> </div><table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table"><tr><td><code class="funcdef">crc_t <strong>fsfunccrc_update</strong>(</code></td><td>crc_t <var class="pdparam">crc</var>, </td></tr><tr><td> </td><td>const unsigned char *<var class="pdparam">data</var>, </td></tr><tr><td> </td><td>size_t <var class="pdparam">data_len</var><code>)</code>;</td></tr></table><div class="funcprototype-spacer"> </div><table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table"><tr><td><code class="funcdef">crc_t <strong>fsfunccrc_finalize</strong>(</code></td><td>crc_t <var class="pdparam">crc</var><code>)</code>;</td></tr></table><div class="funcprototype-spacer"> </div></div><p>
The following code snippet shows how to use the generated functions.
</p><pre class="programlisting">
#include "pycrc_generated_crc.h"
#include &lt;stdio.h&gt;
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;
}
</pre><p>
</p></div><div class="refsect2" title="Models with runtime-configurable parameters"><a id="idp218176"></a><h3>Models with runtime-configurable parameters</h3><p>
When the model is not fully defined then the missing parameters are contained in a structure of
type <span class="type">crc_cfg_t</span>.
The first argument of the CRC functions is a pointer to that structure, and its fields must be initialised
properly by the user before the first call to the CRC functions.
This structure contains three additional parameters, <em class="parameter"><code>msb_mask</code></em>, <em class="parameter"><code>crc_mask</code></em>
and <em class="parameter"><code>crc_shift</code></em>, if the <em class="replaceable"><code>Width</code></em> was undefined when the code was generated.
</p><pre class="programlisting">
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 1UL &lt;&lt; (cfg-&gt;width - 1)
crc_t crc_mask; // initialise as (cfg-&gt;msb_mask - 1) | cfg-&gt;msb_mask
unsigned int crc_shift; // initialise as cfg-&gt;width &lt; 8 ? 8 - cfg-&gt;width : 0
} crc_cfg_t;
</pre><p>
</p><p>
<em class="parameter"><code>msb_mask</code></em> is a bitmask with the most significant bit of a <em class="replaceable"><code>Width</code></em> bits
wide data type set to 1.
<em class="parameter"><code>crc_mask</code></em> is a bitmask with all bits of a <em class="replaceable"><code>Width</code></em> bits
wide data type set to 1.
<em class="parameter"><code>crc_shift</code></em> is a shift counter that is used when <em class="replaceable"><code>Width</code></em> is less than 8.
It is the number of bits to shift the CRC register to align its top bit at a byte boundary.
</p><p>
The file <code class="filename">test/main.c</code> in the source package of pycrc contains a fully featured example
of how to use the generated source code.
A shorter, more compact <code class="code">main()</code> function can be generated with the <em class="replaceable"><code>--generate c-main</code></em>
option.
This second variant is the better option when some of the CRC parameters are known and some are unknown during code generation.
</p></div></div><div class="refsect1" title="Examples"><a id="idp228256"></a><h2>Examples</h2><p>
</p><div class="glosslist"><dl><dt>Calculate the CRC-32 checksum of the string 123456789:</dt><dd><p>
<strong class="userinput"><code>python pycrc.py --model crc-32 --check-string 123456789</code></strong>
</p></dd><dt>Generate the source code of the table-driven algorithm for an embedded application.</dt><dd><p>
<strong class="userinput"><code>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate h -o crc.h</code></strong>
</p><p>
<strong class="userinput"><code>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c -o crc.c</code></strong>
</p><p>
The table index width of 4 bits ensures a moderate memory consumption.
In fact, the size of the resulting table is <code class="code">16 * sizeof(crc_t)</code> bytes.
A variant of the last generated output is the <em class="replaceable"><code>c-main</code></em> target:
a simple <em class="replaceable"><code>main()</code></em> function is generated in addition to the CRC routines:
</p><p>
<strong class="userinput"><code>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c-main -o crc.c</code></strong>
</p></dd><dt>Generate the CRC table only:</dt><dd><p>
<strong class="userinput"><code>python pycrc.py --model kermit --generate table -o crc-table.txt</code></strong>
</p></dd></dl></div><p>
</p></div><div class="refsect1" title="See Also"><a id="idp239696"></a><h2>See Also</h2><p>
The homepage of pycrc is <a class="ulink" href="http://www.tty1.net/pycrc/" target="_top">http://www.tty1.net/pycrc/</a>.
</p><p>
For a long list of known CRC models, see Greg Cook's <a class="ulink" href="http://regregex.bbcmicro.net/crc-catalogue.htm" target="_top">Catalogue of Parameterised CRC Algorithms</a>.
</p></div><div class="refsect1" title="Copyright"><a id="idp242800"></a><h2>Copyright</h2><p>
This work is licensed under a
<a class="ulink" href="http://creativecommons.org/licenses/by-sa/3.0/" target="_top">Creative Commons Attribution-Share Alike 3.0 Unported License</a>.
</p></div></div></body></html>