react-native-mmkv 3.3.0 → 4.0.0-beta.0

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 (87) 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/package.json +22 -23
  10. package/react-native-mmkv.podspec +3 -9
  11. package/react-native.config.js +9 -0
  12. package/MMKV/Core/CMakeLists.txt +0 -172
  13. package/MMKV/Core/CodedInputData.cpp +0 -252
  14. package/MMKV/Core/CodedInputData.h +0 -87
  15. package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
  16. package/MMKV/Core/CodedInputDataCrypt.h +0 -87
  17. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
  18. package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
  19. package/MMKV/Core/CodedOutputData.cpp +0 -186
  20. package/MMKV/Core/CodedOutputData.h +0 -88
  21. package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
  22. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  23. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  24. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
  25. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
  26. package/MMKV/Core/InterProcessLock.cpp +0 -186
  27. package/MMKV/Core/InterProcessLock.h +0 -119
  28. package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
  29. package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
  30. package/MMKV/Core/KeyValueHolder.cpp +0 -236
  31. package/MMKV/Core/KeyValueHolder.h +0 -122
  32. package/MMKV/Core/MMBuffer.cpp +0 -210
  33. package/MMKV/Core/MMBuffer.h +0 -111
  34. package/MMKV/Core/MMKV.cpp +0 -1702
  35. package/MMKV/Core/MMKV.h +0 -595
  36. package/MMKV/Core/MMKVLog.cpp +0 -127
  37. package/MMKV/Core/MMKVLog.h +0 -86
  38. package/MMKV/Core/MMKVLog_Android.cpp +0 -134
  39. package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
  40. package/MMKV/Core/MMKVPredef.h +0 -293
  41. package/MMKV/Core/MMKV_Android.cpp +0 -261
  42. package/MMKV/Core/MMKV_IO.cpp +0 -1905
  43. package/MMKV/Core/MMKV_IO.h +0 -57
  44. package/MMKV/Core/MMKV_OSX.cpp +0 -423
  45. package/MMKV/Core/MMKV_OSX.h +0 -57
  46. package/MMKV/Core/MemoryFile.cpp +0 -603
  47. package/MMKV/Core/MemoryFile.h +0 -194
  48. package/MMKV/Core/MemoryFile_Android.cpp +0 -236
  49. package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
  50. package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
  51. package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
  52. package/MMKV/Core/MiniPBCoder.cpp +0 -672
  53. package/MMKV/Core/MiniPBCoder.h +0 -151
  54. package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
  55. package/MMKV/Core/PBEncodeItem.hpp +0 -104
  56. package/MMKV/Core/PBUtility.cpp +0 -61
  57. package/MMKV/Core/PBUtility.h +0 -148
  58. package/MMKV/Core/ScopedLock.hpp +0 -69
  59. package/MMKV/Core/ThreadLock.cpp +0 -75
  60. package/MMKV/Core/ThreadLock.h +0 -81
  61. package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
  62. package/MMKV/Core/aes/AESCrypt.cpp +0 -273
  63. package/MMKV/Core/aes/AESCrypt.h +0 -112
  64. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
  65. package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
  66. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
  67. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
  68. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
  69. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
  70. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
  71. package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
  72. package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
  73. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
  74. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
  75. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
  76. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
  77. package/MMKV/Core/core.vcxproj +0 -288
  78. package/MMKV/Core/core.vcxproj.filters +0 -150
  79. package/MMKV/Core/crc32/Checksum.h +0 -75
  80. package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
  81. package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
  82. package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
  83. package/MMKV/Core/crc32/zlib/crc32.h +0 -48
  84. package/MMKV/Core/crc32/zlib/zconf.h +0 -380
  85. package/MMKV/Core/crc32/zlib/zutil.h +0 -25
  86. package/MMKV/LICENSE.TXT +0 -193
  87. package/MMKV/README.md +0 -354
@@ -1,252 +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 "CodedInputData.h"
22
- #include "PBUtility.h"
23
- #include <stdexcept>
24
- #include <cstring>
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
- CodedInputData::CodedInputData(const void *oData, size_t length)
37
- : m_ptr((uint8_t *) oData), m_size(length), m_position(0) {
38
- MMKV_ASSERT(m_ptr);
39
- }
40
-
41
- void CodedInputData::seek(size_t addedSize) {
42
- if (m_position + addedSize > m_size) {
43
- throw out_of_range("OutOfSpace");
44
- }
45
- m_position += addedSize;
46
- }
47
-
48
- double CodedInputData::readDouble() {
49
- return Int64ToFloat64(this->readRawLittleEndian64());
50
- }
51
-
52
- float CodedInputData::readFloat() {
53
- return Int32ToFloat32(this->readRawLittleEndian32());
54
- }
55
-
56
- int64_t CodedInputData::readInt64() {
57
- int32_t shift = 0;
58
- int64_t result = 0;
59
- while (shift < 64) {
60
- int8_t b = this->readRawByte();
61
- result |= (int64_t)(b & 0x7f) << shift;
62
- if ((b & 0x80) == 0) {
63
- return result;
64
- }
65
- shift += 7;
66
- }
67
- throw invalid_argument("InvalidProtocolBuffer malformedInt64");
68
- }
69
-
70
- uint64_t CodedInputData::readUInt64() {
71
- return static_cast<uint64_t>(readInt64());
72
- }
73
-
74
- int32_t CodedInputData::readInt32() {
75
- return this->readRawVarint32();
76
- }
77
-
78
- uint32_t CodedInputData::readUInt32() {
79
- return static_cast<uint32_t>(readRawVarint32());
80
- }
81
-
82
- bool CodedInputData::readBool() {
83
- return this->readRawVarint32() != 0;
84
- }
85
-
86
- #ifndef MMKV_APPLE
87
-
88
- string CodedInputData::readString() {
89
- int32_t size = readRawVarint32();
90
- if (size < 0) {
91
- throw length_error("InvalidProtocolBuffer negativeSize");
92
- }
93
-
94
- auto s_size = static_cast<size_t>(size);
95
- if (s_size <= m_size - m_position) {
96
- string result((char *) (m_ptr + m_position), s_size);
97
- m_position += s_size;
98
- return result;
99
- } else {
100
- throw out_of_range("InvalidProtocolBuffer truncatedMessage");
101
- }
102
- }
103
-
104
- void CodedInputData::readString(string &s) {
105
- int32_t size = readRawVarint32();
106
- if (size < 0) {
107
- throw length_error("InvalidProtocolBuffer negativeSize");
108
- }
109
-
110
- auto s_size = static_cast<size_t>(size);
111
- if (s_size <= m_size - m_position) {
112
- s.resize(s_size);
113
- memcpy((void *) s.data(), (char *) (m_ptr + m_position), s_size);
114
- m_position += s_size;
115
- } else {
116
- throw out_of_range("InvalidProtocolBuffer truncatedMessage");
117
- }
118
- }
119
-
120
- string CodedInputData::readString(KeyValueHolder &kvHolder) {
121
- kvHolder.offset = static_cast<uint32_t>(m_position);
122
-
123
- int32_t size = this->readRawVarint32();
124
- if (size < 0) {
125
- throw length_error("InvalidProtocolBuffer negativeSize");
126
- }
127
-
128
- auto s_size = static_cast<size_t>(size);
129
- if (s_size <= m_size - m_position) {
130
- kvHolder.keySize = static_cast<uint16_t>(s_size);
131
-
132
- auto ptr = m_ptr + m_position;
133
- string result((char *) (m_ptr + m_position), s_size);
134
- m_position += s_size;
135
- return result;
136
- } else {
137
- throw out_of_range("InvalidProtocolBuffer truncatedMessage");
138
- }
139
- }
140
-
141
- #endif
142
-
143
- MMBuffer CodedInputData::readRealData(mmkv::MMBuffer & data) {
144
- CodedInputData input(data.getPtr(), data.length());
145
- return input.readData(false, true);
146
- }
147
-
148
- MMBuffer CodedInputData::readData(bool copy, bool exactly) {
149
- int32_t size = this->readRawVarint32();
150
- if (size < 0) {
151
- throw length_error("InvalidProtocolBuffer negativeSize");
152
- }
153
-
154
- auto s_size = static_cast<size_t>(size);
155
- bool isSizeValid = exactly ? (s_size == m_size - m_position) : (s_size <= m_size - m_position);
156
- if (isSizeValid) {
157
- size_t pos = m_position;
158
- m_position += s_size;
159
- auto copyFlag = copy ? MMBufferCopy : MMBufferNoCopy;
160
- return MMBuffer(((int8_t *) m_ptr) + pos, s_size, copyFlag);
161
- } else {
162
- throw out_of_range("InvalidProtocolBuffer truncatedMessage");
163
- }
164
- }
165
-
166
- void CodedInputData::readData(KeyValueHolder &kvHolder) {
167
- int32_t size = this->readRawVarint32();
168
- if (size < 0) {
169
- throw length_error("InvalidProtocolBuffer negativeSize");
170
- }
171
-
172
- auto s_size = static_cast<size_t>(size);
173
- if (s_size <= m_size - m_position) {
174
- kvHolder.computedKVSize = static_cast<uint16_t>(m_position - kvHolder.offset);
175
- kvHolder.valueSize = static_cast<uint32_t>(s_size);
176
-
177
- m_position += s_size;
178
- } else {
179
- throw out_of_range("InvalidProtocolBuffer truncatedMessage");
180
- }
181
- }
182
-
183
- int32_t CodedInputData::readRawVarint32() {
184
- int8_t tmp = this->readRawByte();
185
- if (tmp >= 0) {
186
- return tmp;
187
- }
188
- int32_t result = tmp & 0x7f;
189
- if ((tmp = this->readRawByte()) >= 0) {
190
- result |= tmp << 7;
191
- } else {
192
- result |= (tmp & 0x7f) << 7;
193
- if ((tmp = this->readRawByte()) >= 0) {
194
- result |= tmp << 14;
195
- } else {
196
- result |= (tmp & 0x7f) << 14;
197
- if ((tmp = this->readRawByte()) >= 0) {
198
- result |= tmp << 21;
199
- } else {
200
- result |= (tmp & 0x7f) << 21;
201
- result |= (tmp = this->readRawByte()) << 28;
202
- if (tmp < 0) {
203
- // discard upper 32 bits
204
- for (int i = 0; i < 5; i++) {
205
- if (this->readRawByte() >= 0) {
206
- return result;
207
- }
208
- }
209
- throw invalid_argument("InvalidProtocolBuffer malformed varint32");
210
- }
211
- }
212
- }
213
- }
214
- return result;
215
- }
216
-
217
- int32_t CodedInputData::readRawLittleEndian32() {
218
- int8_t b1 = this->readRawByte();
219
- int8_t b2 = this->readRawByte();
220
- int8_t b3 = this->readRawByte();
221
- int8_t b4 = this->readRawByte();
222
- return (((int32_t) b1 & 0xff)) | (((int32_t) b2 & 0xff) << 8) | (((int32_t) b3 & 0xff) << 16) |
223
- (((int32_t) b4 & 0xff) << 24);
224
- }
225
-
226
- int64_t CodedInputData::readRawLittleEndian64() {
227
- int8_t b1 = this->readRawByte();
228
- int8_t b2 = this->readRawByte();
229
- int8_t b3 = this->readRawByte();
230
- int8_t b4 = this->readRawByte();
231
- int8_t b5 = this->readRawByte();
232
- int8_t b6 = this->readRawByte();
233
- int8_t b7 = this->readRawByte();
234
- int8_t b8 = this->readRawByte();
235
- return (((int64_t) b1 & 0xff)) | (((int64_t) b2 & 0xff) << 8) | (((int64_t) b3 & 0xff) << 16) |
236
- (((int64_t) b4 & 0xff) << 24) | (((int64_t) b5 & 0xff) << 32) | (((int64_t) b6 & 0xff) << 40) |
237
- (((int64_t) b7 & 0xff) << 48) | (((int64_t) b8 & 0xff) << 56);
238
- }
239
-
240
- int8_t CodedInputData::readRawByte() {
241
- if (m_position == m_size) {
242
- auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
243
- throw out_of_range(msg);
244
- }
245
- auto *bytes = (int8_t *) m_ptr;
246
- return bytes[m_position++];
247
- }
248
-
249
- #ifdef MMKV_APPLE
250
- #endif // MMKV_APPLE
251
-
252
- } // namespace mmkv
@@ -1,87 +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
- #ifndef MMKV_CODEDINPUTDATA_H
22
- #define MMKV_CODEDINPUTDATA_H
23
- #ifdef __cplusplus
24
-
25
- #include "MMKVPredef.h"
26
-
27
- #include "KeyValueHolder.h"
28
- #include "MMBuffer.h"
29
- #include <cstdint>
30
-
31
- namespace mmkv {
32
-
33
- class CodedInputData {
34
- uint8_t *const m_ptr;
35
- size_t m_size;
36
- size_t m_position;
37
-
38
- int8_t readRawByte();
39
-
40
- int32_t readRawVarint32();
41
-
42
- int32_t readRawLittleEndian32();
43
-
44
- int64_t readRawLittleEndian64();
45
-
46
- public:
47
- CodedInputData(const void *oData, size_t length);
48
-
49
- bool isAtEnd() const { return m_position == m_size; };
50
-
51
- void seek(size_t addedSize);
52
-
53
- bool readBool();
54
-
55
- double readDouble();
56
-
57
- float readFloat();
58
-
59
- int64_t readInt64();
60
-
61
- uint64_t readUInt64();
62
-
63
- int32_t readInt32();
64
-
65
- uint32_t readUInt32();
66
-
67
- // exactly is like getValueSize(actualSize = true)
68
- MMBuffer readData(bool copy = true, bool exactly = false);
69
- void readData(KeyValueHolder &kvHolder);
70
-
71
- static MMBuffer readRealData(mmkv::MMBuffer & data);
72
-
73
- #ifndef MMKV_APPLE
74
- std::string readString();
75
- void readString(std::string &s);
76
- std::string readString(KeyValueHolder &kvHolder);
77
- #else
78
- NSString *readString();
79
- NSString *readString(KeyValueHolder &kvHolder);
80
- NSData *readNSData();
81
- #endif
82
- };
83
-
84
- } // namespace mmkv
85
-
86
- #endif
87
- #endif //MMKV_CODEDINPUTDATA_H
@@ -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 */