comparison cutils/crcmod/python3/_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) 2009 Raymond L. Buvel
8 # Copyright (c) 2010 Craig McQueen
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27 #-----------------------------------------------------------------------------
28
29 def _get_buffer_view(in_obj):
30 if isinstance(in_obj, str):
31 raise TypeError('Unicode-objects must be encoded before calculating a CRC')
32 mv = memoryview(in_obj)
33 if mv.ndim > 1:
34 raise BufferError('Buffer must be single dimension')
35 return mv
36
37
38 def _crc8(data, crc, table):
39 mv = _get_buffer_view(data)
40 crc = crc & 0xFF
41 for x in mv.tobytes():
42 crc = table[x ^ crc]
43 return crc
44
45 def _crc8r(data, crc, table):
46 mv = _get_buffer_view(data)
47 crc = crc & 0xFF
48 for x in mv.tobytes():
49 crc = table[x ^ crc]
50 return crc
51
52 def _crc16(data, crc, table):
53 mv = _get_buffer_view(data)
54 crc = crc & 0xFFFF
55 for x in mv.tobytes():
56 crc = table[x ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
57 return crc
58
59 def _crc16r(data, crc, table):
60 mv = _get_buffer_view(data)
61 crc = crc & 0xFFFF
62 for x in mv.tobytes():
63 crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
64 return crc
65
66 def _crc24(data, crc, table):
67 mv = _get_buffer_view(data)
68 crc = crc & 0xFFFFFF
69 for x in mv.tobytes():
70 crc = table[x ^ (crc>>16 & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
71 return crc
72
73 def _crc24r(data, crc, table):
74 mv = _get_buffer_view(data)
75 crc = crc & 0xFFFFFF
76 for x in mv.tobytes():
77 crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
78 return crc
79
80 def _crc32(data, crc, table):
81 mv = _get_buffer_view(data)
82 crc = crc & 0xFFFFFFFF
83 for x in mv.tobytes():
84 crc = table[x ^ ((crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00)
85 return crc
86
87 def _crc32r(data, crc, table):
88 mv = _get_buffer_view(data)
89 crc = crc & 0xFFFFFFFF
90 for x in mv.tobytes():
91 crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
92 return crc
93
94 def _crc64(data, crc, table):
95 mv = _get_buffer_view(data)
96 crc = crc & 0xFFFFFFFFFFFFFFFF
97 for x in mv.tobytes():
98 crc = table[x ^ ((crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00)
99 return crc
100
101 def _crc64r(data, crc, table):
102 mv = _get_buffer_view(data)
103 crc = crc & 0xFFFFFFFFFFFFFFFF
104 for x in mv.tobytes():
105 crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
106 return crc
107