crc.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2022 The Abseil Authors.
  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. #ifndef ABSL_CRC_INTERNAL_CRC_H_
  15. #define ABSL_CRC_INTERNAL_CRC_H_
  16. #include <cstdint>
  17. #include "absl/base/config.h"
  18. // This class implements CRCs (aka Rabin Fingerprints).
  19. // Treats the input as a polynomial with coefficients in Z(2),
  20. // and finds the remainder when divided by an primitive polynomial
  21. // of the appropriate length.
  22. // A polynomial is represented by the bit pattern formed by its coefficients,
  23. // but with the highest order bit not stored.
  24. // The highest degree coefficient is stored in the lowest numbered bit
  25. // in the lowest addressed byte. Thus, in what follows, the highest degree
  26. // coefficient that is stored is in the low order bit of "lo" or "*lo".
  27. // Hardware acceleration is used when available.
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace crc_internal {
  31. class CRC {
  32. public:
  33. virtual ~CRC();
  34. // If "*crc" is the CRC of bytestring A, place the CRC of
  35. // the bytestring formed from the concatenation of A and the "length"
  36. // bytes at "bytes" into "*crc".
  37. virtual void Extend(uint32_t* crc, const void* bytes,
  38. size_t length) const = 0;
  39. // Equivalent to Extend(crc, bytes, length) where "bytes"
  40. // points to an array of "length" zero bytes.
  41. virtual void ExtendByZeroes(uint32_t* crc, size_t length) const = 0;
  42. // Inverse operation of ExtendByZeroes. If `crc` is the CRC value of a string
  43. // ending in `length` zero bytes, this returns a CRC value of that string
  44. // with those zero bytes removed.
  45. virtual void UnextendByZeroes(uint32_t* crc, size_t length) const = 0;
  46. // Apply a non-linear transformation to "*crc" so that
  47. // it is safe to CRC the result with the same polynomial without
  48. // any reduction of error-detection ability in the outer CRC.
  49. // Unscramble() performs the inverse transformation.
  50. // It is strongly recommended that CRCs be scrambled before storage or
  51. // transmission, and unscrambled at the other end before further manipulation.
  52. virtual void Scramble(uint32_t* crc) const = 0;
  53. virtual void Unscramble(uint32_t* crc) const = 0;
  54. // Crc32c() returns the singleton implementation of CRC for the CRC32C
  55. // polynomial. Returns a handle that MUST NOT be destroyed with delete.
  56. static CRC* Crc32c();
  57. protected:
  58. CRC(); // Clients may not call constructor; use Crc32c() instead.
  59. private:
  60. CRC(const CRC&) = delete;
  61. CRC& operator=(const CRC&) = delete;
  62. };
  63. } // namespace crc_internal
  64. ABSL_NAMESPACE_END
  65. } // namespace absl
  66. #endif // ABSL_CRC_INTERNAL_CRC_H_