OLD | NEW |
(Empty) | |
| 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 # local imports |
| 36 import crcmod |
| 37 |
| 38 __all__ = [ |
| 39 'PredefinedCrc', |
| 40 'mkPredefinedCrcFun', |
| 41 ] |
| 42 |
| 43 REVERSE = True |
| 44 NON_REVERSE = False |
| 45 |
| 46 # The following table defines the parameters of well-known CRC algorithms. |
| 47 # The "Check" value is the CRC for the ASCII byte sequence "123456789". It |
| 48 # can be used for unit tests. |
| 49 _crc_definitions_table = [ |
| 50 # Name Identifier-name, Poly Reverse
Init-value XOR-out Check |
| 51 [ 'crc-8', 'Crc8', 0x107, NON_REVERSE,
0x00, 0x00, 0xF4, ], |
| 52 [ 'crc-8-darc', 'Crc8Darc', 0x139, REVERSE,
0x00, 0x00, 0x15, ], |
| 53 [ 'crc-8-i-code', 'Crc8ICode', 0x11D, NON_REVERSE,
0xFD, 0x00, 0x7E, ], |
| 54 [ 'crc-8-itu', 'Crc8Itu', 0x107, NON_REVERSE,
0x55, 0x55, 0xA1, ], |
| 55 [ 'crc-8-maxim', 'Crc8Maxim', 0x131, REVERSE,
0x00, 0x00, 0xA1, ], |
| 56 [ 'crc-8-rohc', 'Crc8Rohc', 0x107, REVERSE,
0xFF, 0x00, 0xD0, ], |
| 57 [ 'crc-8-wcdma', 'Crc8Wcdma', 0x19B, REVERSE,
0x00, 0x00, 0x25, ], |
| 58 |
| 59 [ 'crc-16', 'Crc16', 0x18005, REVERSE,
0x0000, 0x0000, 0xBB3D, ], |
| 60 [ 'crc-16-buypass', 'Crc16Buypass', 0x18005, NON_REVERSE,
0x0000, 0x0000, 0xFEE8, ], |
| 61 [ 'crc-16-dds-110', 'Crc16Dds110', 0x18005, NON_REVERSE,
0x800D, 0x0000, 0x9ECF, ], |
| 62 [ 'crc-16-dect', 'Crc16Dect', 0x10589, NON_REVERSE,
0x0001, 0x0001, 0x007E, ], |
| 63 [ 'crc-16-dnp', 'Crc16Dnp', 0x13D65, REVERSE,
0xFFFF, 0xFFFF, 0xEA82, ], |
| 64 [ 'crc-16-en-13757', 'Crc16En13757', 0x13D65, NON_REVERSE,
0xFFFF, 0xFFFF, 0xC2B7, ], |
| 65 [ 'crc-16-genibus', 'Crc16Genibus', 0x11021, NON_REVERSE,
0x0000, 0xFFFF, 0xD64E, ], |
| 66 [ 'crc-16-maxim', 'Crc16Maxim', 0x18005, REVERSE,
0xFFFF, 0xFFFF, 0x44C2, ], |
| 67 [ 'crc-16-mcrf4xx', 'Crc16Mcrf4xx', 0x11021, REVERSE,
0xFFFF, 0x0000, 0x6F91, ], |
| 68 [ 'crc-16-riello', 'Crc16Riello', 0x11021, REVERSE,
0x554D, 0x0000, 0x63D0, ], |
| 69 [ 'crc-16-t10-dif', 'Crc16T10Dif', 0x18BB7, NON_REVERSE,
0x0000, 0x0000, 0xD0DB, ], |
| 70 [ 'crc-16-teledisk', 'Crc16Teledisk', 0x1A097, NON_REVERSE,
0x0000, 0x0000, 0x0FB3, ], |
| 71 [ 'crc-16-usb', 'Crc16Usb', 0x18005, REVERSE,
0x0000, 0xFFFF, 0xB4C8, ], |
| 72 [ 'x-25', 'CrcX25', 0x11021, REVERSE,
0x0000, 0xFFFF, 0x906E, ], |
| 73 [ 'xmodem', 'CrcXmodem', 0x11021, NON_REVERSE,
0x0000, 0x0000, 0x31C3, ], |
| 74 [ 'modbus', 'CrcModbus', 0x18005, REVERSE,
0xFFFF, 0x0000, 0x4B37, ], |
| 75 |
| 76 # Note definitions of CCITT are disputable. See: |
| 77 # http://homepages.tesco.net/~rainstorm/crc-catalogue.htm |
| 78 # http://web.archive.org/web/20071229021252/http://www.joegeluso.com/soft
ware/articles/ccitt.htm |
| 79 [ 'kermit', 'CrcKermit', 0x11021, REVERSE,
0x0000, 0x0000, 0x2189, ], |
| 80 [ 'crc-ccitt-false', 'CrcCcittFalse', 0x11021, NON_REVERSE,
0xFFFF, 0x0000, 0x29B1, ], |
| 81 [ 'crc-aug-ccitt', 'CrcAugCcitt', 0x11021, NON_REVERSE,
0x1D0F, 0x0000, 0xE5CC, ], |
| 82 |
| 83 [ 'crc-24', 'Crc24', 0x1864CFB, NON_REVERSE,
0xB704CE, 0x000000, 0x21CF02, ], |
| 84 [ 'crc-24-flexray-a', 'Crc24FlexrayA', 0x15D6DCB, NON_REVERSE,
0xFEDCBA, 0x000000, 0x7979BD, ], |
| 85 [ 'crc-24-flexray-b', 'Crc24FlexrayB', 0x15D6DCB, NON_REVERSE,
0xABCDEF, 0x000000, 0x1F23B8, ], |
| 86 |
| 87 [ 'crc-32', 'Crc32', 0x104C11DB7, REVERSE,
0x00000000, 0xFFFFFFFF, 0xCBF43926, ], |
| 88 [ 'crc-32-bzip2', 'Crc32Bzip2', 0x104C11DB7, NON_REVERSE,
0x00000000, 0xFFFFFFFF, 0xFC891918, ], |
| 89 [ 'crc-32c', 'Crc32C', 0x11EDC6F41, REVERSE,
0x00000000, 0xFFFFFFFF, 0xE3069283, ], |
| 90 [ 'crc-32d', 'Crc32D', 0x1A833982B, REVERSE,
0x00000000, 0xFFFFFFFF, 0x87315576, ], |
| 91 [ 'crc-32-mpeg', 'Crc32Mpeg', 0x104C11DB7, NON_REVERSE,
0xFFFFFFFF, 0x00000000, 0x0376E6E7, ], |
| 92 [ 'posix', 'CrcPosix', 0x104C11DB7, NON_REVERSE,
0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ], |
| 93 [ 'crc-32q', 'Crc32Q', 0x1814141AB, NON_REVERSE,
0x00000000, 0x00000000, 0x3010BF7F, ], |
| 94 [ 'jamcrc', 'CrcJamCrc', 0x104C11DB7, REVERSE,
0xFFFFFFFF, 0x00000000, 0x340BC6D9, ], |
| 95 [ 'xfer', 'CrcXfer', 0x1000000AF, NON_REVERSE,
0x00000000, 0x00000000, 0xBD0BE338, ], |
| 96 |
| 97 # 64-bit |
| 98 # Name Identifier-name, Poly Reverse
Init-value XOR-out Check |
| 99 [ 'crc-64', 'Crc64', 0x1000000000000001B, REVERSE,
0x0000000000000000, 0x0000000000000000, 0x46A5A9388A5BEFFE, ], |
| 100 [ 'crc-64-we', 'Crc64We', 0x142F0E1EBA9EA3693, NON_REVE
RSE, 0x0000000000000000, 0xFFFFFFFFFFFFFFFF, 0x62EC59E3F1A4F00A, ], |
| 101 [ 'crc-64-jones', 'Crc64Jones', 0x1AD93D23594C935A9, REVERSE,
0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0xCAA717168609F281, ], |
| 102 ] |
| 103 |
| 104 |
| 105 def _simplify_name(name): |
| 106 """ |
| 107 Reduce CRC definition name to a simplified form: |
| 108 * lowercase |
| 109 * dashes removed |
| 110 * spaces removed |
| 111 * any initial "CRC" string removed |
| 112 """ |
| 113 name = name.lower() |
| 114 name = name.replace('-', '') |
| 115 name = name.replace(' ', '') |
| 116 if name.startswith('crc'): |
| 117 name = name[len('crc'):] |
| 118 return name |
| 119 |
| 120 |
| 121 _crc_definitions_by_name = {} |
| 122 _crc_definitions_by_identifier = {} |
| 123 _crc_definitions = [] |
| 124 |
| 125 _crc_table_headings = [ 'name', 'identifier', 'poly', 'reverse', 'init', 'xor_ou
t', 'check' ] |
| 126 |
| 127 for table_entry in _crc_definitions_table: |
| 128 crc_definition = dict(zip(_crc_table_headings, table_entry)) |
| 129 _crc_definitions.append(crc_definition) |
| 130 name = _simplify_name(table_entry[0]) |
| 131 if name in _crc_definitions_by_name: |
| 132 raise Exception("Duplicate entry for '%s' in CRC table" % name) |
| 133 _crc_definitions_by_name[name] = crc_definition |
| 134 _crc_definitions_by_identifier[table_entry[1]] = crc_definition |
| 135 |
| 136 |
| 137 def _get_definition_by_name(crc_name): |
| 138 definition = _crc_definitions_by_name.get(_simplify_name(crc_name), None) |
| 139 if not definition: |
| 140 definition = _crc_definitions_by_identifier.get(crc_name, None) |
| 141 if not definition: |
| 142 raise KeyError("Unkown CRC name '%s'" % crc_name) |
| 143 return definition |
| 144 |
| 145 |
| 146 class PredefinedCrc(crcmod.Crc): |
| 147 def __init__(self, crc_name): |
| 148 definition = _get_definition_by_name(crc_name) |
| 149 crcmod.Crc.__init__(self, poly=definition['poly'], initCrc=definition['i
nit'], rev=definition['reverse'], xorOut=definition['xor_out']) |
| 150 |
| 151 |
| 152 # crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc |
| 153 Crc = PredefinedCrc |
| 154 |
| 155 |
| 156 def mkPredefinedCrcFun(crc_name): |
| 157 definition = _get_definition_by_name(crc_name) |
| 158 return crcmod.mkCrcFun(poly=definition['poly'], initCrc=definition['init'],
rev=definition['reverse'], xorOut=definition['xor_out']) |
| 159 |
| 160 |
| 161 # crcmod.predefined.mkCrcFun is an alias for crcmod.predefined.mkPredefinedCrcFu
n |
| 162 mkCrcFun = mkPredefinedCrcFun |
OLD | NEW |