Brian Silverman | 84b2223 | 2019-01-25 20:29:29 -0800 | [diff] [blame^] | 1 | # pycrc -- parameterisable CRC calculation utility and C source code generator |
| 2 | # |
| 3 | # Copyright (c) 2006-2017 Thomas Pircher <tehpeh-web@tty1.net> |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to |
| 7 | # deal in the Software without restriction, including without limitation the |
| 8 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 | # sell copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | # IN THE SOFTWARE. |
| 22 | |
| 23 | |
| 24 | """ |
| 25 | Collection of CRC models. This module contains the CRC models known to pycrc. |
| 26 | |
| 27 | To print the parameters of a particular model: |
| 28 | |
| 29 | from pycrc.models import CrcModels |
| 30 | |
| 31 | models = CrcModels() |
| 32 | print(", ".join(models.names())) |
| 33 | m = models.get_params("crc-32") |
| 34 | if m != None: |
| 35 | print("Width: {width:d}".format(**m)) |
| 36 | print("Poly: {poly:#x}".format(**m)) |
| 37 | print("ReflectIn: {reflect_in}".format(**m)) |
| 38 | print("XorIn: {xor_in:#x}".format(**m)) |
| 39 | print("ReflectOut: {reflect_out}".format(**m)) |
| 40 | print("XorOut: {xor_out:#x}".format(**m)) |
| 41 | print("Check: {check:#x}".format(**m)) |
| 42 | else: |
| 43 | print("model not found.") |
| 44 | """ |
| 45 | |
| 46 | |
| 47 | |
| 48 | class CrcModels(object): |
| 49 | """ |
| 50 | CRC Models. |
| 51 | |
| 52 | All models are defined as constant class variables. |
| 53 | """ |
| 54 | |
| 55 | models = [] |
| 56 | |
| 57 | models.append({ |
| 58 | 'name': 'crc-5', |
| 59 | 'width': 5, |
| 60 | 'poly': 0x05, |
| 61 | 'reflect_in': True, |
| 62 | 'xor_in': 0x1f, |
| 63 | 'reflect_out': True, |
| 64 | 'xor_out': 0x1f, |
| 65 | 'check': 0x19, |
| 66 | }) |
| 67 | models.append({ |
| 68 | 'name': 'crc-8', |
| 69 | 'width': 8, |
| 70 | 'poly': 0x07, |
| 71 | 'reflect_in': False, |
| 72 | 'xor_in': 0x0, |
| 73 | 'reflect_out': False, |
| 74 | 'xor_out': 0x0, |
| 75 | 'check': 0xf4, |
| 76 | }) |
| 77 | models.append({ |
| 78 | 'name': 'dallas-1-wire', |
| 79 | 'width': 8, |
| 80 | 'poly': 0x31, |
| 81 | 'reflect_in': True, |
| 82 | 'xor_in': 0x0, |
| 83 | 'reflect_out': True, |
| 84 | 'xor_out': 0x0, |
| 85 | 'check': 0xa1, |
| 86 | }) |
| 87 | models.append({ |
| 88 | 'name': 'crc-12-3gpp', |
| 89 | 'width': 12, |
| 90 | 'poly': 0x80f, |
| 91 | 'reflect_in': False, |
| 92 | 'xor_in': 0x0, |
| 93 | 'reflect_out': True, |
| 94 | 'xor_out': 0x0, |
| 95 | 'check': 0xdaf, |
| 96 | }) |
| 97 | models.append({ |
| 98 | 'name': 'crc-15', |
| 99 | 'width': 15, |
| 100 | 'poly': 0x4599, |
| 101 | 'reflect_in': False, |
| 102 | 'xor_in': 0x0, |
| 103 | 'reflect_out': False, |
| 104 | 'xor_out': 0x0, |
| 105 | 'check': 0x59e, |
| 106 | }) |
| 107 | models.append({ |
| 108 | 'name': 'crc-16', |
| 109 | 'width': 16, |
| 110 | 'poly': 0x8005, |
| 111 | 'reflect_in': True, |
| 112 | 'xor_in': 0x0, |
| 113 | 'reflect_out': True, |
| 114 | 'xor_out': 0x0, |
| 115 | 'check': 0xbb3d, |
| 116 | }) |
| 117 | models.append({ |
| 118 | 'name': 'crc-16-usb', |
| 119 | 'width': 16, |
| 120 | 'poly': 0x8005, |
| 121 | 'reflect_in': True, |
| 122 | 'xor_in': 0xffff, |
| 123 | 'reflect_out': True, |
| 124 | 'xor_out': 0xffff, |
| 125 | 'check': 0xb4c8, |
| 126 | }) |
| 127 | models.append({ |
| 128 | 'name': 'crc-16-modbus', |
| 129 | 'width': 16, |
| 130 | 'poly': 0x8005, |
| 131 | 'reflect_in': True, |
| 132 | 'xor_in': 0xffff, |
| 133 | 'reflect_out': True, |
| 134 | 'xor_out': 0x0, |
| 135 | 'check': 0x4b37, |
| 136 | }) |
| 137 | models.append({ |
| 138 | 'name': 'crc-16-genibus', |
| 139 | 'width': 16, |
| 140 | 'poly': 0x1021, |
| 141 | 'reflect_in': False, |
| 142 | 'xor_in': 0xffff, |
| 143 | 'reflect_out': False, |
| 144 | 'xor_out': 0xffff, |
| 145 | 'check': 0xd64e, |
| 146 | }) |
| 147 | models.append({ |
| 148 | 'name': 'crc-16-ccitt', |
| 149 | 'width': 16, |
| 150 | 'poly': 0x1021, |
| 151 | 'reflect_in': False, |
| 152 | 'xor_in': 0x1d0f, |
| 153 | 'reflect_out': False, |
| 154 | 'xor_out': 0x0, |
| 155 | 'check': 0xe5cc, |
| 156 | }) |
| 157 | models.append({ |
| 158 | 'name': 'r-crc-16', |
| 159 | 'width': 16, |
| 160 | 'poly': 0x0589, |
| 161 | 'reflect_in': False, |
| 162 | 'xor_in': 0x0, |
| 163 | 'reflect_out': False, |
| 164 | 'xor_out': 0x0001, |
| 165 | 'check': 0x007e, |
| 166 | }) |
| 167 | models.append({ |
| 168 | 'name': 'kermit', |
| 169 | 'width': 16, |
| 170 | 'poly': 0x1021, |
| 171 | 'reflect_in': True, |
| 172 | 'xor_in': 0x0, |
| 173 | 'reflect_out': True, |
| 174 | 'xor_out': 0x0, |
| 175 | 'check': 0x2189, |
| 176 | }) |
| 177 | models.append({ |
| 178 | 'name': 'x-25', |
| 179 | 'width': 16, |
| 180 | 'poly': 0x1021, |
| 181 | 'reflect_in': True, |
| 182 | 'xor_in': 0xffff, |
| 183 | 'reflect_out': True, |
| 184 | 'xor_out': 0xffff, |
| 185 | 'check': 0x906e, |
| 186 | }) |
| 187 | models.append({ |
| 188 | 'name': 'xmodem', |
| 189 | 'width': 16, |
| 190 | 'poly': 0x1021, |
| 191 | 'reflect_in': False, |
| 192 | 'xor_in': 0x0, |
| 193 | 'reflect_out': False, |
| 194 | 'xor_out': 0x0, |
| 195 | 'check': 0x31c3, |
| 196 | }) |
| 197 | models.append({ |
| 198 | 'name': 'zmodem', |
| 199 | 'width': 16, |
| 200 | 'poly': 0x1021, |
| 201 | 'reflect_in': False, |
| 202 | 'xor_in': 0x0, |
| 203 | 'reflect_out': False, |
| 204 | 'xor_out': 0x0, |
| 205 | 'check': 0x31c3, |
| 206 | }) |
| 207 | models.append({ |
| 208 | 'name': 'crc-24', |
| 209 | 'width': 24, |
| 210 | 'poly': 0x864cfb, |
| 211 | 'reflect_in': False, |
| 212 | 'xor_in': 0xb704ce, |
| 213 | 'reflect_out': False, |
| 214 | 'xor_out': 0x0, |
| 215 | 'check': 0x21cf02, |
| 216 | }) |
| 217 | models.append({ |
| 218 | 'name': 'crc-32', |
| 219 | 'width': 32, |
| 220 | 'poly': 0x4c11db7, |
| 221 | 'reflect_in': True, |
| 222 | 'xor_in': 0xffffffff, |
| 223 | 'reflect_out': True, |
| 224 | 'xor_out': 0xffffffff, |
| 225 | 'check': 0xcbf43926, |
| 226 | }) |
| 227 | models.append({ |
| 228 | 'name': 'crc-32c', |
| 229 | 'width': 32, |
| 230 | 'poly': 0x1edc6f41, |
| 231 | 'reflect_in': True, |
| 232 | 'xor_in': 0xffffffff, |
| 233 | 'reflect_out': True, |
| 234 | 'xor_out': 0xffffffff, |
| 235 | 'check': 0xe3069283, |
| 236 | }) |
| 237 | models.append({ |
| 238 | 'name': 'crc-32-mpeg', |
| 239 | 'width': 32, |
| 240 | 'poly': 0x4c11db7, |
| 241 | 'reflect_in': False, |
| 242 | 'xor_in': 0xffffffff, |
| 243 | 'reflect_out': False, |
| 244 | 'xor_out': 0x0, |
| 245 | 'check': 0x0376e6e7, |
| 246 | }) |
| 247 | models.append({ |
| 248 | 'name': 'crc-32-bzip2', |
| 249 | 'width': 32, |
| 250 | 'poly': 0x04c11db7, |
| 251 | 'reflect_in': False, |
| 252 | 'xor_in': 0xffffffff, |
| 253 | 'reflect_out': False, |
| 254 | 'xor_out': 0xffffffff, |
| 255 | 'check': 0xfc891918, |
| 256 | }) |
| 257 | models.append({ |
| 258 | 'name': 'posix', |
| 259 | 'width': 32, |
| 260 | 'poly': 0x4c11db7, |
| 261 | 'reflect_in': False, |
| 262 | 'xor_in': 0x0, |
| 263 | 'reflect_out': False, |
| 264 | 'xor_out': 0xffffffff, |
| 265 | 'check': 0x765e7680, |
| 266 | }) |
| 267 | models.append({ |
| 268 | 'name': 'jam', |
| 269 | 'width': 32, |
| 270 | 'poly': 0x4c11db7, |
| 271 | 'reflect_in': True, |
| 272 | 'xor_in': 0xffffffff, |
| 273 | 'reflect_out': True, |
| 274 | 'xor_out': 0x0, |
| 275 | 'check': 0x340bc6d9, |
| 276 | }) |
| 277 | models.append({ |
| 278 | 'name': 'xfer', |
| 279 | 'width': 32, |
| 280 | 'poly': 0x000000af, |
| 281 | 'reflect_in': False, |
| 282 | 'xor_in': 0x0, |
| 283 | 'reflect_out': False, |
| 284 | 'xor_out': 0x0, |
| 285 | 'check': 0xbd0be338, |
| 286 | }) |
| 287 | models.append({ |
| 288 | 'name': 'crc-64', |
| 289 | 'width': 64, |
| 290 | 'poly': 0x000000000000001b, |
| 291 | 'reflect_in': True, |
| 292 | 'xor_in': 0x0, |
| 293 | 'reflect_out': True, |
| 294 | 'xor_out': 0x0, |
| 295 | 'check': 0x46a5a9388a5beffe, |
| 296 | }) |
| 297 | models.append({ |
| 298 | 'name': 'crc-64-jones', |
| 299 | 'width': 64, |
| 300 | 'poly': 0xad93d23594c935a9, |
| 301 | 'reflect_in': True, |
| 302 | 'xor_in': 0xffffffffffffffff, |
| 303 | 'reflect_out': True, |
| 304 | 'xor_out': 0x0, |
| 305 | 'check': 0xcaa717168609f281, |
| 306 | }) |
| 307 | models.append({ |
| 308 | 'name': 'crc-64-xz', |
| 309 | 'width': 64, |
| 310 | 'poly': 0x42f0e1eba9ea3693, |
| 311 | 'reflect_in': True, |
| 312 | 'xor_in': 0xffffffffffffffff, |
| 313 | 'reflect_out': True, |
| 314 | 'xor_out': 0xffffffffffffffff, |
| 315 | 'check': 0x995dc9bbdf1939fa, |
| 316 | }) |
| 317 | |
| 318 | |
| 319 | def names(self): |
| 320 | """ |
| 321 | This function returns the list of supported CRC models. |
| 322 | """ |
| 323 | return [model['name'] for model in self.models] |
| 324 | |
| 325 | |
| 326 | def get_params(self, model): |
| 327 | """ |
| 328 | This function returns the parameters of a given model. |
| 329 | """ |
| 330 | model = model.lower() |
| 331 | for i in self.models: |
| 332 | if i['name'] == model: |
| 333 | return i |
| 334 | return None |