Skip to content

Commit

Permalink
Merge pull request #486 from Tencent/dev
Browse files Browse the repository at this point in the history
for v1.2.1
  • Loading branch information
lingol authored Jul 3, 2020
2 parents b982002 + da9543e commit d34e25a
Show file tree
Hide file tree
Showing 44 changed files with 509 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Android/MMKV/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME_PREFIX=1.2.0
VERSION_NAME_PREFIX=1.2.1
#VERSION_NAME_SUFFIX=-SNAPSHOT
VERSION_NAME_SUFFIX=
20 changes: 14 additions & 6 deletions Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,20 @@ extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) {
// get CPU status of ARMv8 extensions (CRC32, AES)
#ifdef __aarch64__
auto hwcaps = getauxval(AT_HWCAP);
# ifndef MMKV_DISABLE_CRYPT
if (hwcaps & HWCAP_AES) {
AES_set_encrypt_key = openssl_aes_armv8_set_encrypt_key;
AES_set_decrypt_key = openssl_aes_armv8_set_decrypt_key;
AES_encrypt = openssl_aes_armv8_encrypt;
AES_decrypt = openssl_aes_armv8_decrypt;
MMKVInfo("armv8 AES instructions is supported");
}
# endif // MMKV_DISABLE_CRYPT
if (hwcaps & HWCAP_CRC32) {
CRC32 = mmkv::armv8_crc32;
MMKVInfo("armv8 CRC32 instructions is supported");
}
#endif
#endif // __aarch64__

return JNI_VERSION_1_6;
}
Expand Down Expand Up @@ -253,7 +255,7 @@ static void onContentChangedByOuterProcess(const std::string &mmapID) {
}
}

MMKV_JNI jlong getMMKVWithID(JNIEnv *env, jobject, jstring mmapID, jint mode, jstring cryptKey, jstring relativePath) {
MMKV_JNI jlong getMMKVWithID(JNIEnv *env, jobject, jstring mmapID, jint mode, jstring cryptKey, jstring rootPath) {
MMKV *kv = nullptr;
if (!mmapID) {
return (jlong) kv;
Expand All @@ -264,8 +266,8 @@ MMKV_JNI jlong getMMKVWithID(JNIEnv *env, jobject, jstring mmapID, jint mode, js
if (cryptKey) {
string crypt = jstring2string(env, cryptKey);
if (crypt.length() > 0) {
if (relativePath) {
string path = jstring2string(env, relativePath);
if (rootPath) {
string path = jstring2string(env, rootPath);
kv = MMKV::mmkvWithID(str, DEFAULT_MMAP_SIZE, (MMKVMode) mode, &crypt, &path);
} else {
kv = MMKV::mmkvWithID(str, DEFAULT_MMAP_SIZE, (MMKVMode) mode, &crypt, nullptr);
Expand All @@ -274,8 +276,8 @@ MMKV_JNI jlong getMMKVWithID(JNIEnv *env, jobject, jstring mmapID, jint mode, js
}
}
if (!done) {
if (relativePath) {
string path = jstring2string(env, relativePath);
if (rootPath) {
string path = jstring2string(env, rootPath);
kv = MMKV::mmkvWithID(str, DEFAULT_MMAP_SIZE, (MMKVMode) mode, nullptr, &path);
} else {
kv = MMKV::mmkvWithID(str, DEFAULT_MMAP_SIZE, (MMKVMode) mode, nullptr, nullptr);
Expand Down Expand Up @@ -658,6 +660,8 @@ MMKV_JNI jint pageSize(JNIEnv *env, jclass type) {
return DEFAULT_MMAP_SIZE;
}

#ifndef MMKV_DISABLE_CRYPT

MMKV_JNI jstring cryptKey(JNIEnv *env, jobject instance) {
MMKV *kv = getMMKV(env, instance);
if (kv) {
Expand Down Expand Up @@ -697,6 +701,8 @@ MMKV_JNI void checkReSetCryptKey(JNIEnv *env, jobject instance, jstring cryptKey
}
}

#endif // MMKV_DISABLE_CRYPT

MMKV_JNI void trim(JNIEnv *env, jobject instance) {
MMKV *kv = getMMKV(env, instance);
if (kv) {
Expand Down Expand Up @@ -780,9 +786,11 @@ MMKV_JNI void checkContentChanged(JNIEnv *env, jobject instance) {

static JNINativeMethod g_methods[] = {
{"onExit", "()V", (void *) mmkv::onExit},
#ifndef MMKV_DISABLE_CRYPT
{"cryptKey", "()Ljava/lang/String;", (void *) mmkv::cryptKey},
{"reKey", "(Ljava/lang/String;)Z", (void *) mmkv::reKey},
{"checkReSetCryptKey", "(Ljava/lang/String;)V", (void *) mmkv::checkReSetCryptKey},
#endif
{"pageSize", "()I", (void *) mmkv::pageSize},
{"mmapID", "()Ljava/lang/String;", (void *) mmkv::mmapID},
{"lock", "()V", (void *) mmkv::lock},
Expand Down
10 changes: 5 additions & 5 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ public static MMKV mmkvWithID(String mmapID, int mode, String cryptKey) {
}

@Nullable
public static MMKV mmkvWithID(String mmapID, String relativePath) {
public static MMKV mmkvWithID(String mmapID, String rootPath) {
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, relativePath);
long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, rootPath);
if (handle == 0) {
return null;
}
Expand All @@ -190,12 +190,12 @@ public static MMKV mmkvWithID(String mmapID, String relativePath) {

// cryptKey's length <= 16
@Nullable
public static MMKV mmkvWithID(String mmapID, int mode, String cryptKey, String relativePath) {
public static MMKV mmkvWithID(String mmapID, int mode, String cryptKey, String rootPath) {
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, cryptKey, relativePath);
long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath);
if (handle == 0) {
return null;
}
Expand Down Expand Up @@ -823,7 +823,7 @@ private MMKV(long handle) {

private static native void jniInitialize(String rootDir, int level);

private native static long getMMKVWithID(String mmapID, int mode, String cryptKey, String relativePath);
private native static long getMMKVWithID(String mmapID, int mode, String cryptKey, String rootPath);

private native static long getMMKVWithIDAndSize(String mmapID, int size, int mode, String cryptKey);

Expand Down
4 changes: 2 additions & 2 deletions Android/MMKV/mmkvdemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// implementation project(':mmkv')
// implementation 'com.tencent:mmkv:1.2.0'
implementation 'com.tencent:mmkv-static:1.2.0'
// implementation 'com.tencent:mmkv:1.2.1'
implementation 'com.tencent:mmkv-static:1.2.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ public void mmkvBaselineTest() {
mmkvBatchReadString();

//mmkvBatchDeleteString();
//MMKV mmkv = mmkvForTest();
MMKV mmkv = mmkvForTest();
//mmkv.trim();
mmkv.clearMemoryCache();
mmkv.totalSize();
}

private MMKV mmkvForTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void onClick(View v) {
//testCornerSize();
//testFastRemoveCornerSize();
//testTrimNonEmptyInterProcess();
//testItemSizeHolderOverride();
}

private void testInterProcessLogic() {
Expand All @@ -133,9 +134,9 @@ private void testInterProcessLogic() {
}

@Nullable
private MMKV testMMKV(String mmapID, String cryptKey, boolean decodeOnly, String relativePath) {
private MMKV testMMKV(String mmapID, String cryptKey, boolean decodeOnly, String rootPath) {
//MMKV kv = MMKV.defaultMMKV();
MMKV kv = MMKV.mmkvWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, cryptKey, relativePath);
MMKV kv = MMKV.mmkvWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, cryptKey, rootPath);
if (kv == null) {
return null;
}
Expand Down Expand Up @@ -444,4 +445,30 @@ private void testTrimNonEmptyInterProcess() {
SystemClock.sleep(1000 * 3);
Log.i("MMKV", "NonEmptyKey: " + mmkv.decodeString("NonEmptyKey"));
}

private void testItemSizeHolderOverride() {
// final String mmapID = "testItemSizeHolderOverride_crypted";
// final String encryptKey = "encrypeKey";
final String mmapID = "testItemSizeHolderOverride_plaintext";
final String encryptKey = null;
MMKV mmkv = MMKV.mmkvWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, encryptKey);
/* do this in v1.1.2
{
// mmkv.encode("b", true);
byte[] value = new byte[512];
mmkv.encode("data", value);
Log.i("MMKV", "allKeys: " + Arrays.toString(mmkv.allKeys()));
}*/
// do this in v1.2.0
{
long totalSize = mmkv.totalSize();
long bufferSize = totalSize - 512;
byte[] value = new byte[(int) bufferSize];
// force a fullwriteback()
mmkv.encode("bigData", value);

mmkv.clearMemoryCache();
Log.i("MMKV", "allKeys: " + Arrays.toString(mmkv.allKeys()));
}
}
}
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# MMKV Change Log

## v1.2.1 / 2020-07-03
This is a hotfix release. Anyone who has upgraded to v1.2.0 should upgrade to this version **immediately**.

* Fix a potential file corruption bug when writing a file that was created in versions older than v1.2.0. This bug was introduced in v1.2.0.
* Add a preprocess directive `MMKV_DISABLE_CRYPT` to turn off MMKV encryption ability once and for all. If you integrate MMKV by source code, and if you are pretty sure encryption is not needed, you can turn that off to save some binary size.
* The parameter `relativePath` (customizing a separate folder for an MMKV instance), has been renamed to `rootPath`. Making it clear that an absolute path is expected for that parameter.

### iOS / macOS
* `-[MMKV mmkvWithID: relativePath:]` is deprecated. Use `-[MMKV mmkvWithID: rootPath:]` instead.
* Likewise, `-[MMKV mmkvWithID: cryptKey: relativePath:]` is deprecated. Use `-[MMKV mmkvWithID: cryptKey: rootPath:]` instead.

## v1.2.0 / 2020-06-30
This is the second **major version** of MMKV. Everything you call is the same as the last version, while almost everything underneath has been improved.

Expand Down
5 changes: 2 additions & 3 deletions Core/CodedInputData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ CodedInputData::CodedInputData(const void *oData, size_t length)
}

void CodedInputData::seek(size_t addedSize) {
m_position += addedSize;

if (m_position > m_size) {
if (m_position + addedSize > m_size) {
throw out_of_range("OutOfSpace");
}
m_position += addedSize;
}

double CodedInputData::readDouble() {
Expand Down
8 changes: 6 additions & 2 deletions Core/CodedInputDataCrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# endif
#endif // MMKV_APPLE

#ifndef MMKV_DISABLE_CRYPT

using namespace std;

namespace mmkv {
Expand Down Expand Up @@ -213,7 +215,7 @@ int32_t CodedInputDataCrypt::readInt32() {
return this->readRawVarint32();
}

#ifndef MMKV_APPLE
# ifndef MMKV_APPLE

string CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
kvHolder.offset = static_cast<uint32_t>(m_position);
Expand All @@ -238,7 +240,7 @@ string CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
}
}

#endif
# endif

void CodedInputDataCrypt::readData(KeyValueHolderCrypt &kvHolder) {
int32_t size = this->readRawVarint32();
Expand Down Expand Up @@ -272,3 +274,5 @@ void CodedInputDataCrypt::readData(KeyValueHolderCrypt &kvHolder) {
}

} // namespace mmkv

#endif // MMKV_DISABLE_CRYPT
11 changes: 10 additions & 1 deletion Core/CodedInputDataCrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
#include "aes/AESCrypt.h"
#include <cstdint>

#ifdef MMKV_DISABLE_CRYPT

namespace mmkv {
class CodedInputDataCrypt;
}

#else

namespace mmkv {

class CodedInputDataCrypt {
Expand Down Expand Up @@ -74,5 +82,6 @@ class CodedInputDataCrypt {

} // namespace mmkv

#endif
#endif // MMKV_DISABLE_CRYPT
#endif // __cplusplus
#endif /* CodedInputDataCrypt_h */
4 changes: 2 additions & 2 deletions Core/CodedInputDataCrypt_OSX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "CodedInputDataCrypt.h"

#ifdef MMKV_APPLE
#if defined(MMKV_APPLE) && !defined(MMKV_DISABLE_CRYPT)

# include "PBUtility.h"
# include <stdexcept>
Expand Down Expand Up @@ -59,4 +59,4 @@ NSString *CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {

} // namespace mmkv

#endif // MMKV_APPLE
#endif // MMKV_APPLE && !MMKV_DISABLE_CRYPT
10 changes: 7 additions & 3 deletions Core/KeyValueHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ MMBuffer KeyValueHolder::toMMBuffer(const void *basePtr) const {
return MMBuffer(realPtr, valueSize, MMBufferNoCopy);
}

#ifndef MMKV_DISABLE_CRYPT

KeyValueHolderCrypt::KeyValueHolderCrypt(const void *src, size_t length) {
if (length <= SmallBufferSize()) {
type = KeyValueHolderType_Direct;
Expand All @@ -63,9 +65,9 @@ KeyValueHolderCrypt::KeyValueHolderCrypt(MMBuffer &&data) {
paddedSize = static_cast<uint8_t>(data.length());
memcpy(paddedValue, data.getPtr(), data.length());
} else {
#ifdef MMKV_APPLE
# ifdef MMKV_APPLE
assert(data.m_data == nil);
#endif
# endif
type = KeyValueHolderType_Memory;
memSize = static_cast<uint32_t>(data.length());
memPtr = data.getPtr();
Expand Down Expand Up @@ -147,9 +149,11 @@ MMBuffer KeyValueHolderCrypt::toMMBuffer(const void *basePtr, const AESCrypt *cr
}
}

#endif // MMKV_DISABLE_CRYPT

} // namespace mmkv

#ifndef NDEBUG
#if !defined(MMKV_DISABLE_CRYPT) && !defined(NDEBUG)
# include "CodedInputData.h"
# include "CodedOutputData.h"
# include "MMKVLog.h"
Expand Down
4 changes: 4 additions & 0 deletions Core/KeyValueHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct KeyValueHolder {
MMBuffer toMMBuffer(const void *basePtr) const;
};

#ifndef MMKV_DISABLE_CRYPT

enum KeyValueHolderType : uint8_t {
KeyValueHolderType_Direct, // store value directly
KeyValueHolderType_Memory, // store value in the heap memory
Expand Down Expand Up @@ -104,6 +106,8 @@ struct KeyValueHolderCrypt {
#endif
};

#endif // MMKV_DISABLE_CRYPT

#pragma pack(pop)

} // namespace mmkv
Expand Down
4 changes: 4 additions & 0 deletions Core/MMBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ enum MMBufferCopyFlag : bool {

#pragma pack(push, 1)

#ifndef MMKV_DISABLE_CRYPT
struct KeyValueHolderCrypt;
#endif

class MMBuffer {
enum MMBufferType : uint8_t {
Expand Down Expand Up @@ -89,7 +91,9 @@ class MMBuffer {
explicit MMBuffer(const MMBuffer &other) = delete;
MMBuffer &operator=(const MMBuffer &other) = delete;

#ifndef MMKV_DISABLE_CRYPT
friend KeyValueHolderCrypt;
#endif
};

#pragma pack(pop)
Expand Down
Loading

0 comments on commit d34e25a

Please sign in to comment.