KeyValueHolder.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2020 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 KeyValueHolder_hpp
  21. #define KeyValueHolder_hpp
  22. #ifdef __cplusplus
  23. #include "MMBuffer.h"
  24. #include "aes/AESCrypt.h"
  25. namespace mmkv {
  26. #pragma pack(push, 1)
  27. struct KeyValueHolder {
  28. uint16_t computedKVSize; // internal use only
  29. uint16_t keySize;
  30. uint32_t valueSize;
  31. uint32_t offset;
  32. KeyValueHolder() = default;
  33. KeyValueHolder(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
  34. MMBuffer toMMBuffer(const void *basePtr) const;
  35. };
  36. #ifndef MMKV_DISABLE_CRYPT
  37. enum KeyValueHolderType : uint8_t {
  38. KeyValueHolderType_Direct, // store value directly
  39. KeyValueHolderType_Memory, // store value in the heap memory
  40. KeyValueHolderType_Offset, // store value by offset
  41. };
  42. // kv holder for encrypted mmkv
  43. struct KeyValueHolderCrypt {
  44. KeyValueHolderType type = KeyValueHolderType_Direct;
  45. union {
  46. // store value by offset
  47. struct {
  48. uint8_t pbKeyValueSize; // size needed to encode keySize & valueSize
  49. uint16_t keySize;
  50. uint32_t valueSize;
  51. uint32_t offset;
  52. AESCryptStatus cryptStatus;
  53. };
  54. // store value directly
  55. struct {
  56. uint8_t paddedSize;
  57. uint8_t paddedValue[1];
  58. };
  59. // store value in the heap memory
  60. struct {
  61. uint32_t memSize;
  62. void *memPtr;
  63. };
  64. };
  65. static constexpr size_t SmallBufferSize() {
  66. return sizeof(KeyValueHolderCrypt) - offsetof(KeyValueHolderCrypt, paddedValue);
  67. }
  68. static bool isValueStoredAsOffset(size_t valueSize) { return valueSize >= 256; }
  69. KeyValueHolderCrypt() = default;
  70. KeyValueHolderCrypt(const void *valuePtr, size_t valueLength);
  71. explicit KeyValueHolderCrypt(MMBuffer &&data);
  72. KeyValueHolderCrypt(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
  73. KeyValueHolderCrypt(KeyValueHolderCrypt &&other) noexcept;
  74. KeyValueHolderCrypt &operator=(KeyValueHolderCrypt &&other) noexcept;
  75. void move(KeyValueHolderCrypt &&other) noexcept;
  76. ~KeyValueHolderCrypt();
  77. uint32_t realValueSize() const;
  78. MMBuffer toMMBuffer(const void *basePtr, const AESCrypt *crypter) const;
  79. std::tuple<uint32_t, uint32_t, AESCryptStatus *> toTuple() {
  80. return std::make_tuple(offset, pbKeyValueSize + keySize + valueSize, &cryptStatus);
  81. }
  82. // those are expensive, just forbid it for possibly misuse
  83. explicit KeyValueHolderCrypt(const KeyValueHolderCrypt &other) = delete;
  84. KeyValueHolderCrypt &operator=(const KeyValueHolderCrypt &other) = delete;
  85. #ifdef MMKV_DEBUG
  86. static void testAESToMMBuffer();
  87. #endif
  88. };
  89. #endif // MMKV_DISABLE_CRYPT
  90. #pragma pack(pop)
  91. } // namespace mmkv
  92. #endif
  93. #endif /* KeyValueHolder_hpp */