Mercurial > hgrepos > Python > apps > py-cutils
diff cutils/treesum.py @ 260:07a0bc723139
treesum: Implement the CRC-32 using the new util.crc32 module
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 16 Feb 2025 11:07:04 +0100 |
| parents | 61e5b1c2685c |
| children | a3e25957afb7 |
line wrap: on
line diff
--- a/cutils/treesum.py Sun Feb 16 09:46:41 2025 +0100 +++ b/cutils/treesum.py Sun Feb 16 11:07:04 2025 +0100 @@ -24,13 +24,13 @@ import stat import sys import time -import zlib from . import (__version__, __revision__) from . import util from .util import cm from .util import digest from .util import walk +from .util.crc32 import crc32 def main(argv=None): @@ -937,14 +937,14 @@ def write(self, what): self._fp.write(what) - self._crc32 = zlib.crc32(what, self._crc32) + self._crc32.update(what) def flush(self): self._fp.flush() def resetdigest(self): """Reset the current CRC digest""" - self._crc32 = zlib.crc32(b"") + self._crc32 = crc32() def hexcrcdigest(self): """ @@ -952,22 +952,7 @@ :rtype: str """ - return (hex(self.crcdigest())[2:]).upper() - - def crcdigest(self): - """ - - :rtype: int - - """ - if util.PY2: - if self._crc32 < 0: - # Return the bitpattern as unsigned 32-bit number - return (~self._crc32 ^ 0xFFFFFFFF) - else: - return self._crc32 - else: - return self._crc32 + return self._crc32.hexdigest() def normalized_compatible_mode_str(mode): @@ -1196,31 +1181,13 @@ return line def _reset_crc(self): - self._crc32 = zlib.crc32(b"") + self._crc32 = crc32() def _update_crc(self, data): - self._crc32 = zlib.crc32(data, self._crc32) + self._crc32.update(data) def _hex_crc(self): - return (hex(self._get_crc())[2:]).upper() - - def _get_crc(self): - """Get the current CRC always as positive number with the same bit# - pattern because Python2 yields negative numbers also. - - :return: The current CRC value as positive number on all Python - versions - :rtype: int - - """ - if util.PY2: - if self._crc32 < 0: - # Return the bitpattern as unsigned 32-bit number - return (~self._crc32 ^ 0xFFFFFFFF) - else: - return self._crc32 - else: - return self._crc32 + return self._crc32.hexdigest() def _get_digest_size(self, algo_name): if self._current_algo_name == algo_name:
