MMKVMetaInfo.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 MMKV_MMKVMETAINFO_H
  21. #define MMKV_MMKVMETAINFO_H
  22. #ifdef __cplusplus
  23. #include "aes/AESCrypt.h"
  24. #include <cstdint>
  25. #include <cstring>
  26. namespace mmkv {
  27. enum MMKVVersion : uint32_t {
  28. MMKVVersionDefault = 0,
  29. // record full write back count
  30. MMKVVersionSequence = 1,
  31. // store random iv for encryption
  32. MMKVVersionRandomIV = 2,
  33. // store actual size together with crc checksum, try to reduce file corruption
  34. MMKVVersionActualSize = 3,
  35. // store extra flags
  36. MMKVVersionFlag = 4,
  37. // preserved for next use
  38. MMKVVersionNext = 5,
  39. // always large than next, a placeholder for error check
  40. MMKVVersionHolder = MMKVVersionNext + 1,
  41. };
  42. struct MMKVMetaInfo {
  43. uint32_t m_crcDigest = 0;
  44. uint32_t m_version = MMKVVersionSequence;
  45. uint32_t m_sequence = 0; // full write-back count
  46. uint8_t m_vector[AES_KEY_LEN] = {};
  47. uint32_t m_actualSize = 0;
  48. // confirmed info: it's been synced to file
  49. struct {
  50. uint32_t lastActualSize = 0;
  51. uint32_t lastCRCDigest = 0;
  52. uint32_t _reserved[16] = {};
  53. } m_lastConfirmedMetaInfo;
  54. uint64_t m_flags = 0;
  55. enum MMKVMetaInfoFlag : uint64_t {
  56. EnableKeyExipre = 1 << 0,
  57. };
  58. bool hasFlag(MMKVMetaInfoFlag flag) { return (m_flags & flag) != 0; }
  59. void setFlag(MMKVMetaInfoFlag flag) { m_flags |= flag; }
  60. void unsetFlag(MMKVMetaInfoFlag flag) { m_flags &= ~flag; }
  61. void write(void *ptr) const {
  62. MMKV_ASSERT(ptr);
  63. memcpy(ptr, this, sizeof(MMKVMetaInfo));
  64. }
  65. void writeCRCAndActualSizeOnly(void *ptr) const {
  66. MMKV_ASSERT(ptr);
  67. auto other = (MMKVMetaInfo *) ptr;
  68. other->m_crcDigest = m_crcDigest;
  69. other->m_actualSize = m_actualSize;
  70. }
  71. void read(const void *ptr) {
  72. MMKV_ASSERT(ptr);
  73. memcpy(this, ptr, sizeof(MMKVMetaInfo));
  74. }
  75. };
  76. static_assert(sizeof(MMKVMetaInfo) <= (4 * 1024), "MMKVMetaInfo lager than one pagesize");
  77. } // namespace mmkv
  78. #endif
  79. #endif //MMKV_MMKVMETAINFO_H