Debian Codegraph @

docs/source/crcmod.predefined.rst

raw
1
2 :mod:`crcmod.predefined` -- CRC calculation using predefined algorithms
3 =======================================================================
4
5 .. module:: crcmod.predefined
6 :synopsis: CRC calculation using predefined algorithms
7 .. moduleauthor:: Craig McQueen
8 .. sectionauthor:: Craig McQueen
9
10 This module provides a function factory :func:`mkPredefinedCrcFun` and a class :class:`PredefinedCrc`
11 for calculating CRCs of byte strings using common predefined CRC algorithms.
12
13 The function factory and the class are very similar to those defined in :mod:`crcmod`,
14 except that the CRC algorithm is specified by a predefined name, rather than the
15 individual polynomial, reflection, and initial and final-XOR parameters.
16
17 Predefined CRC algorithms
18 -------------------------
19
20 The :mod:`crcmod.predefined` module offers the following predefined algorithms:
21
22 ================================ ====================== ========== ==================== ==================== ====================
23 Name Polynomial Reversed? Init-value XOR-out Check
24 ================================ ====================== ========== ==================== ==================== ====================
25 ``crc-8`` 0x107 False 0x00 0x00 0xF4
26 ``crc-8-darc`` 0x139 True 0x00 0x00 0x15
27 ``crc-8-i-code`` 0x11D False 0xFD 0x00 0x7E
28 ``crc-8-itu`` 0x107 False 0x55 0x55 0xA1
29 ``crc-8-maxim`` 0x131 True 0x00 0x00 0xA1
30 ``crc-8-rohc`` 0x107 True 0xFF 0x00 0xD0
31 ``crc-8-wcdma`` 0x19B True 0x00 0x00 0x25
32 ``crc-16`` 0x18005 True 0x0000 0x0000 0xBB3D
33 ``crc-16-buypass`` 0x18005 False 0x0000 0x0000 0xFEE8
34 ``crc-16-dds-110`` 0x18005 False 0x800D 0x0000 0x9ECF
35 ``crc-16-dect`` 0x10589 False 0x0001 0x0001 0x007E
36 ``crc-16-dnp`` 0x13D65 True 0xFFFF 0xFFFF 0xEA82
37 ``crc-16-en-13757`` 0x13D65 False 0xFFFF 0xFFFF 0xC2B7
38 ``crc-16-genibus`` 0x11021 False 0x0000 0xFFFF 0xD64E
39 ``crc-16-maxim`` 0x18005 True 0xFFFF 0xFFFF 0x44C2
40 ``crc-16-mcrf4xx`` 0x11021 True 0xFFFF 0x0000 0x6F91
41 ``crc-16-riello`` 0x11021 True 0x554D 0x0000 0x63D0
42 ``crc-16-t10-dif`` 0x18BB7 False 0x0000 0x0000 0xD0DB
43 ``crc-16-teledisk`` 0x1A097 False 0x0000 0x0000 0x0FB3
44 ``crc-16-usb`` 0x18005 True 0x0000 0xFFFF 0xB4C8
45 ``x-25`` 0x11021 True 0x0000 0xFFFF 0x906E
46 ``xmodem`` 0x11021 False 0x0000 0x0000 0x31C3
47 ``modbus`` 0x18005 True 0xFFFF 0x0000 0x4B37
48 ``kermit`` [#ccitt]_ 0x11021 True 0x0000 0x0000 0x2189
49 ``crc-ccitt-false`` [#ccitt]_ 0x11021 False 0xFFFF 0x0000 0x29B1
50 ``crc-aug-ccitt`` [#ccitt]_ 0x11021 False 0x1D0F 0x0000 0xE5CC
51 ``crc-24`` 0x1864CFB False 0xB704CE 0x000000 0x21CF02
52 ``crc-24-flexray-a`` 0x15D6DCB False 0xFEDCBA 0x000000 0x7979BD
53 ``crc-24-flexray-b`` 0x15D6DCB False 0xABCDEF 0x000000 0x1F23B8
54 ``crc-32`` 0x104C11DB7 True 0x00000000 0xFFFFFFFF 0xCBF43926
55 ``crc-32-bzip2`` 0x104C11DB7 False 0x00000000 0xFFFFFFFF 0xFC891918
56 ``crc-32c`` 0x11EDC6F41 True 0x00000000 0xFFFFFFFF 0xE3069283
57 ``crc-32d`` 0x1A833982B True 0x00000000 0xFFFFFFFF 0x87315576
58 ``crc-32-mpeg`` 0x104C11DB7 False 0xFFFFFFFF 0x00000000 0x0376E6E7
59 ``posix`` 0x104C11DB7 False 0xFFFFFFFF 0xFFFFFFFF 0x765E7680
60 ``crc-32q`` 0x1814141AB False 0x00000000 0x00000000 0x3010BF7F
61 ``jamcrc`` 0x104C11DB7 True 0xFFFFFFFF 0x00000000 0x340BC6D9
62 ``xfer`` 0x1000000AF False 0x00000000 0x00000000 0xBD0BE338
63 ``crc-64`` 0x1000000000000001B True 0x0000000000000000 0x0000000000000000 0x46A5A9388A5BEFFE
64 ``crc-64-we`` 0x142F0E1EBA9EA3693 False 0x0000000000000000 0xFFFFFFFFFFFFFFFF 0x62EC59E3F1A4F00A
65 ``crc-64-jones`` 0x1AD93D23594C935A9 True 0xFFFFFFFFFFFFFFFF 0x0000000000000000 0xCAA717168609F281
66 ================================ ====================== ========== ==================== ==================== ====================
67
68 .. rubric:: Notes
69
70 .. [#ccitt] Definitions of CCITT are disputable. See:
71
72 * http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
73 * http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
74
75 :func:`mkPredefinedCrcFun` -- CRC function factory
76 --------------------------------------------------
77
78 The function factory provides a simple interface for CRC calculation. It is similar
79 to :func:`crcmod.mkCrcFun`, except that it specifies a CRC algorithm by name rather
80 than its parameters.
81
82 .. function:: mkPredefinedCrcFun(crc_name)
83
84 Function factory that returns a new function for calculating CRCs
85 using a specified CRC algorithm.
86
87 :param crc_name: The name of the predefined CRC algorithm to use.
88 :type crc_name: string
89
90 :return: CRC calculation function
91 :rtype: function
92
93 The function that is returned is the same as that returned by :func:`crcmod.mkCrcFun`:
94
95 .. function:: .crc_function(data[, crc=initCrc])
96
97 :param data: Data for which to calculate the CRC.
98 :type data: byte string
99
100 :param crc: Initial CRC value.
101
102 :return: Calculated CRC value.
103 :rtype: integer
104
105 .. function:: mkCrcFun(crc_name)
106
107 This is an alias for :func:`crcmod.predefined.mkPredefinedCrcFun`. However, it is not defined when
108 :mod:`crcmod.predefined` is imported using the form::
109
110 >>> from crcmod.predefined import *
111
112 Examples
113 ^^^^^^^^
114
115 **CRC-32** example::
116
117 >>> import crcmod.predefined
118
119 >>> crc32_func = crcmod.predefined.mkCrcFun('crc-32')
120 >>> hex(crc32_func('123456789'))
121 '0xcbf43926L'
122
123 **XMODEM** example::
124
125 >>> xmodem_crc_func = crcmod.predefined.mkCrcFun('xmodem')
126 >>> hex(xmodem_crc_func('123456789'))
127 '0x31c3'
128
129
130 Class :class:`PredefinedCrc`
131 ----------------------------
132
133 This class is inherited from the :class:`crcmod.Crc` class, and is the same except for the
134 initialization. It specifies a CRC algorithm by name rather than its parameters.
135
136 .. class:: PredefinedCrc(crc_name)
137
138 Returns a new :class:`Crc` object for calculating CRCs using a specified CRC algorithm.
139
140 The parameter is the same as that for the factory function :func:`crcmod.predefined.mkPredefinedCrcFun`.
141
142 :param crc_name: The name of the predefined CRC algorithm to use.
143 :type crc_name: string
144
145 .. class:: Crc(poly[, initCrc, rev, xorOut])
146
147 This is an alias for :class:`crcmod.predefined.PredefinedCrc`. However, it is not defined when
148 :mod:`crcmod.predefined` is imported using the form::
149
150 >>> from crcmod.predefined import *
151
152 Examples
153 ^^^^^^^^
154
155 **CRC-32** Example::
156
157 >>> import crcmod.predefined
158
159 >>> crc32 = crcmod.predefined.Crc('crc-32')
160 >>> crc32.update('123456789')
161 >>> hex(crc32.crcValue)
162 '0xcbf43926L'
163 >>> crc32.hexdigest()
164 'CBF43926'