react-native-mmkv 3.3.0 → 4.0.0-beta.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 (95) hide show
  1. package/android/CMakeLists.txt +18 -9
  2. package/android/build.gradle +6 -2
  3. package/cpp/{MMKVManagedBuffer.h → ManagedMMBuffer.h} +6 -6
  4. package/cpp/MmkvHostObject.cpp +13 -8
  5. package/cpp/MmkvHostObject.h +1 -2
  6. package/cpp/MmkvTypes.h +50 -0
  7. package/cpp/NativeMmkvModule.cpp +3 -3
  8. package/cpp/NativeMmkvModule.h +1 -8
  9. package/lib/commonjs/hooks.js +2 -2
  10. package/lib/commonjs/hooks.js.map +1 -1
  11. package/lib/module/hooks.js +2 -2
  12. package/lib/module/hooks.js.map +1 -1
  13. package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +1 -1
  14. package/lib/typescript/src/createMMKV.d.ts.map +1 -1
  15. package/lib/typescript/src/createMMKV.web.d.ts.map +1 -1
  16. package/package.json +24 -24
  17. package/react-native-mmkv.podspec +3 -9
  18. package/react-native.config.js +9 -0
  19. package/src/hooks.ts +2 -2
  20. package/MMKV/Core/CMakeLists.txt +0 -172
  21. package/MMKV/Core/CodedInputData.cpp +0 -252
  22. package/MMKV/Core/CodedInputData.h +0 -87
  23. package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
  24. package/MMKV/Core/CodedInputDataCrypt.h +0 -87
  25. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
  26. package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
  27. package/MMKV/Core/CodedOutputData.cpp +0 -186
  28. package/MMKV/Core/CodedOutputData.h +0 -88
  29. package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
  30. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  31. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  32. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
  33. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
  34. package/MMKV/Core/InterProcessLock.cpp +0 -186
  35. package/MMKV/Core/InterProcessLock.h +0 -119
  36. package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
  37. package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
  38. package/MMKV/Core/KeyValueHolder.cpp +0 -236
  39. package/MMKV/Core/KeyValueHolder.h +0 -122
  40. package/MMKV/Core/MMBuffer.cpp +0 -210
  41. package/MMKV/Core/MMBuffer.h +0 -111
  42. package/MMKV/Core/MMKV.cpp +0 -1702
  43. package/MMKV/Core/MMKV.h +0 -595
  44. package/MMKV/Core/MMKVLog.cpp +0 -127
  45. package/MMKV/Core/MMKVLog.h +0 -86
  46. package/MMKV/Core/MMKVLog_Android.cpp +0 -134
  47. package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
  48. package/MMKV/Core/MMKVPredef.h +0 -293
  49. package/MMKV/Core/MMKV_Android.cpp +0 -261
  50. package/MMKV/Core/MMKV_IO.cpp +0 -1905
  51. package/MMKV/Core/MMKV_IO.h +0 -57
  52. package/MMKV/Core/MMKV_OSX.cpp +0 -423
  53. package/MMKV/Core/MMKV_OSX.h +0 -57
  54. package/MMKV/Core/MemoryFile.cpp +0 -603
  55. package/MMKV/Core/MemoryFile.h +0 -194
  56. package/MMKV/Core/MemoryFile_Android.cpp +0 -236
  57. package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
  58. package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
  59. package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
  60. package/MMKV/Core/MiniPBCoder.cpp +0 -672
  61. package/MMKV/Core/MiniPBCoder.h +0 -151
  62. package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
  63. package/MMKV/Core/PBEncodeItem.hpp +0 -104
  64. package/MMKV/Core/PBUtility.cpp +0 -61
  65. package/MMKV/Core/PBUtility.h +0 -148
  66. package/MMKV/Core/ScopedLock.hpp +0 -69
  67. package/MMKV/Core/ThreadLock.cpp +0 -75
  68. package/MMKV/Core/ThreadLock.h +0 -81
  69. package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
  70. package/MMKV/Core/aes/AESCrypt.cpp +0 -273
  71. package/MMKV/Core/aes/AESCrypt.h +0 -112
  72. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
  73. package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
  74. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
  75. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
  76. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
  77. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
  78. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
  79. package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
  80. package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
  81. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
  82. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
  83. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
  84. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
  85. package/MMKV/Core/core.vcxproj +0 -288
  86. package/MMKV/Core/core.vcxproj.filters +0 -150
  87. package/MMKV/Core/crc32/Checksum.h +0 -75
  88. package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
  89. package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
  90. package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
  91. package/MMKV/Core/crc32/zlib/crc32.h +0 -48
  92. package/MMKV/Core/crc32/zlib/zconf.h +0 -380
  93. package/MMKV/Core/crc32/zlib/zutil.h +0 -25
  94. package/MMKV/LICENSE.TXT +0 -193
  95. package/MMKV/README.md +0 -354
@@ -1,280 +0,0 @@
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
@@ -1,87 +0,0 @@
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 */
@@ -1,62 +0,0 @@
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
@@ -1,92 +0,0 @@
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
@@ -1,186 +0,0 @@
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::reset() {
113
- m_position = 0;
114
- }
115
-
116
- size_t CodedOutputData::getPosition() {
117
- return m_position;
118
- }
119
-
120
- void CodedOutputData::setPosition(size_t position) {
121
- m_position = position;
122
- }
123
-
124
- void CodedOutputData::writeRawByte(uint8_t value) {
125
- if (m_position == m_size) {
126
- throw out_of_range("m_position: " + to_string(m_position) + " m_size: " + to_string(m_size));
127
- return;
128
- }
129
-
130
- m_ptr[m_position++] = value;
131
- }
132
-
133
- void CodedOutputData::writeRawData(const MMBuffer &data) {
134
- size_t numberOfBytes = data.length();
135
- if (m_position + numberOfBytes > m_size) {
136
- auto msg = "m_position: " + to_string(m_position) + ", numberOfBytes: " + to_string(numberOfBytes) +
137
- ", m_size: " + to_string(m_size);
138
- throw out_of_range(msg);
139
- }
140
- memcpy(m_ptr + m_position, data.getPtr(), numberOfBytes);
141
- m_position += numberOfBytes;
142
- }
143
-
144
- void CodedOutputData::writeRawVarint32(int32_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 = logicalRightShift32(value, 7);
152
- }
153
- }
154
- }
155
-
156
- void CodedOutputData::writeRawVarint64(int64_t value) {
157
- while (true) {
158
- if ((value & ~0x7f) == 0) {
159
- this->writeRawByte(static_cast<uint8_t>(value));
160
- return;
161
- } else {
162
- this->writeRawByte(static_cast<uint8_t>((value & 0x7f) | 0x80));
163
- value = logicalRightShift64(value, 7);
164
- }
165
- }
166
- }
167
-
168
- void CodedOutputData::writeRawLittleEndian32(int32_t value) {
169
- this->writeRawByte(static_cast<uint8_t>((value) &0xff));
170
- this->writeRawByte(static_cast<uint8_t>((value >> 8) & 0xff));
171
- this->writeRawByte(static_cast<uint8_t>((value >> 16) & 0xff));
172
- this->writeRawByte(static_cast<uint8_t>((value >> 24) & 0xff));
173
- }
174
-
175
- void CodedOutputData::writeRawLittleEndian64(int64_t value) {
176
- this->writeRawByte(static_cast<uint8_t>((value) &0xff));
177
- this->writeRawByte(static_cast<uint8_t>((value >> 8) & 0xff));
178
- this->writeRawByte(static_cast<uint8_t>((value >> 16) & 0xff));
179
- this->writeRawByte(static_cast<uint8_t>((value >> 24) & 0xff));
180
- this->writeRawByte(static_cast<uint8_t>((value >> 32) & 0xff));
181
- this->writeRawByte(static_cast<uint8_t>((value >> 40) & 0xff));
182
- this->writeRawByte(static_cast<uint8_t>((value >> 48) & 0xff));
183
- this->writeRawByte(static_cast<uint8_t>((value >> 56) & 0xff));
184
- }
185
-
186
- } // namespace mmkv