comparison cutils/crcmod/python2/_crcfunpy.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
comparison
equal deleted inserted replaced
179:53614a724bf0 180:d038f0a9ba49
1 #-----------------------------------------------------------------------------
2 # Low level CRC functions for use by crcmod. This version is implemented in
3 # Python for a couple of reasons. 1) Provide a reference implememtation.
4 # 2) Provide a version that can be used on systems where a C compiler is not
5 # available for building extension modules.
6 #
7 # Copyright (c) 2004 Raymond L. Buvel
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # SOFTWARE.
26 #-----------------------------------------------------------------------------
27
28 def _crc8(data, crc, table):
29 crc = crc & 0xFF
30 for x in data:
31 crc = table[ord(x) ^ crc]
32 return crc
33
34 def _crc8r(data, crc, table):
35 crc = crc & 0xFF
36 for x in data:
37 crc = table[ord(x) ^ crc]
38 return crc
39
40 def _crc16(data, crc, table):
41 crc = crc & 0xFFFF
42 for x in data:
43 crc = table[ord(x) ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
44 return crc
45
46 def _crc16r(data, crc, table):
47 crc = crc & 0xFFFF
48 for x in data:
49 crc = table[ord(x) ^ (crc & 0xFF)] ^ (crc >> 8)
50 return crc
51
52 def _crc24(data, crc, table):
53 crc = crc & 0xFFFFFF
54 for x in data:
55 crc = table[ord(x) ^ (int(crc>>16) & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
56 return crc
57
58 def _crc24r(data, crc, table):
59 crc = crc & 0xFFFFFF
60 for x in data:
61 crc = table[ord(x) ^ int(crc & 0xFF)] ^ (crc >> 8)
62 return crc
63
64 def _crc32(data, crc, table):
65 crc = crc & 0xFFFFFFFFL
66 for x in data:
67 crc = table[ord(x) ^ (int(crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00L)
68 return crc
69
70 def _crc32r(data, crc, table):
71 crc = crc & 0xFFFFFFFFL
72 for x in data:
73 crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
74 return crc
75
76 def _crc64(data, crc, table):
77 crc = crc & 0xFFFFFFFFFFFFFFFFL
78 for x in data:
79 crc = table[ord(x) ^ (int(crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00L)
80 return crc
81
82 def _crc64r(data, crc, table):
83 crc = crc & 0xFFFFFFFFFFFFFFFFL
84 for x in data:
85 crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
86 return crc
87