react-native-mmkv 3.0.0-beta.6 → 3.0.0-beta.8
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.
- package/MMKV/Core/CMakeLists.txt +1 -1
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +4 -6
- package/MMKV/Core/KeyValueHolder.cpp +1 -1
- package/MMKV/Core/KeyValueHolder.h +5 -1
- package/MMKV/Core/MMBuffer.h +1 -1
- package/MMKV/Core/MMKV.cpp +70 -73
- package/MMKV/Core/MMKV.h +126 -11
- package/MMKV/Core/MMKVPredef.h +44 -3
- package/MMKV/Core/MMKV_IO.cpp +47 -33
- package/MMKV/Core/MMKV_OSX.cpp +16 -29
- package/MMKV/Core/MemoryFile_Android.cpp +27 -13
- package/MMKV/Core/MiniPBCoder.cpp +308 -9
- package/MMKV/Core/MiniPBCoder.h +36 -17
- package/MMKV/Core/MiniPBCoder_OSX.cpp +1 -0
- package/MMKV/Core/PBEncodeItem.hpp +18 -0
- package/MMKV/Core/PBUtility.h +0 -12
- package/MMKV/Core/ThreadLock.cpp +1 -3
- package/MMKV/Core/aes/AESCrypt.cpp +26 -6
- package/MMKV/Core/aes/AESCrypt.h +5 -1
- package/MMKV/Core/core.vcxproj +4 -4
- package/MMKV/Core/crc32/Checksum.h +4 -1
- package/android/CMakeLists.txt +0 -1
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +9 -0
- package/cpp/MmkvHostObject.cpp +35 -37
- package/ios/MmkvPlatformContextModule.mm +19 -1
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/commonjs/createMMKV.js +17 -0
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/module/createMMKV.js +17 -0
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/src/MMKV.d.ts +0 -1
- package/lib/typescript/src/MMKV.d.ts.map +1 -1
- package/lib/typescript/src/NativeMmkv.d.ts +3 -3
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +11 -0
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -1
- package/lib/typescript/src/Types.d.ts +4 -0
- package/lib/typescript/src/Types.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.d.ts +1 -1
- package/lib/typescript/src/createMMKV.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.web.d.ts +1 -1
- package/lib/typescript/src/createMMKV.web.d.ts.map +1 -1
- package/lib/typescript/src/hooks.d.ts +1 -1
- package/lib/typescript/src/hooks.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/MMKV.ts +0 -1
- package/src/NativeMmkv.ts +3 -3
- package/src/NativeMmkvPlatformContext.ts +11 -0
- package/src/Types.ts +4 -0
- package/src/createMMKV.ts +20 -2
- package/src/createMMKV.web.ts +1 -1
- package/src/hooks.ts +1 -1
- package/android/src/main/cpp/cpp-adapter.cpp +0 -7
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
#include "CodedInputDataCrypt.h"
|
|
24
24
|
#include "CodedOutputData.h"
|
|
25
25
|
#include "PBEncodeItem.hpp"
|
|
26
|
+
#include "PBUtility.h"
|
|
27
|
+
#include "MMKVLog.h"
|
|
26
28
|
|
|
27
29
|
#ifdef MMKV_APPLE
|
|
28
30
|
# if __has_feature(objc_arc)
|
|
@@ -76,6 +78,24 @@ void MiniPBCoder::writeRootObject() {
|
|
|
76
78
|
break;
|
|
77
79
|
}
|
|
78
80
|
#ifndef MMKV_APPLE
|
|
81
|
+
#ifdef MMKV_HAS_CPP20
|
|
82
|
+
case PBEncodeItemType_Int32: {
|
|
83
|
+
m_outputData->writeInt32(encodeItem->value.int32Value);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case PBEncodeItemType_UInt32: {
|
|
87
|
+
m_outputData->writeUInt32(encodeItem->value.uint32Value);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case PBEncodeItemType_Int64: {
|
|
91
|
+
m_outputData->writeInt64(encodeItem->value.int64Value);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case PBEncodeItemType_UInt64: {
|
|
95
|
+
m_outputData->writeUInt64(encodeItem->value.uint64Value);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
#endif // MMKV_HAS_CPP20
|
|
79
99
|
case PBEncodeItemType_String: {
|
|
80
100
|
m_outputData->writeString(*(encodeItem->value.strValue));
|
|
81
101
|
break;
|
|
@@ -166,15 +186,23 @@ size_t MiniPBCoder::prepareObjectForEncode(const MMKVVector &vec) {
|
|
|
166
186
|
}
|
|
167
187
|
|
|
168
188
|
MMBuffer MiniPBCoder::writePreparedItems(size_t index) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
189
|
+
try {
|
|
190
|
+
PBEncodeItem *oItem = (index < m_encodeItems->size()) ? &(*m_encodeItems)[index] : nullptr;
|
|
191
|
+
if (oItem && oItem->compiledSize > 0) {
|
|
192
|
+
m_outputBuffer = new MMBuffer(oItem->compiledSize);
|
|
193
|
+
m_outputData = new CodedOutputData(m_outputBuffer->getPtr(), m_outputBuffer->length());
|
|
173
194
|
|
|
174
|
-
|
|
175
|
-
|
|
195
|
+
writeRootObject();
|
|
196
|
+
}
|
|
176
197
|
|
|
177
|
-
|
|
198
|
+
return std::move(*m_outputBuffer);
|
|
199
|
+
} catch (const std::exception &exception) {
|
|
200
|
+
MMKVError("%s", exception.what());
|
|
201
|
+
return MMBuffer();
|
|
202
|
+
} catch (...) {
|
|
203
|
+
MMKVError("encode fail");
|
|
204
|
+
return MMBuffer();
|
|
205
|
+
}
|
|
178
206
|
}
|
|
179
207
|
|
|
180
208
|
MMBuffer MiniPBCoder::encodeDataWithObject(const MMBuffer &obj) {
|
|
@@ -210,7 +238,7 @@ size_t MiniPBCoder::prepareObjectForEncode(const string &str) {
|
|
|
210
238
|
return index;
|
|
211
239
|
}
|
|
212
240
|
|
|
213
|
-
size_t MiniPBCoder::prepareObjectForEncode(const
|
|
241
|
+
size_t MiniPBCoder::prepareObjectForEncode(const MMKV_STRING_CONTAINER &v) {
|
|
214
242
|
m_encodeItems->push_back(PBEncodeItem());
|
|
215
243
|
PBEncodeItem *encodeItem = &(m_encodeItems->back());
|
|
216
244
|
size_t index = m_encodeItems->size() - 1;
|
|
@@ -232,6 +260,110 @@ size_t MiniPBCoder::prepareObjectForEncode(const vector<string> &v) {
|
|
|
232
260
|
return index;
|
|
233
261
|
}
|
|
234
262
|
|
|
263
|
+
#ifdef MMKV_HAS_CPP20
|
|
264
|
+
|
|
265
|
+
size_t MiniPBCoder::prepareObjectForEncode(const std::span<const int32_t> &vec) {
|
|
266
|
+
m_encodeItems->push_back(PBEncodeItem());
|
|
267
|
+
PBEncodeItem *encodeItem = &(m_encodeItems->back());
|
|
268
|
+
size_t index = m_encodeItems->size() - 1;
|
|
269
|
+
{
|
|
270
|
+
encodeItem->type = PBEncodeItemType_Container;
|
|
271
|
+
encodeItem->value.bufferValue = nullptr;
|
|
272
|
+
|
|
273
|
+
for (const auto value : vec) {
|
|
274
|
+
PBEncodeItem item;
|
|
275
|
+
item.type = PBEncodeItemType_Int32;
|
|
276
|
+
item.value.int32Value = value;
|
|
277
|
+
item.compiledSize = pbInt32Size(value);
|
|
278
|
+
|
|
279
|
+
(*m_encodeItems)[index].valueSize += item.compiledSize;
|
|
280
|
+
m_encodeItems->push_back(std::move(item));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
encodeItem = &(*m_encodeItems)[index];
|
|
284
|
+
}
|
|
285
|
+
encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
|
|
286
|
+
|
|
287
|
+
return index;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
size_t MiniPBCoder::prepareObjectForEncode(const std::span<const uint32_t> &vec) {
|
|
291
|
+
m_encodeItems->push_back(PBEncodeItem());
|
|
292
|
+
PBEncodeItem *encodeItem = &(m_encodeItems->back());
|
|
293
|
+
size_t index = m_encodeItems->size() - 1;
|
|
294
|
+
{
|
|
295
|
+
encodeItem->type = PBEncodeItemType_Container;
|
|
296
|
+
encodeItem->value.bufferValue = nullptr;
|
|
297
|
+
|
|
298
|
+
for (const auto value : vec) {
|
|
299
|
+
PBEncodeItem item;
|
|
300
|
+
item.type = PBEncodeItemType_UInt32;
|
|
301
|
+
item.value.uint32Value = value;
|
|
302
|
+
item.compiledSize = pbUInt32Size(value);
|
|
303
|
+
|
|
304
|
+
(*m_encodeItems)[index].valueSize += item.compiledSize;
|
|
305
|
+
m_encodeItems->push_back(std::move(item));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
encodeItem = &(*m_encodeItems)[index];
|
|
309
|
+
}
|
|
310
|
+
encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
|
|
311
|
+
|
|
312
|
+
return index;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
size_t MiniPBCoder::prepareObjectForEncode(const std::span<const int64_t> &vec) {
|
|
316
|
+
m_encodeItems->push_back(PBEncodeItem());
|
|
317
|
+
PBEncodeItem *encodeItem = &(m_encodeItems->back());
|
|
318
|
+
size_t index = m_encodeItems->size() - 1;
|
|
319
|
+
{
|
|
320
|
+
encodeItem->type = PBEncodeItemType_Container;
|
|
321
|
+
encodeItem->value.bufferValue = nullptr;
|
|
322
|
+
|
|
323
|
+
for (const auto value : vec) {
|
|
324
|
+
PBEncodeItem item;
|
|
325
|
+
item.type = PBEncodeItemType_Int64;
|
|
326
|
+
item.value.int64Value = value;
|
|
327
|
+
item.compiledSize = pbInt64Size(value);
|
|
328
|
+
|
|
329
|
+
(*m_encodeItems)[index].valueSize += item.compiledSize;
|
|
330
|
+
m_encodeItems->push_back(std::move(item));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
encodeItem = &(*m_encodeItems)[index];
|
|
334
|
+
}
|
|
335
|
+
encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
|
|
336
|
+
|
|
337
|
+
return index;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
size_t MiniPBCoder::prepareObjectForEncode(const std::span<const uint64_t> &vec) {
|
|
341
|
+
m_encodeItems->push_back(PBEncodeItem());
|
|
342
|
+
PBEncodeItem *encodeItem = &(m_encodeItems->back());
|
|
343
|
+
size_t index = m_encodeItems->size() - 1;
|
|
344
|
+
{
|
|
345
|
+
encodeItem->type = PBEncodeItemType_Container;
|
|
346
|
+
encodeItem->value.bufferValue = nullptr;
|
|
347
|
+
|
|
348
|
+
for (const auto value : vec) {
|
|
349
|
+
PBEncodeItem item;
|
|
350
|
+
item.type = PBEncodeItemType_UInt64;
|
|
351
|
+
item.value.uint64Value = value;
|
|
352
|
+
item.compiledSize = pbUInt64Size(value);
|
|
353
|
+
|
|
354
|
+
(*m_encodeItems)[index].valueSize += item.compiledSize;
|
|
355
|
+
m_encodeItems->push_back(std::move(item));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
encodeItem = &(*m_encodeItems)[index];
|
|
359
|
+
}
|
|
360
|
+
encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
|
|
361
|
+
|
|
362
|
+
return index;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
#endif // MMKV_HAS_CPP20
|
|
366
|
+
|
|
235
367
|
vector<string> MiniPBCoder::decodeOneVector() {
|
|
236
368
|
vector<string> v;
|
|
237
369
|
|
|
@@ -245,6 +377,132 @@ vector<string> MiniPBCoder::decodeOneVector() {
|
|
|
245
377
|
return v;
|
|
246
378
|
}
|
|
247
379
|
|
|
380
|
+
#ifdef MMKV_HAS_CPP20
|
|
381
|
+
|
|
382
|
+
bool MiniPBCoder::decodeOneVector(std::vector<bool> &result) {
|
|
383
|
+
try {
|
|
384
|
+
auto size = m_inputData->readInt32();
|
|
385
|
+
result.reserve(size / pbBoolSize());
|
|
386
|
+
|
|
387
|
+
while (!m_inputData->isAtEnd()) {
|
|
388
|
+
auto value = m_inputData->readBool();
|
|
389
|
+
result.push_back(value);
|
|
390
|
+
}
|
|
391
|
+
return true;
|
|
392
|
+
} catch (std::exception &exception) {
|
|
393
|
+
MMKVError("%s", exception.what());
|
|
394
|
+
} catch (...) {
|
|
395
|
+
MMKVError("decode fail");
|
|
396
|
+
}
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
bool MiniPBCoder::decodeOneVector(std::vector<int32_t> &result) {
|
|
401
|
+
try {
|
|
402
|
+
m_inputData->readInt32();
|
|
403
|
+
|
|
404
|
+
while (!m_inputData->isAtEnd()) {
|
|
405
|
+
auto value = m_inputData->readInt32();
|
|
406
|
+
result.push_back(value);
|
|
407
|
+
}
|
|
408
|
+
return true;
|
|
409
|
+
} catch (std::exception &exception) {
|
|
410
|
+
MMKVError("%s", exception.what());
|
|
411
|
+
} catch (...) {
|
|
412
|
+
MMKVError("decode fail");
|
|
413
|
+
}
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
bool MiniPBCoder::decodeOneVector(std::vector<uint32_t> &result) {
|
|
418
|
+
try {
|
|
419
|
+
m_inputData->readInt32();
|
|
420
|
+
|
|
421
|
+
while (!m_inputData->isAtEnd()) {
|
|
422
|
+
auto value = m_inputData->readUInt32();
|
|
423
|
+
result.push_back(value);
|
|
424
|
+
}
|
|
425
|
+
return true;
|
|
426
|
+
} catch (std::exception &exception) {
|
|
427
|
+
MMKVError("%s", exception.what());
|
|
428
|
+
} catch (...) {
|
|
429
|
+
MMKVError("decode fail");
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
bool MiniPBCoder::decodeOneVector(std::vector<int64_t> &result) {
|
|
435
|
+
try {
|
|
436
|
+
m_inputData->readInt32();
|
|
437
|
+
|
|
438
|
+
while (!m_inputData->isAtEnd()) {
|
|
439
|
+
auto value = m_inputData->readInt64();
|
|
440
|
+
result.push_back(value);
|
|
441
|
+
}
|
|
442
|
+
return true;
|
|
443
|
+
} catch (std::exception &exception) {
|
|
444
|
+
MMKVError("%s", exception.what());
|
|
445
|
+
} catch (...) {
|
|
446
|
+
MMKVError("decode fail");
|
|
447
|
+
}
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
bool MiniPBCoder::decodeOneVector(std::vector<uint64_t> &result) {
|
|
452
|
+
try {
|
|
453
|
+
m_inputData->readInt32();
|
|
454
|
+
|
|
455
|
+
while (!m_inputData->isAtEnd()) {
|
|
456
|
+
auto value = m_inputData->readUInt64();
|
|
457
|
+
result.push_back(value);
|
|
458
|
+
}
|
|
459
|
+
return true;
|
|
460
|
+
} catch (std::exception &exception) {
|
|
461
|
+
MMKVError("%s", exception.what());
|
|
462
|
+
} catch (...) {
|
|
463
|
+
MMKVError("decode fail");
|
|
464
|
+
}
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
bool MiniPBCoder::decodeOneVector(std::vector<float> &result) {
|
|
469
|
+
try {
|
|
470
|
+
auto size = m_inputData->readInt32();
|
|
471
|
+
result.reserve(size / pbFloatSize());
|
|
472
|
+
|
|
473
|
+
while (!m_inputData->isAtEnd()) {
|
|
474
|
+
auto value = m_inputData->readFloat();
|
|
475
|
+
result.push_back(value);
|
|
476
|
+
}
|
|
477
|
+
return true;
|
|
478
|
+
} catch (std::exception &exception) {
|
|
479
|
+
MMKVError("%s", exception.what());
|
|
480
|
+
} catch (...) {
|
|
481
|
+
MMKVError("decode fail");
|
|
482
|
+
}
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
bool MiniPBCoder::decodeOneVector(std::vector<double> &result) {
|
|
487
|
+
try {
|
|
488
|
+
auto size = m_inputData->readInt32();
|
|
489
|
+
result.reserve(size / pbDoubleSize());
|
|
490
|
+
|
|
491
|
+
while (!m_inputData->isAtEnd()) {
|
|
492
|
+
auto value = m_inputData->readDouble();
|
|
493
|
+
result.push_back(value);
|
|
494
|
+
}
|
|
495
|
+
return true;
|
|
496
|
+
} catch (std::exception &exception) {
|
|
497
|
+
MMKVError("%s", exception.what());
|
|
498
|
+
} catch (...) {
|
|
499
|
+
MMKVError("decode fail");
|
|
500
|
+
}
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
#endif // MMKV_HAS_CPP20
|
|
505
|
+
|
|
248
506
|
void MiniPBCoder::decodeOneMap(MMKVMap &dic, size_t position, bool greedy) {
|
|
249
507
|
auto block = [position, this](MMKVMap &dictionary) {
|
|
250
508
|
if (position) {
|
|
@@ -344,7 +602,48 @@ vector<string> MiniPBCoder::decodeVector(const MMBuffer &oData) {
|
|
|
344
602
|
return oCoder.decodeOneVector();
|
|
345
603
|
}
|
|
346
604
|
|
|
347
|
-
#
|
|
605
|
+
#ifdef MMKV_HAS_CPP20
|
|
606
|
+
MMBuffer MiniPBCoder::getEncodeData(const std::vector<bool> &value) {
|
|
607
|
+
auto valueLength = static_cast<uint32_t>(value.size() * pbBoolSize());
|
|
608
|
+
auto size = pbRawVarint32Size(valueLength) + valueLength;
|
|
609
|
+
auto buffer = MMBuffer(size);
|
|
610
|
+
CodedOutputData output(buffer.getPtr(), size);
|
|
611
|
+
output.writeUInt32(valueLength);
|
|
612
|
+
|
|
613
|
+
for (auto single : value) {
|
|
614
|
+
output.writeBool(single);
|
|
615
|
+
}
|
|
616
|
+
return buffer;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
MMBuffer MiniPBCoder::getEncodeData(const std::span<const float> &value) {
|
|
620
|
+
auto valueLength = static_cast<uint32_t>(value.size() * pbFloatSize());
|
|
621
|
+
auto size = pbRawVarint32Size(valueLength) + valueLength;
|
|
622
|
+
auto buffer = MMBuffer(size);
|
|
623
|
+
CodedOutputData output(buffer.getPtr(), size);
|
|
624
|
+
output.writeUInt32(valueLength);
|
|
625
|
+
|
|
626
|
+
for (auto single : value) {
|
|
627
|
+
output.writeFloat(single);
|
|
628
|
+
}
|
|
629
|
+
return buffer;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
MMBuffer MiniPBCoder::getEncodeData(const std::span<const double> &value) {
|
|
633
|
+
auto valueLength = static_cast<uint32_t>(value.size() * pbDoubleSize());
|
|
634
|
+
auto size = pbRawVarint32Size(valueLength) + valueLength;
|
|
635
|
+
auto buffer = MMBuffer(size);
|
|
636
|
+
CodedOutputData output(buffer.getPtr(), size);
|
|
637
|
+
output.writeUInt32(valueLength);
|
|
638
|
+
|
|
639
|
+
for (auto single : value) {
|
|
640
|
+
output.writeDouble(single);
|
|
641
|
+
}
|
|
642
|
+
return buffer;
|
|
643
|
+
}
|
|
644
|
+
#endif // MMKV_HAS_CPP20
|
|
645
|
+
|
|
646
|
+
#endif // !MMKV_APPLE
|
|
348
647
|
|
|
349
648
|
void MiniPBCoder::decodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position) {
|
|
350
649
|
MiniPBCoder oCoder(&oData);
|
package/MMKV/Core/MiniPBCoder.h
CHANGED
|
@@ -24,12 +24,15 @@
|
|
|
24
24
|
|
|
25
25
|
#include "MMKVPredef.h"
|
|
26
26
|
|
|
27
|
-
#include "KeyValueHolder.h"
|
|
28
27
|
#include "MMBuffer.h"
|
|
29
|
-
#include "MMBuffer.h"
|
|
30
|
-
#include "MMKVLog.h"
|
|
31
|
-
#include "PBUtility.h"
|
|
32
28
|
#include <cstdint>
|
|
29
|
+
#ifdef MMKV_HAS_CPP20
|
|
30
|
+
# include <span>
|
|
31
|
+
# define MMKV_STRING_CONTAINER std::span<const std::string>
|
|
32
|
+
#else
|
|
33
|
+
# define MMKV_STRING_CONTAINER std::vector<std::string>
|
|
34
|
+
#endif
|
|
35
|
+
|
|
33
36
|
|
|
34
37
|
namespace mmkv {
|
|
35
38
|
|
|
@@ -72,27 +75,37 @@ class MiniPBCoder {
|
|
|
72
75
|
|
|
73
76
|
#ifndef MMKV_APPLE
|
|
74
77
|
size_t prepareObjectForEncode(const std::string &str);
|
|
75
|
-
size_t prepareObjectForEncode(const
|
|
76
|
-
|
|
78
|
+
size_t prepareObjectForEncode(const MMKV_STRING_CONTAINER &vector);
|
|
77
79
|
std::vector<std::string> decodeOneVector();
|
|
80
|
+
#ifdef MMKV_HAS_CPP20
|
|
81
|
+
size_t prepareObjectForEncode(const std::span<const int32_t> &vec);
|
|
82
|
+
size_t prepareObjectForEncode(const std::span<const uint32_t> &vec);
|
|
83
|
+
size_t prepareObjectForEncode(const std::span<const int64_t> &vec);
|
|
84
|
+
size_t prepareObjectForEncode(const std::span<const uint64_t> &vec);
|
|
85
|
+
|
|
86
|
+
bool decodeOneVector(std::vector<bool> &result);
|
|
87
|
+
bool decodeOneVector(std::vector<int32_t> &result);
|
|
88
|
+
bool decodeOneVector(std::vector<uint32_t> &result);
|
|
89
|
+
bool decodeOneVector(std::vector<int64_t> &result);
|
|
90
|
+
bool decodeOneVector(std::vector<uint64_t> &result);
|
|
91
|
+
bool decodeOneVector(std::vector<float> &result);
|
|
92
|
+
bool decodeOneVector(std::vector<double> &result);
|
|
93
|
+
|
|
94
|
+
// special case for fixed size types
|
|
95
|
+
MMBuffer getEncodeData(const std::vector<bool> &obj);
|
|
96
|
+
MMBuffer getEncodeData(const std::span<const float> &obj);
|
|
97
|
+
MMBuffer getEncodeData(const std::span<const double> &obj);
|
|
98
|
+
#endif // MMKV_HAS_CPP20
|
|
78
99
|
#else
|
|
79
100
|
// NSString, NSData, NSDate
|
|
80
101
|
size_t prepareObjectForEncode(__unsafe_unretained NSObject *obj);
|
|
81
|
-
#endif
|
|
102
|
+
#endif // MMKV_APPLE
|
|
82
103
|
|
|
83
104
|
public:
|
|
84
105
|
template <typename T>
|
|
85
106
|
static MMBuffer encodeDataWithObject(const T &obj) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return pbcoder.getEncodeData(obj);
|
|
89
|
-
} catch (const std::exception &exception) {
|
|
90
|
-
MMKVError("%s", exception.what());
|
|
91
|
-
return MMBuffer();
|
|
92
|
-
} catch (...) {
|
|
93
|
-
MMKVError("encode fail");
|
|
94
|
-
return MMBuffer();
|
|
95
|
-
}
|
|
107
|
+
MiniPBCoder pbCoder;
|
|
108
|
+
return pbCoder.getEncodeData(obj);
|
|
96
109
|
}
|
|
97
110
|
|
|
98
111
|
// opt encoding a single MMBuffer
|
|
@@ -114,6 +127,12 @@ public:
|
|
|
114
127
|
|
|
115
128
|
#ifndef MMKV_APPLE
|
|
116
129
|
static std::vector<std::string> decodeVector(const MMBuffer &oData);
|
|
130
|
+
|
|
131
|
+
template <typename T>
|
|
132
|
+
static bool decodeVector(const MMBuffer &oData, std::vector<T> &result) {
|
|
133
|
+
MiniPBCoder oCoder(&oData);
|
|
134
|
+
return oCoder.decodeOneVector(result);
|
|
135
|
+
}
|
|
117
136
|
#else
|
|
118
137
|
// NSString, NSData, NSDate
|
|
119
138
|
static NSObject *decodeObject(const MMBuffer &oData, Class cls);
|
|
@@ -36,6 +36,15 @@ enum PBEncodeItemType {
|
|
|
36
36
|
PBEncodeItemType_Container,
|
|
37
37
|
#ifndef MMKV_APPLE
|
|
38
38
|
PBEncodeItemType_String,
|
|
39
|
+
#ifdef MMKV_HAS_CPP20
|
|
40
|
+
PBEncodeItemType_Int32,
|
|
41
|
+
PBEncodeItemType_UInt32,
|
|
42
|
+
PBEncodeItemType_Int64,
|
|
43
|
+
PBEncodeItemType_UInt64,
|
|
44
|
+
// PBEncodeItemType_Bool,
|
|
45
|
+
// PBEncodeItemType_Float,
|
|
46
|
+
// PBEncodeItemType_Double,
|
|
47
|
+
#endif // MMKV_HAS_CPP20
|
|
39
48
|
#else
|
|
40
49
|
PBEncodeItemType_NSString,
|
|
41
50
|
PBEncodeItemType_NSData,
|
|
@@ -50,6 +59,15 @@ struct PBEncodeItem {
|
|
|
50
59
|
union {
|
|
51
60
|
const MMBuffer *bufferValue;
|
|
52
61
|
#ifndef MMKV_APPLE
|
|
62
|
+
#ifdef MMKV_HAS_CPP20
|
|
63
|
+
// bool boolValue;
|
|
64
|
+
int32_t int32Value;
|
|
65
|
+
int64_t int64Value;
|
|
66
|
+
uint32_t uint32Value;
|
|
67
|
+
uint64_t uint64Value;
|
|
68
|
+
#endif // MMKV_HAS_CPP20
|
|
69
|
+
// float floatValue;
|
|
70
|
+
// double doubleValue;
|
|
53
71
|
const std::string *strValue;
|
|
54
72
|
#else
|
|
55
73
|
void *objectValue;
|
package/MMKV/Core/PBUtility.h
CHANGED
|
@@ -26,18 +26,6 @@
|
|
|
26
26
|
|
|
27
27
|
#include <cstdint>
|
|
28
28
|
|
|
29
|
-
#ifndef MMKV_WIN32
|
|
30
|
-
# ifndef likely
|
|
31
|
-
# define unlikely(x) (__builtin_expect(bool(x), 0))
|
|
32
|
-
# define likely(x) (__builtin_expect(bool(x), 1))
|
|
33
|
-
# endif
|
|
34
|
-
#else
|
|
35
|
-
# ifndef likely
|
|
36
|
-
# define unlikely(x) (x)
|
|
37
|
-
# define likely(x) (x)
|
|
38
|
-
# endif
|
|
39
|
-
#endif
|
|
40
|
-
|
|
41
29
|
namespace mmkv {
|
|
42
30
|
|
|
43
31
|
template <typename T, typename P>
|
package/MMKV/Core/ThreadLock.cpp
CHANGED
|
@@ -38,10 +38,8 @@ ThreadLock::ThreadLock() : m_lock({}) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
ThreadLock::~ThreadLock() {
|
|
41
|
-
#ifdef MMKV_OHOS
|
|
42
|
-
// OHOS's pthread is buggy, check for POC: https://gist.github.com/lingol/622af352e090e0490ebacfe3a38b9221
|
|
43
41
|
pthread_mutex_unlock(&m_lock);
|
|
44
|
-
|
|
42
|
+
|
|
45
43
|
pthread_mutex_destroy(&m_lock);
|
|
46
44
|
}
|
|
47
45
|
|
|
@@ -25,12 +25,25 @@
|
|
|
25
25
|
#include <cstring>
|
|
26
26
|
#include <ctime>
|
|
27
27
|
|
|
28
|
+
namespace mmkv {
|
|
29
|
+
|
|
30
|
+
// assuming size in [1, 5]
|
|
31
|
+
uint32_t AESCrypt::randomItemSizeHolder(uint32_t size) {
|
|
32
|
+
constexpr uint32_t ItemSizeHolders[] = {0, 0x80, 0x4000, 0x200000, 0x10000000, 0};
|
|
33
|
+
auto ItemSizeHolderMin = ItemSizeHolders[size - 1];
|
|
34
|
+
auto ItemSizeHolderMax = ItemSizeHolders[size] - 1;
|
|
35
|
+
|
|
36
|
+
srand((unsigned) time(nullptr));
|
|
37
|
+
auto result = static_cast<uint32_t>(rand());
|
|
38
|
+
result = result % (ItemSizeHolderMax - ItemSizeHolderMin + 1);
|
|
39
|
+
result += ItemSizeHolderMin;
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
28
43
|
#ifndef MMKV_DISABLE_CRYPT
|
|
29
44
|
|
|
30
45
|
using namespace openssl;
|
|
31
46
|
|
|
32
|
-
namespace mmkv {
|
|
33
|
-
|
|
34
47
|
AESCrypt::AESCrypt(const void *key, size_t keyLength, const void *iv, size_t ivLength) {
|
|
35
48
|
if (key && keyLength > 0) {
|
|
36
49
|
memcpy(m_key, key, (keyLength > AES_KEY_LEN) ? AES_KEY_LEN : keyLength);
|
|
@@ -161,8 +174,6 @@ AESCrypt AESCrypt::cloneWithStatus(const AESCryptStatus &status) const {
|
|
|
161
174
|
return AESCrypt(*this, status);
|
|
162
175
|
}
|
|
163
176
|
|
|
164
|
-
} // namespace mmkv
|
|
165
|
-
|
|
166
177
|
# ifdef MMKV_DEBUG
|
|
167
178
|
|
|
168
179
|
# include "../MMKVLog.h"
|
|
@@ -170,8 +181,17 @@ AESCrypt AESCrypt::cloneWithStatus(const AESCryptStatus &status) const {
|
|
|
170
181
|
|
|
171
182
|
namespace mmkv {
|
|
172
183
|
|
|
184
|
+
void testRandomPlaceHolder() {
|
|
185
|
+
for (uint32_t size = 1; size < 6; size++) {
|
|
186
|
+
auto holder = AESCrypt::randomItemSizeHolder(size);
|
|
187
|
+
MMKVInfo("holder 0x%x for size %u", holder, size);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
173
191
|
// check if AESCrypt is encrypt-decrypt full-duplex
|
|
174
192
|
void AESCrypt::testAESCrypt() {
|
|
193
|
+
testRandomPlaceHolder();
|
|
194
|
+
|
|
175
195
|
const uint8_t plainText[] = "Hello, OpenSSL-mmkv::AESCrypt::testAESCrypt() with AES CFB 128.";
|
|
176
196
|
constexpr size_t textLength = sizeof(plainText) - 1;
|
|
177
197
|
|
|
@@ -250,7 +270,7 @@ void AESCrypt::testAESCrypt() {
|
|
|
250
270
|
delete[] decryptText;
|
|
251
271
|
}
|
|
252
272
|
|
|
253
|
-
} // namespace mmkv
|
|
254
|
-
|
|
255
273
|
# endif // MMKV_DEBUG
|
|
256
274
|
#endif // MMKV_DISABLE_CRYPT
|
|
275
|
+
|
|
276
|
+
} // namespace mmkv
|
package/MMKV/Core/aes/AESCrypt.h
CHANGED
|
@@ -29,7 +29,10 @@
|
|
|
29
29
|
#ifdef MMKV_DISABLE_CRYPT
|
|
30
30
|
|
|
31
31
|
namespace mmkv {
|
|
32
|
-
class AESCrypt
|
|
32
|
+
class AESCrypt {
|
|
33
|
+
public:
|
|
34
|
+
static uint32_t randomItemSizeHolder(uint32_t size);
|
|
35
|
+
};
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
#else
|
|
@@ -88,6 +91,7 @@ public:
|
|
|
88
91
|
void getKey(void *output) const;
|
|
89
92
|
|
|
90
93
|
static void fillRandomIV(void *vector);
|
|
94
|
+
static uint32_t randomItemSizeHolder(uint32_t size);
|
|
91
95
|
|
|
92
96
|
// just forbid it for possibly misuse
|
|
93
97
|
explicit AESCrypt(const AESCrypt &other) = delete;
|
package/MMKV/Core/core.vcxproj
CHANGED
|
@@ -156,7 +156,7 @@
|
|
|
156
156
|
<AdditionalOptions>%(AdditionalOptions) /machine:X86</AdditionalOptions>
|
|
157
157
|
</Lib>
|
|
158
158
|
<PostBuildEvent>
|
|
159
|
-
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
159
|
+
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h", "$(ProjectDir)MiniPBCoder.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
160
160
|
</PostBuildEvent>
|
|
161
161
|
<PostBuildEvent>
|
|
162
162
|
<Message>Copying Headers</Message>
|
|
@@ -196,7 +196,7 @@
|
|
|
196
196
|
<AdditionalOptions>%(AdditionalOptions)</AdditionalOptions>
|
|
197
197
|
</Lib>
|
|
198
198
|
<PostBuildEvent>
|
|
199
|
-
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
199
|
+
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h", "$(ProjectDir)MiniPBCoder.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
200
200
|
</PostBuildEvent>
|
|
201
201
|
<PostBuildEvent>
|
|
202
202
|
<Message>Copying Headers</Message>
|
|
@@ -236,7 +236,7 @@
|
|
|
236
236
|
<AdditionalOptions>%(AdditionalOptions) /machine:X86</AdditionalOptions>
|
|
237
237
|
</Lib>
|
|
238
238
|
<PostBuildEvent>
|
|
239
|
-
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
239
|
+
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h", "$(ProjectDir)MiniPBCoder.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
240
240
|
</PostBuildEvent>
|
|
241
241
|
<PostBuildEvent>
|
|
242
242
|
<Message>Copying Headers</Message>
|
|
@@ -276,7 +276,7 @@
|
|
|
276
276
|
<AdditionalOptions>%(AdditionalOptions)</AdditionalOptions>
|
|
277
277
|
</Lib>
|
|
278
278
|
<PostBuildEvent>
|
|
279
|
-
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
279
|
+
<Command>for %%f in ("$(ProjectDir)MMKV.h", "$(ProjectDir)MMBuffer.h", "$(ProjectDir)MMKVPredef.h", "$(ProjectDir)MiniPBCoder.h") do xcopy /y /i %%f "$(OutDir)\include\MMKV\"</Command>
|
|
280
280
|
</PostBuildEvent>
|
|
281
281
|
<PostBuildEvent>
|
|
282
282
|
<Message>Copying Headers</Message>
|
|
@@ -39,7 +39,10 @@ uLong crc32(uLong crc, const Bytef *buf, z_size_t len);
|
|
|
39
39
|
#else // MMKV_EMBED_ZLIB
|
|
40
40
|
|
|
41
41
|
# include <zlib.h>
|
|
42
|
-
|
|
42
|
+
// some old version of zlib doesn't define z_size_t
|
|
43
|
+
# ifndef z_size_t
|
|
44
|
+
typedef size_t z_size_t;
|
|
45
|
+
# endif
|
|
43
46
|
# define ZLIB_CRC32(crc, buf, len) ::crc32(crc, buf, static_cast<uInt>(len))
|
|
44
47
|
|
|
45
48
|
#endif // MMKV_EMBED_ZLIB
|
package/android/CMakeLists.txt
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
package com.mrousavy.mmkv;
|
|
2
2
|
|
|
3
|
+
import androidx.annotation.Nullable;
|
|
4
|
+
|
|
3
5
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
4
6
|
|
|
5
7
|
public class MmkvPlatformContextModule extends NativeMmkvPlatformContextSpec {
|
|
@@ -14,4 +16,11 @@ public class MmkvPlatformContextModule extends NativeMmkvPlatformContextSpec {
|
|
|
14
16
|
public String getBaseDirectory() {
|
|
15
17
|
return context.getFilesDir().getAbsolutePath() + "/mmkv";
|
|
16
18
|
}
|
|
19
|
+
|
|
20
|
+
@Nullable
|
|
21
|
+
@Override
|
|
22
|
+
public String getAppGroupDirectory() {
|
|
23
|
+
// AppGroups do not exist on Android. It's iOS only.
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
17
26
|
}
|