react-native-mmkv 3.2.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.
- package/android/CMakeLists.txt +18 -9
- package/android/build.gradle +6 -2
- package/cpp/{MMKVManagedBuffer.h → ManagedMMBuffer.h} +6 -6
- package/cpp/MmkvHostObject.cpp +22 -17
- package/cpp/MmkvHostObject.h +1 -2
- package/cpp/MmkvTypes.h +50 -0
- package/cpp/NativeMmkvModule.cpp +3 -3
- package/cpp/NativeMmkvModule.h +1 -8
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/Types.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +1 -1
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/Types.js.map +1 -1
- package/lib/module/createMMKV.web.js +1 -1
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/typescript/src/MMKV.d.ts +1 -1
- package/lib/typescript/src/MMKV.d.ts.map +1 -1
- package/lib/typescript/src/Types.d.ts +7 -1
- package/lib/typescript/src/Types.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 +22 -27
- package/react-native-mmkv.podspec +4 -10
- package/react-native.config.js +9 -0
- package/src/MMKV.ts +1 -1
- package/src/Types.ts +7 -1
- package/src/createMMKV.web.ts +1 -1
- package/src/hooks.ts +1 -1
- package/MMKV/Core/CMakeLists.txt +0 -172
- package/MMKV/Core/CodedInputData.cpp +0 -252
- package/MMKV/Core/CodedInputData.h +0 -87
- package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
- package/MMKV/Core/CodedInputDataCrypt.h +0 -87
- package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
- package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
- package/MMKV/Core/CodedOutputData.cpp +0 -186
- package/MMKV/Core/CodedOutputData.h +0 -88
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
- package/MMKV/Core/InterProcessLock.cpp +0 -186
- package/MMKV/Core/InterProcessLock.h +0 -119
- package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
- package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
- package/MMKV/Core/KeyValueHolder.cpp +0 -236
- package/MMKV/Core/KeyValueHolder.h +0 -122
- package/MMKV/Core/MMBuffer.cpp +0 -210
- package/MMKV/Core/MMBuffer.h +0 -111
- package/MMKV/Core/MMKV.cpp +0 -1702
- package/MMKV/Core/MMKV.h +0 -595
- package/MMKV/Core/MMKVLog.cpp +0 -127
- package/MMKV/Core/MMKVLog.h +0 -86
- package/MMKV/Core/MMKVLog_Android.cpp +0 -134
- package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
- package/MMKV/Core/MMKVPredef.h +0 -293
- package/MMKV/Core/MMKV_Android.cpp +0 -261
- package/MMKV/Core/MMKV_IO.cpp +0 -1905
- package/MMKV/Core/MMKV_IO.h +0 -57
- package/MMKV/Core/MMKV_OSX.cpp +0 -423
- package/MMKV/Core/MMKV_OSX.h +0 -57
- package/MMKV/Core/MemoryFile.cpp +0 -603
- package/MMKV/Core/MemoryFile.h +0 -194
- package/MMKV/Core/MemoryFile_Android.cpp +0 -236
- package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
- package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
- package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
- package/MMKV/Core/MiniPBCoder.cpp +0 -672
- package/MMKV/Core/MiniPBCoder.h +0 -151
- package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
- package/MMKV/Core/PBEncodeItem.hpp +0 -104
- package/MMKV/Core/PBUtility.cpp +0 -61
- package/MMKV/Core/PBUtility.h +0 -148
- package/MMKV/Core/ScopedLock.hpp +0 -69
- package/MMKV/Core/ThreadLock.cpp +0 -75
- package/MMKV/Core/ThreadLock.h +0 -81
- package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
- package/MMKV/Core/aes/AESCrypt.cpp +0 -273
- package/MMKV/Core/aes/AESCrypt.h +0 -112
- package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
- package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
- package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
- package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
- package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
- package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
- package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
- package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
- package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
- package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
- package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
- package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
- package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
- package/MMKV/Core/core.vcxproj +0 -288
- package/MMKV/Core/core.vcxproj.filters +0 -150
- package/MMKV/Core/crc32/Checksum.h +0 -75
- package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
- package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
- package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
- package/MMKV/Core/crc32/zlib/crc32.h +0 -48
- package/MMKV/Core/crc32/zlib/zconf.h +0 -380
- package/MMKV/Core/crc32/zlib/zutil.h +0 -25
- package/MMKV/README.md +0 -354
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
require "json"
|
|
2
2
|
|
|
3
3
|
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
-
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
|
|
5
4
|
|
|
6
5
|
Pod::UI.puts "[react-native-mmkv] Thank you for using react-native-mmkv ❤️"
|
|
7
6
|
Pod::UI.puts "[react-native-mmkv] If you enjoy using react-native-mmkv, please consider sponsoring this project: https://github.com/sponsors/mrousavy"
|
|
@@ -18,21 +17,16 @@ Pod::Spec.new do |s|
|
|
|
18
17
|
s.source = { :git => "https://github.com/mrousavy/react-native-mmkv.git", :tag => "#{s.version}" }
|
|
19
18
|
|
|
20
19
|
s.pod_target_xcconfig = {
|
|
21
|
-
"CLANG_CXX_LANGUAGE_STANDARD" => "c++
|
|
22
|
-
"CLANG_CXX_LIBRARY" => "libc++",
|
|
23
|
-
"CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF" => "NO",
|
|
24
|
-
# FORCE_POSIX ensures we are using C++ types instead of Objective-C types for MMKV.
|
|
25
|
-
"GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) FORCE_POSIX",
|
|
20
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
|
|
26
21
|
}
|
|
27
22
|
s.compiler_flags = '-x objective-c++'
|
|
28
|
-
|
|
23
|
+
s.libraries = "z", "c++"
|
|
29
24
|
s.source_files = [
|
|
30
25
|
# react-native-mmkv
|
|
31
26
|
"ios/**/*.{h,m,mm}",
|
|
32
|
-
"cpp/**/*.{hpp,cpp,c,h}"
|
|
33
|
-
# MMKV/Core
|
|
34
|
-
"MMKV/Core/**/*.{h,cpp,hpp,S}",
|
|
27
|
+
"cpp/**/*.{hpp,cpp,c,h}"
|
|
35
28
|
]
|
|
36
29
|
|
|
30
|
+
s.dependency 'MMKVCore', '>= 2.2.3'
|
|
37
31
|
install_modules_dependencies(s)
|
|
38
32
|
end
|
package/react-native.config.js
CHANGED
package/src/MMKV.ts
CHANGED
|
@@ -83,7 +83,7 @@ export class MMKV implements MMKVInterface {
|
|
|
83
83
|
const func = this.getFunctionFromCache('getNumber');
|
|
84
84
|
return func(key);
|
|
85
85
|
}
|
|
86
|
-
getBuffer(key: string):
|
|
86
|
+
getBuffer(key: string): ArrayBufferLike | undefined {
|
|
87
87
|
const func = this.getFunctionFromCache('getBuffer');
|
|
88
88
|
return func(key);
|
|
89
89
|
}
|
package/src/Types.ts
CHANGED
|
@@ -70,6 +70,12 @@ export interface Configuration {
|
|
|
70
70
|
* @default SINGLE_PROCESS
|
|
71
71
|
*/
|
|
72
72
|
mode?: Mode;
|
|
73
|
+
/**
|
|
74
|
+
* If `true`, the MMKV instance can only read from the storage, but not write to it.
|
|
75
|
+
* This is more efficient if you do not need to write to it.
|
|
76
|
+
* @default false
|
|
77
|
+
*/
|
|
78
|
+
readOnly?: boolean;
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
/**
|
|
@@ -105,7 +111,7 @@ export interface NativeMMKV {
|
|
|
105
111
|
*
|
|
106
112
|
* @default undefined
|
|
107
113
|
*/
|
|
108
|
-
getBuffer: (key: string) =>
|
|
114
|
+
getBuffer: (key: string) => ArrayBufferLike | undefined;
|
|
109
115
|
/**
|
|
110
116
|
* Checks whether the given `key` is being stored in this MMKV instance.
|
|
111
117
|
*/
|
package/src/createMMKV.web.ts
CHANGED
|
@@ -106,7 +106,7 @@ export const createMMKV = (config: Configuration): NativeMMKV => {
|
|
|
106
106
|
getBuffer: (key) => {
|
|
107
107
|
const value = storage().getItem(prefixedKey(key));
|
|
108
108
|
if (value == null) return undefined;
|
|
109
|
-
return textEncoder.encode(value);
|
|
109
|
+
return textEncoder.encode(value).buffer;
|
|
110
110
|
},
|
|
111
111
|
getAllKeys: () => {
|
|
112
112
|
const keys = Object.keys(storage());
|
package/src/hooks.ts
CHANGED
|
@@ -51,7 +51,7 @@ export function useMMKV(configuration?: Configuration): MMKV {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
function createMMKVHook<
|
|
54
|
-
T extends (boolean | number | string |
|
|
54
|
+
T extends (boolean | number | string | ArrayBufferLike) | undefined,
|
|
55
55
|
TSet extends T | undefined,
|
|
56
56
|
TSetAction extends TSet | ((current: T) => TSet),
|
|
57
57
|
>(getter: (instance: MMKV, key: string) => T) {
|
package/MMKV/Core/CMakeLists.txt
DELETED
|
@@ -1,172 +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
|
-
# Sets the minimum version of CMake required to build the native library.
|
|
22
|
-
|
|
23
|
-
cmake_minimum_required(VERSION 3.10.0)
|
|
24
|
-
|
|
25
|
-
IF(APPLE)
|
|
26
|
-
# tell ranlib to ignore empty compilation units
|
|
27
|
-
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
|
|
28
|
-
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
|
|
29
|
-
# prevents ar from invoking ranlib, let CMake do it
|
|
30
|
-
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")
|
|
31
|
-
SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")
|
|
32
|
-
|
|
33
|
-
add_compile_definitions(FORCE_POSIX)
|
|
34
|
-
ENDIF()
|
|
35
|
-
|
|
36
|
-
set(can_use_assembler TRUE)
|
|
37
|
-
enable_language(ASM)
|
|
38
|
-
IF("${ANDROID_ABI}" STREQUAL "arm64-v8a")
|
|
39
|
-
SET(ASM_OPTIONS "-x assembler-with-cpp")
|
|
40
|
-
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8+crypto -D__ANDROID__")
|
|
41
|
-
ELSEIF("${ANDROID_ABI}" STREQUAL "armeabi-v7a")
|
|
42
|
-
SET(ASM_OPTIONS "-x assembler-with-cpp")
|
|
43
|
-
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv7a -D__ANDROID__")
|
|
44
|
-
ELSEIF("${ANDROID_ABI}" STREQUAL "armeabi")
|
|
45
|
-
SET(ASM_OPTIONS "-x assembler-with-cpp")
|
|
46
|
-
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv5 -D__ANDROID__")
|
|
47
|
-
ENDIF()
|
|
48
|
-
|
|
49
|
-
#include(CMakePrintHelpers)
|
|
50
|
-
#cmake_print_variables(CMAKE_SYSTEM_PROCESSOR OHOS OHOS_ARCH CFLAGS ASM_OPTIONS CMAKE_ASM_FLAGS)
|
|
51
|
-
|
|
52
|
-
IF(OHOS)
|
|
53
|
-
IF("${OHOS_ARCH}" STREQUAL "arm64-v8a")
|
|
54
|
-
SET(ASM_OPTIONS "-x assembler-with-cpp")
|
|
55
|
-
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8+crypto -D__MUSL__")
|
|
56
|
-
ENDIF()
|
|
57
|
-
ELSEIF(UNIX AND (NOT APPLE))
|
|
58
|
-
IF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
|
|
59
|
-
SET(ASM_OPTIONS "-x assembler-with-cpp")
|
|
60
|
-
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8-a+crypto")
|
|
61
|
-
ENDIF()
|
|
62
|
-
ENDIF()
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
project(core)
|
|
66
|
-
|
|
67
|
-
# Creates and names a library, sets it as either STATIC
|
|
68
|
-
# or SHARED, and provides the relative paths to its source code.
|
|
69
|
-
# You can define multiple libraries, and CMake builds them for you.
|
|
70
|
-
# Gradle automatically packages shared libraries with your APK.
|
|
71
|
-
|
|
72
|
-
add_library(core
|
|
73
|
-
|
|
74
|
-
# Sets the library as a shared library.
|
|
75
|
-
STATIC
|
|
76
|
-
|
|
77
|
-
# Provides a relative path to your source file(s).
|
|
78
|
-
MMKV.h
|
|
79
|
-
MMKV.cpp
|
|
80
|
-
MMKV_Android.cpp
|
|
81
|
-
MMKV_IO.h
|
|
82
|
-
MMKV_IO.cpp
|
|
83
|
-
MMKV_OSX.cpp
|
|
84
|
-
MMKVLog.h
|
|
85
|
-
MMKVLog.cpp
|
|
86
|
-
MMKVLog_Android.cpp
|
|
87
|
-
CodedInputData.h
|
|
88
|
-
CodedInputData.cpp
|
|
89
|
-
CodedInputData_OSX.cpp
|
|
90
|
-
CodedInputDataCrypt.h
|
|
91
|
-
CodedInputDataCrypt.cpp
|
|
92
|
-
CodedInputDataCrypt_OSX.cpp
|
|
93
|
-
CodedOutputData.h
|
|
94
|
-
CodedOutputData.cpp
|
|
95
|
-
KeyValueHolder.h
|
|
96
|
-
KeyValueHolder.cpp
|
|
97
|
-
PBUtility.h
|
|
98
|
-
PBUtility.cpp
|
|
99
|
-
MiniPBCoder.h
|
|
100
|
-
MiniPBCoder.cpp
|
|
101
|
-
MiniPBCoder_OSX.cpp
|
|
102
|
-
MMBuffer.h
|
|
103
|
-
MMBuffer.cpp
|
|
104
|
-
InterProcessLock.h
|
|
105
|
-
InterProcessLock.cpp
|
|
106
|
-
InterProcessLock_Win32.cpp
|
|
107
|
-
InterProcessLock_Android.cpp
|
|
108
|
-
MemoryFile.h
|
|
109
|
-
MemoryFile.cpp
|
|
110
|
-
MemoryFile_Android.cpp
|
|
111
|
-
MemoryFile_Linux.cpp
|
|
112
|
-
MemoryFile_Win32.cpp
|
|
113
|
-
MemoryFile_OSX.cpp
|
|
114
|
-
ThreadLock.h
|
|
115
|
-
ThreadLock.cpp
|
|
116
|
-
ThreadLock_Win32.cpp
|
|
117
|
-
MMKVMetaInfo.hpp
|
|
118
|
-
aes/AESCrypt.h
|
|
119
|
-
aes/AESCrypt.cpp
|
|
120
|
-
aes/openssl/openssl_aes.h
|
|
121
|
-
aes/openssl/openssl_aes_core.cpp
|
|
122
|
-
aes/openssl/openssl_aes_locl.h
|
|
123
|
-
aes/openssl/openssl_cfb128.cpp
|
|
124
|
-
aes/openssl/openssl_opensslconf.h
|
|
125
|
-
aes/openssl/openssl_md5_dgst.cpp
|
|
126
|
-
aes/openssl/openssl_md5_locl.h
|
|
127
|
-
aes/openssl/openssl_md5_one.cpp
|
|
128
|
-
aes/openssl/openssl_md5.h
|
|
129
|
-
aes/openssl/openssl_md32_common.h
|
|
130
|
-
aes/openssl/openssl_arm_arch.h
|
|
131
|
-
crc32/Checksum.h
|
|
132
|
-
crc32/crc32_armv8.cpp
|
|
133
|
-
crc32/zlib/zconf.h
|
|
134
|
-
crc32/zlib/zutil.h
|
|
135
|
-
crc32/zlib/crc32.h
|
|
136
|
-
crc32/zlib/crc32.cpp
|
|
137
|
-
MMKVPredef.h
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
IF (MSVC OR (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"))
|
|
141
|
-
# .S files is not supported by MSVC.
|
|
142
|
-
# x86_64 asm not supported in OHOS
|
|
143
|
-
ELSE()
|
|
144
|
-
target_sources(core PRIVATE
|
|
145
|
-
aes/openssl/openssl_aesv8-armx.S
|
|
146
|
-
aes/openssl/openssl_aes-armv4.S)
|
|
147
|
-
ENDIF()
|
|
148
|
-
|
|
149
|
-
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
150
|
-
|
|
151
|
-
IF (WIN32)
|
|
152
|
-
# MMKV can be used only with Unicode on Windows.
|
|
153
|
-
target_compile_definitions(core PUBLIC UNICODE)
|
|
154
|
-
target_compile_definitions(core PUBLIC _UNICODE)
|
|
155
|
-
ENDIF()
|
|
156
|
-
|
|
157
|
-
set_target_properties(core PROPERTIES
|
|
158
|
-
CXX_STANDARD 20
|
|
159
|
-
CXX_EXTENSIONS OFF
|
|
160
|
-
POSITION_INDEPENDENT_CODE ON
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
find_library(zlib
|
|
164
|
-
z
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
IF (NOT zlib)
|
|
168
|
-
target_compile_definitions(core PUBLIC MMKV_EMBED_ZLIB=1)
|
|
169
|
-
ELSE()
|
|
170
|
-
target_link_libraries(core ${zlib})
|
|
171
|
-
ENDIF()
|
|
172
|
-
|
|
@@ -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
|