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,69 +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_SCOPEDLOCK_HPP
22
- #define MMKV_SCOPEDLOCK_HPP
23
- #ifdef __cplusplus
24
-
25
- namespace mmkv {
26
-
27
- template <typename T>
28
- class ScopedLock {
29
- T *m_lock;
30
-
31
- void lock() {
32
- if (m_lock) {
33
- m_lock->lock();
34
- }
35
- }
36
-
37
- void unlock() {
38
- if (m_lock) {
39
- m_lock->unlock();
40
- }
41
- }
42
-
43
- public:
44
- explicit ScopedLock(T *oLock) : m_lock(oLock) {
45
- MMKV_ASSERT(m_lock);
46
- lock();
47
- }
48
-
49
- ~ScopedLock() {
50
- unlock();
51
- m_lock = nullptr;
52
- }
53
-
54
- // just forbid it for possibly misuse
55
- explicit ScopedLock(const ScopedLock<T> &other) = delete;
56
- ScopedLock &operator=(const ScopedLock<T> &other) = delete;
57
- };
58
-
59
- } // namespace mmkv
60
-
61
- #include <type_traits>
62
-
63
- #define SCOPED_LOCK(lock) _SCOPEDLOCK(lock, __COUNTER__)
64
- #define _SCOPEDLOCK(lock, counter) __SCOPEDLOCK(lock, counter)
65
- #define __SCOPEDLOCK(lock, counter) \
66
- mmkv::ScopedLock<std::remove_pointer<decltype(lock)>::type> __scopedLock##counter(lock)
67
-
68
- #endif
69
- #endif //MMKV_SCOPEDLOCK_HPP
@@ -1,75 +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 "ThreadLock.h"
22
- #include "MMKVLog.h"
23
-
24
- #if MMKV_USING_PTHREAD
25
-
26
- using namespace std;
27
-
28
- namespace mmkv {
29
-
30
- ThreadLock::ThreadLock() : m_lock({}) {
31
- pthread_mutexattr_t attr;
32
- pthread_mutexattr_init(&attr);
33
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
34
-
35
- pthread_mutex_init(&m_lock, &attr);
36
-
37
- pthread_mutexattr_destroy(&attr);
38
- }
39
-
40
- ThreadLock::~ThreadLock() {
41
- pthread_mutex_unlock(&m_lock);
42
-
43
- pthread_mutex_destroy(&m_lock);
44
- }
45
-
46
- void ThreadLock::lock() {
47
- auto ret = pthread_mutex_lock(&m_lock);
48
- if (ret != 0) {
49
- MMKVError("fail to lock %p, ret=%d, errno=%s", &m_lock, ret, strerror(errno));
50
- }
51
- }
52
-
53
- void ThreadLock::unlock() {
54
- auto ret = pthread_mutex_unlock(&m_lock);
55
- if (ret != 0) {
56
- MMKVError("fail to unlock %p, ret=%d, errno=%s", &m_lock, ret, strerror(errno));
57
- }
58
- }
59
-
60
- bool ThreadLock::try_lock() {
61
- auto ret = pthread_mutex_trylock(&m_lock);
62
- return (ret == 0);
63
- }
64
-
65
- void ThreadLock::initialize() {
66
- return;
67
- }
68
-
69
- void ThreadLock::ThreadOnce(ThreadOnceToken_t *onceToken, void (*callback)()) {
70
- pthread_once(onceToken, callback);
71
- }
72
-
73
- } // namespace mmkv
74
-
75
- #endif // MMKV_USING_PTHREAD
@@ -1,81 +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_THREADLOCK_H
22
- #define MMKV_THREADLOCK_H
23
-
24
- #ifdef __cplusplus
25
-
26
- #include "MMKVPredef.h"
27
-
28
- #ifndef MMKV_WIN32
29
- # include <pthread.h>
30
- # define MMKV_USING_PTHREAD 1
31
- #endif
32
-
33
- #ifndef MMKV_USING_PTHREAD
34
- # include <atomic>
35
- #endif
36
-
37
- namespace mmkv {
38
-
39
- #if MMKV_USING_PTHREAD
40
- # define ThreadOnceToken_t pthread_once_t
41
- # define ThreadOnceUninitialized PTHREAD_ONCE_INIT
42
- #else
43
- enum ThreadOnceTokenEnum : int32_t { ThreadOnceUninitialized = 0, ThreadOnceInitializing, ThreadOnceInitialized };
44
- using ThreadOnceToken_t = std::atomic<ThreadOnceTokenEnum>;
45
- #endif
46
-
47
- class ThreadLock {
48
- #if MMKV_USING_PTHREAD
49
- pthread_mutex_t m_lock;
50
- #else
51
- CRITICAL_SECTION m_lock;
52
- #endif
53
-
54
- public:
55
- ThreadLock();
56
- ~ThreadLock();
57
-
58
- void initialize();
59
-
60
- void lock();
61
- void unlock();
62
-
63
- #ifndef MMKV_WIN32
64
- bool try_lock();
65
- #endif
66
-
67
- static void ThreadOnce(ThreadOnceToken_t *onceToken, void (*callback)(void));
68
-
69
- #ifdef MMKV_WIN32
70
- static void Sleep(int ms);
71
- #endif
72
-
73
- // just forbid it for possibly misuse
74
- explicit ThreadLock(const ThreadLock &other) = delete;
75
- ThreadLock &operator=(const ThreadLock &other) = delete;
76
- };
77
-
78
- } // namespace mmkv
79
-
80
- #endif
81
- #endif //MMKV_THREADLOCK_H
@@ -1,89 +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 "ThreadLock.h"
22
-
23
- #if !(MMKV_USING_PTHREAD)
24
-
25
- # include "MMKVLog.h"
26
- # include <atomic>
27
- # include <cassert>
28
-
29
- namespace mmkv {
30
-
31
- ThreadLock::ThreadLock() : m_lock{0} {
32
- }
33
-
34
- ThreadLock::~ThreadLock() {
35
- DeleteCriticalSection(&m_lock);
36
- }
37
-
38
- void ThreadLock::initialize() {
39
- // TODO: a better spin count?
40
- if (!InitializeCriticalSectionAndSpinCount(&m_lock, 1024)) {
41
- MMKVError("fail to init critical section:%d", GetLastError());
42
- }
43
- }
44
-
45
- void ThreadLock::lock() {
46
- EnterCriticalSection(&m_lock);
47
- }
48
-
49
- void ThreadLock::unlock() {
50
- LeaveCriticalSection(&m_lock);
51
- }
52
-
53
- void ThreadLock::ThreadOnce(ThreadOnceToken_t *onceToken, void (*callback)()) {
54
- if (!onceToken || !callback) {
55
- assert(onceToken);
56
- assert(callback);
57
- return;
58
- }
59
- while (true) {
60
- auto expected = ThreadOnceUninitialized;
61
- atomic_compare_exchange_weak(onceToken, &expected, ThreadOnceInitializing);
62
- switch (expected) {
63
- case ThreadOnceInitialized:
64
- return;
65
- case ThreadOnceUninitialized:
66
- callback();
67
- onceToken->store(ThreadOnceInitialized);
68
- return;
69
- case ThreadOnceInitializing: {
70
- // another thread is initializing, let's wait for 1ms
71
- ThreadLock::Sleep(1);
72
- break;
73
- }
74
- default: {
75
- MMKVError("should never happen:%d", expected);
76
- assert(0);
77
- return;
78
- }
79
- }
80
- }
81
- }
82
-
83
- void ThreadLock::Sleep(int ms) {
84
- ::Sleep(ms);
85
- }
86
-
87
- } // namespace mmkv
88
-
89
- #endif // MMKV_USING_PTHREAD
@@ -1,273 +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 "AESCrypt.h"
22
- #include "openssl/openssl_aes.h"
23
- #include <cstdint>
24
- #include <cstdlib>
25
- #include <cstring>
26
- #include <ctime>
27
- #include "../MMKVLog.h"
28
- #include "../MemoryFile.h"
29
-
30
- namespace mmkv {
31
-
32
- // assuming size in [1, 5]
33
- uint32_t AESCrypt::randomItemSizeHolder(uint32_t size) {
34
- constexpr uint32_t ItemSizeHolders[] = {0, 0x80, 0x4000, 0x200000, 0x10000000, 0};
35
- auto ItemSizeHolderMin = ItemSizeHolders[size - 1];
36
- auto ItemSizeHolderMax = ItemSizeHolders[size] - 1;
37
-
38
- srand((unsigned) time(nullptr));
39
- auto result = static_cast<uint32_t>(rand());
40
- result = result % (ItemSizeHolderMax - ItemSizeHolderMin + 1);
41
- result += ItemSizeHolderMin;
42
- return result;
43
- }
44
-
45
- #ifndef MMKV_DISABLE_CRYPT
46
-
47
- using namespace openssl;
48
-
49
- AESCrypt::AESCrypt(const void *key, size_t keyLength, const void *iv, size_t ivLength) {
50
- if (key && keyLength > 0) {
51
- memcpy(m_key, key, (keyLength > AES_KEY_LEN) ? AES_KEY_LEN : keyLength);
52
-
53
- resetIV(iv, ivLength);
54
-
55
- m_aesKey = new AES_KEY;
56
- memset(m_aesKey, 0, sizeof(AES_KEY));
57
- int ret = AES_set_encrypt_key(m_key, AES_KEY_BITSET_LEN, m_aesKey);
58
- MMKV_ASSERT(ret == 0);
59
- }
60
- }
61
-
62
- AESCrypt::AESCrypt(const AESCrypt &other, const AESCryptStatus &status) : m_isClone(true), m_number(status.m_number) {
63
- //memcpy(m_key, other.m_key, sizeof(m_key));
64
- memcpy(m_vector, status.m_vector, sizeof(m_vector));
65
- m_aesKey = other.m_aesKey;
66
- }
67
-
68
- AESCrypt::~AESCrypt() {
69
- if (!m_isClone) {
70
- delete m_aesKey;
71
- delete m_aesRollbackKey;
72
- }
73
- }
74
-
75
- void AESCrypt::resetIV(const void *iv, size_t ivLength) {
76
- m_number = 0;
77
- if (iv && ivLength > 0) {
78
- memcpy(m_vector, iv, (ivLength > AES_KEY_LEN) ? AES_KEY_LEN : ivLength);
79
- } else {
80
- memcpy(m_vector, m_key, AES_KEY_LEN);
81
- }
82
- }
83
-
84
- void AESCrypt::resetStatus(const AESCryptStatus &status) {
85
- m_number = status.m_number;
86
- memcpy(m_vector, status.m_vector, AES_KEY_LEN);
87
- }
88
-
89
- void AESCrypt::getKey(void *output) const {
90
- if (output) {
91
- memcpy(output, m_key, AES_KEY_LEN);
92
- }
93
- }
94
-
95
- void AESCrypt::encrypt(const void *input, void *output, size_t length) {
96
- if (!input || !output || length == 0) {
97
- return;
98
- }
99
- AES_cfb128_encrypt((const uint8_t *) input, (uint8_t *) output, length, m_aesKey, m_vector, &m_number);
100
- }
101
-
102
- void AESCrypt::decrypt(const void *input, void *output, size_t length) {
103
- if (!input || !output || length == 0) {
104
- return;
105
- }
106
- AES_cfb128_decrypt((const uint8_t *) input, (uint8_t *) output, length, m_aesKey, m_vector, &m_number);
107
- }
108
-
109
- void AESCrypt::fillRandomIV(void *vector) {
110
- if (!vector) {
111
- return;
112
- }
113
- srand((unsigned) time(nullptr));
114
- int *ptr = (int *) vector;
115
- for (uint32_t i = 0; i < AES_KEY_LEN / sizeof(int); i++) {
116
- ptr[i] = rand();
117
- }
118
- }
119
-
120
- static inline void
121
- Rollback_cfb_decrypt(const uint8_t *input, const uint8_t *output, size_t len, AES_KEY *key, AESCryptStatus &status) {
122
- auto ivec = status.m_vector;
123
- auto n = status.m_number;
124
-
125
- while (n && len) {
126
- auto c = *(--output);
127
- ivec[--n] = *(--input) ^ c;
128
- len--;
129
- }
130
- if (n == 0 && (status.m_number != 0)) {
131
- AES_decrypt(ivec, ivec, key);
132
- }
133
- while (len >= 16) {
134
- len -= 16;
135
- output -= 16;
136
- input -= 16;
137
- for (; n < 16; n += sizeof(size_t)) {
138
- size_t t = *(size_t *) (output + n);
139
- *(size_t *) (ivec + n) = *(size_t *) (input + n) ^ t;
140
- }
141
- n = 0;
142
- AES_decrypt(ivec, ivec, key);
143
- }
144
- if (len) {
145
- n = 16;
146
- do {
147
- auto c = *(--output);
148
- ivec[--n] = *(--input) ^ c;
149
- len--;
150
- } while (len);
151
- }
152
-
153
- status.m_number = n;
154
- }
155
-
156
- void AESCrypt::statusBeforeDecrypt(const void *input, const void *output, size_t length, AESCryptStatus &status) {
157
- if (length == 0) {
158
- return;
159
- }
160
- if (!m_aesRollbackKey) {
161
- m_aesRollbackKey = new AES_KEY;
162
- memset(m_aesRollbackKey, 0, sizeof(AES_KEY));
163
- int ret = AES_set_decrypt_key(m_key, AES_KEY_BITSET_LEN, m_aesRollbackKey);
164
- MMKV_ASSERT(ret == 0);
165
- }
166
- getCurStatus(status);
167
- Rollback_cfb_decrypt((const uint8_t *) input, (const uint8_t *) output, length, m_aesRollbackKey, status);
168
- }
169
-
170
- void AESCrypt::getCurStatus(AESCryptStatus &status) {
171
- status.m_number = static_cast<uint8_t>(m_number);
172
- memcpy(status.m_vector, m_vector, sizeof(m_vector));
173
- }
174
-
175
- AESCrypt AESCrypt::cloneWithStatus(const AESCryptStatus &status) const {
176
- return AESCrypt(*this, status);
177
- }
178
-
179
- # ifdef MMKV_DEBUG
180
-
181
- void testRandomPlaceHolder() {
182
- for (uint32_t size = 1; size < 6; size++) {
183
- auto holder = AESCrypt::randomItemSizeHolder(size);
184
- MMKVInfo("holder 0x%x for size %u", holder, size);
185
- }
186
- }
187
-
188
- // check if AESCrypt is encrypt-decrypt full-duplex
189
- void AESCrypt::testAESCrypt() {
190
- testRandomPlaceHolder();
191
-
192
- const uint8_t plainText[] = "Hello, OpenSSL-mmkv::AESCrypt::testAESCrypt() with AES CFB 128.";
193
- constexpr size_t textLength = sizeof(plainText) - 1;
194
-
195
- const uint8_t key[] = "TheAESKey";
196
- constexpr size_t keyLength = sizeof(key) - 1;
197
-
198
- uint8_t iv[AES_KEY_LEN];
199
- srand((unsigned) time(nullptr));
200
- for (uint32_t i = 0; i < AES_KEY_LEN; i++) {
201
- iv[i] = (uint8_t) rand();
202
- }
203
- AESCrypt crypt1(key, keyLength, iv, sizeof(iv));
204
- AESCrypt crypt2(key, keyLength, iv, sizeof(iv));
205
-
206
- auto encryptText = new uint8_t[DEFAULT_MMAP_SIZE];
207
- auto decryptText = new uint8_t[DEFAULT_MMAP_SIZE];
208
- memset(encryptText, 0, DEFAULT_MMAP_SIZE);
209
- memset(decryptText, 0, DEFAULT_MMAP_SIZE);
210
-
211
- /* in-place encryption & decryption
212
- memcpy(encryptText, plainText, textLength);
213
- crypt1.encrypt(encryptText, encryptText, textLength);
214
- crypt2.decrypt(encryptText, encryptText, textLength);
215
- return;
216
- */
217
- AES_KEY decryptKey;
218
- AES_set_decrypt_key(crypt1.m_key, AES_KEY_BITSET_LEN, &decryptKey);
219
-
220
- size_t actualSize = 0;
221
- bool flip = false;
222
- for (const uint8_t *ptr = plainText; ptr < plainText + textLength;) {
223
- auto tokenPtr = (const uint8_t *) strchr((const char *) ptr, ' ');
224
- size_t size = 0;
225
- if (!tokenPtr) {
226
- size = static_cast<size_t>(plainText + textLength - ptr);
227
- } else {
228
- size = static_cast<size_t>(tokenPtr - ptr + 1);
229
- }
230
-
231
- AESCrypt *decrypter;
232
- uint32_t oldNum;
233
- uint8_t oldVector[sizeof(crypt1.m_vector)];
234
-
235
- flip = !flip;
236
- if (flip) {
237
- crypt1.encrypt(plainText + actualSize, encryptText + actualSize, size);
238
-
239
- decrypter = &crypt2;
240
- oldNum = decrypter->m_number;
241
- memcpy(oldVector, decrypter->m_vector, sizeof(oldVector));
242
- crypt2.decrypt(encryptText + actualSize, decryptText + actualSize, size);
243
- } else {
244
- crypt2.encrypt(plainText + actualSize, encryptText + actualSize, size);
245
-
246
- decrypter = &crypt1;
247
- oldNum = decrypter->m_number;
248
- memcpy(oldVector, decrypter->m_vector, sizeof(oldVector));
249
- crypt1.decrypt(encryptText + actualSize, decryptText + actualSize, size);
250
- }
251
- // that's why AESCrypt can be full-duplex
252
- assert(crypt1.m_number == crypt2.m_number);
253
- assert(0 == memcmp(crypt1.m_vector, crypt2.m_vector, sizeof(crypt1.m_vector)));
254
-
255
- // how rollback works
256
- AESCryptStatus status;
257
- decrypter->statusBeforeDecrypt(encryptText + actualSize + size, decryptText + actualSize + size, size, status);
258
- assert(oldNum == status.m_number);
259
- assert(0 == memcmp(oldVector, status.m_vector, sizeof(oldVector)));
260
-
261
- actualSize += size;
262
- ptr += size;
263
- }
264
- MMKVInfo("AES CFB decode: %s", decryptText);
265
-
266
- delete[] encryptText;
267
- delete[] decryptText;
268
- }
269
-
270
- # endif // MMKV_DEBUG
271
- #endif // MMKV_DISABLE_CRYPT
272
-
273
- } // namespace mmkv
@@ -1,112 +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 AES_CRYPT_H_
22
- #define AES_CRYPT_H_
23
- #ifdef __cplusplus
24
-
25
- #include "../MMKVPredef.h"
26
- #include <cstddef>
27
- #include <cstdint>
28
-
29
- #ifdef MMKV_DISABLE_CRYPT
30
-
31
- namespace mmkv {
32
- class AESCrypt {
33
- public:
34
- static uint32_t randomItemSizeHolder(uint32_t size);
35
- };
36
- }
37
-
38
- #else
39
-
40
- namespace openssl {
41
- struct AES_KEY;
42
- }
43
-
44
- namespace mmkv {
45
-
46
- #pragma pack(push, 1)
47
-
48
- struct AESCryptStatus {
49
- uint8_t m_number;
50
- uint8_t m_vector[AES_KEY_LEN];
51
- };
52
-
53
- #pragma pack(pop)
54
-
55
- class CodedInputDataCrypt;
56
-
57
- // a AES CFB-128 encrypt-decrypt full-duplex wrapper
58
- class AESCrypt {
59
- bool m_isClone = false;
60
- uint32_t m_number = 0;
61
- openssl::AES_KEY *m_aesKey = nullptr;
62
- openssl::AES_KEY *m_aesRollbackKey = nullptr;
63
- uint8_t m_key[AES_KEY_LEN] = {};
64
-
65
- public:
66
- uint8_t m_vector[AES_KEY_LEN] = {};
67
-
68
- private:
69
- // for cloneWithStatus()
70
- AESCrypt(const AESCrypt &other, const AESCryptStatus &status);
71
-
72
- public:
73
- AESCrypt(const void *key, size_t keyLength, const void *iv = nullptr, size_t ivLength = 0);
74
- AESCrypt(AESCrypt &&other) = default;
75
-
76
- ~AESCrypt();
77
-
78
- void encrypt(const void *input, void *output, size_t length);
79
-
80
- void decrypt(const void *input, void *output, size_t length);
81
-
82
- void getCurStatus(AESCryptStatus &status);
83
- void statusBeforeDecrypt(const void *input, const void *output, size_t length, AESCryptStatus &status);
84
-
85
- AESCrypt cloneWithStatus(const AESCryptStatus &status) const;
86
-
87
- void resetIV(const void *iv = nullptr, size_t ivLength = 0);
88
- void resetStatus(const AESCryptStatus &status);
89
-
90
- // output must have [AES_KEY_LEN] space
91
- void getKey(void *output) const;
92
-
93
- static void fillRandomIV(void *vector);
94
- static uint32_t randomItemSizeHolder(uint32_t size);
95
-
96
- // just forbid it for possibly misuse
97
- explicit AESCrypt(const AESCrypt &other) = delete;
98
- AESCrypt &operator=(const AESCrypt &other) = delete;
99
-
100
- friend CodedInputDataCrypt;
101
-
102
- #ifdef MMKV_DEBUG
103
- // check if AESCrypt is encrypt-decrypt full-duplex
104
- static void testAESCrypt();
105
- #endif
106
- };
107
-
108
- } // namespace mmkv
109
-
110
- #endif // MMKV_DISABLE_CRYPT
111
- #endif // __cplusplus
112
- #endif /* AES_CRYPT_H_ */