Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/util/crc32.py @ 388:85da58b83475
>>>>> tag v0.9 for changeset a90c0b9a06d7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 18 May 2025 06:00:30 +0200 |
| parents | 48430941c18c |
| children |
| rev | line source |
|---|---|
|
255
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
2 # :- |
|
323
48430941c18c
Adopt copyright and license wordings from https://reuse.software/faq/.
Franz Glasner <fzglas.hg@dom66.de>
parents:
259
diff
changeset
|
3 # SPDX-FileCopyrightText: © 2025 Franz Glasner |
|
48430941c18c
Adopt copyright and license wordings from https://reuse.software/faq/.
Franz Glasner <fzglas.hg@dom66.de>
parents:
259
diff
changeset
|
4 # SPDX-License-Identifier: BSD-3-Clause |
|
255
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
5 # :- |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
6 r"""Wrap :func:`zlib.crc32` into a :mod:`hashlib` compatible interface. |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 """ |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
10 from __future__ import print_function, absolute_import |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
11 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
12 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
13 __all__ = ["crc32"] |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
14 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
15 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
16 import struct |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 import zlib |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
19 from . import PY2 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
20 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
21 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
22 try: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
23 long |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
24 except NameError: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
25 INT_TYPES = (int, ) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 else: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
27 INT_TYPES = (int, long) # noqa: F821 long is undefined |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
30 class crc32(object): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
31 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
32 __slots__ = ["_crc"] |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
33 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
34 zlib_crc32 = zlib.crc32 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
35 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
36 def __init__(self, data=b""): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
37 self._crc = self.zlib_crc32(data) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
38 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
39 def update(self, data): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
40 self._crc = self.zlib_crc32(data, self._crc) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
41 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
42 def digest(self): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
43 return struct.pack(">I", self._get_crc_as_uint32()) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
44 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
45 def hexdigest(self): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
46 return "{:08X}".format(self._get_crc_as_uint32()) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
47 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
48 def copy(self): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
49 copied_crc = crc32() |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
50 copied_crc._crc = self._crc |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
51 return copied_crc |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
52 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
53 def __eq__(self, other): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
54 if isinstance(other, crc32): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
55 return self._crc == other._crc |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
56 elif isinstance(other, INT_TYPES): |
|
259
b24080e5ca96
FIX: Handle OverflowErrlr when comparing CRCs with ints
Franz Glasner <fzglas.hg@dom66.de>
parents:
258
diff
changeset
|
57 try: |
|
b24080e5ca96
FIX: Handle OverflowErrlr when comparing CRCs with ints
Franz Glasner <fzglas.hg@dom66.de>
parents:
258
diff
changeset
|
58 return self._get_crc_as_uint32() == self.as_uint32(other) |
|
b24080e5ca96
FIX: Handle OverflowErrlr when comparing CRCs with ints
Franz Glasner <fzglas.hg@dom66.de>
parents:
258
diff
changeset
|
59 except OverflowError: |
|
b24080e5ca96
FIX: Handle OverflowErrlr when comparing CRCs with ints
Franz Glasner <fzglas.hg@dom66.de>
parents:
258
diff
changeset
|
60 return False |
|
255
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
61 else: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
62 return NotImplemented |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
63 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
64 def __ne__(self, other): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
65 equal = self.__eq__(other) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
66 return equal if equal is NotImplemented else not equal |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
67 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
68 def _get_crc_as_uint32(self): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
69 """Get the current CRC always as positive number with the same bit |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
70 pattern because Python2 yields negative numbers also. |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
71 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
72 :return: The current CRC value as positive number on all Python |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
73 versions |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
74 :rtype: int |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
75 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
76 """ |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
77 if PY2: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
78 if self._crc < 0: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
79 # Return the bitpattern as unsigned 32-bit number |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
80 return (~self._crc ^ 0xFFFFFFFF) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
81 else: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
82 return self._crc |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
83 else: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
84 return self._crc |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
85 |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
86 @staticmethod |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
87 def as_uint32(i): |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
88 if i < 0: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
89 if i < -2147483648: |
|
258
57057028f13e
FIX: description for overflow error
Franz Glasner <fzglas.hg@dom66.de>
parents:
255
diff
changeset
|
90 raise OverflowError("invalid number for a 32-bit CRC") |
|
255
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
91 return (~i ^ 0xFFFFFFFF) |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
92 else: |
|
d852559df523
Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
93 return i |
