cext.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright 2020 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import struct
  15. # NOTE: ``__config__`` **must** be the first import because it (may)
  16. # modify the search path used to locate shared libraries.
  17. import google_crc32c.__config__ # type: ignore
  18. from google_crc32c._crc32c import extend # type: ignore
  19. from google_crc32c._crc32c import value # type: ignore
  20. from google_crc32c._checksum import CommonChecksum
  21. class Checksum(CommonChecksum):
  22. """Hashlib-alike helper for CRC32C operations.
  23. Args:
  24. initial_value (Optional[bytes]): the initial chunk of data from
  25. which the CRC32C checksum is computed. Defaults to b''.
  26. """
  27. __slots__ = ("_crc",)
  28. def __init__(self, initial_value=b""):
  29. self._crc = value(initial_value)
  30. def update(self, chunk):
  31. """Update the checksum with a new chunk of data.
  32. Args:
  33. chunk (Optional[bytes]): a chunk of data used to extend
  34. the CRC32C checksum.
  35. """
  36. self._crc = extend(self._crc, chunk)