25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_UTIL_CRC32C_H_
  5. #define STORAGE_LEVELDB_UTIL_CRC32C_H_
  6. #include <cstddef>
  7. #include <cstdint>
  8. namespace leveldb {
  9. namespace crc32c {
  10. // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
  11. // crc32c of some string A. Extend() is often used to maintain the
  12. // crc32c of a stream of data.
  13. uint32_t Extend(uint32_t init_crc, const char* data, size_t n);
  14. // Return the crc32c of data[0,n-1]
  15. inline uint32_t Value(const char* data, size_t n) { return Extend(0, data, n); }
  16. static const uint32_t kMaskDelta = 0xa282ead8ul;
  17. // Return a masked representation of crc.
  18. //
  19. // Motivation: it is problematic to compute the CRC of a string that
  20. // contains embedded CRCs. Therefore we recommend that CRCs stored
  21. // somewhere (e.g., in files) should be masked before being stored.
  22. inline uint32_t Mask(uint32_t crc) {
  23. // Rotate right by 15 bits and add a constant.
  24. return ((crc >> 15) | (crc << 17)) + kMaskDelta;
  25. }
  26. // Return the crc whose masked representation is masked_crc.
  27. inline uint32_t Unmask(uint32_t masked_crc) {
  28. uint32_t rot = masked_crc - kMaskDelta;
  29. return ((rot >> 17) | (rot << 15));
  30. }
  31. } // namespace crc32c
  32. } // namespace leveldb
  33. #endif // STORAGE_LEVELDB_UTIL_CRC32C_H_