annotate cutils/crcmod/python2/_crcfunpy.py @ 368:7761a15b9736

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