Skip to content

Commit

Permalink
add MMKV Core
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Mar 4, 2021
1 parent 297eb4a commit df457bd
Show file tree
Hide file tree
Showing 69 changed files with 14,532 additions and 0 deletions.
152 changes: 152 additions & 0 deletions MMKV/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#
# Tencent is pleased to support the open source community by making
# MMKV available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company.
# All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.8.0)

IF(APPLE)
# tell ranlib to ignore empty compilation units
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
# prevents ar from invoking ranlib, let CMake do it
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")
SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")

add_compile_definitions(FORCE_POSIX)
ENDIF()

set(can_use_assembler TRUE)
enable_language(ASM)
IF("${ANDROID_ABI}" STREQUAL "arm64-v8a")
SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8+crypto -D__ANDROID__")
ELSEIF("${ANDROID_ABI}" STREQUAL "armeabi-v7a")
SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv7a -D__ANDROID__")
ELSEIF("${ANDROID_ABI}" STREQUAL "armeabi")
SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv5 -D__ANDROID__")
ENDIF()

#include(CMakePrintHelpers)
#cmake_print_variables(CMAKE_SYSTEM_PROCESSOR)
IF(UNIX AND (NOT APPLE))
IF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8-a+crypto")
ENDIF()
ENDIF()


project(core)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library(core

# Sets the library as a shared library.
STATIC

# Provides a relative path to your source file(s).
MMKV.h
MMKV.cpp
MMKV_Android.cpp
MMKV_IO.h
MMKV_IO.cpp
MMKV_OSX.cpp
MMKVLog.h
MMKVLog.cpp
MMKVLog_Android.cpp
CodedInputData.h
CodedInputData.cpp
CodedInputData_OSX.cpp
CodedInputDataCrypt.h
CodedInputDataCrypt.cpp
CodedInputDataCrypt_OSX.cpp
CodedOutputData.h
CodedOutputData.cpp
KeyValueHolder.h
KeyValueHolder.cpp
PBUtility.h
PBUtility.cpp
MiniPBCoder.h
MiniPBCoder.cpp
MiniPBCoder_OSX.cpp
MMBuffer.h
MMBuffer.cpp
InterProcessLock.h
InterProcessLock.cpp
InterProcessLock_Win32.cpp
InterProcessLock_Android.cpp
MemoryFile.h
MemoryFile.cpp
MemoryFile_Android.cpp
MemoryFile_Win32.cpp
MemoryFile_OSX.cpp
ThreadLock.h
ThreadLock.cpp
ThreadLock_Win32.cpp
MMKVMetaInfo.hpp
aes/AESCrypt.h
aes/AESCrypt.cpp
aes/openssl/openssl_aes.h
aes/openssl/openssl_aes_core.cpp
aes/openssl/openssl_aes_locl.h
aes/openssl/openssl_cfb128.cpp
aes/openssl/openssl_opensslconf.h
aes/openssl/openssl_md5_dgst.cpp
aes/openssl/openssl_md5_locl.h
aes/openssl/openssl_md5_one.cpp
aes/openssl/openssl_md5.h
aes/openssl/openssl_md32_common.h
aes/openssl/openssl_aesv8-armx.S
aes/openssl/openssl_aes-armv4.S
aes/openssl/openssl_arm_arch.h
crc32/Checksum.h
crc32/crc32_armv8.cpp
crc32/zlib/zconf.h
crc32/zlib/zutil.h
crc32/zlib/crc32.h
crc32/zlib/crc32.cpp
MMKVPredef.h
)

target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

set_target_properties(core PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

find_library(zlib
z
)

IF (NOT zlib)
target_compile_definitions(core PUBLIC MMKV_EMBED_ZLIB=1)
ELSE()
target_link_libraries(core ${zlib})
ENDIF()

228 changes: 228 additions & 0 deletions MMKV/Core/CodedInputData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "CodedInputData.h"
#include "PBUtility.h"
#include <stdexcept>

#ifdef MMKV_APPLE
# if __has_feature(objc_arc)
# error This file must be compiled with MRC. Use -fno-objc-arc flag.
# endif
#endif // MMKV_APPLE

using namespace std;

namespace mmkv {

CodedInputData::CodedInputData(const void *oData, size_t length)
: m_ptr((uint8_t *) oData), m_size(length), m_position(0) {
MMKV_ASSERT(m_ptr);
}

void CodedInputData::seek(size_t addedSize) {
if (m_position + addedSize > m_size) {
throw out_of_range("OutOfSpace");
}
m_position += addedSize;
}

double CodedInputData::readDouble() {
return Int64ToFloat64(this->readRawLittleEndian64());
}

float CodedInputData::readFloat() {
return Int32ToFloat32(this->readRawLittleEndian32());
}

int64_t CodedInputData::readInt64() {
int32_t shift = 0;
int64_t result = 0;
while (shift < 64) {
int8_t b = this->readRawByte();
result |= (int64_t)(b & 0x7f) << shift;
if ((b & 0x80) == 0) {
return result;
}
shift += 7;
}
throw invalid_argument("InvalidProtocolBuffer malformedInt64");
}

uint64_t CodedInputData::readUInt64() {
return static_cast<uint64_t>(readInt64());
}

int32_t CodedInputData::readInt32() {
return this->readRawVarint32();
}

uint32_t CodedInputData::readUInt32() {
return static_cast<uint32_t>(readRawVarint32());
}

bool CodedInputData::readBool() {
return this->readRawVarint32() != 0;
}

#ifndef MMKV_APPLE

string CodedInputData::readString() {
int32_t size = readRawVarint32();
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}

auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
string result((char *) (m_ptr + m_position), s_size);
m_position += s_size;
return result;
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}

string CodedInputData::readString(KeyValueHolder &kvHolder) {
kvHolder.offset = static_cast<uint32_t>(m_position);

int32_t size = this->readRawVarint32();
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}

auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
kvHolder.keySize = static_cast<uint16_t>(s_size);

auto ptr = m_ptr + m_position;
string result((char *) (m_ptr + m_position), s_size);
m_position += s_size;
return result;
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}

#endif

MMBuffer CodedInputData::readData() {
int32_t size = this->readRawVarint32();
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}

auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
MMBuffer data(((int8_t *) m_ptr) + m_position, s_size);
m_position += s_size;
return data;
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}

void CodedInputData::readData(KeyValueHolder &kvHolder) {
int32_t size = this->readRawVarint32();
if (size < 0) {
throw length_error("InvalidProtocolBuffer negativeSize");
}

auto s_size = static_cast<size_t>(size);
if (s_size <= m_size - m_position) {
kvHolder.computedKVSize = static_cast<uint16_t>(m_position - kvHolder.offset);
kvHolder.valueSize = static_cast<uint32_t>(s_size);

m_position += s_size;
} else {
throw out_of_range("InvalidProtocolBuffer truncatedMessage");
}
}

int32_t CodedInputData::readRawVarint32() {
int8_t tmp = this->readRawByte();
if (tmp >= 0) {
return tmp;
}
int32_t result = tmp & 0x7f;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 7;
} else {
result |= (tmp & 0x7f) << 7;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 14;
} else {
result |= (tmp & 0x7f) << 14;
if ((tmp = this->readRawByte()) >= 0) {
result |= tmp << 21;
} else {
result |= (tmp & 0x7f) << 21;
result |= (tmp = this->readRawByte()) << 28;
if (tmp < 0) {
// discard upper 32 bits
for (int i = 0; i < 5; i++) {
if (this->readRawByte() >= 0) {
return result;
}
}
throw invalid_argument("InvalidProtocolBuffer malformed varint32");
}
}
}
}
return result;
}

int32_t CodedInputData::readRawLittleEndian32() {
int8_t b1 = this->readRawByte();
int8_t b2 = this->readRawByte();
int8_t b3 = this->readRawByte();
int8_t b4 = this->readRawByte();
return (((int32_t) b1 & 0xff)) | (((int32_t) b2 & 0xff) << 8) | (((int32_t) b3 & 0xff) << 16) |
(((int32_t) b4 & 0xff) << 24);
}

int64_t CodedInputData::readRawLittleEndian64() {
int8_t b1 = this->readRawByte();
int8_t b2 = this->readRawByte();
int8_t b3 = this->readRawByte();
int8_t b4 = this->readRawByte();
int8_t b5 = this->readRawByte();
int8_t b6 = this->readRawByte();
int8_t b7 = this->readRawByte();
int8_t b8 = this->readRawByte();
return (((int64_t) b1 & 0xff)) | (((int64_t) b2 & 0xff) << 8) | (((int64_t) b3 & 0xff) << 16) |
(((int64_t) b4 & 0xff) << 24) | (((int64_t) b5 & 0xff) << 32) | (((int64_t) b6 & 0xff) << 40) |
(((int64_t) b7 & 0xff) << 48) | (((int64_t) b8 & 0xff) << 56);
}

int8_t CodedInputData::readRawByte() {
if (m_position == m_size) {
auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
throw out_of_range(msg);
}
auto *bytes = (int8_t *) m_ptr;
return bytes[m_position++];
}

#ifdef MMKV_APPLE
#endif // MMKV_APPLE

} // namespace mmkv
Loading

1 comment on commit df457bd

@vercel
Copy link

@vercel vercel bot commented on df457bd Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.