annotate cutils/util/crc32.py @ 258:57057028f13e

FIX: description for overflow error
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 16 Feb 2025 09:46:21 +0100
parents d852559df523
children b24080e5ca96
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 # :-
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
3 # :Copyright: (c) 2025 Franz Glasner
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
4 # :License: BSD-3-Clause
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):
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
57 return self._get_crc_as_uint32() == self.as_uint32(other)
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
58 else:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
59 return NotImplemented
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
60
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
61 def __ne__(self, other):
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
62 equal = self.__eq__(other)
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
63 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
64
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
65 def _get_crc_as_uint32(self):
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
66 """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
67 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
68
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
69 :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
70 versions
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
71 :rtype: int
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
72
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
73 """
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
74 if PY2:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
75 if self._crc < 0:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
76 # 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
77 return (~self._crc ^ 0xFFFFFFFF)
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
78 else:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
79 return self._crc
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
80 else:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
81 return self._crc
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
82
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
83 @staticmethod
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
84 def as_uint32(i):
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
85 if i < 0:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
86 if i < -2147483648:
258
57057028f13e FIX: description for overflow error
Franz Glasner <fzglas.hg@dom66.de>
parents: 255
diff changeset
87 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
88 return (~i ^ 0xFFFFFFFF)
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
89 else:
d852559df523 Wrap zlib.crc32 into a hashlib-compatible interface
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
90 return i