react-native-mmkv-dz 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/LICENSE +219 -0
  2. package/MMKV/CHANGELOG.md +553 -0
  3. package/MMKV/Core/CMakeLists.txt +153 -0
  4. package/MMKV/Core/CodedInputData.cpp +228 -0
  5. package/MMKV/Core/CodedInputData.h +83 -0
  6. package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
  7. package/MMKV/Core/CodedInputDataCrypt.h +87 -0
  8. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
  9. package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
  10. package/MMKV/Core/CodedOutputData.cpp +174 -0
  11. package/MMKV/Core/CodedOutputData.h +82 -0
  12. package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
  13. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  14. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  15. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
  16. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
  17. package/MMKV/Core/InterProcessLock.cpp +186 -0
  18. package/MMKV/Core/InterProcessLock.h +119 -0
  19. package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
  20. package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
  21. package/MMKV/Core/KeyValueHolder.cpp +236 -0
  22. package/MMKV/Core/KeyValueHolder.h +118 -0
  23. package/MMKV/Core/MMBuffer.cpp +185 -0
  24. package/MMKV/Core/MMBuffer.h +107 -0
  25. package/MMKV/Core/MMKV.cpp +1418 -0
  26. package/MMKV/Core/MMKV.h +386 -0
  27. package/MMKV/Core/MMKVLog.cpp +127 -0
  28. package/MMKV/Core/MMKVLog.h +86 -0
  29. package/MMKV/Core/MMKVLog_Android.cpp +79 -0
  30. package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
  31. package/MMKV/Core/MMKVPredef.h +245 -0
  32. package/MMKV/Core/MMKV_Android.cpp +259 -0
  33. package/MMKV/Core/MMKV_IO.cpp +1119 -0
  34. package/MMKV/Core/MMKV_IO.h +57 -0
  35. package/MMKV/Core/MMKV_OSX.cpp +347 -0
  36. package/MMKV/Core/MMKV_OSX.h +51 -0
  37. package/MMKV/Core/MemoryFile.cpp +537 -0
  38. package/MMKV/Core/MemoryFile.h +182 -0
  39. package/MMKV/Core/MemoryFile_Android.cpp +211 -0
  40. package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
  41. package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
  42. package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
  43. package/MMKV/Core/MiniPBCoder.cpp +366 -0
  44. package/MMKV/Core/MiniPBCoder.h +129 -0
  45. package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
  46. package/MMKV/Core/PBEncodeItem.hpp +86 -0
  47. package/MMKV/Core/PBUtility.cpp +61 -0
  48. package/MMKV/Core/PBUtility.h +153 -0
  49. package/MMKV/Core/ScopedLock.hpp +69 -0
  50. package/MMKV/Core/ThreadLock.cpp +68 -0
  51. package/MMKV/Core/ThreadLock.h +78 -0
  52. package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
  53. package/MMKV/Core/aes/AESCrypt.cpp +256 -0
  54. package/MMKV/Core/aes/AESCrypt.h +107 -0
  55. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
  56. package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
  57. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
  58. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
  59. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
  60. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
  61. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
  62. package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
  63. package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
  64. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
  65. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
  66. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
  67. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
  68. package/MMKV/Core/core.vcxproj +186 -0
  69. package/MMKV/Core/core.vcxproj.filters +150 -0
  70. package/MMKV/Core/crc32/Checksum.h +67 -0
  71. package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
  72. package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
  73. package/MMKV/Core/crc32/zlib/crc32.h +48 -0
  74. package/MMKV/Core/crc32/zlib/zconf.h +380 -0
  75. package/MMKV/Core/crc32/zlib/zutil.h +25 -0
  76. package/MMKV/LICENSE.TXT +193 -0
  77. package/MMKV/README.md +288 -0
  78. package/README.md +221 -0
  79. package/android/CMakeLists.txt +71 -0
  80. package/android/build.gradle +371 -0
  81. package/android/gradle.properties +4 -0
  82. package/android/src/main/AndroidManifest.xml +4 -0
  83. package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
  84. package/android/src/main/cpp/MmkvHostObject.h +26 -0
  85. package/android/src/main/cpp/cpp-adapter.cpp +65 -0
  86. package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
  87. package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
  88. package/cpp/TypedArray.cpp +341 -0
  89. package/cpp/TypedArray.h +175 -0
  90. package/ios/JSIUtils.h +50 -0
  91. package/ios/JSIUtils.mm +194 -0
  92. package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
  93. package/ios/MmkvHostObject.h +27 -0
  94. package/ios/MmkvHostObject.mm +299 -0
  95. package/ios/MmkvModule.h +5 -0
  96. package/ios/MmkvModule.mm +73 -0
  97. package/lib/commonjs/MMKV.js +146 -0
  98. package/lib/commonjs/MMKV.js.map +1 -0
  99. package/lib/commonjs/PlatformChecker.js +16 -0
  100. package/lib/commonjs/PlatformChecker.js.map +1 -0
  101. package/lib/commonjs/createMMKV.js +66 -0
  102. package/lib/commonjs/createMMKV.js.map +1 -0
  103. package/lib/commonjs/createMMKV.mock.js +40 -0
  104. package/lib/commonjs/createMMKV.mock.js.map +1 -0
  105. package/lib/commonjs/createMMKV.web.js +77 -0
  106. package/lib/commonjs/createMMKV.web.js.map +1 -0
  107. package/lib/commonjs/createTextEncoder.js +24 -0
  108. package/lib/commonjs/createTextEncoder.js.map +1 -0
  109. package/lib/commonjs/hooks.js +200 -0
  110. package/lib/commonjs/hooks.js.map +1 -0
  111. package/lib/commonjs/index.js +32 -0
  112. package/lib/commonjs/index.js.map +1 -0
  113. package/lib/module/MMKV.js +134 -0
  114. package/lib/module/MMKV.js.map +1 -0
  115. package/lib/module/PlatformChecker.js +9 -0
  116. package/lib/module/PlatformChecker.js.map +1 -0
  117. package/lib/module/createMMKV.js +55 -0
  118. package/lib/module/createMMKV.js.map +1 -0
  119. package/lib/module/createMMKV.mock.js +31 -0
  120. package/lib/module/createMMKV.mock.js.map +1 -0
  121. package/lib/module/createMMKV.web.js +67 -0
  122. package/lib/module/createMMKV.web.js.map +1 -0
  123. package/lib/module/createTextEncoder.js +17 -0
  124. package/lib/module/createTextEncoder.js.map +1 -0
  125. package/lib/module/hooks.js +181 -0
  126. package/lib/module/hooks.js.map +1 -0
  127. package/lib/module/index.js +3 -0
  128. package/lib/module/index.js.map +1 -0
  129. package/lib/typescript/MMKV.d.ts +137 -0
  130. package/lib/typescript/PlatformChecker.d.ts +1 -0
  131. package/lib/typescript/createMMKV.d.ts +6 -0
  132. package/lib/typescript/createMMKV.mock.d.ts +2 -0
  133. package/lib/typescript/createMMKV.web.d.ts +2 -0
  134. package/lib/typescript/createTextEncoder.d.ts +1 -0
  135. package/lib/typescript/hooks.d.ts +81 -0
  136. package/lib/typescript/index.d.ts +2 -0
  137. package/package.json +168 -0
  138. package/react-native-mmkv.podspec +32 -0
@@ -0,0 +1,236 @@
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
+
21
+ #include "KeyValueHolder.h"
22
+ #include "PBUtility.h"
23
+ #include "aes/AESCrypt.h"
24
+ #include <cerrno>
25
+ #include <cstring>
26
+ #include <stdexcept>
27
+
28
+ namespace mmkv {
29
+
30
+ KeyValueHolder::KeyValueHolder(uint32_t keyLength, uint32_t valueLength, uint32_t off)
31
+ : keySize(static_cast<uint16_t>(keyLength)), valueSize(valueLength), offset(off) {
32
+ computedKVSize = keySize + static_cast<uint16_t>(pbRawVarint32Size(keySize));
33
+ computedKVSize += static_cast<uint16_t>(pbRawVarint32Size(valueSize));
34
+ }
35
+
36
+ MMBuffer KeyValueHolder::toMMBuffer(const void *basePtr) const {
37
+ auto realPtr = (uint8_t *) basePtr + offset;
38
+ realPtr += computedKVSize;
39
+ return MMBuffer(realPtr, valueSize, MMBufferNoCopy);
40
+ }
41
+
42
+ #ifndef MMKV_DISABLE_CRYPT
43
+
44
+ KeyValueHolderCrypt::KeyValueHolderCrypt(const void *src, size_t length) {
45
+ if (length <= SmallBufferSize()) {
46
+ type = KeyValueHolderType_Direct;
47
+ paddedSize = static_cast<uint8_t>(length);
48
+ memcpy(paddedValue, src, length);
49
+ } else {
50
+ type = KeyValueHolderType_Memory;
51
+ memSize = static_cast<uint32_t>(length);
52
+ memPtr = malloc(length);
53
+ if (!memPtr) {
54
+ throw std::runtime_error(strerror(errno));
55
+ }
56
+ memcpy(memPtr, src, memSize);
57
+ }
58
+ }
59
+
60
+ KeyValueHolderCrypt::KeyValueHolderCrypt(MMBuffer &&data) {
61
+ if (data.type == MMBuffer::MMBufferType_Small) {
62
+ static_assert(SmallBufferSize() >= MMBuffer::SmallBufferSize(), "KeyValueHolderCrypt can't hold MMBuffer");
63
+
64
+ type = KeyValueHolderType_Direct;
65
+ paddedSize = static_cast<uint8_t>(data.length());
66
+ memcpy(paddedValue, data.getPtr(), data.length());
67
+ } else {
68
+ type = KeyValueHolderType_Memory;
69
+ memSize = static_cast<uint32_t>(data.length());
70
+ # ifdef MMKV_APPLE
71
+ if (data.m_data != nil) {
72
+ memPtr = malloc(memSize);
73
+ if (!memPtr) {
74
+ throw std::runtime_error(strerror(errno));
75
+ }
76
+ memcpy(memPtr, data.getPtr(), memSize);
77
+ return;
78
+ }
79
+ # endif
80
+ memPtr = data.getPtr();
81
+ data.detach();
82
+ }
83
+ }
84
+
85
+ KeyValueHolderCrypt::KeyValueHolderCrypt(uint32_t keyLength, uint32_t valueLength, uint32_t off)
86
+ : type(KeyValueHolderType_Offset), keySize(static_cast<uint16_t>(keyLength)), valueSize(valueLength), offset(off) {
87
+
88
+ pbKeyValueSize = static_cast<uint8_t>(pbRawVarint32Size(keySize) + pbRawVarint32Size(valueSize));
89
+ }
90
+
91
+ KeyValueHolderCrypt::KeyValueHolderCrypt(KeyValueHolderCrypt &&other) noexcept {
92
+ this->move(std::move(other));
93
+ }
94
+
95
+ KeyValueHolderCrypt &KeyValueHolderCrypt::operator=(KeyValueHolderCrypt &&other) noexcept {
96
+ if (type == KeyValueHolderType_Memory && memPtr) {
97
+ free(memPtr);
98
+ }
99
+ this->move(std::move(other));
100
+ return *this;
101
+ }
102
+
103
+ void KeyValueHolderCrypt::move(KeyValueHolderCrypt &&other) noexcept {
104
+ if (other.type == KeyValueHolderType_Direct || other.type == KeyValueHolderType_Offset) {
105
+ memcpy(this, &other, sizeof(other));
106
+ } else if (other.type == KeyValueHolderType_Memory) {
107
+ type = KeyValueHolderType_Memory;
108
+ memSize = other.memSize;
109
+ memPtr = other.memPtr;
110
+ other.memPtr = nullptr;
111
+ }
112
+ }
113
+
114
+ KeyValueHolderCrypt::~KeyValueHolderCrypt() {
115
+ if (type == KeyValueHolderType_Memory && memPtr) {
116
+ free(memPtr);
117
+ }
118
+ }
119
+
120
+ uint32_t KeyValueHolderCrypt::realValueSize() const {
121
+ switch (type) {
122
+ case KeyValueHolderType_Direct:
123
+ return paddedSize;
124
+ case KeyValueHolderType_Offset:
125
+ return valueSize;
126
+ case KeyValueHolderType_Memory:
127
+ return memSize;
128
+ }
129
+ return 0;
130
+ }
131
+
132
+ // get decrypt data with [position, -1)
133
+ static MMBuffer decryptBuffer(AESCrypt &crypter, const MMBuffer &inputBuffer, size_t position) {
134
+ static size_t smallBuffer[16 / sizeof(size_t)];
135
+ auto basePtr = (uint8_t *) inputBuffer.getPtr();
136
+ auto ptr = basePtr;
137
+ for (size_t index = sizeof(smallBuffer); index < position; index += sizeof(smallBuffer)) {
138
+ crypter.decrypt(ptr, smallBuffer, sizeof(smallBuffer));
139
+ ptr += sizeof(smallBuffer);
140
+ }
141
+ if (ptr < basePtr + position) {
142
+ crypter.decrypt(ptr, smallBuffer, static_cast<size_t>(basePtr + position - ptr));
143
+ ptr = basePtr + position;
144
+ }
145
+ size_t length = inputBuffer.length() - position;
146
+ MMBuffer tmp(length);
147
+
148
+ auto input = ptr;
149
+ auto output = tmp.getPtr();
150
+ crypter.decrypt(input, output, length);
151
+
152
+ return tmp;
153
+ }
154
+
155
+ MMBuffer KeyValueHolderCrypt::toMMBuffer(const void *basePtr, const AESCrypt *crypter) const {
156
+ if (type == KeyValueHolderType_Direct) {
157
+ return MMBuffer((void *) paddedValue, paddedSize, MMBufferNoCopy);
158
+ } else if (type == KeyValueHolderType_Memory) {
159
+ return MMBuffer(memPtr, memSize, MMBufferNoCopy);
160
+ } else {
161
+ auto realPtr = (uint8_t *) basePtr + offset;
162
+ auto position = static_cast<uint32_t>(pbKeyValueSize + keySize);
163
+ auto realSize = position + valueSize;
164
+ auto kvBuffer = MMBuffer(realPtr, realSize, MMBufferNoCopy);
165
+ auto decrypter = crypter->cloneWithStatus(cryptStatus);
166
+ return decryptBuffer(decrypter, kvBuffer, position);
167
+ }
168
+ }
169
+
170
+ #endif // MMKV_DISABLE_CRYPT
171
+
172
+ } // namespace mmkv
173
+
174
+ #if !defined(MMKV_DISABLE_CRYPT) && defined(MMKV_DEBUG)
175
+ # include "CodedInputData.h"
176
+ # include "CodedOutputData.h"
177
+ # include "MMKVLog.h"
178
+ # include <ctime>
179
+
180
+ using namespace std;
181
+
182
+ namespace mmkv {
183
+
184
+ void KeyValueHolderCrypt::testAESToMMBuffer() {
185
+ const uint8_t plainText[] = "Hello, OpenSSL-mmkv::KeyValueHolderCrypt::testAESToMMBuffer() with AES CFB 128.";
186
+ constexpr size_t textLength = sizeof(plainText) - 1;
187
+
188
+ const uint8_t key[] = "TheAESKey";
189
+ constexpr size_t keyLength = sizeof(key) - 1;
190
+
191
+ uint8_t iv[AES_KEY_LEN];
192
+ srand((unsigned) time(nullptr));
193
+ for (uint32_t i = 0; i < AES_KEY_LEN; i++) {
194
+ iv[i] = (uint8_t) rand();
195
+ }
196
+ AESCrypt crypt1(key, keyLength, iv, sizeof(iv));
197
+
198
+ auto encryptText = new uint8_t[DEFAULT_MMAP_SIZE];
199
+ memset(encryptText, 0, DEFAULT_MMAP_SIZE);
200
+ CodedOutputData output(encryptText, DEFAULT_MMAP_SIZE);
201
+ output.writeData(MMBuffer((void *) key, keyLength, MMBufferNoCopy));
202
+ auto lengthOfValue = textLength + pbRawVarint32Size((uint32_t) textLength);
203
+ output.writeRawVarint32((int32_t) lengthOfValue);
204
+ output.writeData(MMBuffer((void *) plainText, textLength, MMBufferNoCopy));
205
+ crypt1.encrypt(encryptText, encryptText, (size_t)(output.curWritePointer() - encryptText));
206
+
207
+ AESCrypt decrypt(key, keyLength, iv, sizeof(iv));
208
+ uint8_t smallBuffer[32];
209
+ decrypt.decrypt(encryptText, smallBuffer, 5);
210
+ auto keySize = CodedInputData(smallBuffer, 5).readUInt32();
211
+ auto sizeOfKeySize = pbRawVarint32Size(keySize);
212
+ auto position = sizeOfKeySize;
213
+ decrypt.decrypt(encryptText + 5, smallBuffer + 5, static_cast<size_t>(sizeOfKeySize + keySize - 5));
214
+ position += keySize;
215
+ decrypt.decrypt(encryptText + position, smallBuffer + position, 5);
216
+ auto valueSize = CodedInputData(smallBuffer + position, 5).readUInt32();
217
+ // auto sizeOfValueSize = pbRawVarint32Size(valueSize);
218
+ KeyValueHolderCrypt kvHolder(keySize, valueSize, 0);
219
+ auto rollbackSize = position + 5;
220
+ decrypt.statusBeforeDecrypt(encryptText + rollbackSize, smallBuffer + rollbackSize, rollbackSize,
221
+ kvHolder.cryptStatus);
222
+ auto value = kvHolder.toMMBuffer(encryptText, &decrypt);
223
+ # ifdef MMKV_APPLE
224
+ MMKVInfo("testAESToMMBuffer: %@", CodedInputData((char *) value.getPtr(), value.length()).readString());
225
+ # else
226
+ MMKVInfo("testAESToMMBuffer: %s", CodedInputData((char *) value.getPtr(), value.length()).readString().c_str());
227
+ # endif
228
+ MMKVInfo("MMBuffer::SmallBufferSize() = %u, KeyValueHolderCrypt::SmallBufferSize() = %u",
229
+ MMBuffer::SmallBufferSize(), KeyValueHolderCrypt::SmallBufferSize());
230
+
231
+ delete[] encryptText;
232
+ }
233
+
234
+ } // namespace mmkv
235
+
236
+ #endif
@@ -0,0 +1,118 @@
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
+
21
+ #ifndef KeyValueHolder_hpp
22
+ #define KeyValueHolder_hpp
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMBuffer.h"
26
+ #include "aes/AESCrypt.h"
27
+
28
+ namespace mmkv {
29
+
30
+ #pragma pack(push, 1)
31
+
32
+ struct KeyValueHolder {
33
+ uint16_t computedKVSize; // internal use only
34
+ uint16_t keySize;
35
+ uint32_t valueSize;
36
+ uint32_t offset;
37
+
38
+ KeyValueHolder() = default;
39
+ KeyValueHolder(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
40
+
41
+ MMBuffer toMMBuffer(const void *basePtr) const;
42
+ };
43
+
44
+ #ifndef MMKV_DISABLE_CRYPT
45
+
46
+ enum KeyValueHolderType : uint8_t {
47
+ KeyValueHolderType_Direct, // store value directly
48
+ KeyValueHolderType_Memory, // store value in the heap memory
49
+ KeyValueHolderType_Offset, // store value by offset
50
+ };
51
+
52
+ // kv holder for encrypted mmkv
53
+ struct KeyValueHolderCrypt {
54
+ KeyValueHolderType type = KeyValueHolderType_Direct;
55
+
56
+ union {
57
+ // store value by offset
58
+ struct {
59
+ uint8_t pbKeyValueSize; // size needed to encode keySize & valueSize
60
+ uint16_t keySize;
61
+ uint32_t valueSize;
62
+ uint32_t offset;
63
+ AESCryptStatus cryptStatus;
64
+ };
65
+ // store value directly
66
+ struct {
67
+ uint8_t paddedSize;
68
+ uint8_t paddedValue[1];
69
+ };
70
+ // store value in the heap memory
71
+ struct {
72
+ uint32_t memSize;
73
+ void *memPtr;
74
+ };
75
+ };
76
+
77
+ static constexpr size_t SmallBufferSize() {
78
+ return sizeof(KeyValueHolderCrypt) - offsetof(KeyValueHolderCrypt, paddedValue);
79
+ }
80
+
81
+ static bool isValueStoredAsOffset(size_t valueSize) { return valueSize >= 256; }
82
+
83
+ KeyValueHolderCrypt() = default;
84
+ KeyValueHolderCrypt(const void *valuePtr, size_t valueLength);
85
+ explicit KeyValueHolderCrypt(MMBuffer &&data);
86
+ KeyValueHolderCrypt(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
87
+
88
+ KeyValueHolderCrypt(KeyValueHolderCrypt &&other) noexcept;
89
+ KeyValueHolderCrypt &operator=(KeyValueHolderCrypt &&other) noexcept;
90
+ void move(KeyValueHolderCrypt &&other) noexcept;
91
+
92
+ ~KeyValueHolderCrypt();
93
+
94
+ uint32_t realValueSize() const;
95
+
96
+ MMBuffer toMMBuffer(const void *basePtr, const AESCrypt *crypter) const;
97
+
98
+ std::tuple<uint32_t, uint32_t, AESCryptStatus *> toTuple() {
99
+ return std::make_tuple(offset, pbKeyValueSize + keySize + valueSize, &cryptStatus);
100
+ }
101
+
102
+ // those are expensive, just forbid it for possibly misuse
103
+ explicit KeyValueHolderCrypt(const KeyValueHolderCrypt &other) = delete;
104
+ KeyValueHolderCrypt &operator=(const KeyValueHolderCrypt &other) = delete;
105
+
106
+ #ifdef MMKV_DEBUG
107
+ static void testAESToMMBuffer();
108
+ #endif
109
+ };
110
+
111
+ #endif // MMKV_DISABLE_CRYPT
112
+
113
+ #pragma pack(pop)
114
+
115
+ } // namespace mmkv
116
+
117
+ #endif
118
+ #endif /* KeyValueHolder_hpp */
@@ -0,0 +1,185 @@
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
+
21
+ #include "MMBuffer.h"
22
+ #include <cerrno>
23
+ #include <cstdlib>
24
+ #include <cstring>
25
+ #include <utility>
26
+ #include <stdexcept>
27
+
28
+ #ifdef MMKV_APPLE
29
+ # if __has_feature(objc_arc)
30
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
31
+ # endif
32
+ #endif
33
+
34
+ using namespace std;
35
+
36
+ namespace mmkv {
37
+
38
+ MMBuffer::MMBuffer(size_t length) {
39
+ if (length > SmallBufferSize()) {
40
+ type = MMBufferType_Normal;
41
+ isNoCopy = MMBufferCopy;
42
+ size = length;
43
+ ptr = malloc(size);
44
+ if (!ptr) {
45
+ throw std::runtime_error(strerror(errno));
46
+ }
47
+ #ifdef MMKV_APPLE
48
+ m_data = nil;
49
+ #endif
50
+ } else {
51
+ type = MMBufferType_Small;
52
+ paddedSize = static_cast<uint8_t>(length);
53
+ }
54
+ }
55
+
56
+ MMBuffer::MMBuffer(void *source, size_t length, MMBufferCopyFlag flag) : isNoCopy(flag) {
57
+ if (isNoCopy == MMBufferCopy) {
58
+ if (length > SmallBufferSize()) {
59
+ type = MMBufferType_Normal;
60
+ size = length;
61
+ ptr = malloc(size);
62
+ if (!ptr) {
63
+ throw std::runtime_error(strerror(errno));
64
+ }
65
+ memcpy(ptr, source, size);
66
+ #ifdef MMKV_APPLE
67
+ m_data = nil;
68
+ #endif
69
+ } else {
70
+ type = MMBufferType_Small;
71
+ paddedSize = static_cast<uint8_t>(length);
72
+ memcpy(paddedBuffer, source, length);
73
+ }
74
+ } else {
75
+ type = MMBufferType_Normal;
76
+ size = length;
77
+ ptr = source;
78
+ #ifdef MMKV_APPLE
79
+ m_data = nil;
80
+ #endif
81
+ }
82
+ }
83
+
84
+ #ifdef MMKV_APPLE
85
+ MMBuffer::MMBuffer(NSData *data, MMBufferCopyFlag flag)
86
+ : type(MMBufferType_Normal), ptr((void *) data.bytes), size(data.length), isNoCopy(flag) {
87
+ if (isNoCopy == MMBufferCopy) {
88
+ m_data = [data retain];
89
+ } else {
90
+ m_data = data;
91
+ }
92
+ }
93
+ #endif
94
+
95
+ MMBuffer::MMBuffer(MMBuffer &&other) noexcept : type(other.type) {
96
+ if (type == MMBufferType_Normal) {
97
+ size = other.size;
98
+ ptr = other.ptr;
99
+ isNoCopy = other.isNoCopy;
100
+ #ifdef MMKV_APPLE
101
+ m_data = other.m_data;
102
+ #endif
103
+ other.detach();
104
+ } else {
105
+ paddedSize = other.paddedSize;
106
+ memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
107
+ }
108
+ }
109
+
110
+ MMBuffer &MMBuffer::operator=(MMBuffer &&other) noexcept {
111
+ if (type == MMBufferType_Normal) {
112
+ if (other.type == MMBufferType_Normal) {
113
+ std::swap(isNoCopy, other.isNoCopy);
114
+ std::swap(size, other.size);
115
+ std::swap(ptr, other.ptr);
116
+ #ifdef MMKV_APPLE
117
+ std::swap(m_data, other.m_data);
118
+ #endif
119
+ } else {
120
+ type = MMBufferType_Small;
121
+ if (isNoCopy == MMBufferCopy) {
122
+ #ifdef MMKV_APPLE
123
+ if (m_data) {
124
+ [m_data release];
125
+ } else if (ptr) {
126
+ free(ptr);
127
+ }
128
+ #else
129
+ if (ptr) {
130
+ free(ptr);
131
+ }
132
+ #endif
133
+ }
134
+ paddedSize = other.paddedSize;
135
+ memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
136
+ }
137
+ } else {
138
+ if (other.type == MMBufferType_Normal) {
139
+ type = MMBufferType_Normal;
140
+ isNoCopy = other.isNoCopy;
141
+ size = other.size;
142
+ ptr = other.ptr;
143
+ #ifdef MMKV_APPLE
144
+ m_data = other.m_data;
145
+ #endif
146
+ other.detach();
147
+ } else {
148
+ uint8_t tmp[SmallBufferSize()];
149
+ memcpy(tmp, other.paddedBuffer, other.paddedSize);
150
+ memcpy(other.paddedBuffer, paddedBuffer, paddedSize);
151
+ memcpy(paddedBuffer, tmp, other.paddedSize);
152
+ std::swap(paddedSize, other.paddedSize);
153
+ }
154
+ }
155
+
156
+ return *this;
157
+ }
158
+
159
+ MMBuffer::~MMBuffer() {
160
+ if (isStoredOnStack()) {
161
+ return;
162
+ }
163
+
164
+ #ifdef MMKV_APPLE
165
+ if (m_data) {
166
+ if (isNoCopy == MMBufferCopy) {
167
+ [m_data release];
168
+ }
169
+ return;
170
+ }
171
+ #endif
172
+
173
+ if (isNoCopy == MMBufferCopy && ptr) {
174
+ free(ptr);
175
+ }
176
+ }
177
+
178
+ void MMBuffer::detach() {
179
+ // type = MMBufferType_Small;
180
+ // paddedSize = 0;
181
+ auto memsetPtr = (size_t *) &type;
182
+ *memsetPtr = 0;
183
+ }
184
+
185
+ } // namespace mmkv
@@ -0,0 +1,107 @@
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
+
21
+ #ifndef MMKV_MMBUFFER_H
22
+ #define MMKV_MMBUFFER_H
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMKVPredef.h"
26
+
27
+ #include <cstdint>
28
+ #include <cstdlib>
29
+ #include <cstddef>
30
+
31
+ namespace mmkv {
32
+
33
+ enum MMBufferCopyFlag : bool {
34
+ MMBufferCopy = false,
35
+ MMBufferNoCopy = true,
36
+ };
37
+
38
+ #pragma pack(push, 1)
39
+
40
+ #ifndef MMKV_DISABLE_CRYPT
41
+ struct KeyValueHolderCrypt;
42
+ #endif
43
+
44
+ class MMBuffer {
45
+ enum MMBufferType : uint8_t {
46
+ MMBufferType_Small, // store small buffer in stack memory
47
+ MMBufferType_Normal, // store in heap memory
48
+ };
49
+ MMBufferType type;
50
+
51
+ union {
52
+ struct {
53
+ MMBufferCopyFlag isNoCopy;
54
+ size_t size;
55
+ void *ptr;
56
+ #ifdef MMKV_APPLE
57
+ NSData *m_data;
58
+ #endif
59
+ };
60
+ struct {
61
+ uint8_t paddedSize;
62
+ // make at least 10 bytes to hold all primitive types (negative int32, int64, double etc) on 32 bit device
63
+ // on 64 bit device it's guaranteed larger than 10 bytes
64
+ uint8_t paddedBuffer[10];
65
+ };
66
+ };
67
+
68
+ static constexpr size_t SmallBufferSize() {
69
+ return sizeof(MMBuffer) - offsetof(MMBuffer, paddedBuffer);
70
+ }
71
+
72
+ public:
73
+ explicit MMBuffer(size_t length = 0);
74
+ MMBuffer(void *source, size_t length, MMBufferCopyFlag flag = MMBufferCopy);
75
+ #ifdef MMKV_APPLE
76
+ explicit MMBuffer(NSData *data, MMBufferCopyFlag flag = MMBufferCopy);
77
+ #endif
78
+
79
+ MMBuffer(MMBuffer &&other) noexcept;
80
+ MMBuffer &operator=(MMBuffer &&other) noexcept;
81
+
82
+ ~MMBuffer();
83
+
84
+ bool isStoredOnStack() const { return (type == MMBufferType_Small); }
85
+
86
+ void *getPtr() const { return isStoredOnStack() ? (void *) paddedBuffer : ptr; }
87
+
88
+ size_t length() const { return isStoredOnStack() ? paddedSize : size; }
89
+
90
+ // transfer ownership to others
91
+ void detach();
92
+
93
+ // those are expensive, just forbid it for possibly misuse
94
+ explicit MMBuffer(const MMBuffer &other) = delete;
95
+ MMBuffer &operator=(const MMBuffer &other) = delete;
96
+
97
+ #ifndef MMKV_DISABLE_CRYPT
98
+ friend KeyValueHolderCrypt;
99
+ #endif
100
+ };
101
+
102
+ #pragma pack(pop)
103
+
104
+ } // namespace mmkv
105
+
106
+ #endif
107
+ #endif //MMKV_MMBUFFER_H