Checksum.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef CHECKSUM_H
  21. #define CHECKSUM_H
  22. #ifdef __cplusplus
  23. #include "../MMKVPredef.h"
  24. #if MMKV_EMBED_ZLIB
  25. # include "zlib/zconf.h"
  26. namespace zlib {
  27. uLong crc32(uLong crc, const Bytef *buf, z_size_t len);
  28. } // namespace zlib
  29. # define ZLIB_CRC32(crc, buf, len) zlib::crc32(crc, buf, len)
  30. #else // MMKV_EMBED_ZLIB
  31. # include <zlib.h>
  32. # define ZLIB_CRC32(crc, buf, len) ::crc32(crc, buf, static_cast<uInt>(len))
  33. #endif // MMKV_EMBED_ZLIB
  34. #if defined(__aarch64__) && defined(__linux__)
  35. # define MMKV_USE_ARMV8_CRC32
  36. namespace mmkv {
  37. uint32_t armv8_crc32(uint32_t crc, const uint8_t *buf, size_t len);
  38. }
  39. // have to check CPU's instruction set dynamically
  40. typedef uint32_t (*CRC32_Func_t)(uint32_t crc, const uint8_t *buf, size_t len);
  41. extern CRC32_Func_t CRC32;
  42. #else // defined(__aarch64__) && defined(__linux__)
  43. # define CRC32(crc, buf, len) ZLIB_CRC32(crc, buf, len)
  44. #endif // defined(__aarch64__) && defined(__linux__)
  45. #endif // __cplusplus
  46. #endif // CHECKSUM_H