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,280 @@
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 "CodedInputDataCrypt.h"
22
+ #include "MMKVLog.h"
23
+ #include "PBUtility.h"
24
+ #include <cassert>
25
+ #include <cerrno>
26
+ #include <cstring>
27
+ #include <stdexcept>
28
+
29
+ #ifdef MMKV_APPLE
30
+ # if __has_feature(objc_arc)
31
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
32
+ # endif
33
+ #endif // MMKV_APPLE
34
+
35
+ #ifndef MMKV_DISABLE_CRYPT
36
+
37
+ using namespace std;
38
+
39
+ namespace mmkv {
40
+
41
+ CodedInputDataCrypt::CodedInputDataCrypt(const void *oData, size_t length, AESCrypt &crypt)
42
+ : m_ptr((uint8_t *) oData), m_size(length), m_position(0), m_decryptPosition(0), m_decrypter(crypt) {
43
+ m_decryptBufferSize = AES_KEY_LEN * 2;
44
+ m_decryptBufferPosition = static_cast<size_t>(crypt.m_number);
45
+ m_decryptBufferDiscardPosition = m_decryptBufferPosition;
46
+ m_decryptBufferDecryptLength = m_decryptBufferPosition;
47
+
48
+ m_decryptBuffer = (uint8_t *) malloc(m_decryptBufferSize);
49
+ if (!m_decryptBuffer) {
50
+ throw runtime_error(strerror(errno));
51
+ }
52
+ }
53
+
54
+ CodedInputDataCrypt::~CodedInputDataCrypt() {
55
+ if (m_decryptBuffer) {
56
+ free(m_decryptBuffer);
57
+ }
58
+ }
59
+
60
+ void CodedInputDataCrypt::seek(size_t addedSize) {
61
+ m_position += addedSize;
62
+ m_decryptPosition += addedSize;
63
+
64
+ if (m_position > m_size) {
65
+ throw out_of_range("OutOfSpace");
66
+ }
67
+ assert(m_position % AES_KEY_LEN == m_decrypter.m_number);
68
+ }
69
+
70
+ void CodedInputDataCrypt::consumeBytes(size_t length, bool discardPreData) {
71
+ if (discardPreData) {
72
+ m_decryptBufferDiscardPosition = m_decryptBufferPosition;
73
+ }
74
+ auto decryptedBytesLeft = m_decryptBufferDecryptLength - m_decryptBufferPosition;
75
+ if (decryptedBytesLeft >= length) {
76
+ return;
77
+ }
78
+ length -= decryptedBytesLeft;
79
+
80
+ // if there's some data left inside m_decrypter.m_vector, use them first
81
+ // it will be faster when always decrypt with (n * AES_KEY_LEN) bytes
82
+ if (m_decrypter.m_number != 0) {
83
+ auto alignDecrypter = AES_KEY_LEN - m_decrypter.m_number;
84
+ // make sure no data left inside m_decrypter.m_vector after decrypt
85
+ if (length < alignDecrypter) {
86
+ length = alignDecrypter;
87
+ } else {
88
+ length -= alignDecrypter;
89
+ length = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
90
+ length += alignDecrypter;
91
+ }
92
+ } else {
93
+ length = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
94
+ }
95
+ auto bytesLeftInSrc = m_size - m_decryptPosition;
96
+ length = min(bytesLeftInSrc, length);
97
+
98
+ auto bytesLeftInBuffer = m_decryptBufferSize - m_decryptBufferDecryptLength;
99
+ // try move some space
100
+ if (bytesLeftInBuffer < length && m_decryptBufferDiscardPosition > 0) {
101
+ auto posToMove = (m_decryptBufferDiscardPosition / AES_KEY_LEN) * AES_KEY_LEN;
102
+ if (posToMove) {
103
+ auto sizeToMove = m_decryptBufferDecryptLength - posToMove;
104
+ memmove(m_decryptBuffer, m_decryptBuffer + posToMove, sizeToMove);
105
+ m_decryptBufferPosition -= posToMove;
106
+ m_decryptBufferDecryptLength -= posToMove;
107
+ m_decryptBufferDiscardPosition = 0;
108
+ bytesLeftInBuffer = m_decryptBufferSize - m_decryptBufferDecryptLength;
109
+ }
110
+ }
111
+ // still no enough space, try realloc()
112
+ if (bytesLeftInBuffer < length) {
113
+ auto newSize = m_decryptBufferSize + length;
114
+ auto newBuffer = realloc(m_decryptBuffer, newSize);
115
+ if (!newBuffer) {
116
+ throw runtime_error(strerror(errno));
117
+ }
118
+ m_decryptBuffer = (uint8_t *) newBuffer;
119
+ m_decryptBufferSize = newSize;
120
+ }
121
+ m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer + m_decryptBufferDecryptLength, length);
122
+ m_decryptPosition += length;
123
+ m_decryptBufferDecryptLength += length;
124
+ assert(m_decryptPosition == m_size || m_decrypter.m_number == 0);
125
+ }
126
+
127
+ void CodedInputDataCrypt::skipBytes(size_t length) {
128
+ m_position += length;
129
+
130
+ auto decryptedBytesLeft = m_decryptBufferDecryptLength - m_decryptBufferPosition;
131
+ if (decryptedBytesLeft >= length) {
132
+ m_decryptBufferPosition += length;
133
+ return;
134
+ }
135
+ length -= decryptedBytesLeft;
136
+ // if this happens, we need optimization like the alignDecrypter above
137
+ assert(m_decrypter.m_number == 0);
138
+
139
+ size_t alignSize = ((length + AES_KEY_LEN - 1) / AES_KEY_LEN) * AES_KEY_LEN;
140
+ auto bytesLeftInSrc = m_size - m_decryptPosition;
141
+ auto size = min(alignSize, bytesLeftInSrc);
142
+ decryptedBytesLeft = size - length;
143
+ for (size_t index = 0, round = size / AES_KEY_LEN; index < round; index++) {
144
+ m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer, AES_KEY_LEN);
145
+ m_decryptPosition += AES_KEY_LEN;
146
+ size -= AES_KEY_LEN;
147
+ }
148
+ if (size) {
149
+ m_decrypter.decrypt(m_ptr + m_decryptPosition, m_decryptBuffer, size);
150
+ m_decryptPosition += size;
151
+ m_decryptBufferPosition = size - decryptedBytesLeft;
152
+ m_decryptBufferDecryptLength = size;
153
+ } else {
154
+ m_decryptBufferPosition = AES_KEY_LEN - decryptedBytesLeft;
155
+ m_decryptBufferDecryptLength = AES_KEY_LEN;
156
+ }
157
+ assert(m_decryptBufferPosition <= m_decryptBufferDecryptLength);
158
+ assert(m_decryptPosition - m_decryptBufferDecryptLength + m_decryptBufferPosition == m_position);
159
+ }
160
+
161
+ inline void CodedInputDataCrypt::statusBeforeDecrypt(size_t rollbackSize, AESCryptStatus &status) {
162
+ rollbackSize += m_decryptBufferDecryptLength - m_decryptBufferPosition;
163
+ m_decrypter.statusBeforeDecrypt(m_ptr + m_decryptPosition, m_decryptBuffer + m_decryptBufferDecryptLength,
164
+ rollbackSize, status);
165
+ }
166
+
167
+ int8_t CodedInputDataCrypt::readRawByte() {
168
+ assert(m_position <= m_decryptPosition);
169
+ if (m_position == m_size) {
170
+ auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
171
+ throw out_of_range(msg);
172
+ }
173
+ m_position++;
174
+
175
+ assert(m_decryptBufferPosition < m_decryptBufferSize);
176
+ auto *bytes = (int8_t *) m_decryptBuffer;
177
+ return bytes[m_decryptBufferPosition++];
178
+ }
179
+
180
+ int32_t CodedInputDataCrypt::readRawVarint32(bool discardPreData) {
181
+ consumeBytes(10, discardPreData);
182
+
183
+ int8_t tmp = this->readRawByte();
184
+ if (tmp >= 0) {
185
+ return tmp;
186
+ }
187
+ int32_t result = tmp & 0x7f;
188
+ if ((tmp = this->readRawByte()) >= 0) {
189
+ result |= tmp << 7;
190
+ } else {
191
+ result |= (tmp & 0x7f) << 7;
192
+ if ((tmp = this->readRawByte()) >= 0) {
193
+ result |= tmp << 14;
194
+ } else {
195
+ result |= (tmp & 0x7f) << 14;
196
+ if ((tmp = this->readRawByte()) >= 0) {
197
+ result |= tmp << 21;
198
+ } else {
199
+ result |= (tmp & 0x7f) << 21;
200
+ result |= (tmp = this->readRawByte()) << 28;
201
+ if (tmp < 0) {
202
+ // discard upper 32 bits
203
+ for (int i = 0; i < 5; i++) {
204
+ if (this->readRawByte() >= 0) {
205
+ return result;
206
+ }
207
+ }
208
+ throw invalid_argument("InvalidProtocolBuffer malformed varint32");
209
+ }
210
+ }
211
+ }
212
+ }
213
+ return result;
214
+ }
215
+
216
+ int32_t CodedInputDataCrypt::readInt32() {
217
+ return this->readRawVarint32();
218
+ }
219
+
220
+ # ifndef MMKV_APPLE
221
+
222
+ string CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
223
+ kvHolder.offset = static_cast<uint32_t>(m_position);
224
+
225
+ int32_t size = this->readRawVarint32(true);
226
+ if (size < 0) {
227
+ throw length_error("InvalidProtocolBuffer negativeSize");
228
+ }
229
+
230
+ auto s_size = static_cast<size_t>(size);
231
+ if (s_size <= m_size - m_position) {
232
+ consumeBytes(s_size);
233
+
234
+ kvHolder.keySize = static_cast<uint16_t>(s_size);
235
+
236
+ string result((char *) (m_decryptBuffer + m_decryptBufferPosition), s_size);
237
+ m_position += s_size;
238
+ m_decryptBufferPosition += s_size;
239
+ return result;
240
+ } else {
241
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
242
+ }
243
+ }
244
+
245
+ # endif
246
+
247
+ void CodedInputDataCrypt::readData(KeyValueHolderCrypt &kvHolder) {
248
+ int32_t size = this->readRawVarint32();
249
+ if (size < 0) {
250
+ throw length_error("InvalidProtocolBuffer negativeSize");
251
+ }
252
+
253
+ auto s_size = static_cast<size_t>(size);
254
+ if (s_size <= m_size - m_position) {
255
+ if (KeyValueHolderCrypt::isValueStoredAsOffset(s_size)) {
256
+ kvHolder.type = KeyValueHolderType_Offset;
257
+ kvHolder.valueSize = static_cast<uint32_t>(s_size);
258
+ kvHolder.pbKeyValueSize =
259
+ static_cast<uint8_t>(pbRawVarint32Size(kvHolder.valueSize) + pbRawVarint32Size(kvHolder.keySize));
260
+
261
+ size_t rollbackSize = kvHolder.pbKeyValueSize + kvHolder.keySize;
262
+ statusBeforeDecrypt(rollbackSize, kvHolder.cryptStatus);
263
+
264
+ skipBytes(s_size);
265
+ } else {
266
+ consumeBytes(s_size);
267
+
268
+ kvHolder.type = KeyValueHolderType_Direct;
269
+ kvHolder = KeyValueHolderCrypt(m_decryptBuffer + m_decryptBufferPosition, s_size);
270
+ m_decryptBufferPosition += s_size;
271
+ m_position += s_size;
272
+ }
273
+ } else {
274
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
275
+ }
276
+ }
277
+
278
+ } // namespace mmkv
279
+
280
+ #endif // MMKV_DISABLE_CRYPT
@@ -0,0 +1,87 @@
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 CodedInputDataCrypt_h
22
+ #define CodedInputDataCrypt_h
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMKVPredef.h"
26
+
27
+ #include "KeyValueHolder.h"
28
+ #include "MMBuffer.h"
29
+ #include "aes/AESCrypt.h"
30
+ #include <cstdint>
31
+
32
+ #ifdef MMKV_DISABLE_CRYPT
33
+
34
+ namespace mmkv {
35
+ class CodedInputDataCrypt;
36
+ }
37
+
38
+ #else
39
+
40
+ namespace mmkv {
41
+
42
+ class CodedInputDataCrypt {
43
+ uint8_t *const m_ptr;
44
+ size_t m_size;
45
+ size_t m_position;
46
+ size_t m_decryptPosition; // position of text that has beed decrypted
47
+
48
+ AESCrypt &m_decrypter;
49
+ uint8_t *m_decryptBuffer; // internal decrypt buffer, grows by (n * AES_KEY_LEN) bytes
50
+ size_t m_decryptBufferSize;
51
+ size_t m_decryptBufferPosition; // reader position in the buffer, synced with m_position
52
+ size_t m_decryptBufferDecryptLength; // length of the buffer that has been used
53
+ size_t m_decryptBufferDiscardPosition; // recycle position, any data before that can be discarded
54
+
55
+ void consumeBytes(size_t length, bool discardPreData = false);
56
+ void skipBytes(size_t length);
57
+ void statusBeforeDecrypt(size_t rollbackSize, AESCryptStatus &status);
58
+
59
+ int8_t readRawByte();
60
+
61
+ int32_t readRawVarint32(bool discardPreData = false);
62
+
63
+ public:
64
+ CodedInputDataCrypt(const void *oData, size_t length, AESCrypt &crypt);
65
+
66
+ ~CodedInputDataCrypt();
67
+
68
+ bool isAtEnd() { return m_position == m_size; };
69
+
70
+ void seek(size_t addedSize);
71
+
72
+ int32_t readInt32();
73
+
74
+ void readData(KeyValueHolderCrypt &kvHolder);
75
+
76
+ #ifndef MMKV_APPLE
77
+ std::string readString(KeyValueHolderCrypt &kvHolder);
78
+ #else
79
+ NSString *readString(KeyValueHolderCrypt &kvHolder);
80
+ #endif
81
+ };
82
+
83
+ } // namespace mmkv
84
+
85
+ #endif // MMKV_DISABLE_CRYPT
86
+ #endif // __cplusplus
87
+ #endif /* CodedInputDataCrypt_h */
@@ -0,0 +1,62 @@
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 "CodedInputDataCrypt.h"
22
+
23
+ #if defined(MMKV_APPLE) && !defined(MMKV_DISABLE_CRYPT)
24
+
25
+ # include "PBUtility.h"
26
+ # include <stdexcept>
27
+
28
+ # if __has_feature(objc_arc)
29
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
30
+ # endif
31
+
32
+ using namespace std;
33
+
34
+ namespace mmkv {
35
+
36
+ NSString *CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
37
+ kvHolder.offset = static_cast<uint32_t>(m_position);
38
+
39
+ int32_t size = this->readRawVarint32(true);
40
+ if (size < 0) {
41
+ throw length_error("InvalidProtocolBuffer negativeSize");
42
+ }
43
+
44
+ auto s_size = static_cast<size_t>(size);
45
+ if (s_size <= m_size - m_position) {
46
+ consumeBytes(s_size);
47
+
48
+ kvHolder.keySize = static_cast<uint16_t>(s_size);
49
+
50
+ auto ptr = m_decryptBuffer + m_decryptBufferPosition;
51
+ NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
52
+ m_position += s_size;
53
+ m_decryptBufferPosition += s_size;
54
+ return [result autorelease];
55
+ } else {
56
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
57
+ }
58
+ }
59
+
60
+ } // namespace mmkv
61
+
62
+ #endif // MMKV_APPLE && !MMKV_DISABLE_CRYPT
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making
3
+ * MMKV available.
4
+ *
5
+ * Copyright (C) 2019 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 "CodedInputData.h"
22
+
23
+ #ifdef MMKV_APPLE
24
+
25
+ # include "PBUtility.h"
26
+ # include <stdexcept>
27
+
28
+ # if __has_feature(objc_arc)
29
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
30
+ # endif
31
+
32
+ using namespace std;
33
+
34
+ namespace mmkv {
35
+
36
+ NSString *CodedInputData::readString() {
37
+ int32_t size = this->readRawVarint32();
38
+ if (size < 0) {
39
+ throw length_error("InvalidProtocolBuffer negativeSize");
40
+ }
41
+
42
+ auto s_size = static_cast<size_t>(size);
43
+ if (s_size <= m_size - m_position) {
44
+ auto ptr = m_ptr + m_position;
45
+ NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
46
+ m_position += s_size;
47
+ return [result autorelease];
48
+ } else {
49
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
50
+ }
51
+ }
52
+
53
+ NSString *CodedInputData::readString(KeyValueHolder &kvHolder) {
54
+ kvHolder.offset = static_cast<uint32_t>(m_position);
55
+
56
+ int32_t size = this->readRawVarint32();
57
+ if (size < 0) {
58
+ throw length_error("InvalidProtocolBuffer negativeSize");
59
+ }
60
+
61
+ auto s_size = static_cast<size_t>(size);
62
+ if (s_size <= m_size - m_position) {
63
+ kvHolder.keySize = static_cast<uint16_t>(s_size);
64
+
65
+ auto ptr = m_ptr + m_position;
66
+ NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
67
+ m_position += s_size;
68
+ return [result autorelease];
69
+ } else {
70
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
71
+ }
72
+ }
73
+
74
+ NSData *CodedInputData::readNSData() {
75
+ int32_t size = this->readRawVarint32();
76
+ if (size < 0) {
77
+ throw length_error("InvalidProtocolBuffer negativeSize");
78
+ }
79
+
80
+ auto s_size = static_cast<size_t>(size);
81
+ if (s_size <= m_size - m_position) {
82
+ NSData *result = [NSData dataWithBytes:(m_ptr + m_position) length:s_size];
83
+ m_position += s_size;
84
+ return result;
85
+ } else {
86
+ throw out_of_range("InvalidProtocolBuffer truncatedMessage");
87
+ }
88
+ }
89
+
90
+ } // namespace mmkv
91
+
92
+ #endif // MMKV_APPLE
@@ -0,0 +1,174 @@
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 "CodedOutputData.h"
22
+ #include "PBUtility.h"
23
+ #include <cstring>
24
+ #include <stdexcept>
25
+
26
+ #ifdef MMKV_APPLE
27
+ # if __has_feature(objc_arc)
28
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
29
+ # endif
30
+ #endif // MMKV_APPLE
31
+
32
+ using namespace std;
33
+
34
+ namespace mmkv {
35
+
36
+ CodedOutputData::CodedOutputData(void *ptr, size_t len) : m_ptr((uint8_t *) ptr), m_size(len), m_position(0) {
37
+ MMKV_ASSERT(m_ptr);
38
+ }
39
+
40
+ uint8_t *CodedOutputData::curWritePointer() {
41
+ return m_ptr + m_position;
42
+ }
43
+
44
+ void CodedOutputData::writeDouble(double value) {
45
+ this->writeRawLittleEndian64(Float64ToInt64(value));
46
+ }
47
+
48
+ void CodedOutputData::writeFloat(float value) {
49
+ this->writeRawLittleEndian32(Float32ToInt32(value));
50
+ }
51
+
52
+ void CodedOutputData::writeInt64(int64_t value) {
53
+ this->writeRawVarint64(value);
54
+ }
55
+
56
+ void CodedOutputData::writeUInt64(uint64_t value) {
57
+ writeRawVarint64(static_cast<int64_t>(value));
58
+ }
59
+
60
+ void CodedOutputData::writeInt32(int32_t value) {
61
+ if (value >= 0) {
62
+ this->writeRawVarint32(value);
63
+ } else {
64
+ this->writeRawVarint64(value);
65
+ }
66
+ }
67
+
68
+ void CodedOutputData::writeUInt32(uint32_t value) {
69
+ writeRawVarint32(static_cast<int32_t>(value));
70
+ }
71
+
72
+ void CodedOutputData::writeBool(bool value) {
73
+ this->writeRawByte(static_cast<uint8_t>(value ? 1 : 0));
74
+ }
75
+
76
+ void CodedOutputData::writeData(const MMBuffer &value) {
77
+ this->writeRawVarint32((int32_t) value.length());
78
+ this->writeRawData(value);
79
+ }
80
+
81
+ #ifndef MMKV_APPLE
82
+
83
+ void CodedOutputData::writeString(const string &value) {
84
+ size_t numberOfBytes = value.size();
85
+ this->writeRawVarint32((int32_t) numberOfBytes);
86
+ if (m_position + numberOfBytes > m_size) {
87
+ auto msg = "m_position: " + to_string(m_position) + ", numberOfBytes: " + to_string(numberOfBytes) +
88
+ ", m_size: " + to_string(m_size);
89
+ throw out_of_range(msg);
90
+ }
91
+ memcpy(m_ptr + m_position, ((uint8_t *) value.data()), numberOfBytes);
92
+ m_position += numberOfBytes;
93
+ }
94
+
95
+ #endif // MMKV_APPLE
96
+
97
+ size_t CodedOutputData::spaceLeft() {
98
+ if (m_size <= m_position) {
99
+ return 0;
100
+ }
101
+ return m_size - m_position;
102
+ }
103
+
104
+ void CodedOutputData::seek(size_t addedSize) {
105
+ m_position += addedSize;
106
+
107
+ if (m_position > m_size) {
108
+ throw out_of_range("OutOfSpace");
109
+ }
110
+ }
111
+
112
+ void CodedOutputData::writeRawByte(uint8_t value) {
113
+ if (m_position == m_size) {
114
+ throw out_of_range("m_position: " + to_string(m_position) + " m_size: " + to_string(m_size));
115
+ return;
116
+ }
117
+
118
+ m_ptr[m_position++] = value;
119
+ }
120
+
121
+ void CodedOutputData::writeRawData(const MMBuffer &data) {
122
+ size_t numberOfBytes = data.length();
123
+ if (m_position + numberOfBytes > m_size) {
124
+ auto msg = "m_position: " + to_string(m_position) + ", numberOfBytes: " + to_string(numberOfBytes) +
125
+ ", m_size: " + to_string(m_size);
126
+ throw out_of_range(msg);
127
+ }
128
+ memcpy(m_ptr + m_position, data.getPtr(), numberOfBytes);
129
+ m_position += numberOfBytes;
130
+ }
131
+
132
+ void CodedOutputData::writeRawVarint32(int32_t value) {
133
+ while (true) {
134
+ if ((value & ~0x7f) == 0) {
135
+ this->writeRawByte(static_cast<uint8_t>(value));
136
+ return;
137
+ } else {
138
+ this->writeRawByte(static_cast<uint8_t>((value & 0x7F) | 0x80));
139
+ value = logicalRightShift32(value, 7);
140
+ }
141
+ }
142
+ }
143
+
144
+ void CodedOutputData::writeRawVarint64(int64_t value) {
145
+ while (true) {
146
+ if ((value & ~0x7f) == 0) {
147
+ this->writeRawByte(static_cast<uint8_t>(value));
148
+ return;
149
+ } else {
150
+ this->writeRawByte(static_cast<uint8_t>((value & 0x7f) | 0x80));
151
+ value = logicalRightShift64(value, 7);
152
+ }
153
+ }
154
+ }
155
+
156
+ void CodedOutputData::writeRawLittleEndian32(int32_t value) {
157
+ this->writeRawByte(static_cast<uint8_t>((value) &0xff));
158
+ this->writeRawByte(static_cast<uint8_t>((value >> 8) & 0xff));
159
+ this->writeRawByte(static_cast<uint8_t>((value >> 16) & 0xff));
160
+ this->writeRawByte(static_cast<uint8_t>((value >> 24) & 0xff));
161
+ }
162
+
163
+ void CodedOutputData::writeRawLittleEndian64(int64_t value) {
164
+ this->writeRawByte(static_cast<uint8_t>((value) &0xff));
165
+ this->writeRawByte(static_cast<uint8_t>((value >> 8) & 0xff));
166
+ this->writeRawByte(static_cast<uint8_t>((value >> 16) & 0xff));
167
+ this->writeRawByte(static_cast<uint8_t>((value >> 24) & 0xff));
168
+ this->writeRawByte(static_cast<uint8_t>((value >> 32) & 0xff));
169
+ this->writeRawByte(static_cast<uint8_t>((value >> 40) & 0xff));
170
+ this->writeRawByte(static_cast<uint8_t>((value >> 48) & 0xff));
171
+ this->writeRawByte(static_cast<uint8_t>((value >> 56) & 0xff));
172
+ }
173
+
174
+ } // namespace mmkv