Brian Silverman | 84b2223 | 2019-01-25 20:29:29 -0800 | [diff] [blame] | 1 | <?xml version='1.0' encoding="utf-8"?> |
| 2 | |
| 3 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V5.1//EN" |
| 4 | "http://www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd" [ |
| 5 | <!ENTITY project_version "0.9.1"> |
| 6 | <!ENTITY project_homepage "https://pycrc.org"> |
| 7 | <!ENTITY project_models "https://pycrc.org/models.html"> |
| 8 | <!ENTITY date "2017-08-11"> |
| 9 | <!ENTITY author_firstname "Thomas"> |
| 10 | <!ENTITY author_surname "Pircher"> |
| 11 | <!ENTITY author_email "tehpeh-web@tty1.net"> |
| 12 | <!ENTITY bit-by-bit "bit-by-bit"> |
| 13 | <!ENTITY bbb "bbb"> |
| 14 | <!ENTITY bit-by-bit-fast "bit-by-bit-fast"> |
| 15 | <!ENTITY bbf "bbf"> |
| 16 | <!ENTITY table-driven "table-driven"> |
| 17 | <!ENTITY tbl "tbl"> |
| 18 | <!ENTITY slice-by "slice-by"> |
| 19 | <!ENTITY width "Width"> |
| 20 | <!ENTITY poly "Polynomial"> |
| 21 | <!ENTITY reflect_in "ReflectIn"> |
| 22 | <!ENTITY xor_in "XorIn"> |
| 23 | <!ENTITY reflect_out "ReflectOut"> |
| 24 | <!ENTITY xor_out "XorOut"> |
| 25 | <!ENTITY check "Check"> |
| 26 | ]> |
| 27 | |
| 28 | |
| 29 | <refentry xmlns='http://docbook.org/ns/docbook' version="5.0" xml:id="pycrc"> |
| 30 | <info> |
| 31 | <title>pycrc</title> |
| 32 | <productname>pycrc</productname> |
| 33 | <productnumber>&project_version;</productnumber> |
| 34 | <author> |
| 35 | <personname> |
| 36 | <firstname>&author_firstname;</firstname> |
| 37 | <surname>&author_surname;</surname> |
| 38 | </personname> |
| 39 | <contrib>Author of pycrc and this manual page.</contrib> |
| 40 | <email>&author_email;</email> |
| 41 | </author> |
| 42 | <date>&date;</date> |
| 43 | </info> |
| 44 | |
| 45 | <refmeta> |
| 46 | <refentrytitle>pycrc</refentrytitle> |
| 47 | <manvolnum>1</manvolnum> |
| 48 | </refmeta> |
| 49 | |
| 50 | <refnamediv> |
| 51 | <refname>pycrc</refname> |
| 52 | <refpurpose>a free, easy to use Cyclic Redundancy Check (CRC) calculator and C source code generator.</refpurpose> |
| 53 | </refnamediv> |
| 54 | |
| 55 | <refsynopsisdiv> |
| 56 | <cmdsynopsis> |
| 57 | <command>python pycrc.py</command> |
| 58 | <arg>OPTIONS</arg> |
| 59 | </cmdsynopsis> |
| 60 | </refsynopsisdiv> |
| 61 | |
| 62 | <refsect1> |
| 63 | <title>Description</title> |
| 64 | <para> |
| 65 | <link xlink:href="&project_homepage;">pycrc</link> |
| 66 | is a CRC reference implementation in Python and a C source code generator for parametrised CRC models. |
| 67 | The generated C source code can be optimised for simplicity, |
| 68 | speed or small memory footprint, as required on small embedded systems. |
| 69 | |
| 70 | The following operations are implemented: |
| 71 | <itemizedlist> |
| 72 | <listitem> |
| 73 | <para>calculate the checksum of a string (ASCII or hex)</para> |
| 74 | </listitem> |
| 75 | <listitem> |
| 76 | <para>calculate the checksum of a file</para> |
| 77 | </listitem> |
| 78 | <listitem> |
| 79 | <para>generate the header and source files for a C implementation.</para> |
| 80 | </listitem> |
| 81 | </itemizedlist> |
| 82 | </para> |
| 83 | <para> |
| 84 | pycrc supports the following variants of the CRC algorithm: |
| 85 | <itemizedlist> |
| 86 | <listitem> |
| 87 | <para><replaceable>&bit-by-bit;</replaceable> or <replaceable>&bbb;</replaceable>: |
| 88 | the basic algorithm which operates individually on every bit of the augmented message |
| 89 | (i.e. the input data with <replaceable>&width;</replaceable> zero bits added at the end). |
| 90 | This algorithm is a straightforward implementation of the basic polynomial division and |
| 91 | is the easiest one to understand, but it is also the slowest one among all possible |
| 92 | variants. |
| 93 | </para> |
| 94 | </listitem> |
| 95 | <listitem> |
| 96 | <para><replaceable>&bit-by-bit-fast;</replaceable> or <replaceable>&bbf;</replaceable>: |
| 97 | a variation of the simple <replaceable>&bit-by-bit;</replaceable> algorithm. |
| 98 | This algorithm still iterates over every bit of the message, but does not augment |
| 99 | it (does not add <replaceable>&width;</replaceable> zero bits at the end). |
| 100 | It gives the same result as the <replaceable>&bit-by-bit;</replaceable> method by |
| 101 | carefully choosing the initial value of the algorithm. |
| 102 | This method might be a good choice for embedded platforms, where code space is more |
| 103 | important than execution speed. |
| 104 | </para> |
| 105 | </listitem> |
| 106 | <listitem> |
| 107 | <para><replaceable>&table-driven;</replaceable> or <replaceable>&tbl;</replaceable>: |
| 108 | the standard table driven algorithm. |
| 109 | This is the fastest variant because it operates on one byte at a time, as opposed to one |
| 110 | bit at the time. |
| 111 | This method uses a look-up table (usually of 256 elements), which might not be acceptable |
| 112 | for small embedded systems. The number of elements in the look-up table can be reduced |
| 113 | with the <option>--table-idx-width</option> command line switch. |
| 114 | The value of 4 bits for the table index (16 elements in the look-up table) can be a good |
| 115 | compromise between execution speed and code size. |
| 116 | </para> |
| 117 | <para> |
| 118 | The <option>--slice-by</option> option enables a variant of the <replaceable>&table-driven;</replaceable> |
| 119 | algorithm that operates on 32 bits of data or more at a time rather than 8 bits. |
| 120 | This can dramatically speed-up the calculation of the CRC, at the cost of |
| 121 | increased code and data size. |
| 122 | <emphasis>Note</emphasis>: this option is experimental and not well-tested. |
| 123 | Check your results and please raise bugs if you find problems. |
| 124 | </para> |
| 125 | </listitem> |
| 126 | </itemizedlist> |
| 127 | </para> |
| 128 | </refsect1> |
| 129 | |
| 130 | <refsect1> |
| 131 | <title>Options</title> |
| 132 | <variablelist> |
| 133 | <varlistentry> |
| 134 | <term> |
| 135 | <option>--version</option> |
| 136 | </term> |
| 137 | <listitem> |
| 138 | <para>show the program version number and exit.</para> |
| 139 | </listitem> |
| 140 | </varlistentry> |
| 141 | <varlistentry> |
| 142 | <term> |
| 143 | <option>-h</option> |
| 144 | </term> |
| 145 | <term> |
| 146 | <option>--help</option> |
| 147 | </term> |
| 148 | <listitem> |
| 149 | <para>show this help message and exit.</para> |
| 150 | </listitem> |
| 151 | </varlistentry> |
| 152 | <varlistentry> |
| 153 | <term> |
| 154 | <option>--verbose</option> |
| 155 | </term> |
| 156 | <listitem> |
| 157 | <para>be more verbose; in particular, print the value of the parameters and the chosen model to <filename>stdout</filename>.</para> |
| 158 | </listitem> |
| 159 | </varlistentry> |
| 160 | <varlistentry> |
| 161 | <term> |
| 162 | <option>--check-string=</option><replaceable>STRING</replaceable> |
| 163 | </term> |
| 164 | <listitem> |
| 165 | <para>calculate the checksum of a string (default: <quote><replaceable>123456789</replaceable></quote>). If the string contains non-ASCII characters then it will be UTF-8 decoded.</para> |
| 166 | </listitem> |
| 167 | </varlistentry> |
| 168 | <varlistentry> |
| 169 | <term> |
| 170 | <option>--check-hexstring=</option><replaceable>STRING</replaceable> |
| 171 | </term> |
| 172 | <listitem> |
| 173 | <para>calculate the checksum of a hexadecimal number string.</para> |
| 174 | </listitem> |
| 175 | </varlistentry> |
| 176 | <varlistentry> |
| 177 | <term> |
| 178 | <option>--check-file=</option><replaceable>FILE</replaceable> |
| 179 | </term> |
| 180 | <listitem> |
| 181 | <para>calculate the checksum of a file. If the file contains non-ASCII characters then it will be UTF-8 decoded.</para> |
| 182 | </listitem> |
| 183 | </varlistentry> |
| 184 | <varlistentry> |
| 185 | <term> |
| 186 | <option>--generate=</option><replaceable>CODE</replaceable> |
| 187 | </term> |
| 188 | <listitem> |
| 189 | <para>generate C source code; choose the type from {<replaceable>h</replaceable>, |
| 190 | <replaceable>c</replaceable>, <replaceable>c-main</replaceable>, <replaceable>table</replaceable>}.</para> |
| 191 | </listitem> |
| 192 | </varlistentry> |
| 193 | <varlistentry> |
| 194 | <term> |
| 195 | <option>--std=</option><replaceable>STD</replaceable> |
| 196 | </term> |
| 197 | <listitem> |
| 198 | <para>specify the C dialect of the generated code from {C89, ANSI, C99}.</para> |
| 199 | </listitem> |
| 200 | </varlistentry> |
| 201 | <varlistentry> |
| 202 | <term> |
| 203 | <option>--algorithm=</option><replaceable>ALGO</replaceable> |
| 204 | </term> |
| 205 | <listitem> |
| 206 | <para>choose an algorithm from {<replaceable>bit-by-bit</replaceable>, <replaceable>bbb</replaceable>, |
| 207 | <replaceable>bit-by-bit-fast</replaceable>, <replaceable>bbf</replaceable>, |
| 208 | <replaceable>table-driven</replaceable>, <replaceable>tbl</replaceable>, |
| 209 | <replaceable>all</replaceable>}.</para> |
| 210 | </listitem> |
| 211 | </varlistentry> |
| 212 | <varlistentry> |
| 213 | <term> |
| 214 | <option>--model=</option><replaceable>MODEL</replaceable> |
| 215 | </term> |
| 216 | <listitem> |
| 217 | <para>choose a parameter set from |
| 218 | {<replaceable>crc-5</replaceable>, |
| 219 | <replaceable>crc-8</replaceable>, |
| 220 | <replaceable>dallas-1-wire</replaceable>, |
| 221 | <replaceable>crc-12-3gpp</replaceable>, |
| 222 | <replaceable>crc-15</replaceable>, |
| 223 | <replaceable>crc-16</replaceable>, |
| 224 | <replaceable>crc-16-usb</replaceable>, |
| 225 | <replaceable>crc-16-modbus</replaceable>, |
| 226 | <replaceable>crc-16-genibus</replaceable>, |
| 227 | <replaceable>crc-16-ccitt</replaceable>, |
| 228 | <replaceable>r-crc-16</replaceable>, |
| 229 | <replaceable>kermit</replaceable>, |
| 230 | <replaceable>x-25</replaceable>, |
| 231 | <replaceable>xmodem</replaceable>, |
| 232 | <replaceable>zmodem</replaceable>, |
| 233 | <replaceable>crc-24</replaceable>, |
| 234 | <replaceable>crc-32</replaceable>, |
| 235 | <replaceable>crc-32c</replaceable>, |
| 236 | <replaceable>crc-32-mpeg</replaceable>, |
| 237 | <replaceable>crc-32-bzip2</replaceable>, |
| 238 | <replaceable>posix</replaceable>, |
| 239 | <replaceable>jam</replaceable>, |
| 240 | <replaceable>xfer</replaceable>, |
| 241 | <replaceable>crc-64</replaceable>, |
| 242 | <replaceable>crc-64-jones</replaceable>, |
| 243 | <replaceable>crc-64-xz</replaceable>}.</para> |
| 244 | </listitem> |
| 245 | </varlistentry> |
| 246 | <varlistentry> |
| 247 | <term> |
| 248 | <option>--width=</option><replaceable>NUM</replaceable> |
| 249 | </term> |
| 250 | <listitem> |
| 251 | <para>use <replaceable>NUM</replaceable> bits in the <replaceable>&poly;</replaceable>.</para> |
| 252 | </listitem> |
| 253 | </varlistentry> |
| 254 | <varlistentry> |
| 255 | <term> |
| 256 | <option>--poly=</option><replaceable>HEX</replaceable> |
| 257 | </term> |
| 258 | <listitem> |
| 259 | <para>use <replaceable>HEX</replaceable> as <replaceable>&poly;</replaceable>.</para> |
| 260 | </listitem> |
| 261 | </varlistentry> |
| 262 | <varlistentry> |
| 263 | <term> |
| 264 | <option>--reflect-in=</option><replaceable>BOOL</replaceable> |
| 265 | </term> |
| 266 | <listitem> |
| 267 | <para>reflect the octets in the input message.</para> |
| 268 | </listitem> |
| 269 | </varlistentry> |
| 270 | <varlistentry> |
| 271 | <term> |
| 272 | <option>--xor-in=</option><replaceable>HEX</replaceable> |
| 273 | </term> |
| 274 | <listitem> |
| 275 | <para>use <replaceable>HEX</replaceable> as initial value.</para> |
| 276 | </listitem> |
| 277 | </varlistentry> |
| 278 | <varlistentry> |
| 279 | <term> |
| 280 | <option>--reflect-out=</option><replaceable>BOOL</replaceable> |
| 281 | </term> |
| 282 | <listitem> |
| 283 | <para>reflect the resulting checksum before applying the &xor_out; value.</para> |
| 284 | </listitem> |
| 285 | </varlistentry> |
| 286 | <varlistentry> |
| 287 | <term> |
| 288 | <option>--xor-out=</option><replaceable>HEX</replaceable> |
| 289 | </term> |
| 290 | <listitem> |
| 291 | <para>xor the final CRC value with <replaceable>HEX</replaceable>.</para> |
| 292 | </listitem> |
| 293 | </varlistentry> |
| 294 | <varlistentry> |
| 295 | <term> |
| 296 | <option>--slice-by=</option><replaceable>NUM</replaceable> |
| 297 | </term> |
| 298 | <listitem> |
| 299 | <para>speed-up the &table-driven; calculation by operating on |
| 300 | <replaceable>NUM</replaceable> octets of data rather than a |
| 301 | single octet at a time. |
| 302 | <replaceable>NUM</replaceable> must be one of the values |
| 303 | {<replaceable>4</replaceable>, <replaceable>8</replaceable>, |
| 304 | <replaceable>16</replaceable>}.</para> |
| 305 | </listitem> |
| 306 | </varlistentry> |
| 307 | <varlistentry> |
| 308 | <term> |
| 309 | <option>--table-idx-width=</option><replaceable>NUM</replaceable> |
| 310 | </term> |
| 311 | <listitem> |
| 312 | <para>use <replaceable>NUM</replaceable> bits to index the CRC table; |
| 313 | <replaceable>NUM</replaceable> must be one of the values |
| 314 | {<replaceable>1</replaceable>, <replaceable>2</replaceable>, |
| 315 | <replaceable>4</replaceable>, <replaceable>8</replaceable>}.</para> |
| 316 | </listitem> |
| 317 | </varlistentry> |
| 318 | <varlistentry> |
| 319 | <term> |
| 320 | <option>--force-poly</option> |
| 321 | </term> |
| 322 | <listitem> |
| 323 | <para>override any errors about possibly unsuitable |
| 324 | polynoms. pycrc does not allow even polynoms or |
| 325 | polynoms that are wider than &width;. Use this option |
| 326 | to override the error, if you know what you are |
| 327 | doing.</para> |
| 328 | </listitem> |
| 329 | </varlistentry> |
| 330 | <varlistentry> |
| 331 | <term> |
| 332 | <option>--symbol-prefix=</option><replaceable>STRING</replaceable> |
| 333 | </term> |
| 334 | <listitem> |
| 335 | <para>when generating source code, use <replaceable>STRING</replaceable> |
| 336 | as prefix to the exported C symbols.</para> |
| 337 | </listitem> |
| 338 | </varlistentry> |
| 339 | <varlistentry> |
| 340 | <term> |
| 341 | <option>--crc-type=</option><replaceable>STRING</replaceable> |
| 342 | </term> |
| 343 | <listitem> |
| 344 | <para>when generating source code, use <replaceable>STRING</replaceable> as crc_t type.</para> |
| 345 | </listitem> |
| 346 | </varlistentry> |
| 347 | <varlistentry> |
| 348 | <term> |
| 349 | <option>--include-file=</option><replaceable>FILE</replaceable> |
| 350 | </term> |
| 351 | <listitem> |
| 352 | <para>when generating source code, include also <replaceable>FILE</replaceable> as header file. |
| 353 | This option can be specified multiple times.</para> |
| 354 | </listitem> |
| 355 | </varlistentry> |
| 356 | <varlistentry> |
| 357 | <term> |
| 358 | <option>-o</option><replaceable>FILE</replaceable> |
| 359 | </term> |
| 360 | <term> |
| 361 | <option>--output=</option><replaceable>FILE</replaceable> |
| 362 | </term> |
| 363 | <listitem> |
| 364 | <para>write the generated code to <replaceable>FILE</replaceable> instead of <filename>stdout</filename>.</para> |
| 365 | </listitem> |
| 366 | </varlistentry> |
| 367 | </variablelist> |
| 368 | </refsect1> |
| 369 | |
| 370 | <refsect1> |
| 371 | <title>The CRC Parametric Model</title> |
| 372 | <para> |
| 373 | The parametric model follows Ross N. Williams' convention described in |
| 374 | <link xlink:href="http://www.ross.net/crc/crcpaper.html">A Painless Guide to CRC Error Detection Algorithms</link>, |
| 375 | often called the Rocksoft Model. |
| 376 | Since most people are familiar with this kind of parameters, pycrc follows this convention, described as follows: |
| 377 | <glosslist> |
| 378 | <glossentry> |
| 379 | <glossterm><replaceable>&width;</replaceable></glossterm> |
| 380 | <glossdef> |
| 381 | <para> |
| 382 | The number of significant bits in the CRC <replaceable>&poly;</replaceable>, |
| 383 | excluding the most significant 1. |
| 384 | This will also be the number of bits in the final CRC result. |
| 385 | In previous versions of pycrc only multiples of 8 could be used as |
| 386 | <replaceable>&width;</replaceable> for the <replaceable>&table-driven;</replaceable> algorithm. |
| 387 | As of version 0.7.5 any value is accepted for <replaceable>&width;</replaceable> for all algorithms. |
| 388 | </para> |
| 389 | </glossdef> |
| 390 | </glossentry> |
| 391 | <glossentry> |
| 392 | <glossterm><replaceable>&poly;</replaceable></glossterm> |
| 393 | <glossdef> |
| 394 | <para> |
| 395 | The unreflected polynomial of the CRC algorithm. |
| 396 | </para> |
| 397 | <para> |
| 398 | The <replaceable>&poly;</replaceable> may be specified in its standard form, |
| 399 | i.e. with bit <replaceable>&width;</replaceable>+1 set to 1, but the most significant |
| 400 | bit may also be omitted. |
| 401 | For example, both numbers 0x18005 and 0x8005 are accepted for a 16-bit |
| 402 | <replaceable>&poly;</replaceable>. |
| 403 | </para> |
| 404 | <para> |
| 405 | Most polynomials used in real world applications are odd (the least significant |
| 406 | bit is 1), but there are some good even ones. |
| 407 | pycrc allows the use of even polynomials with the <option>--force-poly</option> |
| 408 | option. |
| 409 | Some even polynomials may yield incorrect checksums depending on the used algorithm. |
| 410 | Use at your own risk and if at all possible use a well-known <replaceable>MODEL</replaceable> above. |
| 411 | </para> |
| 412 | </glossdef> |
| 413 | </glossentry> |
| 414 | <glossentry> |
| 415 | <glossterm><replaceable>&reflect_in;</replaceable></glossterm> |
| 416 | <glossdef> |
| 417 | <para> |
| 418 | Reflect the octets of the message before processing them. |
| 419 | </para> |
| 420 | <para> |
| 421 | A word is reflected or reversed by <quote>flipping</quote> its bits around the |
| 422 | mid-point of the word. |
| 423 | The most significant bit of the word is moved to the least significant position, |
| 424 | the second-most significant bit is moved to the second-least significant position |
| 425 | and so on. |
| 426 | The reflected value of 0xa2 (10100010b) is 0x45 (01000101b), for example. |
| 427 | </para> |
| 428 | <para> |
| 429 | Some CRC algorithms can be implemented more efficiently in a bit reversed version, |
| 430 | that's why many of the standard CRC models use reflected input octets. |
| 431 | </para> |
| 432 | </glossdef> |
| 433 | </glossentry> |
| 434 | <glossentry> |
| 435 | <glossterm><replaceable>&xor_in;</replaceable></glossterm> |
| 436 | <glossdef> |
| 437 | <para> |
| 438 | The initial value (usually all 0 or all 1) for algorithms which operate on the |
| 439 | non-augmented message, that is, any algorithm other than the |
| 440 | <replaceable>&bit-by-bit;</replaceable> one. |
| 441 | This value can be interpreted as a value which will be XOR-ed into the CRC register |
| 442 | after <replaceable>&width;</replaceable> iterations of the |
| 443 | <replaceable>&bit-by-bit;</replaceable> algorithm. |
| 444 | This implies that the simple <replaceable>&bit-by-bit;</replaceable> algorithm must |
| 445 | calculate the initial value using some sort of reverse CRC algorithm on the |
| 446 | <replaceable>&xor_in;</replaceable> value. |
| 447 | </para> |
| 448 | </glossdef> |
| 449 | </glossentry> |
| 450 | <glossentry> |
| 451 | <glossterm><replaceable>&reflect_out;</replaceable></glossterm> |
| 452 | <glossdef> |
| 453 | <para> |
| 454 | Reflect the final CRC result. This operation takes place before XOR-ing the final CRC |
| 455 | value with the <replaceable>&xor_out;</replaceable> parameter. |
| 456 | </para> |
| 457 | </glossdef> |
| 458 | </glossentry> |
| 459 | <glossentry> |
| 460 | <glossterm><replaceable>&xor_out;</replaceable></glossterm> |
| 461 | <glossdef> |
| 462 | <para> |
| 463 | A value (usually all bits 0 or all 1) which will be XOR-ed to the final CRC value. |
| 464 | </para> |
| 465 | </glossdef> |
| 466 | </glossentry> |
| 467 | <glossentry> |
| 468 | <glossterm><replaceable>✓</replaceable></glossterm> |
| 469 | <glossdef> |
| 470 | <para> |
| 471 | This value is not exactly a parameter of a model but it is sometimes given together |
| 472 | with the Rocksoft Model parameters. |
| 473 | It is the CRC value of the parametrised model over the string |
| 474 | <quote><replaceable>123456789</replaceable></quote> and |
| 475 | can be used as a sanity check for a particular CRC implementation. |
| 476 | </para> |
| 477 | </glossdef> |
| 478 | </glossentry> |
| 479 | </glosslist> |
| 480 | </para> |
| 481 | </refsect1> |
| 482 | |
| 483 | <refsect1> |
| 484 | <title>Code generation</title> |
| 485 | <para> |
| 486 | In the default configuration, the generated code is strict ISO C99. |
| 487 | A minimal set of three functions are defined for each algorithm: |
| 488 | <function>crc_init()</function>, <function>crc_update()</function> and <function>crc_finalize()</function>. |
| 489 | Depending on the number of parameters given to pycrc, a different interface will be defined. |
| 490 | A fully parametrised model has a simpler API, while the generated code for a runtime-specified |
| 491 | implementation requires a pointer to a configuration structure as first parameter to all functions. |
| 492 | </para> |
| 493 | <para> |
| 494 | The generated source code uses the type <type>crc_t</type>, which is used throughout the code |
| 495 | to hold intermediate results and also the final CRC value. |
| 496 | It is defined in the generated header file and its type may be overridden with the |
| 497 | <option>--crc-type</option> option. |
| 498 | </para> |
| 499 | |
| 500 | <refsect2><title>Fully parametrised models</title> |
| 501 | <para> |
| 502 | The prototypes of the CRC functions are normally generated by pycrc using the |
| 503 | <replaceable>--generate h</replaceable> option. |
| 504 | The CRC functions for a fully parametrised model will look like: |
| 505 | </para> |
| 506 | <funcsynopsis> |
| 507 | <funcsynopsisinfo> |
| 508 | #include <stdlib.h> |
| 509 | typedef uint16_t crc_t; /* pycrc will use an appropriate size here */ |
| 510 | </funcsynopsisinfo> |
| 511 | <funcprototype> |
| 512 | <?dbhtml funcsynopsis-style='ansi'?> |
| 513 | <funcdef>crc_t <function>crc_init</function></funcdef> |
| 514 | <void/> |
| 515 | </funcprototype> |
| 516 | |
| 517 | <funcprototype> |
| 518 | <?dbhtml funcsynopsis-style='ansi'?> |
| 519 | <funcdef>crc_t <function>crc_update</function></funcdef> |
| 520 | <paramdef>crc_t <parameter>crc</parameter></paramdef> |
| 521 | <paramdef>const unsigned char *<parameter>data</parameter></paramdef> |
| 522 | <paramdef>size_t <parameter>data_len</parameter></paramdef> |
| 523 | </funcprototype> |
| 524 | |
| 525 | <funcprototype> |
| 526 | <?dbhtml funcsynopsis-style='ansi'?> |
| 527 | <funcdef>crc_t <function>crc_finalize</function></funcdef> |
| 528 | <paramdef>crc_t <parameter>crc</parameter></paramdef> |
| 529 | </funcprototype> |
| 530 | </funcsynopsis> |
| 531 | |
| 532 | <para> |
| 533 | The code snippet below shows how to use the generated functions. |
| 534 | <programlisting> |
| 535 | #include "pycrc_generated_crc.h" |
| 536 | #include <stdio.h> |
| 537 | |
| 538 | int main(void) |
| 539 | { |
| 540 | static const unsigned char str1[] = "1234"; |
| 541 | static const unsigned char str2[] = "56789"; |
| 542 | crc_t crc; |
| 543 | |
| 544 | crc = crc_init(); |
| 545 | crc = crc_update(crc, str1, sizeof(str1) - 1); |
| 546 | crc = crc_update(crc, str2, sizeof(str2) - 1); |
| 547 | /* more calls to crc_update... */ |
| 548 | crc = crc_finalize(crc); |
| 549 | |
| 550 | printf("0x%lx\n", (long)crc); |
| 551 | return 0; |
| 552 | } |
| 553 | </programlisting> |
| 554 | </para> |
| 555 | </refsect2> |
| 556 | |
| 557 | <refsect2> |
| 558 | <title>Models with runtime-configurable parameters</title> |
| 559 | <para> |
| 560 | When the model is not fully defined then the missing parameters are stored in a structure of |
| 561 | type <type>crc_cfg_t</type>. |
| 562 | If a CRC function requires a value from the <type>crc_cfg_t</type> structure, then the first |
| 563 | function argument is always a pointer to that structure. |
| 564 | All fields of the configuration structure must be properly initialised before the first call |
| 565 | to any CRC function. |
| 566 | </para> |
| 567 | <para> |
| 568 | If the <replaceable>&width;</replaceable> was not specified when the code was generated, then |
| 569 | the <type>crc_cfg_t</type> structure will contain three more fields: |
| 570 | <parameter>msb_mask</parameter>, <parameter>crc_mask</parameter> and <parameter>crc_shift</parameter>. |
| 571 | They are defined for performance reasons and must be initialised to the value given next to the |
| 572 | field definition. |
| 573 | </para> |
| 574 | <para> |
| 575 | For example, a completely undefined CRC implementation will generate a <type>crc_cfg_t</type> |
| 576 | structure as below: |
| 577 | <programlisting> |
| 578 | typedef struct { |
| 579 | unsigned int width; |
| 580 | crc_t poly; |
| 581 | bool reflect_in; |
| 582 | crc_t xor_in; |
| 583 | bool reflect_out; |
| 584 | crc_t xor_out; |
| 585 | |
| 586 | // internal parameters |
| 587 | crc_t msb_mask; // initialise as (crc_t)1u << (cfg->width - 1) |
| 588 | crc_t crc_mask; // initialise as (cfg->msb_mask - 1) | cfg->msb_mask |
| 589 | unsigned int crc_shift; // initialise as cfg->width < 8 ? 8 - cfg->width : 0 |
| 590 | } crc_cfg_t; |
| 591 | </programlisting> |
| 592 | </para> |
| 593 | <para> |
| 594 | <parameter>msb_mask</parameter> is a bitmask with the most significant bit of a |
| 595 | <replaceable>&width;</replaceable> bits wide data type set to 1. |
| 596 | |
| 597 | <parameter>crc_mask</parameter> is a bitmask with all bits of a |
| 598 | <replaceable>&width;</replaceable> bits wide data type set to 1. |
| 599 | |
| 600 | <parameter>crc_shift</parameter> is a shift counter that is used when |
| 601 | <replaceable>&width;</replaceable> is less than 8. |
| 602 | It is the number of bits to shift the CRC register to align its top bit to a byte boundary. |
| 603 | </para> |
| 604 | |
| 605 | <para> |
| 606 | The file <filename>test/main.c</filename> in the source package of pycrc |
| 607 | contains a fully featured example of how to use the generated source code. |
| 608 | A shorter, more compact <code>main()</code> function can be generated with the |
| 609 | <replaceable>--generate c-main</replaceable> option. |
| 610 | This second variant is the better option as it will always output valid code when |
| 611 | some of the CRC parameters are known and some are unknown during code generation. |
| 612 | </para> |
| 613 | </refsect2> |
| 614 | </refsect1> |
| 615 | |
| 616 | <refsect1><title>Examples</title> |
| 617 | <para> |
| 618 | <glosslist> |
| 619 | <glossentry> |
| 620 | <glossterm>Calculate the CRC-32 checksum of the string <quote>123456789</quote>:</glossterm> |
| 621 | <glossdef> |
| 622 | <para> |
| 623 | <userinput>python pycrc.py --model crc-32 --check-string 123456789</userinput> |
| 624 | </para> |
| 625 | </glossdef> |
| 626 | </glossentry> |
| 627 | <glossentry> |
| 628 | <glossterm>Generate the source code of the table-driven algorithm for an embedded application.</glossterm> |
| 629 | <glossdef> |
| 630 | <para> |
| 631 | The table index width of 4 bits ensures a moderate memory usage. |
| 632 | To be precise, the size of the resulting table will be <code>16 * sizeof(crc_t)</code>. |
| 633 | </para> |
| 634 | <para> |
| 635 | <userinput>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate h -o crc.h</userinput> |
| 636 | </para> |
| 637 | <para> |
| 638 | <userinput>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c -o crc.c</userinput> |
| 639 | </para> |
| 640 | <para> |
| 641 | A variant of the <replaceable>c</replaceable> target is <replaceable>c-main</replaceable>: |
| 642 | this target will generate a simple <replaceable>main()</replaceable> function in addition to |
| 643 | the CRC functions: |
| 644 | </para> |
| 645 | <para> |
| 646 | <userinput>python pycrc.py --model crc-16 --algorithm table-driven --table-idx-width 4 --generate c-main -o crc.c</userinput> |
| 647 | </para> |
| 648 | </glossdef> |
| 649 | </glossentry> |
| 650 | <glossentry> |
| 651 | <glossterm>Generate the CRC table only:</glossterm> |
| 652 | <glossdef> |
| 653 | <para> |
| 654 | <userinput>python pycrc.py --model kermit --generate table -o crc-table.txt</userinput> |
| 655 | </para> |
| 656 | </glossdef> |
| 657 | </glossentry> |
| 658 | </glosslist> |
| 659 | </para> |
| 660 | </refsect1> |
| 661 | |
| 662 | <refsect1> |
| 663 | <title>See Also</title> |
| 664 | <para> |
| 665 | The homepage of pycrc is <link xlink:href="&project_homepage;">&project_homepage;</link>. |
| 666 | </para> |
| 667 | <para> |
| 668 | A list of common CRC models is at <link xlink:href="&project_models;">&project_models;</link>. |
| 669 | For a long list of known CRC models, see Greg Cook's |
| 670 | <link xlink:href="http://reveng.sourceforge.net/crc-catalogue/">Catalogue of Parameterised CRC Algorithms</link>. |
| 671 | </para> |
| 672 | </refsect1> |
| 673 | |
| 674 | <refsect1> |
| 675 | <title>Copyright</title> |
| 676 | <para> |
| 677 | This work is licensed under a |
| 678 | <link xlink:href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</link>. |
| 679 | </para> |
| 680 | </refsect1> |
| 681 | </refentry> |