Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/crcmod/python2/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 |
| 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 md5 and sha modules in the Python standard library. These instances also |
|
d038f0a9ba49
Vendored 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 provide a method for generating a C/C++ 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
|
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
|
33 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
|
34 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
|
35 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
|
36 ''' |
|
d038f0a9ba49
Vendored 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 from __future__ import absolute_import |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
39 |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
40 |
|
d038f0a9ba49
Vendored 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 __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
|
42 |
|
d038f0a9ba49
Vendored 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 |
|
d038f0a9ba49
Vendored 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 # 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
|
45 # 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
|
46 # 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
|
47 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
|
48 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
|
49 _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
|
50 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
|
51 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
|
52 _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
|
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 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
|
55 |
|
d038f0a9ba49
Vendored 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 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
|
58 '''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
|
59 |
|
d038f0a9ba49
Vendored 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 Instances of this class have the same interface as the md5 and sha modules |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
61 in the Python standard library. See the documentation for these modules |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
62 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
|
63 |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 |
|
d038f0a9ba49
Vendored 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 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
|
69 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
|
70 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
|
71 |
|
d038f0a9ba49
Vendored 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 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
|
73 |
|
d038f0a9ba49
Vendored 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 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
|
75 is specified as a Python integer or long integer. The bits in this integer |
|
d038f0a9ba49
Vendored 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 are the coefficients of the polynomial. The only polynomials allowed 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
|
77 those 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
|
78 |
|
d038f0a9ba49
Vendored 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 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
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 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
|
87 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
|
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 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
|
90 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
|
91 ''' |
|
d038f0a9ba49
Vendored 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 def __init__(self, poly, initCrc=~0L, 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
|
93 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
|
94 # 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
|
95 # 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
|
96 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
|
97 |
|
d038f0a9ba49
Vendored 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 (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
|
99 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
|
100 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
|
101 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
|
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 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
|
104 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
|
105 |
|
d038f0a9ba49
Vendored 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 (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
|
107 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
|
108 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
|
109 |
|
d038f0a9ba49
Vendored 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 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
|
111 |
|
d038f0a9ba49
Vendored 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 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 |
|
d038f0a9ba49
Vendored 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 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
|
123 '''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
|
124 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
|
125 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
|
126 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
|
127 ''' |
|
d038f0a9ba49
Vendored 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 = 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 |
|
d038f0a9ba49
Vendored 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 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
|
142 '''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
|
143 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
|
144 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
|
145 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
|
146 ''' |
|
d038f0a9ba49
Vendored 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 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
|
148 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
|
149 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
|
150 |
|
d038f0a9ba49
Vendored 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 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
|
152 '''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
|
153 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
|
154 ''' |
|
d038f0a9ba49
Vendored 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 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
|
156 |
|
d038f0a9ba49
Vendored 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 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
|
158 '''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
|
159 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
|
160 ''' |
|
d038f0a9ba49
Vendored 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 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
|
162 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
|
163 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
|
164 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
|
165 lst.append(chr(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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 |
|
d038f0a9ba49
Vendored 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 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
|
172 '''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
|
173 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
|
174 ''' |
|
d038f0a9ba49
Vendored 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 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 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
|
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 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
|
186 '''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
|
187 |
|
d038f0a9ba49
Vendored 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 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
|
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 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
|
191 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
|
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 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
|
194 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
|
195 |
|
d038f0a9ba49
Vendored 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 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
|
197 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
|
198 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
|
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 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
|
201 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
|
202 |
|
d038f0a9ba49
Vendored 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 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 |
|
d038f0a9ba49
Vendored 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 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
|
210 # 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
|
211 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
|
212 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
|
213 # 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
|
214 # 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
|
215 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
|
216 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
|
217 # 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
|
218 # 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
|
219 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
|
220 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
|
221 |
|
d038f0a9ba49
Vendored 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 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
|
223 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
|
224 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
|
225 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
|
226 # 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
|
227 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
|
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 # 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
|
230 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
|
231 |
|
d038f0a9ba49
Vendored 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 = [] |
|
d038f0a9ba49
Vendored 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 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
|
234 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
|
235 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
|
236 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
|
237 |
|
d038f0a9ba49
Vendored 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 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
|
239 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
|
240 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
|
241 |
|
d038f0a9ba49
Vendored 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 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
|
243 # 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 |
|
d038f0a9ba49
Vendored 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.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
|
251 # 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
|
252 # 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 |
|
d038f0a9ba49
Vendored 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 |
|
d038f0a9ba49
Vendored 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 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
|
260 '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
|
261 '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
|
262 '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
|
263 '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
|
264 '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
|
265 '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
|
266 '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
|
267 '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
|
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 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
|
270 |
|
d038f0a9ba49
Vendored 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 def mkCrcFun(poly, initCrc=~0L, 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
|
273 '''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
|
274 |
|
d038f0a9ba49
Vendored 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 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
|
276 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
|
277 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
|
278 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
|
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 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
|
281 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
|
282 ''' |
|
d038f0a9ba49
Vendored 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 |
|
d038f0a9ba49
Vendored 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 # 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
|
285 (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
|
286 # 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
|
287 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
|
288 |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
291 # 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
|
292 # 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
|
293 |
|
d038f0a9ba49
Vendored 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 # 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
|
296 # 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
|
297 |
|
d038f0a9ba49
Vendored 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 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
|
299 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
|
300 poly = long(poly) # Use a common representation for all operations |
|
d038f0a9ba49
Vendored 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 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
|
302 low = 1L<<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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
310 |
|
d038f0a9ba49
Vendored 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 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
|
312 x = long(x) |
|
d038f0a9ba49
Vendored 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 y = 0L |
|
d038f0a9ba49
Vendored 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 for i in xrange(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
|
315 y = (y << 1) | (x & 1L) |
|
d038f0a9ba49
Vendored 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 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
|
317 if ((1L<<n)-1) <= sys.maxint: |
|
d038f0a9ba49
Vendored 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 return int(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
|
319 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
|
320 |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
323 # 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
|
324 # 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
|
325 |
|
d038f0a9ba49
Vendored 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 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
|
327 crc = long(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
|
328 poly = long(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
|
329 mask = 1L<<(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
|
330 for i in xrange(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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 mask = (1L<<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
|
336 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
|
337 if mask <= sys.maxint: |
|
d038f0a9ba49
Vendored 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 int(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 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
|
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 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
|
342 crc = long(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
|
343 poly = long(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
|
344 for i in xrange(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
|
345 if crc & 1L: |
|
d038f0a9ba49
Vendored 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 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
|
347 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
|
348 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
|
349 mask = (1L<<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 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
|
351 if mask <= sys.maxint: |
|
d038f0a9ba49
Vendored 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 int(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
|
353 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
|
354 |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
357 # 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
|
358 # 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
|
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 # 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
|
361 # 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
|
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 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
|
364 mask = (1L<<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
|
365 poly = long(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
|
366 table = [_bytecrc(long(i)<<(n-8),poly,n) for i in xrange(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
|
367 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
|
368 |
|
d038f0a9ba49
Vendored 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 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
|
370 mask = (1L<<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
|
371 poly = _bitrev(long(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
|
372 table = [_bytecrc_r(long(i),poly,n) for i in xrange(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
|
373 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
|
374 |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
377 |
|
d038f0a9ba49
Vendored 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 _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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 } |
|
d038f0a9ba49
Vendored 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 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored 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 # 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
|
388 # 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
|
389 # 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
|
390 # 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
|
391 |
|
d038f0a9ba49
Vendored 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 _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
|
393 |
|
d038f0a9ba49
Vendored 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 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
|
395 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
|
396 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
|
397 _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
|
398 |
|
d038f0a9ba49
Vendored 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 _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
|
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 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
|
402 |
|
d038f0a9ba49
Vendored 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 # 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
|
405 # 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
|
406 # 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
|
407 |
|
d038f0a9ba49
Vendored 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 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
|
409 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
|
410 |
|
d038f0a9ba49
Vendored 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 mask = (1L<<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
|
412 |
|
d038f0a9ba49
Vendored 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 # 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
|
414 initCrc = long(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
|
415 if mask <= sys.maxint: |
|
d038f0a9ba49
Vendored 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 initCrc = int(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
|
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 # 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
|
419 xorOut = long(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
|
420 if mask <= sys.maxint: |
|
d038f0a9ba49
Vendored 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 xorOut = int(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
|
422 |
|
d038f0a9ba49
Vendored 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 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
|
424 |
|
d038f0a9ba49
Vendored 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 # 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
|
427 # |
|
d038f0a9ba49
Vendored 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 # 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
|
429 # _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
|
430 # |
|
d038f0a9ba49
Vendored 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 # 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
|
432 # 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
|
433 # 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
|
434 # |
|
d038f0a9ba49
Vendored 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 # 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
|
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 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
|
438 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
|
439 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
|
440 _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
|
441 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
|
442 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
|
443 _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
|
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 _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
|
446 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
|
447 _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
|
448 |
|
d038f0a9ba49
Vendored 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 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
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 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
|
457 |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
458 #----------------------------------------------------------------------------- |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
459 _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
|
460 // %(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
|
461 %(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
|
462 %(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
|
463 { |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
464 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
|
465 }; |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
466 %(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
|
467 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
|
468 { |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
469 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
|
470 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
|
471 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
|
472 }%(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
|
473 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
|
474 } |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
475 ''' |
|
d038f0a9ba49
Vendored crcmod2 into sub-package "cutils.crcmod" as pure-Python implementation of additional "digests": CRC sums.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
476 |
