Skip to content

Commit df457bd

Browse files
committed
add MMKV Core
1 parent 297eb4a commit df457bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+14532
-0
lines changed

MMKV/Core/CMakeLists.txt

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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.8.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)
51+
IF(UNIX AND (NOT APPLE))
52+
IF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
53+
SET(ASM_OPTIONS "-x assembler-with-cpp")
54+
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8-a+crypto")
55+
ENDIF()
56+
ENDIF()
57+
58+
59+
project(core)
60+
61+
# Creates and names a library, sets it as either STATIC
62+
# or SHARED, and provides the relative paths to its source code.
63+
# You can define multiple libraries, and CMake builds them for you.
64+
# Gradle automatically packages shared libraries with your APK.
65+
66+
add_library(core
67+
68+
# Sets the library as a shared library.
69+
STATIC
70+
71+
# Provides a relative path to your source file(s).
72+
MMKV.h
73+
MMKV.cpp
74+
MMKV_Android.cpp
75+
MMKV_IO.h
76+
MMKV_IO.cpp
77+
MMKV_OSX.cpp
78+
MMKVLog.h
79+
MMKVLog.cpp
80+
MMKVLog_Android.cpp
81+
CodedInputData.h
82+
CodedInputData.cpp
83+
CodedInputData_OSX.cpp
84+
CodedInputDataCrypt.h
85+
CodedInputDataCrypt.cpp
86+
CodedInputDataCrypt_OSX.cpp
87+
CodedOutputData.h
88+
CodedOutputData.cpp
89+
KeyValueHolder.h
90+
KeyValueHolder.cpp
91+
PBUtility.h
92+
PBUtility.cpp
93+
MiniPBCoder.h
94+
MiniPBCoder.cpp
95+
MiniPBCoder_OSX.cpp
96+
MMBuffer.h
97+
MMBuffer.cpp
98+
InterProcessLock.h
99+
InterProcessLock.cpp
100+
InterProcessLock_Win32.cpp
101+
InterProcessLock_Android.cpp
102+
MemoryFile.h
103+
MemoryFile.cpp
104+
MemoryFile_Android.cpp
105+
MemoryFile_Win32.cpp
106+
MemoryFile_OSX.cpp
107+
ThreadLock.h
108+
ThreadLock.cpp
109+
ThreadLock_Win32.cpp
110+
MMKVMetaInfo.hpp
111+
aes/AESCrypt.h
112+
aes/AESCrypt.cpp
113+
aes/openssl/openssl_aes.h
114+
aes/openssl/openssl_aes_core.cpp
115+
aes/openssl/openssl_aes_locl.h
116+
aes/openssl/openssl_cfb128.cpp
117+
aes/openssl/openssl_opensslconf.h
118+
aes/openssl/openssl_md5_dgst.cpp
119+
aes/openssl/openssl_md5_locl.h
120+
aes/openssl/openssl_md5_one.cpp
121+
aes/openssl/openssl_md5.h
122+
aes/openssl/openssl_md32_common.h
123+
aes/openssl/openssl_aesv8-armx.S
124+
aes/openssl/openssl_aes-armv4.S
125+
aes/openssl/openssl_arm_arch.h
126+
crc32/Checksum.h
127+
crc32/crc32_armv8.cpp
128+
crc32/zlib/zconf.h
129+
crc32/zlib/zutil.h
130+
crc32/zlib/crc32.h
131+
crc32/zlib/crc32.cpp
132+
MMKVPredef.h
133+
)
134+
135+
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
136+
137+
set_target_properties(core PROPERTIES
138+
CXX_STANDARD 17
139+
CXX_EXTENSIONS OFF
140+
POSITION_INDEPENDENT_CODE ON
141+
)
142+
143+
find_library(zlib
144+
z
145+
)
146+
147+
IF (NOT zlib)
148+
target_compile_definitions(core PUBLIC MMKV_EMBED_ZLIB=1)
149+
ELSE()
150+
target_link_libraries(core ${zlib})
151+
ENDIF()
152+

MMKV/Core/CodedInputData.cpp

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
25+
#ifdef MMKV_APPLE
26+
# if __has_feature(objc_arc)
27+
# error This file must be compiled with MRC. Use -fno-objc-arc flag.
28+
# endif
29+
#endif // MMKV_APPLE
30+
31+
using namespace std;
32+
33+
namespace mmkv {
34+
35+
CodedInputData::CodedInputData(const void *oData, size_t length)
36+
: m_ptr((uint8_t *) oData), m_size(length), m_position(0) {
37+
MMKV_ASSERT(m_ptr);
38+
}
39+
40+
void CodedInputData::seek(size_t addedSize) {
41+
if (m_position + addedSize > m_size) {
42+
throw out_of_range("OutOfSpace");
43+
}
44+
m_position += addedSize;
45+
}
46+
47+
double CodedInputData::readDouble() {
48+
return Int64ToFloat64(this->readRawLittleEndian64());
49+
}
50+
51+
float CodedInputData::readFloat() {
52+
return Int32ToFloat32(this->readRawLittleEndian32());
53+
}
54+
55+
int64_t CodedInputData::readInt64() {
56+
int32_t shift = 0;
57+
int64_t result = 0;
58+
while (shift < 64) {
59+
int8_t b = this->readRawByte();
60+
result |= (int64_t)(b & 0x7f) << shift;
61+
if ((b & 0x80) == 0) {
62+
return result;
63+
}
64+
shift += 7;
65+
}
66+
throw invalid_argument("InvalidProtocolBuffer malformedInt64");
67+
}
68+
69+
uint64_t CodedInputData::readUInt64() {
70+
return static_cast<uint64_t>(readInt64());
71+
}
72+
73+
int32_t CodedInputData::readInt32() {
74+
return this->readRawVarint32();
75+
}
76+
77+
uint32_t CodedInputData::readUInt32() {
78+
return static_cast<uint32_t>(readRawVarint32());
79+
}
80+
81+
bool CodedInputData::readBool() {
82+
return this->readRawVarint32() != 0;
83+
}
84+
85+
#ifndef MMKV_APPLE
86+
87+
string CodedInputData::readString() {
88+
int32_t size = readRawVarint32();
89+
if (size < 0) {
90+
throw length_error("InvalidProtocolBuffer negativeSize");
91+
}
92+
93+
auto s_size = static_cast<size_t>(size);
94+
if (s_size <= m_size - m_position) {
95+
string result((char *) (m_ptr + m_position), s_size);
96+
m_position += s_size;
97+
return result;
98+
} else {
99+
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
100+
}
101+
}
102+
103+
string CodedInputData::readString(KeyValueHolder &kvHolder) {
104+
kvHolder.offset = static_cast<uint32_t>(m_position);
105+
106+
int32_t size = this->readRawVarint32();
107+
if (size < 0) {
108+
throw length_error("InvalidProtocolBuffer negativeSize");
109+
}
110+
111+
auto s_size = static_cast<size_t>(size);
112+
if (s_size <= m_size - m_position) {
113+
kvHolder.keySize = static_cast<uint16_t>(s_size);
114+
115+
auto ptr = m_ptr + m_position;
116+
string result((char *) (m_ptr + m_position), s_size);
117+
m_position += s_size;
118+
return result;
119+
} else {
120+
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
121+
}
122+
}
123+
124+
#endif
125+
126+
MMBuffer CodedInputData::readData() {
127+
int32_t size = this->readRawVarint32();
128+
if (size < 0) {
129+
throw length_error("InvalidProtocolBuffer negativeSize");
130+
}
131+
132+
auto s_size = static_cast<size_t>(size);
133+
if (s_size <= m_size - m_position) {
134+
MMBuffer data(((int8_t *) m_ptr) + m_position, s_size);
135+
m_position += s_size;
136+
return data;
137+
} else {
138+
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
139+
}
140+
}
141+
142+
void CodedInputData::readData(KeyValueHolder &kvHolder) {
143+
int32_t size = this->readRawVarint32();
144+
if (size < 0) {
145+
throw length_error("InvalidProtocolBuffer negativeSize");
146+
}
147+
148+
auto s_size = static_cast<size_t>(size);
149+
if (s_size <= m_size - m_position) {
150+
kvHolder.computedKVSize = static_cast<uint16_t>(m_position - kvHolder.offset);
151+
kvHolder.valueSize = static_cast<uint32_t>(s_size);
152+
153+
m_position += s_size;
154+
} else {
155+
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
156+
}
157+
}
158+
159+
int32_t CodedInputData::readRawVarint32() {
160+
int8_t tmp = this->readRawByte();
161+
if (tmp >= 0) {
162+
return tmp;
163+
}
164+
int32_t result = tmp & 0x7f;
165+
if ((tmp = this->readRawByte()) >= 0) {
166+
result |= tmp << 7;
167+
} else {
168+
result |= (tmp & 0x7f) << 7;
169+
if ((tmp = this->readRawByte()) >= 0) {
170+
result |= tmp << 14;
171+
} else {
172+
result |= (tmp & 0x7f) << 14;
173+
if ((tmp = this->readRawByte()) >= 0) {
174+
result |= tmp << 21;
175+
} else {
176+
result |= (tmp & 0x7f) << 21;
177+
result |= (tmp = this->readRawByte()) << 28;
178+
if (tmp < 0) {
179+
// discard upper 32 bits
180+
for (int i = 0; i < 5; i++) {
181+
if (this->readRawByte() >= 0) {
182+
return result;
183+
}
184+
}
185+
throw invalid_argument("InvalidProtocolBuffer malformed varint32");
186+
}
187+
}
188+
}
189+
}
190+
return result;
191+
}
192+
193+
int32_t CodedInputData::readRawLittleEndian32() {
194+
int8_t b1 = this->readRawByte();
195+
int8_t b2 = this->readRawByte();
196+
int8_t b3 = this->readRawByte();
197+
int8_t b4 = this->readRawByte();
198+
return (((int32_t) b1 & 0xff)) | (((int32_t) b2 & 0xff) << 8) | (((int32_t) b3 & 0xff) << 16) |
199+
(((int32_t) b4 & 0xff) << 24);
200+
}
201+
202+
int64_t CodedInputData::readRawLittleEndian64() {
203+
int8_t b1 = this->readRawByte();
204+
int8_t b2 = this->readRawByte();
205+
int8_t b3 = this->readRawByte();
206+
int8_t b4 = this->readRawByte();
207+
int8_t b5 = this->readRawByte();
208+
int8_t b6 = this->readRawByte();
209+
int8_t b7 = this->readRawByte();
210+
int8_t b8 = this->readRawByte();
211+
return (((int64_t) b1 & 0xff)) | (((int64_t) b2 & 0xff) << 8) | (((int64_t) b3 & 0xff) << 16) |
212+
(((int64_t) b4 & 0xff) << 24) | (((int64_t) b5 & 0xff) << 32) | (((int64_t) b6 & 0xff) << 40) |
213+
(((int64_t) b7 & 0xff) << 48) | (((int64_t) b8 & 0xff) << 56);
214+
}
215+
216+
int8_t CodedInputData::readRawByte() {
217+
if (m_position == m_size) {
218+
auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
219+
throw out_of_range(msg);
220+
}
221+
auto *bytes = (int8_t *) m_ptr;
222+
return bytes[m_position++];
223+
}
224+
225+
#ifdef MMKV_APPLE
226+
#endif // MMKV_APPLE
227+
228+
} // namespace mmkv

0 commit comments

Comments
 (0)