Checksum.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <cstdint>
  25. #if MMKV_EMBED_ZLIB
  26. # include "zlib/zconf.h"
  27. namespace zlib {
  28. uLong crc32(uLong crc, const Bytef *buf, z_size_t len);
  29. } // namespace zlib
  30. # define ZLIB_CRC32(crc, buf, len) zlib::crc32(crc, buf, len)
  31. #else // MMKV_EMBED_ZLIB
  32. # include <zlib.h>
  33. // some old version of zlib doesn't define z_size_t
  34. # ifndef z_size_t
  35. typedef size_t z_size_t;
  36. # endif
  37. # define ZLIB_CRC32(crc, buf, len) ::crc32(crc, buf, static_cast<uInt>(len))
  38. #endif // MMKV_EMBED_ZLIB
  39. #if defined(__aarch64__) && defined(__linux__)
  40. # define MMKV_USE_ARMV8_CRC32
  41. namespace mmkv {
  42. uint32_t armv8_crc32(uint32_t crc, const uint8_t *buf, size_t len);
  43. }
  44. # ifdef MMKV_OHOS
  45. // getauxval(AT_HWCAP) in OHOS returns wrong value, we just assume all OHOS device have crc32 instr
  46. # define CRC32 mmkv::armv8_crc32
  47. # else
  48. // have to check CPU's instruction set dynamically
  49. typedef uint32_t (*CRC32_Func_t)(uint32_t crc, const uint8_t *buf, size_t len);
  50. extern CRC32_Func_t CRC32;
  51. # endif
  52. #else // defined(__aarch64__) && defined(__linux__)
  53. # define CRC32(crc, buf, len) ZLIB_CRC32(crc, buf, len)
  54. #endif // defined(__aarch64__) && defined(__linux__)
  55. #endif // __cplusplus
  56. #endif // CHECKSUM_H