File: crcmod.predefined.rst

package info (click to toggle)
python-crcmod 1.7%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 304 kB
  • sloc: python: 1,782; ansic: 671; makefile: 100
file content (164 lines) | stat: -rw-r--r-- 9,235 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

:mod:`crcmod.predefined` -- CRC calculation using predefined algorithms
=======================================================================

.. module:: crcmod.predefined
   :synopsis: CRC calculation using predefined algorithms
.. moduleauthor:: Craig McQueen
.. sectionauthor:: Craig McQueen

This module provides a function factory :func:`mkPredefinedCrcFun` and a class :class:`PredefinedCrc`
for calculating CRCs of byte strings using common predefined CRC algorithms.

The function factory and the class are very similar to those defined in :mod:`crcmod`,
except that the CRC algorithm is specified by a predefined name, rather than the
individual polynomial, reflection, and initial and final-XOR parameters.

Predefined CRC algorithms
-------------------------

The :mod:`crcmod.predefined` module offers the following predefined algorithms:

================================  ======================  ==========  ====================  ====================  ====================
Name                              Polynomial              Reversed?   Init-value            XOR-out               Check
================================  ======================  ==========  ====================  ====================  ====================
``crc-8``                         0x107                   False       0x00                  0x00                  0xF4
``crc-8-darc``                    0x139                   True        0x00                  0x00                  0x15
``crc-8-i-code``                  0x11D                   False       0xFD                  0x00                  0x7E
``crc-8-itu``                     0x107                   False       0x55                  0x55                  0xA1
``crc-8-maxim``                   0x131                   True        0x00                  0x00                  0xA1
``crc-8-rohc``                    0x107                   True        0xFF                  0x00                  0xD0
``crc-8-wcdma``                   0x19B                   True        0x00                  0x00                  0x25
``crc-16``                        0x18005                 True        0x0000                0x0000                0xBB3D
``crc-16-buypass``                0x18005                 False       0x0000                0x0000                0xFEE8
``crc-16-dds-110``                0x18005                 False       0x800D                0x0000                0x9ECF
``crc-16-dect``                   0x10589                 False       0x0001                0x0001                0x007E
``crc-16-dnp``                    0x13D65                 True        0xFFFF                0xFFFF                0xEA82
``crc-16-en-13757``               0x13D65                 False       0xFFFF                0xFFFF                0xC2B7
``crc-16-genibus``                0x11021                 False       0x0000                0xFFFF                0xD64E
``crc-16-maxim``                  0x18005                 True        0xFFFF                0xFFFF                0x44C2
``crc-16-mcrf4xx``                0x11021                 True        0xFFFF                0x0000                0x6F91
``crc-16-riello``                 0x11021                 True        0x554D                0x0000                0x63D0
``crc-16-t10-dif``                0x18BB7                 False       0x0000                0x0000                0xD0DB
``crc-16-teledisk``               0x1A097                 False       0x0000                0x0000                0x0FB3
``crc-16-usb``                    0x18005                 True        0x0000                0xFFFF                0xB4C8
``x-25``                          0x11021                 True        0x0000                0xFFFF                0x906E
``xmodem``                        0x11021                 False       0x0000                0x0000                0x31C3
``modbus``                        0x18005                 True        0xFFFF                0x0000                0x4B37
``kermit`` [#ccitt]_              0x11021                 True        0x0000                0x0000                0x2189
``crc-ccitt-false`` [#ccitt]_     0x11021                 False       0xFFFF                0x0000                0x29B1
``crc-aug-ccitt`` [#ccitt]_       0x11021                 False       0x1D0F                0x0000                0xE5CC
``crc-24``                        0x1864CFB               False       0xB704CE              0x000000              0x21CF02
``crc-24-flexray-a``              0x15D6DCB               False       0xFEDCBA              0x000000              0x7979BD
``crc-24-flexray-b``              0x15D6DCB               False       0xABCDEF              0x000000              0x1F23B8
``crc-32``                        0x104C11DB7             True        0x00000000            0xFFFFFFFF            0xCBF43926
``crc-32-bzip2``                  0x104C11DB7             False       0x00000000            0xFFFFFFFF            0xFC891918
``crc-32c``                       0x11EDC6F41             True        0x00000000            0xFFFFFFFF            0xE3069283
``crc-32d``                       0x1A833982B             True        0x00000000            0xFFFFFFFF            0x87315576
``crc-32-mpeg``                   0x104C11DB7             False       0xFFFFFFFF            0x00000000            0x0376E6E7
``posix``                         0x104C11DB7             False       0xFFFFFFFF            0xFFFFFFFF            0x765E7680
``crc-32q``                       0x1814141AB             False       0x00000000            0x00000000            0x3010BF7F
``jamcrc``                        0x104C11DB7             True        0xFFFFFFFF            0x00000000            0x340BC6D9
``xfer``                          0x1000000AF             False       0x00000000            0x00000000            0xBD0BE338
``crc-64``                        0x1000000000000001B     True        0x0000000000000000    0x0000000000000000    0x46A5A9388A5BEFFE
``crc-64-we``                     0x142F0E1EBA9EA3693     False       0x0000000000000000    0xFFFFFFFFFFFFFFFF    0x62EC59E3F1A4F00A
``crc-64-jones``                  0x1AD93D23594C935A9     True        0xFFFFFFFFFFFFFFFF    0x0000000000000000    0xCAA717168609F281
================================  ======================  ==========  ====================  ====================  ====================

.. rubric:: Notes

.. [#ccitt] Definitions of CCITT are disputable. See:

    * http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
    * http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm

:func:`mkPredefinedCrcFun` -- CRC function factory
--------------------------------------------------

The function factory provides a simple interface for CRC calculation. It is similar
to :func:`crcmod.mkCrcFun`, except that it specifies a CRC algorithm by name rather
than its parameters.

.. function:: mkPredefinedCrcFun(crc_name)

   Function factory that returns a new function for calculating CRCs
   using a specified CRC algorithm.

   :param crc_name: The name of the predefined CRC algorithm to use.
   :type crc_name:  string

   :return:         CRC calculation function
   :rtype:          function

   The function that is returned is the same as that returned by :func:`crcmod.mkCrcFun`:
   
   .. function:: .crc_function(data[, crc=initCrc])

   :param data:     Data for which to calculate the CRC.
   :type data:      byte string

   :param crc:      Initial CRC value.

   :return:         Calculated CRC value.
   :rtype:          integer

.. function:: mkCrcFun(crc_name)

   This is an alias for :func:`crcmod.predefined.mkPredefinedCrcFun`. However, it is not defined when
   :mod:`crcmod.predefined` is imported using the form::
   
       >>> from crcmod.predefined import *

Examples
^^^^^^^^

**CRC-32** example::

   >>> import crcmod.predefined
   
   >>> crc32_func = crcmod.predefined.mkCrcFun('crc-32')
   >>> hex(crc32_func('123456789'))
   '0xcbf43926L'

**XMODEM** example::

   >>> xmodem_crc_func = crcmod.predefined.mkCrcFun('xmodem')
   >>> hex(xmodem_crc_func('123456789'))
   '0x31c3'


Class :class:`PredefinedCrc`
----------------------------

This class is inherited from the :class:`crcmod.Crc` class, and is the same except for the
initialization.  It specifies a CRC algorithm by name rather than its parameters.

.. class:: PredefinedCrc(crc_name)

   Returns a new :class:`Crc` object for calculating CRCs using a specified CRC algorithm.
   
   The parameter is the same as that for the factory function :func:`crcmod.predefined.mkPredefinedCrcFun`.

   :param crc_name: The name of the predefined CRC algorithm to use.
   :type crc_name:  string

.. class:: Crc(poly[, initCrc, rev, xorOut])

   This is an alias for :class:`crcmod.predefined.PredefinedCrc`. However, it is not defined when
   :mod:`crcmod.predefined` is imported using the form::
   
       >>> from crcmod.predefined import *

Examples
^^^^^^^^

**CRC-32** Example::

   >>> import crcmod.predefined
   
   >>> crc32 = crcmod.predefined.Crc('crc-32')
   >>> crc32.update('123456789')
   >>> hex(crc32.crcValue)
   '0xcbf43926L'
   >>> crc32.hexdigest()
   'CBF43926'