comparison cutils/crcmod/python2/predefined.py @ 180:d038f0a9ba49

Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums. Running python -m cutils.crcmod.test works successfully.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 13 Jan 2025 04:09:35 +0100
parents
children 31d478ff4e09
comparison
equal deleted inserted replaced
179:53614a724bf0 180:d038f0a9ba49
1 #-----------------------------------------------------------------------------
2 # Copyright (c) 2010 Craig McQueen
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21 #-----------------------------------------------------------------------------
22 '''
23 crcmod.predefined defines some well-known CRC algorithms.
24
25 To use it, e.g.:
26 import crcmod.predefined
27
28 crc32func = crcmod.predefined.mkPredefinedCrcFun("crc-32")
29 crc32class = crcmod.predefined.PredefinedCrc("crc-32")
30
31 crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
32 But if doing 'from crc.predefined import *', only PredefinedCrc is imported.
33 '''
34
35 from __future__ import absolute_import
36
37 # local imports
38 from . import crcmod
39
40 __all__ = [
41 'PredefinedCrc',
42 'mkPredefinedCrcFun',
43 ]
44
45 REVERSE = True
46 NON_REVERSE = False
47
48 # The following table defines the parameters of well-known CRC algorithms.
49 # The "Check" value is the CRC for the ASCII byte sequence "123456789". It
50 # can be used for unit tests.
51 _crc_definitions_table = [
52 # Name Identifier-name, Poly Reverse Init-value XOR-out Check
53 [ 'crc-8', 'Crc8', 0x107, NON_REVERSE, 0x00, 0x00, 0xF4, ],
54 [ 'crc-8-darc', 'Crc8Darc', 0x139, REVERSE, 0x00, 0x00, 0x15, ],
55 [ 'crc-8-i-code', 'Crc8ICode', 0x11D, NON_REVERSE, 0xFD, 0x00, 0x7E, ],
56 [ 'crc-8-itu', 'Crc8Itu', 0x107, NON_REVERSE, 0x55, 0x55, 0xA1, ],
57 [ 'crc-8-maxim', 'Crc8Maxim', 0x131, REVERSE, 0x00, 0x00, 0xA1, ],
58 [ 'crc-8-rohc', 'Crc8Rohc', 0x107, REVERSE, 0xFF, 0x00, 0xD0, ],
59 [ 'crc-8-wcdma', 'Crc8Wcdma', 0x19B, REVERSE, 0x00, 0x00, 0x25, ],
60
61 [ 'crc-16', 'Crc16', 0x18005, REVERSE, 0x0000, 0x0000, 0xBB3D, ],
62 [ 'crc-16-buypass', 'Crc16Buypass', 0x18005, NON_REVERSE, 0x0000, 0x0000, 0xFEE8, ],
63 [ 'crc-16-dds-110', 'Crc16Dds110', 0x18005, NON_REVERSE, 0x800D, 0x0000, 0x9ECF, ],
64 [ 'crc-16-dect', 'Crc16Dect', 0x10589, NON_REVERSE, 0x0001, 0x0001, 0x007E, ],
65 [ 'crc-16-dnp', 'Crc16Dnp', 0x13D65, REVERSE, 0xFFFF, 0xFFFF, 0xEA82, ],
66 [ 'crc-16-en-13757', 'Crc16En13757', 0x13D65, NON_REVERSE, 0xFFFF, 0xFFFF, 0xC2B7, ],
67 [ 'crc-16-genibus', 'Crc16Genibus', 0x11021, NON_REVERSE, 0x0000, 0xFFFF, 0xD64E, ],
68 [ 'crc-16-maxim', 'Crc16Maxim', 0x18005, REVERSE, 0xFFFF, 0xFFFF, 0x44C2, ],
69 [ 'crc-16-mcrf4xx', 'Crc16Mcrf4xx', 0x11021, REVERSE, 0xFFFF, 0x0000, 0x6F91, ],
70 [ 'crc-16-riello', 'Crc16Riello', 0x11021, REVERSE, 0x554D, 0x0000, 0x63D0, ],
71 [ 'crc-16-t10-dif', 'Crc16T10Dif', 0x18BB7, NON_REVERSE, 0x0000, 0x0000, 0xD0DB, ],
72 [ 'crc-16-teledisk', 'Crc16Teledisk', 0x1A097, NON_REVERSE, 0x0000, 0x0000, 0x0FB3, ],
73 [ 'crc-16-usb', 'Crc16Usb', 0x18005, REVERSE, 0x0000, 0xFFFF, 0xB4C8, ],
74 [ 'x-25', 'CrcX25', 0x11021, REVERSE, 0x0000, 0xFFFF, 0x906E, ],
75 [ 'xmodem', 'CrcXmodem', 0x11021, NON_REVERSE, 0x0000, 0x0000, 0x31C3, ],
76 [ 'modbus', 'CrcModbus', 0x18005, REVERSE, 0xFFFF, 0x0000, 0x4B37, ],
77
78 # Note definitions of CCITT are disputable. See:
79 # http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
80 # http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
81 [ 'kermit', 'CrcKermit', 0x11021, REVERSE, 0x0000, 0x0000, 0x2189, ],
82 [ 'crc-ccitt-false', 'CrcCcittFalse', 0x11021, NON_REVERSE, 0xFFFF, 0x0000, 0x29B1, ],
83 [ 'crc-aug-ccitt', 'CrcAugCcitt', 0x11021, NON_REVERSE, 0x1D0F, 0x0000, 0xE5CC, ],
84
85 [ 'crc-24', 'Crc24', 0x1864CFB, NON_REVERSE, 0xB704CE, 0x000000, 0x21CF02, ],
86 [ 'crc-24-flexray-a', 'Crc24FlexrayA', 0x15D6DCB, NON_REVERSE, 0xFEDCBA, 0x000000, 0x7979BD, ],
87 [ 'crc-24-flexray-b', 'Crc24FlexrayB', 0x15D6DCB, NON_REVERSE, 0xABCDEF, 0x000000, 0x1F23B8, ],
88
89 [ 'crc-32', 'Crc32', 0x104C11DB7, REVERSE, 0x00000000, 0xFFFFFFFF, 0xCBF43926, ],
90 [ 'crc-32-bzip2', 'Crc32Bzip2', 0x104C11DB7, NON_REVERSE, 0x00000000, 0xFFFFFFFF, 0xFC891918, ],
91 [ 'crc-32c', 'Crc32C', 0x11EDC6F41, REVERSE, 0x00000000, 0xFFFFFFFF, 0xE3069283, ],
92 [ 'crc-32d', 'Crc32D', 0x1A833982B, REVERSE, 0x00000000, 0xFFFFFFFF, 0x87315576, ],
93 [ 'crc-32-mpeg', 'Crc32Mpeg', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0x00000000, 0x0376E6E7, ],
94 [ 'posix', 'CrcPosix', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ],
95 [ 'crc-32q', 'Crc32Q', 0x1814141AB, NON_REVERSE, 0x00000000, 0x00000000, 0x3010BF7F, ],
96 [ 'jamcrc', 'CrcJamCrc', 0x104C11DB7, REVERSE, 0xFFFFFFFF, 0x00000000, 0x340BC6D9, ],
97 [ 'xfer', 'CrcXfer', 0x1000000AF, NON_REVERSE, 0x00000000, 0x00000000, 0xBD0BE338, ],
98
99 # 64-bit
100 # Name Identifier-name, Poly Reverse Init-value XOR-out Check
101 [ 'crc-64', 'Crc64', 0x1000000000000001B, REVERSE, 0x0000000000000000, 0x0000000000000000, 0x46A5A9388A5BEFFE, ],
102 [ 'crc-64-we', 'Crc64We', 0x142F0E1EBA9EA3693, NON_REVERSE, 0x0000000000000000, 0xFFFFFFFFFFFFFFFF, 0x62EC59E3F1A4F00A, ],
103 [ 'crc-64-jones', 'Crc64Jones', 0x1AD93D23594C935A9, REVERSE, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0xCAA717168609F281, ],
104 ]
105
106
107 def _simplify_name(name):
108 """
109 Reduce CRC definition name to a simplified form:
110 * lowercase
111 * dashes removed
112 * spaces removed
113 * any initial "CRC" string removed
114 """
115 name = name.lower()
116 name = name.replace('-', '')
117 name = name.replace(' ', '')
118 if name.startswith('crc'):
119 name = name[len('crc'):]
120 return name
121
122
123 _crc_definitions_by_name = {}
124 _crc_definitions_by_identifier = {}
125 _crc_definitions = []
126
127 _crc_table_headings = [ 'name', 'identifier', 'poly', 'reverse', 'init', 'xor_out', 'check' ]
128
129 for table_entry in _crc_definitions_table:
130 crc_definition = dict(zip(_crc_table_headings, table_entry))
131 _crc_definitions.append(crc_definition)
132 name = _simplify_name(table_entry[0])
133 if name in _crc_definitions_by_name:
134 raise Exception("Duplicate entry for '%s' in CRC table" % name)
135 _crc_definitions_by_name[name] = crc_definition
136 _crc_definitions_by_identifier[table_entry[1]] = crc_definition
137
138
139 def _get_definition_by_name(crc_name):
140 definition = _crc_definitions_by_name.get(_simplify_name(crc_name), None)
141 if not definition:
142 definition = _crc_definitions_by_identifier.get(crc_name, None)
143 if not definition:
144 raise KeyError("Unkown CRC name '%s'" % crc_name)
145 return definition
146
147
148 class PredefinedCrc(crcmod.Crc):
149 def __init__(self, crc_name):
150 definition = _get_definition_by_name(crc_name)
151 crcmod.Crc.__init__(self, poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
152
153
154 # crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
155 Crc = PredefinedCrc
156
157
158 def mkPredefinedCrcFun(crc_name):
159 definition = _get_definition_by_name(crc_name)
160 return crcmod.mkCrcFun(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
161
162
163 # crcmod.predefined.mkCrcFun is an alias for crcmod.predefined.mkPredefinedCrcFun
164 mkCrcFun = mkPredefinedCrcFun