annotate cutils/crcmod/python3/crcmod.py @ 186:5c5c0c5a7402

Sync with latest crcmod2
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 14 Jan 2025 13:31:39 +0100
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 # Copyright (c) 2010 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
3 # Copyright (c) 2010 Craig McQueen
186
5c5c0c5a7402 Sync with latest crcmod2
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
4 # Copyright (c) 2025 Franz Glasner
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
5 #
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 # 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
7 # 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
8 # 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
9 # 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
10 # 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
11 # 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
12 #
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 # 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
14 # 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
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 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
17 # 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
18 # 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
19 # 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
20 # 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
21 # 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
22 # 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
23 #-----------------------------------------------------------------------------
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 '''crcmod is a Python module for gererating objects that compute the Cyclic
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 Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
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 The following are the public components of this module.
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
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 -- a class that creates instances providing the same interface as 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
30 algorithms in the hashlib module in the Python standard library. These
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 instances also provide a method for generating a C/C++ function to compute
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 the 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 mkCrcFun -- create a Python function to compute the CRC using the specified
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 polynomial and initial value. This provides a much simpler interface if
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 all you need is a function for CRC calculation.
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 '''
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
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 __all__ = '''mkCrcFun Crc'''.split()
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
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 # Select the appropriate set of low-level CRC functions for this installation.
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 # If the extension module was not built, drop back to the Python implementation
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 # even though it is significantly slower.
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 try:
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 from . import _crcfunext as _crcfun
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 _usingExtension = True
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 except ImportError:
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 from . import _crcfunpy as _crcfun
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 _usingExtension = False
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
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 import sys, struct
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
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 #-----------------------------------------------------------------------------
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 class 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
55 '''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
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
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 Instances of this class have the same interface as the algorithms 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
58 hashlib module in the Python standard library. See the documentation of
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 this module for examples of how to use a Crc instance.
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
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 The string representation of a Crc instance identifies the polynomial,
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 initial value, XOR out value, and the current CRC value. The print
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 statement can be used to output this information.
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
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 If you need to generate a C/C++ function for use in another application,
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 use the generateCode method. If you need to generate code for another
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 language, subclass Crc and override the generateCode method.
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
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 The following are the parameters supplied to the constructor.
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
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 poly -- The generator polynomial to use in calculating the CRC. The value
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 is specified as a Python integer. The bits in this integer are 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
73 coefficients of the polynomial. The only polynomials allowed are those
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 that generate 8, 16, 24, 32, or 64 bit CRCs.
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 initCrc -- Initial value used to start the CRC calculation. This initial
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 value should be the initial shift register value XORed with the final XOR
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 value. That is equivalent to the CRC result the algorithm should return for
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 a zero-length string. Defaults to all bits set because that starting value
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 will take leading zero bytes into account. Starting with zero will ignore
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 all leading zero bytes.
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
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 rev -- A flag that selects a bit reversed algorithm when True. Defaults to
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 True because the bit reversed algorithms are more efficient.
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
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 xorOut -- Final value to XOR with the calculated CRC value. Used by some
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 CRC algorithms. Defaults to zero.
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
88 '''
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
89 def __init__(self, poly, initCrc=~0, rev=True, xorOut=0, initialize=True):
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
90 if not initialize:
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
91 # Don't want to perform the initialization when using new or 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
92 # to create a new instance.
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
93 return
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
94
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
95 (sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
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
96 self.digest_size = sizeBits//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
97 self.initCrc = initCrc
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
98 self.xorOut = xorOut
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
99
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
100 self.poly = poly
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
101 self.reverse = rev
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
102
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
103 (crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
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
104 self._crc = crcfun
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
105 self.table = 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
106
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
107 self.crcValue = self.initCrc
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
108
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
109 def __str__(self):
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
110 lst = []
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
111 lst.append('poly = 0x%X' % self.poly)
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
112 lst.append('reverse = %s' % self.reverse)
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
113 fmt = '0x%%0%dX' % (self.digest_size*2)
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
114 lst.append('initCrc = %s' % (fmt % self.initCrc))
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
115 lst.append('xorOut = %s' % (fmt % self.xorOut))
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
116 lst.append('crcValue = %s' % (fmt % self.crcValue))
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
117 return '\n'.join(lst)
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
118
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
119 def new(self, arg=None):
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
120 '''Create a new instance of the Crc class initialized to the same
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
121 values as the original instance. The current CRC is set to the initial
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
122 value. If a string is provided in the optional arg parameter, it 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
123 passed to the update method.
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
124 '''
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
125 n = Crc(poly=None, initialize=False)
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
126 n._crc = self._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
127 n.digest_size = self.digest_size
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
128 n.initCrc = self.initCrc
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
129 n.xorOut = self.xorOut
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
130 n.table = self.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
131 n.crcValue = self.initCrc
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
132 n.reverse = self.reverse
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
133 n.poly = self.poly
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
134 if arg is not None:
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
135 n.update(arg)
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
136 return n
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
137
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
138 def copy(self):
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
139 '''Create a new instance of the Crc class initialized to the same
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
140 values as the original instance. The current CRC is set to the current
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
141 value. This allows multiple CRC calculations using a common initial
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
142 string.
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
143 '''
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
144 c = self.new()
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
145 c.crcValue = self.crcValue
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
146 return c
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
147
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
148 def update(self, 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
149 '''Update the current CRC value using the string specified as the 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
150 parameter.
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
151 '''
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
152 self.crcValue = self._crc(data, self.crcValue)
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
153
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
154 def digest(self):
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
155 '''Return the current CRC value as a string of bytes. The length of
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
156 this string is specified in the digest_size attribute.
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
157 '''
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
158 n = self.digest_size
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
159 crc = self.crcValue
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
160 lst = []
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
161 while n > 0:
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
162 lst.append(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
163 crc = 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
164 n -= 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
165 lst.reverse()
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
166 return bytes(lst)
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
167
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
168 def hexdigest(self):
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
169 '''Return the current CRC value as a string of hex digits. The length
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
170 of this string is twice the digest_size attribute.
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
171 '''
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
172 n = self.digest_size
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
173 crc = self.crcValue
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
174 lst = []
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
175 while n > 0:
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
176 lst.append('%02X' % (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
177 crc = 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
178 n -= 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
179 lst.reverse()
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
180 return ''.join(lst)
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
181
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
182 def generateCode(self, functionName, out, dataType=None, crcType=None):
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
183 '''Generate a C/C++ function.
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
184
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
185 functionName -- String specifying the name of the function.
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
186
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
187 out -- An open file-like object with a write method. This specifies
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
188 where the generated code is written.
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
189
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
190 dataType -- An optional parameter specifying the data type of the input
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
191 data to the function. Defaults to UINT8.
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
192
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
193 crcType -- An optional parameter specifying the data type of the 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
194 value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
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
195 on the size of the CRC value.
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
196 '''
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
197 if dataType is None:
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
198 dataType = 'UINT8'
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
199
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
200 if crcType is None:
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
201 size = 8*self.digest_size
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
202 if size == 24:
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
203 size = 32
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
204 crcType = 'UINT%d' % size
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
205
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
206 if self.digest_size == 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
207 # Both 8-bit CRC algorithms are the same
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
208 crcAlgor = 'table[*data ^ (%s)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
209 elif self.reverse:
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
210 # The bit reverse algorithms are all the same except for the 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
211 # type of the crc variable which is specified elsewhere.
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
212 crcAlgor = 'table[*data ^ (%s)crc] ^ (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
213 else:
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
214 # The forward CRC algorithms larger than 8 bits have an extra shift
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
215 # operation to get the high byte.
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
216 shift = 8*(self.digest_size - 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
217 crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
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
218
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
219 fmt = '0x%%0%dX' % (2*self.digest_size)
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
220 if self.digest_size <= 4:
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
221 fmt = fmt + 'U,'
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
222 else:
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
223 # Need the long long type identifier to keep gcc from complaining.
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
224 fmt = fmt + 'ULL,'
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
225
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
226 # Select the number of entries per row in the output code.
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
227 n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
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
228
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
229 lst = []
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
230 for i, val in enumerate(self.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
231 if (i % n) == 0:
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
232 lst.append('\n ')
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
233 lst.append(fmt % val)
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
234
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
235 poly = 'polynomial: 0x%X' % self.poly
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
236 if self.reverse:
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
237 poly = poly + ', bit reverse algorithm'
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
238
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
239 if self.xorOut:
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
240 # Need to remove the comma from the format.
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
241 preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
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
242 postCondition = preCondition
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
243 else:
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
244 preCondition = ''
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
245 postCondition = ''
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
246
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
247 if self.digest_size == 3:
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
248 # The 24-bit CRC needs to be conditioned so that only 24-bits are
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
249 # used from the 32-bit variable.
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
250 if self.reverse:
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
251 preCondition += '\n crc = crc & 0xFFFFFFU;'
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
252 else:
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
253 postCondition += '\n crc = crc & 0xFFFFFFU;'
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
254
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
255
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
256 parms = {
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
257 'dataType' : dataType,
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
258 'crcType' : crcType,
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
259 'name' : functionName,
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
260 'crcAlgor' : crcAlgor % dataType,
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
261 'crcTable' : ''.join(lst),
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
262 'poly' : poly,
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
263 'preCondition' : preCondition,
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
264 'postCondition' : postCondition,
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
265 }
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
266 out.write(_codeTemplate % parms)
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
267
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
268 #-----------------------------------------------------------------------------
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
269 def mkCrcFun(poly, initCrc=~0, rev=True, xorOut=0):
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
270 '''Return a function that computes the CRC using the specified polynomial.
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
271
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
272 poly -- integer representation of the generator polynomial
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
273 initCrc -- default initial CRC value
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
274 rev -- when true, indicates that the data is processed bit reversed.
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
275 xorOut -- the final XOR value
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
276
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
277 The returned function has the following user interface
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
278 def crcfun(data, crc=initCrc):
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
279 '''
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
280
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
281 # First we must verify the params
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
282 (sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
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
283 # Make the function (and table), return the function
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
284 return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
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
285
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
286 #-----------------------------------------------------------------------------
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
287 # Naming convention:
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
288 # All function names ending with r are bit reverse variants of the ones
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
289 # without the r.
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
290
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
291 #-----------------------------------------------------------------------------
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
292 # Check the polynomial to make sure that it is acceptable and return the number
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
293 # of bits in the 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
294
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
295 def _verifyPoly(poly):
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
296 msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
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
297 for n in (8,16,24,32,64):
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
298 low = 1<<n
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
299 high = low*2
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
300 if low <= poly < high:
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
301 return n
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
302 raise ValueError(msg)
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
303
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
304 #-----------------------------------------------------------------------------
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
305 # Bit reverse the input value.
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
306
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
307 def _bitrev(x, n):
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
308 y = 0
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
309 for i in range(n):
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
310 y = (y << 1) | (x & 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
311 x = x >> 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
312 return y
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
313
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
314 #-----------------------------------------------------------------------------
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
315 # The following functions compute the CRC for a single byte. These are used
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
316 # to build up the tables needed in the CRC algorithm. Assumes the high order
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
317 # bit of the polynomial has been stripped off.
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
318
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
319 def _bytecrc(crc, poly, n):
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
320 mask = 1<<(n-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
321 for i in range(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
322 if crc & mask:
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
323 crc = (crc << 1) ^ poly
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
324 else:
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
325 crc = crc << 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
326 mask = (1<<n) - 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
327 crc = crc & mask
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
328 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
329
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
330 def _bytecrc_r(crc, poly, n):
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
331 for i in range(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
332 if crc & 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
333 crc = (crc >> 1) ^ poly
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
334 else:
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
335 crc = crc >> 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
336 mask = (1<<n) - 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
337 crc = crc & mask
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
338 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
339
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
340 #-----------------------------------------------------------------------------
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
341 # The following functions compute the table needed to compute the CRC. 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
342 # table is returned as a list. Note that the array module does not support
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
343 # 64-bit integers on a 32-bit architecture as of Python 2.3.
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
344 #
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
345 # These routines assume that the polynomial and the number of bits in the 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
346 # have been checked for validity by the caller.
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
347
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
348 def _mkTable(poly, n):
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
349 mask = (1<<n) - 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
350 poly = poly & mask
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
351 table = [_bytecrc(i<<(n-8),poly,n) for i in range(256)]
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
352 return 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
353
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
354 def _mkTable_r(poly, n):
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
355 mask = (1<<n) - 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
356 poly = _bitrev(poly & mask, n)
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
357 table = [_bytecrc_r(i,poly,n) for i in range(256)]
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
358 return 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
359
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
360 #-----------------------------------------------------------------------------
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
361 # Map the CRC size onto the functions that handle these sizes.
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
362
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
363 _sizeMap = {
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
364 8 : [_crcfun._crc8, _crcfun._crc8r],
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
365 16 : [_crcfun._crc16, _crcfun._crc16r],
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
366 24 : [_crcfun._crc24, _crcfun._crc24r],
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
367 32 : [_crcfun._crc32, _crcfun._crc32r],
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
368 64 : [_crcfun._crc64, _crcfun._crc64r],
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
369 }
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
370
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
371 #-----------------------------------------------------------------------------
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
372 # Build a mapping of size to struct module type code. This table 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
373 # constructed dynamically so that it has the best chance of picking the best
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
374 # code to use for the platform we are running on. This should properly adapt
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
375 # to 32 and 64 bit machines.
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
376
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
377 _sizeToTypeCode = {}
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
378
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
379 for typeCode in 'B H I L Q'.split():
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
380 size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
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
381 if size is not None and size not in _sizeToTypeCode:
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
382 _sizeToTypeCode[size] = '256%s' % typeCode
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
383
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
384 _sizeToTypeCode[24] = _sizeToTypeCode[32]
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
385
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
386 del typeCode, size
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
387
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
388 #-----------------------------------------------------------------------------
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
389 # The following function validates the parameters of the CRC, namely,
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
390 # poly, and initial/final XOR values.
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
391 # It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
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
392
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
393 def _verifyParams(poly, initCrc, xorOut):
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
394 sizeBits = _verifyPoly(poly)
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
395
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
396 mask = (1<<sizeBits) - 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
397
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
398 # Adjust the initial CRC to the correct data type (unsigned value).
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
399 initCrc = initCrc & mask
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
400
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
401 # Similar for XOR-out value.
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
402 xorOut = xorOut & mask
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
403
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
404 return (sizeBits, initCrc, xorOut)
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
405
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
406 #-----------------------------------------------------------------------------
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
407 # The following function returns a Python function to compute the 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
408 #
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
409 # It must be passed parameters that are already verified & sanitized by
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
410 # _verifyParams().
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
411 #
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
412 # The returned function calls a low level function that is written in C if 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
413 # extension module could be loaded. Otherwise, a Python implementation 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
414 # used.
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
415 #
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
416 # In addition to this function, a list containing the CRC table is returned.
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
417
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
418 def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
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
419 if rev:
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
420 tableList = _mkTable_r(poly, sizeBits)
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
421 _fun = _sizeMap[sizeBits][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
422 else:
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
423 tableList = _mkTable(poly, sizeBits)
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
424 _fun = _sizeMap[sizeBits][0]
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
425
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
426 _table = tableList
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
427 if _usingExtension:
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
428 _table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
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
429
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
430 if xorOut == 0:
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
431 def crcfun(data, crc=initCrc, table=_table, fun=_fun):
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
432 return fun(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
433 else:
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
434 def crcfun(data, crc=initCrc, table=_table, fun=_fun):
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
435 return xorOut ^ fun(data, xorOut ^ 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
436
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
437 return crcfun, tableList
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
438
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
439 #-----------------------------------------------------------------------------
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
440 _codeTemplate = '''// Automatically generated CRC function
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
441 // %(poly)s
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
442 %(crcType)s
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
443 %(name)s(%(dataType)s *data, int len, %(crcType)s 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
444 {
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
445 static const %(crcType)s table[256] = {%(crcTable)s
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
446 };
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
447 %(preCondition)s
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
448 while (len > 0)
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
449 {
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
450 crc = %(crcAlgor)s;
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
451 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
452 len--;
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
453 }%(postCondition)s
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
454 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
455 }
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
456 '''
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
457